dolibarr  9.0.0
paymentsocialcontribution.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
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 <http://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
27 
28 
33 {
37  public $element='paiementcharge';
38 
42  public $table_element='paiementcharge';
43 
47  public $picto = 'payment';
48 
52  public $fk_charge;
53 
54  public $datec='';
55  public $tms='';
56  public $datep='';
57 
62  public $total;
63 
64  public $amount; // Total amount of payment
65  public $amounts=array(); // Array of amounts
66 
70  public $fk_typepaiement;
71 
72  public $num_paiement;
73 
77  public $fk_bank;
78 
82  public $fk_user_creat;
83 
87  public $fk_user_modif;
88 
94  function __construct($db)
95  {
96  $this->db = $db;
97  }
98 
107  function create($user, $closepaidcontrib=0)
108  {
109  global $conf, $langs;
110 
111  $error=0;
112 
113  $now=dol_now();
114 
115  dol_syslog(get_class($this)."::create", LOG_DEBUG);
116 
117  // Validate parametres
118  if (! $this->datepaye)
119  {
120  $this->error='ErrorBadValueForParameterCreatePaymentSocialContrib';
121  return -1;
122  }
123 
124  // Clean parameters
125  if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
126  if (isset($this->amount)) $this->amount=trim($this->amount);
127  if (isset($this->fk_typepaiement)) $this->fk_typepaiement=trim($this->fk_typepaiement);
128  if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
129  if (isset($this->note)) $this->note=trim($this->note);
130  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
131  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
132  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
133 
134  $totalamount = 0;
135  foreach ($this->amounts as $key => $value) // How payment is dispatch
136  {
137  $newvalue = price2num($value,'MT');
138  $this->amounts[$key] = $newvalue;
139  $totalamount += $newvalue;
140  }
141  $totalamount = price2num($totalamount);
142 
143  // Check parameters
144  if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
145 
146 
147  $this->db->begin();
148 
149  if ($totalamount != 0)
150  {
151  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,";
152  $sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
153  $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
154  $sql.= " '".$this->db->idate($this->datepaye)."',";
155  $sql.= " ".$totalamount.",";
156  $sql.= " ".$this->paiementtype.", '".$this->db->escape($this->num_paiement)."', '".$this->db->escape($this->note)."', ".$user->id.",";
157  $sql.= " 0)";
158 
159  $resql=$this->db->query($sql);
160  if ($resql)
161  {
162  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementcharge");
163 
164  // Insere tableau des montants / factures
165  foreach ($this->amounts as $key => $amount)
166  {
167  $contribid = $key;
168  if (is_numeric($amount) && $amount <> 0)
169  {
170  $amount = price2num($amount);
171 
172  // If we want to closed payed invoices
173  if ($closepaidcontrib)
174  {
175 
176  $contrib=new ChargeSociales($this->db);
177  $contrib->fetch($contribid);
178  $paiement = $contrib->getSommePaiement();
179  //$creditnotes=$contrib->getSumCreditNotesUsed();
180  $creditnotes=0;
181  //$deposits=$contrib->getSumDepositsUsed();
182  $deposits=0;
183  $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
184  $remaintopay=price2num($contrib->amount - $paiement - $creditnotes - $deposits,'MT');
185  if ($remaintopay == 0)
186  {
187  $result=$contrib->set_paid($user, '', '');
188  }
189  else dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
190  }
191  }
192  }
193  }
194  else
195  {
196  $error++;
197  }
198  }
199 
200  $result = $this->call_trigger('PAYMENTSOCIALCONTRIBUTION_CREATE',$user);
201  if($result < 0) $error++;
202 
203  if ($totalamount != 0 && ! $error)
204  {
205  $this->amount=$totalamount;
206  $this->total=$totalamount; // deprecated
207  $this->db->commit();
208  return $this->id;
209  }
210  else
211  {
212  $this->error=$this->db->error();
213  $this->db->rollback();
214  return -1;
215  }
216  }
217 
224  function fetch($id)
225  {
226  global $langs;
227  $sql = "SELECT";
228  $sql.= " t.rowid,";
229  $sql.= " t.fk_charge,";
230  $sql.= " t.datec,";
231  $sql.= " t.tms,";
232  $sql.= " t.datep,";
233  $sql.= " t.amount,";
234  $sql.= " t.fk_typepaiement,";
235  $sql.= " t.num_paiement,";
236  $sql.= " t.note,";
237  $sql.= " t.fk_bank,";
238  $sql.= " t.fk_user_creat,";
239  $sql.= " t.fk_user_modif,";
240  $sql.= " pt.code as type_code, pt.libelle as type_libelle,";
241  $sql.= ' b.fk_account';
242  $sql.= " FROM ".MAIN_DB_PREFIX."paiementcharge as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id";
243  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
244  $sql.= " WHERE t.rowid = ".$id;
245  // TODO link on entity of tax;
246 
247  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
248  $resql=$this->db->query($sql);
249  if ($resql)
250  {
251  if ($this->db->num_rows($resql))
252  {
253  $obj = $this->db->fetch_object($resql);
254 
255  $this->id = $obj->rowid;
256  $this->ref = $obj->rowid;
257 
258  $this->fk_charge = $obj->fk_charge;
259  $this->datec = $this->db->jdate($obj->datec);
260  $this->tms = $this->db->jdate($obj->tms);
261  $this->datep = $this->db->jdate($obj->datep);
262  $this->amount = $obj->amount;
263  $this->fk_typepaiement = $obj->fk_typepaiement;
264  $this->num_paiement = $obj->num_paiement;
265  $this->note = $obj->note;
266  $this->fk_bank = $obj->fk_bank;
267  $this->fk_user_creat = $obj->fk_user_creat;
268  $this->fk_user_modif = $obj->fk_user_modif;
269 
270  $this->type_code = $obj->type_code;
271  $this->type_libelle = $obj->type_libelle;
272 
273  $this->bank_account = $obj->fk_account;
274  $this->bank_line = $obj->fk_bank;
275  }
276  $this->db->free($resql);
277 
278  return 1;
279  }
280  else
281  {
282  $this->error="Error ".$this->db->lasterror();
283  return -1;
284  }
285  }
286 
287 
295  function update($user=null, $notrigger=0)
296  {
297  global $conf, $langs;
298  $error=0;
299 
300  // Clean parameters
301 
302  if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
303  if (isset($this->amount)) $this->amount=trim($this->amount);
304  if (isset($this->fk_typepaiement)) $this->fk_typepaiement=trim($this->fk_typepaiement);
305  if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
306  if (isset($this->note)) $this->note=trim($this->note);
307  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
308  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
309  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
310 
311 
312 
313  // Check parameters
314  // Put here code to add control on parameters values
315 
316  // Update request
317  $sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET";
318 
319  $sql.= " fk_charge=".(isset($this->fk_charge)?$this->fk_charge:"null").",";
320  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
321  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
322  $sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
323  $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
324  $sql.= " fk_typepaiement=".(isset($this->fk_typepaiement)?$this->fk_typepaiement:"null").",";
325  $sql.= " num_paiement=".(isset($this->num_paiement)?"'".$this->db->escape($this->num_paiement)."'":"null").",";
326  $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
327  $sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
328  $sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
329  $sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
330 
331 
332  $sql.= " WHERE rowid=".$this->id;
333 
334  $this->db->begin();
335 
336  dol_syslog(get_class($this)."::update", LOG_DEBUG);
337  $resql = $this->db->query($sql);
338  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
339 
340  if (! $error)
341  {
342  if (! $notrigger)
343  {
344  // Uncomment this and change MYOBJECT to your own tag if you
345  // want this action call a trigger.
346 
348  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
349  //$interface=new Interfaces($this->db);
350  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
351  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
353  }
354  }
355 
356  // Commit or rollback
357  if ($error)
358  {
359  foreach($this->errors as $errmsg)
360  {
361  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
362  $this->error.=($this->error?', '.$errmsg:$errmsg);
363  }
364  $this->db->rollback();
365  return -1*$error;
366  }
367  else
368  {
369  $this->db->commit();
370  return 1;
371  }
372  }
373 
374 
382  function delete($user, $notrigger=0)
383  {
384  global $conf, $langs;
385  $error=0;
386 
387  dol_syslog(get_class($this)."::delete");
388 
389  $this->db->begin();
390 
391  if ($this->bank_line > 0)
392  {
393  $accline = new AccountLine($this->db);
394  $accline->fetch($this->bank_line);
395  $result = $accline->delete();
396  if($result < 0) {
397  $this->errors[] = $accline->error;
398  $error++;
399  }
400  }
401 
402  if (! $error)
403  {
404  $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge";
405  $sql.= " WHERE rowid=".$this->id;
406 
407  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
408  $resql = $this->db->query($sql);
409  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
410  }
411 
412  if (! $error)
413  {
414  if (! $notrigger)
415  {
416  // Uncomment this and change MYOBJECT to your own tag if you
417  // want this action call a trigger.
418 
420  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
421  //$interface=new Interfaces($this->db);
422  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
423  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
425  }
426  }
427 
428  // Commit or rollback
429  if ($error)
430  {
431  foreach($this->errors as $errmsg)
432  {
433  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
434  $this->error.=($this->error?', '.$errmsg:$errmsg);
435  }
436  $this->db->rollback();
437  return -1*$error;
438  }
439  else
440  {
441  $this->db->commit();
442  return 1;
443  }
444  }
445 
446 
447 
454  function createFromClone($fromid)
455  {
456  global $user,$langs;
457 
458  $error=0;
459 
460  $object=new PaymentSocialContribution($this->db);
461 
462  $this->db->begin();
463 
464  // Load source object
465  $object->fetch($fromid);
466  $object->id=0;
467  $object->statut=0;
468 
469  // Clear fields
470  // ...
471 
472  // Create clone
473  $object->context['createfromclone'] = 'createfromclone';
474  $result=$object->create($user);
475 
476  // Other options
477  if ($result < 0)
478  {
479  $this->error=$object->error;
480  $error++;
481  }
482 
483  if (! $error)
484  {
485 
486 
487 
488  }
489 
490  unset($object->context['createfromclone']);
491 
492  // End
493  if (! $error)
494  {
495  $this->db->commit();
496  return $object->id;
497  }
498  else
499  {
500  $this->db->rollback();
501  return -1;
502  }
503  }
504 
505 
513  function initAsSpecimen()
514  {
515  $this->id=0;
516 
517  $this->fk_charge='';
518  $this->datec='';
519  $this->tms='';
520  $this->datep='';
521  $this->amount='';
522  $this->fk_typepaiement='';
523  $this->num_paiement='';
524  $this->note='';
525  $this->fk_bank='';
526  $this->fk_user_creat='';
527  $this->fk_user_modif='';
528  }
529 
530 
543  function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
544  {
545  global $conf;
546 
547  $error=0;
548 
549  if (! empty($conf->banque->enabled))
550  {
551  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
552 
553  $acc = new Account($this->db);
554  $acc->fetch($accountid);
555 
557  if ($mode == 'payment_sc') $total=-$total;
558 
559  // Insert payment into llx_bank
560  $bank_line_id = $acc->addline(
561  $this->datepaye,
562  $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
563  $label,
564  $total,
565  $this->num_paiement,
566  '',
567  $user,
568  $emetteur_nom,
569  $emetteur_banque
570  );
571 
572  // Mise a jour fk_bank dans llx_paiement.
573  // On connait ainsi le paiement qui a genere l'ecriture bancaire
574  if ($bank_line_id > 0)
575  {
576  $result=$this->update_fk_bank($bank_line_id);
577  if ($result <= 0)
578  {
579  $error++;
580  dol_print_error($this->db);
581  }
582 
583  // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
584  $url='';
585  if ($mode == 'payment_sc') $url=DOL_URL_ROOT.'/compta/payment_sc/card.php?id=';
586  if ($url)
587  {
588  $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
589  if ($result <= 0)
590  {
591  $error++;
592  dol_print_error($this->db);
593  }
594  }
595 
596  // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
597  $linkaddedforthirdparty=array();
598  foreach ($this->amounts as $key => $value)
599  {
600  if ($mode == 'payment_sc')
601  {
602  $socialcontrib = new ChargeSociales($this->db);
603  $socialcontrib->fetch($key);
604  $result=$acc->add_url_line($bank_line_id, $socialcontrib->id, DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''),'sc');
605  if ($result <= 0) dol_print_error($this->db);
606  }
607  }
608  }
609  else
610  {
611  $this->error=$acc->error;
612  $error++;
613  }
614  }
615 
616  if (! $error)
617  {
618  return 1;
619  }
620  else
621  {
622  return -1;
623  }
624  }
625 
626 
627  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
634  function update_fk_bank($id_bank)
635  {
636  // phpcs:enable
637  $sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
638 
639  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
640  $result = $this->db->query($sql);
641  if ($result)
642  {
643  return 1;
644  }
645  else
646  {
647  $this->error=$this->db->error();
648  return 0;
649  }
650  }
651 
652 
659  function getLibStatut($mode=0)
660  {
661  return $this->LibStatut($this->statut,$mode);
662  }
663 
664  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
672  function LibStatut($status,$mode=0)
673  {
674  // phpcs:enable
675  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
676 
677  $langs->load('compta');
678  /*if ($mode == 0)
679  {
680  if ($status == 0) return $langs->trans('ToValidate');
681  if ($status == 1) return $langs->trans('Validated');
682  }
683  if ($mode == 1)
684  {
685  if ($status == 0) return $langs->trans('ToValidate');
686  if ($status == 1) return $langs->trans('Validated');
687  }
688  if ($mode == 2)
689  {
690  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
691  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
692  }
693  if ($mode == 3)
694  {
695  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
696  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
697  }
698  if ($mode == 4)
699  {
700  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
701  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
702  }
703  if ($mode == 5)
704  {
705  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
706  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
707  }
708  if ($mode == 6)
709  {
710  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
711  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
712  }*/
713  return '';
714  }
715 
723  function getNomUrl($withpicto=0,$maxlen=0)
724  {
725  global $langs;
726 
727  $result='';
728 
729  if (empty($this->ref)) $this->ref=$this->lib;
730  $label = $langs->trans("ShowPayment").': '.$this->ref;
731 
732  if (!empty($this->id))
733  {
734  $link = '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
735  $linkend='</a>';
736 
737  if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
738  if ($withpicto && $withpicto != 2) $result.=' ';
739  if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$linkend;
740  }
741 
742  return $result;
743  }
744 }
getLibStatut($mode=0)
Retourne le libelle du statut d&#39;une facture (brouillon, validee, abandonnee, payee) ...
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
fetch($id)
Load object in memory from database.
if(! empty($search_group)) natural_search(array("g.nom" g note
Definition: list.php:123
Class to manage payments of social contributions.
Class to manage bank transaction lines.
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=null, $notrigger=0)
Update database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
initAsSpecimen()
Initialise an instance with random values.
createFromClone($fromid)
Load an object from its id and create a new one in database.
dol_now($mode='gmt')
Return date for now.
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
update_fk_bank($id_bank)
Mise a jour du lien entre le paiement de charge et la ligne dans llx_bank generee.
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...
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un statut donne.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
Classe permettant la gestion des paiements des charges La tva collectee n&#39;est calculee que sur les fa...
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, ...)
create($user, $closepaidcontrib=0)
Create payment of social contribution into database.