dolibarr  9.0.0
doc_generic_invoice_odt.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2012 Laurent Destailleur <ely@users.sourceforge.net>
3  * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
5  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
6  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  * or see http://www.gnu.org/
21  */
22 
29 require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
30 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
35 
36 
41 {
46  public $emetteur;
47 
52  public $phpmin = array(5, 4);
53 
58  public $version = 'dolibarr';
59 
60 
66  function __construct($db)
67  {
68  global $conf, $langs, $mysoc;
69 
70  // Load translation files required by the page
71  $langs->loadLangs(array("main","companies"));
72 
73  $this->db = $db;
74  $this->name = "ODT/ODS templates";
75  $this->description = $langs->trans("DocumentModelOdt");
76  $this->scandir = 'FACTURE_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
77 
78  // Dimension page pour format A4
79  $this->type = 'odt';
80  $this->page_largeur = 0;
81  $this->page_hauteur = 0;
82  $this->format = array($this->page_largeur,$this->page_hauteur);
83  $this->marge_gauche=0;
84  $this->marge_droite=0;
85  $this->marge_haute=0;
86  $this->marge_basse=0;
87 
88  $this->option_logo = 1; // Affiche logo
89  $this->option_tva = 0; // Gere option tva FACTURE_TVAOPTION
90  $this->option_modereg = 0; // Affiche mode reglement
91  $this->option_condreg = 0; // Affiche conditions reglement
92  $this->option_codeproduitservice = 0; // Affiche code produit-service
93  $this->option_multilang = 1; // Dispo en plusieurs langues
94  $this->option_escompte = 0; // Affiche si il y a eu escompte
95  $this->option_credit_note = 0; // Support credit notes
96  $this->option_freetext = 1; // Support add of a personalised text
97  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
98 
99  // Recupere emetteur
100  $this->emetteur=$mysoc;
101  if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // Par defaut, si n'etait pas defini
102  }
103 
104 
111  function info($langs)
112  {
113  global $conf, $langs;
114 
115  // Load translation files required by the page
116  $langs->loadLangs(array("errors","companies"));
117 
118  $form = new Form($this->db);
119 
120  $texte = $this->description.".<br>\n";
121  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
122  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
123  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
124  $texte.= '<input type="hidden" name="param1" value="FACTURE_ADDON_PDF_ODT_PATH">';
125  $texte.= '<table class="nobordernopadding" width="100%">';
126 
127  // List of directories area
128  $texte.= '<tr><td valign="middle">';
129  $texttitle=$langs->trans("ListOfDirectories");
130  $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->FACTURE_ADDON_PDF_ODT_PATH)));
131  $listoffiles=array();
132  foreach($listofdir as $key=>$tmpdir)
133  {
134  $tmpdir=trim($tmpdir);
135  $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
136  if (! $tmpdir) {
137  unset($listofdir[$key]); continue;
138  }
139  if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
140  else
141  {
142  $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.(ods|odt)');
143  if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
144  }
145  }
146  $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
147  // Add list of substitution keys
148  $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
149  $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
150 
151  $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
152  $texte.= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
153  $texte.= '<textarea class="flat" cols="60" name="value1">';
154  $texte.=$conf->global->FACTURE_ADDON_PDF_ODT_PATH;
155  $texte.= '</textarea>';
156  $texte.= '</div><div style="display: inline-block; vertical-align: middle;">';
157  $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
158  $texte.= '<br></div></div>';
159 
160  // Scan directories
161  $nbofiles=count($listoffiles);
162  if (! empty($conf->global->FACTURE_ADDON_PDF_ODT_PATH))
163  {
164  $texte.=$langs->trans("NumberOfModelFilesFound").': <b>';
165  //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
166  $texte.=count($listoffiles);
167  //$texte.=$nbofiles?'</a>':'';
168  $texte.='</b>';
169  }
170  if ($nbofiles)
171  {
172  $texte.='<div id="div_'.get_class($this).'" class="hidden">';
173  foreach($listoffiles as $file)
174  {
175  $texte.=$file['name'].'<br>';
176  }
177  $texte.='<div id="div_'.get_class($this).'">';
178  }
179 
180  $texte.= '</td>';
181 
182  $texte.= '<td valign="top" rowspan="2" class="hideonsmartphone">';
183  $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
184  $texte.= '</td>';
185  $texte.= '</tr>';
186 
187  $texte.= '</table>';
188  $texte.= '</form>';
189 
190  return $texte;
191  }
192 
193  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
205  function write_file($object,$outputlangs,$srctemplatepath,$hidedetails=0,$hidedesc=0,$hideref=0)
206  {
207  // phpcs:enable
208  global $user,$langs,$conf,$mysoc,$hookmanager;
209 
210  if (empty($srctemplatepath))
211  {
212  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
213  return -1;
214  }
215 
216  // Add odtgeneration hook
217  if (! is_object($hookmanager))
218  {
219  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
220  $hookmanager=new HookManager($this->db);
221  }
222  $hookmanager->initHooks(array('odtgeneration'));
223  global $action;
224 
225  if (! is_object($outputlangs)) $outputlangs=$langs;
226  $sav_charset_output=$outputlangs->charset_output;
227  $outputlangs->charset_output='UTF-8';
228 
229  // Load translation files required by the page
230  $outputlangs->loadLangs(array("main", "dict", "companies", "bills"));
231 
232  if ($conf->facture->dir_output)
233  {
234  // If $object is id instead of object
235  if (! is_object($object))
236  {
237  $id = $object;
238  $object = new Facture($this->db);
239  $result=$object->fetch($id);
240  if ($result < 0)
241  {
242  dol_print_error($this->db,$object->error);
243  return -1;
244  }
245  }
246 
247  $dir = $conf->facture->dir_output;
248  $objectref = dol_sanitizeFileName($object->ref);
249  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
250  $file = $dir . "/" . $objectref . ".odt";
251 
252  if (! file_exists($dir))
253  {
254  if (dol_mkdir($dir) < 0)
255  {
256  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
257  return -1;
258  }
259  }
260 
261  if (file_exists($dir))
262  {
263  //print "srctemplatepath=".$srctemplatepath; // Src filename
264  $newfile=basename($srctemplatepath);
265  $newfiletmp=preg_replace('/\.od(t|s)/i','',$newfile);
266  $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
267  $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
268 
269  $newfiletmp=$objectref.'_'.$newfiletmp;
270 
271  // Get extension (ods or odt)
272  $newfileformat=substr($newfile, strrpos($newfile, '.')+1);
273  if ( ! empty($conf->global->MAIN_DOC_USE_TIMING))
274  {
275  $format=$conf->global->MAIN_DOC_USE_TIMING;
276  if ($format == '1') $format='%Y%m%d%H%M%S';
277  $filename=$newfiletmp.'-'.dol_print_date(dol_now(),$format).'.'.$newfileformat;
278  }
279  else
280  {
281  $filename=$newfiletmp.'.'.$newfileformat;
282  }
283  $file=$dir.'/'.$filename;
284  //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
285  //print "newdir=".$dir;
286  //print "newfile=".$newfile;
287  //print "file=".$file;
288  //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
289 
290  dol_mkdir($conf->facture->dir_temp);
291 
292 
293  // If BILLING contact defined on invoice, we use it
294  $usecontact=false;
295  $arrayidcontact=$object->getIdContact('external','BILLING');
296  if (count($arrayidcontact) > 0)
297  {
298  $usecontact=true;
299  $result=$object->fetch_contact($arrayidcontact[0]);
300  }
301 
302  // Recipient name
303  $contactobject = null;
304  if (! empty($usecontact)) {
305  // On peut utiliser le nom de la societe du contact
306  if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))
307  $socobject = $object->contact;
308  else {
309  $socobject = $object->thirdparty;
310  // if we have a BILLING contact and we dont use it as recipient we store the contact object for later use
311  $contactobject = $object->contact;
312  }
313  } else {
314  $socobject = $object->thirdparty;
315  }
316 
317  // Fetch info for linked propal
318  $object->fetchObjectLinked('','','','');
319  //print_r($object->linkedObjects['propal']); exit;
320 
321  $propal_object = $object->linkedObjects['propal'][0];
322 
323  // Make substitution
324  $substitutionarray=array(
325  '__FROM_NAME__' => $this->emetteur->name,
326  '__FROM_EMAIL__' => $this->emetteur->email,
327  '__TOTAL_TTC__' => $object->total_ttc,
328  '__TOTAL_HT__' => $object->total_ht,
329  '__TOTAL_VAT__' => $object->total_tva
330  );
331  complete_substitutions_array($substitutionarray, $langs, $object);
332  // Call the ODTSubstitution hook
333  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$substitutionarray);
334  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
335 
336  // Line of free text
337  $newfreetext='';
338  $paramfreetext='INVOICE_FREE_TEXT';
339  if (! empty($conf->global->$paramfreetext))
340  {
341  $newfreetext=make_substitutions($conf->global->$paramfreetext,$substitutionarray);
342  }
343 
344  // Open and load template
345  require_once ODTPHP_PATH.'odf.php';
346  try {
347  $odfHandler = new odf(
348  $srctemplatepath,
349  array(
350  'PATH_TO_TMP' => $conf->facture->dir_temp,
351  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
352  'DELIMITER_LEFT' => '{',
353  'DELIMITER_RIGHT' => '}'
354  )
355  );
356  }
357  catch (Exception $e)
358  {
359  $this->error=$e->getMessage();
360  dol_syslog($e->getMessage(), LOG_INFO);
361  return -1;
362  }
363  // After construction $odfHandler->contentXml contains content and
364  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
365  // [!-- BEGIN lines --]*[!-- END lines --]
366  //print html_entity_decode($odfHandler->__toString());
367  //print exit;
368 
369 
370  // Make substitutions into odt of freetext
371  try {
372  $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
373  }
374  catch (OdfException $e)
375  {
376  dol_syslog($e->getMessage(), LOG_INFO);
377  }
378 
379  // Define substitution array
380  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
381  $array_object_from_properties=$this->get_substitutionarray_each_var_object($object, $outputlangs);
382  $array_objet=$this->get_substitutionarray_object($object,$outputlangs);
383  $array_user=$this->get_substitutionarray_user($user,$outputlangs);
384  $array_soc=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
385  $array_thirdparty=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
386  $array_propal=is_object($propal_object)?$this->get_substitutionarray_object($propal_object,$outputlangs,'propal'):array();
387  $array_other=$this->get_substitutionarray_other($outputlangs);
388  // retrieve contact information for use in object as contact_xxx tags
389  $array_thirdparty_contact = array();
390  if ($usecontact && is_object($contactobject)) $array_thirdparty_contact=$this->get_substitutionarray_contact($contactobject,$outputlangs,'contact');
391 
392  $tmparray = array_merge($substitutionarray,$array_object_from_properties,$array_user,$array_soc,$array_thirdparty,$array_objet,$array_propal,$array_other,$array_thirdparty_contact);
393  complete_substitutions_array($tmparray, $outputlangs, $object);
394 
395  // Call the ODTSubstitution hook
396  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
397  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
398 
399  //var_dump($tmparray); exit;
400  foreach($tmparray as $key=>$value)
401  {
402  try {
403  if (preg_match('/logo$/',$key)) // Image
404  {
405  //var_dump($value);exit;
406  if (file_exists($value)) $odfHandler->setImage($key, $value);
407  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
408  }
409  else // Text
410  {
411  $odfHandler->setVars($key, $value, true, 'UTF-8');
412  }
413  }
414  catch (OdfException $e)
415  {
416  dol_syslog($e->getMessage(), LOG_INFO);
417  }
418  }
419  // Replace tags of lines
420  try
421  {
422  $foundtagforlines = 1;
423  try {
424  $listlines = $odfHandler->setSegment('lines');
425  }
426  catch(OdfException $e)
427  {
428  // We may arrive here if tags for lines not present into template
429  $foundtagforlines = 0;
430  dol_syslog($e->getMessage(), LOG_INFO);
431  }
432  if ($foundtagforlines)
433  {
434  foreach ($object->lines as $line)
435  {
436  $tmparray=$this->get_substitutionarray_lines($line,$outputlangs);
437  complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
438  // Call the ODTSubstitutionLine hook
439  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray,'line'=>$line);
440  $reshook=$hookmanager->executeHooks('ODTSubstitutionLine',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
441  foreach($tmparray as $key => $val)
442  {
443  try
444  {
445  $listlines->setVars($key, $val, true, 'UTF-8');
446  }
447  catch(OdfException $e)
448  {
449  dol_syslog($e->getMessage(), LOG_INFO);
450  }
451  catch(SegmentException $e)
452  {
453  dol_syslog($e->getMessage(), LOG_INFO);
454  }
455  }
456  $listlines->merge();
457  }
458  $odfHandler->mergeSegment($listlines);
459  }
460  }
461  catch(OdfException $e)
462  {
463  $this->error=$e->getMessage();
464  dol_syslog($this->error, LOG_WARNING);
465  return -1;
466  }
467 
468  // Replace labels translated
469  $tmparray=$outputlangs->get_translations_for_substitutions();
470  foreach($tmparray as $key=>$value)
471  {
472  try {
473  $odfHandler->setVars($key, $value, true, 'UTF-8');
474  }
475  catch(OdfException $e)
476  {
477  dol_syslog($e->getMessage(), LOG_INFO);
478  }
479  }
480 
481  // Call the beforeODTSave hook
482  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
483  $reshook=$hookmanager->executeHooks('beforeODTSave',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
484 
485  // Write new file
486  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
487  try {
488  $odfHandler->exportAsAttachedPDF($file);
489  }catch (Exception $e){
490  $this->error=$e->getMessage();
491  dol_syslog($e->getMessage(), LOG_INFO);
492  return -1;
493  }
494  }
495  else {
496  try {
497  $odfHandler->saveToDisk($file);
498  } catch (Exception $e) {
499  $this->error=$e->getMessage();
500  dol_syslog($e->getMessage(), LOG_INFO);
501  return -1;
502  }
503  }
504  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
505  $reshook=$hookmanager->executeHooks('afterODTCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
506 
507  if (! empty($conf->global->MAIN_UMASK))
508  @chmod($file, octdec($conf->global->MAIN_UMASK));
509 
510  $odfHandler=null; // Destroy object
511 
512  $this->result = array('fullpath'=>$file);
513 
514  return 1; // Success
515  }
516  else
517  {
518  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
519  return -1;
520  }
521  }
522 
523  return -1;
524  }
525 }
img_warning($titlealt='default', $moreatt='')
Show warning logo.
get_substitutionarray_lines($line, $outputlangs)
Define array with couple substitution key => substitution value.
</td >< td class="liste_titre" align="right"></td ></tr >< tr class="liste_titre">< input type="checkbox" onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< tr class="oddeven">< td >< input type="checkbox" class="check" name="' . $i . '"' . $disabled . '></td >< td >< input type="checkbox" class="check" name="choose'.$i.'"></td >< td class="nowrap"></td >< td >< input type="hidden" name="desc' . $i . '" value="' . dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:573
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive=true)
Define array with couple subtitution key => subtitution value.
get_substitutionarray_object($object, $outputlangs, $array_key='object')
Define array with couple substitution key => substitution value.
get_substitutionarray_user($user, $outputlangs)
Define array with couple subtitution key => subtitution value.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
Return array of possible common substitutions.
Class to manage hooks.
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
info($langs)
Return description of a module.
Class to build documents using ODF templates generator.
$version
Dolibarr version of the loaded document string.
Parent class of invoice document generators.
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple subtitution key => subtitution value.
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:59
dol_now($mode='gmt')
Return date for now.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
get_substitutionarray_contact($object, $outputlangs, $array_key='object')
Define array with couple subtitution key => subtitution value.
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)
Define array with couple subtitution key => subtitution value.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
make_substitutions($text, $substitutionarray, $outputlangs=null)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
get_substitutionarray_other($outputlangs)
Define array with couple subtitution key => subtitution value.
Class to manage invoices.
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 "subst...