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) {
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;
284 if (!is_writable($conf->product->dir_temp)) {
285 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->product->dir_temp);
286 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
292 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
293 if (count($arrayidcontact) > 0) {
295 $result =
$object->fetch_contact($arrayidcontact[0]);
299 $contactobject =
null;
300 if (!empty($usecontact)) {
302 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
303 $object->contact->fetch_thirdparty();
304 $socobject =
$object->contact->thirdparty;
305 $contactobject =
$object->contact;
307 $socobject =
$object->thirdparty;
309 $contactobject =
$object->contact;
312 $socobject =
$object->thirdparty;
315 $substitutionarray = array(
316 '__FROM_NAME__' => $this->emetteur->name,
317 '__FROM_EMAIL__' => $this->emetteur->email,
318 '__TOTAL_TTC__' =>
$object->total_ttc,
319 '__TOTAL_HT__' =>
$object->total_ht,
320 '__TOTAL_VAT__' =>
$object->total_tva
324 $parameters = array(
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
325 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
329 $paramfreetext =
'stock_FREE_TEXT';
330 if (!empty($conf->global->$paramfreetext)) {
335 require_once ODTPHP_PATH.
'odf.php';
337 $odfHandler =
new Odf(
340 'PATH_TO_TMP' => $conf->product->dir_temp,
342 'DELIMITER_LEFT' =>
'{',
343 'DELIMITER_RIGHT' =>
'}'
347 $this->error = $e->getMessage();
359 $odfHandler->setVars(
'free_text', $newfreetext,
true,
'UTF-8');
360 }
catch (OdfException $e) {
373 $array_thirdparty_contact = array();
374 if ($usecontact && is_object($contactobject)) {
378 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact);
382 $parameters = array(
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
383 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
385 foreach ($tmparray as $key => $value) {
387 if (preg_match(
'/logo$/', $key)) {
388 if (file_exists($value)) {
389 $odfHandler->setImage($key, $value);
391 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
394 $odfHandler->setVars($key, $value,
true,
'UTF-8');
396 }
catch (OdfException $e) {
401 $foundtagforlines = 1;
403 $listlines = $odfHandler->setSegment(
'supplierprices');
404 }
catch (OdfExceptionSegmentNotFound $e) {
406 $foundtagforlines = 0;
409 if ($foundtagforlines && !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 (SegmentException $e) {
426 $odfHandler->mergeSegment($listlines);
427 }
catch (OdfException $e) {
428 $this->error = $e->getMessage();
435 $tmparray = $outputlangs->get_translations_for_substitutions();
436 foreach ($tmparray as $key => $value) {
438 $odfHandler->setVars($key, $value,
true,
'UTF-8');
439 }
catch (OdfException $e) {
445 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs);
446 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
451 $odfHandler->exportAsAttachedPDF($file);
453 $this->error = $e->getMessage();
459 $odfHandler->saveToDisk($file);
461 $this->error = $e->getMessage();
467 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
473 $this->result = array(
'fullpath'=>$file);
477 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);