dolibarr 21.0.0-beta
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
117 public $total;
118
122 public $type_code;
126 public $type_label;
130 public $chid;
134 public $datepaid;
138 public $bank_account;
142 public $bank_line;
143
147 public $ext_payment_id;
148
152 public $ext_payment_site;
153
159 public function __construct($db)
160 {
161 $this->db = $db;
162 }
163
172 public function create($user, $notrigger = 0)
173 {
174 $error = 0;
175
176 $now = dol_now();
177
178 // Validate parameters
179 if (!$this->datep) {
180 $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
181 return -1;
182 }
183
184 // Clean parameters
185 if (isset($this->chid)) {
186 $this->chid = (int) $this->chid;
187 } elseif (isset($this->fk_donation)) {
188 // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
189 // (keep priority to chid property)
190 $this->chid = (int) $this->fk_donation;
191 }
192 if (isset($this->fk_donation)) {
193 $this->fk_donation = (int) $this->fk_donation;
194 }
195 if (isset($this->amount)) {
196 $this->amount = (float) price2num($this->amount);
197 }
198 if (isset($this->fk_typepayment)) {
199 $this->fk_typepayment = (int) price2num($this->fk_typepayment);
200 }
201 if (isset($this->num_payment)) {
202 $this->num_payment = trim($this->num_payment);
203 }
204 if (isset($this->note_public)) {
205 $this->note_public = trim($this->note_public);
206 }
207 if (isset($this->fk_bank)) {
208 $this->fk_bank = (int) $this->fk_bank;
209 }
210 if (isset($this->fk_user_creat)) {
211 $this->fk_user_creat = (int) $this->fk_user_creat;
212 }
213 if (isset($this->fk_user_modif)) {
214 $this->fk_user_modif = (int) $this->fk_user_modif;
215 }
216
217 $totalamount = 0;
218 foreach ($this->amounts as $key => $value) { // How payment is dispatch
219 $newvalue = (float) price2num($value, 'MT');
220 $this->amounts[$key] = $newvalue;
221 $totalamount += $newvalue;
222 }
223 $totalamount = (float) price2num($totalamount);
224
225 // Check parameters
226 if ($totalamount == 0) {
227 return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
228 }
229
230
231 $this->db->begin();
232
233 if ($totalamount != 0) {
234 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
235 $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
236 $sql .= " fk_user_creat, fk_bank)";
237 $sql .= " VALUES (".((int) $this->chid).", '".$this->db->idate($now)."',";
238 $sql .= " '".$this->db->idate($this->datep)."',";
239 $sql .= " ".((float) price2num($totalamount)).",";
240 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ";
241 $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").",";
242 $sql .= " ".((int) $user->id).", 0)";
243
244 dol_syslog(get_class($this)."::create", LOG_DEBUG);
245 $resql = $this->db->query($sql);
246 if ($resql) {
247 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
248 $this->ref = (string) $this->id;
249 } else {
250 $error++;
251 }
252 }
253
254 if (!$error && !$notrigger) {
255 // Call triggers
256 $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
257 if ($result < 0) {
258 $error++;
259 }
260 // End call triggers
261 }
262
263 if ($totalamount != 0 && !$error) {
264 $this->amount = $totalamount;
265 $this->total = $totalamount; // deprecated
266 $this->db->commit();
267 return $this->id;
268 } else {
269 $this->error = $this->db->error();
270 $this->db->rollback();
271 return -1;
272 }
273 }
274
281 public function fetch($id)
282 {
283 global $langs;
284 $sql = "SELECT";
285 $sql .= " t.rowid,";
286 $sql .= " t.fk_donation,";
287 $sql .= " t.datec,";
288 $sql .= " t.tms,";
289 $sql .= " t.datep,";
290 $sql .= " t.amount,";
291 $sql .= " t.fk_typepayment,";
292 $sql .= " t.num_payment,";
293 $sql .= " t.note as note_public,";
294 $sql .= " t.fk_bank,";
295 $sql .= " t.fk_user_creat,";
296 $sql .= " t.fk_user_modif,";
297 $sql .= " pt.code as type_code, pt.libelle as type_label,";
298 $sql .= ' b.fk_account';
299 $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
300 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
301 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
302 $sql .= " WHERE t.rowid = ".((int) $id);
303
304 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
305 $resql = $this->db->query($sql);
306 if ($resql) {
307 if ($this->db->num_rows($resql)) {
308 $obj = $this->db->fetch_object($resql);
309
310 $this->id = $obj->rowid;
311 $this->ref = $obj->rowid;
312
313 $this->fk_donation = $obj->fk_donation;
314 $this->datec = $this->db->jdate($obj->datec);
315 $this->tms = $this->db->jdate($obj->tms);
316 $this->datep = $this->db->jdate($obj->datep);
317 $this->amount = $obj->amount;
318 $this->fk_typepayment = $obj->fk_typepayment; // Id on type of payent
319 $this->paymenttype = $obj->fk_typepayment; // Id on type of payment. We should store the code into paymenttype.
320 $this->num_payment = $obj->num_payment;
321 $this->note_public = $obj->note_public;
322 $this->fk_bank = $obj->fk_bank;
323 $this->fk_user_creat = $obj->fk_user_creat;
324 $this->fk_user_modif = $obj->fk_user_modif;
325
326 $this->type_code = $obj->type_code;
327 $this->type_label = $obj->type_label;
328
329 $this->bank_account = $obj->fk_account;
330 $this->bank_line = $obj->fk_bank;
331 }
332 $this->db->free($resql);
333
334 return 1;
335 } else {
336 $this->error = "Error ".$this->db->lasterror();
337 return -1;
338 }
339 }
340
341
349 public function update($user, $notrigger = 0)
350 {
351 global $conf, $langs;
352 $error = 0;
353
354 // Clean parameters
355
356 if (isset($this->fk_donation)) {
357 $this->fk_donation = (int) $this->fk_donation;
358 }
359 if (isset($this->amount)) {
360 $this->amount = (float) price2num($this->amount);
361 }
362 if (isset($this->fk_typepayment)) {
363 $this->fk_typepayment = (int) price2num($this->fk_typepayment);
364 }
365 if (isset($this->num_payment)) {
366 $this->num_payment = trim($this->num_payment);
367 }
368 if (isset($this->note_public)) {
369 $this->note_public = trim($this->note_public);
370 }
371 if (isset($this->fk_bank)) {
372 $this->fk_bank = (int) $this->fk_bank;
373 }
374 if (isset($this->fk_user_creat)) {
375 $this->fk_user_creat = (int) $this->fk_user_creat;
376 }
377 if (isset($this->fk_user_modif)) {
378 $this->fk_user_modif = (int) $this->fk_user_modif;
379 }
380
381 // Check parameters
382 // Put here code to add control on parameters values
383
384 // Update request
385 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
386 $sql .= " fk_donation=".(isset($this->fk_donation) ? $this->fk_donation : "null").",";
387 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
388 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
389 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
390 $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
391 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
392 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
393 $sql .= " note=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
394 $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
395 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
396 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
397 $sql .= " WHERE rowid=".(int) $this->id;
398
399 $this->db->begin();
400
401 dol_syslog(get_class($this)."::update", LOG_DEBUG);
402 $resql = $this->db->query($sql);
403 if (!$resql) {
404 $error++;
405 $this->errors[] = "Error ".$this->db->lasterror();
406 }
407
408 if (!$error) {
409 if (!$notrigger) {
410 if (!$error && !$notrigger) {
411 // Call triggers
412 $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
413 if ($result < 0) {
414 $error++;
415 }
416 // End call triggers
417 }
418 }
419 }
420
421 // Commit or rollback
422 if ($error) {
423 foreach ($this->errors as $errmsg) {
424 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
425 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
426 }
427 $this->db->rollback();
428 return -1 * $error;
429 } else {
430 $this->db->commit();
431 return 1;
432 }
433 }
434
435
443 public function delete($user, $notrigger = 0)
444 {
445 global $conf, $langs;
446 $error = 0;
447
448 $this->db->begin();
449
450 if (!$error) {
451 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
452 $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
453
454 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
455 $resql = $this->db->query($sql);
456 if (!$resql) {
457 $error++;
458 $this->errors[] = "Error ".$this->db->lasterror();
459 }
460 }
461
462 if (!$error) {
463 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
464 $sql .= " WHERE rowid=".((int) $this->id);
465
466 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
467 $resql = $this->db->query($sql);
468 if (!$resql) {
469 $error++;
470 $this->errors[] = "Error ".$this->db->lasterror();
471 }
472 }
473
474 if (!$error) {
475 if (!$notrigger) {
476 if (!$error && !$notrigger) {
477 // Call triggers
478 $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
479 if ($result < 0) {
480 $error++;
481 }
482 // End call triggers
483 }
484 }
485 }
486
487 // Commit or rollback
488 if ($error) {
489 foreach ($this->errors as $errmsg) {
490 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
491 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
492 }
493 $this->db->rollback();
494 return -1 * $error;
495 } else {
496 $this->db->commit();
497 return 1;
498 }
499 }
500
501
502
510 public function createFromClone(User $user, $fromid)
511 {
512 $error = 0;
513
514 $object = new PaymentDonation($this->db);
515
516 $this->db->begin();
517
518 // Load source object
519 $object->fetch($fromid);
520 $object->id = 0;
521 $object->statut = 0;
522
523 // Clear fields
524 // ...
525
526 // Create clone
527 $object->context['createfromclone'] = 'createfromclone';
528 $result = $object->create($user);
529
530 // Other options
531 if ($result < 0) {
532 $this->error = $object->error;
533 $error++;
534 }
535
536 if (!$error) {
537 }
538
539 unset($object->context['createfromclone']);
540
541 // End
542 if (!$error) {
543 $this->db->commit();
544 return $object->id;
545 } else {
546 $this->db->rollback();
547 return -1;
548 }
549 }
550
551
558 public function getLibStatut($mode = 0)
559 {
560 return '';
561 }
562
563 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
571 public function LibStatut($status, $mode = 0)
572 {
573 // phpcs:enable
574 global $langs;
575
576 return '';
577 }
578
579
587 public function initAsSpecimen()
588 {
589 $this->id = 0;
590
591 $this->fk_donation = 0;
592 $this->datec = dol_now();
593 $this->tms = dol_now();
594 $this->datep = '';
595 $this->amount = 1000.80;
596 $this->fk_typepayment = 0;
597 $this->paymenttype = 0;
598 $this->num_payment = '';
599 $this->note_public = 'Public note';
600 $this->fk_bank = 0;
601 $this->fk_user_creat = 1;
602 $this->fk_user_modif = 1;
603
604 return 1;
605 }
606
607
620 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
621 {
622 global $conf;
623
624 $error = 0;
625 $amount = 0;
626
627 if (isModEnabled("bank")) {
628 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
629
630 $acc = new Account($this->db);
631 $acc->fetch($accountid);
632
633 $total = $this->total;
634 if ($mode == 'payment_donation') {
635 $amount = $total;
636 }
637 // Insert payment into llx_bank
638 $bank_line_id = $acc->addline(
639 $this->datep,
640 $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
641 $label,
642 $amount,
643 $this->num_payment,
644 0,
645 $user,
646 $emetteur_nom,
647 $emetteur_banque
648 );
649
650 // Update fk_bank in llx_paiement.
651 // On connait ainsi le paiement qui a genere l'ecriture bancaire
652 if ($bank_line_id > 0) {
653 $result = $this->update_fk_bank($bank_line_id);
654 if ($result <= 0) {
655 $error++;
656 dol_print_error($this->db);
657 }
658
659 // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
660 $url = '';
661 if ($mode == 'payment_donation') {
662 $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
663 }
664 if ($url) {
665 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
666 if ($result <= 0) {
667 $error++;
668 dol_print_error($this->db);
669 }
670 }
671 } else {
672 $this->error = $acc->error;
673 $error++;
674 }
675 }
676
677 if (!$error) {
678 return 1;
679 } else {
680 return -1;
681 }
682 }
683
684
685 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
692 public function update_fk_bank($id_bank)
693 {
694 // phpcs:enable
695 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
696
697 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
698 $result = $this->db->query($sql);
699 if ($result) {
700 return 1;
701 } else {
702 $this->error = $this->db->error();
703 return 0;
704 }
705 }
706
714 public function getNomUrl($withpicto = 0, $maxlen = 0)
715 {
716 global $langs, $hookmanager;
717
718 $result = '';
719
720 $label = '<u>'.$langs->trans("DonationPayment").'</u>';
721 $label .= '<br>';
722 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
723
724 if (!empty($this->id)) {
725 $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
726 $linkend = '</a>';
727
728 if ($withpicto) {
729 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
730 }
731 if ($withpicto && $withpicto != 2) {
732 $result .= ' ';
733 }
734 if ($withpicto != 2) {
735 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
736 }
737 }
738
739 global $action;
740 $hookmanager->initHooks(array($this->element . 'dao'));
741 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
742 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
743 if ($reshook > 0) {
744 $result = $hookmanager->resPrint;
745 } else {
746 $result .= $hookmanager->resPrint;
747 }
748 return $result;
749 }
750}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
$object ref
Definition info.php:89
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79