pdf_blochet.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /* Copyright (C) 2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2009-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. * or see https://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/modules/cheque/doc/pdf_blochet.class.php
  22. * \ingroup banque
  23. * \brief File to build cheque deposit receipts
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/modules/cheque/modules_chequereceipts.php';
  28. /**
  29. * Class of file to build cheque deposit receipts
  30. */
  31. class BordereauChequeBlochet extends ModeleChequeReceipts
  32. {
  33. /**
  34. * Issuer
  35. * @var Societe
  36. */
  37. public $emetteur;
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. public function __construct($db)
  44. {
  45. global $conf, $langs, $mysoc;
  46. // Load traductions files required by page
  47. $langs->loadLangs(array("main", "bills"));
  48. $this->db = $db;
  49. $this->name = "blochet";
  50. $this->tab_top = 60;
  51. // Page size for A4 format
  52. $this->type = 'pdf';
  53. $formatarray = pdf_getFormat();
  54. $this->page_largeur = $formatarray['width'];
  55. $this->page_hauteur = $formatarray['height'];
  56. $this->format = array($this->page_largeur, $this->page_hauteur);
  57. $this->marge_gauche = isset($conf->global->MAIN_PDF_MARGIN_LEFT) ? $conf->global->MAIN_PDF_MARGIN_LEFT : 10;
  58. $this->marge_droite = isset($conf->global->MAIN_PDF_MARGIN_RIGHT) ? $conf->global->MAIN_PDF_MARGIN_RIGHT : 10;
  59. $this->marge_haute = isset($conf->global->MAIN_PDF_MARGIN_TOP) ? $conf->global->MAIN_PDF_MARGIN_TOP : 10;
  60. $this->marge_basse = isset($conf->global->MAIN_PDF_MARGIN_BOTTOM) ? $conf->global->MAIN_PDF_MARGIN_BOTTOM : 10;
  61. // Retrieves transmitter
  62. $this->emetteur = $mysoc;
  63. if (!$this->emetteur->country_code) $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
  64. // Define column position
  65. $this->line_height = 5;
  66. $this->line_per_page = 40;
  67. $this->tab_height = 200; //$this->line_height * $this->line_per_page;
  68. }
  69. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  70. /**
  71. * Fonction to generate document on disk
  72. *
  73. * @param RemiseCheque $object Object RemiseCheque
  74. * @param string $_dir Directory
  75. * @param string $number Number
  76. * @param Translate $outputlangs Lang output object
  77. * @return int 1=ok, 0=ko
  78. */
  79. public function write_file($object, $_dir, $number, $outputlangs)
  80. {
  81. // phpcs:enable
  82. global $user, $conf, $langs, $hookmanager;
  83. if (!is_object($outputlangs)) $outputlangs = $langs;
  84. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  85. $sav_charset_output = $outputlangs->charset_output;
  86. if (!empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output = 'ISO-8859-1';
  87. // Load traductions files required by page
  88. $outputlangs->loadLangs(array("main", "companies", "bills", "products", "compta"));
  89. $dir = $_dir."/".get_exdir($number, 0, 1, 0, $object, 'cheque').$number;
  90. if (!is_dir($dir))
  91. {
  92. $result = dol_mkdir($dir);
  93. if ($result < 0)
  94. {
  95. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  96. return -1;
  97. }
  98. }
  99. $file = $dir."/bordereau-".$number.".pdf";
  100. // Add pdfgeneration hook
  101. if (!is_object($hookmanager))
  102. {
  103. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  104. $hookmanager = new HookManager($this->db);
  105. }
  106. $hookmanager->initHooks(array('pdfgeneration'));
  107. $parameters = array('file'=>$file, 'outputlangs'=>$outputlangs);
  108. global $action;
  109. $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  110. // Create PDF instance
  111. $pdf = pdf_getInstance($this->format);
  112. $heightforinfotot = 50; // Height reserved to output the info and total part
  113. $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
  114. $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
  115. if ($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS > 0) $heightforfooter += 6;
  116. $pdf->SetAutoPageBreak(1, 0);
  117. if (class_exists('TCPDF'))
  118. {
  119. $pdf->setPrintHeader(false);
  120. $pdf->setPrintFooter(false);
  121. }
  122. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  123. $pdf->Open();
  124. $pagenb = 0;
  125. $pdf->SetDrawColor(128, 128, 128);
  126. $pdf->SetTitle($outputlangs->transnoentities("CheckReceipt")." ".$number);
  127. $pdf->SetSubject($outputlangs->transnoentities("CheckReceipt"));
  128. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  129. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  130. $pdf->SetKeyWords($outputlangs->transnoentities("CheckReceipt")." ".$number);
  131. if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
  132. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  133. $nboflines = count($this->lines);
  134. // Define nb of page
  135. $pages = intval($nboflines / $this->line_per_page);
  136. if (($nboflines % $this->line_per_page) > 0)
  137. {
  138. $pages++;
  139. }
  140. if ($pages == 0)
  141. {
  142. // force to build at least one page if report has no lines
  143. $pages = 1;
  144. }
  145. $pdf->AddPage();
  146. $pagenb++;
  147. $this->Header($pdf, $pagenb, $pages, $outputlangs);
  148. $this->Body($pdf, $pagenb, $pages, $outputlangs);
  149. // Pied de page
  150. $this->_pagefoot($pdf, '', $outputlangs);
  151. if (method_exists($pdf, 'AliasNbPages')) $pdf->AliasNbPages();
  152. $pdf->Close();
  153. $pdf->Output($file, 'F');
  154. // Add pdfgeneration hook
  155. if (!is_object($hookmanager))
  156. {
  157. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  158. $hookmanager = new HookManager($this->db);
  159. }
  160. $hookmanager->initHooks(array('pdfgeneration'));
  161. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  162. global $action;
  163. $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  164. if ($reshook < 0)
  165. {
  166. $this->error = $hookmanager->error;
  167. $this->errors = $hookmanager->errors;
  168. }
  169. if (!empty($conf->global->MAIN_UMASK))
  170. @chmod($file, octdec($conf->global->MAIN_UMASK));
  171. $this->result = array('fullpath'=>$file);
  172. $outputlangs->charset_output = $sav_charset_output;
  173. return 1; // No error
  174. }
  175. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  176. /**
  177. * Generate Header
  178. *
  179. * @param TCPDF $pdf Pdf object
  180. * @param int $page Current page number
  181. * @param int $pages Total number of pages
  182. * @param Translate $outputlangs Object language for output
  183. * @return void
  184. */
  185. public function Header(&$pdf, $page, $pages, $outputlangs)
  186. {
  187. // phpcs:enable
  188. global $langs;
  189. $default_font_size = pdf_getPDFFontSize($outputlangs);
  190. // Load traductions files required by page
  191. $outputlangs->loadLangs(array("compta", "banks"));
  192. $title = $outputlangs->transnoentities("CheckReceipt");
  193. $pdf->SetFont('', 'B', $default_font_size);
  194. $pdf->SetXY(10, 8);
  195. $pdf->MultiCell(0, 2, $title, 0, 'L');
  196. $pdf->SetFont('', '', $default_font_size);
  197. $pdf->SetXY(10, 15);
  198. $pdf->MultiCell(22, 2, $outputlangs->transnoentities("Ref"), 0, 'L');
  199. $pdf->SetXY(32, 15);
  200. $pdf->SetFont('', '', $default_font_size);
  201. $pdf->MultiCell(60, 2, $outputlangs->convToOutputCharset($this->ref.($this->ref_ext ? " - ".$this->ref_ext : '')), 0, 'L');
  202. $pdf->SetFont('', '', $default_font_size);
  203. $pdf->SetXY(10, 20);
  204. $pdf->MultiCell(22, 2, $outputlangs->transnoentities("Date"), 0, 'L');
  205. $pdf->SetXY(32, 20);
  206. $pdf->SetFont('', '', $default_font_size);
  207. $pdf->MultiCell(60, 2, dol_print_date($this->date, "day", false, $outputlangs));
  208. $pdf->SetFont('', '', $default_font_size);
  209. $pdf->SetXY(10, 26);
  210. $pdf->MultiCell(22, 2, $outputlangs->transnoentities("Owner"), 0, 'L');
  211. $pdf->SetFont('', '', $default_font_size);
  212. $pdf->SetXY(32, 26);
  213. $pdf->MultiCell(80, 2, $outputlangs->convToOutputCharset($this->account->proprio), 0, 'L');
  214. $pdf->SetFont('', '', $default_font_size);
  215. $pdf->SetXY(10, 32);
  216. $pdf->MultiCell(0, 2, $outputlangs->transnoentities("Account"), 0, 'L');
  217. pdf_bank($pdf, $outputlangs, 32, 32, $this->account, 1);
  218. $pdf->SetFont('', '', $default_font_size);
  219. $pdf->SetXY(114, 15);
  220. $pdf->MultiCell(40, 2, $outputlangs->transnoentities("Signature"), 0, 'L');
  221. $pdf->Rect(9, 14, 192, 35);
  222. $pdf->line(9, 19, 112, 19);
  223. $pdf->line(9, 25, 112, 25);
  224. //$pdf->line(9, 31, 201, 31);
  225. $pdf->line(9, 31, 112, 31);
  226. $pdf->line(30, 14, 30, 49);
  227. $pdf->line(112, 14, 112, 49);
  228. // Number of cheques
  229. $posy = 51;
  230. $pdf->Rect(9, $posy, 192, 6);
  231. $pdf->line(55, $posy, 55, $posy + 6);
  232. $pdf->line(140, $posy, 140, $posy + 6);
  233. $pdf->line(170, $posy, 170, $posy + 6);
  234. $pdf->SetFont('', '', $default_font_size);
  235. $pdf->SetXY(10, $posy + 1);
  236. $pdf->MultiCell(40, 2, $outputlangs->transnoentities("NumberOfCheques"), 0, 'L');
  237. $pdf->SetFont('', 'B', $default_font_size);
  238. $pdf->SetXY(57, $posy + 1);
  239. $pdf->MultiCell(40, 2, $this->nbcheque, 0, 'L');
  240. $pdf->SetFont('', '', $default_font_size);
  241. $pdf->SetXY(148, $posy + 1);
  242. $pdf->MultiCell(40, 2, $langs->trans("Total"));
  243. $pdf->SetFont('', 'B', $default_font_size);
  244. $pdf->SetXY(170, $posy + 1);
  245. $pdf->MultiCell(31, 2, price($this->amount), 0, 'C', 0);
  246. // Tableau
  247. $pdf->SetFont('', '', $default_font_size - 2);
  248. $pdf->SetXY(11, $this->tab_top + 2);
  249. $pdf->MultiCell(40, 2, $outputlangs->transnoentities("Num"), 0, 'L');
  250. $pdf->line(40, $this->tab_top, 40, $this->tab_top + $this->tab_height + 10);
  251. $pdf->SetXY(41, $this->tab_top + 2);
  252. $pdf->MultiCell(40, 2, $outputlangs->transnoentities("Bank"), 0, 'L');
  253. $pdf->line(100, $this->tab_top, 100, $this->tab_top + $this->tab_height + 10);
  254. $pdf->SetXY(101, $this->tab_top + 2);
  255. $pdf->MultiCell(40, 2, $outputlangs->transnoentities("CheckTransmitter"), 0, 'L');
  256. $pdf->line(180, $this->tab_top, 180, $this->tab_top + $this->tab_height + 10);
  257. $pdf->SetXY(180, $this->tab_top + 2);
  258. $pdf->MultiCell(20, 2, $outputlangs->transnoentities("Amount"), 0, 'R');
  259. $pdf->line(9, $this->tab_top + 8, 201, $this->tab_top + 8);
  260. $pdf->Rect(9, $this->tab_top, 192, $this->tab_height + 10);
  261. }
  262. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  263. /**
  264. * Output array
  265. *
  266. * @param TCPDF $pdf PDF object
  267. * @param int $pagenb Page nb
  268. * @param int $pages Pages
  269. * @param Translate $outputlangs Object lang
  270. * @return void
  271. */
  272. public function Body(&$pdf, $pagenb, $pages, $outputlangs)
  273. {
  274. // phpcs:enable
  275. // x=10 - Num
  276. // x=30 - Banque
  277. // x=100 - Emetteur
  278. $default_font_size = pdf_getPDFFontSize($outputlangs);
  279. $pdf->SetFont('', '', $default_font_size - 1);
  280. $oldprowid = 0;
  281. $pdf->SetFillColor(220, 220, 220);
  282. $yp = 0;
  283. $lineinpage = 0;
  284. $num = count($this->lines);
  285. for ($j = 0; $j < $num; $j++)
  286. {
  287. // Dynamic max line heigh calculation
  288. $dynamic_line_height = array();
  289. $dynamic_line_height[] = $pdf->getStringHeight(60, $outputlangs->convToOutputCharset($this->lines[$j]->bank_chq));
  290. $dynamic_line_height[] = $pdf->getStringHeight(80, $outputlangs->convToOutputCharset($this->lines[$j]->emetteur_chq));
  291. $max_line_height = max($dynamic_line_height);
  292. // Calculate number of line used function of estimated line size
  293. if ($max_line_height > $this->line_height) $nb_lines = floor($max_line_height / $this->line_height) + 1;
  294. else $nb_lines = 1;
  295. // Add page break if we do not have space to add current line
  296. if ($lineinpage >= ($this->line_per_page - 1))
  297. {
  298. $lineinpage = 0; $yp = 0;
  299. // New page
  300. $pdf->AddPage();
  301. $pagenb++;
  302. $this->Header($pdf, $pagenb, $pages, $outputlangs);
  303. $pdf->SetFont('', '', $default_font_size - 1);
  304. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  305. $pdf->SetTextColor(0, 0, 0);
  306. }
  307. $lineinpage += $nb_lines;
  308. $pdf->SetXY(1, $this->tab_top + 10 + $yp);
  309. $pdf->MultiCell(8, $this->line_height, $j + 1, 0, 'R', 0);
  310. $pdf->SetXY(10, $this->tab_top + 10 + $yp);
  311. $pdf->MultiCell(30, $this->line_height, $this->lines[$j]->num_chq ? $this->lines[$j]->num_chq : '', 0, 'L', 0);
  312. $pdf->SetXY(40, $this->tab_top + 10 + $yp);
  313. $pdf->MultiCell(60, $this->line_height, $outputlangs->convToOutputCharset($this->lines[$j]->bank_chq, 44), 0, 'L', 0);
  314. $pdf->SetXY(100, $this->tab_top + 10 + $yp);
  315. $pdf->MultiCell(80, $this->line_height, $outputlangs->convToOutputCharset($this->lines[$j]->emetteur_chq, 50), 0, 'L', 0);
  316. $pdf->SetXY(180, $this->tab_top + 10 + $yp);
  317. $pdf->MultiCell(20, $this->line_height, price($this->lines[$j]->amount_chq), 0, 'R', 0);
  318. $yp = $yp + ($this->line_height * $nb_lines);
  319. }
  320. }
  321. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  322. /**
  323. * Show footer of page. Need this->emetteur object
  324. *
  325. * @param TCPDF $pdf PDF
  326. * @param Object $object Object to show
  327. * @param Translate $outputlangs Object lang for output
  328. * @param int $hidefreetext 1=Hide free text
  329. * @return void
  330. */
  331. protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
  332. {
  333. global $conf;
  334. $default_font_size = pdf_getPDFFontSize($outputlangs);
  335. $showdetails = $conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS;
  336. // Line of free text
  337. $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object);
  338. complete_substitutions_array($substitutionarray, $outputlangs, $object);
  339. $newfreetext = '';
  340. $paramfreetext = 'BANK_CHEQUERECEIPT_FREE_TEXT';
  341. if (!empty($conf->global->$paramfreetext))
  342. {
  343. $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
  344. }
  345. return pdf_pagefoot($pdf, $outputlangs, $newfreetext, $this->emetteur, $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext);
  346. }
  347. }