dolibarr 21.0.0-alpha
paymentdonation.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
3 * Copyright (C) 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
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27
28
33{
37 public $element = 'payment_donation';
38
42 public $table_element = 'payment_donation';
43
47 public $picto = 'payment';
48
52 public $rowid;
53
57 public $fk_donation;
58
59 public $datec = '';
60
61 public $datep = '';
62
63 public $amount; // Total amount of payment
64
65 public $amounts = array(); // Array of amounts
66
67 public $fk_typepayment; // Payment mode ID
68 public $paymenttype; // Payment mode ID or Code. TODO Use only the code in this field.
69
74 public $num_payment;
75
79 public $fk_bank;
80
84 public $fk_user_creat;
85
89 public $fk_user_modif;
90
95 public $total;
96
97 public $type_code;
98 public $type_label;
99 public $chid;
100 public $datepaid;
101 public $bank_account;
102 public $bank_line;
103
107 public $ext_payment_id;
108
112 public $ext_payment_site;
113
119 public function __construct($db)
120 {
121 $this->db = $db;
122 }
123
132 public function create($user, $notrigger = 0)
133 {
134 $error = 0;
135
136 $now = dol_now();
137
138 // Validate parameters
139 if (!$this->datep) {
140 $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
141 return -1;
142 }
143
144 // Clean parameters
145 if (isset($this->chid)) {
146 $this->chid = (int) $this->chid;
147 } elseif (isset($this->fk_donation)) {
148 // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
149 // (keep priority to chid property)
150 $this->chid = (int) $this->fk_donation;
151 }
152 if (isset($this->fk_donation)) {
153 $this->fk_donation = (int) $this->fk_donation;
154 }
155 if (isset($this->amount)) {
156 $this->amount = trim($this->amount);
157 }
158 if (isset($this->fk_typepayment)) {
159 $this->fk_typepayment = trim($this->fk_typepayment);
160 }
161 if (isset($this->num_payment)) {
162 $this->num_payment = trim($this->num_payment);
163 }
164 if (isset($this->note_public)) {
165 $this->note_public = trim($this->note_public);
166 }
167 if (isset($this->fk_bank)) {
168 $this->fk_bank = (int) $this->fk_bank;
169 }
170 if (isset($this->fk_user_creat)) {
171 $this->fk_user_creat = (int) $this->fk_user_creat;
172 }
173 if (isset($this->fk_user_modif)) {
174 $this->fk_user_modif = (int) $this->fk_user_modif;
175 }
176
177 $totalamount = 0;
178 foreach ($this->amounts as $key => $value) { // How payment is dispatch
179 $newvalue = price2num($value, 'MT');
180 $this->amounts[$key] = $newvalue;
181 $totalamount += $newvalue;
182 }
183 $totalamount = price2num($totalamount);
184
185 // Check parameters
186 if ($totalamount == 0) {
187 return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
188 }
189
190
191 $this->db->begin();
192
193 if ($totalamount != 0) {
194 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
195 $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
196 $sql .= " fk_user_creat, fk_bank)";
197 $sql .= " VALUES (".((int) $this->chid).", '".$this->db->idate($now)."',";
198 $sql .= " '".$this->db->idate($this->datep)."',";
199 $sql .= " ".((float) price2num($totalamount)).",";
200 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ";
201 $sql .= " ".($this->ext_payment_id ? "'".$this->db->escape($this->ext_payment_id)."'" : "null").", ".($this->ext_payment_site ? "'".$this->db->escape($this->ext_payment_site)."'" : "null").",";
202 $sql .= " ".((int) $user->id).", 0)";
203
204 dol_syslog(get_class($this)."::create", LOG_DEBUG);
205 $resql = $this->db->query($sql);
206 if ($resql) {
207 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
208 $this->ref = (string) $this->id;
209 } else {
210 $error++;
211 }
212 }
213
214 if (!$error && !$notrigger) {
215 // Call triggers
216 $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
217 if ($result < 0) {
218 $error++;
219 }
220 // End call triggers
221 }
222
223 if ($totalamount != 0 && !$error) {
224 $this->amount = $totalamount;
225 $this->total = $totalamount; // deprecated
226 $this->db->commit();
227 return $this->id;
228 } else {
229 $this->error = $this->db->error();
230 $this->db->rollback();
231 return -1;
232 }
233 }
234
241 public function fetch($id)
242 {
243 global $langs;
244 $sql = "SELECT";
245 $sql .= " t.rowid,";
246 $sql .= " t.fk_donation,";
247 $sql .= " t.datec,";
248 $sql .= " t.tms,";
249 $sql .= " t.datep,";
250 $sql .= " t.amount,";
251 $sql .= " t.fk_typepayment,";
252 $sql .= " t.num_payment,";
253 $sql .= " t.note as note_public,";
254 $sql .= " t.fk_bank,";
255 $sql .= " t.fk_user_creat,";
256 $sql .= " t.fk_user_modif,";
257 $sql .= " pt.code as type_code, pt.libelle as type_label,";
258 $sql .= ' b.fk_account';
259 $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
260 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
261 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
262 $sql .= " WHERE t.rowid = ".((int) $id);
263
264 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
265 $resql = $this->db->query($sql);
266 if ($resql) {
267 if ($this->db->num_rows($resql)) {
268 $obj = $this->db->fetch_object($resql);
269
270 $this->id = $obj->rowid;
271 $this->ref = $obj->rowid;
272
273 $this->fk_donation = $obj->fk_donation;
274 $this->datec = $this->db->jdate($obj->datec);
275 $this->tms = $this->db->jdate($obj->tms);
276 $this->datep = $this->db->jdate($obj->datep);
277 $this->amount = $obj->amount;
278 $this->fk_typepayment = $obj->fk_typepayment; // Id on type of payent
279 $this->paymenttype = $obj->fk_typepayment; // Id on type of payment. We should store the code into paymenttype.
280 $this->num_payment = $obj->num_payment;
281 $this->note_public = $obj->note_public;
282 $this->fk_bank = $obj->fk_bank;
283 $this->fk_user_creat = $obj->fk_user_creat;
284 $this->fk_user_modif = $obj->fk_user_modif;
285
286 $this->type_code = $obj->type_code;
287 $this->type_label = $obj->type_label;
288
289 $this->bank_account = $obj->fk_account;
290 $this->bank_line = $obj->fk_bank;
291 }
292 $this->db->free($resql);
293
294 return 1;
295 } else {
296 $this->error = "Error ".$this->db->lasterror();
297 return -1;
298 }
299 }
300
301
309 public function update($user, $notrigger = 0)
310 {
311 global $conf, $langs;
312 $error = 0;
313
314 // Clean parameters
315
316 if (isset($this->fk_donation)) {
317 $this->fk_donation = (int) $this->fk_donation;
318 }
319 if (isset($this->amount)) {
320 $this->amount = trim($this->amount);
321 }
322 if (isset($this->fk_typepayment)) {
323 $this->fk_typepayment = trim($this->fk_typepayment);
324 }
325 if (isset($this->num_payment)) {
326 $this->num_payment = trim($this->num_payment);
327 }
328 if (isset($this->note_public)) {
329 $this->note_public = trim($this->note_public);
330 }
331 if (isset($this->fk_bank)) {
332 $this->fk_bank = (int) $this->fk_bank;
333 }
334 if (isset($this->fk_user_creat)) {
335 $this->fk_user_creat = (int) $this->fk_user_creat;
336 }
337 if (isset($this->fk_user_modif)) {
338 $this->fk_user_modif = (int) $this->fk_user_modif;
339 }
340
341 // Check parameters
342 // Put here code to add control on parameters values
343
344 // Update request
345 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
346 $sql .= " fk_donation=".(isset($this->fk_donation) ? $this->fk_donation : "null").",";
347 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
348 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
349 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
350 $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
351 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
352 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
353 $sql .= " note=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
354 $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
355 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
356 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
357 $sql .= " WHERE rowid=".(int) $this->id;
358
359 $this->db->begin();
360
361 dol_syslog(get_class($this)."::update", LOG_DEBUG);
362 $resql = $this->db->query($sql);
363 if (!$resql) {
364 $error++;
365 $this->errors[] = "Error ".$this->db->lasterror();
366 }
367
368 if (!$error) {
369 if (!$notrigger) {
370 if (!$error && !$notrigger) {
371 // Call triggers
372 $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
373 if ($result < 0) {
374 $error++;
375 }
376 // End call triggers
377 }
378 }
379 }
380
381 // Commit or rollback
382 if ($error) {
383 foreach ($this->errors as $errmsg) {
384 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
385 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
386 }
387 $this->db->rollback();
388 return -1 * $error;
389 } else {
390 $this->db->commit();
391 return 1;
392 }
393 }
394
395
403 public function delete($user, $notrigger = 0)
404 {
405 global $conf, $langs;
406 $error = 0;
407
408 $this->db->begin();
409
410 if (!$error) {
411 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
412 $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
413
414 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
415 $resql = $this->db->query($sql);
416 if (!$resql) {
417 $error++;
418 $this->errors[] = "Error ".$this->db->lasterror();
419 }
420 }
421
422 if (!$error) {
423 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
424 $sql .= " WHERE rowid=".((int) $this->id);
425
426 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
427 $resql = $this->db->query($sql);
428 if (!$resql) {
429 $error++;
430 $this->errors[] = "Error ".$this->db->lasterror();
431 }
432 }
433
434 if (!$error) {
435 if (!$notrigger) {
436 if (!$error && !$notrigger) {
437 // Call triggers
438 $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
439 if ($result < 0) {
440 $error++;
441 }
442 // End call triggers
443 }
444 }
445 }
446
447 // Commit or rollback
448 if ($error) {
449 foreach ($this->errors as $errmsg) {
450 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
451 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
452 }
453 $this->db->rollback();
454 return -1 * $error;
455 } else {
456 $this->db->commit();
457 return 1;
458 }
459 }
460
461
462
470 public function createFromClone(User $user, $fromid)
471 {
472 $error = 0;
473
474 $object = new PaymentDonation($this->db);
475
476 $this->db->begin();
477
478 // Load source object
479 $object->fetch($fromid);
480 $object->id = 0;
481 $object->statut = 0;
482
483 // Clear fields
484 // ...
485
486 // Create clone
487 $object->context['createfromclone'] = 'createfromclone';
488 $result = $object->create($user);
489
490 // Other options
491 if ($result < 0) {
492 $this->error = $object->error;
493 $error++;
494 }
495
496 if (!$error) {
497 }
498
499 unset($object->context['createfromclone']);
500
501 // End
502 if (!$error) {
503 $this->db->commit();
504 return $object->id;
505 } else {
506 $this->db->rollback();
507 return -1;
508 }
509 }
510
511
518 public function getLibStatut($mode = 0)
519 {
520 return '';
521 }
522
523 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
531 public function LibStatut($status, $mode = 0)
532 {
533 // phpcs:enable
534 global $langs;
535
536 return '';
537 }
538
539
547 public function initAsSpecimen()
548 {
549 $this->id = 0;
550
551 $this->fk_donation = 0;
552 $this->datec = '';
553 $this->tms = dol_now();
554 $this->datep = '';
555 $this->amount = '';
556 $this->fk_typepayment = '';
557 $this->paymenttype = '';
558 $this->num_payment = '';
559 $this->note_public = '';
560 $this->fk_bank = 0;
561 $this->fk_user_creat = dol_now();
562 $this->fk_user_modif = dol_now();
563
564 return 1;
565 }
566
567
580 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
581 {
582 global $conf;
583
584 $error = 0;
585
586 if (isModEnabled("bank")) {
587 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
588
589 $acc = new Account($this->db);
590 $acc->fetch($accountid);
591
593 if ($mode == 'payment_donation') {
594 $amount = $total;
595 }
596 // Insert payment into llx_bank
597 $bank_line_id = $acc->addline(
598 $this->datep,
599 $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
600 $label,
601 $amount,
602 $this->num_payment,
603 '',
604 $user,
605 $emetteur_nom,
606 $emetteur_banque
607 );
608
609 // Update fk_bank in llx_paiement.
610 // On connait ainsi le paiement qui a genere l'ecriture bancaire
611 if ($bank_line_id > 0) {
612 $result = $this->update_fk_bank($bank_line_id);
613 if ($result <= 0) {
614 $error++;
615 dol_print_error($this->db);
616 }
617
618 // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
619 $url = '';
620 if ($mode == 'payment_donation') {
621 $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
622 }
623 if ($url) {
624 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
625 if ($result <= 0) {
626 $error++;
627 dol_print_error($this->db);
628 }
629 }
630 } else {
631 $this->error = $acc->error;
632 $error++;
633 }
634 }
635
636 if (!$error) {
637 return 1;
638 } else {
639 return -1;
640 }
641 }
642
643
644 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
651 public function update_fk_bank($id_bank)
652 {
653 // phpcs:enable
654 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
655
656 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
657 $result = $this->db->query($sql);
658 if ($result) {
659 return 1;
660 } else {
661 $this->error = $this->db->error();
662 return 0;
663 }
664 }
665
673 public function getNomUrl($withpicto = 0, $maxlen = 0)
674 {
675 global $langs, $hookmanager;
676
677 $result = '';
678
679 $label = '<u>'.$langs->trans("DonationPayment").'</u>';
680 $label .= '<br>';
681 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
682
683 if (!empty($this->id)) {
684 $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
685 $linkend = '</a>';
686
687 if ($withpicto) {
688 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
689 }
690 if ($withpicto && $withpicto != 2) {
691 $result .= ' ';
692 }
693 if ($withpicto != 2) {
694 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
695 }
696 }
697
698 global $action;
699 $hookmanager->initHooks(array($this->element . 'dao'));
700 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
701 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
702 if ($reshook > 0) {
703 $result = $hookmanager->resPrint;
704 } else {
705 $result .= $hookmanager->resPrint;
706 }
707 return $result;
708 }
709}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
$object ref
Definition info.php:79
Class to manage bank accounts.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage payments of donations.
addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
Add record into bank for payment with links between this bank record and invoices of payment.
initAsSpecimen()
Initialise an instance with random values.
create($user, $notrigger=0)
Create payment of donation into database.
LibStatut($status, $mode=0)
Return the label of a given status.
update_fk_bank($id_bank)
Update link between the donation payment and the generated line in llx_bank.
__construct($db)
Constructor.
getLibStatut($mode=0)
Return the label of the status.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
fetch($id)
Load object in memory from database.
update($user, $notrigger=0)
Update database.
getNomUrl($withpicto=0, $maxlen=0)
Return clickable name (with picto eventually)
Class to manage Dolibarr users.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...