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