204 public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
207 global $stock, $langs, $conf, $mysoc, $hookmanager, $user;
209 if (empty($srctemplatepath)) {
210 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
215 if (!is_object($hookmanager)) {
216 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
219 $hookmanager->initHooks(array(
'odtgeneration'));
222 if (!is_object($outputlangs)) {
223 $outputlangs = $langs;
225 $sav_charset_output = $outputlangs->charset_output;
226 $outputlangs->charset_output =
'UTF-8';
229 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"bills"));
231 if ($conf->product->dir_output) {
233 if (!is_object($object)) {
236 $result = $object->fetch($id);
244 $supplierprices = $stockFournisseur->list_stock_fournisseur_price($object->id);
245 $object->supplierprices = $supplierprices;
247 $dir = $conf->product->dir_output;
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;
288 if (!is_writable($conf->product->dir_temp)) {
289 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
290 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
296 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
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;
319 $substitutionarray = array(
320 '__FROM_NAME__' => $this->emetteur->name,
321 '__FROM_EMAIL__' => $this->emetteur->email,
322 '__TOTAL_TTC__' => $object->total_ttc,
323 '__TOTAL_HT__' => $object->total_ht,
324 '__TOTAL_VAT__' => $object->total_tva
328 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
329 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
333 $paramfreetext =
'stock_FREE_TEXT';
334 if (!empty($conf->global->$paramfreetext)) {
339 require_once ODTPHP_PATH.
'odf.php';
341 $odfHandler =
new Odf(
344 'PATH_TO_TMP' => $conf->product->dir_temp,
346 'DELIMITER_LEFT' =>
'{',
347 'DELIMITER_RIGHT' =>
'}'
351 $this->error = $e->getMessage();
361 $object->fetch_optionals();
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) {
408 $listlines = $odfHandler->setSegment(
'supplierprices');
409 if (!empty($object->supplierprices)) {
410 foreach ($object->supplierprices as $supplierprice) {
414 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$array_lines,
'line'=>$supplierprice);
415 $reshook = $hookmanager->executeHooks(
'ODTSubstitutionLine', $parameters, $this, $action);
416 foreach ($array_lines as $key => $val) {
418 $listlines->setVars($key, $val,
true,
'UTF-8');
419 }
catch (OdfException $e) {
421 }
catch (SegmentException $e) {
428 $odfHandler->mergeSegment($listlines);
429 }
catch (OdfException $e) {
430 $this->error = $e->getMessage();
436 $tmparray = $outputlangs->get_translations_for_substitutions();
437 foreach ($tmparray as $key => $value) {
439 $odfHandler->setVars($key, $value,
true,
'UTF-8');
440 }
catch (OdfException $e) {
446 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
447 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
452 $odfHandler->exportAsAttachedPDF($file);
454 $this->error = $e->getMessage();
460 $odfHandler->saveToDisk($file);
462 $this->error = $e->getMessage();
468 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
474 $this->result = array(
'fullpath'=>$file);
478 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);