27 require_once DOL_DOCUMENT_ROOT.
'/core/modules/stock/modules_stock.php';
28 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
49 public $version =
'dolibarr';
58 global $conf, $langs, $mysoc;
61 $langs->loadLangs(array(
"main",
"companies"));
64 $this->
name =
"ODT templates";
65 $this->
description = $langs->trans(
"DocumentModelOdt");
66 $this->scandir =
'STOCK_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 = 0;
81 $this->option_condreg = 0;
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;
89 $this->emetteur = $mysoc;
90 if (!$this->emetteur->country_code) {
91 $this->emetteur->country_code = substr($langs->defaultlang, -2);
104 global $conf, $langs;
107 $langs->loadLangs(array(
"errors",
"companies"));
112 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST" enctype="multipart/form-data">';
113 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
114 $texte .=
'<input type="hidden" name="page_y" value="">';
115 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
116 $texte .=
'<input type="hidden" name="param1" value="STOCK_ADDON_PDF_ODT_PATH">';
117 $texte .=
'<table class="nobordernopadding" width="100%">';
120 $texte .=
'<tr><td>';
121 $texttitle = $langs->trans(
"ListOfDirectories");
122 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', trim($conf->global->STOCK_ADDON_PDF_ODT_PATH)));
123 $listoffiles = array();
124 foreach ($listofdir as $key => $tmpdir) {
125 $tmpdir = trim($tmpdir);
126 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
128 unset($listofdir[$key]);
131 if (!is_dir($tmpdir)) {
132 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
134 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
135 if (count($tmpfiles)) {
136 $listoffiles = array_merge($listoffiles, $tmpfiles);
140 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
142 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
143 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
145 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
146 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
147 $texte .=
'<textarea class="flat" cols="60" name="value1">';
148 $texte .= $conf->global->STOCK_ADDON_PDF_ODT_PATH;
149 $texte .=
'</textarea>';
150 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
151 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
152 $texte .=
'<br></div></div>';
155 $nbofiles = count($listoffiles);
156 if (!empty($conf->global->STOCK_ADDON_PDF_ODT_PATH)) {
157 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
159 $texte .= count($listoffiles);
165 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
167 foreach ($listoffiles as $file) {
168 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=stocks/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
169 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=STOCK_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
175 $texte .=
'<div>'.$langs->trans(
"UploadNewTemplate");
177 $maxmin = $maxfilesizearray[
'maxmin'];
179 $texte .=
'<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).
'">';
181 $texte .=
' <input type="file" name="uploadfile">';
182 $texte .=
'<input type="hidden" value="STOCK_ADDON_PDF_ODT_PATH" name="keyforuploaddir">';
183 $texte .=
'<input type="submit" class="button small reposition" value="'.dol_escape_htmltag($langs->trans(
"Upload")).
'" name="upload">';
188 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
189 $texte .=
'<span class="opacitymedium">';
190 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
195 $texte .=
'</table>';
213 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
216 global $stock, $langs, $conf, $mysoc, $hookmanager, $user;
218 if (empty($srctemplatepath)) {
219 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
224 if (!is_object($hookmanager)) {
225 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
228 $hookmanager->initHooks(array(
'odtgeneration'));
231 if (!is_object($outputlangs)) {
232 $outputlangs = $langs;
234 $sav_charset_output = $outputlangs->charset_output;
235 $outputlangs->charset_output =
'UTF-8';
238 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
240 if ($conf->product->dir_output) {
242 if (!is_object($object)) {
245 $result = $object->fetch($id);
253 $supplierprices = $stockFournisseur->list_stock_fournisseur_price($object->id);
254 $object->supplierprices = $supplierprices;
256 $dir = $conf->product->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;
297 if (!is_writable($conf->product->dir_temp)) {
298 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
299 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
305 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
306 if (count($arrayidcontact) > 0) {
308 $result = $object->fetch_contact($arrayidcontact[0]);
312 $contactobject =
null;
313 if (!empty($usecontact)) {
315 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))) {
316 $object->contact->fetch_thirdparty();
317 $socobject = $object->contact->thirdparty;
318 $contactobject = $object->contact;
320 $socobject = $object->thirdparty;
322 $contactobject = $object->contact;
325 $socobject = $object->thirdparty;
328 $substitutionarray = array(
329 '__FROM_NAME__' => $this->emetteur->name,
330 '__FROM_EMAIL__' => $this->emetteur->email,
331 '__TOTAL_TTC__' => $object->total_ttc,
332 '__TOTAL_HT__' => $object->total_ht,
333 '__TOTAL_VAT__' => $object->total_tva
337 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
338 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
342 $paramfreetext =
'stock_FREE_TEXT';
343 if (!empty($conf->global->$paramfreetext)) {
348 require_once ODTPHP_PATH.
'odf.php';
350 $odfHandler =
new Odf(
353 'PATH_TO_TMP' => $conf->product->dir_temp,
354 'ZIP_PROXY' =>
'PclZipProxy',
355 'DELIMITER_LEFT' =>
'{',
356 'DELIMITER_RIGHT' =>
'}'
360 $this->error = $e->getMessage();
370 $object->fetch_optionals();
374 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
375 }
catch (OdfException $e) {
388 $array_thirdparty_contact = array();
389 if ($usecontact && is_object($contactobject)) {
393 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
397 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
398 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
400 foreach ($tmparray as $key => $value) {
402 if (preg_match(
'/logo$/', $key)) {
403 if (file_exists($value)) {
404 $odfHandler->setImage($key, $value);
406 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
410 $odfHandler->setVars($key, $value,
true,
'UTF-8');
412 }
catch (OdfException $e) {
418 $listlines = $odfHandler->setSegment(
'supplierprices');
419 if (!empty($object->supplierprices)) {
420 foreach ($object->supplierprices as $supplierprice) {
424 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$array_lines,
'line'=>$supplierprice);
425 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
426 foreach ($array_lines as $key => $val) {
428 $listlines->setVars($key, $val,
true,
'UTF-8');
429 }
catch (OdfException $e) {
431 }
catch (SegmentException $e) {
438 $odfHandler->mergeSegment($listlines);
439 }
catch (OdfException $e) {
440 $this->error = $e->getMessage();
446 $tmparray = $outputlangs->get_translations_for_substitutions();
447 foreach ($tmparray as $key => $value) {
449 $odfHandler->setVars($key, $value,
true,
'UTF-8');
450 }
catch (OdfException $e) {
456 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
457 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
460 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
462 $odfHandler->exportAsAttachedPDF($file);
464 $this->error = $e->getMessage();
470 $odfHandler->saveToDisk($file);
472 $this->error = $e->getMessage();
478 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
484 $this->result = array(
'fullpath'=>$file);
488 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);