accountancyexport.class.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. <?php
  2. /*
  3. * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  7. * Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
  8. * Copyright (C) 2016-2019 Alexandre Spangaro <aspangaro@open-dsi.fr>
  9. * Copyright (C) 2013-2017 Olivier Geffroy <jeff@jeffinfo.com>
  10. * Copyright (C) 2017 Elarifr. Ari Elbaz <github@accedinfo.com>
  11. * Copyright (C) 2017-2019 Frédéric France <frederic.france@netlogic.fr>
  12. * Copyright (C) 2017 André Schild <a.schild@aarboard.ch>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/accountancy/class/accountancyexport.class.php
  29. * \ingroup Accountancy (Double entries)
  30. * \brief Class accountancy export
  31. */
  32. require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
  33. /**
  34. * Manage the different format accountancy export
  35. */
  36. class AccountancyExport
  37. {
  38. // Type of export. Used into $conf->global->ACCOUNTING_EXPORT_MODELCSV
  39. public static $EXPORT_TYPE_CONFIGURABLE = 1; // CSV
  40. public static $EXPORT_TYPE_AGIRIS = 10;
  41. public static $EXPORT_TYPE_EBP = 15;
  42. public static $EXPORT_TYPE_CEGID = 20;
  43. public static $EXPORT_TYPE_COGILOG = 25;
  44. public static $EXPORT_TYPE_COALA = 30;
  45. public static $EXPORT_TYPE_BOB50 = 35;
  46. public static $EXPORT_TYPE_CIEL = 40;
  47. public static $EXPORT_TYPE_SAGE50_SWISS = 45;
  48. public static $EXPORT_TYPE_CHARLEMAGNE = 50;
  49. public static $EXPORT_TYPE_QUADRATUS = 60;
  50. public static $EXPORT_TYPE_OPENCONCERTO = 100;
  51. public static $EXPORT_TYPE_LDCOMPTA = 110;
  52. public static $EXPORT_TYPE_FEC = 1000;
  53. /**
  54. * @var string[] Error codes (or messages)
  55. */
  56. public $errors = array();
  57. /**
  58. *
  59. * @var string Separator
  60. */
  61. public $separator = '';
  62. /**
  63. *
  64. * @var string End of line
  65. */
  66. public $end_line = '';
  67. /**
  68. * Constructor
  69. *
  70. * @param DoliDb $db Database handler
  71. */
  72. public function __construct(DoliDB &$db)
  73. {
  74. global $conf;
  75. $this->db = &$db;
  76. $this->separator = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  77. $this->end_line = empty($conf->global->ACCOUNTING_EXPORT_ENDLINE)?"\n":($conf->global->ACCOUNTING_EXPORT_ENDLINE==1?"\n":"\r\n");
  78. }
  79. /**
  80. * Array with all export type available (key + label)
  81. *
  82. * @return array of type
  83. */
  84. public static function getType()
  85. {
  86. global $langs;
  87. $listofexporttypes = array(
  88. self::$EXPORT_TYPE_CONFIGURABLE => $langs->trans('Modelcsv_configurable'),
  89. self::$EXPORT_TYPE_CEGID => $langs->trans('Modelcsv_CEGID'),
  90. self::$EXPORT_TYPE_COALA => $langs->trans('Modelcsv_COALA'),
  91. self::$EXPORT_TYPE_BOB50 => $langs->trans('Modelcsv_bob50'),
  92. self::$EXPORT_TYPE_CIEL => $langs->trans('Modelcsv_ciel'),
  93. self::$EXPORT_TYPE_QUADRATUS => $langs->trans('Modelcsv_quadratus'),
  94. self::$EXPORT_TYPE_EBP => $langs->trans('Modelcsv_ebp'),
  95. self::$EXPORT_TYPE_COGILOG => $langs->trans('Modelcsv_cogilog'),
  96. self::$EXPORT_TYPE_AGIRIS => $langs->trans('Modelcsv_agiris'),
  97. self::$EXPORT_TYPE_OPENCONCERTO => $langs->trans('Modelcsv_openconcerto'),
  98. self::$EXPORT_TYPE_SAGE50_SWISS => $langs->trans('Modelcsv_Sage50_Swiss'),
  99. self::$EXPORT_TYPE_LDCOMPTA => $langs->trans('Modelcsv_LDCompta'),
  100. self::$EXPORT_TYPE_FEC => $langs->trans('Modelcsv_FEC'),
  101. self::$EXPORT_TYPE_CHARLEMAGNE => $langs->trans('Modelcsv_charlemagne'),
  102. );
  103. ksort($listofexporttypes, SORT_NUMERIC);
  104. return $listofexporttypes;
  105. }
  106. /**
  107. * Return string to summarize the format (Used to generated export filename)
  108. *
  109. * @param int $type Format id
  110. * @return string Format code
  111. */
  112. private static function getFormatCode($type)
  113. {
  114. $formatcode = array (
  115. self::$EXPORT_TYPE_CONFIGURABLE => 'csv',
  116. self::$EXPORT_TYPE_CEGID => 'cegid',
  117. self::$EXPORT_TYPE_COALA => 'coala',
  118. self::$EXPORT_TYPE_BOB50 => 'bob50',
  119. self::$EXPORT_TYPE_CIEL => 'ciel',
  120. self::$EXPORT_TYPE_QUADRATUS => 'quadratus',
  121. self::$EXPORT_TYPE_EBP => 'ebp',
  122. self::$EXPORT_TYPE_COGILOG => 'cogilog',
  123. self::$EXPORT_TYPE_AGIRIS => 'agiris',
  124. self::$EXPORT_TYPE_OPENCONCERTO => 'openconcerto',
  125. self::$EXPORT_TYPE_SAGE50_SWISS => 'sage50ch',
  126. self::$EXPORT_TYPE_LDCOMPTA => 'ldcompta',
  127. self::$EXPORT_TYPE_FEC => 'fec',
  128. );
  129. return $formatcode[$type];
  130. }
  131. /**
  132. * Array with all export type available (key + label) and parameters for config
  133. *
  134. * @return array of type
  135. */
  136. public static function getTypeConfig()
  137. {
  138. global $conf, $langs;
  139. return array (
  140. 'param' => array(
  141. self::$EXPORT_TYPE_CONFIGURABLE => array(
  142. 'label' => $langs->trans('Modelcsv_configurable'),
  143. 'ACCOUNTING_EXPORT_FORMAT' => empty($conf->global->ACCOUNTING_EXPORT_FORMAT)?'txt':$conf->global->ACCOUNTING_EXPORT_FORMAT,
  144. 'ACCOUNTING_EXPORT_SEPARATORCSV' => empty($conf->global->ACCOUNTING_EXPORT_SEPARATORCSV)?',':$conf->global->ACCOUNTING_EXPORT_SEPARATORCSV,
  145. 'ACCOUNTING_EXPORT_ENDLINE' => empty($conf->global->ACCOUNTING_EXPORT_ENDLINE)?1:$conf->global->ACCOUNTING_EXPORT_ENDLINE,
  146. 'ACCOUNTING_EXPORT_DATE' => empty($conf->global->ACCOUNTING_EXPORT_DATE)?'%d%m%Y':$conf->global->ACCOUNTING_EXPORT_DATE,
  147. ),
  148. self::$EXPORT_TYPE_CEGID => array(
  149. 'label' => $langs->trans('Modelcsv_CEGID'),
  150. ),
  151. self::$EXPORT_TYPE_COALA => array(
  152. 'label' => $langs->trans('Modelcsv_COALA'),
  153. ),
  154. self::$EXPORT_TYPE_BOB50 => array(
  155. 'label' => $langs->trans('Modelcsv_bob50'),
  156. ),
  157. self::$EXPORT_TYPE_CIEL => array(
  158. 'label' => $langs->trans('Modelcsv_ciel'),
  159. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  160. ),
  161. self::$EXPORT_TYPE_QUADRATUS => array(
  162. 'label' => $langs->trans('Modelcsv_quadratus'),
  163. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  164. ),
  165. self::$EXPORT_TYPE_EBP => array(
  166. 'label' => $langs->trans('Modelcsv_ebp'),
  167. ),
  168. self::$EXPORT_TYPE_COGILOG => array(
  169. 'label' => $langs->trans('Modelcsv_cogilog'),
  170. ),
  171. self::$EXPORT_TYPE_AGIRIS => array(
  172. 'label' => $langs->trans('Modelcsv_agiris'),
  173. ),
  174. self::$EXPORT_TYPE_OPENCONCERTO => array(
  175. 'label' => $langs->trans('Modelcsv_openconcerto'),
  176. 'ACCOUNTING_EXPORT_FORMAT' => 'csv',
  177. ),
  178. self::$EXPORT_TYPE_SAGE50_SWISS => array(
  179. 'label' => $langs->trans('Modelcsv_Sage50_Swiss'),
  180. 'ACCOUNTING_EXPORT_FORMAT' => 'csv',
  181. ),
  182. self::$EXPORT_TYPE_LDCOMPTA => array(
  183. 'label' => $langs->trans('Modelcsv_LDCompta'),
  184. 'ACCOUNTING_EXPORT_FORMAT' => 'csv',
  185. ),
  186. self::$EXPORT_TYPE_FEC => array(
  187. 'label' => $langs->trans('Modelcsv_FEC'),
  188. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  189. ),
  190. self::$EXPORT_TYPE_CHARLEMAGNE => array(
  191. 'label' => $langs->trans('Modelcsv_charlemagne'),
  192. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  193. ),
  194. ),
  195. 'cr'=> array (
  196. '1' => $langs->trans("Unix"),
  197. '2' => $langs->trans("Windows")
  198. ),
  199. 'format' => array (
  200. 'csv' => $langs->trans("csv"),
  201. 'txt' => $langs->trans("txt")
  202. ),
  203. );
  204. }
  205. /**
  206. * Function who chose which export to use with the default config, and make the export into a file
  207. *
  208. * @param array $TData data
  209. * @return void
  210. */
  211. public function export(&$TData)
  212. {
  213. global $conf, $langs;
  214. global $search_date_end; // Used into /accountancy/tpl/export_journal.tpl.php
  215. // Define name of file to save
  216. $filename = 'general_ledger-'.$this->getFormatCode($conf->global->ACCOUNTING_EXPORT_MODELCSV);
  217. $type_export = 'general_ledger';
  218. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  219. switch ($conf->global->ACCOUNTING_EXPORT_MODELCSV) {
  220. case self::$EXPORT_TYPE_CONFIGURABLE :
  221. $this->exportConfigurable($TData);
  222. break;
  223. case self::$EXPORT_TYPE_CEGID :
  224. $this->exportCegid($TData);
  225. break;
  226. case self::$EXPORT_TYPE_COALA :
  227. $this->exportCoala($TData);
  228. break;
  229. case self::$EXPORT_TYPE_BOB50 :
  230. $this->exportBob50($TData);
  231. break;
  232. case self::$EXPORT_TYPE_CIEL :
  233. $this->exportCiel($TData);
  234. break;
  235. case self::$EXPORT_TYPE_QUADRATUS :
  236. $this->exportQuadratus($TData);
  237. break;
  238. case self::$EXPORT_TYPE_EBP :
  239. $this->exportEbp($TData);
  240. break;
  241. case self::$EXPORT_TYPE_COGILOG :
  242. $this->exportCogilog($TData);
  243. break;
  244. case self::$EXPORT_TYPE_AGIRIS :
  245. $this->exportAgiris($TData);
  246. break;
  247. case self::$EXPORT_TYPE_OPENCONCERTO :
  248. $this->exportOpenConcerto($TData);
  249. break;
  250. case self::$EXPORT_TYPE_SAGE50_SWISS :
  251. $this->exportSAGE50SWISS($TData);
  252. break;
  253. case self::$EXPORT_TYPE_LDCOMPTA :
  254. $this->exportLDCompta($TData);
  255. break;
  256. case self::$EXPORT_TYPE_FEC :
  257. $this->exportFEC($TData);
  258. break;
  259. case self::$EXPORT_TYPE_CHARLEMAGNE :
  260. $this->exportCharlemagne($TData);
  261. break;
  262. default:
  263. $this->errors[] = $langs->trans('accountancy_error_modelnotfound');
  264. break;
  265. }
  266. }
  267. /**
  268. * Export format : CEGID
  269. *
  270. * @param array $objectLines data
  271. * @return void
  272. */
  273. public function exportCegid($objectLines)
  274. {
  275. foreach ($objectLines as $line) {
  276. $date = dol_print_date($line->doc_date, '%d%m%Y');
  277. $separator = ";";
  278. $end_line = "\n";
  279. print $date . $separator;
  280. print $line->code_journal . $separator;
  281. print length_accountg($line->numero_compte) . $separator;
  282. print length_accounta($line->subledger_account) . $separator;
  283. print $line->sens . $separator;
  284. print price($line->montant) . $separator;
  285. print $line->label_operation . $separator;
  286. print $line->doc_ref;
  287. print $end_line;
  288. }
  289. }
  290. /**
  291. * Export format : COGILOG
  292. *
  293. * @param array $objectLines data
  294. * @return void
  295. */
  296. public function exportCogilog($objectLines)
  297. {
  298. foreach ($objectLines as $line) {
  299. $date = dol_print_date($line->doc_date, '%d%m%Y');
  300. $separator = ";";
  301. $end_line = "\n";
  302. print $line->code_journal . $separator;
  303. print $date . $separator;
  304. print $line->piece_num . $separator;
  305. print length_accountg($line->numero_compte) . $separator;
  306. print '' . $separator;
  307. print $line->label_operation . $separator;
  308. print $date . $separator;
  309. if ($line->sens=='D') {
  310. print price($line->montant) . $separator;
  311. print '' . $separator;
  312. }elseif ($line->sens=='C') {
  313. print '' . $separator;
  314. print price($line->montant) . $separator;
  315. }
  316. print $line->doc_ref . $separator;
  317. print $line->label_operation . $separator;
  318. print $end_line;
  319. }
  320. }
  321. /**
  322. * Export format : COALA
  323. *
  324. * @param array $objectLines data
  325. * @return void
  326. */
  327. public function exportCoala($objectLines)
  328. {
  329. // Coala export
  330. $separator = ";";
  331. $end_line = "\n";
  332. foreach ($objectLines as $line) {
  333. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  334. print $date . $separator;
  335. print $line->code_journal . $separator;
  336. print length_accountg($line->numero_compte) . $separator;
  337. print $line->piece_num . $separator;
  338. print $line->doc_ref . $separator;
  339. print price($line->debit) . $separator;
  340. print price($line->credit) . $separator;
  341. print 'E' . $separator;
  342. print length_accountg($line->subledger_account) . $separator;
  343. print $end_line;
  344. }
  345. }
  346. /**
  347. * Export format : BOB50
  348. *
  349. * @param array $objectLines data
  350. * @return void
  351. */
  352. public function exportBob50($objectLines)
  353. {
  354. // Bob50
  355. $separator = ";";
  356. $end_line = "\n";
  357. foreach ($objectLines as $line) {
  358. print $line->piece_num . $separator;
  359. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  360. print $date . $separator;
  361. if (empty($line->subledger_account)) {
  362. print 'G' . $separator;
  363. print length_accounta($line->numero_compte) . $separator;
  364. } else {
  365. if (substr($line->numero_compte, 0, 3) == '411') {
  366. print 'C' . $separator;
  367. }
  368. if (substr($line->numero_compte, 0, 3) == '401') {
  369. print 'F' . $separator;
  370. }
  371. print length_accountg($line->subledger_account) . $separator;
  372. }
  373. print price($line->debit) . $separator;
  374. print price($line->credit) . $separator;
  375. print dol_trunc($line->label_operation, 32) . $separator;
  376. print $end_line;
  377. }
  378. }
  379. /**
  380. * Export format : CIEL
  381. *
  382. * @param array $TData data
  383. * @return void
  384. */
  385. public function exportCiel(&$TData)
  386. {
  387. global $conf;
  388. $end_line ="\r\n";
  389. $i = 1;
  390. $date_ecriture = dol_print_date(dol_now(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be yyyymmdd
  391. foreach ($TData as $data) {
  392. $code_compta = $data->numero_compte;
  393. if (! empty($data->subledger_account))
  394. $code_compta = $data->subledger_account;
  395. $Tab = array ();
  396. $Tab['num_ecriture'] = str_pad($i, 5);
  397. $Tab['code_journal'] = str_pad($data->code_journal, 2);
  398. $Tab['date_ecriture'] = $date_ecriture;
  399. $Tab['date_ope'] = dol_print_date($data->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
  400. $Tab['num_piece'] = str_pad(self::trunc($data->piece_num, 12), 12);
  401. $Tab['num_compte'] = str_pad(self::trunc($code_compta, 11), 11);
  402. $Tab['libelle_ecriture'] = str_pad(self::trunc(dol_string_unaccent($data->doc_ref) . dol_string_unaccent($data->label_operation), 25), 25);
  403. $Tab['montant'] = str_pad(abs($data->montant), 13, ' ', STR_PAD_LEFT);
  404. $Tab['type_montant'] = str_pad($data->sens, 1);
  405. $Tab['vide'] = str_repeat(' ', 18);
  406. $Tab['intitule_compte'] = str_pad(self::trunc(dol_string_unaccent($data->label_operation), 34), 34);
  407. $Tab['end'] = 'O2003';
  408. $Tab['end_line'] = $end_line;
  409. print implode($Tab);
  410. $i ++;
  411. }
  412. }
  413. /**
  414. * Export format : Quadratus
  415. *
  416. * @param array $TData data
  417. * @return void
  418. */
  419. public function exportQuadratus(&$TData)
  420. {
  421. global $conf;
  422. $end_line ="\r\n";
  423. //We should use dol_now function not time however this is wrong date to transfert in accounting
  424. //$date_ecriture = dol_print_date(dol_now(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
  425. //$date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
  426. foreach ($TData as $data) {
  427. $code_compta = $data->numero_compte;
  428. if (! empty($data->subledger_account))
  429. $code_compta = $data->subledger_account;
  430. $Tab = array ();
  431. $Tab['type_ligne'] = 'M';
  432. $Tab['num_compte'] = str_pad(self::trunc($code_compta, 8), 8);
  433. $Tab['code_journal'] = str_pad(self::trunc($data->code_journal, 2), 2);
  434. $Tab['folio'] = '000';
  435. //We use invoice date $data->doc_date not $date_ecriture which is the transfert date
  436. //maybe we should set an option for customer who prefer to keep in accounting software the tranfert date instead of invoice date ?
  437. //$Tab['date_ecriture'] = $date_ecriture;
  438. $Tab['date_ecriture'] = dol_print_date($data->doc_date, '%d%m%y');
  439. $Tab['filler'] = ' ';
  440. $Tab['libelle_ecriture'] = str_pad(self::trunc(dol_string_unaccent($data->doc_ref) . ' ' . dol_string_unaccent($data->label_operation), 20), 20);
  441. $Tab['sens'] = $data->sens; // C or D
  442. $Tab['signe_montant'] = '+';
  443. //elarifr le montant doit etre en centimes sans point decimal !
  444. $Tab['montant'] = str_pad(abs($data->montant*100), 12, '0', STR_PAD_LEFT); // TODO manage negative amount
  445. // $Tab['montant'] = str_pad(abs($data->montant), 12, '0', STR_PAD_LEFT); // TODO manage negative amount
  446. $Tab['contrepartie'] = str_repeat(' ', 8);
  447. // elarifr: date format must be fixed format : 6 char ddmmyy = %d%m%yand not defined by user / dolibarr setting
  448. if (! empty($data->date_echeance))
  449. //$Tab['date_echeance'] = dol_print_date($data->date_echeance, $conf->global->ACCOUNTING_EXPORT_DATE);
  450. $Tab['date_echeance'] = dol_print_date($data->date_echeance, '%d%m%y'); // elarifr: format must be ddmmyy
  451. else
  452. $Tab['date_echeance'] = '000000';
  453. //elarifr please keep quadra named field lettrage(2) + codestat(3) instead of fake lettrage(5)
  454. //$Tab['lettrage'] = str_repeat(' ', 5);
  455. $Tab['lettrage'] = str_repeat(' ', 2);
  456. $Tab['codestat'] = str_repeat(' ', 3);
  457. $Tab['num_piece'] = str_pad(self::trunc($data->piece_num, 5), 5);
  458. //elarifr keep correct quadra named field instead of anon filler
  459. //$Tab['filler2'] = str_repeat(' ', 20);
  460. $Tab['affaire'] = str_repeat(' ', 10);
  461. $Tab['quantity1'] = str_repeat(' ', 10);
  462. $Tab['num_piece2'] = str_pad(self::trunc($data->piece_num, 8), 8);
  463. $Tab['devis'] = str_pad($conf->currency, 3);
  464. $Tab['code_journal2'] = str_pad(self::trunc($data->code_journal, 3), 3);
  465. $Tab['filler3'] = str_repeat(' ', 3);
  466. //elarifr keep correct quadra named field instead of anon filler libelle_ecriture2 is 30 char not 32 !!!!
  467. //as we use utf8, we must remove accent to have only one ascii char instead of utf8 2 chars for specials that report wrong line size that will exceed import format spec
  468. //todo we should filter more than only accent to avoid wrong line size
  469. //TODO: remove invoice number doc_ref in libelle,
  470. //TODO: we should offer an option for customer to build the libelle using invoice number / name / date in accounting software
  471. //$Tab['libelle_ecriture2'] = str_pad(self::trunc(dol_string_unaccent($data->doc_ref) . ' ' . dol_string_unaccent($data->label_operation), 30), 30);
  472. $Tab['libelle_ecriture2'] = str_pad(self::trunc(dol_string_unaccent($data->label_operation), 30), 30);
  473. $Tab['codetva'] = str_repeat(' ', 2);
  474. //elarifr we need to keep the 10 lastest number of invoice doc_ref not the beginning part that is the unusefull almost same part
  475. //$Tab['num_piece3'] = str_pad(self::trunc($data->piece_num, 10), 10);
  476. $Tab['num_piece3'] = substr(self::trunc($data->doc_ref, 20), -10);
  477. $Tab['filler4'] = str_repeat(' ', 73);
  478. $Tab['end_line'] = $end_line;
  479. print implode($Tab);
  480. }
  481. }
  482. /**
  483. * Export format : EBP
  484. *
  485. * @param array $objectLines data
  486. * @return void
  487. */
  488. public function exportEbp($objectLines)
  489. {
  490. $separator = ',';
  491. $end_line = "\n";
  492. foreach ($objectLines as $line) {
  493. $date = dol_print_date($line->doc_date, '%d%m%Y');
  494. print $line->id . $separator;
  495. print $date . $separator;
  496. print $line->code_journal . $separator;
  497. if (empty($line->subledger_account)) {
  498. print $line->numero_compte . $separator;
  499. } else {
  500. print $line->subledger_account . $separator;
  501. }
  502. //print substr(length_accountg($line->numero_compte), 0, 2) . $separator;
  503. print '"'.dol_trunc($line->label_operation, 40, 'right', 'UTF-8', 1).'"' . $separator;
  504. print '"'.dol_trunc($line->piece_num, 15, 'right', 'UTF-8', 1).'"'.$separator;
  505. print price2num(abs($line->montant)).$separator;
  506. print $line->sens.$separator;
  507. print $date . $separator;
  508. //print 'EUR';
  509. print $end_line;
  510. }
  511. }
  512. /**
  513. * Export format : Agiris Isacompta
  514. *
  515. * @param array $objectLines data
  516. * @return void
  517. */
  518. public function exportAgiris($objectLines)
  519. {
  520. $separator = ';';
  521. $end_line = "\n";
  522. foreach ($objectLines as $line) {
  523. $date = dol_print_date($line->doc_date, '%d%m%Y');
  524. print $line->piece_num . $separator;
  525. print self::toAnsi($line->label_operation) . $separator;
  526. print $date . $separator;
  527. print self::toAnsi($line->label_operation) . $separator;
  528. if (empty($line->subledger_account)) {
  529. print length_accountg($line->numero_compte) . $separator;
  530. print self::toAnsi($line->label_compte) . $separator;
  531. } else {
  532. print length_accounta($line->subledger_account) . $separator;
  533. print self::toAnsi($line->subledger_label) . $separator;
  534. }
  535. print self::toAnsi($line->doc_ref) . $separator;
  536. print price($line->debit) . $separator;
  537. print price($line->credit) . $separator;
  538. print price($line->montant) . $separator;
  539. print $line->sens . $separator;
  540. print $line->lettering_code . $separator;
  541. print $line->code_journal;
  542. print $end_line;
  543. }
  544. }
  545. /**
  546. * Export format : OpenConcerto
  547. *
  548. * @param array $objectLines data
  549. * @return void
  550. */
  551. public function exportOpenConcerto($objectLines)
  552. {
  553. $separator = ';';
  554. $end_line = "\n";
  555. foreach ($objectLines as $line) {
  556. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  557. print $date . $separator;
  558. print $line->code_journal . $separator;
  559. if (empty($line->subledger_account)) {
  560. print length_accountg($line->numero_compte) . $separator;
  561. } else {
  562. print length_accounta($line->subledger_account) . $separator;
  563. }
  564. print $line->doc_ref . $separator;
  565. print $line->label_operation . $separator;
  566. print price($line->debit) . $separator;
  567. print price($line->credit) . $separator;
  568. print $end_line;
  569. }
  570. }
  571. /**
  572. * Export format : Configurable CSV
  573. *
  574. * @param array $objectLines data
  575. * @return void
  576. */
  577. public function exportConfigurable($objectLines)
  578. {
  579. global $conf;
  580. $separator = $this->separator;
  581. foreach ($objectLines as $line) {
  582. $tab = array();
  583. // export configurable
  584. $date = dol_print_date($line->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
  585. $tab[] = $line->piece_num;
  586. $tab[] = $date;
  587. $tab[] = $line->doc_ref;
  588. $tab[] = preg_match('/'.$separator.'/', $line->label_operation) ? "'".$line->label_operation."'" : $line->label_operation;
  589. $tab[] = length_accountg($line->numero_compte);
  590. $tab[] = length_accounta($line->subledger_account);
  591. $tab[] = price2num($line->debit);
  592. $tab[] = price2num($line->credit);
  593. $tab[] = price2num($line->montant);
  594. $tab[] = $line->code_journal;
  595. print implode($separator, $tab) . $this->end_line;
  596. }
  597. }
  598. /**
  599. * Export format : FEC
  600. *
  601. * @param array $objectLines data
  602. * @return void
  603. */
  604. public function exportFEC($objectLines)
  605. {
  606. $separator = "\t";
  607. $end_line = "\n";
  608. print "JournalCode" . $separator;
  609. print "JournalLib" . $separator;
  610. print "EcritureNum" . $separator;
  611. print "EcritureDate" . $separator;
  612. print "CompteNum" . $separator;
  613. print "CompteLib" . $separator;
  614. print "CompAuxNum" . $separator;
  615. print "CompAuxLib" . $separator;
  616. print "PieceRef" . $separator;
  617. print "PieceDate" . $separator;
  618. print "EcritureLib" . $separator;
  619. print "Debit" . $separator;
  620. print "Credit" . $separator;
  621. print "EcritureLet" . $separator;
  622. print "DateLet" . $separator;
  623. print "ValidDate" . $separator;
  624. print "Montantdevise" . $separator;
  625. print "Idevise";
  626. print $end_line;
  627. foreach ($objectLines as $line) {
  628. $date_creation = dol_print_date($line->date_creation, '%d%m%Y');
  629. $date_doc = dol_print_date($line->doc_date, '%d%m%Y');
  630. $date_valid = dol_print_date($line->date_validated, '%d%m%Y');
  631. // FEC:JournalCode
  632. print $line->code_journal . $separator;
  633. // FEC:JournalLib
  634. print $line->journal_label . $separator;
  635. // FEC:EcritureNum
  636. print $line->piece_num . $separator;
  637. // FEC:EcritureDate
  638. print $date_creation . $separator;
  639. // FEC:CompteNum
  640. print $line->numero_compte . $separator;
  641. // FEC:CompteLib
  642. print $line->label_compte . $separator;
  643. // FEC:CompAuxNum
  644. print $line->subledger_account . $separator;
  645. // FEC:CompAuxLib
  646. print $line->subledger_label . $separator;
  647. // FEC:PieceRef
  648. print $line->doc_ref . $separator;
  649. // FEC:PieceDate
  650. print $date_doc . $separator;
  651. // FEC:EcritureLib
  652. print $line->label_operation . $separator;
  653. // FEC:Debit
  654. print price2num($line->debit) . $separator;
  655. // FEC:Credit
  656. print price2num($line->credit) . $separator;
  657. // FEC:EcritureLet
  658. print $line->lettering_code . $separator;
  659. // FEC:DateLet
  660. print $line->date_lettering . $separator;
  661. // FEC:ValidDate
  662. print $date_valid . $separator;
  663. // FEC:Montantdevise
  664. print $line->multicurrency_amount . $separator;
  665. // FEC:Idevise
  666. print $line->multicurrency_code;
  667. print $end_line;
  668. }
  669. }
  670. /**
  671. * Export format : SAGE50SWISS
  672. *
  673. * https://onlinehelp.sageschweiz.ch/default.aspx?tabid=19984
  674. * http://media.topal.ch/Public/Schnittstellen/TAF/Specification/Sage50-TAF-format.pdf
  675. *
  676. * @param array $objectLines data
  677. *
  678. * @return void
  679. */
  680. public function exportSAGE50SWISS($objectLines)
  681. {
  682. // SAGE50SWISS
  683. $this->separator = ',';
  684. $this->end_line = "\r\n";
  685. // Print header line
  686. print "Blg,Datum,Kto,S/H,Grp,GKto,SId,SIdx,KIdx,BTyp,MTyp,Code,Netto,Steuer,FW-Betrag,Tx1,Tx2,PkKey,OpId,Flag";
  687. print $this->end_line;
  688. $thisPieceNum= "";
  689. $thisPieceAccountNr= "";
  690. $aSize= count($objectLines);
  691. foreach ($objectLines as $aIndex=>$line)
  692. {
  693. $sammelBuchung= false;
  694. if ($aIndex-2 >= 0 && $objectLines[$aIndex-2]->piece_num == $line->piece_num)
  695. {
  696. $sammelBuchung= true;
  697. }
  698. elseif ($aIndex+2 < $aSize && $objectLines[$aIndex+2]->piece_num == $line->piece_num)
  699. {
  700. $sammelBuchung= true;
  701. }
  702. elseif ($aIndex+1 < $aSize
  703. && $objectLines[$aIndex+1]->piece_num == $line->piece_num
  704. && $aIndex-1 < $aSize
  705. && $objectLines[$aIndex-1]->piece_num == $line->piece_num
  706. )
  707. {
  708. $sammelBuchung= true;
  709. }
  710. //Blg
  711. print $line->piece_num . $this->separator;
  712. // Datum
  713. $date = dol_print_date($line->doc_date, '%d.%m.%Y');
  714. print $date . $this->separator;
  715. // Kto
  716. print length_accountg($line->numero_compte) . $this->separator;
  717. // S/H
  718. if ($line->sens == 'D')
  719. {
  720. print 'S' . $this->separator;
  721. }
  722. else
  723. {
  724. print 'H' . $this->separator;
  725. }
  726. //Grp
  727. print self::trunc($line->code_journal, 1) . $this->separator;
  728. // GKto
  729. if (empty($line->code_tiers))
  730. {
  731. if ($line->piece_num == $thisPieceNum)
  732. {
  733. print length_accounta($thisPieceAccountNr) . $this->separator;
  734. }
  735. else
  736. {
  737. print "div" . $this->separator;
  738. }
  739. }
  740. else
  741. {
  742. print length_accounta($line->code_tiers) . $this->separator;
  743. }
  744. //SId
  745. print $this->separator;
  746. //SIdx
  747. print "0" . $this->separator;
  748. //KIdx
  749. print "0" . $this->separator;
  750. //BTyp
  751. print "0" . $this->separator;
  752. //MTyp 1=Fibu Einzelbuchung 2=Sammebuchung
  753. if ($sammelBuchung)
  754. {
  755. print "2" . $this->separator;
  756. }
  757. else
  758. {
  759. print "1" . $this->separator;
  760. }
  761. // Code
  762. print '""' . $this->separator;
  763. // Netto
  764. if ($line->montant >= 0)
  765. {
  766. print $line->montant . $this->separator;
  767. }
  768. else
  769. {
  770. print $line->montant*-1 . $this->separator;
  771. }
  772. // Steuer
  773. print "0.00" . $this->separator;
  774. // FW-Betrag
  775. print "0.00" . $this->separator;
  776. // Tx1
  777. $line1= self::toAnsi($line->label_compte, 29);
  778. if ($line1 == "LIQ" || $line1 == "LIQ Beleg ok" || strlen($line1) <= 3)
  779. {
  780. $line1= "";
  781. }
  782. $line2= self::toAnsi($line->doc_ref, 29);
  783. if (strlen($line1) == 0)
  784. {
  785. $line1= $line2;
  786. $line2= "";
  787. }
  788. if (strlen($line1) > 0 && strlen($line2) > 0 && (strlen($line1) + strlen($line2)) < 27)
  789. {
  790. $line1= $line1 . ' / ' . $line2;
  791. $line2= "";
  792. }
  793. print '"' . self::toAnsi($line1). '"' . $this->separator;
  794. // Tx2
  795. print '"' . self::toAnsi($line2). '"' . $this->separator;
  796. //PkKey
  797. print "0" . $this->separator;
  798. //OpId
  799. print $this->separator;
  800. // Flag
  801. print "0";
  802. print $this->end_line;
  803. if ($line->piece_num !== $thisPieceNum)
  804. {
  805. $thisPieceNum= $line->piece_num;
  806. $thisPieceAccountNr= $line->numero_compte;
  807. }
  808. }
  809. }
  810. /**
  811. * Export format : LD Compta version 9 & higher
  812. * http://www.ldsysteme.fr/fileadmin/telechargement/np/ldcompta/Documentation/IntCptW10.pdf
  813. *
  814. * @param array $objectLines data
  815. *
  816. * @return void
  817. */
  818. public function exportLDCompta($objectLines)
  819. {
  820. $separator = ';';
  821. $end_line = "\n";
  822. foreach ($objectLines as $line) {
  823. $date_document = dol_print_date($line->doc_date, '%Y%m%d');
  824. $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
  825. // TYPE
  826. $type_enregistrement = 'E'; // For write movement
  827. print $type_enregistrement . $separator;
  828. // JNAL
  829. print substr($line->code_journal, 0, 2) . $separator;
  830. // NECR
  831. print $line->id . $separator;
  832. // NPIE
  833. print $line->piece_num . $separator;
  834. // DATP
  835. print $date_document . $separator;
  836. // LIBE
  837. print $line->label_operation . $separator;
  838. // DATH
  839. print $line->date_lim_reglement . $separator;
  840. // CNPI
  841. if ($line->doc_type == 'supplier_invoice') {
  842. if ($line->montant < 0) {
  843. $nature_piece = 'AF';
  844. } else {
  845. $nature_piece = 'FF';
  846. }
  847. } elseif ($line->doc_type == 'customer_invoice') {
  848. if ($line->montant < 0) {
  849. $nature_piece = 'AC';
  850. } else {
  851. $nature_piece = 'FC';
  852. }
  853. } else {
  854. $nature_piece = '';
  855. }
  856. print $nature_piece . $separator;
  857. // RACI
  858. /*
  859. if (! empty($line->subledger_account)) {
  860. if ($line->doc_type == 'supplier_invoice') {
  861. $racine_subledger_account = '40';
  862. } elseif ($line->doc_type == 'customer_invoice') {
  863. $racine_subledger_account = '41';
  864. } else {
  865. $nature_piece = '';
  866. }
  867. print $racine_subledger_account . $separator;
  868. } else {
  869. print $separator;
  870. }
  871. */
  872. // MONT
  873. print price(abs($line->montant)) . $separator;
  874. // CODC
  875. print $line->sens . $separator;
  876. // CPTG
  877. print length_accountg($line->numero_compte) . $separator;
  878. // DATE
  879. print $date_creation . $separator;
  880. // CLET
  881. print $line->lettering_code . $separator;
  882. // DATL
  883. print $line->date_lettering . $separator;
  884. // CPTA
  885. if (! empty($line->subledger_account)) {
  886. print length_accounta($line->subledger_account) . $separator;
  887. }
  888. // CNAT
  889. if ($line->doc_type == 'supplier_invoice' && ! empty($line->subledger_account)) {
  890. print 'F';
  891. } elseif ($line->doc_type == 'customer_invoice' && ! empty($line->subledger_account)) {
  892. print 'C';
  893. } else {
  894. print '';
  895. }
  896. print $end_line;
  897. }
  898. }
  899. /**
  900. * Export format : Charlemagne
  901. *
  902. * @param array $objectLines data
  903. * @return void
  904. */
  905. public function exportCharlemagne($objectLines)
  906. {
  907. global $langs;
  908. $langs->load('compta');
  909. $separator = "\t";
  910. $end_line = "\n";
  911. /*
  912. * Charlemagne export need header
  913. */
  914. print $langs->transnoentitiesnoconv('Date') . $separator;
  915. print self::trunc($langs->transnoentitiesnoconv('Journal'), 6) . $separator;
  916. print self::trunc($langs->transnoentitiesnoconv('Account'), 15) . $separator;
  917. print self::trunc($langs->transnoentitiesnoconv('LabelAccount'), 60) . $separator;
  918. print self::trunc($langs->transnoentitiesnoconv('Piece'), 20) . $separator;
  919. print self::trunc($langs->transnoentitiesnoconv('LabelOperation'), 60) . $separator;
  920. print $langs->transnoentitiesnoconv('Amount') . $separator;
  921. print 'S' . $separator;
  922. print self::trunc($langs->transnoentitiesnoconv('Analytic') . ' 1', 15) . $separator;
  923. print self::trunc($langs->transnoentitiesnoconv('AnalyticLabel') . ' 1', 60) . $separator;
  924. print self::trunc($langs->transnoentitiesnoconv('Analytic') . ' 2', 15) . $separator;
  925. print self::trunc($langs->transnoentitiesnoconv('AnalyticLabel') . ' 2', 60) . $separator;
  926. print self::trunc($langs->transnoentitiesnoconv('Analytic') . ' 3', 15) . $separator;
  927. print self::trunc($langs->transnoentitiesnoconv('AnalyticLabel') . ' 3', 60) . $separator;
  928. print $end_line;
  929. foreach($objectLines as $line) {
  930. $date = dol_print_date($line->doc_date, '%Y%m%d');
  931. print $date . $separator; //Date
  932. print self::trunc($line->code_journal, 6) . $separator; //Journal code
  933. if(!empty($line->subledger_account)) $account = $line->subledger_account;
  934. else $account = $line->numero_compte;
  935. print self::trunc($account, 15) . $separator;//Account number
  936. print self::trunc($line->label_compte, 60) . $separator;//Account label
  937. print self::trunc($line->doc_ref, 20) . $separator;//Piece
  938. print self::trunc($line->label_operation, 60) . $separator;//Operation label
  939. print price(abs($line->montant)) . $separator;//Amount
  940. print $line->sens . $separator;//Direction
  941. print $separator;//Analytic
  942. print $separator;//Analytic
  943. print $separator;//Analytic
  944. print $separator;//Analytic
  945. print $separator;//Analytic
  946. print $separator;//Analytic
  947. print $end_line;
  948. }
  949. }
  950. /**
  951. * trunc
  952. *
  953. * @param string $str String
  954. * @param integer $size Data to trunc
  955. * @return string
  956. */
  957. public static function trunc($str, $size)
  958. {
  959. return dol_trunc($str, $size, 'right', 'UTF-8', 1);
  960. }
  961. /**
  962. * toAnsi
  963. *
  964. * @param string $str Original string to encode and optionaly truncate
  965. * @param integer $size Truncate string after $size characters
  966. * @return string String encoded in Windows-1251 charset
  967. */
  968. public static function toAnsi($str, $size = -1)
  969. {
  970. $retVal= dol_string_nohtmltag($str, 1, 'Windows-1251');
  971. if ($retVal >= 0 && $size >= 0)
  972. {
  973. $retVal= mb_substr($retVal, 0, $size, 'Windows-1251');
  974. }
  975. return $retVal;
  976. }
  977. }