pdf_espadon.modules.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
  6. * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. * or see https://www.gnu.org/
  21. */
  22. /**
  23. * \file htdocs/core/modules/expedition/doc/pdf_espadon.modules.php
  24. * \ingroup expedition
  25. * \brief Class file allowing Espadons shipping template generation
  26. */
  27. require_once DOL_DOCUMENT_ROOT.'/core/modules/expedition/modules_expedition.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  31. /**
  32. * Class to build sending documents with model espadon
  33. */
  34. class pdf_espadon extends ModelePdfExpedition
  35. {
  36. /**
  37. * @var DoliDb Database handler
  38. */
  39. public $db;
  40. /**
  41. * @var string model name
  42. */
  43. public $name;
  44. /**
  45. * @var string model description (short text)
  46. */
  47. public $description;
  48. /**
  49. * @var int Save the name of generated file as the main doc when generating a doc with this template
  50. */
  51. public $update_main_doc_field;
  52. /**
  53. * @var string document type
  54. */
  55. public $type;
  56. /**
  57. * @var array Minimum version of PHP required by module.
  58. * e.g.: PHP ≥ 5.6 = array(5, 6)
  59. */
  60. public $phpmin = array(5, 6);
  61. /**
  62. * Dolibarr version of the loaded document
  63. * @var string
  64. */
  65. public $version = 'dolibarr';
  66. /**
  67. * @var int page_largeur
  68. */
  69. public $page_largeur;
  70. /**
  71. * @var int page_hauteur
  72. */
  73. public $page_hauteur;
  74. /**
  75. * @var array format
  76. */
  77. public $format;
  78. /**
  79. * @var int marge_gauche
  80. */
  81. public $marge_gauche;
  82. /**
  83. * @var int marge_droite
  84. */
  85. public $marge_droite;
  86. /**
  87. * @var int marge_haute
  88. */
  89. public $marge_haute;
  90. /**
  91. * @var int marge_basse
  92. */
  93. public $marge_basse;
  94. /**
  95. * Issuer
  96. * @var Societe object that emits
  97. */
  98. public $emetteur;
  99. /**
  100. * Constructor
  101. *
  102. * @param DoliDB $db Database handler
  103. */
  104. public function __construct($db = 0)
  105. {
  106. global $conf, $langs, $mysoc;
  107. $this->db = $db;
  108. $this->name = "espadon";
  109. $this->description = $langs->trans("DocumentModelStandardPDF");
  110. $this->update_main_doc_field = 1; // Save the name of generated file as the main doc when generating a doc with this template
  111. $this->type = 'pdf';
  112. $formatarray = pdf_getFormat();
  113. $this->page_largeur = $formatarray['width'];
  114. $this->page_hauteur = $formatarray['height'];
  115. $this->format = array($this->page_largeur, $this->page_hauteur);
  116. $this->marge_gauche = isset($conf->global->MAIN_PDF_MARGIN_LEFT) ? $conf->global->MAIN_PDF_MARGIN_LEFT : 10;
  117. $this->marge_droite = isset($conf->global->MAIN_PDF_MARGIN_RIGHT) ? $conf->global->MAIN_PDF_MARGIN_RIGHT : 10;
  118. $this->marge_haute = isset($conf->global->MAIN_PDF_MARGIN_TOP) ? $conf->global->MAIN_PDF_MARGIN_TOP : 10;
  119. $this->marge_basse = isset($conf->global->MAIN_PDF_MARGIN_BOTTOM) ? $conf->global->MAIN_PDF_MARGIN_BOTTOM : 10;
  120. $this->option_logo = 1; // Display logo
  121. $this->option_draft_watermark = 1; // Support add of a watermark on drafts
  122. $this->watermark = '';
  123. // Get source company
  124. $this->emetteur = $mysoc;
  125. if (!$this->emetteur->country_code) {
  126. $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
  127. }
  128. $this->tabTitleHeight = 5; // default height
  129. }
  130. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  131. /**
  132. * Function to build pdf onto disk
  133. *
  134. * @param Expedition $object Object expedition to generate (or id if old method)
  135. * @param Translate $outputlangs Lang output object
  136. * @param string $srctemplatepath Full path of source filename for generator using a template file
  137. * @param int $hidedetails Do not show line details
  138. * @param int $hidedesc Do not show desc
  139. * @param int $hideref Do not show ref
  140. * @return int 1=OK, 0=KO
  141. */
  142. public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  143. {
  144. // phpcs:enable
  145. global $user, $conf, $langs, $hookmanager;
  146. $object->fetch_thirdparty();
  147. if (!is_object($outputlangs)) {
  148. $outputlangs = $langs;
  149. }
  150. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  151. if (!empty($conf->global->MAIN_USE_FPDF)) {
  152. $outputlangs->charset_output = 'ISO-8859-1';
  153. }
  154. // Load traductions files required by page
  155. $outputlangs->loadLangs(array("main", "bills", "orders", "products", "dict", "companies", "propal", "deliveries", "sendings", "productbatch"));
  156. // Show Draft Watermark
  157. if ($object->statut == $object::STATUS_DRAFT && (!empty($conf->global->SHIPPING_DRAFT_WATERMARK))) {
  158. $this->watermark = $conf->global->SHIPPING_DRAFT_WATERMARK;
  159. }
  160. global $outputlangsbis;
  161. $outputlangsbis = null;
  162. if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
  163. $outputlangsbis = new Translate('', $conf);
  164. $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
  165. $outputlangsbis->loadLangs(array("main", "bills", "orders", "products", "dict", "companies", "propal", "deliveries", "sendings", "productbatch"));
  166. }
  167. $nblines = count($object->lines);
  168. // Loop on each lines to detect if there is at least one image to show
  169. $realpatharray = array();
  170. $this->atleastonephoto = false;
  171. if (!empty($conf->global->MAIN_GENERATE_SHIPMENT_WITH_PICTURE)) {
  172. $objphoto = new Product($this->db);
  173. for ($i = 0; $i < $nblines; $i++) {
  174. if (empty($object->lines[$i]->fk_product)) {
  175. continue;
  176. }
  177. $objphoto->fetch($object->lines[$i]->fk_product);
  178. if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) {
  179. $pdir = get_exdir($object->lines[$i]->fk_product, 2, 0, 0, $objphoto, 'product').$object->lines[$i]->fk_product."/photos/";
  180. $dir = $conf->product->dir_output.'/'.$pdir;
  181. } else {
  182. $pdir = get_exdir(0, 0, 0, 0, $objphoto, 'product');
  183. $dir = $conf->product->dir_output.'/'.$pdir;
  184. }
  185. $realpath = '';
  186. foreach ($objphoto->liste_photos($dir, 1) as $key => $obj) {
  187. if (empty($conf->global->CAT_HIGH_QUALITY_IMAGES)) { // If CAT_HIGH_QUALITY_IMAGES not defined, we use thumb if defined and then original photo
  188. if ($obj['photo_vignette']) {
  189. $filename = $obj['photo_vignette'];
  190. } else {
  191. $filename = $obj['photo'];
  192. }
  193. } else {
  194. $filename = $obj['photo'];
  195. }
  196. $realpath = $dir.$filename;
  197. $this->atleastonephoto = true;
  198. break;
  199. }
  200. if ($realpath) {
  201. $realpatharray[$i] = $realpath;
  202. }
  203. }
  204. }
  205. if (count($realpatharray) == 0) {
  206. $this->posxpicture = $this->posxweightvol;
  207. }
  208. if ($conf->expedition->dir_output) {
  209. // Definition of $dir and $file
  210. if ($object->specimen) {
  211. $dir = $conf->expedition->dir_output."/sending";
  212. $file = $dir."/SPECIMEN.pdf";
  213. } else {
  214. $expref = dol_sanitizeFileName($object->ref);
  215. $dir = $conf->expedition->dir_output."/sending/".$expref;
  216. $file = $dir."/".$expref.".pdf";
  217. }
  218. if (!file_exists($dir)) {
  219. if (dol_mkdir($dir) < 0) {
  220. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  221. return 0;
  222. }
  223. }
  224. if (file_exists($dir)) {
  225. // Add pdfgeneration hook
  226. if (!is_object($hookmanager)) {
  227. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  228. $hookmanager = new HookManager($this->db);
  229. }
  230. $hookmanager->initHooks(array('pdfgeneration'));
  231. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  232. global $action;
  233. $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  234. // Set nblines with the new facture lines content after hook
  235. $nblines = count($object->lines);
  236. $pdf = pdf_getInstance($this->format);
  237. $default_font_size = pdf_getPDFFontSize($outputlangs);
  238. $heightforinfotot = 8; // Height reserved to output the info and total part
  239. $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
  240. $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
  241. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
  242. $heightforfooter += 6;
  243. }
  244. $pdf->SetAutoPageBreak(1, 0);
  245. if (class_exists('TCPDF')) {
  246. $pdf->setPrintHeader(false);
  247. $pdf->setPrintFooter(false);
  248. }
  249. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  250. // Set path to the background PDF File
  251. if (!empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) {
  252. $pagecount = $pdf->setSourceFile($conf->mycompany->dir_output.'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND);
  253. $tplidx = $pdf->importPage(1);
  254. }
  255. $pdf->Open();
  256. $pagenb = 0;
  257. $pdf->SetDrawColor(128, 128, 128);
  258. if (method_exists($pdf, 'AliasNbPages')) {
  259. $pdf->AliasNbPages();
  260. }
  261. $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
  262. $pdf->SetSubject($outputlangs->transnoentities("Shipment"));
  263. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  264. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  265. $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("Shipment"));
  266. if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) {
  267. $pdf->SetCompression(false);
  268. }
  269. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  270. // New page
  271. $pdf->AddPage();
  272. if (!empty($tplidx)) {
  273. $pdf->useTemplate($tplidx);
  274. }
  275. $pagenb++;
  276. $top_shift = $this->_pagehead($pdf, $object, 1, $outputlangs);
  277. $pdf->SetFont('', '', $default_font_size - 1);
  278. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  279. $pdf->SetTextColor(0, 0, 0);
  280. $tab_top = 90;
  281. $tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD) ? 42 + $top_shift: 10);
  282. $tab_height = $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext;
  283. $this->posxdesc = $this->marge_gauche + 1;
  284. // Incoterm
  285. $height_incoterms = 0;
  286. if (!empty($conf->incoterm->enabled)) {
  287. $desc_incoterms = $object->getIncotermsForPDF();
  288. if ($desc_incoterms) {
  289. $tab_top -= 2;
  290. $pdf->SetFont('', '', $default_font_size - 1);
  291. $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1, dol_htmlentitiesbr($desc_incoterms), 0, 1);
  292. $nexY = $pdf->GetY();
  293. $height_incoterms = $nexY - $tab_top;
  294. // Rect takes a length in 3rd parameter
  295. $pdf->SetDrawColor(192, 192, 192);
  296. $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_incoterms + 1);
  297. $tab_top = $nexY + 6;
  298. $height_incoterms += 4;
  299. }
  300. }
  301. // display note
  302. $notetoshow = empty($object->note_public) ? '' : $object->note_public;
  303. // Extrafields in note
  304. $extranote = $this->getExtrafieldsInHtml($object, $outputlangs);
  305. if (!empty($extranote)) {
  306. $notetoshow = dol_concatdesc($notetoshow, $extranote);
  307. }
  308. if (!empty($notetoshow) || !empty($object->tracking_number)) {
  309. $tab_top -= 2;
  310. // Tracking number
  311. if (!empty($object->tracking_number)) {
  312. $pdf->SetFont('', 'B', $default_font_size - 2);
  313. $pdf->writeHTMLCell(60, 4, $this->posxdesc - 1, $tab_top - 1, $outputlangs->transnoentities("TrackingNumber") . " : " . $object->tracking_number, 0, 1, false, true, 'L');
  314. $tab_top_alt = $pdf->GetY();
  315. $object->getUrlTrackingStatus($object->tracking_number);
  316. if (!empty($object->tracking_url)) {
  317. if ($object->shipping_method_id > 0) {
  318. // Get code using getLabelFromKey
  319. $code = $outputlangs->getLabelFromKey($this->db, $object->shipping_method_id, 'c_shipment_mode', 'rowid', 'code');
  320. $label = '';
  321. if ($object->tracking_url != $object->tracking_number) {
  322. $label .= $outputlangs->trans("LinkToTrackYourPackage")."<br>";
  323. }
  324. $label .= $outputlangs->trans("SendingMethod").": ".$outputlangs->trans("SendingMethod".strtoupper($code));
  325. //var_dump($object->tracking_url != $object->tracking_number);exit;
  326. if ($object->tracking_url != $object->tracking_number) {
  327. $label .= " : ";
  328. $label .= $object->tracking_url;
  329. }
  330. $pdf->SetFont('', 'B', $default_font_size - 2);
  331. $pdf->writeHTMLCell(60, 4, $this->posxdesc - 1, $tab_top_alt, $label, 0, 1, false, true, 'L');
  332. $tab_top = $pdf->GetY();
  333. }
  334. }
  335. }
  336. // Notes
  337. $pagenb = $pdf->getPage();
  338. if (!empty($notetoshow)) {
  339. $tab_top -= 2;
  340. $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
  341. $pageposbeforenote = $pagenb;
  342. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object);
  343. complete_substitutions_array($substitutionarray, $outputlangs, $object);
  344. $notetoshow = make_substitutions($notetoshow, $substitutionarray, $outputlangs);
  345. $notetoshow = convertBackOfficeMediasLinksToPublicLinks($notetoshow);
  346. $pdf->startTransaction();
  347. $pdf->SetFont('', '', $default_font_size - 1);
  348. $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1);
  349. // Description
  350. $pageposafternote = $pdf->getPage();
  351. $posyafter = $pdf->GetY();
  352. if ($pageposafternote > $pageposbeforenote) {
  353. $pdf->rollbackTransaction(true);
  354. // prepare pages to receive notes
  355. while ($pagenb < $pageposafternote) {
  356. $pdf->AddPage();
  357. $pagenb++;
  358. if (!empty($tplidx)) $pdf->useTemplate($tplidx);
  359. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs);
  360. // $this->_pagefoot($pdf,$object,$outputlangs,1);
  361. $pdf->setTopMargin($tab_top_newpage);
  362. // The only function to edit the bottom margin of current page to set it.
  363. $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext);
  364. }
  365. // back to start
  366. $pdf->setPage($pageposbeforenote);
  367. $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext);
  368. $pdf->SetFont('', '', $default_font_size - 1);
  369. $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1);
  370. $pageposafternote = $pdf->getPage();
  371. $posyafter = $pdf->GetY();
  372. if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) { // There is no space left for total+free text
  373. $pdf->AddPage('', '', true);
  374. $pagenb++;
  375. $pageposafternote++;
  376. $pdf->setPage($pageposafternote);
  377. $pdf->setTopMargin($tab_top_newpage);
  378. // The only function to edit the bottom margin of current page to set it.
  379. $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext);
  380. //$posyafter = $tab_top_newpage;
  381. }
  382. // apply note frame to previous pages
  383. $i = $pageposbeforenote;
  384. while ($i < $pageposafternote) {
  385. $pdf->setPage($i);
  386. $pdf->SetDrawColor(128, 128, 128);
  387. // Draw note frame
  388. if ($i > $pageposbeforenote) {
  389. $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
  390. $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
  391. } else {
  392. $height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
  393. $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
  394. }
  395. // Add footer
  396. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  397. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  398. $i++;
  399. }
  400. // apply note frame to last page
  401. $pdf->setPage($pageposafternote);
  402. if (!empty($tplidx)) $pdf->useTemplate($tplidx);
  403. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs);
  404. $height_note = $posyafter - $tab_top_newpage;
  405. $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
  406. } else // No pagebreak
  407. {
  408. $pdf->commitTransaction();
  409. $posyafter = $pdf->GetY();
  410. $height_note = $posyafter - $tab_top;
  411. $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
  412. if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
  413. // not enough space, need to add page
  414. $pdf->AddPage('', '', true);
  415. $pagenb++;
  416. $pageposafternote++;
  417. $pdf->setPage($pageposafternote);
  418. if (!empty($tplidx)) $pdf->useTemplate($tplidx);
  419. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs);
  420. $posyafter = $tab_top_newpage;
  421. }
  422. }
  423. $tab_height = $tab_height - $height_note;
  424. $tab_top = $posyafter + 6;
  425. } else {
  426. $height_note = 0;
  427. }
  428. }
  429. // Use new auto column system
  430. $this->prepareArrayColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref);
  431. // Table simulation to know the height of the title line
  432. $pdf->startTransaction();
  433. $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs);
  434. $pdf->rollbackTransaction(true);
  435. $nexY = $tab_top + $this->tabTitleHeight;
  436. // Loop on each lines
  437. $pageposbeforeprintlines = $pdf->getPage();
  438. $pagenb = $pageposbeforeprintlines;
  439. for ($i = 0; $i < $nblines; $i++) {
  440. $curY = $nexY;
  441. $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage
  442. $pdf->SetTextColor(0, 0, 0);
  443. // Define size of image if we need it
  444. $imglinesize = array();
  445. if (!empty($realpatharray[$i])) {
  446. $imglinesize = pdf_getSizeForImage($realpatharray[$i]);
  447. }
  448. $pdf->setTopMargin($tab_top_newpage);
  449. $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext + $heightforinfotot); // The only function to edit the bottom margin of current page to set it.
  450. $pageposbefore = $pdf->getPage();
  451. $showpricebeforepagebreak = 1;
  452. $posYAfterImage = 0;
  453. $posYAfterDescription = 0;
  454. if ($this->getColumnStatus('photo')) {
  455. // We start with Photo of product line
  456. if (isset($imglinesize['width']) && isset($imglinesize['height']) && ($curY + $imglinesize['height']) > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforsignature + $heightforinfotot))) { // If photo too high, we moved completely on new page
  457. $pdf->AddPage('', '', true);
  458. if (!empty($tplidx)) {
  459. $pdf->useTemplate($tplidx);
  460. }
  461. //if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs);
  462. $pdf->setPage($pageposbefore + 1);
  463. $curY = $tab_top_newpage;
  464. // Allows data in the first page if description is long enough to break in multiples pages
  465. if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
  466. $showpricebeforepagebreak = 1;
  467. } else {
  468. $showpricebeforepagebreak = 0;
  469. }
  470. }
  471. if (!empty($this->cols['photo']) && isset($imglinesize['width']) && isset($imglinesize['height'])) {
  472. $pdf->Image($realpatharray[$i], $this->getColumnContentXStart('photo'), $curY + 1, $imglinesize['width'], $imglinesize['height'], '', '', '', 2, 300); // Use 300 dpi
  473. // $pdf->Image does not increase value return by getY, so we save it manually
  474. $posYAfterImage = $curY + $imglinesize['height'];
  475. }
  476. }
  477. // Description of product line
  478. if ($this->getColumnStatus('desc')) {
  479. $pdf->startTransaction();
  480. $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc);
  481. $pageposafter = $pdf->getPage();
  482. if ($pageposafter > $pageposbefore) { // There is a pagebreak
  483. $pdf->rollbackTransaction(true);
  484. $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc);
  485. $pageposafter = $pdf->getPage();
  486. $posyafter = $pdf->GetY();
  487. //var_dump($posyafter); var_dump(($this->page_hauteur - ($heightforfooter+$heightforfreetext+$heightforinfotot))); exit;
  488. if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforsignature + $heightforinfotot))) { // There is no space left for total+free text
  489. if ($i == ($nblines - 1)) { // No more lines, and no space left to show total, so we create a new page
  490. $pdf->AddPage('', '', true);
  491. if (!empty($tplidx)) {
  492. $pdf->useTemplate($tplidx);
  493. }
  494. //if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs);
  495. $pdf->setPage($pageposafter + 1);
  496. }
  497. } else {
  498. // We found a page break
  499. // Allows data in the first page if description is long enough to break in multiples pages
  500. if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
  501. $showpricebeforepagebreak = 1;
  502. } else {
  503. $showpricebeforepagebreak = 0;
  504. }
  505. }
  506. } else // No pagebreak
  507. {
  508. $pdf->commitTransaction();
  509. }
  510. $posYAfterDescription = $pdf->GetY();
  511. }
  512. $nexY = max($pdf->GetY(), $posYAfterImage);
  513. $pageposafter = $pdf->getPage();
  514. $pdf->setPage($pageposbefore);
  515. $pdf->setTopMargin($this->marge_haute);
  516. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  517. // We suppose that a too long description or photo were moved completely on next page
  518. if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
  519. $pdf->setPage($pageposafter);
  520. $curY = $tab_top_newpage;
  521. }
  522. // We suppose that a too long description is moved completely on next page
  523. if ($pageposafter > $pageposbefore) {
  524. $pdf->setPage($pageposafter);
  525. $curY = $tab_top_newpage;
  526. }
  527. $pdf->SetFont('', '', $default_font_size - 1); // We reposition the default font
  528. // weight
  529. $weighttxt = '';
  530. if ($object->lines[$i]->fk_product_type == 0 && $object->lines[$i]->weight) {
  531. $weighttxt = round($object->lines[$i]->weight * $object->lines[$i]->qty_shipped, 5).' '.measuringUnitString(0, "weight", $object->lines[$i]->weight_units, 1);
  532. }
  533. $voltxt = '';
  534. if ($object->lines[$i]->fk_product_type == 0 && $object->lines[$i]->volume) {
  535. $voltxt = round($object->lines[$i]->volume * $object->lines[$i]->qty_shipped, 5).' '.measuringUnitString(0, "volume", $object->lines[$i]->volume_units ? $object->lines[$i]->volume_units : 0, 1);
  536. }
  537. if ($this->getColumnStatus('weight')) {
  538. $this->printStdColumnContent($pdf, $curY, 'weight', $weighttxt.(($weighttxt && $voltxt) ? '<br>' : '').$voltxt, array('html'=>1));
  539. $nexY = max($pdf->GetY(), $nexY);
  540. }
  541. if ($this->getColumnStatus('qty_asked')) {
  542. $this->printStdColumnContent($pdf, $curY, 'qty_asked', $object->lines[$i]->qty_asked);
  543. $nexY = max($pdf->GetY(), $nexY);
  544. }
  545. if ($this->getColumnStatus('unit_order')) {
  546. $this->printStdColumnContent($pdf, $curY, 'unit_order', measuringUnitString($object->lines[$i]->fk_unit));
  547. $nexY = max($pdf->GetY(), $nexY);
  548. }
  549. if ($this->getColumnStatus('qty_shipped')) {
  550. $this->printStdColumnContent($pdf, $curY, 'qty_shipped', $object->lines[$i]->qty_shipped);
  551. $nexY = max($pdf->GetY(), $nexY);
  552. }
  553. if ($this->getColumnStatus('subprice')) {
  554. $this->printStdColumnContent($pdf, $curY, 'subprice', price($object->lines[$i]->subprice, 0, $outputlangs));
  555. $nexY = max($pdf->GetY(), $nexY);
  556. }
  557. // Extrafields
  558. if (!empty($object->lines[$i]->array_options)) {
  559. foreach ($object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue) {
  560. if ($this->getColumnStatus($extrafieldColKey)) {
  561. $extrafieldValue = $this->getExtrafieldContent($object->lines[$i], $extrafieldColKey, $outputlangs);
  562. $this->printStdColumnContent($pdf, $curY, $extrafieldColKey, $extrafieldValue);
  563. $nexY = max($pdf->GetY(), $nexY);
  564. }
  565. }
  566. }
  567. // Add line
  568. if (!empty($conf->global->MAIN_PDF_DASH_BETWEEN_LINES) && $i < ($nblines - 1)) {
  569. $pdf->setPage($pageposafter);
  570. $pdf->SetLineStyle(array('dash'=>'1,1', 'color'=>array(80, 80, 80)));
  571. //$pdf->SetDrawColor(190,190,200);
  572. $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY);
  573. $pdf->SetLineStyle(array('dash'=>0));
  574. }
  575. // Detect if some page were added automatically and output _tableau for past pages
  576. while ($pagenb < $pageposafter) {
  577. $pdf->setPage($pagenb);
  578. if ($pagenb == $pageposbeforeprintlines) {
  579. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
  580. } else {
  581. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
  582. }
  583. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  584. $pagenb++;
  585. $pdf->setPage($pagenb);
  586. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  587. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
  588. $this->_pagehead($pdf, $object, 0, $outputlangs);
  589. }
  590. }
  591. if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
  592. if ($pagenb == 1) {
  593. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
  594. } else {
  595. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
  596. }
  597. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  598. // New page
  599. $pdf->AddPage();
  600. if (!empty($tplidx)) {
  601. $pdf->useTemplate($tplidx);
  602. }
  603. $pagenb++;
  604. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
  605. $this->_pagehead($pdf, $object, 0, $outputlangs);
  606. }
  607. }
  608. }
  609. // Show square
  610. if ($pagenb == 1) {
  611. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
  612. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  613. } else {
  614. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0);
  615. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  616. }
  617. // Display total area
  618. $posy = $this->_tableau_tot($pdf, $object, 0, $bottomlasttab, $outputlangs);
  619. // Pagefoot
  620. $this->_pagefoot($pdf, $object, $outputlangs);
  621. if (method_exists($pdf, 'AliasNbPages')) {
  622. $pdf->AliasNbPages();
  623. }
  624. $pdf->Close();
  625. $pdf->Output($file, 'F');
  626. // Add pdfgeneration hook
  627. $hookmanager->initHooks(array('pdfgeneration'));
  628. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  629. global $action;
  630. $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  631. if ($reshook < 0) {
  632. $this->error = $hookmanager->error;
  633. $this->errors = $hookmanager->errors;
  634. }
  635. if (!empty($conf->global->MAIN_UMASK)) {
  636. @chmod($file, octdec($conf->global->MAIN_UMASK));
  637. }
  638. $this->result = array('fullpath'=>$file);
  639. return 1; // No error
  640. } else {
  641. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  642. return 0;
  643. }
  644. } else {
  645. $this->error = $langs->transnoentities("ErrorConstantNotDefined", "EXP_OUTPUTDIR");
  646. return 0;
  647. }
  648. }
  649. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  650. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  651. /**
  652. * Show total to pay
  653. *
  654. * @param TCPDF $pdf Object PDF
  655. * @param Expedition $object Object expedition
  656. * @param int $deja_regle Amount already paid
  657. * @param int $posy Start Position
  658. * @param Translate $outputlangs Objet langs
  659. * @return int Position for suite
  660. */
  661. protected function _tableau_tot(&$pdf, $object, $deja_regle, $posy, $outputlangs)
  662. {
  663. // phpcs:enable
  664. global $conf, $mysoc;
  665. $sign = 1;
  666. $default_font_size = pdf_getPDFFontSize($outputlangs);
  667. $tab2_top = $posy;
  668. $tab2_hl = 4;
  669. $pdf->SetFont('', 'B', $default_font_size - 1);
  670. // Total table
  671. $col1x = $this->posxweightvol - 50;
  672. $col2x = $this->posxweightvol;
  673. /*if ($this->page_largeur < 210) // To work with US executive format
  674. {
  675. $col2x-=20;
  676. }*/
  677. if (empty($conf->global->SHIPPING_PDF_HIDE_ORDERED)) {
  678. $largcol2 = ($this->posxqtyordered - $this->posxweightvol);
  679. } else {
  680. $largcol2 = ($this->posxqtytoship - $this->posxweightvol);
  681. }
  682. $useborder = 0;
  683. $index = 0;
  684. $totalWeighttoshow = '';
  685. $totalVolumetoshow = '';
  686. // Load dim data
  687. $tmparray = $object->getTotalWeightVolume();
  688. $totalWeight = $tmparray['weight'];
  689. $totalVolume = $tmparray['volume'];
  690. $totalOrdered = $tmparray['ordered'];
  691. $totalToShip = $tmparray['toship'];
  692. // Set trueVolume and volume_units not currently stored into database
  693. if ($object->trueWidth && $object->trueHeight && $object->trueDepth) {
  694. $object->trueVolume = price(($object->trueWidth * $object->trueHeight * $object->trueDepth), 0, $outputlangs, 0, 0);
  695. $object->volume_units = $object->size_units * 3;
  696. }
  697. if ($totalWeight != '') {
  698. $totalWeighttoshow = showDimensionInBestUnit($totalWeight, 0, "weight", $outputlangs);
  699. }
  700. if ($totalVolume != '') {
  701. $totalVolumetoshow = showDimensionInBestUnit($totalVolume, 0, "volume", $outputlangs);
  702. }
  703. if ($object->trueWeight) {
  704. $totalWeighttoshow = showDimensionInBestUnit($object->trueWeight, $object->weight_units, "weight", $outputlangs);
  705. }
  706. if ($object->trueVolume) {
  707. $totalVolumetoshow = showDimensionInBestUnit($object->trueVolume, $object->volume_units, "volume", $outputlangs);
  708. }
  709. if ($this->getColumnStatus('desc')) {
  710. $this->printStdColumnContent($pdf, $tab2_top, 'desc', $outputlangs->transnoentities("Total"));
  711. }
  712. if ($this->getColumnStatus('weight')) {
  713. if ($totalWeighttoshow) {
  714. $this->printStdColumnContent($pdf, $tab2_top, 'weight', $totalWeighttoshow);
  715. $index++;
  716. }
  717. if ($totalVolumetoshow) {
  718. $y = $tab2_top + ($tab2_hl * $index);
  719. $this->printStdColumnContent($pdf, $y, 'weight', $totalVolumetoshow);
  720. }
  721. }
  722. if ($this->getColumnStatus('qty_asked') && $totalOrdered) {
  723. $this->printStdColumnContent($pdf, $tab2_top, 'qty_asked', $totalOrdered);
  724. }
  725. if ($this->getColumnStatus('qty_shipped') && $totalToShip) {
  726. $this->printStdColumnContent($pdf, $tab2_top, 'qty_shipped', $totalToShip);
  727. }
  728. if ($this->getColumnStatus('subprice')) {
  729. $this->printStdColumnContent($pdf, $tab2_top, 'subprice', price($object->total_ht, 0, $outputlangs));
  730. }
  731. $pdf->SetTextColor(0, 0, 0);
  732. return ($tab2_top + ($tab2_hl * $index));
  733. }
  734. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  735. /**
  736. * Show table for lines
  737. *
  738. * @param TCPDF $pdf Object PDF
  739. * @param string $tab_top Top position of table
  740. * @param string $tab_height Height of table (rectangle)
  741. * @param int $nexY Y
  742. * @param Translate $outputlangs Langs object
  743. * @param int $hidetop Hide top bar of array
  744. * @param int $hidebottom Hide bottom bar of array
  745. * @return void
  746. */
  747. protected function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop = 0, $hidebottom = 0)
  748. {
  749. global $conf;
  750. // Force to disable hidetop and hidebottom
  751. $hidebottom = 0;
  752. if ($hidetop) {
  753. $hidetop = -1;
  754. }
  755. $currency = !empty($currency) ? $currency : $conf->currency;
  756. $default_font_size = pdf_getPDFFontSize($outputlangs);
  757. // Amount in (at tab_top - 1)
  758. $pdf->SetTextColor(0, 0, 0);
  759. $pdf->SetFont('', '', $default_font_size - 2);
  760. if (empty($hidetop)) {
  761. //$conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR='230,230,230';
  762. if (!empty($conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR)) {
  763. $pdf->Rect($this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_droite - $this->marge_gauche, $this->tabTitleHeight, 'F', null, explode(',', $conf->global->MAIN_PDF_TITLE_BACKGROUND_COLOR));
  764. }
  765. }
  766. $pdf->SetDrawColor(128, 128, 128);
  767. $pdf->SetFont('', '', $default_font_size - 1);
  768. // Output Rect
  769. $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
  770. $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
  771. if (empty($hidetop)) {
  772. $pdf->line($this->marge_gauche, $tab_top + $this->tabTitleHeight, $this->page_largeur - $this->marge_droite, $tab_top + $this->tabTitleHeight); // line takes a position y in 2nd parameter and 4th parameter
  773. }
  774. }
  775. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  776. /**
  777. * Show top header of page.
  778. *
  779. * @param TCPDF $pdf Object PDF
  780. * @param Expedition $object Object to show
  781. * @param int $showaddress 0=no, 1=yes
  782. * @param Translate $outputlangs Object lang for output
  783. * @return void
  784. */
  785. protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs)
  786. {
  787. global $conf, $langs, $mysoc;
  788. $langs->load("orders");
  789. $default_font_size = pdf_getPDFFontSize($outputlangs);
  790. pdf_pagehead($pdf, $outputlangs, $this->page_hauteur);
  791. //Prepare next
  792. $pdf->SetTextColor(0, 0, 60);
  793. $pdf->SetFont('', 'B', $default_font_size + 3);
  794. $w = 110;
  795. $posy = $this->marge_haute;
  796. $posx = $this->page_largeur - $this->marge_droite - $w;
  797. $pdf->SetXY($this->marge_gauche, $posy);
  798. // Logo
  799. $logo = $conf->mycompany->dir_output.'/logos/'.$this->emetteur->logo;
  800. if ($this->emetteur->logo) {
  801. if (is_readable($logo)) {
  802. $height = pdf_getHeightForLogo($logo);
  803. $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
  804. } else {
  805. $pdf->SetTextColor(200, 0, 0);
  806. $pdf->SetFont('', 'B', $default_font_size - 2);
  807. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ErrorLogoFileNotFound", $logo), 0, 'L');
  808. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ErrorGoToGlobalSetup"), 0, 'L');
  809. }
  810. } else {
  811. $text = $this->emetteur->name;
  812. $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, 'L');
  813. }
  814. // Show barcode
  815. if (!empty($conf->barcode->enabled)) {
  816. $posx = 105;
  817. } else {
  818. $posx = $this->marge_gauche + 3;
  819. }
  820. //$pdf->Rect($this->marge_gauche, $this->marge_haute, $this->page_largeur-$this->marge_gauche-$this->marge_droite, 30);
  821. if (!empty($conf->barcode->enabled)) {
  822. // TODO Build code bar with function writeBarCode of barcode module for sending ref $object->ref
  823. //$pdf->SetXY($this->marge_gauche+3, $this->marge_haute+3);
  824. //$pdf->Image($logo,10, 5, 0, 24);
  825. }
  826. $pdf->SetDrawColor(128, 128, 128);
  827. if (!empty($conf->barcode->enabled)) {
  828. // TODO Build code bar with function writeBarCode of barcode module for sending ref $object->ref
  829. //$pdf->SetXY($this->marge_gauche+3, $this->marge_haute+3);
  830. //$pdf->Image($logo,10, 5, 0, 24);
  831. }
  832. $posx = $this->page_largeur - $w - $this->marge_droite;
  833. $posy = $this->marge_haute;
  834. $pdf->SetFont('', 'B', $default_font_size + 2);
  835. $pdf->SetXY($posx, $posy);
  836. $pdf->SetTextColor(0, 0, 60);
  837. $title = $outputlangs->transnoentities("SendingSheet");
  838. $pdf->MultiCell($w, 4, $title, '', 'R');
  839. $pdf->SetFont('', '', $default_font_size + 1);
  840. $posy += 5;
  841. $pdf->SetXY($posx, $posy);
  842. $pdf->SetTextColor(0, 0, 60);
  843. $pdf->MultiCell($w, 4, $outputlangs->transnoentities("RefSending")." : ".$object->ref, '', 'R');
  844. // Date planned delivery
  845. if (!empty($object->date_delivery)) {
  846. $posy += 4;
  847. $pdf->SetXY($posx, $posy);
  848. $pdf->SetTextColor(0, 0, 60);
  849. $pdf->MultiCell($w, 4, $outputlangs->transnoentities("DateDeliveryPlanned")." : ".dol_print_date($object->date_delivery, "day", false, $outputlangs, true), '', 'R');
  850. }
  851. if (empty($conf->global->MAIN_PDF_HIDE_CUSTOMER_CODE) && !empty($object->thirdparty->code_client)) {
  852. $posy += 4;
  853. $pdf->SetXY($posx, $posy);
  854. $pdf->SetTextColor(0, 0, 60);
  855. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("CustomerCode")." : ".$outputlangs->transnoentities($object->thirdparty->code_client), '', 'R');
  856. }
  857. $pdf->SetFont('', '', $default_font_size + 3);
  858. $Yoff = 25;
  859. // Add list of linked orders
  860. $origin = $object->origin;
  861. $origin_id = $object->origin_id;
  862. // TODO move to external function
  863. if (!empty($conf->$origin->enabled)) { // commonly $origin='commande'
  864. $outputlangs->load('orders');
  865. $classname = ucfirst($origin);
  866. $linkedobject = new $classname($this->db);
  867. $result = $linkedobject->fetch($origin_id);
  868. if ($result >= 0) {
  869. //$linkedobject->fetchObjectLinked() Get all linked object to the $linkedobject (commonly order) into $linkedobject->linkedObjects
  870. $pdf->SetFont('', '', $default_font_size - 2);
  871. $text = $linkedobject->ref;
  872. if ($linkedobject->ref_client) {
  873. $text .= ' ('.$linkedobject->ref_client.')';
  874. }
  875. $Yoff = $Yoff + 8;
  876. $pdf->SetXY($this->page_largeur - $this->marge_droite - $w, $Yoff);
  877. $pdf->MultiCell($w, 2, $outputlangs->transnoentities("RefOrder")." : ".$outputlangs->transnoentities($text), 0, 'R');
  878. $Yoff = $Yoff + 3;
  879. $pdf->SetXY($this->page_largeur - $this->marge_droite - $w, $Yoff);
  880. $pdf->MultiCell($w, 2, $outputlangs->transnoentities("OrderDate")." : ".dol_print_date($linkedobject->date, "day", false, $outputlangs, true), 0, 'R');
  881. }
  882. }
  883. if ($showaddress) {
  884. // Sender properties
  885. $carac_emetteur = '';
  886. // Add internal contact of origin element if defined
  887. $arrayidcontact = array();
  888. if (!empty($origin) && is_object($object->$origin)) {
  889. $arrayidcontact = $object->$origin->getIdContact('internal', 'SALESREPFOLL');
  890. }
  891. if (is_array($arrayidcontact) && count($arrayidcontact) > 0) {
  892. $object->fetch_user(reset($arrayidcontact));
  893. $carac_emetteur .= ($carac_emetteur ? "\n" : '').$outputlangs->transnoentities("Name").": ".$outputlangs->convToOutputCharset($object->user->getFullName($outputlangs))."\n";
  894. }
  895. $carac_emetteur .= pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, '', 0, 'source', $object);
  896. // Show sender
  897. $posy = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 40 : 42;
  898. $posx = $this->marge_gauche;
  899. if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
  900. $posx = $this->page_largeur - $this->marge_droite - 80;
  901. }
  902. $hautcadre = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 38 : 40;
  903. $widthrecbox = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 92 : 82;
  904. // Show sender frame
  905. if (empty($conf->global->MAIN_PDF_NO_SENDER_FRAME)) {
  906. $pdf->SetTextColor(0, 0, 0);
  907. $pdf->SetFont('', '', $default_font_size - 2);
  908. $pdf->SetXY($posx, $posy - 5);
  909. $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities("Sender"), 0, 'L');
  910. $pdf->SetXY($posx, $posy);
  911. $pdf->SetFillColor(230, 230, 230);
  912. $pdf->MultiCell($widthrecbox, $hautcadre, "", 0, 'R', 1);
  913. $pdf->SetTextColor(0, 0, 60);
  914. $pdf->SetFillColor(255, 255, 255);
  915. }
  916. // Show sender name
  917. if (empty($conf->global->MAIN_PDF_HIDE_SENDER_NAME)) {
  918. $pdf->SetXY($posx + 2, $posy + 3);
  919. $pdf->SetFont('', 'B', $default_font_size);
  920. $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0, 'L');
  921. $posy = $pdf->getY();
  922. }
  923. // Show sender information
  924. $pdf->SetXY($posx + 2, $posy);
  925. $pdf->SetFont('', '', $default_font_size - 1);
  926. $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0, 'L');
  927. // If SHIPPING contact defined, we use it
  928. $usecontact = false;
  929. $arrayidcontact = $object->$origin->getIdContact('external', 'SHIPPING');
  930. if (count($arrayidcontact) > 0) {
  931. $usecontact = true;
  932. $result = $object->fetch_contact($arrayidcontact[0]);
  933. }
  934. // Recipient name
  935. if ($usecontact && ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)))) {
  936. $thirdparty = $object->contact;
  937. } else {
  938. $thirdparty = $object->thirdparty;
  939. }
  940. $carac_client_name = pdfBuildThirdpartyName($thirdparty, $outputlangs);
  941. $carac_client = pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, (!empty($object->contact) ? $object->contact : null), $usecontact, 'targetwithdetails', $object);
  942. // Show recipient
  943. $widthrecbox = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 92 : 100;
  944. if ($this->page_largeur < 210) {
  945. $widthrecbox = 84; // To work with US executive format
  946. }
  947. $posy = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 40 : 42;
  948. $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
  949. if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
  950. $posx = $this->marge_gauche;
  951. }
  952. // Show recipient frame
  953. if (empty($conf->global->MAIN_PDF_NO_RECIPENT_FRAME)) {
  954. $pdf->SetTextColor(0, 0, 0);
  955. $pdf->SetFont('', '', $default_font_size - 2);
  956. $pdf->SetXY($posx + 2, $posy - 5);
  957. $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities("Recipient"), 0, 'L');
  958. $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
  959. }
  960. // Show recipient name
  961. $pdf->SetXY($posx + 2, $posy + 3);
  962. $pdf->SetFont('', 'B', $default_font_size);
  963. $pdf->MultiCell($widthrecbox, 2, $carac_client_name, 0, 'L');
  964. $posy = $pdf->getY();
  965. // Show recipient information
  966. $pdf->SetFont('', '', $default_font_size - 1);
  967. $pdf->SetXY($posx + 2, $posy);
  968. $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, 'L');
  969. }
  970. $pdf->SetTextColor(0, 0, 0);
  971. }
  972. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  973. /**
  974. * Show footer of page. Need this->emetteur object
  975. *
  976. * @param TCPDF $pdf PDF
  977. * @param Expedition $object Object to show
  978. * @param Translate $outputlangs Object lang for output
  979. * @param int $hidefreetext 1=Hide free text
  980. * @return int Return height of bottom margin including footer text
  981. */
  982. protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
  983. {
  984. global $conf;
  985. $showdetails = empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS) ? 0 : $conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS;
  986. return pdf_pagefoot($pdf, $outputlangs, 'SHIPPING_FREE_TEXT', $this->emetteur, $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext, $this->page_largeur, $this->watermark);
  987. }
  988. /**
  989. * Define Array Column Field
  990. *
  991. * @param Expedition $object common object
  992. * @param Translate $outputlangs langs
  993. * @param int $hidedetails Do not show line details
  994. * @param int $hidedesc Do not show desc
  995. * @param int $hideref Do not show ref
  996. * @return null
  997. */
  998. public function defineColumnField($object, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  999. {
  1000. global $conf, $hookmanager;
  1001. // Default field style for content
  1002. $this->defaultContentsFieldsStyle = array(
  1003. 'align' => 'R', // R,C,L
  1004. 'padding' => array(1, 0.5, 1, 0.5), // Like css 0 => top , 1 => right, 2 => bottom, 3 => left
  1005. );
  1006. // Default field style for content
  1007. $this->defaultTitlesFieldsStyle = array(
  1008. 'align' => 'C', // R,C,L
  1009. 'padding' => array(0.5, 0, 0.5, 0), // Like css 0 => top , 1 => right, 2 => bottom, 3 => left
  1010. );
  1011. /*
  1012. * For exemple
  1013. $this->cols['theColKey'] = array(
  1014. 'rank' => $rank, // int : use for ordering columns
  1015. 'width' => 20, // the column width in mm
  1016. 'title' => array(
  1017. 'textkey' => 'yourLangKey', // if there is no label, yourLangKey will be translated to replace label
  1018. 'label' => ' ', // the final label : used fore final generated text
  1019. 'align' => 'L', // text alignement : R,C,L
  1020. 'padding' => array(0.5,0.5,0.5,0.5), // Like css 0 => top , 1 => right, 2 => bottom, 3 => left
  1021. ),
  1022. 'content' => array(
  1023. 'align' => 'L', // text alignement : R,C,L
  1024. 'padding' => array(0.5,0.5,0.5,0.5), // Like css 0 => top , 1 => right, 2 => bottom, 3 => left
  1025. ),
  1026. );
  1027. */
  1028. $rank = 0; // do not use negative rank
  1029. $this->cols['desc'] = array(
  1030. 'rank' => $rank,
  1031. 'width' => false, // only for desc
  1032. 'status' => true,
  1033. 'title' => array(
  1034. 'textkey' => 'Designation', // use lang key is usefull in somme case with module
  1035. 'align' => 'L',
  1036. // 'textkey' => 'yourLangKey', // if there is no label, yourLangKey will be translated to replace label
  1037. // 'label' => ' ', // the final label
  1038. 'padding' => array(0.5, 1, 0.5, 1.5), // Like css 0 => top , 1 => right, 2 => bottom, 3 => left
  1039. ),
  1040. 'content' => array(
  1041. 'padding' => array(1, 0.5, 1, 1.5), // Like css 0 => top , 1 => right, 2 => bottom, 3 => left
  1042. ),
  1043. );
  1044. $rank = $rank + 10;
  1045. $this->cols['photo'] = array(
  1046. 'rank' => $rank,
  1047. 'width' => (empty($conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH) ? 20 : $conf->global->MAIN_DOCUMENTS_WITH_PICTURE_WIDTH), // in mm
  1048. 'status' => false,
  1049. 'title' => array(
  1050. 'textkey' => 'Photo',
  1051. 'label' => ' '
  1052. ),
  1053. 'content' => array(
  1054. 'padding' => array(0, 0, 0, 0), // Like css 0 => top , 1 => right, 2 => bottom, 3 => left
  1055. ),
  1056. 'border-left' => false, // remove left line separator
  1057. );
  1058. if (!empty($conf->global->MAIN_GENERATE_SHIPMENT_WITH_PICTURE) && !empty($this->atleastonephoto)) {
  1059. $this->cols['photo']['status'] = true;
  1060. }
  1061. $rank = $rank + 10;
  1062. $this->cols['weight'] = array(
  1063. 'rank' => $rank,
  1064. 'width' => 30, // in mm
  1065. 'status' => true,
  1066. 'title' => array(
  1067. 'textkey' => 'WeightVolShort'
  1068. ),
  1069. 'border-left' => true, // add left line separator
  1070. );
  1071. $rank = $rank + 10;
  1072. $this->cols['subprice'] = array(
  1073. 'rank' => $rank,
  1074. 'width' => 19, // in mm
  1075. 'status' => !empty($conf->global->MAIN_PDF_SHIPPING_DISPLAY_AMOUNT_HT) ? 1 : 0,
  1076. 'title' => array(
  1077. 'textkey' => 'PriceUHT'
  1078. ),
  1079. 'border-left' => true, // add left line separator
  1080. );
  1081. $rank = $rank + 10;
  1082. $this->cols['totalexcltax'] = array(
  1083. 'rank' => $rank,
  1084. 'width' => 26, // in mm
  1085. 'status' => !empty($conf->global->MAIN_PDF_SHIPPING_DISPLAY_AMOUNT_HT) ? 1 : 0,
  1086. 'title' => array(
  1087. 'textkey' => 'TotalHT'
  1088. ),
  1089. 'border-left' => true, // add left line separator
  1090. );
  1091. $rank = $rank + 10;
  1092. $this->cols['qty_asked'] = array(
  1093. 'rank' => $rank,
  1094. 'width' => 30, // in mm
  1095. 'status' => empty($conf->global->SHIPPING_PDF_HIDE_ORDERED) ? 1 : 0,
  1096. 'title' => array(
  1097. 'textkey' => 'QtyOrdered'
  1098. ),
  1099. 'border-left' => true, // add left line separator
  1100. 'content' => array(
  1101. 'align' => 'C',
  1102. ),
  1103. );
  1104. $rank = $rank + 10;
  1105. $this->cols['unit_order'] = array(
  1106. 'rank' => $rank,
  1107. 'width' => 15, // in mm
  1108. 'status' => empty($conf->global->PRODUCT_USE_UNITS) ? 0 : 1,
  1109. 'title' => array(
  1110. 'textkey' => 'Unit'
  1111. ),
  1112. 'border-left' => true, // add left line separator
  1113. 'content' => array(
  1114. 'align' => 'C',
  1115. ),
  1116. );
  1117. $rank = $rank + 10;
  1118. $this->cols['qty_shipped'] = array(
  1119. 'rank' => $rank,
  1120. 'width' => 30, // in mm
  1121. 'status' => true,
  1122. 'title' => array(
  1123. 'textkey' => 'QtyToShip'
  1124. ),
  1125. 'border-left' => true, // add left line separator
  1126. 'content' => array(
  1127. 'align' => 'C',
  1128. ),
  1129. );
  1130. // Add extrafields cols
  1131. if (!empty($object->lines)) {
  1132. $line = reset($object->lines);
  1133. $this->defineColumnExtrafield($line, $outputlangs, $hidedetails);
  1134. }
  1135. $parameters = array(
  1136. 'object' => $object,
  1137. 'outputlangs' => $outputlangs,
  1138. 'hidedetails' => $hidedetails,
  1139. 'hidedesc' => $hidedesc,
  1140. 'hideref' => $hideref
  1141. );
  1142. $reshook = $hookmanager->executeHooks('defineColumnField', $parameters, $this); // Note that $object may have been modified by hook
  1143. if ($reshook < 0) {
  1144. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  1145. } elseif (empty($reshook)) {
  1146. $this->cols = array_replace($this->cols, $hookmanager->resArray); // array_replace is used to preserve keys
  1147. } else {
  1148. $this->cols = $hookmanager->resArray;
  1149. }
  1150. }
  1151. }