dolibarr  13.0.2
cchargesociales.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2015 RaphaĆ«l Doursenaud <rdoursenaud@gpcsolutions.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 
27 // Put here all includes required by your class file
28 //require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
29 //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
30 //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
31 
36 {
40  public $element = 'cchargesociales';
41 
45  public $table_element = 'c_chargesociales';
46 
50  public $libelle;
51 
52  public $deductible;
53  public $active;
54  public $code;
55 
59  public $fk_pays;
60 
64  public $module;
65  public $accountancy_code;
66 
67 
73  public function __construct(DoliDB $db)
74  {
75  $this->db = $db;
76  }
77 
86  public function create(User $user, $notrigger = false)
87  {
88  dol_syslog(__METHOD__, LOG_DEBUG);
89 
90  $error = 0;
91 
92  // Clean parameters
93  $this->trimParameters(
94  array(
95  'libelle',
96  'deductible',
97  'active',
98  'code',
99  'fk_pays',
100  'module',
101  'accountancy_code',
102  )
103  );
104 
105  // Check parameters
106  // Put here code to add control on parameters values
107 
108  // Insert request
109  $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
110 
111  $sql .= 'libelle,';
112  $sql .= 'deductible,';
113  $sql .= 'active,';
114  $sql .= 'code,';
115  $sql .= 'fk_pays,';
116  $sql .= 'module';
117  $sql .= 'accountancy_code';
118 
119 
120  $sql .= ') VALUES (';
121 
122  $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
123  $sql .= ' '.(!isset($this->deductible) ? 'NULL' : $this->deductible).',';
124  $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
125  $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
126  $sql .= ' '.(!isset($this->fk_pays) ? 'NULL' : $this->fk_pays).',';
127  $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'").',';
128  $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'");
129 
130 
131  $sql .= ')';
132 
133  $this->db->begin();
134 
135  $resql = $this->db->query($sql);
136  if (!$resql) {
137  $error++;
138  $this->errors[] = 'Error '.$this->db->lasterror();
139  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
140  }
141 
142  if (!$error) {
143  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
144 
145  //if (!$notrigger) {
146  // Uncomment this and change MYOBJECT to your own tag if you
147  // want this action to call a trigger.
148 
150  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
151  //if ($result < 0) $error++;
153  //}
154  }
155 
156  // Commit or rollback
157  if ($error) {
158  $this->db->rollback();
159 
160  return -1 * $error;
161  } else {
162  $this->db->commit();
163 
164  return $this->id;
165  }
166  }
167 
176  public function fetch($id, $ref = null)
177  {
178  dol_syslog(__METHOD__, LOG_DEBUG);
179 
180  $sql = 'SELECT';
181  $sql .= " t.id,";
182  $sql .= " t.libelle,";
183  $sql .= " t.deductible,";
184  $sql .= " t.active,";
185  $sql .= " t.code,";
186  $sql .= " t.fk_pays,";
187  $sql .= " t.module,";
188  $sql .= " t.accountancy_code";
189  $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
190  if (null !== $ref) {
191  $sql .= " WHERE t.code = '".$this->db->escape($ref)."'";
192  } else {
193  $sql .= ' WHERE t.id = '.$id;
194  }
195 
196  $resql = $this->db->query($sql);
197  if ($resql) {
198  $numrows = $this->db->num_rows($resql);
199  if ($numrows) {
200  $obj = $this->db->fetch_object($resql);
201 
202  $this->id = $obj->id;
203 
204  $this->libelle = $obj->libelle;
205  $this->deductible = $obj->deductible;
206  $this->active = $obj->active;
207  $this->code = $obj->code;
208  $this->fk_pays = $obj->fk_pays;
209  $this->module = $obj->module;
210  $this->accountancy_code = $obj->accountancy_code;
211  }
212  $this->db->free($resql);
213 
214  if ($numrows) {
215  return 1;
216  } else {
217  return 0;
218  }
219  } else {
220  $this->errors[] = 'Error '.$this->db->lasterror();
221  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
222 
223  return -1;
224  }
225  }
226 
235  public function update(User $user, $notrigger = false)
236  {
237  $error = 0;
238 
239  dol_syslog(__METHOD__, LOG_DEBUG);
240 
241  // Clean parameters
242 
243  $this->trimParameters(
244  array(
245  'libelle',
246  'deductible',
247  'active',
248  'code',
249  'fk_pays',
250  'module',
251  'accountancy_code',
252  )
253  );
254 
255 
256  // Check parameters
257  // Put here code to add a control on parameters values
258 
259  // Update request
260  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
261  $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
262  $sql .= ' deductible = '.(isset($this->deductible) ? $this->deductible : "null").',';
263  $sql .= ' active = '.(isset($this->active) ? $this->active : "null").',';
264  $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
265  $sql .= ' fk_pays = '.(isset($this->fk_pays) ? $this->fk_pays : "null").',';
266  $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null").',';
267  $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null");
268  $sql .= ' WHERE id='.$this->id;
269 
270  $this->db->begin();
271 
272  $resql = $this->db->query($sql);
273  if (!$resql) {
274  $error++;
275  $this->errors[] = 'Error '.$this->db->lasterror();
276  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
277  }
278 
279  //if (!$error && !$notrigger) {
280  // Uncomment this and change MYOBJECT to your own tag if you
281  // want this action calls a trigger.
282 
284  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
285  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
287  //}
288 
289  // Commit or rollback
290  if ($error) {
291  $this->db->rollback();
292 
293  return -1 * $error;
294  } else {
295  $this->db->commit();
296 
297  return 1;
298  }
299  }
300 
309  public function delete(User $user, $notrigger = false)
310  {
311  dol_syslog(__METHOD__, LOG_DEBUG);
312 
313  $error = 0;
314 
315  $this->db->begin();
316 
317  //if (!$error) {
318  //if (!$notrigger) {
319  // Uncomment this and change MYOBJECT to your own tag if you
320  // want this action calls a trigger.
321 
323  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
324  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
326  //}
327  //}
328 
329  if (!$error) {
330  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
331  $sql .= ' WHERE id='.$this->id;
332 
333  $resql = $this->db->query($sql);
334  if (!$resql) {
335  $error++;
336  $this->errors[] = 'Error '.$this->db->lasterror();
337  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
338  }
339  }
340 
341  // Commit or rollback
342  if ($error) {
343  $this->db->rollback();
344 
345  return -1 * $error;
346  } else {
347  $this->db->commit();
348 
349  return 1;
350  }
351  }
352 
360  public function createFromClone(User $user, $fromid)
361  {
362  dol_syslog(__METHOD__, LOG_DEBUG);
363 
364  $error = 0;
365  $object = new Cchargesociales($this->db);
366 
367  $this->db->begin();
368 
369  // Load source object
370  $object->fetch($fromid);
371  // Reset object
372  $object->id = 0;
373 
374  // Clear fields
375  // ...
376 
377  // Create clone
378  $this->context['createfromclone'] = 'createfromclone';
379  $result = $object->create($user);
380 
381  // Other options
382  if ($result < 0) {
383  $error++;
384  $this->errors = $object->errors;
385  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
386  }
387 
388  unset($this->context['createfromclone']);
389 
390  // End
391  if (!$error) {
392  $this->db->commit();
393 
394  return $object->id;
395  } else {
396  $this->db->rollback();
397 
398  return -1;
399  }
400  }
401 
413  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
414  {
415  global $langs, $conf, $db;
416  global $dolibarr_main_authentication, $dolibarr_main_demo;
417  global $menumanager;
418 
419 
420  $result = '';
421  $companylink = '';
422 
423  $label = '<u>'.$langs->trans("MyModule").'</u>';
424  $label .= '<div width="100%">';
425  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
426 
427  $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
428  $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
429  $link .= '>';
430  $linkend = '</a>';
431 
432  if ($withpicto)
433  {
434  $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
435  if ($withpicto != 2) $result .= ' ';
436  }
437  $result .= $link.$this->ref.$linkend;
438  return $result;
439  }
440 
447  public function getLibStatut($mode = 0)
448  {
449  return $this->LibStatut($this->status, $mode);
450  }
451 
452  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
460  public function LibStatut($status, $mode = 0)
461  {
462  // phpcs:enable
463  global $langs;
464 
465  if ($mode == 0)
466  {
467  if ($status == 1) return $langs->trans('Enabled');
468  elseif ($status == 0) return $langs->trans('Disabled');
469  } elseif ($mode == 1)
470  {
471  if ($status == 1) return $langs->trans('Enabled');
472  elseif ($status == 0) return $langs->trans('Disabled');
473  } elseif ($mode == 2)
474  {
475  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
476  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
477  } elseif ($mode == 3)
478  {
479  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4');
480  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5');
481  } elseif ($mode == 4)
482  {
483  if ($status == 1) return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
484  elseif ($status == 0) return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
485  } elseif ($mode == 5)
486  {
487  if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
488  elseif ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
489  }
490  }
491 
492 
499  public function initAsSpecimen()
500  {
501  $this->id = 0;
502 
503  $this->libelle = '';
504  $this->deductible = '';
505  $this->active = '';
506  $this->code = '';
507  $this->fk_pays = '';
508  $this->module = '';
509  $this->accountancy_code = '';
510  }
511 
518  private function trimParameters($parameters)
519  {
520  foreach ($parameters as $parameter) {
521  if (isset($this->$parameter)) {
522  $this->$parameter = trim($this->$parameter);
523  }
524  }
525  }
526 }
__construct(DoliDB $db)
Constructor.
getLibStatut($mode=0)
Retourne le libelle du status d&#39;un user (actif, inactif)
trimParameters($parameters)
Trim object parameters.
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
Definition: list.php:566
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
$conf db
API class for accounts.
Definition: inc.php:54
update(User $user, $notrigger=false)
Update object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
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.
create(User $user, $notrigger=false)
Create object into database.
Class Cchargesociales.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $maxlen=24, $morecss= '')
Return a link to the user card (with optionaly the picto) Use this-&gt;id,this-&gt;lastname, this-&gt;firstname.
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
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un status donne.
fetch($id, $ref=null)
Load object in memory from the database.