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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 
27 
32 {
36  public $element = 'payment_donation';
37 
41  public $table_element = 'payment_donation';
42 
46  public $picto = 'payment';
47 
51  public $rowid;
52 
56  public $fk_donation;
57 
58  public $datec = '';
59 
60  public $datep = '';
61 
62  public $amount; // Total amount of payment
63 
64  public $amounts = array(); // Array of amounts
65 
66  public $fk_typepayment; // Payment mode ID
67  public $paymenttype; // Payment mode ID or Code. TODO Use only the code in this field.
68 
73  public $num_payment;
74 
78  public $fk_bank;
79 
83  public $fk_user_creat;
84 
88  public $fk_user_modif;
89 
94  public $total;
95 
96  public $type_code;
97  public $type_label;
98  public $chid;
99  public $datepaid;
100  public $bank_account;
101  public $bank_line;
102 
106  public $ext_payment_id;
107 
111  public $ext_payment_site;
112 
118  public function __construct($db)
119  {
120  $this->db = $db;
121  }
122 
131  public function create($user, $notrigger = 0)
132  {
133  $error = 0;
134 
135  $now = dol_now();
136 
137  // Validate parameters
138  if (!$this->datep) {
139  $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
140  return -1;
141  }
142 
143  // Clean parameters
144  if (isset($this->chid)) {
145  $this->chid = (int) $this->chid;
146  } elseif (isset($this->fk_donation)) {
147  // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
148  // (keep priority to chid property)
149  $this->chid = (int) $this->fk_donation;
150  }
151  if (isset($this->fk_donation)) {
152  $this->fk_donation = (int) $this->fk_donation;
153  }
154  if (isset($this->amount)) {
155  $this->amount = trim($this->amount);
156  }
157  if (isset($this->fk_typepayment)) {
158  $this->fk_typepayment = trim($this->fk_typepayment);
159  }
160  if (isset($this->num_payment)) {
161  $this->num_payment = trim($this->num_payment);
162  }
163  if (isset($this->note_public)) {
164  $this->note_public = trim($this->note_public);
165  }
166  if (isset($this->fk_bank)) {
167  $this->fk_bank = (int) $this->fk_bank;
168  }
169  if (isset($this->fk_user_creat)) {
170  $this->fk_user_creat = (int) $this->fk_user_creat;
171  }
172  if (isset($this->fk_user_modif)) {
173  $this->fk_user_modif = (int) $this->fk_user_modif;
174  }
175 
176  $totalamount = 0;
177  foreach ($this->amounts as $key => $value) { // How payment is dispatch
178  $newvalue = price2num($value, 'MT');
179  $this->amounts[$key] = $newvalue;
180  $totalamount += $newvalue;
181  }
182  $totalamount = price2num($totalamount);
183 
184  // Check parameters
185  if ($totalamount == 0) {
186  return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
187  }
188 
189 
190  $this->db->begin();
191 
192  if ($totalamount != 0) {
193  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
194  $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
195  $sql .= " fk_user_creat, fk_bank)";
196  $sql .= " VALUES (".((int) $this->chid).", '".$this->db->idate($now)."',";
197  $sql .= " '".$this->db->idate($this->datep)."',";
198  $sql .= " ".((float) price2num($totalamount)).",";
199  $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ";
200  $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").",";
201  $sql .= " ".((int) $user->id).", 0)";
202 
203  dol_syslog(get_class($this)."::create", LOG_DEBUG);
204  $resql = $this->db->query($sql);
205  if ($resql) {
206  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
207  $this->ref = (string) $this->id;
208  } else {
209  $error++;
210  }
211  }
212 
213  if (!$error && !$notrigger) {
214  // Call triggers
215  $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
216  if ($result < 0) {
217  $error++;
218  }
219  // End call triggers
220  }
221 
222  if ($totalamount != 0 && !$error) {
223  $this->amount = $totalamount;
224  $this->total = $totalamount; // deprecated
225  $this->db->commit();
226  return $this->id;
227  } else {
228  $this->error = $this->db->error();
229  $this->db->rollback();
230  return -1;
231  }
232  }
233 
240  public function fetch($id)
241  {
242  global $langs;
243  $sql = "SELECT";
244  $sql .= " t.rowid,";
245  $sql .= " t.fk_donation,";
246  $sql .= " t.datec,";
247  $sql .= " t.tms,";
248  $sql .= " t.datep,";
249  $sql .= " t.amount,";
250  $sql .= " t.fk_typepayment,";
251  $sql .= " t.num_payment,";
252  $sql .= " t.note as note_public,";
253  $sql .= " t.fk_bank,";
254  $sql .= " t.fk_user_creat,";
255  $sql .= " t.fk_user_modif,";
256  $sql .= " pt.code as type_code, pt.libelle as type_label,";
257  $sql .= ' b.fk_account';
258  $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
259  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
260  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
261  $sql .= " WHERE t.rowid = ".((int) $id);
262 
263  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
264  $resql = $this->db->query($sql);
265  if ($resql) {
266  if ($this->db->num_rows($resql)) {
267  $obj = $this->db->fetch_object($resql);
268 
269  $this->id = $obj->rowid;
270  $this->ref = $obj->rowid;
271 
272  $this->fk_donation = $obj->fk_donation;
273  $this->datec = $this->db->jdate($obj->datec);
274  $this->tms = $this->db->jdate($obj->tms);
275  $this->datep = $this->db->jdate($obj->datep);
276  $this->amount = $obj->amount;
277  $this->fk_typepayment = $obj->fk_typepayment; // Id on type of payent
278  $this->paymenttype = $obj->fk_typepayment; // Id on type of payment. We should store the code into paymenttype.
279  $this->num_payment = $obj->num_payment;
280  $this->note_public = $obj->note_public;
281  $this->fk_bank = $obj->fk_bank;
282  $this->fk_user_creat = $obj->fk_user_creat;
283  $this->fk_user_modif = $obj->fk_user_modif;
284 
285  $this->type_code = $obj->type_code;
286  $this->type_label = $obj->type_label;
287 
288  $this->bank_account = $obj->fk_account;
289  $this->bank_line = $obj->fk_bank;
290  }
291  $this->db->free($resql);
292 
293  return 1;
294  } else {
295  $this->error = "Error ".$this->db->lasterror();
296  return -1;
297  }
298  }
299 
300 
308  public function update($user, $notrigger = 0)
309  {
310  global $conf, $langs;
311  $error = 0;
312 
313  // Clean parameters
314 
315  if (isset($this->fk_donation)) {
316  $this->fk_donation = (int) $this->fk_donation;
317  }
318  if (isset($this->amount)) {
319  $this->amount = trim($this->amount);
320  }
321  if (isset($this->fk_typepayment)) {
322  $this->fk_typepayment = trim($this->fk_typepayment);
323  }
324  if (isset($this->num_payment)) {
325  $this->num_payment = trim($this->num_payment);
326  }
327  if (isset($this->note_public)) {
328  $this->note_public = trim($this->note_public);
329  }
330  if (isset($this->fk_bank)) {
331  $this->fk_bank = (int) $this->fk_bank;
332  }
333  if (isset($this->fk_user_creat)) {
334  $this->fk_user_creat = (int) $this->fk_user_creat;
335  }
336  if (isset($this->fk_user_modif)) {
337  $this->fk_user_modif = (int) $this->fk_user_modif;
338  }
339 
340  // Check parameters
341  // Put here code to add control on parameters values
342 
343  // Update request
344  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
345  $sql .= " fk_donation=".(isset($this->fk_donation) ? $this->fk_donation : "null").",";
346  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
347  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
348  $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
349  $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
350  $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
351  $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
352  $sql .= " note=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
353  $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
354  $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
355  $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
356  $sql .= " WHERE rowid=".(int) $this->id;
357 
358  $this->db->begin();
359 
360  dol_syslog(get_class($this)."::update", LOG_DEBUG);
361  $resql = $this->db->query($sql);
362  if (!$resql) {
363  $error++;
364  $this->errors[] = "Error ".$this->db->lasterror();
365  }
366 
367  if (!$error) {
368  if (!$notrigger) {
369  if (!$error && !$notrigger) {
370  // Call triggers
371  $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
372  if ($result < 0) {
373  $error++;
374  }
375  // End call triggers
376  }
377  }
378  }
379 
380  // Commit or rollback
381  if ($error) {
382  foreach ($this->errors as $errmsg) {
383  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
384  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
385  }
386  $this->db->rollback();
387  return -1 * $error;
388  } else {
389  $this->db->commit();
390  return 1;
391  }
392  }
393 
394 
402  public function delete($user, $notrigger = 0)
403  {
404  global $conf, $langs;
405  $error = 0;
406 
407  $this->db->begin();
408 
409  if (!$error) {
410  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
411  $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
412 
413  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
414  $resql = $this->db->query($sql);
415  if (!$resql) {
416  $error++;
417  $this->errors[] = "Error ".$this->db->lasterror();
418  }
419  }
420 
421  if (!$error) {
422  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
423  $sql .= " WHERE rowid=".((int) $this->id);
424 
425  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
426  $resql = $this->db->query($sql);
427  if (!$resql) {
428  $error++;
429  $this->errors[] = "Error ".$this->db->lasterror();
430  }
431  }
432 
433  if (!$error) {
434  if (!$notrigger) {
435  if (!$error && !$notrigger) {
436  // Call triggers
437  $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
438  if ($result < 0) {
439  $error++;
440  }
441  // End call triggers
442  }
443  }
444  }
445 
446  // Commit or rollback
447  if ($error) {
448  foreach ($this->errors as $errmsg) {
449  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
450  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
451  }
452  $this->db->rollback();
453  return -1 * $error;
454  } else {
455  $this->db->commit();
456  return 1;
457  }
458  }
459 
460 
461 
469  public function createFromClone(User $user, $fromid)
470  {
471  $error = 0;
472 
473  $object = new PaymentDonation($this->db);
474 
475  $this->db->begin();
476 
477  // Load source object
478  $object->fetch($fromid);
479  $object->id = 0;
480  $object->statut = 0;
481 
482  // Clear fields
483  // ...
484 
485  // Create clone
486  $object->context['createfromclone'] = 'createfromclone';
487  $result = $object->create($user);
488 
489  // Other options
490  if ($result < 0) {
491  $this->error = $object->error;
492  $error++;
493  }
494 
495  if (!$error) {
496  }
497 
498  unset($object->context['createfromclone']);
499 
500  // End
501  if (!$error) {
502  $this->db->commit();
503  return $object->id;
504  } else {
505  $this->db->rollback();
506  return -1;
507  }
508  }
509 
510 
517  public function getLibStatut($mode = 0)
518  {
519  return '';
520  }
521 
522  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
530  public function LibStatut($status, $mode = 0)
531  {
532  // phpcs:enable
533  global $langs;
534 
535  return '';
536  }
537 
538 
546  public function initAsSpecimen()
547  {
548  $this->id = 0;
549 
550  $this->fk_donation = 0;
551  $this->datec = '';
552  $this->tms = dol_now();
553  $this->datep = '';
554  $this->amount = '';
555  $this->fk_typepayment = '';
556  $this->paymenttype = '';
557  $this->num_payment = '';
558  $this->note_public = '';
559  $this->fk_bank = 0;
560  $this->fk_user_creat = dol_now();
561  $this->fk_user_modif = dol_now();
562 
563  return 1;
564  }
565 
566 
579  public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
580  {
581  global $conf;
582 
583  $error = 0;
584 
585  if (isModEnabled("bank")) {
586  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
587 
588  $acc = new Account($this->db);
589  $acc->fetch($accountid);
590 
592  if ($mode == 'payment_donation') {
593  $amount = $total;
594  }
595  // Insert payment into llx_bank
596  $bank_line_id = $acc->addline(
597  $this->datep,
598  $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
599  $label,
600  $amount,
601  $this->num_payment,
602  '',
603  $user,
604  $emetteur_nom,
605  $emetteur_banque
606  );
607 
608  // Update fk_bank in llx_paiement.
609  // On connait ainsi le paiement qui a genere l'ecriture bancaire
610  if ($bank_line_id > 0) {
611  $result = $this->update_fk_bank($bank_line_id);
612  if ($result <= 0) {
613  $error++;
614  dol_print_error($this->db);
615  }
616 
617  // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
618  $url = '';
619  if ($mode == 'payment_donation') {
620  $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
621  }
622  if ($url) {
623  $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
624  if ($result <= 0) {
625  $error++;
626  dol_print_error($this->db);
627  }
628  }
629  } else {
630  $this->error = $acc->error;
631  $error++;
632  }
633  }
634 
635  if (!$error) {
636  return 1;
637  } else {
638  return -1;
639  }
640  }
641 
642 
643  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
650  public function update_fk_bank($id_bank)
651  {
652  // phpcs:enable
653  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
654 
655  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
656  $result = $this->db->query($sql);
657  if ($result) {
658  return 1;
659  } else {
660  $this->error = $this->db->error();
661  return 0;
662  }
663  }
664 
672  public function getNomUrl($withpicto = 0, $maxlen = 0)
673  {
674  global $langs, $hookmanager;
675 
676  $result = '';
677 
678  $label = '<u>'.$langs->trans("DonationPayment").'</u>';
679  $label .= '<br>';
680  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
681 
682  if (!empty($this->id)) {
683  $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
684  $linkend = '</a>';
685 
686  if ($withpicto) {
687  $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
688  }
689  if ($withpicto && $withpicto != 2) {
690  $result .= ' ';
691  }
692  if ($withpicto != 2) {
693  $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
694  }
695  }
696 
697  global $action;
698  $hookmanager->initHooks(array($this->element . 'dao'));
699  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
700  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
701  if ($reshook > 0) {
702  $result = $hookmanager->resPrint;
703  } else {
704  $result .= $hookmanager->resPrint;
705  }
706  return $result;
707  }
708 }
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 clicable name (with picto eventually)
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
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.
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...
td weekend span amount
Definition: global.inc.php:561