214 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
217 global $user, $langs, $conf, $mysoc, $hookmanager;
219 if (empty($srctemplatepath)) {
220 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
225 if (!is_object($hookmanager)) {
226 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
229 $hookmanager->initHooks(array(
'odtgeneration'));
232 if (!is_object($outputlangs)) {
233 $outputlangs = $langs;
235 $sav_charset_output = $outputlangs->charset_output;
236 $outputlangs->charset_output =
'UTF-8';
238 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
240 if ($conf->fournisseur->facture->dir_output) {
241 $object->fetch_thirdparty();
243 if ($object->specimen) {
244 $dir = $conf->fournisseur->facture->dir_output;
245 $file = $dir.
"/SPECIMEN.pdf";
250 $dir = $conf->fournisseur->facture->dir_output.
'/'.
get_exdir($object->id, 2, 0, 0, $object,
'invoice_supplier').$objectref;
251 $file = $dir.
"/".$objectref.
".pdf";
252 if (!empty($conf->global->SUPPLIER_REF_IN_NAME)) {
253 $file = $dir.
"/".$objectref.($objectrefsupplier ?
"_".$objectrefsupplier :
"").
".pdf";
257 if (!file_exists($dir)) {
259 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
264 if (file_exists($dir)) {
266 $newfile = basename($srctemplatepath);
267 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
268 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
269 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
270 $newfiletmp = $objectref.
'_'.$newfiletmp;
273 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
276 if ($format ==
'1') {
277 $format =
'%Y%m%d%H%M%S';
281 $filename = $newfiletmp.
'.'.$newfileformat;
283 $file = $dir.
'/'.$filename;
289 dol_mkdir($conf->fournisseur->facture->dir_temp);
290 if (!is_writable($conf->fournisseur->facture->dir_temp)) {
291 $this->error =
"Failed to write in temp directory ".$conf->fournisseur->facture->dir_temp;
292 dol_syslog(
'Error in write_file: '.$this->error, LOG_ERR);
298 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
299 if (count($arrayidcontact) > 0) {
301 $result = $object->fetch_contact($arrayidcontact[0]);
305 $contactobject =
null;
306 if (!empty($usecontact)) {
308 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))) {
309 $object->contact->fetch_thirdparty();
310 $socobject = $object->contact->thirdparty;
311 $contactobject = $object->contact;
313 $socobject = $object->thirdparty;
315 $contactobject = $object->contact;
318 $socobject = $object->thirdparty;
322 $substitutionarray = array(
323 '__FROM_NAME__' => $this->issuer->name,
324 '__FROM_EMAIL__' => $this->issuer->email,
325 '__TOTAL_TTC__' => $object->total_ttc,
326 '__TOTAL_HT__' => $object->total_ht,
327 '__TOTAL_VAT__' => $object->total_tva
331 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
332 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
336 $paramfreetext =
'INVOICE_FREE_TEXT';
337 if (!empty($conf->global->$paramfreetext)) {
342 require_once ODTPHP_PATH.
'odf.php';
344 $odfHandler =
new Odf(
347 'PATH_TO_TMP' => $conf->fournisseur->dir_temp,
348 'ZIP_PROXY' =>
'PclZipProxy',
349 'DELIMITER_LEFT' =>
'{',
350 'DELIMITER_RIGHT' =>
'}'
354 $this->error = $e->getMessage();
367 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
368 }
catch (OdfException $e) {
381 $array_thirdparty_contact = array();
382 if ($usecontact && is_object($contactobject)) {
386 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
390 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
391 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
393 foreach ($tmparray as $key => $value) {
395 if (preg_match(
'/logo$/', $key)) {
396 if (file_exists($value)) {
397 $odfHandler->setImage($key, $value);
399 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
403 $odfHandler->setVars($key, $value,
true,
'UTF-8');
405 }
catch (OdfException $e) {
411 $foundtagforlines = 1;
413 $listlines = $odfHandler->setSegment(
'lines');
414 }
catch (OdfException $e) {
416 $foundtagforlines = 0;
419 if ($foundtagforlines) {
421 foreach ($object->lines as $line) {
426 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
427 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
428 foreach ($tmparray as $key => $val) {
430 $listlines->setVars($key, $val,
true,
'UTF-8');
431 }
catch (OdfException $e) {
433 }
catch (SegmentException $e) {
439 $odfHandler->mergeSegment($listlines);
441 }
catch (OdfException $e) {
442 $this->error = $e->getMessage();
448 $tmparray = $outputlangs->get_translations_for_substitutions();
449 foreach ($tmparray as $key => $value) {
451 $odfHandler->setVars($key, $value,
true,
'UTF-8');
452 }
catch (OdfException $e) {
459 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
460 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
463 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
465 $odfHandler->exportAsAttachedPDF($file);
467 $this->error = $e->getMessage();
473 $odfHandler->saveToDisk($file);
475 $this->error = $e->getMessage();
481 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
482 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
488 $this->result = array(
'fullpath'=>$file);
492 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);