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
62 public $datec = '';
63
67 public $datep = '';
68
72 public $amount; // Total amount of payment
73
77 public $amounts = array(); // Array of amounts
78
84 public $fk_typepayment;
85
89 public $paymenttype;
90
95 public $num_payment;
96
100 public $fk_bank;
101
105 public $fk_user_creat;
106
110 public $fk_user_modif;
111
116 public $total;
117
118 public $type_code;
119 public $type_label;
120 public $chid;
121 public $datepaid;
122 public $bank_account;
123 public $bank_line;
124
128 public $ext_payment_id;
129
133 public $ext_payment_site;
134
140 public function __construct($db)
141 {
142 $this->db = $db;
143 }
144
153 public function create($user, $notrigger = 0)
154 {
155 $error = 0;
156
157 $now = dol_now();
158
159 // Validate parameters
160 if (!$this->datep) {
161 $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
162 return -1;
163 }
164
165 // Clean parameters
166 if (isset($this->chid)) {
167 $this->chid = (int) $this->chid;
168 } elseif (isset($this->fk_donation)) {
169 // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
170 // (keep priority to chid property)
171 $this->chid = (int) $this->fk_donation;
172 }
173 if (isset($this->fk_donation)) {
174 $this->fk_donation = (int) $this->fk_donation;
175 }
176 if (isset($this->amount)) {
177 $this->amount = (float) price2num($this->amount);
178 }
179 if (isset($this->fk_typepayment)) {
180 $this->fk_typepayment = (int) price2num($this->fk_typepayment);
181 }
182 if (isset($this->num_payment)) {
183 $this->num_payment = trim($this->num_payment);
184 }
185 if (isset($this->note_public)) {
186 $this->note_public = trim($this->note_public);
187 }
188 if (isset($this->fk_bank)) {
189 $this->fk_bank = (int) $this->fk_bank;
190 }
191 if (isset($this->fk_user_creat)) {
192 $this->fk_user_creat = (int) $this->fk_user_creat;
193 }
194 if (isset($this->fk_user_modif)) {
195 $this->fk_user_modif = (int) $this->fk_user_modif;
196 }
197
198 $totalamount = 0;
199 foreach ($this->amounts as $key => $value) { // How payment is dispatch
200 $newvalue = (float) price2num($value, 'MT');
201 $this->amounts[$key] = $newvalue;
202 $totalamount += $newvalue;
203 }
204 $totalamount = (float) price2num($totalamount);
205
206 // Check parameters
207 if ($totalamount == 0) {
208 return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
209 }
210
211
212 $this->db->begin();
213
214 if ($totalamount != 0) {
215 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
216 $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
217 $sql .= " fk_user_creat, fk_bank)";
218 $sql .= " VALUES (".((int) $this->chid).", '".$this->db->idate($now)."',";
219 $sql .= " '".$this->db->idate($this->datep)."',";
220 $sql .= " ".((float) price2num($totalamount)).",";
221 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ";
222 $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").",";
223 $sql .= " ".((int) $user->id).", 0)";
224
225 dol_syslog(get_class($this)."::create", LOG_DEBUG);
226 $resql = $this->db->query($sql);
227 if ($resql) {
228 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
229 $this->ref = (string) $this->id;
230 } else {
231 $error++;
232 }
233 }
234
235 if (!$error && !$notrigger) {
236 // Call triggers
237 $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
238 if ($result < 0) {
239 $error++;
240 }
241 // End call triggers
242 }
243
244 if ($totalamount != 0 && !$error) {
245 $this->amount = $totalamount;
246 $this->total = $totalamount; // deprecated
247 $this->db->commit();
248 return $this->id;
249 } else {
250 $this->error = $this->db->error();
251 $this->db->rollback();
252 return -1;
253 }
254 }
255
262 public function fetch($id)
263 {
264 global $langs;
265 $sql = "SELECT";
266 $sql .= " t.rowid,";
267 $sql .= " t.fk_donation,";
268 $sql .= " t.datec,";
269 $sql .= " t.tms,";
270 $sql .= " t.datep,";
271 $sql .= " t.amount,";
272 $sql .= " t.fk_typepayment,";
273 $sql .= " t.num_payment,";
274 $sql .= " t.note as note_public,";
275 $sql .= " t.fk_bank,";
276 $sql .= " t.fk_user_creat,";
277 $sql .= " t.fk_user_modif,";
278 $sql .= " pt.code as type_code, pt.libelle as type_label,";
279 $sql .= ' b.fk_account';
280 $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
281 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
282 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
283 $sql .= " WHERE t.rowid = ".((int) $id);
284
285 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
286 $resql = $this->db->query($sql);
287 if ($resql) {
288 if ($this->db->num_rows($resql)) {
289 $obj = $this->db->fetch_object($resql);
290
291 $this->id = $obj->rowid;
292 $this->ref = $obj->rowid;
293
294 $this->fk_donation = $obj->fk_donation;
295 $this->datec = $this->db->jdate($obj->datec);
296 $this->tms = $this->db->jdate($obj->tms);
297 $this->datep = $this->db->jdate($obj->datep);
298 $this->amount = $obj->amount;
299 $this->fk_typepayment = $obj->fk_typepayment; // Id on type of payent
300 $this->paymenttype = $obj->fk_typepayment; // Id on type of payment. We should store the code into paymenttype.
301 $this->num_payment = $obj->num_payment;
302 $this->note_public = $obj->note_public;
303 $this->fk_bank = $obj->fk_bank;
304 $this->fk_user_creat = $obj->fk_user_creat;
305 $this->fk_user_modif = $obj->fk_user_modif;
306
307 $this->type_code = $obj->type_code;
308 $this->type_label = $obj->type_label;
309
310 $this->bank_account = $obj->fk_account;
311 $this->bank_line = $obj->fk_bank;
312 }
313 $this->db->free($resql);
314
315 return 1;
316 } else {
317 $this->error = "Error ".$this->db->lasterror();
318 return -1;
319 }
320 }
321
322
330 public function update($user, $notrigger = 0)
331 {
332 global $conf, $langs;
333 $error = 0;
334
335 // Clean parameters
336
337 if (isset($this->fk_donation)) {
338 $this->fk_donation = (int) $this->fk_donation;
339 }
340 if (isset($this->amount)) {
341 $this->amount = (float) price2num($this->amount);
342 }
343 if (isset($this->fk_typepayment)) {
344 $this->fk_typepayment = (int) price2num($this->fk_typepayment);
345 }
346 if (isset($this->num_payment)) {
347 $this->num_payment = trim($this->num_payment);
348 }
349 if (isset($this->note_public)) {
350 $this->note_public = trim($this->note_public);
351 }
352 if (isset($this->fk_bank)) {
353 $this->fk_bank = (int) $this->fk_bank;
354 }
355 if (isset($this->fk_user_creat)) {
356 $this->fk_user_creat = (int) $this->fk_user_creat;
357 }
358 if (isset($this->fk_user_modif)) {
359 $this->fk_user_modif = (int) $this->fk_user_modif;
360 }
361
362 // Check parameters
363 // Put here code to add control on parameters values
364
365 // Update request
366 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
367 $sql .= " fk_donation=".(isset($this->fk_donation) ? $this->fk_donation : "null").",";
368 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
369 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
370 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
371 $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
372 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
373 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
374 $sql .= " note=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
375 $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
376 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
377 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
378 $sql .= " WHERE rowid=".(int) $this->id;
379
380 $this->db->begin();
381
382 dol_syslog(get_class($this)."::update", LOG_DEBUG);
383 $resql = $this->db->query($sql);
384 if (!$resql) {
385 $error++;
386 $this->errors[] = "Error ".$this->db->lasterror();
387 }
388
389 if (!$error) {
390 if (!$notrigger) {
391 if (!$error && !$notrigger) {
392 // Call triggers
393 $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
394 if ($result < 0) {
395 $error++;
396 }
397 // End call triggers
398 }
399 }
400 }
401
402 // Commit or rollback
403 if ($error) {
404 foreach ($this->errors as $errmsg) {
405 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
406 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
407 }
408 $this->db->rollback();
409 return -1 * $error;
410 } else {
411 $this->db->commit();
412 return 1;
413 }
414 }
415
416
424 public function delete($user, $notrigger = 0)
425 {
426 global $conf, $langs;
427 $error = 0;
428
429 $this->db->begin();
430
431 if (!$error) {
432 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
433 $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
434
435 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
436 $resql = $this->db->query($sql);
437 if (!$resql) {
438 $error++;
439 $this->errors[] = "Error ".$this->db->lasterror();
440 }
441 }
442
443 if (!$error) {
444 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
445 $sql .= " WHERE rowid=".((int) $this->id);
446
447 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
448 $resql = $this->db->query($sql);
449 if (!$resql) {
450 $error++;
451 $this->errors[] = "Error ".$this->db->lasterror();
452 }
453 }
454
455 if (!$error) {
456 if (!$notrigger) {
457 if (!$error && !$notrigger) {
458 // Call triggers
459 $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
460 if ($result < 0) {
461 $error++;
462 }
463 // End call triggers
464 }
465 }
466 }
467
468 // Commit or rollback
469 if ($error) {
470 foreach ($this->errors as $errmsg) {
471 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
472 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
473 }
474 $this->db->rollback();
475 return -1 * $error;
476 } else {
477 $this->db->commit();
478 return 1;
479 }
480 }
481
482
483
491 public function createFromClone(User $user, $fromid)
492 {
493 $error = 0;
494
495 $object = new PaymentDonation($this->db);
496
497 $this->db->begin();
498
499 // Load source object
500 $object->fetch($fromid);
501 $object->id = 0;
502 $object->statut = 0;
503
504 // Clear fields
505 // ...
506
507 // Create clone
508 $object->context['createfromclone'] = 'createfromclone';
509 $result = $object->create($user);
510
511 // Other options
512 if ($result < 0) {
513 $this->error = $object->error;
514 $error++;
515 }
516
517 if (!$error) {
518 }
519
520 unset($object->context['createfromclone']);
521
522 // End
523 if (!$error) {
524 $this->db->commit();
525 return $object->id;
526 } else {
527 $this->db->rollback();
528 return -1;
529 }
530 }
531
532
539 public function getLibStatut($mode = 0)
540 {
541 return '';
542 }
543
544 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
552 public function LibStatut($status, $mode = 0)
553 {
554 // phpcs:enable
555 global $langs;
556
557 return '';
558 }
559
560
568 public function initAsSpecimen()
569 {
570 $this->id = 0;
571
572 $this->fk_donation = 0;
573 $this->datec = dol_now();
574 $this->tms = dol_now();
575 $this->datep = '';
576 $this->amount = 1000.80;
577 $this->fk_typepayment = 0;
578 $this->paymenttype = 0;
579 $this->num_payment = '';
580 $this->note_public = 'Public note';
581 $this->fk_bank = 0;
582 $this->fk_user_creat = 1;
583 $this->fk_user_modif = 1;
584
585 return 1;
586 }
587
588
601 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
602 {
603 global $conf;
604
605 $error = 0;
606
607 if (isModEnabled("bank")) {
608 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
609
610 $acc = new Account($this->db);
611 $acc->fetch($accountid);
612
614 if ($mode == 'payment_donation') {
615 $amount = $total;
616 }
617 // Insert payment into llx_bank
618 $bank_line_id = $acc->addline(
619 $this->datep,
620 $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
621 $label,
622 $amount,
623 $this->num_payment,
624 '',
625 $user,
626 $emetteur_nom,
627 $emetteur_banque
628 );
629
630 // Update fk_bank in llx_paiement.
631 // On connait ainsi le paiement qui a genere l'ecriture bancaire
632 if ($bank_line_id > 0) {
633 $result = $this->update_fk_bank($bank_line_id);
634 if ($result <= 0) {
635 $error++;
636 dol_print_error($this->db);
637 }
638
639 // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
640 $url = '';
641 if ($mode == 'payment_donation') {
642 $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
643 }
644 if ($url) {
645 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
646 if ($result <= 0) {
647 $error++;
648 dol_print_error($this->db);
649 }
650 }
651 } else {
652 $this->error = $acc->error;
653 $error++;
654 }
655 }
656
657 if (!$error) {
658 return 1;
659 } else {
660 return -1;
661 }
662 }
663
664
665 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
672 public function update_fk_bank($id_bank)
673 {
674 // phpcs:enable
675 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
676
677 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
678 $result = $this->db->query($sql);
679 if ($result) {
680 return 1;
681 } else {
682 $this->error = $this->db->error();
683 return 0;
684 }
685 }
686
694 public function getNomUrl($withpicto = 0, $maxlen = 0)
695 {
696 global $langs, $hookmanager;
697
698 $result = '';
699
700 $label = '<u>'.$langs->trans("DonationPayment").'</u>';
701 $label .= '<br>';
702 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
703
704 if (!empty($this->id)) {
705 $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
706 $linkend = '</a>';
707
708 if ($withpicto) {
709 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
710 }
711 if ($withpicto && $withpicto != 2) {
712 $result .= ' ';
713 }
714 if ($withpicto != 2) {
715 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
716 }
717 }
718
719 global $action;
720 $hookmanager->initHooks(array($this->element . 'dao'));
721 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
722 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
723 if ($reshook > 0) {
724 $result = $hookmanager->resPrint;
725 } else {
726 $result .= $hookmanager->resPrint;
727 }
728 return $result;
729 }
730}
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...