Test Failed
Branch main (fda838)
by Rafael
54:38 queued 11s
created

OrderLine::delete()   D

Complexity

Conditions 15
Paths 228

Size

Total Lines 76
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 50
nc 228
nop 2
dl 0
loc 76
rs 4.7333
c 1
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) 2003-2006  Rodolphe Quiedeville    <[email protected]>
4
 * Copyright (C) 2004-2012  Laurent Destailleur     <[email protected]>
5
 * Copyright (C) 2005-2014  Regis Houssin           <[email protected]>
6
 * Copyright (C) 2006       Andre Cianfarani        <[email protected]>
7
 * Copyright (C) 2010-2020  Juanjo Menent           <[email protected]>
8
 * Copyright (C) 2011       Jean Heimburger         <[email protected]>
9
 * Copyright (C) 2012-2014  Christophe Battarel     <[email protected]>
10
 * Copyright (C) 2012       Cedric Salvador         <[email protected]>
11
 * Copyright (C) 2013       Florian Henry		    <[email protected]>
12
 * Copyright (C) 2014-2015  Marcos García           <[email protected]>
13
 * Copyright (C) 2018       Nicolas ZABOURI	        <[email protected]>
14
 * Copyright (C) 2016-2022  Ferran Marcet           <[email protected]>
15
 * Copyright (C) 2021-2024  Frédéric France         <[email protected]>
16
 * Copyright (C) 2022       Gauthier VERDOL         <[email protected]>
17
 * Copyright (C) 2024		MDW						<[email protected]>
18
 * Copyright (C) 2024       Rafael San José         <[email protected]>
19
 *
20
 * This program is free software; you can redistribute it and/or modify
21
 * it under the terms of the GNU General Public License as published by
22
 * the Free Software Foundation; either version 3 of the License, or
23
 * (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32
 */
33
34
/**
35
 *  \file       htdocs/commande/class/commande.class.php
36
 *  \ingroup    commande
37
 *  \brief      class for orders
38
 */
39
40
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php';
41
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
42
require_once DOL_DOCUMENT_ROOT . '/margin/lib/margins.lib.php';
43
require_once DOL_DOCUMENT_ROOT . '/multicurrency/class/multicurrency.class.php';
44
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
45
46
/**
47
 *  Class to manage order lines
48
 */
49
class OrderLine extends CommonOrderLine
50
{
51
    /**
52
     * @var string ID to identify managed object
53
     */
54
    public $element = 'commandedet';
55
56
    public $table_element = 'commandedet';
57
58
    public $oldline;
59
60
    /**
61
     * Id of parent order
62
     * @var int
63
     */
64
    public $fk_commande;
65
66
    /**
67
     * Id of parent order
68
     * @var int
69
     * @deprecated Use fk_commande
70
     * @see $fk_commande
71
     */
72
    public $commande_id;
73
74
    public $fk_parent_line;
75
76
    /**
77
     * @var int Id of invoice
78
     */
79
    public $fk_facture;
80
81
    /**
82
     * @var string External ref
83
     */
84
    public $ref_ext;
85
86
    public $fk_remise_except;
87
88
    /**
89
     * @var int line rank
90
     */
91
    public $rang = 0;
92
    public $fk_fournprice;
93
94
    /**
95
     * Buy price without taxes
96
     * @var float
97
     */
98
    public $pa_ht;
99
    public $marge_tx;
100
    public $marque_tx;
101
102
    /**
103
     * @deprecated
104
     * @see $remise_percent, $fk_remise_except
105
     */
106
    public $remise;
107
108
    // Start and end date of the line
109
    public $date_start;
110
    public $date_end;
111
112
    public $skip_update_total; // Skip update price total for special lines
113
114
115
    /**
116
     *      Constructor
117
     *
118
     *      @param     DoliDB   $db      handler d'acces base de donnee
119
     */
120
    public function __construct($db)
121
    {
122
        $this->db = $db;
123
    }
124
125
    /**
126
     *  Load line order
127
     *
128
     *  @param  int     $rowid          Id line order
129
     *  @return int                     Return integer <0 if KO, >0 if OK
130
     */
131
    public function fetch($rowid)
132
    {
133
        $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_parent_line, cd.fk_product, cd.product_type, cd.label as custom_label, cd.description, cd.price, cd.qty, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx,';
134
        $sql .= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
135
        $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.fk_product_fournisseur_price as fk_fournprice, cd.buy_price_ht as pa_ht, cd.rang, cd.special_code,';
136
        $sql .= ' cd.fk_unit,';
137
        $sql .= ' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
138
        $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
139
        $sql .= ' cd.date_start, cd.date_end, cd.vat_src_code';
140
        $sql .= ' FROM ' . MAIN_DB_PREFIX . 'commandedet as cd';
141
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON cd.fk_product = p.rowid';
142
        $sql .= ' WHERE cd.rowid = ' . ((int) $rowid);
143
        $result = $this->db->query($sql);
144
        if ($result) {
145
            $objp = $this->db->fetch_object($result);
146
147
            if (!$objp) {
148
                $this->error = 'OrderLine with id ' . $rowid . ' not found sql=' . $sql;
149
                return 0;
150
            }
151
152
            $this->rowid            = $objp->rowid;
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocumentLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

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

152
            /** @scrutinizer ignore-deprecated */ $this->rowid            = $objp->rowid;

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...
153
            $this->id = $objp->rowid;
154
            $this->fk_commande      = $objp->fk_commande;
155
            $this->fk_parent_line   = $objp->fk_parent_line;
156
            $this->label            = $objp->custom_label;
157
            $this->desc             = $objp->description;
158
            $this->qty              = $objp->qty;
159
            $this->price            = $objp->price;
160
            $this->subprice         = $objp->subprice;
161
            $this->ref_ext          = $objp->ref_ext;
162
            $this->vat_src_code     = $objp->vat_src_code;
163
            $this->tva_tx           = $objp->tva_tx;
164
            $this->localtax1_tx     = $objp->localtax1_tx;
165
            $this->localtax2_tx     = $objp->localtax2_tx;
166
            $this->remise           = $objp->remise;
167
            $this->remise_percent   = $objp->remise_percent;
168
            $this->fk_remise_except = $objp->fk_remise_except;
169
            $this->fk_product       = $objp->fk_product;
170
            $this->product_type     = $objp->product_type;
171
            $this->info_bits        = $objp->info_bits;
172
            $this->special_code = $objp->special_code;
173
            $this->total_ht         = $objp->total_ht;
174
            $this->total_tva        = $objp->total_tva;
175
            $this->total_localtax1  = $objp->total_localtax1;
176
            $this->total_localtax2  = $objp->total_localtax2;
177
            $this->total_ttc        = $objp->total_ttc;
178
            $this->fk_fournprice = $objp->fk_fournprice;
179
            $marginInfos            = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
180
            $this->pa_ht            = $marginInfos[0];
181
            $this->marge_tx         = $marginInfos[1];
182
            $this->marque_tx        = $marginInfos[2];
183
            $this->special_code = $objp->special_code;
184
            $this->rang = $objp->rang;
185
186
            $this->ref = $objp->product_ref; // deprecated
0 ignored issues
show
Deprecated Code introduced by
The property CommonOrderLine::$ref has been deprecated: Use product_ref ( Ignorable by Annotation )

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

186
            /** @scrutinizer ignore-deprecated */ $this->ref = $objp->product_ref; // deprecated

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...
187
188
            $this->product_ref      = $objp->product_ref;
189
            $this->product_label    = $objp->product_label;
190
            $this->product_desc     = $objp->product_desc;
191
            $this->product_tobatch  = $objp->product_tobatch;
192
            $this->fk_unit          = $objp->fk_unit;
193
194
            $this->date_start       = $this->db->jdate($objp->date_start);
195
            $this->date_end         = $this->db->jdate($objp->date_end);
196
197
            $this->fk_multicurrency = $objp->fk_multicurrency;
198
            $this->multicurrency_code = $objp->multicurrency_code;
199
            $this->multicurrency_subprice   = $objp->multicurrency_subprice;
200
            $this->multicurrency_total_ht   = $objp->multicurrency_total_ht;
201
            $this->multicurrency_total_tva  = $objp->multicurrency_total_tva;
202
            $this->multicurrency_total_ttc  = $objp->multicurrency_total_ttc;
203
204
            $this->db->free($result);
205
206
            return 1;
207
        } else {
208
            $this->error = $this->db->lasterror();
209
            return -1;
210
        }
211
    }
212
213
    /**
214
     *  Delete line in database
215
     *
216
     *  @param      User    $user           User that modify
217
     *  @param      int     $notrigger      0=launch triggers after, 1=disable triggers
218
     *  @return  int  Return integer <0 si ko, >0 si ok
219
     */
220
    public function delete(User $user, $notrigger = 0)
221
    {
222
        global $conf, $langs;
223
224
        $error = 0;
225
226
        if (empty($this->id) && !empty($this->rowid)) {     // For backward compatibility
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocumentLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

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

226
        if (empty($this->id) && !empty(/** @scrutinizer ignore-deprecated */ $this->rowid)) {     // For backward compatibility

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...
227
            $this->id = $this->rowid;
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocumentLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

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

227
            $this->id = /** @scrutinizer ignore-deprecated */ $this->rowid;

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...
228
        }
229
230
        // check if order line is not in a shipment line before deleting
231
        $sqlCheckShipmentLine  = "SELECT";
232
        $sqlCheckShipmentLine .= " ed.rowid";
233
        $sqlCheckShipmentLine .= " FROM " . MAIN_DB_PREFIX . "expeditiondet ed";
234
        $sqlCheckShipmentLine .= " WHERE ed.fk_origin_line = " . ((int) $this->id);
235
236
        $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
237
        if (!$resqlCheckShipmentLine) {
238
            $error++;
239
            $this->error    = $this->db->lasterror();
240
            $this->errors[] = $this->error;
241
        } else {
242
            $langs->load('errors');
243
            $num = $this->db->num_rows($resqlCheckShipmentLine);
244
            if ($num > 0) {
245
                $error++;
246
                $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
247
                $this->error = $langs->trans('ErrorRecordAlreadyExists') . ' : ' . $langs->trans('ShipmentLine') . ' ' . $objCheckShipmentLine->rowid;
248
                $this->errors[] = $this->error;
249
            }
250
            $this->db->free($resqlCheckShipmentLine);
251
        }
252
        if ($error) {
253
            dol_syslog(__METHOD__ . 'Error ; ' . $this->error, LOG_ERR);
254
            return -1;
255
        }
256
257
        $this->db->begin();
258
259
        $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . "commandedet WHERE rowid = " . ((int) $this->id);
260
261
        dol_syslog("OrderLine::delete", LOG_DEBUG);
262
        $resql = $this->db->query($sql);
263
        if ($resql) {
264
            if (!$error && !$notrigger) {
265
                // Call trigger
266
                $result = $this->call_trigger('LINEORDER_DELETE', $user);
267
                if ($result < 0) {
268
                    $error++;
269
                }
270
                // End call triggers
271
            }
272
273
            // Remove extrafields
274
            if (!$error) {
275
                $result = $this->deleteExtraFields();
276
                if ($result < 0) {
277
                    $error++;
278
                    dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
279
                }
280
            }
281
282
            if (!$error) {
283
                $this->db->commit();
284
                return 1;
285
            }
286
287
            foreach ($this->errors as $errmsg) {
288
                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
289
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
290
            }
291
            $this->db->rollback();
292
            return -1 * $error;
293
        } else {
294
            $this->error = $this->db->lasterror();
295
            return -1;
296
        }
297
    }
298
299
    /**
300
     *  Insert line into database
301
     *
302
     *  @param      User    $user           User that modify
303
     *  @param      int     $notrigger      1 = disable triggers
304
     *  @return     int                     Return integer <0 if KO, >0 if OK
305
     */
306
    public function insert($user = null, $notrigger = 0)
307
    {
308
        $error = 0;
309
310
        $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'.
311
312
        dol_syslog(get_class($this) . "::insert rang=" . $this->rang);
313
314
        // Clean parameters
315
        if (empty($this->tva_tx)) {
316
            $this->tva_tx = 0;
317
        }
318
        if (empty($this->localtax1_tx)) {
319
            $this->localtax1_tx = 0;
320
        }
321
        if (empty($this->localtax2_tx)) {
322
            $this->localtax2_tx = 0;
323
        }
324
        if (empty($this->localtax1_type)) {
325
            $this->localtax1_type = 0;
326
        }
327
        if (empty($this->localtax2_type)) {
328
            $this->localtax2_type = 0;
329
        }
330
        if (empty($this->total_localtax1)) {
331
            $this->total_localtax1 = 0;
332
        }
333
        if (empty($this->total_localtax2)) {
334
            $this->total_localtax2 = 0;
335
        }
336
        if (empty($this->rang)) {
337
            $this->rang = 0;
338
        }
339
        if (empty($this->remise_percent)) {
340
            $this->remise_percent = 0;
341
        }
342
        if (empty($this->info_bits)) {
343
            $this->info_bits = 0;
344
        }
345
        if (empty($this->special_code)) {
346
            $this->special_code = 0;
347
        }
348
        if (empty($this->fk_parent_line)) {
349
            $this->fk_parent_line = 0;
350
        }
351
        if (empty($this->pa_ht)) {
352
            $this->pa_ht = 0;
353
        }
354
        if (empty($this->ref_ext)) {
355
            $this->ref_ext = '';
356
        }
357
358
        // if buy price not defined, define buyprice as configured in margin admin
359
        if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
360
            $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
361
            if ($result < 0) {
362
                return $result;
363
            } else {
364
                $this->pa_ht = $result;
365
            }
366
        }
367
368
        // Check parameters
369
        if ($this->product_type < 0) {
370
            return -1;
371
        }
372
373
        $this->db->begin();
374
375
        // Insertion dans base de la ligne
376
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'commandedet';
377
        $sql .= ' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
378
        $sql .= ' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
379
        $sql .= ' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
380
        $sql .= ' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
381
        $sql .= ' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
382
        $sql .= ' fk_unit,';
383
        $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
384
        $sql .= ')';
385
        $sql .= " VALUES (" . $this->fk_commande . ",";
386
        $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->db->escape($this->fk_parent_line) . "'" : "null") . ",";
387
        $sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ",";
388
        $sql .= " '" . $this->db->escape($this->desc) . "',";
389
        $sql .= " '" . price2num($this->qty) . "',";
390
        $sql .= " '" . $this->db->escape($this->ref_ext) . "',";
391
        $sql .= " " . (empty($this->vat_src_code) ? "''" : "'" . $this->db->escape($this->vat_src_code) . "'") . ",";
392
        $sql .= " '" . price2num($this->tva_tx) . "',";
393
        $sql .= " '" . price2num($this->localtax1_tx) . "',";
394
        $sql .= " '" . price2num($this->localtax2_tx) . "',";
395
        $sql .= " '" . $this->db->escape($this->localtax1_type) . "',";
396
        $sql .= " '" . $this->db->escape($this->localtax2_type) . "',";
397
        $sql .= ' ' . ((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product : "null") . ',';
398
        $sql .= " '" . $this->db->escape($this->product_type) . "',";
399
        $sql .= " '" . price2num($this->remise_percent) . "',";
400
        $sql .= " " . (price2num($this->subprice) !== '' ? price2num($this->subprice) : "null") . ",";
401
        $sql .= " " . ($this->price != '' ? "'" . price2num($this->price) . "'" : "null") . ",";
402
        $sql .= ' ' . (!empty($this->fk_remise_except) ? $this->fk_remise_except : "null") . ',';
403
        $sql .= ' ' . ((int) $this->special_code) . ',';
404
        $sql .= ' ' . ((int) $this->rang) . ',';
405
        $sql .= ' ' . (!empty($this->fk_fournprice) ? $this->fk_fournprice : "null") . ',';
406
        $sql .= ' ' . price2num($this->pa_ht) . ',';
407
        $sql .= " " . ((int) $this->info_bits) . ",";
408
        $sql .= " " . price2num($this->total_ht, 'MT') . ",";
409
        $sql .= " " . price2num($this->total_tva, 'MT') . ",";
410
        $sql .= " " . price2num($this->total_localtax1, 'MT') . ",";
411
        $sql .= " " . price2num($this->total_localtax2, 'MT') . ",";
412
        $sql .= " " . price2num($this->total_ttc, 'MT') . ",";
413
        $sql .= " " . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ',';
414
        $sql .= " " . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ',';
415
        $sql .= ' ' . (!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
416
        $sql .= ", " . (!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL');
417
        $sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'";
418
        $sql .= ", " . price2num($this->multicurrency_subprice, 'CU');
419
        $sql .= ", " . price2num($this->multicurrency_total_ht, 'CT');
420
        $sql .= ", " . price2num($this->multicurrency_total_tva, 'CT');
421
        $sql .= ", " . price2num($this->multicurrency_total_ttc, 'CT');
422
        $sql .= ')';
423
424
        dol_syslog(get_class($this) . "::insert", LOG_DEBUG);
425
        $resql = $this->db->query($sql);
426
        if ($resql) {
427
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'commandedet');
428
            $this->rowid = $this->id;
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocumentLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

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

428
            /** @scrutinizer ignore-deprecated */ $this->rowid = $this->id;

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...
429
430
            if (!$error) {
431
                $result = $this->insertExtraFields();
432
                if ($result < 0) {
433
                    $error++;
434
                }
435
            }
436
437
            if (!$error && !$notrigger) {
438
                // Call trigger
439
                $result = $this->call_trigger('LINEORDER_INSERT', $user);
440
                if ($result < 0) {
441
                    $error++;
442
                }
443
                // End call triggers
444
            }
445
446
            if (!$error) {
447
                $this->db->commit();
448
                return 1;
449
            }
450
451
            foreach ($this->errors as $errmsg) {
452
                dol_syslog(get_class($this) . "::insert " . $errmsg, LOG_ERR);
453
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
454
            }
455
            $this->db->rollback();
456
            return -1 * $error;
457
        } else {
458
            $this->error = $this->db->error();
459
            $this->db->rollback();
460
            return -2;
461
        }
462
    }
463
464
    /**
465
     *  Update the line object into db
466
     *
467
     *  @param      User    $user           User that modify
468
     *  @param      int     $notrigger      1 = disable triggers
469
     *  @return     int     Return integer <0 si ko, >0 si ok
470
     */
471
    public function update(User $user, $notrigger = 0)
472
    {
473
        $error = 0;
474
475
        $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht == ''); // If true, we can use a default value. If this->pa_ht = '0', we must use '0'.
476
477
        // Clean parameters
478
        if (empty($this->tva_tx)) {
479
            $this->tva_tx = 0;
480
        }
481
        if (empty($this->localtax1_tx)) {
482
            $this->localtax1_tx = 0;
483
        }
484
        if (empty($this->localtax2_tx)) {
485
            $this->localtax2_tx = 0;
486
        }
487
        if (empty($this->localtax1_type)) {
488
            $this->localtax1_type = 0;
489
        }
490
        if (empty($this->localtax2_type)) {
491
            $this->localtax2_type = 0;
492
        }
493
        if (empty($this->qty)) {
494
            $this->qty = 0;
495
        }
496
        if (empty($this->total_localtax1)) {
497
            $this->total_localtax1 = 0;
498
        }
499
        if (empty($this->total_localtax2)) {
500
            $this->total_localtax2 = 0;
501
        }
502
        if (empty($this->marque_tx)) {
503
            $this->marque_tx = 0;
504
        }
505
        if (empty($this->marge_tx)) {
506
            $this->marge_tx = 0;
507
        }
508
        if (empty($this->remise_percent)) {
509
            $this->remise_percent = 0;
510
        }
511
        if (empty($this->remise)) {
512
            $this->remise = 0;
513
        }
514
        if (empty($this->info_bits)) {
515
            $this->info_bits = 0;
516
        }
517
        if (empty($this->special_code)) {
518
            $this->special_code = 0;
519
        }
520
        if (empty($this->product_type)) {
521
            $this->product_type = 0;
522
        }
523
        if (empty($this->fk_parent_line)) {
524
            $this->fk_parent_line = 0;
525
        }
526
        if (empty($this->pa_ht)) {
527
            $this->pa_ht = 0;
528
        }
529
        if (empty($this->ref_ext)) {
530
            $this->ref_ext = '';
531
        }
532
533
        // if buy price not defined, define buyprice as configured in margin admin
534
        if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
535
            $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
536
            if ($result < 0) {
537
                return $result;
538
            } else {
539
                $this->pa_ht = $result;
540
            }
541
        }
542
543
        $this->db->begin();
544
545
        // Mise a jour ligne en base
546
        $sql = "UPDATE " . MAIN_DB_PREFIX . "commandedet SET";
547
        $sql .= " description='" . $this->db->escape($this->desc) . "'";
548
        $sql .= " , label=" . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null");
549
        $sql .= " , vat_src_code=" . (!empty($this->vat_src_code) ? "'" . $this->db->escape($this->vat_src_code) . "'" : "''");
550
        $sql .= " , tva_tx=" . price2num($this->tva_tx);
551
        $sql .= " , localtax1_tx=" . price2num($this->localtax1_tx);
552
        $sql .= " , localtax2_tx=" . price2num($this->localtax2_tx);
553
        $sql .= " , localtax1_type='" . $this->db->escape($this->localtax1_type) . "'";
554
        $sql .= " , localtax2_type='" . $this->db->escape($this->localtax2_type) . "'";
555
        $sql .= " , qty=" . price2num($this->qty);
556
        $sql .= " , ref_ext='" . $this->db->escape($this->ref_ext) . "'";
557
        $sql .= " , subprice=" . price2num($this->subprice);
558
        $sql .= " , remise_percent=" . price2num($this->remise_percent);
559
        $sql .= " , price=" . price2num($this->price); // TODO A virer
560
        $sql .= " , remise=" . price2num($this->remise); // TODO A virer
561
        if (empty($this->skip_update_total)) {
562
            $sql .= " , total_ht=" . price2num($this->total_ht);
563
            $sql .= " , total_tva=" . price2num($this->total_tva);
564
            $sql .= " , total_ttc=" . price2num($this->total_ttc);
565
            $sql .= " , total_localtax1=" . price2num($this->total_localtax1);
566
            $sql .= " , total_localtax2=" . price2num($this->total_localtax2);
567
        }
568
        $sql .= " , fk_product_fournisseur_price=" . (!empty($this->fk_fournprice) ? $this->fk_fournprice : "null");
569
        $sql .= " , buy_price_ht='" . price2num($this->pa_ht) . "'";
570
        $sql .= " , info_bits=" . ((int) $this->info_bits);
571
        $sql .= " , special_code=" . ((int) $this->special_code);
572
        $sql .= " , date_start=" . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null");
573
        $sql .= " , date_end=" . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null");
574
        $sql .= " , product_type=" . $this->product_type;
575
        $sql .= " , fk_parent_line=" . (!empty($this->fk_parent_line) ? $this->fk_parent_line : "null");
576
        if (!empty($this->rang)) {
577
            $sql .= ", rang=" . ((int) $this->rang);
578
        }
579
        $sql .= " , fk_unit=" . (!$this->fk_unit ? 'NULL' : $this->fk_unit);
580
581
        // Multicurrency
582
        $sql .= " , multicurrency_subprice=" . price2num($this->multicurrency_subprice);
583
        $sql .= " , multicurrency_total_ht=" . price2num($this->multicurrency_total_ht);
584
        $sql .= " , multicurrency_total_tva=" . price2num($this->multicurrency_total_tva);
585
        $sql .= " , multicurrency_total_ttc=" . price2num($this->multicurrency_total_ttc);
586
587
        $sql .= " WHERE rowid = " . ((int) $this->rowid);
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocumentLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

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

587
        $sql .= " WHERE rowid = " . ((int) /** @scrutinizer ignore-deprecated */ $this->rowid);

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...
588
589
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
590
        $resql = $this->db->query($sql);
591
        if ($resql) {
592
            if (!$error) {
593
                $this->id = $this->rowid;
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocumentLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

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

593
                $this->id = /** @scrutinizer ignore-deprecated */ $this->rowid;

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...
594
                $result = $this->insertExtraFields();
595
                if ($result < 0) {
596
                    $error++;
597
                }
598
            }
599
600
            if (!$error && !$notrigger) {
601
                // Call trigger
602
                $result = $this->call_trigger('LINEORDER_MODIFY', $user);
603
                if ($result < 0) {
604
                    $error++;
605
                }
606
                // End call triggers
607
            }
608
609
            if (!$error) {
610
                $this->db->commit();
611
                return 1;
612
            }
613
614
            foreach ($this->errors as $errmsg) {
615
                dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
616
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
617
            }
618
            $this->db->rollback();
619
            return -1 * $error;
620
        } else {
621
            $this->error = $this->db->error();
622
            $this->db->rollback();
623
            return -2;
624
        }
625
    }
626
627
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
628
    /**
629
     *  Update DB line fields total_xxx
630
     *  Used by migration
631
     *
632
     *  @return     int     Return integer <0 if KO, >0 if OK
633
     */
634
    public function update_total()
635
    {
636
		// phpcs:enable
637
        $this->db->begin();
638
639
        // Clean parameters
640
        if (empty($this->total_localtax1)) {
641
            $this->total_localtax1 = 0;
642
        }
643
        if (empty($this->total_localtax2)) {
644
            $this->total_localtax2 = 0;
645
        }
646
647
        // Mise a jour ligne en base
648
        $sql = "UPDATE " . MAIN_DB_PREFIX . "commandedet SET";
649
        $sql .= " total_ht='" . price2num($this->total_ht) . "'";
650
        $sql .= ",total_tva='" . price2num($this->total_tva) . "'";
651
        $sql .= ",total_localtax1='" . price2num($this->total_localtax1) . "'";
652
        $sql .= ",total_localtax2='" . price2num($this->total_localtax2) . "'";
653
        $sql .= ",total_ttc='" . price2num($this->total_ttc) . "'";
654
        $sql .= " WHERE rowid = " . ((int) $this->rowid);
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocumentLine::$rowid has been deprecated: Try to use id property as possible (even if field into database is still rowid) ( Ignorable by Annotation )

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

654
        $sql .= " WHERE rowid = " . ((int) /** @scrutinizer ignore-deprecated */ $this->rowid);

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...
655
656
        dol_syslog("OrderLine::update_total", LOG_DEBUG);
657
658
        $resql = $this->db->query($sql);
659
        if ($resql) {
660
            $this->db->commit();
661
            return 1;
662
        } else {
663
            $this->error = $this->db->error();
664
            $this->db->rollback();
665
            return -2;
666
        }
667
    }
668
}
669