dolibarr  7.0.0-beta
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 {
34  public $element='paiementcharge';
35  public $table_element='paiementcharge';
36  public $picto = 'payment';
37 
38  var $fk_charge;
39  var $datec='';
40  var $tms='';
41  var $datep='';
46  var $total;
47  var $amount; // Total amount of payment
48  var $amounts=array(); // Array of amounts
49  var $fk_typepaiement;
50  var $num_paiement;
51  var $fk_bank;
52  var $fk_user_creat;
53  var $fk_user_modif;
54 
60  function __construct($db)
61  {
62  $this->db = $db;
63  }
64 
73  function create($user, $closepaidcontrib=0)
74  {
75  global $conf, $langs;
76 
77  $error=0;
78 
79  $now=dol_now();
80 
81  dol_syslog(get_class($this)."::create", LOG_DEBUG);
82 
83  // Validate parametres
84  if (! $this->datepaye)
85  {
86  $this->error='ErrorBadValueForParameterCreatePaymentSocialContrib';
87  return -1;
88  }
89 
90  // Clean parameters
91  if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
92  if (isset($this->amount)) $this->amount=trim($this->amount);
93  if (isset($this->fk_typepaiement)) $this->fk_typepaiement=trim($this->fk_typepaiement);
94  if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
95  if (isset($this->note)) $this->note=trim($this->note);
96  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
97  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
98  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
99 
100  $totalamount = 0;
101  foreach ($this->amounts as $key => $value) // How payment is dispatch
102  {
103  $newvalue = price2num($value,'MT');
104  $this->amounts[$key] = $newvalue;
105  $totalamount += $newvalue;
106  }
107  $totalamount = price2num($totalamount);
108 
109  // Check parameters
110  if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
111 
112 
113  $this->db->begin();
114 
115  if ($totalamount != 0)
116  {
117  $sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,";
118  $sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
119  $sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
120  $sql.= " '".$this->db->idate($this->datepaye)."',";
121  $sql.= " ".$totalamount.",";
122  $sql.= " ".$this->paiementtype.", '".$this->db->escape($this->num_paiement)."', '".$this->db->escape($this->note)."', ".$user->id.",";
123  $sql.= " 0)";
124 
125  $resql=$this->db->query($sql);
126  if ($resql)
127  {
128  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementcharge");
129 
130  // Insere tableau des montants / factures
131  foreach ($this->amounts as $key => $amount)
132  {
133  $contribid = $key;
134  if (is_numeric($amount) && $amount <> 0)
135  {
136  $amount = price2num($amount);
137 
138  // If we want to closed payed invoices
139  if ($closepaidcontrib)
140  {
141 
142  $contrib=new ChargeSociales($this->db);
143  $contrib->fetch($contribid);
144  $paiement = $contrib->getSommePaiement();
145  //$creditnotes=$contrib->getSumCreditNotesUsed();
146  $creditnotes=0;
147  //$deposits=$contrib->getSumDepositsUsed();
148  $deposits=0;
149  $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT');
150  $remaintopay=price2num($contrib->amount - $paiement - $creditnotes - $deposits,'MT');
151  if ($remaintopay == 0)
152  {
153  $result=$contrib->set_paid($user, '', '');
154  }
155  else dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
156  }
157  }
158  }
159  }
160  else
161  {
162  $error++;
163  }
164 
165  }
166 
167  if ($totalamount != 0 && ! $error)
168  {
169  $this->amount=$totalamount;
170  $this->total=$totalamount; // deprecated
171  $this->db->commit();
172  return $this->id;
173  }
174  else
175  {
176  $this->error=$this->db->error();
177  $this->db->rollback();
178  return -1;
179  }
180  }
181 
188  function fetch($id)
189  {
190  global $langs;
191  $sql = "SELECT";
192  $sql.= " t.rowid,";
193  $sql.= " t.fk_charge,";
194  $sql.= " t.datec,";
195  $sql.= " t.tms,";
196  $sql.= " t.datep,";
197  $sql.= " t.amount,";
198  $sql.= " t.fk_typepaiement,";
199  $sql.= " t.num_paiement,";
200  $sql.= " t.note,";
201  $sql.= " t.fk_bank,";
202  $sql.= " t.fk_user_creat,";
203  $sql.= " t.fk_user_modif,";
204  $sql.= " pt.code as type_code, pt.libelle as type_libelle,";
205  $sql.= ' b.fk_account';
206  $sql.= " FROM ".MAIN_DB_PREFIX."paiementcharge as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id AND pt.entity IN (" . getEntity('c_paiement') . ")";
207  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
208  $sql.= " WHERE t.rowid = ".$id;
209  // TODO link on entity of tax;
210 
211  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
212  $resql=$this->db->query($sql);
213  if ($resql)
214  {
215  if ($this->db->num_rows($resql))
216  {
217  $obj = $this->db->fetch_object($resql);
218 
219  $this->id = $obj->rowid;
220  $this->ref = $obj->rowid;
221 
222  $this->fk_charge = $obj->fk_charge;
223  $this->datec = $this->db->jdate($obj->datec);
224  $this->tms = $this->db->jdate($obj->tms);
225  $this->datep = $this->db->jdate($obj->datep);
226  $this->amount = $obj->amount;
227  $this->fk_typepaiement = $obj->fk_typepaiement;
228  $this->num_paiement = $obj->num_paiement;
229  $this->note = $obj->note;
230  $this->fk_bank = $obj->fk_bank;
231  $this->fk_user_creat = $obj->fk_user_creat;
232  $this->fk_user_modif = $obj->fk_user_modif;
233 
234  $this->type_code = $obj->type_code;
235  $this->type_libelle = $obj->type_libelle;
236 
237  $this->bank_account = $obj->fk_account;
238  $this->bank_line = $obj->fk_bank;
239  }
240  $this->db->free($resql);
241 
242  return 1;
243  }
244  else
245  {
246  $this->error="Error ".$this->db->lasterror();
247  return -1;
248  }
249  }
250 
251 
259  function update($user=null, $notrigger=0)
260  {
261  global $conf, $langs;
262  $error=0;
263 
264  // Clean parameters
265 
266  if (isset($this->fk_charge)) $this->fk_charge=trim($this->fk_charge);
267  if (isset($this->amount)) $this->amount=trim($this->amount);
268  if (isset($this->fk_typepaiement)) $this->fk_typepaiement=trim($this->fk_typepaiement);
269  if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
270  if (isset($this->note)) $this->note=trim($this->note);
271  if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
272  if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
273  if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
274 
275 
276 
277  // Check parameters
278  // Put here code to add control on parameters values
279 
280  // Update request
281  $sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET";
282 
283  $sql.= " fk_charge=".(isset($this->fk_charge)?$this->fk_charge:"null").",";
284  $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
285  $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
286  $sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
287  $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
288  $sql.= " fk_typepaiement=".(isset($this->fk_typepaiement)?$this->fk_typepaiement:"null").",";
289  $sql.= " num_paiement=".(isset($this->num_paiement)?"'".$this->db->escape($this->num_paiement)."'":"null").",";
290  $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
291  $sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
292  $sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
293  $sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
294 
295 
296  $sql.= " WHERE rowid=".$this->id;
297 
298  $this->db->begin();
299 
300  dol_syslog(get_class($this)."::update", LOG_DEBUG);
301  $resql = $this->db->query($sql);
302  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
303 
304  if (! $error)
305  {
306  if (! $notrigger)
307  {
308  // Uncomment this and change MYOBJECT to your own tag if you
309  // want this action call a trigger.
310 
312  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
313  //$interface=new Interfaces($this->db);
314  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
315  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
317  }
318  }
319 
320  // Commit or rollback
321  if ($error)
322  {
323  foreach($this->errors as $errmsg)
324  {
325  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
326  $this->error.=($this->error?', '.$errmsg:$errmsg);
327  }
328  $this->db->rollback();
329  return -1*$error;
330  }
331  else
332  {
333  $this->db->commit();
334  return 1;
335  }
336  }
337 
338 
346  function delete($user, $notrigger=0)
347  {
348  global $conf, $langs;
349  $error=0;
350 
351  dol_syslog(get_class($this)."::delete");
352 
353  $this->db->begin();
354 
355  if ($this->bank_line > 0)
356  {
357  $accline = new AccountLine($this->db);
358  $accline->fetch($this->bank_line);
359  $result = $accline->delete();
360  if($result < 0) {
361  $this->errors[] = $accline->error;
362  $error++;
363  }
364  }
365 
366  if (! $error)
367  {
368  $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge";
369  $sql.= " WHERE rowid=".$this->id;
370 
371  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
372  $resql = $this->db->query($sql);
373  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
374  }
375 
376  if (! $error)
377  {
378  if (! $notrigger)
379  {
380  // Uncomment this and change MYOBJECT to your own tag if you
381  // want this action call a trigger.
382 
384  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
385  //$interface=new Interfaces($this->db);
386  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
387  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
389  }
390  }
391 
392  // Commit or rollback
393  if ($error)
394  {
395  foreach($this->errors as $errmsg)
396  {
397  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
398  $this->error.=($this->error?', '.$errmsg:$errmsg);
399  }
400  $this->db->rollback();
401  return -1*$error;
402  }
403  else
404  {
405  $this->db->commit();
406  return 1;
407  }
408  }
409 
410 
411 
418  function createFromClone($fromid)
419  {
420  global $user,$langs;
421 
422  $error=0;
423 
424  $object=new PaymentSocialContribution($this->db);
425 
426  $object->context['createfromclone'] = 'createfromclone';
427 
428  $this->db->begin();
429 
430  // Load source object
431  $object->fetch($fromid);
432  $object->id=0;
433  $object->statut=0;
434 
435  // Clear fields
436  // ...
437 
438  // Create clone
439  $result=$object->create($user);
440 
441  // Other options
442  if ($result < 0)
443  {
444  $this->error=$object->error;
445  $error++;
446  }
447 
448  if (! $error)
449  {
450 
451 
452 
453  }
454 
455  unset($this->context['createfromclone']);
456 
457  // End
458  if (! $error)
459  {
460  $this->db->commit();
461  return $object->id;
462  }
463  else
464  {
465  $this->db->rollback();
466  return -1;
467  }
468  }
469 
470 
478  function initAsSpecimen()
479  {
480  $this->id=0;
481 
482  $this->fk_charge='';
483  $this->datec='';
484  $this->tms='';
485  $this->datep='';
486  $this->amount='';
487  $this->fk_typepaiement='';
488  $this->num_paiement='';
489  $this->note='';
490  $this->fk_bank='';
491  $this->fk_user_creat='';
492  $this->fk_user_modif='';
493 
494 
495  }
496 
497 
510  function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
511  {
512  global $conf;
513 
514  $error=0;
515 
516  if (! empty($conf->banque->enabled))
517  {
518  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
519 
520  $acc = new Account($this->db);
521  $acc->fetch($accountid);
522 
524  if ($mode == 'payment_sc') $total=-$total;
525 
526  // Insert payment into llx_bank
527  $bank_line_id = $acc->addline(
528  $this->datepaye,
529  $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
530  $label,
531  $total,
532  $this->num_paiement,
533  '',
534  $user,
535  $emetteur_nom,
536  $emetteur_banque
537  );
538 
539  // Mise a jour fk_bank dans llx_paiement.
540  // On connait ainsi le paiement qui a genere l'ecriture bancaire
541  if ($bank_line_id > 0)
542  {
543  $result=$this->update_fk_bank($bank_line_id);
544  if ($result <= 0)
545  {
546  $error++;
547  dol_print_error($this->db);
548  }
549 
550  // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
551  $url='';
552  if ($mode == 'payment_sc') $url=DOL_URL_ROOT.'/compta/payment_sc/card.php?id=';
553  if ($url)
554  {
555  $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
556  if ($result <= 0)
557  {
558  $error++;
559  dol_print_error($this->db);
560  }
561  }
562 
563  // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
564  $linkaddedforthirdparty=array();
565  foreach ($this->amounts as $key => $value)
566  {
567  if ($mode == 'payment_sc')
568  {
569  $socialcontrib = new ChargeSociales($this->db);
570  $socialcontrib->fetch($key);
571  $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');
572  if ($result <= 0) dol_print_error($this->db);
573  }
574  }
575  }
576  else
577  {
578  $this->error=$acc->error;
579  $error++;
580  }
581  }
582 
583  if (! $error)
584  {
585  return 1;
586  }
587  else
588  {
589  return -1;
590  }
591  }
592 
593 
600  function update_fk_bank($id_bank)
601  {
602  $sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
603 
604  dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
605  $result = $this->db->query($sql);
606  if ($result)
607  {
608  return 1;
609  }
610  else
611  {
612  $this->error=$this->db->error();
613  return 0;
614  }
615  }
616 
617 
624  function getLibStatut($mode=0)
625  {
626  return $this->LibStatut($this->statut,$mode);
627  }
628 
636  function LibStatut($status,$mode=0)
637  {
638  global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
639 
640  $langs->load('compta');
641  /*if ($mode == 0)
642  {
643  if ($status == 0) return $langs->trans('ToValidate');
644  if ($status == 1) return $langs->trans('Validated');
645  }
646  if ($mode == 1)
647  {
648  if ($status == 0) return $langs->trans('ToValidate');
649  if ($status == 1) return $langs->trans('Validated');
650  }
651  if ($mode == 2)
652  {
653  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
654  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
655  }
656  if ($mode == 3)
657  {
658  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
659  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
660  }
661  if ($mode == 4)
662  {
663  if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
664  if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
665  }
666  if ($mode == 5)
667  {
668  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
669  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
670  }
671  if ($mode == 6)
672  {
673  if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
674  if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
675  }*/
676  return '';
677  }
678 
686  function getNomUrl($withpicto=0,$maxlen=0)
687  {
688  global $langs;
689 
690  $result='';
691 
692  if (empty($this->ref)) $this->ref=$this->lib;
693  $label = $langs->trans("ShowPayment").': '.$this->ref;
694 
695  if (!empty($this->id))
696  {
697  $link = '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
698  $linkend='</a>';
699 
700  if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
701  if ($withpicto && $withpicto != 2) $result.=' ';
702  if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$linkend;
703  }
704 
705  return $result;
706  }
707 }
708 
709 
getLibStatut($mode=0)
Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee) ...
$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.
$table_element
Name of table without prefix where object is stored.
fetch($id)
Load object in memory from database.
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.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
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.
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
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'un statut donne.
Classe permettant la gestion des paiements des charges La tva collectee n'est calculee que sur les fa...
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, ...)
create($user, $closepaidcontrib=0)
Create payment of social contribution into database.