206 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
209 global $user, $langs, $conf, $mysoc, $hookmanager;
211 if (empty($srctemplatepath)) {
212 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
217 if (!is_object($hookmanager)) {
218 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
221 $hookmanager->initHooks(array(
'odtgeneration'));
224 if (!is_object($outputlangs)) {
225 $outputlangs = $langs;
227 $sav_charset_output = $outputlangs->charset_output;
228 $outputlangs->charset_output =
'UTF-8';
231 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
233 if ($conf->facture->dir_output) {
247 $dir = empty($conf->facture->multidir_output[
$object->entity]) ? $conf->facture->dir_output : $conf->facture->multidir_output[
$object->entity];
249 if (!preg_match(
'/specimen/i', $objectref)) {
250 $dir .=
"/".$objectref;
252 $file = $dir.
"/".$objectref.
".odt";
254 if (!file_exists($dir)) {
256 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
261 if (file_exists($dir)) {
263 $newfile = basename($srctemplatepath);
264 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
265 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
266 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
268 $newfiletmp = $objectref .
'_' . $newfiletmp;
271 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
274 if ($format ==
'1') {
275 $format =
'%Y%m%d%H%M%S';
279 $filename = $newfiletmp .
'.' . $newfileformat;
281 $file = $dir .
'/' . $filename;
289 if (!is_writable($conf->facture->dir_temp)) {
290 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->facture->dir_temp);
291 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
297 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
298 if (count($arrayidcontact) > 0) {
300 $result =
$object->fetch_contact($arrayidcontact[0]);
304 $contactobject =
null;
305 if (!empty($usecontact)) {
307 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
308 $object->contact->fetch_thirdparty();
309 $socobject =
$object->contact->thirdparty;
310 $contactobject =
$object->contact;
312 $socobject =
$object->thirdparty;
314 $contactobject =
$object->contact;
317 $socobject =
$object->thirdparty;
321 $object->fetchObjectLinked(
'',
'',
'',
'');
324 $propal_object =
null;
325 if (!empty(
$object->linkedObjects[
'propal'])) {
326 $array_propal_object =
$object->linkedObjects[
'propal'];
327 if (isset($array_propal_object) && is_array($array_propal_object) && count($array_propal_object) > 0) {
328 $tmparrayofvalue = array_values($array_propal_object);
329 $propal_object = $tmparrayofvalue[0];
334 $categoryOfOperation = 0;
337 foreach (
$object->lines as $line) {
339 if ($categoryOfOperation < 2) {
340 $lineProductType = $line->product_type;
346 if ($nbProduct > 0 && $nbService > 0) {
348 $categoryOfOperation = 2;
354 if ($categoryOfOperation <= 0) {
356 if ($nbProduct == 0 && $nbService > 0) {
357 $categoryOfOperation = 1;
362 $substitutionarray = array(
363 '__FROM_NAME__' => $this->emetteur->name,
364 '__FROM_EMAIL__' => $this->emetteur->email,
365 '__TOTAL_TTC__' =>
$object->total_ttc,
366 '__TOTAL_HT__' =>
$object->total_ht,
367 '__TOTAL_VAT__' =>
$object->total_tva
371 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
372 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
376 $paramfreetext =
'INVOICE_FREE_TEXT';
382 require_once ODTPHP_PATH.
'odf.php';
384 $odfHandler =
new Odf(
387 'PATH_TO_TMP' => $conf->facture->dir_temp,
388 'ZIP_PROXY' =>
'PclZipProxy',
389 'DELIMITER_LEFT' =>
'{',
390 'DELIMITER_RIGHT' =>
'}'
394 $this->error = $e->getMessage();
407 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
408 }
catch (OdfException $e) {
415 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
419 $array_propal = is_object($propal_object) ? $this->get_substitutionarray_object($propal_object, $outputlangs,
'propal') : array();
422 $array_thirdparty_contact = array();
423 if ($usecontact && is_object($contactobject)) {
427 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_propal, $array_other, $array_thirdparty_contact);
444 $tmparray[
'object_productorservice_operation'] = $outputlangs->transnoentities(
"MentionCategoryOfOperations" . $categoryOfOperation);
448 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
449 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
451 foreach ($tmparray as $key => $value) {
453 if (preg_match(
'/logo$/', $key)) {
455 if (file_exists($value)) {
456 $odfHandler->setImage($key, $value);
458 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
462 $odfHandler->setVars($key, $value,
true,
'UTF-8');
464 }
catch (OdfException $e) {
469 $foundtagforlines = 1;
471 $listlines = $odfHandler->setSegment(
'lines');
472 }
catch (OdfExceptionSegmentNotFound $e) {
474 $foundtagforlines = 0;
477 if ($foundtagforlines) {
479 foreach (
$object->lines as $line) {
484 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
485 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
486 foreach ($tmparray as $key => $val) {
488 $listlines->setVars($key, $val,
true,
'UTF-8');
489 }
catch (SegmentException $e) {
496 $odfHandler->mergeSegment($listlines);
497 }
catch (OdfException $e) {
498 $this->error = $e->getMessage();
505 $tmparray = $outputlangs->get_translations_for_substitutions();
506 foreach ($tmparray as $key => $value) {
508 $odfHandler->setVars($key, $value,
true,
'UTF-8');
509 }
catch (OdfException $e) {
515 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
516 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
521 $odfHandler->exportAsAttachedPDF($file);
523 $this->error = $e->getMessage();
529 $odfHandler->saveToDisk($file);
531 $this->error = $e->getMessage();
536 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
537 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
543 $this->result = array(
'fullpath' => $file);
547 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);