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

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

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

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

759
                /** @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...
760
                $line->batch = $obj->batch;
761
                $line->eatby = $this->db->jdate($obj->eatby);
762
                $line->sellby = $this->db->jdate($obj->sellby);
763
                $line->fetch_optionals();
764
765
                $this->lines[$line->id] = $line;
766
            }
767
            $this->db->free($resql);
768
769
            return $num;
770
        } else {
771
            $this->errors[] = 'Error ' . $this->db->lasterror();
772
            dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
773
774
            return -1;
775
        }
776
    }
777
}
778