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

Deplacement   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 433
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 204
dl 0
loc 433
rs 8.64
c 0
b 0
f 0
wmc 47

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getNomUrl() 0 23 5
C create() 0 67 12
A listOfTypes() 0 25 4
C update() 0 46 12
A getLibStatut() 0 3 1
A delete() 0 17 2
A LibStatut() 0 20 3
A fetch() 0 34 3
A __construct() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Deplacement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Deplacement, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* Copyright (C) 2003       Rodolphe Quiedeville        <[email protected]>
4
 * Copyright (C) 2004-2011	Laurent Destailleur		    <[email protected]>
5
 * Copyright (C) 2009-2012	Regis Houssin			    <[email protected]>
6
 * Copyright (C) 2013       Florian Henry		  	    <[email protected]>
7
 * Copyright (C) 2019-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/deplacement/class/deplacement.class.php
28
 *      \ingroup    deplacement
29
 *      \brief      File of class to manage trips
30
 */
31
32
use Dolibarr\Core\Base\CommonObject;
33
use DoliDB;
34
35
/**
36
 *      Class to manage trips and working credit notes
37
 */
38
class Deplacement extends CommonObject
39
{
40
    /**
41
     * @var string ID to identify managed object
42
     */
43
    public $element = 'deplacement';
44
45
    /**
46
     * @var string Name of table without prefix where object is stored
47
     */
48
    public $table_element = 'deplacement';
49
50
    /**
51
     * @var string    Name of subtable line
52
     */
53
    public $table_element_line = '';
54
55
    /**
56
     * @var string Fieldname with ID of parent key if this field has a parent
57
     */
58
    public $fk_element = '';
59
60
    public $fk_soc;
61
    public $date;
62
    public $type;
63
64
    /**
65
     * Date creation record (datec)
66
     *
67
     * @var integer
68
     */
69
    public $datec;
70
71
    /**
72
     * Date (dated)
73
     *
74
     * @var integer
75
     */
76
    public $dated;
77
78
    /**
79
     * @var int ID
80
     */
81
    public $fk_user_author;
82
83
    /**
84
     * @var int User ID
85
     */
86
    public $fk_user;
87
88
    /**
89
     * @var float km value formatted
90
     */
91
    public $km;
92
93
    /**
94
     * @var int Thirdparty id
95
     */
96
    public $socid;
97
98
    /**
99
     * @var int Status 0=draft, 1=validated, 2=Refunded
100
     */
101
    public $statut;
102
    public $extraparams = array();
103
104
105
    /**
106
     * Draft status
107
     */
108
    const STATUS_DRAFT = 0;
109
110
    /**
111
     * Validated status
112
     */
113
    const STATUS_VALIDATED = 1;
114
115
    /**
116
     * Refunded status
117
     */
118
    const STATUS_REFUNDED = 2;
119
120
    /**
121
     * Constructor
122
     *
123
     * @param   DoliDB      $db     Database handler
124
     */
125
    public function __construct(DoliDB $db)
126
    {
127
        $this->db = $db;
128
129
        $this->ismultientitymanaged = 0;
130
    }
131
132
    /**
133
     * Create object in database
134
     * TODO Add ref number
135
     *
136
     * @param   User    $user   User that creates
137
     * @return  int             Return integer <0 if KO, >0 if OK
138
     */
139
    public function create($user)
140
    {
141
        global $conf;
142
143
        // Check parameters
144
        if (empty($this->type) || $this->type < 0) {
145
            $this->error = 'ErrorBadParameter';
146
            return -1;
147
        }
148
        if (empty($this->fk_user) || $this->fk_user < 0) {
149
            $this->error = 'ErrorBadParameter';
150
            return -1;
151
        }
152
153
        $now = dol_now();
154
155
        $this->db->begin();
156
157
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "deplacement (";
158
        $sql .= "datec";
159
        //$sql.= ", dated";
160
        $sql .= ", entity";
161
        $sql .= ", fk_user_author";
162
        $sql .= ", fk_user";
163
        $sql .= ", type";
164
        $sql .= ", note_private";
165
        $sql .= ", note_public";
166
        $sql .= ", fk_projet";
167
        $sql .= ", fk_soc";
168
        $sql .= ") VALUES (";
169
        $sql .= " '" . $this->db->idate($now) . "'";
170
        $sql .= ", " . ((int) $conf->entity);
171
        $sql .= ", " . ((int) $user->id);
172
        $sql .= ", " . ((int) $this->fk_user);
173
        $sql .= ", '" . $this->db->escape($this->type) . "'";
174
        $sql .= ", " . ($this->note_private ? "'" . $this->db->escape($this->note_private) . "'" : "null");
175
        $sql .= ", " . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null");
176
        $sql .= ", " . ($this->fk_project > 0 ? ((int) $this->fk_project) : 0);
177
        $sql .= ", " . ($this->fk_soc > 0 ? ((int) $this->fk_soc) : "null");
178
        $sql .= ")";
179
180
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
181
        $result = $this->db->query($sql);
182
        if ($result) {
183
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "deplacement");
184
185
            // Call trigger
186
            $result = $this->call_trigger('DEPLACEMENT_CREATE', $user);
187
            if ($result < 0) {
188
                $this->db->rollback();
189
                return -2;
190
            }
191
            // End call triggers
192
193
            $result = $this->update($user);
194
            if ($result > 0) {
195
                $this->db->commit();
196
                return $this->id;
197
            } else {
198
                $this->error = $this->db->error();
199
                $this->db->rollback();
200
                return $result;
201
            }
202
        } else {
203
            $this->error = $this->db->error() . " sql=" . $sql;
204
            $this->db->rollback();
205
            return -1;
206
        }
207
    }
208
209
    /**
210
     *  Update record
211
     *
212
     *  @param  User    $user       User making update
213
     *  @return int                 Return integer <0 if KO, >0 if OK
214
     */
215
    public function update($user)
216
    {
217
        // Clean parameters
218
        $this->km = (float) price2num($this->km);
219
220
        // Check parameters
221
        if (!is_numeric($this->km)) {
222
            $this->km = 0;
223
        }
224
        if (empty($this->date)) {
225
            $this->error = 'ErrorBadParameter';
226
            return -1;
227
        }
228
        if (empty($this->type) || $this->type < 0) {
229
            $this->error = 'ErrorBadParameter';
230
            return -1;
231
        }
232
        if (empty($this->fk_user) || $this->fk_user < 0) {
233
            $this->error = 'ErrorBadParameter';
234
            return -1;
235
        }
236
237
        $this->db->begin();
238
239
        $sql = "UPDATE " . MAIN_DB_PREFIX . "deplacement ";
240
        $sql .= " SET km = " . ((float) $this->km); // This is a distance or amount
241
        $sql .= " , dated = '" . $this->db->idate($this->date) . "'";
242
        $sql .= " , type = '" . $this->db->escape($this->type) . "'";
243
        $sql .= " , fk_statut = '" . $this->db->escape($this->statut) . "'";
244
        $sql .= " , fk_user = " . ((int) $this->fk_user);
245
        $sql .= " , fk_user_modif = " . ((int) $user->id);
246
        $sql .= " , fk_soc = " . ($this->socid > 0 ? $this->socid : 'null');
247
        $sql .= " , note_private = " . ($this->note_private ? "'" . $this->db->escape($this->note_private) . "'" : "null");
248
        $sql .= " , note_public = " . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null");
249
        $sql .= " , fk_projet = " . ($this->fk_project > 0 ? $this->fk_project : 0);
250
        $sql .= " WHERE rowid = " . ((int) $this->id);
251
252
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
253
        $result = $this->db->query($sql);
254
        if ($result) {
255
            $this->db->commit();
256
            return 1;
257
        } else {
258
            $this->error = $this->db->lasterror();
259
            $this->db->rollback();
260
            return -1;
261
        }
262
    }
263
264
    /**
265
     * Load an object from database
266
     *
267
     * @param   int     $id     Id of record to load
268
     * @param   string  $ref    Ref of record
269
     * @return  int             Return integer <0 if KO, >0 if OK
270
     */
271
    public function fetch($id, $ref = '')
272
    {
273
        $sql = "SELECT rowid, fk_user, type, fk_statut, km, fk_soc, dated, note_private, note_public, fk_projet as fk_project, extraparams";
274
        $sql .= " FROM " . MAIN_DB_PREFIX . "deplacement";
275
        $sql .= " WHERE entity IN (" . getEntity('deplacement') . ")";
276
        if ($ref) {
277
            $sql .= " AND ref ='" . $this->db->escape($ref) . "'";
278
        } else {
279
            $sql .= " AND rowid = " . ((int) $id);
280
        }
281
282
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
283
        $result = $this->db->query($sql);
284
        if ($result) {
285
            $obj = $this->db->fetch_object($result);
286
287
            $this->id = $obj->rowid;
288
            $this->ref = $obj->rowid;
289
            $this->date         = $this->db->jdate($obj->dated);
290
            $this->fk_user = $obj->fk_user;
291
            $this->socid        = $obj->fk_soc;
292
            $this->km = $obj->km;
293
            $this->type         = $obj->type;
294
            $this->statut       = $obj->fk_statut;
295
            $this->note_private = $obj->note_private;
296
            $this->note_public  = $obj->note_public;
297
            $this->fk_project   = $obj->fk_project;
298
299
            $this->extraparams  = (array) json_decode($obj->extraparams, true);
300
301
            return 1;
302
        } else {
303
            $this->error = $this->db->error();
304
            return -1;
305
        }
306
    }
307
308
    /**
309
     *  Delete record
310
     *
311
     *  @param  User    $user       USer that Delete
312
     *  @return int             Return integer <0 if KO, >0 if OK
313
     */
314
    public function delete($user)
315
    {
316
        $this->db->begin();
317
318
        $id = $this->id;
319
320
        $sql = "DELETE FROM " . MAIN_DB_PREFIX . "deplacement WHERE rowid = " . ((int) $id);
321
322
        dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
323
        $result = $this->db->query($sql);
324
        if ($result) {
325
            $this->db->commit();
326
            return 1;
327
        } else {
328
            $this->error = $this->db->error();
329
            $this->db->rollback();
330
            return -1;
331
        }
332
    }
333
334
335
    /**
336
     *  Return the label of the status
337
     *
338
     *  @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
339
     *  @return string                 Label of status
340
     */
341
    public function getLibStatut($mode = 0)
342
    {
343
        return $this->LibStatut($this->statut, $mode);
344
    }
345
346
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
347
    /**
348
     *  Return the label of a given status
349
     *
350
     *  @param  int     $status        Id status
351
     *  @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
352
     *  @return string                 Label of status
353
     */
354
    public function LibStatut($status, $mode = 0)
355
    {
356
		// phpcs:enable
357
        global $langs;
358
359
        if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
360
            global $langs;
361
            //$langs->load("mymodule@mymodule");
362
            $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
363
            $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
364
            $this->labelStatus[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
365
            $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
366
            $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
367
            $this->labelStatusShort[self::STATUS_REFUNDED] = $langs->transnoentitiesnoconv('Refunded');
368
        }
369
370
        $status_logo = array(0 => 'status0', 1 => 'status4', 2 => 'status1', 4 => 'status6', 5 => 'status4', 6 => 'status6', 99 => 'status5');
371
        $statusType = $status_logo[$status];
372
373
        return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
374
    }
375
376
    /**
377
     *  Return clicable name (with picto eventually)
378
     *
379
     *  @param      int     $withpicto      0=No picto, 1=Include picto into link, 2=Only picto
380
     *  @return     string                  Chaine avec URL
381
     */
382
    public function getNomUrl($withpicto = 0)
383
    {
384
        global $langs;
385
386
        $result = '';
387
        $label = $langs->trans("Show") . ': ' . $this->ref;
388
389
        $link = '<a href="' . constant('BASE_URL') . '/compta/deplacement/card.php?id=' . $this->id . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">';
390
        $linkend = '</a>';
391
392
        $picto = 'trip';
393
394
395
        if ($withpicto) {
396
            $result .= ($link . img_object($label, $picto, 'class="classfortooltip"') . $linkend);
397
        }
398
        if ($withpicto && $withpicto != 2) {
399
            $result .= ' ';
400
        }
401
        if ($withpicto != 2) {
402
            $result .= $link . $this->ref . $linkend;
403
        }
404
        return $result;
405
    }
406
407
408
    /**
409
     * List of types
410
     *
411
     * @param   int     $active     Active or not
412
     * @return  array
413
     */
414
    public function listOfTypes($active = 1)
415
    {
416
        global $langs;
417
418
        $ret = array();
419
420
        $sql = "SELECT id, code, label";
421
        $sql .= " FROM " . MAIN_DB_PREFIX . "c_type_fees";
422
        $sql .= " WHERE active = " . ((int) $active);
423
424
        dol_syslog(get_class($this) . "::listOfTypes", LOG_DEBUG);
425
        $result = $this->db->query($sql);
426
        if ($result) {
427
            $num = $this->db->num_rows($result);
428
            $i = 0;
429
            while ($i < $num) {
430
                $obj = $this->db->fetch_object($result);
431
                $ret[$obj->code] = (($langs->trans($obj->code) != $obj->code) ? $langs->trans($obj->code) : $obj->label);
432
                $i++;
433
            }
434
        } else {
435
            dol_print_error($this->db);
436
        }
437
438
        return $ret;
439
    }
440
441
    /**
442
     * Information on record
443
     *
444
     * @param   int     $id      Id of record
445
     * @return  void
446
     */
447
    public function info($id)
448
    {
449
        $sql = 'SELECT c.rowid, c.datec, c.fk_user_author, c.fk_user_modif,';
450
        $sql .= ' c.tms as datem';
451
        $sql .= ' FROM ' . MAIN_DB_PREFIX . 'deplacement as c';
452
        $sql .= ' WHERE c.rowid = ' . ((int) $id);
453
454
        dol_syslog(get_class($this) . '::info', LOG_DEBUG);
455
        $result = $this->db->query($sql);
456
457
        if ($result) {
458
            if ($this->db->num_rows($result)) {
459
                $obj = $this->db->fetch_object($result);
460
461
                $this->id = $obj->rowid;
462
463
                $this->user_creation_id = $obj->fk_user_author;
464
                $this->user_modification_id = $obj->fk_user_modif;
465
                $this->date_creation     = $this->db->jdate($obj->datec);
466
                $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
467
            }
468
            $this->db->free($result);
469
        } else {
470
            dol_print_error($this->db);
471
        }
472
    }
473
}
474