dolibarr  16.0.5
payment.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
3  * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.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.'/expensereport/class/expensereport.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.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('bills', 'banks', 'trips'));
33 
34 $id = GETPOST("id", 'int');
35 $ref = GETPOST('ref', 'alpha');
36 $action = GETPOST('action', 'aZ09');
37 $amounts = array();
38 $accountid = GETPOST('accountid', 'int');
39 $cancel = GETPOST('cancel');
40 
41 // Security check
42 $socid = 0;
43 if ($user->socid > 0) {
44  $socid = $user->socid;
45 }
46 
47 
48 /*
49  * Actions
50  */
51 
52 if ($action == 'add_payment') {
53  $error = 0;
54 
55  if ($cancel) {
56  $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id;
57  header("Location: ".$loc);
58  exit;
59  }
60 
61  $expensereport = new ExpenseReport($db);
62  $result = $expensereport->fetch($id, $ref);
63  if (!$result) {
64  $error++;
65  setEventMessages($expensereport->error, $expensereport->errors, 'errors');
66  }
67 
68  $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
69 
70  if (!(GETPOST("fk_typepayment", 'int') > 0)) {
71  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
72  $error++;
73  }
74  if ($datepaid == '') {
75  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
76  $error++;
77  }
78 
79  if (!empty($conf->banque->enabled) && !($accountid > 0)) {
80  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
81  $error++;
82  }
83 
84  if (!$error) {
85  $paymentid = 0;
86  $total = 0;
87 
88  // Read possible payments
89  foreach ($_POST as $key => $value) {
90  if (substr($key, 0, 7) == 'amount_') {
91  if (GETPOST($key)) {
92  $amounts[$expensereport->fk_user_author] = price2num(GETPOST($key));
93  $total += price2num(GETPOST($key));
94  }
95  }
96  }
97 
98  if (count($amounts) <= 0) {
99  $error++;
100  setEventMessages('ErrorNoPaymentDefined', null, 'errors');
101  }
102 
103  if (!$error) {
104  $db->begin();
105 
106  // Create a line of payments
107  $payment = new PaymentExpenseReport($db);
108  $payment->fk_expensereport = $expensereport->id;
109  $payment->datepaid = $datepaid;
110  $payment->amounts = $amounts; // Tableau de montant
111  $payment->total = $total;
112  $payment->fk_typepayment = GETPOST("fk_typepayment", 'int');
113  $payment->num_payment = GETPOST("num_payment", 'alphanothtml');
114  $payment->note_public = GETPOST("note_public", 'restricthtml');
115  $payment->fk_bank = $accountid;
116 
117  if (!$error) {
118  $paymentid = $payment->create($user);
119  if ($paymentid < 0) {
120  setEventMessages($payment->error, $payment->errors, 'errors');
121  $error++;
122  }
123  }
124 
125  if (!$error) {
126  $result = $payment->addPaymentToBank($user, 'payment_expensereport', '(ExpenseReportPayment)', $accountid, '', '');
127  if ($result <= 0) {
128  setEventMessages($payment->error, $payment->errors, 'errors');
129  $error++;
130  }
131  }
132 
133  if (!$error) {
134  $payment->fetch($paymentid);
135  if ($expensereport->total_ttc - $payment->amount == 0) {
136  $result = $expensereport->setPaid($expensereport->id, $user);
137  if (!($result > 0)) {
138  setEventMessages($payment->error, $payment->errors, 'errors');
139  $error++;
140  }
141  }
142  }
143 
144  if (!$error) {
145  $db->commit();
146  $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id;
147  header('Location: '.$loc);
148  exit;
149  } else {
150  $db->rollback();
151  }
152  }
153  }
154 
155  $action = 'create';
156 }
157 
158 
159 /*
160  * View
161  */
162 
163 llxHeader();
164 
165 $form = new Form($db);
166 
167 
168 // Form to create expense report payment
169 if ($action == 'create' || empty($action)) {
170  $expensereport = new ExpenseReport($db);
171  $expensereport->fetch($id, $ref);
172 
173  $total = $expensereport->total_ttc;
174 
175  // autofill remainder amount
176  if (!empty($conf->use_javascript_ajax)) {
177  print "\n".'<script type="text/javascript">';
178  //Add js for AutoFill
179  print ' $(document).ready(function () {';
180  print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
181  var amount = $(this).data("value");
182  document.getElementById($(this).data(\'rowid\')).value = amount ;
183  });';
184  print "\t});\n";
185  print "</script>\n";
186  }
187 
188  print load_fiche_titre($langs->trans("DoPayment"));
189 
190  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
191  print '<input type="hidden" name="token" value="'.newToken().'">';
192  print '<input type="hidden" name="id" value="'.$expensereport->id.'">';
193  print '<input type="hidden" name="chid" value="'.$expensereport->id.'">';
194  print '<input type="hidden" name="action" value="add_payment">';
195 
196  print dol_get_fiche_head(null, '0', '', -1);
197 
198  $linkback = '';
199  // $linkback = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/list.php">' . $langs->trans("BackToList") . '</a>';
200 
201  dol_banner_tab($expensereport, 'ref', $linkback, 1, 'ref', 'ref', '');
202 
203  print '<div class="fichecenter">';
204  print '<div class="underbanner clearboth"></div>';
205 
206  print '<table class="border centpercent">'."\n";
207 
208  print '<tr><td class="titlefield">'.$langs->trans("Period").'</td><td>'.get_date_range($expensereport->date_debut, $expensereport->date_fin, "", $langs, 0).'</td></tr>';
209  print '<tr><td>'.$langs->trans("Amount").'</td><td><span class="amount">'.price($expensereport->total_ttc, 0, $langs, 1, -1, -1, $conf->currency).'</span></td></tr>';
210 
211  $sql = "SELECT sum(p.amount) as total";
212  $sql .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as p, ".MAIN_DB_PREFIX."expensereport as e";
213  $sql .= " WHERE p.fk_expensereport = e.rowid AND p.fk_expensereport = ".((int) $id);
214  $sql .= ' AND e.entity IN ('.getEntity('expensereport').')';
215  $resql = $db->query($sql);
216  if ($resql) {
217  $obj = $db->fetch_object($resql);
218  $sumpaid = $obj->total;
219  $db->free($resql);
220  }
221  print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td><span class="amount">'.price($sumpaid, 0, $langs, 1, -1, -1, $conf->currency).'</span></td></tr>';
222  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td><span class="amount">'.price($total - $sumpaid, 0, $langs, 1, -1, -1, $conf->currency).'</span></td></tr>';
223 
224  print '</table>';
225 
226  print '</div>';
227 
228  print dol_get_fiche_end();
229 
230  print '<br>';
231 
232  print dol_get_fiche_head();
233 
234  print '<table class="border centpercent">'."\n";
235 
236  print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
237  $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
238  $datepayment = ($datepaid == '' ? (empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 : '') : $datepaid);
239  print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1);
240  print "</td>";
241  print '</tr>';
242 
243  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
244  $form->select_types_paiements(GETPOSTISSET("fk_typepayment") ? GETPOST("fk_typepayment", 'alpha') : $expensereport->fk_c_paiement, "fk_typepayment");
245  print "</td>\n";
246  print '</tr>';
247 
248  if (!empty($conf->banque->enabled)) {
249  print '<tr>';
250  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
251  print '<td colspan="2">';
252  print img_picto('', 'bank_account', 'class="pictofixedwidth"');
253  $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", "int") : 0, "accountid", 0, '', 2); // Show open bank account list
254  print '</td></tr>';
255  }
256 
257  // Number
258  print '<tr><td>'.$langs->trans('Numero');
259  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
260  print '</td>';
261  print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
262 
263  print '<tr>';
264  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
265  print '<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
266  print '</tr>';
267 
268  print '</table>';
269 
270  print dol_get_fiche_end();
271 
272  print '<br>';
273 
274  // List of expenses ereport not already paid completely
275  $num = 1;
276  $i = 0;
277 
278  print '<table class="noborder centpercent">';
279  print '<tr class="liste_titre">';
280  print '<td>'.$langs->trans("ExpenseReport").'</td>';
281  print '<td class="right">'.$langs->trans("Amount").'</td>';
282  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
283  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
284  print '<td class="center">'.$langs->trans("Amount").'</td>';
285  print "</tr>\n";
286 
287  $total_ttc = 0;
288  $totalrecu = 0;
289 
290  while ($i < $num) {
291  $objp = $expensereport;
292 
293  print '<tr class="oddeven">';
294 
295  print '<td>'.$expensereport->getNomUrl(1)."</td>";
296  print '<td class="right">'.price($objp->total_ttc)."</td>";
297  print '<td class="right">'.price($sumpaid)."</td>";
298  print '<td class="right">'.price($objp->total_ttc - $sumpaid)."</td>";
299  print '<td class="center">';
300  if ($sumpaid < $objp->total_ttc) {
301  $namef = "amount_".$objp->id;
302  $nameRemain = "remain_".$objp->id; // autofill remainder amount
303  if (!empty($conf->use_javascript_ajax)) { // autofill remainder amount
304  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->total_ttc - $sumpaid)."'"); // autofill remainder amount
305  }
306  $remaintopay = $objp->total_ttc - $sumpaid; // autofill remainder amount
307  print '<input type=hidden class="sum_remain" name="'.$nameRemain.'" value="'.$remaintopay.'">'; // autofill remainder amount
308  print '<input type="text" class="width75" name="'.$namef.'" id="'.$namef.'" value="'.GETPOST($namef).'">';
309  } else {
310  print '-';
311  }
312  print "</td>";
313 
314  print "</tr>\n";
315 
316  $total_ttc += $objp->total_ttc;
317  $totalrecu += $sumpaid;
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 class="center">&nbsp;</td>';
328  print "</tr>\n";
329  }
330 
331  print "</table>";
332 
333  print $form->buttonsSaveCancel();
334 
335  print "</form>\n";
336 }
337 
338 // End of page
339 llxFooter();
340 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
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
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2046
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
get_date_range
get_date_range($date_start, $date_end, $format='', $outputlangs='', $withparenthesis=1)
Format output for start and end date.
Definition: functions.lib.php:8031
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
PaymentExpenseReport
Class to manage payments of expense report.
Definition: paymentexpensereport.class.php:31
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
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
ExpenseReport
Class to manage Trips and Expenses.
Definition: expensereport.class.php:36
$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
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5541
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