pdf_standard.modules.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@stocks.sourceforge.net>
  3. * Copyright (C) 2022 Ferran Marcet <fmarcet@2byte.es>
  4. * Copyright (C) 2022 Nicolas Silobre <nsilobre@ns-info90.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/stock/doc/pdf_standard.modules.php
  22. * \ingroup societe
  23. * \brief File of class to build PDF documents for stocks/services
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/modules/stock/modules_stock.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  32. /**
  33. * Class to build documents using ODF templates generator
  34. */
  35. class pdf_standard extends ModelePDFStock
  36. {
  37. /**
  38. * @var DoliDb Database handler
  39. */
  40. public $db;
  41. /**
  42. * @var string model name
  43. */
  44. public $name;
  45. /**
  46. * @var string model description (short text)
  47. */
  48. public $description;
  49. /**
  50. * @var string document type
  51. */
  52. public $type;
  53. /**
  54. * @var array Minimum version of PHP required by module.
  55. * e.g.: PHP ≥ 5.6 = array(5, 6)
  56. */
  57. public $phpmin = array(5, 6);
  58. /**
  59. * Dolibarr version of the loaded document
  60. * @var string
  61. */
  62. public $version = 'dolibarr';
  63. /**
  64. * Issuer
  65. * @var Societe
  66. */
  67. public $emetteur;
  68. public $wref;
  69. public $posxdesc;
  70. public $posxlabel;
  71. public $posxtva;
  72. public $posxqty;
  73. public $posxup;
  74. public $posxunit;
  75. public $posxdiscount;
  76. public $postotalht;
  77. public $tabTitleHeight;
  78. /**
  79. * Constructor
  80. *
  81. * @param DoliDB $db Database handler
  82. */
  83. public function __construct($db)
  84. {
  85. global $conf, $langs, $mysoc;
  86. // Load traductions files required by page
  87. $langs->loadLangs(array("main", "companies"));
  88. $this->db = $db;
  89. $this->name = "standard";
  90. $this->description = $langs->trans("DocumentModelStandardPDF");
  91. // Page size for A4 format
  92. $this->type = 'pdf';
  93. $formatarray = pdf_getFormat();
  94. $this->page_largeur = $formatarray['width'];
  95. $this->page_hauteur = $formatarray['height'];
  96. $this->format = array($this->page_largeur, $this->page_hauteur);
  97. $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
  98. $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
  99. $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
  100. $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
  101. $this->option_logo = 1; // Display logo
  102. $this->option_codestockservice = 0; // Display product-service code
  103. $this->option_multilang = 1; // Available in several languages
  104. $this->option_freetext = 0; // Support add of a personalised text
  105. // Get source company
  106. $this->emetteur = $mysoc;
  107. if (!$this->emetteur->country_code) {
  108. $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
  109. }
  110. // Define position of columns
  111. $this->wref = 35;
  112. $this->posxdesc = $this->marge_gauche + 1;
  113. $this->posxlabel = $this->posxdesc + $this->wref;
  114. $this->posxtva = 80;
  115. $this->posxqty = 95;
  116. $this->posxup = 115;
  117. $this->posxunit = 135;
  118. $this->posxdiscount = 155;
  119. $this->postotalht = 175;
  120. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT) || !empty($conf->global->MAIN_GENERATE_DOCUMENTS_WITHOUT_VAT_COLUMN)) {
  121. $this->posxtva = $this->posxup;
  122. }
  123. $this->posxpicture = $this->posxtva - (empty($conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH) ? 20 : $conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH); // width of images
  124. if ($this->page_largeur < 210) { // To work with US executive format
  125. $this->posxpicture -= 20;
  126. $this->posxtva -= 20;
  127. $this->posxup -= 20;
  128. $this->posxqty -= 20;
  129. $this->posxunit -= 20;
  130. $this->posxdiscount -= 20;
  131. $this->postotalht -= 20;
  132. }
  133. $this->tabTitleHeight = 11;
  134. }
  135. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  136. /**
  137. * Function to build a document on disk using the generic odt module.
  138. *
  139. * @param Entrepot $object Object source to build document
  140. * @param Translate $outputlangs Lang output object
  141. * @param string $srctemplatepath Full path of source filename for generator using a template file
  142. * @param int $hidedetails Do not show line details
  143. * @param int $hidedesc Do not show desc
  144. * @param int $hideref Do not show ref
  145. * @return int 1 if OK, <=0 if KO
  146. */
  147. public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  148. {
  149. // phpcs:enable
  150. global $user, $langs, $conf, $mysoc, $db, $hookmanager;
  151. if (!is_object($outputlangs)) {
  152. $outputlangs = $langs;
  153. }
  154. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  155. if (!empty($conf->global->MAIN_USE_FPDF)) {
  156. $outputlangs->charset_output = 'ISO-8859-1';
  157. }
  158. // Load traductions files required by page
  159. $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "stocks", "orders", "deliveries"));
  160. if ($conf->stock->dir_output) {
  161. // Definition of $dir and $file
  162. if ($object->specimen) {
  163. $dir = $conf->stock->dir_output;
  164. $file = $dir."/SPECIMEN.pdf";
  165. } else {
  166. $objectref = dol_sanitizeFileName($object->ref);
  167. $dir = $conf->stock->dir_output."/".$objectref;
  168. $file = $dir."/".$objectref.".pdf";
  169. }
  170. $stockFournisseur = new ProductFournisseur($this->db);
  171. $supplierprices = $stockFournisseur->list_product_fournisseur_price($object->id);
  172. $object->supplierprices = $supplierprices;
  173. $productstatic = new Product($this->db);
  174. if (!file_exists($dir)) {
  175. if (dol_mkdir($dir) < 0) {
  176. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  177. return -1;
  178. }
  179. }
  180. if (file_exists($dir)) {
  181. // Add pdfgeneration hook
  182. if (!is_object($hookmanager)) {
  183. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  184. $hookmanager = new HookManager($this->db);
  185. }
  186. $hookmanager->initHooks(array('pdfgeneration'));
  187. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  188. global $action;
  189. $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  190. // Create pdf instance
  191. $pdf = pdf_getInstance($this->format);
  192. $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance
  193. $pdf->SetAutoPageBreak(1, 0);
  194. $heightforinfotot = 40; // Height reserved to output the info and total part
  195. $heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5); // Height reserved to output the free text on last page
  196. $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
  197. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
  198. $heightforfooter += 6;
  199. }
  200. if (class_exists('TCPDF')) {
  201. $pdf->setPrintHeader(false);
  202. $pdf->setPrintFooter(false);
  203. }
  204. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  205. // Set path to the background PDF File
  206. if (empty($conf->global->MAIN_DISABLE_FPDI) && !empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) {
  207. $pagecount = $pdf->setSourceFile($conf->mycompany->dir_output.'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND);
  208. $tplidx = $pdf->importPage(1);
  209. }
  210. $pdf->Open();
  211. $pagenb = 0;
  212. $pdf->SetDrawColor(128, 128, 128);
  213. $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
  214. $pdf->SetSubject($outputlangs->transnoentities("Stock"));
  215. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  216. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  217. $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("Stock")." ".$outputlangs->convToOutputCharset($object->label));
  218. if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) {
  219. $pdf->SetCompression(false);
  220. }
  221. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  222. // New page
  223. $pdf->AddPage();
  224. if (!empty($tplidx)) {
  225. $pdf->useTemplate($tplidx);
  226. }
  227. $pagenb++;
  228. $top_shift = $this->_pagehead($pdf, $object, 1, $outputlangs);
  229. $pdf->SetFont('', '', $default_font_size - 1);
  230. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  231. $pdf->SetTextColor(0, 0, 0);
  232. $tab_top = 65 + $top_shift;
  233. $tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD) ? 42 + $top_shift : 10);
  234. $tab_height = $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext;
  235. // Show list of product in warehouse */
  236. $totalunit = 0;
  237. $totalvalue = $totalvaluesell = 0;
  238. $sortfield = 'p.ref';
  239. $sortorder = 'ASC';
  240. $sql = "SELECT p.rowid as rowid, p.ref, p.label as produit, p.tobatch, p.fk_product_type as type, p.pmp as ppmp, p.price, p.price_ttc, p.entity,";
  241. $sql .= " ps.reel as value";
  242. $sql .= " FROM ".MAIN_DB_PREFIX."product_stock as ps, ".MAIN_DB_PREFIX."product as p";
  243. $sql .= " WHERE ps.fk_product = p.rowid";
  244. $sql .= " AND ps.reel <> 0"; // We do not show if stock is 0 (no product in this warehouse)
  245. $sql .= " AND ps.fk_entrepot = ".((int) $object->id);
  246. $sql .= $this->db->order($sortfield, $sortorder);
  247. //dol_syslog('List products', LOG_DEBUG);
  248. $resql = $this->db->query($sql);
  249. if ($resql) {
  250. $num = $this->db->num_rows($resql);
  251. $i = 0;
  252. $nblines = $num;
  253. $nexY = $tab_top + $this->tabTitleHeight;
  254. for ($i = 0; $i < $nblines; $i++) {
  255. $curY = $nexY;
  256. $objp = $this->db->fetch_object($resql);
  257. // Multilangs
  258. if (!empty($conf->global->MAIN_MULTILANGS)) { // si l'option est active
  259. $sql = "SELECT label";
  260. $sql .= " FROM ".MAIN_DB_PREFIX."product_lang";
  261. $sql .= " WHERE fk_product = ".((int) $objp->rowid);
  262. $sql .= " AND lang = '".$this->db->escape($langs->getDefaultLang())."'";
  263. $sql .= " LIMIT 1";
  264. $result = $this->db->query($sql);
  265. if ($result) {
  266. $objtp = $this->db->fetch_object($result);
  267. if ($objtp->label != '') {
  268. $objp->produit = $objtp->label;
  269. }
  270. }
  271. }
  272. $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage
  273. $pdf->SetTextColor(0, 0, 0);
  274. $pdf->setTopMargin($tab_top_newpage);
  275. $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext + $heightforinfotot); // The only function to edit the bottom margin of current page to set it.
  276. $pageposbefore = $pdf->getPage();
  277. // Description of product line
  278. $curX = $this->posxdesc - 1;
  279. $showpricebeforepagebreak = 1;
  280. $pdf->startTransaction();
  281. $pdf->writeHTMLCell($this->wref, 3, $curX, $curY, $outputlangs->convToOutputCharset($objp->ref), 0, 1, false, true, 'J', true);
  282. //pdf_writelinedesc($pdf, $object, $i, $outputlangs, $this->posxtva - $curX, 3, $curX, $curY, $hideref, $hidedesc);
  283. $pageposafter = $pdf->getPage();
  284. if ($pageposafter > $pageposbefore) { // There is a pagebreak
  285. $pdf->rollbackTransaction(true);
  286. $pageposafter = $pageposbefore;
  287. //print $pageposafter.'-'.$pageposbefore;exit;
  288. $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it.
  289. $pdf->writeHTMLCell($this->wref, 4, $curX, $curY, $outputlangs->convToOutputCharset($objp->ref), 0, 1, false, true, 'J', true);
  290. //pdf_writelinedesc($pdf, $object, $i, $outputlangs, $this->posxtva - $curX, 4, $curX, $curY, $hideref, $hidedesc);
  291. $pageposafter = $pdf->getPage();
  292. $posyafter = $pdf->GetY();
  293. if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) { // There is no space left for total+free text
  294. if ($i == ($nblines - 1)) { // No more lines, and no space left to show total, so we create a new page
  295. $pdf->AddPage('', '', true);
  296. if (!empty($tplidx)) {
  297. $pdf->useTemplate($tplidx);
  298. }
  299. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
  300. $this->_pagehead($pdf, $object, 0, $outputlangs);
  301. }
  302. $pdf->setPage($pageposafter + 1);
  303. }
  304. } else {
  305. // We found a page break
  306. // Allows data in the first page if description is long enough to break in multiples pages
  307. if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
  308. $showpricebeforepagebreak = 1;
  309. } else {
  310. $showpricebeforepagebreak = 0;
  311. }
  312. }
  313. } else // No pagebreak
  314. {
  315. $pdf->commitTransaction();
  316. }
  317. $posYAfterDescription = $pdf->GetY();
  318. $nexY = $pdf->GetY();
  319. $pageposafter = $pdf->getPage();
  320. $pdf->setPage($pageposbefore);
  321. $pdf->setTopMargin($this->marge_haute);
  322. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  323. // We suppose that a too long description is moved completely on next page
  324. if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
  325. $pdf->setPage($pageposafter);
  326. $curY = $tab_top_newpage;
  327. }
  328. $pdf->SetFont('', '', $default_font_size - 1); // On repositionne la police par defaut
  329. $productstatic->id = $objp->rowid;
  330. $productstatic->ref = $objp->ref;
  331. $productstatic->label = $objp->produit;
  332. $productstatic->type = $objp->type;
  333. $productstatic->entity = $objp->entity;
  334. $productstatic->status_batch = $objp->tobatch;
  335. // Ref.
  336. //$pdf->SetXY($this->posxdesc, $curY);
  337. //$pdf->MultiCell($this->wref, 3, $productstatic->ref, 0, 'L');
  338. // Label
  339. $pdf->SetXY($this->posxlabel + 0.8, $curY);
  340. $pdf->MultiCell($this->posxqty - $this->posxlabel - 0.8, 3, dol_trunc($objp->produit, 24), 0, 'L');
  341. // Quantity
  342. $valtoshow = price2num($objp->value, 'MS');
  343. $towrite = (empty($valtoshow) ? '0' : $valtoshow);
  344. $pdf->SetXY($this->posxqty, $curY);
  345. $pdf->MultiCell($this->posxup - $this->posxqty - 0.8, 3, $towrite, 0, 'R');
  346. // AWP
  347. $totalunit += $objp->value;
  348. $pdf->SetXY($this->posxup, $curY);
  349. $pdf->MultiCell($this->posxunit - $this->posxup - 0.8, 3, price(price2num($objp->ppmp, 'MU'), 0, $outputlangs), 0, 'R');
  350. // Total PMP
  351. $pdf->SetXY($this->posxunit, $curY);
  352. $pdf->MultiCell($this->posxdiscount - $this->posxunit - 0.8, 3, price(price2num($objp->ppmp * $objp->value, 'MT'), 0, $outputlangs), 0, 'R');
  353. $totalvalue += price2num($objp->ppmp * $objp->value, 'MT');
  354. // Price sell min
  355. if (empty($conf->global->PRODUIT_MULTIPRICES)) {
  356. $pricemin = $objp->price;
  357. $pdf->SetXY($this->posxdiscount, $curY);
  358. $pdf->MultiCell($this->postotalht - $this->posxdiscount, 3, price(price2num($pricemin, 'MU'), 0, $outputlangs), 0, 'R', 0);
  359. // Total sell min
  360. $pdf->SetXY($this->postotalht, $curY);
  361. $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->postotalht, 3, price(price2num($pricemin * $objp->value, 'MT'), 0, $outputlangs), 0, 'R', 0);
  362. }
  363. $totalvaluesell += price2num($pricemin * $objp->value, 'MT');
  364. // Add line
  365. if (!empty($conf->global->MAIN_PDF_DASH_BETWEEN_LINES) && $i < ($nblines - 1)) {
  366. $pdf->setPage($pageposafter);
  367. $pdf->SetLineStyle(array('dash'=>'1,1', 'color'=>array(80, 80, 80)));
  368. //$pdf->SetDrawColor(190,190,200);
  369. $pdf->line($this->marge_gauche, $nexY + 1, $this->page_largeur - $this->marge_droite, $nexY + 1);
  370. $pdf->SetLineStyle(array('dash'=>0));
  371. }
  372. $nexY += 2; // Add space between lines
  373. // Detect if some page were added automatically and output _tableau for past pages
  374. while ($pagenb < $pageposafter) {
  375. $pdf->setPage($pagenb);
  376. if ($pagenb == 1) {
  377. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code);
  378. } else {
  379. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code);
  380. }
  381. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  382. $pagenb++;
  383. $pdf->setPage($pagenb);
  384. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  385. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
  386. $this->_pagehead($pdf, $object, 0, $outputlangs);
  387. }
  388. if (!empty($tplidx)) {
  389. $pdf->useTemplate($tplidx);
  390. }
  391. }
  392. if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
  393. if ($pagenb == 1) {
  394. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code);
  395. } else {
  396. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code);
  397. }
  398. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  399. // New page
  400. $pdf->AddPage();
  401. if (!empty($tplidx)) {
  402. $pdf->useTemplate($tplidx);
  403. }
  404. $pagenb++;
  405. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
  406. $this->_pagehead($pdf, $object, 0, $outputlangs);
  407. }
  408. }
  409. }
  410. $this->db->free($resql);
  411. /**
  412. * Footer table
  413. */
  414. //$nexY = $pdf->GetY();
  415. $nexY += 2;
  416. $curY = $nexY;
  417. if ($nblines > 0) {
  418. $pdf->SetLineStyle(array('dash'=>'0', 'color'=>array(200, 200, 200)));
  419. $pdf->line($this->marge_gauche, $curY - 1, $this->page_largeur - $this->marge_droite, $curY - 1);
  420. $pdf->SetLineStyle(array('dash'=>0));
  421. $pdf->SetFont('', 'B', $default_font_size - 1);
  422. $pdf->SetTextColor(0, 0, 0);
  423. // Ref.
  424. $pdf->SetXY($this->posxdesc, $curY);
  425. $pdf->MultiCell($this->wref, 3, $langs->trans("Total"), 0, 'L');
  426. // Quantity
  427. $valtoshow = price2num($totalunit, 'MS');
  428. $towrite = empty($valtoshow) ? '0' : $valtoshow;
  429. $pdf->SetXY($this->posxqty, $curY);
  430. $pdf->MultiCell($this->posxup - $this->posxqty - 0.8, 3, $towrite, 0, 'R');
  431. // Total PMP
  432. $pdf->SetXY($this->posxunit, $curY);
  433. $pdf->MultiCell($this->posxdiscount - $this->posxunit - 0.8, 3, price(price2num($totalvalue, 'MT'), 0, $outputlangs), 0, 'R');
  434. // Price sell min
  435. if (empty($conf->global->PRODUIT_MULTIPRICES)) {
  436. // Total sell min
  437. $pdf->SetXY($this->postotalht, $curY);
  438. $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->postotalht, 3, price(price2num($totalvaluesell, 'MT'), 0, $outputlangs), 0, 'R', 0);
  439. }
  440. }
  441. } else {
  442. dol_print_error($this->db);
  443. }
  444. // Displays notes
  445. $notetoshow = empty($object->note_public) ? '' : $object->note_public;
  446. if ($notetoshow) {
  447. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object);
  448. complete_substitutions_array($substitutionarray, $outputlangs, $object);
  449. $notetoshow = make_substitutions($notetoshow, $substitutionarray, $outputlangs);
  450. $notetoshow = convertBackOfficeMediasLinksToPublicLinks($notetoshow);
  451. $tab_top = 88;
  452. $pdf->SetFont('', '', $default_font_size - 1);
  453. $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1);
  454. $nexY = $pdf->GetY();
  455. $height_note = $nexY - $tab_top;
  456. // Rect takes a length in 3rd parameter
  457. $pdf->SetDrawColor(192, 192, 192);
  458. $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
  459. $tab_height = $tab_height - $height_note;
  460. $tab_top = $nexY + 6;
  461. } else {
  462. $height_note = 0;
  463. }
  464. /*$iniY = $tab_top + 7;
  465. $curY = $tab_top + 7;
  466. $nexY = $tab_top + 7;
  467. $tab_top = $tab_top_newpage + 25 + $top_shift;*/
  468. // Show square
  469. if ($pagenb == 1) {
  470. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0, $object->multicurrency_code);
  471. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  472. } else {
  473. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0, $object->multicurrency_code);
  474. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  475. }
  476. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  477. // Affiche zone infos
  478. //$posy=$this->_tableau_info($pdf, $object, $bottomlasttab, $outputlangs);
  479. // Affiche zone totaux
  480. //$posy=$this->_tableau_tot($pdf, $object, $deja_regle, $bottomlasttab, $outputlangs);
  481. // Pied de page
  482. $this->_pagefoot($pdf, $object, $outputlangs);
  483. if (method_exists($pdf, 'AliasNbPages')) {
  484. $pdf->AliasNbPages();
  485. }
  486. $pdf->Close();
  487. $pdf->Output($file, 'F');
  488. // Add pdfgeneration hook
  489. $hookmanager->initHooks(array('pdfgeneration'));
  490. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  491. global $action;
  492. $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  493. if ($reshook < 0) {
  494. $this->error = $hookmanager->error;
  495. $this->errors = $hookmanager->errors;
  496. }
  497. if (!empty($conf->global->MAIN_UMASK)) {
  498. @chmod($file, octdec($conf->global->MAIN_UMASK));
  499. }
  500. $this->result = array('fullpath'=>$file);
  501. return 1; // No error
  502. } else {
  503. $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
  504. return 0;
  505. }
  506. } else {
  507. $this->error = $langs->trans("ErrorConstantNotDefined", "PRODUCT_OUTPUTDIR");
  508. return 0;
  509. }
  510. }
  511. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  512. /**
  513. * Show table for lines
  514. *
  515. * @param TCPDF $pdf Object PDF
  516. * @param string $tab_top Top position of table
  517. * @param string $tab_height Height of table (rectangle)
  518. * @param int $nexY Y (not used)
  519. * @param Translate $outputlangs Langs object
  520. * @param int $hidetop 1=Hide top bar of array and title, 0=Hide nothing, -1=Hide only title
  521. * @param int $hidebottom Hide bottom bar of array
  522. * @param string $currency Currency code
  523. * @return void
  524. */
  525. protected function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop = 0, $hidebottom = 0, $currency = '')
  526. {
  527. global $conf;
  528. // Force to disable hidetop and hidebottom
  529. $hidebottom = 0;
  530. if ($hidetop) {
  531. $hidetop = -1;
  532. }
  533. $currency = !empty($currency) ? $currency : $conf->currency;
  534. $default_font_size = pdf_getPDFFontSize($outputlangs);
  535. // Amount in (at tab_top - 1)
  536. $pdf->SetTextColor(0, 0, 0);
  537. $pdf->SetFont('', '', $default_font_size - 2);
  538. if (empty($hidetop)) {
  539. $titre = $outputlangs->transnoentities("AmountInCurrency", $outputlangs->transnoentitiesnoconv("Currency".$currency));
  540. $pdf->SetXY($this->page_largeur - $this->marge_droite - ($pdf->GetStringWidth($titre) + 3), $tab_top - 4);
  541. $pdf->MultiCell(($pdf->GetStringWidth($titre) + 3), 2, $titre);
  542. //$conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR='230,230,230';
  543. if (!empty($conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR)) {
  544. $pdf->Rect($this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_droite - $this->marge_gauche, 5, 'F', null, explode(',', $conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR));
  545. }
  546. }
  547. $pdf->SetDrawColor(128, 128, 128);
  548. $pdf->SetFont('', 'B', $default_font_size - 3);
  549. // Output Rect
  550. $this->printRect($pdf, $this->marge_gauche, $tab_top, $this->page_largeur-$this->marge_gauche-$this->marge_droite, $tab_height, $hidetop, $hidebottom); // Rect takes a length in 3rd parameter and 4th parameter
  551. $pdf->SetLineStyle(array('dash'=>'0', 'color'=>array(200, 200, 200)));
  552. $pdf->SetDrawColor(200, 200, 200);
  553. $pdf->line($this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_droite, $tab_top);
  554. $pdf->SetLineStyle(array('dash'=>0));
  555. $pdf->SetDrawColor(128, 128, 128);
  556. $pdf->SetTextColor(0, 0, 0);
  557. if (empty($hidetop)) {
  558. $pdf->line($this->marge_gauche, $tab_top+11, $this->page_largeur-$this->marge_droite, $tab_top+11); // line takes a position y in 2nd parameter and 4th parameter
  559. $pdf->SetXY($this->posxdesc - 1, $tab_top + 1);
  560. $pdf->MultiCell($this->wref, 3, $outputlangs->transnoentities("Ref"), '', 'L');
  561. }
  562. $pdf->line($this->posxlabel-1, $tab_top, $this->posxlabel-1, $tab_top + $tab_height);
  563. if (empty($hidetop)) {
  564. $pdf->SetXY($this->posxlabel - 1, $tab_top + 1);
  565. $pdf->MultiCell($this->posxqty - $this->posxlabel - 1, 2, $outputlangs->transnoentities("Label"), '', 'C');
  566. }
  567. $pdf->line($this->posxqty-1, $tab_top, $this->posxqty-1, $tab_top + $tab_height);
  568. if (empty($hidetop)) {
  569. $pdf->SetXY($this->posxqty - 1, $tab_top + 1);
  570. $pdf->MultiCell($this->posxup - $this->posxqty - 1, 2, $outputlangs->transnoentities("Units"), '', 'C');
  571. }
  572. $pdf->line($this->posxup-1, $tab_top, $this->posxup-1, $tab_top + $tab_height);
  573. if (empty($hidetop)) {
  574. $pdf->SetXY($this->posxup - 1, $tab_top + 1);
  575. $pdf->MultiCell($this->posxunit - $this->posxup - 1, 2, $outputlangs->transnoentities("AverageUnitPricePMPShort"), '', 'C');
  576. }
  577. $pdf->line($this->posxunit - 1, $tab_top, $this->posxunit - 1, $tab_top + $tab_height);
  578. if (empty($hidetop)) {
  579. $pdf->SetXY($this->posxunit - 1, $tab_top + 1);
  580. $pdf->MultiCell($this->posxdiscount - $this->posxunit - 1, 2, $outputlangs->transnoentities("EstimatedStockValueShort"), '', 'C');
  581. }
  582. $pdf->line($this->posxdiscount-1, $tab_top, $this->posxdiscount-1, $tab_top + $tab_height);
  583. if (empty($hidetop)) {
  584. $pdf->SetXY($this->posxdiscount - 1, $tab_top + 1);
  585. $pdf->MultiCell($this->postotalht - $this->posxdiscount + 1, 2, $outputlangs->transnoentities("SellPriceMin"), '', 'C');
  586. }
  587. $pdf->line($this->postotalht, $tab_top, $this->postotalht, $tab_top + $tab_height);
  588. if (empty($hidetop)) {
  589. $pdf->SetXY($this->postotalht - 1, $tab_top + 1);
  590. $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->postotalht, 2, $outputlangs->transnoentities("EstimatedStockValueSellShort"), '', 'C');
  591. }
  592. if (empty($hidetop)) {
  593. $pdf->SetDrawColor(200, 200, 200);
  594. $pdf->SetLineStyle(array('dash' => '0', 'color' => array(200, 200, 200)));
  595. $pdf->line($this->marge_gauche, $tab_top + $this->tabTitleHeight, $this->page_largeur - $this->marge_droite, $tab_top + $this->tabTitleHeight);
  596. $pdf->SetLineStyle(array('dash' => 0));
  597. }
  598. }
  599. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  600. /**
  601. * Show top header of page.
  602. *
  603. * @param TCPDF $pdf Object PDF
  604. * @param Object $object Object to show
  605. * @param int $showaddress 0=no, 1=yes
  606. * @param Translate $outputlangs Object lang for output
  607. * @param string $titlekey Translation key to show as title of document
  608. * @return int Return topshift value
  609. */
  610. protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs, $titlekey = "")
  611. {
  612. global $conf, $langs, $db, $hookmanager;
  613. // Load traductions files required by page
  614. $outputlangs->loadLangs(array("main", "propal", "companies", "bills", "orders", "stocks"));
  615. $default_font_size = pdf_getPDFFontSize($outputlangs);
  616. if ($object->type == 1) {
  617. $titlekey = 'ServiceSheet';
  618. } else {
  619. $titlekey = 'StockSheet';
  620. }
  621. pdf_pagehead($pdf, $outputlangs, $this->page_hauteur);
  622. // Show Draft Watermark
  623. if ($object->statut == 0 && (!empty($conf->global->STOCK_DRAFT_WATERMARK))) {
  624. pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur, 'mm', $conf->global->STOCK_DRAFT_WATERMARK);
  625. }
  626. $pdf->SetTextColor(0, 0, 60);
  627. $pdf->SetFont('', 'B', $default_font_size + 3);
  628. $posy = $this->marge_haute;
  629. $posx = $this->page_largeur - $this->marge_droite - 100;
  630. $pdf->SetXY($this->marge_gauche, $posy);
  631. // Logo
  632. $height = 0;
  633. $logo = $conf->mycompany->dir_output.'/logos/'.$this->emetteur->logo;
  634. if ($this->emetteur->logo) {
  635. if (is_readable($logo)) {
  636. $height = pdf_getHeightForLogo($logo);
  637. $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
  638. } else {
  639. $pdf->SetTextColor(200, 0, 0);
  640. $pdf->SetFont('', 'B', $default_font_size - 2);
  641. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("ErrorLogoFileNotFound", $logo), 0, 'L');
  642. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("ErrorGoToGlobalSetup"), 0, 'L');
  643. }
  644. } else {
  645. $text = $this->emetteur->name;
  646. $pdf->MultiCell(100, 4, $outputlangs->convToOutputCharset($text), 0, 'L');
  647. }
  648. $yafterleft = $pdf->GetY() + $height;
  649. $pdf->SetFont('', 'B', $default_font_size + 3);
  650. $pdf->SetXY($posx, $posy);
  651. $pdf->SetTextColor(0, 0, 60);
  652. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("Warehouse").' '.$outputlangs->convToOutputCharset($object->label), '', 'R');
  653. $posy += 6;
  654. $pdf->SetFont('', '', $default_font_size - 1);
  655. $pdf->SetXY($posx, $posy);
  656. $pdf->SetTextColor(0, 0, 60);
  657. if (!empty($object->lieu)) {
  658. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("Label").' : '.$object->lieu, '', 'R');
  659. }
  660. $posy += 4;
  661. $pdf->SetXY($posx, $posy);
  662. $pdf->SetTextColor(0, 0, 60);
  663. // Parent entrepot
  664. $e = new Entrepot($this->db);
  665. $hasparent = (!empty($object->fk_parent) && $e->fetch($object->fk_parent) > 0);
  666. if ($hasparent) {
  667. $pdf->MultiCell(100, 3, $outputlangs->transnoentities("ParentWarehouse").' :', '', 'R');
  668. $posy += 4;
  669. $pdf->SetXY($posx - 50, $posy);
  670. $pdf->MultiCell(150, 3, $e->label, '', 'R');
  671. }
  672. $yafterright = $pdf->GetY();
  673. // Description
  674. $nbpage = $pdf->getPage();
  675. if ($nbpage == 1) {
  676. $nexY = max($yafterleft, $yafterright);
  677. $nexY += 5;
  678. $pdf->SetXY($posx, $posy);
  679. $pdf->writeHTMLCell(190, 2, $this->marge_gauche, $nexY, '<b>'.$outputlangs->transnoentities("Description").' : </b>'.nl2br($object->description), 0, 1);
  680. $nexY = $pdf->GetY();
  681. $calcproductsunique = $object->nb_different_products();
  682. $calcproducts = $object->nb_products();
  683. // Total nb of different products
  684. $pdf->writeHTMLCell(190, 2, $this->marge_gauche, $nexY, '<b>'.$outputlangs->transnoentities("NumberOfDifferentProducts").' : </b>'.(empty($calcproductsunique['nb']) ? '0' : $calcproductsunique['nb']), 0, 1);
  685. $nexY = $pdf->GetY();
  686. // Nb of products
  687. $valtoshow = price2num($calcproducts['nb'], 'MS');
  688. $pdf->writeHTMLCell(190, 2, $this->marge_gauche, $nexY, '<b>'.$outputlangs->transnoentities("NumberOfProducts").' : </b>'.(empty($valtoshow) ? '0' : $valtoshow), 0, 1);
  689. $nexY = $pdf->GetY();
  690. // Value
  691. $pdf->writeHTMLCell(190, 2, $this->marge_gauche, $nexY, '<b>'.$outputlangs->transnoentities("EstimatedStockValueShort").' : </b>'.price((empty($calcproducts['value']) ? '0' : price2num($calcproducts['value'], 'MT')), 0, $langs, 0, -1, -1, $conf->currency), 0, 1);
  692. $nexY = $pdf->GetY();
  693. // Value
  694. $pdf->writeHTMLCell(190, 2, $this->marge_gauche, $nexY, '<b>'.$outputlangs->transnoentities("Date").' : </b>'.dol_print_date(dol_now(), 'dayhour'), 0, 1);
  695. $nexY = $pdf->GetY();
  696. // Last movement
  697. $sql = "SELECT max(m.datem) as datem";
  698. $sql .= " FROM ".MAIN_DB_PREFIX."stock_mouvement as m";
  699. $sql .= " WHERE m.fk_entrepot = ".((int) $object->id);
  700. $resqlbis = $this->db->query($sql);
  701. if ($resqlbis) {
  702. $obj = $this->db->fetch_object($resqlbis);
  703. $lastmovementdate = $this->db->jdate($obj->datem);
  704. } else {
  705. dol_print_error($this->db);
  706. }
  707. if ($lastmovementdate) {
  708. $toWrite = dol_print_date($lastmovementdate, 'dayhour').' ';
  709. } else {
  710. $toWrite = $outputlangs->transnoentities("None");
  711. }
  712. $pdf->writeHTMLCell(190, 2, $this->marge_gauche, $nexY, '<b>'.$outputlangs->transnoentities("LastMovement").' : </b>'.$toWrite, 0, 1);
  713. }
  714. $nexY = $pdf->GetY();
  715. $posy += 2;
  716. $top_shift = 0;
  717. // Show list of linked objects
  718. $current_y = $pdf->getY();
  719. $posy = pdf_writeLinkedObjects($pdf, $object, $outputlangs, $posx, $posy, 100, 3, 'R', $default_font_size);
  720. if ($current_y < $pdf->getY()) {
  721. $top_shift = $pdf->getY() - $current_y;
  722. }
  723. $pdf->SetTextColor(0, 0, 0);
  724. return $top_shift;
  725. }
  726. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  727. /**
  728. * Show footer of page. Need this->emetteur object
  729. *
  730. * @param TCPDF $pdf PDF
  731. * @param Object $object Object to show
  732. * @param Translate $outputlangs Object lang for output
  733. * @param int $hidefreetext 1=Hide free text
  734. * @return int Return height of bottom margin including footer text
  735. */
  736. protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
  737. {
  738. global $conf;
  739. $showdetails = empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS) ? 0 : $conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS;
  740. return pdf_pagefoot($pdf, $outputlangs, 'PRODUCT_FREE_TEXT', $this->emetteur, $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext);
  741. }
  742. }