dolibarr  17.0.4
pdf_blochet.class.php
Go to the documentation of this file.
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 
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/modules/cheque/modules_chequereceipts.php';
30 
31 
36 {
41  public $emetteur;
42 
48  public function __construct($db)
49  {
50  global $conf, $langs, $mysoc;
51 
52  // Load traductions files required by page
53  $langs->loadLangs(array("main", "bills"));
54 
55  $this->db = $db;
56  $this->name = "blochet";
57 
58  $this->tab_top = 60;
59 
60  // Page size for A4 format
61  $this->type = 'pdf';
62  $formatarray = pdf_getFormat();
63  $this->page_largeur = $formatarray['width'];
64  $this->page_hauteur = $formatarray['height'];
65  $this->format = array($this->page_largeur, $this->page_hauteur);
66  $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
67  $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
68  $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
69  $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
70 
71  // Retrieves transmitter
72  $this->emetteur = $mysoc;
73  if (!$this->emetteur->country_code) {
74  $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined
75  }
76 
77  // Define column position
78  $this->line_height = 5;
79  $this->line_per_page = 40;
80  $this->tab_height = 200; //$this->line_height * $this->line_per_page;
81  }
82 
83  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
93  public function write_file($object, $_dir, $number, $outputlangs)
94  {
95  // phpcs:enable
96  global $user, $conf, $langs, $hookmanager;
97 
98  if (!is_object($outputlangs)) {
99  $outputlangs = $langs;
100  }
101  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
102  $sav_charset_output = $outputlangs->charset_output;
103  if (!empty($conf->global->MAIN_USE_FPDF)) {
104  $outputlangs->charset_output = 'ISO-8859-1';
105  }
106 
107  // Load traductions files required by page
108  $outputlangs->loadLangs(array("main", "companies", "bills", "products", "compta"));
109 
110  $dir = $_dir."/".get_exdir($number, 0, 1, 0, $object, 'checkdeposits');
111 
112  if (!is_dir($dir)) {
113  $result = dol_mkdir($dir);
114 
115  if ($result < 0) {
116  $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
117  return -1;
118  }
119  }
120 
121  $file = $dir."/bordereau-".$number.".pdf";
122 
123  // Add pdfgeneration hook
124  if (!is_object($hookmanager)) {
125  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
126  $hookmanager = new HookManager($this->db);
127  }
128  $hookmanager->initHooks(array('pdfgeneration'));
129  $parameters = array('file'=>$file, 'outputlangs'=>$outputlangs);
130  global $action;
131  $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
132 
133  // Create PDF instance
134  $pdf = pdf_getInstance($this->format);
135  $heightforinfotot = 50; // Height reserved to output the info and total part
136  $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
137  $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
138  if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
139  $heightforfooter += 6;
140  }
141  $pdf->SetAutoPageBreak(1, 0);
142 
143  if (class_exists('TCPDF')) {
144  $pdf->setPrintHeader(false);
145  $pdf->setPrintFooter(false);
146  }
147  $pdf->SetFont(pdf_getPDFFont($outputlangs));
148 
149  $pdf->Open();
150  $pagenb = 0;
151  $pdf->SetDrawColor(128, 128, 128);
152 
153  $pdf->SetTitle($outputlangs->transnoentities("CheckReceipt")." ".$number);
154  $pdf->SetSubject($outputlangs->transnoentities("CheckReceipt"));
155  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
156  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
157  $pdf->SetKeyWords($outputlangs->transnoentities("CheckReceipt")." ".$number);
158  if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
159  $pdf->SetCompression(false);
160  }
161 
162  $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
163 
164  $nboflines = count($this->lines);
165 
166  // Define nb of page
167  $pages = intval($nboflines / $this->line_per_page);
168  if (($nboflines % $this->line_per_page) > 0) {
169  $pages++;
170  }
171  if ($pages == 0) {
172  // force to build at least one page if report has no lines
173  $pages = 1;
174  }
175 
176  $pdf->AddPage();
177  $pagenb++;
178  $this->Header($pdf, $pagenb, $pages, $outputlangs);
179 
180  $this->Body($pdf, $pagenb, $pages, $outputlangs);
181 
182  // Pied de page
183  $this->_pagefoot($pdf, '', $outputlangs);
184  if (method_exists($pdf, 'AliasNbPages')) {
185  $pdf->AliasNbPages();
186  }
187 
188  $pdf->Close();
189 
190  $pdf->Output($file, 'F');
191 
192  // Add pdfgeneration hook
193  if (!is_object($hookmanager)) {
194  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
195  $hookmanager = new HookManager($this->db);
196  }
197  $hookmanager->initHooks(array('pdfgeneration'));
198  $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
199  global $action;
200  $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
201  if ($reshook < 0) {
202  $this->error = $hookmanager->error;
203  $this->errors = $hookmanager->errors;
204  }
205 
206  if (!empty($conf->global->MAIN_UMASK)) {
207  @chmod($file, octdec($conf->global->MAIN_UMASK));
208  }
209 
210  $this->result = array('fullpath'=>$file);
211 
212  $outputlangs->charset_output = $sav_charset_output;
213  return 1; // No error
214  }
215 
216 
217  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
227  public function Header(&$pdf, $page, $pages, $outputlangs)
228  {
229  // phpcs:enable
230  global $langs;
231  $default_font_size = pdf_getPDFFontSize($outputlangs);
232 
233  // Load traductions files required by page
234  $outputlangs->loadLangs(array("compta", "banks"));
235 
236  $title = $outputlangs->transnoentities("CheckReceipt");
237  $pdf->SetFont('', 'B', $default_font_size);
238  $pdf->SetXY(10, 8);
239  $pdf->MultiCell(0, 2, $title, 0, 'L');
240 
241  $pdf->SetFont('', '', $default_font_size);
242  $pdf->SetXY(10, 15);
243  $pdf->MultiCell(22, 2, $outputlangs->transnoentities("Ref"), 0, 'L');
244  $pdf->SetXY(32, 15);
245  $pdf->SetFont('', '', $default_font_size);
246  $pdf->MultiCell(60, 2, $outputlangs->convToOutputCharset($this->ref.($this->ref_ext ? " - ".$this->ref_ext : '')), 0, 'L');
247 
248  $pdf->SetFont('', '', $default_font_size);
249  $pdf->SetXY(10, 20);
250  $pdf->MultiCell(22, 2, $outputlangs->transnoentities("Date"), 0, 'L');
251  $pdf->SetXY(32, 20);
252  $pdf->SetFont('', '', $default_font_size);
253  $pdf->MultiCell(60, 2, dol_print_date($this->date, "day", false, $outputlangs));
254 
255  $pdf->SetFont('', '', $default_font_size);
256  $pdf->SetXY(10, 26);
257  $pdf->MultiCell(22, 2, $outputlangs->transnoentities("Owner"), 0, 'L');
258  $pdf->SetFont('', '', $default_font_size);
259  $pdf->SetXY(32, 26);
260  $pdf->MultiCell(80, 2, $outputlangs->convToOutputCharset($this->account->proprio), 0, 'L');
261 
262  $pdf->SetFont('', '', $default_font_size);
263  $pdf->SetXY(10, 32);
264  $pdf->MultiCell(0, 2, $outputlangs->transnoentities("Account"), 0, 'L');
265  pdf_bank($pdf, $outputlangs, 32, 32, $this->account, 1);
266 
267  $pdf->SetFont('', '', $default_font_size);
268  $pdf->SetXY(114, 15);
269  $pdf->MultiCell(40, 2, $outputlangs->transnoentities("Signature"), 0, 'L');
270 
271  $pdf->Rect(9, 14, 192, 35);
272  $pdf->line(9, 19, 112, 19);
273  $pdf->line(9, 25, 112, 25);
274  //$pdf->line(9, 31, 201, 31);
275  $pdf->line(9, 31, 112, 31);
276 
277  $pdf->line(30, 14, 30, 49);
278  $pdf->line(112, 14, 112, 49);
279 
280  // Number of cheques
281  $posy = 51;
282  $pdf->Rect(9, $posy, 192, 6);
283  $pdf->line(55, $posy, 55, $posy + 6);
284  $pdf->line(140, $posy, 140, $posy + 6);
285  $pdf->line(170, $posy, 170, $posy + 6);
286 
287  $pdf->SetFont('', '', $default_font_size);
288  $pdf->SetXY(10, $posy + 1);
289  $pdf->MultiCell(40, 2, $outputlangs->transnoentities("NumberOfCheques"), 0, 'L');
290 
291  $pdf->SetFont('', 'B', $default_font_size);
292  $pdf->SetXY(57, $posy + 1);
293  $pdf->MultiCell(40, 2, $this->nbcheque, 0, 'L');
294 
295  $pdf->SetFont('', '', $default_font_size);
296  $pdf->SetXY(148, $posy + 1);
297  $pdf->MultiCell(40, 2, $langs->trans("Total"));
298 
299  $pdf->SetFont('', 'B', $default_font_size);
300  $pdf->SetXY(170, $posy + 1);
301  $pdf->MultiCell(31, 2, price($this->amount), 0, 'C', 0);
302 
303  // Tableau
304  $pdf->SetFont('', '', $default_font_size - 2);
305  $pdf->SetXY(11, $this->tab_top + 2);
306  $pdf->MultiCell(40, 2, $outputlangs->transnoentities("Num"), 0, 'L');
307  $pdf->line(40, $this->tab_top, 40, $this->tab_top + $this->tab_height + 10);
308 
309  $pdf->SetXY(41, $this->tab_top + 2);
310  $pdf->MultiCell(40, 2, $outputlangs->transnoentities("Bank"), 0, 'L');
311  $pdf->line(100, $this->tab_top, 100, $this->tab_top + $this->tab_height + 10);
312 
313  $pdf->SetXY(101, $this->tab_top + 2);
314  $pdf->MultiCell(40, 2, $outputlangs->transnoentities("CheckTransmitter"), 0, 'L');
315  $pdf->line(180, $this->tab_top, 180, $this->tab_top + $this->tab_height + 10);
316 
317  $pdf->SetXY(180, $this->tab_top + 2);
318  $pdf->MultiCell(20, 2, $outputlangs->transnoentities("Amount"), 0, 'R');
319  $pdf->line(9, $this->tab_top + 8, 201, $this->tab_top + 8);
320 
321  $pdf->Rect(9, $this->tab_top, 192, $this->tab_height + 10);
322  }
323 
324 
325  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
335  public function Body(&$pdf, $pagenb, $pages, $outputlangs)
336  {
337  // phpcs:enable
338  // x=10 - Num
339  // x=30 - Banque
340  // x=100 - Emetteur
341  $default_font_size = pdf_getPDFFontSize($outputlangs);
342  $pdf->SetFont('', '', $default_font_size - 1);
343  $oldprowid = 0;
344  $pdf->SetFillColor(220, 220, 220);
345  $yp = 0;
346  $lineinpage = 0;
347  $num = count($this->lines);
348  for ($j = 0; $j < $num; $j++) {
349  // Dynamic max line heigh calculation
350  $dynamic_line_height = array();
351  $dynamic_line_height[] = $pdf->getStringHeight(60, $outputlangs->convToOutputCharset($this->lines[$j]->bank_chq));
352  $dynamic_line_height[] = $pdf->getStringHeight(80, $outputlangs->convToOutputCharset($this->lines[$j]->emetteur_chq));
353  $max_line_height = max($dynamic_line_height);
354  // Calculate number of line used function of estimated line size
355  if ($max_line_height > $this->line_height) {
356  $nb_lines = floor($max_line_height / $this->line_height) + 1;
357  } else {
358  $nb_lines = 1;
359  }
360 
361  // Add page break if we do not have space to add current line
362  if ($lineinpage >= ($this->line_per_page - 1)) {
363  $lineinpage = 0;
364  $yp = 0;
365 
366  // New page
367  $pdf->AddPage();
368  $pagenb++;
369  $this->Header($pdf, $pagenb, $pages, $outputlangs);
370  $pdf->SetFont('', '', $default_font_size - 1);
371  $pdf->MultiCell(0, 3, ''); // Set interline to 3
372  $pdf->SetTextColor(0, 0, 0);
373  }
374 
375  $lineinpage += $nb_lines;
376 
377  $pdf->SetXY(1, $this->tab_top + 10 + $yp);
378  $pdf->MultiCell(8, $this->line_height, $j + 1, 0, 'R', 0);
379 
380  $pdf->SetXY(10, $this->tab_top + 10 + $yp);
381  $pdf->MultiCell(30, $this->line_height, $this->lines[$j]->num_chq ? $this->lines[$j]->num_chq : '', 0, 'L', 0);
382 
383  $pdf->SetXY(40, $this->tab_top + 10 + $yp);
384  $pdf->MultiCell(60, $this->line_height, $outputlangs->convToOutputCharset($this->lines[$j]->bank_chq, 44), 0, 'L', 0);
385 
386  $pdf->SetXY(100, $this->tab_top + 10 + $yp);
387  $pdf->MultiCell(80, $this->line_height, $outputlangs->convToOutputCharset($this->lines[$j]->emetteur_chq, 50), 0, 'L', 0);
388 
389  $pdf->SetXY(180, $this->tab_top + 10 + $yp);
390  $pdf->MultiCell(20, $this->line_height, price($this->lines[$j]->amount_chq), 0, 'R', 0);
391 
392  $yp = $yp + ($this->line_height * $nb_lines);
393  }
394  }
395 
396  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
406  protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0)
407  {
408  global $conf;
409 
410  $showdetails = getDolGlobalInt('MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS', 0);
411 
412  // Line of free text
413  $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object);
414  complete_substitutions_array($substitutionarray, $outputlangs, $object);
415  $newfreetext = '';
416  $paramfreetext = 'BANK_CHEQUERECEIPT_FREE_TEXT';
417  if (!empty($conf->global->$paramfreetext)) {
418  $newfreetext = make_substitutions($conf->global->$paramfreetext, $substitutionarray);
419  }
420 
421  return pdf_pagefoot($pdf, $outputlangs, $newfreetext, $this->emetteur, $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext);
422  }
423 }
Class of file to build cheque deposit receipts.
__construct($db)
Constructor.
Body(&$pdf, $pagenb, $pages, $outputlangs)
Output array.
_pagefoot(&$pdf, $object, $outputlangs, $hidefreetext=0)
Show footer of page.
Header(&$pdf, $page, $pages, $outputlangs)
Generate Header.
write_file($object, $_dir, $number, $outputlangs)
Fonction to generate document on disk.
Class to manage hooks.
Class parent for templates of document generation.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
pdf_getPDFFontSize($outputlangs)
Return font size to use for PDF generation.
Definition: pdf.lib.php:288
pdf_getFormat(Translate $outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
Definition: pdf.lib.php:84
pdf_pagefoot(&$pdf, $outputlangs, $paramfreetext, $fromcompany, $marge_basse, $marge_gauche, $page_hauteur, $object, $showdetails=0, $hidefreetext=0, $page_largeur=0, $watermark='')
Show footer of page for PDF generation.
Definition: pdf.lib.php:1001
pdf_getSubstitutionArray($outputlangs, $exclude=null, $object=null, $onlykey=0)
Return array of possible substitutions for PDF content (without external module substitutions).
Definition: pdf.lib.php:749
pdf_bank(&$pdf, $outputlangs, $curx, $cury, $account, $onlynumber=0, $default_font_size=10)
Show bank informations for PDF generation.
Definition: pdf.lib.php:824
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:265
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:126
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:122
$conf db
API class for accounts.
Definition: inc.php:41