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