dolibarr 24.0.1
payment_salary.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2014 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2016-2026 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
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 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
30require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32
41// Load translation files required by the page
42$langs->loadLangs(array("banks", "bills"));
43
44$action = GETPOST('action', 'alpha');
45$cancel = GETPOST('cancel', 'alpha');
46$confirm = GETPOST('confirm', 'alpha');
47
48$id = GETPOSTINT('id');
49$ref = GETPOST('ref', 'alpha');
50$amounts = array();
51
52$object = new Salary($db);
53if ($id > 0) {
54 $object->fetch($id);
55}
56
57// Security check
58$socid = GETPOSTINT("socid");
59if ($user->socid > 0) {
60 $socid = $user->socid;
61}
62restrictedArea($user, 'salaries', $object->id, 'salary', '');
63
64// restrictedArea() called without a feature2 only checks that the salaries read permission
65// exists, and checkUserAccessToObject() lists 'salaries' among the features tested on the
66// entity alone, so neither of them looks at who the salary belongs to. Without the check
67// below, any holder of salaries->read lists, adds and deletes the payments of a colleague.
68// Same condition as salaries/card.php, the screen this page is reached from.
69if ($object->id > 0) {
70 $childids = $user->getAllChildIds(1);
71 $canread = 0;
72 if ($user->hasRight('salaries', 'readall')) {
73 $canread = 1;
74 }
75 if ($user->hasRight('salaries', 'read') && $object->fk_user > 0 && in_array($object->fk_user, $childids)) {
76 $canread = 1;
77 }
78 if (!$canread) {
80 }
81}
82
83
84/*
85 * Actions
86 */
87
88if (($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) && $user->hasRight('salaries', 'write')) {
89 $error = 0;
90
91 if ($cancel) {
92 $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
93 header("Location: ".$loc);
94 exit;
95 }
96
97 $datepaye = dol_mktime(GETPOSTINT("rehour"), GETPOSTINT("remin"), GETPOSTINT("resec"), GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"), 'tzuserrel');
98
99 if (!(GETPOSTINT("paiementtype") > 0)) {
100 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
101 $error++;
102 $action = 'create';
103 }
104 if ($datepaye == '') {
105 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
106 $error++;
107 $action = 'create';
108 }
109 if (isModEnabled("bank") && !(GETPOSTINT("accountid") > 0)) {
110 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
111 $error++;
112 $action = 'create';
113 }
114
115 // Read possible payments
116 foreach ($_POST as $key => $value) {
117 if (substr($key, 0, 7) == 'amount_') {
118 $other_chid = substr($key, 7);
119 $amounts[$other_chid] = price2num(GETPOST($key));
120 }
121 }
122
123 if ($amounts[key($amounts)] <= 0) {
124 $error++;
125 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
126 $action = 'create';
127 }
128
129 if (!$error) {
130 $paymentid = 0;
131 $db->begin();
132
133 // Create a line of payments
134 $paiement = new PaymentSalary($db);
135 $paiement->fk_salary = $id;
136 $paiement->chid = $id; // deprecated
137 $paiement->datep = $datepaye;
138 $paiement->amounts = $amounts; // Amount array
139 $paiement->fk_typepayment = GETPOSTINT("paiementtype");
140 $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
141 $paiement->note = GETPOST("note", 'restricthtml');
142 $paiement->note_private = GETPOST("note", 'restricthtml');
143
144 $paymentid = $paiement->create($user, (GETPOST('closepaidsalary') == 'on' ? 1 : 0));
145 if ($paymentid < 0) {
146 $error++;
147 setEventMessages($paiement->error, null, 'errors');
148 $action = 'create';
149 }
150
151 if (!$error) {
152 $result = $paiement->addPaymentToBank($user, 'payment_salary', '(SalaryPayment)', GETPOSTINT('accountid'), '', '');
153
154 if (!($result > 0)) {
155 $error++;
156 setEventMessages($paiement->error, null, 'errors');
157 $action = 'create';
158 }
159 }
160
161 if (!$error) {
162 $db->commit();
163 $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
164 header('Location: '.$loc);
165 exit;
166 } else {
167 $db->rollback();
168 }
169 }
170}
171
172
173/*
174 * View
175 */
176
177$form = new Form($db);
178
179$help_url = '';
180
181llxHeader('', '', $help_url);
182
183$salary = $object;
184$sumpaid = 0.0;
185
186// Payment creation form
187if ($action == 'create') {
188 $salary->accountid = $salary->fk_account ? $salary->fk_account : $salary->accountid;
189 $salary->fk_typepayment = $salary->mode_reglement_id ? $salary->mode_reglement_id : $salary->paiementtype;
190
191 if (!empty($conf->use_javascript_ajax)) {
192 print "\n".'<script type="text/javascript">';
193
194 //Add js for AutoFill
195 print ' $(document).ready(function () {';
196 print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
197 var amount = $(this).data("value");
198 document.getElementById($(this).data(\'rowid\')).value = amount ;
199 });';
200 print ' });'."\n";
201
202 print ' </script>'."\n";
203 }
204
205 print load_fiche_titre($langs->trans("DoPayment"));
206
207 print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
208 print '<input type="hidden" name="token" value="'.newToken().'">';
209 print '<input type="hidden" name="id" value="'.$id.'">';
210 print '<input type="hidden" name="chid" value="'.$id.'">';
211 print '<input type="hidden" name="action" value="add_payment">';
212
213 print dol_get_fiche_head();
214
215 print '<table class="border centpercent">';
216
217 print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$id.'">'.$id.'</a></td></tr>';
218 print '<tr><td>'.$langs->trans("Label").'</td><td>'.$salary->label."</td></tr>\n";
219 print '<tr><td>'.$langs->trans("DateStart")."</td><td>".dol_print_date($salary->datesp, 'day')."</td></tr>\n";
220 print '<tr><td>'.$langs->trans("DateEnd")."</td><td>".dol_print_date($salary->dateep, 'day')."</td></tr>\n";
221 /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($salary->date_ech,'day')."</td></tr>\n";
222 print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($salary->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
223
224 $sql = "SELECT sum(p.amount) as total";
225 $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
226 $sql .= " WHERE p.fk_salary = ".((int) $id);
227 $resql = $db->query($sql);
228 if ($resql) {
229 $obj = $db->fetch_object($resql);
230 $sumpaid = (float) $obj->total;
231 $db->free($resql);
232 }
233 /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
234 print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
235
236 print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
237 $datepaye = dol_mktime(GETPOSTINT("rehour"), GETPOSTINT("remin"), GETPOSTINT("resec"), GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
238 $datepayment = getDolGlobalString('MAIN_AUTOFILL_DATE') ? '' : (GETPOST("remonth") ? $datepaye : -1);
239 print $form->selectDate($datepayment, '', 1, 1, 0, "add_payment", 1, 1, 0, '', '', $salary->dateep, '', 1, $langs->trans("DateEnd"));
240 print "</td>";
241 print '</tr>';
242
243 print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
244 $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $salary->type_payment, "paiementtype");
245 print "</td>\n";
246 print '</tr>';
247
248 print '<tr>';
249 print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
250 print '<td>';
251 print img_picto('', 'bank_account', 'class="pictofixedwidth"');
252 $form->select_comptes(GETPOSTISSET("accountid") ? GETPOSTINT("accountid") : $salary->accountid, "accountid", 0, '', 1); // Show opened bank account list
253 print '</td></tr>';
254
255 // Number
256 print '<tr><td>'.$langs->trans('Numero');
257 print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
258 print '</td>';
259 print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
260
261 print '<tr>';
262 print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
263 print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_2.'">';
264 print GETPOST('note');
265 print '</textarea></td>';
266 print '</tr>';
267
268 print '</table>';
269
270 print dol_get_fiche_end();
271
272
273 print '<br>';
274
275
276 // List of salaries unpaid
277 $num = 1;
278 $i = 0;
279
280 print '<table class="noborder centpercent">';
281 print '<tr class="liste_titre">';
282 //print '<td>'.$langs->trans("SocialContribution").'</td>';
283 print '<td class="left">'.$langs->trans("DateEnd").'</td>';
284 print '<td class="right">'.$langs->trans("Amount").'</td>';
285 print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
286 print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
287 print '<td class="center">'.$langs->trans("Amount").'</td>';
288 print "</tr>\n";
289
290 $total_ttc = 0.;
291 $totalrecu = 0;
292
293 while ($i < $num) {
294 $objp = $salary;
295
296 print '<tr class="oddeven">';
297
298 if ($objp->dateep > 0) {
299 print '<td class="left">'.dol_print_date($objp->dateep, 'day').'</td>'."\n";
300 } else {
301 print '<td align="center"><b>!!!</b></td>'."\n";
302 }
303
304 print '<td class="right">'.price($objp->amount)."</td>";
305
306 print '<td class="right">'.price($sumpaid)."</td>";
307
308 print '<td class="right">'.price((float) $objp->amount - $sumpaid)."</td>";
309
310 print '<td class="center">';
311 if ($sumpaid < $objp->amount) {
312 $namef = "amount_".$objp->id;
313 $nameRemain = "remain_".$objp->id;
314 /* Disabled, we autofil the amount with remain to pay by default
315 if (!empty($conf->use_javascript_ajax)) {
316 print img_picto("Auto fill", 'rightarrow.png', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
317 } */
318 $valuetoshow = GETPOSTISSET($namef) ? GETPOST($namef) : ((float) $objp->amount - $sumpaid);
319
320 print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$valuetoshow.'">';
321 print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$valuetoshow.'">';
322 } else {
323 print '-';
324 }
325 print "</td>";
326
327 print "</tr>\n";
328 $total_ttc += $objp->total_ttc;
329 $totalrecu += $objp->amount;
330 $i++;
331 }
332
333 /*
334 if ($i > 1) {
335 // Print total
336 print '<tr class="oddeven">';
337 print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
338 print '<td class="right"><b>'.price($total_ttc).'</b></td>';
339 print '<td class="right"><b>'.price($totalrecu).'</b></td>';
340 print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
341 print '<td align="center">&nbsp;</td>';
342 print "</tr>\n";
343 }
344 */
345
346 print "</table>";
347
348 print '<br>';
349
350 // Save payment button
351 print '<div class="center">';
352 print '<div class="paddingbottom"><input type="checkbox" checked name="closepaidsalary" id="closepaidsalary" class="marginrightonly"><label for="closepaidsalary" class="opacitymedium">'.$langs->trans("ClosePaidSalaryAutomatically").'</label></div>';
353 print $form->buttonsSaveCancel("ToMakePayment", "Cancel", array(), true);
354 print '</div>';
355
356
357 print "</form>\n";
358}
359
360llxFooter();
361$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
Class to manage payments of salaries.
Class to manage salary payments.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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).
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:489
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.