dolibarr  19.0.0-dev
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 
51  public $libelle;
52 
56  public $label;
57 
58  public $deductible;
59  public $active;
60  public $code;
61 
65  public $fk_pays;
66 
70  public $module;
71  public $accountancy_code;
72 
73 
79  public function __construct(DoliDB $db)
80  {
81  $this->db = $db;
82  }
83 
92  public function create(User $user, $notrigger = false)
93  {
94  dol_syslog(__METHOD__, LOG_DEBUG);
95 
96  $error = 0;
97 
98  // Clean parameters
99  $this->trimParameters(
100  array(
101  'libelle',
102  'deductible',
103  'active',
104  'code',
105  'fk_pays',
106  'module',
107  'accountancy_code',
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  $sql .= 'libelle,';
117  $sql .= 'deductible,';
118  $sql .= 'active,';
119  $sql .= 'code,';
120  $sql .= 'fk_pays,';
121  $sql .= 'module';
122  $sql .= 'accountancy_code';
123  $sql .= ') VALUES (';
124  $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
125  $sql .= ' '.(!isset($this->deductible) ? 'NULL' : $this->deductible).',';
126  $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
127  $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
128  $sql .= ' '.(!isset($this->fk_pays) ? 'NULL' : $this->fk_pays).',';
129  $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'").',';
130  $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'");
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 as label,";
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 = '.((int) $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->label;
205  $this->label = $obj->label;
206  $this->deductible = $obj->deductible;
207  $this->active = $obj->active;
208  $this->code = $obj->code;
209  $this->fk_pays = $obj->fk_pays;
210  $this->module = $obj->module;
211  $this->accountancy_code = $obj->accountancy_code;
212  }
213  $this->db->free($resql);
214 
215  if ($numrows) {
216  return 1;
217  } else {
218  return 0;
219  }
220  } else {
221  $this->errors[] = 'Error '.$this->db->lasterror();
222  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
223 
224  return -1;
225  }
226  }
227 
236  public function update(User $user, $notrigger = false)
237  {
238  $error = 0;
239 
240  dol_syslog(__METHOD__, LOG_DEBUG);
241 
242  // Clean parameters
243 
244  $this->trimParameters(
245  array(
246  'libelle',
247  'deductible',
248  'active',
249  'code',
250  'fk_pays',
251  'module',
252  'accountancy_code',
253  )
254  );
255 
256 
257  // Check parameters
258  // Put here code to add a control on parameters values
259 
260  // Update request
261  $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
262  $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
263  $sql .= ' deductible = '.(isset($this->deductible) ? ((int) $this->deductible) : "null").',';
264  $sql .= ' active = '.(isset($this->active) ? ((int) $this->active) : "null").',';
265  $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
266  $sql .= ' fk_pays = '.((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null").',';
267  $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null").',';
268  $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null");
269  $sql .= ' WHERE id='.((int) $this->id);
270 
271  $this->db->begin();
272 
273  $resql = $this->db->query($sql);
274  if (!$resql) {
275  $error++;
276  $this->errors[] = 'Error '.$this->db->lasterror();
277  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
278  }
279 
280  //if (!$error && !$notrigger) {
281  // Uncomment this and change MYOBJECT to your own tag if you
282  // want this action calls a trigger.
283 
285  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
286  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
288  //}
289 
290  // Commit or rollback
291  if ($error) {
292  $this->db->rollback();
293 
294  return -1 * $error;
295  } else {
296  $this->db->commit();
297 
298  return 1;
299  }
300  }
301 
310  public function delete(User $user, $notrigger = false)
311  {
312  dol_syslog(__METHOD__, LOG_DEBUG);
313 
314  $error = 0;
315 
316  $this->db->begin();
317 
318  //if (!$error) {
319  //if (!$notrigger) {
320  // Uncomment this and change MYOBJECT to your own tag if you
321  // want this action calls a trigger.
322 
324  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
325  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
327  //}
328  //}
329 
330  if (!$error) {
331  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
332  $sql .= ' WHERE id = '.((int) $this->id);
333 
334  $resql = $this->db->query($sql);
335  if (!$resql) {
336  $error++;
337  $this->errors[] = 'Error '.$this->db->lasterror();
338  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
339  }
340  }
341 
342  // Commit or rollback
343  if ($error) {
344  $this->db->rollback();
345 
346  return -1 * $error;
347  } else {
348  $this->db->commit();
349 
350  return 1;
351  }
352  }
353 
361  public function createFromClone(User $user, $fromid)
362  {
363  dol_syslog(__METHOD__, LOG_DEBUG);
364 
365  $error = 0;
366  $object = new Cchargesociales($this->db);
367 
368  $this->db->begin();
369 
370  // Load source object
371  $object->fetch($fromid);
372  // Reset object
373  $object->id = 0;
374 
375  // Clear fields
376  // ...
377 
378  // Create clone
379  $this->context['createfromclone'] = 'createfromclone';
380  $result = $object->create($user);
381 
382  // Other options
383  if ($result < 0) {
384  $error++;
385  $this->errors = $object->errors;
386  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
387  }
388 
389  unset($this->context['createfromclone']);
390 
391  // End
392  if (!$error) {
393  $this->db->commit();
394 
395  return $object->id;
396  } else {
397  $this->db->rollback();
398 
399  return -1;
400  }
401  }
402 
414  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
415  {
416  global $langs, $conf, $db;
417  global $dolibarr_main_authentication, $dolibarr_main_demo;
418  global $menumanager;
419 
420 
421  $result = '';
422  $companylink = '';
423 
424  $label = '<u>'.$langs->trans("MyModule").'</u>';
425  $label .= '<div width="100%">';
426  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
427 
428  $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
429  $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
430  $link .= '>';
431  $linkend = '</a>';
432 
433  if ($withpicto) {
434  $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
435  if ($withpicto != 2) {
436  $result .= ' ';
437  }
438  }
439  $result .= $link.$this->ref.$linkend;
440  return $result;
441  }
442 
449  public function getLibStatut($mode = 0)
450  {
451  return $this->LibStatut($this->status, $mode);
452  }
453 
454  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
462  public function LibStatut($status, $mode = 0)
463  {
464  // phpcs:enable
465  global $langs;
466 
467  if ($mode == 0) {
468  if ($status == 1) {
469  return $langs->trans('Enabled');
470  } elseif ($status == 0) {
471  return $langs->trans('Disabled');
472  }
473  } elseif ($mode == 1) {
474  if ($status == 1) {
475  return $langs->trans('Enabled');
476  } elseif ($status == 0) {
477  return $langs->trans('Disabled');
478  }
479  } elseif ($mode == 2) {
480  if ($status == 1) {
481  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
482  } elseif ($status == 0) {
483  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
484  }
485  } elseif ($mode == 3) {
486  if ($status == 1) {
487  return img_picto($langs->trans('Enabled'), 'statut4');
488  } elseif ($status == 0) {
489  return img_picto($langs->trans('Disabled'), 'statut5');
490  }
491  } elseif ($mode == 4) {
492  if ($status == 1) {
493  return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
494  } elseif ($status == 0) {
495  return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
496  }
497  } elseif ($mode == 5) {
498  if ($status == 1) {
499  return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
500  } elseif ($status == 0) {
501  return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
502  }
503  }
504  return "";
505  }
506 
507 
514  public function initAsSpecimen()
515  {
516  $this->id = 0;
517 
518  $this->libelle = '';
519  $this->label = '';
520  $this->deductible = '';
521  $this->active = '';
522  $this->code = '';
523  $this->fk_pays = '';
524  $this->module = '';
525  $this->accountancy_code = '';
526  }
527 
534  private function trimParameters($parameters)
535  {
536  foreach ($parameters as $parameter) {
537  if (isset($this->$parameter)) {
538  $this->$parameter = trim($this->$parameter);
539  }
540  }
541  }
542 }
Class Cchargesociales.
fetch($id, $ref=null)
Load object in memory from the database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
getLibStatut($mode=0)
Return the label of the status.
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,...
LibStatut($status, $mode=0)
Return the label of a given status.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
trimParameters($parameters)
Trim object parameters.
update(User $user, $notrigger=false)
Update object into database.
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=false)
Create object into database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...