dolibarr 21.0.0-beta
paiement_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-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
5 * Copyright (C) 2024 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 || !empty($ref)) {
54 $object->fetch($id, $ref);
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
65/*
66 * Actions
67 */
68
69if (($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) && $user->hasRight('salaries', 'write')) {
70 $error = 0;
71
72 if ($cancel) {
73 $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
74 header("Location: ".$loc);
75 exit;
76 }
77
78 $datepaye = dol_mktime(GETPOSTINT("rehour"), GETPOSTINT("remin"), GETPOSTINT("resec"), GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"), 'tzuserrel');
79
80 if (!(GETPOSTINT("paiementtype") > 0)) {
81 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
82 $error++;
83 $action = 'create';
84 }
85 if ($datepaye == '') {
86 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
87 $error++;
88 $action = 'create';
89 }
90 if (isModEnabled("bank") && !(GETPOSTINT("accountid") > 0)) {
91 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
92 $error++;
93 $action = 'create';
94 }
95
96 // Read possible payments
97 foreach ($_POST as $key => $value) {
98 if (substr($key, 0, 7) == 'amount_') {
99 $other_chid = substr($key, 7);
100 $amounts[$other_chid] = price2num(GETPOST($key));
101 }
102 }
103
104 if ($amounts[key($amounts)] <= 0) {
105 $error++;
106 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
107 $action = 'create';
108 }
109
110 if (!$error) {
111 $paymentid = 0;
112
113 if (!$error) {
114 $db->begin();
115
116 // Create a line of payments
117 $paiement = new PaymentSalary($db);
118 $paiement->fk_salary = $id;
119 $paiement->chid = $id; // deprecated
120 $paiement->datep = $datepaye;
121 $paiement->amounts = $amounts; // Tableau de montant
122 $paiement->fk_typepayment = GETPOSTINT("paiementtype");
123 $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
124 $paiement->note = GETPOST("note", 'restricthtml');
125 $paiement->note_private = GETPOST("note", 'restricthtml');
126
127 if (!$error) {
128 $paymentid = $paiement->create($user, (GETPOST('closepaidsalary') == 'on' ? 1 : 0));
129 if ($paymentid < 0) {
130 $error++;
131 setEventMessages($paiement->error, null, 'errors');
132 $action = 'create';
133 }
134 }
135
136 if (!$error) {
137 $result = $paiement->addPaymentToBank($user, 'payment_salary', '(SalaryPayment)', GETPOSTINT('accountid'), '', '');
138
139 if (!($result > 0)) {
140 $error++;
141 setEventMessages($paiement->error, null, 'errors');
142 $action = 'create';
143 }
144 }
145
146 if (!$error) {
147 $db->commit();
148 $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
149 header('Location: '.$loc);
150 exit;
151 } else {
152 $db->rollback();
153 }
154 }
155 }
156}
157
158
159/*
160 * View
161 */
162
163$form = new Form($db);
164
165$help_url = '';
166
167llxHeader('', '', $help_url);
168
169$salary = $object;
170$sumpaid = 0.0;
171
172// Formulaire de creation d'un paiement de charge
173if ($action == 'create') {
174 $salary->accountid = $salary->fk_account ? $salary->fk_account : $salary->accountid;
175 $salary->paiementtype = $salary->mode_reglement_id ? $salary->mode_reglement_id : $salary->paiementtype;
176
177 $total = $salary->amount;
178 if (!empty($conf->use_javascript_ajax)) {
179 print "\n".'<script type="text/javascript">';
180
181 //Add js for AutoFill
182 print ' $(document).ready(function () {';
183 print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
184 var amount = $(this).data("value");
185 document.getElementById($(this).data(\'rowid\')).value = amount ;
186 });';
187 print ' });'."\n";
188
189 print ' </script>'."\n";
190 }
191
192 print load_fiche_titre($langs->trans("DoPayment"));
193 print "<br>\n";
194
195 print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
196 print '<input type="hidden" name="token" value="'.newToken().'">';
197 print '<input type="hidden" name="id" value="'.$id.'">';
198 print '<input type="hidden" name="chid" value="'.$id.'">';
199 print '<input type="hidden" name="action" value="add_payment">';
200
201 print dol_get_fiche_head();
202
203 print '<table class="border centpercent">';
204
205 print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$id.'">'.$id.'</a></td></tr>';
206 print '<tr><td>'.$langs->trans("DateStart")."</td><td>".dol_print_date($salary->datesp, 'day')."</td></tr>\n";
207 print '<tr><td>'.$langs->trans("DateEnd")."</td><td>".dol_print_date($salary->dateep, 'day')."</td></tr>\n";
208 print '<tr><td>'.$langs->trans("Label").'</td><td>'.$salary->label."</td></tr>\n";
209 /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($salary->date_ech,'day')."</td></tr>\n";
210 print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($salary->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
211
212 $sql = "SELECT sum(p.amount) as total";
213 $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
214 $sql .= " WHERE p.fk_salary = ".((int) $id);
215 $resql = $db->query($sql);
216 if ($resql) {
217 $obj = $db->fetch_object($resql);
218 $sumpaid = (float) $obj->total;
219 $db->free($resql);
220 }
221 /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
222 print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
223
224 print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
225 $datepaye = dol_mktime(GETPOSTINT("rehour"), GETPOSTINT("remin"), GETPOSTINT("resec"), GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
226 $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOST("remonth") ? $datepaye : -1) : '';
227 print $form->selectDate($datepayment, '', 1, 1, 0, "add_payment", 1, 1, 0, '', '', $salary->dateep, '', 1, $langs->trans("DateEnd"));
228 print "</td>";
229 print '</tr>';
230
231 print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
232 $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $salary->type_payment, "paiementtype");
233 print "</td>\n";
234 print '</tr>';
235
236 print '<tr>';
237 print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
238 print '<td>';
239 print img_picto('', 'bank_account', 'class="pictofixedwidth"');
240 $form->select_comptes(GETPOSTISSET("accountid") ? GETPOSTINT("accountid") : $salary->accountid, "accountid", 0, '', 1); // Show opend bank account list
241 print '</td></tr>';
242
243 // Number
244 print '<tr><td>'.$langs->trans('Numero');
245 print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
246 print '</td>';
247 print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
248
249 print '<tr>';
250 print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
251 print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'">';
252 print GETPOST('note');
253 print '</textarea></td>';
254 print '</tr>';
255
256 print '</table>';
257
258 print dol_get_fiche_end();
259
260 /*
261 * Autres charges impayees
262 */
263 $num = 1;
264 $i = 0;
265
266 print '<table class="noborder centpercent">';
267 print '<tr class="liste_titre">';
268 //print '<td>'.$langs->trans("SocialContribution").'</td>';
269 print '<td class="left">'.$langs->trans("DateEnd").'</td>';
270 print '<td class="right">'.$langs->trans("Amount").'</td>';
271 print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
272 print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
273 print '<td class="center">'.$langs->trans("Amount").'</td>';
274 print "</tr>\n";
275
276 $total = 0;
277 $total_ttc = 0.;
278 $totalrecu = 0;
279
280 while ($i < $num) {
281 $objp = $salary;
282
283 print '<tr class="oddeven">';
284
285 if ($objp->dateep > 0) {
286 print '<td class="left">'.dol_print_date($objp->dateep, 'day').'</td>'."\n";
287 } else {
288 print '<td align="center"><b>!!!</b></td>'."\n";
289 }
290
291 print '<td class="right">'.price($objp->amount)."</td>";
292
293 print '<td class="right">'.price($sumpaid)."</td>";
294
295 print '<td class="right">'.price((float) $objp->amount - $sumpaid)."</td>";
296
297 print '<td class="center">';
298 if ($sumpaid < $objp->amount) {
299 $namef = "amount_".$objp->id;
300 $nameRemain = "remain_".$objp->id;
301 /* Disabled, we autofil the amount with remain to pay by default
302 if (!empty($conf->use_javascript_ajax)) {
303 print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
304 } */
305 $valuetoshow = GETPOSTISSET($namef) ? GETPOST($namef) : ((float) $objp->amount - $sumpaid);
306
307 print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$valuetoshow.'">';
308 print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$valuetoshow.'">';
309 } else {
310 print '-';
311 }
312 print "</td>";
313
314 print "</tr>\n";
315 $total += $objp->total;
316 $total_ttc += $objp->total_ttc;
317 $totalrecu += $objp->am;
318 $i++;
319 }
320 if ($i > 1) {
321 // Print total
322 print '<tr class="oddeven">';
323 print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
324 print '<td class="right"><b>'.price($total_ttc).'</b></td>';
325 print '<td class="right"><b>'.price($totalrecu).'</b></td>';
326 print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
327 print '<td align="center">&nbsp;</td>';
328 print "</tr>\n";
329 }
330
331 print "</table>";
332
333 print '<br>';
334
335 // Bouton Save payment
336 print '<div class="center">';
337 print '<div class="paddingbottom"><input type="checkbox" checked name="closepaidsalary" id="closepaidsalary"><label for="closepaidsalary">'.$langs->trans("ClosePaidSalaryAutomatically").'</label></div>';
338 print $form->buttonsSaveCancel("ToMakePayment", "Cancel", '', true);
339 print '</div>';
340
341
342 print "</form>\n";
343}
344
345llxFooter();
346$db->close();
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:87
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:71
Class to manage generation of HTML components Only common components must be here.
Class to manage payments of salaries.
Class to manage salary payments.
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...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
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.
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).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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.