dolibarr  19.0.0-dev
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 or Code. TODO Use only the code in this field.
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  public $chid;
96  public $datepaid;
97  public $bank_account;
98  public $bank_line;
99 
103  public $ext_payment_id;
104 
108  public $ext_payment_site;
109 
115  public function __construct($db)
116  {
117  $this->db = $db;
118  }
119 
128  public function create($user, $notrigger = false)
129  {
130  global $conf, $langs;
131 
132  $error = 0;
133 
134  $now = dol_now();
135 
136  // Validate parameters
137  if (!$this->datep) {
138  $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
139  return -1;
140  }
141 
142  // Clean parameters
143  if (isset($this->chid)) {
144  $this->chid = (int) $this->chid;
145  } elseif (isset($this->fk_donation)) {
146  // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
147  // (keep priority to chid property)
148  $this->chid = (int) $this->fk_donation;
149  }
150  if (isset($this->fk_donation)) {
151  $this->fk_donation = (int) $this->fk_donation;
152  }
153  if (isset($this->amount)) {
154  $this->amount = trim($this->amount);
155  }
156  if (isset($this->fk_typepayment)) {
157  $this->fk_typepayment = trim($this->fk_typepayment);
158  }
159  if (isset($this->num_payment)) {
160  $this->num_payment = trim($this->num_payment);
161  }
162  if (isset($this->note_public)) {
163  $this->note_public = trim($this->note_public);
164  }
165  if (isset($this->fk_bank)) {
166  $this->fk_bank = (int) $this->fk_bank;
167  }
168  if (isset($this->fk_user_creat)) {
169  $this->fk_user_creat = (int) $this->fk_user_creat;
170  }
171  if (isset($this->fk_user_modif)) {
172  $this->fk_user_modif = (int) $this->fk_user_modif;
173  }
174 
175  $totalamount = 0;
176  foreach ($this->amounts as $key => $value) { // How payment is dispatch
177  $newvalue = price2num($value, 'MT');
178  $this->amounts[$key] = $newvalue;
179  $totalamount += $newvalue;
180  }
181  $totalamount = price2num($totalamount);
182 
183  // Check parameters
184  if ($totalamount == 0) {
185  return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
186  }
187 
188 
189  $this->db->begin();
190 
191  if ($totalamount != 0) {
192  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
193  $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
194  $sql .= " fk_user_creat, fk_bank)";
195  $sql .= " VALUES (".((int) $this->chid).", '".$this->db->idate($now)."',";
196  $sql .= " '".$this->db->idate($this->datep)."',";
197  $sql .= " ".((float) price2num($totalamount)).",";
198  $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ";
199  $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").",";
200  $sql .= " ".((int) $user->id).", 0)";
201 
202  dol_syslog(get_class($this)."::create", LOG_DEBUG);
203  $resql = $this->db->query($sql);
204  if ($resql) {
205  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
206  $this->ref = $this->id;
207  } else {
208  $error++;
209  }
210  }
211 
212  if (!$error && !$notrigger) {
213  // Call triggers
214  $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
215  if ($result < 0) {
216  $error++;
217  }
218  // End call triggers
219  }
220 
221  if ($totalamount != 0 && !$error) {
222  $this->amount = $totalamount;
223  $this->total = $totalamount; // deprecated
224  $this->db->commit();
225  return $this->id;
226  } else {
227  $this->error = $this->db->error();
228  $this->db->rollback();
229  return -1;
230  }
231  }
232 
239  public function fetch($id)
240  {
241  global $langs;
242  $sql = "SELECT";
243  $sql .= " t.rowid,";
244  $sql .= " t.fk_donation,";
245  $sql .= " t.datec,";
246  $sql .= " t.tms,";
247  $sql .= " t.datep,";
248  $sql .= " t.amount,";
249  $sql .= " t.fk_typepayment,";
250  $sql .= " t.num_payment,";
251  $sql .= " t.note as note_public,";
252  $sql .= " t.fk_bank,";
253  $sql .= " t.fk_user_creat,";
254  $sql .= " t.fk_user_modif,";
255  $sql .= " pt.code as type_code, pt.libelle as type_label,";
256  $sql .= ' b.fk_account';
257  $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
258  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
259  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
260  $sql .= " WHERE t.rowid = ".((int) $id);
261 
262  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
263  $resql = $this->db->query($sql);
264  if ($resql) {
265  if ($this->db->num_rows($resql)) {
266  $obj = $this->db->fetch_object($resql);
267 
268  $this->id = $obj->rowid;
269  $this->ref = $obj->rowid;
270 
271  $this->fk_donation = $obj->fk_donation;
272  $this->datec = $this->db->jdate($obj->datec);
273  $this->tms = $this->db->jdate($obj->tms);
274  $this->datep = $this->db->jdate($obj->datep);
275  $this->amount = $obj->amount;
276  $this->fk_typepayment = $obj->fk_typepayment; // Id on type of payent
277  $this->paymenttype = $obj->fk_typepayment; // Id on type of payment. We should store the code into paymenttype.
278  $this->num_payment = $obj->num_payment;
279  $this->note_public = $obj->note_public;
280  $this->fk_bank = $obj->fk_bank;
281  $this->fk_user_creat = $obj->fk_user_creat;
282  $this->fk_user_modif = $obj->fk_user_modif;
283 
284  $this->type_code = $obj->type_code;
285  $this->type_label = $obj->type_label;
286 
287  $this->bank_account = $obj->fk_account;
288  $this->bank_line = $obj->fk_bank;
289  }
290  $this->db->free($resql);
291 
292  return 1;
293  } else {
294  $this->error = "Error ".$this->db->lasterror();
295  return -1;
296  }
297  }
298 
299 
307  public function update($user, $notrigger = 0)
308  {
309  global $conf, $langs;
310  $error = 0;
311 
312  // Clean parameters
313 
314  if (isset($this->fk_donation)) {
315  $this->fk_donation = (int) $this->fk_donation;
316  }
317  if (isset($this->amount)) {
318  $this->amount = trim($this->amount);
319  }
320  if (isset($this->fk_typepayment)) {
321  $this->fk_typepayment = trim($this->fk_typepayment);
322  }
323  if (isset($this->num_payment)) {
324  $this->num_payment = trim($this->num_payment);
325  }
326  if (isset($this->note_public)) {
327  $this->note_public = trim($this->note_public);
328  }
329  if (isset($this->fk_bank)) {
330  $this->fk_bank = (int) $this->fk_bank;
331  }
332  if (isset($this->fk_user_creat)) {
333  $this->fk_user_creat = (int) $this->fk_user_creat;
334  }
335  if (isset($this->fk_user_modif)) {
336  $this->fk_user_modif = (int) $this->fk_user_modif;
337  }
338 
339  // Check parameters
340  // Put here code to add control on parameters values
341 
342  // Update request
343  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
344  $sql .= " fk_donation=".(isset($this->fk_donation) ? $this->fk_donation : "null").",";
345  $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
346  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
347  $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
348  $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
349  $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
350  $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
351  $sql .= " note=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
352  $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
353  $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
354  $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
355  $sql .= " WHERE rowid=".(int) $this->id;
356 
357  $this->db->begin();
358 
359  dol_syslog(get_class($this)."::update", LOG_DEBUG);
360  $resql = $this->db->query($sql);
361  if (!$resql) {
362  $error++;
363  $this->errors[] = "Error ".$this->db->lasterror();
364  }
365 
366  if (!$error) {
367  if (!$notrigger) {
368  if (!$error && !$notrigger) {
369  // Call triggers
370  $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
371  if ($result < 0) {
372  $error++;
373  }
374  // End call triggers
375  }
376  }
377  }
378 
379  // Commit or rollback
380  if ($error) {
381  foreach ($this->errors as $errmsg) {
382  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
383  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
384  }
385  $this->db->rollback();
386  return -1 * $error;
387  } else {
388  $this->db->commit();
389  return 1;
390  }
391  }
392 
393 
401  public function delete($user, $notrigger = 0)
402  {
403  global $conf, $langs;
404  $error = 0;
405 
406  $this->db->begin();
407 
408  if (!$error) {
409  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
410  $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
411 
412  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
413  $resql = $this->db->query($sql);
414  if (!$resql) {
415  $error++; $this->errors[] = "Error ".$this->db->lasterror();
416  }
417  }
418 
419  if (!$error) {
420  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
421  $sql .= " WHERE rowid=".((int) $this->id);
422 
423  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
424  $resql = $this->db->query($sql);
425  if (!$resql) {
426  $error++;
427  $this->errors[] = "Error ".$this->db->lasterror();
428  }
429  }
430 
431  if (!$error) {
432  if (!$notrigger) {
433  if (!$error && !$notrigger) {
434  // Call triggers
435  $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
436  if ($result < 0) {
437  $error++;
438  }
439  // End call triggers
440  }
441  }
442  }
443 
444  // Commit or rollback
445  if ($error) {
446  foreach ($this->errors as $errmsg) {
447  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
448  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
449  }
450  $this->db->rollback();
451  return -1 * $error;
452  } else {
453  $this->db->commit();
454  return 1;
455  }
456  }
457 
458 
459 
467  public function createFromClone(User $user, $fromid)
468  {
469  $error = 0;
470 
471  $object = new PaymentDonation($this->db);
472 
473  $this->db->begin();
474 
475  // Load source object
476  $object->fetch($fromid);
477  $object->id = 0;
478  $object->statut = 0;
479 
480  // Clear fields
481  // ...
482 
483  // Create clone
484  $object->context['createfromclone'] = 'createfromclone';
485  $result = $object->create($user);
486 
487  // Other options
488  if ($result < 0) {
489  $this->error = $object->error;
490  $error++;
491  }
492 
493  if (!$error) {
494  }
495 
496  unset($object->context['createfromclone']);
497 
498  // End
499  if (!$error) {
500  $this->db->commit();
501  return $object->id;
502  } else {
503  $this->db->rollback();
504  return -1;
505  }
506  }
507 
508 
515  public function getLibStatut($mode = 0)
516  {
517  return '';
518  }
519 
520  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
528  public function LibStatut($status, $mode = 0)
529  {
530  // phpcs:enable
531  global $langs;
532 
533  return '';
534  }
535 
536 
544  public function initAsSpecimen()
545  {
546  $this->id = 0;
547 
548  $this->fk_donation = '';
549  $this->datec = '';
550  $this->tms = '';
551  $this->datep = '';
552  $this->amount = '';
553  $this->fk_typepayment = '';
554  $this->paymenttype = '';
555  $this->num_payment = '';
556  $this->note_public = '';
557  $this->fk_bank = '';
558  $this->fk_user_creat = '';
559  $this->fk_user_modif = '';
560  }
561 
562 
575  public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
576  {
577  global $conf;
578 
579  $error = 0;
580 
581  if (isModEnabled("banque")) {
582  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
583 
584  $acc = new Account($this->db);
585  $acc->fetch($accountid);
586 
588  if ($mode == 'payment_donation') {
589  $amount = $total;
590  }
591  // Insert payment into llx_bank
592  $bank_line_id = $acc->addline(
593  $this->datep,
594  $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
595  $label,
596  $amount,
597  $this->num_payment,
598  '',
599  $user,
600  $emetteur_nom,
601  $emetteur_banque
602  );
603 
604  // Update fk_bank in llx_paiement.
605  // On connait ainsi le paiement qui a genere l'ecriture bancaire
606  if ($bank_line_id > 0) {
607  $result = $this->update_fk_bank($bank_line_id);
608  if ($result <= 0) {
609  $error++;
610  dol_print_error($this->db);
611  }
612 
613  // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
614  $url = '';
615  if ($mode == 'payment_donation') {
616  $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
617  }
618  if ($url) {
619  $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
620  if ($result <= 0) {
621  $error++;
622  dol_print_error($this->db);
623  }
624  }
625  } else {
626  $this->error = $acc->error;
627  $error++;
628  }
629  }
630 
631  if (!$error) {
632  return 1;
633  } else {
634  return -1;
635  }
636  }
637 
638 
639  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
646  public function update_fk_bank($id_bank)
647  {
648  // phpcs:enable
649  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
650 
651  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
652  $result = $this->db->query($sql);
653  if ($result) {
654  return 1;
655  } else {
656  $this->error = $this->db->error();
657  return 0;
658  }
659  }
660 
668  public function getNomUrl($withpicto = 0, $maxlen = 0)
669  {
670  global $langs, $hookmanager;
671 
672  $result = '';
673 
674  $label = '<u>'.$langs->trans("DonationPayment").'</u>';
675  $label .= '<br>';
676  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
677 
678  if (!empty($this->id)) {
679  $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
680  $linkend = '</a>';
681 
682  if ($withpicto) {
683  $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
684  }
685  if ($withpicto && $withpicto != 2) {
686  $result .= ' ';
687  }
688  if ($withpicto != 2) {
689  $result .= $link.($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
690  }
691  }
692 
693  global $action;
694  $hookmanager->initHooks(array($this->element . 'dao'));
695  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
696  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
697  if ($reshook > 0) {
698  $result = $hookmanager->resPrint;
699  } else {
700  $result .= $hookmanager->resPrint;
701  }
702  return $result;
703  }
704 }
$object ref
Definition: info.php:78
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.
create($user, $notrigger=false)
Create payment of donation into database.
initAsSpecimen()
Initialise an instance with random values.
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:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
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...