dolibarr 23.0.3
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-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2025 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 && !$notrigger) {
409 // Call triggers
410 $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
411 if ($result < 0) {
412 $error++;
413 }
414 // End call triggers
415 }
416
417 // Commit or rollback
418 if ($error) {
419 foreach ($this->errors as $errmsg) {
420 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
421 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
422 }
423 $this->db->rollback();
424 return -1 * $error;
425 } else {
426 $this->db->commit();
427 return 1;
428 }
429 }
430
431
439 public function delete($user, $notrigger = 0)
440 {
441 global $conf, $langs;
442
443 $error = 0;
444
445 $this->db->begin();
446
447 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
448 $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
449
450 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
451 $resql = $this->db->query($sql);
452 if (!$resql) {
453 $error++;
454 $this->errors[] = "Error ".$this->db->lasterror();
455 }
456
457 if (!$error) {
458 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
459 $sql .= " WHERE rowid=".((int) $this->id);
460
461 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
462 $resql = $this->db->query($sql);
463 if (!$resql) {
464 $error++;
465 $this->errors[] = "Error ".$this->db->lasterror();
466 }
467 }
468
469 if (!$error && !$notrigger) {
470 // Call triggers
471 $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
472 if ($result < 0) {
473 $error++;
474 }
475 // End call triggers
476 }
477
478 // Commit or rollback
479 if ($error) {
480 foreach ($this->errors as $errmsg) {
481 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
482 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
483 }
484 $this->db->rollback();
485 return -1 * $error;
486 } else {
487 $this->db->commit();
488 return 1;
489 }
490 }
491
492
493
501 public function createFromClone(User $user, $fromid)
502 {
503 $error = 0;
504
505 $object = new PaymentDonation($this->db);
506
507 $this->db->begin();
508
509 // Load source object
510 $object->fetch($fromid);
511 $object->id = 0;
512 $object->statut = 0;
513
514 // Clear fields
515 // ...
516
517 // Create clone
518 $object->context['createfromclone'] = 'createfromclone';
519 $result = $object->create($user);
520
521 // Other options
522 if ($result < 0) {
524 $error++;
525 }
526
527 if (!$error) {
528 }
529
530 unset($object->context['createfromclone']);
531
532 // End
533 if (!$error) {
534 $this->db->commit();
535 return $object->id;
536 } else {
537 $this->db->rollback();
538 return -1;
539 }
540 }
541
542
549 public function getLibStatut($mode = 0)
550 {
551 return '';
552 }
553
554 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
562 public function LibStatut($status, $mode = 0)
563 {
564 // phpcs:enable
565 global $langs;
566
567 return '';
568 }
569
570
578 public function initAsSpecimen()
579 {
580 $this->id = 0;
581
582 $this->fk_donation = 0;
583 $this->datec = dol_now();
584 $this->tms = dol_now();
585 $this->datep = '';
586 $this->amount = 1000.80;
587 $this->fk_typepayment = 0;
588 $this->paymenttype = 0;
589 $this->num_payment = '';
590 $this->note_public = 'Public note';
591 $this->fk_bank = 0;
592 $this->fk_user_creat = 1;
593 $this->fk_user_modif = 1;
594
595 return 1;
596 }
597
598
611 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
612 {
613 global $conf;
614
615 $error = 0;
616 $amount = 0;
617
618 if (isModEnabled("bank")) {
619 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
620
621 $acc = new Account($this->db);
622 $acc->fetch($accountid);
623
624 $total = $this->total;
625 if ($mode == 'payment_donation') {
626 $amount = $total;
627 }
628 // Insert payment into llx_bank
629 $bank_line_id = $acc->addline(
630 $this->datep,
631 (string) $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
632 $label,
633 (float) $amount,
634 $this->num_payment,
635 0,
636 $user,
637 $emetteur_nom,
638 $emetteur_banque
639 );
640
641 // Update fk_bank in llx_paiement.
642 // On connait ainsi le paiement qui a genere l'ecriture bancaire
643 if ($bank_line_id > 0) {
644 $result = $this->update_fk_bank($bank_line_id);
645 if ($result <= 0) {
646 $error++;
647 dol_print_error($this->db);
648 }
649
650 // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
651 $url = '';
652 if ($mode == 'payment_donation') {
653 $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
654 }
655 if ($url) {
656 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
657 if ($result <= 0) {
658 $error++;
659 dol_print_error($this->db);
660 }
661 }
662 } else {
663 $this->error = $acc->error;
664 $error++;
665 }
666 }
667
668 if (!$error) {
669 return 1;
670 } else {
671 return -1;
672 }
673 }
674
675
676 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
683 public function update_fk_bank($id_bank)
684 {
685 // phpcs:enable
686 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
687
688 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
689 $result = $this->db->query($sql);
690 if ($result) {
691 return 1;
692 } else {
693 $this->error = $this->db->error();
694 return 0;
695 }
696 }
697
705 public function getNomUrl($withpicto = 0, $maxlen = 0)
706 {
707 global $langs, $hookmanager;
708
709 $result = '';
710
711 $label = '<u>'.$langs->trans("DonationPayment").'</u>';
712 $label .= '<br>';
713 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
714
715 if (!empty($this->id)) {
716 $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
717 $linkend = '</a>';
718
719 if ($withpicto) {
720 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
721 }
722 if ($withpicto && $withpicto != 2) {
723 $result .= ' ';
724 }
725 if ($withpicto != 2) {
726 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
727 }
728 }
729
730 global $action;
731 $hookmanager->initHooks(array($this->element . 'dao'));
732 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
733 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
734 if ($reshook > 0) {
735 $result = $hookmanager->resPrint;
736 } else {
737 $result .= $hookmanager->resPrint;
738 }
739 return $result;
740 }
741}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$object ref
Definition info.php:90
Class to manage bank accounts.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
setErrorsFromObject($object)
setErrorsFromObject
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.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
isModEnabled($module)
Is Dolibarr module enabled.
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...
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:466