dolibarr 21.0.0-alpha
pdf_standard_actions.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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 Nick Fragoulis
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
34require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
35
40{
44 public $db;
45
49 public $error;
50
54 public $errors;
55
59 public $description;
60
64 public $date_edition;
65
69 public $year;
70
74 public $month;
75
79 public $title;
80
84 public $subject;
85
89 public $marge_gauche;
90
94 public $marge_droite;
95
99 public $marge_haute;
100
104 public $marge_basse;
105
109 public $format;
110
114 public $type;
115
119 public $page_hauteur;
120
124 public $page_largeur;
125
129 public $corner_radius;
130
134 public $result;
135
143 public function __construct($db, $month, $year)
144 {
145 global $langs;
146
147 // Load translation files required by the page
148 $langs->loadLangs(array("commercial", "projects"));
149
150 $this->db = $db;
151 $this->description = "";
152 $this->date_edition = time();
153 $this->month = $month;
154 $this->year = $year;
155 $this->corner_radius = getDolGlobalInt('MAIN_PDF_FRAME_CORNER_RADIUS', 0);
156 // Page size for A4 format
157 $this->type = 'pdf';
158 $formatarray = pdf_getFormat();
159 $this->page_largeur = $formatarray['width'];
160 $this->page_hauteur = $formatarray['height'];
161 $this->format = array($this->page_largeur, $this->page_hauteur);
162 $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
163 $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
164 $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
165 $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
166
167 $this->title = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
168 $this->subject = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
169 }
170
171 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
179 public function write_file($object, $outputlangs)
180 {
181 // phpcs:enable
182 global $user, $conf, $langs, $hookmanager;
183
184 if (!is_object($outputlangs)) {
185 $outputlangs = $langs;
186 }
187 // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
188 if (getDolGlobalString('MAIN_USE_FPDF')) {
189 $outputlangs->charset_output = 'ISO-8859-1';
190 }
191
192 // Load traductions files required by page
193 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products"));
194
195 $dir = $conf->agenda->dir_temp."/";
196 $file = $dir."actions-".sprintf("%02d", $this->month)."-".sprintf("%04d", $this->year).".pdf";
197
198 if (!file_exists($dir)) {
199 if (dol_mkdir($dir) < 0) {
200 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
201 return 0;
202 }
203 }
204
205 if (file_exists($dir)) {
206 // Add pdfgeneration hook
207 if (!is_object($hookmanager)) {
208 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
209 $hookmanager = new HookManager($this->db);
210 }
211 $hookmanager->initHooks(array('pdfgeneration'));
212
213 global $action;
214 $object = new stdClass();
215
216 $parameters = array('file' => $file, 'outputlangs' => $outputlangs);
217 $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
218
219 $pdf = pdf_getInstance($this->format);
220 $heightforinfotot = 50; // Height reserved to output the info and total part
221 $heightforfreetext = getDolGlobalInt('MAIN_PDF_FREETEXT_HEIGHT', 5); // Height reserved to output the free text on last page
222 $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
223 $pdf->SetAutoPageBreak(1, 0);
224
225 if (class_exists('TCPDF')) {
226 $pdf->setPrintHeader(false);
227 $pdf->setPrintFooter(false);
228 }
229 $pdf->SetFont(pdf_getPDFFont($outputlangs));
230
231 $pdf->Open();
232 $pagenb = 0;
233 $pdf->SetDrawColor(128, 128, 128);
234 $pdf->SetFillColor(220, 220, 220);
235
236 $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
237 $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
238 $pdf->SetCreator("Dolibarr ".DOL_VERSION);
239 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
240 $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
241
242 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
243 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
244
245 $nbpage = $this->_pages($pdf, $outputlangs); // Write content
246
247 if (method_exists($pdf, 'AliasNbPages')) {
248 $pdf->AliasNbPages(); // @phan-suppress-current-line PhanUndeclaredMethod
249 }
250 $pdf->Close();
251
252 $pdf->Output($file, 'F');
253
254 // Add pdfgeneration hook
255 if (!is_object($hookmanager)) {
256 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
257 $hookmanager = new HookManager($this->db);
258 }
259 $hookmanager->initHooks(array('pdfgeneration'));
260 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs);
261 global $action;
262 $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
263 if ($reshook < 0) {
264 $this->error = $hookmanager->error;
265 $this->errors = $hookmanager->errors;
266 }
267
268 dolChmod($file);
269
270 $this->result = array('fullpath' => $file);
271
272 return 1;
273 }
274 return 0;
275 }
276
284 private function _pages(&$pdf, $outputlangs)
285 {
286 global $conf;
287
288 $height = 3; // height for text separation
289 $pagenb = 1;
290
291 $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
292 $y++;
293 $pdf->SetFont('', '', 8);
294
295 $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
296 $sql .= " a.id, a.datep as dp, a.datep2 as dp2,";
297 $sql .= " a.fk_contact, a.note, a.percent as percent, a.fulldayevent, a.label, a.fk_project,";
298 $sql .= " c.code, c.libelle as label_type,";
299 $sql .= " u.login";
300 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
301 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
302 $sql .= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
303 $sql .= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year, $this->month, false))."'";
304 $sql .= " AND '".$this->db->idate(dol_get_last_day($this->year, $this->month, false))."'";
305 $sql .= " AND a.entity = ".$conf->entity;
306 $sql .= " ORDER BY a.datep DESC";
307
308 $eventstatic = new ActionComm($this->db);
309 $projectstatic = new Project($this->db);
310
311 dol_syslog(get_class($this)."::_page", LOG_DEBUG);
312 $resql = $this->db->query($sql);
313 if ($resql) {
314 $num = $this->db->num_rows($resql);
315 $i = 0;
316 $y0 = $y1 = $y2 = $y3 = 0;
317
318 while ($i < $num) {
319 $obj = $this->db->fetch_object($resql);
320
321 $eventstatic->id = $obj->id;
322 $eventstatic->percentage = $obj->percent;
323 $eventstatic->fulldayevent = $obj->fulldayevent;
324
325 $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
326
327 // Calculate height of text
328 $text = '';
329 if (!preg_match('/^'.preg_quote($obj->label, '/').'/', $obj->note)) {
330 $text = $obj->label."\n";
331 }
332 $text .= dolGetFirstLineOfText(dol_string_nohtmltag($obj->note), 1);
333 // Add status to text
334 $text .= "\n";
335 $status = $outputlangs->trans("Status").': '.dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1, 1));
336 $text .= $status;
337 if ($obj->fk_project > 0) {
338 $projectstatic->fetch($obj->fk_project);
339 if ($projectstatic->ref) {
340 $text .= ($status ? ' - ' : '').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->ref);
341 }
342 }
343
344 //print 'd'.$text; exit;
345 $nboflines = dol_nboflines($text);
346
347 $heightlinemax = max(2 * $height, $nboflines * $height);
348 // Check if there is enough space to print record
349 if ((1 + $y + $heightlinemax) >= ($this->page_hauteur - $this->marge_haute)) {
350 // We need to break page
351 $pagenb++;
352 $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
353 $y++;
354 $pdf->SetFont('', '', 8);
355 }
356 $y++;
357
358 // Date
359 $pdf->SetXY($this->marge_gauche, $y);
360 $textdate = dol_print_date($this->db->jdate($obj->dp), "day")."\n".dol_print_date($this->db->jdate($obj->dp), "hour");
361 if ($obj->dp2) {
362 if (dol_print_date($this->db->jdate($obj->dp), "day") != dol_print_date($this->db->jdate($obj->dp2), "day")) {
363 $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
364 } else {
365 $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
366 }
367 }
368 $textdate = $outputlangs->trans("ID").' '.$obj->id.' - '.$textdate;
369 $pdf->MultiCell(45 - $this->marge_gauche, $height, $textdate, 0, 'L', 0);
370 $y0 = $pdf->GetY();
371
372 // Third party
373 $pdf->SetXY(45, $y);
374 $pdf->MultiCell(28, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty), 28), 0, 'L', 0);
375 $y1 = $pdf->GetY();
376
377 // Action code
378 $code = $obj->code;
379 if (!getDolGlobalString('AGENDA_USE_EVENT_TYPE')) {
380 if ($code == 'AC_OTH') {
381 $code = 'AC_MANUAL';
382 }
383 if ($code == 'AC_OTH_AUTO') {
384 $code = 'AC_AUTO';
385 }
386 }
387 $pdf->SetXY(73, $y);
388 $labelactiontype = $outputlangs->transnoentitiesnoconv("Action".$code);
389 $labelactiontypeshort = $outputlangs->transnoentitiesnoconv("Action".$code.'Short');
390 $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($labelactiontypeshort == "Action".$code.'Short' ? $labelactiontype : $labelactiontypeshort), 32), 0, 'L', 0);
391 $y2 = $pdf->GetY();
392
393 // Description of event
394 $pdf->SetXY(106, $y);
395 $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset(dol_trunc(dol_string_nohtmltag($text, 0), 250, 'right', 'UTF-8', 0)), 0, 'L', 0);
396 $y3 = $pdf->GetY();
397
398 $i++;
399 }
400 }
401
402 return 1;
403 }
404
413 private function _pagehead(&$pdf, $outputlangs, $pagenb)
414 {
415 // Do not add the BACKGROUND as this is a report
416 //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
417
418 // New page
419 $pdf->AddPage();
420
421 // Show title
422 $pdf->SetFont('', 'B', 10);
423 $pdf->SetXY($this->marge_gauche, $this->marge_haute);
424 $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', 0);
425 // Show page nb only on iso languages (so default Helvetica font)
426 if (pdf_getPDFFont($outputlangs) == 'Helvetica') {
427 $pdf->SetXY($this->page_largeur - $this->marge_droite - 40, $this->marge_haute);
428 $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', 0);
429 }
430
431 $y = $pdf->GetY() + 2;
432
433 $pdf->RoundedRect($this->marge_gauche, $y, ($this->page_largeur - $this->marge_gauche - $this->marge_droite), ($this->page_hauteur - $this->marge_haute - $this->marge_basse), $this->corner_radius, '1234', 'D');
434 $y = $pdf->GetY() + 1;
435
436 return $y;
437 }
438}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage agenda events (actions)
Class to manage hooks.
Class to manage projects.
Class to generate event report.
_pagehead(&$pdf, $outputlangs, $pagenb)
Show top header of page.
__construct($db, $month, $year)
Constructor.
write_file($object, $outputlangs)
Write the object to document file to disk.
_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:596
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615
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.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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 a 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($outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
Definition pdf.lib.php:86
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition pdf.lib.php:265
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition pdf.lib.php:128
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137