pdf_strato.modules.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  6. * Copyright (C) 2011 Fabrice CHERRIER
  7. * Copyright (C) 2013-2020 Philippe Grand <philippe.grand@atoo-net.com>
  8. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  9. * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. * or see https://www.gnu.org/
  24. */
  25. /**
  26. * \file htdocs/core/modules/contract/doc/pdf_strato.modules.php
  27. * \ingroup ficheinter
  28. * \brief Strato contracts template class file
  29. */
  30. require_once DOL_DOCUMENT_ROOT.'/core/modules/contract/modules_contract.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  35. /**
  36. * Class to build contracts documents with model Strato
  37. */
  38. class pdf_strato extends ModelePDFContract
  39. {
  40. /**
  41. * @var DoliDb Database handler
  42. */
  43. public $db;
  44. /**
  45. * @var int The environment ID when using a multicompany module
  46. */
  47. public $entity;
  48. /**
  49. * @var string model name
  50. */
  51. public $name;
  52. /**
  53. * @var string model description (short text)
  54. */
  55. public $description;
  56. /**
  57. * @var int Save the name of generated file as the main doc when generating a doc with this template
  58. */
  59. public $update_main_doc_field;
  60. /**
  61. * @var string document type
  62. */
  63. public $type;
  64. /**
  65. * @var array Minimum version of PHP required by module.
  66. * e.g.: PHP ≥ 7.0 = array(7, 0)
  67. */
  68. public $phpmin = array(7, 0);
  69. /**
  70. * Dolibarr version of the loaded document
  71. * @var string
  72. */
  73. public $version = 'dolibarr';
  74. /**
  75. * @var int page_largeur
  76. */
  77. public $page_largeur;
  78. /**
  79. * @var int page_hauteur
  80. */
  81. public $page_hauteur;
  82. /**
  83. * @var array format
  84. */
  85. public $format;
  86. /**
  87. * @var int marge_gauche
  88. */
  89. public $marge_gauche;
  90. /**
  91. * @var int marge_droite
  92. */
  93. public $marge_droite;
  94. /**
  95. * @var int marge_haute
  96. */
  97. public $marge_haute;
  98. /**
  99. * @var int marge_basse
  100. */
  101. public $marge_basse;
  102. /**
  103. * Issuer
  104. * @var Societe Object that emits
  105. */
  106. public $emetteur;
  107. /**
  108. * Recipient
  109. * @var Societe
  110. */
  111. public $recipient;
  112. /**
  113. * Constructor
  114. *
  115. * @param DoliDB $db Database handler
  116. */
  117. public function __construct($db)
  118. {
  119. global $conf, $langs, $mysoc;
  120. $this->db = $db;
  121. $this->name = 'strato';
  122. $this->description = $langs->trans("StandardContractsTemplate");
  123. $this->update_main_doc_field = 1; // Save the name of generated file as the main doc when generating a doc with this template
  124. // Dimension page
  125. $this->type = 'pdf';
  126. $formatarray = pdf_getFormat();
  127. $this->page_largeur = $formatarray['width'];
  128. $this->page_hauteur = $formatarray['height'];
  129. $this->format = array($this->page_largeur, $this->page_hauteur);
  130. $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
  131. $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
  132. $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
  133. $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
  134. $this->option_logo = 1; // Display logo
  135. $this->option_tva = 0; // Manage the vat option FACTURE_TVAOPTION
  136. $this->option_modereg = 0; // Display payment mode
  137. $this->option_condreg = 0; // Display payment terms
  138. $this->option_multilang = 0; // Available in several languages
  139. $this->option_draft_watermark = 1; // Support add of a watermark on drafts
  140. // Get source company
  141. $this->emetteur = $mysoc;
  142. if (empty($this->emetteur->country_code)) {
  143. $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default, if was not defined
  144. }
  145. // Define position of columns
  146. $this->posxdesc = $this->marge_gauche + 1;
  147. }
  148. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  149. /**
  150. * Function to build pdf onto disk
  151. *
  152. * @param Contrat $object Object to generate
  153. * @param Translate $outputlangs Lang output object
  154. * @param string $srctemplatepath Full path of source filename for generator using a template file
  155. * @param int $hidedetails Do not show line details
  156. * @param int $hidedesc Do not show desc
  157. * @param int $hideref Do not show ref
  158. * @return int 1=OK, 0=KO
  159. */
  160. public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
  161. {
  162. // phpcs:enable
  163. global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
  164. if (!is_object($outputlangs)) {
  165. $outputlangs = $langs;
  166. }
  167. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  168. if (!empty($conf->global->MAIN_USE_FPDF)) {
  169. $outputlangs->charset_output = 'ISO-8859-1';
  170. }
  171. // Load translation files required by page
  172. $outputlangs->loadLangs(array("main", "dict", "companies", "contracts"));
  173. // Show Draft Watermark
  174. if ($object->statut == $object::STATUS_DRAFT && (!empty($conf->global->CONTRACT_DRAFT_WATERMARK))) {
  175. $this->watermark = $conf->global->CONTRACT_DRAFT_WATERMARK;
  176. }
  177. global $outputlangsbis;
  178. $outputlangsbis = null;
  179. if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
  180. $outputlangsbis = new Translate('', $conf);
  181. $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
  182. $outputlangsbis->loadLangs(array("main", "dict", "companies", "bills", "products", "orders", "deliveries"));
  183. }
  184. $nblines = count($object->lines);
  185. if ($conf->contract->multidir_output[$conf->entity]) {
  186. $object->fetch_thirdparty();
  187. // Definition of $dir and $file
  188. if ($object->specimen) {
  189. $dir = $conf->contract->multidir_output[$conf->entity];
  190. $file = $dir."/SPECIMEN.pdf";
  191. } else {
  192. $objectref = dol_sanitizeFileName($object->ref);
  193. $dir = $conf->contrat->multidir_output[$object->entity]."/".$objectref;
  194. $file = $dir."/".$objectref.".pdf";
  195. }
  196. if (!file_exists($dir)) {
  197. if (dol_mkdir($dir) < 0) {
  198. $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
  199. return 0;
  200. }
  201. }
  202. if (file_exists($dir)) {
  203. // Add pdfgeneration hook
  204. if (!is_object($hookmanager)) {
  205. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  206. $hookmanager = new HookManager($this->db);
  207. }
  208. $hookmanager->initHooks(array('pdfgeneration'));
  209. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  210. global $action;
  211. $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  212. // Set nblines with the new command lines content after hook
  213. $nblines = count($object->lines);
  214. // Create pdf instance
  215. $pdf = pdf_getInstance($this->format);
  216. $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance
  217. $pdf->SetAutoPageBreak(1, 0);
  218. $heightforinfotot = 50; // Height reserved to output the info and total part
  219. $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
  220. $heightforfooter = $this->marge_basse + 9; // Height reserved to output the footer (value include bottom margin)
  221. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
  222. $heightforfooter += 6;
  223. }
  224. if (class_exists('TCPDF')) {
  225. $pdf->setPrintHeader(false);
  226. $pdf->setPrintFooter(false);
  227. }
  228. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  229. // Set path to the background PDF File
  230. if (!empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) {
  231. $logodir = $conf->mycompany->dir_output;
  232. if (!empty($conf->mycompany->multidir_output[$object->entity])) {
  233. $logodir = $conf->mycompany->multidir_output[$object->entity];
  234. }
  235. $pagecount = $pdf->setSourceFile($logodir.'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND);
  236. $tplidx = $pdf->importPage(1);
  237. }
  238. $pdf->Open();
  239. $pagenb = 0;
  240. $pdf->SetDrawColor(128, 128, 128);
  241. $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
  242. $pdf->SetSubject($outputlangs->transnoentities("Contract"));
  243. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  244. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  245. $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("Contract")." ".$outputlangs->convToOutputCharset($object->thirdparty->name));
  246. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  247. $pdf->SetCompression(false);
  248. }
  249. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  250. // New page
  251. $pdf->AddPage();
  252. if (!empty($tplidx)) {
  253. $pdf->useTemplate($tplidx);
  254. }
  255. $pagenb++;
  256. $top_shift = $this->_pagehead($pdf, $object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis : null));
  257. $pdf->SetFont('', '', $default_font_size - 1);
  258. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  259. $pdf->SetTextColor(0, 0, 0);
  260. $tab_top = 90;
  261. $tab_top_newpage = (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
  262. // Display notes
  263. if (!empty($object->note_public)) {
  264. $tab_top -= 2;
  265. $pdf->SetFont('', '', $default_font_size - 1);
  266. $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1, dol_htmlentitiesbr($object->note_public), 0, 1);
  267. $nexY = $pdf->GetY();
  268. $height_note = $nexY - $tab_top;
  269. // Rect takes a length in 3rd parameter
  270. $pdf->SetDrawColor(192, 192, 192);
  271. $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
  272. $tab_top = $nexY + 6;
  273. }
  274. $iniY = $tab_top + 7;
  275. $curY = $tab_top + 7;
  276. $nexY = $tab_top + 2;
  277. $pdf->SetXY($this->marge_gauche, $tab_top);
  278. $pdf->MultiCell(0, 2, ''); // Set interline to 3. Then writeMultiCell must use 3 also.
  279. $nblines = count($object->lines);
  280. // Loop on each lines
  281. for ($i = 0; $i < $nblines; $i++) {
  282. $objectligne = $object->lines[$i];
  283. $valide = $objectligne->id ? 1 : 0;
  284. if ($valide > 0 || $object->specimen) {
  285. $curX = $this->posxdesc - 1;
  286. $curY = $nexY;
  287. $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage
  288. $pdf->SetTextColor(0, 0, 0);
  289. $pdf->setTopMargin($tab_top_newpage);
  290. $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext + $heightforinfotot); // The only function to edit the bottom margin of current page to set it.
  291. $pageposbefore = $pdf->getPage();
  292. // Description of product line
  293. if (!empty($objectligne->date_start)) {
  294. $datei = dol_print_date((int) $objectligne->date_start, 'day', false, $outputlangs, true);
  295. } else {
  296. $datei = $langs->trans("Unknown");
  297. }
  298. if (!empty($objectligne->date_end)) {
  299. $durationi = convertSecondToTime((int) $objectligne->date_end - (int) $objectligne->date_start, 'allwithouthour');
  300. $datee = dol_print_date($objectligne->date_end, 'day', false, $outputlangs, true);
  301. } else {
  302. $durationi = $langs->trans("Unknown");
  303. $datee = $langs->trans("Unknown");
  304. }
  305. if (!empty($objectligne->date_start_real)) {
  306. $daters = dol_print_date((int) $objectligne->date_start_real, 'day', false, $outputlangs, true);
  307. } else {
  308. $daters = $langs->trans("Unknown");
  309. }
  310. if (!empty($objectligne->date_end_real)) {
  311. $datere = dol_print_date((int) $objectligne->date_end_real, 'day', false, $outputlangs, true);
  312. } else {
  313. $datere = $langs->trans("Unknown");
  314. }
  315. $txtpredefinedservice = $objectligne->product_ref;
  316. if ($objectligne->product_label) {
  317. $txtpredefinedservice .= ' - ';
  318. $txtpredefinedservice .= $objectligne->product_label;
  319. }
  320. $desc = dol_htmlentitiesbr($objectligne->desc, 1); // Desc (not empty for free lines)
  321. $txt = '';
  322. if (empty($conf->global->CONTRACT_HIDE_QTY_ON_PDF)) {
  323. $txt .= $outputlangs->transnoentities("Quantity") . ' : <strong>' . $objectligne->qty . '</strong>';
  324. }
  325. if (empty($conf->global->CONTRACT_HIDE_PRICE_ON_PDF)) {
  326. $txt .= ' - ' . $outputlangs->transnoentities("UnitPrice") . ' : <strong>' . price($objectligne->subprice) . '</strong>';
  327. }
  328. if (empty($conf->global->CONTRACT_HIDE_PLANNED_DATE_ON_PDF)) {
  329. $txt .= '<br>';
  330. $txt .= $outputlangs->transnoentities("DateStartPlannedShort")." : <strong>".$datei."</strong> - ".$outputlangs->transnoentities("DateEndPlanned")." : <strong>".$datee.'</strong>';
  331. }
  332. if (empty($conf->global->CONTRACT_HIDE_REAL_DATE_ON_PDF)) {
  333. $txt .= '<br>';
  334. $txt .= $outputlangs->transnoentities("DateStartRealShort")." : <strong>".$daters.'</strong>';
  335. if (!empty($objectligne->date_end_real)) {
  336. $txt .= " - ".$outputlangs->transnoentities("DateEndRealShort")." : '<strong>'".$datere.'</strong>';
  337. }
  338. }
  339. $pdf->startTransaction();
  340. $pdf->writeHTMLCell(0, 0, $curX, $curY, dol_concatdesc($txtpredefinedservice, dol_concatdesc($txt, $desc)), 0, 1, 0);
  341. $pageposafter = $pdf->getPage();
  342. if ($pageposafter > $pageposbefore) { // There is a pagebreak
  343. $pdf->rollbackTransaction(true);
  344. $pageposafter = $pageposbefore;
  345. //print $pageposafter.'-'.$pageposbefore;exit;
  346. $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it.
  347. $pdf->writeHTMLCell(0, 0, $curX, $curY, dol_concatdesc($txtpredefinedservice, dol_concatdesc($txt, $desc)), 0, 1, 0);
  348. $pageposafter = $pdf->getPage();
  349. $posyafter = $pdf->GetY();
  350. if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) { // There is no space left for total+free text
  351. if ($i == ($nblines - 1)) { // No more lines, and no space left to show total, so we create a new page
  352. $pdf->AddPage('', '', true);
  353. if (!empty($tplidx)) {
  354. $pdf->useTemplate($tplidx);
  355. }
  356. if (!getDolGlobalInt('MAIN_PDF_DONOTREPEAT_HEAD')) {
  357. $this->_pagehead($pdf, $object, 0, $outputlangs);
  358. }
  359. $pdf->setPage($pageposafter + 1);
  360. }
  361. } else {
  362. // We found a page break
  363. // Allows data in the first page if description is long enough to break in multiples pages
  364. if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
  365. $showpricebeforepagebreak = 1;
  366. } else {
  367. $showpricebeforepagebreak = 0;
  368. }
  369. }
  370. } else // No pagebreak
  371. {
  372. $pdf->commitTransaction();
  373. }
  374. $posYAfterDescription = $pdf->GetY();
  375. $nexY = $pdf->GetY() + 2;
  376. $pageposafter = $pdf->getPage();
  377. $pdf->setPage($pageposbefore);
  378. $pdf->setTopMargin($this->marge_haute);
  379. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  380. // We suppose that a too long description is moved completely on next page
  381. if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
  382. $pdf->setPage($pageposafter);
  383. $curY = $tab_top_newpage;
  384. }
  385. $pdf->SetFont('', '', $default_font_size - 1); // We reposition the default font
  386. // Detect if some page were added automatically and output _tableau for past pages
  387. while ($pagenb < $pageposafter) {
  388. $pdf->setPage($pagenb);
  389. if ($pagenb == 1) {
  390. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
  391. } else {
  392. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
  393. }
  394. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  395. $pagenb++;
  396. $pdf->setPage($pagenb);
  397. $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
  398. if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
  399. $this->_pagehead($pdf, $object, 0, $outputlangs);
  400. }
  401. if (!empty($tplidx)) {
  402. $pdf->useTemplate($tplidx);
  403. }
  404. }
  405. if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
  406. if ($pagenb == 1) {
  407. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
  408. } else {
  409. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
  410. }
  411. $this->_pagefoot($pdf, $object, $outputlangs, 1);
  412. // New page
  413. $pdf->AddPage();
  414. if (!empty($tplidx)) {
  415. $pdf->useTemplate($tplidx);
  416. }
  417. $pagenb++;
  418. }
  419. }
  420. }
  421. // Show square
  422. if ($pagenb == 1) {
  423. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
  424. $this->tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
  425. $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
  426. } else {
  427. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
  428. $this->tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
  429. $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
  430. }
  431. $this->_pagefoot($pdf, $object, $outputlangs);
  432. if (method_exists($pdf, 'AliasNbPages')) {
  433. $pdf->AliasNbPages();
  434. }
  435. $pdf->Close();
  436. $pdf->Output($file, 'F');
  437. // Add pdfgeneration hook
  438. $hookmanager->initHooks(array('pdfgeneration'));
  439. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  440. global $action;
  441. $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  442. if ($reshook < 0) {
  443. $this->error = $hookmanager->error;
  444. $this->errors = $hookmanager->errors;
  445. }
  446. if (!empty($conf->global->MAIN_UMASK)) {
  447. @chmod($file, octdec($conf->global->MAIN_UMASK));
  448. }
  449. $this->result = array('fullpath'=>$file);
  450. return 1; // No error
  451. } else {
  452. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  453. return 0;
  454. }
  455. } else {
  456. $this->error = $langs->transnoentities("ErrorConstantNotDefined", "CONTRACT_OUTPUTDIR");
  457. return 0;
  458. }
  459. }
  460. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  461. /**
  462. * Show table for lines
  463. *
  464. * @param TCPDF $pdf Object PDF
  465. * @param string $tab_top Top position of table
  466. * @param string $tab_height Height of table (rectangle)
  467. * @param int $nexY Y
  468. * @param Translate $outputlangs Langs object
  469. * @param int $hidetop Hide top bar of array
  470. * @param int $hidebottom Hide bottom bar of array
  471. * @return void
  472. */
  473. protected function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop = 0, $hidebottom = 0)
  474. {
  475. global $conf;
  476. // Force to disable hidetop and hidebottom
  477. $hidebottom = 0;
  478. if ($hidetop) {
  479. $hidetop = -1;
  480. }
  481. $currency = !empty($currency) ? $currency : $conf->currency;
  482. $default_font_size = pdf_getPDFFontSize($outputlangs);
  483. /*
  484. $pdf->SetXY($this->marge_gauche, $tab_top);
  485. $pdf->MultiCell(190,8,$outputlangs->transnoentities("Description"),0,'L',0);
  486. $pdf->line($this->marge_gauche, $tab_top + 8, $this->page_largeur-$this->marge_droite, $tab_top + 8);
  487. $pdf->SetFont('','', $default_font_size - 1);
  488. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  489. $pdf->SetXY($this->marge_gauche, $tab_top + 8);
  490. $text=$object->description;
  491. if ($object->duree > 0)
  492. {
  493. $totaltime=convertSecondToTime($object->duree,'all',$conf->global->MAIN_DURATION_OF_WORKDAY);
  494. $text.=($text?' - ':'').$langs->trans("Total").": ".$totaltime;
  495. }
  496. $desc=dol_htmlentitiesbr($text,1);
  497. //print $outputlangs->convToOutputCharset($desc); exit;
  498. $pdf->writeHTMLCell(180, 3, 10, $tab_top + 8, $outputlangs->convToOutputCharset($desc), 0, 1);
  499. $nexY = $pdf->GetY();
  500. $pdf->line($this->marge_gauche, $nexY, $this->page_largeur-$this->marge_droite, $nexY);
  501. $pdf->MultiCell(0, 3, ''); // Set interline to 3. Then writeMultiCell must use 3 also.
  502. */
  503. // Output Rect
  504. $this->printRect($pdf, $this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $tab_height + 3); // Rect takes a length in 3rd parameter and 4th parameter
  505. }
  506. /**
  507. * Show footer signature of page
  508. * @param TCPDF $pdf Object PDF
  509. * @param int $tab_top tab height position
  510. * @param int $tab_height tab height
  511. * @param Translate $outputlangs Object language for output
  512. * @return void
  513. */
  514. protected function tabSignature(&$pdf, $tab_top, $tab_height, $outputlangs)
  515. {
  516. $pdf->SetDrawColor(128, 128, 128);
  517. $posmiddle = $this->marge_gauche + round(($this->page_largeur - $this->marge_gauche - $this->marge_droite) / 2);
  518. $posy = $tab_top + $tab_height + 3 + 3;
  519. $pdf->SetXY($this->marge_gauche, $posy);
  520. $pdf->MultiCell($posmiddle - $this->marge_gauche - 5, 5, $outputlangs->transnoentities("ContactNameAndSignature", $this->emetteur->name), 0, 'L', 0);
  521. $pdf->SetXY($this->marge_gauche, $posy + 5);
  522. $pdf->MultiCell($posmiddle - $this->marge_gauche - 5, 20, '', 1);
  523. $pdf->SetXY($posmiddle + 5, $posy);
  524. $pdf->MultiCell($this->page_largeur - $this->marge_droite - $posmiddle - 5, 5, $outputlangs->transnoentities("ContactNameAndSignature", $this->recipient->name), 0, 'L', 0);
  525. $pdf->SetXY($posmiddle + 5, $posy + 5);
  526. $pdf->MultiCell($this->page_largeur - $this->marge_droite - $posmiddle - 5, 20, '', 1);
  527. }
  528. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  529. /**
  530. * Show top header of page.
  531. *
  532. * @param TCPDF $pdf Object PDF
  533. * @param Contrat $object Object to show
  534. * @param int $showaddress 0=no, 1=yes
  535. * @param Translate $outputlangs Object lang for output
  536. * @param Translate $outputlangsbis Object lang for output bis
  537. * @param string $titlekey Translation key to show as title of document
  538. * @return int Return topshift value
  539. */
  540. protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs, $outputlangsbis = null, $titlekey = "Contract")
  541. {
  542. // phpcs:enable
  543. global $conf, $langs, $hookmanager;
  544. $ltrdirection = 'L';
  545. if ($outputlangs->trans("DIRECTION") == 'rtl') {
  546. $ltrdirection = 'R';
  547. }
  548. // Load traductions files required by page
  549. $outputlangs->loadLangs(array("main", "dict", "contract", "companies"));
  550. $default_font_size = pdf_getPDFFontSize($outputlangs);
  551. pdf_pagehead($pdf, $outputlangs, $this->page_hauteur);
  552. $pdf->SetTextColor(0, 0, 60);
  553. $pdf->SetFont('', 'B', $default_font_size + 3);
  554. $w = 100;
  555. $posy = $this->marge_haute;
  556. $posx = $this->page_largeur - $this->marge_droite - $w;
  557. $pdf->SetXY($this->marge_gauche, $posy);
  558. // Logo
  559. if (empty($conf->global->PDF_DISABLE_MYCOMPANY_LOGO)) {
  560. if ($this->emetteur->logo) {
  561. $logodir = $conf->mycompany->dir_output;
  562. if (!empty($conf->mycompany->multidir_output[$object->entity])) {
  563. $logodir = $conf->mycompany->multidir_output[$object->entity];
  564. }
  565. if (empty($conf->global->MAIN_PDF_USE_LARGE_LOGO)) {
  566. $logo = $logodir.'/logos/thumbs/'.$this->emetteur->logo_small;
  567. } else {
  568. $logo = $logodir.'/logos/'.$this->emetteur->logo;
  569. }
  570. if (is_readable($logo)) {
  571. $height = pdf_getHeightForLogo($logo);
  572. $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
  573. } else {
  574. $pdf->SetTextColor(200, 0, 0);
  575. $pdf->SetFont('', 'B', $default_font_size - 2);
  576. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ErrorLogoFileNotFound", $logo), 0, 'L');
  577. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("ErrorGoToGlobalSetup"), 0, 'L');
  578. }
  579. } else {
  580. $text = $this->emetteur->name;
  581. $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
  582. }
  583. }
  584. $pdf->SetFont('', 'B', $default_font_size + 3);
  585. $pdf->SetXY($posx, $posy);
  586. $pdf->SetTextColor(0, 0, 60);
  587. $title = $outputlangs->transnoentities($titlekey);
  588. $title .= ' '.$outputlangs->convToOutputCharset($object->ref);
  589. if ($object->statut == $object::STATUS_DRAFT) {
  590. $pdf->SetTextColor(128, 0, 0);
  591. $title .= ' - '.$outputlangs->transnoentities("NotValidated");
  592. }
  593. $pdf->MultiCell($w, 3, $title, '', 'R');
  594. $pdf->SetFont('', 'B', $default_font_size);
  595. /*
  596. $posy += 5;
  597. $pdf->SetXY($posx, $posy);
  598. $pdf->SetTextColor(0, 0, 60);
  599. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("Ref")." : ".$outputlangs->convToOutputCharset($object->ref), '', 'R');
  600. */
  601. $posy += 3;
  602. $pdf->SetFont('', '', $default_font_size - 1);
  603. $posy += 4;
  604. $pdf->SetXY($posx, $posy);
  605. $pdf->SetTextColor(0, 0, 60);
  606. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("Date")." : ".dol_print_date($object->date_contrat, "day", false, $outputlangs, true), '', 'R');
  607. if (empty($conf->global->MAIN_PDF_HIDE_CUSTOMER_CODE) && $object->thirdparty->code_client) {
  608. $posy += 4;
  609. $pdf->SetXY($posx, $posy);
  610. $pdf->SetTextColor(0, 0, 60);
  611. $pdf->MultiCell($w, 3, $outputlangs->transnoentities("CustomerCode")." : ".$outputlangs->transnoentities($object->thirdparty->code_client), '', 'R');
  612. }
  613. if ($showaddress) {
  614. // Sender properties
  615. $carac_emetteur = '';
  616. // Add internal contact of proposal if defined
  617. $arrayidcontact = $object->getIdContact('internal', 'INTERREPFOLL');
  618. if (count($arrayidcontact) > 0) {
  619. $object->fetch_user($arrayidcontact[0]);
  620. $carac_emetteur .= ($carac_emetteur ? "\n" : '').$outputlangs->transnoentities("Name").": ".$outputlangs->convToOutputCharset($object->user->getFullName($outputlangs))."\n";
  621. }
  622. $carac_emetteur .= pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, '', 0, 'source', $object);
  623. // Show sender
  624. $posy = 42;
  625. $posx = $this->marge_gauche;
  626. if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
  627. $posx = $this->page_largeur - $this->marge_droite - 80;
  628. }
  629. $hautcadre = 40;
  630. // Show sender frame
  631. if (empty($conf->global->MAIN_PDF_NO_SENDER_FRAME)) {
  632. $pdf->SetTextColor(0, 0, 0);
  633. $pdf->SetFont('', '', $default_font_size - 2);
  634. $pdf->SetXY($posx, $posy - 5);
  635. $pdf->SetXY($posx, $posy);
  636. $pdf->SetFillColor(230, 230, 230);
  637. $pdf->MultiCell(82, $hautcadre, "", 0, 'R', 1);
  638. $pdf->SetTextColor(0, 0, 60);
  639. }
  640. // Show sender name
  641. if (empty($conf->global->MAIN_PDF_HIDE_SENDER_NAME)) {
  642. $pdf->SetXY($posx + 2, $posy + 3);
  643. $pdf->SetTextColor(0, 0, 60);
  644. $pdf->SetFont('', 'B', $default_font_size);
  645. $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0, 'L');
  646. $posy = $pdf->getY();
  647. }
  648. // Show sender information
  649. $pdf->SetXY($posx + 2, $posy);
  650. $pdf->SetFont('', '', $default_font_size - 1);
  651. $pdf->MultiCell(80, 4, $carac_emetteur, 0, 'L');
  652. // If CUSTOMER contact defined, we use it
  653. $usecontact = false;
  654. $arrayidcontact = $object->getIdContact('external', 'CUSTOMER');
  655. if (count($arrayidcontact) > 0) {
  656. $usecontact = true;
  657. $result = $object->fetch_contact($arrayidcontact[0]);
  658. }
  659. $this->recipient = $object->thirdparty;
  660. // Recipient name
  661. 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)))) {
  662. $thirdparty = $object->contact;
  663. } else {
  664. $thirdparty = $object->thirdparty;
  665. }
  666. $this->recipient->name = pdfBuildThirdpartyName($thirdparty, $outputlangs);
  667. $mode = 'target';
  668. $carac_client = pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, (isset($object->contact) ? $object->contact : ''), $usecontact, $mode, $object);
  669. // Show recipient
  670. $widthrecbox = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 92 : 100;
  671. if ($this->page_largeur < 210) {
  672. $widthrecbox = 84; // To work with US executive format
  673. }
  674. $posy = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 40 : 42;
  675. $posy += $top_shift;
  676. $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
  677. if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
  678. $posx = $this->marge_gauche;
  679. }
  680. // Show recipient frame
  681. if (empty($conf->global->MAIN_PDF_NO_RECIPENT_FRAME)) {
  682. $pdf->SetTextColor(0, 0, 0);
  683. $pdf->SetFont('', '', $default_font_size - 2);
  684. $pdf->SetXY($posx + 2, $posy - 5);
  685. $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
  686. $pdf->SetTextColor(0, 0, 0);
  687. }
  688. // Show recipient name
  689. $pdf->SetXY($posx + 2, $posy + 3);
  690. $pdf->SetFont('', 'B', $default_font_size);
  691. $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
  692. $posy = $pdf->getY();
  693. // Show recipient information
  694. $pdf->SetFont('', '', $default_font_size - 1);
  695. $pdf->SetXY($posx + 2, $posy);
  696. $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
  697. }
  698. $pdf->SetTextColor(0, 0, 0);
  699. return $top_shift;
  700. }
  701. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  702. /**
  703. * Show footer of page. Need this->emetteur object
  704. *
  705. * @param PDF $pdf PDF
  706. * @param Contrat $object Object to show
  707. * @param Translate $outputlangs Object lang for output
  708. * @param int $hidefreetext 1=Hide free text
  709. * @return integer
  710. */
  711. protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
  712. {
  713. $showdetails = getDolGlobalInt('MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS', 0);
  714. return pdf_pagefoot($pdf, $outputlangs, 'CONTRACT_FREE_TEXT', $this->emetteur, $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext, $this->page_largeur, $this->watermark);
  715. }
  716. }