dolibarr  7.0.0-beta
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@capnetworks.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 {
38  var $db;
39  var $description;
40  var $date_edition;
41  var $year;
42  var $month;
43 
44  var $title;
45  var $subject;
46 
47  var $marge_gauche;
48  var $marge_droite;
49  var $marge_haute;
50  var $marge_basse;
51 
52 
60  function __construct($db, $month, $year)
61  {
62  global $conf,$langs;
63  $langs->load("commercial");
64  $langs->load("projects");
65 
66  $this->db = $db;
67  $this->description = "";
68  $this->date_edition = time();
69  $this->month = $month;
70  $this->year = $year;
71 
72  // Dimension page pour format A4
73  $this->type = 'pdf';
74  $formatarray=pdf_getFormat();
75  $this->page_largeur = $formatarray['width'];
76  $this->page_hauteur = $formatarray['height'];
77  $this->format = array($this->page_largeur,$this->page_hauteur);
78  $this->marge_gauche=isset($conf->global->MAIN_PDF_MARGIN_LEFT)?$conf->global->MAIN_PDF_MARGIN_LEFT:10;
79  $this->marge_droite=isset($conf->global->MAIN_PDF_MARGIN_RIGHT)?$conf->global->MAIN_PDF_MARGIN_RIGHT:10;
80  $this->marge_haute =isset($conf->global->MAIN_PDF_MARGIN_TOP)?$conf->global->MAIN_PDF_MARGIN_TOP:10;
81  $this->marge_basse =isset($conf->global->MAIN_PDF_MARGIN_BOTTOM)?$conf->global->MAIN_PDF_MARGIN_BOTTOM:10;
82 
83  $this->title=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
84  $this->subject=$langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
85  }
86 
95  function write_file($socid = 0, $catid = 0, $outputlangs='')
96  {
97  global $user,$conf,$langs,$hookmanager;
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("bills");
107  $outputlangs->load("products");
108 
109  $dir = $conf->agenda->dir_temp."/";
110  $file = $dir . "actions-".$this->month."-".$this->year.".pdf";
111 
112  if (! file_exists($dir))
113  {
114  if (dol_mkdir($dir) < 0)
115  {
116  $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
117  return 0;
118  }
119  }
120 
121  if (file_exists($dir))
122  {
123  // Add pdfgeneration hook
124  if (! is_object($hookmanager))
125  {
126  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
127  $hookmanager=new HookManager($this->db);
128  }
129  $hookmanager->initHooks(array('pdfgeneration'));
130  $parameters=array('file'=>$file, 'outputlangs'=>$outputlangs);
131  global $action;
132  $reshook=$hookmanager->executeHooks('beforePDFCreation',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
133 
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  $pdf->SetAutoPageBreak(1,0);
139 
140  if (class_exists('TCPDF'))
141  {
142  $pdf->setPrintHeader(false);
143  $pdf->setPrintFooter(false);
144  }
145  $pdf->SetFont(pdf_getPDFFont($outputlangs));
146 
147  $pdf->Open();
148  $pagenb=0;
149  $pdf->SetDrawColor(128,128,128);
150  $pdf->SetFillColor(220,220,220);
151 
152  $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
153  $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
154  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
155  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
156  $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
157 
158  $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
159 
160  $nbpage = $this->_pages($pdf, $outputlangs);
161 
162  if (method_exists($pdf,'AliasNbPages')) $pdf->AliasNbPages();
163  $pdf->Close();
164 
165  $pdf->Output($file,'F');
166 
167  // Add pdfgeneration hook
168  if (! is_object($hookmanager))
169  {
170  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
171  $hookmanager=new HookManager($this->db);
172  }
173  $hookmanager->initHooks(array('pdfgeneration'));
174  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs);
175  global $action;
176  $reshook=$hookmanager->executeHooks('afterPDFCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
177 
178  if (! empty($conf->global->MAIN_UMASK))
179  @chmod($file, octdec($conf->global->MAIN_UMASK));
180 
181  $this->result = array('fullpath'=>$file);
182 
183  return 1;
184  }
185  }
186 
194  function _pages(&$pdf, $outputlangs)
195  {
196  global $conf;
197 
198  $height=3; // height for text separation
199  $pagenb=1;
200 
201  $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
202  $y++;
203  $pdf->SetFont('','',8);
204 
205  $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
206  $sql.= " a.id, a.datep as dp, a.datep2 as dp2,";
207  $sql.= " a.fk_contact, a.note, a.percent as percent, a.label, a.fk_project,";
208  $sql.= " c.code, c.libelle,";
209  $sql.= " u.login";
210  $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
211  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
212  $sql.= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
213  $sql.= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year,$this->month,false))."'";
214  $sql.= " AND '".$this->db->idate(dol_get_last_day($this->year,$this->month,false))."'";
215  $sql.= " AND a.entity = ".$conf->entity;
216  $sql.= " ORDER BY a.datep DESC";
217 
218  $eventstatic=new ActionComm($this->db);
219  $projectstatic=new Project($this->db);
220 
221  dol_syslog(get_class($this)."::_page", LOG_DEBUG);
222  $resql=$this->db->query($sql);
223  if ($resql)
224  {
225  $num = $this->db->num_rows($resql);
226  $i = 0;
227  $y0=$y1=$y2=$y3=0;
228 
229  while ($i < $num)
230  {
231  $obj = $this->db->fetch_object($resql);
232 
233  $eventstatic->id=$obj->id;
234  $eventstatic->percentage=$obj->percent;
235  $eventstatic->fulldayevent=$obj->fulldayevent;
236  $eventstatic->punctual=$obj->punctual;
237 
238  $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
239 
240  // Calculate height of text
241  $text='';
242  if (! preg_match('/^'.preg_quote($obj->label).'/',$obj->note)) $text=$obj->label."\n";
243  $text.=$obj->note;
244  $text=dol_trunc(dol_htmlentitiesbr_decode($text),150);
245  // Add status to text
246  $text.="\n";
247  $status=dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1,1));
248  $text.=$status;
249  if ($obj->fk_project > 0)
250  {
251  $projectstatic->fetch($obj->fk_project);
252  $text.=($status?' - ':'').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->getNomUrl(0, 'nolink'));
253  }
254 
255  //print 'd'.$text; exit;
256  $nboflines=dol_nboflines($text);
257  $heightlinemax=max(2*$height,$nboflines*$height);
258  // Check if there is enough space to print record
259  if ((1+$y+$heightlinemax) >= ($this->page_hauteur - $this->marge_haute))
260  {
261  // We need to break page
262  $pagenb++;
263  $y=$this->_pagehead($pdf, $outputlangs, $pagenb);
264  $y++;
265  $pdf->SetFont('','',8);
266  }
267  $y++;
268 
269  // Date
270  $pdf->SetXY($this->marge_gauche, $y);
271  $textdate = dol_print_date($this->db->jdate($obj->dp),"day")."\n".dol_print_date($this->db->jdate($obj->dp),"hour");
272  if ($obj->dp2) {
273  if (dol_print_date($this->db->jdate($obj->dp),"day") != dol_print_date($this->db->jdate($obj->dp2),"day"))
274  $textdate.= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
275  else
276  $textdate.= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
277  }
278  $pdf->MultiCell(22, $height, $textdate, 0, 'L', 0);
279  $y0 = $pdf->GetY();
280 
281  // Third party
282  $pdf->SetXY(26, $y);
283  $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty),32), 0, 'L', 0);
284  $y1 = $pdf->GetY();
285 
286  // Action code
287  $code=$obj->code;
288  if (empty($conf->global->AGENDA_USE_EVENT_TYPE))
289  {
290  if ($code == 'AC_OTH') $code='AC_MANUAL';
291  if ($code == 'AC_OTH_AUTO') $code='AC_AUTO';
292  }
293  $pdf->SetXY(60,$y);
294  $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($outputlangs->transnoentitiesnoconv("Action".$code)),32), 0, 'L', 0);
295  $y2 = $pdf->GetY();
296 
297  // Description of event
298  $pdf->SetXY(106,$y);
299  $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset($text), 0, 'L', 0);
300  $y3 = $pdf->GetY();
301 
302  $i++;
303  }
304  }
305 
306  return 1;
307  }
308 
317  function _pagehead(&$pdf, $outputlangs, $pagenb)
318  {
319  global $conf,$langs;
320 
321  // Do not add the BACKGROUND as this is a report
322  //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
323 
324  // New page
325  $pdf->AddPage();
326 
327  // Show title
328  $pdf->SetFont('','B',10);
329  $pdf->SetXY($this->marge_gauche, $this->marge_haute);
330  $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', 0);
331  // Show page nb only on iso languages (so default Helvetica font)
332  if (pdf_getPDFFont($outputlangs) == 'Helvetica')
333  {
334  $pdf->SetXY($this->page_largeur-$this->marge_droite-40, $this->marge_haute);
335  $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
336  }
337 
338  $y=$pdf->GetY()+2;
339 
340  $pdf->Rect($this->marge_gauche, $y, ($this->page_largeur - $this->marge_gauche - $this->marge_droite), ($this->page_hauteur - $this->marge_haute - $this->marge_basse));
341  $y=$pdf->GetY()+1;
342 
343  return $y;
344  }
345 }
346 
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 '...' if string larger than length.
Class to manage agenda events (actions)
</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
_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:445
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:233
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.
Class to manage projects.
__construct($db, $month, $year)
Constructor.
Definition: rapport.pdf.php:60
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:459
write_file($socid=0, $catid=0, $outputlangs='')
Write the object to document file to disk.
Definition: rapport.pdf.php:95
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
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:1013
Class to generate event report.
Definition: rapport.pdf.php:36
type
Definition: viewcat.php:283