PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/modules/livraison/pdf/pdf_sirocco.modules.php

https://bitbucket.org/speedealing/speedealing
PHP | 476 lines | 293 code | 81 blank | 102 comment | 38 complexity | 633007f5a0e0b6d53f9d2f001475211e MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2011 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. * or see http://www.gnu.org/
  20. */
  21. /**
  22. * \file htdocs/core/modules/livraison/pdf/pdf_sirocco.modules.php
  23. * \ingroup livraison
  24. * \brief File of class to manage receving receipts with template Sirocco
  25. */
  26. require_once DOL_DOCUMENT_ROOT.'/core/modules/livraison/modules_livraison.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  29. /**
  30. * Classe permettant de generer les bons de livraison au modele Sirocco
  31. */
  32. class pdf_sirocco extends ModelePDFDeliveryOrder
  33. {
  34. /**
  35. * Constructor
  36. *
  37. * @param DoliDB $db Database handler
  38. */
  39. function __construct($db = '')
  40. {
  41. global $conf,$langs,$mysoc;
  42. $langs->load("main");
  43. $langs->load("bills");
  44. $langs->load("sendings");
  45. $langs->load("companies");
  46. $this->db = $db;
  47. $this->name = "sirocco";
  48. $this->description = $langs->trans("DocumentModelSirocco");
  49. // Dimension page pour format A4
  50. $this->type = 'pdf';
  51. $formatarray=pdf_getFormat();
  52. $this->page_largeur = $formatarray['width'];
  53. $this->page_hauteur = $formatarray['height'];
  54. $this->format = array($this->page_largeur,$this->page_hauteur);
  55. $this->marge_gauche=10;
  56. $this->marge_droite=10;
  57. $this->marge_haute=10;
  58. $this->marge_basse=10;
  59. // Recupere emmetteur
  60. $this->emetteur=$mysoc;
  61. if (! $this->emetteur->pays_code) $this->emetteur->pays_code=substr($langs->defaultlang,-2); // Par defaut, si n'�tait pas d�fini
  62. $this->tva=array();
  63. }
  64. /**
  65. * Fonction generant le bon de livraison sur le disque
  66. *
  67. * @param Object $object Object livraison a generer
  68. * @param Translate $outputlangs Lang output object
  69. * @return int 1 if OK, <=0 if KO
  70. */
  71. function write_file($object,$outputlangs)
  72. {
  73. global $user,$conf,$langs;
  74. $default_font_size = pdf_getPDFFontSize($outputlangs);
  75. if (! is_object($outputlangs)) $outputlangs=$langs;
  76. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  77. if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
  78. $outputlangs->load("main");
  79. $outputlangs->load("dict");
  80. $outputlangs->load("bills");
  81. $outputlangs->load("products");
  82. $outputlangs->load("deliveries");
  83. $outputlangs->load("sendings");
  84. if ($conf->expedition->dir_output."/receipt")
  85. {
  86. $object->fetch_thirdparty();
  87. $nblines = count($object->lines);
  88. $objectref = dol_sanitizeFileName($object->ref);
  89. $dir = $conf->expedition->dir_output."/receipt";
  90. if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
  91. $file = $dir . "/" . $objectref . ".pdf";
  92. if (! file_exists($dir))
  93. {
  94. if (dol_mkdir($dir) < 0)
  95. {
  96. $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
  97. return 0;
  98. }
  99. }
  100. if (file_exists($dir))
  101. {
  102. $pdf=pdf_getInstance($this->format);
  103. $heightforinfotot = 50; // Height reserved to output the info and total part
  104. $heightforfooter = 25; // Height reserved to output the footer (value include bottom margin)
  105. $pdf->SetAutoPageBreak(1,0);
  106. if (class_exists('TCPDF'))
  107. {
  108. $pdf->setPrintHeader(false);
  109. $pdf->setPrintFooter(false);
  110. }
  111. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  112. // Set path to the background PDF File
  113. if (empty($conf->global->MAIN_DISABLE_FPDI) && ! empty($conf->global->MAIN_ADD_PDF_BACKGROUND))
  114. {
  115. $pagecount = $pdf->setSourceFile($conf->mycompany->dir_output.'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND);
  116. $tplidx = $pdf->importPage(1);
  117. }
  118. // Complete object by loading several other informations
  119. $expedition=new Expedition($this->db);
  120. $result = $expedition->fetch($object->expedition_id);
  121. $commande = new Commande($this->db);
  122. if ($expedition->origin == 'commande')
  123. {
  124. $commande->fetch($expedition->origin_id);
  125. }
  126. $object->commande=$commande;
  127. $pdf->Open();
  128. $pagenb=0;
  129. $pdf->SetDrawColor(128,128,128);
  130. $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
  131. $pdf->SetSubject($outputlangs->transnoentities("DeliveryOrder"));
  132. $pdf->SetCreator("Speedealing ".DOL_VERSION);
  133. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  134. $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("DeliveryOrder"));
  135. if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
  136. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  137. // New page
  138. $pdf->AddPage();
  139. if (! empty($tplidx)) $pdf->useTemplate($tplidx);
  140. $pagenb++;
  141. $this->_pagehead($pdf, $object, 1, $outputlangs);
  142. $pdf->SetFont('','', $default_font_size - 1);
  143. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  144. $pdf->SetTextColor(0,0,0);
  145. $tab_top = 90;
  146. $tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)?42:10);
  147. $tab_height = 130;
  148. $tab_height_newpage = 150;
  149. $iniY = $tab_top + 7;
  150. $curY = $tab_top + 7;
  151. $nexY = $tab_top + 7;
  152. // Loop on each lines
  153. for ($i = 0 ; $i < $nblines ; $i++)
  154. {
  155. $curY = $nexY;
  156. $pdf->SetFont('','', $default_font_size - 1); // Into loop to work with multipage
  157. $pdf->SetTextColor(0,0,0);
  158. $pdf->setTopMargin($tab_top_newpage);
  159. $pdf->setPageOrientation('', 1, $this->marge_basse+$heightforfooter+$heightforinfotot); // The only function to edit the bottom margin of current page to set it.
  160. $pageposbefore=$pdf->getPage();
  161. // Description of product line
  162. pdf_writelinedesc($pdf,$object,$i,$outputlangs,100,3,30,$curY,1);
  163. $nexY = $pdf->GetY();
  164. $pageposafter=$pdf->getPage();
  165. $pdf->setPage($pageposbefore);
  166. $pdf->setTopMargin($this->marge_haute);
  167. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  168. // We suppose that a too long description is moved completely on next page
  169. if ($pageposafter > $pageposbefore) {
  170. $pdf->setPage($pageposafter); $curY = $tab_top_newpage;
  171. }
  172. $pdf->SetFont('','', $default_font_size - 1); // Dans boucle pour gerer multi-page
  173. $pdf->SetXY(10, $curY);
  174. $pdf->MultiCell(20, 3, $outputlangs->convToOutputCharset($object->lines[$i]->ref), 0, 'C');
  175. // TODO Field not yet saved in database
  176. //$pdf->SetXY(133, $curY );
  177. //$pdf->MultiCell(10, 5, $object->lines[$i]->tva_tx, 0, 'C');
  178. $pdf->SetXY(145, $curY);
  179. $pdf->MultiCell(10, 3, $object->lines[$i]->qty_shipped, 0, 'C');
  180. // TODO Field not yet saved in database
  181. //$pdf->SetXY(156, $curY );
  182. //$pdf->MultiCell(20, 3, price($object->lines[$i]->price), 0, 'R', 0);
  183. // TODO Field not yet saved in database
  184. //$pdf->SetXY(174, $curY );
  185. //$total = price($object->lines[$i]->price * $object->lines[$i]->qty_shipped);
  186. //$pdf->MultiCell(26, 3, $total, 0, 'R', 0);
  187. $pdf->line(10, $curY-1, 200, $curY-1);
  188. $nexY+=2; // Passe espace entre les lignes
  189. // Detect if some page were added automatically and output _tableau for past pages
  190. while ($pagenb < $pageposafter)
  191. {
  192. $pdf->setPage($pagenb);
  193. if ($pagenb == 1)
  194. {
  195. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
  196. }
  197. else
  198. {
  199. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
  200. }
  201. $this->_pagefoot($pdf,$object,$outputlangs);
  202. $pagenb++;
  203. $pdf->setPage($pagenb);
  204. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  205. }
  206. if (isset($object->lines[$i+1]->pagebreak) && $object->lines[$i+1]->pagebreak)
  207. {
  208. if ($pagenb == 1)
  209. {
  210. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
  211. }
  212. else
  213. {
  214. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
  215. }
  216. $this->_pagefoot($pdf,$object,$outputlangs);
  217. // New page
  218. $pdf->AddPage();
  219. if (! empty($tplidx)) $pdf->useTemplate($tplidx);
  220. $pagenb++;
  221. }
  222. }
  223. // Show square
  224. if ($pagenb == 1)
  225. {
  226. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfooter, 0, $outputlangs, 0, 0);
  227. $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfooter + 1;
  228. }
  229. else
  230. {
  231. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfooter, 0, $outputlangs, 1, 0);
  232. $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfooter + 1;
  233. }
  234. /*
  235. * Pied de page
  236. */
  237. $this->_pagefoot($pdf, $object, $outputlangs);
  238. $pdf->AliasNbPages();
  239. $pdf->Close();
  240. $pdf->Output($file,'F');
  241. if (! empty($conf->global->MAIN_UMASK))
  242. @chmod($file, octdec($conf->global->MAIN_UMASK));
  243. return 1;
  244. }
  245. }
  246. else
  247. {
  248. $this->error=$langs->transnoentities("ErrorConstantNotDefined","LIVRAISON_OUTPUTDIR");
  249. return 0;
  250. }
  251. }
  252. /**
  253. * Show table for lines
  254. *
  255. * @param PDF &$pdf Object PDF
  256. * @param string $tab_top Top position of table
  257. * @param string $tab_height Height of table (rectangle)
  258. * @param int $nexY Y
  259. * @param Translate $outputlangs Langs object
  260. * @param int $hidetop Hide top bar of array
  261. * @param int $hidebottom Hide bottom bar of array
  262. * @return void
  263. */
  264. function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop=0, $hidebottom=0)
  265. {
  266. $default_font_size = pdf_getPDFFontSize($outputlangs);
  267. $pdf->SetFont('','', $default_font_size - 1);
  268. $pdf->SetXY(30, $tab_top+1);
  269. $pdf->MultiCell(60, 2, $outputlangs->transnoentities("Designation"), 0, 'L');
  270. $pdf->line(144, $tab_top, 144, $tab_top + $tab_height);
  271. $pdf->SetXY(147, $tab_top+1);
  272. $pdf->MultiCell(30, 2, $outputlangs->transnoentities("QtyShipped"), 0, 'L');
  273. $pdf->Rect(10, $tab_top, 190, $tab_height);
  274. }
  275. /**
  276. * Show top header of page.
  277. *
  278. * @param PDF &$pdf Object PDF
  279. * @param Object $object Object to show
  280. * @param int $showaddress 0=no, 1=yes
  281. * @param Translate $outputlangs Object lang for output
  282. * @return void
  283. */
  284. function _pagehead(&$pdf, $object, $showaddress, $outputlangs)
  285. {
  286. global $langs,$conf,$mysoc;
  287. $default_font_size = pdf_getPDFFontSize($outputlangs);
  288. $outputlangs->load("companies");
  289. pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
  290. $pdf->SetTextColor(0,0,60);
  291. $pdf->SetFont('','B', $default_font_size + 3);
  292. $posx=$this->page_largeur-$this->marge_droite-100;
  293. $posy=$this->marge_haute;
  294. $pdf->SetXY($this->marge_gauche,$posy);
  295. if (! empty($conf->global->MAIN_INFO_SOCIETE_NOM))
  296. {
  297. $pdf->SetTextColor(0,0,200);
  298. $pdf->SetFont('','B', $default_font_size + 2);
  299. $pdf->MultiCell(76, 4, $outputlangs->convToOutputCharset(MAIN_INFO_SOCIETE_NOM), 0, 'L');
  300. }
  301. // Sender properties
  302. $carac_emetteur = pdf_build_address($outputlangs,$this->emetteur);
  303. $pdf->SetFont('','', $default_font_size - 1);
  304. $pdf->SetXY($this->marge_gauche,$posy+4);
  305. $pdf->MultiCell(80, 3, $carac_emetteur, 0, 'L');
  306. /*
  307. * Adresse Client
  308. */
  309. // If SHIPPING contact defined on invoice, we use it
  310. $usecontact=false;
  311. $arrayidcontact=$object->commande->getIdContact('external','SHIPPING');
  312. if (count($arrayidcontact) > 0)
  313. {
  314. $usecontact=true;
  315. $result=$object->fetch_contact($arrayidcontact[0]);
  316. }
  317. // Recipient name
  318. if (! empty($usecontact))
  319. {
  320. // On peut utiliser le nom de la societe du contact
  321. if (! empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)) $socname = $object->contact->socname;
  322. else $socname = $object->client->nom;
  323. $carac_client_name=$outputlangs->convToOutputCharset($socname);
  324. }
  325. else
  326. {
  327. $carac_client_name=$outputlangs->convToOutputCharset($object->client->nom);
  328. }
  329. $carac_client=pdf_build_address($outputlangs,$this->emetteur,$object->client,$object->contact,$usecontact,'target');
  330. $pdf->SetTextColor(0,0,0);
  331. $pdf->SetFont('','B', $default_font_size + 1);
  332. $pdf->SetXY(102,42);
  333. $pdf->MultiCell(96,5, $carac_client_name, 0, 'L');
  334. $pdf->SetFont('','B', $default_font_size);
  335. $pdf->SetXY(102,47);
  336. $pdf->MultiCell(96,5, $carac_client, 0, 'L');
  337. $pdf->Rect(100, 40, 100, 40);
  338. $pdf->SetTextColor(0,0,60);
  339. $pdf->SetFont('','B', $default_font_size + 1);
  340. $pdf->SetXY($posx, 86);
  341. $pdf->MultiCell(100, 2, $outputlangs->transnoentities("Date")." : " . dol_print_date(($object->date_delivery?$object->date_delivery:$date->valid),"day",false,$outputlangs,true), 0, 'R');
  342. $pdf->SetXY($posx, 92);
  343. $pdf->MultiCell(100, 2, $outputlangs->transnoentities("DeliveryOrder")." ".$outputlangs->convToOutputCharset($object->ref), 0, 'R');
  344. if ($object->client->code_client)
  345. {
  346. $posy+=7;
  347. $pdf->SetXY($posx, $posy);
  348. $pdf->SetTextColor(0,0,60);
  349. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("CustomerCode")." : " . $outputlangs->transnoentities($object->client->code_client), '', 'R');
  350. }
  351. $pdf->SetFont('','B', $default_font_size - 1);
  352. // Add origin linked objects
  353. // TODO extend to other objects
  354. $object->fetchObjectLinked('','',$object->id,'delivery');
  355. if (! empty($object->linkedObjects))
  356. {
  357. $outputlangs->load('orders');
  358. foreach($object->linkedObjects as $elementtype => $objects)
  359. {
  360. $object->fetchObjectLinked('','',$objects[0]->id,$objects[0]->element);
  361. foreach($object->linkedObjects as $elementtype => $objects)
  362. {
  363. $num=count($objects);
  364. for ($i=0;$i<$num;$i++)
  365. {
  366. $order=new Commande($this->db);
  367. $result=$order->fetch($objects[$i]->id);
  368. if ($result >= 0)
  369. {
  370. $posy+=5;
  371. $pdf->SetXY($posx,$posy);
  372. $pdf->SetFont('','', $default_font_size - 1);
  373. $text=$order->ref;
  374. if ($order->ref_client) $text.=' ('.$order->ref_client.')';
  375. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("RefOrder")." : ".$outputlangs->transnoentities($text), '', 'R');
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. /**
  383. * Show footer of page. Need this->emetteur object
  384. *
  385. * @param PDF &$pdf PDF
  386. * @param Object $object Object to show
  387. * @param Translate $outputlangs Object lang for output
  388. * @return void
  389. */
  390. function _pagefoot(&$pdf,$object,$outputlangs)
  391. {
  392. return pdf_pagefoot($pdf,$outputlangs,'DELIVERY_FREE_TEXT',$this->emetteur,$this->marge_basse,$this->marge_gauche,$this->page_hauteur,$object);
  393. }
  394. }
  395. ?>