dolibarr  16.0.5
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 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array("banks", "bills"));
33 
34 $action = GETPOST('action', 'alpha');
35 $cancel = GETPOST('cancel', 'alpha');
36 $confirm = GETPOST('confirm', 'alpha');
37 
38 $id = GETPOSTINT('id');
39 $ref = GETPOST('ref', 'alpha');
40 $amounts = array();
41 
42 $object = new Salary($db);
43 if ($id > 0 || !empty($ref)) {
44  $object->fetch($id, $ref);
45 }
46 
47 // Security check
48 $socid = GETPOST("socid", "int");
49 if ($user->socid > 0) {
50  $socid = $user->socid;
51 }
52 restrictedArea($user, 'salaries', $object->id, 'salary', '');
53 
54 
55 /*
56  * Actions
57  */
58 
59 if ($action == 'add_payment' || ($action == 'confirm_paiement' && $confirm == 'yes')) {
60  $error = 0;
61 
62  if ($cancel) {
63  $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
64  header("Location: ".$loc);
65  exit;
66  }
67 
68  $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
69 
70  if (!(GETPOST("paiementtype", 'int') > 0)) {
71  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
72  $error++;
73  $action = 'create';
74  }
75  if ($datepaye == '') {
76  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
77  $error++;
78  $action = 'create';
79  }
80  if (isModEnabled('banque') && !(GETPOST("accountid", 'int') > 0)) {
81  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
82  $error++;
83  $action = 'create';
84  }
85 
86  // Read possible payments
87  foreach ($_POST as $key => $value) {
88  if (substr($key, 0, 7) == 'amount_') {
89  $other_chid = substr($key, 7);
90  $amounts[$other_chid] = price2num(GETPOST($key));
91  }
92  }
93 
94  if ($amounts[key($amounts)] <= 0) {
95  $error++;
96  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount")), null, 'errors');
97  $action = 'create';
98  }
99 
100  if (!$error) {
101  $paymentid = 0;
102 
103  if (!$error) {
104  $db->begin();
105 
106  // Create a line of payments
107  $paiement = new PaymentSalary($db);
108  $paiement->chid = $id;
109  $paiement->datepaye = $datepaye;
110  $paiement->amounts = $amounts; // Tableau de montant
111  $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
112  $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
113  $paiement->note = GETPOST("note", 'restricthtml');
114  $paiement->note_private = GETPOST("note", 'restricthtml');
115 
116  if (!$error) {
117  $paymentid = $paiement->create($user, (GETPOST('closepaidsalary') == 'on' ? 1 : 0));
118  if ($paymentid < 0) {
119  $error++;
120  setEventMessages($paiement->error, null, 'errors');
121  $action = 'create';
122  }
123  }
124 
125  if (!$error) {
126  $result = $paiement->addPaymentToBank($user, 'payment_salary', '(SalaryPayment)', GETPOST('accountid', 'int'), '', '');
127  if (!($result > 0)) {
128  $error++;
129  setEventMessages($paiement->error, null, 'errors');
130  $action = 'create';
131  }
132  }
133 
134  if (!$error) {
135  $db->commit();
136  $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
137  header('Location: '.$loc);
138  exit;
139  } else {
140  $db->rollback();
141  }
142  }
143  }
144 }
145 
146 
147 /*
148  * View
149  */
150 
151 $form = new Form($db);
152 
153 $help_url = '';
154 
155 llxHeader('', '', $help_url);
156 
157 $salary = $object;
158 
159 // Formulaire de creation d'un paiement de charge
160 if ($action == 'create') {
161  $salary->accountid = $salary->fk_account ? $salary->fk_account : $salary->accountid;
162  $salary->paiementtype = $salary->mode_reglement_id ? $salary->mode_reglement_id : $salary->paiementtype;
163 
164  $total = $salary->amount;
165  if (!empty($conf->use_javascript_ajax)) {
166  print "\n".'<script type="text/javascript">';
167 
168  //Add js for AutoFill
169  print ' $(document).ready(function () {';
170  print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
171  var amount = $(this).data("value");
172  document.getElementById($(this).data(\'rowid\')).value = amount ;
173  });';
174  print ' });'."\n";
175 
176  print ' </script>'."\n";
177  }
178 
179  print load_fiche_titre($langs->trans("DoPayment"));
180  print "<br>\n";
181 
182  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
183  print '<input type="hidden" name="token" value="'.newToken().'">';
184  print '<input type="hidden" name="id" value="'.$id.'">';
185  print '<input type="hidden" name="chid" value="'.$id.'">';
186  print '<input type="hidden" name="action" value="add_payment">';
187 
188  print dol_get_fiche_head();
189 
190  print '<table class="border centpercent">';
191 
192  print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$id.'">'.$id.'</a></td></tr>';
193  print '<tr><td>'.$langs->trans("DateStart")."</td><td>".dol_print_date($salary->datesp, 'day')."</td></tr>\n";
194  print '<tr><td>'.$langs->trans("DateEnd")."</td><td>".dol_print_date($salary->dateep, 'day')."</td></tr>\n";
195  print '<tr><td>'.$langs->trans("Label").'</td><td>'.$salary->label."</td></tr>\n";
196  /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($salary->date_ech,'day')."</td></tr>\n";
197  print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($salary->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
198 
199  $sql = "SELECT sum(p.amount) as total";
200  $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
201  $sql .= " WHERE p.fk_salary = ".((int) $id);
202  $resql = $db->query($sql);
203  if ($resql) {
204  $obj = $db->fetch_object($resql);
205  $sumpaid = $obj->total;
206  $db->free($resql);
207  }
208  /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
209  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
210 
211  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
212  $datepaye = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
213  $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth") ? $datepaye : -1) : '';
214  print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
215  print "</td>";
216  print '</tr>';
217 
218  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
219  $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $salary->type_payment, "paiementtype");
220  print "</td>\n";
221  print '</tr>';
222 
223  print '<tr>';
224  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
225  print '<td>';
226  print img_picto('', 'bank_account', 'class="pictofixedwidth"');
227  $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $salary->accountid, "accountid", 0, '', 1); // Show opend bank account list
228  print '</td></tr>';
229 
230  // Number
231  print '<tr><td>'.$langs->trans('Numero');
232  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
233  print '</td>';
234  print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
235 
236  print '<tr>';
237  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
238  print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
239  print '</tr>';
240 
241  print '</table>';
242 
243  print dol_get_fiche_end();
244 
245  /*
246  * Autres charges impayees
247  */
248  $num = 1;
249  $i = 0;
250 
251  print '<table class="noborder centpercent">';
252  print '<tr class="liste_titre">';
253  //print '<td>'.$langs->trans("SocialContribution").'</td>';
254  print '<td class="left">'.$langs->trans("DateEnd").'</td>';
255  print '<td class="right">'.$langs->trans("Amount").'</td>';
256  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
257  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
258  print '<td class="center">'.$langs->trans("Amount").'</td>';
259  print "</tr>\n";
260 
261  $total = 0;
262  $totalrecu = 0;
263 
264  while ($i < $num) {
265  $objp = $salary;
266 
267  print '<tr class="oddeven">';
268 
269  if ($objp->dateep > 0) {
270  print '<td class="left">'.dol_print_date($objp->dateep, 'day').'</td>'."\n";
271  } else {
272  print '<td align="center"><b>!!!</b></td>'."\n";
273  }
274 
275  print '<td class="right">'.price($objp->amount)."</td>";
276 
277  print '<td class="right">'.price($sumpaid)."</td>";
278 
279  print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
280 
281  print '<td class="center">';
282  if ($sumpaid < $objp->amount) {
283  $namef = "amount_".$objp->id;
284  $nameRemain = "remain_".$objp->id;
285  /* Disabled, we autofil the amount with remain to pay by default
286  if (!empty($conf->use_javascript_ajax)) {
287  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
288  } */
289  $valuetoshow = GETPOSTISSET($namef) ? GETPOST($namef) : ($objp->amount - $sumpaid);
290 
291  print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$valuetoshow.'">';
292  print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$valuetoshow.'">';
293  } else {
294  print '-';
295  }
296  print "</td>";
297 
298  print "</tr>\n";
299  $total += $objp->total;
300  $total_ttc += $objp->total_ttc;
301  $totalrecu += $objp->am;
302  $i++;
303  }
304  if ($i > 1) {
305  // Print total
306  print '<tr class="oddeven">';
307  print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
308  print '<td class="right"><b>'.price($total_ttc).'</b></td>';
309  print '<td class="right"><b>'.price($totalrecu).'</b></td>';
310  print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
311  print '<td align="center">&nbsp;</td>';
312  print "</tr>\n";
313  }
314 
315  print "</table>";
316 
317  print '<br>';
318 
319  // Bouton Save payment
320  print '<div class="center">';
321  print '<div class="paddingbottom"><input type="checkbox" checked name="closepaidsalary" id="closepaidsalary"><label for="closepaidsalary">'.$langs->trans("ClosePaidSalaryAutomatically").'</label></div>';
322  print $form->buttonsSaveCancel("ToMakePayment", "Cancel", '', true);
323  print '</div>';
324 
325 
326  print "</form>\n";
327 }
328 
329 llxFooter();
330 $db->close();
restrictedArea
restrictedArea($user, $features, $objectid=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.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
PaymentSalary
Class to manage payments of salaries.
Definition: paymentsalary.class.php:34
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
$help_url
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:116
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
GETPOSTINT
GETPOSTINT($paramname, $method=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:795
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
Salary
Class to manage salary payments.
Definition: salary.class.php:33
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
dol_mktime
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
Definition: functions.lib.php:2757
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59