dolibarr 25.0.0-alpha
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-2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
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-2026 Frédéric France <frederic.france@free.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 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 */
32
39require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
42require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
43require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
44
49{
50 // Types of export.
52 public static $EXPORT_TYPE_CONFIGURABLE = 1; // CSV
54 public static $EXPORT_TYPE_AGIRIS = 10;
56 public static $EXPORT_TYPE_EBP = 15;
58 public static $EXPORT_TYPE_CEGID = 20;
60 public static $EXPORT_TYPE_COGILOG = 25;
62 public static $EXPORT_TYPE_COALA = 30;
64 public static $EXPORT_TYPE_BOB50 = 35;
66 public static $EXPORT_TYPE_CIEL = 40;
68 public static $EXPORT_TYPE_SAGE50_SWISS = 45;
70 public static $EXPORT_TYPE_CHARLEMAGNE = 50;
72 public static $EXPORT_TYPE_QUADRATUS = 60;
74 public static $EXPORT_TYPE_WINFIC = 70;
76 public static $EXPORT_TYPE_OPENCONCERTO = 100;
78 public static $EXPORT_TYPE_LDCOMPTA = 110;
80 public static $EXPORT_TYPE_LDCOMPTA10 = 120;
82 public static $EXPORT_TYPE_GESTIMUMV3 = 130;
84 public static $EXPORT_TYPE_GESTIMUMV5 = 135;
86 public static $EXPORT_TYPE_ISUITEEXPERT = 200;
88 public static $EXPORT_TYPE_ISTEA = 205;
89 // Generic FEC after that
91 public static $EXPORT_TYPE_FEC = 1000;
93 public static $EXPORT_TYPE_FEC2 = 1010;
94
98 public $db;
99
103 public $errors = array();
104
108 public $separator = '';
109
113 public $end_line = '';
114
118 public $generatedfiledata = array();
119
120
126 public function __construct(DoliDB $db)
127 {
128 global $hookmanager;
129
130 $this->db = $db;
131 $this->separator = getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV');
132 $this->end_line = getDolGlobalString('ACCOUNTING_EXPORT_ENDLINE') ? (getDolGlobalInt('ACCOUNTING_EXPORT_ENDLINE') == 1 ? "\n" : "\r\n") : "\n";
133
134 $hookmanager->initHooks(array('accountancyexport'));
135 }
136
143 public function getType($mode = 0)
144 {
145 global $langs, $hookmanager;
146
147 $listofspecialformatexport = array(
148 self::$EXPORT_TYPE_CEGID => $langs->trans('Modelcsv_CEGID'),
149 self::$EXPORT_TYPE_COALA => $langs->trans('Modelcsv_COALA'),
150 self::$EXPORT_TYPE_BOB50 => $langs->trans('Modelcsv_bob50'),
151 self::$EXPORT_TYPE_CIEL => $langs->trans('Modelcsv_ciel'),
152 self::$EXPORT_TYPE_QUADRATUS => $langs->trans('Modelcsv_quadratus'),
153 self::$EXPORT_TYPE_WINFIC => $langs->trans('Modelcsv_winfic'),
154 self::$EXPORT_TYPE_EBP => $langs->trans('Modelcsv_ebp'),
155 self::$EXPORT_TYPE_COGILOG => $langs->trans('Modelcsv_cogilog'),
156 self::$EXPORT_TYPE_AGIRIS => $langs->trans('Modelcsv_agiris'),
157 self::$EXPORT_TYPE_OPENCONCERTO => $langs->trans('Modelcsv_openconcerto'),
158 self::$EXPORT_TYPE_SAGE50_SWISS => $langs->trans('Modelcsv_Sage50_Swiss'),
159 self::$EXPORT_TYPE_CHARLEMAGNE => $langs->trans('Modelcsv_charlemagne'),
160 self::$EXPORT_TYPE_LDCOMPTA => $langs->trans('Modelcsv_LDCompta'),
161 self::$EXPORT_TYPE_LDCOMPTA10 => $langs->trans('Modelcsv_LDCompta10'),
162 self::$EXPORT_TYPE_GESTIMUMV3 => $langs->trans('Modelcsv_Gestinumv3'),
163 self::$EXPORT_TYPE_GESTIMUMV5 => $langs->trans('Modelcsv_Gestinumv5'),
164 self::$EXPORT_TYPE_ISUITEEXPERT => 'Export iSuite Expert',
165 self::$EXPORT_TYPE_ISTEA => $langs->trans('Modelcsv_ISTEA'),
166 );
167
168 $listofgenericformatexport = array(
169 self::$EXPORT_TYPE_CONFIGURABLE => $langs->trans('Modelcsv_configurable'),
170 self::$EXPORT_TYPE_FEC => $langs->trans('Modelcsv_FEC'),
171 self::$EXPORT_TYPE_FEC2 => $langs->trans('Modelcsv_FEC2'),
172 );
173
174 if (empty($mode)) {
175 $listofexporttypes = $listofgenericformatexport + $listofspecialformatexport;
176 ksort($listofexporttypes, SORT_NUMERIC);
177 } else {
178 ksort($listofspecialformatexport, SORT_NUMERIC);
179 $listofexporttypes = array();
180 $i = 0;
181 foreach ($listofgenericformatexport as $key => $val) {
182 $i++;
183 $listofexporttypes[$key] = array('id' => $key, 'label' => $val, 'position' => $i);
184 }
185 $listofexporttypes['separator_'.$i] = array('id' => 0, 'label' => '----------------', 'position' => $i, 'disabled' => 'disabled');
186 foreach ($listofspecialformatexport as $key => $val) {
187 $i++;
188 $listofexporttypes[$key] = array('id' => $key, 'label' => $val, 'position' => $i);
189 }
190 }
191
192 // allow modules to define export formats
193 $parameters = array();
194 $reshook = $hookmanager->executeHooks('getType', $parameters, $listofexporttypes);
195
196 return $listofexporttypes;
197 }
198
205 public static function getFormatCode($type)
206 {
207 $formatcode = array(
208 self::$EXPORT_TYPE_CONFIGURABLE => 'csv',
209 self::$EXPORT_TYPE_CEGID => 'cegid',
210 self::$EXPORT_TYPE_COALA => 'coala',
211 self::$EXPORT_TYPE_BOB50 => 'bob50',
212 self::$EXPORT_TYPE_CIEL => 'ciel',
213 self::$EXPORT_TYPE_QUADRATUS => 'quadratus',
214 self::$EXPORT_TYPE_WINFIC => 'winfic',
215 self::$EXPORT_TYPE_EBP => 'ebp',
216 self::$EXPORT_TYPE_COGILOG => 'cogilog',
217 self::$EXPORT_TYPE_AGIRIS => 'agiris',
218 self::$EXPORT_TYPE_OPENCONCERTO => 'openconcerto',
219 self::$EXPORT_TYPE_SAGE50_SWISS => 'sage50ch',
220 self::$EXPORT_TYPE_CHARLEMAGNE => 'charlemagne',
221 self::$EXPORT_TYPE_LDCOMPTA => 'ldcompta',
222 self::$EXPORT_TYPE_LDCOMPTA10 => 'ldcompta10',
223 self::$EXPORT_TYPE_GESTIMUMV3 => 'gestimumv3',
224 self::$EXPORT_TYPE_GESTIMUMV5 => 'gestimumv5',
225 self::$EXPORT_TYPE_FEC => 'fec',
226 self::$EXPORT_TYPE_FEC2 => 'fec2',
227 self::$EXPORT_TYPE_ISUITEEXPERT => 'isuiteexpert',
228 self::$EXPORT_TYPE_ISTEA => 'istea',
229 );
230
231 global $hookmanager;
232 $code = $formatcode[$type] ?? '';
233 $parameters = array('type' => $type);
234 $reshook = $hookmanager->executeHooks('getFormatCode', $parameters, $code);
235
236 return $code;
237 }
238
244 public function getTypeConfig()
245 {
246 global $langs;
247
248 $exporttypes = array(
249 'param' => array(
250 self::$EXPORT_TYPE_CONFIGURABLE => array(
251 'label' => $langs->trans('Modelcsv_configurable'),
252 'ACCOUNTING_EXPORT_FORMAT' => getDolGlobalString('ACCOUNTING_EXPORT_FORMAT', 'txt'),
253 'ACCOUNTING_EXPORT_SEPARATORCSV' => getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV', ','),
254 'ACCOUNTING_EXPORT_ENDLINE' => getDolGlobalString('ACCOUNTING_EXPORT_ENDLINE', 1),
255 'ACCOUNTING_EXPORT_DATE' => getDolGlobalString('ACCOUNTING_EXPORT_DATE', '%Y-%m-%d'),
256 ),
257 self::$EXPORT_TYPE_CEGID => array(
258 'label' => $langs->trans('Modelcsv_CEGID'),
259 ),
260 self::$EXPORT_TYPE_COALA => array(
261 'label' => $langs->trans('Modelcsv_COALA'),
262 ),
263 self::$EXPORT_TYPE_BOB50 => array(
264 'label' => $langs->trans('Modelcsv_bob50'),
265 ),
266 self::$EXPORT_TYPE_CIEL => array(
267 'label' => $langs->trans('Modelcsv_ciel'),
268 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
269 ),
270 self::$EXPORT_TYPE_QUADRATUS => array(
271 'label' => $langs->trans('Modelcsv_quadratus'),
272 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
273 ),
274 self::$EXPORT_TYPE_WINFIC => array(
275 'label' => $langs->trans('Modelcsv_winfic'),
276 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
277 ),
278 self::$EXPORT_TYPE_EBP => array(
279 'label' => $langs->trans('Modelcsv_ebp'),
280 ),
281 self::$EXPORT_TYPE_COGILOG => array(
282 'label' => $langs->trans('Modelcsv_cogilog'),
283 ),
284 self::$EXPORT_TYPE_AGIRIS => array(
285 'label' => $langs->trans('Modelcsv_agiris'),
286 ),
287 self::$EXPORT_TYPE_OPENCONCERTO => array(
288 'label' => $langs->trans('Modelcsv_openconcerto'),
289 ),
290 self::$EXPORT_TYPE_SAGE50_SWISS => array(
291 'label' => $langs->trans('Modelcsv_Sage50_Swiss'),
292 ),
293 self::$EXPORT_TYPE_CHARLEMAGNE => array(
294 'label' => $langs->trans('Modelcsv_charlemagne'),
295 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
296 ),
297 self::$EXPORT_TYPE_LDCOMPTA => array(
298 'label' => $langs->trans('Modelcsv_LDCompta'),
299 ),
300 self::$EXPORT_TYPE_LDCOMPTA10 => array(
301 'label' => $langs->trans('Modelcsv_LDCompta10'),
302 ),
303 self::$EXPORT_TYPE_GESTIMUMV3 => array(
304 'label' => $langs->trans('Modelcsv_Gestinumv3'),
305 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
306 ),
307 self::$EXPORT_TYPE_GESTIMUMV5 => array(
308 'label' => $langs->trans('Modelcsv_Gestinumv5'),
309 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
310 ),
311 self::$EXPORT_TYPE_FEC => array(
312 'label' => $langs->trans('Modelcsv_FEC'),
313 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
314 ),
315 self::$EXPORT_TYPE_FEC2 => array(
316 'label' => $langs->trans('Modelcsv_FEC2'),
317 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
318 ),
319 self::$EXPORT_TYPE_ISUITEEXPERT => array(
320 'label' => 'iSuite Expert',
321 'ACCOUNTING_EXPORT_FORMAT' => 'csv',
322 ),
323 self::$EXPORT_TYPE_ISTEA => array(
324 'label' => 'ISTEA',
325 'ACCOUNTING_EXPORT_FORMAT' => 'csv',
326 ),
327 ),
328 'cr' => array(
329 '1' => $langs->trans("Unix"),
330 '2' => $langs->trans("Windows")
331 ),
332 'format' => array(
333 'csv' => $langs->trans("csv"),
334 'txt' => $langs->trans("txt")
335 ),
336 );
337
338 global $hookmanager;
339 $parameters = array();
340 $reshook = $hookmanager->executeHooks('getTypeConfig', $parameters, $exporttypes);
341 return $exporttypes;
342 }
343
344
351 public function getMimeType($formatexportset)
352 {
353 switch ($formatexportset) {
354 case self::$EXPORT_TYPE_FEC:
355 $mime = 'text/tab-separated-values';
356 break;
357 default:
358 $mime = 'text/csv';
359 break;
360 }
361
362 return $mime;
363 }
364
382 public function export(&$TData, $formatexportset, $withAttachment = 0, $downloadMode = 1, $outputMode = 1, $noouput = 1)
383 {
384 global $db, $conf, $langs; // Used into /accountancy/tpl/export_journal.tpl.php
385 global $search_date_end, $hookmanager; // Used into /accountancy/tpl/export_journal.tpl.php
386
387 // Define name of file to save
388 $formatcode = $this->getFormatCode($formatexportset);
389 $filename = 'general_ledger-'.(!empty($formatcode) ? $formatcode : $formatexportset); // Used into /accountancy/tpl/export_journal.tpl.php
390 $type_export = 'general_ledger'; // Used into /accountancy/tpl/export_journal.tpl.php
391
392 $completefilename = '';
393 $exportFile = null;
394 $exportFileName = '';
395 $exportFilePath = '';
396 $exportFileFullName = '';
397 $downloadFileMimeType = '';
398 $downloadFileFullName = '';
399 $downloadFilePath = '';
400 $archiveFullName = '';
401 $archivePath = '';
403 $archiveFileList = array();
404 if ($withAttachment == 1) {
405 if ($downloadMode == 0) {
406 $downloadMode = 1; // force to download after writing all files (can't use direct download)
407 }
408 if ($outputMode == 0) {
409 $outputMode = 1; // force to put files in a temp directory (can't use print on screen)
410 }
411
412 // PHP ZIP extension must be enabled
413 if (!extension_loaded('zip')) {
414 $langs->load('install');
415 $this->errors[] = $langs->trans('ErrorPHPDoesNotSupport', 'ZIP');
416 return -1;
417 }
418 }
419
420 $mimetype = $this->getMimeType($formatexportset);
421 if ($downloadMode == 0) {
422 // begin to print header for direct download
423 top_httphead($mimetype, 1);
424 }
425
426 // Set var $completefilename and add HTTP header.
427 include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php'; // TODO Fix this: A tpl is for rendering data on output. For including common code, we must use .inc.php
428
429 if ($outputMode == 1 || $outputMode == 2) {
430 if ($outputMode == 1) {
431 // uses the temp directory by default to write files
432 if (!empty($conf->accounting->multidir_temp[$conf->entity])) {
433 $outputDir = $conf->accounting->multidir_temp[$conf->entity];
434 } else {
435 $outputDir = $conf->accounting->dir_temp;
436 }
437 } else {
438 // uses the default export directory "accounting/export"
439 if (!empty($conf->accounting->multidir_output[$conf->entity])) {
440 $outputDir = $conf->accounting->multidir_output[$conf->entity];
441 } else {
442 $outputDir = $conf->accounting->dir_output;
443 }
444
445 // directory already created when module is enabled
446 $outputDir .= '/export';
447 $outputDir .= '/'.dol_sanitizePathName((string) $formatexportset);
448 }
449
450 if (!dol_is_dir($outputDir)) {
451 if (dol_mkdir($outputDir) < 0) {
452 $this->errors[] = $langs->trans('ErrorCanNotCreateDir', $outputDir);
453 return -1;
454 }
455 }
456
457 if ($outputDir != '') {
458 if (!dol_is_dir($outputDir)) {
459 $langs->load('errors');
460 $this->errors[] = $langs->trans('ErrorDirNotFound', $outputDir);
461 return -1;
462 }
463
464 // Fallback if template did not set $completefilename
465 if (empty($completefilename)) {
466 $completefilename = dol_sanitizeFileName($filename).'_'.dol_print_date(dol_now(), '%Y%m%d%H%M%S').'.txt';
467 }
468
469 // create export file
470 $exportFileFullName = $completefilename;
471 $exportFileBaseName = basename($exportFileFullName);
472 $exportFileName = pathinfo($exportFileBaseName, PATHINFO_FILENAME);
473 $exportFilePath = $outputDir . '/' . $exportFileFullName;
474 $exportFile = fopen($exportFilePath, 'w');
475 if (!$exportFile) {
476 $this->errors[] = $langs->trans('ErrorFileNotFound', $exportFilePath);
477 return -1;
478 }
479
480 if ($withAttachment == 1) {
481 $archiveFileList['0'] = array(
482 'path' => $exportFilePath,
483 'name' => $exportFileFullName,
484 );
485
486 // archive name and path
487 $archiveFullName = $exportFileName . '.zip';
488 $archivePath = $outputDir . '/' . $archiveFullName;
489 }
490 }
491 }
492
493 // export file (print on screen or write in a file) and prepare archive list if with attachment is set to 1
494 switch ($formatexportset) {
495 case self::$EXPORT_TYPE_CONFIGURABLE:
496 $this->exportConfigurable($TData, $exportFile);
497 break;
498 case self::$EXPORT_TYPE_CEGID:
499 $this->exportCegid($TData, $exportFile);
500 break;
501 case self::$EXPORT_TYPE_COALA:
502 $this->exportCoala($TData, $exportFile);
503 break;
504 case self::$EXPORT_TYPE_BOB50:
505 $this->exportBob50($TData, $exportFile);
506 break;
507 case self::$EXPORT_TYPE_CIEL:
508 $this->exportCiel($TData, $exportFile);
509 break;
510 case self::$EXPORT_TYPE_QUADRATUS:
511 $archiveFileList = $this->exportQuadratus($TData, $exportFile, $archiveFileList, $withAttachment);
512 break;
513 case self::$EXPORT_TYPE_WINFIC:
514 $this->exportWinfic($TData, $exportFile);
515 break;
516 case self::$EXPORT_TYPE_EBP:
517 $this->exportEbp($TData, $exportFile);
518 break;
519 case self::$EXPORT_TYPE_COGILOG:
520 $this->exportCogilog($TData, $exportFile);
521 break;
522 case self::$EXPORT_TYPE_AGIRIS:
523 $this->exportAgiris($TData, $exportFile);
524 break;
525 case self::$EXPORT_TYPE_OPENCONCERTO:
526 $this->exportOpenConcerto($TData, $exportFile);
527 break;
528 case self::$EXPORT_TYPE_SAGE50_SWISS:
529 $this->exportSAGE50SWISS($TData, $exportFile);
530 break;
531 case self::$EXPORT_TYPE_CHARLEMAGNE:
532 $this->exportCharlemagne($TData, $exportFile);
533 break;
534 case self::$EXPORT_TYPE_LDCOMPTA:
535 $this->exportLDCompta($TData, $exportFile);
536 break;
537 case self::$EXPORT_TYPE_LDCOMPTA10:
538 $this->exportLDCompta10($TData, $exportFile);
539 break;
540 case self::$EXPORT_TYPE_GESTIMUMV3:
541 $this->exportGestimumV3($TData, $exportFile);
542 break;
543 case self::$EXPORT_TYPE_GESTIMUMV5:
544 $this->exportGestimumV5($TData, $exportFile);
545 break;
546 case self::$EXPORT_TYPE_FEC:
547 $archiveFileList = $this->exportFEC($TData, $exportFile, $archiveFileList, $withAttachment);
548 break;
549 case self::$EXPORT_TYPE_FEC2:
550 $archiveFileList = $this->exportFEC2($TData, $exportFile, $archiveFileList, $withAttachment);
551 break;
552 case self::$EXPORT_TYPE_ISUITEEXPERT:
553 $this->exportiSuiteExpert($TData, $exportFile);
554 break;
555 case self::$EXPORT_TYPE_ISTEA:
556 $this->exportISTEA($TData, $exportFile);
557 break;
558 default:
559 global $hookmanager;
560 $parameters = array(
561 'format' => $formatexportset,
562 'file' => $exportFile,
563 'filepath' => $exportFilePath,
564 'filefullname' => $exportFileFullName,
565 );
566 // file contents will be created in the hooked function via print and name will be returned.
567 $reshook = $hookmanager->executeHooks('export', $parameters, $TData);
568 if ($reshook != 1) {
569 $this->errors[] = $langs->trans('accountancy_error_modelnotfound');
570 } elseif (!empty($hookmanager->resArray['downloadFileFullName']) && !empty($hookmanager->resArray['downloadFilePath'])) {
571 $exportFileFullName = $hookmanager->resArray['downloadFileFullName'];
572 $exportFilePath = $hookmanager->resArray['downloadFilePath'];
573 }
574 break;
575 }
576
577
578 // Create and download export file or archive
579 if ($outputMode == 1 || $outputMode == 2) {
580 $error = 0;
581
582 // close export file
583 if ($exportFile) {
584 fclose($exportFile);
585 }
586
587 if ($withAttachment == 1) {
588 // create archive file
589 if (!empty($archiveFullName) && !empty($archivePath) && !empty($archiveFileList)) {
590 // archive files
591 $downloadFileMimeType = 'application/zip';
592 $downloadFileFullName = $archiveFullName;
593 $downloadFilePath = $archivePath;
594
595 // create archive
596 $archive = new ZipArchive();
597 $res = $archive->open($archivePath, ZipArchive::OVERWRITE | ZipArchive::CREATE);
598 if ($res !== true) {
599 $error++;
600 $this->errors[] = $langs->trans('ErrorFileNotFound', $archivePath);
601 }
602 if (!$error) {
603 // add files
604 foreach ($archiveFileList as $archiveFileArr) {
605 $res = $archive->addFile($archiveFileArr['path'], $archiveFileArr['name']);
606 if (!$res) {
607 $error++;
608 $this->errors[] = $langs->trans('ErrorArchiveAddFile', $archiveFileArr['name']);
609 break;
610 }
611 }
612 }
613 if (!$error) {
614 // close archive
615 $archive->close();
616 }
617 }
618 }
619
620 if (!$error) {
621 // download after writing files
622 if ($downloadMode == 1) {
623 if ($withAttachment == 0) {
624 // only download exported file
625 if (!empty($exportFileFullName) && !empty($exportFilePath)) {
626 $downloadFileMimeType = $mimetype;
627 $downloadFileFullName = $exportFileFullName;
628 $downloadFilePath = $exportFilePath;
629 }
630 }
631
632 // download export file or archive
633 if (!empty($downloadFileMimeType) && !empty($downloadFileFullName) && !empty($downloadFilePath) && empty($noouput)) {
634 // deprecated. We must not use this anymore, but have $noouput = 1 because HTTP header must be sent
635 // into main page not into a method.
636 header('Content-Type: ' . $downloadFileMimeType);
637 header('Content-Disposition: attachment; filename=' . $downloadFileFullName);
638 header('Cache-Control: Public, must-revalidate');
639 header('Pragma: public');
640 header('Content-Length: ' . dol_filesize($downloadFilePath));
641
642 readfileLowMemory($downloadFilePath);
643 }
644
645 $this->generatedfiledata = array('downloadFilePath' => $downloadFilePath, 'downloadFileMimeType' => $downloadFileMimeType, 'downloadFileFullName' => $downloadFileFullName);
646 }
647 }
648
649 if ($error) {
650 return -1;
651 }
652 }
653
654 return 1;
655 }
656
657
665 public function exportCegid($objectLines, $exportFile = null)
666 {
667 $separator = ";";
668 $end_line = "\n";
669
670 foreach ($objectLines as $line) {
671 $date_document = dol_print_date($line->doc_date, '%d%m%Y');
672
673 $tab = array();
674
675 $tab[] = $date_document;
676 $tab[] = $line->code_journal;
677 $tab[] = length_accountg($line->numero_compte);
678 $tab[] = length_accounta($line->subledger_account);
679 $tab[] = $line->sens;
680 $tab[] = price2fec(abs($line->debit - $line->credit));
681 $tab[] = dol_string_unaccent($line->label_operation);
682 $tab[] = dol_string_unaccent($line->doc_ref);
683
684 $output = implode($separator, $tab).$end_line;
685 if ($exportFile) {
686 fwrite($exportFile, $output);
687 } else {
688 print $output;
689 }
690 }
691 }
692
701 public function exportCogilog($objectLines, $exportFile = null)
702 {
703 $separator = "\t";
704 $end_line = "\n";
705
706 foreach ($objectLines as $line) {
707 $date_document = dol_print_date($line->doc_date, '%d%m%Y');
708
709 $refInvoice = '';
710 if ($line->doc_type == 'customer_invoice') {
711 // Customer invoice
712 require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
713 $invoice = new Facture($this->db);
714 $invoice->fetch($line->fk_doc);
715
716 $refInvoice = $invoice->ref;
717 } elseif ($line->doc_type == 'supplier_invoice') {
718 // Supplier invoice
719 require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
720 $invoice = new FactureFournisseur($this->db);
721 $invoice->fetch($line->fk_doc);
722
723 $refInvoice = $invoice->ref_supplier;
724 }
725
726 $tab = array();
727
728 $tab[] = $line->code_journal;
729 $tab[] = $date_document;
730 $tab[] = $refInvoice;
731 if (empty($line->subledger_account)) {
732 $tab[] = length_accountg($line->numero_compte);
733 } else {
734 $tab[] = length_accounta($line->subledger_account);
735 }
736 $tab[] = "";
737 $tab[] = $line->label_operation;
738 $tab[] = $date_document;
739 if ($line->sens == 'D') {
740 $tab[] = price($line->debit);
741 $tab[] = "";
742 } elseif ($line->sens == 'C') {
743 $tab[] = "";
744 $tab[] = price($line->credit);
745 }
746 $tab[] = $line->doc_ref;
747 $tab[] = $line->label_operation;
748
749 $output = implode($separator, $tab).$end_line;
750 if ($exportFile) {
751 fwrite($exportFile, $output);
752 } else {
753 print $output;
754 }
755 }
756 }
757
765 public function exportCoala($objectLines, $exportFile = null)
766 {
767 // Coala export
768 $separator = ";";
769 $end_line = "\n";
770
771 foreach ($objectLines as $line) {
772 $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
773
774 $tab = array();
775
776 $tab[] = $date_document;
777 $tab[] = $line->code_journal;
778 $tab[] = length_accountg($line->numero_compte);
779 $tab[] = $line->piece_num;
780 $tab[] = $line->doc_ref;
781 $tab[] = price($line->debit);
782 $tab[] = price($line->credit);
783 $tab[] = 'E';
784 $tab[] = length_accounta($line->subledger_account);
785
786 $output = implode($separator, $tab).$end_line;
787 if ($exportFile) {
788 fwrite($exportFile, $output);
789 } else {
790 print $output;
791 }
792 }
793 }
794
802 public function exportBob50($objectLines, $exportFile = null)
803 {
804 // Bob50
805 $separator = ";";
806 $end_line = "\n";
807
808 foreach ($objectLines as $line) {
809 $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
810
811 $tab = array();
812
813 $tab[] = $line->piece_num;
814 $tab[] = $date_document;
815
816 if (empty($line->subledger_account)) {
817 $tab[] = 'G';
818 $tab[] = length_accountg($line->numero_compte);
819 } else {
820 if (substr($line->numero_compte, 0, 3) == '411') {
821 $tab[] = 'C';
822 }
823 if (substr($line->numero_compte, 0, 3) == '401') {
824 $tab[] = 'F';
825 }
826 $tab[] = length_accounta($line->subledger_account);
827 }
828
829 $tab[] = price($line->debit);
830 $tab[] = price($line->credit);
831 $tab[] = dol_trunc($line->label_operation, 32);
832
833 $output = implode($separator, $tab).$end_line;
834 if ($exportFile) {
835 fwrite($exportFile, $output);
836 } else {
837 print $output;
838 }
839 }
840 }
841
857 public function exportCiel($objectLines, $exportFile = null)
858 {
859 $end_line = "\r\n";
860
861 $i = 1;
862
863 foreach ($objectLines as $line) {
864 $code_compta = length_accountg($line->numero_compte);
865 if (!empty($line->subledger_account)) {
866 $code_compta = length_accounta($line->subledger_account);
867 }
868
869 $date_document = dol_print_date($line->doc_date, '%Y%m%d');
870 $date_echeance = dol_print_date($line->date_lim_reglement, '%Y%m%d');
871
872 $tab = array();
873
874 $tab[] = str_pad((string) $line->piece_num, 5);
875 $tab[] = str_pad(self::trunc($line->code_journal, 2), 2);
876 $tab[] = str_pad($date_document, 8, ' ', STR_PAD_LEFT);
877 $tab[] = str_pad($date_echeance, 8, ' ', STR_PAD_LEFT);
878 $tab[] = str_pad(self::trunc($line->doc_ref, 12), 12);
879 $tab[] = str_pad(self::trunc($code_compta, 11), 11);
880 $tab[] = str_pad(self::trunc(dol_string_unaccent($line->doc_ref).dol_string_unaccent($line->label_operation), 25), 25);
881 $tab[] = str_pad(price2fec(abs($line->debit - $line->credit)), 13, ' ', STR_PAD_LEFT);
882 $tab[] = str_pad($line->sens, 1);
883 $tab[] = str_repeat(' ', 18); // Analytical accounting - Not managed in Dolibarr
884 $tab[] = str_pad(self::trunc(dol_string_unaccent($line->label_operation), 34), 34);
885 $tab[] = 'O2003'; // 0 = EUR | 2003 = Format Ciel
886
887 $output = implode($tab).$end_line;
888 if ($exportFile) {
889 fwrite($exportFile, $output);
890 } else {
891 print $output;
892 }
893 $i++;
894 }
895 }
896
912 public function exportQuadratus($objectLines, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
913 {
914 global $conf;
915
916 $end_line = "\r\n";
917
918 $conf->cache['archiveFileList_notfound'] = array();
919
920 // We should use dol_now function not time however this is wrong date to transfer in accounting
921 $i = 0;
922 foreach ($objectLines as $line) {
923 $i++;
924
925 // Clean some data
926 $line->doc_ref = dol_string_unaccent($line->doc_ref);
927
928 $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
929 $line->label_operation = str_replace(array("- ", "…", "..."), "", $line->label_operation);
930 $line->label_operation = dol_string_unaccent($line->label_operation);
931
932 $line->numero_compte = dol_string_unaccent($line->numero_compte);
933 $line->label_compte = dol_string_unaccent($line->label_compte);
934 $line->subledger_account = dol_string_unaccent($line->subledger_account);
935
936 $line->subledger_label = str_replace(array("- ", "…", "..."), "", $line->subledger_label);
937 $line->subledger_label = dol_string_unaccent($line->subledger_label);
938
939 $code_compta = $line->numero_compte;
940 if (!empty($line->subledger_account)) {
941 $code_compta = $line->subledger_account;
942 }
943
944 $tab = array();
945
946 if (!empty($line->subledger_account)) {
947 $tab['type_ligne'] = 'C';
948 $tab['num_compte'] = str_pad(self::trunc($line->subledger_account, 8), 8);
949 $tab['lib_compte'] = str_pad(self::trunc($line->subledger_label, 30), 30);
950
951 if ($line->doc_type == 'customer_invoice') {
952 $tab['lib_alpha'] = strtoupper(self::trunc(dol_string_unaccent($line->subledger_label), 7));
953 $tab['filler'] = str_repeat(' ', 52);
954 $tab['coll_compte'] = str_pad(self::trunc(getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'), 8), 8);
955 } elseif ($line->doc_type == 'supplier_invoice') {
956 $tab['lib_alpha'] = strtoupper(self::trunc(dol_string_unaccent($line->subledger_label), 7));
957 $tab['filler'] = str_repeat(' ', 52);
958 $tab['coll_compte'] = str_pad(self::trunc(getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'), 8), 8);
959 } else {
960 $tab['filler'] = str_repeat(' ', 59);
961 $tab['coll_compte'] = str_pad(' ', 8);
962 }
963
964 $tab['filler2'] = str_repeat(' ', 110);
965
966 // Field "Maj" (static position 1 char):
967 // blank = no update if account already exists
968 // 2 = partial update (alpha key, label, address, collectif, RIB)
969 if (getDolGlobalString('ACCOUNTING_EXPORT_QUADRATUS_DISABLE_THIRDPARTY_UPDATE')) {
970 $tab['Maj'] = ' ';
971 } else {
972 $tab['Maj'] = 2;
973 }
974
975 if ($line->doc_type == 'customer_invoice') {
976 $tab['type_compte'] = 'C';
977 } elseif ($line->doc_type == 'supplier_invoice') {
978 $tab['type_compte'] = 'F';
979 } else {
980 $tab['type_compte'] = 'G';
981 }
982
983 $tab['filler3'] = str_repeat(' ', 235);
984
985 $tab['end_line'] = $end_line;
986
987 if ($exportFile) {
988 fwrite($exportFile, implode($tab));
989 } else {
990 print implode($tab);
991 }
992 }
993
994 $tab = array();
995 $tab['type_ligne'] = 'M';
996 $tab['num_compte'] = str_pad(self::trunc((string) $code_compta, 8), 8);
997 $tab['code_journal'] = str_pad(self::trunc($line->code_journal, 2), 2);
998 $tab['folio'] = '000';
999
1000 // We use invoice date $line->doc_date not $date_ecriture which is the transfer date
1001 // maybe we should set an option for customer who prefer to keep in accounting software the tranfert date instead of invoice date ?
1002 //$tab['date_ecriture'] = $date_ecriture;
1003 $tab['date_ecriture'] = dol_print_date($line->doc_date, '%d%m%y');
1004 $tab['filler'] = ' ';
1005 $tab['libelle_ecriture'] = str_pad(self::trunc($line->doc_ref.' '.$line->label_operation, 20), 20);
1006
1007 // Credit invoice - invert sens
1008 /*
1009 if ($line->montant < 0) {
1010 if ($line->sens == 'C') {
1011 $tab['sens'] = 'D';
1012 } else {
1013 $tab['sens'] = 'C';
1014 }
1015 $tab['signe_montant'] = '-';
1016 } else {
1017 $tab['sens'] = $line->sens; // C or D
1018 $tab['signe_montant'] = '+';
1019 }*/
1020 $tab['sens'] = $line->sens; // C or D
1021 $tab['signe_montant'] = '+';
1022
1023 // The amount must be in centimes without decimal points.
1024 $tab['montant'] = str_pad((string) abs(($line->debit - $line->credit) * 100), 12, '0', STR_PAD_LEFT);
1025 $tab['contrepartie'] = str_repeat(' ', 8);
1026
1027 // Force date format : %d%m%y
1028 if (!empty($line->date_lim_reglement)) {
1029 $tab['date_echeance'] = dol_print_date($line->date_lim_reglement, '%d%m%y'); // Format must be ddmmyy
1030 } else {
1031 $tab['date_echeance'] = '000000';
1032 }
1033
1034 // Please keep quadra named field lettrage(2) + codestat(3) instead of fake lettrage(5)
1035 // $tab['lettrage'] = str_repeat(' ', 5);
1036 $tab['lettrage'] = str_repeat(' ', 2);
1037 $tab['codestat'] = str_repeat(' ', 3);
1038 $tab['num_piece'] = str_pad(self::trunc((string) $line->piece_num, 5), 5);
1039
1040 // Keep correct quadra named field instead of anon filler
1041 // $tab['filler2'] = str_repeat(' ', 20);
1042 $tab['affaire'] = str_repeat(' ', 10);
1043 $tab['quantity1'] = str_repeat(' ', 10);
1044 $tab['num_piece2'] = str_pad(self::trunc((string) $line->piece_num, 8), 8);
1045 $tab['devis'] = str_pad(getDolCurrency(), 3);
1046 $tab['code_journal2'] = str_pad(self::trunc($line->code_journal, 3), 3);
1047 $tab['filler3'] = str_repeat(' ', 3);
1048
1049 // Keep correct quadra named field instead of anon filler libelle_ecriture2 is 30 char not 32 !!!!
1050 // 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
1051 // TODO: we should filter more than only accent to avoid wrong line size
1052 // TODO: remove invoice number doc_ref in label,
1053 // TODO: we should offer an option for customer to build the label using invoice number / name / date in accounting software
1054 //$tab['libelle_ecriture2'] = str_pad(self::trunc($line->doc_ref . ' ' . $line->label_operation, 30), 30);
1055 $tab['libelle_ecriture2'] = str_pad(self::trunc($line->label_operation, 30), 30);
1056 $tab['codetva'] = str_repeat(' ', 2);
1057
1058 // We need to keep the 10 latest number of invoices doc_ref not the beginning part that is the useless almost same part
1059 // $tab['num_piece3'] = str_pad(self::trunc($line->piece_num, 10), 10);
1060 $tab['num_piece3'] = str_pad(substr(self::trunc($line->doc_ref, 20), -10), 10);
1061 $tab['reserved'] = str_repeat(' ', 10); // position 159
1062 $tab['currency_amount'] = str_repeat(' ', 13); // position 169
1063
1064 // get document file
1065 $attachmentFileName = '';
1066 if ($withAttachment == 1) {
1067 $attachmentFileKey = trim((string) $line->piece_num);
1068
1069 if (!isset($archiveFileList[$attachmentFileKey])) {
1070 // We complete the $archiveFileList to add the file to the existing list (first entry was filled when function was called, we add here the next one)
1071 $objectDirPath = '';
1072 $objectFileName = dol_sanitizeFileName($line->doc_ref);
1073 if ($line->doc_type == 'customer_invoice') {
1074 $objectDirPath = !empty($conf->invoice->multidir_output[$conf->entity]) ? $conf->invoice->multidir_output[$conf->entity] : $conf->invoice->dir_output;
1075 } elseif ($line->doc_type == 'expense_report') {
1076 $objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->expensereport->dir_output;
1077 } elseif ($line->doc_type == 'supplier_invoice') {
1078 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
1079 $invoice = new FactureFournisseur($this->db);
1080 $invoice->fetch($line->fk_doc);
1081 $objectDirPath = !empty($conf->fournisseur->facture->multidir_output[$conf->entity]) ? $conf->fournisseur->facture->multidir_output[$conf->entity] : $conf->fournisseur->facture->dir_output;
1082 $objectDirPath .= '/'.rtrim(get_exdir($invoice->id, 2, 0, 0, $invoice, 'invoice_supplier'), '/');
1083 }
1084
1085 if ($objectDirPath && empty($conf->cache['archiveFileList_notfound'][$attachmentFileKey])) {
1086 $arrayofinclusion = array();
1087 // If it is a supplier invoice, we want to use last uploaded file
1088 $arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').(($line->doc_type == 'supplier_invoice') ? '.+' : '').'\.pdf$';
1089
1090 $fileFoundPath = '';
1091 $fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, 1);
1092
1093 if (!empty($fileFoundList)) {
1094 $attachmentFileNameTrunc = str_pad(self::trunc((string) $line->piece_num, 8), 8, '0', STR_PAD_LEFT);
1095
1096 foreach ($fileFoundList as $fileFound) {
1097 if (strstr($fileFound['name'], $objectFileName)) {
1098 // skip native invoice pdfs (canelle)
1099 // We want to retrieve an attachment representative of the supplier invoice, not a fake document generated by Dolibarr.
1100 if ($line->doc_type == 'supplier_invoice') {
1101 if ($fileFound['name'] === $objectFileName.'.pdf') {
1102 continue;
1103 }
1104 } elseif ($fileFound['name'] !== $objectFileName.'.pdf') {
1105 continue;
1106 }
1107 $fileFoundPath = $objectDirPath.'/'.$objectFileName.'/'.$fileFound['name'];
1108 if (file_exists($fileFoundPath)) {
1109 $archiveFileList[$attachmentFileKey] = array(
1110 'path' => $fileFoundPath,
1111 'name' => $attachmentFileNameTrunc.'.pdf',
1112 );
1113 break;
1114 }
1115 }
1116 }
1117 }
1118
1119 if (empty($fileFoundPath)) {
1120 // Use also a cache if no file were found
1121 $conf->cache['archiveFileList_notfound'][$attachmentFileKey] = 1;
1122 }
1123 }
1124 }
1125
1126 if (isset($archiveFileList[$attachmentFileKey])) {
1127 $attachmentFileName = $archiveFileList[$attachmentFileKey]['name'];
1128 }
1129 }
1130
1131 if (dol_strlen((string) $attachmentFileName) == 12) {
1132 $tab['attachment'] = $attachmentFileName; // position 182
1133 } else {
1134 $tab['attachment'] = str_repeat(' ', 12); // position 182
1135 }
1136 $tab['filler4'] = str_repeat(' ', 38);
1137 $tab['end_line'] = $end_line;
1138
1139 if ($exportFile) {
1140 fwrite($exportFile, implode($tab));
1141 } else {
1142 print implode($tab);
1143 }
1144 }
1145
1146 return $archiveFileList;
1147 }
1148
1159 public function exportWinfic($objectLines, $exportFile = null)
1160 {
1161 global $conf;
1162
1163 $end_line = "\r\n";
1164 $index = 1;
1165
1166 // Warning ! When truncation is necessary, no dot because 3 dots = three characters. The columns are shifted
1167
1168 foreach ($objectLines as $line) {
1169 $code_compta = $line->numero_compte;
1170 if (!empty($line->subledger_account)) {
1171 $code_compta = $line->subledger_account;
1172 }
1173
1174 $tab = array();
1175 //$tab['type_ligne'] = 'M';
1176 $tab['code_journal'] = str_pad(dol_trunc($line->code_journal, 2, 'right', 'UTF-8', 1), 2);
1177
1178 //We use invoice date $line->doc_date not $date_ecriture which is the transfer date
1179 //maybe we should set an option for customer who prefer to keep in accounting software the tranfert date instead of invoice date ?
1180 //$tab['date_ecriture'] = $date_ecriture;
1181 $tab['date_operation'] = dol_print_date($line->doc_date, '%d%m%Y');
1182
1183 $tab['folio'] = ' 1';
1184
1185 $tab['num_ecriture'] = str_pad(dol_trunc((string) $index, 6, 'right', 'UTF-8', 1), 6, ' ', STR_PAD_LEFT);
1186
1187 $tab['jour_ecriture'] = dol_print_date($line->doc_date, '%d%m%y');
1188
1189 $tab['num_compte'] = str_pad(dol_trunc((string) $code_compta, 6, 'right', 'UTF-8', 1), 6, '0');
1190
1191 if ($line->sens == 'D') {
1192 $tab['montant_debit'] = str_pad(number_format($line->debit, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1193
1194 $tab['montant_crebit'] = str_pad(number_format(0, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1195 } else {
1196 $tab['montant_debit'] = str_pad(number_format(0, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1197
1198 $tab['montant_crebit'] = str_pad(number_format($line->credit, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
1199 }
1200
1201 $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);
1202
1203 $tab['lettrage'] = str_repeat(dol_trunc((string) $line->lettering_code, 2, 'left', 'UTF-8', 1), 2);
1204
1205 $tab['code_piece'] = str_pad(dol_trunc((string) $line->piece_num, 5, 'left', 'UTF-8', 1), 5, ' ', STR_PAD_LEFT);
1206
1207 $tab['code_stat'] = str_repeat(' ', 4);
1208
1209 if (!empty($line->date_lim_reglement)) {
1210 $tab['date_echeance'] = dol_print_date($line->date_lim_reglement, '%d%m%Y');
1211 } else {
1212 $tab['date_echeance'] = dol_print_date($line->doc_date, '%d%m%Y');
1213 }
1214
1215 $tab['monnaie'] = '1';
1216
1217 $tab['filler'] = ' ';
1218
1219 $tab['ind_compteur'] = ' ';
1220
1221 $tab['quantite'] = '0,000000000';
1222
1223 $tab['code_pointage'] = str_repeat(' ', 2);
1224
1225 $tab['end_line'] = $end_line;
1226
1227 $output = implode('|', $tab);
1228 if ($exportFile) {
1229 fwrite($exportFile, $output);
1230 } else {
1231 print $output;
1232 }
1233
1234 $index++;
1235 }
1236 }
1237
1238
1246 public function exportEbp($objectLines, $exportFile = null)
1247 {
1248 $separator = ',';
1249 $end_line = "\n";
1250
1251 foreach ($objectLines as $line) {
1252 $date_document = dol_print_date($line->doc_date, '%d%m%Y');
1253
1254 $tab = array();
1255
1256 $tab[] = $line->id;
1257 $tab[] = $date_document;
1258 $tab[] = $line->code_journal;
1259 if (empty($line->subledger_account)) {
1260 $tab[] = $line->numero_compte;
1261 } else {
1262 $tab[] = $line->subledger_account;
1263 }
1264 //$tab[] = substr(length_accountg($line->numero_compte), 0, 2) . $separator;
1265 $tab[] = '"'.dol_trunc($line->label_operation, 40, 'right', 'UTF-8', 1).'"';
1266 $tab[] = '"'.dol_trunc((string) $line->piece_num, 15, 'right', 'UTF-8', 1).'"';
1267 $tab[] = price2num(abs($line->debit - $line->credit));
1268 $tab[] = $line->sens;
1269 $tab[] = $date_document;
1270 //print 'EUR';
1271
1272 $output = implode($separator, $tab).$end_line;
1273 if ($exportFile) {
1274 fwrite($exportFile, $output);
1275 } else {
1276 print $output;
1277 }
1278 }
1279 }
1280
1281
1289 public function exportAgiris($objectLines, $exportFile = null)
1290 {
1291 $separator = ';';
1292 $end_line = "\n";
1293
1294 foreach ($objectLines as $line) {
1295 $date_document = dol_print_date($line->doc_date, '%d%m%Y');
1296
1297 $tab = array();
1298
1299 $tab[] = $line->piece_num;
1300 $tab[] = self::toAnsi($line->label_operation);
1301 $tab[] = $date_document;
1302 $tab[] = self::toAnsi($line->label_operation);
1303
1304 if (empty($line->subledger_account)) {
1305 $tab[] = length_accountg($line->numero_compte);
1306 $tab[] = self::toAnsi($line->label_compte);
1307 } else {
1308 $tab[] = length_accounta($line->subledger_account);
1309 $tab[] = self::toAnsi($line->subledger_label);
1310 }
1311
1312 $tab[] = self::toAnsi($line->doc_ref);
1313 $tab[] = price($line->debit);
1314 $tab[] = price($line->credit);
1315 $tab[] = price(abs($line->debit - $line->credit));
1316 $tab[] = $line->sens;
1317 $tab[] = $line->lettering_code;
1318 $tab[] = $line->code_journal;
1319
1320 $output = implode($separator, $tab).$end_line;
1321 if ($exportFile) {
1322 fwrite($exportFile, $output);
1323 } else {
1324 print $output;
1325 }
1326 }
1327 }
1328
1336 public function exportOpenConcerto($objectLines, $exportFile = null)
1337 {
1338 $separator = ';';
1339 $end_line = "\n";
1340
1341 foreach ($objectLines as $line) {
1342 $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
1343
1344 $tab = array();
1345
1346 $tab[] = $date_document;
1347 $tab[] = $line->code_journal;
1348 if (empty($line->subledger_account)) {
1349 $tab[] = length_accountg($line->numero_compte);
1350 } else {
1351 $tab[] = length_accounta($line->subledger_account);
1352 }
1353 $tab[] = $line->doc_ref;
1354 $tab[] = $line->label_operation;
1355 $tab[] = price($line->debit);
1356 $tab[] = price($line->credit);
1357
1358 $output = implode($separator, $tab).$end_line;
1359 if ($exportFile) {
1360 fwrite($exportFile, $output);
1361 } else {
1362 print $output;
1363 }
1364 }
1365 }
1366
1374 public function exportConfigurable($objectLines, $exportFile = null)
1375 {
1376 global $conf;
1377
1378 $separator = $this->separator;
1379
1380 foreach ($objectLines as $line) {
1381 $date_document = dol_print_date($line->doc_date, getDolGlobalString('ACCOUNTING_EXPORT_DATE'));
1382
1383 $tab = array();
1384 // export configurable
1385 $tab[] = $line->piece_num;
1386 $tab[] = $date_document;
1387 $tab[] = $line->doc_ref;
1388 $tab[] = preg_match('/'.$separator.'/', $line->label_operation) ? "'".$line->label_operation."'" : $line->label_operation;
1389 $tab[] = length_accountg($line->numero_compte);
1390 $tab[] = length_accounta($line->subledger_account);
1391 $tab[] = price2num($line->debit);
1392 $tab[] = price2num($line->credit);
1393 $tab[] = price2num($line->debit - $line->credit);
1394 $tab[] = $line->code_journal;
1395
1396 $output = implode($separator, $tab).$this->end_line;
1397 if ($exportFile) {
1398 fwrite($exportFile, $output);
1399 } else {
1400 print $output;
1401 }
1402 }
1403 }
1404
1412 public function exportISTEA($objectLines, $exportFile = null)
1413 {
1414 global $conf;
1415
1416 $separator = ';';
1417 $end_line = "\n";
1418
1419 // Extract the Third party account numbers from the table to provide the correct line for ISTEA
1420 $tiers = [];
1421 foreach ($objectLines as $line) {
1422 if ($line->subledger_account && substr($line->subledger_account, 0, 1) == '4') {
1423 $tiers[$line->piece_num] = $line->subledger_label;
1424 }
1425 }
1426
1427 foreach ($objectLines as $line) {
1428 $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
1429
1430 /*** prepare label operation field for ISTEA ***/
1431 // truncate the field because ISTEA does not display many characters.
1432 $search = array('Paiement fournisseur ', 'Virement ', 'Paiement ');
1433 $replace = array('Paiemt fourn ','Virt ','Paiemt ');
1434 $label_operation = str_replace($search, $replace, $line->label_operation);
1435 // frame with quotes if field contains the separator
1436 $label_operation = preg_match('/'.$separator.'/', $label_operation) ? "'".$label_operation."'" : $label_operation;
1437
1438 $tab = array();
1439 // export configurable
1440 $tab[] = $line->piece_num; // column 1: piece number ISTEA
1441 $tab[] = $date_document; // column 2: date ISTEA
1442 $tab[] = $line->doc_ref; // column 3: piece reference ISTEA
1443 $tab[] = array_key_exists($line->piece_num, $tiers) ? $tiers[$line->piece_num] : ''; // column 4: third party name ISTEA
1444 $tab[] = length_accountg(($line->subledger_account && (substr($line->subledger_account, 0, 2) == substr($line->numero_compte, 0, 2))) ? $line->subledger_account : $line->numero_compte); // column 5: account number ISTEA
1445 $tab[] = length_accountg($line->subledger_account ? $line->subledger_account : $line->numero_compte); // column 6: account number
1446 $tab[] = length_accountg($line->subledger_account ? $line->numero_compte : ''); // G // column 7: main account number (various payments or 40100000 or 41100000)
1447 $tab[] = ($line->doc_type == 'bank') ? $label_operation : ($line->subledger_account ? $line->subledger_label : $line->label_compte); // column 8: operation label ISTEA
1448 $tab[] = $label_operation; // column 9: operation label (seems not taken into account by ISTEA)
1449 $tab[] = price2num($line->debit); // column 10: debit ISTEA
1450 $tab[] = price2num($line->credit); // column 11: credit ISTEA
1451 $tab[] = $line->code_journal; // column 12: journal ISTEA
1452
1453 $output = mb_convert_encoding('"'.implode('"'.$separator.'"', $tab).'"'.$this->end_line, 'ISO-8859-1');
1454 if ($exportFile) {
1455 fwrite($exportFile, $output);
1456 } else {
1457 print $output;
1458 }
1459 }
1460 }
1461
1474 public function exportFEC($objectLines, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
1475 {
1476 global $conf, $langs;
1477
1478 $separator = "\t";
1479 $end_line = "\r\n";
1480
1481 $tab = array();
1482 $tab[] = "JournalCode";
1483 $tab[] = "JournalLib";
1484 $tab[] = "EcritureNum";
1485 $tab[] = "EcritureDate";
1486 $tab[] = "CompteNum";
1487 $tab[] = "CompteLib";
1488 $tab[] = "CompAuxNum";
1489 $tab[] = "CompAuxLib";
1490 $tab[] = "PieceRef";
1491 $tab[] = "PieceDate";
1492 $tab[] = "EcritureLib";
1493 $tab[] = "Debit";
1494 $tab[] = "Credit";
1495 $tab[] = "EcritureLet";
1496 $tab[] = "DateLet";
1497 $tab[] = "ValidDate";
1498 $tab[] = "Montantdevise";
1499 $tab[] = "Idevise";
1500 $tab[] = "DateLimitReglmt";
1501 $tab[] = "NumFacture";
1502 $tab[] = "FichierFacture";
1503
1504 $output = implode($separator, $tab).$end_line;
1505 if ($exportFile) {
1506 fwrite($exportFile, $output);
1507 } else {
1508 print $output;
1509 }
1510
1511 $conf->cache['archiveFileList_notfound'] = array();
1512
1513 $i = 0;
1514 foreach ($objectLines as $line) {
1515 if ($line->debit == 0 && $line->credit == 0) {
1516 //var_dump($line->id);
1517 //unset($array[$line]);
1518 } else {
1519 $i++;
1520
1521 $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
1522 $date_document = dol_print_date($line->doc_date, '%Y%m%d');
1523 $date_lettering = dol_print_date($line->date_lettering, '%Y%m%d');
1524 $date_validation = dol_print_date($line->date_validation, '%Y%m%d');
1525 $date_limit_payment = dol_print_date($line->date_lim_reglement, '%Y%m%d');
1526
1527 $refInvoice = '';
1528 $invoice = null;
1529 // TODO The fetch of invoice is just to get the ->id, ->ref (and ->ref_supplier for supplier invoice) so
1530 // may be we can get them into the $objectLines and reuse a generic static objectinstead ?
1531 if ($line->doc_type == 'customer_invoice') {
1532 // Customer invoice
1533 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1534 $invoice = new Facture($this->db);
1535 $invoice->fetch($line->fk_doc);
1536
1537 $refInvoice = (string) $invoice->ref;
1538 } elseif ($line->doc_type == 'supplier_invoice') {
1539 // Supplier invoice
1540 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
1541 $invoice = new FactureFournisseur($this->db);
1542 $invoice->fetch($line->fk_doc);
1543
1544 $refInvoice = (string) $invoice->ref_supplier;
1545 }
1546
1547 $tab = array();
1548
1549 // FEC:JournalCode
1550 $tab[] = $line->code_journal;
1551
1552 // FEC:JournalLib
1553 $labeljournal = dol_string_unaccent($langs->transnoentities($line->journal_label));
1554 $labeljournal = str_replace(array("\t", "\n", "\r"), " ", $labeljournal);
1555 $labeljournal = dol_string_nospecial($labeljournal, ' ');
1556 $tab[] = $labeljournal;
1557
1558 // FEC:EcritureNum
1559 $tab[] = $line->piece_num;
1560
1561 // FEC:EcritureDate
1562 $tab[] = $date_document;
1563
1564 // FEC:CompteNum
1565 $tab[] = length_accountg($line->numero_compte);
1566
1567 // FEC:CompteLib
1568 $line->label_compte = str_replace(array("\t", "\n", "\r"), " ", $line->label_compte);
1569 $tab[] = dol_string_unaccent($line->label_compte);
1570
1571 // FEC:CompAuxNum
1572 $tab[] = length_accounta($line->subledger_account);
1573
1574 // FEC:CompAuxLib
1575 $line->subledger_label = str_replace(array("\t", "\n", "\r"), " ", $line->subledger_label);
1576 $tab[] = dol_string_unaccent($line->subledger_label);
1577
1578 // FEC:PieceRef
1579 $tab[] = $line->doc_ref;
1580
1581 // FEC:PieceDate
1582 $tab[] = dol_string_unaccent($date_creation);
1583
1584 // FEC:EcritureLib
1585 // Clean label operation to prevent problem on export with tab separator & other character
1586 $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
1587 $line->label_operation = str_replace(array("..."), "", $line->label_operation);
1588 $tab[] = dol_string_unaccent($line->label_operation);
1589
1590 // FEC:Debit
1591 $tab[] = price2fec($line->debit);
1592
1593 // FEC:Credit
1594 $tab[] = price2fec($line->credit);
1595
1596 // FEC:EcritureLet
1597 $line->lettering_code = str_replace(array("\t", "\n", "\r"), " ", (string) $line->lettering_code);
1598 $tab[] = $line->lettering_code;
1599
1600 // FEC:DateLet
1601 $tab[] = $date_lettering;
1602
1603 // FEC:ValidDate
1604 $tab[] = $date_validation;
1605
1606 // FEC:Montantdevise
1607 $tab[] = $line->multicurrency_amount;
1608
1609 // FEC:Idevise
1610 $tab[] = $line->multicurrency_code;
1611
1612 // FEC_suppl:DateLimitReglmt
1613 $tab[] = $date_limit_payment;
1614
1615 // FEC_suppl:NumFacture
1616 // Clean ref invoice to prevent problem on export with tab separator & other character
1617 $refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
1618 $tab[] = dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);
1619
1620 // FEC_suppl:FichierFacture
1621 // get document file
1622 $attachmentFileName = '';
1623 if ($withAttachment == 1) {
1624 $attachmentFileKey = trim((string) $line->piece_num);
1625
1626 if (!isset($archiveFileList[$attachmentFileKey])) {
1627 // We complete the $archiveFileList to add the file to the existing list (first entry was filled when function was called, we add here the next one)
1628 $objectDirPath = '';
1629 $objectFileName = dol_sanitizeFileName($line->doc_ref);
1630 if ($line->doc_type == 'customer_invoice') {
1631 if (!getDolGlobalInt('ACCOUNTING_EXPORT_REMOVE_INVOICE_SOURCE_FILE')) {
1632 $objectDirPath = !empty($conf->invoice->multidir_output[$conf->entity]) ? $conf->invoice->multidir_output[$conf->entity] : $conf->invoice->dir_output;
1633 }
1634 } elseif ($line->doc_type == 'expense_report') {
1635 if (!getDolGlobalInt('ACCOUNTING_EXPORT_REMOVE_EXPENSEREPORT_SOURCE_FILE')) {
1636 $objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->expensereport->dir_output;
1637 }
1638 } elseif ($line->doc_type == 'supplier_invoice' && $invoice instanceof FactureFournisseur) {
1639 if (!getDolGlobalInt('ACCOUNTING_EXPORT_REMOVE_SUPPLIERINVOICE_SOURCE_FILE')) {
1640 '@phan-var-force FactureFournisseur $invoice';
1641 $objectDirPath = !empty($conf->fournisseur->facture->multidir_output[$conf->entity]) ? $conf->fournisseur->facture->multidir_output[$conf->entity] : $conf->fournisseur->facture->dir_output;
1642 $objectDirPath .= '/' . rtrim(get_exdir($invoice->id, 2, 0, 0, $invoice, 'invoice_supplier'), '/');
1643 }
1644 }
1645
1646 if ($objectDirPath && empty($conf->cache['archiveFileList_notfound'][$attachmentFileKey])) {
1647 $arrayofinclusion = array();
1648 // If it is a supplier invoice, we want to use last uploaded file
1649 $arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').(($line->doc_type == 'supplier_invoice') ? '.+' : '').'\.pdf$';
1650
1651 $fileFoundPath = ''; // The path of last file found
1652 $fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, 1);
1653
1654 if (!empty($fileFoundList)) {
1655 $attachmentFileNameTrunc = $line->doc_ref;
1656
1657 foreach ($fileFoundList as $fileFound) {
1658 if (strstr($fileFound['name'], $objectFileName)) {
1659 // skip native invoice pdfs (canelle)
1660 // We want to retrieve an attachment representative of the supplier invoice, not a fake document generated by Dolibarr.
1661 if ($line->doc_type == 'supplier_invoice') {
1662 if ($fileFound['name'] === $objectFileName.'.pdf') {
1663 continue;
1664 }
1665 } elseif ($fileFound['name'] !== $objectFileName.'.pdf') {
1666 continue;
1667 }
1668 $fileFoundPath = $objectDirPath.'/'.$objectFileName.'/'.$fileFound['name'];
1669 if (file_exists($fileFoundPath)) {
1670 $archiveFileList[$attachmentFileKey] = array(
1671 'path' => $fileFoundPath,
1672 'name' => $attachmentFileNameTrunc.'.pdf',
1673 );
1674 break;
1675 }
1676 }
1677 }
1678 }
1679
1680 if (empty($fileFoundPath)) {
1681 // Use also a cache if no file were found
1682 $conf->cache['archiveFileList_notfound'][$attachmentFileKey] = 1;
1683 }
1684 }
1685 }
1686
1687 if (isset($archiveFileList[$attachmentFileKey])) {
1688 $attachmentFileName = $archiveFileList[$attachmentFileKey]['name'];
1689 }
1690 }
1691
1692 $tab[] = $attachmentFileName;
1693
1694 $output = implode($separator, $tab).$end_line;
1695
1696 if ($exportFile) {
1697 fwrite($exportFile, $output);
1698 } else {
1699 print $output;
1700 }
1701 }
1702 }
1703
1704 return $archiveFileList;
1705 }
1706
1719 public function exportFEC2($objectLines, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
1720 {
1721 global $conf, $langs;
1722
1723 $separator = "\t";
1724 $end_line = "\r\n";
1725
1726 $tab = array();
1727 $tab[] = "JournalCode";
1728 $tab[] = "JournalLib";
1729 $tab[] = "EcritureNum";
1730 $tab[] = "EcritureDate";
1731 $tab[] = "CompteNum";
1732 $tab[] = "CompteLib";
1733 $tab[] = "CompAuxNum";
1734 $tab[] = "CompAuxLib";
1735 $tab[] = "PieceRef";
1736 $tab[] = "PieceDate";
1737 $tab[] = "EcritureLib";
1738 $tab[] = "Debit";
1739 $tab[] = "Credit";
1740 $tab[] = "EcritureLet";
1741 $tab[] = "DateLet";
1742 $tab[] = "ValidDate";
1743 $tab[] = "Montantdevise";
1744 $tab[] = "Idevise";
1745 $tab[] = "DateLimitReglmt";
1746 $tab[] = "NumFacture";
1747 $tab[] = "FichierFacture";
1748
1749 $output = implode($separator, $tab).$end_line;
1750 if ($exportFile) {
1751 fwrite($exportFile, $output);
1752 } else {
1753 print $output;
1754 }
1755
1756 $conf->cache['archiveFileList_notfound'] = array();
1757
1758 $i = 0;
1759 foreach ($objectLines as $line) {
1760 if ($line->debit == 0 && $line->credit == 0) {
1761 //unset($array[$line]);
1762 } else {
1763 $i++;
1764
1765 $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
1766 $date_document = dol_print_date($line->doc_date, '%Y%m%d');
1767 $date_lettering = dol_print_date($line->date_lettering, '%Y%m%d');
1768 $date_validation = dol_print_date($line->date_validation, '%Y%m%d');
1769 $date_limit_payment = dol_print_date($line->date_lim_reglement, '%Y%m%d');
1770
1771 $refInvoice = '';
1772 $invoice = null;
1773 // TODO The fetch of invoice is just to get the ->id, ->ref (and ->ref_supplier for supplier invoice) so
1774 // may be we can get them into the $objectLines and reuse a generic static objectinstead ?
1775 if ($line->doc_type == 'customer_invoice') {
1776 // Customer invoice
1777 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
1778 $invoice = new Facture($this->db);
1779 $invoice->fetch($line->fk_doc);
1780
1781 $refInvoice = (string) $invoice->ref;
1782 } elseif ($line->doc_type == 'supplier_invoice') {
1783 // Supplier invoice
1784 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
1785 $invoice = new FactureFournisseur($this->db);
1786 $invoice->fetch($line->fk_doc);
1787
1788 $refInvoice = (string) $invoice->ref_supplier;
1789 }
1790
1791 $tab = array();
1792
1793 // FEC:JournalCode
1794 $tab[] = $line->code_journal;
1795
1796 // FEC:JournalLib
1797 $labeljournal = dol_string_unaccent($langs->transnoentities($line->journal_label));
1798 $labeljournal = str_replace(array("\t", "\n", "\r"), " ", $labeljournal);
1799 $labeljournal = dol_string_nospecial($labeljournal, ' ');
1800 $tab[] = $labeljournal;
1801
1802 // FEC:EcritureNum
1803 $tab[] = $line->piece_num;
1804
1805 // FEC:EcritureDate
1806 $tab[] = $date_creation;
1807
1808 // FEC:CompteNum
1809 $tab[] = length_accountg($line->numero_compte);
1810
1811 // FEC:CompteLib
1812 $line->label_compte = str_replace(array("\t", "\n", "\r"), " ", $line->label_compte);
1813 $tab[] = dol_string_unaccent($line->label_compte);
1814
1815 // FEC:CompAuxNum
1816 $tab[] = length_accounta($line->subledger_account);
1817
1818 // FEC:CompAuxLib
1819 $line->subledger_label = str_replace(array("\t", "\n", "\r"), " ", $line->subledger_label);
1820 $tab[] = dol_string_unaccent($line->subledger_label);
1821
1822 // FEC:PieceRef
1823 $tab[] = $line->doc_ref;
1824
1825 // FEC:PieceDate
1826 $tab[] = $date_document;
1827
1828 // FEC:EcritureLib
1829 // Clean label operation to prevent problem on export with tab separator & other character
1830 $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
1831 $line->label_operation = str_replace(array("..."), "", $line->label_operation);
1832 $tab[] = dol_string_unaccent($line->label_operation);
1833
1834 // FEC:Debit
1835 $tab[] = price2fec($line->debit);
1836
1837 // FEC:Credit
1838 $tab[] = price2fec($line->credit);
1839
1840 // FEC:EcritureLet
1841 $line->lettering_code = str_replace(array("\t", "\n", "\r"), " ", (string) $line->lettering_code);
1842 $tab[] = $line->lettering_code;
1843
1844 // FEC:DateLet
1845 $tab[] = $date_lettering;
1846
1847 // FEC:ValidDate
1848 $tab[] = $date_validation;
1849
1850 // FEC:Montantdevise
1851 $tab[] = $line->multicurrency_amount;
1852
1853 // FEC:Idevise
1854 $tab[] = $line->multicurrency_code;
1855
1856 // FEC_suppl:DateLimitReglmt
1857 $tab[] = $date_limit_payment;
1858
1859 // FEC_suppl:NumFacture
1860 // Clean ref invoice to prevent problem on export with tab separator & other character
1861 $refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
1862 $tab[] = dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);
1863
1864 // FEC_suppl:FichierFacture
1865 // get document file
1866 $attachmentFileName = '';
1867 if ($withAttachment == 1) {
1868 $attachmentFileKey = trim((string) $line->piece_num);
1869
1870 if (!isset($archiveFileList[$attachmentFileKey])) {
1871 // We complete the $archiveFileList to add the file to the existing list (first entry was filled when function was called, we add here the next one)
1872 $objectDirPath = '';
1873 $objectFileName = dol_sanitizeFileName($line->doc_ref);
1874 if ($line->doc_type == 'customer_invoice') {
1875 if (!getDolGlobalInt('ACCOUNTING_EXPORT_REMOVE_INVOICE_SOURCE_FILE')) {
1876 $objectDirPath = !empty($conf->invoice->multidir_output[$conf->entity]) ? $conf->invoice->multidir_output[$conf->entity] : $conf->invoice->dir_output;
1877 }
1878 } elseif ($line->doc_type == 'expense_report') {
1879 if (!getDolGlobalInt('ACCOUNTING_EXPORT_REMOVE_EXPENSEREPORT_SOURCE_FILE')) {
1880 $objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->expensereport->dir_output;
1881 }
1882 } elseif ($line->doc_type == 'supplier_invoice' && $invoice instanceof FactureFournisseur) {
1883 if (!getDolGlobalInt('ACCOUNTING_EXPORT_REMOVE_SUPPLIERINVOICE_SOURCE_FILE')) {
1884 '@phan-var-force FactureFournisseur $invoice';
1885 $objectDirPath = !empty($conf->fournisseur->facture->multidir_output[$conf->entity]) ? $conf->fournisseur->facture->multidir_output[$conf->entity] : $conf->fournisseur->facture->dir_output;
1886 $objectDirPath .= '/' . rtrim(get_exdir($invoice->id, 2, 0, 0, $invoice, 'invoice_supplier'), '/');
1887 }
1888 }
1889
1890 if ($objectDirPath && empty($conf->cache['archiveFileList_notfound'][$attachmentFileKey])) {
1891 $arrayofinclusion = array();
1892 // If it is a supplier invoice, we want to use last uploaded file
1893 $arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').(($line->doc_type == 'supplier_invoice') ? '.+' : '').'\.pdf$';
1894
1895 $fileFoundPath = ''; // The path of last file found
1896 $fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, 1);
1897
1898 if (!empty($fileFoundList)) {
1899 $attachmentFileNameTrunc = $line->doc_ref;
1900
1901 foreach ($fileFoundList as $fileFound) {
1902 if (strstr($fileFound['name'], $objectFileName)) {
1903 // skip native invoice pdfs (canelle)
1904 // We want to retrieve an attachment representative of the supplier invoice, not a fake document generated by Dolibarr.
1905 if ($line->doc_type == 'supplier_invoice') {
1906 if ($fileFound['name'] === $objectFileName.'.pdf') {
1907 continue;
1908 }
1909 } elseif ($fileFound['name'] !== $objectFileName.'.pdf') {
1910 continue;
1911 }
1912 $fileFoundPath = $objectDirPath.'/'.$objectFileName.'/'.$fileFound['name'];
1913 if (file_exists($fileFoundPath)) {
1914 $archiveFileList[$attachmentFileKey] = array(
1915 'path' => $fileFoundPath,
1916 'name' => $attachmentFileNameTrunc.'.pdf',
1917 );
1918 break;
1919 }
1920 }
1921 }
1922 }
1923
1924 if (empty($fileFoundPath)) {
1925 // Use also a cache if no file were found
1926 $conf->cache['archiveFileList_notfound'][$attachmentFileKey] = 1;
1927 }
1928 }
1929 }
1930
1931 if (isset($archiveFileList[$attachmentFileKey])) {
1932 $attachmentFileName = $archiveFileList[$attachmentFileKey]['name'];
1933 }
1934 }
1935
1936 $tab[] = $attachmentFileName;
1937
1938 $output = implode($separator, $tab).$end_line;
1939 if ($exportFile) {
1940 fwrite($exportFile, $output);
1941 } else {
1942 print $output;
1943 }
1944 }
1945 }
1946
1947 return $archiveFileList;
1948 }
1949
1960 public function exportSAGE50SWISS($objectLines, $exportFile = null)
1961 {
1962 // SAGE50SWISS
1963 $separator = ',';
1964 $end_line = "\r\n";
1965
1966 // Print header line
1967 $tab = array();
1968
1969 $tab[] = "Blg";
1970 $tab[] = "Datum";
1971 $tab[] = "Kto";
1972 $tab[] = "S/H";
1973 $tab[] = "Grp";
1974 $tab[] = "GKto";
1975 $tab[] = "SId";
1976 $tab[] = "SIdx";
1977 $tab[] = "KIdx";
1978 $tab[] = "BTyp";
1979 $tab[] = "MTyp";
1980 $tab[] = "Code";
1981 $tab[] = "Netto";
1982 $tab[] = "Steuer";
1983 $tab[] = "FW-Betrag";
1984 $tab[] = "Tx1";
1985 $tab[] = "Tx2";
1986 $tab[] = "PkKey";
1987 $tab[] = "OpId";
1988 $tab[] = "Flag";
1989
1990 $output = implode($separator, $tab).$end_line;
1991 if ($exportFile) {
1992 fwrite($exportFile, $output);
1993 } else {
1994 print $output;
1995 }
1996
1997 $thisPieceNum = "";
1998 $thisPieceAccountNr = "";
1999 $aSize = count($objectLines);
2000 foreach ($objectLines as $aIndex => $line) {
2001 $sammelBuchung = false;
2002 if ($aIndex - 2 >= 0 && $objectLines[$aIndex - 2]->piece_num == $line->piece_num) {
2003 $sammelBuchung = true;
2004 } elseif ($aIndex + 2 < $aSize && $objectLines[$aIndex + 2]->piece_num == $line->piece_num) {
2005 $sammelBuchung = true;
2006 } elseif ($aIndex + 1 < $aSize
2007 && $objectLines[$aIndex + 1]->piece_num == $line->piece_num
2008 && $aIndex - 1 < $aSize
2009 && $objectLines[$aIndex - 1]->piece_num == $line->piece_num
2010 ) {
2011 $sammelBuchung = true;
2012 }
2013
2014 $tab = array();
2015
2016 //Blg
2017 $tab[] = $line->piece_num;
2018
2019 // Datum
2020 $date_document = dol_print_date($line->doc_date, '%d.%m.%Y');
2021 $tab[] = $date_document;
2022
2023 // Kto
2024 $tab[] = length_accountg($line->numero_compte);
2025 // S/H
2026 if ($line->sens == 'D') {
2027 $tab[] = 'S';
2028 } else {
2029 $tab[] = 'H';
2030 }
2031 // Grp
2032 $tab[] = self::trunc($line->code_journal, 1);
2033 // GKto
2034 if (empty($line->code_tiers)) {
2035 if ($line->piece_num == $thisPieceNum) {
2036 $tab[] = length_accounta($thisPieceAccountNr);
2037 } else {
2038 $tab[] = "div";
2039 }
2040 } else {
2041 $tab[] = length_accounta($line->code_tiers);
2042 }
2043 // SId
2044 $tab[] = $this->separator;
2045 // SIdx
2046 $tab[] = "0";
2047 // KIdx
2048 $tab[] = "0";
2049 // BTyp
2050 $tab[] = "0";
2051
2052 // MTyp 1=Fibu Einzelbuchung 2=Sammebuchung
2053 if ($sammelBuchung) {
2054 $tab[] = "2";
2055 } else {
2056 $tab[] = "1";
2057 }
2058 // Code
2059 $tab[] = '""';
2060 // Netto
2061 $tab[] = abs($line->debit - $line->credit);
2062 // Steuer
2063 $tab[] = "0.00";
2064 // FW-Betrag
2065 $tab[] = "0.00";
2066 // Tx1
2067 $line1 = self::toAnsi($line->label_compte, 29);
2068 if ($line1 == "LIQ" || $line1 == "LIQ Beleg ok" || strlen($line1) <= 3) {
2069 $line1 = "";
2070 }
2071 $line2 = self::toAnsi($line->doc_ref, 29);
2072 if (strlen($line1) == 0) {
2073 $line1 = $line2;
2074 $line2 = "";
2075 }
2076 if (strlen($line1) > 0 && strlen($line2) > 0 && (strlen($line1) + strlen($line2)) < 27) {
2077 $line1 = $line1.' / '.$line2;
2078 $line2 = "";
2079 }
2080
2081 $tab[] = '"'.self::toAnsi($line1).'"';
2082 // Tx2
2083 $tab[] = '"'.self::toAnsi($line2).'"';
2084 //PkKey
2085 $tab[] = "0";
2086 //OpId
2087 $tab[] = $this->separator;
2088
2089 // Flag
2090 $tab[] = "0";
2091
2092 $output = implode($separator, $tab).$end_line;
2093 if ($exportFile) {
2094 fwrite($exportFile, $output);
2095 } else {
2096 print $output;
2097 }
2098
2099 if ($line->piece_num !== $thisPieceNum) {
2100 $thisPieceNum = $line->piece_num;
2101 $thisPieceAccountNr = $line->numero_compte;
2102 }
2103 }
2104 }
2105
2114 public function exportLDCompta($objectLines, $exportFile = null)
2115 {
2116 $separator = ';';
2117 $end_line = "\r\n";
2118
2119 foreach ($objectLines as $line) {
2120 $date_document = dol_print_date($line->doc_date, '%Y%m%d');
2121 $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
2122 $date_lim_reglement = dol_print_date($line->date_lim_reglement, '%Y%m%d');
2123
2124 $tab = array();
2125
2126 // TYPE
2127 $type_enregistrement = 'E'; // For write movement
2128 $tab[] = $type_enregistrement;
2129 // JNAL
2130 $tab[] = substr($line->code_journal, 0, 2);
2131 // NECR
2132 $tab[] = $line->id;
2133 // NPIE
2134 $tab[] = $line->piece_num;
2135 // DATP
2136 $tab[] = $date_document;
2137 // LIBE
2138 $tab[] = $line->label_operation;
2139 // DATH
2140 $tab[] = $date_lim_reglement;
2141 // CNPI
2142 if ($line->doc_type == 'supplier_invoice') {
2143 if (($line->debit - $line->credit) > 0) {
2144 $nature_piece = 'AF';
2145 } else {
2146 $nature_piece = 'FF';
2147 }
2148 } elseif ($line->doc_type == 'customer_invoice') {
2149 if (($line->debit - $line->credit) < 0) {
2150 $nature_piece = 'AC';
2151 } else {
2152 $nature_piece = 'FC';
2153 }
2154 } else {
2155 $nature_piece = '';
2156 }
2157 $tab[] = $nature_piece;
2158 // RACI
2159 // if (!empty($line->subledger_account)) {
2160 // if ($line->doc_type == 'supplier_invoice') {
2161 // $racine_subledger_account = '40';
2162 // } elseif ($line->doc_type == 'customer_invoice') {
2163 // $racine_subledger_account = '41';
2164 // } else {
2165 // $racine_subledger_account = '';
2166 // }
2167 // } else {
2168 $racine_subledger_account = ''; // for records of type E leave this field blank
2169 // }
2170
2171 $tab[] = $racine_subledger_account; // deprecated CPTG & CPTA use instead
2172 // MONT
2173 $tab[] = price(abs($line->debit - $line->credit), 0, '', 1, 2, 2);
2174 // CODC
2175 $tab[] = $line->sens;
2176 // CPTG
2177 $tab[] = length_accountg($line->numero_compte);
2178 // DATE
2179 $tab[] = $date_creation;
2180 // CLET
2181 $tab[] = $line->lettering_code;
2182 // DATL
2183 $tab[] = $line->date_lettering;
2184 // CPTA
2185 if (!empty($line->subledger_account)) {
2186 $tab[] = length_accounta($line->subledger_account);
2187 } else {
2188 $tab[] = "";
2189 }
2190 // C.N.A.T
2191 if ($line->doc_type == 'supplier_invoice' && !empty($line->subledger_account)) {
2192 $tab[] = 'F';
2193 } elseif ($line->doc_type == 'customer_invoice' && !empty($line->subledger_account)) {
2194 $tab[] = 'C';
2195 } else {
2196 $tab[] = "";
2197 }
2198 // SECT
2199 $tab[] = "";
2200 // CTRE
2201 $tab[] = "";
2202 // NORL
2203 $tab[] = "";
2204 // DATV
2205 $tab[] = "";
2206 // REFD
2207 $tab[] = $line->doc_ref;
2208 // CODH
2209 $tab[] = "";
2210 // NSEQ
2211 $tab[] = "";
2212 // MTDV
2213 $tab[] = '0';
2214 // CODV
2215 $tab[] = "";
2216 // TXDV
2217 $tab[] = '0';
2218 // MOPM
2219 $tab[] = "";
2220 // BONP
2221 $tab[] = "";
2222 // BQAF
2223 $tab[] = "";
2224 // ECES
2225 $tab[] = "";
2226 // TXTL
2227 $tab[] = "";
2228 // ECRM
2229 $tab[] = "";
2230 // DATK
2231 $tab[] = "";
2232 // HEUK
2233 $tab[] = "";
2234
2235 $output = implode($separator, $tab).$end_line;
2236 if ($exportFile) {
2237 fwrite($exportFile, $output);
2238 } else {
2239 print $output;
2240 }
2241 }
2242 }
2243
2254 public function exportLDCompta10($objectLines, $exportFile = null)
2255 {
2256 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
2257
2258 $separator = ';';
2259 $end_line = "\r\n";
2260 $last_codeinvoice = '';
2261
2262 foreach ($objectLines as $line) {
2263 // TYPE C
2264 if ($last_codeinvoice != $line->doc_ref) {
2265 //search company by customer code
2266 $sql = "SELECT code_client, fk_forme_juridique, nom, address, zip, town, fk_pays, phone, siret FROM ".MAIN_DB_PREFIX."societe";
2267 $sql .= " WHERE code_client = '".$this->db->escape($line->thirdparty_code)."'";
2268 $resql = $this->db->query($sql);
2269
2270 if ($resql && $this->db->num_rows($resql) > 0) {
2271 $soc = $this->db->fetch_object($resql);
2272
2273 $address = array('', '', '');
2274 if (strpos($soc->address, "\n") !== false) {
2275 $address = explode("\n", $soc->address);
2276 if (is_array($address) && count($address) > 0) {
2277 foreach ($address as $key => $data) {
2278 $address[$key] = str_replace(array("\t", "\n", "\r"), "", $data);
2279 $address[$key] = dol_trunc($address[$key], 40, 'right', 'UTF-8', 1);
2280 }
2281 }
2282 } else {
2283 $address[0] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 0, 40);
2284 $address[1] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 41, 40);
2285 $address[2] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 82, 40);
2286 }
2287
2288 $tab = array();
2289
2290 $type_enregistrement = 'C';
2291 //TYPE
2292 $tab[] = $type_enregistrement;
2293 //NOCL
2294 $tab[] = $soc->code_client;
2295 //NMCM
2296 $tab[] = "";
2297 //LIBI
2298 $tab[] = "";
2299 //TITR
2300 $tab[] = "";
2301 //RSSO
2302 $tab[] = $soc->nom;
2303 //CAD1
2304 $tab[] = $address[0];
2305 //CAD2
2306 $tab[] = $address[1];
2307 //CAD3
2308 $tab[] = $address[2];
2309 //COPO
2310 $tab[] = $soc->zip;
2311 //BUDI
2312 $tab[] = substr($soc->town, 0, 40);
2313 //CPAY
2314 $tab[] = "";
2315 //PAYS
2316 $tab[] = substr(getCountry($soc->fk_pays), 0, 40);
2317 //NTEL
2318 $tab[] = $soc->phone;
2319 //TLEX
2320 $tab[] = "";
2321 //TLPO
2322 $tab[] = "";
2323 //TLCY
2324 $tab[] = "";
2325 //NINT
2326 $tab[] = "";
2327 //COMM
2328 $tab[] = "";
2329 //SIRE
2330 $tab[] = str_replace(" ", "", $soc->siret);
2331 //RIBP
2332 $tab[] = "";
2333 //DOBQ
2334 $tab[] = "";
2335 //IBBQ
2336 $tab[] = "";
2337 //COBQ
2338 $tab[] = "";
2339 //GUBQ
2340 $tab[] = "";
2341 //CPBQ
2342 $tab[] = "";
2343 //CLBQ
2344 $tab[] = "";
2345 //BIBQ
2346 $tab[] = "";
2347 //MOPM
2348 $tab[] = "";
2349 //DJPM
2350 $tab[] = "";
2351 //DMPM
2352 $tab[] = "";
2353 //REFM
2354 $tab[] = "";
2355 //SLVA
2356 $tab[] = "";
2357 //PLCR
2358 $tab[] = "";
2359 //ECFI
2360 $tab[] = "";
2361 //CREP
2362 $tab[] = "";
2363 //NREP
2364 $tab[] = "";
2365 //TREP
2366 $tab[] = "";
2367 //MREP
2368 $tab[] = "";
2369 //GRRE
2370 $tab[] = "";
2371 //LTTA
2372 $tab[] = "";
2373 //CACT
2374 $tab[] = "";
2375 //CODV
2376 $tab[] = "";
2377 //GRTR
2378 $tab[] = "";
2379 //NOFP
2380 $tab[] = "";
2381 //BQAF
2382 $tab[] = "";
2383 //BONP
2384 $tab[] = "";
2385 //CESC
2386 $tab[] = "";
2387
2388 $output = implode($separator, $tab).$end_line;
2389 if ($exportFile) {
2390 fwrite($exportFile, $output);
2391 } else {
2392 print $output;
2393 }
2394 }
2395 }
2396
2397 $tab = array();
2398
2399 $date_document = dol_print_date($line->doc_date, '%Y%m%d');
2400 $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
2401 $date_lim_reglement = dol_print_date($line->date_lim_reglement, '%Y%m%d');
2402
2403 // TYPE E
2404 $type_enregistrement = 'E'; // For write movement
2405 $tab[] = $type_enregistrement;
2406 // JNAL
2407 $tab[] = substr($line->code_journal, 0, 2);
2408 // NECR
2409 $tab[] = $line->id;
2410 // NPIE
2411 $tab[] = $line->piece_num;
2412 // DATP
2413 $tab[] = $date_document;
2414 // LIBE
2415 $tab[] = dol_trunc($line->label_operation, 25, 'right', 'UTF-8', 1);
2416 // DATH
2417 $tab[] = $date_lim_reglement;
2418 // CNPI
2419 if ($line->doc_type == 'supplier_invoice') {
2420 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.
2421 $nature_piece = 'AF';
2422 } else {
2423 $nature_piece = 'FF';
2424 }
2425 } elseif ($line->doc_type == 'customer_invoice') {
2426 if (($line->amount) < 0) {
2427 $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.
2428 } else {
2429 $nature_piece = 'FC';
2430 }
2431 } else {
2432 $nature_piece = '';
2433 }
2434 $tab[] = $nature_piece;
2435 // RACI
2436 // if (!empty($line->subledger_account)) {
2437 // if ($line->doc_type == 'supplier_invoice') {
2438 // $racine_subledger_account = '40';
2439 // } elseif ($line->doc_type == 'customer_invoice') {
2440 // $racine_subledger_account = '41';
2441 // } else {
2442 // $racine_subledger_account = '';
2443 // }
2444 // } else {
2445 $racine_subledger_account = ''; // for records of type E leave this field blank
2446 // }
2447
2448 $tab[] = $racine_subledger_account; // deprecated CPTG & CPTA use instead
2449 // MONT
2450 $tab[] = price(abs($line->debit - $line->credit), 0, '', 1, 2);
2451 // CODC
2452 $tab[] = $line->sens;
2453 // CPTG
2454 $tab[] = length_accountg($line->numero_compte);
2455 // DATE
2456 $tab[] = $date_document;
2457 // CLET
2458 $tab[] = $line->lettering_code;
2459 // DATL
2460 $tab[] = $line->date_lettering;
2461 // CPTA
2462 if (!empty($line->subledger_account)) {
2463 $tab[] = length_accounta($line->subledger_account);
2464 } else {
2465 $tab[] = "";
2466 }
2467 // C.N.A.T
2468 if ($line->doc_type == 'supplier_invoice' && !empty($line->subledger_account)) {
2469 $tab[] = 'F';
2470 } elseif ($line->doc_type == 'customer_invoice' && !empty($line->subledger_account)) {
2471 $tab[] = 'C';
2472 } else {
2473 $tab[] = "";
2474 }
2475 // CTRE
2476 $tab[] = "";
2477 // NORL
2478 $tab[] = "";
2479 // DATV
2480 $tab[] = "";
2481 // REFD
2482 $tab[] = $line->doc_ref;
2483 // NECA
2484 $tab[] = '0';
2485 // CSEC
2486 $tab[] = "";
2487 // CAFF
2488 $tab[] = "";
2489 // CDES
2490 $tab[] = "";
2491 // QTUE
2492 $tab[] = "";
2493 // MTDV
2494 $tab[] = '0';
2495 // CODV
2496 $tab[] = "";
2497 // TXDV
2498 $tab[] = '0';
2499 // MOPM
2500 $tab[] = "";
2501 // BONP
2502 $tab[] = "";
2503 // BQAF
2504 $tab[] = "";
2505 // ECES
2506 $tab[] = "";
2507 // TXTL
2508 $tab[] = "";
2509 // ECRM
2510 $tab[] = "";
2511 // DATK
2512 $tab[] = "";
2513 // HEUK
2514 $tab[] = "";
2515
2516 $output = implode($separator, $tab).$end_line;
2517 if ($exportFile) {
2518 fwrite($exportFile, $output);
2519 } else {
2520 print $output;
2521 }
2522
2523 $last_codeinvoice = $line->doc_ref;
2524 }
2525 }
2526
2534 public function exportCharlemagne($objectLines, $exportFile = null)
2535 {
2536 global $langs;
2537 $langs->load('compta');
2538
2539 $separator = "\t";
2540 $end_line = "\n";
2541
2542 $tab = array();
2543
2544 $tab[] = $langs->transnoentitiesnoconv('Date');
2545 $tab[] = self::trunc($langs->transnoentitiesnoconv('Journal'), 6);
2546 $tab[] = self::trunc($langs->transnoentitiesnoconv('Account'), 15);
2547 $tab[] = self::trunc($langs->transnoentitiesnoconv('LabelAccount'), 60);
2548 $tab[] = self::trunc($langs->transnoentitiesnoconv('Piece'), 20);
2549 $tab[] = self::trunc($langs->transnoentitiesnoconv('LabelOperation'), 60);
2550 $tab[] = $langs->transnoentitiesnoconv('Amount');
2551 $tab[] = 'S';
2552 $tab[] = self::trunc($langs->transnoentitiesnoconv('Analytic').' 1', 15);
2553 $tab[] = self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 1', 60);
2554 $tab[] = self::trunc($langs->transnoentitiesnoconv('Analytic').' 2', 15);
2555 $tab[] = self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 2', 60);
2556 $tab[] = self::trunc($langs->transnoentitiesnoconv('Analytic').' 3', 15);
2557 $tab[] = self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 3', 60);
2558
2559 $output = implode($separator, $tab).$end_line;
2560 if ($exportFile) {
2561 fwrite($exportFile, $output);
2562 } else {
2563 print $output;
2564 }
2565
2566 foreach ($objectLines as $line) {
2567 $date_document = dol_print_date($line->doc_date, '%Y%m%d');
2568
2569 $tab = array();
2570
2571 $tab[] = $date_document; //Date
2572
2573 $tab[] = self::trunc($line->code_journal, 6); //Journal code
2574
2575 if (!empty($line->subledger_account)) {
2576 $account = $line->subledger_account;
2577 } else {
2578 $account = $line->numero_compte;
2579 }
2580 $tab[] = self::trunc((string) $account, 15); //Account number
2581
2582 $tab[] = self::trunc($line->label_compte, 60); //Account label
2583 $tab[] = self::trunc($line->doc_ref, 20); //Piece
2584 // Clean label operation to prevent problem on export with tab separator & other character
2585 $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
2586 $tab[] = self::trunc($line->label_operation, 60); //Operation label
2587 $tab[] = price(abs($line->debit - $line->credit)); //Amount
2588 $tab[] = $line->sens; //Direction
2589 $tab[] = ""; //Analytic
2590 $tab[] = ""; //Analytic
2591 $tab[] = ""; //Analytic
2592 $tab[] = ""; //Analytic
2593 $tab[] = ""; //Analytic
2594 $tab[] = ""; //Analytic
2595
2596 $output = implode($separator, $tab).$end_line;
2597 if ($exportFile) {
2598 fwrite($exportFile, $output);
2599 } else {
2600 print $output;
2601 }
2602 }
2603 }
2604
2612 public function exportGestimumV3($objectLines, $exportFile = null)
2613 {
2614 global $langs;
2615
2616 $separator = ',';
2617 $end_line = "\r\n";
2618
2619 $invoices_infos = array();
2620 $supplier_invoices_infos = array();
2621 foreach ($objectLines as $line) {
2622 if ($line->debit == 0 && $line->credit == 0) {
2623 //unset($array[$line]);
2624 } else {
2625 $date_document = dol_print_date($line->doc_date, '%d/%m/%Y');
2626 $date_echeance = dol_print_date($line->date_lim_reglement, '%Y%m%d');
2627
2628 $invoice_ref = $line->doc_ref;
2629 $company_name = "";
2630
2631 if (($line->doc_type == 'customer_invoice' || $line->doc_type == 'supplier_invoice') && $line->fk_doc > 0) {
2632 if (($line->doc_type == 'customer_invoice' && !isset($invoices_infos[$line->fk_doc])) ||
2633 ($line->doc_type == 'supplier_invoice' && !isset($supplier_invoices_infos[$line->fk_doc]))) {
2634 if ($line->doc_type == 'customer_invoice') {
2635 // Get new customer invoice ref and company name
2636 $sql = 'SELECT f.ref, s.nom FROM ' . MAIN_DB_PREFIX . 'facture as f';
2637 $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'societe AS s ON f.fk_soc = s.rowid';
2638 $sql .= ' WHERE f.rowid = '.((int) $line->fk_doc);
2639 $resql = $this->db->query($sql);
2640 if ($resql) {
2641 if ($obj = $this->db->fetch_object($resql)) {
2642 // Save invoice infos
2643 $invoices_infos[$line->fk_doc] = array('ref' => $obj->ref, 'company_name' => $obj->nom);
2644 $invoice_ref = $obj->ref;
2645 $company_name = $obj->nom;
2646 }
2647 }
2648 } else {
2649 // Get new supplier invoice ref and company name
2650 $sql = 'SELECT ff.ref, s.nom FROM ' . MAIN_DB_PREFIX . 'facture_fourn as ff';
2651 $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'societe AS s ON ff.fk_soc = s.rowid';
2652 $sql .= ' WHERE ff.rowid = '.((int) $line->fk_doc);
2653 $resql = $this->db->query($sql);
2654 if ($resql) {
2655 if ($obj = $this->db->fetch_object($resql)) {
2656 // Save invoice infos
2657 $supplier_invoices_infos[$line->fk_doc] = array('ref' => $obj->ref, 'company_name' => $obj->nom);
2658 $invoice_ref = $obj->ref;
2659 $company_name = $obj->nom;
2660 }
2661 }
2662 }
2663 } elseif ($line->doc_type == 'customer_invoice') {
2664 // Retrieve invoice infos
2665 $invoice_ref = $invoices_infos[$line->fk_doc]['ref'];
2666 $company_name = $invoices_infos[$line->fk_doc]['company_name'];
2667 } else {
2668 // Retrieve invoice infos
2669 $invoice_ref = $supplier_invoices_infos[$line->fk_doc]['ref'];
2670 $company_name = $supplier_invoices_infos[$line->fk_doc]['company_name'];
2671 }
2672 }
2673
2674 $tab = array();
2675
2676 $tab[] = $line->id;
2677 $tab[] = $date_document;
2678 $tab[] = substr($line->code_journal, 0, 4);
2679
2680 if ((substr($line->numero_compte, 0, 3) == '411') || (substr($line->numero_compte, 0, 3) == '401')) {
2681 $tab[] = length_accountg($line->subledger_account);
2682 } else {
2683 $tab[] = substr(length_accountg($line->numero_compte), 0, 15);
2684 }
2685 //Auto label
2686 $tab[] = "";
2687 //print '"'.dol_trunc(str_replace('"', '', $line->label_operation),40,'right','UTF-8',1).'"';
2688 //Manual label
2689 $tab[] = dol_trunc(str_replace('"', '', $invoice_ref . (!empty($company_name) ? ' - ' : '') . $company_name), 40, 'right', 'UTF-8', 1);
2690 //Document number
2691 $tab[] = dol_trunc(str_replace('"', '', (string) $line->piece_num), 10, 'right', 'UTF-8', 1);
2692 //Currency
2693 $tab[] = 'EUR';
2694 //Amount
2695 $tab[] = price2num(abs($line->debit - $line->credit));
2696 //Direction
2697 $tab[] = $line->sens;
2698 //Matching code
2699 $tab[] = "";
2700 //Due date
2701 $tab[] = $date_echeance;
2702
2703 $output = implode($separator, $tab).$end_line;
2704 if ($exportFile) {
2705 fwrite($exportFile, $output);
2706 } else {
2707 print $output;
2708 }
2709 }
2710 }
2711 }
2712
2720 public function exportGestimumV5($objectLines, $exportFile = null)
2721 {
2722 $separator = ',';
2723 $end_line = "\r\n";
2724
2725 foreach ($objectLines as $line) {
2726 if ($line->debit == 0 && $line->credit == 0) {
2727 //unset($array[$line]);
2728 } else {
2729 $date_document = dol_print_date($line->doc_date, '%d%m%Y');
2730
2731 $tab = array();
2732
2733 $tab[] = $line->id;
2734 $tab[] = $date_document;
2735 $tab[] = substr($line->code_journal, 0, 4);
2736 if ((substr($line->numero_compte, 0, 3) == '411') || (substr($line->numero_compte, 0, 3) == '401')) { // TODO No hard code value
2737 $tab[] = length_accountg($line->subledger_account);
2738 } else {
2739 $tab[] = substr(length_accountg($line->numero_compte), 0, 15);
2740 }
2741 $tab[] = "";
2742 $tab[] = '"'.dol_trunc(str_replace('"', '', $line->label_operation), 40, 'right', 'UTF-8', 1).'"';
2743 $tab[] = '"' . dol_trunc(str_replace('"', '', $line->doc_ref), 40, 'right', 'UTF-8', 1) . '"';
2744 $tab[] = '"' . dol_trunc(str_replace('"', '', (string) $line->piece_num), 10, 'right', 'UTF-8', 1) . '"';
2745 $tab[] = price2num(abs($line->debit - $line->credit));
2746 $tab[] = $line->sens;
2747 $tab[] = $date_document;
2748 $tab[] = "";
2749 $tab[] = "";
2750 $tab[] = 'EUR';
2751
2752 $output = implode($separator, $tab).$end_line;
2753 if ($exportFile) {
2754 fwrite($exportFile, $output);
2755 } else {
2756 print $output;
2757 }
2758 }
2759 }
2760 }
2761
2771 public function exportiSuiteExpert($objectLines, $exportFile = null)
2772 {
2773 $separator = ';';
2774 $end_line = "\r\n";
2775
2776
2777 foreach ($objectLines as $line) {
2778 $tab = array();
2779
2780 $date = dol_print_date($line->doc_date, '%d/%m/%Y');
2781
2782 $tab[] = $line->piece_num;
2783 $tab[] = $date;
2784 $tab[] = substr($date, 6, 4);
2785 $tab[] = substr($date, 3, 2);
2786 $tab[] = substr($date, 0, 2);
2787 $tab[] = $line->doc_ref;
2788 // Convert the UTF-8 string in latin9
2789 $tab[] = mb_convert_encoding(str_replace(' - Compte auxiliaire', '', $line->label_operation), "Windows-1252", 'UTF-8');
2790
2791 //Calculate account number length
2792 $taille_numero = strlen(length_accountg($line->numero_compte));
2793
2794 //Build generic customer and supplier account number
2795 $numero_cpt_client = '411';
2796 $numero_cpt_fourn = '401';
2797 for ($i = 1; $i <= ($taille_numero - 3); $i++) {
2798 $numero_cpt_client .= '0';
2799 $numero_cpt_fourn .= '0';
2800 }
2801
2802 //Build auxiliary accounts for customers and suppliers
2803 if (length_accountg($line->numero_compte) == $numero_cpt_client || length_accountg($line->numero_compte) == $numero_cpt_fourn) {
2804 $tab[] = rtrim(length_accounta($line->subledger_account), "0");
2805 } else {
2806 $tab[] = length_accountg($line->numero_compte);
2807 }
2808 $nom_client = explode(" - ", $line->label_operation);
2809 $tab[] = mb_convert_encoding($nom_client[0], "Windows-1252", 'UTF-8');
2810 $tab[] = price($line->debit);
2811 $tab[] = price($line->credit);
2812 $tab[] = price($line->montant);
2813 $tab[] = $line->code_journal;
2814
2815 $output = implode($separator, $tab).$end_line;
2816 if ($exportFile) {
2817 fwrite($exportFile, $output);
2818 } else {
2819 print $output;
2820 }
2821 }
2822 }
2823
2831 public static function trunc($str, $size)
2832 {
2833 return dol_trunc($str, $size, 'right', 'UTF-8', 1);
2834 }
2835
2843 public static function toAnsi($str, $size = -1)
2844 {
2845 $retVal = dol_string_nohtmltag($str, 1, 'Windows-1251');
2846 if ($retVal >= 0 && $size >= 0) {
2847 $retVal = dol_substr($retVal, 0, $size, 'Windows-1251');
2848 }
2849 return $retVal;
2850 }
2851}
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.
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...
exportCharlemagne($objectLines, $exportFile=null)
Export format : Charlemagne.
getTypeConfig()
Array with all export types available (key + label) and parameters for config.
exportFEC($objectLines, $exportFile=null, $archiveFileList=array(), $withAttachment=0)
Export format : FEC Last review for this format : 2023/10/12 Alexandre Spangaro (aspangaro@open-dsi....
exportISTEA($objectLines, $exportFile=null)
Export format : ISTEA.
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.
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 ...
getType($mode=0)
Array with all export type available (key + label)
static toAnsi($str, $size=-1)
toAnsi
exportConfigurable($objectLines, $exportFile=null)
Export format : Configurable CSV.
static getFormatCode($type)
Return string to summarize the format (Used to generated export filename)
exportFEC2($objectLines, $exportFile=null, $archiveFileList=array(), $withAttachment=0)
Export format : FEC2 Last review for this format : 2023/10/12 Alexandre Spangaro (aspangaro@open-dsi....
__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=null, $outputlangs=null, $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_filesize($pathoffile)
Return size of a file.
dol_dir_list($utf8_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:65
dol_is_dir($folder)
Test if filename is a directory.
price2fec($amount)
Function to format a value into a defined format for French administration (no thousand separator & d...
dol_now($mode='gmt')
Return date for now.
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_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
getDolCurrency()
Return the main currency ('EUR', 'USD', ...)
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_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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 a Dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
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.