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

Cpaiement::update()   F

Complexity

Conditions 17
Paths > 20000

Size

Total Lines 74
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 37
nc 65536
nop 2
dl 0
loc 74
rs 1.0499
c 0
b 0
f 0

How to fix   Long Method    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) 2007-2012  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) 2023-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/paiement/class/cpaiement.class.php
28
 * \ingroup facture
29
 * \brief   This file is to manage CRUD function of type of payments
30
 */
31
32
// Put here all includes required by your class file
33
34
35
/**
36
 * Class Cpaiement
37
 */
38
class Cpaiement extends CommonDict
39
{
40
    /**
41
     * @var string Id to identify managed objects
42
     */
43
    public $element = 'cpaiement';
44
45
    /**
46
     * @var string Name of table without prefix where object is stored
47
     */
48
    public $table_element = 'c_paiement';
49
50
    /**
51
     * @var string
52
     * @deprecated
53
     * @see $label
54
     */
55
    public $libelle;
56
57
    public $type;
58
    public $active;
59
    public $accountancy_code;
60
    public $module;
61
62
63
    /**
64
     * Constructor
65
     *
66
     * @param DoliDB $db Database handler
67
     */
68
    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...
69
    {
70
        $this->db = $db;
71
    }
72
73
    /**
74
     * Create object into database
75
     *
76
     * @param  User $user       User that creates
77
     * @param  int  $notrigger  0=launch triggers after, 1=disable triggers
78
     * @return int              Return integer <0 if KO, Id of created object if OK
79
     */
80
    public function create(User $user, $notrigger = 0)
81
    {
82
        dol_syslog(__METHOD__, LOG_DEBUG);
83
84
        $error = 0;
85
86
        // Clean parameters
87
88
        if (isset($this->code)) {
89
            $this->code = trim($this->code);
90
        }
91
        if (isset($this->libelle)) {
92
            $this->libelle = trim($this->libelle);
93
        }
94
        if (isset($this->label)) {
95
            $this->label = trim($this->label);
96
        }
97
        if (isset($this->type)) {
98
            $this->type = trim($this->type);
99
        }
100
        if (isset($this->active)) {
101
            $this->active = (int) $this->active;
102
        }
103
        if (isset($this->accountancy_code)) {
104
            $this->accountancy_code = trim($this->accountancy_code);
105
        }
106
        if (isset($this->module)) {
107
            $this->module = trim($this->module);
108
        }
109
110
111
112
        // Check parameters
113
        // Put here code to add control on parameters values
114
115
        // Insert request
116
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
117
        $sql .= 'entity,';
118
        $sql .= 'code,';
119
        $sql .= 'libelle,';
120
        $sql .= 'type,';
121
        $sql .= 'active,';
122
        $sql .= 'accountancy_code,';
123
        $sql .= 'module';
124
        $sql .= ') VALUES (';
125
        $sql .= ' ' . (!isset($this->entity) ? getEntity('c_paiement') : $this->entity) . ',';
126
        $sql .= ' ' . (!isset($this->code) ? 'NULL' : "'" . $this->db->escape($this->code) . "'") . ',';
127
        $sql .= ' ' . (!isset($this->libelle) ? 'NULL' : "'" . $this->db->escape($this->libelle) . "'") . ',';
128
        $sql .= ' ' . (!isset($this->type) ? 'NULL' : $this->type) . ',';
129
        $sql .= ' ' . (!isset($this->active) ? 'NULL' : $this->active) . ',';
130
        $sql .= ' ' . (!isset($this->accountancy_code) ? 'NULL' : "'" . $this->db->escape($this->accountancy_code) . "'") . ',';
131
        $sql .= ' ' . (!isset($this->module) ? 'NULL' : "'" . $this->db->escape($this->module) . "'");
132
        $sql .= ')';
133
134
        $this->db->begin();
135
136
        $resql = $this->db->query($sql);
137
        if (!$resql) {
138
            $error++;
139
            $this->errors[] = 'Error ' . $this->db->lasterror();
140
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
141
        }
142
143
        if (!$error) {
144
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
145
146
            // Uncomment this and change MYOBJECT to your own tag if you
147
            // want this action to call a trigger.
148
            //if (!$notrigger) {
149
150
            //  // Call triggers
151
            //  $result=$this->call_trigger('MYOBJECT_CREATE',$user);
152
            //  if ($result < 0) $error++;
153
            //  // End call triggers
154
            //}
155
        }
156
157
        // Commit or rollback
158
        if ($error) {
159
            $this->db->rollback();
160
161
            return -1 * $error;
162
        } else {
163
            $this->db->commit();
164
165
            return $this->id;
166
        }
167
    }
168
169
    /**
170
     * Load object in memory from the database
171
     *
172
     * @param int    $id  Id object
173
     * @param string $ref Ref
174
     *
175
     * @return int Return integer <0 if KO, 0 if not found, >0 if OK
176
     */
177
    public function fetch($id, $ref = null)
178
    {
179
        dol_syslog(__METHOD__, LOG_DEBUG);
180
181
        $sql = 'SELECT';
182
        $sql .= ' t.id,';
183
        $sql .= " t.code,";
184
        $sql .= " t.libelle as label,";
185
        $sql .= " t.type,";
186
        $sql .= " t.active,";
187
        $sql .= " t.accountancy_code,";
188
        $sql .= " t.module";
189
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
190
        if (null !== $ref) {
191
            $sql .= ' WHERE t.entity IN (' . getEntity('c_paiement') . ')';
192
            $sql .= " AND t.code = '" . $this->db->escape($ref) . "'";
193
        } else {
194
            $sql .= ' WHERE t.id = ' . ((int) $id);
195
        }
196
197
        $resql = $this->db->query($sql);
198
        if ($resql) {
199
            $numrows = $this->db->num_rows($resql);
200
            if ($numrows) {
201
                $obj = $this->db->fetch_object($resql);
202
203
                $this->id = $obj->id;
204
205
                $this->code = $obj->code;
206
                $this->libelle = $obj->label;
207
                $this->label = $obj->label;
208
                $this->type = $obj->type;
209
                $this->active = $obj->active;
210
                $this->accountancy_code = $obj->accountancy_code;
211
                $this->module = $obj->module;
212
            }
213
            $this->db->free($resql);
214
215
            if ($numrows) {
216
                return 1;
217
            } else {
218
                return 0;
219
            }
220
        } else {
221
            $this->errors[] = 'Error ' . $this->db->lasterror();
222
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
223
224
            return -1;
225
        }
226
    }
227
228
    /**
229
     * Update object into database
230
     *
231
     * @param  User $user       User that modifies
232
     * @param  int $notrigger   0=launch triggers after, 1=disable triggers
233
     * @return int              Return integer <0 if KO, >0 if OK
234
     */
235
    public function update(User $user, $notrigger = 0)
236
    {
237
        $error = 0;
238
239
        dol_syslog(__METHOD__, LOG_DEBUG);
240
241
        // Clean parameters
242
243
        if (isset($this->code)) {
244
            $this->code = trim($this->code);
245
        }
246
        if (isset($this->libelle)) {
247
            $this->libelle = trim($this->libelle);
248
        }
249
        if (isset($this->label)) {
250
            $this->label = trim($this->label);
251
        }
252
        if (isset($this->type)) {
253
            $this->type = trim($this->type);
254
        }
255
        if (isset($this->active)) {
256
            $this->active = (int) $this->active;
257
        }
258
        if (isset($this->accountancy_code)) {
259
            $this->accountancy_code = trim($this->accountancy_code);
260
        }
261
        if (isset($this->module)) {
262
            $this->module = trim($this->module);
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 .= ' id = ' . (isset($this->id) ? $this->id : "null") . ',';
273
        $sql .= ' code = ' . (isset($this->code) ? "'" . $this->db->escape($this->code) . "'" : "null") . ',';
274
        $sql .= ' libelle = ' . (isset($this->libelle) ? "'" . $this->db->escape($this->libelle) . "'" : "null") . ',';
275
        $sql .= ' type = ' . (isset($this->type) ? $this->type : "null") . ',';
276
        $sql .= ' active = ' . (isset($this->active) ? $this->active : "null") . ',';
277
        $sql .= ' accountancy_code = ' . (isset($this->accountancy_code) ? "'" . $this->db->escape($this->accountancy_code) . "'" : "null") . ',';
278
        $sql .= ' module = ' . (isset($this->module) ? "'" . $this->db->escape($this->module) . "'" : "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
        // Uncomment this and change MYOBJECT to your own tag if you
291
        // want this action calls a trigger.
292
        //if (!$error && !$notrigger) {
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
        // Uncomment this and change MYOBJECT to your own tag if you
328
        // want this action calls a trigger.
329
        //if (!$error && !$notrigger) {
330
331
        //  // Call triggers
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}
334
        //  // End call triggers
335
        //}
336
337
        if (!$error) {
338
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
339
            $sql .= ' WHERE id = ' . ((int) $this->id);
340
341
            $resql = $this->db->query($sql);
342
            if (!$resql) {
343
                $error++;
344
                $this->errors[] = 'Error ' . $this->db->lasterror();
345
                dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
346
            }
347
        }
348
349
        // Commit or rollback
350
        if ($error) {
351
            $this->db->rollback();
352
353
            return -1 * $error;
354
        } else {
355
            $this->db->commit();
356
357
            return 1;
358
        }
359
    }
360
361
362
    /**
363
     * Initialise object with example values
364
     * Id must be 0 if object instance is a specimen
365
     *
366
     * @return int
367
     */
368
    public function initAsSpecimen()
369
    {
370
        $this->id = 0;
371
        $this->code = '';
372
        $this->libelle = '';
373
        $this->label = '';
374
        $this->type = '';
375
        $this->active = 1;
376
        $this->accountancy_code = '';
377
        $this->module = '';
378
379
        return 1;
380
    }
381
}
382