dolibarr  19.0.0-dev
accountancyexport.class.php
Go to the documentation of this file.
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-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
9  * Copyright (C) 2022 Lionel Vessiller <lvessiller@open-dsi.fr>
10  * Copyright (C) 2013-2017 Olivier Geffroy <jeff@jeffinfo.com>
11  * Copyright (C) 2017 Elarifr. Ari Elbaz <github@accedinfo.com>
12  * Copyright (C) 2017-2019 Frédéric France <frederic.france@netlogic.fr>
13  * Copyright (C) 2017 André Schild <a.schild@aarboard.ch>
14  * Copyright (C) 2020 Guillaume Alexandre <guillaume@tag-info.fr>
15  * Copyright (C) 2022 Joachim Kueter <jkueter@gmx.de>
16  * Copyright (C) 2022 Progiseize <a.bisotti@progiseize.fr>
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <https://www.gnu.org/licenses/>.
30  */
31 
38 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
39 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
40 require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
41 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
42 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
43 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
44 
49 {
50  // Type of export. Used into $conf->global->ACCOUNTING_EXPORT_MODELCSV
51  public static $EXPORT_TYPE_CONFIGURABLE = 1; // CSV
52  public static $EXPORT_TYPE_AGIRIS = 10;
53  public static $EXPORT_TYPE_EBP = 15;
54  public static $EXPORT_TYPE_CEGID = 20;
55  public static $EXPORT_TYPE_COGILOG = 25;
56  public static $EXPORT_TYPE_COALA = 30;
57  public static $EXPORT_TYPE_BOB50 = 35;
58  public static $EXPORT_TYPE_CIEL = 40;
59  public static $EXPORT_TYPE_SAGE50_SWISS = 45;
60  public static $EXPORT_TYPE_CHARLEMAGNE = 50;
61  public static $EXPORT_TYPE_QUADRATUS = 60;
62  public static $EXPORT_TYPE_WINFIC = 70;
63  public static $EXPORT_TYPE_OPENCONCERTO = 100;
64  public static $EXPORT_TYPE_LDCOMPTA = 110;
65  public static $EXPORT_TYPE_LDCOMPTA10 = 120;
66  public static $EXPORT_TYPE_GESTIMUMV3 = 130;
67  public static $EXPORT_TYPE_GESTIMUMV5 = 135;
68  public static $EXPORT_TYPE_ISUITEEXPERT = 200;
69  // Generic FEC after that
70  public static $EXPORT_TYPE_FEC = 1000;
71  public static $EXPORT_TYPE_FEC2 = 1010;
72 
76  public $db;
77 
81  public $errors = array();
82 
87  public $separator = '';
88 
93  public $end_line = '';
94 
100  public function __construct(DoliDB $db)
101  {
102  global $conf, $hookmanager;
103 
104  $this->db = $db;
105  $this->separator = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
106  $this->end_line = empty($conf->global->ACCOUNTING_EXPORT_ENDLINE) ? "\n" : ($conf->global->ACCOUNTING_EXPORT_ENDLINE == 1 ? "\n" : "\r\n");
107 
108  $hookmanager->initHooks(array('accountancyexport'));
109  }
110 
116  public function getType()
117  {
118  global $langs, $hookmanager;
119 
120  $listofexporttypes = array(
121  self::$EXPORT_TYPE_CONFIGURABLE => $langs->trans('Modelcsv_configurable'),
122  self::$EXPORT_TYPE_CEGID => $langs->trans('Modelcsv_CEGID'),
123  self::$EXPORT_TYPE_COALA => $langs->trans('Modelcsv_COALA'),
124  self::$EXPORT_TYPE_BOB50 => $langs->trans('Modelcsv_bob50'),
125  self::$EXPORT_TYPE_CIEL => $langs->trans('Modelcsv_ciel'),
126  self::$EXPORT_TYPE_QUADRATUS => $langs->trans('Modelcsv_quadratus'),
127  self::$EXPORT_TYPE_WINFIC => $langs->trans('Modelcsv_winfic'),
128  self::$EXPORT_TYPE_EBP => $langs->trans('Modelcsv_ebp'),
129  self::$EXPORT_TYPE_COGILOG => $langs->trans('Modelcsv_cogilog'),
130  self::$EXPORT_TYPE_AGIRIS => $langs->trans('Modelcsv_agiris'),
131  self::$EXPORT_TYPE_OPENCONCERTO => $langs->trans('Modelcsv_openconcerto'),
132  self::$EXPORT_TYPE_SAGE50_SWISS => $langs->trans('Modelcsv_Sage50_Swiss'),
133  self::$EXPORT_TYPE_CHARLEMAGNE => $langs->trans('Modelcsv_charlemagne'),
134  self::$EXPORT_TYPE_LDCOMPTA => $langs->trans('Modelcsv_LDCompta'),
135  self::$EXPORT_TYPE_LDCOMPTA10 => $langs->trans('Modelcsv_LDCompta10'),
136  self::$EXPORT_TYPE_GESTIMUMV3 => $langs->trans('Modelcsv_Gestinumv3'),
137  self::$EXPORT_TYPE_GESTIMUMV5 => $langs->trans('Modelcsv_Gestinumv5'),
138  self::$EXPORT_TYPE_FEC => $langs->trans('Modelcsv_FEC'),
139  self::$EXPORT_TYPE_FEC2 => $langs->trans('Modelcsv_FEC2'),
140  self::$EXPORT_TYPE_ISUITEEXPERT => 'Export iSuite Expert',
141  );
142 
143  // allow modules to define export formats
144  $parameters = array();
145  $reshook = $hookmanager->executeHooks('getType', $parameters, $listofexporttypes);
146 
147  ksort($listofexporttypes, SORT_NUMERIC);
148 
149  return $listofexporttypes;
150  }
151 
158  public static function getFormatCode($type)
159  {
160  $formatcode = array(
161  self::$EXPORT_TYPE_CONFIGURABLE => 'csv',
162  self::$EXPORT_TYPE_CEGID => 'cegid',
163  self::$EXPORT_TYPE_COALA => 'coala',
164  self::$EXPORT_TYPE_BOB50 => 'bob50',
165  self::$EXPORT_TYPE_CIEL => 'ciel',
166  self::$EXPORT_TYPE_QUADRATUS => 'quadratus',
167  self::$EXPORT_TYPE_WINFIC => 'winfic',
168  self::$EXPORT_TYPE_EBP => 'ebp',
169  self::$EXPORT_TYPE_COGILOG => 'cogilog',
170  self::$EXPORT_TYPE_AGIRIS => 'agiris',
171  self::$EXPORT_TYPE_OPENCONCERTO => 'openconcerto',
172  self::$EXPORT_TYPE_SAGE50_SWISS => 'sage50ch',
173  self::$EXPORT_TYPE_CHARLEMAGNE => 'charlemagne',
174  self::$EXPORT_TYPE_LDCOMPTA => 'ldcompta',
175  self::$EXPORT_TYPE_LDCOMPTA10 => 'ldcompta10',
176  self::$EXPORT_TYPE_GESTIMUMV3 => 'gestimumv3',
177  self::$EXPORT_TYPE_GESTIMUMV5 => 'gestimumv5',
178  self::$EXPORT_TYPE_FEC => 'fec',
179  self::$EXPORT_TYPE_FEC2 => 'fec2',
180  self::$EXPORT_TYPE_ISUITEEXPERT => 'isuiteexpert',
181  );
182 
183  global $hookmanager;
184  $code = $formatcode[$type];
185  $parameters = array('type' => $type);
186  $reshook = $hookmanager->executeHooks('getFormatCode', $parameters, $code);
187 
188  return $code;
189  }
190 
196  public function getTypeConfig()
197  {
198  global $conf, $langs;
199 
200  $exporttypes = array(
201  'param' => array(
202  self::$EXPORT_TYPE_CONFIGURABLE => array(
203  'label' => $langs->trans('Modelcsv_configurable'),
204  'ACCOUNTING_EXPORT_FORMAT' => empty($conf->global->ACCOUNTING_EXPORT_FORMAT) ? 'txt' : $conf->global->ACCOUNTING_EXPORT_FORMAT,
205  'ACCOUNTING_EXPORT_SEPARATORCSV' => empty($conf->global->ACCOUNTING_EXPORT_SEPARATORCSV) ? ',' : $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV,
206  'ACCOUNTING_EXPORT_ENDLINE' => empty($conf->global->ACCOUNTING_EXPORT_ENDLINE) ? 1 : $conf->global->ACCOUNTING_EXPORT_ENDLINE,
207  'ACCOUNTING_EXPORT_DATE' => getDolGlobalString('ACCOUNTING_EXPORT_DATE', '%Y-%m-%d'),
208  ),
209  self::$EXPORT_TYPE_CEGID => array(
210  'label' => $langs->trans('Modelcsv_CEGID'),
211  ),
212  self::$EXPORT_TYPE_COALA => array(
213  'label' => $langs->trans('Modelcsv_COALA'),
214  ),
215  self::$EXPORT_TYPE_BOB50 => array(
216  'label' => $langs->trans('Modelcsv_bob50'),
217  ),
218  self::$EXPORT_TYPE_CIEL => array(
219  'label' => $langs->trans('Modelcsv_ciel'),
220  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
221  ),
222  self::$EXPORT_TYPE_QUADRATUS => array(
223  'label' => $langs->trans('Modelcsv_quadratus'),
224  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
225  ),
226  self::$EXPORT_TYPE_WINFIC => array(
227  'label' => $langs->trans('Modelcsv_winfic'),
228  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
229  ),
230  self::$EXPORT_TYPE_EBP => array(
231  'label' => $langs->trans('Modelcsv_ebp'),
232  ),
233  self::$EXPORT_TYPE_COGILOG => array(
234  'label' => $langs->trans('Modelcsv_cogilog'),
235  ),
236  self::$EXPORT_TYPE_AGIRIS => array(
237  'label' => $langs->trans('Modelcsv_agiris'),
238  ),
239  self::$EXPORT_TYPE_OPENCONCERTO => array(
240  'label' => $langs->trans('Modelcsv_openconcerto'),
241  ),
242  self::$EXPORT_TYPE_SAGE50_SWISS => array(
243  'label' => $langs->trans('Modelcsv_Sage50_Swiss'),
244  ),
245  self::$EXPORT_TYPE_CHARLEMAGNE => array(
246  'label' => $langs->trans('Modelcsv_charlemagne'),
247  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
248  ),
249  self::$EXPORT_TYPE_LDCOMPTA => array(
250  'label' => $langs->trans('Modelcsv_LDCompta'),
251  ),
252  self::$EXPORT_TYPE_LDCOMPTA10 => array(
253  'label' => $langs->trans('Modelcsv_LDCompta10'),
254  ),
255  self::$EXPORT_TYPE_GESTIMUMV3 => array(
256  'label' => $langs->trans('Modelcsv_Gestinumv3'),
257  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
258  ),
259  self::$EXPORT_TYPE_GESTIMUMV5 => array(
260  'label' => $langs->trans('Modelcsv_Gestinumv5'),
261  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
262  ),
263  self::$EXPORT_TYPE_FEC => array(
264  'label' => $langs->trans('Modelcsv_FEC'),
265  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
266  ),
267  self::$EXPORT_TYPE_FEC2 => array(
268  'label' => $langs->trans('Modelcsv_FEC2'),
269  'ACCOUNTING_EXPORT_FORMAT' => 'txt',
270  ),
271  self::$EXPORT_TYPE_ISUITEEXPERT => array(
272  'label' => 'iSuite Expert',
273  'ACCOUNTING_EXPORT_FORMAT' => 'csv',
274  ),
275  ),
276  'cr'=> array(
277  '1' => $langs->trans("Unix"),
278  '2' => $langs->trans("Windows")
279  ),
280  'format' => array(
281  'csv' => $langs->trans("csv"),
282  'txt' => $langs->trans("txt")
283  ),
284  );
285 
286  global $hookmanager;
287  $parameters = array();
288  $reshook = $hookmanager->executeHooks('getTypeConfig', $parameters, $exporttypes);
289  return $exporttypes;
290  }
291 
292 
299  public function getMimeType($formatexportset)
300  {
301  $mime = 'text/csv';
302 
303  switch ($formatexportset) {
304  case self::$EXPORT_TYPE_FEC:
305  $mime = 'text/tab-separated-values';
306  break;
307  default:
308  $mime = 'text/csv';
309  break;
310  }
311 
312  return $mime;
313  }
314 
330  public function export(&$TData, $formatexportset, $withAttachment = 0, $downloadMode = 0, $outputMode = 0)
331  {
332  global $conf, $langs;
333  global $search_date_end; // Used into /accountancy/tpl/export_journal.tpl.php
334 
335  // Define name of file to save
336  $filename = 'general_ledger-'.$this->getFormatCode($formatexportset);
337  $type_export = 'general_ledger';
338 
339  global $db; // The tpl file use $db
340  $completefilename = '';
341  $exportFile = null;
342  $exportFileName = '';
343  $exportFilePath = '';
344  $archiveFullName = '';
345  $archivePath = '';
346  $archiveFileList = array();
347  if ($withAttachment == 1) {
348  if ($downloadMode == 0) {
349  $downloadMode = 1; // force to download after writing all files (can't use direct download)
350  }
351  if ($outputMode == 0) {
352  $outputMode = 1; // force to put files in a temp directory (can't use print on screen)
353  }
354 
355  // PHP ZIP extension must be enabled
356  if (!extension_loaded('zip')) {
357  $langs->load('install');
358  $this->errors[] = $langs->trans('ErrorPHPDoesNotSupport', 'ZIP');
359  return -1;
360  }
361  }
362 
363  $mimetype = $this->getMimeType($formatexportset);
364  if ($downloadMode == 0) {
365  // begin to print header for direct download
366  top_httphead($mimetype, 1);
367  }
368  include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
369  if ($outputMode == 1 || $outputMode == 2) {
370  if ($outputMode == 1) {
371  // uses temp directory by default to write files
372  if (!empty($conf->accounting->multidir_temp[$conf->entity])) {
373  $outputDir = $conf->accounting->multidir_temp[$conf->entity];
374  } else {
375  $outputDir = $conf->accounting->dir_temp;
376  }
377  } else {
378  // uses default export directory "accounting/export"
379  if (!empty($conf->accounting->multidir_output[$conf->entity])) {
380  $outputDir = $conf->accounting->multidir_output[$conf->entity];
381  } else {
382  $outputDir = $conf->accounting->dir_output;
383  }
384 
385  // directory already created when module is enabled
386  $outputDir .= '/export';
387  $outputDir .= '/'.dol_sanitizePathName($formatexportset);
388  if (!dol_is_dir($outputDir)) {
389  if (dol_mkdir($outputDir) < 0) {
390  $this->errors[] = $langs->trans('ErrorCanNotCreateDir', $outputDir);
391  return -1;
392  }
393  }
394  }
395 
396  if ($outputDir != '') {
397  if (!dol_is_dir($outputDir)) {
398  $langs->load('errors');
399  $this->errors[] = $langs->trans('ErrorDirNotFound', $outputDir);
400  return -1;
401  }
402 
403  if (!empty($completefilename)) {
404  // create export file
405  $exportFileFullName = $completefilename;
406  $exportFileBaseName = basename($exportFileFullName);
407  $exportFileName = pathinfo($exportFileBaseName, PATHINFO_FILENAME);
408  $exportFilePath = $outputDir . '/' . $exportFileFullName;
409  $exportFile = fopen($exportFilePath, 'w');
410  if (!$exportFile) {
411  $this->errors[] = $langs->trans('ErrorFileNotFound', $exportFilePath);
412  return -1;
413  }
414 
415  if ($withAttachment == 1) {
416  $archiveFileList[0] = array(
417  'path' => $exportFilePath,
418  'name' => $exportFileFullName,
419  );
420 
421  // archive name and path
422  $archiveFullName = $exportFileName . '.zip';
423  $archivePath = $outputDir . '/' . $archiveFullName;
424  }
425  }
426  }
427  }
428 
429  // export file (print on screen or write in a file) and prepare archive list if with attachment is set to 1
430  switch ($formatexportset) {
431  case self::$EXPORT_TYPE_CONFIGURABLE:
432  $this->exportConfigurable($TData, $exportFile);
433  break;
434  case self::$EXPORT_TYPE_CEGID:
435  $this->exportCegid($TData, $exportFile);
436  break;
437  case self::$EXPORT_TYPE_COALA:
438  $this->exportCoala($TData, $exportFile);
439  break;
440  case self::$EXPORT_TYPE_BOB50:
441  $this->exportBob50($TData, $exportFile);
442  break;
443  case self::$EXPORT_TYPE_CIEL:
444  $this->exportCiel($TData, $exportFile);
445  break;
446  case self::$EXPORT_TYPE_QUADRATUS:
447  $archiveFileList = $this->exportQuadratus($TData, $exportFile, $archiveFileList, $withAttachment);
448  break;
449  case self::$EXPORT_TYPE_WINFIC:
450  $this->exportWinfic($TData, $exportFile);
451  break;
452  case self::$EXPORT_TYPE_EBP:
453  $this->exportEbp($TData, $exportFile);
454  break;
455  case self::$EXPORT_TYPE_COGILOG:
456  $this->exportCogilog($TData, $exportFile);
457  break;
458  case self::$EXPORT_TYPE_AGIRIS:
459  $this->exportAgiris($TData, $exportFile);
460  break;
461  case self::$EXPORT_TYPE_OPENCONCERTO:
462  $this->exportOpenConcerto($TData, $exportFile);
463  break;
464  case self::$EXPORT_TYPE_SAGE50_SWISS:
465  $this->exportSAGE50SWISS($TData, $exportFile);
466  break;
467  case self::$EXPORT_TYPE_CHARLEMAGNE:
468  $this->exportCharlemagne($TData, $exportFile);
469  break;
470  case self::$EXPORT_TYPE_LDCOMPTA:
471  $this->exportLDCompta($TData, $exportFile);
472  break;
473  case self::$EXPORT_TYPE_LDCOMPTA10:
474  $this->exportLDCompta10($TData, $exportFile);
475  break;
476  case self::$EXPORT_TYPE_GESTIMUMV3:
477  $this->exportGestimumV3($TData, $exportFile);
478  break;
479  case self::$EXPORT_TYPE_GESTIMUMV5:
480  $this->exportGestimumV5($TData, $exportFile);
481  break;
482  case self::$EXPORT_TYPE_FEC:
483  $this->exportFEC($TData, $exportFile);
484  break;
485  case self::$EXPORT_TYPE_FEC2:
486  $this->exportFEC2($TData, $exportFile);
487  break;
488  case self::$EXPORT_TYPE_ISUITEEXPERT :
489  $this->exportiSuiteExpert($TData, $exportFile);
490  break;
491  default:
492  global $hookmanager;
493  $parameters = array('format' => $formatexportset);
494  // file contents will be created in the hooked function via print
495  $reshook = $hookmanager->executeHooks('export', $parameters, $TData);
496  if ($reshook != 1) {
497  $this->errors[] = $langs->trans('accountancy_error_modelnotfound');
498  }
499  break;
500  }
501 
502  // create and download export file or archive
503  if ($outputMode == 1 || $outputMode == 2) {
504  $error = 0;
505 
506  // close export file
507  if ($exportFile) {
508  fclose($exportFile);
509  }
510 
511  if ($withAttachment == 1) {
512  // create archive file
513  if (!empty($archiveFullName) && !empty($archivePath) && !empty($archiveFileList)) {
514  // archive files
515  $downloadFileMimeType = 'application/zip';
516  $downloadFileFullName = $archiveFullName;
517  $downloadFilePath = $archivePath;
518 
519  // create archive
520  $archive = new ZipArchive();
521  $res = $archive->open($archivePath, ZipArchive::OVERWRITE | ZipArchive::CREATE);
522  if ($res !== true) {
523  $error++;
524  $this->errors[] = $langs->trans('ErrorFileNotFound', $archivePath);
525  }
526  if (!$error) {
527  // add files
528  foreach ($archiveFileList as $archiveFileArr) {
529  $res = $archive->addFile($archiveFileArr['path'], $archiveFileArr['name']);
530  if (!$res) {
531  $error++;
532  $this->errors[] = $langs->trans('ErrorArchiveAddFile', $archiveFileArr['name']);
533  break;
534  }
535  }
536  }
537  if (!$error) {
538  // close archive
539  $archive->close();
540  }
541  }
542  }
543 
544  if (!$error) {
545  // download after writing files
546  if ($downloadMode == 1) {
547  if ($withAttachment == 0) {
548  // only download exported file
549  if (!empty($exportFileFullName) && !empty($exportFilePath)) {
550  $downloadFileMimeType = $mimetype;
551  $downloadFileFullName = $exportFileFullName;
552  $downloadFilePath = $exportFilePath;
553  }
554  }
555 
556  // download export file or archive
557  if (!empty($downloadFileMimeType) && !empty($downloadFileFullName) && !empty($downloadFilePath)) {
558  header('Content-Type: ' . $downloadFileMimeType);
559  header('Content-Disposition: attachment; filename=' . $downloadFileFullName);
560  header('Cache-Control: Public, must-revalidate');
561  header('Pragma: public');
562  header('Content-Length: ' . dol_filesize($downloadFilePath));
563  readfileLowMemory($downloadFilePath);
564  }
565  }
566  }
567 
568  if ($error) {
569  return -1;
570  }
571  }
572 
573  return 1;
574  }
575 
576 
584  public function exportCegid($objectLines, $exportFile = null)
585  {
586  $separator = ";";
587  $end_line = "\n";
588 
589  foreach ($objectLines as $line) {
590  $date_document = dol_print_date($line->doc_date, '%d%m%Y');
591 
592  $tab = array();
593 
594  $tab[] = $date_document;
595  $tab[] = $line->code_journal;
596  $tab[] = length_accountg($line->numero_compte);
597  $tab[] = length_accounta($line->subledger_account);
598  $tab[] = $line->sens;
599  $tab[] = price2fec(abs($line->debit - $line->credit));
600  $tab[] = dol_string_unaccent($line->label_operation);
601  $tab[] = dol_string_unaccent($line->doc_ref);
602 
603  $output = implode($separator, $tab).$end_line;
604  if ($exportFile) {
605  fwrite($exportFile, $output);
606  } else {
607  print $output;
608  }
609  }
610  }
611 
620  public function exportCogilog($objectLines, $exportFile = null)
621  {
622  $separator = "\t";
623  $end_line = "\n";
624 
625  foreach ($objectLines as $line) {
626  $date_document = dol_print_date($line->doc_date, '%d%m%Y');
627 
628  $refInvoice = '';
629  if ($line->doc_type == 'customer_invoice') {
630  // Customer invoice
631  require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
632  $invoice = new Facture($this->db);
633  $invoice->fetch($line->fk_doc);
634 
635  $refInvoice = $invoice->ref;
636  } elseif ($line->doc_type == 'supplier_invoice') {
637  // Supplier invoice
638  require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
639  $invoice = new FactureFournisseur($this->db);
640  $invoice->fetch($line->fk_doc);
641 
642  $refInvoice = $invoice->ref_supplier;
643  }
644 
645  $tab = array();
646 
647  $tab[] = $line->code_journal;
648  $tab[] = $date_document;
649  $tab[] = $refInvoice;
650  if (empty($line->subledger_account)) {
651  $tab[] = length_accountg($line->numero_compte);
652  } else {
653  $tab[] = length_accounta($line->subledger_account);
654  }
655  $tab[] = "";
656  $tab[] = $line->label_operation;
657  $tab[] = $date_document;
658  if ($line->sens == 'D') {
659  $tab[] = price($line->debit);
660  $tab[] = "";
661  } elseif ($line->sens == 'C') {
662  $tab[] = "";
663  $tab[] = price($line->credit);
664  }
665  $tab[] = $line->doc_ref;
666  $tab[] = $line->label_operation;
667 
668  $output = implode($separator, $tab).$end_line;
669  if ($exportFile) {
670  fwrite($exportFile, $output);
671  } else {
672  print $output;
673  }
674  }
675  }
676 
684  public function exportCoala($objectLines, $exportFile = null)
685  {
686  // Coala export
687  $separator = ";";
688  $end_line = "\n";
689 
690  foreach ($objectLines as $line) {
691  $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
692 
693  $tab = array();
694 
695  $tab[] = $date_document;
696  $tab[] = $line->code_journal;
697  $tab[] = length_accountg($line->numero_compte);
698  $tab[] = $line->piece_num;
699  $tab[] = $line->doc_ref;
700  $tab[] = price($line->debit);
701  $tab[] = price($line->credit);
702  $tab[] = 'E';
703  $tab[] = length_accounta($line->subledger_account);
704 
705  $output = implode($separator, $tab).$end_line;
706  if ($exportFile) {
707  fwrite($exportFile, $output);
708  } else {
709  print $output;
710  }
711  }
712  }
713 
721  public function exportBob50($objectLines, $exportFile = null)
722  {
723  // Bob50
724  $separator = ";";
725  $end_line = "\n";
726 
727  foreach ($objectLines as $line) {
728  $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
729 
730  $tab = array();
731 
732  $tab[] = $line->piece_num;
733  $tab[] = $date_document;
734 
735  if (empty($line->subledger_account)) {
736  $tab[] = 'G';
737  $tab[] = length_accountg($line->numero_compte);
738  } else {
739  if (substr($line->numero_compte, 0, 3) == '411') {
740  $tab[] = 'C';
741  }
742  if (substr($line->numero_compte, 0, 3) == '401') {
743  $tab[] = 'F';
744  }
745  $tab[] = length_accounta($line->subledger_account);
746  }
747 
748  $tab[] = price($line->debit);
749  $tab[] = price($line->credit);
750  $tab[] = dol_trunc($line->label_operation, 32);
751 
752  $output = implode($separator, $tab).$end_line;
753  if ($exportFile) {
754  fwrite($exportFile, $output);
755  } else {
756  print $output;
757  }
758  }
759  }
760 
776  public function exportCiel($objectLines, $exportFile = null)
777  {
778  $end_line = "\r\n";
779 
780  $i = 1;
781 
782  foreach ($objectLines as $line) {
783  $code_compta = length_accountg($line->numero_compte);
784  if (!empty($line->subledger_account)) {
785  $code_compta = length_accounta($line->subledger_account);
786  }
787 
788  $date_document = dol_print_date($line->doc_date, '%Y%m%d');
789  $date_echeance = dol_print_date($line->date_lim_reglement, '%Y%m%d');
790 
791  $tab = array();
792 
793  $tab[] = str_pad($line->piece_num, 5);
794  $tab[] = str_pad(self::trunc($line->code_journal, 2), 2);
795  $tab[] = str_pad($date_document, 8, ' ', STR_PAD_LEFT);
796  $tab[] = str_pad($date_echeance, 8, ' ', STR_PAD_LEFT);
797  $tab[] = str_pad(self::trunc($line->doc_ref, 12), 12);
798  $tab[] = str_pad(self::trunc($code_compta, 11), 11);
799  $tab[] = str_pad(self::trunc(dol_string_unaccent($line->doc_ref).dol_string_unaccent($line->label_operation), 25), 25);
800  $tab[] = str_pad(price2fec(abs($line->debit - $line->credit)), 13, ' ', STR_PAD_LEFT);
801  $tab[] = str_pad($line->sens, 1);
802  $tab[] = str_repeat(' ', 18); // Analytical accounting - Not managed in Dolibarr
803  $tab[] = str_pad(self::trunc(dol_string_unaccent($line->label_operation), 34), 34);
804  $tab[] = 'O2003'; // 0 = EUR | 2003 = Format Ciel
805 
806  $output = implode($tab).$end_line;
807  if ($exportFile) {
808  fwrite($exportFile, $output);
809  } else {
810  print $output;
811  }
812  $i++;
813  }
814  }
815 
830  public function exportQuadratus($objectLines, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
831  {
832  global $conf, $db;
833 
834  $end_line = "\r\n";
835 
836  // We should use dol_now function not time however this is wrong date to transfert in accounting
837  // $date_ecriture = dol_print_date(dol_now(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
838  // $date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
839  foreach ($objectLines as $line) {
840  // Clean some data
841  $line->doc_ref = dol_string_unaccent($line->doc_ref);
842  $line->label_operation = dol_string_unaccent($line->label_operation);
843  $line->numero_compte = dol_string_unaccent($line->numero_compte);
844  $line->label_compte = dol_string_unaccent($line->label_compte);
845  $line->subledger_account = dol_string_unaccent($line->subledger_account);
846  $line->subledger_label = dol_string_unaccent($line->subledger_label);
847 
848  $code_compta = $line->numero_compte;
849  if (!empty($line->subledger_account)) {
850  $code_compta = $line->subledger_account;
851  }
852 
853  $tab = array();
854 
855  if (!empty($line->subledger_account)) {
856  $tab['type_ligne'] = 'C';
857  $tab['num_compte'] = str_pad(self::trunc($line->subledger_account, 8), 8);
858  $tab['lib_compte'] = str_pad(self::trunc($line->subledger_label, 30), 30);
859 
860  if ($line->doc_type == 'customer_invoice') {
861  $tab['lib_alpha'] = strtoupper(str_pad('C'.self::trunc(dol_string_unaccent($line->subledger_label), 6), 6));
862  $tab['filler'] = str_repeat(' ', 52);
863  $tab['coll_compte'] = str_pad(self::trunc($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER, 8), 8);
864  } elseif ($line->doc_type == 'supplier_invoice') {
865  $tab['lib_alpha'] = strtoupper(str_pad('F'.self::trunc(dol_string_unaccent($line->subledger_label), 6), 6));
866  $tab['filler'] = str_repeat(' ', 52);
867  $tab['coll_compte'] = str_pad(self::trunc($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER, 8), 8);
868  } else {
869  $tab['filler'] = str_repeat(' ', 59);
870  $tab['coll_compte'] = str_pad(' ', 8);
871  }
872 
873  $tab['filler2'] = str_repeat(' ', 110);
874  $tab['Maj'] = 2; // Partial update (alpha key, label, address, collectif, RIB)
875 
876  if ($line->doc_type == 'customer_invoice') {
877  $tab['type_compte'] = 'C';
878  } elseif ($line->doc_type == 'supplier_invoice') {
879  $tab['type_compte'] = 'F';
880  } else {
881  $tab['type_compte'] = 'G';
882  }
883 
884  $tab['filler3'] = str_repeat(' ', 235);
885 
886  $tab['end_line'] = $end_line;
887 
888  if ($exportFile) {
889  fwrite($exportFile, implode($tab));
890  } else {
891  print implode($tab);
892  }
893  }
894 
895  $tab = array();
896  $tab['type_ligne'] = 'M';
897  $tab['num_compte'] = str_pad(self::trunc($code_compta, 8), 8);
898  $tab['code_journal'] = str_pad(self::trunc($line->code_journal, 2), 2);
899  $tab['folio'] = '000';
900 
901  // We use invoice date $line->doc_date not $date_ecriture which is the transfert date
902  // maybe we should set an option for customer who prefer to keep in accounting software the tranfert date instead of invoice date ?
903  //$tab['date_ecriture'] = $date_ecriture;
904  $tab['date_ecriture'] = dol_print_date($line->doc_date, '%d%m%y');
905  $tab['filler'] = ' ';
906  $tab['libelle_ecriture'] = str_pad(self::trunc($line->doc_ref.' '.$line->label_operation, 20), 20);
907 
908  // Credit invoice - invert sens
909  /*
910  if ($line->montant < 0) {
911  if ($line->sens == 'C') {
912  $tab['sens'] = 'D';
913  } else {
914  $tab['sens'] = 'C';
915  }
916  $tab['signe_montant'] = '-';
917  } else {
918  $tab['sens'] = $line->sens; // C or D
919  $tab['signe_montant'] = '+';
920  }*/
921  $tab['sens'] = $line->sens; // C or D
922  $tab['signe_montant'] = '+';
923 
924  // The amount must be in centimes without decimal points.
925  $tab['montant'] = str_pad(abs(($line->debit - $line->credit) * 100), 12, '0', STR_PAD_LEFT);
926  $tab['contrepartie'] = str_repeat(' ', 8);
927 
928  // Force date format : %d%m%y
929  if (!empty($line->date_lim_reglement)) {
930  //$tab['date_echeance'] = dol_print_date($line->date_lim_reglement, $conf->global->ACCOUNTING_EXPORT_DATE);
931  $tab['date_echeance'] = dol_print_date($line->date_lim_reglement, '%d%m%y'); // Format must be ddmmyy
932  } else {
933  $tab['date_echeance'] = '000000';
934  }
935 
936  // Please keep quadra named field lettrage(2) + codestat(3) instead of fake lettrage(5)
937  // $tab['lettrage'] = str_repeat(' ', 5);
938  $tab['lettrage'] = str_repeat(' ', 2);
939  $tab['codestat'] = str_repeat(' ', 3);
940  $tab['num_piece'] = str_pad(self::trunc($line->piece_num, 5), 5);
941 
942  // Keep correct quadra named field instead of anon filler
943  // $tab['filler2'] = str_repeat(' ', 20);
944  $tab['affaire'] = str_repeat(' ', 10);
945  $tab['quantity1'] = str_repeat(' ', 10);
946  $tab['num_piece2'] = str_pad(self::trunc($line->piece_num, 8), 8);
947  $tab['devis'] = str_pad($conf->currency, 3);
948  $tab['code_journal2'] = str_pad(self::trunc($line->code_journal, 3), 3);
949  $tab['filler3'] = str_repeat(' ', 3);
950 
951  // Keep correct quadra named field instead of anon filler libelle_ecriture2 is 30 char not 32 !!!!
952  // 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
953  // TODO: we should filter more than only accent to avoid wrong line size
954  // TODO: remove invoice number doc_ref in label,
955  // TODO: we should offer an option for customer to build the label using invoice number / name / date in accounting software
956  //$tab['libelle_ecriture2'] = str_pad(self::trunc($line->doc_ref . ' ' . $line->label_operation, 30), 30);
957  $tab['libelle_ecriture2'] = str_pad(self::trunc($line->label_operation, 30), 30);
958  $tab['codetva'] = str_repeat(' ', 2);
959 
960  // We need to keep the 10 lastest number of invoice doc_ref not the beginning part that is the unusefull almost same part
961  // $tab['num_piece3'] = str_pad(self::trunc($line->piece_num, 10), 10);
962  $tab['num_piece3'] = substr(self::trunc($line->doc_ref, 20), -10);
963  $tab['reserved'] = str_repeat(' ', 10); // position 159
964  $tab['currency_amount'] = str_repeat(' ', 13); // position 169
965  // get document file
966  $attachmentFileName = '';
967  if ($withAttachment == 1) {
968  $attachmentFileKey = trim($line->piece_num);
969 
970  if (!isset($archiveFileList[$attachmentFileKey])) {
971  $objectDirPath = '';
972  $objectFileName = dol_sanitizeFileName($line->doc_ref);
973  if ($line->doc_type == 'customer_invoice') {
974  $objectDirPath = !empty($conf->facture->multidir_output[$conf->entity]) ? $conf->facture->multidir_output[$conf->entity] : $conf->facture->dir_output;
975  } elseif ($line->doc_type == 'expense_report') {
976  $objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->factureexpensereport->dir_output;
977  } elseif ($line->doc_type == 'supplier_invoice') {
978  $objectDirPath = !empty($conf->fournisseur->facture->multidir_output[$conf->entity]) ? $conf->fournisseur->facture->multidir_output[$conf->entity] : $conf->fournisseur->facture->dir_output;
979  }
980  $arrayofinclusion = array();
981  $arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').'\.pdf$';
982  $fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, true);
983  if (!empty($fileFoundList)) {
984  $attachmentFileNameTrunc = str_pad(self::trunc($line->piece_num, 8), 8, '0', STR_PAD_LEFT);
985  foreach ($fileFoundList as $fileFound) {
986  if (strstr($fileFound['name'], $objectFileName)) {
987  $fileFoundPath = $objectDirPath.'/'.$objectFileName.'/'.$fileFound['name'];
988  if (file_exists($fileFoundPath)) {
989  $archiveFileList[$attachmentFileKey] = array(
990  'path' => $fileFoundPath,
991  'name' => $attachmentFileNameTrunc.'.pdf',
992  );
993  break;
994  }
995  }
996  }
997  }
998  }
999 
1000  if (isset($archiveFileList[$attachmentFileKey])) {
1001  $attachmentFileName = $archiveFileList[$attachmentFileKey]['name'];
1002  }
1003  }
1004  if (dol_strlen($attachmentFileName) == 12) {
1005  $tab['attachment'] = $attachmentFileName; // position 182
1006  } else {
1007  $tab['attachment'] = str_repeat(' ', 12); // position 182
1008  }
1009  $tab['filler4'] = str_repeat(' ', 38);
1010  $tab['end_line'] = $end_line;
1011 
1012  if ($exportFile) {
1013  fwrite($exportFile, implode($tab));
1014  } else {
1015  print implode($tab);
1016  }
1017  }
1018 
1019  return $archiveFileList;
1020  }
1021 
1032  public function exportWinfic($objectLines, $exportFile = null)
1033  {
1034  global $conf;
1035 
1036  $end_line = "\r\n";
1037  $index = 1;
1038 
1039  //We should use dol_now function not time however this is wrong date to transfert in accounting
1040  //$date_ecriture = dol_print_date(dol_now(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
1041  //$date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
1042 
1043  // Warning ! When truncation is necessary, no dot because 3 dots = three characters. The columns are shifted
1044 
1045  foreach ($objectLines as $line) {
1046  $code_compta = $line->numero_compte;
1047  if (!empty($line->subledger_account)) {
1048  $code_compta = $line->subledger_account;
1049  }
1050 
1051  $tab = array();
1052  //$tab['type_ligne'] = 'M';
1053  $tab['code_journal'] = str_pad(dol_trunc($line->code_journal, 2, 'right', 'UTF-8', 1), 2);
1054 
1055  //We use invoice date $line->doc_date not $date_ecriture which is the transfert date
1056  //maybe we should set an option for customer who prefer to keep in accounting software the tranfert date instead of invoice date ?
1057  //$tab['date_ecriture'] = $date_ecriture;
1058  $tab['date_operation'] = dol_print_date($line->doc_date, '%d%m%Y');
1059 
1060  $tab['folio'] = ' 1';
1061 
1062  $tab['num_ecriture'] = str_pad(dol_trunc($index, 6, 'right', 'UTF-8', 1), 6, ' ', STR_PAD_LEFT);
1063 
1064  $tab['jour_ecriture'] = dol_print_date($line->doc_date, '%d%m%y');
1065 
1066  $tab['num_compte'] = str_pad(dol_trunc($code_compta, 6, 'right', 'UTF-8', 1), 6, '0');
1067 
1068  if ($line->sens == 'D') {
1069  $tab['montant_debit'] = str_pad(number_format($line->debit, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1070 
1071  $tab['montant_crebit'] = str_pad(number_format(0, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1072  } else {
1073  $tab['montant_debit'] = str_pad(number_format(0, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1074 
1075  $tab['montant_crebit'] = str_pad(number_format($line->credit, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1076  }
1077 
1078  $tab['libelle_ecriture'] = str_pad(dol_trunc(dol_string_unaccent($line->doc_ref).' '.dol_string_unaccent($line->label_operation), 30, 'right', 'UTF-8', 1), 30);
1079 
1080  $tab['lettrage'] = str_repeat(dol_trunc($line->lettering_code, 2, 'left', 'UTF-8', 1), 2);
1081 
1082  $tab['code_piece'] = str_pad(dol_trunc($line->piece_num, 5, 'left', 'UTF-8', 1), 5, ' ', STR_PAD_LEFT);
1083 
1084  $tab['code_stat'] = str_repeat(' ', 4);
1085 
1086  if (!empty($line->date_lim_reglement)) {
1087  //$tab['date_echeance'] = dol_print_date($line->date_lim_reglement, $conf->global->ACCOUNTING_EXPORT_DATE);
1088  $tab['date_echeance'] = dol_print_date($line->date_lim_reglement, '%d%m%Y');
1089  } else {
1090  $tab['date_echeance'] = dol_print_date($line->doc_date, '%d%m%Y');
1091  }
1092 
1093  $tab['monnaie'] = '1';
1094 
1095  $tab['filler'] = ' ';
1096 
1097  $tab['ind_compteur'] = ' ';
1098 
1099  $tab['quantite'] = '0,000000000';
1100 
1101  $tab['code_pointage'] = str_repeat(' ', 2);
1102 
1103  $tab['end_line'] = $end_line;
1104 
1105  print implode('|', $tab);
1106 
1107  $index++;
1108  }
1109  }
1110 
1111 
1119  public function exportEbp($objectLines, $exportFile = null)
1120  {
1121 
1122  $separator = ',';
1123  $end_line = "\n";
1124 
1125  foreach ($objectLines as $line) {
1126  $date_document = dol_print_date($line->doc_date, '%d%m%Y');
1127 
1128  $tab = array();
1129 
1130  $tab[] = $line->id;
1131  $tab[] = $date_document;
1132  $tab[] = $line->code_journal;
1133  if (empty($line->subledger_account)) {
1134  $tab[] = $line->numero_compte;
1135  } else {
1136  $tab[] = $line->subledger_account;
1137  }
1138  //$tab[] = substr(length_accountg($line->numero_compte), 0, 2) . $separator;
1139  $tab[] = '"'.dol_trunc($line->label_operation, 40, 'right', 'UTF-8', 1).'"';
1140  $tab[] = '"'.dol_trunc($line->piece_num, 15, 'right', 'UTF-8', 1).'"';
1141  $tab[] = price2num(abs($line->debit - $line->credit));
1142  $tab[] = $line->sens;
1143  $tab[] = $date_document;
1144  //print 'EUR';
1145 
1146  $output = implode($separator, $tab).$end_line;
1147  if ($exportFile) {
1148  fwrite($exportFile, $output);
1149  } else {
1150  print $output;
1151  }
1152  }
1153  }
1154 
1155 
1163  public function exportAgiris($objectLines, $exportFile = null)
1164  {
1165  $separator = ';';
1166  $end_line = "\n";
1167 
1168  foreach ($objectLines as $line) {
1169  $date_document = dol_print_date($line->doc_date, '%d%m%Y');
1170 
1171  $tab = array();
1172 
1173  $tab[] = $line->piece_num;
1174  $tab[] = self::toAnsi($line->label_operation);
1175  $tab[] = $date_document;
1176  $tab[] = self::toAnsi($line->label_operation);
1177 
1178  if (empty($line->subledger_account)) {
1179  $tab[] = length_accountg($line->numero_compte);
1180  $tab[] = self::toAnsi($line->label_compte);
1181  } else {
1182  $tab[] = length_accounta($line->subledger_account);
1183  $tab[] = self::toAnsi($line->subledger_label);
1184  }
1185 
1186  $tab[] = self::toAnsi($line->doc_ref);
1187  $tab[] = price($line->debit);
1188  $tab[] = price($line->credit);
1189  $tab[] = price(abs($line->debit - $line->credit));
1190  $tab[] = $line->sens;
1191  $tab[] = $line->lettering_code;
1192  $tab[] = $line->code_journal;
1193 
1194  $output = implode($separator, $tab).$end_line;
1195  if ($exportFile) {
1196  fwrite($exportFile, $output);
1197  } else {
1198  print $output;
1199  }
1200  }
1201  }
1202 
1210  public function exportOpenConcerto($objectLines, $exportFile = null)
1211  {
1212  $separator = ';';
1213  $end_line = "\n";
1214 
1215  foreach ($objectLines as $line) {
1216  $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
1217 
1218  $tab = array();
1219 
1220  $tab[] = $date_document;
1221  $tab[] = $line->code_journal;
1222  if (empty($line->subledger_account)) {
1223  $tab[] = length_accountg($line->numero_compte);
1224  } else {
1225  $tab[] = length_accounta($line->subledger_account);
1226  }
1227  $tab[] = $line->doc_ref;
1228  $tab[] = $line->label_operation;
1229  $tab[] = price($line->debit);
1230  $tab[] = price($line->credit);
1231 
1232  $output = implode($separator, $tab).$end_line;
1233  if ($exportFile) {
1234  fwrite($exportFile, $output);
1235  } else {
1236  print $output;
1237  }
1238  }
1239  }
1240 
1248  public function exportConfigurable($objectLines, $exportFile = null)
1249  {
1250  global $conf;
1251 
1252  $separator = $this->separator;
1253 
1254  foreach ($objectLines as $line) {
1255  $date_document = dol_print_date($line->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
1256 
1257  $tab = array();
1258  // export configurable
1259  $tab[] = $line->piece_num;
1260  $tab[] = $date_document;
1261  $tab[] = $line->doc_ref;
1262  $tab[] = preg_match('/'.$separator.'/', $line->label_operation) ? "'".$line->label_operation."'" : $line->label_operation;
1263  $tab[] = length_accountg($line->numero_compte);
1264  $tab[] = length_accounta($line->subledger_account);
1265  $tab[] = price2num($line->debit);
1266  $tab[] = price2num($line->credit);
1267  $tab[] = price2num($line->debit - $line->credit);
1268  $tab[] = $line->code_journal;
1269 
1270  $output = implode($separator, $tab).$this->end_line;
1271  if ($exportFile) {
1272  fwrite($exportFile, $output);
1273  } else {
1274  print $output;
1275  }
1276  }
1277  }
1278 
1286  public function exportFEC($objectLines, $exportFile = null)
1287  {
1288  global $langs;
1289 
1290  $separator = "\t";
1291  $end_line = "\r\n";
1292 
1293  $tab = array();
1294  $tab[] = "JournalCode";
1295  $tab[] = "JournalLib";
1296  $tab[] = "EcritureNum";
1297  $tab[] = "EcritureDate";
1298  $tab[] = "CompteNum";
1299  $tab[] = "CompteLib";
1300  $tab[] = "CompAuxNum";
1301  $tab[] = "CompAuxLib";
1302  $tab[] = "PieceRef";
1303  $tab[] = "PieceDate";
1304  $tab[] = "EcritureLib";
1305  $tab[] = "Debit";
1306  $tab[] = "Credit";
1307  $tab[] = "EcritureLet";
1308  $tab[] = "DateLet";
1309  $tab[] = "ValidDate";
1310  $tab[] = "Montantdevise";
1311  $tab[] = "Idevise";
1312  $tab[] = "DateLimitReglmt";
1313  $tab[] = "NumFacture";
1314 
1315  $output = implode($separator, $tab).$end_line;
1316  if ($exportFile) {
1317  fwrite($exportFile, $output);
1318  } else {
1319  print $output;
1320  }
1321 
1322  foreach ($objectLines as $line) {
1323  if ($line->debit == 0 && $line->credit == 0) {
1324  //unset($array[$line]);
1325  } else {
1326  $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
1327  $date_document = dol_print_date($line->doc_date, '%Y%m%d');
1328  $date_lettering = dol_print_date($line->date_lettering, '%Y%m%d');
1329  $date_validation = dol_print_date($line->date_validation, '%Y%m%d');
1330  $date_limit_payment = dol_print_date($line->date_lim_reglement, '%Y%m%d');
1331 
1332  $refInvoice = '';
1333  if ($line->doc_type == 'customer_invoice') {
1334  // Customer invoice
1335  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1336  $invoice = new Facture($this->db);
1337  $invoice->fetch($line->fk_doc);
1338 
1339  $refInvoice = $invoice->ref;
1340  } elseif ($line->doc_type == 'supplier_invoice') {
1341  // Supplier invoice
1342  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
1343  $invoice = new FactureFournisseur($this->db);
1344  $invoice->fetch($line->fk_doc);
1345 
1346  $refInvoice = $invoice->ref_supplier;
1347  }
1348 
1349  $tab = array();
1350 
1351  // FEC:JournalCode
1352  $tab[] = $line->code_journal;
1353 
1354  // FEC:JournalLib
1355  $labeljournal = dol_string_unaccent($langs->transnoentities($line->journal_label));
1356  $labeljournal = dol_string_nospecial($labeljournal, ' ');
1357  $tab[] = $labeljournal;
1358 
1359  // FEC:EcritureNum
1360  $tab[] = $line->piece_num;
1361 
1362  // FEC:EcritureDate
1363  $tab[] = $date_document;
1364 
1365  // FEC:CompteNum
1366  $tab[] = length_accountg($line->numero_compte);
1367 
1368  // FEC:CompteLib
1369  $tab[] = dol_string_unaccent($line->label_compte);
1370 
1371  // FEC:CompAuxNum
1372  $tab[] = length_accounta($line->subledger_account);
1373 
1374  // FEC:CompAuxLib
1375  $tab[] = dol_string_unaccent($line->subledger_label);
1376 
1377  // FEC:PieceRef
1378  $tab[] = $line->doc_ref;
1379 
1380  // FEC:PieceDate
1381  $tab[] = dol_string_unaccent($date_creation);
1382 
1383  // FEC:EcritureLib
1384  // Clean label operation to prevent problem on export with tab separator & other character
1385  $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
1386  $tab[] = dol_string_unaccent($line->label_operation);
1387 
1388  // FEC:Debit
1389  $tab[] = price2fec($line->debit);
1390 
1391  // FEC:Credit
1392  $tab[] = price2fec($line->credit);
1393 
1394  // FEC:EcritureLet
1395  $tab[] = $line->lettering_code;
1396 
1397  // FEC:DateLet
1398  $tab[] = $date_lettering;
1399 
1400  // FEC:ValidDate
1401  $tab[] = $date_validation;
1402 
1403  // FEC:Montantdevise
1404  $tab[] = $line->multicurrency_amount;
1405 
1406  // FEC:Idevise
1407  $tab[] = $line->multicurrency_code;
1408 
1409  // FEC_suppl:DateLimitReglmt
1410  $tab[] = $date_limit_payment;
1411 
1412  // FEC_suppl:NumFacture
1413  // Clean ref invoice to prevent problem on export with tab separator & other character
1414  $refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
1415  $tab[] = dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);
1416 
1417  $output = implode($separator, $tab).$end_line;
1418  if ($exportFile) {
1419  fwrite($exportFile, $output);
1420  } else {
1421  print $output;
1422  }
1423  }
1424  }
1425  }
1426 
1434  public function exportFEC2($objectLines, $exportFile = null)
1435  {
1436  global $langs;
1437 
1438  $separator = "\t";
1439  $end_line = "\r\n";
1440 
1441  $tab = array();
1442  $tab[] = "JournalCode";
1443  $tab[] = "JournalLib";
1444  $tab[] = "EcritureNum";
1445  $tab[] = "EcritureDate";
1446  $tab[] = "CompteNum";
1447  $tab[] = "CompteLib";
1448  $tab[] = "CompAuxNum";
1449  $tab[] = "CompAuxLib";
1450  $tab[] = "PieceRef";
1451  $tab[] = "PieceDate";
1452  $tab[] = "EcritureLib";
1453  $tab[] = "Debit";
1454  $tab[] = "Credit";
1455  $tab[] = "EcritureLet";
1456  $tab[] = "DateLet";
1457  $tab[] = "ValidDate";
1458  $tab[] = "Montantdevise";
1459  $tab[] = "Idevise";
1460  $tab[] = "DateLimitReglmt";
1461  $tab[] = "NumFacture";
1462 
1463  $output = implode($separator, $tab).$end_line;
1464  if ($exportFile) {
1465  fwrite($exportFile, $output);
1466  } else {
1467  print $output;
1468  }
1469 
1470  foreach ($objectLines as $line) {
1471  if ($line->debit == 0 && $line->credit == 0) {
1472  //unset($array[$line]);
1473  } else {
1474  $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
1475  $date_document = dol_print_date($line->doc_date, '%Y%m%d');
1476  $date_lettering = dol_print_date($line->date_lettering, '%Y%m%d');
1477  $date_validation = dol_print_date($line->date_validation, '%Y%m%d');
1478  $date_limit_payment = dol_print_date($line->date_lim_reglement, '%Y%m%d');
1479 
1480  $refInvoice = '';
1481  if ($line->doc_type == 'customer_invoice') {
1482  // Customer invoice
1483  require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1484  $invoice = new Facture($this->db);
1485  $invoice->fetch($line->fk_doc);
1486 
1487  $refInvoice = $invoice->ref;
1488  } elseif ($line->doc_type == 'supplier_invoice') {
1489  // Supplier invoice
1490  require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
1491  $invoice = new FactureFournisseur($this->db);
1492  $invoice->fetch($line->fk_doc);
1493 
1494  $refInvoice = $invoice->ref_supplier;
1495  }
1496 
1497  $tab = array();
1498 
1499  // FEC:JournalCode
1500  $tab[] = $line->code_journal;
1501 
1502  // FEC:JournalLib
1503  $labeljournal = dol_string_unaccent($langs->transnoentities($line->journal_label));
1504  $labeljournal = dol_string_nospecial($labeljournal, ' ');
1505  $tab[] = $labeljournal;
1506 
1507  // FEC:EcritureNum
1508  $tab[] = $line->piece_num;
1509 
1510  // FEC:EcritureDate
1511  $tab[] = $date_creation;
1512 
1513  // FEC:CompteNum
1514  $tab[] = length_accountg($line->numero_compte);
1515 
1516  // FEC:CompteLib
1517  $tab[] = dol_string_unaccent($line->label_compte);
1518 
1519  // FEC:CompAuxNum
1520  $tab[] = length_accounta($line->subledger_account);
1521 
1522  // FEC:CompAuxLib
1523  $tab[] = dol_string_unaccent($line->subledger_label);
1524 
1525  // FEC:PieceRef
1526  $tab[] = $line->doc_ref;
1527 
1528  // FEC:PieceDate
1529  $tab[] = $date_document;
1530 
1531  // FEC:EcritureLib
1532  // Clean label operation to prevent problem on export with tab separator & other character
1533  $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
1534  $tab[] = dol_string_unaccent($line->label_operation);
1535 
1536  // FEC:Debit
1537  $tab[] = price2fec($line->debit);
1538 
1539  // FEC:Credit
1540  $tab[] = price2fec($line->credit);
1541 
1542  // FEC:EcritureLet
1543  $tab[] = $line->lettering_code;
1544 
1545  // FEC:DateLet
1546  $tab[] = $date_lettering;
1547 
1548  // FEC:ValidDate
1549  $tab[] = $date_validation;
1550 
1551  // FEC:Montantdevise
1552  $tab[] = $line->multicurrency_amount;
1553 
1554  // FEC:Idevise
1555  $tab[] = $line->multicurrency_code;
1556 
1557  // FEC_suppl:DateLimitReglmt
1558  $tab[] = $date_limit_payment;
1559 
1560  // FEC_suppl:NumFacture
1561  // Clean ref invoice to prevent problem on export with tab separator & other character
1562  $refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
1563  $tab[] = dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);
1564 
1565  $output = implode($separator, $tab).$end_line;
1566  if ($exportFile) {
1567  fwrite($exportFile, $output);
1568  } else {
1569  print $output;
1570  }
1571  }
1572  }
1573  }
1574 
1585  public function exportSAGE50SWISS($objectLines, $exportFile = null)
1586  {
1587  // SAGE50SWISS
1588  $separator = ',';
1589  $end_line = "\r\n";
1590 
1591  // Print header line
1592  $tab = array();
1593 
1594  $tab[] = "Blg";
1595  $tab[] = "Datum";
1596  $tab[] = "Kto";
1597  $tab[] = "S/H";
1598  $tab[] = "Grp";
1599  $tab[] = "GKto";
1600  $tab[] = "SId";
1601  $tab[] = "SIdx";
1602  $tab[] = "KIdx";
1603  $tab[] = "BTyp";
1604  $tab[] = "MTyp";
1605  $tab[] = "Code";
1606  $tab[] = "Netto";
1607  $tab[] = "Steuer";
1608  $tab[] = "FW-Betrag";
1609  $tab[] = "Tx1";
1610  $tab[] = "Tx2";
1611  $tab[] = "PkKey";
1612  $tab[] = "OpId";
1613  $tab[] = "Flag";
1614 
1615  $output = implode($separator, $tab).$end_line;
1616  if ($exportFile) {
1617  fwrite($exportFile, $output);
1618  } else {
1619  print $output;
1620  }
1621 
1622  $thisPieceNum = "";
1623  $thisPieceAccountNr = "";
1624  $aSize = count($objectLines);
1625  foreach ($objectLines as $aIndex => $line) {
1626  $sammelBuchung = false;
1627  if ($aIndex - 2 >= 0 && $objectLines[$aIndex - 2]->piece_num == $line->piece_num) {
1628  $sammelBuchung = true;
1629  } elseif ($aIndex + 2 < $aSize && $objectLines[$aIndex + 2]->piece_num == $line->piece_num) {
1630  $sammelBuchung = true;
1631  } elseif ($aIndex + 1 < $aSize
1632  && $objectLines[$aIndex + 1]->piece_num == $line->piece_num
1633  && $aIndex - 1 < $aSize
1634  && $objectLines[$aIndex - 1]->piece_num == $line->piece_num
1635  ) {
1636  $sammelBuchung = true;
1637  }
1638 
1639  $tab = array();
1640 
1641  //Blg
1642  $tab[] = $line->piece_num;
1643 
1644  // Datum
1645  $date_document = dol_print_date($line->doc_date, '%d.%m.%Y');
1646  $tab[] = $date_document;
1647 
1648  // Kto
1649  $tab[] = length_accountg($line->numero_compte);
1650  // S/H
1651  if ($line->sens == 'D') {
1652  $tab[] = 'S';
1653  } else {
1654  $tab[] = 'H';
1655  }
1656  // Grp
1657  $tab[] = self::trunc($line->code_journal, 1);
1658  // GKto
1659  if (empty($line->code_tiers)) {
1660  if ($line->piece_num == $thisPieceNum) {
1661  $tab[] = length_accounta($thisPieceAccountNr);
1662  } else {
1663  $tab[] = "div";
1664  }
1665  } else {
1666  $tab[] = length_accounta($line->code_tiers);
1667  }
1668  // SId
1669  $tab[] = $this->separator;
1670  // SIdx
1671  $tab[] = "0";
1672  // KIdx
1673  $tab[] = "0";
1674  // BTyp
1675  $tab[] = "0";
1676 
1677  // MTyp 1=Fibu Einzelbuchung 2=Sammebuchung
1678  if ($sammelBuchung) {
1679  $tab[] = "2";
1680  } else {
1681  $tab[] = "1";
1682  }
1683  // Code
1684  $tab[] = '""';
1685  // Netto
1686  $tab[] = abs($line->debit - $line->credit);
1687  // Steuer
1688  $tab[] = "0.00";
1689  // FW-Betrag
1690  $tab[] = "0.00";
1691  // Tx1
1692  $line1 = self::toAnsi($line->label_compte, 29);
1693  if ($line1 == "LIQ" || $line1 == "LIQ Beleg ok" || strlen($line1) <= 3) {
1694  $line1 = "";
1695  }
1696  $line2 = self::toAnsi($line->doc_ref, 29);
1697  if (strlen($line1) == 0) {
1698  $line1 = $line2;
1699  $line2 = "";
1700  }
1701  if (strlen($line1) > 0 && strlen($line2) > 0 && (strlen($line1) + strlen($line2)) < 27) {
1702  $line1 = $line1.' / '.$line2;
1703  $line2 = "";
1704  }
1705 
1706  $tab[] = '"'.self::toAnsi($line1).'"';
1707  // Tx2
1708  $tab[] = '"'.self::toAnsi($line2).'"';
1709  //PkKey
1710  $tab[] = "0";
1711  //OpId
1712  $tab[] = $this->separator;
1713 
1714  // Flag
1715  $tab[] = "0";
1716 
1717  $output = implode($separator, $tab).$end_line;
1718  if ($exportFile) {
1719  fwrite($exportFile, $output);
1720  } else {
1721  print $output;
1722  }
1723 
1724  if ($line->piece_num !== $thisPieceNum) {
1725  $thisPieceNum = $line->piece_num;
1726  $thisPieceAccountNr = $line->numero_compte;
1727  }
1728  }
1729  }
1730 
1739  public function exportLDCompta($objectLines, $exportFile = null)
1740  {
1741 
1742  $separator = ';';
1743  $end_line = "\r\n";
1744 
1745  foreach ($objectLines as $line) {
1746  $date_document = dol_print_date($line->doc_date, '%Y%m%d');
1747  $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
1748  $date_lim_reglement = dol_print_date($line->date_lim_reglement, '%Y%m%d');
1749 
1750  $tab = array();
1751 
1752  // TYPE
1753  $type_enregistrement = 'E'; // For write movement
1754  $tab[] = $type_enregistrement;
1755  // JNAL
1756  $tab[] = substr($line->code_journal, 0, 2);
1757  // NECR
1758  $tab[] = $line->id;
1759  // NPIE
1760  $tab[] = $line->piece_num;
1761  // DATP
1762  $tab[] = $date_document;
1763  // LIBE
1764  $tab[] = $line->label_operation;
1765  // DATH
1766  $tab[] = $date_lim_reglement;
1767  // CNPI
1768  if ($line->doc_type == 'supplier_invoice') {
1769  if (($line->debit - $line->credit) > 0) {
1770  $nature_piece = 'AF';
1771  } else {
1772  $nature_piece = 'FF';
1773  }
1774  } elseif ($line->doc_type == 'customer_invoice') {
1775  if (($line->debit - $line->credit) < 0) {
1776  $nature_piece = 'AC';
1777  } else {
1778  $nature_piece = 'FC';
1779  }
1780  } else {
1781  $nature_piece = '';
1782  }
1783  $tab[] = $nature_piece;
1784  // RACI
1785  // if (!empty($line->subledger_account)) {
1786  // if ($line->doc_type == 'supplier_invoice') {
1787  // $racine_subledger_account = '40';
1788  // } elseif ($line->doc_type == 'customer_invoice') {
1789  // $racine_subledger_account = '41';
1790  // } else {
1791  // $racine_subledger_account = '';
1792  // }
1793  // } else {
1794  $racine_subledger_account = ''; // for records of type E leave this field blank
1795  // }
1796 
1797  $tab[] = $racine_subledger_account; // deprecated CPTG & CPTA use instead
1798  // MONT
1799  $tab[] = price(abs($line->debit - $line->credit), 0, '', 1, 2, 2);
1800  // CODC
1801  $tab[] = $line->sens;
1802  // CPTG
1803  $tab[] = length_accountg($line->numero_compte);
1804  // DATE
1805  $tab[] = $date_creation;
1806  // CLET
1807  $tab[] = $line->lettering_code;
1808  // DATL
1809  $tab[] = $line->date_lettering;
1810  // CPTA
1811  if (!empty($line->subledger_account)) {
1812  $tab[] = length_accounta($line->subledger_account);
1813  } else {
1814  $tab[] = "";
1815  }
1816  // CNAT
1817  if ($line->doc_type == 'supplier_invoice' && !empty($line->subledger_account)) {
1818  $tab[] = 'F';
1819  } elseif ($line->doc_type == 'customer_invoice' && !empty($line->subledger_account)) {
1820  $tab[] = 'C';
1821  } else {
1822  $tab[] = "";
1823  }
1824  // SECT
1825  $tab[] = "";
1826  // CTRE
1827  $tab[] = "";
1828  // NORL
1829  $tab[] = "";
1830  // DATV
1831  $tab[] = "";
1832  // REFD
1833  $tab[] = $line->doc_ref;
1834  // CODH
1835  $tab[] = "";
1836  // NSEQ
1837  $tab[] = "";
1838  // MTDV
1839  $tab[] = '0';
1840  // CODV
1841  $tab[] = "";
1842  // TXDV
1843  $tab[] = '0';
1844  // MOPM
1845  $tab[] = "";
1846  // BONP
1847  $tab[] = "";
1848  // BQAF
1849  $tab[] = "";
1850  // ECES
1851  $tab[] = "";
1852  // TXTL
1853  $tab[] = "";
1854  // ECRM
1855  $tab[] = "";
1856  // DATK
1857  $tab[] = "";
1858  // HEUK
1859  $tab[] = "";
1860 
1861  $output = implode($separator, $tab).$end_line;
1862  if ($exportFile) {
1863  fwrite($exportFile, $output);
1864  } else {
1865  print $output;
1866  }
1867  }
1868  }
1869 
1880  public function exportLDCompta10($objectLines, $exportFile = null)
1881  {
1882  require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
1883 
1884  $separator = ';';
1885  $end_line = "\r\n";
1886  $last_codeinvoice = '';
1887 
1888  foreach ($objectLines as $line) {
1889  // TYPE C
1890  if ($last_codeinvoice != $line->doc_ref) {
1891  //recherche societe en fonction de son code client
1892  $sql = "SELECT code_client, fk_forme_juridique, nom, address, zip, town, fk_pays, phone, siret FROM ".MAIN_DB_PREFIX."societe";
1893  $sql .= " WHERE code_client = '".$this->db->escape($line->thirdparty_code)."'";
1894  $resql = $this->db->query($sql);
1895 
1896  if ($resql && $this->db->num_rows($resql) > 0) {
1897  $soc = $this->db->fetch_object($resql);
1898 
1899  $address = array('', '', '');
1900  if (strpos($soc->address, "\n") !== false) {
1901  $address = explode("\n", $soc->address);
1902  if (is_array($address) && count($address) > 0) {
1903  foreach ($address as $key => $data) {
1904  $address[$key] = str_replace(array("\t", "\n", "\r"), "", $data);
1905  $address[$key] = dol_trunc($address[$key], 40, 'right', 'UTF-8', 1);
1906  }
1907  }
1908  } else {
1909  $address[0] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 0, 40);
1910  $address[1] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 41, 40);
1911  $address[2] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 82, 40);
1912  }
1913 
1914  $tab = array();
1915 
1916  $type_enregistrement = 'C';
1917  //TYPE
1918  $tab[] = $type_enregistrement;
1919  //NOCL
1920  $tab[] = $soc->code_client;
1921  //NMCM
1922  $tab[] = "";
1923  //LIBI
1924  $tab[] = "";
1925  //TITR
1926  $tab[] = "";
1927  //RSSO
1928  $tab[] = $soc->nom;
1929  //CAD1
1930  $tab[] = $address[0];
1931  //CAD2
1932  $tab[] = $address[1];
1933  //CAD3
1934  $tab[] = $address[2];
1935  //COPO
1936  $tab[] = $soc->zip;
1937  //BUDI
1938  $tab[] = substr($soc->town, 0, 40);
1939  //CPAY
1940  $tab[] = "";
1941  //PAYS
1942  $tab[] = substr(getCountry($soc->fk_pays), 0, 40);
1943  //NTEL
1944  $tab[] = $soc->phone;
1945  //TLEX
1946  $tab[] = "";
1947  //TLPO
1948  $tab[] = "";
1949  //TLCY
1950  $tab[] = "";
1951  //NINT
1952  $tab[] = "";
1953  //COMM
1954  $tab[] = "";
1955  //SIRE
1956  $tab[] = str_replace(" ", "", $soc->siret);
1957  //RIBP
1958  $tab[] = "";
1959  //DOBQ
1960  $tab[] = "";
1961  //IBBQ
1962  $tab[] = "";
1963  //COBQ
1964  $tab[] = "";
1965  //GUBQ
1966  $tab[] = "";
1967  //CPBQ
1968  $tab[] = "";
1969  //CLBQ
1970  $tab[] = "";
1971  //BIBQ
1972  $tab[] = "";
1973  //MOPM
1974  $tab[] = "";
1975  //DJPM
1976  $tab[] = "";
1977  //DMPM
1978  $tab[] = "";
1979  //REFM
1980  $tab[] = "";
1981  //SLVA
1982  $tab[] = "";
1983  //PLCR
1984  $tab[] = "";
1985  //ECFI
1986  $tab[] = "";
1987  //CREP
1988  $tab[] = "";
1989  //NREP
1990  $tab[] = "";
1991  //TREP
1992  $tab[] = "";
1993  //MREP
1994  $tab[] = "";
1995  //GRRE
1996  $tab[] = "";
1997  //LTTA
1998  $tab[] = "";
1999  //CACT
2000  $tab[] = "";
2001  //CODV
2002  $tab[] = "";
2003  //GRTR
2004  $tab[] = "";
2005  //NOFP
2006  $tab[] = "";
2007  //BQAF
2008  $tab[] = "";
2009  //BONP
2010  $tab[] = "";
2011  //CESC
2012  $tab[] = "";
2013 
2014  $output = implode($separator, $tab).$end_line;
2015  if ($exportFile) {
2016  fwrite($exportFile, $output);
2017  } else {
2018  print $output;
2019  }
2020  }
2021  }
2022 
2023  $tab = array();
2024 
2025  $date_document = dol_print_date($line->doc_date, '%Y%m%d');
2026  $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
2027  $date_lim_reglement = dol_print_date($line->date_lim_reglement, '%Y%m%d');
2028 
2029  // TYPE E
2030  $type_enregistrement = 'E'; // For write movement
2031  $tab[] = $type_enregistrement;
2032  // JNAL
2033  $tab[] = substr($line->code_journal, 0, 2);
2034  // NECR
2035  $tab[] = $line->id;
2036  // NPIE
2037  $tab[] = $line->piece_num;
2038  // DATP
2039  $tab[] = $date_document;
2040  // LIBE
2041  $tab[] = dol_trunc($line->label_operation, 25, 'right', 'UTF-8', 1);
2042  // DATH
2043  $tab[] = $date_lim_reglement;
2044  // CNPI
2045  if ($line->doc_type == 'supplier_invoice') {
2046  if (($line->amount) < 0) { // Currently, only the sign of amount allows to know the type of invoice (standard or credit note). Other solution is to analyse debit/credit/role of account. TODO Add column doc_type_long or make amount mandatory with rule on sign.
2047  $nature_piece = 'AF';
2048  } else {
2049  $nature_piece = 'FF';
2050  }
2051  } elseif ($line->doc_type == 'customer_invoice') {
2052  if (($line->amount) < 0) {
2053  $nature_piece = 'AC'; // Currently, only the sign of amount allows to know the type of invoice (standard or credit note). Other solution is to analyse debit/credit/role of account. TODO Add column doc_type_long or make amount mandatory with rule on sign.
2054  } else {
2055  $nature_piece = 'FC';
2056  }
2057  } else {
2058  $nature_piece = '';
2059  }
2060  $tab[] = $nature_piece;
2061  // RACI
2062  // if (!empty($line->subledger_account)) {
2063  // if ($line->doc_type == 'supplier_invoice') {
2064  // $racine_subledger_account = '40';
2065  // } elseif ($line->doc_type == 'customer_invoice') {
2066  // $racine_subledger_account = '41';
2067  // } else {
2068  // $racine_subledger_account = '';
2069  // }
2070  // } else {
2071  $racine_subledger_account = ''; // for records of type E leave this field blank
2072  // }
2073 
2074  $tab[] = $racine_subledger_account; // deprecated CPTG & CPTA use instead
2075  // MONT
2076  $tab[] = price(abs($line->debit - $line->credit), 0, '', 1, 2);
2077  // CODC
2078  $tab[] = $line->sens;
2079  // CPTG
2080  $tab[] = length_accountg($line->numero_compte);
2081  // DATE
2082  $tab[] = $date_document;
2083  // CLET
2084  $tab[] = $line->lettering_code;
2085  // DATL
2086  $tab[] = $line->date_lettering;
2087  // CPTA
2088  if (!empty($line->subledger_account)) {
2089  $tab[] = length_accounta($line->subledger_account);
2090  } else {
2091  $tab[] = "";
2092  }
2093  // CNAT
2094  if ($line->doc_type == 'supplier_invoice' && !empty($line->subledger_account)) {
2095  $tab[] = 'F';
2096  } elseif ($line->doc_type == 'customer_invoice' && !empty($line->subledger_account)) {
2097  $tab[] = 'C';
2098  } else {
2099  $tab[] = "";
2100  }
2101  // CTRE
2102  $tab[] = "";
2103  // NORL
2104  $tab[] = "";
2105  // DATV
2106  $tab[] = "";
2107  // REFD
2108  $tab[] = $line->doc_ref;
2109  // NECA
2110  $tab[] = '0';
2111  // CSEC
2112  $tab[] = "";
2113  // CAFF
2114  $tab[] = "";
2115  // CDES
2116  $tab[] = "";
2117  // QTUE
2118  $tab[] = "";
2119  // MTDV
2120  $tab[] = '0';
2121  // CODV
2122  $tab[] = "";
2123  // TXDV
2124  $tab[] = '0';
2125  // MOPM
2126  $tab[] = "";
2127  // BONP
2128  $tab[] = "";
2129  // BQAF
2130  $tab[] = "";
2131  // ECES
2132  $tab[] = "";
2133  // TXTL
2134  $tab[] = "";
2135  // ECRM
2136  $tab[] = "";
2137  // DATK
2138  $tab[] = "";
2139  // HEUK
2140  $tab[] = "";
2141 
2142  $output = implode($separator, $tab).$end_line;
2143  if ($exportFile) {
2144  fwrite($exportFile, $output);
2145  } else {
2146  print $output;
2147  }
2148 
2149  $last_codeinvoice = $line->doc_ref;
2150  }
2151  }
2152 
2160  public function exportCharlemagne($objectLines, $exportFile = null)
2161  {
2162  global $langs;
2163  $langs->load('compta');
2164 
2165  $separator = "\t";
2166  $end_line = "\n";
2167 
2168  $tab = array();
2169 
2170  $tab[] = $langs->transnoentitiesnoconv('Date');
2171  $tab[] = self::trunc($langs->transnoentitiesnoconv('Journal'), 6);
2172  $tab[] = self::trunc($langs->transnoentitiesnoconv('Account'), 15);
2173  $tab[] = self::trunc($langs->transnoentitiesnoconv('LabelAccount'), 60);
2174  $tab[] = self::trunc($langs->transnoentitiesnoconv('Piece'), 20);
2175  $tab[] = self::trunc($langs->transnoentitiesnoconv('LabelOperation'), 60);
2176  $tab[] = $langs->transnoentitiesnoconv('Amount');
2177  $tab[] = 'S';
2178  $tab[] = self::trunc($langs->transnoentitiesnoconv('Analytic').' 1', 15);
2179  $tab[] = self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 1', 60);
2180  $tab[] = self::trunc($langs->transnoentitiesnoconv('Analytic').' 2', 15);
2181  $tab[] = self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 2', 60);
2182  $tab[] = self::trunc($langs->transnoentitiesnoconv('Analytic').' 3', 15);
2183  $tab[] = self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 3', 60);
2184 
2185  $output = implode($separator, $tab).$end_line;
2186  if ($exportFile) {
2187  fwrite($exportFile, $output);
2188  } else {
2189  print $output;
2190  }
2191 
2192  foreach ($objectLines as $line) {
2193  $date_document = dol_print_date($line->doc_date, '%Y%m%d');
2194 
2195  $tab = array();
2196 
2197  $tab[] = $date_document; //Date
2198 
2199  $tab[] = self::trunc($line->code_journal, 6); //Journal code
2200 
2201  if (!empty($line->subledger_account)) {
2202  $account = $line->subledger_account;
2203  } else {
2204  $account = $line->numero_compte;
2205  }
2206  $tab[] = self::trunc($account, 15); //Account number
2207 
2208  $tab[] = self::trunc($line->label_compte, 60); //Account label
2209  $tab[] = self::trunc($line->doc_ref, 20); //Piece
2210  // Clean label operation to prevent problem on export with tab separator & other character
2211  $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
2212  $tab[] = self::trunc($line->label_operation, 60); //Operation label
2213  $tab[] = price(abs($line->debit - $line->credit)); //Amount
2214  $tab[] = $line->sens; //Direction
2215  $tab[] = ""; //Analytic
2216  $tab[] = ""; //Analytic
2217  $tab[] = ""; //Analytic
2218  $tab[] = ""; //Analytic
2219  $tab[] = ""; //Analytic
2220  $tab[] = ""; //Analytic
2221 
2222  $output = implode($separator, $tab).$end_line;
2223  if ($exportFile) {
2224  fwrite($exportFile, $output);
2225  } else {
2226  print $output;
2227  }
2228  }
2229  }
2230 
2238  public function exportGestimumV3($objectLines, $exportFile = null)
2239  {
2240  global $langs;
2241 
2242  $separator = ',';
2243  $end_line = "\r\n";
2244 
2245  $invoices_infos = array();
2246  $supplier_invoices_infos = array();
2247  foreach ($objectLines as $line) {
2248  if ($line->debit == 0 && $line->credit == 0) {
2249  //unset($array[$line]);
2250  } else {
2251  $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
2252  $date_echeance = dol_print_date($line->date_lim_reglement, '%Y%m%d');
2253 
2254  $invoice_ref = $line->doc_ref;
2255  $company_name = "";
2256 
2257  if (($line->doc_type == 'customer_invoice' || $line->doc_type == 'supplier_invoice') && $line->fk_doc > 0) {
2258  if (($line->doc_type == 'customer_invoice' && !isset($invoices_infos[$line->fk_doc])) ||
2259  ($line->doc_type == 'supplier_invoice' && !isset($supplier_invoices_infos[$line->fk_doc]))) {
2260  if ($line->doc_type == 'customer_invoice') {
2261  // Get new customer invoice ref and company name
2262  $sql = 'SELECT f.ref, s.nom FROM ' . MAIN_DB_PREFIX . 'facture as f';
2263  $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'societe AS s ON f.fk_soc = s.rowid';
2264  $sql .= ' WHERE f.rowid = '.((int) $line->fk_doc);
2265  $resql = $this->db->query($sql);
2266  if ($resql) {
2267  if ($obj = $this->db->fetch_object($resql)) {
2268  // Save invoice infos
2269  $invoices_infos[$line->fk_doc] = array('ref' => $obj->ref, 'company_name' => $obj->nom);
2270  $invoice_ref = $obj->ref;
2271  $company_name = $obj->nom;
2272  }
2273  }
2274  } else {
2275  // Get new supplier invoice ref and company name
2276  $sql = 'SELECT ff.ref, s.nom FROM ' . MAIN_DB_PREFIX . 'facture_fourn as ff';
2277  $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'societe AS s ON ff.fk_soc = s.rowid';
2278  $sql .= ' WHERE ff.rowid = '.((int) $line->fk_doc);
2279  $resql = $this->db->query($sql);
2280  if ($resql) {
2281  if ($obj = $this->db->fetch_object($resql)) {
2282  // Save invoice infos
2283  $supplier_invoices_infos[$line->fk_doc] = array('ref' => $obj->ref, 'company_name' => $obj->nom);
2284  $invoice_ref = $obj->ref;
2285  $company_name = $obj->nom;
2286  }
2287  }
2288  }
2289  } elseif ($line->doc_type == 'customer_invoice') {
2290  // Retrieve invoice infos
2291  $invoice_ref = $invoices_infos[$line->fk_doc]['ref'];
2292  $company_name = $invoices_infos[$line->fk_doc]['company_name'];
2293  } else {
2294  // Retrieve invoice infos
2295  $invoice_ref = $supplier_invoices_infos[$line->fk_doc]['ref'];
2296  $company_name = $supplier_invoices_infos[$line->fk_doc]['company_name'];
2297  }
2298  }
2299 
2300  $tab = array();
2301 
2302  $tab[] = $line->id;
2303  $tab[] = $date_document;
2304  $tab[] = substr($line->code_journal, 0, 4);
2305 
2306  if ((substr($line->numero_compte, 0, 3) == '411') || (substr($line->numero_compte, 0, 3) == '401')) {
2307  $tab[] = length_accountg($line->subledger_account);
2308  } else {
2309  $tab[] = substr(length_accountg($line->numero_compte), 0, 15);
2310  }
2311  //Libellé Auto
2312  $tab[] = "";
2313  //print '"'.dol_trunc(str_replace('"', '', $line->label_operation),40,'right','UTF-8',1).'"';
2314  //Libellé manuel
2315  $tab[] = dol_trunc(str_replace('"', '', $invoice_ref . (!empty($company_name) ? ' - ' : '') . $company_name), 40, 'right', 'UTF-8', 1);
2316  //Numéro de pièce
2317  $tab[] = dol_trunc(str_replace('"', '', $line->piece_num), 10, 'right', 'UTF-8', 1);
2318  //Devise
2319  $tab[] = 'EUR';
2320  //Amount
2321  $tab[] = price2num(abs($line->debit - $line->credit));
2322  //Sens
2323  $tab[] = $line->sens;
2324  //Code lettrage
2325  $tab[] = "";
2326  //Date Echéance
2327  $tab[] = $date_echeance;
2328 
2329  $output = implode($separator, $tab).$end_line;
2330  if ($exportFile) {
2331  fwrite($exportFile, $output);
2332  } else {
2333  print $output;
2334  }
2335  }
2336  }
2337  }
2338 
2346  public function exportGestimumV5($objectLines, $exportFile = null)
2347  {
2348 
2349  $separator = ',';
2350  $end_line = "\r\n";
2351 
2352  foreach ($objectLines as $line) {
2353  if ($line->debit == 0 && $line->credit == 0) {
2354  //unset($array[$line]);
2355  } else {
2356  $date_document = dol_print_date($line->doc_date, '%d%m%Y');
2357 
2358  $tab = array();
2359 
2360  $tab[] = $line->id;
2361  $tab[] = $date_document;
2362  $tab[] = substr($line->code_journal, 0, 4);
2363  if ((substr($line->numero_compte, 0, 3) == '411') || (substr($line->numero_compte, 0, 3) == '401')) { // TODO No hard code value
2364  $tab[] = length_accountg($line->subledger_account);
2365  } else {
2366  $tab[] = substr(length_accountg($line->numero_compte), 0, 15);
2367  }
2368  $tab[] = "";
2369  $tab[] = '"'.dol_trunc(str_replace('"', '', $line->label_operation), 40, 'right', 'UTF-8', 1).'"';
2370  $tab[] = '"' . dol_trunc(str_replace('"', '', $line->doc_ref), 40, 'right', 'UTF-8', 1) . '"';
2371  $tab[] = '"' . dol_trunc(str_replace('"', '', $line->piece_num), 10, 'right', 'UTF-8', 1) . '"';
2372  $tab[] = price2num(abs($line->debit - $line->credit));
2373  $tab[] = $line->sens;
2374  $tab[] = $date_document;
2375  $tab[] = "";
2376  $tab[] = "";
2377  $tab[] = 'EUR';
2378 
2379  $output = implode($separator, $tab).$end_line;
2380  if ($exportFile) {
2381  fwrite($exportFile, $output);
2382  } else {
2383  print $output;
2384  }
2385  }
2386  }
2387  }
2388 
2398  public function exportiSuiteExpert($objectLines, $exportFile = null)
2399  {
2400  $separator = ';';
2401  $end_line = "\r\n";
2402 
2403 
2404  foreach ($objectLines as $line) {
2405  $tab = array();
2406 
2407  $date = dol_print_date($line->doc_date, '%d/%m/%Y');
2408 
2409  $tab[] = $line->piece_num;
2410  $tab[] = $date;
2411  $tab[] = substr($date, 6, 4);
2412  $tab[] = substr($date, 3, 2);
2413  $tab[] = substr($date, 0, 2);
2414  $tab[] = $line->doc_ref;
2415  //Conversion de chaine UTF8 en Latin9
2416  $tab[] = mb_convert_encoding(str_replace(' - Compte auxiliaire', '', $line->label_operation), "Windows-1252", 'UTF-8');
2417 
2418  //Calcul de la longueur des numéros de comptes
2419  $taille_numero = strlen(length_accountg($line->numero_compte));
2420 
2421  //Création du numéro de client et fournisseur générique
2422  $numero_cpt_client = '411';
2423  $numero_cpt_fourn = '401';
2424  for ($i = 1; $i <= ($taille_numero - 3); $i++) {
2425  $numero_cpt_client .= '0';
2426  $numero_cpt_fourn .= '0';
2427  }
2428 
2429  //Création des comptes auxiliaire des clients et fournisseur
2430  if (length_accountg($line->numero_compte) == $numero_cpt_client || length_accountg($line->numero_compte) == $numero_cpt_fourn) {
2431  $tab[] = rtrim(length_accounta($line->subledger_account), "0");
2432  } else {
2433  $tab[] = length_accountg($line->numero_compte);
2434  }
2435  $nom_client = explode(" - ", $line->label_operation);
2436  $tab[] = mb_convert_encoding($nom_client[0], "Windows-1252", 'UTF-8');
2437  $tab[] = price($line->debit);
2438  $tab[] = price($line->credit);
2439  $tab[] = price($line->montant);
2440  $tab[] = $line->code_journal;
2441 
2442  $output = implode($separator, $tab).$end_line;
2443  if ($exportFile) {
2444  fwrite($exportFile, $output);
2445  } else {
2446  print $output;
2447  }
2448  }
2449  }
2450 
2458  public static function trunc($str, $size)
2459  {
2460  return dol_trunc($str, $size, 'right', 'UTF-8', 1);
2461  }
2462 
2470  public static function toAnsi($str, $size = -1)
2471  {
2472  $retVal = dol_string_nohtmltag($str, 1, 'Windows-1251');
2473  if ($retVal >= 0 && $size >= 0) {
2474  $retVal = dol_substr($retVal, 0, $size, 'Windows-1251');
2475  }
2476  return $retVal;
2477  }
2478 }
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
length_accounta($accounta)
Return Auxiliary accounting account of thirdparties with defined length.
Manage the different format accountancy export.
exportCogilog($objectLines, $exportFile=null)
Export format : COGILOG Last review for this format : 2022-07-12 Alexandre Spangaro (aspangaro@open-d...
exportAgiris($objectLines, $exportFile=null)
Export format : Agiris Isacompta.
exportFEC2($objectLines, $exportFile=null)
Export format : FEC2.
exportQuadratus($objectLines, $exportFile=null, $archiveFileList=array(), $withAttachment=0)
Export format : Quadratus (Format ASCII) Format since 2015 compatible QuadraCOMPTA Last review for th...
exportWinfic($objectLines, $exportFile=null)
Export format : WinFic - eWinfic - WinSis Compta Last review for this format : 2022-11-01 Alexandre S...
exportSAGE50SWISS($objectLines, $exportFile=null)
Export format : SAGE50SWISS.
exportOpenConcerto($objectLines, $exportFile=null)
Export format : OpenConcerto.
exportEbp($objectLines, $exportFile=null)
Export format : EBP.
exportLDCompta10($objectLines, $exportFile=null)
Export format : LD Compta version 10 & higher Last review for this format : 08-15-2021 Alexandre Span...
exportFEC($objectLines, $exportFile=null)
Export format : FEC.
exportCharlemagne($objectLines, $exportFile=null)
Export format : Charlemagne.
getTypeConfig()
Array with all export type available (key + label) and parameters for config.
exportLDCompta($objectLines, $exportFile=null)
Export format : LD Compta version 9 http://www.ldsysteme.fr/fileadmin/telechargement/np/ldcompta/Docu...
exportBob50($objectLines, $exportFile=null)
Export format : BOB50.
export(&$TData, $formatexportset, $withAttachment=0, $downloadMode=0, $outputMode=0)
Function who chose which export to use with the default config, and make the export into a file.
exportGestimumV3($objectLines, $exportFile=null)
Export format : Gestimum V3.
static trunc($str, $size)
trunc
exportiSuiteExpert($objectLines, $exportFile=null)
Export format : iSuite Expert.
exportCiel($objectLines, $exportFile=null)
Export format : CIEL (Format XIMPORT) Format since 2003 compatible CIEL version > 2002 / Sage50 Last ...
static toAnsi($str, $size=-1)
toAnsi
getType()
Array with all export type available (key + label)
exportConfigurable($objectLines, $exportFile=null)
Export format : Configurable CSV.
static getFormatCode($type)
Return string to summarize the format (Used to generated export filename)
__construct(DoliDB $db)
Constructor.
exportCegid($objectLines, $exportFile=null)
Export format : CEGID.
exportCoala($objectLines, $exportFile=null)
Export format : COALA.
getMimeType($formatexportset)
Return the MIME type of a file.
exportGestimumV5($objectLines, $exportFile=null)
Export format : Gestimum V5.
Class to manage Dolibarr database access.
Class to manage suppliers invoices.
Class to manage invoices.
getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_filesize($pathoffile)
Return size of a file.
Definition: files.lib.php:587
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:62
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:453
price2fec($amount)
Function to format a value into a defined format for French administration (no thousand separator & d...
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
readfileLowMemory($fullpath_original_file_osencoded, $method=-1)
Return a file on output using a low memory.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
if(!defined('NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1494