dolibarr  7.0.0-beta
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
25 require '../../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
29 
30 $langs->load("bills");
31 $langs->load("banks");
32 $langs->load("trips");
33 
34 $chid=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='.$chid;
59  header("Location: ".$loc);
60  exit;
61  }
62 
63  $expensereport = new ExpenseReport($db);
64  $expensereport->fetch($chid, $ref);
65 
66  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
67 
68  if (! ($_POST["fk_typepayment"] > 0))
69  {
70  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode")), null, 'errors');
71  $error++;
72  }
73  if ($datepaid == '')
74  {
75  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("Date")), null, 'errors');
76  $error++;
77  }
78  if (! empty($conf->banque->enabled) && ! ($accountid > 0))
79  {
80  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("AccountToDebit")), null, 'errors');
81  $error++;
82  }
83 
84  if (! $error)
85  {
86  $paymentid = 0;
87  $total = 0;
88 
89  // Read possible payments
90  foreach ($_POST as $key => $value)
91  {
92  if (substr($key,0,7) == 'amount_')
93  {
94  $amounts[$expensereport->fk_user_author] = price2num($_POST[$key]);
95  $total += price2num($_POST[$key]);
96  }
97  }
98 
99  if (count($amounts) <= 0)
100  {
101  $error++;
102  $errmsg='ErrorNoPaymentDefined';
103  }
104 
105  if (! $error)
106  {
107  $db->begin();
108 
109  // Create a line of payments
110  $payment = new PaymentExpenseReport($db);
111  $payment->chid = $chid;
112  $payment->datepaid = $datepaid;
113  $payment->amounts = $amounts; // Tableau de montant
114  $payment->total = $total;
115  $payment->fk_typepayment = $_POST["fk_typepayment"];
116  $payment->num_payment = $_POST["num_payment"];
117  $payment->note = $_POST["note"];
118 
119  if (! $error)
120  {
121  $paymentid = $payment->create($user);
122  if ($paymentid < 0)
123  {
124  $errmsg=$payment->error;
125  $error++;
126  }
127  }
128 
129  if (! $error)
130  {
131  $result=$payment->addPaymentToBank($user,'payment_expensereport','(ExpenseReportPayment)',$accountid,'','');
132  if (! $result > 0)
133  {
134  $errmsg=$payment->error;
135  $error++;
136  }
137  }
138 
139  if (!$error) {
140  $payment->fetch($paymentid);
141  if ($expensereport->total_ttc - $payment->amount == 0) {
142  $result = $expensereport->set_paid($expensereport->id, $user);
143  if (!$result > 0) {
144  $errmsg = $payment->error;
145  $error++;
146  }
147  }
148 
149  }
150 
151  if (! $error)
152  {
153  $db->commit();
154  $loc = DOL_URL_ROOT.'/expensereport/card.php?id='.$chid;
155  header('Location: '.$loc);
156  exit;
157  }
158  else
159  {
160  $db->rollback();
161  }
162  }
163  }
164 
165  $action='create';
166 }
167 
168 
169 /*
170  * View
171  */
172 
173 llxHeader();
174 
175 $form=new Form($db);
176 
177 
178 // Form to create expense report payment
179 if ($action == 'create' || empty($action))
180 {
181  $expensereport = new ExpenseReport($db);
182  $expensereport->fetch($chid, $ref);
183 
184  $total = $expensereport->total_ttc;
185 
186  print load_fiche_titre($langs->trans("DoPayment"));
187 
188  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
189  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
190  print '<input type="hidden" name="id" value="'.$chid.'">';
191  print '<input type="hidden" name="chid" value="'.$chid.'">';
192  print '<input type="hidden" name="action" value="add_payment">';
193 
194  dol_fiche_head(null, '0', '', -1);
195 
196  $linkback = '';
197  // $linkback = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/list.php">' . $langs->trans("BackToList") . '</a>';
198 
199  dol_banner_tab($expensereport, 'ref', $linkback, 1, 'ref', 'ref', '');
200 
201  print '<div class="fichecenter">';
202  print '<div class="underbanner clearboth"></div>';
203 
204  print '<table class="border centpercent">'."\n";
205 
206  print '<tr><td class="titlefield">'.$langs->trans("Period").'</td><td>'.get_date_range($expensereport->date_debut,$expensereport->date_fin,"",$langs,0).'</td></tr>';
207  print '<tr><td>'.$langs->trans("Amount").'</td><td>'.price($expensereport->total_ttc,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
208 
209  $sql = "SELECT sum(p.amount) as total";
210  $sql.= " FROM ".MAIN_DB_PREFIX."payment_expensereport as p, ".MAIN_DB_PREFIX."expensereport as e";
211  $sql.= " WHERE p.fk_expensereport = e.rowid AND p.fk_expensereport = ".$chid;
212  $sql.= ' AND e.entity IN ('.getEntity('expensereport').')';
213  $resql = $db->query($sql);
214  if ($resql)
215  {
216  $obj=$db->fetch_object($resql);
217  $sumpaid = $obj->total;
218  $db->free();
219  }
220  print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td>'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
221  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td>'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
222 
223  print '</table>';
224 
225  print '<br>';
226 
227  print '<div class="underbanner clearboth"></div>';
228 
229  print '<table class="border centpercent">'."\n";
230 
231  print '<tr><td class="titlefield fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
232  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
233  $datepayment=empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaid):0;
234  $form->select_date($datepayment,'','','','',"add_payment",1,1);
235  print "</td>";
236  print '</tr>';
237 
238  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
239  $form->select_types_paiements(isset($_POST["fk_typepayment"])?$_POST["fk_typepayment"]:$expensereport->fk_typepayment, "fk_typepayment");
240  print "</td>\n";
241  print '</tr>';
242 
243  if (! empty($conf->banque->enabled))
244  {
245  print '<tr>';
246  print '<td class="fieldrequired">'.$langs->trans('AccountToDebit').'</td>';
247  print '<td colspan="2">';
248  $form->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$expensereport->accountid, "accountid", 0, '',1); // Show open bank account list
249  print '</td></tr>';
250  }
251 
252  // Number
253  print '<tr><td>'.$langs->trans('Numero');
254  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
255  print '</td>';
256  print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
257 
258  print '<tr>';
259  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
260  print '<td valign="top" colspan="2"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
261  print '</tr>';
262 
263  print '</table>';
264 
265  print '</div>';
266 
267  dol_fiche_end();
268 
269  // List of expenses ereport not already paid completely
270  $num = 1;
271  $i = 0;
272 
273  print '<table class="noborder" width="100%">';
274  print '<tr class="liste_titre">';
275  print '<td align="right">'.$langs->trans("Amount").'</td>';
276  print '<td align="right">'.$langs->trans("AlreadyPaid").'</td>';
277  print '<td align="right">'.$langs->trans("RemainderToPay").'</td>';
278  print '<td align="center">'.$langs->trans("Amount").'</td>';
279  print "</tr>\n";
280 
281  $var=true;
282  $total=0;
283  $totalrecu=0;
284 
285  while ($i < $num)
286  {
287  $objp = $expensereport;
288 
289  print '<tr class="oddeven">';
290 
291  print '<td align="right">'.price($objp->total_ttc)."</td>";
292  print '<td align="right">'.price($sumpaid)."</td>";
293  print '<td align="right">'.price($objp->total_ttc - $sumpaid)."</td>";
294  print '<td align="center">';
295  if ($sumpaid < $objp->total_ttc)
296  {
297  $namef = "amount_".$objp->id;
298  print '<input type="text" size="8" name="'.$namef.'">';
299  }
300  else
301  {
302  print '-';
303  }
304  print "</td>";
305 
306  print "</tr>\n";
307 
308  $total+=$objp->total;
309  $total_ttc+=$objp->total_ttc;
310  $totalrecu+=$objp->am;
311  $i++;
312  }
313  if ($i > 1)
314  {
315  // Print total
316  print '<tr class="oddeven">';
317  print '<td colspan="2" align="left">'.$langs->trans("Total").':</td>';
318  print "<td align=\"right\"><b>".price($total_ttc)."</b></td>";
319  print "<td align=\"right\"><b>".price($totalrecu)."</b></td>";
320  print "<td align=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
321  print '<td align="center">&nbsp;</td>';
322  print "</tr>\n";
323  }
324 
325  print "</table>";
326 
327  print '<br><div class="center">';
328  print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
329  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
330  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
331  print '</div>';
332 
333  print "</form>\n";
334 }
335 
336 llxFooter();
337 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header 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.
if(empty($reshook)) $form
View.
Definition: perms.php:103
get_date_range($date_start, $date_end, $format= '', $outputlangs='', $withparenthesis=1)
Format output for start and end date.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
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...
print
Draft customers invoices.
Definition: index.php:91
Class to manage payments of expense report.
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:1013
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...