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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * or see https://www.gnu.org/
21 */
22
29require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
33require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
34
39{
43 public $db;
44
45 public $error;
46
47 public $errors;
48
52 public $description;
53
54 public $date_edition;
55
56 public $year;
57
58 public $month;
59
60 public $title;
61
62 public $subject;
63
64 public $marge_gauche;
65
66 public $marge_droite;
67
68 public $marge_haute;
69
70 public $marge_basse;
71
72 public $format;
73
74 public $type;
75
76 public $page_hauteur;
77
78 public $page_largeur;
79
83 public $result;
84
92 public function __construct($db, $month, $year)
93 {
94 global $langs;
95
96 // Load translation files required by the page
97 $langs->loadLangs(array("commercial", "projects"));
98
99 $this->db = $db;
100 $this->description = "";
101 $this->date_edition = time();
102 $this->month = $month;
103 $this->year = $year;
104
105 // Page size for A4 format
106 $this->type = 'pdf';
107 $formatarray = pdf_getFormat();
108 $this->page_largeur = $formatarray['width'];
109 $this->page_hauteur = $formatarray['height'];
110 $this->format = array($this->page_largeur, $this->page_hauteur);
111 $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
112 $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
113 $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
114 $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
115
116 $this->title = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
117 $this->subject = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
118 }
119
120 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
128 public function write_file($object, $outputlangs)
129 {
130 // phpcs:enable
131 global $user, $conf, $langs, $hookmanager;
132
133 if (!is_object($outputlangs)) {
134 $outputlangs = $langs;
135 }
136 // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
137 if (getDolGlobalString('MAIN_USE_FPDF')) {
138 $outputlangs->charset_output = 'ISO-8859-1';
139 }
140
141 // Load traductions files required by page
142 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products"));
143
144 $dir = $conf->agenda->dir_temp."/";
145 $file = $dir."actions-".sprintf("%02d", $this->month)."-".sprintf("%04d", $this->year).".pdf";
146
147 if (!file_exists($dir)) {
148 if (dol_mkdir($dir) < 0) {
149 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
150 return 0;
151 }
152 }
153
154 if (file_exists($dir)) {
155 // Add pdfgeneration hook
156 if (!is_object($hookmanager)) {
157 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
158 $hookmanager = new HookManager($this->db);
159 }
160 $hookmanager->initHooks(array('pdfgeneration'));
161
162 global $action;
163 $object = new stdClass();
164
165 $parameters = array('file' => $file, 'outputlangs' => $outputlangs);
166 $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
167
168 $pdf = pdf_getInstance($this->format);
169 $heightforinfotot = 50; // Height reserved to output the info and total part
170 $heightforfreetext = getDolGlobalInt('MAIN_PDF_FREETEXT_HEIGHT', 5); // Height reserved to output the free text on last page
171 $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
172 $pdf->SetAutoPageBreak(1, 0);
173
174 if (class_exists('TCPDF')) {
175 $pdf->setPrintHeader(false);
176 $pdf->setPrintFooter(false);
177 }
178 $pdf->SetFont(pdf_getPDFFont($outputlangs));
179
180 $pdf->Open();
181 $pagenb = 0;
182 $pdf->SetDrawColor(128, 128, 128);
183 $pdf->SetFillColor(220, 220, 220);
184
185 $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
186 $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
187 $pdf->SetCreator("Dolibarr ".DOL_VERSION);
188 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
189 $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
190
191 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
192 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
193
194 $nbpage = $this->_pages($pdf, $outputlangs); // Write content
195
196 if (method_exists($pdf, 'AliasNbPages')) {
197 $pdf->AliasNbPages(); // @phan-suppress-current-line PhanUndeclaredMethod
198 }
199 $pdf->Close();
200
201 $pdf->Output($file, 'F');
202
203 // Add pdfgeneration hook
204 if (!is_object($hookmanager)) {
205 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
206 $hookmanager = new HookManager($this->db);
207 }
208 $hookmanager->initHooks(array('pdfgeneration'));
209 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs);
210 global $action;
211 $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
212 if ($reshook < 0) {
213 $this->error = $hookmanager->error;
214 $this->errors = $hookmanager->errors;
215 }
216
217 dolChmod($file);
218
219 $this->result = array('fullpath' => $file);
220
221 return 1;
222 }
223 return 0;
224 }
225
233 private function _pages(&$pdf, $outputlangs)
234 {
235 global $conf;
236
237 $height = 3; // height for text separation
238 $pagenb = 1;
239
240 $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
241 $y++;
242 $pdf->SetFont('', '', 8);
243
244 $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
245 $sql .= " a.id, a.datep as dp, a.datep2 as dp2,";
246 $sql .= " a.fk_contact, a.note, a.percent as percent, a.fulldayevent, a.label, a.fk_project,";
247 $sql .= " c.code, c.libelle as label_type,";
248 $sql .= " u.login";
249 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
250 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
251 $sql .= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
252 $sql .= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year, $this->month, false))."'";
253 $sql .= " AND '".$this->db->idate(dol_get_last_day($this->year, $this->month, false))."'";
254 $sql .= " AND a.entity = ".$conf->entity;
255 $sql .= " ORDER BY a.datep DESC";
256
257 $eventstatic = new ActionComm($this->db);
258 $projectstatic = new Project($this->db);
259
260 dol_syslog(get_class($this)."::_page", LOG_DEBUG);
261 $resql = $this->db->query($sql);
262 if ($resql) {
263 $num = $this->db->num_rows($resql);
264 $i = 0;
265 $y0 = $y1 = $y2 = $y3 = 0;
266
267 while ($i < $num) {
268 $obj = $this->db->fetch_object($resql);
269
270 $eventstatic->id = $obj->id;
271 $eventstatic->percentage = $obj->percent;
272 $eventstatic->fulldayevent = $obj->fulldayevent;
273
274 $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
275
276 // Calculate height of text
277 $text = '';
278 if (!preg_match('/^'.preg_quote($obj->label, '/').'/', $obj->note)) {
279 $text = $obj->label."\n";
280 }
281 $text .= dolGetFirstLineOfText(dol_string_nohtmltag($obj->note), 1);
282 // Add status to text
283 $text .= "\n";
284 $status = $outputlangs->trans("Status").': '.dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1, 1));
285 $text .= $status;
286 if ($obj->fk_project > 0) {
287 $projectstatic->fetch($obj->fk_project);
288 if ($projectstatic->ref) {
289 $text .= ($status ? ' - ' : '').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->ref);
290 }
291 }
292
293 //print 'd'.$text; exit;
294 $nboflines = dol_nboflines($text);
295
296 $heightlinemax = max(2 * $height, $nboflines * $height);
297 // Check if there is enough space to print record
298 if ((1 + $y + $heightlinemax) >= ($this->page_hauteur - $this->marge_haute)) {
299 // We need to break page
300 $pagenb++;
301 $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
302 $y++;
303 $pdf->SetFont('', '', 8);
304 }
305 $y++;
306
307 // Date
308 $pdf->SetXY($this->marge_gauche, $y);
309 $textdate = dol_print_date($this->db->jdate($obj->dp), "day")."\n".dol_print_date($this->db->jdate($obj->dp), "hour");
310 if ($obj->dp2) {
311 if (dol_print_date($this->db->jdate($obj->dp), "day") != dol_print_date($this->db->jdate($obj->dp2), "day")) {
312 $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
313 } else {
314 $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
315 }
316 }
317 $textdate = $outputlangs->trans("ID").' '.$obj->id.' - '.$textdate;
318 $pdf->MultiCell(45 - $this->marge_gauche, $height, $textdate, 0, 'L', 0);
319 $y0 = $pdf->GetY();
320
321 // Third party
322 $pdf->SetXY(45, $y);
323 $pdf->MultiCell(28, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty), 28), 0, 'L', 0);
324 $y1 = $pdf->GetY();
325
326 // Action code
327 $code = $obj->code;
328 if (!getDolGlobalString('AGENDA_USE_EVENT_TYPE')) {
329 if ($code == 'AC_OTH') {
330 $code = 'AC_MANUAL';
331 }
332 if ($code == 'AC_OTH_AUTO') {
333 $code = 'AC_AUTO';
334 }
335 }
336 $pdf->SetXY(73, $y);
337 $labelactiontype = $outputlangs->transnoentitiesnoconv("Action".$code);
338 $labelactiontypeshort = $outputlangs->transnoentitiesnoconv("Action".$code.'Short');
339 $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($labelactiontypeshort == "Action".$code.'Short' ? $labelactiontype : $labelactiontypeshort), 32), 0, 'L', 0);
340 $y2 = $pdf->GetY();
341
342 // Description of event
343 $pdf->SetXY(106, $y);
344 $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset(dol_trunc(dol_string_nohtmltag($text, 0), 250, 'right', 'UTF-8', 0)), 0, 'L', 0);
345 $y3 = $pdf->GetY();
346
347 $i++;
348 }
349 }
350
351 return 1;
352 }
353
362 private function _pagehead(&$pdf, $outputlangs, $pagenb)
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}
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:595
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:614
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(Translate $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:267
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:139