dolibarr 21.0.0-alpha
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Put here all includes required by your class file
29//require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
30//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
31//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
32
37{
38 public $db;
39
40 public $id;
41
45 public $element = 'cchargesociales';
46
50 public $table_element = 'c_chargesociales';
51
56 public $libelle;
57
61 public $label;
62
63 public $deductible;
64 public $active;
65 public $code;
66
70 public $fk_pays;
71
75 public $module;
76 public $accountancy_code;
77
81 public $errors = array();
82
88 public function __construct(DoliDB $db)
89 {
90 $this->db = $db;
91 }
92
100 public function create(User $user, $notrigger = 0)
101 {
102 dol_syslog(__METHOD__, LOG_DEBUG);
103
104 $error = 0;
105
106 // Clean parameters
107 $this->trimParameters(
108 array(
109 'libelle',
110 'deductible',
111 'active',
112 'code',
113 'fk_pays',
114 'module',
115 'accountancy_code',
116 )
117 );
118
119 // Check parameters
120 // Put here code to add control on parameters values
121
122 // Insert request
123 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
124 $sql .= 'libelle,';
125 $sql .= 'deductible,';
126 $sql .= 'active,';
127 $sql .= 'code,';
128 $sql .= 'fk_pays,';
129 $sql .= 'module';
130 $sql .= 'accountancy_code';
131 $sql .= ') VALUES (';
132 $sql .= ' '.(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").',';
133 $sql .= ' '.(!isset($this->deductible) ? 'NULL' : $this->deductible).',';
134 $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active).',';
135 $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
136 $sql .= ' '.(!isset($this->fk_pays) ? 'NULL' : $this->fk_pays).',';
137 $sql .= ' '.(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'").',';
138 $sql .= ' '.(!isset($this->accountancy_code) ? 'NULL' : "'".$this->db->escape($this->accountancy_code)."'");
139 $sql .= ')';
140
141 $this->db->begin();
142
143 $resql = $this->db->query($sql);
144 if (!$resql) {
145 $error++;
146 $this->errors[] = 'Error '.$this->db->lasterror();
147 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
148 }
149
150 if (!$error) {
151 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
152
153 //if (!$notrigger) {
154 // Uncomment this and change MYOBJECT to your own tag if you
155 // want this action to call a trigger.
156
158 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
159 //if ($result < 0) $error++;
161 //}
162 }
163
164 // Commit or rollback
165 if ($error) {
166 $this->db->rollback();
167
168 return -1 * $error;
169 } else {
170 $this->db->commit();
171
172 return $this->id;
173 }
174 }
175
184 public function fetch($id, $ref = null)
185 {
186 dol_syslog(__METHOD__, LOG_DEBUG);
187
188 $sql = 'SELECT';
189 $sql .= " t.id,";
190 $sql .= " t.libelle as label,";
191 $sql .= " t.deductible,";
192 $sql .= " t.active,";
193 $sql .= " t.code,";
194 $sql .= " t.fk_pays,";
195 $sql .= " t.module,";
196 $sql .= " t.accountancy_code";
197 $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
198 if (null !== $ref) {
199 $sql .= " WHERE t.code = '".$this->db->escape($ref)."'";
200 } else {
201 $sql .= ' WHERE t.id = '.((int) $id);
202 }
203
204 $resql = $this->db->query($sql);
205 if ($resql) {
206 $numrows = $this->db->num_rows($resql);
207 if ($numrows) {
208 $obj = $this->db->fetch_object($resql);
209
210 $this->id = $obj->id;
211
212 $this->libelle = $obj->label;
213 $this->label = $obj->label;
214 $this->deductible = $obj->deductible;
215 $this->active = $obj->active;
216 $this->code = $obj->code;
217 $this->fk_pays = $obj->fk_pays;
218 $this->module = $obj->module;
219 $this->accountancy_code = $obj->accountancy_code;
220 }
221 $this->db->free($resql);
222
223 if ($numrows) {
224 return 1;
225 } else {
226 return 0;
227 }
228 } else {
229 $this->errors[] = 'Error '.$this->db->lasterror();
230 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
231
232 return -1;
233 }
234 }
235
243 public function update(User $user, $notrigger = 0)
244 {
245 $error = 0;
246
247 dol_syslog(__METHOD__, LOG_DEBUG);
248
249 // Clean parameters
250
251 $this->trimParameters(
252 array(
253 'libelle',
254 'deductible',
255 'active',
256 'code',
257 'fk_pays',
258 'module',
259 'accountancy_code',
260 )
261 );
262
263
264 // Check parameters
265 // Put here code to add a control on parameters values
266
267 // Update request
268 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
269 $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
270 $sql .= ' deductible = '.(isset($this->deductible) ? ((int) $this->deductible) : "null").',';
271 $sql .= ' active = '.(isset($this->active) ? ((int) $this->active) : "null").',';
272 $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
273 $sql .= ' fk_pays = '.((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null").',';
274 $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null").',';
275 $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null");
276 $sql .= ' WHERE id='.((int) $this->id);
277
278 $this->db->begin();
279
280 $resql = $this->db->query($sql);
281 if (!$resql) {
282 $error++;
283 $this->errors[] = 'Error '.$this->db->lasterror();
284 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
285 }
286
287 //if (!$error && !$notrigger) {
288 // Uncomment this and change MYOBJECT to your own tag if you
289 // want this action calls a trigger.
290
292 //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
293 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
295 //}
296
297 // Commit or rollback
298 if ($error) {
299 $this->db->rollback();
300
301 return -1 * $error;
302 } else {
303 $this->db->commit();
304
305 return 1;
306 }
307 }
308
316 public function delete(User $user, $notrigger = 0)
317 {
318 dol_syslog(__METHOD__, LOG_DEBUG);
319
320 $error = 0;
321
322 $this->db->begin();
323
324 //if (!$error) {
325 //if (!$notrigger) {
326 // Uncomment this and change MYOBJECT to your own tag if you
327 // want this action calls a trigger.
328
330 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
331 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
333 //}
334 //}
335
336 if (!$error) {
337 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
338 $sql .= ' WHERE id = '.((int) $this->id);
339
340 $resql = $this->db->query($sql);
341 if (!$resql) {
342 $error++;
343 $this->errors[] = 'Error '.$this->db->lasterror();
344 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
345 }
346 }
347
348 // Commit or rollback
349 if ($error) {
350 $this->db->rollback();
351
352 return -1 * $error;
353 } else {
354 $this->db->commit();
355
356 return 1;
357 }
358 }
359
367 /*public function createFromClone(User $user, $fromid)
368 {
369 dol_syslog(__METHOD__, LOG_DEBUG);
370
371 $error = 0;
372 $object = new Cchargesociales($this->db);
373
374 $this->db->begin();
375
376 // Load source object
377 $object->fetch($fromid);
378 // Reset object
379 $object->id = 0;
380
381 // Clear fields
382 // ...
383
384 // Create clone
385 $this->context['createfromclone'] = 'createfromclone';
386 $result = $object->create($user);
387
388 // Other options
389 if ($result < 0) {
390 $error++;
391 $this->errors = $object->errors;
392 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
393 }
394
395 unset($this->context['createfromclone']);
396
397 // End
398 if (!$error) {
399 $this->db->commit();
400
401 return $object->id;
402 } else {
403 $this->db->rollback();
404
405 return -1;
406 }
407 }*/
408
420 /*public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
421 {
422 global $langs, $conf, $db;
423 global $dolibarr_main_authentication, $dolibarr_main_demo;
424 global $menumanager;
425
426
427 $result = '';
428 $companylink = '';
429
430 $label = '<u>'.$langs->trans("MyModule").'</u>';
431 $label .= '<div width="100%">';
432 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
433
434 $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
435 $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
436 $link .= '>';
437 $linkend = '</a>';
438
439 if ($withpicto) {
440 $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
441 if ($withpicto != 2) {
442 $result .= ' ';
443 }
444 }
445 $result .= $link.$this->ref.$linkend;
446 return $result;
447 }*/
448
455 /*public function getLibStatut($mode = 0)
456 {
457 return $this->LibStatut($this->status, $mode);
458 }*/
459
460 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
468 public function LibStatut($status, $mode = 0)
469 {
470 // phpcs:enable
471 global $langs;
472
473 if ($mode == 0) {
474 if ($status == 1) {
475 return $langs->trans('Enabled');
476 } elseif ($status == 0) {
477 return $langs->trans('Disabled');
478 }
479 } elseif ($mode == 1) {
480 if ($status == 1) {
481 return $langs->trans('Enabled');
482 } elseif ($status == 0) {
483 return $langs->trans('Disabled');
484 }
485 } elseif ($mode == 2) {
486 if ($status == 1) {
487 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
488 } elseif ($status == 0) {
489 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
490 }
491 } elseif ($mode == 3) {
492 if ($status == 1) {
493 return img_picto($langs->trans('Enabled'), 'statut4');
494 } elseif ($status == 0) {
495 return img_picto($langs->trans('Disabled'), 'statut5');
496 }
497 } elseif ($mode == 4) {
498 if ($status == 1) {
499 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
500 } elseif ($status == 0) {
501 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
502 }
503 } elseif ($mode == 5) {
504 if ($status == 1) {
505 return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
506 } elseif ($status == 0) {
507 return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
508 }
509 }
510 return "";
511 }
512
513
520 public function initAsSpecimen()
521 {
522 $this->id = 0;
523 $this->libelle = '';
524 $this->label = '';
525 $this->deductible = '';
526 $this->active = '';
527 $this->code = '';
528 $this->fk_pays = 0;
529 $this->module = '';
530 $this->accountancy_code = '';
531
532 return 1;
533 }
534
541 private function trimParameters($parameters)
542 {
543 foreach ($parameters as $parameter) {
544 if (isset($this->$parameter)) {
545 $this->$parameter = trim($this->$parameter);
546 }
547 }
548 }
549}
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.