Completed
Branch develop (f8a444)
by
unknown
32:44
created

AccountancyExport   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 537
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 537
rs 6.8
c 0
b 0
f 0
wmc 55
lcom 1
cbo 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A getType() 0 16 1
C getTypeConfig() 0 56 9
A downloadFile() 0 5 1
C export() 0 41 11
A exportNormal() 0 16 2
A exportCegid() 0 17 2
B exportCogilog() 0 25 4
A exportCoala() 0 19 2
B exportBob50() 0 30 5
B exportCiel() 0 32 3
B exportQuadratus() 0 43 4
A exportEbp() 0 23 2
B exportAgiris() 0 29 3
A exportConfigurable() 0 22 2
A trunc() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like AccountancyExport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AccountancyExport, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * Copyright (C) 2007-2012	Laurent Destailleur	<[email protected]>
4
 * Copyright (C) 2014		Juanjo Menent		<[email protected]>
5
 * Copyright (C) 2015		Florian Henry		<[email protected]>
6
 * Copyright (C) 2015		Raphaël Doursenaud	<[email protected]>
7
 * Copyright (C) 2016		Pierre-Henry Favre	<[email protected]>
8
 * Copyright (C) 2016-2017	Alexandre Spangaro	<[email protected]>
9
 * Copyright (C) 2017       Frédéric France     <[email protected]>
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
/**
26
 * \file		htdocs/accountancy/class/accountancyexport.class.php
27
 * \ingroup		Advanced accountancy
28
 * \brief 		Class accountancy export
29
 */
30
31
/**
32
 * Class AccountancyExport
33
 *
34
 * Manage the different format accountancy export
35
 */
36
37
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
38
39
class AccountancyExport
40
{
41
	/**
42
	 * @var Type of export. Defined by $conf->global->ACCOUNTING_EXPORT_MODELCSV
43
	 */
44
	public static $EXPORT_TYPE_NORMAL = 1;     // Classic CSV
45
	public static $EXPORT_TYPE_CEGID = 2;
46
	public static $EXPORT_TYPE_COALA = 3;
47
	public static $EXPORT_TYPE_BOB50 = 4;
48
	public static $EXPORT_TYPE_CIEL = 5;
49
	public static $EXPORT_TYPE_QUADRATUS = 6;
50
	public static $EXPORT_TYPE_EBP = 7;
51
	public static $EXPORT_TYPE_COGILOG = 8;
52
	public static $EXPORT_TYPE_AGIRIS = 9;
53
	public static $EXPORT_TYPE_CONFIGURABLE = 10;
54
    
55
	/**
56
	 *
57
	 * @var string[] Error codes (or messages)
58
	 */
59
	public $errors = array ();
60
61
	/**
62
	 *
63
	 * @var string Separator
64
	 */
65
	public $separator = '';
66
67
	/**
68
	 *
69
	 * @var string End of line
70
	 */
71
	public $end_line = '';
72
73
	/**
74
	 * Constructor
75
	 *
76
	 * @param DoliDb $db Database handler
77
	 */
78
	public function __construct(DoliDB &$db) {
79
		global $conf;
80
81
		$this->db = &$db;
82
		$this->separator = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
83
        $this->end_line = empty($conf->global->ACCOUNTING_EXPORT_ENDLINE)?"\n":($conf->global->ACCOUNTING_EXPORT_ENDLINE==1?"\n":"\r\n");
84
	}
85
86
	/**
87
	 * Array with all export type available (key + label)
88
	 *
89
	 * @return array of type
90
	 */
91
	public static function getType() {
92
		global $langs;
93
94
		return array (
95
				self::$EXPORT_TYPE_NORMAL => $langs->trans('Modelcsv_normal'),
96
				self::$EXPORT_TYPE_CEGID => $langs->trans('Modelcsv_CEGID'),
97
				self::$EXPORT_TYPE_COALA => $langs->trans('Modelcsv_COALA'),
98
				self::$EXPORT_TYPE_BOB50 => $langs->trans('Modelcsv_bob50'),
99
				self::$EXPORT_TYPE_CIEL => $langs->trans('Modelcsv_ciel'),
100
				self::$EXPORT_TYPE_QUADRATUS => $langs->trans('Modelcsv_quadratus'),
101
				self::$EXPORT_TYPE_EBP => $langs->trans('Modelcsv_ebp'),
102
				self::$EXPORT_TYPE_COGILOG => $langs->trans('Modelcsv_cogilog'),
103
				self::$EXPORT_TYPE_AGIRIS => $langs->trans('Modelcsv_agiris'),
104
				self::$EXPORT_TYPE_CONFIGURABLE => $langs->trans('Modelcsv_configurable'),
105
            );
106
	}
107
108
    /**
109
     * Array with all export type available (key + label) and parameters for config
110
     *
111
     * @return array of type
112
     */
113
    public static function getTypeConfig() {
114
        global $conf, $langs;
115
116
        return array (
117
            'param' => array(
118
                self::$EXPORT_TYPE_NORMAL => array(
119
                    'label' => $langs->trans('Modelcsv_normal'),
120
                    'ACCOUNTING_EXPORT_FORMAT' => empty($conf->global->ACCOUNTING_EXPORT_FORMAT)?'txt':$conf->global->ACCOUNTING_EXPORT_FORMAT,
121
                    'ACCOUNTING_EXPORT_SEPARATORCSV' => empty($conf->global->ACCOUNTING_EXPORT_SEPARATORCSV)?',':$conf->global->ACCOUNTING_EXPORT_SEPARATORCSV,
122
                    'ACCOUNTING_EXPORT_ENDLINE' => empty($conf->global->ACCOUNTING_EXPORT_ENDLINE)?1:$conf->global->ACCOUNTING_EXPORT_ENDLINE,
123
                    'ACCOUNTING_EXPORT_DATE' => empty($conf->global->ACCOUNTING_EXPORT_DATE)?'%d%m%Y':$conf->global->ACCOUNTING_EXPORT_DATE,
124
                ),
125
                self::$EXPORT_TYPE_CEGID => array(
126
                    'label' => $langs->trans('Modelcsv_CEGID'),
127
                ),
128
                self::$EXPORT_TYPE_COALA => array(
129
                    'label' => $langs->trans('Modelcsv_COALA'),
130
                ),
131
                self::$EXPORT_TYPE_BOB50 => array(
132
                    'label' => $langs->trans('Modelcsv_bob50'),
133
                ),
134
                self::$EXPORT_TYPE_CIEL => array(
135
                    'label' => $langs->trans('Modelcsv_ciel'),
136
                    'ACCOUNTING_EXPORT_FORMAT' => 'txt',
137
                ),
138
                self::$EXPORT_TYPE_QUADRATUS => array(
139
                    'label' => $langs->trans('Modelcsv_quadratus'),
140
                    'ACCOUNTING_EXPORT_FORMAT' => 'txt',
141
                ),
142
                self::$EXPORT_TYPE_EBP => array(
143
                    'label' => $langs->trans('Modelcsv_ebp'),
144
                ),
145
                self::$EXPORT_TYPE_COGILOG => array(
146
                    'label' => $langs->trans('Modelcsv_cogilog'),
147
                ),
148
                self::$EXPORT_TYPE_AGIRIS => array(
149
                    'label' => $langs->trans('Modelcsv_agiris'),
150
                ),
151
                self::$EXPORT_TYPE_CONFIGURABLE => array(
152
                    'label' => $langs->trans('Modelcsv_configurable'),
153
                    'ACCOUNTING_EXPORT_FORMAT' => empty($conf->global->ACCOUNTING_EXPORT_FORMAT)?'txt':$conf->global->ACCOUNTING_EXPORT_FORMAT,
154
                    'ACCOUNTING_EXPORT_SEPARATORCSV' => empty($conf->global->ACCOUNTING_EXPORT_SEPARATORCSV)?',':$conf->global->ACCOUNTING_EXPORT_SEPARATORCSV,
155
                    'ACCOUNTING_EXPORT_ENDLINE' => empty($conf->global->ACCOUNTING_EXPORT_ENDLINE)?1:$conf->global->ACCOUNTING_EXPORT_ENDLINE,
156
                    'ACCOUNTING_EXPORT_DATE' => empty($conf->global->ACCOUNTING_EXPORT_DATE)?'%d%m%Y':$conf->global->ACCOUNTING_EXPORT_DATE,
157
                ),
158
            ),
159
            'cr'=> array (
160
                '1' => $langs->trans("Unix"),
161
                '2' => $langs->trans("Windows")
162
            ),
163
            'format' => array (
164
                'csv' => $langs->trans("csv"),
165
                'txt' => $langs->trans("txt")
166
            ),
167
        );
168
    }
169
170
	/**
171
	 * Download the export
172
	 *
173
	 * @return void
174
	 */
175
	public static function downloadFile() {
176
		global $conf;
177
		$journal = 'bookkepping';
178
		include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
179
	}
180
181
	/**
182
	 * Function who chose which export to use with the default config
183
	 *
184
	 * @param unknown $TData data
185
	 */
186
	public function export(&$TData) {
187
		global $conf, $langs;
188
189
		self::downloadFile();
190
191
		switch ($conf->global->ACCOUNTING_EXPORT_MODELCSV) {
192
			case self::$EXPORT_TYPE_NORMAL :
193
				$this->exportNormal($TData);
194
				break;
195
			case self::$EXPORT_TYPE_CEGID :
196
				$this->exportCegid($TData);
197
				break;
198
			case self::$EXPORT_TYPE_COALA :
199
				$this->exportCoala($TData);
200
				break;
201
			case self::$EXPORT_TYPE_BOB50 :
202
				$this->exportBob50($TData);
203
				break;
204
			case self::$EXPORT_TYPE_CIEL :
205
				$this->exportCiel($TData);
206
				break;
207
			case self::$EXPORT_TYPE_QUADRATUS :
208
				$this->exportQuadratus($TData);
209
				break;
210
			case self::$EXPORT_TYPE_EBP :
211
				$this->exportEbp($TData);
212
				break;
213
			case self::$EXPORT_TYPE_COGILOG :
214
				$this->exportCogilog($TData);
215
				break;
216
			case self::$EXPORT_TYPE_AGIRIS :
217
				$this->exportAgiris($TData);
218
				break;
219
            case self::$EXPORT_TYPE_CONFIGURABLE :
220
				$this->exportConfigurable($TData);
221
				break;
222
			default:
223
				$this->errors[] = $langs->trans('accountancy_error_modelnotfound');
224
				break;
225
		}
226
	}
227
228
	/**
229
	 * Export format : Normal
230
	 *
231
	 * @param array $objectLines data
232
	 *
233
	 * @return void
234
	 */
235
	public function exportNormal($objectLines) {
236
		global $conf;
237
238
		foreach ( $objectLines as $line ) {
239
			// Std export
240
			$date = dol_print_date($line->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
241
			print $date . $this->separator;
242
			print $line->doc_ref . $this->separator;
243
			print length_accountg($line->numero_compte) . $this->separator;
244
			print length_accounta($line->subledger_account) . $this->separator;
245
			print price($line->debit) . $this->separator;
246
			print price($line->credit) . $this->separator;
247
			print $line->code_journal . $this->separator;
248
			print $this->end_line;
249
		}
250
	}
251
252
	/**
253
	 * Export format : CEGID
254
	 *
255
	 * @param array $objectLines data
256
	 *
257
	 * @return void
258
	 */
259
	public function exportCegid($objectLines) {
260
		foreach ( $objectLines as $line ) {
261
			$date = dol_print_date($line->doc_date, '%d%m%Y');
262
            $separator = ";";
263
            $end_line = "\n";
264
265
			print $date . $separator;
266
			print $line->code_journal . $separator;
267
			print length_accountg($line->numero_compte) . $separator;
268
			print length_accounta($line->subledger_account) . $separator;
269
			print $line->sens . $separator;
270
			print price($line->montant) . $separator;
271
			print $line->label_operation . $separator;
272
			print $line->doc_ref;
273
			print $end_line;
274
		}
275
	}
276
277
	/**
278
	 * Export format : COGILOG
279
	 *
280
	 * @param array $objectLines data
281
	 *
282
	 * @return void
283
	 */
284
	public function exportCogilog($objectLines) {
285
		foreach ( $objectLines as $line ) {
286
			$date = dol_print_date($line->doc_date, '%d%m%Y');
287
            $separator = ";";
288
            $end_line = "\n";
289
290
			print $line->code_journal . $separator;
291
			print $date . $separator;
292
			print $line->piece_num . $separator;
293
			print length_accountg($line->numero_compte) . $separator;
294
			print '' . $separator;
295
			print $line->label_operation . $separator;
296
			print $date . $separator;
297
			if ($line->sens=='D') {
298
				print price($line->montant) . $separator;
299
				print '' . $separator;
300
			}elseif ($line->sens=='C') {
301
				print '' . $separator;
302
				print price($line->montant) . $separator;
303
			}
304
			print $line->doc_ref . $separator;
305
			print $line->label_operation . $separator;
306
			print $end_line;
307
		}
308
	}
309
310
	/**
311
	 * Export format : COALA
312
	 *
313
	 * @param array $objectLines data
314
	 *
315
	 * @return void
316
	 */
317
	public function exportCoala($objectLines) {
318
		// Coala export
319
        $separator = ";";
320
        $end_line = "\n";
321
322
		foreach ( $objectLines as $line ) {
323
			$date = dol_print_date($line->doc_date, '%d/%m/%Y');
324
			print $date . $separator;
325
			print $line->code_journal . $separator;
326
			print length_accountg($line->numero_compte) . $separator;
327
			print $line->piece_num . $separator;
328
			print $line->doc_ref . $separator;
329
			print price($line->debit) . $separator;
330
			print price($line->credit) . $separator;
331
			print 'E' . $separator;
332
			print length_accountg($line->subledger_account) . $separator;
333
			print $end_line;
334
		}
335
	}
336
337
	/**
338
	 * Export format : BOB50
339
	 *
340
	 * @param array $objectLines data
341
	 *
342
	 * @return void
343
	 */
344
	public function exportBob50($objectLines) {
345
346
		// Bob50
347
        $separator = ";";
348
        $end_line = "\n";
349
350
		foreach ( $objectLines as $line ) {
351
			print $line->piece_num . $separator;
352
			$date = dol_print_date($line->doc_date, '%d/%m/%Y');
353
			print $date . $separator;
354
355
			if (empty($line->subledger_account)) {
356
				print 'G' . $separator;
357
				print length_accounta($line->numero_compte) . $separator;
358
			} else {
359
				if (substr($line->numero_compte, 0, 3) == '411') {
360
					print 'C' . $separator;
361
				}
362
				if (substr($line->numero_compte, 0, 3) == '401') {
363
					print 'F' . $separator;
364
				}
365
				print length_accountg($line->subledger_account) . $separator;
366
			}
367
368
			print price($line->debit) . $separator;
369
			print price($line->credit) . $separator;
370
			print dol_trunc($line->label_operation, 32) . $separator;
371
			print $end_line;
372
		}
373
	}
374
375
	/**
376
	 * Export format : CIEL
377
	 *
378
	 * @param array $TData data
379
	 *
380
	 * @return void
381
	 */
382
	public function exportCiel(&$TData) {
383
		global $conf;
384
385
		$end_line ="\r\n";
386
387
		$i = 1;
388
		$date_ecriture = dol_print_date(dol_now(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be yyyymmdd
389
		foreach ( $TData as $data ) {
390
			$code_compta = $data->numero_compte;
391
			if (! empty($data->subledger_account))
392
				$code_compta = $data->subledger_account;
393
394
			$Tab = array ();
395
			$Tab['num_ecriture'] = str_pad($i, 5);
396
			$Tab['code_journal'] = str_pad($data->code_journal, 2);
397
			$Tab['date_ecriture'] = $date_ecriture;
398
			$Tab['date_ope'] = dol_print_date($data->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
399
			$Tab['num_piece'] = str_pad(self::trunc($data->piece_num, 12), 12);
400
			$Tab['num_compte'] = str_pad(self::trunc($code_compta, 11), 11);
401
			$Tab['libelle_ecriture'] = str_pad(self::trunc($data->doc_ref . $data->label_operation, 25), 25);
402
			$Tab['montant'] = str_pad(abs($data->montant), 13, ' ', STR_PAD_LEFT);
403
			$Tab['type_montant'] = str_pad($data->sens, 1);
404
			$Tab['vide'] = str_repeat(' ', 18);
405
			$Tab['intitule_compte'] = str_pad(self::trunc($data->label_operation, 34), 34);
406
			$Tab['end'] = 'O2003';
407
408
			$Tab['end_line'] = $end_line;
409
410
			print implode($Tab);
411
			$i ++;
412
		}
413
	}
414
415
	/**
416
	 * Export format : Quadratus
417
	 *
418
	 * @param array $TData data
419
	 *
420
	 * @return void
421
	 */
422
	public function exportQuadratus(&$TData) {
423
		global $conf;
424
425
		$end_line ="\r\n";
426
427
		$date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
428
		foreach ( $TData as $data ) {
429
			$code_compta = $data->numero_compte;
430
			if (! empty($data->subledger_account))
431
				$code_compta = $data->subledger_account;
432
433
			$Tab = array ();
434
			$Tab['type_ligne'] = 'M';
435
			$Tab['num_compte'] = str_pad(self::trunc($code_compta, 8), 8);
436
			$Tab['code_journal'] = str_pad(self::trunc($data->code_journal, 2), 2);
437
			$Tab['folio'] = '000';
438
			$Tab['date_ecriture'] = $date_ecriture;
439
			$Tab['filler'] = ' ';
440
			$Tab['libelle_ecriture'] = str_pad(self::trunc($data->doc_ref . ' ' . $data->label_operation, 20), 20);
441
			$Tab['sens'] = $data->sens; // C or D
442
			$Tab['signe_montant'] = '+';
443
			$Tab['montant'] = str_pad(abs($data->montant), 12, '0', STR_PAD_LEFT); // TODO manage negative amount
444
			$Tab['contrepartie'] = str_repeat(' ', 8);
445
			if (! empty($data->date_echeance))
446
				$Tab['date_echeance'] = dol_print_date($data->date_echeance, $conf->global->ACCOUNTING_EXPORT_DATE);
447
			else
448
				$Tab['date_echeance'] = '000000';
449
			$Tab['lettrage'] = str_repeat(' ', 5);
450
			$Tab['num_piece'] = str_pad(self::trunc($data->piece_num, 5), 5);
451
			$Tab['filler2'] = str_repeat(' ', 20);
452
			$Tab['num_piece2'] = str_pad(self::trunc($data->piece_num, 8), 8);
453
			$Tab['devis'] = str_pad($conf->currency, 3);
454
			$Tab['code_journal2'] = str_pad(self::trunc($data->code_journal, 3), 3);
455
			$Tab['filler3'] = str_repeat(' ', 3);
456
			$Tab['libelle_ecriture2'] = str_pad(self::trunc($data->doc_ref . ' ' . $data->label_operation, 32), 32);
457
			$Tab['num_piece3'] = str_pad(self::trunc($data->piece_num, 10), 10);
458
			$Tab['filler4'] = str_repeat(' ', 73);
459
460
			$Tab['end_line'] = $end_line;
461
462
			print implode($Tab);
463
		}
464
	}
465
466
467
	/**
468
	 * Export format : EBP
469
	 *
470
	 * @param array $objectLines data
471
	 *
472
	 * @return void
473
	 */
474
	public function exportEbp($objectLines) {
475
476
		$separator = ',';
477
        $end_line = "\n";
478
479
		foreach ( $objectLines as $line ) {
480
481
			$date = dol_print_date($line->doc_date, '%d%m%Y');
482
483
			print $line->id . $separator;
484
			print $date . $separator;
485
			print $line->code_journal . $separator;
486
			print length_accountg($line->numero_compte) . $separator;
487
			print substr(length_accountg($line->numero_compte),0,2) . $separator;
488
			print '"'.dol_trunc($line->label_operation,40,'right','UTF-8',1).'"' . $separator;
489
			print '"'.dol_trunc($line->piece_num,15,'right','UTF-8',1).'"'.$separator;
490
			print price2num($line->montant).$separator;
491
			print $line->sens.$separator;
492
			print $date . $separator;
493
			print 'EUR';
494
			print $end_line;
495
		}
496
	}
497
498
499
	/**
500
	 * Export format : Agiris Isacompta
501
	 *
502
	 * @param array $objectLines data
503
	 *
504
	 * @return void
505
	 */
506
	public function exportAgiris($objectLines) {
507
508
		$separator = ';';
509
        $end_line = "\n";
510
511
		foreach ( $objectLines as $line ) {
512
513
			$date = dol_print_date($line->doc_date, '%d%m%Y');
514
515
			print $line->piece_num . $separator;
516
			print $line->label_operation . $separator;
517
			print $date . $separator;
518
			print $line->label_operation . $separator;
519
520
			if (empty($line->subledger_account)) {
521
				print length_accountg($line->numero_compte) . $separator;
522
			} else {
523
				print length_accounta($line->subledger_account) . $separator;
524
			}
525
526
			print $line->doc_ref . $separator;
527
			print price($line->debit) . $separator;
528
			print price($line->credit) . $separator;
529
			print price($line->montant) . $separator;
530
			print $line->sens . $separator;
531
			print $line->code_journal;
532
			print $end_line;
533
		}
534
	}
535
536
    /**
537
     * Export format : Configurable
538
     *
539
     * @param array $objectLines data
540
     *
541
     * @return void
542
     */
543
    public function exportConfigurable($objectLines) {
544
        global $conf;
545
546
        foreach ($objectLines as $line) {
547
            $tab = array();
548
            // export configurable
549
            $date = dol_print_date($line->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
550
            $tab[] = $date;
551
            $tab[] = $line->doc_ref;
552
            $tab[] = $line->piece_num;
553
            $tab[] = $line->label_operation;
554
            $tab[] =  length_accountg($line->numero_compte);
555
            $tab[] =  length_accounta($line->subledger_account);
556
            $tab[] =  price($line->debit);
557
            $tab[] =  price($line->credit);
558
            $tab[] =  price($line->montant);
559
            $tab[] =  $line->code_journal;
560
561
            $separator = $this->separator;
562
            print implode($separator, $tab) . $this->end_line;
563
        }
564
    }
565
566
567
	/**
568
	 *
569
	 * @param unknown $str data
570
	 * @param integer $size data
571
	 */
572
	public static function trunc($str, $size) {
573
		return dol_trunc($str, $size, 'right', 'UTF-8', 1);
574
	}
575
}
576