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 = empty($conf->facture->multidir_output[$object->entity]) ? $conf->facture->dir_output : $conf->facture->multidir_output[$object->entity];
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);