dolibarr  18.0.0
rapport.class.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 
43  public $error;
44 
45  public $errors;
46 
47 
51  public $description;
52 
53  public $date_edition;
54 
55  public $year;
56 
57  public $month;
58 
59  public $title;
60 
61  public $subject;
62 
63  public $marge_gauche;
64 
65  public $marge_droite;
66 
67  public $marge_haute;
68 
69  public $marge_basse;
70 
71  public $format;
72 
73  public $type;
74 
75  public $page_hauteur;
76 
77  public $page_largeur;
78 
79 
87  public function __construct($db, $month, $year)
88  {
89  global $conf, $langs;
90 
91  // Load translation files required by the page
92  $langs->loadLangs(array("commercial", "projects"));
93 
94  $this->db = $db;
95  $this->description = "";
96  $this->date_edition = time();
97  $this->month = $month;
98  $this->year = $year;
99 
100  // Page size for A4 format
101  $this->type = 'pdf';
102  $formatarray = pdf_getFormat();
103  $this->page_largeur = $formatarray['width'];
104  $this->page_hauteur = $formatarray['height'];
105  $this->format = array($this->page_largeur, $this->page_hauteur);
106  $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
107  $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
108  $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
109  $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
110 
111  $this->title = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
112  $this->subject = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
113  }
114 
115  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
124  public function write_file($socid = 0, $catid = 0, $outputlangs = '')
125  {
126  // phpcs:enable
127  global $user, $conf, $langs, $hookmanager;
128 
129  if (!is_object($outputlangs)) {
130  $outputlangs = $langs;
131  }
132  // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
133  if (!empty($conf->global->MAIN_USE_FPDF)) {
134  $outputlangs->charset_output = 'ISO-8859-1';
135  }
136 
137  // Load traductions files required by page
138  $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products"));
139 
140  $dir = $conf->agenda->dir_temp."/";
141  $file = $dir."actions-".sprintf("%02d", $this->month)."-".sprintf("%04d", $this->year).".pdf";
142 
143  if (!file_exists($dir)) {
144  if (dol_mkdir($dir) < 0) {
145  $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
146  return 0;
147  }
148  }
149 
150  if (file_exists($dir)) {
151  // Add pdfgeneration hook
152  if (!is_object($hookmanager)) {
153  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
154  $hookmanager = new HookManager($this->db);
155  }
156  $hookmanager->initHooks(array('pdfgeneration'));
157 
158  global $action;
159  $object = new stdClass();
160 
161  $parameters = array('file'=>$file, 'outputlangs'=>$outputlangs);
162  $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
163 
164  $pdf = pdf_getInstance($this->format);
165  $heightforinfotot = 50; // Height reserved to output the info and total part
166  $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
167  $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
168  $pdf->SetAutoPageBreak(1, 0);
169 
170  if (class_exists('TCPDF')) {
171  $pdf->setPrintHeader(false);
172  $pdf->setPrintFooter(false);
173  }
174  $pdf->SetFont(pdf_getPDFFont($outputlangs));
175 
176  $pdf->Open();
177  $pagenb = 0;
178  $pdf->SetDrawColor(128, 128, 128);
179  $pdf->SetFillColor(220, 220, 220);
180 
181  $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
182  $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
183  $pdf->SetCreator("Dolibarr ".DOL_VERSION);
184  $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
185  $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
186 
187  $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
188 
189  $nbpage = $this->_pages($pdf, $outputlangs); // Write content
190 
191  if (method_exists($pdf, 'AliasNbPages')) {
192  $pdf->AliasNbPages();
193  }
194  $pdf->Close();
195 
196  $pdf->Output($file, 'F');
197 
198  // Add pdfgeneration hook
199  if (!is_object($hookmanager)) {
200  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
201  $hookmanager = new HookManager($this->db);
202  }
203  $hookmanager->initHooks(array('pdfgeneration'));
204  $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs);
205  global $action;
206  $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
207  if ($reshook < 0) {
208  $this->error = $hookmanager->error;
209  $this->errors = $hookmanager->errors;
210  }
211 
212  dolChmod($file);
213 
214  $this->result = array('fullpath'=>$file);
215 
216  return 1;
217  }
218  return 0;
219  }
220 
228  private function _pages(&$pdf, $outputlangs)
229  {
230  global $conf;
231 
232  $height = 3; // height for text separation
233  $pagenb = 1;
234 
235  $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
236  $y++;
237  $pdf->SetFont('', '', 8);
238 
239  $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
240  $sql .= " a.id, a.datep as dp, a.datep2 as dp2,";
241  $sql .= " a.fk_contact, a.note, a.percent as percent, a.fulldayevent, a.label, a.fk_project,";
242  $sql .= " c.code, c.libelle as label_type,";
243  $sql .= " u.login";
244  $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
245  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
246  $sql .= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
247  $sql .= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year, $this->month, false))."'";
248  $sql .= " AND '".$this->db->idate(dol_get_last_day($this->year, $this->month, false))."'";
249  $sql .= " AND a.entity = ".$conf->entity;
250  $sql .= " ORDER BY a.datep DESC";
251 
252  $eventstatic = new ActionComm($this->db);
253  $projectstatic = new Project($this->db);
254 
255  dol_syslog(get_class($this)."::_page", LOG_DEBUG);
256  $resql = $this->db->query($sql);
257  if ($resql) {
258  $num = $this->db->num_rows($resql);
259  $i = 0;
260  $y0 = $y1 = $y2 = $y3 = 0;
261 
262  while ($i < $num) {
263  $obj = $this->db->fetch_object($resql);
264 
265  $eventstatic->id = $obj->id;
266  $eventstatic->percentage = $obj->percent;
267  $eventstatic->fulldayevent = $obj->fulldayevent;
268 
269  $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
270 
271  // Calculate height of text
272  $text = '';
273  if (!preg_match('/^'.preg_quote($obj->label, '/').'/', $obj->note)) {
274  $text = $obj->label."\n";
275  }
276  $text .= dolGetFirstLineOfText(dol_string_nohtmltag($obj->note), 1);
277  // Add status to text
278  $text .= "\n";
279  $status = $outputlangs->trans("Status").': '.dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1, 1));
280  $text .= $status;
281  if ($obj->fk_project > 0) {
282  $projectstatic->fetch($obj->fk_project);
283  if ($projectstatic->ref) {
284  $text .= ($status ? ' - ' : '').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->ref);
285  }
286  }
287 
288  //print 'd'.$text; exit;
289  $nboflines = dol_nboflines($text);
290 
291  $heightlinemax = max(2 * $height, $nboflines * $height);
292  // Check if there is enough space to print record
293  if ((1 + $y + $heightlinemax) >= ($this->page_hauteur - $this->marge_haute)) {
294  // We need to break page
295  $pagenb++;
296  $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
297  $y++;
298  $pdf->SetFont('', '', 8);
299  }
300  $y++;
301 
302  // Date
303  $pdf->SetXY($this->marge_gauche, $y);
304  $textdate = dol_print_date($this->db->jdate($obj->dp), "day")."\n".dol_print_date($this->db->jdate($obj->dp), "hour");
305  if ($obj->dp2) {
306  if (dol_print_date($this->db->jdate($obj->dp), "day") != dol_print_date($this->db->jdate($obj->dp2), "day")) {
307  $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
308  } else {
309  $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
310  }
311  }
312  $textdate = $outputlangs->trans("ID").' '.$obj->id.' - '.$textdate;
313  $pdf->MultiCell(45 - $this->marge_gauche, $height, $textdate, 0, 'L', 0);
314  $y0 = $pdf->GetY();
315 
316  // Third party
317  $pdf->SetXY(45, $y);
318  $pdf->MultiCell(28, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty), 28), 0, 'L', 0);
319  $y1 = $pdf->GetY();
320 
321  // Action code
322  $code = $obj->code;
323  if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
324  if ($code == 'AC_OTH') {
325  $code = 'AC_MANUAL';
326  }
327  if ($code == 'AC_OTH_AUTO') {
328  $code = 'AC_AUTO';
329  }
330  }
331  $pdf->SetXY(73, $y);
332  $labelactiontype = $outputlangs->transnoentitiesnoconv("Action".$code);
333  $labelactiontypeshort = $outputlangs->transnoentitiesnoconv("Action".$code.'Short');
334  $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($labelactiontypeshort == "Action".$code.'Short' ? $labelactiontype : $labelactiontypeshort), 32), 0, 'L', 0);
335  $y2 = $pdf->GetY();
336 
337  // Description of event
338  $pdf->SetXY(106, $y);
339  $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset(dol_trunc(dol_string_nohtmltag($text, 0), 250, 'right', 'UTF-8', 0)), 0, 'L', 0);
340  $y3 = $pdf->GetY();
341 
342  $i++;
343  }
344  }
345 
346  return 1;
347  }
348 
357  private function _pagehead(&$pdf, $outputlangs, $pagenb)
358  {
359  global $conf, $langs;
360 
361  // Do not add the BACKGROUND as this is a report
362  //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
363 
364  // New page
365  $pdf->AddPage();
366 
367  // Show title
368  $pdf->SetFont('', 'B', 10);
369  $pdf->SetXY($this->marge_gauche, $this->marge_haute);
370  $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', 0);
371  // Show page nb only on iso languages (so default Helvetica font)
372  if (pdf_getPDFFont($outputlangs) == 'Helvetica') {
373  $pdf->SetXY($this->page_largeur - $this->marge_droite - 40, $this->marge_haute);
374  $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
375  }
376 
377  $y = $pdf->GetY() + 2;
378 
379  $pdf->Rect($this->marge_gauche, $y, ($this->page_largeur - $this->marge_gauche - $this->marge_droite), ($this->page_hauteur - $this->marge_haute - $this->marge_basse));
380  $y = $pdf->GetY() + 1;
381 
382  return $y;
383  }
384 }
dol_trunc
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.
Definition: functions.lib.php:4059
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
pdf_getFormat
pdf_getFormat(Translate $outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
Definition: pdf.lib.php:85
Project
Class to manage projects.
Definition: project.class.php:36
ActionComm
Class to manage agenda events (actions)
Definition: actioncomm.class.php:38
dol_htmlentitiesbr_decode
dol_htmlentitiesbr_decode($stringtodecode, $pagecodeto='UTF-8')
This function is called to decode a HTML string (it decodes entities and br tags)
Definition: functions.lib.php:7502
pdf_getInstance
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:127
pdf_getPDFFont
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:266
CommActionRapport\_pagehead
_pagehead(&$pdf, $outputlangs, $pagenb)
Show top header of page.
Definition: rapport.class.php:357
dol_nboflines
dol_nboflines($s, $maxchar=0)
Return nb of lines of a clear text.
Definition: functions.lib.php:7603
CommActionRapport\write_file
write_file($socid=0, $catid=0, $outputlangs='')
Write the object to document file to disk.
Definition: rapport.class.php:124
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2675
CommActionRapport
Class to generate event report.
Definition: rapport.class.php:36
dolChmod
dolChmod($filepath, $newmask='')
Change mod of a file.
Definition: functions.lib.php:7007
dol_string_nohtmltag
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Definition: functions.lib.php:7046
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:576
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:595
CommActionRapport\_pages
_pages(&$pdf, $outputlangs)
Write content of pages.
Definition: rapport.class.php:228
dolGetFirstLineOfText
dolGetFirstLineOfText($text, $nboflines=1, $charset='UTF-8')
Return first line of text.
Definition: functions.lib.php:7278
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6936
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:156
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120
CommActionRapport\__construct
__construct($db, $month, $year)
Constructor.
Definition: rapport.class.php:87
HookManager
Class to manage hooks.
Definition: hookmanager.class.php:30