dolibarr  13.0.2
doc_generic_reception_odt.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2018 Quentin Vial-Gouteyron <quentin.vial-gouteyron@atm-consulting.fr>
3  * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
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 
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 
38 {
42  public $emetteur; // Objet societe qui emet
43 
48  public $phpmin = array(5, 6);
49 
53  public $version = 'dolibarr';
54 
55 
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; // Affiche logo
84  $this->option_tva = 0; // Gere option tva RECEPTION_TVAOPTION
85  $this->option_modereg = 0; // Affiche mode reglement
86  $this->option_condreg = 0; // Affiche conditions reglement
87  $this->option_codeproduitservice = 0; // Affiche code produit-service
88  $this->option_multilang = 1; // Dispo en plusieurs langues
89  $this->option_escompte = 0; // Affiche si il y a eu escompte
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  // Recupere emetteur
95  $this->emetteur = $mysoc;
96  if (!$this->emetteur->country_code) $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
97  }
98 
99 
106  public function info($langs)
107  {
108  global $conf, $langs;
109 
110  $langs->load("companies");
111  $langs->load("errors");
112 
113  $form = new Form($this->db);
114 
115  $texte = $this->description.".<br>\n";
116  $texte .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
117  $texte .= '<input type="hidden" name="token" value="'.newToken().'">';
118  $texte .= '<input type="hidden" name="action" value="setModuleOptions">';
119  $texte .= '<input type="hidden" name="param1" value="RECEPTION_ADDON_PDF_ODT_PATH">';
120  $texte .= '<table class="nobordernopadding" width="100%">';
121 
122  // List of directories area
123  $texte .= '<tr><td>';
124  $texttitle = $langs->trans("ListOfDirectories");
125  $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->RECEPTION_ADDON_PDF_ODT_PATH)));
126  $listoffiles = array();
127  foreach ($listofdir as $key=>$tmpdir)
128  {
129  $tmpdir = trim($tmpdir);
130  $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
131  if (!$tmpdir) {
132  unset($listofdir[$key]); continue;
133  }
134  if (!is_dir($tmpdir)) $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0);
135  else {
136  $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)');
137  if (count($tmpfiles)) $listoffiles = array_merge($listoffiles, $tmpfiles);
138  }
139  }
140  $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT");
141  // Add list of substitution keys
142  $texthelp .= '<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
143  $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
144 
145  $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1);
146  $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
147  $texte .= '<textarea class="flat" cols="60" name="value1">';
148  $texte .= $conf->global->RECEPTION_ADDON_PDF_ODT_PATH;
149  $texte .= '</textarea>';
150  $texte .= '</div><div style="display: inline-block; vertical-align: middle;">';
151  $texte .= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
152  $texte .= '<br></div></div>';
153 
154  // Scan directories
155  $nbofiles = count($listoffiles);
156  if (!empty($conf->global->RECEPTION_ADDON_PDF_ODT_PATH))
157  {
158  $texte .= $langs->trans("NumberOfModelFilesFound").': <b>';
159  //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
160  $texte .= count($listoffiles);
161  //$texte.=$nbofiles?'</a>':'';
162  $texte .= '</b>';
163  }
164  if ($nbofiles)
165  {
166  $texte .= '<div id="div_'.get_class($this).'" class="hiddenx">';
167  // Show list of found files
168  foreach ($listoffiles as $file) {
169  $texte .= '- '.$file['name'].' <a href="'.DOL_URL_ROOT.'/document.php?modulepart=doctemplates&file=receptions/'.urlencode(basename($file['name'])).'">'.img_picto('', 'listlight').'</a><br>';
170  }
171  $texte .= '</div>';
172  }
173 
174  $texte .= '</td>';
175 
176  $texte .= '<td rowspan="2" class="tdtop hideonsmartphone">';
177  $texte .= $langs->trans("ExampleOfDirectoriesForModelGen");
178  $texte .= '</td>';
179  $texte .= '</tr>';
180 
181  $texte .= '</table>';
182  $texte .= '</form>';
183 
184  return $texte;
185  }
186 
187  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
199  public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
200  {
201  // phpcs:enable
202  global $user, $langs, $conf, $mysoc, $hookmanager;
203 
204  if (empty($srctemplatepath))
205  {
206  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
207  return -1;
208  }
209 
210  // Add odtgeneration hook
211  if (!is_object($hookmanager))
212  {
213  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
214  $hookmanager = new HookManager($this->db);
215  }
216  $hookmanager->initHooks(array('odtgeneration'));
217  global $action;
218 
219  if (!is_object($outputlangs)) $outputlangs = $langs;
220  $sav_charset_output = $outputlangs->charset_output;
221  $outputlangs->charset_output = 'UTF-8';
222 
223  $outputlangs->load("main");
224  $outputlangs->load("dict");
225  $outputlangs->load("companies");
226  $outputlangs->load("bills");
227 
228  if ($conf->reception->dir_output."/reception")
229  {
230  // If $object is id instead of object
231  if (!is_object($object))
232  {
233  $id = $object;
234  $object = new Reception($this->db);
235  $result = $object->fetch($id);
236  if ($result < 0)
237  {
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)) $dir .= "/".$objectref;
248  $file = $dir."/".$objectref.".odt";
249 
250  if (!file_exists($dir))
251  {
252  if (dol_mkdir($dir) < 0)
253  {
254  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
255  return -1;
256  }
257  }
258 
259  if (file_exists($dir))
260  {
261  //print "srctemplatepath=".$srctemplatepath; // Src filename
262  $newfile = basename($srctemplatepath);
263  $newfiletmp = preg_replace('/\.od(t|s)/i', '', $newfile);
264  $newfiletmp = preg_replace('/template_/i', '', $newfiletmp);
265  $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp);
266  $newfiletmp = $objectref.'_'.$newfiletmp;
267  //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
268  // Get extension (ods or odt)
269  $newfileformat = substr($newfile, strrpos($newfile, '.') + 1);
270  if (!empty($conf->global->MAIN_DOC_USE_TIMING))
271  {
272  $format = $conf->global->MAIN_DOC_USE_TIMING;
273  if ($format == '1') $format = '%Y%m%d%H%M%S';
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 
286 
287  // If BILLING contact defined on invoice, we use it
288  $usecontact = false;
289  $arrayidcontact = $object->getIdContact('external', 'BILLING');
290  if (count($arrayidcontact) > 0)
291  {
292  $usecontact = true;
293  $result = $object->fetch_contact($arrayidcontact[0]);
294  }
295 
296  // Recipient name
297  if (!empty($usecontact)) {
298  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)))) {
299  $socobject = $object->contact;
300  } else {
301  $socobject = $object->thirdparty;
302  // if we have a BILLING contact and we dont use it as recipient we store the contact object for later use
303  $contactobject = $object->contact;
304  }
305  } else {
306  $socobject = $object->thirdparty;
307  }
308 
309  // Make substitution
310  $substitutionarray = array(
311  '__FROM_NAME__' => $this->emetteur->name,
312  '__FROM_EMAIL__' => $this->emetteur->email,
313  '__TOTAL_TTC__' => $object->total_ttc,
314  '__TOTAL_HT__' => $object->total_ht,
315  '__TOTAL_VAT__' => $object->total_tva
316  );
317  complete_substitutions_array($substitutionarray, $langs, $object);
318  // Call the ODTSubstitution hook
319  $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$substitutionarray);
320  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
321 
322  // Line of free text
323  $newfreetext = '';
324  $paramfreetext = 'RECEPTION_FREE_TEXT';
325  if (!empty($conf->global->$paramfreetext))
326  {
327  $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
328  }
329 
330  // Open and load template
331  require_once ODTPHP_PATH.'odf.php';
332  try {
333  $odfHandler = new odf(
334  $srctemplatepath,
335  array(
336  'PATH_TO_TMP' => $conf->reception->dir_temp,
337  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
338  'DELIMITER_LEFT' => '{',
339  'DELIMITER_RIGHT' => '}'
340  )
341  );
342  } catch (Exception $e)
343  {
344  $this->error = $e->getMessage();
345  return -1;
346  }
347  // After construction $odfHandler->contentXml contains content and
348  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
349  // [!-- BEGIN lines --]*[!-- END lines --]
350  //print html_entity_decode($odfHandler->__toString());
351  //print exit;
352 
353 
354  // Make substitutions into odt of freetext
355  try {
356  $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
357  } catch (OdfException $e) {
358  dol_syslog($e->getMessage(), LOG_INFO);
359  }
360 
361  // Make substitutions into odt of user info
362  $tmparray = $this->get_substitutionarray_user($user, $outputlangs);
363  //var_dump($tmparray); exit;
364  foreach ($tmparray as $key=>$value)
365  {
366  try {
367  if (preg_match('/logo$/', $key)) // Image
368  {
369  //var_dump($value);exit;
370  if (file_exists($value)) $odfHandler->setImage($key, $value);
371  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
372  } else // Text
373  {
374  $odfHandler->setVars($key, $value, true, 'UTF-8');
375  }
376  } catch (OdfException $e) {
377  dol_syslog($e->getMessage(), LOG_INFO);
378  }
379  }
380  // Make substitutions into odt of mysoc
381  $tmparray = $this->get_substitutionarray_mysoc($mysoc, $outputlangs);
382  //var_dump($tmparray); exit;
383  foreach ($tmparray as $key=>$value)
384  {
385  try {
386  if (preg_match('/logo$/', $key)) // Image
387  {
388  //var_dump($value);exit;
389  if (file_exists($value)) $odfHandler->setImage($key, $value);
390  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
391  } else // Text
392  {
393  $odfHandler->setVars($key, $value, true, 'UTF-8');
394  }
395  } catch (OdfException $e) {
396  dol_syslog($e->getMessage(), LOG_INFO);
397  }
398  }
399  // Make substitutions into odt of thirdparty
400  $tmparray = $this->get_substitutionarray_thirdparty($socobject, $outputlangs);
401  foreach ($tmparray as $key=>$value)
402  {
403  try {
404  if (preg_match('/logo$/', $key)) // Image
405  {
406  if (file_exists($value)) $odfHandler->setImage($key, $value);
407  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
408  } else // Text
409  {
410  $odfHandler->setVars($key, $value, true, 'UTF-8');
411  }
412  } catch (OdfException $e) {
413  dol_syslog($e->getMessage(), LOG_INFO);
414  }
415  }
416  // Replace tags of object + external modules
417  $tmparray = $this->get_substitutionarray_reception($object, $outputlangs);
418  complete_substitutions_array($tmparray, $outputlangs, $object);
419  // Call the ODTSubstitution hook
420  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
421  $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
422  foreach ($tmparray as $key=>$value)
423  {
424  try {
425  if (preg_match('/logo$/', $key)) // Image
426  {
427  if (file_exists($value)) $odfHandler->setImage($key, $value);
428  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
429  } else // Text
430  {
431  $odfHandler->setVars($key, $value, true, 'UTF-8');
432  }
433  } catch (OdfException $e) {
434  dol_syslog($e->getMessage(), LOG_INFO);
435  }
436  }
437  // Replace tags of lines
438  try {
439  $listlines = $odfHandler->setSegment('lines');
440  foreach ($object->lines as $line)
441  {
442  $tmparray = $this->get_substitutionarray_reception_lines($line, $outputlangs);
443  complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
444  // Call the ODTSubstitutionLine hook
445  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$line);
446  $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
447  foreach ($tmparray as $key => $val)
448  {
449  try {
450  $listlines->setVars($key, $val, true, 'UTF-8');
451  } catch (OdfException $e) {
452  dol_syslog($e->getMessage(), LOG_INFO);
453  } catch (SegmentException $e) {
454  dol_syslog($e->getMessage(), LOG_INFO);
455  }
456  }
457  $listlines->merge();
458  }
459  $odfHandler->mergeSegment($listlines);
460  } catch (OdfException $e)
461  {
462  $this->error = $e->getMessage();
463  dol_syslog($this->error, LOG_WARNING);
464  return -1;
465  }
466 
467  // Replace labels translated
468  $tmparray = $outputlangs->get_translations_for_substitutions();
469  foreach ($tmparray as $key=>$value)
470  {
471  try {
472  $odfHandler->setVars($key, $value, true, 'UTF-8');
473  } catch (OdfException $e) {
474  dol_syslog($e->getMessage(), LOG_INFO);
475  }
476  }
477 
478  // Call the beforeODTSave hook
479  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
480  $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
481 
482  // Write new file
483  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
484  try {
485  $odfHandler->exportAsAttachedPDF($file);
486  } catch (Exception $e) {
487  $this->error = $e->getMessage();
488  return -1;
489  }
490  } else {
491  try {
492  $odfHandler->saveToDisk($file);
493  } catch (Exception $e) {
494  $this->error = $e->getMessage();
495  return -1;
496  }
497  }
498  $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray);
499  $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
500 
501  if (!empty($conf->global->MAIN_UMASK))
502  @chmod($file, octdec($conf->global->MAIN_UMASK));
503 
504  $odfHandler = null; // Destroy object
505 
506  return 1; // Success
507  } else {
508  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
509  return -1;
510  }
511  }
512 
513  return -1;
514  }
515 }
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
dol_now($mode= 'auto')
Return date for now.
get_substitutionarray_user($user, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:108
$conf db
API class for accounts.
Definition: inc.php:54
img_warning($titlealt= 'default', $moreatt= '', $morecss= 'pictowarning')
Show warning logo.
Class to manage hooks.
Class to manage generation of HTML components Only common components must be here.
write_file($object, $outputlangs, $srctemplatepath, $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build a document on disk using the generic odt module.
get_substitutionarray_thirdparty($object, $outputlangs, $array_key= 'company')
Define array with couple substitution key =&gt; substitution value.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
Class to manage receptions.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Parent class of sending receipts models.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=&gt;newva...
info($langs)
Return description of a module.
Class to build documents using ODF templates generator.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the &quot;subst...