211 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
214 global $user, $langs,
$conf, $mysoc, $hookmanager;
216 if (empty($srctemplatepath)) {
217 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
222 if (!is_object($hookmanager)) {
223 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
226 $hookmanager->initHooks(array(
'odtgeneration'));
229 if (!is_object($outputlangs)) {
230 $outputlangs = $langs;
232 $sav_charset_output = $outputlangs->charset_output;
233 $outputlangs->charset_output =
'UTF-8';
236 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
238 if (
$conf->facture->dir_output) {
252 $dir = empty(
$conf->facture->multidir_output[
$object->entity]) ?
$conf->facture->dir_output :
$conf->facture->multidir_output[
$object->entity];
254 if (!preg_match(
'/specimen/i', $objectref)) {
255 $dir .=
"/".$objectref;
257 $file = $dir.
"/".$objectref.
".odt";
259 if (!file_exists($dir)) {
261 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
266 if (file_exists($dir)) {
268 $newfile = basename($srctemplatepath);
269 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
270 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
271 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
273 $newfiletmp = $objectref .
'_' . $newfiletmp;
276 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
279 if ($format ==
'1') {
280 $format =
'%Y%m%d%H%M%S';
284 $filename = $newfiletmp .
'.' . $newfileformat;
286 $file = $dir .
'/' . $filename;
294 if (!is_writable(
$conf->facture->dir_temp)) {
295 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory",
$conf->facture->dir_temp);
296 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
302 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
303 if (count($arrayidcontact) > 0) {
305 $result =
$object->fetch_contact($arrayidcontact[0]);
309 $contactobject =
null;
310 if (!empty($usecontact)) {
313 $object->contact->fetch_thirdparty();
314 $socobject =
$object->contact->thirdparty;
315 $contactobject =
$object->contact;
317 $socobject =
$object->thirdparty;
319 $contactobject =
$object->contact;
322 $socobject =
$object->thirdparty;
326 $object->fetchObjectLinked(0,
'',
null,
'');
329 $propal_object =
null;
330 if (!empty(
$object->linkedObjects[
'propal'])) {
331 $array_propal_object =
$object->linkedObjects[
'propal'];
332 if (isset($array_propal_object) && is_array($array_propal_object) && count($array_propal_object) > 0) {
333 $tmparrayofvalue = array_values($array_propal_object);
334 $propal_object = $tmparrayofvalue[0];
339 $categoryOfOperation = 0;
342 foreach (
$object->lines as $line) {
344 if ($categoryOfOperation < 2) {
345 $lineProductType = $line->product_type;
351 if ($nbProduct > 0 && $nbService > 0) {
353 $categoryOfOperation = 2;
359 if ($categoryOfOperation <= 0) {
361 if ($nbProduct == 0 && $nbService > 0) {
362 $categoryOfOperation = 1;
367 $substitutionarray = array(
368 '__FROM_NAME__' => $this->emetteur->name,
369 '__FROM_EMAIL__' => $this->emetteur->email,
370 '__TOTAL_TTC__' =>
$object->total_ttc,
371 '__TOTAL_HT__' =>
$object->total_ht,
372 '__TOTAL_VAT__' =>
$object->total_tva
376 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
377 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
381 $paramfreetext =
'INVOICE_FREE_TEXT';
387 require_once ODTPHP_PATH.
'odf.php';
389 $odfHandler =
new Odf(
392 'PATH_TO_TMP' =>
$conf->facture->dir_temp,
394 'DELIMITER_LEFT' =>
'{',
395 'DELIMITER_RIGHT' =>
'}'
399 $this->error = $e->getMessage();
412 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
413 }
catch (OdfException $e) {
420 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
424 $array_propal = is_object($propal_object) ? $this->get_substitutionarray_object($propal_object, $outputlangs,
'propal') : array();
427 $array_thirdparty_contact = array();
428 if ($usecontact && is_object($contactobject)) {
432 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_propal, $array_other, $array_thirdparty_contact);
449 $tmparray[
'object_productorservice_operation'] = $outputlangs->transnoentities(
"MentionCategoryOfOperations" . $categoryOfOperation);
453 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
454 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
456 foreach ($tmparray as $key => $value) {
458 if (preg_match(
'/logo$/', $key)) {
460 if (file_exists($value)) {
461 $odfHandler->setImage($key, $value);
463 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
467 $odfHandler->setVars($key, $value,
true,
'UTF-8');
469 }
catch (OdfException $e) {
474 $foundtagforlines = 1;
476 $listlines = $odfHandler->setSegment(
'lines');
477 }
catch (OdfExceptionSegmentNotFound $e) {
479 $foundtagforlines = 0;
482 if ($foundtagforlines) {
484 foreach (
$object->lines as $line) {
489 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray,
'line' => $line);
490 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
491 foreach ($tmparray as $key => $val) {
493 $listlines->setVars($key, $val,
true,
'UTF-8');
494 }
catch (SegmentException $e) {
501 $odfHandler->mergeSegment($listlines);
502 }
catch (OdfException $e) {
503 $this->error = $e->getMessage();
510 $tmparray = $outputlangs->get_translations_for_substitutions();
511 foreach ($tmparray as $key => $value) {
513 $odfHandler->setVars($key, $value,
true,
'UTF-8');
514 }
catch (OdfException $e) {
520 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
521 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
526 $odfHandler->exportAsAttachedPDF($file);
528 $this->error = $e->getMessage();
534 $odfHandler->saveToDisk($file);
536 $this->error = $e->getMessage();
541 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
542 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
548 $this->result = array(
'fullpath' => $file);
552 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);