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) 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 <https://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->socid > 0) {
39  $socid = $user->socid;
40 }
41 
42 $object = new Don($db);
43 
44 
45 /*
46  * Actions
47  */
48 
49 if ($action == 'add_payment')
50 {
51  $error = 0;
52 
53  if ($_POST["cancel"])
54  {
55  $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
56  header("Location: ".$loc);
57  exit;
58  }
59 
60  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
61 
62  if (!$_POST["paymenttype"] > 0)
63  {
64  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
65  $error++;
66  }
67  if ($datepaid == '')
68  {
69  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
70  $error++;
71  }
72  if (!empty($conf->banque->enabled) && !$_POST["accountid"] > 0)
73  {
74  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
75  $error++;
76  }
77 
78  if (!$error)
79  {
80  $paymentid = 0;
81 
82  // Read possible payments
83  foreach ($_POST as $key => $value)
84  {
85  if (substr($key, 0, 7) == 'amount_')
86  {
87  $other_chid = substr($key, 7);
88  $amounts[$other_chid] = price2num($_POST[$key]);
89  }
90  }
91 
92  if (count($amounts) <= 0)
93  {
94  $error++;
95  $errmsg = 'ErrorNoPaymentDefined';
96  setEventMessages($errmsg, null, 'errors');
97  }
98 
99  if (!$error)
100  {
101  $db->begin();
102 
103  // Create a line of payments
104  $payment = new PaymentDonation($db);
105  $payment->chid = $chid;
106  $payment->datepaid = $datepaid;
107  $payment->amounts = $amounts; // Tableau de montant
108  $payment->paymenttype = GETPOST("paymenttype", 'int');
109  $payment->num_payment = GETPOST("num_payment", 'alphanohtml');
110  $payment->note_public = GETPOST("note_public", 'restricthtml');
111 
112  if (!$error)
113  {
114  $paymentid = $payment->create($user);
115  if ($paymentid < 0)
116  {
117  $errmsg = $payment->error;
118  setEventMessages($errmsg, null, 'errors');
119  $error++;
120  }
121  }
122 
123  if (!$error)
124  {
125  $result = $payment->addPaymentToBank($user, 'payment_donation', '(DonationPayment)', $_POST['accountid'], '', '');
126  if (!$result > 0)
127  {
128  $errmsg = $payment->error;
129  setEventMessages($errmsg, null, 'errors');
130  $error++;
131  }
132  }
133 
134  if (!$error)
135  {
136  $db->commit();
137  $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
138  header('Location: '.$loc);
139  exit;
140  } else {
141  $db->rollback();
142  }
143  }
144  }
145 
146  $action = 'create';
147 }
148 
149 
150 /*
151  * View
152  */
153 
154 $form = new Form($db);
155 
156 llxHeader();
157 
158 
159 $sql = "SELECT sum(p.amount) as total";
160 $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as p";
161 $sql .= " WHERE p.fk_donation = ".$chid;
162 $resql = $db->query($sql);
163 if ($resql)
164 {
165  $obj = $db->fetch_object($resql);
166  $sumpaid = $obj->total;
167  $db->free();
168 }
169 
170 
171 // Form to create donation payment
172 if ($action == 'create')
173 {
174  $object->fetch($chid);
175 
176  $total = $object->amount;
177 
178  print load_fiche_titre($langs->trans("DoPayment"));
179 
180  if (!empty($conf->use_javascript_ajax))
181  {
182  print "\n".'<script type="text/javascript" language="javascript">';
183  //Add js for AutoFill
184  print ' $(document).ready(function () {';
185  print ' $(".AutoFillAmout").on(\'click touchstart\', function(){
186  $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change");
187  });';
188  print ' });'."\n";
189 
190  print ' </script>'."\n";
191  }
192 
193  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
194  print '<input type="hidden" name="token" value="'.newToken().'">';
195  print '<input type="hidden" name="rowid" value="'.$chid.'">';
196  print '<input type="hidden" name="chid" value="'.$chid.'">';
197  print '<input type="hidden" name="action" value="add_payment">';
198 
200 
201  print '<table class="border centpercent tableforfieldcreate">';
202 
203  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
204  $datepaid = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
205  $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (empty($_POST["remonth"]) ?-1 : $datepaid) : 0;
206  print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $object->date, '', 1, $langs->trans("DonationDate"));
207  print "</td>";
208  print '</tr>';
209 
210  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
211  $form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype") : $object->paymenttype, "paymenttype");
212  print "</td>\n";
213  print '</tr>';
214 
215  print '<tr>';
216  print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
217  print '<td colspan="2">';
218  $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid") : $object->accountid, "accountid", 0, '', 2); // Show open bank account list
219  print '</td></tr>';
220 
221  // Number
222  print '<tr><td>'.$langs->trans('Numero');
223  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
224  print '</td>';
225  print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
226 
227  print '<tr>';
228  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
229  print '<td class="tdtop" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
230  print '</tr>';
231 
232  print '</table>';
233 
235 
236  /*
237  * List of payments on donation
238  */
239 
240  $num = 1;
241  $i = 0;
242 
243  print '<table class="noborder centpercent">';
244  print '<tr class="liste_titre">';
245  print '<td>'.$langs->trans("Donation").'</td>';
246  print '<td class="right">'.$langs->trans("Amount").'</td>';
247  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
248  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
249  print '<td class="center">'.$langs->trans("Amount").'</td>';
250  print "</tr>\n";
251 
252  $total = 0;
253  $totalrecu = 0;
254 
255  while ($i < $num)
256  {
257  $objp = $object;
258 
259  print '<tr class="oddeven">';
260 
261  print '<td>'.$object->getNomUrl(1)."</td>";
262 
263  print '<td class="right">'.price($objp->amount)."</td>";
264 
265  print '<td class="right">'.price($sumpaid)."</td>";
266 
267  print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
268 
269  print '<td class="center">';
270  if ($sumpaid < $objp->amount)
271  {
272  $namef = "amount_".$objp->id;
273  if (!empty($conf->use_javascript_ajax))
274  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmout' data-rowname='".$namef."' data-value='".price($objp->amount - $sumpaid)."'");
275  print '<input type="text" size="8" name="'.$namef.'">';
276  } else {
277  print '-';
278  }
279  print "</td>";
280 
281  print "</tr>\n";
282  /*$total+=$objp->total;
283  $total_ttc+=$objp->total_ttc;
284  $totalrecu+=$objp->am;*/ //Useless code ?
285  $i++;
286  }
287  /*if ($i > 1)
288  {
289  // Print total
290  print '<tr class="oddeven">';
291  print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
292  print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
293  print "<td class=\"right\"><b>".price($totalrecu)."</b></td>";
294  print "<td class=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
295  print '<td class="center">&nbsp;</td>';
296  print "</tr>\n";
297  }*/ //Useless code ?
298 
299  print "</table>";
300 
301  print '<br><div class="center">';
302  print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
303  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
304  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
305  print '</div>';
306 
307  print "</form>\n";
308 }
309 
310 llxFooter();
311 $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...
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 payments of donations.
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 donations.
Definition: don.class.php:37
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.
llxFooter()
Empty footer.
Definition: wrapper.php:59