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