dolibarr  7.0.0-beta
pdf_baleine.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2012 Regis Houssin <regis.houssin@capnetworks.com>
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 <http://www.gnu.org/licenses/>.
16  * or see http://www.gnu.org/
17  */
18 
26 require_once DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php';
27 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32 
33 
39 {
40  var $emetteur; // Objet societe qui emet
41 
47  function __construct($db)
48  {
49  global $conf,$langs,$mysoc;
50 
51  $langs->load("main");
52  $langs->load("projects");
53  $langs->load("companies");
54 
55  $this->db = $db;
56  $this->name = "baleine";
57  $this->description = $langs->trans("DocumentModelBaleine");
58 
59  // Dimension page pour format A4
60  $this->type = 'pdf';
61  $formatarray=pdf_getFormat();
62  $this->page_largeur = $formatarray['width'];
63  $this->page_hauteur = $formatarray['height'];
64  $this->format = array($this->page_largeur,$this->page_hauteur);
65  $this->marge_gauche=isset($conf->global->MAIN_PDF_MARGIN_LEFT)?$conf->global->MAIN_PDF_MARGIN_LEFT:10;
66  $this->marge_droite=isset($conf->global->MAIN_PDF_MARGIN_RIGHT)?$conf->global->MAIN_PDF_MARGIN_RIGHT:10;
67  $this->marge_haute =isset($conf->global->MAIN_PDF_MARGIN_TOP)?$conf->global->MAIN_PDF_MARGIN_TOP:10;
68  $this->marge_basse =isset($conf->global->MAIN_PDF_MARGIN_BOTTOM)?$conf->global->MAIN_PDF_MARGIN_BOTTOM:10;
69 
70  $this->option_logo = 1; // Affiche logo FAC_PDF_LOGO
71  $this->option_tva = 1; // Gere option tva FACTURE_TVAOPTION
72  $this->option_codeproduitservice = 1; // Affiche code produit-service
73 
74  // Recupere emmetteur
75  $this->emetteur=$mysoc;
76  if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // By default if not defined
77 
78  // Defini position des colonnes
79  $this->posxref=$this->marge_gauche+1;
80  $this->posxlabel=$this->marge_gauche+25;
81  $this->posxworkload=$this->marge_gauche+100;
82  $this->posxprogress=$this->marge_gauche+130;
83  $this->posxdatestart=$this->marge_gauche+150;
84  $this->posxdateend=$this->marge_gauche+170;
85  }
86 
87 
95  function write_file($object,$outputlangs)
96  {
97  global $conf, $hookmanager, $langs, $user;
98 
99  if (! is_object($outputlangs)) $outputlangs=$langs;
100  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
101  if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
102 
103  $outputlangs->load("main");
104  $outputlangs->load("dict");
105  $outputlangs->load("companies");
106  $outputlangs->load("projects");
107 
108  if ($conf->projet->dir_output)
109  {
110  //$nblignes = count($object->lines); // This is set later with array of tasks
111 
112  $objectref = dol_sanitizeFileName($object->ref);
113  $dir = $conf->projet->dir_output;
114  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
115  $file = $dir . "/" . $objectref . ".pdf";
116 
117  if (! file_exists($dir))
118  {
119  if (dol_mkdir($dir) < 0)
120  {
121  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
122  return 0;
123  }
124  }
125 
126  if (file_exists($dir))
127  {
128  // Add pdfgeneration hook
129  if (! is_object($hookmanager))
130  {
131  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
132  $hookmanager=new HookManager($this->db);
133  }
134  $hookmanager->initHooks(array('pdfgeneration'));
135  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
136  global $action;
137  $reshook=$hookmanager->executeHooks('beforePDFCreation',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
138 
139  $pdf=pdf_getInstance($this->format);
140  $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance
141  $heightforinfotot = 50; // Height reserved to output the info and total part
142  $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
143  $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
144  $pdf->SetAutoPageBreak(1,0);
145 
146  if (class_exists('TCPDF'))
147  {
148  $pdf->setPrintHeader(false);
149  $pdf->setPrintFooter(false);
150  }
151  $pdf->SetFont(pdf_getPDFFont($outputlangs));
152 
153  // Complete object by loading several other informations
154  $task = new Task($this->db);
155  $tasksarray = $task->getTasksArray(0,0,$object->id);
156 
157  if (! $object->id > 0) // Special case when used with object = specimen, we may return all lines
158  {
159  $tasksarray=array_slice($tasksarray, 0, min(5, count($tasksarray)));
160  }
161 
162  $object->lines=$tasksarray;
163  $nblignes=count($object->lines);
164 
165  $pdf->Open();
166  $pagenb=0;
167  $pdf->SetDrawColor(128,128,128);
168 
169  $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
170  $pdf->SetSubject($outputlangs->transnoentities("Project"));
171  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
172  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
173  $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("Project"));
174  if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
175 
176  $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
177 
178  // New page
179  $pdf->AddPage();
180  $pagenb++;
181  $this->_pagehead($pdf, $object, 1, $outputlangs);
182  $pdf->SetFont('','', $default_font_size - 1);
183  $pdf->MultiCell(0, 3, ''); // Set interline to 3
184  $pdf->SetTextColor(0,0,0);
185 
186  $tab_top = 50;
187  $tab_height = 200;
188  $tab_top_newpage = 40;
189  $tab_height_newpage = 210;
190 
191  // Affiche notes
192  if (! empty($object->note_public))
193  {
194  $pdf->SetFont('','', $default_font_size - 1);
195  $pdf->writeHTMLCell(190, 3, $this->posxref-1, $tab_top-2, dol_htmlentitiesbr($object->note_public), 0, 1);
196  $nexY = $pdf->GetY();
197  $height_note=$nexY-($tab_top-2);
198 
199  // Rect prend une longueur en 3eme param
200  $pdf->SetDrawColor(192,192,192);
201  $pdf->Rect($this->marge_gauche, $tab_top-3, $this->page_largeur-$this->marge_gauche-$this->marge_droite, $height_note+1);
202 
203  $tab_height = $tab_height - $height_note;
204  $tab_top = $nexY+6;
205  }
206  else
207  {
208  $height_note=0;
209  }
210 
211  $iniY = $tab_top + 7;
212  $curY = $tab_top + 7;
213  $nexY = $tab_top + 7;
214 
215  // Boucle sur les lignes
216  for ($i = 0 ; $i < $nblignes ; $i++)
217  {
218  $curY = $nexY;
219 
220  // Description of ligne
221  $ref=$object->lines[$i]->ref;
222  $libelleline=$object->lines[$i]->label;
223  $progress=$object->lines[$i]->progress.'%';
224  $datestart=dol_print_date($object->lines[$i]->date_start,'day');
225  $dateend=dol_print_date($object->lines[$i]->date_end,'day');
226  $planned_workload=convertSecondToTime((int) $object->lines[$i]->planned_workload,'allhourmin');
227 
228  $pdf->SetFont('','', $default_font_size - 1); // Dans boucle pour gerer multi-page
229 
230  $pdf->SetXY($this->posxref, $curY);
231  $pdf->MultiCell($this->posxlabel-$this->posxref, 3, $outputlangs->convToOutputCharset($ref), 0, 'L');
232  $pdf->SetXY($this->posxlabel, $curY);
233  $pdf->MultiCell($this->posxworkload-$this->posxlabel, 3, $outputlangs->convToOutputCharset($libelleline), 0, 'L');
234  $pdf->SetXY($this->posxworkload, $curY);
235  $pdf->MultiCell($this->posxprogress-$this->posxworkload, 3, $planned_workload, 0, 'R');
236  $pdf->SetXY($this->posxprogress, $curY);
237  $pdf->MultiCell($this->posxdatestart-$this->posxprogress, 3, $progress, 0, 'R');
238 
239  $pdf->SetXY($this->posxdatestart, $curY);
240  $pdf->MultiCell($this->posxdateend-$this->posxdatestart, 3, $datestart, 0, 'C');
241  $pdf->SetXY($this->posxdateend, $curY);
242  $pdf->MultiCell($this->page_largeur-$this->marge_droite-$this->posxdateend, 3, $dateend, 0, 'C');
243 
244  $pageposafter=$pdf->getPage();
245 
246  $pdf->SetFont('','', $default_font_size - 1); // On repositionne la police par defaut
247  $nexY = $pdf->GetY();
248 
249  // Add line
250  if (! empty($conf->global->MAIN_PDF_DASH_BETWEEN_LINES) && $i < ($nblignes - 1))
251  {
252  $pdf->setPage($pageposafter);
253  $pdf->SetLineStyle(array('dash'=>'1,1','color'=>array(80,80,80)));
254  //$pdf->SetDrawColor(190,190,200);
255  $pdf->line($this->marge_gauche, $nexY+1, $this->page_largeur - $this->marge_droite, $nexY+1);
256  $pdf->SetLineStyle(array('dash'=>0));
257  }
258 
259  $nexY+=2; // Passe espace entre les lignes
260 
261  // Detect if some page were added automatically and output _tableau for past pages
262  while ($pagenb < $pageposafter)
263  {
264  $pdf->setPage($pagenb);
265  if ($pagenb == 1)
266  {
267  $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
268  }
269  else
270  {
271  $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
272  }
273  $this->_pagefoot($pdf,$object,$outputlangs,1);
274  $pagenb++;
275  $pdf->setPage($pagenb);
276  $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it.
277  }
278  if (isset($object->lines[$i+1]->pagebreak) && $object->lines[$i+1]->pagebreak)
279  {
280  if ($pagenb == 1)
281  {
282  $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
283  }
284  else
285  {
286  $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
287  }
288  $this->_pagefoot($pdf,$object,$outputlangs,1);
289  // New page
290  $pdf->AddPage();
291  if (! empty($tplidx)) $pdf->useTemplate($tplidx);
292  $pagenb++;
293  }
294  }
295 
296  // Show square
297  if ($pagenb == 1)
298  {
299  $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
300  $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
301  }
302  else
303  {
304  $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0);
305  $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
306  }
307 
308  /*
309  * Pied de page
310  */
311  $this->_pagefoot($pdf,$object,$outputlangs);
312  if (method_exists($pdf,'AliasNbPages')) $pdf->AliasNbPages();
313 
314  $pdf->Close();
315 
316  $pdf->Output($file,'F');
317 
318  // Add pdfgeneration hook
319  if (! is_object($hookmanager))
320  {
321  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
322  $hookmanager=new HookManager($this->db);
323  }
324  $hookmanager->initHooks(array('pdfgeneration'));
325  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
326  global $action;
327  $reshook=$hookmanager->executeHooks('afterPDFCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
328 
329  if (! empty($conf->global->MAIN_UMASK))
330  @chmod($file, octdec($conf->global->MAIN_UMASK));
331 
332  $this->result = array('fullpath'=>$file);
333 
334  return 1; // Pas d'erreur
335  }
336  else
337  {
338  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
339  return 0;
340  }
341  }
342 
343  $this->error=$langs->transnoentities("ErrorConstantNotDefined","LIVRAISON_OUTPUTDIR");
344  return 0;
345  }
346 
347 
360  function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop=0, $hidebottom=0)
361  {
362  global $conf,$mysoc;
363 
364  $default_font_size = pdf_getPDFFontSize($outputlangs);
365 
366  $pdf->SetDrawColor(128,128,128);
367 
368  // Rect prend une longueur en 3eme param
369  $pdf->Rect($this->marge_gauche, $tab_top, $this->page_largeur-$this->marge_gauche-$this->marge_droite, $tab_height);
370  // line prend une position y en 3eme param
371  $pdf->line($this->marge_gauche, $tab_top+6, $this->page_largeur-$this->marge_droite, $tab_top+6);
372 
373  $pdf->SetTextColor(0,0,0);
374  $pdf->SetFont('','', $default_font_size);
375 
376  $pdf->SetXY($this->posxref, $tab_top+1);
377  $pdf->MultiCell($this->posxlabel-$this->posxref,3, $outputlangs->transnoentities("Tasks"),'','L');
378 
379  $pdf->SetXY($this->posxlabel, $tab_top+1);
380  $pdf->MultiCell($this->posxworkload-$this->posxlabel, 3, $outputlangs->transnoentities("Description"), 0, 'L');
381 
382  $pdf->SetXY($this->posxworkload, $tab_top+1);
383  $pdf->MultiCell($this->posxprogress-$this->posxworkload, 3, $outputlangs->transnoentities("PlannedWorkloadShort"), 0, 'R');
384 
385  $pdf->SetXY($this->posxprogress, $tab_top+1);
386  $pdf->MultiCell($this->posxdatestart-$this->posxprogress, 3, '%', 0, 'R');
387 
388  $pdf->SetXY($this->posxdatestart, $tab_top+1);
389  $pdf->MultiCell($this->posxdateend-$this->posxdatestart, 3, '', 0, 'C');
390 
391  $pdf->SetXY($this->posxdateend, $tab_top+1);
392  $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->posxdatestart, 3, '', 0, 'C');
393 
394  }
395 
405  function _pagehead(&$pdf, $object, $showaddress, $outputlangs)
406  {
407  global $langs,$conf,$mysoc;
408 
409  $default_font_size = pdf_getPDFFontSize($outputlangs);
410 
411  pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
412 
413  $pdf->SetTextColor(0,0,60);
414  $pdf->SetFont('','B', $default_font_size + 3);
415 
416  $posx=$this->page_largeur-$this->marge_droite-100;
417  $posy=$this->marge_haute;
418 
419  $pdf->SetXY($this->marge_gauche,$posy);
420 
421  // Logo
422  $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
423  if ($mysoc->logo)
424  {
425  if (is_readable($logo))
426  {
427  $height=pdf_getHeightForLogo($logo);
428  $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
429  }
430  else
431  {
432  $pdf->SetTextColor(200,0,0);
433  $pdf->SetFont('','B', $default_font_size - 2);
434  $pdf->MultiCell(100, 3, $langs->transnoentities("ErrorLogoFileNotFound",$logo), 0, 'L');
435  $pdf->MultiCell(100, 3, $langs->transnoentities("ErrorGoToModuleSetup"), 0, 'L');
436  }
437  }
438  else $pdf->MultiCell(100, 4, $outputlangs->transnoentities($this->emetteur->name), 0, 'L');
439 
440  $pdf->SetFont('','B', $default_font_size + 3);
441  $pdf->SetXY($posx,$posy);
442  $pdf->SetTextColor(0,0,60);
443  $pdf->MultiCell(100, 4, $outputlangs->transnoentities("Project")." ".$outputlangs->convToOutputCharset($object->ref), '', 'R');
444  $pdf->SetFont('','', $default_font_size + 2);
445 
446  $posy+=6;
447  $pdf->SetXY($posx,$posy);
448  $pdf->SetTextColor(0,0,60);
449  $pdf->MultiCell(100, 4, $outputlangs->transnoentities("DateStart")." : " . dol_print_date($object->date_start,'day',false,$outputlangs,true), '', 'R');
450  $posy+=6;
451  $pdf->SetXY($posx,$posy);
452  $pdf->MultiCell(100, 4, $outputlangs->transnoentities("DateEnd")." : " . dol_print_date($object->date_end,'day',false,$outputlangs,true), '', 'R');
453 
454  $pdf->SetTextColor(0,0,60);
455 
456  // Add list of linked objects
457  /* Removed: A project can have more than thousands linked objects (orders, invoices, proposals, etc....
458  $object->fetchObjectLinked();
459 
460  foreach($object->linkedObjects as $objecttype => $objects)
461  {
462  var_dump($objects);exit;
463  if ($objecttype == 'commande')
464  {
465  $outputlangs->load('orders');
466  $num=count($objects);
467  for ($i=0;$i<$num;$i++)
468  {
469  $posy+=4;
470  $pdf->SetXY($posx,$posy);
471  $pdf->SetFont('','', $default_font_size - 1);
472  $text=$objects[$i]->ref;
473  if ($objects[$i]->ref_client) $text.=' ('.$objects[$i]->ref_client.')';
474  $pdf->MultiCell(100, 4, $outputlangs->transnoentities("RefOrder")." : ".$outputlangs->transnoentities($text), '', 'R');
475  }
476  }
477  }
478  */
479 
480  }
481 
491  function _pagefoot(&$pdf,$object,$outputlangs,$hidefreetext=0)
492  {
493  global $conf;
494  $showdetails=$conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS;
495  return pdf_pagefoot($pdf,$outputlangs,'PROJECT_FREE_TEXT',$this->emetteur,$this->marge_basse,$this->marge_gauche,$this->page_hauteur,$object,$showdetails,$hidefreetext);
496  }
497 
498 }
499 
convertSecondToTime($iSecond, $format='all', $lengthOfDay=86400, $lengthOfWeek=7)
Return, in clear text, value of a number of seconds in days, hours and minutes.
Definition: date.lib.php:171
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:82
pdf_pagehead(&$pdf, $outputlangs, $page_height)
Show header of page for PDF generation.
Definition: pdf.lib.php:580
</td >< tdclass="liste_titre"align="right"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="'.$i.'"'.$disabled.'></td >< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"></td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:554
pdf_getFormat(Translate $outputlangs=null)
Return array with format properties of default PDF format.
Definition: pdf.lib.php:42
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
write_file($object, $outputlangs)
Fonction generant le projet sur le disque.
__construct($db)
Constructor.
pdf_getPDFFontSize($outputlangs)
Return font size to use for PDF generation.
Definition: pdf.lib.php:256
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:233
Class to manage hooks.
Parent class for projects models.
pdf_pagefoot(&$pdf, $outputlangs, $paramfreetext, $fromcompany, $marge_basse, $marge_gauche, $page_hauteur, $object, $showdetails=0, $hidefreetext=0)
Show footer of page for PDF generation.
Definition: pdf.lib.php:852
pdf_getHeightForLogo($logo, $url=false)
Return height to use for Logo onto PDF.
Definition: pdf.lib.php:277
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:104
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class to manage tasks.
Definition: task.class.php:32
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
_pagefoot(&$pdf, $object, $outputlangs, $hidefreetext=0)
Show footer of page.
dol_htmlentitiesbr($stringtoencode, $nl2brmode=0, $pagecodefrom='UTF-8', $removelasteolbr=1)
This function is called to encode a string into a HTML string but differs from htmlentities because a...
Classe permettant de generer les projets au modele Baleine.
_pagehead(&$pdf, $object, $showaddress, $outputlangs)
Show top header of page.
type
Definition: viewcat.php:283
_tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop=0, $hidebottom=0)
Show table for lines.