pdf_ban.modules.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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; // Affiche logo FAC_PDF_LOGO
  65. $this->option_tva = 1; // Gere option tva FACTURE_TVAOPTION
  66. $this->option_codeproduitservice = 1; // Affiche code produit-service
  67. // Retrieves transmitter
  68. $this->emetteur = $mysoc;
  69. if (!$this->emetteur->country_code) $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
  70. // Define column position
  71. $this->posxref = $this->marge_gauche + 1;
  72. $this->posxlabel = $this->marge_gauche + 25;
  73. $this->posxworkload = $this->marge_gauche + 100;
  74. $this->posxdatestart = $this->marge_gauche + 150;
  75. $this->posxdateend = $this->marge_gauche + 170;
  76. }
  77. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  78. /**
  79. * Fonction generant le projet sur le disque
  80. *
  81. * @param Project $object Object project a generer
  82. * @param Translate $outputlangs Lang output object
  83. * @return int 1 if OK, <=0 if KO
  84. */
  85. public function write_file($object, $outputlangs)
  86. {
  87. // phpcs:enable
  88. global $conf, $hookmanager, $langs, $user;
  89. if (!is_object($outputlangs)) $outputlangs = $langs;
  90. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  91. if (!empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output = 'ISO-8859-1';
  92. // Load traductions files required by page
  93. $outputlangs->loadLangs(array("main", "dict", "companies", "projects"));
  94. if ($conf->bank->dir_output)
  95. {
  96. //$nblines = count($object->lines); // This is set later with array of tasks
  97. // Definition of $dir and $file
  98. if ($object->specimen)
  99. {
  100. $dir = $conf->bank->dir_output;
  101. $file = $dir."/SPECIMEN.pdf";
  102. } else {
  103. $objectref = dol_sanitizeFileName($object->ref);
  104. $dir = $conf->bank->dir_output."/".$objectref;
  105. $file = $dir."/".$objectref.".pdf";
  106. }
  107. if (!file_exists($dir))
  108. {
  109. if (dol_mkdir($dir) < 0)
  110. {
  111. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  112. return 0;
  113. }
  114. }
  115. if (file_exists($dir))
  116. {
  117. // Add pdfgeneration hook
  118. if (!is_object($hookmanager))
  119. {
  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 ($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS > 0) $heightforfooter += 6;
  133. $pdf->SetAutoPageBreak(1, 0);
  134. if (class_exists('TCPDF'))
  135. {
  136. $pdf->setPrintHeader(false);
  137. $pdf->setPrintFooter(false);
  138. }
  139. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  140. $pdf->Open();
  141. $pagenb = 0;
  142. $pdf->SetDrawColor(128, 128, 128);
  143. $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
  144. $pdf->SetSubject($outputlangs->transnoentities("BAN"));
  145. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  146. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  147. $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("BAN"));
  148. if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
  149. $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
  150. // New page
  151. $pdf->AddPage();
  152. $pagenb++;
  153. $this->_pagehead($pdf, $object, 1, $outputlangs);
  154. $pdf->SetFont('', '', $default_font_size - 1);
  155. $pdf->MultiCell(0, 3, ''); // Set interline to 3
  156. $pdf->SetTextColor(0, 0, 0);
  157. $tab_top = 50;
  158. $tab_height = 200;
  159. $tab_top_newpage = 40;
  160. $tab_height_newpage = 210;
  161. // Affiche notes
  162. if (!empty($object->note_public))
  163. {
  164. $pdf->SetFont('', '', $default_font_size - 1);
  165. $pdf->writeHTMLCell(190, 3, $this->posxref - 1, $tab_top - 2, dol_htmlentitiesbr($object->note_public), 0, 1);
  166. $nexY = $pdf->GetY();
  167. $height_note = $nexY - ($tab_top - 2);
  168. // Rect takes a length in 3rd parameter
  169. $pdf->SetDrawColor(192, 192, 192);
  170. $pdf->Rect($this->marge_gauche, $tab_top - 3, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
  171. $tab_height = $tab_height - $height_note;
  172. $tab_top = $nexY + 6;
  173. } else {
  174. $height_note = 0;
  175. }
  176. $iniY = $tab_top + 7;
  177. $curY = $tab_top + 7;
  178. $nexY = $tab_top + 7;
  179. $pdf->SetXY($this->marge_gauche, $curY);
  180. $pdf->MultiCell(200, 3, $outputlangs->trans("BAN").' : '.$object->account_number, 0, 'L');
  181. // Show square
  182. if ($pagenb == 1)
  183. {
  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')) $pdf->AliasNbPages();
  195. $pdf->Close();
  196. $pdf->Output($file, 'F');
  197. // Add pdfgeneration hook
  198. if (!is_object($hookmanager))
  199. {
  200. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  201. $hookmanager = new HookManager($this->db);
  202. }
  203. $hookmanager->initHooks(array('pdfgeneration'));
  204. $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
  205. global $action;
  206. $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  207. if ($reshook < 0)
  208. {
  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. $this->result = array('fullpath'=>$file);
  215. return 1; // No error
  216. } else {
  217. $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
  218. return 0;
  219. }
  220. }
  221. $this->error = $langs->transnoentities("ErrorConstantNotDefined", "DELIVERY_OUTPUTDIR");
  222. return 0;
  223. }
  224. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  225. /**
  226. * Show table for lines
  227. *
  228. * @param PDF $pdf Object PDF
  229. * @param string $tab_top Top position of table
  230. * @param string $tab_height Height of table (rectangle)
  231. * @param int $nexY Y
  232. * @param Translate $outputlangs Langs object
  233. * @param int $hidetop Hide top bar of array
  234. * @param int $hidebottom Hide bottom bar of array
  235. * @return void
  236. */
  237. protected function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop = 0, $hidebottom = 0)
  238. {
  239. // phpcs:enable
  240. global $conf, $mysoc;
  241. $default_font_size = pdf_getPDFFontSize($outputlangs);
  242. }
  243. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  244. /**
  245. * Show top header of page.
  246. *
  247. * @param TCPDF $pdf Object PDF
  248. * @param Project $object Object to show
  249. * @param int $showaddress 0=no, 1=yes
  250. * @param Translate $outputlangs Object lang for output
  251. * @return void
  252. */
  253. protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs)
  254. {
  255. global $langs, $conf, $mysoc;
  256. // phpcs:enable
  257. $default_font_size = pdf_getPDFFontSize($outputlangs);
  258. pdf_pagehead($pdf, $outputlangs, $this->page_hauteur);
  259. $pdf->SetTextColor(0, 0, 60);
  260. $pdf->SetFont('', 'B', $default_font_size + 3);
  261. $posx = $this->page_largeur - $this->marge_droite - 100;
  262. $posy = $this->marge_haute;
  263. $pdf->SetXY($this->marge_gauche, $posy);
  264. // Logo
  265. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  266. if ($mysoc->logo)
  267. {
  268. if (is_readable($logo))
  269. {
  270. $height = pdf_getHeightForLogo($logo);
  271. $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
  272. } else {
  273. $pdf->SetTextColor(200, 0, 0);
  274. $pdf->SetFont('', 'B', $default_font_size - 2);
  275. $pdf->MultiCell(100, 3, $langs->transnoentities("ErrorLogoFileNotFound", $logo), 0, 'L');
  276. $pdf->MultiCell(100, 3, $langs->transnoentities("ErrorGoToModuleSetup"), 0, 'L');
  277. }
  278. } else $pdf->MultiCell(100, 4, $outputlangs->transnoentities($this->emetteur->name), 0, 'L');
  279. $pdf->SetFont('', 'B', $default_font_size + 3);
  280. $pdf->SetXY($posx, $posy);
  281. $pdf->SetTextColor(0, 0, 60);
  282. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("BAN")." ".$outputlangs->convToOutputCharset($object->ref), '', 'R');
  283. $pdf->SetFont('', '', $default_font_size + 2);
  284. $posy += 6;
  285. $pdf->SetXY($posx, $posy);
  286. $pdf->SetTextColor(0, 0, 60);
  287. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("Date")." : ".dol_print_date(dol_now(), 'day', false, $outputlangs, true), '', 'R');
  288. /*$posy+=6;
  289. $pdf->SetXY($posx,$posy);
  290. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("DateEnd")." : " . dol_print_date($object->date_end,'day',false,$outputlangs,true), '', 'R');
  291. */
  292. $pdf->SetTextColor(0, 0, 60);
  293. // Add list of linked objects
  294. /* Removed: A project can have more than thousands linked objects (orders, invoices, proposals, etc....
  295. $object->fetchObjectLinked();
  296. foreach($object->linkedObjects as $objecttype => $objects)
  297. {
  298. var_dump($objects);exit;
  299. if ($objecttype == 'commande')
  300. {
  301. $outputlangs->load('orders');
  302. $num=count($objects);
  303. for ($i=0;$i<$num;$i++)
  304. {
  305. $posy+=4;
  306. $pdf->SetXY($posx,$posy);
  307. $pdf->SetFont('','', $default_font_size - 1);
  308. $text=$objects[$i]->ref;
  309. if ($objects[$i]->ref_client) $text.=' ('.$objects[$i]->ref_client.')';
  310. $pdf->MultiCell(100, 4, $outputlangs->transnoentities("RefOrder")." : ".$outputlangs->transnoentities($text), '', 'R');
  311. }
  312. }
  313. }
  314. */
  315. }
  316. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  317. /**
  318. * Show footer of page. Need this->emetteur object
  319. *
  320. * @param TCPDF $pdf PDF
  321. * @param Project $object Object to show
  322. * @param Translate $outputlangs Object lang for output
  323. * @param int $hidefreetext 1=Hide free text
  324. * @return integer
  325. */
  326. protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
  327. {
  328. // phpcs:enable
  329. global $conf;
  330. $showdetails = $conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS;
  331. //return pdf_pagefoot($pdf,$outputlangs,'BANK_FREE_TEXT',$this->emetteur,$this->marge_basse,$this->marge_gauche,$this->page_hauteur,$object,$showdetails,$hidefreetext);
  332. }
  333. }