dolibarr  9.0.0
paymentdonation.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
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 <http://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  public $tms='';
59  public $datep='';
60  public $amount; // Total amount of payment
61  public $amounts=array(); // Array of amounts
62  public $typepayment;
63  public $num_payment;
64 
68  public $fk_bank;
69 
73  public $fk_user_creat;
74 
78  public $fk_user_modif;
79 
84  public $total;
85 
91  function __construct($db)
92  {
93  $this->db = $db;
94  }
95 
104  function create($user, $notrigger=false)
105  {
106  global $conf, $langs;
107 
108  $error=0;
109 
110  $now=dol_now();
111 
112  // Validate parameters
113  if (! $this->datepaid)
114  {
115  $this->error='ErrorBadValueForParameterCreatePaymentDonation';
116  return -1;
117  }
118 
119  // Clean parameters
120  if (isset($this->fk_donation)) $this->fk_donation=trim($this->fk_donation);
121  if (isset($this->amount)) $this->amount=trim($this->amount);
122  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
123  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
124  if (isset($this->note)) $this->note=trim($this->note);
125  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
126  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
127  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
128 
129  $totalamount = 0;
130  foreach ($this->amounts as $key => $value) // How payment is dispatch
131  {
132  $newvalue = price2num($value,'MT');
133  $this->amounts[$key] = $newvalue;
134  $totalamount += $newvalue;
135  }
136  $totalamount = price2num($totalamount);
137 
138  // Check parameters
139  if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
140 
141 
142  $this->db->begin();
143 
144  if ($totalamount != 0)
145  {
146  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
147  $sql.= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
148  $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
149  $sql.= " '".$this->db->idate($this->datepaid)."',";
150  $sql.= " ".$totalamount.",";
151  $sql.= " ".$this->paymenttype.", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
152  $sql.= " 0)";
153 
154  dol_syslog(get_class($this)."::create", LOG_DEBUG);
155  $resql=$this->db->query($sql);
156  if ($resql)
157  {
158  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
159  $this->ref = $this->id;
160  }
161  else
162  {
163  $error++;
164  }
165  }
166 
167  if (! $error && ! $notrigger)
168  {
169  // Call triggers
170  $result=$this->call_trigger('DONATION_PAYMENT_CREATE',$user);
171  if ($result < 0) { $error++; }
172  // End call triggers
173  }
174 
175  if ($totalamount != 0 && ! $error)
176  {
177  $this->amount=$totalamount;
178  $this->total=$totalamount; // deprecated
179  $this->db->commit();
180  return $this->id;
181  }
182  else
183  {
184  $this->error=$this->db->error();
185  $this->db->rollback();
186  return -1;
187  }
188  }
189 
196  function fetch($id)
197  {
198  global $langs;
199  $sql = "SELECT";
200  $sql.= " t.rowid,";
201  $sql.= " t.fk_donation,";
202  $sql.= " t.datec,";
203  $sql.= " t.tms,";
204  $sql.= " t.datep,";
205  $sql.= " t.amount,";
206  $sql.= " t.fk_typepayment,";
207  $sql.= " t.num_payment,";
208  $sql.= " t.note,";
209  $sql.= " t.fk_bank,";
210  $sql.= " t.fk_user_creat,";
211  $sql.= " t.fk_user_modif,";
212  $sql.= " pt.code as type_code, pt.libelle as type_libelle,";
213  $sql.= ' b.fk_account';
214  $sql.= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
215  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
216  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
217  $sql.= " WHERE t.rowid = ".$id;
218 
219  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
220  $resql=$this->db->query($sql);
221  if ($resql)
222  {
223  if ($this->db->num_rows($resql))
224  {
225  $obj = $this->db->fetch_object($resql);
226 
227  $this->id = $obj->rowid;
228  $this->ref = $obj->rowid;
229 
230  $this->fk_donation = $obj->fk_donation;
231  $this->datec = $this->db->jdate($obj->datec);
232  $this->tms = $this->db->jdate($obj->tms);
233  $this->datep = $this->db->jdate($obj->datep);
234  $this->amount = $obj->amount;
235  $this->fk_typepayment = $obj->fk_typepayment;
236  $this->num_payment = $obj->num_payment;
237  $this->note = $obj->note;
238  $this->fk_bank = $obj->fk_bank;
239  $this->fk_user_creat = $obj->fk_user_creat;
240  $this->fk_user_modif = $obj->fk_user_modif;
241 
242  $this->type_code = $obj->type_code;
243  $this->type_libelle = $obj->type_libelle;
244 
245  $this->bank_account = $obj->fk_account;
246  $this->bank_line = $obj->fk_bank;
247  }
248  $this->db->free($resql);
249 
250  return 1;
251  }
252  else
253  {
254  $this->error="Error ".$this->db->lasterror();
255  return -1;
256  }
257  }
258 
259 
267  function update($user, $notrigger=0)
268  {
269  global $conf, $langs;
270  $error=0;
271 
272  // Clean parameters
273 
274  if (isset($this->fk_donation)) $this->fk_donation=trim($this->fk_donation);
275  if (isset($this->amount)) $this->amount=trim($this->amount);
276  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
277  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
278  if (isset($this->note)) $this->note=trim($this->note);
279  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
280  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
281  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
282 
283  // Check parameters
284  // Put here code to add control on parameters values
285 
286  // Update request
287  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
288  $sql.= " fk_donation=".(isset($this->fk_donation)?$this->fk_donation:"null").",";
289  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
290  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
291  $sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
292  $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
293  $sql.= " fk_typepayment=".(isset($this->fk_typepayment)?$this->fk_typepayment:"null").",";
294  $sql.= " num_payment=".(isset($this->num_payment)?"'".$this->db->escape($this->num_payment)."'":"null").",";
295  $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
296  $sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
297  $sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
298  $sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
299  $sql.= " WHERE rowid=".$this->id;
300 
301  $this->db->begin();
302 
303  dol_syslog(get_class($this)."::update", LOG_DEBUG);
304  $resql = $this->db->query($sql);
305  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
306 
307  if (! $error)
308  {
309  if (! $notrigger)
310  {
311  if (! $error && ! $notrigger)
312  {
313  // Call triggers
314  $result=$this->call_trigger('DONATION_PAYMENT_MODIFY',$user);
315  if ($result < 0) { $error++; }
316  // End call triggers
317  }
318  }
319  }
320 
321  // Commit or rollback
322  if ($error)
323  {
324  foreach($this->errors as $errmsg)
325  {
326  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
327  $this->error.=($this->error?', '.$errmsg:$errmsg);
328  }
329  $this->db->rollback();
330  return -1*$error;
331  }
332  else
333  {
334  $this->db->commit();
335  return 1;
336  }
337  }
338 
339 
347  function delete($user, $notrigger=0)
348  {
349  global $conf, $langs;
350  $error=0;
351 
352  $this->db->begin();
353 
354  if (! $error)
355  {
356  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
357  $sql.= " WHERE type='payment_donation' AND url_id=".$this->id;
358 
359  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
360  $resql = $this->db->query($sql);
361  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
362  }
363 
364  if (! $error)
365  {
366  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
367  $sql.= " WHERE rowid=".$this->id;
368 
369  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
370  $resql = $this->db->query($sql);
371  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
372  }
373 
374  if (! $error)
375  {
376  if (! $notrigger)
377  {
378  if (! $error && ! $notrigger)
379  {
380  // Call triggers
381  $result=$this->call_trigger('DONATION_PAYMENT_DELETE',$user);
382  if ($result < 0) { $error++; }
383  // End call triggers
384  }
385  }
386  }
387 
388  // Commit or rollback
389  if ($error)
390  {
391  foreach($this->errors as $errmsg)
392  {
393  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
394  $this->error.=($this->error?', '.$errmsg:$errmsg);
395  }
396  $this->db->rollback();
397  return -1*$error;
398  }
399  else
400  {
401  $this->db->commit();
402  return 1;
403  }
404  }
405 
406 
407 
414  function createFromClone($fromid)
415  {
416  global $user,$langs;
417 
418  $error=0;
419 
420  $object=new PaymentDonation($this->db);
421 
422  $this->db->begin();
423 
424  // Load source object
425  $object->fetch($fromid);
426  $object->id=0;
427  $object->statut=0;
428 
429  // Clear fields
430  // ...
431 
432  // Create clone
433  $object->context['createfromclone'] = 'createfromclone';
434  $result=$object->create($user);
435 
436  // Other options
437  if ($result < 0)
438  {
439  $this->error=$object->error;
440  $error++;
441  }
442 
443  if (! $error)
444  {
445 
446  }
447 
448  unset($object->context['createfromclone']);
449 
450  // End
451  if (! $error)
452  {
453  $this->db->commit();
454  return $object->id;
455  }
456  else
457  {
458  $this->db->rollback();
459  return -1;
460  }
461  }
462 
463 
470  function getLibStatut($mode=0)
471  {
472  return '';
473  }
474 
475  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
483  function LibStatut($statut,$mode=0)
484  {
485  // phpcs:enable
486  global $langs;
487 
488  return '';
489  }
490 
491 
499  function initAsSpecimen()
500  {
501  $this->id=0;
502 
503  $this->fk_donation='';
504  $this->datec='';
505  $this->tms='';
506  $this->datep='';
507  $this->amount='';
508  $this->fk_typepayment='';
509  $this->num_payment='';
510  $this->note='';
511  $this->fk_bank='';
512  $this->fk_user_creat='';
513  $this->fk_user_modif='';
514  }
515 
516 
529  function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
530  {
531  global $conf;
532 
533  $error=0;
534 
535  if (! empty($conf->banque->enabled))
536  {
537  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
538 
539  $acc = new Account($this->db);
540  $acc->fetch($accountid);
541 
543  if ($mode == 'payment_donation') $amount=$total;
544 
545  // Insert payment into llx_bank
546  $bank_line_id = $acc->addline(
547  $this->datepaid,
548  $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
549  $label,
550  $amount,
551  $this->num_payment,
552  '',
553  $user,
554  $emetteur_nom,
555  $emetteur_banque
556  );
557 
558  // Update fk_bank in llx_paiement.
559  // On connait ainsi le paiement qui a genere l'ecriture bancaire
560  if ($bank_line_id > 0)
561  {
562  $result=$this->update_fk_bank($bank_line_id);
563  if ($result <= 0)
564  {
565  $error++;
566  dol_print_error($this->db);
567  }
568 
569  // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
570  $url='';
571  if ($mode == 'payment_donation') $url=DOL_URL_ROOT.'/don/payment/card.php?rowid=';
572  if ($url)
573  {
574  $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
575  if ($result <= 0)
576  {
577  $error++;
578  dol_print_error($this->db);
579  }
580  }
581  }
582  else
583  {
584  $this->error=$acc->error;
585  $error++;
586  }
587  }
588 
589  if (! $error)
590  {
591  return 1;
592  }
593  else
594  {
595  return -1;
596  }
597  }
598 
599 
600  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
607  function update_fk_bank($id_bank)
608  {
609  // phpcs:enable
610  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
611 
612  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
613  $result = $this->db->query($sql);
614  if ($result)
615  {
616  return 1;
617  }
618  else
619  {
620  $this->error=$this->db->error();
621  return 0;
622  }
623  }
624 
632  function getNomUrl($withpicto=0,$maxlen=0)
633  {
634  global $langs;
635 
636  $result='';
637 
638  if (empty($this->ref)) $this->ref=$this->lib;
639  $label = $langs->trans("ShowPayment").': '.$this->ref;
640 
641  if (!empty($this->id))
642  {
643  $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
644  $linkend='</a>';
645 
646  if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
647  if ($withpicto && $withpicto != 2) $result.=' ';
648  if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$linkend;
649  }
650 
651  return $result;
652  }
653 }
create($user, $notrigger=false)
Create payment of donation into database.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
Class to manage bank accounts.
update($user, $notrigger=0)
Update database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
fetch($id)
Load object in memory from database.
LibStatut($statut, $mode=0)
Renvoi le libelle d&#39;un statut donne.
createFromClone($fromid)
Load an object from its id and create a new one in database.
dol_now($mode='gmt')
Return date for now.
Class to manage payments of donations.
update_fk_bank($id_bank)
Update link between the donation payment and the generated line in llx_bank.
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.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;un don (brouillon, validee, abandonnee, payee)
__construct($db)
Constructor.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)