dolibarr 21.0.0-beta
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-2024 Frédéric France <frederic.france@free.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
40$langs->load("bills");
41
42$chid = GETPOSTINT("rowid");
43$action = GETPOST('action', 'aZ09');
44$amounts = array();
45$cancel = GETPOST('cancel');
46
47// Security check
48$socid = 0;
49if ($user->socid > 0) {
50 $socid = $user->socid;
51}
52
53$object = new Don($db);
54
55$permissiontoread = $user->hasRight('don', 'lire');
56$permissiontoadd = $user->hasRight('don', 'creer');
57$permissiontodelete = $user->hasRight('don', 'supprimer');
58
59
60/*
61 * Actions
62 */
63
64if ($action == 'add_payment' && $permissiontoadd) {
65 $error = 0;
66
67 if ($cancel) {
68 $loc = DOL_URL_ROOT.'/don/card.php?rowid='.$chid;
69 header("Location: ".$loc);
70 exit;
71 }
72
73 $datepaid = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
74
75 if (!(GETPOST("paymenttype") > 0)) {
76 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode")), null, 'errors');
77 $error++;
78 }
79 if ($datepaid == '') {
80 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Date")), null, 'errors');
81 $error++;
82 }
83 if (isModEnabled("bank") && !(GETPOSTINT("accountid") > 0)) {
84 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountToCredit")), null, 'errors');
85 $error++;
86 }
87
88 if (!$error) {
89 $paymentid = 0;
90
91 // Read possible payments
92 foreach ($_POST as $key => $value) {
93 if (substr($key, 0, 7) == 'amount_') {
94 $other_chid = (int) substr($key, 7);
95 $amounts[$other_chid] = (float) price2num(GETPOST($key));
96 }
97 }
98
99 if (count($amounts) <= 0) {
100 $error++;
101 $errmsg = 'ErrorNoPaymentDefined';
102 setEventMessages($errmsg, null, 'errors');
103 }
104
105 if (!$error) {
106 $db->begin();
107
108 // Create a line of payments
109 $payment = new PaymentDonation($db);
110 $payment->chid = $chid;
111 $payment->datep = $datepaid;
112 $payment->amounts = $amounts; // Tableau de montant
113 $payment->paymenttype = GETPOSTINT("paymenttype");
114 $payment->num_payment = GETPOST("num_payment", 'alphanohtml');
115 $payment->note_public = GETPOST("note_public", 'restricthtml');
116
117 if (!$error) {
118 $paymentid = $payment->create($user);
119 if ($paymentid < 0) {
120 $errmsg = $payment->error;
121 setEventMessages($errmsg, null, 'errors');
122 $error++;
123 }
124 }
125
126 if (!$error) {
127 $result = $payment->addPaymentToBank($user, 'payment_donation', '(DonationPayment)', GETPOSTINT('accountid'), '', '');
128 if (!($result > 0)) {
129 $errmsg = $payment->error;
130 setEventMessages($errmsg, null, 'errors');
131 $error++;
132 }
133 }
134
135 if (!$error) {
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$title = $langs->trans("Payment");
156llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-donation page-payment');
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 = ".((int) $chid);
162$resql = $db->query($sql);
163if ($resql) {
164 $obj = $db->fetch_object($resql);
165 $sumpaid = $obj->total;
166 $db->free($resql);
167}
168
169
170// Form to create donation payment
171if ($action == 'create') {
172 $object->fetch($chid);
173
174 $total = $object->amount;
175
176 print load_fiche_titre($langs->trans("DoPayment"));
177
178 if (!empty($conf->use_javascript_ajax)) {
179 print "\n".'<script type="text/javascript">';
180 //Add js for AutoFill
181 print ' $(document).ready(function () {';
182 print ' $(".AutoFillAmount").on(\'click touchstart\', function(){
183 $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change");
184 });';
185 print ' });'."\n";
186
187 print ' </script>'."\n";
188 }
189
190 print '<form name="add_payment" action="'.$_SERVER['PHP_SELF'].'" method="post">';
191 print '<input type="hidden" name="token" value="'.newToken().'">';
192 print '<input type="hidden" name="rowid" value="'.$chid.'">';
193 print '<input type="hidden" name="chid" value="'.$chid.'">';
194 print '<input type="hidden" name="action" value="add_payment">';
195
196 print dol_get_fiche_head();
197
198 print '<table class="border centpercent tableforfieldcreate">';
199
200 print '<tr><td class="fieldrequired">'.$langs->trans("Date").'</td><td colspan="2">';
201 $datepaid = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear"));
202 $datepayment = !getDolGlobalString('MAIN_AUTOFILL_DATE') ? (GETPOST("remonth") ? $datepaid : -1) : 0;
203 print $form->selectDate($datepayment, '', 0, 0, 0, "add_payment", 1, 1, 0, '', '', $object->date, '', 1, $langs->trans("DonationDate"));
204 print "</td>";
205 print '</tr>';
206
207 print '<tr><td class="fieldrequired">'.$langs->trans("PaymentMode").'</td><td colspan="2">';
208 $form->select_types_paiements(GETPOSTISSET("paymenttype") ? GETPOST("paymenttype") : $object->fk_typepayment, "paymenttype");
209 print "</td>\n";
210 print '</tr>';
211
212 print '<tr>';
213 print '<td class="fieldrequired">'.$langs->trans('AccountToCredit').'</td>';
214 print '<td colspan="2">';
215 $form->select_comptes(GETPOSTISSET("accountid") ? GETPOST("accountid") : "0", "accountid", 0, '', 2); // Show open bank account list
216 print '</td></tr>';
217
218 // Number
219 print '<tr><td>'.$langs->trans('Numero');
220 print ' <em>('.$langs->trans("ChequeOrTransferNumber").')</em>';
221 print '</td>';
222 print '<td colspan="2"><input name="num_payment" type="text" value="'.GETPOST('num_payment').'"></td></tr>'."\n";
223
224 print '<tr>';
225 print '<td class="tdtop">'.$langs->trans("Comments").'</td>';
226 print '<td class="tdtop" colspan="2"><textarea name="note_public" wrap="soft" cols="60" rows="'.ROWS_3.'"></textarea></td>';
227 print '</tr>';
228
229 print '</table>';
230
231 print dol_get_fiche_end();
232
233 /*
234 * List of payments on donation
235 */
236
237 $num = 1;
238 $i = 0;
239
240 print '<table class="noborder centpercent">';
241 print '<tr class="liste_titre">';
242 print '<td>'.$langs->trans("Donation").'</td>';
243 print '<td class="right">'.$langs->trans("Amount").'</td>';
244 print '<td class="right">'.$langs->trans("AlreadyPaid").'</td>';
245 print '<td class="right">'.$langs->trans("RemainderToPay").'</td>';
246 print '<td class="center">'.$langs->trans("Amount").'</td>';
247 print "</tr>\n";
248
249 $total = 0;
250 $totalrecu = 0;
251
252 while ($i < $num) {
253 $objp = $object;
254
255 print '<tr class="oddeven">';
256
257 print '<td>'.$object->getNomUrl(1)."</td>";
258
259 print '<td class="right">'.price($objp->amount)."</td>";
260
261 print '<td class="right">'.price($sumpaid)."</td>";
262
263 print '<td class="right">'.price($objp->amount - $sumpaid)."</td>";
264
265 print '<td class="center">';
266 if ($sumpaid < $objp->amount) {
267 $namef = "amount_".$objp->id;
268 if (!empty($conf->use_javascript_ajax)) {
269 print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowname='".$namef."' data-value='".price($objp->amount - $sumpaid)."'");
270 }
271 print '<input type="text" size="8" name="'.$namef.'">';
272 } else {
273 print '-';
274 }
275 print "</td>";
276
277 print "</tr>\n";
278 /*$total+=$objp->total;
279 $total_ttc+=$objp->total_ttc;
280 $totalrecu+=$objp->am;*/ //Useless code ?
281 $i++;
282 }
283 /*if ($i > 1)
284 {
285 // Print total
286 print '<tr class="oddeven">';
287 print '<td colspan="2" class="left">'.$langs->trans("Total").':</td>';
288 print "<td class=\"right\"><b>".price($total_ttc)."</b></td>";
289 print "<td class=\"right\"><b>".price($totalrecu)."</b></td>";
290 print "<td class=\"right\"><b>".price($total_ttc - $totalrecu)."</b></td>";
291 print '<td class="center">&nbsp;</td>';
292 print "</tr>\n";
293 }*/ //Useless code ?
294
295 print "</table>";
296
297 print $form->buttonsSaveCancel();
298
299 print "</form>\n";
300}
301
302llxFooter();
303$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
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.
llxFooter()
Footer empty.
Definition document.php:107
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79