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