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