dolibarr  13.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 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 
40 // Security check
41 $socid = 0;
42 if ($user->socid > 0)
43 {
44  $socid = $user->socid;
45 }
46 
47 
48 /*
49  * Actions
50  */
51 
52 if ($action == 'add_payment')
53 {
54  $error = 0;
55 
56  if ($_POST["cancel"])
57  {
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  {
67  $error++;
68  setEventMessages($expensereport->error, $expensereport->errors, 'errors');
69  }
70 
71  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
72 
73  if (!($_POST["fk_typepayment"] > 0))
74  {
75  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
76  $error++;
77  }
78  if ($datepaid == '')
79  {
80  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
81  $error++;
82  }
83  if (!empty($conf->banque->enabled) && !($accountid > 0))
84  {
85  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToDebit")), null, 'errors');
86  $error++;
87  }
88 
89  if (!$error)
90  {
91  $paymentid = 0;
92  $total = 0;
93 
94  // Read possible payments
95  foreach ($_POST as $key => $value)
96  {
97  if (substr($key, 0, 7) == 'amount_')
98  {
99  $amounts[$expensereport->fk_user_author] = price2num($_POST[$key]);
100  $total += price2num($_POST[$key]);
101  }
102  }
103 
104  if (count($amounts) <= 0)
105  {
106  $error++;
107  $errmsg = 'ErrorNoPaymentDefined';
108  }
109 
110  if (!$error)
111  {
112  $db->begin();
113 
114  // Create a line of payments
115  $payment = new PaymentExpenseReport($db);
116  $payment->fk_expensereport = $expensereport->id;
117  $payment->datepaid = $datepaid;
118  $payment->amounts = $amounts; // Tableau de montant
119  $payment->total = $total;
120  $payment->fk_typepayment = GETPOST("fk_typepayment", 'int');
121  $payment->num_payment = GETPOST("num_payment", 'alphanothtml');
122  $payment->note_public = GETPOST("note_public", 'restricthtml');
123 
124  if (!$error)
125  {
126  $paymentid = $payment->create($user);
127  if ($paymentid < 0)
128  {
129  setEventMessages($payment->error, $payment->errors, 'errors');
130  $error++;
131  }
132  }
133 
134  if (!$error)
135  {
136  $result = $payment->addPaymentToBank($user, 'payment_expensereport', '(ExpenseReportPayment)', $accountid, '', '');
137  if (!$result > 0)
138  {
139  setEventMessages($payment->error, $payment->errors, 'errors');
140  $error++;
141  }
142  }
143 
144  if (!$error) {
145  $payment->fetch($paymentid);
146  if ($expensereport->total_ttc - $payment->amount == 0) {
147  $result = $expensereport->set_paid($expensereport->id, $user);
148  if (!$result > 0) {
149  setEventMessages($payment->error, $payment->errors, 'errors');
150  $error++;
151  }
152  }
153  }
154 
155  if (!$error)
156  {
157  $db->commit();
158  $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$id;
159  header('Location: '.$loc);
160  exit;
161  } else {
162  $db->rollback();
163  }
164  }
165  }
166 
167  $action = 'create';
168 }
169 
170 
171 /*
172  * View
173  */
174 
175 llxHeader();
176 
177 $form = new Form($db);
178 
179 
180 // Form to create expense report payment
181 if ($action == 'create' || empty($action))
182 {
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" language="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(null, '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>'.price($expensereport->total_ttc, 0, $outputlangs, 1, -1, -1, $conf->currency).'</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 = ".$id;
227  $sql .= ' AND e.entity IN ('.getEntity('expensereport').')';
228  $resql = $db->query($sql);
229  if ($resql)
230  {
231  $obj = $db->fetch_object($resql);
232  $sumpaid = $obj->total;
233  $db->free();
234  }
235  print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
236  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total - $sumpaid, 0, $outputlangs, 1, -1, -1, $conf->currency).'</td></tr>';
237 
238  print '</table>';
239 
240  print '</div>';
241 
243 
245 
246  print '<table class="border centpercent">'."\n";
247 
248  print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
249  $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth", 'int'), GETPOST("reday", 'int'), GETPOST("reyear", 'int'));
250  $datepayment = ($datepaid == '' ? (empty($conf->global->MAIN_AUTOFILL_DATE) ?-1 : '') : $datepaid);
251  print $form->selectDate($datepayment, '', '', '', 0, "add_payment", 1, 1);
252  print "</td>";
253  print '</tr>';
254 
255  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
256  $form->select_types_paiements(GETPOSTISSET("fk_typepayment") ? GETPOST("fk_typepayment", 'alpha') : $expensereport->fk_c_paiement, "fk_typepayment");
257  print "</td>\n";
258  print '</tr>';
259 
260  if (!empty($conf->banque->enabled))
261  {
262  print '<tr>';
263  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
264  print '<td colspan="2">';
265  $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid", "int") : $expensereport->accountid, "accountid", 0, '', 2); // Show open bank account list
266  print '</td></tr>';
267  }
268 
269  // Number
270  print '<tr><td>'.$langs->trans('Numero');
271  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
272  print '</td>';
273  print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
274 
275  print '<tr>';
276  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
277  print '<td valign="top" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
278  print '</tr>';
279 
280  print '</table>';
281 
283 
284  print '<br>';
285 
286  // List of expenses ereport not already paid completely
287  $num = 1;
288  $i = 0;
289 
290  print '<table class="noborder centpercent">';
291  print '<tr class="liste_titre">';
292  print '<td>'.$langs->trans("ExpenseReport").'</td>';
293  print '<td class="right">'.$langs->trans("Amount").'</td>';
294  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
295  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
296  print '<td class="center">'.$langs->trans("Amount").'</td>';
297  print "</tr>\n";
298 
299  $total = 0;
300  $totalrecu = 0;
301 
302  while ($i < $num)
303  {
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  {
315  $namef = "amount_".$objp->id;
316  $nameRemain = "remain_".$objp->id; // autofill remainder amount
317  if (!empty($conf->use_javascript_ajax)) // autofill remainder amount
318  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowid='".$namef."' data-value='".($objp->total_ttc - $sumpaid)."'"); // autofill remainder amount
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" size="8" name="'.$namef.'" id="'.$namef.'">';
322  } else {
323  print '-';
324  }
325  print "</td>";
326 
327  print "</tr>\n";
328 
329  $total += $objp->total;
330  $total_ttc += $objp->total_ttc;
331  $totalrecu += $objp->am;
332  $i++;
333  }
334  if ($i > 1)
335  {
336  // Print total
337  print '<tr class="oddeven">';
338  print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
339  print '<td class="right"><b>'.price($total_ttc).'</b></td>';
340  print '<td class="right"><b>'.price($totalrecu).'</b></td>';
341  print '<td class="right"><b>'.price($total_ttc - $totalrecu).'</b></td>';
342  print '<td class="center">&nbsp;</td>';
343  print "</tr>\n";
344  }
345 
346  print "</table>";
347 
348  print '<br><div class="center">';
349  print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
350  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
351  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
352  print '</div>';
353 
354  print "</form>\n";
355 }
356 
357 // End of page
358 llxFooter();
359 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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...
get_date_range($date_start, $date_end, $format= '', $outputlangs= '', $withparenthesis=1)
Format output for start and end date.
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...
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
Class to manage Trips and Expenses.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
Class to manage payments of expense report.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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.
llxFooter()
Empty footer.
Definition: wrapper.php:59