dolibarr  9.0.0
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 <http://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 
47  public $libelle;
48  public $deductible;
49  public $active;
50  public $code;
51 
55  public $fk_pays;
56 
57  public $module;
58  public $accountancy_code;
59 
60 
66  public function __construct(DoliDB $db)
67  {
68  $this->db = $db;
69  }
70 
79  public function create(User $user, $notrigger = false)
80  {
81  dol_syslog(__METHOD__, LOG_DEBUG);
82 
83  $error = 0;
84 
85  // Clean parameters
86  $this->trimParameters(
87  array(
88  'libelle',
89  'deductible',
90  'active',
91  'code',
92  'fk_pays',
93  'module',
94  'accountancy_code',
95  )
96  );
97 
98  // Check parameters
99  // Put here code to add control on parameters values
100 
101  // Insert request
102  $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
103 
104  $sql.= 'libelle,';
105  $sql.= 'deductible,';
106  $sql.= 'active,';
107  $sql.= 'code,';
108  $sql.= 'fk_pays,';
109  $sql.= 'module';
110  $sql.= 'accountancy_code';
111 
112 
113  $sql .= ') VALUES (';
114 
115  $sql .= ' '.(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").',';
116  $sql .= ' '.(! isset($this->deductible)?'NULL':$this->deductible).',';
117  $sql .= ' '.(! isset($this->active)?'NULL':$this->active).',';
118  $sql .= ' '.(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").',';
119  $sql .= ' '.(! isset($this->fk_pays)?'NULL':$this->fk_pays).',';
120  $sql .= ' '.(! isset($this->module)?'NULL':"'".$this->db->escape($this->module)."'").',';
121  $sql .= ' '.(! isset($this->accountancy_code)?'NULL':"'".$this->db->escape($this->accountancy_code)."'");
122 
123 
124  $sql .= ')';
125 
126  $this->db->begin();
127 
128  $resql = $this->db->query($sql);
129  if (!$resql) {
130  $error ++;
131  $this->errors[] = 'Error ' . $this->db->lasterror();
132  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
133  }
134 
135  if (!$error) {
136  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
137 
138  //if (!$notrigger) {
139  // Uncomment this and change MYOBJECT to your own tag if you
140  // want this action to call a trigger.
141 
143  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
144  //if ($result < 0) $error++;
146  //}
147  }
148 
149  // Commit or rollback
150  if ($error) {
151  $this->db->rollback();
152 
153  return - 1 * $error;
154  } else {
155  $this->db->commit();
156 
157  return $this->id;
158  }
159  }
160 
169  public function fetch($id, $ref = null)
170  {
171  dol_syslog(__METHOD__, LOG_DEBUG);
172 
173  $sql = 'SELECT';
174  $sql .= " t.id,";
175  $sql .= " t.libelle,";
176  $sql .= " t.deductible,";
177  $sql .= " t.active,";
178  $sql .= " t.code,";
179  $sql .= " t.fk_pays,";
180  $sql .= " t.module,";
181  $sql .= " t.accountancy_code";
182  $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
183  if (null !== $ref) {
184  $sql .= " WHERE t.code = '" . $this->db->escape($ref) . "'";
185  } else {
186  $sql .= ' WHERE t.id = ' . $id;
187  }
188 
189  $resql = $this->db->query($sql);
190  if ($resql) {
191  $numrows = $this->db->num_rows($resql);
192  if ($numrows) {
193  $obj = $this->db->fetch_object($resql);
194 
195  $this->id = $obj->id;
196 
197  $this->libelle = $obj->libelle;
198  $this->deductible = $obj->deductible;
199  $this->active = $obj->active;
200  $this->code = $obj->code;
201  $this->fk_pays = $obj->fk_pays;
202  $this->module = $obj->module;
203  $this->accountancy_code = $obj->accountancy_code;
204  }
205  $this->db->free($resql);
206 
207  if ($numrows) {
208  return 1;
209  } else {
210  return 0;
211  }
212  } else {
213  $this->errors[] = 'Error ' . $this->db->lasterror();
214  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
215 
216  return - 1;
217  }
218  }
219 
228  public function update(User $user, $notrigger = false)
229  {
230  $error = 0;
231 
232  dol_syslog(__METHOD__, LOG_DEBUG);
233 
234  // Clean parameters
235 
236  $this->trimParameters(
237  array(
238  'libelle',
239  'deductible',
240  'active',
241  'code',
242  'fk_pays',
243  'module',
244  'accountancy_code',
245  )
246  );
247 
248 
249  // Check parameters
250  // Put here code to add a control on parameters values
251 
252  // Update request
253  $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
254  $sql .= ' libelle = '.(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").',';
255  $sql .= ' deductible = '.(isset($this->deductible)?$this->deductible:"null").',';
256  $sql .= ' active = '.(isset($this->active)?$this->active:"null").',';
257  $sql .= ' code = '.(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").',';
258  $sql .= ' fk_pays = '.(isset($this->fk_pays)?$this->fk_pays:"null").',';
259  $sql .= ' module = '.(isset($this->module)?"'".$this->db->escape($this->module)."'":"null").',';
260  $sql .= ' accountancy_code = '.(isset($this->accountancy_code)?"'".$this->db->escape($this->accountancy_code)."'":"null");
261  $sql .= ' WHERE id=' . $this->id;
262 
263  $this->db->begin();
264 
265  $resql = $this->db->query($sql);
266  if (!$resql) {
267  $error ++;
268  $this->errors[] = 'Error ' . $this->db->lasterror();
269  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
270  }
271 
272  //if (!$error && !$notrigger) {
273  // Uncomment this and change MYOBJECT to your own tag if you
274  // want this action calls a trigger.
275 
277  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
278  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
280  //}
281 
282  // Commit or rollback
283  if ($error) {
284  $this->db->rollback();
285 
286  return - 1 * $error;
287  } else {
288  $this->db->commit();
289 
290  return 1;
291  }
292  }
293 
302  public function delete(User $user, $notrigger = false)
303  {
304  dol_syslog(__METHOD__, LOG_DEBUG);
305 
306  $error = 0;
307 
308  $this->db->begin();
309 
310  //if (!$error) {
311  //if (!$notrigger) {
312  // Uncomment this and change MYOBJECT to your own tag if you
313  // want this action calls a trigger.
314 
316  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
317  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
319  //}
320  //}
321 
322  if (!$error) {
323  $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
324  $sql .= ' WHERE id=' . $this->id;
325 
326  $resql = $this->db->query($sql);
327  if (!$resql) {
328  $error ++;
329  $this->errors[] = 'Error ' . $this->db->lasterror();
330  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
331  }
332  }
333 
334  // Commit or rollback
335  if ($error) {
336  $this->db->rollback();
337 
338  return - 1 * $error;
339  } else {
340  $this->db->commit();
341 
342  return 1;
343  }
344  }
345 
353  public function createFromClone($fromid)
354  {
355  dol_syslog(__METHOD__, LOG_DEBUG);
356 
357  global $user;
358  $error = 0;
359  $object = new Cchargesociales($this->db);
360 
361  $this->db->begin();
362 
363  // Load source object
364  $object->fetch($fromid);
365  // Reset object
366  $object->id = 0;
367 
368  // Clear fields
369  // ...
370 
371  // Create clone
372  $this->context['createfromclone'] = 'createfromclone';
373  $result = $object->create($user);
374 
375  // Other options
376  if ($result < 0) {
377  $error ++;
378  $this->errors = $object->errors;
379  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
380  }
381 
382  unset($this->context['createfromclone']);
383 
384  // End
385  if (!$error) {
386  $this->db->commit();
387 
388  return $object->id;
389  } else {
390  $this->db->rollback();
391 
392  return -1;
393  }
394  }
395 
407  function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
408  {
409  global $langs, $conf, $db;
410  global $dolibarr_main_authentication, $dolibarr_main_demo;
411  global $menumanager;
412 
413 
414  $result = '';
415  $companylink = '';
416 
417  $label = '<u>' . $langs->trans("MyModule") . '</u>';
418  $label.= '<div width="100%">';
419  $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
420 
421  $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
422  $link.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
423  $link.= '>';
424  $linkend='</a>';
425 
426  if ($withpicto)
427  {
428  $result.=($link.img_object(($notooltip?'':$label), 'label', ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
429  if ($withpicto != 2) $result.=' ';
430  }
431  $result.= $link . $this->ref . $linkend;
432  return $result;
433  }
434 
441  function getLibStatut($mode=0)
442  {
443  return $this->LibStatut($this->status,$mode);
444  }
445 
446  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
454  function LibStatut($status,$mode=0)
455  {
456  // phpcs:enable
457  global $langs;
458 
459  if ($mode == 0)
460  {
461  if ($status == 1) return $langs->trans('Enabled');
462  elseif ($status == 0) return $langs->trans('Disabled');
463  }
464  elseif ($mode == 1)
465  {
466  if ($status == 1) return $langs->trans('Enabled');
467  elseif ($status == 0) return $langs->trans('Disabled');
468  }
469  elseif ($mode == 2)
470  {
471  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
472  elseif ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
473  }
474  elseif ($mode == 3)
475  {
476  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
477  elseif ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
478  }
479  elseif ($mode == 4)
480  {
481  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
482  elseif ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
483  }
484  elseif ($mode == 5)
485  {
486  if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
487  elseif ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
488  }
489  }
490 
491 
498  public function initAsSpecimen()
499  {
500  $this->id = 0;
501 
502  $this->libelle = '';
503  $this->deductible = '';
504  $this->active = '';
505  $this->code = '';
506  $this->fk_pays = '';
507  $this->module = '';
508  $this->accountancy_code = '';
509  }
510 
517  private function trimParameters($parameters)
518  {
519  if (!is_array($parameters)) return;
520  foreach ($parameters as $parameter) {
521  if (isset($this->$parameter)) {
522  $this->$parameter = trim($this->$parameter);
523  }
524  }
525  }
526 }
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
__construct(DoliDB $db)
Constructor.
getLibStatut($mode=0)
Retourne le libelle du status d&#39;un user (actif, inactif)
trimParameters($parameters)
Trim object parameters.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage Dolibarr database access.
createFromClone($fromid)
Load an object from its id and create a new one in database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
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.
create(User $user, $notrigger=false)
Create object into database.
Class Cchargesociales.
LibStatut($status, $mode=0)
Renvoi le libelle d&#39;un status donne.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
fetch($id, $ref=null)
Load object in memory from the database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
Return a link to the user card (with optionaly the picto) Use this->id,this->lastname, this->firstname.