pdf_ban.modules.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/modules/bank/doc/pdf_ban.modules.php
  20. * \ingroup bank
  21. * \brief File of class to generate document with template ban
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/modules/bank/modules_bank.php';
  24. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. /**
  29. * Classe permettant de generer les projets au modele Ban
  30. */
  31. class pdf_ban extends ModeleBankAccountDoc
  32. {
  33. /**
  34. * @var Societe Issuer
  35. */
  36. public $emetteur;
  37. /**
  38. * @var string Dolibarr version of the loaded document
  39. */
  40. public $version = 'development';
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. */
  46. public function __construct($db)
  47. {
  48. global $conf, $langs, $mysoc;
  49. // Load translation files required by the page
  50. $langs->loadLangs(array("main", "bank", "withdrawals", "companies"));
  51. $this->db = $db;
  52. $this->name = "ban";
  53. $this->description = $langs->trans("DocumentModelBan").' (Volunteer wanted to finish)';
  54. // Page size for A4 format
  55. $this->type = 'pdf';
  56. $formatarray = pdf_getFormat();
  57. $this->page_largeur = $formatarray['width'];
  58. $this->page_hauteur = $formatarray['height'];
  59. $this->format = array($this->page_largeur, $this->page_hauteur);
  60. $this->marge_gauche = isset($conf->global->MAIN_PDF_MARGIN_LEFT) ? $conf->global->MAIN_PDF_MARGIN_LEFT : 10;
  61. $this->marge_droite = isset($conf->global->MAIN_PDF_MARGIN_RIGHT) ? $conf->global->MAIN_PDF_MARGIN_RIGHT : 10;
  62. $this->marge_haute = isset($conf->global->MAIN_PDF_MARGIN_TOP) ? $conf->global->MAIN_PDF_MARGIN_TOP : 10;
  63. $this->marge_basse = isset($conf->global->MAIN_PDF_MARGIN_BOTTOM) ? $conf->global->MAIN_PDF_MARGIN_BOTTOM : 10;
  64. $this->option_logo = 1; // Display logo FAC_PDF_LOGO
  65. $this->option_tva = 1; // Manage the vat option FACTURE_TVAOPTION
  66. $this->option_codeproduitservice = 1; // Display product-service code
  67. // Retrieves transmitter
  68. $this->emetteur = $mysoc;
  69. if (!$this->emetteur->country_code) {
  70. $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
  71. }
  72. // Define column position
  73. $this->posxref = $this->marge_gauche + 1;
  74. $this->posxlabel = $this->marge_gauche + 25;
  75. $this->posxworkload = $this->marge_gauche + 100;
  76. $this->posxdatestart = $this->marge_gauche + 150;
  77. $this->posxdateend = $this->marge_gauche + 170;
  78. }
  79. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  80. /**
  81. * Fonction generant le projet sur le disque
  82. *
  83. * @param Account $object Object Account to generate
  84. * @param Translate $outputlangs Lang output object
  85. * @return int 1 if OK, <=0 if KO
  86. */
  87. public function write_file($object, $outputlangs)
  88. {
  89. // phpcs:enable
  90. global $conf, $hookmanager, $langs, $user;
  91. if (!is_object($outputlangs)) {
  92. $outputlangs = $langs;
  93. }
  94. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  95. if (!empty($conf->global->MAIN_USE_FPDF)) {
  96. $outputlangs->charset_output = 'ISO-8859-1';
  97. }
  98. // Load traductions files required by page
  99. $outputlangs->loadLangs(array("main", "dict", "companies", "projects"));
  100. if ($conf->bank->dir_output) {
  101. //$nblines = count($object->lines); // This is set later with array of tasks
  102. // Definition of $dir and $file
  103. if ($object->specimen) {
  104. $dir = $conf->bank->dir_output;
  105. $file = $dir."/SPECIMEN.pdf";
  106. } else {
  107. $objectref = dol_sanitizeFileName($object->ref);
  108. $dir = $conf->bank->dir_output."/".$objectref;
  109. $file = $dir."/".$objectref.".pdf";
  110. }
  111. if (!file_exists($dir)) {
  112. if (dol_mkdir($dir) < 0) {
  113. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  114. return 0;
  115. }
  116. }
  117. if (file_exists($dir)) {
  118. // Add pdfgeneration hook
  119. if (!is_object($hookmanager)) {
  120. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  121. $hookmanager = new HookManager($this->db);
  122. }
  123. $hookmanager->initHooks(array('pdfgeneration'));
  124. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  125. global $action;
  126. $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  127. $pdf = pdf_getInstance($this->format);
  128. $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance
  129. $heightforinfotot = 50; // Height reserved to output the info and total part
  130. $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
  131. $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
  132. if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
  133. $heightforfooter += 6;
  134. }
  135. $pdf->SetAutoPageBreak(1, 0);
  136. if (class_exists('TCPDF')) {
  137. $pdf->setPrintHeader(false);
  138. $pdf->setPrintFooter(false);
  139. }
  140. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  141. $pdf->Open();
  142. $pagenb = 0;
  143. $pdf->SetDrawColor(128, 128, 128);
  144. $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
  145. $pdf->SetSubject($outputlangs->transnoentities("BAN"));
  146. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  147. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  148. $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("BAN"));
  149. if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) {
  150. $pdf->SetCompression(false);
  151. }
  152. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  153. // New page
  154. $pdf->AddPage();
  155. $pagenb++;
  156. $this->_pagehead($pdf, $object, 1, $outputlangs);
  157. $pdf->SetFont('', '', $default_font_size - 1);
  158. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  159. $pdf->SetTextColor(0, 0, 0);
  160. $tab_top = 50;
  161. $tab_top_newpage = 40;
  162. $tab_height = $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext;
  163. // Affiche notes
  164. if (!empty($object->note_public)) {
  165. $pdf->SetFont('', '', $default_font_size - 1);
  166. $pdf->writeHTMLCell(190, 3, $this->posxref - 1, $tab_top - 2, dol_htmlentitiesbr($object->note_public), 0, 1);
  167. $nexY = $pdf->GetY();
  168. $height_note = $nexY - ($tab_top - 2);
  169. // Rect takes a length in 3rd parameter
  170. $pdf->SetDrawColor(192, 192, 192);
  171. $pdf->Rect($this->marge_gauche, $tab_top - 3, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
  172. $tab_height = $tab_height - $height_note;
  173. $tab_top = $nexY + 6;
  174. } else {
  175. $height_note = 0;
  176. }
  177. $iniY = $tab_top + 7;
  178. $curY = $tab_top + 7;
  179. $nexY = $tab_top + 7;
  180. $pdf->SetXY($this->marge_gauche, $curY);
  181. $pdf->MultiCell(200, 3, $outputlangs->trans("BAN").' : '.$object->account_number, 0, 'L');
  182. // Show square
  183. if ($pagenb == 1) {
  184. $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
  185. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  186. } else {
  187. $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0);
  188. $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
  189. }
  190. /*
  191. * Footer of the page
  192. */
  193. $this->_pagefoot($pdf, $object, $outputlangs);
  194. if (method_exists($pdf, 'AliasNbPages')) {
  195. $pdf->AliasNbPages();
  196. }
  197. $pdf->Close();
  198. $pdf->Output($file, 'F');
  199. // Add pdfgeneration hook
  200. if (!is_object($hookmanager)) {
  201. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  202. $hookmanager = new HookManager($this->db);
  203. }
  204. $hookmanager->initHooks(array('pdfgeneration'));
  205. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  206. global $action;
  207. $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  208. if ($reshook < 0) {
  209. $this->error = $hookmanager->error;
  210. $this->errors = $hookmanager->errors;
  211. }
  212. if (!empty($conf->global->MAIN_UMASK)) {
  213. @chmod($file, octdec($conf->global->MAIN_UMASK));
  214. }
  215. $this->result = array('fullpath'=>$file);
  216. return 1; // No error
  217. } else {
  218. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  219. return 0;
  220. }
  221. }
  222. $this->error = $langs->transnoentities("ErrorConstantNotDefined", "DELIVERY_OUTPUTDIR");
  223. return 0;
  224. }
  225. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  226. /**
  227. * Show table for lines
  228. *
  229. * @param PDF $pdf Object PDF
  230. * @param string $tab_top Top position of table
  231. * @param string $tab_height Height of table (rectangle)
  232. * @param int $nexY Y
  233. * @param Translate $outputlangs Langs object
  234. * @param int $hidetop Hide top bar of array
  235. * @param int $hidebottom Hide bottom bar of array
  236. * @return void
  237. */
  238. protected function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop = 0, $hidebottom = 0)
  239. {
  240. // phpcs:enable
  241. global $conf, $mysoc;
  242. $default_font_size = pdf_getPDFFontSize($outputlangs);
  243. }
  244. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  245. /**
  246. * Show top header of page.
  247. *
  248. * @param TCPDF $pdf Object PDF
  249. * @param Account $object Object to show
  250. * @param int $showaddress 0=no, 1=yes
  251. * @param Translate $outputlangs Object lang for output
  252. * @return void
  253. */
  254. protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs)
  255. {
  256. global $langs, $conf, $mysoc;
  257. // phpcs:enable
  258. $default_font_size = pdf_getPDFFontSize($outputlangs);
  259. pdf_pagehead($pdf, $outputlangs, $this->page_hauteur);
  260. $pdf->SetTextColor(0, 0, 60);
  261. $pdf->SetFont('', 'B', $default_font_size + 3);
  262. $posx = $this->page_largeur - $this->marge_droite - 100;
  263. $posy = $this->marge_haute;
  264. $pdf->SetXY($this->marge_gauche, $posy);
  265. // Logo
  266. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  267. if ($mysoc->logo) {
  268. if (is_readable($logo)) {
  269. $height = pdf_getHeightForLogo($logo);
  270. $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
  271. } else {
  272. $pdf->SetTextColor(200, 0, 0);
  273. $pdf->SetFont('', 'B', $default_font_size - 2);
  274. $pdf->MultiCell(100, 3, $langs->transnoentities("ErrorLogoFileNotFound", $logo), 0, 'L');
  275. $pdf->MultiCell(100, 3, $langs->transnoentities("ErrorGoToModuleSetup"), 0, 'L');
  276. }
  277. } else {
  278. $pdf->MultiCell(100, 4, $outputlangs->transnoentities($this->emetteur->name), 0, 'L');
  279. }
  280. $pdf->SetFont('', 'B', $default_font_size + 3);
  281. $pdf->SetXY($posx, $posy);
  282. $pdf->SetTextColor(0, 0, 60);
  283. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("BAN")." ".$outputlangs->convToOutputCharset($object->ref), '', 'R');
  284. $pdf->SetFont('', '', $default_font_size + 2);
  285. $posy += 6;
  286. $pdf->SetXY($posx, $posy);
  287. $pdf->SetTextColor(0, 0, 60);
  288. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("Date")." : ".dol_print_date(dol_now(), 'day', false, $outputlangs, true), '', 'R');
  289. /*$posy+=6;
  290. $pdf->SetXY($posx,$posy);
  291. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("DateEnd")." : " . dol_print_date($object->date_end,'day',false,$outputlangs,true), '', 'R');
  292. */
  293. $pdf->SetTextColor(0, 0, 60);
  294. // Add list of linked objects
  295. /* Removed: A project can have more than thousands linked objects (orders, invoices, proposals, etc....
  296. $object->fetchObjectLinked();
  297. foreach($object->linkedObjects as $objecttype => $objects)
  298. {
  299. //var_dump($objects);exit;
  300. if ($objecttype == 'commande')
  301. {
  302. $outputlangs->load('orders');
  303. $num=count($objects);
  304. for ($i=0;$i<$num;$i++)
  305. {
  306. $posy+=4;
  307. $pdf->SetXY($posx,$posy);
  308. $pdf->SetFont('','', $default_font_size - 1);
  309. $text=$objects[$i]->ref;
  310. if ($objects[$i]->ref_client) $text.=' ('.$objects[$i]->ref_client.')';
  311. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("RefOrder")." : ".$outputlangs->transnoentities($text), '', 'R');
  312. }
  313. }
  314. }
  315. */
  316. }
  317. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  318. /**
  319. * Show footer of page. Need this->emetteur object
  320. *
  321. * @param TCPDF $pdf PDF
  322. * @param Account $object Object to show
  323. * @param Translate $outputlangs Object lang for output
  324. * @param int $hidefreetext 1=Hide free text
  325. * @return integer
  326. */
  327. protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
  328. {
  329. // phpcs:enable
  330. global $conf;
  331. $showdetails = empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS) ? 0 : $conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS;
  332. //return pdf_pagefoot($pdf,$outputlangs,'BANK_FREE_TEXT',$this->emetteur,$this->marge_basse,$this->marge_gauche,$this->page_hauteur,$object,$showdetails,$hidefreetext);
  333. }
  334. }