dolibarr  16.0.5
paymentdonation.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
24 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25 
26 
31 {
35  public $element = 'payment_donation';
36 
40  public $table_element = 'payment_donation';
41 
45  public $picto = 'payment';
46 
50  public $rowid;
51 
55  public $fk_donation;
56 
57  public $datec = '';
58 
59  public $tms = '';
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
69 
70  public $num_payment;
71 
75  public $fk_bank;
76 
80  public $fk_user_creat;
81 
85  public $fk_user_modif;
86 
91  public $total;
92 
93  public $type_code;
94  public $type_label;
95 
99  public $ext_payment_id;
100 
104  public $ext_payment_site;
105 
111  public function __construct($db)
112  {
113  $this->db = $db;
114  }
115 
124  public function create($user, $notrigger = false)
125  {
126  global $conf, $langs;
127 
128  $error = 0;
129 
130  $now = dol_now();
131 
132  // Validate parameters
133  if (!$this->datepaid) {
134  $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
135  return -1;
136  }
137 
138  // Clean parameters
139  if (isset($this->chid)) {
140  $this->chid = (int) $this->chid;
141  } elseif (isset($this->fk_donation)) {
142  // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
143  // (keep priority to chid property)
144  $this->chid = (int) $this->fk_donation;
145  }
146  if (isset($this->fk_donation)) {
147  $this->fk_donation = (int) $this->fk_donation;
148  }
149  if (isset($this->amount)) {
150  $this->amount = trim($this->amount);
151  }
152  if (isset($this->fk_typepayment)) {
153  $this->fk_typepayment = trim($this->fk_typepayment);
154  }
155  if (isset($this->num_payment)) {
156  $this->num_payment = trim($this->num_payment);
157  }
158  if (isset($this->note_public)) {
159  $this->note_public = trim($this->note_public);
160  }
161  if (isset($this->fk_bank)) {
162  $this->fk_bank = (int) $this->fk_bank;
163  }
164  if (isset($this->fk_user_creat)) {
165  $this->fk_user_creat = (int) $this->fk_user_creat;
166  }
167  if (isset($this->fk_user_modif)) {
168  $this->fk_user_modif = (int) $this->fk_user_modif;
169  }
170 
171  $totalamount = 0;
172  foreach ($this->amounts as $key => $value) { // How payment is dispatch
173  $newvalue = price2num($value, 'MT');
174  $this->amounts[$key] = $newvalue;
175  $totalamount += $newvalue;
176  }
177  $totalamount = price2num($totalamount);
178 
179  // Check parameters
180  if ($totalamount == 0) {
181  return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
182  }
183 
184 
185  $this->db->begin();
186 
187  if ($totalamount != 0) {
188  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
189  $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
190  $sql .= " fk_user_creat, fk_bank)";
191  $sql .= " VALUES ($this->chid, '".$this->db->idate($now)."',";
192  $sql .= " '".$this->db->idate($this->datepaid)."',";
193  $sql .= " ".price2num($totalamount).",";
194  $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ";
195  $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").",";
196  $sql .= " ".$user->id.", 0)";
197 
198  dol_syslog(get_class($this)."::create", LOG_DEBUG);
199  $resql = $this->db->query($sql);
200  if ($resql) {
201  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
202  $this->ref = $this->id;
203  } else {
204  $error++;
205  }
206  }
207 
208  if (!$error && !$notrigger) {
209  // Call triggers
210  $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
211  if ($result < 0) {
212  $error++;
213  }
214  // End call triggers
215  }
216 
217  if ($totalamount != 0 && !$error) {
218  $this->amount = $totalamount;
219  $this->total = $totalamount; // deprecated
220  $this->db->commit();
221  return $this->id;
222  } else {
223  $this->error = $this->db->error();
224  $this->db->rollback();
225  return -1;
226  }
227  }
228 
235  public function fetch($id)
236  {
237  global $langs;
238  $sql = "SELECT";
239  $sql .= " t.rowid,";
240  $sql .= " t.fk_donation,";
241  $sql .= " t.datec,";
242  $sql .= " t.tms,";
243  $sql .= " t.datep,";
244  $sql .= " t.amount,";
245  $sql .= " t.fk_typepayment,";
246  $sql .= " t.num_payment,";
247  $sql .= " t.note as note_public,";
248  $sql .= " t.fk_bank,";
249  $sql .= " t.fk_user_creat,";
250  $sql .= " t.fk_user_modif,";
251  $sql .= " pt.code as type_code, pt.libelle as type_label,";
252  $sql .= ' b.fk_account';
253  $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
254  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
255  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
256  $sql .= " WHERE t.rowid = ".((int) $id);
257 
258  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
259  $resql = $this->db->query($sql);
260  if ($resql) {
261  if ($this->db->num_rows($resql)) {
262  $obj = $this->db->fetch_object($resql);
263 
264  $this->id = $obj->rowid;
265  $this->ref = $obj->rowid;
266 
267  $this->fk_donation = $obj->fk_donation;
268  $this->datec = $this->db->jdate($obj->datec);
269  $this->tms = $this->db->jdate($obj->tms);
270  $this->datep = $this->db->jdate($obj->datep);
271  $this->amount = $obj->amount;
272  $this->fk_typepayment = $obj->fk_typepayment; // For backward compatibility
273  $this->paymenttype = $obj->fk_typepayment;
274  $this->num_payment = $obj->num_payment;
275  $this->note_public = $obj->note_public;
276  $this->fk_bank = $obj->fk_bank;
277  $this->fk_user_creat = $obj->fk_user_creat;
278  $this->fk_user_modif = $obj->fk_user_modif;
279 
280  $this->type_code = $obj->type_code;
281  $this->type_label = $obj->type_label;
282 
283  $this->bank_account = $obj->fk_account;
284  $this->bank_line = $obj->fk_bank;
285  }
286  $this->db->free($resql);
287 
288  return 1;
289  } else {
290  $this->error = "Error ".$this->db->lasterror();
291  return -1;
292  }
293  }
294 
295 
303  public function update($user, $notrigger = 0)
304  {
305  global $conf, $langs;
306  $error = 0;
307 
308  // Clean parameters
309 
310  if (isset($this->fk_donation)) {
311  $this->fk_donation = (int) $this->fk_donation;
312  }
313  if (isset($this->amount)) {
314  $this->amount = trim($this->amount);
315  }
316  if (isset($this->fk_typepayment)) {
317  $this->fk_typepayment = trim($this->fk_typepayment);
318  }
319  if (isset($this->num_payment)) {
320  $this->num_payment = trim($this->num_payment);
321  }
322  if (isset($this->note_public)) {
323  $this->note_public = trim($this->note_public);
324  }
325  if (isset($this->fk_bank)) {
326  $this->fk_bank = (int) $this->fk_bank;
327  }
328  if (isset($this->fk_user_creat)) {
329  $this->fk_user_creat = (int) $this->fk_user_creat;
330  }
331  if (isset($this->fk_user_modif)) {
332  $this->fk_user_modif = (int) $this->fk_user_modif;
333  }
334 
335  // Check parameters
336  // Put here code to add control on parameters values
337 
338  // Update request
339  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
340  $sql .= " fk_donation=".(isset($this->fk_donation) ? $this->fk_donation : "null").",";
341  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
342  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
343  $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
344  $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
345  $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
346  $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
347  $sql .= " note=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
348  $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
349  $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
350  $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null")."";
351  $sql .= " WHERE rowid=".(int) $this->id;
352 
353  $this->db->begin();
354 
355  dol_syslog(get_class($this)."::update", LOG_DEBUG);
356  $resql = $this->db->query($sql);
357  if (!$resql) {
358  $error++;
359  $this->errors[] = "Error ".$this->db->lasterror();
360  }
361 
362  if (!$error) {
363  if (!$notrigger) {
364  if (!$error && !$notrigger) {
365  // Call triggers
366  $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
367  if ($result < 0) {
368  $error++;
369  }
370  // End call triggers
371  }
372  }
373  }
374 
375  // Commit or rollback
376  if ($error) {
377  foreach ($this->errors as $errmsg) {
378  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
379  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
380  }
381  $this->db->rollback();
382  return -1 * $error;
383  } else {
384  $this->db->commit();
385  return 1;
386  }
387  }
388 
389 
397  public function delete($user, $notrigger = 0)
398  {
399  global $conf, $langs;
400  $error = 0;
401 
402  $this->db->begin();
403 
404  if (!$error) {
405  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
406  $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
407 
408  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
409  $resql = $this->db->query($sql);
410  if (!$resql) {
411  $error++; $this->errors[] = "Error ".$this->db->lasterror();
412  }
413  }
414 
415  if (!$error) {
416  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
417  $sql .= " WHERE rowid=".((int) $this->id);
418 
419  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
420  $resql = $this->db->query($sql);
421  if (!$resql) {
422  $error++;
423  $this->errors[] = "Error ".$this->db->lasterror();
424  }
425  }
426 
427  if (!$error) {
428  if (!$notrigger) {
429  if (!$error && !$notrigger) {
430  // Call triggers
431  $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
432  if ($result < 0) {
433  $error++;
434  }
435  // End call triggers
436  }
437  }
438  }
439 
440  // Commit or rollback
441  if ($error) {
442  foreach ($this->errors as $errmsg) {
443  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
444  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
445  }
446  $this->db->rollback();
447  return -1 * $error;
448  } else {
449  $this->db->commit();
450  return 1;
451  }
452  }
453 
454 
455 
463  public function createFromClone(User $user, $fromid)
464  {
465  $error = 0;
466 
467  $object = new PaymentDonation($this->db);
468 
469  $this->db->begin();
470 
471  // Load source object
472  $object->fetch($fromid);
473  $object->id = 0;
474  $object->statut = 0;
475 
476  // Clear fields
477  // ...
478 
479  // Create clone
480  $object->context['createfromclone'] = 'createfromclone';
481  $result = $object->create($user);
482 
483  // Other options
484  if ($result < 0) {
485  $this->error = $object->error;
486  $error++;
487  }
488 
489  if (!$error) {
490  }
491 
492  unset($object->context['createfromclone']);
493 
494  // End
495  if (!$error) {
496  $this->db->commit();
497  return $object->id;
498  } else {
499  $this->db->rollback();
500  return -1;
501  }
502  }
503 
504 
511  public function getLibStatut($mode = 0)
512  {
513  return '';
514  }
515 
516  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
524  public function LibStatut($status, $mode = 0)
525  {
526  // phpcs:enable
527  global $langs;
528 
529  return '';
530  }
531 
532 
540  public function initAsSpecimen()
541  {
542  $this->id = 0;
543 
544  $this->fk_donation = '';
545  $this->datec = '';
546  $this->tms = '';
547  $this->datep = '';
548  $this->amount = '';
549  $this->fk_typepayment = '';
550  $this->paymenttype = '';
551  $this->num_payment = '';
552  $this->note_public = '';
553  $this->fk_bank = '';
554  $this->fk_user_creat = '';
555  $this->fk_user_modif = '';
556  }
557 
558 
571  public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
572  {
573  global $conf;
574 
575  $error = 0;
576 
577  if (!empty($conf->banque->enabled)) {
578  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
579 
580  $acc = new Account($this->db);
581  $acc->fetch($accountid);
582 
584  if ($mode == 'payment_donation') {
585  $amount = $total;
586  }
587 
588  // Insert payment into llx_bank
589  $bank_line_id = $acc->addline(
590  $this->datepaid,
591  $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
592  $label,
593  $amount,
594  $this->num_payment,
595  '',
596  $user,
597  $emetteur_nom,
598  $emetteur_banque
599  );
600 
601  // Update fk_bank in llx_paiement.
602  // On connait ainsi le paiement qui a genere l'ecriture bancaire
603  if ($bank_line_id > 0) {
604  $result = $this->update_fk_bank($bank_line_id);
605  if ($result <= 0) {
606  $error++;
607  dol_print_error($this->db);
608  }
609 
610  // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
611  $url = '';
612  if ($mode == 'payment_donation') {
613  $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
614  }
615  if ($url) {
616  $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
617  if ($result <= 0) {
618  $error++;
619  dol_print_error($this->db);
620  }
621  }
622  } else {
623  $this->error = $acc->error;
624  $error++;
625  }
626  }
627 
628  if (!$error) {
629  return 1;
630  } else {
631  return -1;
632  }
633  }
634 
635 
636  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
643  public function update_fk_bank($id_bank)
644  {
645  // phpcs:enable
646  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
647 
648  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
649  $result = $this->db->query($sql);
650  if ($result) {
651  return 1;
652  } else {
653  $this->error = $this->db->error();
654  return 0;
655  }
656  }
657 
665  public function getNomUrl($withpicto = 0, $maxlen = 0)
666  {
667  global $langs, $hookmanager;
668 
669  $result = '';
670 
671  $label = '<u>'.$langs->trans("DonationPayment").'</u>';
672  $label .= '<br>';
673  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
674 
675  if (!empty($this->id)) {
676  $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
677  $linkend = '</a>';
678 
679  if ($withpicto) {
680  $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
681  }
682  if ($withpicto && $withpicto != 2) {
683  $result .= ' ';
684  }
685  if ($withpicto != 2) {
686  $result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
687  }
688  }
689 
690  global $action;
691  $hookmanager->initHooks(array($this->element . 'dao'));
692  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
693  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
694  if ($reshook > 0) {
695  $result = $hookmanager->resPrint;
696  } else {
697  $result .= $hookmanager->resPrint;
698  }
699  return $result;
700  }
701 }
db
$conf db
API class for accounts.
Definition: inc.php:41
PaymentDonation\$total
$total
Definition: paymentdonation.class.php:91
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
dol_trunc
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.
Definition: functions.lib.php:3805
PaymentDonation\getLibStatut
getLibStatut($mode=0)
Retourne le libelle du statut d'un don (brouillon, validee, abandonnee, payee)
Definition: paymentdonation.class.php:511
PaymentDonation\__construct
__construct($db)
Constructor.
Definition: paymentdonation.class.php:111
PaymentDonation\LibStatut
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
Definition: paymentdonation.class.php:524
PaymentDonation\create
create($user, $notrigger=false)
Create payment of donation into database.
Definition: paymentdonation.class.php:124
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
PaymentDonation\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: paymentdonation.class.php:463
PaymentDonation
Class to manage payments of donations.
Definition: paymentdonation.class.php:30
ref
$object ref
Definition: info.php:77
PaymentDonation\getNomUrl
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
Definition: paymentdonation.class.php:665
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
PaymentDonation\fetch
fetch($id)
Load object in memory from database.
Definition: paymentdonation.class.php:235
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
PaymentDonation\update
update($user, $notrigger=0)
Update database.
Definition: paymentdonation.class.php:303
PaymentDonation\update_fk_bank
update_fk_bank($id_bank)
Update link between the donation payment and the generated line in llx_bank.
Definition: paymentdonation.class.php:643
User
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
PaymentDonation\addPaymentToBank
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.
Definition: paymentdonation.class.php:571
PaymentDonation\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: paymentdonation.class.php:540
Account
Class to manage bank accounts.
Definition: account.class.php:38