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';
239 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
241 if ($conf->fournisseur->facture->dir_output) {
245 $dir = $conf->fournisseur->facture->dir_output;
246 $file = $dir.
"/SPECIMEN.pdf";
251 $dir = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir(
$object->id, 2, 0, 0,
$object,
'invoice_supplier').$objectref;
252 $file = $dir.
"/".$objectref.
".pdf";
254 $file = $dir.
"/".$objectref.($objectrefsupplier ?
"_".$objectrefsupplier :
"").
".pdf";
258 if (!file_exists($dir)) {
260 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
265 if (file_exists($dir)) {
267 $newfile = basename($srctemplatepath);
268 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
269 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
270 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
271 $newfiletmp = $objectref.
'_'.$newfiletmp;
274 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
277 if ($format ==
'1') {
278 $format =
'%Y%m%d%H%M%S';
282 $filename = $newfiletmp.
'.'.$newfileformat;
284 $file = $dir.
'/'.$filename;
290 dol_mkdir($conf->fournisseur->facture->dir_temp);
291 if (!is_writable($conf->fournisseur->facture->dir_temp)) {
292 $this->error =
"Failed to write in temp directory ".$conf->fournisseur->facture->dir_temp;
293 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
299 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
300 if (count($arrayidcontact) > 0) {
302 $result =
$object->fetch_contact($arrayidcontact[0]);
306 $contactobject =
null;
307 if (!empty($usecontact)) {
309 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
310 $object->contact->fetch_thirdparty();
311 $socobject =
$object->contact->thirdparty;
312 $contactobject =
$object->contact;
314 $socobject =
$object->thirdparty;
316 $contactobject =
$object->contact;
319 $socobject =
$object->thirdparty;
323 $substitutionarray = array(
324 '__FROM_NAME__' => $this->issuer->name,
325 '__FROM_EMAIL__' => $this->issuer->email,
326 '__TOTAL_TTC__' =>
$object->total_ttc,
327 '__TOTAL_HT__' =>
$object->total_ht,
328 '__TOTAL_VAT__' =>
$object->total_tva
332 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
333 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
337 $paramfreetext =
'INVOICE_FREE_TEXT';
343 require_once ODTPHP_PATH.
'odf.php';
345 $odfHandler =
new Odf(
348 'PATH_TO_TMP' => $conf->fournisseur->dir_temp,
350 'DELIMITER_LEFT' =>
'{',
351 'DELIMITER_RIGHT' =>
'}'
355 $this->error = $e->getMessage();
365 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
366 }
catch (OdfException $e) {
373 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
379 $array_thirdparty_contact = array();
380 if ($usecontact && is_object($contactobject)) {
384 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
388 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
389 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
391 foreach ($tmparray as $key => $value) {
393 if (preg_match(
'/logo$/', $key)) {
394 if (file_exists($value)) {
395 $odfHandler->setImage($key, $value);
397 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
400 $odfHandler->setVars($key, $value,
true,
'UTF-8');
402 }
catch (OdfException $e) {
407 $foundtagforlines = 1;
409 $listlines = $odfHandler->setSegment(
'lines');
410 }
catch (OdfExceptionSegmentNotFound $e) {
412 $foundtagforlines = 0;
415 if ($foundtagforlines) {
417 foreach (
$object->lines as $line) {
422 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
423 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
424 foreach ($tmparray as $key => $val) {
426 $listlines->setVars($key, $val,
true,
'UTF-8');
427 }
catch (SegmentException $e) {
434 $odfHandler->mergeSegment($listlines);
435 }
catch (OdfException $e) {
436 $this->error = $e->getMessage();
443 $tmparray = $outputlangs->get_translations_for_substitutions();
444 foreach ($tmparray as $key => $value) {
446 $odfHandler->setVars($key, $value,
true,
'UTF-8');
447 }
catch (OdfException $e) {
454 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
455 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
460 $odfHandler->exportAsAttachedPDF($file);
462 $this->error = $e->getMessage();
468 $odfHandler->saveToDisk($file);
470 $this->error = $e->getMessage();
476 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
477 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
483 $this->result = array(
'fullpath' => $file);
487 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);