dolibarr  7.0.0-beta
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 {
35  public $element='chargesociales';
36  public $table='chargesociales';
37  public $table_element='chargesociales';
38  public $picto = 'bill';
39 
43  protected $table_ref_field = 'ref';
44 
45  var $date_ech;
46  var $lib;
47  var $type;
48  var $type_libelle;
49  var $amount;
50  var $paye;
51  var $periode;
52  var $date_creation;
53  var $date_modification;
54  var $date_validation;
55  var $fk_account;
56  var $fk_project;
57 
58 
64  function __construct($db)
65  {
66  $this->db = $db;
67  return 1;
68  }
69 
77  function fetch($id, $ref='')
78  {
79  $sql = "SELECT cs.rowid, cs.date_ech";
80  $sql.= ", cs.libelle as lib, cs.fk_type, cs.amount, cs.fk_projet as fk_project, cs.paye, cs.periode, cs.import_key";
81  $sql.= ", cs.fk_account, cs.fk_mode_reglement";
82  $sql.= ", c.libelle";
83  $sql.= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
84  $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
85  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as c ON cs.fk_type = c.id";
86  $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON cs.fk_mode_reglement = p.id AND p.entity IN ('.getEntity('c_paiement').')';
87  $sql.= ' WHERE cs.entity IN ('.getEntity('tax').')';
88  if ($ref) $sql.= " AND cs.rowid = ".$ref;
89  else $sql.= " AND cs.rowid = ".$id;
90 
91  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
92  $resql=$this->db->query($sql);
93  if ($resql)
94  {
95  if ($this->db->num_rows($resql))
96  {
97  $obj = $this->db->fetch_object($resql);
98 
99  $this->id = $obj->rowid;
100  $this->ref = $obj->rowid;
101  $this->date_ech = $this->db->jdate($obj->date_ech);
102  $this->lib = $obj->lib;
103  $this->type = $obj->fk_type;
104  $this->type_libelle = $obj->libelle;
105  $this->fk_account = $obj->fk_account;
106  $this->mode_reglement_id = $obj->fk_mode_reglement;
107  $this->mode_reglement_code = $obj->mode_reglement_code;
108  $this->mode_reglement = $obj->mode_reglement_libelle;
109  $this->amount = $obj->amount;
110  $this->fk_project = $obj->fk_project;
111  $this->paye = $obj->paye;
112  $this->periode = $this->db->jdate($obj->periode);
113  $this->import_key = $this->import_key;
114 
115  $this->db->free($resql);
116 
117  return 1;
118  }
119  else
120  {
121  return 0;
122  }
123  }
124  else
125  {
126  $this->error=$this->db->lasterror();
127  return -1;
128  }
129  }
130 
136  function check()
137  {
138  $newamount=price2num($this->amount,'MT');
139 
140  // Validation parametres
141  if (! $newamount > 0 || empty($this->date_ech) || empty($this->periode))
142  {
143  return false;
144  }
145 
146 
147  return true;
148  }
149 
156  function create($user)
157  {
158  global $conf;
159 
160  $now=dol_now();
161 
162  // Nettoyage parametres
163  $newamount=price2num($this->amount,'MT');
164 
165  if (!$this->check())
166  {
167  $this->error="ErrorBadParameter";
168  return -2;
169  }
170 
171  $this->db->begin();
172 
173  $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)";
174  $sql.= " VALUES (".$this->type;
175  $sql.= ", ".($this->fk_account>0 ? $this->fk_account:'NULL');
176  $sql.= ", ".($this->mode_reglement_id>0 ? $this->mode_reglement_id:"NULL");
177  $sql.= ", '".$this->db->escape($this->lib)."'";
178  $sql.= ", '".$this->db->idate($this->date_ech)."'";
179  $sql.= ", '".$this->db->idate($this->periode)."'";
180  $sql.= ", '".price2num($newamount)."'";
181  $sql.= ", ".($this->fk_project>0?$this->fk_project:'NULL');
182  $sql.= ", ".$conf->entity;
183  $sql.= ", ".$user->id;
184  $sql.= ", '".$this->db->idate($now)."'";
185  $sql.= ")";
186 
187  dol_syslog(get_class($this)."::create", LOG_DEBUG);
188  $resql=$this->db->query($sql);
189  if ($resql)
190  {
191  $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."chargesociales");
192 
193  //dol_syslog("ChargesSociales::create this->id=".$this->id);
194  $this->db->commit();
195  return $this->id;
196  }
197  else
198  {
199  $this->error=$this->db->error();
200  $this->db->rollback();
201  return -1;
202  }
203  }
204 
205 
212  function delete($user)
213  {
214  $error=0;
215 
216  $this->db->begin();
217 
218  // Get bank transaction lines for this social contributions
219  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
220  $account=new Account($this->db);
221  $lines_url=$account->get_url('',$this->id,'sc');
222 
223  // Delete bank urls
224  foreach ($lines_url as $line_url)
225  {
226  if (! $error)
227  {
228  $accountline=new AccountLine($this->db);
229  $accountline->fetch($line_url['fk_bank']);
230  $result=$accountline->delete_urls($user);
231  if ($result < 0)
232  {
233  $error++;
234  }
235  }
236  }
237 
238  // Delete payments
239  if (! $error)
240  {
241  $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge WHERE fk_charge=".$this->id;
242  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
243  $resql=$this->db->query($sql);
244  if (! $resql)
245  {
246  $error++;
247  $this->error=$this->db->lasterror();
248  }
249  }
250 
251  if (! $error)
252  {
253  $sql = "DELETE FROM ".MAIN_DB_PREFIX."chargesociales WHERE rowid=".$this->id;
254  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
255  $resql=$this->db->query($sql);
256  if (! $resql)
257  {
258  $error++;
259  $this->error=$this->db->lasterror();
260  }
261  }
262 
263  if (! $error)
264  {
265  $this->db->commit();
266  return 1;
267  }
268  else
269  {
270  $this->db->rollback();
271  return -1;
272  }
273 
274  }
275 
276 
283  function update($user)
284  {
285  $this->db->begin();
286 
287  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
288  $sql.= " SET libelle='".$this->db->escape($this->lib)."'";
289  $sql.= ", date_ech='".$this->db->idate($this->date_ech)."'";
290  $sql.= ", periode='".$this->db->idate($this->periode)."'";
291  $sql.= ", amount='".price2num($this->amount,'MT')."'";
292  $sql.= ", fk_projet='".$this->db->escape($this->fk_project)."'";
293  $sql.= ", fk_user_modif=".$user->id;
294  $sql.= " WHERE rowid=".$this->id;
295 
296  dol_syslog(get_class($this)."::update", LOG_DEBUG);
297  $resql=$this->db->query($sql);
298  if ($resql)
299  {
300  $this->db->commit();
301  return 1;
302  }
303  else
304  {
305  $this->error=$this->db->error();
306  $this->db->rollback();
307  return -1;
308  }
309  }
310 
317  function solde($year = 0)
318  {
319  global $conf;
320 
321  $sql = "SELECT SUM(f.amount) as amount";
322  $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as f";
323  $sql.= " WHERE f.entity = ".$conf->entity;
324  $sql.= " AND paye = 0";
325 
326  if ($year) {
327  $sql .= " AND f.datev >= '$y-01-01' AND f.datev <= '$y-12-31' ";
328  }
329 
330  $result = $this->db->query($sql);
331  if ($result)
332  {
333  if ($this->db->num_rows($result))
334  {
335  $obj = $this->db->fetch_object($result);
336  $this->db->free($result);
337  return $obj->amount;
338  }
339  else
340  {
341  return 0;
342  }
343 
344  }
345  else
346  {
347  print $this->db->error();
348  return -1;
349  }
350  }
351 
358  function set_paid($user)
359  {
360  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
361  $sql.= " paye = 1";
362  $sql.= " WHERE rowid = ".$this->id;
363  $return = $this->db->query($sql);
364  if ($return) return 1;
365  else return -1;
366  }
373  function set_unpaid($user)
374  {
375  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
376  $sql.= " paye = 0";
377  $sql.= " WHERE rowid = ".$this->id;
378  $return = $this->db->query($sql);
379  if ($return) return 1;
380  else return -1;
381  }
382 
390  function getLibStatut($mode=0,$alreadypaid=-1)
391  {
392  return $this->LibStatut($this->paye,$mode,$alreadypaid);
393  }
394 
403  function LibStatut($statut,$mode=0,$alreadypaid=-1)
404  {
405  global $langs;
406  $langs->load('customers');
407  $langs->load('bills');
408 
409  if ($mode == 0)
410  {
411  if ($statut == 0) return $langs->trans("Unpaid");
412  if ($statut == 1) return $langs->trans("Paid");
413  }
414  if ($mode == 1)
415  {
416  if ($statut == 0) return $langs->trans("Unpaid");
417  if ($statut == 1) return $langs->trans("Paid");
418  }
419  if ($mode == 2)
420  {
421  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
422  if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
423  if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
424  }
425  if ($mode == 3)
426  {
427  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1');
428  if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3');
429  if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6');
430  }
431  if ($mode == 4)
432  {
433  if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
434  if ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
435  if ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
436  }
437  if ($mode == 5)
438  {
439  if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
440  if ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
441  if ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
442  }
443  if ($mode == 6)
444  {
445  if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
446  if ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
447  if ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
448  }
449 
450  return "Error, mode/status not found";
451  }
452 
453 
462  function getNomUrl($withpicto=0, $maxlen=0, $notooltip=0)
463  {
464  global $langs;
465 
466  $result='';
467 
468  if (empty($this->ref)) $this->ref=$this->lib;
469  $label = $langs->trans("ShowSocialContribution").': '.$this->ref;
470 
471  $linkstart = '<a href="'.DOL_URL_ROOT.'/compta/sociales/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
472  $linkend='</a>';
473 
474  $result .= $linkstart;
475  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);
476  if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->ref,$maxlen):$this->ref);
477  $result .= $linkend;
478 
479  return $result;
480  }
481 
487  function getSommePaiement()
488  {
489  $table='paiementcharge';
490  $field='fk_charge';
491 
492  $sql = 'SELECT sum(amount) as amount';
493  $sql.= ' FROM '.MAIN_DB_PREFIX.$table;
494  $sql.= ' WHERE '.$field.' = '.$this->id;
495 
496  dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
497  $resql=$this->db->query($sql);
498  if ($resql)
499  {
500  $amount=0;
501 
502  $obj = $this->db->fetch_object($resql);
503  if ($obj) $amount=$obj->amount?$obj->amount:0;
504 
505  $this->db->free($resql);
506  return $amount;
507  }
508  else
509  {
510  return -1;
511  }
512  }
513 
520  function info($id)
521  {
522  $sql = "SELECT e.rowid, e.tms as datem, e.date_creation as datec, e.date_valid as datev, e.import_key,";
523  $sql.= " e.fk_user_author, e.fk_user_modif, e.fk_user_valid";
524  $sql.= " FROM ".MAIN_DB_PREFIX."chargesociales as e";
525  $sql.= " WHERE e.rowid = ".$id;
526 
527  dol_syslog(get_class($this)."::info", LOG_DEBUG);
528  $result=$this->db->query($sql);
529  if ($result)
530  {
531  if ($this->db->num_rows($result))
532  {
533  $obj = $this->db->fetch_object($result);
534 
535  $this->id = $obj->rowid;
536 
537  if ($obj->fk_user_author) {
538  $cuser = new User($this->db);
539  $cuser->fetch($obj->fk_user_author);
540  $this->user_creation = $cuser;
541  }
542 
543  if ($obj->fk_user_modif) {
544  $muser = new User($this->db);
545  $muser->fetch($obj->fk_user_modif);
546  $this->user_modification = $muser;
547  }
548 
549  if ($obj->fk_user_valid) {
550  $vuser = new User($this->db);
551  $vuser->fetch($obj->fk_user_valid);
552  $this->user_validation = $vuser;
553  }
554 
555  $this->date_creation = $this->db->jdate($obj->datec);
556  $this->date_modification = $this->db->jdate($obj->datem);
557  $this->date_validation = $this->db->jdate($obj->datev);
558  $this->import_key = $obj->import_key;
559  }
560 
561  $this->db->free($result);
562 
563  }
564  else
565  {
566  dol_print_error($this->db);
567  }
568  }
569 
577  function initAsSpecimen()
578  {
579  // Initialize parameters
580  $this->id=0;
581  $this->ref = 'SPECIMEN';
582  $this->specimen=1;
583  $this->paye = 0;
584  $this->date = time();
585  $this->date_ech=$this->date+3600*24*30;
586  $this->periode=$this->date+3600*24*30;
587  $this->amount=100;
588  $this->lib = 0;
589  $this->type = 1;
590  $this->type_libelle = 'Social contribution label';
591  }
592 }
593 
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
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.
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'un statut donne.
getNomUrl($withpicto=0, $maxlen=0, $notooltip=0)
Return a link to the object card (with optionaly the picto)
Class to manage Dolibarr users.
Definition: user.class.php:39
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.
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.
info($id)
Charge les informations d'ordre info dans l'objet entrepot.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
initAsSpecimen()
Initialise an instance with random values.
getLibStatut($mode=0, $alreadypaid=-1)
Retourne le libelle du statut d'une charge (impaye, payee)
set_unpaid($user)
Remove tag payed on social contribution.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='gmt')
Return date for now.
print
Draft customers invoices.
Definition: index.php:91
update($user)
Met a jour une charge sociale.
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
fetch($id, $ref='')
Retrouve et charge une charge sociale.
__construct($db)
Constructor.
Classe permettant la gestion des paiements des charges La tva collectee n'est calculee que sur les fa...
type
Definition: viewcat.php:283
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, ...)