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';
52 public $version =
'dolibarr';
62 global $conf, $langs, $mysoc;
65 $langs->loadLangs(array(
"main",
"companies"));
68 $this->
name =
"ODT/ODS templates";
69 $this->
description = $langs->trans(
"DocumentModelOdt");
70 $this->scandir =
'FACTURE_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_multilang = 1;
87 $this->option_escompte = 0;
88 $this->option_credit_note = 0;
89 $this->option_freetext = 1;
90 $this->option_draft_watermark = 0;
93 $this->emetteur = $mysoc;
94 if (!$this->emetteur->country_code) {
95 $this->emetteur->country_code = substr($langs->defaultlang, -2);
108 global $conf, $langs;
111 $langs->loadLangs(array(
"errors",
"companies"));
116 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
117 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
118 $texte .=
'<input type="hidden" name="page_y" value="">';
119 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
120 $texte .=
'<input type="hidden" name="param1" value="FACTURE_ADDON_PDF_ODT_PATH">';
121 $texte .=
'<table class="nobordernopadding" width="100%">';
124 $texte .=
'<tr><td valign="middle">';
125 $texttitle = $langs->trans(
"ListOfDirectories");
126 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->FACTURE_ADDON_PDF_ODT_PATH)));
127 $listoffiles = array();
128 foreach ($listofdir as $key => $tmpdir) {
129 $tmpdir = trim($tmpdir);
130 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
132 unset($listofdir[$key]);
135 if (!is_dir($tmpdir)) {
136 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
138 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
139 if (count($tmpfiles)) {
140 $listoffiles = array_merge($listoffiles, $tmpfiles);
144 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
146 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
147 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
149 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
150 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
151 $texte .=
'<textarea class="flat" cols="60" name="value1">';
152 $texte .= $conf->global->FACTURE_ADDON_PDF_ODT_PATH;
153 $texte .=
'</textarea>';
154 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
155 $texte .=
'<input type="submit" class="button small reposition" name="modify"value="'.$langs->trans(
"Modify").
'">';
156 $texte .=
'<br></div></div>';
159 $nbofiles = count($listoffiles);
160 if (!empty($conf->global->FACTURE_ADDON_PDF_ODT_PATH)) {
161 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
163 $texte .= count($listoffiles);
168 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
170 foreach ($listoffiles as $file) {
171 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=invoices/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
172 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=FACTURE_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
178 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
180 $maxmin = $maxfilesizearray[
'maxmin'];
182 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
184 $texte .=
' <input type="file" name="uploadfile">';
185 $texte .=
'<input type="hidden" value="FACTURE_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
186 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
190 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
191 $texte .=
'<span class="opacitymedium">';
192 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
197 $texte .=
'</table>';
215 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
218 global $user, $langs, $conf, $mysoc, $hookmanager;
220 if (empty($srctemplatepath)) {
221 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
226 if (!is_object($hookmanager)) {
227 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
230 $hookmanager->initHooks(array(
'odtgeneration'));
233 if (!is_object($outputlangs)) {
234 $outputlangs = $langs;
236 $sav_charset_output = $outputlangs->charset_output;
237 $outputlangs->charset_output =
'UTF-8';
240 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
242 if ($conf->facture->dir_output) {
244 if (!is_object($object)) {
246 $object =
new Facture($this->db);
247 $result = $object->fetch($id);
254 $object->fetch_thirdparty();
256 $dir = $conf->facture->dir_output;
258 if (!preg_match(
'/specimen/i', $objectref)) {
259 $dir .=
"/".$objectref;
261 $file = $dir.
"/".$objectref.
".odt";
263 if (!file_exists($dir)) {
265 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
270 if (file_exists($dir)) {
272 $newfile = basename($srctemplatepath);
273 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
274 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
275 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
277 $newfiletmp = $objectref .
'_' . $newfiletmp;
280 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
283 if ($format ==
'1') {
284 $format =
'%Y%m%d%H%M%S';
288 $filename = $newfiletmp .
'.' . $newfileformat;
290 $file = $dir .
'/' . $filename;
298 if (!is_writable($conf->facture->dir_temp)) {
299 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->facture->dir_temp);
300 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
306 $arrayidcontact = $object->getIdContact(
'external',
'BILLING');
307 if (count($arrayidcontact) > 0) {
309 $result = $object->fetch_contact($arrayidcontact[0]);
313 $contactobject =
null;
314 if (!empty($usecontact)) {
316 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))) {
317 $object->contact->fetch_thirdparty();
318 $socobject = $object->contact->thirdparty;
319 $contactobject = $object->contact;
321 $socobject = $object->thirdparty;
323 $contactobject = $object->contact;
326 $socobject = $object->thirdparty;
330 $object->fetchObjectLinked(
'',
'',
'',
'');
333 $propal_object =
null;
334 if (!empty($object->linkedObjects[
'propal'])) {
335 $array_propal_object = $object->linkedObjects[
'propal'];
336 if (isset($array_propal_object) && is_array($array_propal_object) && count($array_propal_object) > 0) {
337 $tmparrayofvalue = array_values($array_propal_object);
338 $propal_object = $tmparrayofvalue[0];
343 $substitutionarray = array(
344 '__FROM_NAME__' => $this->emetteur->name,
345 '__FROM_EMAIL__' => $this->emetteur->email,
346 '__TOTAL_TTC__' => $object->total_ttc,
347 '__TOTAL_HT__' => $object->total_ht,
348 '__TOTAL_VAT__' => $object->total_tva
352 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
353 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
357 $paramfreetext =
'INVOICE_FREE_TEXT';
358 if (!empty($conf->global->$paramfreetext)) {
363 require_once ODTPHP_PATH.
'odf.php';
365 $odfHandler =
new Odf(
368 'PATH_TO_TMP' => $conf->facture->dir_temp,
369 'ZIP_PROXY' =>
'PclZipProxy',
370 'DELIMITER_LEFT' =>
'{',
371 'DELIMITER_RIGHT' =>
'}'
375 $this->error = $e->getMessage();
388 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
389 }
catch (OdfException $e) {
403 $array_thirdparty_contact = array();
404 if ($usecontact && is_object($contactobject)) {
408 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_propal, $array_other, $array_thirdparty_contact);
419 $tmparray[
'object_MONTH_TEXT'] =
dol_print_date($object->date,
'%B');
426 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
427 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
430 foreach ($tmparray as $key => $value) {
432 if (preg_match(
'/logo$/', $key)) {
434 if (file_exists($value)) {
435 $odfHandler->setImage($key, $value);
437 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
441 $odfHandler->setVars($key, $value,
true,
'UTF-8');
443 }
catch (OdfException $e) {
449 $foundtagforlines = 1;
451 $listlines = $odfHandler->setSegment(
'lines');
452 }
catch (OdfException $e) {
454 $foundtagforlines = 0;
457 if ($foundtagforlines) {
459 foreach ($object->lines as $line) {
464 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
465 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
466 foreach ($tmparray as $key => $val) {
468 $listlines->setVars($key, $val,
true,
'UTF-8');
469 }
catch (OdfException $e) {
471 }
catch (SegmentException $e) {
477 $odfHandler->mergeSegment($listlines);
479 }
catch (OdfException $e) {
480 $this->error = $e->getMessage();
486 $tmparray = $outputlangs->get_translations_for_substitutions();
487 foreach ($tmparray as $key => $value) {
489 $odfHandler->setVars($key, $value,
true,
'UTF-8');
490 }
catch (OdfException $e) {
496 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
497 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
500 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
502 $odfHandler->exportAsAttachedPDF($file);
504 $this->error = $e->getMessage();
510 $odfHandler->saveToDisk($file);
512 $this->error = $e->getMessage();
517 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
518 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
524 $this->result = array(
'fullpath'=>$file);
528 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);