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