dolibarr  9.0.0
chargesociales.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  * Copyright (C) 2016 Frédéric France <frederic.france@free.fr>
5  * Copyright (C) 2017 Alexandre Spangaro <aspangaro@zendsi.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 
28 
34 {
38  public $element='chargesociales';
39 
40  public $table='chargesociales';
41 
45  public $table_element='chargesociales';
46 
50  public $picto = 'bill';
51 
55  protected $table_ref_field = 'ref';
56 
57  public $date_ech;
58  public $lib;
59  public $type;
60  public $type_libelle;
61  public $amount;
62  public $paye;
63  public $periode;
64  public $date_creation;
65  public $date_modification;
66  public $date_validation;
67 
71  public $fk_account;
72 
76  public $fk_project;
77 
78 
84  function __construct($db)
85  {
86  $this->db = $db;
87  }
88 
96  function fetch($id, $ref='')
97  {
98  $sql = "SELECT cs.rowid, cs.date_ech";
99  $sql.= ", cs.libelle as lib, cs.fk_type, cs.amount, cs.fk_projet as fk_project, cs.paye, cs.periode, cs.import_key";
100  $sql.= ", cs.fk_account, cs.fk_mode_reglement";
101  $sql.= ", c.libelle";
102  $sql.= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
103  $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
104  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as c ON cs.fk_type = c.id";
105  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON cs.fk_mode_reglement = p.id';
106  $sql.= ' WHERE cs.entity IN ('.getEntity('tax').')';
107  if ($ref) $sql.= " AND cs.rowid = ".$ref;
108  else $sql.= " AND cs.rowid = ".$id;
109 
110  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
111  $resql=$this->db->query($sql);
112  if ($resql)
113  {
114  if ($this->db->num_rows($resql))
115  {
116  $obj = $this->db->fetch_object($resql);
117 
118  $this->id = $obj->rowid;
119  $this->ref = $obj->rowid;
120  $this->date_ech = $this->db->jdate($obj->date_ech);
121  $this->lib = $obj->lib;
122  $this->type = $obj->fk_type;
123  $this->type_libelle = $obj->libelle;
124  $this->fk_account = $obj->fk_account;
125  $this->mode_reglement_id = $obj->fk_mode_reglement;
126  $this->mode_reglement_code = $obj->mode_reglement_code;
127  $this->mode_reglement = $obj->mode_reglement_libelle;
128  $this->amount = $obj->amount;
129  $this->fk_project = $obj->fk_project;
130  $this->paye = $obj->paye;
131  $this->periode = $this->db->jdate($obj->periode);
132  $this->import_key = $this->import_key;
133 
134  $this->db->free($resql);
135 
136  return 1;
137  }
138  else
139  {
140  return 0;
141  }
142  }
143  else
144  {
145  $this->error=$this->db->lasterror();
146  return -1;
147  }
148  }
149 
155  function check()
156  {
157  $newamount=price2num($this->amount,'MT');
158 
159  // Validation parametres
160  if (! $newamount > 0 || empty($this->date_ech) || empty($this->periode))
161  {
162  return false;
163  }
164 
165 
166  return true;
167  }
168 
175  function create($user)
176  {
177  global $conf;
178  $error=0;
179 
180  $now=dol_now();
181 
182  // Nettoyage parametres
183  $newamount=price2num($this->amount,'MT');
184 
185  if (!$this->check())
186  {
187  $this->error="ErrorBadParameter";
188  return -2;
189  }
190 
191  $this->db->begin();
192 
193  $sql = "INSERT INTO ".MAIN_DB_PREFIX."chargesociales (fk_type, fk_account, fk_mode_reglement, libelle, date_ech, periode, amount, fk_projet, entity, fk_user_author, date_creation)";
194  $sql.= " VALUES (".$this->type;
195  $sql.= ", ".($this->fk_account>0 ? $this->fk_account:'NULL');
196  $sql.= ", ".($this->mode_reglement_id>0 ? $this->mode_reglement_id:"NULL");
197  $sql.= ", '".$this->db->escape($this->lib)."'";
198  $sql.= ", '".$this->db->idate($this->date_ech)."'";
199  $sql.= ", '".$this->db->idate($this->periode)."'";
200  $sql.= ", '".price2num($newamount)."'";
201  $sql.= ", ".($this->fk_project>0?$this->fk_project:'NULL');
202  $sql.= ", ".$conf->entity;
203  $sql.= ", ".$user->id;
204  $sql.= ", '".$this->db->idate($now)."'";
205  $sql.= ")";
206 
207  dol_syslog(get_class($this)."::create", LOG_DEBUG);
208  $resql=$this->db->query($sql);
209  if ($resql)
210  {
211  $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."chargesociales");
212 
213  //dol_syslog("ChargesSociales::create this->id=".$this->id);
214  $result=$this->call_trigger('SOCIALCONTRIBUTION_CREATE',$user);
215  if ($result < 0) $error++;
216 
217  if(empty($error)) {
218  $this->db->commit();
219  return $this->id;
220  }
221  else {
222  $this->db->rollback();
223  return -1*$error;
224  }
225  }
226  else
227  {
228  $this->error=$this->db->error();
229  $this->db->rollback();
230  return -1;
231  }
232  }
233 
234 
241  function delete($user)
242  {
243  $error=0;
244 
245  $this->db->begin();
246 
247  // Get bank transaction lines for this social contributions
248  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
249  $account=new Account($this->db);
250  $lines_url=$account->get_url('',$this->id,'sc');
251 
252  // Delete bank urls
253  foreach ($lines_url as $line_url)
254  {
255  if (! $error)
256  {
257  $accountline=new AccountLine($this->db);
258  $accountline->fetch($line_url['fk_bank']);
259  $result=$accountline->delete_urls($user);
260  if ($result < 0)
261  {
262  $error++;
263  }
264  }
265  }
266 
267  // Delete payments
268  if (! $error)
269  {
270  $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge WHERE fk_charge=".$this->id;
271  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
272  $resql=$this->db->query($sql);
273  if (! $resql)
274  {
275  $error++;
276  $this->error=$this->db->lasterror();
277  }
278  }
279 
280  if (! $error)
281  {
282  $sql = "DELETE FROM ".MAIN_DB_PREFIX."chargesociales WHERE rowid=".$this->id;
283  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
284  $resql=$this->db->query($sql);
285  if (! $resql)
286  {
287  $error++;
288  $this->error=$this->db->lasterror();
289  }
290  }
291 
292  if (! $error)
293  {
294  $this->db->commit();
295  return 1;
296  }
297  else
298  {
299  $this->db->rollback();
300  return -1;
301  }
302  }
303 
304 
312  function update($user, $notrigger=0)
313  {
314  $error=0;
315  $this->db->begin();
316 
317  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
318  $sql.= " SET libelle='".$this->db->escape($this->lib)."'";
319  $sql.= ", date_ech='".$this->db->idate($this->date_ech)."'";
320  $sql.= ", periode='".$this->db->idate($this->periode)."'";
321  $sql.= ", amount='".price2num($this->amount,'MT')."'";
322  $sql.= ", fk_projet='".$this->db->escape($this->fk_project)."'";
323  $sql.= ", fk_user_modif=".$user->id;
324  $sql.= " WHERE rowid=".$this->id;
325 
326  dol_syslog(get_class($this)."::update", LOG_DEBUG);
327  $resql=$this->db->query($sql);
328 
329  if (! $resql) {
330  $error++; $this->errors[]="Error ".$this->db->lasterror();
331  }
332 
333  if (! $error)
334  {
335  if (! $notrigger)
336  {
337  // Call trigger
338  $result=$this->call_trigger('SOCIALCHARGES_MODIFY',$user);
339  if ($result < 0) $error++;
340  // End call triggers
341  }
342  }
343 
344  // Commit or rollback
345  if ($error)
346  {
347  foreach($this->errors as $errmsg)
348  {
349  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
350  $this->error.=($this->error?', '.$errmsg:$errmsg);
351  }
352  $this->db->rollback();
353  return -1*$error;
354  }
355  else
356  {
357  $this->db->commit();
358  return 1;
359  }
360  }
361 
368  function solde($year = 0)
369  {
370  global $conf;
371 
372  $sql = "SELECT SUM(f.amount) as amount";
373  $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as f";
374  $sql.= " WHERE f.entity = ".$conf->entity;
375  $sql.= " AND paye = 0";
376 
377  if ($year) {
378  $sql .= " AND f.datev >= '$y-01-01' AND f.datev <= '$y-12-31' ";
379  }
380 
381  $result = $this->db->query($sql);
382  if ($result)
383  {
384  if ($this->db->num_rows($result))
385  {
386  $obj = $this->db->fetch_object($result);
387  $this->db->free($result);
388  return $obj->amount;
389  }
390  else
391  {
392  return 0;
393  }
394  }
395  else
396  {
397  print $this->db->error();
398  return -1;
399  }
400  }
401 
402  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
409  function set_paid($user)
410  {
411  // phpcs:enable
412  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
413  $sql.= " paye = 1";
414  $sql.= " WHERE rowid = ".$this->id;
415  $return = $this->db->query($sql);
416  if ($return) return 1;
417  else return -1;
418  }
419 
420  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
427  function set_unpaid($user)
428  {
429  // phpcs:enable
430  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
431  $sql.= " paye = 0";
432  $sql.= " WHERE rowid = ".$this->id;
433  $return = $this->db->query($sql);
434  if ($return) return 1;
435  else return -1;
436  }
437 
445  function getLibStatut($mode=0,$alreadypaid=-1)
446  {
447  return $this->LibStatut($this->paye,$mode,$alreadypaid);
448  }
449 
450  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
459  function LibStatut($statut,$mode=0,$alreadypaid=-1)
460  {
461  // phpcs:enable
462  global $langs;
463 
464  // Load translation files required by the page
465  $langs->loadLangs(array("customers","bills"));
466 
467  if ($mode == 0 || $mode == 1)
468  {
469  if ($statut == 0) return $langs->trans("Unpaid");
470  if ($statut == 1) return $langs->trans("Paid");
471  }
472  elseif ($mode == 2)
473  {
474  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
475  if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
476  if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
477  }
478  elseif ($mode == 3)
479  {
480  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1');
481  if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3');
482  if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6');
483  }
484  elseif ($mode == 4)
485  {
486  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
487  if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
488  if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
489  }
490  elseif ($mode == 5)
491  {
492  if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
493  if ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
494  if ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
495  }
496  elseif ($mode == 6)
497  {
498  if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
499  if ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
500  if ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
501  }
502 
503  else return "Error, mode/status not found";
504  }
505 
506 
517  function getNomUrl($withpicto=0, $maxlen=0, $notooltip=0, $short=0, $save_lastsearch_value=-1)
518  {
519  global $langs, $conf, $user, $form;
520 
521  if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
522 
523  $result='';
524 
525  $url = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$this->id;
526 
527  if ($short) return $url;
528 
529  if ($option !== 'nolink')
530  {
531  // Add param to save lastsearch_values or not
532  $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
533  if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
534  if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
535  }
536 
537 
538  if (empty($this->ref)) $this->ref=$this->lib;
539 
540  $label = '<u>'.$langs->trans("ShowSocialContribution").'</u>';
541  if (! empty($this->ref))
542  $label .= '<br><b>'.$langs->trans('Ref') . ':</b> ' . $this->ref;
543  if (! empty($this->lib))
544  $label .= '<br><b>'.$langs->trans('Label') . ':</b> ' . $this->lib;
545  if (! empty($this->type_libelle))
546  $label .= '<br><b>'.$langs->trans('Type') . ':</b> ' . $this->type_libelle;
547 
548  $linkclose='';
549  if (empty($notooltip) && $user->rights->facture->lire)
550  {
551  if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
552  {
553  $label=$langs->trans("ShowSocialContribution");
554  $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
555  }
556  $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"';
557  $linkclose.=' class="classfortooltip"';
558  }
559 
560  $linkstart='<a href="'.$url.'"';
561  $linkstart.=$linkclose.'>';
562  $linkend='</a>';
563 
564  $result .= $linkstart;
565  if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
566  if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->ref,$maxlen):$this->ref);
567  $result .= $linkend;
568 
569  return $result;
570  }
571 
577  function getSommePaiement()
578  {
579  $table='paiementcharge';
580  $field='fk_charge';
581 
582  $sql = 'SELECT sum(amount) as amount';
583  $sql.= ' FROM '.MAIN_DB_PREFIX.$table;
584  $sql.= ' WHERE '.$field.' = '.$this->id;
585 
586  dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
587  $resql=$this->db->query($sql);
588  if ($resql)
589  {
590  $amount=0;
591 
592  $obj = $this->db->fetch_object($resql);
593  if ($obj) $amount=$obj->amount?$obj->amount:0;
594 
595  $this->db->free($resql);
596  return $amount;
597  }
598  else
599  {
600  return -1;
601  }
602  }
603 
610  function info($id)
611  {
612  $sql = "SELECT e.rowid, e.tms as datem, e.date_creation as datec, e.date_valid as datev, e.import_key,";
613  $sql.= " e.fk_user_author, e.fk_user_modif, e.fk_user_valid";
614  $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as e";
615  $sql.= " WHERE e.rowid = ".$id;
616 
617  dol_syslog(get_class($this)."::info", LOG_DEBUG);
618  $result=$this->db->query($sql);
619  if ($result)
620  {
621  if ($this->db->num_rows($result))
622  {
623  $obj = $this->db->fetch_object($result);
624 
625  $this->id = $obj->rowid;
626 
627  if ($obj->fk_user_author) {
628  $cuser = new User($this->db);
629  $cuser->fetch($obj->fk_user_author);
630  $this->user_creation = $cuser;
631  }
632 
633  if ($obj->fk_user_modif) {
634  $muser = new User($this->db);
635  $muser->fetch($obj->fk_user_modif);
636  $this->user_modification = $muser;
637  }
638 
639  if ($obj->fk_user_valid) {
640  $vuser = new User($this->db);
641  $vuser->fetch($obj->fk_user_valid);
642  $this->user_validation = $vuser;
643  }
644 
645  $this->date_creation = $this->db->jdate($obj->datec);
646  $this->date_modification = $this->db->jdate($obj->datem);
647  $this->date_validation = $this->db->jdate($obj->datev);
648  $this->import_key = $obj->import_key;
649  }
650 
651  $this->db->free($result);
652  }
653  else
654  {
655  dol_print_error($this->db);
656  }
657  }
658 
666  function initAsSpecimen()
667  {
668  // Initialize parameters
669  $this->id=0;
670  $this->ref = 'SPECIMEN';
671  $this->specimen=1;
672  $this->paye = 0;
673  $this->date = time();
674  $this->date_ech=$this->date+3600*24*30;
675  $this->periode=$this->date+3600*24*30;
676  $this->amount=100;
677  $this->lib = 0;
678  $this->type = 1;
679  $this->type_libelle = 'Social contribution label';
680  }
681 }
print
Draft customers invoices.
Definition: index.php:91
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
check()
Check if a social contribution can be created into database.
getSommePaiement()
Return amount of payments already done.
create($user)
Create a social contribution into database.
LibStatut($statut, $mode=0, $alreadypaid=-1)
Renvoi le libelle d&#39;un statut donne.
Class to manage Dolibarr users.
Definition: user.class.php:41
set_paid($user)
Tag social contribution as payed completely.
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...
solde($year=0)
Calculate amount remaining to pay by year.
Class to manage bank accounts.
info($id)
Charge les informations d&#39;ordre info dans l&#39;objet entrepot.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
initAsSpecimen()
Initialise an instance with random values.
getLibStatut($mode=0, $alreadypaid=-1)
Retourne le libelle du statut d&#39;une charge (impaye, payee)
set_unpaid($user)
Remove tag payed on social contribution.
dol_now($mode='gmt')
Return date for now.
getNomUrl($withpicto=0, $maxlen=0, $notooltip=0, $short=0, $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
fetch($id, $ref='')
Retrouve et charge une charge sociale.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
update($user, $notrigger=0)
Update social or fiscal contribution.
__construct($db)
Constructor.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
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, ...)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)