Test Failed
Branch main (fda838)
by Rafael
50:22
created

CommandeFournisseurLigne   F

Complexity

Total Complexity 68

Size/Duplication

Total Lines 477
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 286
dl 0
loc 477
rs 2.96
c 1
b 0
f 0
wmc 68

5 Methods

Rating   Name   Duplication   Size   Complexity  
F insert() 0 145 38
F update() 0 73 13
B delete() 0 52 10
B fetch() 0 100 6
A __construct() 0 3 1

How to fix   Complexity   

Complex Class

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

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

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

1
<?php
2
3
/* Copyright (C) 2003-2006  Rodolphe Quiedeville    <[email protected]>
4
 * Copyright (C) 2004-2017	Laurent Destailleur		<[email protected]>
5
 * Copyright (C) 2005-2012	Regis Houssin			<[email protected]>
6
 * Copyright (C) 2007		Franky Van Liedekerke	<[email protected]>
7
 * Copyright (C) 2010-2020	Juanjo Menent			<[email protected]>
8
 * Copyright (C) 2010-2018	Philippe Grand			<[email protected]>
9
 * Copyright (C) 2012-2015	Marcos García			<[email protected]>
10
 * Copyright (C) 2013		Florian Henry			<[email protected]>
11
 * Copyright (C) 2013		Cédric Salvador			<[email protected]>
12
 * Copyright (C) 2018		Nicolas ZABOURI			<[email protected]>
13
 * Copyright (C) 2018-2024	Frédéric France			<[email protected]>
14
 * Copyright (C) 2018-2022	Ferran Marcet			<[email protected]>
15
 * Copyright (C) 2021		Josep Lluís Amador		<[email protected]>
16
 * Copyright (C) 2022		Gauthier VERDOL			<[email protected]>
17
 * Copyright (C) 2024		Solution Libre SAS		<[email protected]>
18
 * Copyright (C) 2024		MDW						<[email protected]>
19
 * Copyright (C) 2024       Rafael San José         <[email protected]>
20
 *
21
 * This program is free software; you can redistribute it and/or modify
22
 * it under the terms of the GNU General Public License as published by
23
 * the Free Software Foundation; either version 3 of the License, or
24
 * (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
33
 */
34
35
/**
36
 *  \file       htdocs/fourn/class/fournisseur.commande.class.php
37
 *  \ingroup    fournisseur,commande
38
 *  \brief      File of class to manage suppliers orders
39
 */
40
41
require_once DOL_DOCUMENT_ROOT . '/core/class/commonorder.class.php';
42
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php';
43
require_once DOL_DOCUMENT_ROOT . '/multicurrency/class/multicurrency.class.php';
44
if (isModEnabled('productbatch')) {
45
    require_once DOL_DOCUMENT_ROOT . '/product/class/productbatch.class.php';
46
}
47
48
/**
49
 *  Class to manage line orders
50
 */
51
class CommandeFournisseurLigne extends CommonOrderLine
52
{
53
    /**
54
     * @var string ID to identify managed object
55
     */
56
    public $element = 'commande_fournisseurdet';
57
58
    /**
59
     * @var string Name of table without prefix where object is stored
60
     */
61
    public $table_element = 'commande_fournisseurdet';
62
63
    public $oldline;
64
65
    /**
66
     * Id of parent order
67
     * @var int
68
     */
69
    public $fk_commande;
70
71
    // From llx_commande_fournisseurdet
72
    /**
73
     * @var int ID
74
     */
75
    public $fk_parent_line;
76
77
    /**
78
     * @var int ID
79
     */
80
    public $fk_facture;
81
82
    public $rang = 0;
83
    public $special_code = 0;
84
85
    /**
86
     * Unit price without taxes
87
     * @var float
88
     */
89
    public $pu_ht;
90
91
    public $date_start;
92
    public $date_end;
93
    public $fk_fournprice;
94
    public $packaging;
95
    public $pa_ht;
96
97
    // From llx_product_fournisseur_price
98
99
    /**
100
     * Supplier reference of price when we added the line. May have been changed after line was added.
101
     * @var string
102
     */
103
    public $ref_supplier;
104
105
    /**
106
     * @var string ref supplier
107
     * @deprecated
108
     * @see $ref_supplier
109
     */
110
    public $ref_fourn;
111
112
    public $remise;
113
114
115
    /**
116
     *  Constructor
117
     *
118
     *  @param      DoliDB      $db      Database handler
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_product, cd.product_type, cd.description, cd.qty, cd.tva_tx, cd.special_code,';
134
        $sql .= ' cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.ref as ref_supplier,';
135
        $sql .= ' cd.remise, cd.remise_percent, cd.subprice,';
136
        $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_ttc,';
137
        $sql .= ' cd.total_localtax1, cd.total_localtax2,';
138
        $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
139
        $sql .= ' cd.date_start, cd.date_end, cd.fk_unit,';
140
        $sql .= ' cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
141
        $sql .= ' c.fk_soc as socid';
142
        $sql .= ' FROM ' . $this->db->prefix() . 'commande_fournisseur as c, ' . $this->db->prefix() . 'commande_fournisseurdet as cd';
143
        $sql .= ' LEFT JOIN ' . $this->db->prefix() . 'product as p ON cd.fk_product = p.rowid';
144
        $sql .= ' WHERE cd.fk_commande = c.rowid AND cd.rowid = ' . ((int) $rowid);
145
146
        $result = $this->db->query($sql);
147
        if ($result) {
148
            $objp = $this->db->fetch_object($result);
149
150
            if (!empty($objp)) {
151
                $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

151
                /** @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...
152
                $this->id               = $objp->rowid;
153
                $this->fk_commande      = $objp->fk_commande;
154
                $this->desc             = $objp->description;
155
                $this->qty              = $objp->qty;
156
                $this->ref_fourn        = $objp->ref_supplier;
157
                $this->ref_supplier     = $objp->ref_supplier;
158
                $this->subprice         = $objp->subprice;
159
                $this->tva_tx           = $objp->tva_tx;
160
                $this->localtax1_tx     = $objp->localtax1_tx;
161
                $this->localtax2_tx     = $objp->localtax2_tx;
162
                $this->localtax1_type   = $objp->localtax1_type;
163
                $this->localtax2_type   = $objp->localtax2_type;
164
                $this->remise           = $objp->remise;
165
                $this->remise_percent   = $objp->remise_percent;
166
                $this->fk_product       = $objp->fk_product;
167
                $this->info_bits        = $objp->info_bits;
168
                $this->total_ht         = $objp->total_ht;
169
                $this->total_tva        = $objp->total_tva;
170
                $this->total_localtax1  = $objp->total_localtax1;
171
                $this->total_localtax2  = $objp->total_localtax2;
172
                $this->total_ttc        = $objp->total_ttc;
173
                $this->product_type     = $objp->product_type;
174
                $this->special_code     = $objp->special_code;
175
176
                $this->ref = $objp->product_ref;
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

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

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...
177
178
                $this->product_ref      = $objp->product_ref;
179
                $this->product_label    = $objp->product_label;
180
                $this->product_desc     = $objp->product_desc;
181
182
                if (getDolGlobalInt('PRODUCT_USE_SUPPLIER_PACKAGING')) {
183
                    // TODO We should not fetch this properties into the fetch_lines. This is NOT properties of a line.
184
                    // Move this into another method and call it when required.
185
186
                    // Take better packaging for $objp->qty (first supplier ref quantity <= $objp->qty)
187
                    $sqlsearchpackage = 'SELECT rowid, packaging FROM ' . $this->db->prefix() . "product_fournisseur_price";
188
                    $sqlsearchpackage .= ' WHERE entity IN (' . getEntity('product_fournisseur_price') . ")";
189
                    $sqlsearchpackage .= " AND fk_product = " . ((int) $objp->fk_product);
190
                    $sqlsearchpackage .= " AND ref_fourn = '" . $this->db->escape($objp->ref_supplier) . "'";
191
                    $sqlsearchpackage .= " AND quantity <= " . ((float) $objp->qty);  // required to be qualified
192
                    $sqlsearchpackage .= " AND (packaging IS NULL OR packaging = 0 OR packaging <= " . ((float) $objp->qty) . ")";  // required to be qualified
193
                    $sqlsearchpackage .= " AND fk_soc = " . ((int) $objp->socid);
194
                    $sqlsearchpackage .= " ORDER BY packaging ASC";     // Take the smaller package first
195
                    $sqlsearchpackage .= " LIMIT 1";
196
197
                    $resqlsearchpackage = $this->db->query($sqlsearchpackage);
198
                    if ($resqlsearchpackage) {
199
                        $objsearchpackage = $this->db->fetch_object($resqlsearchpackage);
200
                        if ($objsearchpackage) {
201
                            $this->fk_fournprice = $objsearchpackage->rowid;
202
                            $this->packaging     = $objsearchpackage->packaging;
203
                        }
204
                    } else {
205
                        $this->error = $this->db->lasterror();
206
                        return -1;
207
                    }
208
                }
209
210
                $this->date_start               = $this->db->jdate($objp->date_start);
211
                $this->date_end                 = $this->db->jdate($objp->date_end);
212
                $this->fk_unit = $objp->fk_unit;
213
214
                $this->multicurrency_subprice   = $objp->multicurrency_subprice;
215
                $this->multicurrency_total_ht   = $objp->multicurrency_total_ht;
216
                $this->multicurrency_total_tva  = $objp->multicurrency_total_tva;
217
                $this->multicurrency_total_ttc  = $objp->multicurrency_total_ttc;
218
219
                $this->fetch_optionals();
220
221
                $this->db->free($result);
222
                return 1;
223
            } else {
224
                $this->error = 'Supplier order line  with id=' . $rowid . ' not found';
225
                dol_syslog(get_class($this) . "::fetch Error " . $this->error, LOG_ERR);
226
                return 0;
227
            }
228
        } else {
229
            dol_print_error($this->db);
230
            return -1;
231
        }
232
    }
233
234
    /**
235
     *  Insert line into database
236
     *
237
     *  @param      int     $notrigger      1 = disable triggers
238
     *  @return     int                     Return integer <0 if KO, >0 if OK
239
     */
240
    public function insert($notrigger = 0)
241
    {
242
        global $conf, $user;
243
244
        $error = 0;
245
246
        dol_syslog(get_class($this) . "::insert rang=" . $this->rang);
247
248
        // Clean parameters
249
        if (empty($this->tva_tx)) {
250
            $this->tva_tx = 0;
251
        }
252
        if (empty($this->localtax1_tx)) {
253
            $this->localtax1_tx = 0;
254
        }
255
        if (empty($this->localtax2_tx)) {
256
            $this->localtax2_tx = 0;
257
        }
258
        if (empty($this->localtax1_type)) {
259
            $this->localtax1_type = '0';
260
        }
261
        if (empty($this->localtax2_type)) {
262
            $this->localtax2_type = '0';
263
        }
264
        if (empty($this->total_localtax1)) {
265
            $this->total_localtax1 = 0;
266
        }
267
        if (empty($this->total_localtax2)) {
268
            $this->total_localtax2 = 0;
269
        }
270
        if (empty($this->rang)) {
271
            $this->rang = 0;
272
        }
273
        if (empty($this->remise_percent)) {
274
            $this->remise_percent = 0;
275
        }
276
        if (empty($this->info_bits)) {
277
            $this->info_bits = 0;
278
        }
279
        if (empty($this->special_code)) {
280
            $this->special_code = 0;
281
        }
282
        if (empty($this->fk_parent_line)) {
283
            $this->fk_parent_line = 0;
284
        }
285
        if (empty($this->pa_ht)) {
286
            $this->pa_ht = 0;
287
        }
288
289
        // Multicurrency
290
        if (!empty($this->multicurrency_code)) {
291
            list($this->fk_multicurrency, $this->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($this->db, $this->multicurrency_code);
292
        }
293
        if (empty($this->fk_multicurrency)) {
294
            $this->multicurrency_code = $conf->currency;
295
            $this->fk_multicurrency = 0;
296
            $this->multicurrency_tx = 1;
297
        }
298
299
        // Check parameters
300
        if ($this->product_type < 0) {
301
            return -1;
302
        }
303
304
        $this->db->begin();
305
306
        // Insertion dans base de la ligne
307
        $sql = 'INSERT INTO ' . $this->db->prefix() . $this->table_element;
308
        $sql .= " (fk_commande, label, description, date_start, date_end,";
309
        $sql .= " fk_product, product_type, special_code, rang,";
310
        $sql .= " qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
311
        $sql .= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
312
        $sql .= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc,";
313
        $sql .= " fk_parent_line)";
314
        $sql .= " VALUES (" . $this->fk_commande . ", '" . $this->db->escape($this->label) . "','" . $this->db->escape($this->desc) . "',";
315
        $sql .= " " . ($this->date_start ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ",";
316
        $sql .= " " . ($this->date_end ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ",";
317
        if ($this->fk_product) {
318
            $sql .= $this->fk_product . ",";
319
        } else {
320
            $sql .= "null,";
321
        }
322
        $sql .= "'" . $this->db->escape($this->product_type) . "',";
323
        $sql .= "'" . $this->db->escape($this->special_code) . "',";
324
        $sql .= "'" . $this->db->escape($this->rang) . "',";
325
        $sql .= "'" . $this->db->escape($this->qty) . "', ";
326
        $sql .= " " . (empty($this->vat_src_code) ? "''" : "'" . $this->db->escape($this->vat_src_code) . "'") . ",";
327
        $sql .= " " . price2num($this->tva_tx) . ", ";
328
        $sql .= " " . price2num($this->localtax1_tx) . ",";
329
        $sql .= " " . price2num($this->localtax2_tx) . ",";
330
        $sql .= " '" . $this->db->escape($this->localtax1_type) . "',";
331
        $sql .= " '" . $this->db->escape($this->localtax2_type) . "',";
332
        $sql .= " " . ((float) $this->remise_percent) . ", " . price2num($this->subprice, 'MU') . ", '" . $this->db->escape($this->ref_supplier) . "',";
333
        $sql .= " " . price2num($this->total_ht) . ",";
334
        $sql .= " " . price2num($this->total_tva) . ",";
335
        $sql .= " " . price2num($this->total_localtax1) . ",";
336
        $sql .= " " . price2num($this->total_localtax2) . ",";
337
        $sql .= " " . price2num($this->total_ttc) . ",";
338
        $sql .= ($this->fk_unit ? "'" . $this->db->escape($this->fk_unit) . "'" : "null");
339
        $sql .= ", " . ($this->fk_multicurrency ? ((int) $this->fk_multicurrency) : "null");
340
        $sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'";
341
        $sql .= ", " . ($this->multicurrency_subprice ? price2num($this->multicurrency_subprice) : '0');
342
        $sql .= ", " . ($this->multicurrency_total_ht ? price2num($this->multicurrency_total_ht) : '0');
343
        $sql .= ", " . ($this->multicurrency_total_tva ? price2num($this->multicurrency_total_tva) : '0');
344
        $sql .= ", " . ($this->multicurrency_total_ttc ? price2num($this->multicurrency_total_ttc) : '0');
345
        $sql .= ", " . ((!empty($this->fk_parent_line) && $this->fk_parent_line > 0) ? $this->fk_parent_line : 'null');
346
        $sql .= ")";
347
348
        dol_syslog(get_class($this) . "::insert", LOG_DEBUG);
349
        $resql = $this->db->query($sql);
350
        if ($resql) {
351
            $this->id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);
352
            $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

352
            /** @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...
353
354
            if (!$error) {
355
                $result = $this->insertExtraFields();
356
                if ($result < 0) {
357
                    $error++;
358
                }
359
            }
360
361
            if (!$error && !$notrigger) {
362
                // Call trigger
363
                $result = $this->call_trigger('LINEORDER_SUPPLIER_CREATE', $user);
364
                if ($result < 0) {
365
                    $error++;
366
                }
367
                // End call triggers
368
            }
369
370
            if (!$error) {
371
                $this->db->commit();
372
                return 1;
373
            }
374
375
            foreach ($this->errors as $errmsg) {
376
                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
377
                $this->errors[] = ($this->errors ? ', ' . $errmsg : $errmsg);
378
            }
379
            $this->db->rollback();
380
            return -1 * $error;
381
        } else {
382
            $this->errors[] = $this->db->error();
383
            $this->db->rollback();
384
            return -2;
385
        }
386
    }
387
    /**
388
     *  Update the line object into db
389
     *
390
     *  @param      int     $notrigger      1 = disable triggers
391
     *  @return     int     Return integer <0 si ko, >0 si ok
392
     */
393
    public function update($notrigger = 0)
394
    {
395
        global $user;
396
397
        $error = 0;
398
399
        $this->db->begin();
400
401
        $sql = "UPDATE " . $this->db->prefix() . $this->table_element . " SET";
402
        $sql .= "  description='" . $this->db->escape($this->desc) . "'";
403
        $sql .= ", ref='" . $this->db->escape($this->ref_supplier) . "'";
404
        $sql .= ", subprice='" . price2num($this->subprice) . "'";
405
        //$sql.= ",remise='".price2num($remise)."'";
406
        $sql .= ", remise_percent='" . price2num($this->remise_percent) . "'";
407
408
        $sql .= ", vat_src_code = '" . (empty($this->vat_src_code) ? '' : $this->vat_src_code) . "'";
409
        $sql .= ", tva_tx='" . price2num($this->tva_tx) . "'";
410
        $sql .= ", localtax1_tx='" . price2num($this->localtax1_tx) . "'";
411
        $sql .= ", localtax2_tx='" . price2num($this->localtax2_tx) . "'";
412
        $sql .= ", localtax1_type='" . $this->db->escape($this->localtax1_type) . "'";
413
        $sql .= ", localtax2_type='" . $this->db->escape($this->localtax2_type) . "'";
414
        $sql .= ", qty='" . price2num($this->qty) . "'";
415
        $sql .= ", date_start=" . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null");
416
        $sql .= ", date_end=" . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null");
417
        $sql .= ", info_bits='" . $this->db->escape($this->info_bits) . "'";
418
        $sql .= ", total_ht='" . price2num($this->total_ht) . "'";
419
        $sql .= ", total_tva='" . price2num($this->total_tva) . "'";
420
        $sql .= ", total_localtax1='" . price2num($this->total_localtax1) . "'";
421
        $sql .= ", total_localtax2='" . price2num($this->total_localtax2) . "'";
422
        $sql .= ", total_ttc='" . price2num($this->total_ttc) . "'";
423
        $sql .= ", product_type=" . $this->product_type;
424
        $sql .= ", special_code=" . (!empty($this->special_code) ? $this->special_code : 0);
425
        $sql .= ($this->fk_unit ? ", fk_unit='" . $this->db->escape($this->fk_unit) . "'" : ", fk_unit=null");
426
427
        // Multicurrency
428
        $sql .= ", multicurrency_subprice=" . price2num($this->multicurrency_subprice);
429
        $sql .= ", multicurrency_total_ht=" . price2num($this->multicurrency_total_ht);
430
        $sql .= ", multicurrency_total_tva=" . price2num($this->multicurrency_total_tva);
431
        $sql .= ", multicurrency_total_ttc=" . price2num($this->multicurrency_total_ttc);
432
433
        $sql .= " WHERE rowid = " . ((int) $this->id);
434
435
        dol_syslog(get_class($this) . "::updateline", LOG_DEBUG);
436
        $resql = $this->db->query($sql);
437
        if ($resql) {
438
            if (!$error) {
439
                $result = $this->insertExtraFields();
440
                if ($result < 0) {
441
                    $error++;
442
                }
443
            }
444
445
            if (!$error && !$notrigger) {
446
                // Call trigger
447
                $result = $this->call_trigger('LINEORDER_SUPPLIER_MODIFY', $user);
448
                if ($result < 0) {
449
                    $this->db->rollback();
450
                    return -1;
451
                }
452
                // End call triggers
453
            }
454
455
            if (!$error) {
456
                $this->db->commit();
457
                return 1;
458
            } else {
459
                $this->db->rollback();
460
                return -1;
461
            }
462
        } else {
463
            $this->error = $this->db->lasterror();
464
            $this->db->rollback();
465
            return -1;
466
        }
467
    }
468
469
    /**
470
     *  Delete line in database
471
     *
472
     *  @param      User    $user       User making the change
473
     *  @param      int     $notrigger  1=Disable call to triggers
474
     *  @return     int                 Return integer <0 if KO, >0 if OK
475
     */
476
    public function delete($user, $notrigger = 0)
477
    {
478
        if (empty($user)) {
479
            global $user;
480
        }
481
482
        $error = 0;
483
484
        $this->db->begin();
485
486
        // extrafields
487
        $result = $this->deleteExtraFields();
488
        if ($result < 0) {
489
            $this->db->rollback();
490
            return -1;
491
        }
492
493
        $sql1 = 'UPDATE ' . $this->db->prefix() . "commandedet SET fk_commandefourndet = NULL WHERE rowid=" . ((int) $this->id);
494
        $resql = $this->db->query($sql1);
495
        if (!$resql) {
496
            $this->db->rollback();
497
            return -1;
498
        }
499
500
        $sql2 = 'DELETE FROM ' . $this->db->prefix() . "commande_fournisseurdet WHERE rowid=" . ((int) $this->id);
501
502
        dol_syslog(__METHOD__, LOG_DEBUG);
503
        $resql = $this->db->query($sql2);
504
        if ($resql) {
505
            if (!$notrigger) {
506
                // Call trigger
507
                $result = $this->call_trigger('LINEORDER_SUPPLIER_DELETE', $user);
508
                if ($result < 0) {
509
                    $error++;
510
                }
511
                // End call triggers
512
            }
513
514
            if (!$error) {
515
                $this->db->commit();
516
                return 1;
517
            }
518
519
            foreach ($this->errors as $errmsg) {
520
                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
521
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
522
            }
523
            $this->db->rollback();
524
            return -1 * $error;
525
        } else {
526
            $this->error = $this->db->lasterror();
527
            return -1;
528
        }
529
    }
530
}
531