dolibarr  9.0.0
payment.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
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 <http://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->societe_id > 0)
43 {
44  $socid = $user->societe_id;
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->chid = $expensereport->id;
117  $payment->datepaid = $datepaid;
118  $payment->amounts = $amounts; // Tableau de montant
119  $payment->total = $total;
120  $payment->fk_typepayment = $_POST["fk_typepayment"];
121  $payment->num_payment = $_POST["num_payment"];
122  $payment->note = $_POST["note"];
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  }
162  else
163  {
164  $db->rollback();
165  }
166  }
167  }
168 
169  $action='create';
170 }
171 
172 
173 /*
174  * View
175  */
176 
177 llxHeader();
178 
179 $form=new Form($db);
180 
181 
182 // Form to create expense report payment
183 if ($action == 'create' || empty($action))
184 {
185  $expensereport = new ExpenseReport($db);
186  $expensereport->fetch($id, $ref);
187 
188  $total = $expensereport->total_ttc;
189 
190  print load_fiche_titre($langs->trans("DoPayment"));
191 
192  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
193  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
194  print '<input type="hidden" name="id" value="'.$expensereport->id.'">';
195  print '<input type="hidden" name="chid" value="'.$expensereport->id.'">';
196  print '<input type="hidden" name="action" value="add_payment">';
197 
198  dol_fiche_head(null, '0', '', -1);
199 
200  $linkback = '';
201  // $linkback = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/list.php">' . $langs->trans("BackToList") . '</a>';
202 
203  dol_banner_tab($expensereport, 'ref', $linkback, 1, 'ref', 'ref', '');
204 
205  print '<div class="fichecenter">';
206  print '<div class="underbanner clearboth"></div>';
207 
208  print '<table class="border centpercent">'."\n";
209 
210  print '<tr><td class="titlefield">'.$langs->trans("Period").'</td><td>'.get_date_range($expensereport->date_debut,$expensereport->date_fin,"",$langs,0).'</td></tr>';
211  print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($expensereport->total_ttc,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
212 
213  $sql = "SELECT sum(p.amount) as total";
214  $sql.= " FROM ".MAIN_DB_PREFIX."payment_expensereport as p, ".MAIN_DB_PREFIX."expensereport as e";
215  $sql.= " WHERE p.fk_expensereport = e.rowid AND p.fk_expensereport = ".$id;
216  $sql.= ' AND e.entity IN ('.getEntity('expensereport').')';
217  $resql = $db->query($sql);
218  if ($resql)
219  {
220  $obj=$db->fetch_object($resql);
221  $sumpaid = $obj->total;
222  $db->free();
223  }
224  print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
225  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
226 
227  print '</table>';
228 
229  print '<br>';
230 
231  print '<div class="underbanner clearboth"></div>';
232 
233  print '<table class="border centpercent">'."\n";
234 
235  print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
236  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
237  $datepayment=empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaid):0;
238  print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
239  print "</td>";
240  print '</tr>';
241 
242  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
243  $form->select_types_paiements(isset($_POST["fk_typepayment"])?$_POST["fk_typepayment"]:$expensereport->fk_typepayment, "fk_typepayment");
244  print "</td>\n";
245  print '</tr>';
246 
247  if (! empty($conf->banque->enabled))
248  {
249  print '<tr>';
250  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
251  print '<td colspan="2">';
252  $form->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$expensereport->accountid, "accountid", 0, '',1); // Show open bank account list
253  print '</td></tr>';
254  }
255 
256  // Number
257  print '<tr><td>'.$langs->trans('Numero');
258  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
259  print '</td>';
260  print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
261 
262  print '<tr>';
263  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
264  print '<td valign="top" colspan="2"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
265  print '</tr>';
266 
267  print '</table>';
268 
269  print '</div>';
270 
271  dol_fiche_end();
272 
273  // List of expenses ereport not already paid completely
274  $num = 1;
275  $i = 0;
276 
277  print '<table class="noborder" width="100%">';
278  print '<tr class="liste_titre">';
279  print '<td align="right">'.$langs->trans("Amount").'</td>';
280  print '<td align="right">'.$langs->trans("AlreadyPaid").'</td>';
281  print '<td align="right">'.$langs->trans("RemainderToPay").'</td>';
282  print '<td align="center">'.$langs->trans("Amount").'</td>';
283  print "</tr>\n";
284 
285  $total=0;
286  $totalrecu=0;
287 
288  while ($i < $num)
289  {
290  $objp = $expensereport;
291 
292  print '<tr class="oddeven">';
293 
294  print '<td align="right">'.price($objp->total_ttc)."</td>";
295  print '<td align="right">'.price($sumpaid)."</td>";
296  print '<td align="right">'.price($objp->total_ttc - $sumpaid)."</td>";
297  print '<td align="center">';
298  if ($sumpaid < $objp->total_ttc)
299  {
300  $namef = "amount_".$objp->id;
301  print '<input type="text" size="8" name="'.$namef.'">';
302  }
303  else
304  {
305  print '-';
306  }
307  print "</td>";
308 
309  print "</tr>\n";
310 
311  $total+=$objp->total;
312  $total_ttc+=$objp->total_ttc;
313  $totalrecu+=$objp->am;
314  $i++;
315  }
316  if ($i > 1)
317  {
318  // Print total
319  print '<tr class="oddeven">';
320  print '<td colspan="2" align="left">'.$langs->trans("Total").':</td>';
321  print "<td align=\"right\"><b>".price($total_ttc)."</b></td>";
322  print "<td align=\"right\"><b>".price($totalrecu)."</b></td>";
323  print "<td align=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
324  print '<td align="center">&nbsp;</td>';
325  print "</tr>\n";
326  }
327 
328  print "</table>";
329 
330  print '<br><div class="center">';
331  print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
332  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
333  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
334  print '</div>';
335 
336  print "</form>\n";
337 }
338 
339 // End of page
340 llxFooter();
341 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
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.
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
get_date_range($date_start, $date_end, $format='', $outputlangs='', $withparenthesis=1)
Format output for start and end date.
Class to manage Trips and Expenses.
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...
Class to manage payments of expense report.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.