Passed
Push — EXTRACT_CLASSES ( 0382f2...c25e41 )
by Rafael
52:18
created

PropaleLigne::insert()   F

Complexity

Conditions 45
Paths > 20000

Size

Total Lines 161
Code Lines 115

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 45
eloc 115
nc 3145728
nop 1
dl 0
loc 161
rs 0
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) 2002-2004  Rodolphe Quiedeville        <[email protected]>
4
 * Copyright (C) 2004       Eric Seigne                 <[email protected]>
5
 * Copyright (C) 2004-2011  Laurent Destailleur         <[email protected]>
6
 * Copyright (C) 2005       Marc Barilley               <[email protected]>
7
 * Copyright (C) 2005-2013  Regis Houssin               <[email protected]>
8
 * Copyright (C) 2006       Andre Cianfarani            <[email protected]>
9
 * Copyright (C) 2008       Raphael Bertrand            <[email protected]>
10
 * Copyright (C) 2010-2020  Juanjo Menent               <[email protected]>
11
 * Copyright (C) 2010-2022  Philippe Grand              <[email protected]>
12
 * Copyright (C) 2012-2014  Christophe Battarel         <[email protected]>
13
 * Copyright (C) 2012       Cedric Salvador             <[email protected]>
14
 * Copyright (C) 2013       Florian Henry               <[email protected]>
15
 * Copyright (C) 2014-2015  Marcos García               <[email protected]>
16
 * Copyright (C) 2018       Nicolas ZABOURI             <[email protected]>
17
 * Copyright (C) 2018-2024  Frédéric France             <[email protected]>
18
 * Copyright (C) 2018       Ferran Marcet               <[email protected]>
19
 * Copyright (C) 2022       ATM Consulting              <[email protected]>
20
 * Copyright (C) 2022       OpenDSI                     <[email protected]>
21
 * Copyright (C) 2022      	Gauthier VERDOL     	    <[email protected]>
22
 * Copyright (C) 2023		William Mead			    <[email protected]>
23
 * Copyright (C) 2024		MDW							<[email protected]>
24
 * Copyright (C) 2024       Rafael San José             <[email protected]>
25
 *
26
 * This program is free software; you can redistribute it and/or modify
27
 * it under the terms of the GNU General Public License as published by
28
 * the Free Software Foundation; either version 3 of the License, or
29
 * (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
38
 */
39
40
namespace Dolibarr\Code\Comm\Classes;
41
42
/**
43
 *  \file       htdocs/comm/propal/class/propal.class.php
44
 *  \brief      File of class to manage proposals
45
 */
46
47
use Dolibarr\Code\MultiCurrency\Classes\MultiCurrency;
48
49
use Dolibarr\Core\Base\CommonObject;
50
use Dolibarr\Core\Base\CommonObjectLine;
51
require_once constant('DOL_DOCUMENT_ROOT') . '/product/class/product.class.php';
52
require_once constant('DOL_DOCUMENT_ROOT') . '/contact/class/contact.class.php';
53
require_once constant('DOL_DOCUMENT_ROOT') . '/margin/lib/margins.lib.php';
54
require_once constant('DOL_DOCUMENT_ROOT') . '/core/class/commonincoterm.class.php';
55
56
/**
57
 *  Class to manage commercial proposal lines
58
 */
59
class PropaleLigne extends CommonObjectLine
60
{
61
    /**
62
     * @var string ID to identify managed object
63
     */
64
    public $element = 'propaldet';
65
66
    /**
67
     * @var string Name of table without prefix where object is stored
68
     */
69
    public $table_element = 'propaldet';
70
71
    /**
72
     * @see CommonObjectLine
73
     */
74
    public $parent_element = 'propal';
75
76
    /**
77
     * @see CommonObjectLine
78
     */
79
    public $fk_parent_attribute = 'fk_propal';
80
81
    public $oldline;
82
83
    // From llx_propaldet
84
    public $fk_propal;
85
    public $fk_parent_line;
86
    public $desc; // Description ligne
87
    public $fk_product; // Id produit predefini
88
    /**
89
     * @deprecated
90
     * @see $product_type
91
     */
92
    public $fk_product_type;
93
    /**
94
     * Product type.
95
     * @var int
96
     * @see Product::TYPE_PRODUCT, Product::TYPE_SERVICE
97
     */
98
    public $product_type = Product::TYPE_PRODUCT;
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Comm\Classes\Product was not found. Did you mean Product? If so, make sure to prefix the type with \.
Loading history...
99
100
    public $qty;
101
102
    public $tva_tx;
103
    public $vat_src_code;
104
105
    /**
106
     * Unit price before taxes
107
     * @var float
108
     */
109
    public $subprice;
110
    public $remise_percent;
111
    public $fk_remise_except;
112
113
    public $rang = 0;
114
115
    public $fk_fournprice;
116
    public $pa_ht;
117
    public $marge_tx;
118
    public $marque_tx;
119
120
    /**
121
     * 1: frais de port
122
     * 2: ecotaxe
123
     * 3: option line (when qty = 0)
124
     * @var int special code
125
     */
126
    public $special_code; // Tag for special lines (exclusive tags)
127
128
    public $info_bits = 0; // Some other info:
129
    // Bit 0:   0 si TVA normal - 1 if TVA NPR
130
    // Bit 1:   0 ligne normal - 1 if line with fixed discount
131
132
    public $total_ht; // Total HT  de la ligne toute quantite et incluant la remise ligne
133
    public $total_tva; // Total TVA  de la ligne toute quantite et incluant la remise ligne
134
    public $total_ttc; // Total TTC de la ligne toute quantite et incluant la remise ligne
135
136
    /**
137
     * @deprecated
138
     * @see $remise_percent, $fk_remise_except
139
     */
140
    public $remise;
141
    /**
142
     * @deprecated
143
     * @see $subprice
144
     */
145
    public $price;
146
147
    // From llx_product
148
    /**
149
     * @deprecated
150
     * @see $product_ref
151
     */
152
    public $ref;
153
    /**
154
     * Product reference
155
     * @var string
156
     */
157
    public $product_ref;
158
    /**
159
     * @deprecated
160
     * @see $product_label
161
     */
162
    public $libelle;
163
    /**
164
     * @deprecated
165
     * @see $product_label
166
     */
167
    public $label;
168
    /**
169
     *  Product label
170
     * @var string
171
     */
172
    public $product_label;
173
    /**
174
     * Product description
175
     * @var string
176
     */
177
    public $product_desc;
178
179
    /**
180
     * Product use lot
181
     * @var string
182
     */
183
    public $product_tobatch;
184
185
    /**
186
     * Product barcode
187
     * @var string
188
     */
189
    public $product_barcode;
190
191
    public $localtax1_tx; // Local tax 1
192
    public $localtax2_tx; // Local tax 2
193
    public $localtax1_type; // Local tax 1 type
194
    public $localtax2_type; // Local tax 2 type
195
    public $total_localtax1; // Line total local tax 1
196
    public $total_localtax2; // Line total local tax 2
197
198
    public $date_start;
199
    public $date_end;
200
201
    public $skip_update_total; // Skip update price total for special lines
202
203
    // Multicurrency
204
    public $fk_multicurrency;
205
    public $multicurrency_code;
206
    public $multicurrency_subprice;
207
    public $multicurrency_total_ht;
208
    public $multicurrency_total_tva;
209
    public $multicurrency_total_ttc;
210
211
212
    /**
213
     *  Class line Constructor
214
     *
215
     *  @param  DoliDB  $db Database handler
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Comm\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
216
     */
217
    public function __construct($db)
218
    {
219
        $this->db = $db;
220
    }
221
222
    /**
223
     *  Retrieve the propal line object
224
     *
225
     *  @param  int     $rowid      Propal line id
226
     *  @return int                 Return integer <0 if KO, >0 if OK
227
     */
228
    public function fetch($rowid)
229
    {
230
        $sql = 'SELECT pd.rowid, pd.fk_propal, pd.fk_parent_line, pd.fk_product, pd.label as custom_label, pd.description, pd.price, pd.qty, pd.vat_src_code, pd.tva_tx,';
231
        $sql .= ' pd.remise, pd.remise_percent, pd.fk_remise_except, pd.subprice,';
232
        $sql .= ' pd.info_bits, pd.total_ht, pd.total_tva, pd.total_ttc, pd.fk_product_fournisseur_price as fk_fournprice, pd.buy_price_ht as pa_ht, pd.special_code, pd.rang,';
233
        $sql .= ' pd.fk_unit,';
234
        $sql .= ' pd.localtax1_tx, pd.localtax2_tx, pd.total_localtax1, pd.total_localtax2,';
235
        $sql .= ' pd.fk_multicurrency, pd.multicurrency_code, pd.multicurrency_subprice, pd.multicurrency_total_ht, pd.multicurrency_total_tva, pd.multicurrency_total_ttc,';
236
        $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc,';
237
        $sql .= ' pd.date_start, pd.date_end, pd.product_type';
238
        $sql .= ' FROM ' . MAIN_DB_PREFIX . 'propaldet as pd';
239
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON pd.fk_product = p.rowid';
240
        $sql .= ' WHERE pd.rowid = ' . ((int) $rowid);
241
242
        $result = $this->db->query($sql);
243
        if ($result) {
244
            $objp = $this->db->fetch_object($result);
245
246
            if ($objp) {
247
                $this->id = $objp->rowid;
248
                $this->rowid            = $objp->rowid; // deprecated
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

248
                /** @scrutinizer ignore-deprecated */ $this->rowid            = $objp->rowid; // 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...
249
                $this->fk_propal = $objp->fk_propal;
250
                $this->fk_parent_line = $objp->fk_parent_line;
251
                $this->label            = $objp->custom_label;
252
                $this->desc             = $objp->description;
253
                $this->qty = $objp->qty;
254
                $this->price            = $objp->price; // deprecated
255
                $this->subprice = $objp->subprice;
256
                $this->vat_src_code = $objp->vat_src_code;
257
                $this->tva_tx           = $objp->tva_tx;
258
                $this->remise           = $objp->remise; // deprecated
259
                $this->remise_percent = $objp->remise_percent;
260
                $this->fk_remise_except = $objp->fk_remise_except;
261
                $this->fk_product = $objp->fk_product;
262
                $this->info_bits        = $objp->info_bits;
263
264
                $this->total_ht         = $objp->total_ht;
265
                $this->total_tva        = $objp->total_tva;
266
                $this->total_ttc        = $objp->total_ttc;
267
268
                $this->fk_fournprice = $objp->fk_fournprice;
269
270
                $marginInfos            = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
271
                $this->pa_ht            = $marginInfos[0];
272
                $this->marge_tx         = $marginInfos[1];
273
                $this->marque_tx        = $marginInfos[2];
274
275
                $this->special_code     = $objp->special_code;
276
                $this->product_type     = $objp->product_type;
277
                $this->rang = $objp->rang;
278
279
                $this->ref = $objp->product_ref; // deprecated
280
                $this->product_ref = $objp->product_ref;
281
                $this->libelle = $objp->product_label; // deprecated
282
                $this->product_label    = $objp->product_label;
283
                $this->product_desc     = $objp->product_desc;
284
                $this->fk_unit          = $objp->fk_unit;
285
286
                $this->date_start       = $this->db->jdate($objp->date_start);
287
                $this->date_end         = $this->db->jdate($objp->date_end);
288
289
                // Multicurrency
290
                $this->fk_multicurrency = $objp->fk_multicurrency;
291
                $this->multicurrency_code = $objp->multicurrency_code;
292
                $this->multicurrency_subprice   = $objp->multicurrency_subprice;
293
                $this->multicurrency_total_ht   = $objp->multicurrency_total_ht;
294
                $this->multicurrency_total_tva  = $objp->multicurrency_total_tva;
295
                $this->multicurrency_total_ttc  = $objp->multicurrency_total_ttc;
296
297
                $this->fetch_optionals();
298
299
                $this->db->free($result);
300
301
                return 1;
302
            } else {
303
                return 0;
304
            }
305
        } else {
306
            return -1;
307
        }
308
    }
309
310
    /**
311
     *  Insert object line propal in database
312
     *
313
     *  @param      int     $notrigger      1=Does not execute triggers, 0= execute triggers
314
     *  @return     int                     Return integer <0 if KO, >0 if OK
315
     */
316
    public function insert($notrigger = 0)
317
    {
318
        global $conf, $user;
319
320
        $error = 0;
321
322
        dol_syslog(get_class($this) . "::insert rang=" . $this->rang);
323
324
        $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'.
325
326
        // Clean parameters
327
        if (empty($this->tva_tx)) {
328
            $this->tva_tx = 0;
329
        }
330
        if (empty($this->localtax1_tx)) {
331
            $this->localtax1_tx = 0;
332
        }
333
        if (empty($this->localtax2_tx)) {
334
            $this->localtax2_tx = 0;
335
        }
336
        if (empty($this->localtax1_type)) {
337
            $this->localtax1_type = 0;
338
        }
339
        if (empty($this->localtax2_type)) {
340
            $this->localtax2_type = 0;
341
        }
342
        if (empty($this->total_localtax1)) {
343
            $this->total_localtax1 = 0;
344
        }
345
        if (empty($this->total_localtax2)) {
346
            $this->total_localtax2 = 0;
347
        }
348
        if (empty($this->rang)) {
349
            $this->rang = 0;
350
        }
351
        if (empty($this->remise_percent) || !is_numeric($this->remise_percent)) {
352
            $this->remise_percent = 0;
353
        }
354
        if (empty($this->info_bits)) {
355
            $this->info_bits = 0;
356
        }
357
        if (empty($this->special_code)) {
358
            $this->special_code = 0;
359
        }
360
        if (empty($this->fk_parent_line)) {
361
            $this->fk_parent_line = 0;
362
        }
363
        if (empty($this->fk_fournprice)) {
364
            $this->fk_fournprice = 0;
365
        }
366
        if (!is_numeric($this->qty)) {
367
            $this->qty = 0;
368
        }
369
        if (empty($this->pa_ht)) {
370
            $this->pa_ht = 0;
371
        }
372
        if (empty($this->multicurrency_subprice)) {
373
            $this->multicurrency_subprice = 0;
374
        }
375
        if (empty($this->multicurrency_total_ht)) {
376
            $this->multicurrency_total_ht = 0;
377
        }
378
        if (empty($this->multicurrency_total_tva)) {
379
            $this->multicurrency_total_tva = 0;
380
        }
381
        if (empty($this->multicurrency_total_ttc)) {
382
            $this->multicurrency_total_ttc = 0;
383
        }
384
385
        // if buy price not defined, define buyprice as configured in margin admin
386
        if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
387
            if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
388
                return $result;
389
            } else {
390
                $this->pa_ht = $result;
391
            }
392
        }
393
394
        // Check parameters
395
        if ($this->product_type < 0) {
396
            return -1;
397
        }
398
399
        $this->db->begin();
400
401
        // Insert line into database
402
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'propaldet';
403
        $sql .= ' (fk_propal, fk_parent_line, label, description, fk_product, product_type,';
404
        $sql .= ' fk_remise_except, qty, vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
405
        $sql .= ' subprice, remise_percent, ';
406
        $sql .= ' info_bits, ';
407
        $sql .= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_product_fournisseur_price, buy_price_ht, special_code, rang,';
408
        $sql .= ' fk_unit,';
409
        $sql .= ' date_start, date_end';
410
        $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc)';
411
        $sql .= " VALUES (" . $this->fk_propal . ",";
412
        $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->db->escape($this->fk_parent_line) . "'" : "null") . ",";
413
        $sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ",";
414
        $sql .= " '" . $this->db->escape($this->desc) . "',";
415
        $sql .= " " . ($this->fk_product ? "'" . $this->db->escape($this->fk_product) . "'" : "null") . ",";
416
        $sql .= " '" . $this->db->escape($this->product_type) . "',";
417
        $sql .= " " . ($this->fk_remise_except ? "'" . $this->db->escape($this->fk_remise_except) . "'" : "null") . ",";
418
        $sql .= " " . price2num($this->qty, 'MS') . ",";
419
        $sql .= " " . (empty($this->vat_src_code) ? "''" : "'" . $this->db->escape($this->vat_src_code) . "'") . ",";
420
        $sql .= " " . price2num($this->tva_tx) . ",";
421
        $sql .= " " . price2num($this->localtax1_tx) . ",";
422
        $sql .= " " . price2num($this->localtax2_tx) . ",";
423
        $sql .= " '" . $this->db->escape($this->localtax1_type) . "',";
424
        $sql .= " '" . $this->db->escape($this->localtax2_type) . "',";
425
        $sql .= " " . (price2num($this->subprice) !== '' ? price2num($this->subprice, 'MU') : "null") . ",";
426
        $sql .= " " . price2num($this->remise_percent) . ",";
427
        $sql .= " " . (isset($this->info_bits) ? ((int) $this->info_bits) : "null") . ",";
428
        $sql .= " " . price2num($this->total_ht, 'MT') . ",";
429
        $sql .= " " . price2num($this->total_tva, 'MT') . ",";
430
        $sql .= " " . price2num($this->total_localtax1, 'MT') . ",";
431
        $sql .= " " . price2num($this->total_localtax2, 'MT') . ",";
432
        $sql .= " " . price2num($this->total_ttc, 'MT') . ",";
433
        $sql .= " " . (!empty($this->fk_fournprice) ? "'" . $this->db->escape($this->fk_fournprice) . "'" : "null") . ",";
434
        $sql .= " " . (isset($this->pa_ht) ? "'" . price2num($this->pa_ht) . "'" : "null") . ",";
435
        $sql .= ' ' . ((int) $this->special_code) . ',';
436
        $sql .= ' ' . ((int) $this->rang) . ',';
437
        $sql .= ' ' . (empty($this->fk_unit) ? 'NULL' : ((int) $this->fk_unit)) . ',';
438
        $sql .= " " . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ',';
439
        $sql .= " " . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null");
440
        $sql .= ", " . ($this->fk_multicurrency > 0 ? ((int) $this->fk_multicurrency) : 'null');
441
        $sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'";
442
        $sql .= ", " . price2num($this->multicurrency_subprice, 'CU');
443
        $sql .= ", " . price2num($this->multicurrency_total_ht, 'CT');
444
        $sql .= ", " . price2num($this->multicurrency_total_tva, 'CT');
445
        $sql .= ", " . price2num($this->multicurrency_total_ttc, 'CT');
446
        $sql .= ')';
447
448
        dol_syslog(get_class($this) . '::insert', LOG_DEBUG);
449
        $resql = $this->db->query($sql);
450
        if ($resql) {
451
            $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX . 'propaldet');
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

451
            /** @scrutinizer ignore-deprecated */ $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX . 'propaldet');

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...
452
453
            if (!$error) {
454
                $this->id = $this->rowid;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

454
                $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...
455
                $result = $this->insertExtraFields();
456
                if ($result < 0) {
457
                    $error++;
458
                }
459
            }
460
461
            if (!$error && !$notrigger) {
462
                // Call trigger
463
                $result = $this->call_trigger('LINEPROPAL_INSERT', $user);
464
                if ($result < 0) {
465
                    $this->db->rollback();
466
                    return -1;
467
                }
468
                // End call triggers
469
            }
470
471
            $this->db->commit();
472
            return 1;
473
        } else {
474
            $this->error = $this->db->error() . " sql=" . $sql;
475
            $this->db->rollback();
476
            return -1;
477
        }
478
    }
479
480
    /**
481
     *  Delete line in database
482
     *
483
     *  @param  User    $user       Object user
484
     *  @param  int     $notrigger  1=Does not execute triggers, 0= execute triggers
485
     *  @return  int                Return integer <0 if ko, >0 if ok
486
     */
487
    public function delete(User $user, $notrigger = 0)
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Comm\Classes\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
488
    {
489
        global $conf;
490
491
        $error = 0;
492
        $this->db->begin();
493
494
        if (!$notrigger) {
495
            // Call trigger
496
            $result = $this->call_trigger('LINEPROPAL_DELETE', $user);
497
            if ($result < 0) {
498
                $error++;
499
            }
500
        }
501
        // End call triggers
502
503
        if (!$error) {
504
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "propaldet WHERE rowid = " . ((int) $this->rowid);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

504
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "propaldet 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...
505
            dol_syslog("PropaleLigne::delete", LOG_DEBUG);
506
            if ($this->db->query($sql)) {
507
                // Remove extrafields
508
                if (!$error) {
509
                    $this->id = $this->rowid;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

509
                    $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...
510
                    $result = $this->deleteExtraFields();
511
                    if ($result < 0) {
512
                        $error++;
513
                        dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
514
                    }
515
                }
516
            } else {
517
                $this->error = $this->db->error() . " sql=" . $sql;
518
                $error++;
519
            }
520
        }
521
522
        if ($error) {
523
            $this->db->rollback();
524
            return -1;
525
        } else {
526
            $this->db->commit();
527
            return 1;
528
        }
529
    }
530
531
    /**
532
     *  Update propal line object into DB
533
     *
534
     *  @param  int     $notrigger  1=Does not execute triggers, 0= execute triggers
535
     *  @return int                 Return integer <0 if ko, >0 if ok
536
     */
537
    public function update($notrigger = 0)
538
    {
539
        global $conf, $user;
540
541
        $error = 0;
542
543
        $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'.
544
545
        if (empty($this->id) && !empty($this->rowid)) {
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

545
        if (empty($this->id) && !empty(/** @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...
546
            $this->id = $this->rowid;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

546
            $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...
547
        }
548
549
        // Clean parameters
550
        if (empty($this->tva_tx)) {
551
            $this->tva_tx = 0;
552
        }
553
        if (empty($this->localtax1_tx)) {
554
            $this->localtax1_tx = 0;
555
        }
556
        if (empty($this->localtax2_tx)) {
557
            $this->localtax2_tx = 0;
558
        }
559
        if (empty($this->total_localtax1)) {
560
            $this->total_localtax1 = 0;
561
        }
562
        if (empty($this->total_localtax2)) {
563
            $this->total_localtax2 = 0;
564
        }
565
        if (empty($this->localtax1_type)) {
566
            $this->localtax1_type = 0;
567
        }
568
        if (empty($this->localtax2_type)) {
569
            $this->localtax2_type = 0;
570
        }
571
        if (empty($this->marque_tx)) {
572
            $this->marque_tx = 0;
573
        }
574
        if (empty($this->marge_tx)) {
575
            $this->marge_tx = 0;
576
        }
577
        if (empty($this->price)) {
578
            $this->price = 0; // TODO A virer
579
        }
580
        if (empty($this->remise_percent)) {
581
            $this->remise_percent = 0;
582
        }
583
        if (empty($this->info_bits)) {
584
            $this->info_bits = 0;
585
        }
586
        if (empty($this->special_code)) {
587
            $this->special_code = 0;
588
        }
589
        if (empty($this->fk_parent_line)) {
590
            $this->fk_parent_line = 0;
591
        }
592
        if (empty($this->fk_fournprice)) {
593
            $this->fk_fournprice = 0;
594
        }
595
        if (empty($this->subprice)) {
596
            $this->subprice = 0;
597
        }
598
        if (empty($this->pa_ht)) {
599
            $this->pa_ht = 0;
600
        }
601
602
        // if buy price not defined, define buyprice as configured in margin admin
603
        if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
604
            if (($result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
605
                return $result;
606
            } else {
607
                $this->pa_ht = $result;
608
            }
609
        }
610
611
        $this->db->begin();
612
613
        // Mise a jour ligne en base
614
        $sql = "UPDATE " . MAIN_DB_PREFIX . "propaldet SET";
615
        $sql .= " description='" . $this->db->escape($this->desc) . "'";
616
        $sql .= ", label=" . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null");
617
        $sql .= ", product_type=" . $this->product_type;
618
        $sql .= ", vat_src_code = '" . (empty($this->vat_src_code) ? '' : $this->vat_src_code) . "'";
619
        $sql .= ", tva_tx='" . price2num($this->tva_tx) . "'";
620
        $sql .= ", localtax1_tx=" . price2num($this->localtax1_tx);
621
        $sql .= ", localtax2_tx=" . price2num($this->localtax2_tx);
622
        $sql .= ", localtax1_type='" . $this->db->escape($this->localtax1_type) . "'";
623
        $sql .= ", localtax2_type='" . $this->db->escape($this->localtax2_type) . "'";
624
        $sql .= ", qty='" . price2num($this->qty) . "'";
625
        $sql .= ", subprice=" . price2num($this->subprice);
626
        $sql .= ", remise_percent=" . price2num($this->remise_percent);
627
        $sql .= ", price=" . (float) price2num($this->price); // TODO A virer
628
        $sql .= ", remise=" . (float) price2num($this->remise); // TODO A virer
629
        $sql .= ", info_bits='" . $this->db->escape($this->info_bits) . "'";
630
        if (empty($this->skip_update_total)) {
631
            $sql .= ", total_ht=" . price2num($this->total_ht);
632
            $sql .= ", total_tva=" . price2num($this->total_tva);
633
            $sql .= ", total_ttc=" . price2num($this->total_ttc);
634
            $sql .= ", total_localtax1=" . price2num($this->total_localtax1);
635
            $sql .= ", total_localtax2=" . price2num($this->total_localtax2);
636
        }
637
        $sql .= ", fk_product_fournisseur_price=" . (!empty($this->fk_fournprice) ? "'" . $this->db->escape($this->fk_fournprice) . "'" : "null");
638
        $sql .= ", buy_price_ht=" . price2num($this->pa_ht);
639
        $sql .= ", special_code=" . ((int) $this->special_code);
640
        $sql .= ", fk_parent_line=" . ($this->fk_parent_line > 0 ? (int) $this->fk_parent_line : "null");
641
        if (!empty($this->rang)) {
642
            $sql .= ", rang=" . ((int) $this->rang);
643
        }
644
        $sql .= ", date_start=" . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null");
645
        $sql .= ", date_end=" . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null");
646
        $sql .= ", fk_unit=" . (!$this->fk_unit ? 'NULL' : $this->fk_unit);
647
648
        // Multicurrency
649
        $sql .= ", multicurrency_subprice=" . price2num($this->multicurrency_subprice);
650
        $sql .= ", multicurrency_total_ht=" . price2num($this->multicurrency_total_ht);
651
        $sql .= ", multicurrency_total_tva=" . price2num($this->multicurrency_total_tva);
652
        $sql .= ", multicurrency_total_ttc=" . price2num($this->multicurrency_total_ttc);
653
654
        $sql .= " WHERE rowid = " . ((int) $this->id);
655
656
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
657
        $resql = $this->db->query($sql);
658
        if ($resql) {
659
            if (!$error) {
660
                $result = $this->insertExtraFields();
661
                if ($result < 0) {
662
                    $error++;
663
                }
664
            }
665
666
            if (!$error && !$notrigger) {
667
                // Call trigger
668
                $result = $this->call_trigger('LINEPROPAL_MODIFY', $user);
669
                if ($result < 0) {
670
                    $this->db->rollback();
671
                    return -1;
672
                }
673
                // End call triggers
674
            }
675
676
            $this->db->commit();
677
            return 1;
678
        } else {
679
            $this->error = $this->db->error();
680
            $this->db->rollback();
681
            return -2;
682
        }
683
    }
684
685
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
686
    /**
687
     *  Update DB line fields total_xxx
688
     *  Used by migration
689
     *
690
     *  @return     int     Return integer <0 if KO, >0 if OK
691
     */
692
    public function update_total()
693
    {
694
		// phpcs:enable
695
        $this->db->begin();
696
697
        // Mise a jour ligne en base
698
        $sql = "UPDATE " . MAIN_DB_PREFIX . "propaldet SET";
699
        $sql .= " total_ht=" . price2num($this->total_ht, 'MT');
700
        $sql .= ",total_tva=" . price2num($this->total_tva, 'MT');
701
        $sql .= ",total_ttc=" . price2num($this->total_ttc, 'MT');
702
        $sql .= " WHERE rowid = " . ((int) $this->rowid);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObjectLine::$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

702
        $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...
703
704
        dol_syslog("PropaleLigne::update_total", LOG_DEBUG);
705
706
        $resql = $this->db->query($sql);
707
        if ($resql) {
708
            $this->db->commit();
709
            return 1;
710
        } else {
711
            $this->error = $this->db->error();
712
            $this->db->rollback();
713
            return -2;
714
        }
715
    }
716
}
717