dolibarr  13.0.2
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 <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/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  public 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  // Page size for A4 format
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.ScopeNotCamelCaps
111  public 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 required 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 
145  global $action;
146  $object = new stdClass();
147 
148  $parameters = array('file'=>$file, 'outputlangs'=>$outputlangs);
149  $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
150 
151  $pdf = pdf_getInstance($this->format);
152  $heightforinfotot = 50; // Height reserved to output the info and total part
153  $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
154  $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
155  $pdf->SetAutoPageBreak(1, 0);
156 
157  if (class_exists('TCPDF'))
158  {
159  $pdf->setPrintHeader(false);
160  $pdf->setPrintFooter(false);
161  }
162  $pdf->SetFont(pdf_getPDFFont($outputlangs));
163 
164  $pdf->Open();
165  $pagenb = 0;
166  $pdf->SetDrawColor(128, 128, 128);
167  $pdf->SetFillColor(220, 220, 220);
168 
169  $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
170  $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
171  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
172  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
173  $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
174 
175  $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
176 
177  $nbpage = $this->_pages($pdf, $outputlangs); // Write content
178 
179  if (method_exists($pdf, 'AliasNbPages')) $pdf->AliasNbPages();
180  $pdf->Close();
181 
182  $pdf->Output($file, 'F');
183 
184  // Add pdfgeneration hook
185  if (!is_object($hookmanager))
186  {
187  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
188  $hookmanager = new HookManager($this->db);
189  }
190  $hookmanager->initHooks(array('pdfgeneration'));
191  $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
192  global $action;
193  $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
194  if ($reshook < 0)
195  {
196  $this->error = $hookmanager->error;
197  $this->errors = $hookmanager->errors;
198  }
199 
200  if (!empty($conf->global->MAIN_UMASK))
201  @chmod($file, octdec($conf->global->MAIN_UMASK));
202 
203  $this->result = array('fullpath'=>$file);
204 
205  return 1;
206  }
207  }
208 
216  private function _pages(&$pdf, $outputlangs)
217  {
218  global $conf;
219 
220  $height = 3; // height for text separation
221  $pagenb = 1;
222 
223  $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
224  $y++;
225  $pdf->SetFont('', '', 8);
226 
227  $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
228  $sql .= " a.id, a.datep as dp, a.datep2 as dp2,";
229  $sql .= " a.fk_contact, a.note, a.percent as percent, a.label, a.fk_project,";
230  $sql .= " c.code, c.libelle,";
231  $sql .= " u.login";
232  $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
233  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
234  $sql .= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
235  $sql .= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year, $this->month, false))."'";
236  $sql .= " AND '".$this->db->idate(dol_get_last_day($this->year, $this->month, false))."'";
237  $sql .= " AND a.entity = ".$conf->entity;
238  $sql .= " ORDER BY a.datep DESC";
239 
240  $eventstatic = new ActionComm($this->db);
241  $projectstatic = new Project($this->db);
242 
243  dol_syslog(get_class($this)."::_page", LOG_DEBUG);
244  $resql = $this->db->query($sql);
245  if ($resql)
246  {
247  $num = $this->db->num_rows($resql);
248  $i = 0;
249  $y0 = $y1 = $y2 = $y3 = 0;
250 
251  while ($i < $num)
252  {
253  $obj = $this->db->fetch_object($resql);
254 
255  $eventstatic->id = $obj->id;
256  $eventstatic->percentage = $obj->percent;
257  $eventstatic->fulldayevent = $obj->fulldayevent;
258 
259  $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
260 
261  // Calculate height of text
262  $text = '';
263  if (!preg_match('/^'.preg_quote($obj->label, '/').'/', $obj->note)) $text = $obj->label."\n";
264  $text .= dolGetFirstLineOfText(dol_string_nohtmltag($obj->note), 2);
265  // Add status to text
266  $text .= "\n";
267  $status = $outputlangs->trans("Status").': '.dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1, 1));
268  $text .= $status;
269  if ($obj->fk_project > 0)
270  {
271  $projectstatic->fetch($obj->fk_project);
272  if ($projectstatic->ref) {
273  $text .= ($status ? ' - ' : '').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->ref);
274  }
275  }
276 
277  //print 'd'.$text; exit;
278  $nboflines = dol_nboflines($text);
279 
280  $heightlinemax = max(2 * $height, $nboflines * $height);
281  // Check if there is enough space to print record
282  if ((1 + $y + $heightlinemax) >= ($this->page_hauteur - $this->marge_haute))
283  {
284  // We need to break page
285  $pagenb++;
286  $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
287  $y++;
288  $pdf->SetFont('', '', 8);
289  }
290  $y++;
291 
292  // Date
293  $pdf->SetXY($this->marge_gauche, $y);
294  $textdate = dol_print_date($this->db->jdate($obj->dp), "day")."\n".dol_print_date($this->db->jdate($obj->dp), "hour");
295  if ($obj->dp2) {
296  if (dol_print_date($this->db->jdate($obj->dp), "day") != dol_print_date($this->db->jdate($obj->dp2), "day"))
297  $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
298  else $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
299  }
300  $textdate = $outputlangs->trans("ID").' '.$obj->id.' - '.$textdate;
301  $pdf->MultiCell(45 - $this->marge_gauche, $height, $textdate, 0, 'L', 0);
302  $y0 = $pdf->GetY();
303 
304  // Third party
305  $pdf->SetXY(45, $y);
306  $pdf->MultiCell(28, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty), 28), 0, 'L', 0);
307  $y1 = $pdf->GetY();
308 
309  // Action code
310  $code = $obj->code;
311  if (empty($conf->global->AGENDA_USE_EVENT_TYPE))
312  {
313  if ($code == 'AC_OTH') $code = 'AC_MANUAL';
314  if ($code == 'AC_OTH_AUTO') $code = 'AC_AUTO';
315  }
316  $pdf->SetXY(73, $y);
317  $labelactiontype = $outputlangs->transnoentitiesnoconv("Action".$code);
318  $labelactiontypeshort = $outputlangs->transnoentitiesnoconv("Action".$code.'Short');
319  $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($labelactiontypeshort == "Action".$code.'Short' ? $labelactiontype : $labelactiontypeshort), 32), 0, 'L', 0);
320  $y2 = $pdf->GetY();
321 
322  // Description of event
323  $pdf->SetXY(106, $y);
324  $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset(dol_string_nohtmltag($text, 0)), 0, 'L', 0);
325  $y3 = $pdf->GetY();
326 
327  $i++;
328  }
329  }
330 
331  return 1;
332  }
333 
342  private function _pagehead(&$pdf, $outputlangs, $pagenb)
343  {
344  global $conf, $langs;
345 
346  // Do not add the BACKGROUND as this is a report
347  //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
348 
349  // New page
350  $pdf->AddPage();
351 
352  // Show title
353  $pdf->SetFont('', 'B', 10);
354  $pdf->SetXY($this->marge_gauche, $this->marge_haute);
355  $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', 0);
356  // Show page nb only on iso languages (so default Helvetica font)
357  if (pdf_getPDFFont($outputlangs) == 'Helvetica')
358  {
359  $pdf->SetXY($this->page_largeur - $this->marge_droite - 40, $this->marge_haute);
360  $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
361  }
362 
363  $y = $pdf->GetY() + 2;
364 
365  $pdf->Rect($this->marge_gauche, $y, ($this->page_largeur - $this->marge_gauche - $this->marge_droite), ($this->page_hauteur - $this->marge_haute - $this->marge_basse));
366  $y = $pdf->GetY() + 1;
367 
368  return $y;
369  }
370 }
pdf_getFormat(Translate $outputlangs=null, $mode= 'setup')
Return array with format properties of default PDF format.
Definition: pdf.lib.php:45
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Class to manage agenda events (actions)
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</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:750
_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:481
write_file($socid=0, $catid=0, $outputlangs= '')
Write the object to document file to disk.
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:222
$conf db
API class for accounts.
Definition: inc.php:54
dol_nboflines($s, $maxchar=0)
Return nb of lines of a clear text.
Class to manage hooks.
Class to manage projects.
__construct($db, $month, $year)
Constructor.
Definition: rapport.pdf.php:74
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dolGetFirstLineOfText($text, $nboflines=1, $charset= 'UTF-8')
Return first line of text.
pdf_getInstance($format= '', $metric= 'mm', $pagetype= 'P')
Return a PDF instance object.
Definition: pdf.lib.php:88
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:498
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_htmlentitiesbr_decode($stringtodecode, $pagecodeto= 'UTF-8')
This function is called to decode a HTML string (it decodes entities and br tags) ...
Class to generate event report.
Definition: rapport.pdf.php:36
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(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)