Passed
Push — GENERAL_BUG_REVIEW_240911 ( 3362b2...8cbbee )
by Rafael
49:13
created

Cchargesociales::LibStatut()   D

Complexity

Conditions 19
Paths 19

Size

Total Lines 43
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 32
nc 19
nop 2
dl 0
loc 43
rs 4.5166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2016       Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2014       Juanjo Menent               <[email protected]>
5
 * Copyright (C) 2015       Florian Henry               <[email protected]>
6
 * Copyright (C) 2015       Raphaël Doursenaud          <[email protected]>
7
 * Copyright (C) 2024       Frédéric France             <[email protected]>
8
 * Copyright (C) 2024       Rafael San José             <[email protected]>
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
24
namespace Dolibarr\Code\Compta\Classes;
25
26
/**
27
 * \file    htdocs/compta/sociales/class/cchargesociales.class.php
28
 * \ingroup tax
29
 * \brief   File to manage type of social/fiscal taxes
30
 */
31
32
// Put here all includes required by your class file
33
//use Dolibarr\Core\Base\CommonObject;
34
//use Dolibarr\Code\Societe\Classes\Societe;
35
//
36
/**
37
 * Class Cchargesociales
38
 */
39
class Cchargesociales
40
{
41
    public $db;
42
43
    public $id;
44
45
    /**
46
     * @var string Id to identify managed objects
47
     */
48
    public $element = 'cchargesociales';
49
50
    /**
51
     * @var string Name of table without prefix where object is stored
52
     */
53
    public $table_element = 'c_chargesociales';
54
55
    /**
56
     * @var string Label
57
     * @deprecated
58
     */
59
    public $libelle;
60
61
    /**
62
     * @var string Label
63
     */
64
    public $label;
65
66
    public $deductible;
67
    public $active;
68
    public $code;
69
70
    /**
71
     * @var int ID
72
     */
73
    public $fk_pays;
74
75
    /**
76
     * @var string module
77
     */
78
    public $module;
79
    public $accountancy_code;
80
81
    /**
82
     * @var array array of errors
83
     */
84
    public $errors = array();
85
86
    /**
87
     * Constructor
88
     *
89
     * @param DoliDB $db Database handler
90
     */
91
    public function __construct(DoliDB $db)
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Compta\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
92
    {
93
        $this->db = $db;
94
    }
95
96
    /**
97
     * Create object into database
98
     *
99
     * @param  User $user       User that creates
100
     * @param  int  $notrigger  0=launch triggers after, 1=disable triggers
101
     * @return int              Return integer <0 if KO, Id of created object if OK
102
     */
103
    public function create(User $user, $notrigger = 0)
104
    {
105
        dol_syslog(__METHOD__, LOG_DEBUG);
106
107
        $error = 0;
108
109
        // Clean parameters
110
        $this->trimParameters(
111
            array(
112
                'libelle',
113
                'deductible',
114
                'active',
115
                'code',
116
                'fk_pays',
117
                'module',
118
                'accountancy_code',
119
            )
120
        );
121
122
        // Check parameters
123
        // Put here code to add control on parameters values
124
125
        // Insert request
126
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
127
        $sql .= 'libelle,';
128
        $sql .= 'deductible,';
129
        $sql .= 'active,';
130
        $sql .= 'code,';
131
        $sql .= 'fk_pays,';
132
        $sql .= 'module';
133
        $sql .= 'accountancy_code';
134
        $sql .= ') VALUES (';
135
        $sql .= ' ' . (!isset($this->libelle) ? 'NULL' : "'" . $this->db->escape($this->libelle) . "'") . ',';
136
        $sql .= ' ' . (!isset($this->deductible) ? 'NULL' : $this->deductible) . ',';
137
        $sql .= ' ' . (!isset($this->active) ? 'NULL' : $this->active) . ',';
138
        $sql .= ' ' . (!isset($this->code) ? 'NULL' : "'" . $this->db->escape($this->code) . "'") . ',';
139
        $sql .= ' ' . (!isset($this->fk_pays) ? 'NULL' : $this->fk_pays) . ',';
140
        $sql .= ' ' . (!isset($this->module) ? 'NULL' : "'" . $this->db->escape($this->module) . "'") . ',';
141
        $sql .= ' ' . (!isset($this->accountancy_code) ? 'NULL' : "'" . $this->db->escape($this->accountancy_code) . "'");
142
        $sql .= ')';
143
144
        $this->db->begin();
145
146
        $resql = $this->db->query($sql);
147
        if (!$resql) {
148
            $error++;
149
            $this->errors[] = 'Error ' . $this->db->lasterror();
150
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
151
        }
152
153
        if (!$error) {
154
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
155
156
            //if (!$notrigger) {
157
                // Uncomment this and change MYOBJECT to your own tag if you
158
                // want this action to call a trigger.
159
160
                //// Call triggers
161
                //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
162
                //if ($result < 0) $error++;
163
                //// End call triggers
164
            //}
165
        }
166
167
        // Commit or rollback
168
        if ($error) {
169
            $this->db->rollback();
170
171
            return -1 * $error;
172
        } else {
173
            $this->db->commit();
174
175
            return $this->id;
176
        }
177
    }
178
179
    /**
180
     * Load object in memory from the database
181
     *
182
     * @param int    $id  Id object
183
     * @param string $ref Ref
184
     *
185
     * @return int Return integer <0 if KO, 0 if not found, >0 if OK
186
     */
187
    public function fetch($id, $ref = null)
188
    {
189
        dol_syslog(__METHOD__, LOG_DEBUG);
190
191
        $sql = 'SELECT';
192
        $sql .= " t.id,";
193
        $sql .= " t.libelle as label,";
194
        $sql .= " t.deductible,";
195
        $sql .= " t.active,";
196
        $sql .= " t.code,";
197
        $sql .= " t.fk_pays,";
198
        $sql .= " t.module,";
199
        $sql .= " t.accountancy_code";
200
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
201
        if (null !== $ref) {
202
            $sql .= " WHERE t.code = '" . $this->db->escape($ref) . "'";
203
        } else {
204
            $sql .= ' WHERE t.id = ' . ((int) $id);
205
        }
206
207
        $resql = $this->db->query($sql);
208
        if ($resql) {
209
            $numrows = $this->db->num_rows($resql);
210
            if ($numrows) {
211
                $obj = $this->db->fetch_object($resql);
212
213
                $this->id = $obj->id;
214
215
                $this->libelle = $obj->label;
216
                $this->label = $obj->label;
217
                $this->deductible = $obj->deductible;
218
                $this->active = $obj->active;
219
                $this->code = $obj->code;
220
                $this->fk_pays = $obj->fk_pays;
221
                $this->module = $obj->module;
222
                $this->accountancy_code = $obj->accountancy_code;
223
            }
224
            $this->db->free($resql);
225
226
            if ($numrows) {
227
                return 1;
228
            } else {
229
                return 0;
230
            }
231
        } else {
232
            $this->errors[] = 'Error ' . $this->db->lasterror();
233
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
234
235
            return -1;
236
        }
237
    }
238
239
    /**
240
     * Update object into database
241
     *
242
     * @param  User $user       User that modifies
243
     * @param  int  $notrigger  0=launch triggers after, 1=disable triggers
244
     * @return int              Return integer <0 if KO, >0 if OK
245
     */
246
    public function update(User $user, $notrigger = 0)
247
    {
248
        $error = 0;
249
250
        dol_syslog(__METHOD__, LOG_DEBUG);
251
252
        // Clean parameters
253
254
        $this->trimParameters(
255
            array(
256
                'libelle',
257
                'deductible',
258
                'active',
259
                'code',
260
                'fk_pays',
261
                'module',
262
                'accountancy_code',
263
            )
264
        );
265
266
267
        // Check parameters
268
        // Put here code to add a control on parameters values
269
270
        // Update request
271
        $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
272
        $sql .= ' libelle = ' . (isset($this->libelle) ? "'" . $this->db->escape($this->libelle) . "'" : "null") . ',';
273
        $sql .= ' deductible = ' . (isset($this->deductible) ? ((int) $this->deductible) : "null") . ',';
274
        $sql .= ' active = ' . (isset($this->active) ? ((int) $this->active) : "null") . ',';
275
        $sql .= ' code = ' . (isset($this->code) ? "'" . $this->db->escape($this->code) . "'" : "null") . ',';
276
        $sql .= ' fk_pays = ' . ((isset($this->fk_pays) && $this->fk_pays > 0) ? ((int) $this->fk_pays) : "null") . ',';
277
        $sql .= ' module = ' . (isset($this->module) ? "'" . $this->db->escape($this->module) . "'" : "null") . ',';
278
        $sql .= ' accountancy_code = ' . (isset($this->accountancy_code) ? "'" . $this->db->escape($this->accountancy_code) . "'" : "null");
279
        $sql .= ' WHERE id=' . ((int) $this->id);
280
281
        $this->db->begin();
282
283
        $resql = $this->db->query($sql);
284
        if (!$resql) {
285
            $error++;
286
            $this->errors[] = 'Error ' . $this->db->lasterror();
287
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
288
        }
289
290
        //if (!$error && !$notrigger) {
291
        // Uncomment this and change MYOBJECT to your own tag if you
292
        // want this action calls a trigger.
293
294
        //// Call triggers
295
        //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
296
        //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
297
        //// End call triggers
298
        //}
299
300
        // Commit or rollback
301
        if ($error) {
302
            $this->db->rollback();
303
304
            return -1 * $error;
305
        } else {
306
            $this->db->commit();
307
308
            return 1;
309
        }
310
    }
311
312
    /**
313
     * Delete object in database
314
     *
315
     * @param User  $user       User that deletes
316
     * @param int   $notrigger  0=launch triggers after, 1=disable triggers
317
     * @return int              Return integer <0 if KO, >0 if OK
318
     */
319
    public function delete(User $user, $notrigger = 0)
320
    {
321
        dol_syslog(__METHOD__, LOG_DEBUG);
322
323
        $error = 0;
324
325
        $this->db->begin();
326
327
        //if (!$error) {
328
        //if (!$notrigger) {
329
        // Uncomment this and change MYOBJECT to your own tag if you
330
        // want this action calls a trigger.
331
332
        //// Call triggers
333
        //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
334
        //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
335
        //// End call triggers
336
        //}
337
        //}
338
339
        if (!$error) {
340
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
341
            $sql .= ' WHERE id = ' . ((int) $this->id);
342
343
            $resql = $this->db->query($sql);
344
            if (!$resql) {
345
                $error++;
346
                $this->errors[] = 'Error ' . $this->db->lasterror();
347
                dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
348
            }
349
        }
350
351
        // Commit or rollback
352
        if ($error) {
353
            $this->db->rollback();
354
355
            return -1 * $error;
356
        } else {
357
            $this->db->commit();
358
359
            return 1;
360
        }
361
    }
362
363
    /**
364
     * Load an object from its id and create a new one in database
365
     *
366
     * @param   User    $user       User making the clone
367
     * @param   int     $fromid     Id of object to clone
368
     * @return  int                 New id of clone
369
     */
370
    /*public function createFromClone(User $user, $fromid)
371
    {
372
        dol_syslog(__METHOD__, LOG_DEBUG);
373
374
        $error = 0;
375
        $object = new Cchargesociales($this->db);
376
377
        $this->db->begin();
378
379
        // Load source object
380
        $object->fetch($fromid);
381
        // Reset object
382
        $object->id = 0;
383
384
        // Clear fields
385
        // ...
386
387
        // Create clone
388
        $this->context['createfromclone'] = 'createfromclone';
389
        $result = $object->create($user);
390
391
        // Other options
392
        if ($result < 0) {
393
            $error++;
394
            $this->errors = $object->errors;
395
            dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
396
        }
397
398
        unset($this->context['createfromclone']);
399
400
        // End
401
        if (!$error) {
402
            $this->db->commit();
403
404
            return $object->id;
405
        } else {
406
            $this->db->rollback();
407
408
            return -1;
409
        }
410
    }*/
411
412
    /**
413
     *  Return a link to the user card (with optionally the picto)
414
     *  Use this->id,this->lastname, this->firstname
415
     *
416
     *  @param  int     $withpicto          Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
417
     *  @param  string  $option             On what the link point to
418
     *  @param  integer $notooltip          1=Disable tooltip
419
     *  @param  int     $maxlen             Max length of visible user name
420
     *  @param  string  $morecss            Add more css on link
421
     *  @return string                      String with URL
422
     */
423
    /*public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
424
    {
425
        global $langs, $conf, $db;
426
        global $dolibarr_main_authentication, $dolibarr_main_demo;
427
        global $menumanager;
428
429
430
        $result = '';
431
        $companylink = '';
432
433
        $label = '<u>'.$langs->trans("MyModule").'</u>';
434
        $label .= '<div width="100%">';
435
        $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
436
437
        $link = '<a href="'.DOL_URL_ROOT.'/tax/card.php?id='.$this->id.'"';
438
        $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
439
        $link .= '>';
440
        $linkend = '</a>';
441
442
        if ($withpicto) {
443
            $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
444
            if ($withpicto != 2) {
445
                $result .= ' ';
446
            }
447
        }
448
        $result .= $link.$this->ref.$linkend;
449
        return $result;
450
    }*/
451
452
    /**
453
     *  Return the label of the status
454
     *
455
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
456
     *  @return string                 Label of status
457
     */
458
    /*public function getLibStatut($mode = 0)
459
    {
460
        return $this->LibStatut($this->status, $mode);
461
    }*/
462
463
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
464
    /**
465
     *  Return the label of a given status
466
     *
467
     *  @param  int     $status        Id status
468
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
469
     *  @return string                 Label of status
470
     */
471
    public function LibStatut($status, $mode = 0)
472
    {
473
		// phpcs:enable
474
        global $langs;
475
476
        if ($mode == 0) {
477
            if ($status == 1) {
478
                return $langs->trans('Enabled');
479
            } elseif ($status == 0) {
480
                return $langs->trans('Disabled');
481
            }
482
        } elseif ($mode == 1) {
483
            if ($status == 1) {
484
                return $langs->trans('Enabled');
485
            } elseif ($status == 0) {
486
                return $langs->trans('Disabled');
487
            }
488
        } elseif ($mode == 2) {
489
            if ($status == 1) {
490
                return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled');
491
            } elseif ($status == 0) {
492
                return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled');
493
            }
494
        } elseif ($mode == 3) {
495
            if ($status == 1) {
496
                return img_picto($langs->trans('Enabled'), 'statut4');
497
            } elseif ($status == 0) {
498
                return img_picto($langs->trans('Disabled'), 'statut5');
499
            }
500
        } elseif ($mode == 4) {
501
            if ($status == 1) {
502
                return img_picto($langs->trans('Enabled'), 'statut4') . ' ' . $langs->trans('Enabled');
503
            } elseif ($status == 0) {
504
                return img_picto($langs->trans('Disabled'), 'statut5') . ' ' . $langs->trans('Disabled');
505
            }
506
        } elseif ($mode == 5) {
507
            if ($status == 1) {
508
                return $langs->trans('Enabled') . ' ' . img_picto($langs->trans('Enabled'), 'statut4');
509
            } elseif ($status == 0) {
510
                return $langs->trans('Disabled') . ' ' . img_picto($langs->trans('Disabled'), 'statut5');
511
            }
512
        }
513
        return "";
514
    }
515
516
517
    /**
518
     * Initialise object with example values
519
     * Id must be 0 if object instance is a specimen
520
     *
521
     * @return int
522
     */
523
    public function initAsSpecimen()
524
    {
525
        $this->id = 0;
526
        $this->libelle = '';
527
        $this->label = '';
528
        $this->deductible = '';
529
        $this->active = '';
530
        $this->code = '';
531
        $this->fk_pays = 0;
532
        $this->module = '';
533
        $this->accountancy_code = '';
534
535
        return 1;
536
    }
537
538
    /**
539
     * Trim object parameters
540
     *
541
     * @param string[] $parameters array of parameters to trim
542
     * @return void
543
     */
544
    private function trimParameters($parameters)
545
    {
546
        foreach ($parameters as $parameter) {
547
            if (isset($this->$parameter)) {
548
                $this->$parameter = trim($this->$parameter);
549
            }
550
        }
551
    }
552
}
553