47 public $version =
'dolibarr';
57 global $langs, $mysoc;
60 $langs->loadLangs(array(
"main",
"companies"));
63 $this->
name =
"ODT/ODS templates";
64 $this->
description = $langs->trans(
"DocumentModelOdt");
65 $this->update_main_doc_field = 1;
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;
88 if ($mysoc ===
null) {
94 $this->emetteur = $mysoc;
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->facture->dir_output :
$conf->facture->multidir_output[
$object->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);
274 $newfiletmp = $objectref .
'_' . $newfiletmp;
277 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
280 if ($format ==
'1') {
281 $format =
'%Y%m%d%H%M%S';
285 $filename = $newfiletmp .
'.' . $newfileformat;
287 $file = $dir .
'/' . $filename;
295 if (!is_writable(
$conf->facture->dir_temp)) {
296 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->facture->dir_temp);
297 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
303 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
304 if (count($arrayidcontact) > 0) {
306 $result =
$object->fetch_contact($arrayidcontact[0]);
310 $contactobject =
null;
311 if (!empty($usecontact)) {
314 $object->contact->fetch_thirdparty();
315 $socobject =
$object->contact->thirdparty;
316 $contactobject =
$object->contact;
318 $socobject =
$object->thirdparty;
320 $contactobject =
$object->contact;
323 $socobject =
$object->thirdparty;
327 $object->fetchObjectLinked(0,
'',
null,
'');
330 $propal_object =
null;
331 if (!empty(
$object->linkedObjects[
'propal'])) {
332 $array_propal_object =
$object->linkedObjects[
'propal'];
333 if (isset($array_propal_object) && is_array($array_propal_object) && count($array_propal_object) > 0) {
334 $tmparrayofvalue = array_values($array_propal_object);
335 $propal_object = $tmparrayofvalue[0];
340 $categoryOfOperation = 0;
343 foreach (
$object->lines as $line) {
345 if ($categoryOfOperation < 2) {
346 $lineProductType = $line->product_type;
352 if ($nbProduct > 0 && $nbService > 0) {
354 $categoryOfOperation = 2;
360 if ($categoryOfOperation <= 0) {
362 if ($nbProduct == 0 && $nbService > 0) {
363 $categoryOfOperation = 1;
368 $substitutionarray = array(
369 '__FROM_NAME__' => $this->emetteur->name,
370 '__FROM_EMAIL__' => $this->emetteur->email,
371 '__TOTAL_TTC__' =>
$object->total_ttc,
372 '__TOTAL_HT__' =>
$object->total_ht,
373 '__TOTAL_VAT__' =>
$object->total_tva
377 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
378 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
382 $paramfreetext =
'INVOICE_FREE_TEXT';
388 require_once ODTPHP_PATH.
'odf.php';
390 $odfHandler =
new Odf(
393 'PATH_TO_TMP' =>
$conf->facture->dir_temp,
395 'DELIMITER_LEFT' =>
'{',
396 'DELIMITER_RIGHT' =>
'}'
400 $this->error = $e->getMessage();
413 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
414 }
catch (OdfException $e) {
419 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
421 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
425 $array_propal = is_object($propal_object) ? $this->get_substitutionarray_object($propal_object, $outputlangs,
'propal') : array();
428 $array_thirdparty_contact = array();
429 if ($usecontact && is_object($contactobject)) {
433 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_propal, $array_other, $array_thirdparty_contact);
450 $tmparray[
'object_productorservice_operation'] = $outputlangs->transnoentities(
"MentionCategoryOfOperations" . $categoryOfOperation);
454 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
455 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
465 foreach ($tmparray as $key => $value) {
467 if (preg_match(
'/logo$/', $key)) {
469 if (file_exists($value)) {
470 $odfHandler->setImage($key, $value, $ratio);
472 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
476 $odfHandler->setVars($key, $value,
true,
'UTF-8');
478 }
catch (OdfException $e) {
483 $foundtagforlines = 1;
485 $listlines = $odfHandler->setSegment(
'lines');
486 }
catch (OdfExceptionSegmentNotFound $e) {
488 $foundtagforlines = 0;
491 if ($foundtagforlines) {
493 foreach (
$object->lines as $line) {
499 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
500 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
501 foreach ($tmparray as $key => $val) {
503 $listlines->setVars($key, $val,
true,
'UTF-8');
504 }
catch (SegmentException $e) {
511 $odfHandler->mergeSegment($listlines);
512 }
catch (OdfException $e) {
513 $this->error = $e->getMessage();
520 $tmparray = $outputlangs->get_translations_for_substitutions();
521 foreach ($tmparray as $key => $value) {
523 $odfHandler->setVars($key, $value,
true,
'UTF-8');
524 }
catch (OdfException $e) {
530 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
531 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
536 $odfHandler->exportAsAttachedPDF($file);
538 $this->error = $e->getMessage();
544 $odfHandler->saveToDisk($file);
546 $this->error = $e->getMessage();
551 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
552 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
558 $this->result = array(
'fullpath' => $file);
562 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);