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) {
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) {
414 $array_objet = $this->get_substitutionarray_object(
$object, $outputlangs);
418 $array_propal = is_object($propal_object) ? $this->get_substitutionarray_object($propal_object, $outputlangs,
'propal') : array();
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);
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);
450 foreach ($tmparray as $key => $value) {
452 if (preg_match(
'/logo$/', $key)) {
454 if (file_exists($value)) {
455 $odfHandler->setImage($key, $value);
457 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
461 $odfHandler->setVars($key, $value,
true,
'UTF-8');
463 }
catch (OdfException $e) {
468 $foundtagforlines = 1;
470 $listlines = $odfHandler->setSegment(
'lines');
471 }
catch (OdfExceptionSegmentNotFound $e) {
473 $foundtagforlines = 0;
476 if ($foundtagforlines) {
478 foreach (
$object->lines as $line) {
483 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray,
'line'=>$line);
484 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
485 foreach ($tmparray as $key => $val) {
487 $listlines->setVars($key, $val,
true,
'UTF-8');
488 }
catch (SegmentException $e) {
495 $odfHandler->mergeSegment($listlines);
496 }
catch (OdfException $e) {
497 $this->error = $e->getMessage();
504 $tmparray = $outputlangs->get_translations_for_substitutions();
505 foreach ($tmparray as $key => $value) {
507 $odfHandler->setVars($key, $value,
true,
'UTF-8');
508 }
catch (OdfException $e) {
514 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
515 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
520 $odfHandler->exportAsAttachedPDF($file);
522 $this->error = $e->getMessage();
528 $odfHandler->saveToDisk($file);
530 $this->error = $e->getMessage();
535 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
536 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
542 $this->result = array(
'fullpath'=>$file);
546 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);