48 public $version =
'dolibarr';
61 $langs->loadLangs(array(
"main",
"companies"));
64 $this->
name =
"ODT/ODS templates";
65 $this->
description = $langs->trans(
"DocumentModelOdt");
66 $this->scandir =
'FACTURE_ADDON_PDF_ODT_PATH';
70 $this->page_largeur = 0;
71 $this->page_hauteur = 0;
72 $this->format = array($this->page_largeur, $this->page_hauteur);
73 $this->marge_gauche = 0;
74 $this->marge_droite = 0;
75 $this->marge_haute = 0;
76 $this->marge_basse = 0;
78 $this->option_logo = 1;
79 $this->option_tva = 0;
80 $this->option_modereg = 1;
81 $this->option_condreg = 1;
82 $this->option_multilang = 1;
83 $this->option_escompte = 0;
84 $this->option_credit_note = 0;
85 $this->option_freetext = 1;
86 $this->option_draft_watermark = 0;
95 if (!$this->emetteur->country_code) {
96 $this->emetteur->country_code = substr($langs->defaultlang, -2);
109 global $conf, $langs;
112 $langs->loadLangs(array(
"errors",
"companies"));
114 $form =
new Form($this->db);
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="FACTURE_ADDON_PDF_ODT_PATH">';
122 $texte .=
'<table class="nobordernopadding" width="100%">';
125 $texte .=
'<tr><td valign="middle">';
126 $texttitle = $langs->trans(
"ListOfDirectories");
127 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->FACTURE_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);
133 unset($listofdir[$key]);
136 if (!is_dir($tmpdir)) {
137 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir),
'');
139 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
140 if (count($tmpfiles)) {
141 $listoffiles = array_merge($listoffiles, $tmpfiles);
145 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
146 $texthelp .=
'<br><br><span class="opacitymedium">'.$langs->trans(
"ExampleOfDirectoriesForModelGen").
'</span>';
148 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
149 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
151 $texte .= $form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1, 3, $this->
name);
152 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
153 $texte .=
'<textarea class="flat textareafordir" spellcheck="false" cols="60" name="value1">';
155 $texte .=
'</textarea>';
156 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
157 $texte .=
'<input type="submit" class="button button-edit reposition smallpaddingimp" name="modify" value="'.dol_escape_htmltag($langs->trans(
"Modify")).
'">';
158 $texte .=
'<br></div></div>';
161 $nbofiles = count($listoffiles);
163 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
165 $texte .= count($listoffiles);
170 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
172 foreach ($listoffiles as $file) {
173 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=invoices/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
174 $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>';
180 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
182 $maxmin = $maxfilesizearray[
'maxmin'];
184 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
186 $texte .=
' <input type="file" name="uploadfile">';
187 $texte .=
'<input type="hidden" value="FACTURE_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
188 $texte .=
'<input type="submit" class="button smallpaddingimp reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
194 $texte .=
'</table>';
212 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
215 global $user, $langs, $conf,
$mysoc, $hookmanager;
217 if (empty($srctemplatepath)) {
218 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
223 if (!is_object($hookmanager)) {
224 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
227 $hookmanager->initHooks(array(
'odtgeneration'));
230 if (!is_object($outputlangs)) {
231 $outputlangs = $langs;
233 $sav_charset_output = $outputlangs->charset_output;
234 $outputlangs->charset_output =
'UTF-8';
237 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
239 if ($conf->facture->dir_output) {
253 $dir = empty($conf->facture->multidir_output[
$object->entity ?? $conf->entity]) ? $conf->facture->dir_output : $conf->facture->multidir_output[
$object->entity ?? $conf->entity];
255 if (!preg_match(
'/specimen/i', $objectref)) {
256 $dir .=
"/".$objectref;
258 $file = $dir.
"/".$objectref.
".odt";
260 if (!file_exists($dir)) {
262 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
267 if (file_exists($dir)) {
269 $newfile = basename($srctemplatepath);
270 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
271 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
272 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
275 $newfiletmp = $objectref;
277 $newfiletmp = $objectref .
'_' . $newfiletmp;
281 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
284 if ($format ==
'1') {
285 $format =
'%Y%m%d%H%M%S';
289 $filename = $newfiletmp .
'.' . $newfileformat;
291 $file = $dir .
'/' . $filename;
299 if (!is_writable($conf->facture->dir_temp)) {
300 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->facture->dir_temp);
301 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
307 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
308 if (count($arrayidcontact) > 0) {
310 $result =
$object->fetch_contact($arrayidcontact[0]);
314 $contactobject =
null;
315 if (!empty($usecontact)) {
317 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
318 $object->contact->fetch_thirdparty();
319 $socobject =
$object->contact->thirdparty;
320 $contactobject =
$object->contact;
322 $socobject =
$object->thirdparty;
324 $contactobject =
$object->contact;
327 $socobject =
$object->thirdparty;
331 $object->fetchObjectLinked(0,
'',
null,
'');
334 $propal_object =
null;
335 if (!empty(
$object->linkedObjects[
'propal'])) {
336 $array_propal_object =
$object->linkedObjects[
'propal'];
337 if (isset($array_propal_object) && is_array($array_propal_object) && count($array_propal_object) > 0) {
338 $tmparrayofvalue = array_values($array_propal_object);
339 $propal_object = $tmparrayofvalue[0];
344 $categoryOfOperation = 0;
347 foreach (
$object->lines as $line) {
350 if (preg_match(
'/^\((.*)\)$/', $line->desc, $reg)) {
351 if ($reg[1] ==
'DEPOSIT') {
362 if ($categoryOfOperation < 2) {
363 $lineProductType = $line->product_type;
369 if ($nbProduct > 0 && $nbService > 0) {
371 $categoryOfOperation = 2;
377 if ($categoryOfOperation <= 0) {
379 if ($nbProduct == 0 && $nbService > 0) {
380 $categoryOfOperation = 1;
385 $substitutionarray = array(
386 '__FROM_NAME__' => $this->emetteur->name,
387 '__FROM_EMAIL__' => $this->emetteur->email,
388 '__TOTAL_TTC__' =>
$object->total_ttc,
389 '__TOTAL_HT__' =>
$object->total_ht,
390 '__TOTAL_VAT__' =>
$object->total_tva
394 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
395 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
399 $paramfreetext =
'INVOICE_FREE_TEXT';
405 require_once ODTPHP_PATH.
'odf.php';
407 $odfHandler =
new Odf(
410 'PATH_TO_TMP' => $conf->facture->dir_temp,
412 'DELIMITER_LEFT' =>
'{',
413 'DELIMITER_RIGHT' =>
'}'
417 $this->error = $e->getMessage();
430 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
431 }
catch (OdfException $e) {
436 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
438 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
442 $array_propal = is_object($propal_object) ? $this->get_substitutionarray_object($propal_object, $outputlangs,
'propal') : array();
445 $array_thirdparty_contact = array();
446 if ($usecontact && is_object($contactobject)) {
450 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_propal, $array_other, $array_thirdparty_contact);
467 $tmparray[
'object_productorservice_operation'] = $outputlangs->transnoentities(
"MentionCategoryOfOperations" . $categoryOfOperation);
471 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
472 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
482 foreach ($tmparray as $key => $value) {
484 if (preg_match(
'/logo$/', $key)) {
486 if (file_exists($value)) {
487 $odfHandler->setImage($key, $value, $ratio);
489 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
493 $odfHandler->setVars($key, $value,
true,
'UTF-8');
495 }
catch (OdfException $e) {
500 $foundtagforlines = 1;
502 $listlines = $odfHandler->setSegment(
'lines');
503 }
catch (OdfExceptionSegmentNotFound $e) {
505 $foundtagforlines = 0;
508 if ($foundtagforlines) {
510 foreach (
$object->lines as $line) {
516 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
517 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
518 foreach ($tmparray as $key => $val) {
520 $listlines->setVars($key, $val,
true,
'UTF-8');
521 }
catch (SegmentException $e) {
528 $odfHandler->mergeSegment($listlines);
529 }
catch (OdfException $e) {
530 $this->error = $e->getMessage();
537 $tmparray = $outputlangs->get_translations_for_substitutions();
538 foreach ($tmparray as $key => $value) {
540 $odfHandler->setVars($key, $value,
true,
'UTF-8');
541 }
catch (OdfException $e) {
547 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
548 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
553 $odfHandler->exportAsAttachedPDF($file);
555 $this->error = $e->getMessage();
561 $odfHandler->saveToDisk($file);
563 $this->error = $e->getMessage();
568 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
569 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
575 $this->result = array(
'fullpath' => $file);
579 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);