dolibarr  7.0.0-beta
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';
44  public $table_element = 'c_chargesociales';
45 
49  public $libelle;
50  public $deductible;
51  public $active;
52  public $code;
53  public $fk_pays;
54  public $module;
55  public $accountancy_code;
56 
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 
87  if (isset($this->libelle)) {
88  $this->libelle = trim($this->libelle);
89  }
90  if (isset($this->deductible)) {
91  $this->deductible = trim($this->deductible);
92  }
93  if (isset($this->active)) {
94  $this->active = trim($this->active);
95  }
96  if (isset($this->code)) {
97  $this->code = trim($this->code);
98  }
99  if (isset($this->fk_pays)) {
100  $this->fk_pays = trim($this->fk_pays);
101  }
102  if (isset($this->module)) {
103  $this->module = trim($this->module);
104  }
105  if (isset($this->accountancy_code)) {
106  $this->accountancy_code = trim($this->accountancy_code);
107  }
108 
109 
110 
111  // Check parameters
112  // Put here code to add control on parameters values
113 
114  // Insert request
115  $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
116 
117  $sql.= 'libelle,';
118  $sql.= 'deductible,';
119  $sql.= 'active,';
120  $sql.= 'code,';
121  $sql.= 'fk_pays,';
122  $sql.= 'module';
123  $sql.= 'accountancy_code';
124 
125 
126  $sql .= ') VALUES (';
127 
128  $sql .= ' '.(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").',';
129  $sql .= ' '.(! isset($this->deductible)?'NULL':$this->deductible).',';
130  $sql .= ' '.(! isset($this->active)?'NULL':$this->active).',';
131  $sql .= ' '.(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").',';
132  $sql .= ' '.(! isset($this->fk_pays)?'NULL':$this->fk_pays).',';
133  $sql .= ' '.(! isset($this->module)?'NULL':"'".$this->db->escape($this->module)."'").',';
134  $sql .= ' '.(! isset($this->accountancy_code)?'NULL':"'".$this->db->escape($this->accountancy_code)."'");
135 
136 
137  $sql .= ')';
138 
139  $this->db->begin();
140 
141  $resql = $this->db->query($sql);
142  if (!$resql) {
143  $error ++;
144  $this->errors[] = 'Error ' . $this->db->lasterror();
145  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
146  }
147 
148  if (!$error) {
149  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
150 
151  if (!$notrigger) {
152  // Uncomment this and change MYOBJECT to your own tag if you
153  // want this action to call a trigger.
154 
156  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
157  //if ($result < 0) $error++;
159  }
160  }
161 
162  // Commit or rollback
163  if ($error) {
164  $this->db->rollback();
165 
166  return - 1 * $error;
167  } else {
168  $this->db->commit();
169 
170  return $this->id;
171  }
172  }
173 
182  public function fetch($id, $ref = null)
183  {
184  dol_syslog(__METHOD__, LOG_DEBUG);
185 
186  $sql = 'SELECT';
187  $sql .= " t.id,";
188  $sql .= " t.libelle,";
189  $sql .= " t.deductible,";
190  $sql .= " t.active,";
191  $sql .= " t.code,";
192  $sql .= " t.fk_pays,";
193  $sql .= " t.module,";
194  $sql .= " t.accountancy_code";
195  $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
196  if (null !== $ref) {
197  $sql .= " WHERE t.code = '" . $this->db->escape($ref) . "'";
198  } else {
199  $sql .= ' WHERE t.id = ' . $id;
200  }
201 
202  $resql = $this->db->query($sql);
203  if ($resql) {
204  $numrows = $this->db->num_rows($resql);
205  if ($numrows) {
206  $obj = $this->db->fetch_object($resql);
207 
208  $this->id = $obj->id;
209 
210  $this->libelle = $obj->libelle;
211  $this->deductible = $obj->deductible;
212  $this->active = $obj->active;
213  $this->code = $obj->code;
214  $this->fk_pays = $obj->fk_pays;
215  $this->module = $obj->module;
216  $this->accountancy_code = $obj->accountancy_code;
217 
218 
219  }
220  $this->db->free($resql);
221 
222  if ($numrows) {
223  return 1;
224  } else {
225  return 0;
226  }
227  } else {
228  $this->errors[] = 'Error ' . $this->db->lasterror();
229  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
230 
231  return - 1;
232  }
233  }
234 
243  public function update(User $user, $notrigger = false)
244  {
245  $error = 0;
246 
247  dol_syslog(__METHOD__, LOG_DEBUG);
248 
249  // Clean parameters
250 
251  if (isset($this->libelle)) {
252  $this->libelle = trim($this->libelle);
253  }
254  if (isset($this->deductible)) {
255  $this->deductible = trim($this->deductible);
256  }
257  if (isset($this->active)) {
258  $this->active = trim($this->active);
259  }
260  if (isset($this->code)) {
261  $this->code = trim($this->code);
262  }
263  if (isset($this->fk_pays)) {
264  $this->fk_pays = trim($this->fk_pays);
265  }
266  if (isset($this->module)) {
267  $this->module = trim($this->module);
268  }
269  if (isset($this->accountancy_code)) {
270  $this->accountancy_code = trim($this->accountancy_code);
271  }
272 
273 
274 
275  // Check parameters
276  // Put here code to add a control on parameters values
277 
278  // Update request
279  $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
280  $sql .= ' libelle = '.(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").',';
281  $sql .= ' deductible = '.(isset($this->deductible)?$this->deductible:"null").',';
282  $sql .= ' active = '.(isset($this->active)?$this->active:"null").',';
283  $sql .= ' code = '.(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").',';
284  $sql .= ' fk_pays = '.(isset($this->fk_pays)?$this->fk_pays:"null").',';
285  $sql .= ' module = '.(isset($this->module)?"'".$this->db->escape($this->module)."'":"null").',';
286  $sql .= ' accountancy_code = '.(isset($this->accountancy_code)?"'".$this->db->escape($this->accountancy_code)."'":"null");
287  $sql .= ' WHERE id=' . $this->id;
288 
289  $this->db->begin();
290 
291  $resql = $this->db->query($sql);
292  if (!$resql) {
293  $error ++;
294  $this->errors[] = 'Error ' . $this->db->lasterror();
295  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
296  }
297 
298  if (!$error && !$notrigger) {
299  // Uncomment this and change MYOBJECT to your own tag if you
300  // want this action calls a trigger.
301 
303  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
304  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
306  }
307 
308  // Commit or rollback
309  if ($error) {
310  $this->db->rollback();
311 
312  return - 1 * $error;
313  } else {
314  $this->db->commit();
315 
316  return 1;
317  }
318  }
319 
328  public function delete(User $user, $notrigger = false)
329  {
330  dol_syslog(__METHOD__, LOG_DEBUG);
331 
332  $error = 0;
333 
334  $this->db->begin();
335 
336  if (!$error) {
337  if (!$notrigger) {
338  // Uncomment this and change MYOBJECT to your own tag if you
339  // want this action calls a trigger.
340 
342  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
343  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
345  }
346  }
347 
348  if (!$error) {
349  $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
350  $sql .= ' WHERE id=' . $this->id;
351 
352  $resql = $this->db->query($sql);
353  if (!$resql) {
354  $error ++;
355  $this->errors[] = 'Error ' . $this->db->lasterror();
356  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
357  }
358  }
359 
360  // Commit or rollback
361  if ($error) {
362  $this->db->rollback();
363 
364  return - 1 * $error;
365  } else {
366  $this->db->commit();
367 
368  return 1;
369  }
370  }
371 
379  public function createFromClone($fromid)
380  {
381  dol_syslog(__METHOD__, LOG_DEBUG);
382 
383  global $user;
384  $error = 0;
385  $object = new Cchargesociales($this->db);
386 
387  $this->db->begin();
388 
389  // Load source object
390  $object->fetch($fromid);
391  // Reset object
392  $object->id = 0;
393 
394  // Clear fields
395  // ...
396 
397  // Create clone
398  $result = $object->create($user);
399 
400  // Other options
401  if ($result < 0) {
402  $error ++;
403  $this->errors = $object->errors;
404  dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
405  }
406 
407  // End
408  if (!$error) {
409  $this->db->commit();
410 
411  return $object->id;
412  } else {
413  $this->db->rollback();
414 
415  return - 1;
416  }
417  }
418 
430  function getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='')
431  {
432  global $langs, $conf, $db;
433  global $dolibarr_main_authentication, $dolibarr_main_demo;
434  global $menumanager;
435 
436 
437  $result = '';
438  $companylink = '';
439 
440  $label = '<u>' . $langs->trans("MyModule") . '</u>';
441  $label.= '<div width="100%">';
442  $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
443 
444  $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
445  $link.= ($notooltip?'':' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss?' '.$morecss:'').'"');
446  $link.= '>';
447  $linkend='</a>';
448 
449  if ($withpicto)
450  {
451  $result.=($link.img_object(($notooltip?'':$label), 'label', ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
452  if ($withpicto != 2) $result.=' ';
453  }
454  $result.= $link . $this->ref . $linkend;
455  return $result;
456  }
457 
464  function getLibStatut($mode=0)
465  {
466  return $this->LibStatut($this->status,$mode);
467  }
468 
476  function LibStatut($status,$mode=0)
477  {
478  global $langs;
479 
480  if ($mode == 0)
481  {
482  $prefix='';
483  if ($status == 1) return $langs->trans('Enabled');
484  if ($status == 0) return $langs->trans('Disabled');
485  }
486  if ($mode == 1)
487  {
488  if ($status == 1) return $langs->trans('Enabled');
489  if ($status == 0) return $langs->trans('Disabled');
490  }
491  if ($mode == 2)
492  {
493  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
494  if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
495  }
496  if ($mode == 3)
497  {
498  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4');
499  if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5');
500  }
501  if ($mode == 4)
502  {
503  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4').' '.$langs->trans('Enabled');
504  if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5').' '.$langs->trans('Disabled');
505  }
506  if ($mode == 5)
507  {
508  if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4');
509  if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5');
510  }
511  }
512 
513 
520  public function initAsSpecimen()
521  {
522  $this->id = 0;
523 
524  $this->libelle = '';
525  $this->deductible = '';
526  $this->active = '';
527  $this->code = '';
528  $this->fk_pays = '';
529  $this->module = '';
530  $this->accountancy_code = '';
531 
532 
533  }
534 
535 }
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
__construct(DoliDB $db)
Constructor.
getLibStatut($mode=0)
Retourne le libelle du status d'un user (actif, inactif)
Class to manage Dolibarr users.
Definition: user.class.php:39
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.
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
LibStatut($status, $mode=0)
Renvoi le libelle d'un status donne.
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.