Passed
Push — dev ( f7d146...05f415 )
by Rafael
60:50
created

ReceptionLineBatch::update()   F

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 95
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 59
nc 436207616
nop 2
dl 0
loc 95
rs 0
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) 2015       Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2014       Juanjo Menent	            <[email protected]>
5
 * Copyright (C) 2024       Frédéric France             <[email protected]>
6
 * Copyright (C) 2024       Christophe Battarel	        <[email protected]>
7
 * Copyright (C) 2024       Rafael San José             <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
namespace Dolibarr\Code\Reception\Classes;
24
25
use Dolibarr\Code\User\Classes\User;
26
use Dolibarr\Core\Base\CommonObjectLine;
27
use DoliDB;
28
29
/**
30
 *  \file       htdocs/fourn/class/fournisseur.commande.dispatch.class.php
31
 *  \ingroup    fournisseur stock
32
 *  \brief      This file is an example for a CRUD class file (Create/Read/Update/Delete)
33
 *              Initially built by build_class_from_table on 2015-02-24 10:38
34
 */
35
36
/**
37
 *  Class to manage table commandefournisseurdispatch
38
 */
39
class ReceptionLineBatch extends CommonObjectLine
40
{
41
    /**
42
     * @var DoliDB Database handler.
43
     */
44
    public $db;
45
46
    /**
47
     * @var string ID to identify managed object
48
     */
49
    public $element = 'receptionlinebatch';
50
51
    /**
52
     * @var string Name of table without prefix where object is stored
53
     */
54
    public $table_element = 'receptiondet_batch'; //!< Name of table without prefix where object is stored
55
    public $lines = array();
56
57
    /**
58
     * @var int ID
59
     */
60
    public $id;
61
62
    /**
63
     * @var int ID of reception
64
     */
65
    public $fk_reception;
66
67
    /**
68
     * @var int ID  Duplicate of origin_id (using origin_id is better)
69
     */
70
    public $fk_element;
71
72
    /**
73
     * @var int ID  Duplicate of fk_element
74
     */
75
    public $origin_id;
76
77
    /**
78
     * @var int ID  Duplicate of origin_line_id
79
     */
80
    public $fk_elementdet;
81
82
    /**
83
     * @var int ID  Duplicate of fk_elementdet
84
     */
85
    public $origin_line_id;
86
87
    /**
88
     * @var string      Type of object the fk_element refers to. Example: 'supplier_order'.
89
     */
90
    public $element_type;
91
92
    /**
93
     * @var int ID
94
     */
95
    public $fk_product;
96
97
    /**
98
     * @var float Quantity
99
     */
100
    public $qty;
101
102
    /**
103
     * @var float Quantity asked
104
     */
105
    public $qty_asked;
106
107
    public $libelle;
108
    public $label;
109
    public $desc;
110
    public $tva_tx;
111
    public $vat_src_code;
112
    public $ref_supplier;
113
114
    /**
115
     * @var int ID
116
     */
117
    public $fk_entrepot;
118
119
    /**
120
     * @var int User ID
121
     */
122
    public $fk_user;
123
124
    public $datec = '';
125
    public $comment;
126
127
    /**
128
     * @var int Status
129
     */
130
    public $status;
131
132
    public $batch;
133
    public $eatby = '';
134
    public $sellby = '';
135
    public $cost_price = 0;
136
137
138
139
140
    /**
141
     *  Constructor
142
     *
143
     *  @param  DoliDB      $db      Database handler
144
     */
145
    public function __construct($db)
146
    {
147
        $this->db = $db;
148
149
        // List of language codes for status
150
        $this->labelStatus[0] = 'Received';
151
        $this->labelStatus[1] = 'Verified';
152
        $this->labelStatus[2] = 'Denied';
153
        $this->labelStatusShort[0] = 'Received';
154
        $this->labelStatusShort[1] = 'Verified';
155
        $this->labelStatusShort[2] = 'Denied';
156
    }
157
158
159
    /**
160
     *  Create object into database
161
     *
162
     *  @param  User    $user        User that creates
163
     *  @param  int     $notrigger   0=launch triggers after, 1=disable triggers
164
     *  @return int                  Return integer <0 if KO, Id of created object if OK
165
     */
166
    public function create($user, $notrigger = 0)
167
    {
168
        $error = 0;
169
170
        // Clean parameters
171
        if (isset($this->fk_element)) {
172
            $this->fk_element = (int) $this->fk_element;
173
        }
174
        if (isset($this->fk_product)) {
175
            $this->fk_product = (int) $this->fk_product;
176
        }
177
        if (isset($this->fk_elementdet)) {
178
            $this->fk_elementdet = (int) $this->fk_elementdet;
179
        }
180
        if (isset($this->qty)) {
181
            $this->qty = (float) $this->qty;
182
        }
183
        if (isset($this->fk_entrepot)) {
184
            $this->fk_entrepot = (int) $this->fk_entrepot;
185
        }
186
        if (isset($this->fk_user)) {
187
            $this->fk_user = (int) $this->fk_user;
188
        }
189
        if (isset($this->comment)) {
190
            $this->comment = trim($this->comment);
191
        }
192
        if (isset($this->status)) {
193
            $this->status = (int) $this->status;
194
        }
195
        if (isset($this->batch)) {
196
            $this->batch = trim($this->batch);
197
        }
198
        if (empty($this->datec)) {
199
            $this->datec = dol_now();
200
        }
201
202
        // Check parameters
203
        if (empty($this->fk_product)) {
204
            $this->error = 'Error, property ->fk_product must not be empty to create a line of reception';
205
            return -1;
206
        }
207
        if (empty($this->fk_reception)) {
208
            $this->error = 'Error, property ->fk_reception must not be empty to create a line of reception';
209
            return -1;
210
        }
211
212
        // Insert request
213
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . "(";
214
        $sql .= "fk_product,";
215
        $sql .= "fk_element,";
216
        $sql .= "fk_elementdet,";
217
        $sql .= "element_type,";
218
        $sql .= "qty,";
219
        $sql .= "fk_entrepot,";
220
        $sql .= "fk_user,";
221
        $sql .= "datec,";
222
        $sql .= "comment,";
223
        $sql .= "status,";
224
        $sql .= "batch,";
225
        $sql .= "eatby,";
226
        $sql .= "sellby,";
227
        $sql .= "fk_reception,";
228
        $sql .= "cost_price";
229
        $sql .= ") VALUES (";
230
        $sql .= " " . (!isset($this->fk_product) ? 'NULL' : (int) $this->fk_product) . ",";
231
        $sql .= " " . (!isset($this->fk_element) ? 'NULL' : (int) $this->fk_element) . ",";
232
        $sql .= " " . (!isset($this->fk_elementdet) ? 'NULL' : (int) $this->fk_elementdet) . ",";
233
        $sql .= " '" . (!isset($this->element_type) ? "supplier_order" : $this->db->escape($this->element_type)) . "',";
234
        $sql .= " " . (!isset($this->qty) ? 'NULL' : (float) $this->qty) . ",";
235
        $sql .= " " . (!isset($this->fk_entrepot) ? 'NULL' : (int) $this->fk_entrepot) . ",";
236
        $sql .= " " . (!isset($this->fk_user) ? 'NULL' : (int) $this->fk_user) . ",";
237
        $sql .= " " . (!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'" . $this->db->idate($this->datec) . "'") . ",";
238
        $sql .= " " . (!isset($this->comment) ? 'NULL' : "'" . $this->db->escape($this->comment) . "'") . ",";
239
        $sql .= " " . (!isset($this->status) ? 'NULL' : (int) $this->status) . ",";
240
        $sql .= " " . (!isset($this->batch) ? 'NULL' : "'" . $this->db->escape($this->batch) . "'") . ",";
241
        $sql .= " " . (!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'" . $this->db->idate($this->eatby) . "'") . ",";
242
        $sql .= " " . (!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'" . $this->db->idate($this->sellby) . "'") . ",";
243
        $sql .= " " . ((int) $this->fk_reception) . ",";
244
        $sql .= " " . (!isset($this->cost_price) ? '0' : (float) $this->cost_price);
245
        $sql .= ")";
246
247
        $this->db->begin();
248
249
        dol_syslog(__METHOD__, LOG_DEBUG);
250
        $resql = $this->db->query($sql);
251
        if (!$resql) {
252
            $error++;
253
            $this->errors[] = "Error " . $this->db->lasterror();
254
        }
255
256
        if (!$error) {
257
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
258
259
            if (!$notrigger) {
260
                // Call triggers
261
                $result = $this->call_trigger('LINERECEPTION_CREATE', $user);
262
                if ($result < 0) {
263
                    $error++;
264
                }
265
                // End call triggers
266
            }
267
        }
268
269
        // Create extrafields
270
        if (!$error) {
271
            $result = $this->insertExtraFields();
272
            if ($result < 0) {
273
                $error++;
274
            }
275
        }
276
277
        // Commit or rollback
278
        if ($error) {
279
            foreach ($this->errors as $errmsg) {
280
                dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
281
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
282
            }
283
            $this->db->rollback();
284
            return -1 * $error;
285
        } else {
286
            $this->db->commit();
287
            return $this->id;
288
        }
289
    }
290
291
292
    /**
293
     *  Load object in memory from the database
294
     *
295
     *  @param  int     $id     Id object
296
     *  @param  string  $ref    Ref
297
     *  @return int             Return integer <0 if KO, >0 if OK
298
     */
299
    public function fetch($id, $ref = '')
300
    {
301
        $sql = "SELECT";
302
        $sql .= " t.rowid,";
303
        $sql .= " t.fk_element,";
304
        $sql .= " t.fk_elementdet,";
305
        $sql .= " t.element_type,";
306
        $sql .= " t.fk_product,";
307
        $sql .= " t.qty,";
308
        $sql .= " t.fk_entrepot,";
309
        $sql .= " t.fk_user,";
310
        $sql .= " t.datec,";
311
        $sql .= " t.comment,";
312
        $sql .= " t.status,";
313
        $sql .= " t.tms,";
314
        $sql .= " t.batch,";
315
        $sql .= " t.eatby,";
316
        $sql .= " t.sellby,";
317
        $sql .= " t.fk_reception";
318
        $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element . " as t";
319
        if ($ref) {
320
            $sql .= " WHERE t.ref = '" . $this->db->escape($ref) . "'";
321
        } else {
322
            $sql .= " WHERE t.rowid = " . ((int) $id);
323
        }
324
325
        dol_syslog(get_only_class($this) . "::fetch");
326
        $resql = $this->db->query($sql);
327
        if ($resql) {
328
            if ($this->db->num_rows($resql)) {
329
                $obj = $this->db->fetch_object($resql);
330
331
                $this->id = $obj->rowid;
332
333
                $this->fk_element = $obj->fk_element;
334
                $this->origin_id = $obj->fk_element;
335
                $this->fk_elementdet = $obj->fk_elementdet;
336
                $this->origin_line_id = $obj->fk_elementdet;
337
                $this->element_type = $obj->element_type;
338
                $this->origin_type = $obj->element_type;
339
340
                $this->fk_product = $obj->fk_product;
341
                $this->qty = $obj->qty;
342
                $this->fk_entrepot = $obj->fk_entrepot;
343
                $this->fk_user = $obj->fk_user;
344
                $this->datec = $this->db->jdate($obj->datec);
345
                $this->comment = $obj->comment;
346
                $this->status = $obj->status;
347
                $this->tms = $this->db->jdate($obj->tms);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

347
                /** @scrutinizer ignore-deprecated */ $this->tms = $this->db->jdate($obj->tms);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Documentation Bug introduced by
It seems like $this->db->jdate($obj->tms) can also be of type string. However, the property $tms is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
348
                $this->batch = $obj->batch;
349
                $this->eatby = $this->db->jdate($obj->eatby);
350
                $this->sellby = $this->db->jdate($obj->sellby);
351
                $this->fk_reception = $obj->fk_reception;
352
353
                $this->fetch_optionals();
354
            }
355
            $this->db->free($resql);
356
357
            return 1;
358
        } else {
359
            $this->error = "Error " . $this->db->lasterror();
360
            return -1;
361
        }
362
    }
363
364
365
    /**
366
     *  Update object into database
367
     *
368
     *  @param  User    $user        User that modifies
369
     *  @param  int     $notrigger   0=launch triggers after, 1=disable triggers
370
     *  @return int                  Return integer <0 if KO, >0 if OK
371
     */
372
    public function update($user, $notrigger = 0)
373
    {
374
        $error = 0;
375
376
        // Clean parameters
377
378
        if (isset($this->fk_element)) {
379
            $this->fk_element = (int) $this->fk_element;
380
        }
381
        if (isset($this->fk_product)) {
382
            $this->fk_product = (int) $this->fk_product;
383
        }
384
        if (isset($this->fk_elementdet)) {
385
            $this->fk_elementdet = (int) $this->fk_elementdet;
386
        }
387
        if (isset($this->qty)) {
388
            $this->qty = (float) $this->qty;
389
        }
390
        if (isset($this->fk_entrepot)) {
391
            $this->fk_entrepot = (int) $this->fk_entrepot;
392
        }
393
        if (isset($this->fk_user)) {
394
            $this->fk_user = (int) $this->fk_user;
395
        }
396
        if (isset($this->comment)) {
397
            $this->comment = trim($this->comment);
398
        }
399
        if (isset($this->status)) {
400
            $this->status = (int) $this->status;
401
        }
402
        if (isset($this->batch)) {
403
            $this->batch = trim($this->batch);
404
        }
405
406
407
408
        // Check parameters
409
        // Put here code to add a control on parameters values
410
411
        // Update request
412
        $sql = "UPDATE " . MAIN_DB_PREFIX . $this->table_element . " SET";
413
        $sql .= " fk_element=" . (isset($this->fk_element) ? $this->fk_element : "null") . ",";
414
        $sql .= " fk_product=" . (isset($this->fk_product) ? $this->fk_product : "null") . ",";
415
        $sql .= " fk_elementdet=" . (isset($this->fk_elementdet) ? $this->fk_elementdet : "null") . ",";
416
        $sql .= " qty=" . (isset($this->qty) ? $this->qty : "null") . ",";
417
        $sql .= " fk_entrepot=" . (isset($this->fk_entrepot) ? $this->fk_entrepot : "null") . ",";
418
        $sql .= " fk_user=" . (isset($this->fk_user) ? $this->fk_user : "null") . ",";
419
        $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ",";
420
        $sql .= " comment=" . (isset($this->comment) ? "'" . $this->db->escape($this->comment) . "'" : "null") . ",";
421
        $sql .= " status=" . (isset($this->status) ? $this->status : "null") . ",";
422
        $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ",";
423
        $sql .= " batch=" . (isset($this->batch) ? "'" . $this->db->escape($this->batch) . "'" : "null") . ",";
424
        $sql .= " eatby=" . (dol_strlen($this->eatby) != 0 ? "'" . $this->db->idate($this->eatby) . "'" : 'null') . ",";
425
        $sql .= " sellby=" . (dol_strlen($this->sellby) != 0 ? "'" . $this->db->idate($this->sellby) . "'" : 'null');
426
        $sql .= " WHERE rowid=" . ((int) $this->id);
427
428
        $this->db->begin();
429
430
        dol_syslog(__METHOD__);
431
        $resql = $this->db->query($sql);
432
        if (!$resql) {
433
            $error++;
434
            $this->errors[] = "Error " . $this->db->lasterror();
435
        }
436
437
        if (!$error) {
438
            if (empty($this->id) && !empty($this->rowid)) {
439
                $this->id = $this->rowid;
440
            }
441
            $result = $this->insertExtraFields();
442
            if ($result < 0) {
443
                $error++;
444
            }
445
446
            if (!$notrigger) {
447
                // Call triggers
448
                $result = $this->call_trigger('LINERECEPTION_MODIFY', $user);
449
                if ($result < 0) {
450
                    $error++;
451
                }
452
                // End call triggers
453
            }
454
        }
455
456
        // Commit or rollback
457
        if ($error) {
458
            foreach ($this->errors as $errmsg) {
459
                dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
460
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
461
            }
462
            $this->db->rollback();
463
            return -1 * $error;
464
        } else {
465
            $this->db->commit();
466
            return 1;
467
        }
468
    }
469
470
471
    /**
472
     *  Delete object in database
473
     *
474
     *  @param  User    $user        User that deletes
475
     *  @param  int     $notrigger   0=launch triggers after, 1=disable triggers
476
     *  @return int                  Return integer <0 if KO, >0 if OK
477
     */
478
    public function delete($user, $notrigger = 0)
479
    {
480
        $error = 0;
481
482
        $this->db->begin();
483
484
        if (!$error) {
485
            if (!$notrigger) {
486
                // Call triggers
487
                $result = $this->call_trigger('LINERECEPTION_DELETE', $user);
488
                if ($result < 0) {
489
                    $error++;
490
                }
491
                // End call triggers
492
            }
493
        }
494
495
        // Remove extrafields
496
        if (!$error) {
497
            $result = $this->deleteExtraFields();
498
            if ($result < 0) {
499
                $error++;
500
                dol_syslog(get_only_class($this) . "::delete error deleteExtraFields " . $this->error, LOG_ERR);
501
            }
502
        }
503
504
        if (!$error) {
505
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . $this->table_element;
506
            $sql .= " WHERE rowid=" . ((int) $this->id);
507
508
            dol_syslog(__METHOD__);
509
            $resql = $this->db->query($sql);
510
            if (!$resql) {
511
                $error++;
512
                $this->errors[] = "Error " . $this->db->lasterror();
513
            }
514
        }
515
516
        // Commit or rollback
517
        if ($error) {
518
            foreach ($this->errors as $errmsg) {
519
                dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
520
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
521
            }
522
            $this->db->rollback();
523
            return -1 * $error;
524
        } else {
525
            $this->db->commit();
526
            return 1;
527
        }
528
    }
529
530
531
    /**
532
     *  Load an object from its id and create a new one in database
533
     *
534
     *  @param  User    $user       User making the clone
535
     *  @param  int     $fromid     Id of object to clone
536
     *  @return int                 New id of clone
537
     */
538
    public function createFromClone(User $user, $fromid)
539
    {
540
        $error = 0;
541
542
        $object = new ReceptionLineBatch($this->db);
543
544
        $this->db->begin();
545
546
        // Load source object
547
        $object->fetch($fromid);
548
        $object->id = 0;
549
        $object->statut = 0;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$statut has been deprecated: Use $status instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

549
        /** @scrutinizer ignore-deprecated */ $object->statut = 0;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
550
551
        // Clear fields
552
        // ...
553
554
        // Create clone
555
        $object->context['createfromclone'] = 'createfromclone';
556
        $result = $object->create($user);
557
558
        // Other options
559
        if ($result < 0) {
560
            $this->error = $object->error;
561
            $error++;
562
        }
563
564
        if (!$error) {
565
        }
566
567
        unset($object->context['createfromclone']);
568
569
        // End
570
        if (!$error) {
571
            $this->db->commit();
572
            return $object->id;
573
        } else {
574
            $this->db->rollback();
575
            return -1;
576
        }
577
    }
578
579
580
581
    /**
582
     *  Return label of the status of object
583
     *
584
     *  @param      int     $mode           0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto
585
     *  @return     string                  Label
586
     */
587
    public function getLibStatut($mode = 0)
588
    {
589
        return $this->LibStatut($this->status, $mode);
590
    }
591
592
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
593
    /**
594
     *  Return label of a status
595
     *
596
     *  @param  int     $status     Id status
597
     *  @param  int     $mode       0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto
598
     *  @return string              Label of status
599
     */
600
    public function LibStatut($status, $mode = 0)
601
    {
602
		// phpcs:enable
603
        global $langs;
604
        $langs->load('orders');
605
606
        if ($mode == 0) {
607
            return $langs->trans($this->labelStatus[$status]);
608
        } elseif ($mode == 1) {
609
            return $langs->trans($this->labelStatusShort[$status]);
610
        } elseif ($mode == 2) {
611
            return $langs->trans($this->labelStatus[$status]);
612
        } elseif ($mode == 3) {
613
            if ($status == 0) {
614
                return img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
615
            } elseif ($status == 1) {
616
                return img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
617
            } elseif ($status == 2) {
618
                return img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
619
            }
620
        } elseif ($mode == 4) {
621
            if ($status == 0) {
622
                return img_picto($langs->trans($this->labelStatus[$status]), 'statut0') . ' ' . $langs->trans($this->labelStatus[$status]);
623
            } elseif ($status == 1) {
624
                return img_picto($langs->trans($this->labelStatus[$status]), 'statut4') . ' ' . $langs->trans($this->labelStatus[$status]);
625
            } elseif ($status == 2) {
626
                return img_picto($langs->trans($this->labelStatus[$status]), 'statut8') . ' ' . $langs->trans($this->labelStatus[$status]);
627
            }
628
        } elseif ($mode == 5) {
629
            if ($status == 0) {
630
                return '<span class="hideonsmartphone">' . $langs->trans($this->labelStatusShort[$status]) . ' </span>' . img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
631
            } elseif ($status == 1) {
632
                return '<span class="hideonsmartphone">' . $langs->trans($this->labelStatusShort[$status]) . ' </span>' . img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
633
            } elseif ($status == 2) {
634
                return '<span class="hideonsmartphone">' . $langs->trans($this->labelStatusShort[$status]) . ' </span>' . img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
635
            }
636
        }
637
        return "";
638
    }
639
640
641
    /**
642
     *  Initialise object with example values
643
     *  Id must be 0 if object instance is a specimen
644
     *
645
     *  @return int
646
     */
647
    public function initAsSpecimen()
648
    {
649
        $this->id = 0;
650
651
        $this->fk_element = 0;
652
        $this->fk_product = 0;
653
        $this->fk_elementdet = 0;
654
        $this->qty = 0;
655
        $this->fk_entrepot = 0;
656
        $this->fk_user = 0;
657
        $this->datec = '';
658
        $this->comment = '';
659
        $this->status = 0;
660
        $this->tms = dol_now();
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

660
        /** @scrutinizer ignore-deprecated */ $this->tms = dol_now();

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
661
        $this->batch = '';
662
        $this->eatby = '';
663
        $this->sellby = '';
664
665
        return 1;
666
    }
667
668
    /**
669
     * Load object in memory from the database
670
     *
671
     * @param string        $sortorder      Sort Order
672
     * @param string        $sortfield      Sort field
673
     * @param int           $limit          limit
674
     * @param int           $offset         offset limit
675
     * @param string|array  $filter         filter array
676
     * @param string        $filtermode     filter mode (AND or OR)
677
     * @return                              int Return integer <0 if KO, >0 if OK
678
     */
679
    public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
680
    {
681
        dol_syslog(__METHOD__, LOG_DEBUG);
682
683
        $sql = "SELECT";
684
        $sql .= " t.rowid,";
685
        $sql .= " t.fk_element,";
686
        $sql .= " t.fk_product,";
687
        $sql .= " t.fk_elementdet,";
688
        $sql .= " t.qty,";
689
        $sql .= " t.fk_entrepot,";
690
        $sql .= " t.fk_user,";
691
        $sql .= " t.datec,";
692
        $sql .= " t.comment,";
693
        $sql .= " t.status,";
694
        $sql .= " t.tms,";
695
        $sql .= " t.batch,";
696
        $sql .= " t.eatby,";
697
        $sql .= " t.sellby";
698
        $sql .= " FROM " . MAIN_DB_PREFIX . $this->table_element . " as t";
699
700
        // Manage filter
701
        if (is_array($filter)) {
702
            $sqlwhere = array();
703
            if (count($filter) > 0) {
704
                foreach ($filter as $key => $value) {
705
                    if ($key == 't.comment') {
706
                        $sqlwhere [] = $this->db->sanitize($key) . " LIKE '%" . $this->db->escape($this->db->escapeforlike($value)) . "%'";
707
                    } elseif ($key == 't.datec' || $key == 't.tms' || $key == 't.eatby' || $key == 't.sellby' || $key == 't.batch') {
708
                        $sqlwhere [] = $this->db->sanitize($key) . " = '" . $this->db->escape($value) . "'";
709
                    } elseif ($key == 'qty') {
710
                        $sqlwhere [] = $this->db->sanitize($key) . " = " . ((float) $value);
711
                    } else {
712
                        $sqlwhere [] = $this->db->sanitize($key) . " = " . ((int) $value);
713
                    }
714
                }
715
            }
716
            if (count($sqlwhere) > 0) {
717
                $sql .= ' WHERE ' . implode(' ' . $this->db->escape($filtermode) . ' ', $sqlwhere);
718
            }
719
720
            $filter = '';
721
        }
722
723
        // Manage filter
724
        $errormessage = '';
725
        $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
726
        if ($errormessage) {
727
            $this->errors[] = $errormessage;
728
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
729
            return -1;
730
        }
731
732
        if (!empty($sortfield)) {
733
            $sql .= $this->db->order($sortfield, $sortorder);
734
        }
735
        if (!empty($limit)) {
736
            $sql .= $this->db->plimit($limit, $offset);
737
        }
738
        $this->lines = array();
739
740
        $resql = $this->db->query($sql);
741
        if ($resql) {
742
            $num = $this->db->num_rows($resql);
743
744
            while ($obj = $this->db->fetch_object($resql)) {
745
                $line = new self($this->db);
746
747
                $line->id = $obj->rowid;
748
749
                $line->fk_element = $obj->fk_element;
750
                $line->fk_product = $obj->fk_product;
751
                $line->fk_elementdet = $obj->fk_elementdet;
752
                $line->qty = $obj->qty;
753
                $line->fk_entrepot = $obj->fk_entrepot;
754
                $line->fk_user = $obj->fk_user;
755
                $line->datec = $this->db->jdate($obj->datec);
756
                $line->comment = $obj->comment;
757
                $line->status = $obj->status;
758
                $line->tms = $this->db->jdate($obj->tms);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

758
                /** @scrutinizer ignore-deprecated */ $line->tms = $this->db->jdate($obj->tms);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Documentation Bug introduced by
It seems like $this->db->jdate($obj->tms) can also be of type string. However, the property $tms is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
759
                $line->batch = $obj->batch;
760
                $line->eatby = $this->db->jdate($obj->eatby);
761
                $line->sellby = $this->db->jdate($obj->sellby);
762
                $line->fetch_optionals();
763
764
                $this->lines[$line->id] = $line;
765
            }
766
            $this->db->free($resql);
767
768
            return $num;
769
        } else {
770
            $this->errors[] = 'Error ' . $this->db->lasterror();
771
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
772
773
            return -1;
774
        }
775
    }
776
}
777