31 require_once DOL_DOCUMENT_ROOT.
'/core/modules/supplier_invoice/modules_facturefournisseur.php';
32 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
35 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
36 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
53 public $version =
'dolibarr';
63 global $conf, $langs, $mysoc;
66 $langs->loadLangs(array(
"main",
"companies"));
69 $this->
name =
"ODT templates";
70 $this->
description = $langs->trans(
"DocumentModelOdt");
71 $this->scandir =
'SUPPLIER_INVOICE_ADDON_PDF_ODT_PATH';
74 $this->page_largeur = 0;
75 $this->page_hauteur = 0;
76 $this->format = array($this->page_largeur, $this->page_hauteur);
77 $this->marge_gauche = 0;
78 $this->marge_droite = 0;
79 $this->marge_haute = 0;
80 $this->marge_basse = 0;
82 $this->option_logo = 1;
83 $this->option_tva = 0;
84 $this->option_modereg = 0;
85 $this->option_condreg = 0;
86 $this->option_codeproduitservice = 0;
87 $this->option_multilang = 1;
88 $this->option_escompte = 0;
89 $this->option_credit_note = 0;
90 $this->option_freetext = 1;
91 $this->option_draft_watermark = 0;
94 $this->issuer = $mysoc;
95 if (!$this->issuer->country_code) {
96 $this->issuer->country_code = substr($langs->defaultlang, -2);
109 global $conf, $langs;
112 $langs->loadLangs(array(
"errors",
"companies"));
117 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
118 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
119 $texte .=
'<input type="hidden" name="page_y" value="">';
120 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
121 $texte .=
'<input type="hidden" name="param1" value="SUPPLIER_INVOICE_ADDON_PDF_ODT_PATH">';
122 $texte .=
'<table class="nobordernopadding" width="100%">';
125 $texte .=
'<tr><td>';
126 $texttitle = $langs->trans(
"ListOfDirectories");
127 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->SUPPLIER_INVOICE_ADDON_PDF_ODT_PATH)));
130 $listoffiles = array();
131 foreach ($listofdir as $key => $tmpdir) {
132 $tmpdir = trim($tmpdir);
133 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
135 unset($listofdir[$key]);
138 if (!is_dir($tmpdir)) {
139 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
141 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
142 if (count($tmpfiles)) {
143 $listoffiles = array_merge($listoffiles, $tmpfiles);
148 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
150 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
151 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
153 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
154 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
155 $texte .=
'<textarea class="flat" cols="60" name="value1">';
156 $texte .= $conf->global->SUPPLIER_INVOICE_ADDON_PDF_ODT_PATH;
157 $texte .=
'</textarea>';
158 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
159 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
160 $texte .=
'<br></div></div>';
163 $nbofiles = count($listoffiles);
164 if (!empty($conf->global->SUPPLIER_INVOICE_ADDON_PDF_ODT_PATH)) {
165 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
167 $texte .= count($listoffiles);
173 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
175 foreach ($listoffiles as $file) {
176 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=supplier_invoices/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
177 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=SUPPLIER_INVOICE_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
183 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate").
' <input type="file" name="uploadfile">';
184 $texte .=
'<input type="hidden" value="SUPPLIER_INVOICE_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
185 $texte .=
'<input type="submit" class="button small" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
189 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
190 $texte .=
'<span class="opacitymedium">';
191 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
196 $texte .=
'</table>';
214 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
217 global $user, $langs, $conf, $mysoc, $hookmanager;
219 if (empty($srctemplatepath)) {
220 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
225 if (!is_object($hookmanager)) {
226 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
229 $hookmanager->initHooks(array(
'odtgeneration'));
232 if (!is_object($outputlangs)) {
233 $outputlangs = $langs;
235 $sav_charset_output = $outputlangs->charset_output;
236 $outputlangs->charset_output =
'UTF-8';
238 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
240 if ($conf->fournisseur->facture->dir_output) {
241 $object->fetch_thirdparty();
243 if ($object->specimen) {
244 $dir = $conf->fournisseur->facture->dir_output;
245 $file = $dir.
"/SPECIMEN.pdf";
250 $dir = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').$objectref;
251 $file = $dir.
"/".$objectref.
".pdf";
252 if (!empty($conf->global->SUPPLIER_REF_IN_NAME)) {
253 $file = $dir.
"/".$objectref.($objectrefsupplier ?
"_".$objectrefsupplier :
"").
".pdf";
257 if (!file_exists($dir)) {
259 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
264 if (file_exists($dir)) {
266 $newfile = basename($srctemplatepath);
267 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
268 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
269 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
270 $newfiletmp = $objectref.
'_'.$newfiletmp;
273 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
276 if ($format ==
'1') {
277 $format =
'%Y%m%d%H%M%S';
281 $filename = $newfiletmp.
'.'.$newfileformat;
283 $file = $dir.
'/'.$filename;
289 dol_mkdir($conf->fournisseur->facture->dir_temp);
290 if (!is_writable($conf->fournisseur->facture->dir_temp)) {
291 $this->error =
"Failed to write in temp directory ".$conf->fournisseur->facture->dir_temp;
292 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
298 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
299 if (count($arrayidcontact) > 0) {
301 $result = $object->fetch_contact($arrayidcontact[0]);
305 $contactobject =
null;
306 if (!empty($usecontact)) {
308 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT))) {
309 $object->contact->fetch_thirdparty();
310 $socobject = $object->contact->thirdparty;
311 $contactobject = $object->contact;
313 $socobject = $object->thirdparty;
315 $contactobject = $object->contact;
318 $socobject = $object->thirdparty;
322 $substitutionarray = array(
323 '__FROM_NAME__' => $this->issuer->name,
324 '__FROM_EMAIL__' => $this->issuer->email,
325 '__TOTAL_TTC__' => $object->total_ttc,
326 '__TOTAL_HT__' => $object->total_ht,
327 '__TOTAL_VAT__' => $object->total_tva
331 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
332 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
336 $paramfreetext =
'INVOICE_FREE_TEXT';
337 if (!empty($conf->global->$paramfreetext)) {
342 require_once ODTPHP_PATH.
'odf.php';
344 $odfHandler =
new Odf(
347 'PATH_TO_TMP' => $conf->fournisseur->dir_temp,
348 'ZIP_PROXY' =>
'PclZipProxy',
349 'DELIMITER_LEFT' =>
'{',
350 'DELIMITER_RIGHT' =>
'}'
354 $this->error = $e->getMessage();
367 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
368 }
catch (OdfException $e) {
381 $array_thirdparty_contact = array();
382 if ($usecontact && is_object($contactobject)) {
386 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
390 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
391 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
393 foreach ($tmparray as $key => $value) {
395 if (preg_match(
'/logo$/', $key)) {
396 if (file_exists($value)) {
397 $odfHandler->setImage($key, $value);
399 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
403 $odfHandler->setVars($key, $value,
true,
'UTF-8');
405 }
catch (OdfException $e) {
411 $foundtagforlines = 1;
413 $listlines = $odfHandler->setSegment(
'lines');
414 }
catch (OdfException $e) {
416 $foundtagforlines = 0;
419 if ($foundtagforlines) {
421 foreach ($object->lines as $line) {
426 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
427 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
428 foreach ($tmparray as $key => $val) {
430 $listlines->setVars($key, $val,
true,
'UTF-8');
431 }
catch (OdfException $e) {
433 }
catch (SegmentException $e) {
439 $odfHandler->mergeSegment($listlines);
441 }
catch (OdfException $e) {
442 $this->error = $e->getMessage();
448 $tmparray = $outputlangs->get_translations_for_substitutions();
449 foreach ($tmparray as $key => $value) {
451 $odfHandler->setVars($key, $value,
true,
'UTF-8');
452 }
catch (OdfException $e) {
459 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
460 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
463 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
465 $odfHandler->exportAsAttachedPDF($file);
467 $this->error = $e->getMessage();
473 $odfHandler->saveToDisk($file);
475 $this->error = $e->getMessage();
481 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
482 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
488 $this->result = array(
'fullpath'=>$file);
492 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);