PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/facture/core/modules/facture/doc/doc_generic_invoice_odt.modules.php

https://bitbucket.org/speedealing/speedealing
PHP | 519 lines | 364 code | 56 blank | 99 comment | 35 complexity | 822aa3b1da22fd782685da50ac34e81a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2010-2012 Laurent Destailleur <ely@users.sourceforge.net>
  3. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php
  20. * \ingroup societe
  21. * \brief File of class to build ODT documents for third parties
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php';
  24. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
  29. /**
  30. * Class to build documents using ODF templates generator
  31. */
  32. class doc_generic_invoice_odt extends ModelePDFFactures
  33. {
  34. var $emetteur; // Objet societe qui emet
  35. var $phpmin = array(5,2,0); // Minimum version of PHP required by module
  36. var $version = 'dolibarr';
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function __construct($db = '')
  43. {
  44. global $conf,$langs,$mysoc;
  45. $langs->load("main");
  46. $langs->load("companies");
  47. $this->db = $db;
  48. $this->name = "ODT templates";
  49. $this->description = $langs->trans("DocumentModelOdt");
  50. $this->scandir = 'FACTURE_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
  51. // Dimension page pour format A4
  52. $this->type = 'odt';
  53. $this->page_largeur = 0;
  54. $this->page_hauteur = 0;
  55. $this->format = array($this->page_largeur,$this->page_hauteur);
  56. $this->marge_gauche=0;
  57. $this->marge_droite=0;
  58. $this->marge_haute=0;
  59. $this->marge_basse=0;
  60. $this->option_logo = 1; // Affiche logo
  61. $this->option_tva = 0; // Gere option tva FACTURE_TVAOPTION
  62. $this->option_modereg = 0; // Affiche mode reglement
  63. $this->option_condreg = 0; // Affiche conditions reglement
  64. $this->option_codeproduitservice = 0; // Affiche code produit-service
  65. $this->option_multilang = 0; // Dispo en plusieurs langues
  66. $this->option_escompte = 0; // Affiche si il y a eu escompte
  67. $this->option_credit_note = 0; // Support credit notes
  68. $this->option_freetext = 1; // Support add of a personalised text
  69. $this->option_draft_watermark = 0; // Support add of a watermark on drafts
  70. // Recupere emetteur
  71. $this->emetteur=$mysoc;
  72. if (! $this->emetteur->pays_code) $this->emetteur->pays_code=substr($langs->defaultlang,-2); // Par defaut, si n'etait pas defini
  73. }
  74. /**
  75. * Define array with couple substitution key => substitution value
  76. *
  77. * @param Object $object Main object to use as data source
  78. * @param Translate $outputlangs Lang object to use for output
  79. * @return array Array of substitution
  80. */
  81. function get_substitutionarray_object($object,$outputlangs)
  82. {
  83. global $conf;
  84. $invoice_source=new Facture($this->db);
  85. if ($object->fk_facture_source > 0)
  86. {
  87. $invoice_source->fetch($object->fk_facture_source);
  88. }
  89. $sumpayed = $object->getSommePaiement();
  90. $alreadypayed=price($sumpayed,0,$outputlangs);
  91. return array(
  92. 'object_id'=>$object->id,
  93. 'object_ref'=>$object->ref,
  94. 'object_ref_ext'=>$object->ref_ext,
  95. 'object_ref_customer'=>$object->ref_client,
  96. 'object_ref_supplier'=>(! empty($object->ref_fournisseur)?$object->ref_fournisseur:''),
  97. 'object_source_invoice_ref'=>$invoice_source->ref,
  98. 'object_date'=>dol_print_date($object->date,'day'),
  99. 'object_date_limit'=>dol_print_date($object->date_lim_reglement,'day'),
  100. 'object_date_creation'=>dol_print_date($object->date_creation,'day'),
  101. 'object_date_modification'=>(! empty($object->date_modification)?dol_print_date($object->date_modification,'day'):''),
  102. 'object_date_validation'=>dol_print_date($object->date_validation,'dayhour'),
  103. 'object_payment_mode_code'=>$object->mode_reglement_code,
  104. 'object_payment_mode'=>($outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code)!='PaymentType'.$object->mode_reglement_code?$outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code):$object->mode_reglement),
  105. 'object_payment_term_code'=>$object->cond_reglement_code,
  106. 'object_payment_term'=>($outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code)!='PaymentCondition'.$object->cond_reglement_code?$outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code):$object->cond_reglement),
  107. 'object_total_ht'=>price($object->total_ht,0,$outputlangs),
  108. 'object_total_vat'=>price($object->total_tva,0,$outputlangs),
  109. 'object_total_ttc'=>price($object->total_ttc,0,$outputlangs),
  110. 'object_total_discount' => price($object->getTotalDiscount(), 0, $outputlangs),
  111. 'object_vatrate'=>(isset($object->tva)?vatrate($object->tva):''),
  112. 'object_note_private'=>$object->note,
  113. 'object_note'=>$object->note_public,
  114. // Payments
  115. 'object_already_payed'=>$alreadypayed,
  116. 'object_remain_to_pay'=>price($object->total_ttc - $sumpayed,0,$outputlangs)
  117. );
  118. }
  119. /**
  120. * Define array with couple substitution key => substitution value
  121. *
  122. * @param array $line Array of lines
  123. * @param Translate $outputlangs Lang object to use for output
  124. * @return array Return substitution array
  125. */
  126. function get_substitutionarray_lines($line,$outputlangs)
  127. {
  128. global $conf;
  129. return array(
  130. 'line_fulldesc'=>doc_getlinedesc($line,$outputlangs),
  131. 'line_product_ref'=>$line->product_ref,
  132. 'line_product_label'=>$line->product_label,
  133. 'line_desc'=>$line->desc,
  134. 'line_vatrate'=>vatrate($line->tva_tx,true,$line->info_bits),
  135. 'line_up'=>price($line->subprice, 0, $outputlangs),
  136. 'line_qty'=>$line->qty,
  137. 'line_discount_percent'=>($line->remise_percent?$line->remise_percent.'%':''),
  138. 'line_price_ht'=>price($line->total_ht, 0, $outputlangs),
  139. 'line_price_ttc'=>price($line->total_ttc, 0, $outputlangs),
  140. 'line_price_vat'=>price($line->total_tva, 0, $outputlangs),
  141. 'line_date_start'=>$line->date_start,
  142. 'line_date_end'=>$line->date_end
  143. );
  144. }
  145. /**
  146. * Return description of a module
  147. *
  148. * @param Translate $langs Lang object to use for output
  149. * @return string Description
  150. */
  151. function info($langs)
  152. {
  153. global $conf,$langs;
  154. $langs->load("companies");
  155. $langs->load("errors");
  156. $form = new Form($this->db);
  157. $texte = $this->description.".<br>\n";
  158. $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  159. $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  160. $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
  161. $texte.= '<input type="hidden" name="param1" value="FACTURE_ADDON_PDF_ODT_PATH">';
  162. $texte.= '<table class="nobordernopadding" width="100%">';
  163. // List of directories area
  164. $texte.= '<tr><td>';
  165. $texttitle=$langs->trans("ListOfDirectories");
  166. $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->FACTURE_ADDON_PDF_ODT_PATH)));
  167. $listoffiles=array();
  168. foreach($listofdir as $key=>$tmpdir)
  169. {
  170. $tmpdir=trim($tmpdir);
  171. $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
  172. if (! $tmpdir) { unset($listofdir[$key]); continue; }
  173. if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
  174. else
  175. {
  176. $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.odt');
  177. if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
  178. }
  179. }
  180. $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
  181. // Add list of substitution keys
  182. $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
  183. $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
  184. $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
  185. $texte.= '<table><tr><td>';
  186. $texte.= '<textarea class="flat" cols="60" name="value1">';
  187. $texte.=$conf->global->FACTURE_ADDON_PDF_ODT_PATH;
  188. $texte.= '</textarea>';
  189. $texte.= '</td>';
  190. $texte.= '<td align="center">&nbsp; ';
  191. $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
  192. $texte.= '</td>';
  193. $texte.= '</tr>';
  194. $texte.= '</table>';
  195. // Scan directories
  196. if (count($listofdir)) $texte.=$langs->trans("NumberOfModelFilesFound").': <b>'.count($listoffiles).'</b>';
  197. $texte.= '</td>';
  198. $texte.= '<td valign="top" rowspan="2">';
  199. $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
  200. $texte.= '</td>';
  201. $texte.= '</tr>';
  202. /*$texte.= '<tr>';
  203. $texte.= '<td align="center">';
  204. $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
  205. $texte.= '</td>';
  206. $texte.= '</tr>';*/
  207. $texte.= '</table>';
  208. $texte.= '</form>';
  209. return $texte;
  210. }
  211. /**
  212. * Function to build a document on disk using the generic odt module.
  213. *
  214. * @param Facture $object Object source to build document
  215. * @param Translate $outputlangs Lang output object
  216. * @param string $srctemplatepath Full path of source filename for generator using a template file
  217. * @return int 1 if OK, <=0 if KO
  218. */
  219. function write_file($object,$outputlangs,$srctemplatepath)
  220. {
  221. global $user,$langs,$conf,$mysoc;
  222. if (empty($srctemplatepath))
  223. {
  224. dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
  225. return -1;
  226. }
  227. if (! is_object($outputlangs)) $outputlangs=$langs;
  228. $sav_charset_output=$outputlangs->charset_output;
  229. $outputlangs->charset_output='UTF-8';
  230. $outputlangs->load("main");
  231. $outputlangs->load("dict");
  232. $outputlangs->load("companies");
  233. $outputlangs->load("bills");
  234. if ($conf->facture->dir_output)
  235. {
  236. // If $object is id instead of object
  237. if (! is_object($object))
  238. {
  239. $id = $object;
  240. $object = new Facture($this->db);
  241. $result=$object->fetch($id);
  242. if ($result < 0)
  243. {
  244. dol_print_error($this->db,$object->error);
  245. return -1;
  246. }
  247. }
  248. $dir = $conf->facture->dir_output;
  249. $objectref = dol_sanitizeFileName($object->ref);
  250. if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
  251. $file = $dir . "/" . $objectref . ".odt";
  252. if (! file_exists($dir))
  253. {
  254. if (dol_mkdir($dir) < 0)
  255. {
  256. $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
  257. return -1;
  258. }
  259. }
  260. if (file_exists($dir))
  261. {
  262. //print "srctemplatepath=".$srctemplatepath;exit; // Src filename
  263. $newfile=basename($srctemplatepath);
  264. $newfiletmp=preg_replace('/\.odt/i','',$newfile);
  265. $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
  266. $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
  267. $newfiletmp=$objectref.'_'.$newfiletmp;
  268. //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
  269. $file=$dir.'/'.$newfiletmp.'.odt';
  270. //print "newdir=".$dir;
  271. //print "newfile=".$newfile;
  272. //print "file=".$file;
  273. //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
  274. dol_mkdir($conf->facture->dir_temp);
  275. // If BILLING contact defined on invoice, we use it
  276. $usecontact=false;
  277. $arrayidcontact=$object->getIdContact('external','BILLING');
  278. if (count($arrayidcontact) > 0)
  279. {
  280. $usecontact=true;
  281. $result=$object->fetch_contact($arrayidcontact[0]);
  282. }
  283. // Recipient name
  284. if (! empty($usecontact))
  285. {
  286. // On peut utiliser le nom de la societe du contact
  287. if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)) $socobject = $object->contact;
  288. else $socobject = $object->client;
  289. }
  290. else
  291. {
  292. $socobject=$object->client;
  293. }
  294. // Make substitution
  295. $substitutionarray=array(
  296. '__FROM_NAME__' => $this->emetteur->nom,
  297. '__FROM_EMAIL__' => $this->emetteur->email,
  298. '__TOTAL_TTC__' => $object->total_ttc,
  299. '__TOTAL_HT__' => $object->total_ht,
  300. '__TOTAL_VAT__' => $object->total_tva
  301. );
  302. complete_substitutions_array($substitutionarray, $langs, $object);
  303. // Line of free text
  304. $newfreetext='';
  305. $paramfreetext='FACTURE_FREE_TEXT';
  306. if (! empty($conf->global->$paramfreetext))
  307. {
  308. $newfreetext=make_substitutions($conf->global->$paramfreetext,$substitutionarray);
  309. }
  310. // Open and load template
  311. require_once ODTPHP_PATH.'odf.php';
  312. $odfHandler = new odf(
  313. $srctemplatepath,
  314. array(
  315. 'PATH_TO_TMP' => $conf->facture->dir_temp,
  316. 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
  317. 'DELIMITER_LEFT' => '{',
  318. 'DELIMITER_RIGHT' => '}'
  319. )
  320. );
  321. // After construction $odfHandler->contentXml contains content and
  322. // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
  323. // [!-- BEGIN lines --]*[!-- END lines --]
  324. //print html_entity_decode($odfHandler->__toString());
  325. //print exit;
  326. // Make substitutions into odt of freetext
  327. try {
  328. $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8');
  329. }
  330. catch(OdfException $e)
  331. {
  332. }
  333. // Make substitutions into odt of user info
  334. $tmparray=$this->get_substitutionarray_user($user,$outputlangs);
  335. //var_dump($tmparray); exit;
  336. foreach($tmparray as $key=>$value)
  337. {
  338. try {
  339. if (preg_match('/logo$/',$key)) // Image
  340. {
  341. //var_dump($value);exit;
  342. if (file_exists($value)) $odfHandler->setImage($key, $value);
  343. else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
  344. }
  345. else // Text
  346. {
  347. $odfHandler->setVars($key, $value, true, 'UTF-8');
  348. }
  349. }
  350. catch(OdfException $e)
  351. {
  352. }
  353. }
  354. // Make substitutions into odt of mysoc
  355. $tmparray=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
  356. //var_dump($tmparray); exit;
  357. foreach($tmparray as $key=>$value)
  358. {
  359. try {
  360. if (preg_match('/logo$/',$key)) // Image
  361. {
  362. //var_dump($value);exit;
  363. if (file_exists($value)) $odfHandler->setImage($key, $value);
  364. else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
  365. }
  366. else // Text
  367. {
  368. $odfHandler->setVars($key, $value, true, 'UTF-8');
  369. }
  370. }
  371. catch(OdfException $e)
  372. {
  373. }
  374. }
  375. // Make substitutions into odt of thirdparty
  376. $tmparray=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
  377. foreach($tmparray as $key=>$value)
  378. {
  379. try {
  380. if (preg_match('/logo$/',$key)) // Image
  381. {
  382. if (file_exists($value)) $odfHandler->setImage($key, $value);
  383. else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
  384. }
  385. else // Text
  386. {
  387. $odfHandler->setVars($key, $value, true, 'UTF-8');
  388. }
  389. }
  390. catch(OdfException $e)
  391. {
  392. }
  393. }
  394. // Replace tags of object + external modules
  395. $tmparray=$this->get_substitutionarray_object($object,$outputlangs);
  396. complete_substitutions_array($tmparray, $outputlangs, $object);
  397. foreach($tmparray as $key=>$value)
  398. {
  399. try {
  400. if (preg_match('/logo$/',$key)) // Image
  401. {
  402. if (file_exists($value)) $odfHandler->setImage($key, $value);
  403. else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
  404. }
  405. else // Text
  406. {
  407. $odfHandler->setVars($key, $value, true, 'UTF-8');
  408. }
  409. }
  410. catch(OdfException $e)
  411. {
  412. }
  413. }
  414. // Replace tags of lines
  415. try
  416. {
  417. $listlines = $odfHandler->setSegment('lines');
  418. foreach ($object->lines as $line)
  419. {
  420. $tmparray=$this->get_substitutionarray_lines($line,$outputlangs);
  421. complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines");
  422. foreach($tmparray as $key => $val)
  423. {
  424. try
  425. {
  426. $listlines->setVars($key, $val, true, 'UTF-8');
  427. }
  428. catch(OdfException $e)
  429. {
  430. }
  431. catch(SegmentException $e)
  432. {
  433. }
  434. }
  435. $listlines->merge();
  436. }
  437. $odfHandler->mergeSegment($listlines);
  438. }
  439. catch(OdfException $e)
  440. {
  441. $this->error=$e->getMessage();
  442. dol_syslog($this->error, LOG_WARNING);
  443. return -1;
  444. }
  445. // Write new file
  446. //$result=$odfHandler->exportAsAttachedFile('toto');
  447. $odfHandler->saveToDisk($file);
  448. if (! empty($conf->global->MAIN_UMASK))
  449. @chmod($file, octdec($conf->global->MAIN_UMASK));
  450. $odfHandler=null; // Destroy object
  451. return 1; // Success
  452. }
  453. else
  454. {
  455. $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
  456. return -1;
  457. }
  458. }
  459. return -1;
  460. }
  461. }
  462. ?>