dolibarr 20.0.0
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-2018 Frédéric France <frederic.france@netlogic.fr>
4 * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
29require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
31
32// Load translation files required by the page
33$langs->loadLangs(array("banks", "bills"));
34
35$action = GETPOST('action', 'alpha');
36$cancel = GETPOST('cancel', 'alpha');
37$confirm = GETPOST('confirm', 'alpha');
38
39$id = GETPOSTINT('id');
40$ref = GETPOST('ref', 'alpha');
41$amounts = array();
42
43$object = new Salary($db);
44if ($id > 0 || !empty($ref)) {
45 $object->fetch($id, $ref);
46}
47
48// Security check
49$socid = GETPOSTINT("socid");
50if ($user->socid > 0) {
51 $socid = $user->socid;
52}
53restrictedArea($user, 'salaries', $object->id, 'salary', '');
54
55
56/*
57 * Actions
58 */
59
60if (($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) && $user->hasRight('salaries', 'write')) {
61 $error = 0;
62
63 if ($cancel) {
64 $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
65 header("Location: ".$loc);
66 exit;
67 }
68
69 $datepaye = dol_mktime(GETPOSTINT("rehour"), GETPOSTINT("remin"), GETPOSTINT("resec"), GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"), 'tzuserrel');
70
71 if (!(GETPOSTINT("paiementtype") > 0)) {
72 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
73 $error++;
74 $action = 'create';
75 }
76 if ($datepaye == '') {
77 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
78 $error++;
79 $action = 'create';
80 }
81 if (isModEnabled("bank") && !(GETPOSTINT("accountid") > 0)) {
82 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
83 $error++;
84 $action = 'create';
85 }
86
87 // Read possible payments
88 foreach ($_POST as $key => $value) {
89 if (substr($key, 0, 7) == 'amount_') {
90 $other_chid = substr($key, 7);
91 $amounts[$other_chid] = price2num(GETPOST($key));
92 }
93 }
94
95 if ($amounts[key($amounts)] <= 0) {
96 $error++;
97 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
98 $action = 'create';
99 }
100
101 if (!$error) {
102 $paymentid = 0;
103
104 if (!$error) {
105 $db->begin();
106
107 // Create a line of payments
108 $paiement = new PaymentSalary($db);
109 $paiement->fk_salary = $id;
110 $paiement->chid = $id; // deprecated
111 $paiement->datep = $datepaye;
112 $paiement->amounts = $amounts; // Tableau de montant
113 $paiement->fk_typepayment = GETPOSTINT("paiementtype");
114 $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
115 $paiement->note = GETPOST("note", 'restricthtml');
116 $paiement->note_private = GETPOST("note", 'restricthtml');
117
118 if (!$error) {
119 $paymentid = $paiement->create($user, (GETPOST('closepaidsalary') == 'on' ? 1 : 0));
120 if ($paymentid < 0) {
121 $error++;
122 setEventMessages($paiement->error, null, 'errors');
123 $action = 'create';
124 }
125 }
126
127 if (!$error) {
128 $result = $paiement->addPaymentToBank($user, 'payment_salary', '(SalaryPayment)', GETPOSTINT('accountid'), '', '');
129
130 if (!($result > 0)) {
131 $error++;
132 setEventMessages($paiement->error, null, 'errors');
133 $action = 'create';
134 }
135 }
136
137 if (!$error) {
138 $db->commit();
139 $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
140 header('Location: '.$loc);
141 exit;
142 } else {
143 $db->rollback();
144 }
145 }
146 }
147}
148
149
150/*
151 * View
152 */
153
154$form = new Form($db);
155
156$help_url = '';
157
158llxHeader('', '', $help_url);
159
160$salary = $object;
161
162// Formulaire de creation d'un paiement de charge
163if ($action == 'create') {
164 $salary->accountid = $salary->fk_account ? $salary->fk_account : $salary->accountid;
165 $salary->paiementtype = $salary->mode_reglement_id ? $salary->mode_reglement_id : $salary->paiementtype;
166
167 $total = $salary->amount;
168 if (!empty($conf->use_javascript_ajax)) {
169 print "\n".'<script type="text/javascript">';
170
171 //Add js for AutoFill
172 print ' $(document).ready(function () {';
173 print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
174 var amount = $(this).data("value");
175 document.getElementById($(this).data(\'rowid\')).value = amount ;
176 });';
177 print ' });'."\n";
178
179 print ' </script>'."\n";
180 }
181
182 print load_fiche_titre($langs->trans("DoPayment"));
183 print "<br>\n";
184
185 print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
186 print '<input type="hidden" name="token" value="'.newToken().'">';
187 print '<input type="hidden" name="id" value="'.$id.'">';
188 print '<input type="hidden" name="chid" value="'.$id.'">';
189 print '<input type="hidden" name="action" value="add_payment">';
190
191 print dol_get_fiche_head();
192
193 print '<table class="border centpercent">';
194
195 print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$id.'">'.$id.'</a></td></tr>';
196 print '<tr><td>'.$langs->trans("DateStart")."</td><td>".dol_print_date($salary->datesp, 'day')."</td></tr>\n";
197 print '<tr><td>'.$langs->trans("DateEnd")."</td><td>".dol_print_date($salary->dateep, 'day')."</td></tr>\n";
198 print '<tr><td>'.$langs->trans("Label").'</td><td>'.$salary->label."</td></tr>\n";
199 /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($salary->date_ech,'day')."</td></tr>\n";
200 print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($salary->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
201
202 $sql = "SELECT sum(p.amount) as total";
203 $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
204 $sql .= " WHERE p.fk_salary = ".((int) $id);
205 $resql = $db->query($sql);
206 if ($resql) {
207 $obj = $db->fetch_object($resql);
208 $sumpaid = $obj->total;
209 $db->free($resql);
210 }
211 /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
212 print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
213
214 print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
215 $datepaye = dol_mktime(GETPOSTINT("rehour"), GETPOSTINT("remin"), GETPOSTINT("resec"), GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
216 $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOST("remonth") ? $datepaye : -1) : '';
217 print $form->selectDate($datepayment, '', 1, 1, 0, "add_payment", 1, 1, 0, '', '', $salary->dateep, '', 1, $langs->trans("DateEnd"));
218 print "</td>";
219 print '</tr>';
220
221 print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
222 $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $salary->type_payment, "paiementtype");
223 print "</td>\n";
224 print '</tr>';
225
226 print '<tr>';
227 print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
228 print '<td>';
229 print img_picto('', 'bank_account', 'class="pictofixedwidth"');
230 $form->select_comptes(GETPOSTISSET("accountid") ? GETPOSTINT("accountid") : $salary->accountid, "accountid", 0, '', 1); // Show opend bank account list
231 print '</td></tr>';
232
233 // Number
234 print '<tr><td>'.$langs->trans('Numero');
235 print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
236 print '</td>';
237 print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
238
239 print '<tr>';
240 print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
241 print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'">';
242 print GETPOST('note');
243 print '</textarea></td>';
244 print '</tr>';
245
246 print '</table>';
247
248 print dol_get_fiche_end();
249
250 /*
251 * Autres charges impayees
252 */
253 $num = 1;
254 $i = 0;
255
256 print '<table class="noborder centpercent">';
257 print '<tr class="liste_titre">';
258 //print '<td>'.$langs->trans("SocialContribution").'</td>';
259 print '<td class="left">'.$langs->trans("DateEnd").'</td>';
260 print '<td class="right">'.$langs->trans("Amount").'</td>';
261 print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
262 print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
263 print '<td class="center">'.$langs->trans("Amount").'</td>';
264 print "</tr>\n";
265
266 $total = 0;
267 $total_ttc = 0.;
268 $totalrecu = 0;
269
270 while ($i < $num) {
271 $objp = $salary;
272
273 print '<tr class="oddeven">';
274
275 if ($objp->dateep > 0) {
276 print '<td class="left">'.dol_print_date($objp->dateep, 'day').'</td>'."\n";
277 } else {
278 print '<td align="center"><b>!!!</b></td>'."\n";
279 }
280
281 print '<td class="right">'.price($objp->amount)."</td>";
282
283 print '<td class="right">'.price($sumpaid)."</td>";
284
285 print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
286
287 print '<td class="center">';
288 if ($sumpaid < $objp->amount) {
289 $namef = "amount_".$objp->id;
290 $nameRemain = "remain_".$objp->id;
291 /* Disabled, we autofil the amount with remain to pay by default
292 if (!empty($conf->use_javascript_ajax)) {
293 print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
294 } */
295 $valuetoshow = GETPOSTISSET($namef) ? GETPOST($namef) : ($objp->amount - $sumpaid);
296
297 print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$valuetoshow.'">';
298 print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$valuetoshow.'">';
299 } else {
300 print '-';
301 }
302 print "</td>";
303
304 print "</tr>\n";
305 $total += $objp->total;
306 $total_ttc += $objp->total_ttc;
307 $totalrecu += $objp->am;
308 $i++;
309 }
310 if ($i > 1) {
311 // Print total
312 print '<tr class="oddeven">';
313 print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
314 print '<td class="right"><b>'.price($total_ttc).'</b></td>';
315 print '<td class="right"><b>'.price($totalrecu).'</b></td>';
316 print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
317 print '<td align="center">&nbsp;</td>';
318 print "</tr>\n";
319 }
320
321 print "</table>";
322
323 print '<br>';
324
325 // Bouton Save payment
326 print '<div class="center">';
327 print '<div class="paddingbottom"><input type="checkbox" checked name="closepaidsalary" id="closepaidsalary"><label for="closepaidsalary">'.$langs->trans("ClosePaidSalaryAutomatically").'</label></div>';
328 print $form->buttonsSaveCancel("ToMakePayment", "Cancel", '', true);
329 print '</div>';
330
331
332 print "</form>\n";
333}
334
335llxFooter();
336$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
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.
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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.