dolibarr 24.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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2026 Vincent de Grandpré <vincent@de-grandpre.quebec>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Put here all includes required by your class file
31//require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
32
33
38{
42 public $db;
43
47 public $id;
48
52 public $element = 'cchargesociales';
53
57 public $table_element = 'c_chargesociales';
58
63 public $libelle;
64
68 public $label;
69
73 public $deductible;
77 public $active;
81 public $code;
82
86 public $fk_pays;
87
91 public $module;
95 public $accountancy_code;
96
100 public $errors = array();
101
107 public function __construct(DoliDB $db)
108 {
109 $this->db = $db;
110 }
111
119 public function create(User $user, $notrigger = 0)
120 {
121 dol_syslog(__METHOD__, LOG_DEBUG);
122
123 $error = 0;
124
125 // Clean parameters
126 $this->trimParameters(
127 array(
128 'libelle',
129 'deductible',
130 'code',
131 'module',
132 'accountancy_code',
133 )
134 );
135 if (isset($this->fk_pays)) {
136 $this->fk_pays = (int) $this->fk_pays;
137 }
138 if (isset($this->active)) {
139 $this->active = (int) $this->active;
140 }
141
142 // Check parameters
143 // Put here code to add control on parameters values
144
145 // Insert request
146 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
147 $sql .= 'libelle,';
148 $sql .= 'deductible,';
149 $sql .= 'active,';
150 $sql .= 'code,';
151 $sql .= 'fk_pays,';
152 $sql .= 'module,';
153 $sql .= 'accountancy_code';
154 $sql .= ') VALUES (';
155 $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
156 $sql .= ' '.(!isset($this->deductible) ? 'NULL' : $this->deductible).',';
157 $sql .= ' ' . (int) $this->active . ',';
158 $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
159 $sql .= ' '.(!isset($this->fk_pays) ? 'NULL' : $this->fk_pays).',';
160 $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'").',';
161 $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'");
162 $sql .= ')';
163
164 $this->db->begin();
165
166 $resql = $this->db->query($sql);
167 if (!$resql) {
168 $error++;
169 $this->errors[] = 'Error '.$this->db->lasterror();
170 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
171 }
172
173 if (!$error) {
174 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
175
176 //if (!$notrigger) {
177 // Uncomment this and change MYOBJECT to your own tag if you
178 // want this action to call a trigger.
179
181 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
182 //if ($result < 0) $error++;
184 //}
185 }
186
187 // Commit or rollback
188 if ($error) {
189 $this->db->rollback();
190
191 return -1 * $error;
192 } else {
193 $this->db->commit();
194
195 return $this->id;
196 }
197 }
198
207 public function fetch($id, $ref = null)
208 {
209 dol_syslog(__METHOD__, LOG_DEBUG);
210
211 $sql = 'SELECT';
212 $sql .= " t.id,";
213 $sql .= " t.libelle as label,";
214 $sql .= " t.deductible,";
215 $sql .= " t.active,";
216 $sql .= " t.code,";
217 $sql .= " t.fk_pays,";
218 $sql .= " t.module,";
219 $sql .= " t.accountancy_code";
220 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
221 if (null !== $ref) {
222 $sql .= " WHERE t.code = '".$this->db->escape($ref)."'";
223 } else {
224 $sql .= ' WHERE t.id = '.((int) $id);
225 }
226
227 $resql = $this->db->query($sql);
228 if ($resql) {
229 $numrows = $this->db->num_rows($resql);
230 if ($numrows) {
231 $obj = $this->db->fetch_object($resql);
232
233 $this->id = $obj->id;
234
235 $this->libelle = $obj->label;
236 $this->label = $obj->label;
237 $this->deductible = $obj->deductible;
238 $this->active = $obj->active;
239 $this->code = $obj->code;
240 $this->fk_pays = $obj->fk_pays;
241 $this->module = $obj->module;
242 $this->accountancy_code = $obj->accountancy_code;
243 }
244 $this->db->free($resql);
245
246 if ($numrows) {
247 return 1;
248 } else {
249 return 0;
250 }
251 } else {
252 $this->errors[] = 'Error '.$this->db->lasterror();
253 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
254
255 return -1;
256 }
257 }
258
266 public function update(User $user, $notrigger = 0)
267 {
268 $error = 0;
269
270 dol_syslog(__METHOD__, LOG_DEBUG);
271
272 // Clean parameters
273
274 $this->trimParameters(
275 array(
276 'libelle',
277 'deductible',
278 'code',
279 'module',
280 'accountancy_code',
281 )
282 );
283 if (isset($this->fk_pays)) {
284 $this->fk_pays = (int) $this->fk_pays;
285 }
286 if (isset($this->active)) {
287 $this->active = (int) $this->active;
288 }
289
290 // Check parameters
291 // Put here code to add a control on parameters values
292
293 // Update request
294 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
295 $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
296 $sql .= ' deductible = '.(isset($this->deductible) ? ((int) $this->deductible) : "null").',';
297 $sql .= ' active = '.(isset($this->active) ? ((int) $this->active) : "null").',';
298 $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
299 $sql .= ' fk_pays = '.((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null").',';
300 $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null").',';
301 $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null");
302 $sql .= ' WHERE id='.((int) $this->id);
303
304 $this->db->begin();
305
306 $resql = $this->db->query($sql);
307 if (!$resql) {
308 $error++;
309 $this->errors[] = 'Error '.$this->db->lasterror();
310 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
311 }
312
313 //if (!$error && !$notrigger) {
314 // Uncomment this and change MYOBJECT to your own tag if you
315 // want this action calls a trigger.
316
318 //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
319 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
321 //}
322
323 // Commit or rollback
324 if ($error) {
325 $this->db->rollback();
326
327 return -1 * $error;
328 } else {
329 $this->db->commit();
330
331 return 1;
332 }
333 }
334
342 public function delete(User $user, $notrigger = 0)
343 {
344 dol_syslog(__METHOD__, LOG_DEBUG);
345
346 $error = 0;
347
348 $this->db->begin();
349
350 //if (!$error) {
351 //if (!$notrigger) {
352 // Uncomment this and change MYOBJECT to your own tag if you
353 // want this action calls a trigger.
354
356 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
357 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
359 //}
360 //}
361
362 if (!$error) {
363 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
364 $sql .= ' WHERE id = '.((int) $this->id);
365
366 $resql = $this->db->query($sql);
367 if (!$resql) {
368 $error++;
369 $this->errors[] = 'Error '.$this->db->lasterror();
370 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
371 }
372 }
373
374 // Commit or rollback
375 if ($error) {
376 $this->db->rollback();
377
378 return -1 * $error;
379 } else {
380 $this->db->commit();
381
382 return 1;
383 }
384 }
385
393 /*public function createFromClone(User $user, $fromid)
394 {
395 dol_syslog(__METHOD__, LOG_DEBUG);
396
397 $error = 0;
398 $object = new Cchargesociales($this->db);
399
400 $this->db->begin();
401
402 // Load source object
403 $object->fetch($fromid);
404 // Reset object
405 $object->id = 0;
406
407 // Clear fields
408 // ...
409
410 // Create clone
411 $this->context['createfromclone'] = 'createfromclone';
412 $result = $object->create($user);
413
414 // Other options
415 if ($result < 0) {
416 $error++;
417 $this->errors = $object->errors;
418 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
419 }
420
421 unset($this->context['createfromclone']);
422
423 // End
424 if (!$error) {
425 $this->db->commit();
426
427 return $object->id;
428 } else {
429 $this->db->rollback();
430
431 return -1;
432 }
433 }*/
434
446 /*public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
447 {
448 global $langs, $conf, $db;
449 global $dolibarr_main_authentication, $dolibarr_main_demo;
450 global $menumanager;
451
452
453 $result = '';
454 $companylink = '';
455
456 $label = '<u>'.$langs->trans("MyModule").'</u>';
457 $label .= '<div width="100%">';
458 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
459
460 $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
461 $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
462 $link .= '>';
463 $linkend = '</a>';
464
465 if ($withpicto) {
466 $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
467 if ($withpicto != 2) {
468 $result .= ' ';
469 }
470 }
471 $result .= $link.$this->ref.$linkend;
472 return $result;
473 }*/
474
481 /*public function getLibStatut($mode = 0)
482 {
483 return $this->LibStatut($this->status, $mode);
484 }*/
485
486 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
494 public function LibStatut($status, $mode = 0)
495 {
496 // phpcs:enable
497 global $langs;
498
499 if ($mode == 0) {
500 if ($status == 1) {
501 return $langs->trans('Enabled');
502 } elseif ($status == 0) {
503 return $langs->trans('Disabled');
504 }
505 } elseif ($mode == 1) {
506 if ($status == 1) {
507 return $langs->trans('Enabled');
508 } elseif ($status == 0) {
509 return $langs->trans('Disabled');
510 }
511 } elseif ($mode == 2) {
512 if ($status == 1) {
513 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
514 } elseif ($status == 0) {
515 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
516 }
517 } elseif ($mode == 3) {
518 if ($status == 1) {
519 return img_picto($langs->trans('Enabled'), 'statut4');
520 } elseif ($status == 0) {
521 return img_picto($langs->trans('Disabled'), 'statut5');
522 }
523 } elseif ($mode == 4) {
524 if ($status == 1) {
525 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
526 } elseif ($status == 0) {
527 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
528 }
529 } elseif ($mode == 5) {
530 if ($status == 1) {
531 return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
532 } elseif ($status == 0) {
533 return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
534 }
535 }
536 return "";
537 }
538
539
546 public function initAsSpecimen()
547 {
548 $this->id = 0;
549 $this->libelle = '';
550 $this->label = '';
551 $this->deductible = '';
552 $this->active = 0;
553 $this->code = '';
554 $this->fk_pays = 0;
555 $this->module = '';
556 $this->accountancy_code = '';
557
558 return 1;
559 }
560
567 private function trimParameters($parameters)
568 {
569 foreach ($parameters as $parameter) {
570 if (isset($this->$parameter)) {
571 $this->$parameter = trim($this->$parameter);
572 }
573 }
574 }
575}
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.
create(User $user, $notrigger=0)
Create object into database.
update(User $user, $notrigger=0)
Update object into database.
LibStatut($status, $mode=0)
Load an object from its id and create a new one in database.
trimParameters($parameters)
Trim object parameters.
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
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.
editval_textarea active