dolibarr  19.0.0-dev
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
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/salaries/class/salary.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
30 require_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);
44 if ($id > 0 || !empty($ref)) {
45  $object->fetch($id, $ref);
46 }
47 
48 // Security check
49 $socid = GETPOST("socid", "int");
50 if ($user->socid > 0) {
51  $socid = $user->socid;
52 }
53 restrictedArea($user, 'salaries', $object->id, 'salary', '');
54 
55 
56 /*
57  * Actions
58  */
59 
60 if (($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(GETPOST("rehour", 'int'), GETPOST("remin", 'int'), GETPOST("resec", 'int'), GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'), 'tzuserrel');
70 
71  if (!(GETPOST("paiementtype", 'int') > 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("banque") && !(GETPOST("accountid", 'int') > 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->chid = $id;
110  $paiement->datepaye = $datepaye;
111  $paiement->amounts = $amounts; // Tableau de montant
112  $paiement->paiementtype = GETPOST("paiementtype", 'alphanohtml');
113  $paiement->num_payment = GETPOST("num_payment", 'alphanohtml');
114  $paiement->note = GETPOST("note", 'restricthtml');
115  $paiement->note_private = GETPOST("note", 'restricthtml');
116 
117  if (!$error) {
118  $paymentid = $paiement->create($user, (GETPOST('closepaidsalary') == 'on' ? 1 : 0));
119  if ($paymentid < 0) {
120  $error++;
121  setEventMessages($paiement->error, null, 'errors');
122  $action = 'create';
123  }
124  }
125 
126  if (!$error) {
127  $result = $paiement->addPaymentToBank($user, 'payment_salary', '(SalaryPayment)', GETPOST('accountid', 'int'), '', '');
128  if (!($result > 0)) {
129  $error++;
130  setEventMessages($paiement->error, null, 'errors');
131  $action = 'create';
132  }
133  }
134 
135  if (!$error) {
136  $db->commit();
137  $loc = DOL_URL_ROOT.'/salaries/card.php?id='.$id;
138  header('Location: '.$loc);
139  exit;
140  } else {
141  $db->rollback();
142  }
143  }
144  }
145 }
146 
147 
148 /*
149  * View
150  */
151 
152 $form = new Form($db);
153 
154 $help_url = '';
155 
156 llxHeader('', '', $help_url);
157 
158 $salary = $object;
159 
160 // Formulaire de creation d'un paiement de charge
161 if ($action == 'create') {
162  $salary->accountid = $salary->fk_account ? $salary->fk_account : $salary->accountid;
163  $salary->paiementtype = $salary->mode_reglement_id ? $salary->mode_reglement_id : $salary->paiementtype;
164 
165  $total = $salary->amount;
166  if (!empty($conf->use_javascript_ajax)) {
167  print "\n".'<script type="text/javascript">';
168 
169  //Add js for AutoFill
170  print ' $(document).ready(function () {';
171  print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
172  var amount = $(this).data("value");
173  document.getElementById($(this).data(\'rowid\')).value = amount ;
174  });';
175  print ' });'."\n";
176 
177  print ' </script>'."\n";
178  }
179 
180  print load_fiche_titre($langs->trans("DoPayment"));
181  print "<br>\n";
182 
183  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
184  print '<input type="hidden" name="token" value="'.newToken().'">';
185  print '<input type="hidden" name="id" value="'.$id.'">';
186  print '<input type="hidden" name="chid" value="'.$id.'">';
187  print '<input type="hidden" name="action" value="add_payment">';
188 
189  print dol_get_fiche_head();
190 
191  print '<table class="border centpercent">';
192 
193  print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td><a href="'.DOL_URL_ROOT.'/salaries/card.php?id='.$id.'">'.$id.'</a></td></tr>';
194  print '<tr><td>'.$langs->trans("DateStart")."</td><td>".dol_print_date($salary->datesp, 'day')."</td></tr>\n";
195  print '<tr><td>'.$langs->trans("DateEnd")."</td><td>".dol_print_date($salary->dateep, 'day')."</td></tr>\n";
196  print '<tr><td>'.$langs->trans("Label").'</td><td>'.$salary->label."</td></tr>\n";
197  /*print '<tr><td>'.$langs->trans("DateDue")."</td><td>".dol_print_date($salary->date_ech,'day')."</td></tr>\n";
198  print '<tr><td>'.$langs->trans("Amount")."</td><td>".price($salary->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
199 
200  $sql = "SELECT sum(p.amount) as total";
201  $sql .= " FROM ".MAIN_DB_PREFIX."payment_salary as p";
202  $sql .= " WHERE p.fk_salary = ".((int) $id);
203  $resql = $db->query($sql);
204  if ($resql) {
205  $obj = $db->fetch_object($resql);
206  $sumpaid = $obj->total;
207  $db->free($resql);
208  }
209  /*print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
210  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';*/
211 
212  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td>';
213  $datepaye = dol_mktime(GETPOST("rehour", 'int'), GETPOST("remin", 'int'), GETPOST("resec", 'int'), GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
214  $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth") ? $datepaye : -1) : '';
215  print $form->selectDate($datepayment, '', 1, 1, 0, "add_payment", 1, 1, 0, '', '', $salary->dateep, '', 1, $langs->trans("DateEnd"));
216  print "</td>";
217  print '</tr>';
218 
219  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td>';
220  $form->select_types_paiements(GETPOSTISSET("paiementtype") ? GETPOST("paiementtype") : $salary->type_payment, "paiementtype");
221  print "</td>\n";
222  print '</tr>';
223 
224  print '<tr>';
225  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
226  print '<td>';
227  print img_picto('', 'bank_account', 'class="pictofixedwidth"');
228  $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", 'int') : $salary->accountid, "accountid", 0, '', 1); // Show opend bank account list
229  print '</td></tr>';
230 
231  // Number
232  print '<tr><td>'.$langs->trans('Numero');
233  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
234  print '</td>';
235  print '<td><input name="num_payment" type="text" value="'.GETPOST('num_payment', 'alphanohtml').'"></td></tr>'."\n";
236 
237  print '<tr>';
238  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
239  print '<td class="tdtop"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
240  print '</tr>';
241 
242  print '</table>';
243 
244  print dol_get_fiche_end();
245 
246  /*
247  * Autres charges impayees
248  */
249  $num = 1;
250  $i = 0;
251 
252  print '<table class="noborder centpercent">';
253  print '<tr class="liste_titre">';
254  //print '<td>'.$langs->trans("SocialContribution").'</td>';
255  print '<td class="left">'.$langs->trans("DateEnd").'</td>';
256  print '<td class="right">'.$langs->trans("Amount").'</td>';
257  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
258  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
259  print '<td class="center">'.$langs->trans("Amount").'</td>';
260  print "</tr>\n";
261 
262  $total = 0;
263  $totalrecu = 0;
264 
265  while ($i < $num) {
266  $objp = $salary;
267 
268  print '<tr class="oddeven">';
269 
270  if ($objp->dateep > 0) {
271  print '<td class="left">'.dol_print_date($objp->dateep, 'day').'</td>'."\n";
272  } else {
273  print '<td align="center"><b>!!!</b></td>'."\n";
274  }
275 
276  print '<td class="right">'.price($objp->amount)."</td>";
277 
278  print '<td class="right">'.price($sumpaid)."</td>";
279 
280  print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
281 
282  print '<td class="center">';
283  if ($sumpaid < $objp->amount) {
284  $namef = "amount_".$objp->id;
285  $nameRemain = "remain_".$objp->id;
286  /* Disabled, we autofil the amount with remain to pay by default
287  if (!empty($conf->use_javascript_ajax)) {
288  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->amount - $sumpaid)."'");
289  } */
290  $valuetoshow = GETPOSTISSET($namef) ? GETPOST($namef) : ($objp->amount - $sumpaid);
291 
292  print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$valuetoshow.'">';
293  print '<input type="text" class="right width75" name="'.$namef.'" id="'.$namef.'" value="'.$valuetoshow.'">';
294  } else {
295  print '-';
296  }
297  print "</td>";
298 
299  print "</tr>\n";
300  $total += $objp->total;
301  $total_ttc += $objp->total_ttc;
302  $totalrecu += $objp->am;
303  $i++;
304  }
305  if ($i > 1) {
306  // Print total
307  print '<tr class="oddeven">';
308  print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
309  print '<td class="right"><b>'.price($total_ttc).'</b></td>';
310  print '<td class="right"><b>'.price($totalrecu).'</b></td>';
311  print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
312  print '<td align="center">&nbsp;</td>';
313  print "</tr>\n";
314  }
315 
316  print "</table>";
317 
318  print '<br>';
319 
320  // Bouton Save payment
321  print '<div class="center">';
322  print '<div class="paddingbottom"><input type="checkbox" checked name="closepaidsalary" id="closepaidsalary"><label for="closepaidsalary">'.$langs->trans("ClosePaidSalaryAutomatically").'</label></div>';
323  print $form->buttonsSaveCancel("ToMakePayment", "Cancel", '', true);
324  print '</div>';
325 
326 
327  print "</form>\n";
328 }
329 
330 llxFooter();
331 $db->close();
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:118
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
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('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
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...
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOSTINT($paramname, $method=0)
Return value of a param into GET or POST supervariable.
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='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
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.