dolibarr  7.0.0-beta
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 {
32  public $element='payment_donation';
33  public $table_element='payment_donation';
34  public $picto = 'payment';
35 
36  public $rowid;
37 
38  public $fk_donation;
39  public $datec='';
40  public $tms='';
41  public $datep='';
42  public $amount; // Total amount of payment
43  public $amounts=array(); // Array of amounts
44  public $typepayment;
45  public $num_payment;
46  public $fk_bank;
47  public $fk_user_creat;
48  public $fk_user_modif;
49 
54  public $total;
55 
61  function __construct($db)
62  {
63  $this->db = $db;
64  }
65 
74  function create($user, $notrigger=false)
75  {
76  global $conf, $langs;
77 
78  $error=0;
79 
80  $now=dol_now();
81 
82  // Validate parameters
83  if (! $this->datepaid)
84  {
85  $this->error='ErrorBadValueForParameterCreatePaymentDonation';
86  return -1;
87  }
88 
89  // Clean parameters
90  if (isset($this->fk_donation)) $this->fk_donation=trim($this->fk_donation);
91  if (isset($this->amount)) $this->amount=trim($this->amount);
92  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
93  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
94  if (isset($this->note)) $this->note=trim($this->note);
95  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
96  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
97  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
98 
99  $totalamount = 0;
100  foreach ($this->amounts as $key => $value) // How payment is dispatch
101  {
102  $newvalue = price2num($value,'MT');
103  $this->amounts[$key] = $newvalue;
104  $totalamount += $newvalue;
105  }
106  $totalamount = price2num($totalamount);
107 
108  // Check parameters
109  if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
110 
111 
112  $this->db->begin();
113 
114  if ($totalamount != 0)
115  {
116  $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
117  $sql.= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
118  $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
119  $sql.= " '".$this->db->idate($this->datepaid)."',";
120  $sql.= " ".$totalamount.",";
121  $sql.= " ".$this->paymenttype.", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
122  $sql.= " 0)";
123 
124  dol_syslog(get_class($this)."::create", LOG_DEBUG);
125  $resql=$this->db->query($sql);
126  if ($resql)
127  {
128  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
129  $this->ref = $this->id;
130  }
131  else
132  {
133  $error++;
134  }
135  }
136 
137  if (! $error && ! $notrigger)
138  {
139  // Call triggers
140  $result=$this->call_trigger('DONATION_PAYMENT_CREATE',$user);
141  if ($result < 0) { $error++; }
142  // End call triggers
143  }
144 
145  if ($totalamount != 0 && ! $error)
146  {
147  $this->amount=$totalamount;
148  $this->total=$totalamount; // deprecated
149  $this->db->commit();
150  return $this->id;
151  }
152  else
153  {
154  $this->error=$this->db->error();
155  $this->db->rollback();
156  return -1;
157  }
158  }
159 
166  function fetch($id)
167  {
168  global $langs;
169  $sql = "SELECT";
170  $sql.= " t.rowid,";
171  $sql.= " t.fk_donation,";
172  $sql.= " t.datec,";
173  $sql.= " t.tms,";
174  $sql.= " t.datep,";
175  $sql.= " t.amount,";
176  $sql.= " t.fk_typepayment,";
177  $sql.= " t.num_payment,";
178  $sql.= " t.note,";
179  $sql.= " t.fk_bank,";
180  $sql.= " t.fk_user_creat,";
181  $sql.= " t.fk_user_modif,";
182  $sql.= " pt.code as type_code, pt.libelle as type_libelle,";
183  $sql.= ' b.fk_account';
184  $sql.= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
185  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id AND pt.entity IN (".getEntity('c_paiement').")";
186  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
187  $sql.= " WHERE t.rowid = ".$id;
188 
189  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
190  $resql=$this->db->query($sql);
191  if ($resql)
192  {
193  if ($this->db->num_rows($resql))
194  {
195  $obj = $this->db->fetch_object($resql);
196 
197  $this->id = $obj->rowid;
198  $this->ref = $obj->rowid;
199 
200  $this->fk_donation = $obj->fk_donation;
201  $this->datec = $this->db->jdate($obj->datec);
202  $this->tms = $this->db->jdate($obj->tms);
203  $this->datep = $this->db->jdate($obj->datep);
204  $this->amount = $obj->amount;
205  $this->fk_typepayment = $obj->fk_typepayment;
206  $this->num_payment = $obj->num_payment;
207  $this->note = $obj->note;
208  $this->fk_bank = $obj->fk_bank;
209  $this->fk_user_creat = $obj->fk_user_creat;
210  $this->fk_user_modif = $obj->fk_user_modif;
211 
212  $this->type_code = $obj->type_code;
213  $this->type_libelle = $obj->type_libelle;
214 
215  $this->bank_account = $obj->fk_account;
216  $this->bank_line = $obj->fk_bank;
217  }
218  $this->db->free($resql);
219 
220  return 1;
221  }
222  else
223  {
224  $this->error="Error ".$this->db->lasterror();
225  return -1;
226  }
227  }
228 
229 
237  function update($user, $notrigger=0)
238  {
239  global $conf, $langs;
240  $error=0;
241 
242  // Clean parameters
243 
244  if (isset($this->fk_donation)) $this->fk_donation=trim($this->fk_donation);
245  if (isset($this->amount)) $this->amount=trim($this->amount);
246  if (isset($this->fk_typepayment)) $this->fk_typepayment=trim($this->fk_typepayment);
247  if (isset($this->num_payment)) $this->num_payment=trim($this->num_payment);
248  if (isset($this->note)) $this->note=trim($this->note);
249  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
250  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
251  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
252 
253  // Check parameters
254  // Put here code to add control on parameters values
255 
256  // Update request
257  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
258  $sql.= " fk_donation=".(isset($this->fk_donation)?$this->fk_donation:"null").",";
259  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
260  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
261  $sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
262  $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
263  $sql.= " fk_typepayment=".(isset($this->fk_typepayment)?$this->fk_typepayment:"null").",";
264  $sql.= " num_payment=".(isset($this->num_payment)?"'".$this->db->escape($this->num_payment)."'":"null").",";
265  $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
266  $sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
267  $sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
268  $sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
269  $sql.= " WHERE rowid=".$this->id;
270 
271  $this->db->begin();
272 
273  dol_syslog(get_class($this)."::update", LOG_DEBUG);
274  $resql = $this->db->query($sql);
275  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
276 
277  if (! $error)
278  {
279  if (! $notrigger)
280  {
281  if (! $error && ! $notrigger)
282  {
283  // Call triggers
284  $result=$this->call_trigger('DONATION_PAYMENT_MODIFY',$user);
285  if ($result < 0) { $error++; }
286  // End call triggers
287  }
288  }
289  }
290 
291  // Commit or rollback
292  if ($error)
293  {
294  foreach($this->errors as $errmsg)
295  {
296  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
297  $this->error.=($this->error?', '.$errmsg:$errmsg);
298  }
299  $this->db->rollback();
300  return -1*$error;
301  }
302  else
303  {
304  $this->db->commit();
305  return 1;
306  }
307  }
308 
309 
317  function delete($user, $notrigger=0)
318  {
319  global $conf, $langs;
320  $error=0;
321 
322  $this->db->begin();
323 
324  if (! $error)
325  {
326  $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
327  $sql.= " WHERE type='payment_donation' AND url_id=".$this->id;
328 
329  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
330  $resql = $this->db->query($sql);
331  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
332  }
333 
334  if (! $error)
335  {
336  $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
337  $sql.= " WHERE rowid=".$this->id;
338 
339  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
340  $resql = $this->db->query($sql);
341  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
342  }
343 
344  if (! $error)
345  {
346  if (! $notrigger)
347  {
348  if (! $error && ! $notrigger)
349  {
350  // Call triggers
351  $result=$this->call_trigger('DONATION_PAYMENT_DELETE',$user);
352  if ($result < 0) { $error++; }
353  // End call triggers
354  }
355  }
356  }
357 
358  // Commit or rollback
359  if ($error)
360  {
361  foreach($this->errors as $errmsg)
362  {
363  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
364  $this->error.=($this->error?', '.$errmsg:$errmsg);
365  }
366  $this->db->rollback();
367  return -1*$error;
368  }
369  else
370  {
371  $this->db->commit();
372  return 1;
373  }
374  }
375 
376 
377 
384  function createFromClone($fromid)
385  {
386  global $user,$langs;
387 
388  $error=0;
389 
390  $object=new PaymentDonation($this->db);
391 
392  $object->context['createfromclone'] = 'createfromclone';
393 
394  $this->db->begin();
395 
396  // Load source object
397  $object->fetch($fromid);
398  $object->id=0;
399  $object->statut=0;
400 
401  // Clear fields
402  // ...
403 
404  // Create clone
405  $result=$object->create($user);
406 
407  // Other options
408  if ($result < 0)
409  {
410  $this->error=$object->error;
411  $error++;
412  }
413 
414  if (! $error)
415  {
416 
417 
418 
419  }
420 
421  unset($this->context['createfromclone']);
422 
423  // End
424  if (! $error)
425  {
426  $this->db->commit();
427  return $object->id;
428  }
429  else
430  {
431  $this->db->rollback();
432  return -1;
433  }
434  }
435 
436 
443  function getLibStatut($mode=0)
444  {
445  return '';
446  }
447 
455  function LibStatut($statut,$mode=0)
456  {
457  global $langs;
458 
459  return '';
460  }
461 
462 
470  function initAsSpecimen()
471  {
472  $this->id=0;
473 
474  $this->fk_donation='';
475  $this->datec='';
476  $this->tms='';
477  $this->datep='';
478  $this->amount='';
479  $this->fk_typepayment='';
480  $this->num_payment='';
481  $this->note='';
482  $this->fk_bank='';
483  $this->fk_user_creat='';
484  $this->fk_user_modif='';
485 
486 
487  }
488 
489 
502  function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
503  {
504  global $conf;
505 
506  $error=0;
507 
508  if (! empty($conf->banque->enabled))
509  {
510  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
511 
512  $acc = new Account($this->db);
513  $acc->fetch($accountid);
514 
516  if ($mode == 'payment_donation') $amount=$total;
517 
518  // Insert payment into llx_bank
519  $bank_line_id = $acc->addline(
520  $this->datepaid,
521  $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
522  $label,
523  $amount,
524  $this->num_payment,
525  '',
526  $user,
527  $emetteur_nom,
528  $emetteur_banque
529  );
530 
531  // Update fk_bank in llx_paiement.
532  // On connait ainsi le paiement qui a genere l'ecriture bancaire
533  if ($bank_line_id > 0)
534  {
535  $result=$this->update_fk_bank($bank_line_id);
536  if ($result <= 0)
537  {
538  $error++;
539  dol_print_error($this->db);
540  }
541 
542  // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
543  $url='';
544  if ($mode == 'payment_donation') $url=DOL_URL_ROOT.'/don/payment/card.php?rowid=';
545  if ($url)
546  {
547  $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
548  if ($result <= 0)
549  {
550  $error++;
551  dol_print_error($this->db);
552  }
553  }
554  }
555  else
556  {
557  $this->error=$acc->error;
558  $error++;
559  }
560  }
561 
562  if (! $error)
563  {
564  return 1;
565  }
566  else
567  {
568  return -1;
569  }
570  }
571 
572 
579  function update_fk_bank($id_bank)
580  {
581  $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
582 
583  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
584  $result = $this->db->query($sql);
585  if ($result)
586  {
587  return 1;
588  }
589  else
590  {
591  $this->error=$this->db->error();
592  return 0;
593  }
594  }
595 
603  function getNomUrl($withpicto=0,$maxlen=0)
604  {
605  global $langs;
606 
607  $result='';
608 
609  if (empty($this->ref)) $this->ref=$this->lib;
610  $label = $langs->trans("ShowPayment").': '.$this->ref;
611 
612  if (!empty($this->id))
613  {
614  $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
615  $linkend='</a>';
616 
617  if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
618  if ($withpicto && $withpicto != 2) $result.=' ';
619  if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$linkend;
620  }
621 
622  return $result;
623  }
624 }
create($user, $notrigger=false)
Create payment of donation into database.
$element
Id that identify managed objects.
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.
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
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'un statut donne.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
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.
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:1013
$table_element
Name of table without prefix where object is stored.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
getLibStatut($mode=0)
Retourne le libelle du statut d'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 '...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)