dolibarr 21.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 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29// Put here all includes required by your class file
30//require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
31//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
32//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
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 'active',
131 'code',
132 'fk_pays',
133 'module',
134 'accountancy_code',
135 )
136 );
137
138 // Check parameters
139 // Put here code to add control on parameters values
140
141 // Insert request
142 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
143 $sql .= 'libelle,';
144 $sql .= 'deductible,';
145 $sql .= 'active,';
146 $sql .= 'code,';
147 $sql .= 'fk_pays,';
148 $sql .= 'module';
149 $sql .= 'accountancy_code';
150 $sql .= ') VALUES (';
151 $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
152 $sql .= ' '.(!isset($this->deductible) ? 'NULL' : $this->deductible).',';
153 $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
154 $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
155 $sql .= ' '.(!isset($this->fk_pays) ? 'NULL' : $this->fk_pays).',';
156 $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'").',';
157 $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'");
158 $sql .= ')';
159
160 $this->db->begin();
161
162 $resql = $this->db->query($sql);
163 if (!$resql) {
164 $error++;
165 $this->errors[] = 'Error '.$this->db->lasterror();
166 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
167 }
168
169 if (!$error) {
170 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
171
172 //if (!$notrigger) {
173 // Uncomment this and change MYOBJECT to your own tag if you
174 // want this action to call a trigger.
175
177 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
178 //if ($result < 0) $error++;
180 //}
181 }
182
183 // Commit or rollback
184 if ($error) {
185 $this->db->rollback();
186
187 return -1 * $error;
188 } else {
189 $this->db->commit();
190
191 return $this->id;
192 }
193 }
194
203 public function fetch($id, $ref = null)
204 {
205 dol_syslog(__METHOD__, LOG_DEBUG);
206
207 $sql = 'SELECT';
208 $sql .= " t.id,";
209 $sql .= " t.libelle as label,";
210 $sql .= " t.deductible,";
211 $sql .= " t.active,";
212 $sql .= " t.code,";
213 $sql .= " t.fk_pays,";
214 $sql .= " t.module,";
215 $sql .= " t.accountancy_code";
216 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
217 if (null !== $ref) {
218 $sql .= " WHERE t.code = '".$this->db->escape($ref)."'";
219 } else {
220 $sql .= ' WHERE t.id = '.((int) $id);
221 }
222
223 $resql = $this->db->query($sql);
224 if ($resql) {
225 $numrows = $this->db->num_rows($resql);
226 if ($numrows) {
227 $obj = $this->db->fetch_object($resql);
228
229 $this->id = $obj->id;
230
231 $this->libelle = $obj->label;
232 $this->label = $obj->label;
233 $this->deductible = $obj->deductible;
234 $this->active = $obj->active;
235 $this->code = $obj->code;
236 $this->fk_pays = $obj->fk_pays;
237 $this->module = $obj->module;
238 $this->accountancy_code = $obj->accountancy_code;
239 }
240 $this->db->free($resql);
241
242 if ($numrows) {
243 return 1;
244 } else {
245 return 0;
246 }
247 } else {
248 $this->errors[] = 'Error '.$this->db->lasterror();
249 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
250
251 return -1;
252 }
253 }
254
262 public function update(User $user, $notrigger = 0)
263 {
264 $error = 0;
265
266 dol_syslog(__METHOD__, LOG_DEBUG);
267
268 // Clean parameters
269
270 $this->trimParameters(
271 array(
272 'libelle',
273 'deductible',
274 'active',
275 'code',
276 'fk_pays',
277 'module',
278 'accountancy_code',
279 )
280 );
281
282
283 // Check parameters
284 // Put here code to add a control on parameters values
285
286 // Update request
287 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
288 $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
289 $sql .= ' deductible = '.(isset($this->deductible) ? ((int) $this->deductible) : "null").',';
290 $sql .= ' active = '.(isset($this->active) ? ((int) $this->active) : "null").',';
291 $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
292 $sql .= ' fk_pays = '.((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null").',';
293 $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null").',';
294 $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null");
295 $sql .= ' WHERE id='.((int) $this->id);
296
297 $this->db->begin();
298
299 $resql = $this->db->query($sql);
300 if (!$resql) {
301 $error++;
302 $this->errors[] = 'Error '.$this->db->lasterror();
303 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
304 }
305
306 //if (!$error && !$notrigger) {
307 // Uncomment this and change MYOBJECT to your own tag if you
308 // want this action calls a trigger.
309
311 //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
312 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
314 //}
315
316 // Commit or rollback
317 if ($error) {
318 $this->db->rollback();
319
320 return -1 * $error;
321 } else {
322 $this->db->commit();
323
324 return 1;
325 }
326 }
327
335 public function delete(User $user, $notrigger = 0)
336 {
337 dol_syslog(__METHOD__, LOG_DEBUG);
338
339 $error = 0;
340
341 $this->db->begin();
342
343 //if (!$error) {
344 //if (!$notrigger) {
345 // Uncomment this and change MYOBJECT to your own tag if you
346 // want this action calls a trigger.
347
349 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
350 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
352 //}
353 //}
354
355 if (!$error) {
356 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
357 $sql .= ' WHERE id = '.((int) $this->id);
358
359 $resql = $this->db->query($sql);
360 if (!$resql) {
361 $error++;
362 $this->errors[] = 'Error '.$this->db->lasterror();
363 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
364 }
365 }
366
367 // Commit or rollback
368 if ($error) {
369 $this->db->rollback();
370
371 return -1 * $error;
372 } else {
373 $this->db->commit();
374
375 return 1;
376 }
377 }
378
386 /*public function createFromClone(User $user, $fromid)
387 {
388 dol_syslog(__METHOD__, LOG_DEBUG);
389
390 $error = 0;
391 $object = new Cchargesociales($this->db);
392
393 $this->db->begin();
394
395 // Load source object
396 $object->fetch($fromid);
397 // Reset object
398 $object->id = 0;
399
400 // Clear fields
401 // ...
402
403 // Create clone
404 $this->context['createfromclone'] = 'createfromclone';
405 $result = $object->create($user);
406
407 // Other options
408 if ($result < 0) {
409 $error++;
410 $this->errors = $object->errors;
411 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
412 }
413
414 unset($this->context['createfromclone']);
415
416 // End
417 if (!$error) {
418 $this->db->commit();
419
420 return $object->id;
421 } else {
422 $this->db->rollback();
423
424 return -1;
425 }
426 }*/
427
439 /*public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
440 {
441 global $langs, $conf, $db;
442 global $dolibarr_main_authentication, $dolibarr_main_demo;
443 global $menumanager;
444
445
446 $result = '';
447 $companylink = '';
448
449 $label = '<u>'.$langs->trans("MyModule").'</u>';
450 $label .= '<div width="100%">';
451 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
452
453 $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
454 $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
455 $link .= '>';
456 $linkend = '</a>';
457
458 if ($withpicto) {
459 $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
460 if ($withpicto != 2) {
461 $result .= ' ';
462 }
463 }
464 $result .= $link.$this->ref.$linkend;
465 return $result;
466 }*/
467
474 /*public function getLibStatut($mode = 0)
475 {
476 return $this->LibStatut($this->status, $mode);
477 }*/
478
479 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
487 public function LibStatut($status, $mode = 0)
488 {
489 // phpcs:enable
490 global $langs;
491
492 if ($mode == 0) {
493 if ($status == 1) {
494 return $langs->trans('Enabled');
495 } elseif ($status == 0) {
496 return $langs->trans('Disabled');
497 }
498 } elseif ($mode == 1) {
499 if ($status == 1) {
500 return $langs->trans('Enabled');
501 } elseif ($status == 0) {
502 return $langs->trans('Disabled');
503 }
504 } elseif ($mode == 2) {
505 if ($status == 1) {
506 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
507 } elseif ($status == 0) {
508 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
509 }
510 } elseif ($mode == 3) {
511 if ($status == 1) {
512 return img_picto($langs->trans('Enabled'), 'statut4');
513 } elseif ($status == 0) {
514 return img_picto($langs->trans('Disabled'), 'statut5');
515 }
516 } elseif ($mode == 4) {
517 if ($status == 1) {
518 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
519 } elseif ($status == 0) {
520 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
521 }
522 } elseif ($mode == 5) {
523 if ($status == 1) {
524 return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
525 } elseif ($status == 0) {
526 return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
527 }
528 }
529 return "";
530 }
531
532
539 public function initAsSpecimen()
540 {
541 $this->id = 0;
542 $this->libelle = '';
543 $this->label = '';
544 $this->deductible = '';
545 $this->active = '';
546 $this->code = '';
547 $this->fk_pays = 0;
548 $this->module = '';
549 $this->accountancy_code = '';
550
551 return 1;
552 }
553
560 private function trimParameters($parameters)
561 {
562 foreach ($parameters as $parameter) {
563 if (isset($this->$parameter)) {
564 $this->$parameter = trim($this->$parameter);
565 }
566 }
567 }
568}
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $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.