dolibarr  9.0.0
rapport.pdf.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
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 <http://www.gnu.org/licenses/>.
18  * or see http://www.gnu.org/
19  */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
32 
37 {
41  public $db;
42 
46  public $description;
47 
48  public $date_edition;
49 
50  public $year;
51 
52  public $month;
53 
54  public $title;
55 
56  public $subject;
57 
58  public $marge_gauche;
59 
60  public $marge_droite;
61 
62  public $marge_haute;
63 
64  public $marge_basse;
65 
66 
74  function __construct($db, $month, $year)
75  {
76  global $conf, $langs;
77 
78  // Load translation files required by the page
79  $langs->loadLangs(array("commercial","projects"));
80 
81  $this->db = $db;
82  $this->description = "";
83  $this->date_edition = time();
84  $this->month = $month;
85  $this->year = $year;
86 
87  // Dimension page pour format A4
88  $this->type = 'pdf';
89  $formatarray=pdf_getFormat();
90  $this->page_largeur = $formatarray['width'];
91  $this->page_hauteur = $formatarray['height'];
92  $this->format = array($this->page_largeur,$this->page_hauteur);
93  $this->marge_gauche=isset($conf->global->MAIN_PDF_MARGIN_LEFT)?$conf->global->MAIN_PDF_MARGIN_LEFT:10;
94  $this->marge_droite=isset($conf->global->MAIN_PDF_MARGIN_RIGHT)?$conf->global->MAIN_PDF_MARGIN_RIGHT:10;
95  $this->marge_haute =isset($conf->global->MAIN_PDF_MARGIN_TOP)?$conf->global->MAIN_PDF_MARGIN_TOP:10;
96  $this->marge_basse =isset($conf->global->MAIN_PDF_MARGIN_BOTTOM)?$conf->global->MAIN_PDF_MARGIN_BOTTOM:10;
97 
98  $this->title=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
99  $this->subject=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
100  }
101 
102  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
111  function write_file($socid = 0, $catid = 0, $outputlangs='')
112  {
113  // phpcs:enable
114  global $user,$conf,$langs,$hookmanager;
115 
116  if (! is_object($outputlangs)) $outputlangs=$langs;
117  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
118  if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
119 
120  // Load traductions files requiredby by page
121  $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products"));
122 
123  $dir = $conf->agenda->dir_temp."/";
124  $file = $dir . "actions-".$this->month."-".$this->year.".pdf";
125 
126  if (! file_exists($dir))
127  {
128  if (dol_mkdir($dir) < 0)
129  {
130  $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
131  return 0;
132  }
133  }
134 
135  if (file_exists($dir))
136  {
137  // Add pdfgeneration hook
138  if (! is_object($hookmanager))
139  {
140  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
141  $hookmanager=new HookManager($this->db);
142  }
143  $hookmanager->initHooks(array('pdfgeneration'));
144  $parameters=array('file'=>$file, 'outputlangs'=>$outputlangs);
145  global $action;
146  $reshook=$hookmanager->executeHooks('beforePDFCreation',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
147 
148  $pdf=pdf_getInstance($this->format);
149  $heightforinfotot = 50; // Height reserved to output the info and total part
150  $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
151  $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
152  $pdf->SetAutoPageBreak(1,0);
153 
154  if (class_exists('TCPDF'))
155  {
156  $pdf->setPrintHeader(false);
157  $pdf->setPrintFooter(false);
158  }
159  $pdf->SetFont(pdf_getPDFFont($outputlangs));
160 
161  $pdf->Open();
162  $pagenb=0;
163  $pdf->SetDrawColor(128,128,128);
164  $pdf->SetFillColor(220,220,220);
165 
166  $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
167  $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
168  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
169  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
170  $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
171 
172  $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
173 
174  $nbpage = $this->_pages($pdf, $outputlangs);
175 
176  if (method_exists($pdf,'AliasNbPages')) $pdf->AliasNbPages();
177  $pdf->Close();
178 
179  $pdf->Output($file,'F');
180 
181  // Add pdfgeneration hook
182  if (! is_object($hookmanager))
183  {
184  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
185  $hookmanager=new HookManager($this->db);
186  }
187  $hookmanager->initHooks(array('pdfgeneration'));
188  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
189  global $action;
190  $reshook=$hookmanager->executeHooks('afterPDFCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
191 
192  if (! empty($conf->global->MAIN_UMASK))
193  @chmod($file, octdec($conf->global->MAIN_UMASK));
194 
195  $this->result = array('fullpath'=>$file);
196 
197  return 1;
198  }
199  }
200 
208  function _pages(&$pdf, $outputlangs)
209  {
210  global $conf;
211 
212  $height=3; // height for text separation
213  $pagenb=1;
214 
215  $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
216  $y++;
217  $pdf->SetFont('','',8);
218 
219  $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
220  $sql.= " a.id, a.datep as dp, a.datep2 as dp2,";
221  $sql.= " a.fk_contact, a.note, a.percent as percent, a.label, a.fk_project,";
222  $sql.= " c.code, c.libelle,";
223  $sql.= " u.login";
224  $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
225  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
226  $sql.= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
227  $sql.= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year,$this->month,false))."'";
228  $sql.= " AND '".$this->db->idate(dol_get_last_day($this->year,$this->month,false))."'";
229  $sql.= " AND a.entity = ".$conf->entity;
230  $sql.= " ORDER BY a.datep DESC";
231 
232  $eventstatic=new ActionComm($this->db);
233  $projectstatic=new Project($this->db);
234 
235  dol_syslog(get_class($this)."::_page", LOG_DEBUG);
236  $resql=$this->db->query($sql);
237  if ($resql)
238  {
239  $num = $this->db->num_rows($resql);
240  $i = 0;
241  $y0=$y1=$y2=$y3=0;
242 
243  while ($i < $num)
244  {
245  $obj = $this->db->fetch_object($resql);
246 
247  $eventstatic->id=$obj->id;
248  $eventstatic->percentage=$obj->percent;
249  $eventstatic->fulldayevent=$obj->fulldayevent;
250  $eventstatic->punctual=$obj->punctual;
251 
252  $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
253 
254  // Calculate height of text
255  $text='';
256  if (! preg_match('/^'.preg_quote($obj->label).'/',$obj->note)) $text=$obj->label."\n";
257  $text.=$obj->note;
258  $text=dol_trunc(dol_htmlentitiesbr_decode($text),150);
259  // Add status to text
260  $text.="\n";
261  $status=dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1,1));
262  $text.=$status;
263  if ($obj->fk_project > 0)
264  {
265  $projectstatic->fetch($obj->fk_project);
266  $text.=($status?' - ':'').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->getNomUrl(0, 'nolink'));
267  }
268 
269  //print 'd'.$text; exit;
270  $nboflines=dol_nboflines($text);
271  $heightlinemax=max(2*$height,$nboflines*$height);
272  // Check if there is enough space to print record
273  if ((1+$y+$heightlinemax) >= ($this->page_hauteur - $this->marge_haute))
274  {
275  // We need to break page
276  $pagenb++;
277  $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
278  $y++;
279  $pdf->SetFont('','',8);
280  }
281  $y++;
282 
283  // Date
284  $pdf->SetXY($this->marge_gauche, $y);
285  $textdate = dol_print_date($this->db->jdate($obj->dp),"day")."\n".dol_print_date($this->db->jdate($obj->dp),"hour");
286  if ($obj->dp2) {
287  if (dol_print_date($this->db->jdate($obj->dp),"day") != dol_print_date($this->db->jdate($obj->dp2),"day"))
288  $textdate.= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
289  else
290  $textdate.= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
291  }
292  $pdf->MultiCell(22, $height, $textdate, 0, 'L', 0);
293  $y0 = $pdf->GetY();
294 
295  // Third party
296  $pdf->SetXY(26, $y);
297  $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty),32), 0, 'L', 0);
298  $y1 = $pdf->GetY();
299 
300  // Action code
301  $code=$obj->code;
302  if (empty($conf->global->AGENDA_USE_EVENT_TYPE))
303  {
304  if ($code == 'AC_OTH') $code='AC_MANUAL';
305  if ($code == 'AC_OTH_AUTO') $code='AC_AUTO';
306  }
307  $pdf->SetXY(60,$y);
308  $labelactiontype = $outputlangs->transnoentitiesnoconv("Action".$code);
309  $labelactiontypeshort = $outputlangs->transnoentitiesnoconv("Action".$code.'Short');
310  $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($labelactiontypeshort == "Action".$code.'Short' ? $labelactiontype : $labelactiontypeshort),32), 0, 'L', 0);
311  $y2 = $pdf->GetY();
312 
313  // Description of event
314  $pdf->SetXY(106,$y);
315  $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset($text), 0, 'L', 0);
316  $y3 = $pdf->GetY();
317 
318  $i++;
319  }
320  }
321 
322  return 1;
323  }
324 
333  function _pagehead(&$pdf, $outputlangs, $pagenb)
334  {
335  global $conf,$langs;
336 
337  // Do not add the BACKGROUND as this is a report
338  //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
339 
340  // New page
341  $pdf->AddPage();
342 
343  // Show title
344  $pdf->SetFont('','B',10);
345  $pdf->SetXY($this->marge_gauche, $this->marge_haute);
346  $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', 0);
347  // Show page nb only on iso languages (so default Helvetica font)
348  if (pdf_getPDFFont($outputlangs) == 'Helvetica')
349  {
350  $pdf->SetXY($this->page_largeur-$this->marge_droite-40, $this->marge_haute);
351  $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
352  }
353 
354  $y=$pdf->GetY()+2;
355 
356  $pdf->Rect($this->marge_gauche, $y, ($this->page_largeur - $this->marge_gauche - $this->marge_droite), ($this->page_hauteur - $this->marge_haute - $this->marge_basse));
357  $y=$pdf->GetY()+1;
358 
359  return $y;
360  }
361 }
dol_htmlentitiesbr_decode($stringtodecode, $pagecodeto='UTF-8')
This function is called to decode a HTML string (it decodes entities and br tags) ...
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:82
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
Class to manage agenda events (actions)
</td >< td class="liste_titre" align="right"></td ></tr >< tr class="liste_titre">< input type="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< tr class="oddeven">< td >< input type="checkbox" class="check" name="' . $i . '"' . $disabled . '></td >< td >< input type="checkbox" class="check" name="choose'.$i.'"></td >< td class="nowrap"></td >< td >< input type="hidden" name="desc' . $i . '" value="' . dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:573
pdf_getFormat(Translate $outputlangs=null)
Return array with format properties of default PDF format.
Definition: pdf.lib.php:42
_pagehead(&$pdf, $outputlangs, $pagenb)
Show top header of page.
_pages(&$pdf, $outputlangs)
Write content of pages.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:453
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:201
dol_nboflines($s, $maxchar=0)
Return nb of lines of a clear text.
Class to manage hooks.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
Class to manage projects.
__construct($db, $month, $year)
Constructor.
Definition: rapport.pdf.php:74
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:467
write_file($socid=0, $catid=0, $outputlangs='')
Write the object to document file to disk.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
Class to generate event report.
Definition: rapport.pdf.php:36