240 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
243 global $user, $langs, $conf, $mysoc, $hookmanager;
245 if (empty($srctemplatepath)) {
246 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
251 if (!is_object($hookmanager)) {
252 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
255 $hookmanager->initHooks(array(
'odtgeneration'));
258 if (!is_object($outputlangs)) {
259 $outputlangs = $langs;
261 $sav_charset_output = $outputlangs->charset_output;
262 $outputlangs->charset_output =
'UTF-8';
265 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
267 if ($conf->propal->multidir_output[$conf->entity]) {
269 if (!is_object($object)) {
271 $object =
new Propal($this->db);
272 $result = $object->fetch($id);
279 $object->fetch_thirdparty();
281 $dir = $conf->propal->multidir_output[$object->entity];
283 if (!preg_match(
'/specimen/i', $objectref)) {
284 $dir .=
"/".$objectref;
286 $file = $dir.
"/".$objectref.
".odt";
288 if (!file_exists($dir)) {
290 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
295 if (file_exists($dir)) {
297 $newfile = basename($srctemplatepath);
298 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
299 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
300 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
302 $newfiletmp = $objectref .
'_' . $newfiletmp;
305 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
308 if ($format ==
'1') {
309 $format =
'%Y%m%d%H%M%S';
313 $filename = $newfiletmp .
'.' . $newfileformat;
315 $file = $dir .
'/' . $filename;
321 dol_mkdir($conf->propal->multidir_temp[$object->entity]);
322 if (!is_writable($conf->propal->dir_temp)) {
323 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->propal->dir_temp);
324 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
330 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
331 if (count($arrayidcontact) > 0) {
333 $result = $object->fetch_contact($arrayidcontact[0]);
337 $contactobject =
null;
338 if (!empty($usecontact)) {
340 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
341 $object->contact->fetch_thirdparty();
342 $socobject = $object->contact->thirdparty;
343 $contactobject = $object->contact;
345 $socobject = $object->thirdparty;
347 $contactobject = $object->contact;
350 $socobject = $object->thirdparty;
353 $substitutionarray = array(
354 '__FROM_NAME__' => $this->emetteur->name,
355 '__FROM_EMAIL__' => $this->emetteur->email,
356 '__TOTAL_TTC__' => $object->total_ttc,
357 '__TOTAL_HT__' => $object->total_ht,
358 '__TOTAL_VAT__' => $object->total_tva
362 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
363 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
367 $paramfreetext =
'PROPOSAL_FREE_TEXT';
368 if (!empty($conf->global->$paramfreetext)) {
373 require_once ODTPHP_PATH.
'odf.php';
375 $odfHandler =
new Odf(
378 'PATH_TO_TMP' => $conf->propal->multidir_temp[$object->entity],
380 'DELIMITER_LEFT' =>
'{',
381 'DELIMITER_RIGHT' =>
'}'
385 $this->error = $e->getMessage();
395 $object->fetch_optionals();
399 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
400 }
catch (OdfException $e) {
413 include_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
415 $companybankaccount->fetch(0, $object->thirdparty->id);
416 $array_objet[
'company_default_bank_iban']=$companybankaccount->iban;
417 $array_objet[
'company_default_bank_bic']=$companybankaccount->bic;
420 $array_thirdparty_contact = array();
421 if ($usecontact && is_object($contactobject)) {
425 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact);
429 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
430 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
432 foreach ($tmparray as $key => $value) {
434 if (preg_match(
'/logo$/', $key)) {
435 if (file_exists($value)) {
436 $odfHandler->setImage($key, $value);
438 $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 (OdfExceptionSegmentNotFound $e) {
454 $foundtagforlines = 0;
456 }
catch (OdfException $e) {
457 $foundtagforlines = 0;
460 if ($foundtagforlines) {
462 foreach ($object->lines as $line) {
467 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
468 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
469 foreach ($tmparray as $key => $val) {
471 $listlines->setVars($key, $val,
true,
'UTF-8');
472 }
catch (OdfException $e) {
474 }
catch (SegmentException $e) {
480 $odfHandler->mergeSegment($listlines);
482 }
catch (OdfException $e) {
483 $this->error = $e->getMessage();
489 $tmparray = $outputlangs->get_translations_for_substitutions();
490 foreach ($tmparray as $key => $value) {
492 $odfHandler->setVars($key, $value,
true,
'UTF-8');
493 }
catch (OdfException $e) {
499 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
500 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
505 $odfHandler->exportAsAttachedPDF($file);
507 $this->error = $e->getMessage();
513 $odfHandler->saveToDisk($file);
515 $this->error = $e->getMessage();
520 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
521 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
527 $this->result = array(
'fullpath'=>$file);
531 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);