210 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
213 global $stock, $langs, $conf, $mysoc, $hookmanager, $user;
215 if (empty($srctemplatepath)) {
216 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
221 if (!is_object($hookmanager)) {
222 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
225 $hookmanager->initHooks(array(
'odtgeneration'));
228 if (!is_object($outputlangs)) {
229 $outputlangs = $langs;
231 $sav_charset_output = $outputlangs->charset_output;
232 $outputlangs->charset_output =
'UTF-8';
235 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
237 if ($conf->product->dir_output) {
250 $supplierprices = $stockFournisseur->list_stock_fournisseur_price(
$object->id);
251 $object->supplierprices = $supplierprices;
253 $dir = $conf->product->dir_output;
255 if (!preg_match(
'/specimen/i', $objectref)) {
256 $dir .=
"/".$objectref;
258 $file = $dir.
"/".$objectref.
".odt";
260 if (!file_exists($dir)) {
262 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
267 if (file_exists($dir)) {
269 $newfile = basename($srctemplatepath);
270 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
271 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
272 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
274 $newfiletmp = $objectref .
'_' . $newfiletmp;
277 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
280 if ($format ==
'1') {
281 $format =
'%Y%m%d%H%M%S';
285 $filename = $newfiletmp .
'.' . $newfileformat;
287 $file = $dir .
'/' . $filename;
290 if (!is_writable($conf->product->dir_temp)) {
291 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
292 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
298 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
299 if (count($arrayidcontact) > 0) {
301 $result =
$object->fetch_contact($arrayidcontact[0]);
305 $contactobject =
null;
306 if (!empty($usecontact)) {
308 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
309 $object->contact->fetch_thirdparty();
310 $socobject =
$object->contact->thirdparty;
311 $contactobject =
$object->contact;
313 $socobject =
$object->thirdparty;
315 $contactobject =
$object->contact;
318 $socobject =
$object->thirdparty;
321 $substitutionarray = array(
322 '__FROM_NAME__' => $this->emetteur->name,
323 '__FROM_EMAIL__' => $this->emetteur->email,
324 '__TOTAL_TTC__' =>
$object->total_ttc,
325 '__TOTAL_HT__' =>
$object->total_ht,
326 '__TOTAL_VAT__' =>
$object->total_tva
330 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
331 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
335 $paramfreetext =
'stock_FREE_TEXT';
341 require_once ODTPHP_PATH.
'odf.php';
343 $odfHandler =
new Odf(
346 'PATH_TO_TMP' => $conf->product->dir_temp,
348 'DELIMITER_LEFT' =>
'{',
349 'DELIMITER_RIGHT' =>
'}'
353 $this->error = $e->getMessage();
365 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
366 }
catch (OdfException $e) {
379 $array_thirdparty_contact = array();
380 if ($usecontact && is_object($contactobject)) {
384 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
388 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
389 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
391 foreach ($tmparray as $key => $value) {
393 if (preg_match(
'/logo$/', $key)) {
394 if (file_exists($value)) {
395 $odfHandler->setImage($key, $value);
397 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
400 $odfHandler->setVars($key, $value,
true,
'UTF-8');
402 }
catch (OdfException $e) {
407 $foundtagforlines = 1;
409 $listlines = $odfHandler->setSegment(
'supplierprices');
410 }
catch (OdfExceptionSegmentNotFound $e) {
412 $foundtagforlines = 0;
415 if ($foundtagforlines && !empty(
$object->supplierprices)) {
416 foreach (
$object->supplierprices as $supplierprice) {
420 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$array_lines,
'line' => $supplierprice);
421 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
422 foreach ($array_lines as $key => $val) {
424 $listlines->setVars($key, $val,
true,
'UTF-8');
425 }
catch (SegmentException $e) {
432 $odfHandler->mergeSegment($listlines);
433 }
catch (OdfException $e) {
434 $this->error = $e->getMessage();
441 $tmparray = $outputlangs->get_translations_for_substitutions();
442 foreach ($tmparray as $key => $value) {
444 $odfHandler->setVars($key, $value,
true,
'UTF-8');
445 }
catch (OdfException $e) {
451 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
452 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
457 $odfHandler->exportAsAttachedPDF($file);
459 $this->error = $e->getMessage();
465 $odfHandler->saveToDisk($file);
467 $this->error = $e->getMessage();
473 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
479 $this->result = array(
'fullpath' => $file);
483 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);