dolibarr 19.0.3
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{
37 public $db;
38
39 public $id;
40
44 public $element = 'cchargesociales';
45
49 public $table_element = 'c_chargesociales';
50
55 public $libelle;
56
60 public $label;
61
62 public $deductible;
63 public $active;
64 public $code;
65
69 public $fk_pays;
70
74 public $module;
75 public $accountancy_code;
76
80 public $errors = array();
81
87 public function __construct(DoliDB $db)
88 {
89 $this->db = $db;
90 }
91
100 public function create(User $user, $notrigger = false)
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__.' '.join(',', $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__.' '.join(',', $this->errors), LOG_ERR);
231
232 return -1;
233 }
234 }
235
244 public function update(User $user, $notrigger = false)
245 {
246 $error = 0;
247
248 dol_syslog(__METHOD__, LOG_DEBUG);
249
250 // Clean parameters
251
252 $this->trimParameters(
253 array(
254 'libelle',
255 'deductible',
256 'active',
257 'code',
258 'fk_pays',
259 'module',
260 'accountancy_code',
261 )
262 );
263
264
265 // Check parameters
266 // Put here code to add a control on parameters values
267
268 // Update request
269 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
270 $sql .= ' libelle = '.(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").',';
271 $sql .= ' deductible = '.(isset($this->deductible) ? ((int) $this->deductible) : "null").',';
272 $sql .= ' active = '.(isset($this->active) ? ((int) $this->active) : "null").',';
273 $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
274 $sql .= ' fk_pays = '.((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null").',';
275 $sql .= ' module = '.(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null").',';
276 $sql .= ' accountancy_code = '.(isset($this->accountancy_code) ? "'".$this->db->escape($this->accountancy_code)."'" : "null");
277 $sql .= ' WHERE id='.((int) $this->id);
278
279 $this->db->begin();
280
281 $resql = $this->db->query($sql);
282 if (!$resql) {
283 $error++;
284 $this->errors[] = 'Error '.$this->db->lasterror();
285 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
286 }
287
288 //if (!$error && !$notrigger) {
289 // Uncomment this and change MYOBJECT to your own tag if you
290 // want this action calls a trigger.
291
293 //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
294 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
296 //}
297
298 // Commit or rollback
299 if ($error) {
300 $this->db->rollback();
301
302 return -1 * $error;
303 } else {
304 $this->db->commit();
305
306 return 1;
307 }
308 }
309
318 public function delete(User $user, $notrigger = false)
319 {
320 dol_syslog(__METHOD__, LOG_DEBUG);
321
322 $error = 0;
323
324 $this->db->begin();
325
326 //if (!$error) {
327 //if (!$notrigger) {
328 // Uncomment this and change MYOBJECT to your own tag if you
329 // want this action calls a trigger.
330
332 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
333 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
335 //}
336 //}
337
338 if (!$error) {
339 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
340 $sql .= ' WHERE id = '.((int) $this->id);
341
342 $resql = $this->db->query($sql);
343 if (!$resql) {
344 $error++;
345 $this->errors[] = 'Error '.$this->db->lasterror();
346 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
347 }
348 }
349
350 // Commit or rollback
351 if ($error) {
352 $this->db->rollback();
353
354 return -1 * $error;
355 } else {
356 $this->db->commit();
357
358 return 1;
359 }
360 }
361
369 /*public function createFromClone(User $user, $fromid)
370 {
371 dol_syslog(__METHOD__, LOG_DEBUG);
372
373 $error = 0;
374 $object = new Cchargesociales($this->db);
375
376 $this->db->begin();
377
378 // Load source object
379 $object->fetch($fromid);
380 // Reset object
381 $object->id = 0;
382
383 // Clear fields
384 // ...
385
386 // Create clone
387 $this->context['createfromclone'] = 'createfromclone';
388 $result = $object->create($user);
389
390 // Other options
391 if ($result < 0) {
392 $error++;
393 $this->errors = $object->errors;
394 dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
395 }
396
397 unset($this->context['createfromclone']);
398
399 // End
400 if (!$error) {
401 $this->db->commit();
402
403 return $object->id;
404 } else {
405 $this->db->rollback();
406
407 return -1;
408 }
409 }*/
410
422 /*public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
423 {
424 global $langs, $conf, $db;
425 global $dolibarr_main_authentication, $dolibarr_main_demo;
426 global $menumanager;
427
428
429 $result = '';
430 $companylink = '';
431
432 $label = '<u>'.$langs->trans("MyModule").'</u>';
433 $label .= '<div width="100%">';
434 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
435
436 $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
437 $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
438 $link .= '>';
439 $linkend = '</a>';
440
441 if ($withpicto) {
442 $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
443 if ($withpicto != 2) {
444 $result .= ' ';
445 }
446 }
447 $result .= $link.$this->ref.$linkend;
448 return $result;
449 }*/
450
457 /*public function getLibStatut($mode = 0)
458 {
459 return $this->LibStatut($this->status, $mode);
460 }*/
461
462 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
470 public function LibStatut($status, $mode = 0)
471 {
472 // phpcs:enable
473 global $langs;
474
475 if ($mode == 0) {
476 if ($status == 1) {
477 return $langs->trans('Enabled');
478 } elseif ($status == 0) {
479 return $langs->trans('Disabled');
480 }
481 } elseif ($mode == 1) {
482 if ($status == 1) {
483 return $langs->trans('Enabled');
484 } elseif ($status == 0) {
485 return $langs->trans('Disabled');
486 }
487 } elseif ($mode == 2) {
488 if ($status == 1) {
489 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
490 } elseif ($status == 0) {
491 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
492 }
493 } elseif ($mode == 3) {
494 if ($status == 1) {
495 return img_picto($langs->trans('Enabled'), 'statut4');
496 } elseif ($status == 0) {
497 return img_picto($langs->trans('Disabled'), 'statut5');
498 }
499 } elseif ($mode == 4) {
500 if ($status == 1) {
501 return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
502 } elseif ($status == 0) {
503 return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
504 }
505 } elseif ($mode == 5) {
506 if ($status == 1) {
507 return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
508 } elseif ($status == 0) {
509 return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
510 }
511 }
512 return "";
513 }
514
515
522 public function initAsSpecimen()
523 {
524 $this->id = 0;
525
526 $this->libelle = '';
527 $this->label = '';
528 $this->deductible = '';
529 $this->active = '';
530 $this->code = '';
531 $this->fk_pays = '';
532 $this->module = '';
533 $this->accountancy_code = '';
534 }
535
542 private function trimParameters($parameters)
543 {
544 foreach ($parameters as $parameter) {
545 if (isset($this->$parameter)) {
546 $this->$parameter = trim($this->$parameter);
547 }
548 }
549 }
550}
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.
LibStatut($status, $mode=0)
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.
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.