28 require_once DOL_DOCUMENT_ROOT.
'/core/modules/supplier_proposal/modules_supplier_proposal.php';
29 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/doc.lib.php';
51 public $phpmin = array(7, 0);
57 public $version =
'dolibarr';
67 global $conf, $langs, $mysoc;
70 $langs->loadLangs(array(
"main",
"companies"));
73 $this->
name =
"ODT templates";
74 $this->
description = $langs->trans(
"DocumentModelOdt");
75 $this->scandir =
'SUPPLIER_PROPOSAL_ADDON_PDF_ODT_PATH';
79 $this->page_largeur = 0;
80 $this->page_hauteur = 0;
81 $this->format = array($this->page_largeur, $this->page_hauteur);
82 $this->marge_gauche = 0;
83 $this->marge_droite = 0;
84 $this->marge_haute = 0;
85 $this->marge_basse = 0;
87 $this->option_logo = 1;
88 $this->option_tva = 0;
89 $this->option_modereg = 0;
90 $this->option_condreg = 0;
91 $this->option_multilang = 1;
92 $this->option_escompte = 0;
93 $this->option_credit_note = 0;
94 $this->option_freetext = 1;
95 $this->option_draft_watermark = 0;
98 $this->emetteur = $mysoc;
99 if (!$this->emetteur->country_code) {
100 $this->emetteur->country_code = substr($langs->defaultlang, -2);
113 global $conf, $langs;
116 $langs->loadLangs(array(
'companies',
'errors'));
120 $odtChosen =
getDolGlobalInt(
'MAIN_SUPPLIER_PROPOSAL_CHOOSE_ODT_DOCUMENT') > 0;
124 $texte .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
125 $texte .=
'<input type="hidden" name="token" value="'.newToken().
'">';
126 $texte .=
'<input type="hidden" name="page_y" value="">';
127 $texte .=
'<input type="hidden" name="action" value="setModuleOptions">';
128 $texte .=
'<input type="hidden" name="param1" value="SUPPLIER_PROPOSAL_ADDON_PDF_ODT_PATH">';
130 $texte .=
'<input type="hidden" name="param2" value="SUPPLIER_PROPOSAL_ADDON_PDF_ODT_DEFAULT">';
131 $texte .=
'<input type="hidden" name="param3" value="SUPPLIER_PROPOSAL_ADDON_PDF_ODT_TOBILL">';
132 $texte .=
'<input type="hidden" name="param4" value="SUPPLIER_PROPOSAL_ADDON_PDF_ODT_CLOSED">';
134 $texte .=
'<table class="nobordernopadding" width="100%">';
137 $texte .=
'<tr><td>';
138 $texttitle = $langs->trans(
"ListOfDirectories");
139 $listofdir = explode(
',', preg_replace(
'/[\r\n]+/',
',', $odtPath));
140 $listoffiles = array();
141 foreach ($listofdir as $key => $tmpdir) {
142 $tmpdir = trim($tmpdir);
143 $tmpdir = preg_replace(
'/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir);
145 unset($listofdir[$key]);
148 if (!is_dir($tmpdir)) {
149 $texttitle .=
img_warning($langs->trans(
"ErrorDirNotFound", $tmpdir), 0);
151 $tmpfiles =
dol_dir_list($tmpdir,
'files', 0,
'\.(ods|odt)');
152 if (count($tmpfiles)) {
153 $listoffiles = array_merge($listoffiles, $tmpfiles);
157 $texthelp = $langs->trans(
"ListOfDirectoriesForModelGenODT");
159 $texthelp .=
'<br>'.$langs->trans(
"FollowingSubstitutionKeysCanBeUsed").
'<br>';
160 $texthelp .= $langs->transnoentitiesnoconv(
"FullListOnOnlineDocumentation");
162 $texte .=
$form->textwithpicto($texttitle, $texthelp, 1,
'help',
'', 1);
163 $texte .=
'<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
164 $texte .=
'<textarea class="flat" cols="60" name="value1">';
166 $texte .=
'</textarea>';
167 $texte .=
'</div><div style="display: inline-block; vertical-align: middle;">';
168 $texte .=
'<input type="submit" class="button small reposition" name="modify" value="'.$langs->trans(
"Modify").
'">';
169 $texte .=
'<br></div></div>';
172 $nbofiles = count($listoffiles);
173 if (!empty($odtPath)) {
174 $texte .= $langs->trans(
"NumberOfModelFilesFound").
': <b>';
176 $texte .= count($listoffiles);
182 $texte .=
'<div id="div_'.get_class($this).
'" class="hiddenx">';
184 foreach ($listoffiles as $file) {
185 $texte .=
'- '.$file[
'name'].
' <a href="'.DOL_URL_ROOT.
'/document.php?modulepart=doctemplates&file=supplier_proposal/'.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'listlight').
'</a>';
186 $texte .=
' <a class="reposition" href="'.$_SERVER[
"PHP_SELF"].
'?modulepart=doctemplates&keyforuploaddir=SUPPLIER_PROPOSAL_ADDON_PDF_ODT_PATH&action=deletefile&token='.
newToken().
'&file='.urlencode(basename($file[
'name'])).
'">'.
img_picto(
'',
'delete').
'</a>';
194 $texte .=
'<table width="50%;">';
196 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelSupplierProposalCreate").
'</td>';
197 $texte .=
'<td colspan="">';
199 $texte .=
"</td></tr>";
202 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelSupplierProposalToBill").
'</td>';
203 $texte .=
'<td colspan="">';
205 $texte .=
"</td></tr>";
208 $texte .=
'<td width="60%;">'.$langs->trans(
"DefaultModelSupplierProposalClosed").
'</td>';
209 $texte .=
'<td colspan="">';
211 $texte .=
"</td></tr>";
212 $texte .=
'</table>';
218 $texte .=
'<td rowspan="2" class="tdtop hideonsmartphone">';
219 $texte .=
'<span class="opacitymedium">';
220 $texte .= $langs->trans(
"ExampleOfDirectoriesForModelGen");
225 $texte .=
'</table>';
243 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
246 global $user, $langs, $conf, $mysoc, $hookmanager;
248 if (empty($srctemplatepath)) {
249 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
254 if (!is_object($hookmanager)) {
255 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
258 $hookmanager->initHooks(array(
'odtgeneration'));
261 if (!is_object($outputlangs)) {
262 $outputlangs = $langs;
264 $sav_charset_output = $outputlangs->charset_output;
265 $outputlangs->charset_output =
'UTF-8';
268 $outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"dict"));
270 if ($conf->supplier_proposal->dir_output) {
272 if (!is_object($object)) {
275 $result = $object->fetch($id);
282 $object->fetch_thirdparty();
284 $dir = $conf->supplier_proposal->dir_output;
286 if (!preg_match(
'/specimen/i', $objectref)) {
287 $dir .=
"/".$objectref;
289 $file = $dir.
"/".$objectref.
".odt";
291 if (!file_exists($dir)) {
293 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
298 if (file_exists($dir)) {
300 $newfile = basename($srctemplatepath);
301 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
302 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
303 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
305 $newfiletmp = $objectref .
'_' . $newfiletmp;
308 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
309 if (!empty($conf->global->MAIN_DOC_USE_TIMING)) {
310 $format = $conf->global->MAIN_DOC_USE_TIMING;
311 if ($format ==
'1') {
312 $format =
'%Y%m%d%H%M%S';
316 $filename = $newfiletmp .
'.' . $newfileformat;
318 $file = $dir .
'/' . $filename;
324 dol_mkdir($conf->supplier_proposal->dir_temp);
325 if (!is_writable($conf->supplier_proposal->dir_temp)) {
326 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->supplier_proposal->dir_temp);
327 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
333 $arrayidcontact = $object->getIdContact(
'external',
'BILLING');
334 if (count($arrayidcontact) > 0) {
336 $result = $object->fetch_contact($arrayidcontact[0]);
340 $contactobject =
null;
341 if (!empty($usecontact)) {
343 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))) {
344 $object->contact->fetch_thirdparty();
345 $socobject = $object->contact->thirdparty;
346 $contactobject = $object->contact;
348 $socobject = $object->thirdparty;
350 $contactobject = $object->contact;
353 $socobject = $object->thirdparty;
357 $substitutionarray = array(
358 '__FROM_NAME__' => $this->emetteur->name,
359 '__FROM_EMAIL__' => $this->emetteur->email,
360 '__TOTAL_TTC__' => $object->total_ttc,
361 '__TOTAL_HT__' => $object->total_ht,
362 '__TOTAL_VAT__' => $object->total_tva
366 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
367 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
371 $paramfreetext =
'SUPPLIER_PROPOSAL_FREE_TEXT';
372 if (!empty($conf->global->$paramfreetext)) {
377 require_once ODTPHP_PATH.
'odf.php';
379 $odfHandler =
new odf(
382 'PATH_TO_TMP' => $conf->supplier_proposal->dir_temp,
383 'ZIP_PROXY' =>
'PclZipProxy',
384 'DELIMITER_LEFT' =>
'{',
385 'DELIMITER_RIGHT' =>
'}'
389 $this->error = $e->getMessage();
402 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
403 }
catch (OdfException $e) {
415 $array_thirdparty_contact = array();
416 if ($usecontact && is_object($contactobject)) {
420 $tmparray = array_merge($substitutionarray, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
424 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
425 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
427 foreach ($tmparray as $key => $value) {
429 if (preg_match(
'/logo$/', $key)) {
430 if (file_exists($value)) {
431 $odfHandler->setImage($key, $value);
433 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
437 $odfHandler->setVars($key, $value,
true,
'UTF-8');
439 }
catch (OdfException $e) {
445 $foundtagforlines = 1;
447 $listlines = $odfHandler->setSegment(
'lines');
448 }
catch (OdfException $e) {
450 $foundtagforlines = 0;
453 if ($foundtagforlines) {
455 foreach ($object->lines as $line) {
460 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
461 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
462 foreach ($tmparray as $key => $val) {
464 $listlines->setVars($key, $val,
true,
'UTF-8');
465 }
catch (OdfException $e) {
467 }
catch (SegmentException $e) {
473 $odfHandler->mergeSegment($listlines);
475 }
catch (OdfException $e) {
476 $this->error = $e->getMessage();
482 $tmparray = $outputlangs->get_translations_for_substitutions();
483 foreach ($tmparray as $key => $value) {
485 $odfHandler->setVars($key, $value,
true,
'UTF-8');
486 }
catch (OdfException $e) {
492 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
493 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
496 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
498 $odfHandler->exportAsAttachedPDF($file);
500 $this->error = $e->getMessage();
506 $odfHandler->saveToDisk($file);
508 $this->error = $e->getMessage();
513 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
514 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
520 $this->result = array(
'fullpath'=>$file);
524 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);