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