dolibarr 25.0.0-alpha
pdf_standard_salary.modules.php
1<?php
2
3/* Copyright (C) 2026 Alain CIS
4 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
5 * Inspired by the standard stock and invoice PDF models
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13require_once DOL_DOCUMENT_ROOT.'/core/modules/salaries/modules_salary.php';
14require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
15require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
16require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
17if (isModEnabled('project')) {
18 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
19}
20require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
21
22
27{
29 public $db;
30
32 public $name;
33
35 public $description;
36
38 public $type;
39
41 public $page_largeur;
42
44 public $page_hauteur;
45
47 public $format;
48
50 public $marge_gauche;
51
53 public $marge_droite;
54
56 public $marge_haute;
57
59 public $marge_basse;
60
62 public $posxdesc;
63
65 public $posxearning;
66
68 public $posxdeduction;
69
71 public $emetteur;
72
78 public function __construct(\DoliDB $db)
79 {
80 global $conf, $langs, $mysoc;
81
82 $langs->loadLangs(array("main", "companies", "bills", "salaries", "projects"));
83
84 $this->db = $db;
85 $this->name = "standard_salary";
86 $this->description = $langs->trans("StandardSalaryDocModel");
87
88 // Page dimensions
89 $this->type = 'pdf';
90 $formatarray = pdf_getFormat();
91 $this->page_largeur = $formatarray['width'];
92 $this->page_hauteur = $formatarray['height'];
93 $this->format = array($this->page_largeur, $this->page_hauteur);
94 $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
95 $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
96 $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
97 $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
98
99 // Define position of columns for the table body
100 $this->posxdesc = $this->marge_gauche;
101 $this->posxearning = $this->page_largeur - $this->marge_droite - 80;
102 $this->posxdeduction = $this->page_largeur - $this->marge_droite - 40;
103
104 $this->emetteur = $mysoc;
105 }
106
107
119 public function writeFile($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
120 {
121 global $conf, $langs;
122
123 $outputlangs = is_object($outputlangs) ? $outputlangs : $langs;
124 if (getDolGlobalString('MAIN_USE_FPDF')) {
125 $outputlangs->charset_output = 'ISO-8859-1';
126 }
127
128 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "salaries"));
129
130 if (!$conf->salaries->dir_output) {
131 $this->error = $langs->trans("ErrorConstantNotDefined", "SALARIES_OUTPUTDIR");
132 return 0;
133 }
134
135 $objref = dol_sanitizeFileName($object->ref);
136 $dir = $conf->salaries->dir_output . "/" . $objref;
137 $file = $dir . "/" . $objref . ".pdf";
138
139 if (!dol_is_dir($dir)) {
140 if (dol_mkdir($dir) < 0) {
141 $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir);
142 return -1;
143 }
144 }
145
146 if (is_writable($dir)) {
147 $pdf = pdf_getInstance($this->format);
148 $pdf->setAutoPageBreak(true, 0);
149
150 if (class_exists('TCPDF')) {
151 $pdf->setPrintHeader(false);
152 $pdf->setPrintFooter(false);
153 }
154 $pdf->SetFont(pdf_getPDFFont($outputlangs));
155 $pdf->Open();
156
157 $pdf->AddPage();
158 $this->_pagehead($pdf, $object, $outputlangs);
159 $this->body($pdf, $object, $outputlangs);
160 $this->_pagefoot($pdf, $object, $outputlangs);
161
162 $pdf->Close();
163 $pdf->Output($file, 'F');
164 dolChmod($file);
165
166 $this->result = array('fullpath' => $file);
167
168 return 1;
169 } else {
170 $this->error = $langs->trans("ErrorDirIsNotWritable", $dir);
171 return 0;
172 }
173 }
174
175 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
185 protected function _pagehead(&$pdf, $object, $outputlangs, $showaddress = 1)
186 {
187 global $conf;
188 $default_font_size = pdf_getPDFFontSize($outputlangs);
189
190 pdf_pagehead($pdf, $outputlangs, $this->page_hauteur);
191
192 $posy = $this->marge_haute;
193 $posx = $this->page_largeur - $this->marge_droite - 100;
194
195 $pdf->SetXY($this->marge_gauche, $posy);
196
197 // Logo
198 $logo = $conf->mycompany->dir_output.'/logos/'.$this->emetteur->logo;
199 if ($this->emetteur->logo && is_readable($logo)) {
200 $height = pdf_getHeightForLogo($logo);
201 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height); // width=0 (auto)
202 } else {
203 $pdf->SetFont('', 'B', $default_font_size + 2);
204 $pdf->MultiCell(100, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0, 'L');
205 }
206 $yafterleft = $pdf->GetY();
207
208 // Company Address
209 $pdf->SetFont('', '', $default_font_size);
210 $pdf->SetXY($this->marge_gauche, $yafterleft + 1);
211 pdf_build_address($outputlangs, $this->emetteur, null, 'target');
212 $yafterleft = $pdf->GetY();
213
214
215 // Document Title
216 $pdf->SetFont('', 'B', $default_font_size + 4);
217 $pdf->SetXY($posx, $posy);
218 $pdf->SetTextColor(0, 0, 60);
219 $pdf->MultiCell(100, 5, $outputlangs->transnoentities("SalarySlip") . ' ' . $outputlangs->convToOutputCharset($object->ref), '', 'R');
220
221 $posy = $pdf->GetY() + 8;
222
223 // Employee Information
224 $employee = new User($this->db);
225
226 $fk_user = 0;
227 if (!empty($object->fk_user)) {
228 $fk_user = $object->fk_user;
229 } elseif (!empty($object->fk_user_author)) {
230 $fk_user = $object->fk_user_author;
231 }
232
233 if ($fk_user > 0 && $employee->fetch($fk_user) > 0) {
234 $pdf->SetFont('', '', $default_font_size);
235 $pdf->SetXY($posx, $posy);
236
237 $text = $employee->getFullName($outputlangs) . "\n";
238
239 if (!empty($employee->job)) {
240 $text .= $employee->job . "\n";
241 }
242 if (!empty($employee->address)) {
243 $text .= $employee->address . "\n";
244 }
245 if (!empty($employee->zip) || !empty($employee->town)) {
246 $text .= trim($employee->zip . ' ' . $employee->town) . "\n";
247 }
248 if (!empty($employee->email)) {
249 $text .= $employee->email;
250 }
251 $yafterright = $pdf->GetY();
252 } else {
253 $yafterright = $posy;
254 }
255
256
257 // Move cursor to below the taller of the two columns
258 $pdf->SetY(max($yafterleft, $yafterright) + 8);
259
260 // Info box
261 $pdf->SetFont('', 'B', $default_font_size - 1);
262 $pdf->SetTextColor(0, 0, 0);
263
264 // Build info array
265 $info_array = [];
266
267 // Add employee information to info box
268 if (!empty($employee->id)) {
269 $info_array[$outputlangs->transnoentitiesnoconv("Employee")]
270 = $employee->getFullName($outputlangs);
271
272 if (!empty($employee->job)) {
273 $info_array[$outputlangs->transnoentitiesnoconv("Job")]
274 = $employee->job;
275 }
276
277 if (!empty($employee->email)) {
278 $info_array[$outputlangs->transnoentitiesnoconv("Email")]
279 = $employee->email;
280 }
281 }
282
283 $info_array[$outputlangs->transnoentitiesnoconv("PayPeriod")] = dol_print_date($object->datesp, 'day') . " - " . dol_print_date($object->dateep, 'day');
284 $payment_date = $object->datesp;
285 $info_array[$outputlangs->transnoentitiesnoconv("DatePayment")] = dol_print_date($payment_date, 'day');
286
287 if ($object->type_payment && !empty($object->type_payment_code)) {
288 $info_array[$outputlangs->transnoentitiesnoconv("PaymentMode")] = $outputlangs->trans("PaymentType".$object->type_payment_code);
289 }
290
291 if (!empty($object->num_payment)) {
292 $info_array[$outputlangs->transnoentitiesnoconv("Numero")] = $object->num_payment;
293 }
294 if (isModEnabled('project') && !empty($object->fk_project)) {
295 $project = new Project($this->db);
296 if ($project->fetch($object->fk_project) > 0) {
297 $info_array[$outputlangs->transnoentitiesnoconv("Project")] = $project->ref;
298 }
299 }
300
301 // Print info box
302 $col1_width = 40;
303 foreach ($info_array as $label => $value) {
304 $pdf->SetFont('', 'B', $default_font_size - 1);
305 $pdf->Cell($col1_width, 5, $label . " :", 0, 0, 'L');
306 $pdf->SetFont('', '', $default_font_size - 1);
307 $pdf->MultiCell(0, 5, $value, 0, 'L');
308 }
309 $pdf->Ln(5);
310 }
311
312
321 protected function body(&$pdf, $object, $outputlangs)
322 {
323 $default_font_size = pdf_getPDFFontSize($outputlangs);
324 $tab_top = $pdf->GetY();
325
326 $this->tableauHeader($pdf, $tab_top, $outputlangs);
327 $curY = $tab_top + 7;
328
329 // --- EARNINGS ---
330 $pdf->SetFont('', '', $default_font_size);
331 $pdf->SetXY($this->posxdesc, $curY);
332 $pdf->MultiCell($this->posxearning - $this->posxdesc, 5, $outputlangs->convToOutputCharset($object->label));
333 $y_after_label = $pdf->GetY();
334
335 $pdf->SetXY($this->posxearning, $curY);
336 $pdf->MultiCell($this->posxdeduction - $this->posxearning, 5, price($object->amount, 0, $outputlangs, 1), 0, 'R');
337
338 $gross_pay = $object->amount;
339
340 // --- DEDUCTIONS ---
341 // This section is ready for when the salary object supports deductions.
342 $total_deductions = 0.00;
343
344 // --- PUBLIC NOTE ---
345 if (!empty($object->note_public)) {
346 $pdf->SetY($y_after_label + 5);
347 $pdf->SetFont('', 'B', $default_font_size - 1);
348 $pdf->Cell(0, 5, $outputlangs->transnoentitiesnoconv("Note"), 0, 1, 'L');
349 $pdf->SetFont('', '', $default_font_size - 1);
350 $pdf->writeHTMLCell($this->page_largeur - $this->marge_gauche - $this->marge_droite, 5, $this->marge_gauche, $pdf->GetY(), $object->note_public, 'B', 1);
351 }
352
353 // --- TOTALS ---
354 $net_pay = $gross_pay - $total_deductions;
355 $totals_y_pos = $this->page_hauteur - $this->marge_basse - 30;
356 $pdf->SetY(max($pdf->GetY() + 5, $totals_y_pos));
357
358 $pdf->SetFont('', '', $default_font_size);
359 $pdf->SetX($this->posxearning - 30);
360 $pdf->Cell(40, 5, $outputlangs->transnoentitiesnoconv("TotalEarnings") . " :", 0, 0, 'R');
361 $pdf->Cell(30, 5, price($gross_pay, 0, $outputlangs, 1), 0, 1, 'R');
362
363 $pdf->SetX($this->posxearning - 30);
364 $pdf->Cell(40, 5, $outputlangs->transnoentitiesnoconv("TotalDeductions") . " :", 0, 0, 'R');
365 $pdf->Cell(30, 5, price($total_deductions, 0, $outputlangs, 1), 0, 1, 'R');
366
367 $pdf->SetLineStyle(array('width' => 0.3, 'color' => array(0, 0, 0)));
368 $pdf->Line($this->posxearning - 30, $pdf->GetY(), $this->page_largeur - $this->marge_droite, $pdf->GetY());
369 $pdf->Ln(1);
370
371 $pdf->SetFont('', 'B', $default_font_size + 1);
372 $pdf->SetX($this->posxearning - 30);
373 $pdf->Cell(40, 6, $outputlangs->transnoentitiesnoconv("NetPaid") . " :", 0, 0, 'R');
374 $pdf->Cell(30, 6, price($net_pay, 0, $outputlangs, 1), 0, 1, 'R');
375 }
376
377
386 protected function tableauHeader(&$pdf, $tab_top, $outputlangs)
387 {
388 $default_font_size = pdf_getPDFFontSize($outputlangs);
389
390 $pdf->SetXY($this->posxdesc, $tab_top);
391 $pdf->SetFont('', 'B', $default_font_size - 1);
392 $pdf->SetFillColor(240, 240, 240);
393 $pdf->SetTextColor(0, 0, 0);
394
395 $pdf->Cell($this->posxearning - $this->posxdesc, 7, $outputlangs->transnoentitiesnoconv("Description"), 0, 0, 'L', true);
396 $pdf->Cell($this->posxdeduction - $this->posxearning, 7, $outputlangs->transnoentitiesnoconv("Earnings"), 0, 0, 'R', true);
397 $pdf->Cell($this->page_largeur - $this->marge_droite - $this->posxdeduction, 7, $outputlangs->transnoentitiesnoconv("Deductions"), 0, 1, 'R', true);
398
399 $pdf->SetDrawColor(128, 128, 128);
400 $pdf->Line($this->marge_gauche, $tab_top + 7, $this->page_largeur - $this->marge_droite, $tab_top + 7);
401 }
402
403 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
412 protected function _pagefoot(&$pdf, $object, $outputlangs)
413 {
414 $showdetails = getDolGlobalInt('MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS', 0);
415 pdf_pagefoot($pdf, $outputlangs, 'SALARY_FREE_TEXT', $this->emetteur, $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails);
416 }
417}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition propal.php:281
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage Dolibarr database access.
Base class for Salary PDF document models.
Class to manage projects.
Class to manage Dolibarr users.
Class to build a standard salary slip PDF.
body(&$pdf, $object, $outputlangs)
Show the lines of the salary slip.
writeFile($object, $outputlangs, $srctemplatepath='', $hidedetails=0, $hidedesc=0, $hideref=0)
Function to build the salary slip document.
tableauHeader(&$pdf, $tab_top, $outputlangs)
Show table header.
__construct(\DoliDB $db)
Constructor.
_pagefoot(&$pdf, $object, $outputlangs)
Show footer of page.
_pagehead(&$pdf, $object, $outputlangs, $showaddress=1)
Show top header of page.
global $mysoc
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_is_dir($folder)
Test if filename is a directory.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
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, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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:87
pdf_getPDFFontSize($outputlangs)
Return font size to use for PDF generation.
Definition pdf.lib.php:296
pdf_getHeightForLogo($logo, $url=false)
Return height to use for Logo onto PDF.
Definition pdf.lib.php:319
pdf_pagehead($pdf, $outputlangs, $page_height)
Show header of page for PDF generation.
Definition pdf.lib.php:792
pdf_pagefoot($pdf, $outputlangs, $paramfreetext, $fromcompany, $marge_basse, $marge_gauche, $page_hauteur, $object, $showdetails=0, $hidefreetext=0, $page_largeur=0, $watermark='')
Show footer of page for PDF generation.
Definition pdf.lib.php:1479
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition pdf.lib.php:275
pdf_build_address($outputlangs, $sourcecompany, $targetcompany='', $targetcontact='', $usecontact=0, $mode='source', $object=null)
Return a string with full address formatted for output on PDF documents.
Definition pdf.lib.php:481
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition pdf.lib.php:129
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:165