205 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
208 global $user, $langs, $conf, $mysoc, $hookmanager;
210 if (empty($srctemplatepath)) {
211 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
216 if (!is_object($hookmanager)) {
217 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
220 $hookmanager->initHooks(array(
'odtgeneration'));
223 if (!is_object($outputlangs)) {
224 $outputlangs = $langs;
226 $sav_charset_output = $outputlangs->charset_output;
227 $outputlangs->charset_output =
'UTF-8';
230 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
232 if ($conf->facture->dir_output) {
234 if (!is_object($object)) {
236 $object =
new Facture($this->db);
237 $result = $object->fetch($id);
244 $object->fetch_thirdparty();
246 $dir = empty($conf->facture->multidir_output[$object->entity]) ? $conf->facture->dir_output : $conf->facture->multidir_output[$object->entity];
248 if (!preg_match(
'/specimen/i', $objectref)) {
249 $dir .=
"/".$objectref;
251 $file = $dir.
"/".$objectref.
".odt";
253 if (!file_exists($dir)) {
255 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
260 if (file_exists($dir)) {
262 $newfile = basename($srctemplatepath);
263 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
264 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
265 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
267 $newfiletmp = $objectref .
'_' . $newfiletmp;
270 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
273 if ($format ==
'1') {
274 $format =
'%Y%m%d%H%M%S';
278 $filename = $newfiletmp .
'.' . $newfileformat;
280 $file = $dir .
'/' . $filename;
288 if (!is_writable($conf->facture->dir_temp)) {
289 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->facture->dir_temp);
290 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
296 $arrayidcontact = $object->getIdContact(
'external',
'BILLING');
297 if (count($arrayidcontact) > 0) {
299 $result = $object->fetch_contact($arrayidcontact[0]);
303 $contactobject =
null;
304 if (!empty($usecontact)) {
306 if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
307 $object->contact->fetch_thirdparty();
308 $socobject = $object->contact->thirdparty;
309 $contactobject = $object->contact;
311 $socobject = $object->thirdparty;
313 $contactobject = $object->contact;
316 $socobject = $object->thirdparty;
320 $object->fetchObjectLinked(
'',
'',
'',
'');
323 $propal_object =
null;
324 if (!empty($object->linkedObjects[
'propal'])) {
325 $array_propal_object = $object->linkedObjects[
'propal'];
326 if (isset($array_propal_object) && is_array($array_propal_object) && count($array_propal_object) > 0) {
327 $tmparrayofvalue = array_values($array_propal_object);
328 $propal_object = $tmparrayofvalue[0];
333 $categoryOfOperation = 0;
336 foreach ($object->lines as $line) {
338 if ($categoryOfOperation < 2) {
339 $lineProductType = $line->product_type;
345 if ($nbProduct > 0 && $nbService > 0) {
347 $categoryOfOperation = 2;
353 if ($categoryOfOperation <= 0) {
355 if ($nbProduct == 0 && $nbService > 0) {
356 $categoryOfOperation = 1;
361 $substitutionarray = array(
362 '__FROM_NAME__' => $this->emetteur->name,
363 '__FROM_EMAIL__' => $this->emetteur->email,
364 '__TOTAL_TTC__' => $object->total_ttc,
365 '__TOTAL_HT__' => $object->total_ht,
366 '__TOTAL_VAT__' => $object->total_tva
370 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
371 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
375 $paramfreetext =
'INVOICE_FREE_TEXT';
376 if (!empty($conf->global->$paramfreetext)) {
381 require_once ODTPHP_PATH.
'odf.php';
383 $odfHandler =
new Odf(
386 'PATH_TO_TMP' => $conf->facture->dir_temp,
388 'DELIMITER_LEFT' =>
'{',
389 'DELIMITER_RIGHT' =>
'}'
393 $this->error = $e->getMessage();
406 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
407 }
catch (OdfException $e) {
421 $array_thirdparty_contact = array();
422 if ($usecontact && is_object($contactobject)) {
426 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_propal, $array_other, $array_thirdparty_contact);
438 $tmparray[
'object_MONTH_TEXT'] =
dol_print_date($object->date,
'%B');
443 $tmparray[
'object_productorservice_operation'] = $outputlangs->transnoentities(
"MentionCategoryOfOperations" . $categoryOfOperation);
447 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
448 $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) {
470 $foundtagforlines = 1;
472 $listlines = $odfHandler->setSegment(
'lines');
473 }
catch (OdfExceptionSegmentNotFound $e) {
475 $foundtagforlines = 0;
477 }
catch (OdfException $e) {
478 $foundtagforlines = 0;
481 if ($foundtagforlines) {
483 foreach ($object->lines as $line) {
488 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
489 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
490 foreach ($tmparray as $key => $val) {
492 $listlines->setVars($key, $val,
true,
'UTF-8');
493 }
catch (OdfException $e) {
495 }
catch (SegmentException $e) {
501 $odfHandler->mergeSegment($listlines);
503 }
catch (OdfException $e) {
504 $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);