dolibarr  9.0.0
payment.php
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
3  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
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.'/don/class/don.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
29 
30 $langs->load("bills");
31 
32 $chid=GETPOST("rowid");
33 $action=GETPOST('action','aZ09');
34 $amounts = array();
35 
36 // Security check
37 $socid=0;
38 if ($user->societe_id > 0)
39 {
40  $socid = $user->societe_id;
41 }
42 
43 
44 /*
45  * Actions
46  */
47 
48 if ($action == 'add_payment')
49 {
50  $error=0;
51 
52  if ($_POST["cancel"])
53  {
54  $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
55  header("Location: ".$loc);
56  exit;
57  }
58 
59  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
60 
61  if (! $_POST["paymenttype"] > 0)
62  {
63  $mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("PaymentMode"));
64  $error++;
65  }
66  if ($datepaid == '')
67  {
68  $mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("Date"));
69  $error++;
70  }
71  if (! empty($conf->banque->enabled) && ! $_POST["accountid"] > 0)
72  {
73  $mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("AccountToCredit"));
74  $error++;
75  }
76 
77  if (! $error)
78  {
79  $paymentid = 0;
80 
81  // Read possible payments
82  foreach ($_POST as $key => $value)
83  {
84  if (substr($key,0,7) == 'amount_')
85  {
86  $other_chid = substr($key,7);
87  $amounts[$other_chid] = price2num($_POST[$key]);
88  }
89  }
90 
91  if (count($amounts) <= 0)
92  {
93  $error++;
94  $errmsg='ErrorNoPaymentDefined';
95  }
96 
97  if (! $error)
98  {
99  $db->begin();
100 
101  // Create a line of payments
102  $payment = new PaymentDonation($db);
103  $payment->chid = $chid;
104  $payment->datepaid = $datepaid;
105  $payment->amounts = $amounts; // Tableau de montant
106  $payment->paymenttype = $_POST["paymenttype"];
107  $payment->num_payment = $_POST["num_payment"];
108  $payment->note = $_POST["note"];
109 
110  if (! $error)
111  {
112  $paymentid = $payment->create($user);
113  if ($paymentid < 0)
114  {
115  $errmsg=$payment->error;
116  $error++;
117  }
118  }
119 
120  if (! $error)
121  {
122  $result=$payment->addPaymentToBank($user,'payment_donation','(DonationPayment)',$_POST['accountid'],'','');
123  if (! $result > 0)
124  {
125  $errmsg=$payment->error;
126  $error++;
127  }
128  }
129 
130  if (! $error)
131  {
132  $db->commit();
133  $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
134  header('Location: '.$loc);
135  exit;
136  }
137  else
138  {
139  $db->rollback();
140  }
141  }
142  }
143 
144  $_GET["action"]='create';
145 }
146 
147 
148 /*
149  * View
150  */
151 
152 llxHeader();
153 
154 $form=new Form($db);
155 
156 
157 // Form to create donation payment
158 if (GETPOST('action','aZ09') == 'create')
159 {
160 
161  $don = new Don($db);
162  $don->fetch($chid);
163 
164  $total = $don->amount;
165 
166  print load_fiche_titre($langs->trans("DoPayment"));
167 
168  if ($mesg)
169  {
170  print "<div class=\"error\">$mesg</div>";
171  }
172 
173  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
174  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
175  print '<input type="hidden" name="rowid" value="'.$chid.'">';
176  print '<input type="hidden" name="chid" value="'.$chid.'">';
177  print '<input type="hidden" name="action" value="add_payment">';
178 
179  dol_fiche_head();
180 
181  print '<table cellspacing="0" class="border" width="100%" cellpadding="2">';
182 
183  print '<tr class="liste_titre"><td colspan="3">'.$langs->trans("Donation").'</td>';
184 
185  print '<tr><td>'.$langs->trans("Ref").'</td><td colspan="2"><a href="'.DOL_URL_ROOT.'/don/card.php?rowid='.$chid.'">'.$chid.'</a></td></tr>';
186  print '<tr><td>'.$langs->trans("Date")."</td><td colspan=\"2\">".dol_print_date($don->date,'day')."</td></tr>\n";
187  print '<tr><td>'.$langs->trans("Amount")."</td><td colspan=\"2\">".price($don->amount,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
188 
189  $sql = "SELECT sum(p.amount) as total";
190  $sql.= " FROM ".MAIN_DB_PREFIX."payment_donation as p";
191  $sql.= " WHERE p.fk_donation = ".$chid;
192  $resql = $db->query($sql);
193  if ($resql)
194  {
195  $obj=$db->fetch_object($resql);
196  $sumpaid = $obj->total;
197  $db->free();
198  }
199  print '<tr><td>'.$langs->trans("AlreadyPaid").'</td><td colspan="2">'.price($sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
200  print '<tr><td class="tdtop">'.$langs->trans("RemainderToPay").'</td><td colspan="2">'.price($total-$sumpaid,0,$outputlangs,1,-1,-1,$conf->currency).'</td></tr>';
201 
202  print '<tr class="liste_titre">';
203  print "<td colspan=\"3\">".$langs->trans("Payment").'</td>';
204  print '</tr>';
205 
206  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
207  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
208  $datepayment=empty($conf->global->MAIN_AUTOFILL_DATE)?(empty($_POST["remonth"])?-1:$datepaid):0;
209  print $form->selectDate($datepayment, '', '', '', '', "add_payment", 1, 1);
210  print "</td>";
211  print '</tr>';
212 
213  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
214  $form->select_types_paiements(isset($_POST["paymenttype"])?$_POST["paymenttype"]:$don->paymenttype, "paymenttype");
215  print "</td>\n";
216  print '</tr>';
217 
218  print '<tr>';
219  print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
220  print '<td colspan="2">';
221  $form->select_comptes(isset($_POST["accountid"])?$_POST["accountid"]:$don->accountid, "accountid", 0, '',1); // Show open bank account list
222  print '</td></tr>';
223 
224  // Number
225  print '<tr><td>'.$langs->trans('Numero');
226  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
227  print '</td>';
228  print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
229 
230  print '<tr>';
231  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
232  print '<td valign="top" colspan="2"><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
233  print '</tr>';
234 
235  print '</table>';
236 
237  dol_fiche_end();
238 
239  /*
240  * Autres charges impayees
241  */
242  $num = 1;
243  $i = 0;
244 
245  print '<table class="noborder" width="100%">';
246  print '<tr class="liste_titre">';
247  print '<td align="right">'.$langs->trans("Amount").'</td>';
248  print '<td align="right">'.$langs->trans("AlreadyPaid").'</td>';
249  print '<td align="right">'.$langs->trans("RemainderToPay").'</td>';
250  print '<td align="center">'.$langs->trans("Amount").'</td>';
251  print "</tr>\n";
252 
253  $total=0;
254  $totalrecu=0;
255 
256  while ($i < $num)
257  {
258  $objp = $don;
259 
260  print '<tr class="oddeven">';
261 
262  print '<td align="right">'.price($objp->amount)."</td>";
263 
264  print '<td align="right">'.price($sumpaid)."</td>";
265 
266  print '<td align="right">'.price($objp->amount - $sumpaid)."</td>";
267 
268  print '<td align="center">';
269  if ($sumpaid < $objp->amount)
270  {
271  $namef = "amount_".$objp->id;
272  print '<input type="text" size="8" name="'.$namef.'">';
273  }
274  else
275  {
276  print '-';
277  }
278  print "</td>";
279 
280  print "</tr>\n";
281  $total+=$objp->total;
282  $total_ttc+=$objp->total_ttc;
283  $totalrecu+=$objp->am;
284  $i++;
285  }
286  if ($i > 1)
287  {
288  // Print total
289  print '<tr class="oddeven">';
290  print '<td colspan="2" align="left">'.$langs->trans("Total").':</td>';
291  print "<td align=\"right\"><b>".price($total_ttc)."</b></td>";
292  print "<td align=\"right\"><b>".price($totalrecu)."</b></td>";
293  print "<td align=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
294  print '<td align="center">&nbsp;</td>';
295  print "</tr>\n";
296  }
297 
298  print "</table>";
299 
300  print '<br><div class="center">';
301  print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
302  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
303  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
304  print '</div>';
305 
306  print "</form>\n";
307 }
308 
309 llxFooter();
310 $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
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...
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
Class to manage payments of donations.
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...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class to manage donations.
Definition: don.class.php:35
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.