dolibarr 20.0.0
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26// Load Dolibarr environment
27require '../../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
29require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
31
32$langs->load("bills");
33
34$chid = GETPOSTINT("rowid");
35$action = GETPOST('action', 'aZ09');
36$amounts = array();
37$cancel = GETPOST('cancel');
38
39// Security check
40$socid = 0;
41if ($user->socid > 0) {
42 $socid = $user->socid;
43}
44
45$object = new Don($db);
46
47
48/*
49 * Actions
50 */
51
52if ($action == 'add_payment') {
53 $error = 0;
54
55 if ($cancel) {
56 $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
57 header("Location: ".$loc);
58 exit;
59 }
60
61 $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
62
63 if (!(GETPOST("paymenttype") > 0)) {
64 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
65 $error++;
66 }
67 if ($datepaid == '') {
68 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
69 $error++;
70 }
71 if (isModEnabled("bank") && !(GETPOSTINT("accountid") > 0)) {
72 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
73 $error++;
74 }
75
76 if (!$error) {
77 $paymentid = 0;
78
79 // Read possible payments
80 foreach ($_POST as $key => $value) {
81 if (substr($key, 0, 7) == 'amount_') {
82 $other_chid = substr($key, 7);
83 $amounts[$other_chid] = price2num(GETPOST($key));
84 }
85 }
86
87 if (count($amounts) <= 0) {
88 $error++;
89 $errmsg = 'ErrorNoPaymentDefined';
90 setEventMessages($errmsg, null, 'errors');
91 }
92
93 if (!$error) {
94 $db->begin();
95
96 // Create a line of payments
97 $payment = new PaymentDonation($db);
98 $payment->chid = $chid;
99 $payment->datep = $datepaid;
100 $payment->amounts = $amounts; // Tableau de montant
101 $payment->paymenttype = GETPOSTINT("paymenttype");
102 $payment->num_payment = GETPOST("num_payment", 'alphanohtml');
103 $payment->note_public = GETPOST("note_public", 'restricthtml');
104
105 if (!$error) {
106 $paymentid = $payment->create($user);
107 if ($paymentid < 0) {
108 $errmsg = $payment->error;
109 setEventMessages($errmsg, null, 'errors');
110 $error++;
111 }
112 }
113
114 if (!$error) {
115 $result = $payment->addPaymentToBank($user, 'payment_donation', '(DonationPayment)', GETPOSTINT('accountid'), '', '');
116 if (!($result > 0)) {
117 $errmsg = $payment->error;
118 setEventMessages($errmsg, null, 'errors');
119 $error++;
120 }
121 }
122
123 if (!$error) {
124 $db->commit();
125 $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
126 header('Location: '.$loc);
127 exit;
128 } else {
129 $db->rollback();
130 }
131 }
132 }
133
134 $action = 'create';
135}
136
137
138/*
139 * View
140 */
141
142$form = new Form($db);
143$title = $langs->trans("Payment");
144llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-donation page-payment');
145
146
147$sql = "SELECT sum(p.amount) as total";
148$sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as p";
149$sql .= " WHERE p.fk_donation = ".((int) $chid);
150$resql = $db->query($sql);
151if ($resql) {
152 $obj = $db->fetch_object($resql);
153 $sumpaid = $obj->total;
154 $db->free($resql);
155}
156
157
158// Form to create donation payment
159if ($action == 'create') {
160 $object->fetch($chid);
161
162 $total = $object->amount;
163
164 print load_fiche_titre($langs->trans("DoPayment"));
165
166 if (!empty($conf->use_javascript_ajax)) {
167 print "\n".'<script type="text/javascript">';
168 //Add js for AutoFill
169 print ' $(document).ready(function () {';
170 print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
171 $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change");
172 });';
173 print ' });'."\n";
174
175 print ' </script>'."\n";
176 }
177
178 print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
179 print '<input type="hidden" name="token" value="'.newToken().'">';
180 print '<input type="hidden" name="rowid" value="'.$chid.'">';
181 print '<input type="hidden" name="chid" value="'.$chid.'">';
182 print '<input type="hidden" name="action" value="add_payment">';
183
184 print dol_get_fiche_head();
185
186 print '<table class="border centpercent tableforfieldcreate">';
187
188 print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
189 $datepaid = dol_mktime(12, 0, 0, GETPOST("remonth"), GETPOST("reday"), GETPOST("reyear"));
190 $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOST("remonth") ? $datepaid : -1) : 0;
191 print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $object->date, '', 1, $langs->trans("DonationDate"));
192 print "</td>";
193 print '</tr>';
194
195 print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
196 $form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype") : $object->fk_typepayment, "paymenttype");
197 print "</td>\n";
198 print '</tr>';
199
200 print '<tr>';
201 print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
202 print '<td colspan="2">';
203 $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid") : "0", "accountid", 0, '', 2); // Show open bank account list
204 print '</td></tr>';
205
206 // Number
207 print '<tr><td>'.$langs->trans('Numero');
208 print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
209 print '</td>';
210 print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
211
212 print '<tr>';
213 print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
214 print '<td class="tdtop" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
215 print '</tr>';
216
217 print '</table>';
218
219 print dol_get_fiche_end();
220
221 /*
222 * List of payments on donation
223 */
224
225 $num = 1;
226 $i = 0;
227
228 print '<table class="noborder centpercent">';
229 print '<tr class="liste_titre">';
230 print '<td>'.$langs->trans("Donation").'</td>';
231 print '<td class="right">'.$langs->trans("Amount").'</td>';
232 print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
233 print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
234 print '<td class="center">'.$langs->trans("Amount").'</td>';
235 print "</tr>\n";
236
237 $total = 0;
238 $totalrecu = 0;
239
240 while ($i < $num) {
241 $objp = $object;
242
243 print '<tr class="oddeven">';
244
245 print '<td>'.$object->getNomUrl(1)."</td>";
246
247 print '<td class="right">'.price($objp->amount)."</td>";
248
249 print '<td class="right">'.price($sumpaid)."</td>";
250
251 print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
252
253 print '<td class="center">';
254 if ($sumpaid < $objp->amount) {
255 $namef = "amount_".$objp->id;
256 if (!empty($conf->use_javascript_ajax)) {
257 print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowname='".$namef."' data-value='".price($objp->amount - $sumpaid)."'");
258 }
259 print '<input type="text" size="8" name="'.$namef.'">';
260 } else {
261 print '-';
262 }
263 print "</td>";
264
265 print "</tr>\n";
266 /*$total+=$objp->total;
267 $total_ttc+=$objp->total_ttc;
268 $totalrecu+=$objp->am;*/ //Useless code ?
269 $i++;
270 }
271 /*if ($i > 1)
272 {
273 // Print total
274 print '<tr class="oddeven">';
275 print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
276 print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
277 print "<td class=\"right\"><b>".price($totalrecu)."</b></td>";
278 print "<td class=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
279 print '<td class="center">&nbsp;</td>';
280 print "</tr>\n";
281 }*/ //Useless code ?
282
283 print "</table>";
284
285 print $form->buttonsSaveCancel();
286
287 print "</form>\n";
288}
289
290llxFooter();
291$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage donations.
Definition don.class.php:41
Class to manage generation of HTML components Only common components must be here.
Class to manage payments of donations.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.