dolibarr  16.0.5
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", 'int');
33 $action = GETPOST('action', 'aZ09');
34 $amounts = array();
35 $cancel = GETPOST('cancel');
36 
37 // Security check
38 $socid = 0;
39 if ($user->socid > 0) {
40  $socid = $user->socid;
41 }
42 
43 $object = new Don($db);
44 
45 
46 /*
47  * Actions
48  */
49 
50 if ($action == 'add_payment') {
51  $error = 0;
52 
53  if ($cancel) {
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, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
60 
61  if (!(GETPOST("paymenttype") > 0)) {
62  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
63  $error++;
64  }
65  if ($datepaid == '') {
66  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
67  $error++;
68  }
69  if (!empty($conf->banque->enabled) && !(GETPOST("accountid", 'int') > 0)) {
70  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
71  $error++;
72  }
73 
74  if (!$error) {
75  $paymentid = 0;
76 
77  // Read possible payments
78  foreach ($_POST as $key => $value) {
79  if (substr($key, 0, 7) == 'amount_') {
80  $other_chid = substr($key, 7);
81  $amounts[$other_chid] = price2num(GETPOST($key));
82  }
83  }
84 
85  if (count($amounts) <= 0) {
86  $error++;
87  $errmsg = 'ErrorNoPaymentDefined';
88  setEventMessages($errmsg, null, 'errors');
89  }
90 
91  if (!$error) {
92  $db->begin();
93 
94  // Create a line of payments
95  $payment = new PaymentDonation($db);
96  $payment->chid = $chid;
97  $payment->datepaid = $datepaid;
98  $payment->amounts = $amounts; // Tableau de montant
99  $payment->paymenttype = GETPOST("paymenttype", 'int');
100  $payment->num_payment = GETPOST("num_payment", 'alphanohtml');
101  $payment->note_public = GETPOST("note_public", 'restricthtml');
102 
103  if (!$error) {
104  $paymentid = $payment->create($user);
105  if ($paymentid < 0) {
106  $errmsg = $payment->error;
107  setEventMessages($errmsg, null, 'errors');
108  $error++;
109  }
110  }
111 
112  if (!$error) {
113  $result = $payment->addPaymentToBank($user, 'payment_donation', '(DonationPayment)', GETPOST('accountid', 'int'), '', '');
114  if (!($result > 0)) {
115  $errmsg = $payment->error;
116  setEventMessages($errmsg, null, 'errors');
117  $error++;
118  }
119  }
120 
121  if (!$error) {
122  $db->commit();
123  $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
124  header('Location: '.$loc);
125  exit;
126  } else {
127  $db->rollback();
128  }
129  }
130  }
131 
132  $action = 'create';
133 }
134 
135 
136 /*
137  * View
138  */
139 
140 $form = new Form($db);
141 
142 llxHeader();
143 
144 
145 $sql = "SELECT sum(p.amount) as total";
146 $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as p";
147 $sql .= " WHERE p.fk_donation = ".((int) $chid);
148 $resql = $db->query($sql);
149 if ($resql) {
150  $obj = $db->fetch_object($resql);
151  $sumpaid = $obj->total;
152  $db->free($resql);
153 }
154 
155 
156 // Form to create donation payment
157 if ($action == 'create') {
158  $object->fetch($chid);
159 
160  $total = $object->amount;
161 
162  print load_fiche_titre($langs->trans("DoPayment"));
163 
164  if (!empty($conf->use_javascript_ajax)) {
165  print "\n".'<script type="text/javascript">';
166  //Add js for AutoFill
167  print ' $(document).ready(function () {';
168  print ' $(".AutoFillAmout").on(\'click touchstart\', function(){
169  $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change");
170  });';
171  print ' });'."\n";
172 
173  print ' </script>'."\n";
174  }
175 
176  print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
177  print '<input type="hidden" name="token" value="'.newToken().'">';
178  print '<input type="hidden" name="rowid" value="'.$chid.'">';
179  print '<input type="hidden" name="chid" value="'.$chid.'">';
180  print '<input type="hidden" name="action" value="add_payment">';
181 
182  print dol_get_fiche_head();
183 
184  print '<table class="border centpercent tableforfieldcreate">';
185 
186  print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
187  $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
188  $datepayment = empty($conf->global->MAIN_AUTOFILL_DATE) ? (GETPOST("remonth") ? $datepaid : -1) : 0;
189  print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $object->date, '', 1, $langs->trans("DonationDate"));
190  print "</td>";
191  print '</tr>';
192 
193  print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
194  $form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype") : $object->paymenttype, "paymenttype");
195  print "</td>\n";
196  print '</tr>';
197 
198  print '<tr>';
199  print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
200  print '<td colspan="2">';
201  $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid") : $object->accountid, "accountid", 0, '', 2); // Show open bank account list
202  print '</td></tr>';
203 
204  // Number
205  print '<tr><td>'.$langs->trans('Numero');
206  print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
207  print '</td>';
208  print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
209 
210  print '<tr>';
211  print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
212  print '<td class="tdtop" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
213  print '</tr>';
214 
215  print '</table>';
216 
217  print dol_get_fiche_end();
218 
219  /*
220  * List of payments on donation
221  */
222 
223  $num = 1;
224  $i = 0;
225 
226  print '<table class="noborder centpercent">';
227  print '<tr class="liste_titre">';
228  print '<td>'.$langs->trans("Donation").'</td>';
229  print '<td class="right">'.$langs->trans("Amount").'</td>';
230  print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
231  print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
232  print '<td class="center">'.$langs->trans("Amount").'</td>';
233  print "</tr>\n";
234 
235  $total = 0;
236  $totalrecu = 0;
237 
238  while ($i < $num) {
239  $objp = $object;
240 
241  print '<tr class="oddeven">';
242 
243  print '<td>'.$object->getNomUrl(1)."</td>";
244 
245  print '<td class="right">'.price($objp->amount)."</td>";
246 
247  print '<td class="right">'.price($sumpaid)."</td>";
248 
249  print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
250 
251  print '<td class="center">';
252  if ($sumpaid < $objp->amount) {
253  $namef = "amount_".$objp->id;
254  if (!empty($conf->use_javascript_ajax)) {
255  print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmout' data-rowname='".$namef."' data-value='".price($objp->amount - $sumpaid)."'");
256  }
257  print '<input type="text" size="8" name="'.$namef.'">';
258  } else {
259  print '-';
260  }
261  print "</td>";
262 
263  print "</tr>\n";
264  /*$total+=$objp->total;
265  $total_ttc+=$objp->total_ttc;
266  $totalrecu+=$objp->am;*/ //Useless code ?
267  $i++;
268  }
269  /*if ($i > 1)
270  {
271  // Print total
272  print '<tr class="oddeven">';
273  print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
274  print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
275  print "<td class=\"right\"><b>".price($totalrecu)."</b></td>";
276  print "<td class=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
277  print '<td class="center">&nbsp;</td>';
278  print "</tr>\n";
279  }*/ //Useless code ?
280 
281  print "</table>";
282 
283  print $form->buttonsSaveCancel();
284 
285  print "</form>\n";
286 }
287 
288 llxFooter();
289 $db->close();
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
PaymentDonation
Class to manage payments of donations.
Definition: paymentdonation.class.php:30
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
Don
Class to manage donations.
Definition: don.class.php:38
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
price
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.
Definition: functions.lib.php:5541
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
dol_mktime
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...
Definition: functions.lib.php:2757
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59