Passed
Branch develop (21edb4)
by
unknown
41:33
created

doc_generic_reception_odt::info()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 80
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 55
nc 20
nop 1
dl 0
loc 80
rs 7.7373
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2018       Quentin Vial-Gouteyron      <[email protected]>
3
 * Copyright (C) 2019       Frédéric France             <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 * or see https://www.gnu.org/
18
 */
19
20
/**
21
 *	\file       htdocs/core/modules/reception/doc/doc_generic_reception_odt.modules.php
22
 *	\ingroup    reception
23
 *	\brief      File of class to build ODT documents for reception
24
 */
25
26
require_once DOL_DOCUMENT_ROOT.'/core/modules/reception/modules_reception.php';
27
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
28
require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
29
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
30
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
31
require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
32
33
34
/**
35
 *	Class to build documents using ODF templates generator
36
 */
37
class doc_generic_reception_odt extends ModelePdfReception
38
{
39
	/**
40
	 * @var Societe Issuer object that emits
41
	 */
42
	public $emetteur; // Objet societe qui emet
43
44
	/**
45
	 * @var array Minimum version of PHP required by module.
46
	 * e.g.: PHP ≥ 5.6 = array(5, 6)
47
	 */
48
	public $phpmin = array(5, 6);
49
50
	/**
51
	 * @var string Dolibarr version of the loaded document
52
	 */
53
	public $version = 'dolibarr';
54
55
56
	/**
57
	 *	Constructor
58
	 *
59
	 *  @param		DoliDB		$db      Database handler
60
	 */
61
	public function __construct($db)
62
	{
63
		global $conf, $langs, $mysoc;
64
65
		$langs->load("main");
66
		$langs->load("companies");
67
68
		$this->db = $db;
69
		$this->name = "ODT templates";
70
		$this->description = $langs->trans("DocumentModelOdt");
71
		$this->scandir = 'RECEPTION_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
72
73
		// Page size for A4 format
74
		$this->type = 'odt';
75
		$this->page_largeur = 0;
76
		$this->page_hauteur = 0;
77
		$this->format = array($this->page_largeur, $this->page_hauteur);
78
		$this->marge_gauche = 0;
79
		$this->marge_droite = 0;
80
		$this->marge_haute = 0;
81
		$this->marge_basse = 0;
82
83
		$this->option_logo = 1; // Display logo
84
		$this->option_tva = 0; // Manage the vat option RECEPTION_TVAOPTION
85
		$this->option_modereg = 0; // Display payment mode
86
		$this->option_condreg = 0; // Display payment terms
87
		$this->option_codeproduitservice = 0; // Display product-service code
88
		$this->option_multilang = 1; // Available in several languages
89
		$this->option_escompte = 0; // Displays if there has been a discount
90
		$this->option_credit_note = 0; // Support credit notes
91
		$this->option_freetext = 1; // Support add of a personalised text
92
		$this->option_draft_watermark = 0; // Support add of a watermark on drafts
93
94
		// Get source company
95
		$this->emetteur = $mysoc;
96
		if (!$this->emetteur->country_code) {
97
			$this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
98
		}
99
	}
100
101
102
	/**
103
	 *  Return description of a module
104
	 *
105
	 *  @param	Translate	$langs      Lang object to use for output
106
	 *  @return string       			Description
107
	 */
108
	public function info($langs)
109
	{
110
		global $conf, $langs;
111
112
		$langs->load("companies");
113
		$langs->load("errors");
114
115
		$form = new Form($this->db);
116
117
		$texte = $this->description.".<br>\n";
118
		$texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
119
		$texte .= '<input type="hidden" name="token" value="'.newToken().'">';
120
		$texte .= '<input type="hidden" name="action" value="setModuleOptions">';
121
		$texte .= '<input type="hidden" name="param1" value="RECEPTION_ADDON_PDF_ODT_PATH">';
122
		$texte .= '<table class="nobordernopadding" width="100%">';
123
124
		// List of directories area
125
		$texte .= '<tr><td>';
126
		$texttitle = $langs->trans("ListOfDirectories");
127
		$listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->RECEPTION_ADDON_PDF_ODT_PATH)));
128
		$listoffiles = array();
129
		foreach ($listofdir as $key => $tmpdir) {
130
			$tmpdir = trim($tmpdir);
131
			$tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
132
			if (!$tmpdir) {
133
				unset($listofdir[$key]);
134
				continue;
135
			}
136
			if (!is_dir($tmpdir)) {
137
				$texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
138
			} else {
139
				$tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
140
				if (count($tmpfiles)) {
141
					$listoffiles = array_merge($listoffiles, $tmpfiles);
142
				}
143
			}
144
		}
145
		$texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
146
		// Add list of substitution keys
147
		$texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
148
		$texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
149
150
		$texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
151
		$texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
152
		$texte .= '<textarea class="flat" cols="60" name="value1">';
153
		$texte .= $conf->global->RECEPTION_ADDON_PDF_ODT_PATH;
154
		$texte .= '</textarea>';
155
		$texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
156
		$texte .= '<input type="submit" class="button small" value="'.$langs->trans("Modify").'" name="Button">';
157
		$texte .= '<br></div></div>';
158
159
		// Scan directories
160
		$nbofiles = count($listoffiles);
161
		if (!empty($conf->global->RECEPTION_ADDON_PDF_ODT_PATH)) {
162
			$texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
163
			//$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
164
			$texte .= count($listoffiles);
165
			//$texte.=$nbofiles?'</a>':'';
166
			$texte .= '</b>';
167
		}
168
		if ($nbofiles) {
169
			$texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
170
			// Show list of found files
171
			foreach ($listoffiles as $file) {
172
				$texte .= '- '.$file['name'].' <a href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=receptions/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a><br>';
173
			}
174
			$texte .= '</div>';
175
		}
176
177
		$texte .= '</td>';
178
179
		$texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
180
		$texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
181
		$texte .= '</td>';
182
		$texte .= '</tr>';
183
184
		$texte .= '</table>';
185
		$texte .= '</form>';
186
187
		return $texte;
188
	}
189
190
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
191
	/**
192
	 *  Function to build a document on disk using the generic odt module.
193
	 *
194
	 *  @param		Reception	$object				Object source to build document
195
	 *  @param		Translate	$outputlangs		Lang output object
196
	 * 	@param		string		$srctemplatepath	Full path of source filename for generator using a template file
197
	 *  @param		int			$hidedetails		Do not show line details
198
	 *  @param		int			$hidedesc			Do not show desc
199
	 *  @param		int			$hideref			Do not show ref
200
	 *	@return		int         					1 if OK, <=0 if KO
201
	 */
202
	public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
203
	{
204
		// phpcs:enable
205
		global $user, $langs, $conf, $mysoc, $hookmanager;
206
207
		if (empty($srctemplatepath)) {
208
			dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
209
			return -1;
210
		}
211
212
		// Add odtgeneration hook
213
		if (!is_object($hookmanager)) {
214
			include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
215
			$hookmanager = new HookManager($this->db);
216
		}
217
		$hookmanager->initHooks(array('odtgeneration'));
218
		global $action;
219
220
		if (!is_object($outputlangs)) {
221
			$outputlangs = $langs;
222
		}
223
		$sav_charset_output = $outputlangs->charset_output;
224
		$outputlangs->charset_output = 'UTF-8';
225
226
		$outputlangs->load("main");
227
		$outputlangs->load("dict");
228
		$outputlangs->load("companies");
229
		$outputlangs->load("bills");
230
231
		if ($conf->reception->dir_output."/reception") {
232
			// If $object is id instead of object
233
			if (!is_object($object)) {
234
				$id = $object;
235
				$object = new Reception($this->db);
236
				$result = $object->fetch($id);
237
				if ($result < 0) {
238
					dol_print_error($this->db, $object->error);
239
					return -1;
240
				}
241
			}
242
243
			$object->fetch_thirdparty();
244
245
			$dir = $conf->reception->dir_output."/reception";
246
			$objectref = dol_sanitizeFileName($object->ref);
247
			if (!preg_match('/specimen/i', $objectref)) {
248
				$dir .= "/".$objectref;
249
			}
250
			$file = $dir."/".$objectref.".odt";
251
252
			if (!file_exists($dir)) {
253
				if (dol_mkdir($dir) < 0) {
254
					$this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
255
					return -1;
256
				}
257
			}
258
259
			if (file_exists($dir)) {
260
				//print "srctemplatepath=".$srctemplatepath;	// Src filename
261
				$newfile = basename($srctemplatepath);
262
				$newfiletmp = preg_replace('/\.od(t|s)/i', '', $newfile);
263
				$newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
264
				$newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
265
				$newfiletmp = $objectref.'_'.$newfiletmp;
266
				//$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
267
				// Get extension (ods or odt)
268
				$newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
269
				if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
270
					$format = $conf->global->MAIN_DOC_USE_TIMING;
271
					if ($format == '1') {
272
						$format = '%Y%m%d%H%M%S';
273
					}
274
					$filename = $newfiletmp.'-'.dol_print_date(dol_now(), $format).'.'.$newfileformat;
275
				} else {
276
					$filename = $newfiletmp.'.'.$newfileformat;
277
				}
278
				$file = $dir.'/'.$filename;
279
				//print "newdir=".$dir;
280
				//print "newfile=".$newfile;
281
				//print "file=".$file;
282
				//print "conf->societe->dir_temp=".$conf->societe->dir_temp;
283
284
				dol_mkdir($conf->reception->dir_temp);
285
				if (!is_writable($conf->reception->dir_temp)) {
286
					$this->error = "Failed to write in temp directory ".$conf->reception->dir_temp;
287
					dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
288
					return -1;
289
				}
290
291
				// If BILLING contact defined on invoice, we use it
292
				$usecontact = false;
293
				$arrayidcontact = $object->getIdContact('external', 'BILLING');
294
				if (count($arrayidcontact) > 0) {
295
					$usecontact = true;
296
					$result = $object->fetch_contact($arrayidcontact[0]);
297
				}
298
299
				// Recipient name
300
				if (!empty($usecontact)) {
301
					if ($usecontact && ($object->contact->fk_soc != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)))) {
302
						$socobject = $object->contact;
303
					} else {
304
						$socobject = $object->thirdparty;
305
						// if we have a BILLING contact and we dont use it as recipient we store the contact object for later use
306
						$contactobject = $object->contact;
307
					}
308
				} else {
309
					$socobject = $object->thirdparty;
310
				}
311
312
				// Make substitution
313
				$substitutionarray = array(
314
				'__FROM_NAME__' => $this->emetteur->name,
315
				'__FROM_EMAIL__' => $this->emetteur->email,
316
				'__TOTAL_TTC__' => $object->total_ttc,
317
				'__TOTAL_HT__' => $object->total_ht,
318
				'__TOTAL_VAT__' => $object->total_tva
319
				);
320
				complete_substitutions_array($substitutionarray, $langs, $object);
321
				// Call the ODTSubstitution hook
322
				$parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray);
323
				$reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
324
325
				// Line of free text
326
				$newfreetext = '';
327
				$paramfreetext = 'RECEPTION_FREE_TEXT';
328
				if (!empty($conf->global->$paramfreetext)) {
329
					$newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
330
				}
331
332
				// Open and load template
333
				require_once ODTPHP_PATH.'odf.php';
334
				try {
335
					$odfHandler = new odf(
336
						$srctemplatepath,
337
						array(
338
						'PATH_TO_TMP'	  => $conf->reception->dir_temp,
339
						'ZIP_PROXY'		  => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
340
						'DELIMITER_LEFT'  => '{',
341
						'DELIMITER_RIGHT' => '}'
342
						)
343
					);
344
				} catch (Exception $e) {
345
					$this->error = $e->getMessage();
346
					return -1;
347
				}
348
				// After construction $odfHandler->contentXml contains content and
349
				// [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
350
				// [!-- BEGIN lines --]*[!-- END lines --]
351
				//print html_entity_decode($odfHandler->__toString());
352
				//print exit;
353
354
355
				// Make substitutions into odt of freetext
356
				try {
357
					$odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
358
				} catch (OdfException $e) {
359
					dol_syslog($e->getMessage(), LOG_INFO);
360
				}
361
362
				// Make substitutions into odt of user info
363
				$tmparray = $this->get_substitutionarray_user($user, $outputlangs);
364
				//var_dump($tmparray); exit;
365
				foreach ($tmparray as $key => $value) {
366
					try {
367
						if (preg_match('/logo$/', $key)) { // Image
368
							//var_dump($value);exit;
369
							if (file_exists($value)) {
370
								$odfHandler->setImage($key, $value);
371
							} else {
372
								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
373
							}
374
						} else // Text
375
						{
376
							$odfHandler->setVars($key, $value, true, 'UTF-8');
377
						}
378
					} catch (OdfException $e) {
379
						dol_syslog($e->getMessage(), LOG_INFO);
380
					}
381
				}
382
				// Make substitutions into odt of mysoc
383
				$tmparray = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
384
				//var_dump($tmparray); exit;
385
				foreach ($tmparray as $key => $value) {
386
					try {
387
						if (preg_match('/logo$/', $key)) {	// Image
388
							//var_dump($value);exit;
389
							if (file_exists($value)) {
390
								$odfHandler->setImage($key, $value);
391
							} else {
392
								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
393
							}
394
						} else // Text
395
						{
396
							$odfHandler->setVars($key, $value, true, 'UTF-8');
397
						}
398
					} catch (OdfException $e) {
399
						dol_syslog($e->getMessage(), LOG_INFO);
400
					}
401
				}
402
				// Make substitutions into odt of thirdparty
403
				$tmparray = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
404
				foreach ($tmparray as $key => $value) {
405
					try {
406
						if (preg_match('/logo$/', $key)) {	// Image
407
							if (file_exists($value)) {
408
								$odfHandler->setImage($key, $value);
409
							} else {
410
								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
411
							}
412
						} else // Text
413
						{
414
							$odfHandler->setVars($key, $value, true, 'UTF-8');
415
						}
416
					} catch (OdfException $e) {
417
						dol_syslog($e->getMessage(), LOG_INFO);
418
					}
419
				}
420
				// Replace tags of object + external modules
421
				$tmparray = $this->get_substitutionarray_reception($object, $outputlangs);
422
				complete_substitutions_array($tmparray, $outputlangs, $object);
423
				// Call the ODTSubstitution hook
424
				$parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
425
				$reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
426
				foreach ($tmparray as $key => $value) {
427
					try {
428
						if (preg_match('/logo$/', $key)) { // Image
429
							if (file_exists($value)) {
430
								$odfHandler->setImage($key, $value);
431
							} else {
432
								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
433
							}
434
						} else // Text
435
						{
436
							$odfHandler->setVars($key, $value, true, 'UTF-8');
437
						}
438
					} catch (OdfException $e) {
439
						dol_syslog($e->getMessage(), LOG_INFO);
440
					}
441
				}
442
				// Replace tags of lines
443
				try {
444
					$listlines = $odfHandler->setSegment('lines');
445
					foreach ($object->lines as $line) {
446
						$tmparray = $this->get_substitutionarray_reception_lines($line, $outputlangs);
447
						complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
448
						// Call the ODTSubstitutionLine hook
449
						$parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$line);
450
						$reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
451
						foreach ($tmparray as $key => $val) {
452
							try {
453
								$listlines->setVars($key, $val, true, 'UTF-8');
454
							} catch (OdfException $e) {
455
								dol_syslog($e->getMessage(), LOG_INFO);
456
							} catch (SegmentException $e) {
457
								dol_syslog($e->getMessage(), LOG_INFO);
458
							}
459
						}
460
						$listlines->merge();
461
					}
462
					$odfHandler->mergeSegment($listlines);
463
				} catch (OdfException $e) {
464
					$this->error = $e->getMessage();
465
					dol_syslog($this->error, LOG_WARNING);
466
					return -1;
467
				}
468
469
				// Replace labels translated
470
				$tmparray = $outputlangs->get_translations_for_substitutions();
471
				foreach ($tmparray as $key => $value) {
472
					try {
473
						$odfHandler->setVars($key, $value, true, 'UTF-8');
474
					} catch (OdfException $e) {
475
						dol_syslog($e->getMessage(), LOG_INFO);
476
					}
477
				}
478
479
				// Call the beforeODTSave hook
480
				$parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
481
				$reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
482
483
				// Write new file
484
				if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
485
					try {
486
						$odfHandler->exportAsAttachedPDF($file);
487
					} catch (Exception $e) {
488
						$this->error = $e->getMessage();
489
						return -1;
490
					}
491
				} else {
492
					try {
493
						$odfHandler->saveToDisk($file);
494
					} catch (Exception $e) {
495
						$this->error = $e->getMessage();
496
						return -1;
497
					}
498
				}
499
				$parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
500
				$reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
501
502
				if (!empty($conf->global->MAIN_UMASK)) {
503
					@chmod($file, octdec($conf->global->MAIN_UMASK));
504
				}
505
506
				$odfHandler = null; // Destroy object
507
508
				return 1; // Success
509
			} else {
510
				$this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
511
				return -1;
512
			}
513
		}
514
515
		return -1;
516
	}
517
}
518