dolibarr  13.0.2
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-2020 Frédéric France <frederic.france@netlogic.fr>
5  * Copyright (C) 2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
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 <https://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 
60  public $date_ech;
61 
62 
63  public $label;
64  public $type;
65  public $type_label;
66  public $amount;
67  public $paye;
68  public $periode;
69 
73  public $date_creation;
74 
78  public $date_modification;
79 
83  public $date_validation;
84 
88  public $lib;
89 
93  public $fk_account;
94 
98  public $accountid;
99 
103  public $paiementtype;
104 
108  public $fk_project;
109 
110 
111  const STATUS_UNPAID = 0;
112  const STATUS_PAID = 1;
113 
114 
120  public function __construct($db)
121  {
122  $this->db = $db;
123  }
124 
132  public function fetch($id, $ref = '')
133  {
134  $sql = "SELECT cs.rowid, cs.date_ech";
135  $sql .= ", cs.libelle as label, cs.fk_type, cs.amount, cs.fk_projet as fk_project, cs.paye, cs.periode, cs.import_key";
136  $sql .= ", cs.fk_account, cs.fk_mode_reglement";
137  $sql .= ", c.libelle";
138  $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
139  $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as cs";
140  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_chargesociales as c ON cs.fk_type = c.id";
141  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON cs.fk_mode_reglement = p.id';
142  $sql .= ' WHERE cs.entity IN ('.getEntity('tax').')';
143  if ($ref) $sql .= " AND cs.rowid = ".$ref;
144  else $sql .= " AND cs.rowid = ".$id;
145 
146  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
147  $resql = $this->db->query($sql);
148  if ($resql)
149  {
150  if ($this->db->num_rows($resql))
151  {
152  $obj = $this->db->fetch_object($resql);
153 
154  $this->id = $obj->rowid;
155  $this->ref = $obj->rowid;
156  $this->date_ech = $this->db->jdate($obj->date_ech);
157  $this->lib = $obj->label;
158  $this->label = $obj->label;
159  $this->type = $obj->fk_type;
160  $this->type_label = $obj->libelle;
161  $this->fk_account = $obj->fk_account;
162  $this->mode_reglement_id = $obj->fk_mode_reglement;
163  $this->mode_reglement_code = $obj->mode_reglement_code;
164  $this->mode_reglement = $obj->mode_reglement_libelle;
165  $this->amount = $obj->amount;
166  $this->fk_project = $obj->fk_project;
167  $this->paye = $obj->paye;
168  $this->periode = $this->db->jdate($obj->periode);
169  $this->import_key = $this->import_key;
170 
171  $this->db->free($resql);
172 
173  return 1;
174  } else {
175  return 0;
176  }
177  } else {
178  $this->error = $this->db->lasterror();
179  return -1;
180  }
181  }
182 
188  public function check()
189  {
190  $newamount = price2num($this->amount, 'MT');
191 
192  // Validation parametres
193  if (!$newamount > 0 || empty($this->date_ech) || empty($this->periode))
194  {
195  return false;
196  }
197 
198 
199  return true;
200  }
201 
208  public function create($user)
209  {
210  global $conf;
211  $error = 0;
212 
213  $now = dol_now();
214 
215  // Nettoyage parametres
216  $newamount = price2num($this->amount, 'MT');
217 
218  if (!$this->check()) {
219  $this->error = "ErrorBadParameter";
220  return -2;
221  }
222 
223  $this->db->begin();
224 
225  $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)";
226  $sql .= " VALUES (".$this->type;
227  $sql .= ", ".($this->fk_account > 0 ? $this->fk_account : 'NULL');
228  $sql .= ", ".($this->mode_reglement_id > 0 ? $this->mode_reglement_id : "NULL");
229  $sql .= ", '".$this->db->escape($this->label ? $this->label : $this->lib)."'";
230  $sql .= ", '".$this->db->idate($this->date_ech)."'";
231  $sql .= ", '".$this->db->idate($this->periode)."'";
232  $sql .= ", '".price2num($newamount)."'";
233  $sql .= ", ".($this->fk_project > 0 ? $this->fk_project : 'NULL');
234  $sql .= ", ".$conf->entity;
235  $sql .= ", ".$user->id;
236  $sql .= ", '".$this->db->idate($now)."'";
237  $sql .= ")";
238 
239  dol_syslog(get_class($this)."::create", LOG_DEBUG);
240  $resql = $this->db->query($sql);
241  if ($resql) {
242  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."chargesociales");
243 
244  //dol_syslog("ChargesSociales::create this->id=".$this->id);
245  $result = $this->call_trigger('SOCIALCONTRIBUTION_CREATE', $user);
246  if ($result < 0) $error++;
247 
248  if (empty($error)) {
249  $this->db->commit();
250  return $this->id;
251  } else {
252  $this->db->rollback();
253  return -1 * $error;
254  }
255  } else {
256  $this->error = $this->db->error();
257  $this->db->rollback();
258  return -1;
259  }
260  }
261 
262 
269  public function delete($user)
270  {
271  $error = 0;
272 
273  $this->db->begin();
274 
275  // Get bank transaction lines for this social contributions
276  include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
277  $account = new Account($this->db);
278  $lines_url = $account->get_url('', $this->id, 'sc');
279 
280  // Delete bank urls
281  foreach ($lines_url as $line_url)
282  {
283  if (!$error)
284  {
285  $accountline = new AccountLine($this->db);
286  $accountline->fetch($line_url['fk_bank']);
287  $result = $accountline->delete_urls($user);
288  if ($result < 0)
289  {
290  $error++;
291  }
292  }
293  }
294 
295  // Delete payments
296  if (!$error)
297  {
298  $sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge WHERE fk_charge=".$this->id;
299  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
300  $resql = $this->db->query($sql);
301  if (!$resql)
302  {
303  $error++;
304  $this->error = $this->db->lasterror();
305  }
306  }
307 
308  if (!$error)
309  {
310  $sql = "DELETE FROM ".MAIN_DB_PREFIX."chargesociales WHERE rowid=".$this->id;
311  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
312  $resql = $this->db->query($sql);
313  if (!$resql)
314  {
315  $error++;
316  $this->error = $this->db->lasterror();
317  }
318  }
319 
320  if (!$error)
321  {
322  $this->db->commit();
323  return 1;
324  } else {
325  $this->db->rollback();
326  return -1;
327  }
328  }
329 
330 
338  public function update($user, $notrigger = 0)
339  {
340  $error = 0;
341  $this->db->begin();
342 
343  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales";
344  $sql .= " SET libelle='".$this->db->escape($this->label ? $this->label : $this->lib)."'";
345  $sql .= ", date_ech='".$this->db->idate($this->date_ech)."'";
346  $sql .= ", periode='".$this->db->idate($this->periode)."'";
347  $sql .= ", amount='".price2num($this->amount, 'MT')."'";
348  $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->db->escape($this->fk_project) : "NULL");
349  $sql .= ", fk_user_modif=".$user->id;
350  $sql .= " WHERE rowid=".$this->id;
351 
352  dol_syslog(get_class($this)."::update", LOG_DEBUG);
353  $resql = $this->db->query($sql);
354 
355  if (!$resql) {
356  $error++; $this->errors[] = "Error ".$this->db->lasterror();
357  }
358 
359  if (!$error)
360  {
361  if (!$notrigger)
362  {
363  // Call trigger
364  $result = $this->call_trigger('SOCIALCHARGES_MODIFY', $user);
365  if ($result < 0) $error++;
366  // End call triggers
367  }
368  }
369 
370  // Commit or rollback
371  if ($error)
372  {
373  foreach ($this->errors as $errmsg)
374  {
375  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
376  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
377  }
378  $this->db->rollback();
379  return -1 * $error;
380  } else {
381  $this->db->commit();
382  return 1;
383  }
384  }
385 
392  public function solde($year = 0)
393  {
394  global $conf;
395 
396  $sql = "SELECT SUM(f.amount) as amount";
397  $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as f";
398  $sql .= " WHERE f.entity = ".$conf->entity;
399  $sql .= " AND paye = 0";
400 
401  if ($year) {
402  $sql .= " AND f.datev >= '".((int) $year)."-01-01' AND f.datev <= '".((int) $year)."-12-31' ";
403  }
404 
405  $result = $this->db->query($sql);
406  if ($result)
407  {
408  if ($this->db->num_rows($result))
409  {
410  $obj = $this->db->fetch_object($result);
411  $this->db->free($result);
412  return $obj->amount;
413  } else {
414  return 0;
415  }
416  } else {
417  print $this->db->error();
418  return -1;
419  }
420  }
421 
422  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
429  public function set_paid($user)
430  {
431  // phpcs:enable
432  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
433  $sql .= " paye = 1";
434  $sql .= " WHERE rowid = ".$this->id;
435  $return = $this->db->query($sql);
436  if ($return) return 1;
437  else return -1;
438  }
439 
440  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
447  public function set_unpaid($user)
448  {
449  // phpcs:enable
450  $sql = "UPDATE ".MAIN_DB_PREFIX."chargesociales SET";
451  $sql .= " paye = 0";
452  $sql .= " WHERE rowid = ".$this->id;
453  $return = $this->db->query($sql);
454  if ($return) return 1;
455  else return -1;
456  }
457 
465  public function getLibStatut($mode = 0, $alreadypaid = -1)
466  {
467  return $this->LibStatut($this->paye, $mode, $alreadypaid);
468  }
469 
470  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
479  public function LibStatut($status, $mode = 0, $alreadypaid = -1)
480  {
481  // phpcs:enable
482  global $langs;
483 
484  // Load translation files required by the page
485  $langs->loadLangs(array("customers", "bills"));
486 
487  // We reinit status array to force to redefine them because label may change according to properties values.
488  $this->labelStatus = array();
489  $this->labelStatusShort = array();
490 
491  if (empty($this->labelStatus) || empty($this->labelStatusShort))
492  {
493  global $langs;
494  //$langs->load("mymodule");
495  $this->labelStatus[self::STATUS_UNPAID] = $langs->trans('Unpaid');
496  $this->labelStatus[self::STATUS_PAID] = $langs->trans('Paid');
497  if ($status == self::STATUS_UNPAID && $alreadypaid > 0) $this->labelStatus[self::STATUS_UNPAID] = $langs->trans("BillStatusStarted");
498  $this->labelStatusShort[self::STATUS_UNPAID] = $langs->trans('Unpaid');
499  $this->labelStatusShort[self::STATUS_PAID] = $langs->trans('Paid');
500  if ($status == self::STATUS_UNPAID && $alreadypaid > 0) $this->labelStatusShort[self::STATUS_UNPAID] = $langs->trans("BillStatusStarted");
501  }
502 
503  $statusType = 'status1';
504  if ($status == 0 && $alreadypaid > 0) $statusType = 'status3';
505  if ($status == 1) $statusType = 'status6';
506 
507  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
508  }
509 
510 
521  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $short = 0, $save_lastsearch_value = -1)
522  {
523  global $langs, $conf, $user, $form;
524 
525  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
526 
527  $result = '';
528 
529  $url = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$this->id;
530 
531  if ($short) return $url;
532 
533  if ($option !== 'nolink')
534  {
535  // Add param to save lastsearch_values or not
536  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
537  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
538  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
539  }
540 
541  if (empty($this->ref)) $this->ref = $this->label;
542 
543  $label = img_picto('', 'tax').'<u class="paddingrightonly">'.$langs->trans("SocialContribution").'</u>';
544  if (isset($this->paye)) {
545  $label .= ' '.$this->getLibStatut(5);
546  }
547  if (!empty($this->ref))
548  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
549  if (!empty($this->label))
550  $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
551  if (!empty($this->type_label))
552  $label .= '<br><b>'.$langs->trans('Type').':</b> '.$this->type_label;
553 
554  $linkclose = '';
555  if (empty($notooltip) && $user->rights->facture->lire)
556  {
557  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
558  {
559  $label = $langs->trans("SocialContribution");
560  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
561  }
562  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
563  $linkclose .= ' class="classfortooltip"';
564  }
565 
566  $linkstart = '<a href="'.$url.'"';
567  $linkstart .= $linkclose.'>';
568  $linkend = '</a>';
569 
570  $result .= $linkstart;
571  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);
572  if ($withpicto != 2) $result .= $this->ref;
573  $result .= $linkend;
574 
575  return $result;
576  }
577 
583  public function getSommePaiement()
584  {
585  $table = 'paiementcharge';
586  $field = 'fk_charge';
587 
588  $sql = 'SELECT sum(amount) as amount';
589  $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
590  $sql .= ' WHERE '.$field.' = '.$this->id;
591 
592  dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
593  $resql = $this->db->query($sql);
594  if ($resql) {
595  $amount = 0;
596 
597  $obj = $this->db->fetch_object($resql);
598  if ($obj) $amount = $obj->amount ? $obj->amount : 0;
599 
600  $this->db->free($resql);
601  return $amount;
602  } else {
603  return -1;
604  }
605  }
606 
613  public function info($id)
614  {
615  $sql = "SELECT e.rowid, e.tms as datem, e.date_creation as datec, e.date_valid as datev, e.import_key,";
616  $sql .= " e.fk_user_author, e.fk_user_modif, e.fk_user_valid";
617  $sql .= " FROM ".MAIN_DB_PREFIX."chargesociales as e";
618  $sql .= " WHERE e.rowid = ".$id;
619 
620  dol_syslog(get_class($this)."::info", LOG_DEBUG);
621  $result = $this->db->query($sql);
622  if ($result)
623  {
624  if ($this->db->num_rows($result))
625  {
626  $obj = $this->db->fetch_object($result);
627 
628  $this->id = $obj->rowid;
629 
630  if ($obj->fk_user_author) {
631  $cuser = new User($this->db);
632  $cuser->fetch($obj->fk_user_author);
633  $this->user_creation = $cuser;
634  }
635 
636  if ($obj->fk_user_modif) {
637  $muser = new User($this->db);
638  $muser->fetch($obj->fk_user_modif);
639  $this->user_modification = $muser;
640  }
641 
642  if ($obj->fk_user_valid) {
643  $vuser = new User($this->db);
644  $vuser->fetch($obj->fk_user_valid);
645  $this->user_validation = $vuser;
646  }
647 
648  $this->date_creation = $this->db->jdate($obj->datec);
649  $this->date_modification = $this->db->jdate($obj->datem);
650  $this->date_validation = $this->db->jdate($obj->datev);
651  $this->import_key = $obj->import_key;
652  }
653 
654  $this->db->free($result);
655  } else {
656  dol_print_error($this->db);
657  }
658  }
659 
667  public function initAsSpecimen()
668  {
669  // Initialize parameters
670  $this->id = 0;
671  $this->ref = 'SPECIMEN';
672  $this->specimen = 1;
673  $this->paye = 0;
674  $this->date = dol_now();
675  $this->date_ech = $this->date + 3600 * 24 * 30;
676  $this->periode = $this->date + 3600 * 24 * 30;
677  $this->amount = 100;
678  $this->label = 'Social contribution label';
679  $this->type = 1;
680  $this->type_label = 'Type of social contribution';
681  }
682 }
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.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
set_paid($user)
Tag social contribution as paid completely.
Class to manage bank transaction lines.
solde($year=0)
Calculate amount remaining to pay by year.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $short=0, $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
info($id)
Charge les informations d&#39;ordre info dans l&#39;objet entrepot.
initAsSpecimen()
Initialise an instance with random values.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
getLibStatut($mode=0, $alreadypaid=-1)
Retourne le libelle du statut d&#39;une charge (impaye, payee)
set_unpaid($user)
Remove tag paid on social contribution.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
LibStatut($status, $mode=0, $alreadypaid=-1)
Renvoi le libelle d&#39;un statut donne.
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
Definition: index.php:89
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
update($user, $notrigger=0)
Update social or fiscal contribution.
fetch($id, $ref= '')
Retrouve et charge une charge sociale.
__construct($db)
Constructor.
Classe permettant la gestion des paiements des charges La tva collectee n&#39;est calculee que sur les fa...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)