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

SupplierInvoiceLine::update()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 130
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 85
nc 1048578
nop 1
dl 0
loc 130
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-2012	Laurent Destailleur		<[email protected]>
5
 * Copyright (C) 2004		Christophe Combelles	<[email protected]>
6
 * Copyright (C) 2005		Marc Barilley			<[email protected]>
7
 * Copyright (C) 2005-2012	Regis Houssin			<[email protected]>
8
 * Copyright (C) 2010-2020	Juanjo Menent			<[email protected]>
9
 * Copyright (C) 2013-2019	Philippe Grand			<[email protected]>
10
 * Copyright (C) 2013		Florian Henry			<[email protected]>
11
 * Copyright (C) 2014-2016	Marcos García			<[email protected]>
12
 * Copyright (C) 2015		Bahfir Abbes			<[email protected]>
13
 * Copyright (C) 2015-2022	Ferran Marcet			<[email protected]>
14
 * Copyright (C) 2016-2023	Alexandre Spangaro		<[email protected]>
15
 * Copyright (C) 2018       Nicolas ZABOURI			<[email protected]>
16
 * Copyright (C) 2018-2024  Frédéric France         <[email protected]>
17
 * Copyright (C) 2022      	Gauthier VERDOL     	<[email protected]>
18
 * Copyright (C) 2023		Nick Fragoulis
19
 * Copyright (C) 2024		MDW						<[email protected]>
20
 * Copyright (C) 2024       Rafael San José         <[email protected]>
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 3 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
34
 */
35
36
/**
37
 *  \file       htdocs/fourn/class/fournisseur.facture.class.php
38
 *  \ingroup    fournisseur,facture
39
 *  \brief      File of class to manage suppliers invoices
40
 */
41
42
use DoliCore\Base\GenericDocumentLine;
43
44
include_once DOL_DOCUMENT_ROOT . '/core/class/commoninvoice.class.php';
45
require_once DOL_DOCUMENT_ROOT . '/multicurrency/class/multicurrency.class.php';
46
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php';
47
48
if (isModEnabled('accounting')) {
49
    require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
50
    require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
51
}
52
53
/**
54
 *  Class to manage line invoices
55
 */
56
class SupplierInvoiceLine extends GenericDocumentLine
0 ignored issues
show
Deprecated Code introduced by
The class DoliCore\Base\GenericDocumentLine has been deprecated: This class is only needed for compatibility with Dolibarr. ( Ignorable by Annotation )

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

56
class SupplierInvoiceLine extends /** @scrutinizer ignore-deprecated */ GenericDocumentLine
Loading history...
57
{
58
    /**
59
     * @var string ID to identify managed object
60
     */
61
    public $element = 'facture_fourn_det';
62
63
    /**
64
     * @var string Name of table without prefix where object is stored
65
     */
66
    public $table_element = 'facture_fourn_det';
67
68
    public $oldline;
69
70
    /**
71
     * @deprecated
72
     * @see $product_ref
73
     */
74
    public $ref;
75
76
    /**
77
     * Internal ref
78
     * @var string
79
     */
80
    public $product_ref;
81
82
    /**
83
     * Supplier reference of price when we added the line. May have been changed after line was added.
84
     * TODO Rename field ref to ref_supplier into table llx_facture_fourn_det and llx_commande_fournisseurdet and update fields into updateline
85
     * @var string
86
     */
87
    public $ref_supplier;
88
89
    /**
90
     * Product description
91
     * @var string
92
     */
93
    public $product_desc;
94
95
    /**
96
     * Unit price before taxes
97
     * @var float
98
     * @deprecated Use $subprice
99
     * @see $subprice
100
     */
101
    public $pu_ht;
102
103
    /**
104
     * Unit price excluded taxes
105
     * @var float
106
     */
107
    public $subprice;
108
109
    /**
110
     * Unit price included taxes
111
     * @var float
112
     */
113
    public $pu_ttc;
114
115
116
    /**
117
     * Id of the corresponding supplier invoice
118
     * @var int
119
     */
120
    public $fk_facture_fourn;
121
122
    /**
123
     * This field may contains label of line (when invoice create from order)
124
     * @var string
125
     * @deprecated
126
     */
127
    public $label;
128
129
    /**
130
     * Description of the line
131
     * @var string
132
     * @deprecated      Use $desc
133
     */
134
    public $description;
135
136
    public $date_start;
137
    public $date_end;
138
139
    /**
140
     * @var int
141
     */
142
    public $fk_code_ventilation;
143
144
    public $skip_update_total; // Skip update price total for special lines
145
146
    /**
147
     * @var int     Situation progress percentage
148
     */
149
    public $situation_percent;
150
151
    /**
152
     * @var int Previous situation line id reference
153
     */
154
    public $fk_prev_id;
155
156
    /**
157
     * VAT code
158
     * @var string
159
     */
160
    public $vat_src_code;
161
162
    /**
163
     * VAT %
164
     * @var float
165
     */
166
    public $tva_tx;
167
168
    /**
169
     * Local tax 1 %
170
     * @var float
171
     */
172
    public $localtax1_tx;
173
174
    /**
175
     * Local tax 2 %
176
     * @var float
177
     */
178
    public $localtax2_tx;
179
180
    /**
181
     * Quantity
182
     * @var double
183
     */
184
    public $qty;
185
186
    /**
187
     * Percent of discount
188
     * @var float
189
     */
190
    public $remise_percent;
191
192
    /**
193
     * Buying price value
194
     * @var float
195
     */
196
    public $pa_ht;
197
198
    /**
199
     * Total amount without taxes
200
     * @var float
201
     */
202
    public $total_ht;
203
204
    /**
205
     * Total amount with taxes
206
     * @var float
207
     */
208
    public $total_ttc;
209
210
    /**
211
     * Total amount of taxes
212
     * @var float
213
     */
214
    public $total_tva;
215
216
    /**
217
     * Total local tax 1 amount
218
     * @var float
219
     */
220
    public $total_localtax1;
221
222
    /**
223
     * Total local tax 2 amount
224
     * @var float
225
     */
226
    public $total_localtax2;
227
228
    /**
229
     * @var int ID
230
     */
231
    public $fk_product;
232
233
    /**
234
     * Type of the product. 0 for product 1 for service
235
     * @var int
236
     */
237
    public $product_type;
238
239
    /**
240
     * Label of the product
241
     * @var string
242
     */
243
    public $product_label;
244
245
    /**
246
     * List of cumulative options:
247
     * Bit 0:   0 si TVA normal - 1 si TVA NPR
248
     * Bit 1:   0 si ligne normal - 1 si bit discount (link to line into llx_remise_except)
249
     * @var int
250
     */
251
    public $info_bits;
252
253
    /**
254
     * Link to line into llx_remise_except
255
     * @var int
256
     */
257
    public $fk_remise_except;
258
259
    /**
260
     * @var int ID
261
     */
262
    public $fk_parent_line;
263
264
    public $special_code;
265
266
    /**
267
     * @var int rank of line
268
     */
269
    public $rang;
270
271
    /**
272
     * Total local tax 1 amount
273
     * @var float
274
     */
275
    public $localtax1_type;
276
277
    /**
278
     * Total local tax 2 amount
279
     * @var float
280
     */
281
    public $localtax2_type;
282
283
284
    /**
285
     *  Constructor
286
     *
287
     *  @param      DoliDB      $db      Database handler
288
     */
289
    public function __construct($db)
290
    {
291
        $this->db = $db;
292
    }
293
294
    /**
295
     * Retrieves a supplier invoice line
296
     *
297
     * @param    int    $rowid    Line id
298
     * @return   int              Return integer <0 KO; 0 NOT FOUND; 1 OK
299
     */
300
    public function fetch($rowid)
301
    {
302
        $sql = 'SELECT f.rowid, f.ref as ref_supplier, f.description as line_desc, f.date_start, f.date_end, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.tva_tx';
303
        $sql .= ', f.localtax1_type, f.localtax2_type, f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2, f.fk_remise_except';
304
        $sql .= ', f.total_ht, f.tva as total_tva, f.total_ttc, f.fk_facture_fourn, f.fk_product, f.product_type, f.info_bits, f.rang, f.special_code, f.fk_parent_line, f.fk_unit';
305
        $sql .= ', p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc';
306
        $sql .= ', f.multicurrency_subprice, f.multicurrency_total_ht, f.multicurrency_total_tva, multicurrency_total_ttc';
307
        $sql .= ' FROM ' . MAIN_DB_PREFIX . 'facture_fourn_det as f';
308
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product as p ON f.fk_product = p.rowid';
309
        $sql .= ' WHERE f.rowid = ' . ((int) $rowid);
310
        $sql .= ' ORDER BY f.rang, f.rowid';
311
312
        $query = $this->db->query($sql);
313
314
        if (!$query) {
315
            $this->errors[] = $this->db->error();
316
            return -1;
317
        }
318
319
        if (!$this->db->num_rows($query)) {
320
            return 0;
321
        }
322
323
        $obj = $this->db->fetch_object($query);
324
325
        $this->id = $obj->rowid;
326
        $this->rowid = $obj->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

326
        /** @scrutinizer ignore-deprecated */ $this->rowid = $obj->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...
327
        $this->fk_facture_fourn = $obj->fk_facture_fourn;
328
        $this->description      = $obj->line_desc;
0 ignored issues
show
Deprecated Code introduced by
The property SupplierInvoiceLine::$description has been deprecated: Use $desc ( Ignorable by Annotation )

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

328
        /** @scrutinizer ignore-deprecated */ $this->description      = $obj->line_desc;

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...
329
        $this->desc             = $obj->line_desc;
330
        $this->date_start = $obj->date_start;
331
        $this->date_end = $obj->date_end;
332
        $this->product_ref      = $obj->product_ref;
333
        $this->ref_supplier     = $obj->ref_supplier;
334
        $this->product_desc     = $obj->product_desc;
335
336
        $this->subprice = $obj->pu_ht;
337
        $this->pu_ht = $obj->pu_ht;
0 ignored issues
show
Deprecated Code introduced by
The property SupplierInvoiceLine::$pu_ht has been deprecated: Use $subprice ( Ignorable by Annotation )

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

337
        /** @scrutinizer ignore-deprecated */ $this->pu_ht = $obj->pu_ht;

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...
338
        $this->pu_ttc           = $obj->pu_ttc;
339
        $this->tva_tx           = $obj->tva_tx;
340
        $this->localtax1_tx     = $obj->localtax1_tx;
341
        $this->localtax2_tx     = $obj->localtax2_tx;
342
        $this->localtax1_type   = $obj->localtax1_type;
343
        $this->localtax2_type   = $obj->localtax2_type;
344
345
        $this->qty              = $obj->qty;
346
        $this->remise_percent = $obj->remise_percent;
347
        $this->fk_remise_except = $obj->fk_remise_except;
348
        //$this->tva                = $obj->total_tva; // deprecated
349
        $this->total_ht = $obj->total_ht;
350
        $this->total_tva            = $obj->total_tva;
351
        $this->total_localtax1  = $obj->total_localtax1;
352
        $this->total_localtax2  = $obj->total_localtax2;
353
        $this->total_ttc            = $obj->total_ttc;
354
        $this->fk_product       = $obj->fk_product;
355
        $this->product_type = $obj->product_type;
356
        $this->product_label        = $obj->product_label;
357
        $this->info_bits            = $obj->info_bits;
358
        $this->fk_parent_line    = $obj->fk_parent_line;
359
        $this->special_code = $obj->special_code;
360
        $this->rang = $obj->rang;
361
        $this->fk_unit           = $obj->fk_unit;
362
363
        $this->multicurrency_subprice = $obj->multicurrency_subprice;
364
        $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
365
        $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
366
        $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
367
368
        $this->fetch_optionals();
369
370
        return 1;
371
    }
372
373
    /**
374
     * Deletes a line
375
     *
376
     * @param     int   $notrigger     1=Does not execute triggers, 0=execute triggers
377
     * @return    int                  0 if KO, 1 if OK
378
     */
379
    public function delete($notrigger = 0)
380
    {
381
        global $user;
382
383
        dol_syslog(get_class($this) . "::deleteline rowid=" . ((int) $this->id), LOG_DEBUG);
384
385
        $error = 0;
386
387
        $this->db->begin();
388
389
        if (!$notrigger) {
390
            if ($this->call_trigger('LINEBILL_SUPPLIER_DELETE', $user) < 0) {
391
                $error++;
392
            }
393
        }
394
395
        $this->deleteObjectLinked();
396
397
        // Remove extrafields
398
        if (!$error) {
399
            $result = $this->deleteExtraFields();
400
            if ($result < 0) {
401
                $error++;
402
                dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
403
            }
404
        }
405
406
        if (!$error) {
407
            // Supprime ligne
408
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . 'facture_fourn_det ';
409
            $sql .= " WHERE rowid = " . ((int) $this->id);
410
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
411
            $resql = $this->db->query($sql);
412
            if (!$resql) {
413
                $error++;
414
                $this->error = $this->db->lasterror();
415
            }
416
        }
417
418
        if (!$error) {
419
            $this->db->commit();
420
            return 1;
421
        } else {
422
            $this->db->rollback();
423
            return -1;
424
        }
425
    }
426
427
    /**
428
     * Update a supplier invoice line
429
     *
430
     * @param int $notrigger Disable triggers
431
     * @return int Return integer <0 if KO, >0 if OK
432
     */
433
    public function update($notrigger = 0)
434
    {
435
        global $conf;
436
437
        $pu = price2num($this->pu_ht);
0 ignored issues
show
Deprecated Code introduced by
The property SupplierInvoiceLine::$pu_ht has been deprecated: Use $subprice ( Ignorable by Annotation )

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

437
        $pu = price2num(/** @scrutinizer ignore-deprecated */ $this->pu_ht);

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...
438
        $qty = price2num($this->qty);
439
440
        // Check parameters
441
        if (empty($this->qty)) {
442
            $this->qty = 0;
443
        }
444
445
        if ($this->product_type < 0) {
446
            return -1;
447
        }
448
449
        // Clean parameters
450
        if (empty($this->remise_percent)) {
451
            $this->remise_percent = 0;
452
        }
453
        if (empty($this->tva_tx)) {
454
            $this->tva_tx = 0;
455
        }
456
        if (empty($this->localtax1_tx)) {
457
            $this->localtax1_tx = 0;
458
        }
459
        if (empty($this->localtax2_tx)) {
460
            $this->localtax2_tx = 0;
461
        }
462
463
        if (empty($this->pa_ht)) {
464
            $this->pa_ht = 0;
465
        }
466
        if (empty($this->multicurrency_subprice)) {
467
            $this->multicurrency_subprice = 0;
468
        }
469
        if (empty($this->multicurrency_total_ht)) {
470
            $this->multicurrency_total_ht = 0;
471
        }
472
        if (empty($this->multicurrency_total_tva)) {
473
            $this->multicurrency_total_tva = 0;
474
        }
475
        if (empty($this->multicurrency_total_ttc)) {
476
            $this->multicurrency_total_ttc = 0;
477
        }
478
479
        $fk_product = (int) $this->fk_product;
480
        $fk_unit = (int) $this->fk_unit;
481
482
        $this->db->begin();
483
484
        $sql = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det SET";
485
        $sql .= " description = '" . $this->db->escape(empty($this->description) ? $this->desc : $this->description) . "'";
0 ignored issues
show
Deprecated Code introduced by
The property SupplierInvoiceLine::$description has been deprecated: Use $desc ( Ignorable by Annotation )

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

485
        $sql .= " description = '" . $this->db->escape(empty($this->description) ? $this->desc : /** @scrutinizer ignore-deprecated */ $this->description) . "'";

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...
486
        $sql .= ", ref = '" . $this->db->escape($this->ref_supplier ? $this->ref_supplier : $this->ref) . "'";
487
        $sql .= ", date_start = " . ($this->date_start != '' ? "'" . $this->db->idate($this->date_start) . "'" : "null");
488
        $sql .= ", date_end = " . ($this->date_end != '' ? "'" . $this->db->idate($this->date_end) . "'" : "null");
489
        $sql .= ", pu_ht = " . price2num($this->pu_ht);
0 ignored issues
show
Deprecated Code introduced by
The property SupplierInvoiceLine::$pu_ht has been deprecated: Use $subprice ( Ignorable by Annotation )

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

489
        $sql .= ", pu_ht = " . price2num(/** @scrutinizer ignore-deprecated */ $this->pu_ht);

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...
490
        $sql .= ", pu_ttc = " . price2num($this->pu_ttc);
491
        $sql .= ", qty = " . price2num($this->qty);
492
        $sql .= ", remise_percent = " . price2num($this->remise_percent);
493
        if ($this->fk_remise_except > 0) {
494
            $sql .= ", fk_remise_except=" . ((int) $this->fk_remise_except);
495
        } else {
496
            $sql .= ", fk_remise_except=null";
497
        }
498
        $sql .= ", vat_src_code = '" . $this->db->escape(empty($this->vat_src_code) ? '' : $this->vat_src_code) . "'";
499
        $sql .= ", tva_tx = " . price2num($this->tva_tx);
500
        $sql .= ", localtax1_tx = " . price2num($this->localtax1_tx);
501
        $sql .= ", localtax2_tx = " . price2num($this->localtax2_tx);
502
        $sql .= ", localtax1_type = '" . $this->db->escape($this->localtax1_type) . "'";
503
        $sql .= ", localtax2_type = '" . $this->db->escape($this->localtax2_type) . "'";
504
        $sql .= ", total_ht = " . price2num($this->total_ht);
505
        $sql .= ", tva= " . price2num($this->total_tva);
506
        $sql .= ", total_localtax1= " . price2num($this->total_localtax1);
507
        $sql .= ", total_localtax2= " . price2num($this->total_localtax2);
508
        $sql .= ", total_ttc = " . price2num($this->total_ttc);
509
        $sql .= ", fk_product = " . ($fk_product > 0 ? (int) $fk_product : 'null');
510
        $sql .= ", product_type = " . ((int) $this->product_type);
511
        $sql .= ", info_bits = " . ((int) $this->info_bits);
512
        $sql .= ", fk_unit = " . ($fk_unit > 0 ? (int) $fk_unit : 'null');
513
514
        if (!empty($this->rang)) {
515
            $sql .= ", rang=" . ((int) $this->rang);
516
        }
517
518
        // Multicurrency
519
        $sql .= " , multicurrency_subprice=" . price2num($this->multicurrency_subprice);
520
        $sql .= " , multicurrency_total_ht=" . price2num($this->multicurrency_total_ht);
521
        $sql .= " , multicurrency_total_tva=" . price2num($this->multicurrency_total_tva);
522
        $sql .= " , multicurrency_total_ttc=" . price2num($this->multicurrency_total_ttc);
523
524
        $sql .= " WHERE rowid = " . ((int) $this->id);
525
526
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
527
        $resql = $this->db->query($sql);
528
529
        if (!$resql) {
530
            $this->db->rollback();
531
            $this->error = $this->db->lasterror();
532
            return -1;
533
        }
534
535
        $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

535
        /** @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...
536
        $error = 0;
537
538
        if (!$error) {
539
            $result = $this->insertExtraFields();
540
            if ($result < 0) {
541
                $error++;
542
            }
543
        }
544
545
        if (!$error && !$notrigger) {
546
            global $langs, $user;
547
548
            // Call trigger
549
            if ($this->call_trigger('LINEBILL_SUPPLIER_MODIFY', $user) < 0) {
550
                $this->db->rollback();
551
                return -1;
552
            }
553
            // End call triggers
554
        }
555
556
        if ($error) {
557
            $this->db->rollback();
558
            return -1;
559
        }
560
561
        $this->db->commit();
562
        return 1;
563
    }
564
565
    /**
566
     *  Insert line into database
567
     *
568
     *  @param      int     $notrigger                          1 no triggers
569
     *  @param      int     $noerrorifdiscountalreadylinked     1=Do not make error if lines is linked to a discount and discount already linked to another
570
     *  @return     int                                         Return integer <0 if KO, >0 if OK
571
     */
572
    public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
573
    {
574
        global $user, $langs;
575
576
        $error = 0;
577
578
        dol_syslog(get_class($this) . "::insert rang=" . $this->rang, LOG_DEBUG);
579
580
        // Clean parameters
581
        $this->desc = trim($this->desc);
582
        if (empty($this->tva_tx)) {
583
            $this->tva_tx = 0;
584
        }
585
        if (empty($this->localtax1_tx)) {
586
            $this->localtax1_tx = 0;
587
        }
588
        if (empty($this->localtax2_tx)) {
589
            $this->localtax2_tx = 0;
590
        }
591
        if (empty($this->localtax1_type)) {
592
            $this->localtax1_type = 0.0;
593
        }
594
        if (empty($this->localtax2_type)) {
595
            $this->localtax2_type = 0.0;
596
        }
597
        if (empty($this->total_tva)) {
598
            $this->total_tva = 0;
599
        }
600
        if (empty($this->total_localtax1)) {
601
            $this->total_localtax1 = 0;
602
        }
603
        if (empty($this->total_localtax2)) {
604
            $this->total_localtax2 = 0;
605
        }
606
        if (empty($this->rang)) {
607
            $this->rang = 0;
608
        }
609
        if (empty($this->remise_percent)) {
610
            $this->remise_percent = 0;
611
        }
612
        if (empty($this->info_bits)) {
613
            $this->info_bits = 0;
614
        }
615
        if (empty($this->subprice)) {
616
            $this->subprice = 0;
617
        }
618
        if (empty($this->special_code)) {
619
            $this->special_code = 0;
620
        }
621
        if (empty($this->fk_parent_line)) {
622
            $this->fk_parent_line = 0;
623
        }
624
        if (!isset($this->situation_percent) || $this->situation_percent > 100 || (string) $this->situation_percent == '') {
625
            $this->situation_percent = 100;
626
        }
627
628
        if (empty($this->pa_ht)) {
629
            $this->pa_ht = 0;
630
        }
631
        if (empty($this->multicurrency_subprice)) {
632
            $this->multicurrency_subprice = 0;
633
        }
634
        if (empty($this->multicurrency_total_ht)) {
635
            $this->multicurrency_total_ht = 0;
636
        }
637
        if (empty($this->multicurrency_total_tva)) {
638
            $this->multicurrency_total_tva = 0;
639
        }
640
        if (empty($this->multicurrency_total_ttc)) {
641
            $this->multicurrency_total_ttc = 0;
642
        }
643
644
645
        // Check parameters
646
        if ($this->product_type < 0) {
647
            $this->error = 'ErrorProductTypeMustBe0orMore';
648
            return -1;
649
        }
650
        if (!empty($this->fk_product) && $this->fk_product > 0) {
651
            // Check product exists
652
            $result = Product::isExistingObject('product', $this->fk_product);
653
            if ($result <= 0) {
654
                $this->error = 'ErrorProductIdDoesNotExists';
655
                return -1;
656
            }
657
        }
658
659
        $this->db->begin();
660
661
        // Insertion dans base de la ligne
662
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element;
663
        $sql .= ' (fk_facture_fourn, fk_parent_line, label, description, ref, qty,';
664
        $sql .= ' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
665
        $sql .= ' fk_product, product_type, remise_percent, fk_remise_except, pu_ht, pu_ttc,';
666
        $sql .= ' date_start, date_end, fk_code_ventilation, rang, special_code,';
667
        $sql .= ' info_bits, total_ht, tva, total_ttc, total_localtax1, total_localtax2, fk_unit';
668
        $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
669
        $sql .= ')';
670
        $sql .= " VALUES (" . $this->fk_facture_fourn . ",";
671
        $sql .= " " . ($this->fk_parent_line > 0 ? "'" . $this->db->escape($this->fk_parent_line) . "'" : "null") . ",";
672
        $sql .= " " . (!empty($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ",";
673
        $sql .= " '" . $this->db->escape($this->desc ? $this->desc : $this->description) . "',";
674
        $sql .= " '" . $this->db->escape($this->ref_supplier) . "',";
675
        $sql .= " " . price2num($this->qty) . ",";
676
677
        $sql .= " " . (empty($this->vat_src_code) ? "''" : "'" . $this->db->escape($this->vat_src_code) . "'") . ",";
678
        $sql .= " " . price2num($this->tva_tx) . ",";
679
        $sql .= " " . price2num($this->localtax1_tx) . ",";
680
        $sql .= " " . price2num($this->localtax2_tx) . ",";
681
        $sql .= " '" . $this->db->escape($this->localtax1_type) . "',";
682
        $sql .= " '" . $this->db->escape($this->localtax2_type) . "',";
683
        $sql .= ' ' . ((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product : "null") . ',';
684
        $sql .= " " . ((int) $this->product_type) . ",";
685
        $sql .= " " . price2num($this->remise_percent) . ",";
686
        $sql .= ' ' . (!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) : "null") . ',';
687
        $sql .= " " . price2num($this->subprice) . ",";
688
        $sql .= " " . (!empty($this->qty) ? price2num($this->total_ttc / $this->qty) : price2num($this->total_ttc)) . ",";
689
        $sql .= " " . (!empty($this->date_start) ? "'" . $this->db->idate($this->date_start) . "'" : "null") . ",";
690
        $sql .= " " . (!empty($this->date_end) ? "'" . $this->db->idate($this->date_end) . "'" : "null") . ",";
691
        $sql .= ' ' . (!empty($this->fk_code_ventilation) ? $this->fk_code_ventilation : 0) . ',';
692
        $sql .= ' ' . ((int) $this->rang) . ',';
693
        $sql .= ' ' . ((int) $this->special_code) . ',';
694
        $sql .= " " . ((int) $this->info_bits) . ",";
695
        $sql .= " " . price2num($this->total_ht) . ",";
696
        $sql .= " " . price2num($this->total_tva) . ",";
697
        $sql .= " " . price2num($this->total_ttc) . ",";
698
        $sql .= " " . price2num($this->total_localtax1) . ",";
699
        $sql .= " " . price2num($this->total_localtax2);
700
        $sql .= ", " . (!$this->fk_unit ? 'NULL' : $this->fk_unit);
701
        $sql .= ", " . (int) $this->fk_multicurrency;
702
        $sql .= ", '" . $this->db->escape($this->multicurrency_code) . "'";
703
        $sql .= ", " . price2num($this->multicurrency_subprice);
704
        $sql .= ", " . price2num($this->multicurrency_total_ht);
705
        $sql .= ", " . price2num($this->multicurrency_total_tva);
706
        $sql .= ", " . price2num($this->multicurrency_total_ttc);
707
        $sql .= ')';
708
709
        $resql = $this->db->query($sql);
710
        if ($resql) {
711
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
712
            $this->rowid = $this->id; // backward compatibility
713
714
            if (!$error) {
715
                $result = $this->insertExtraFields();
716
                if ($result < 0) {
717
                    $error++;
718
                }
719
            }
720
721
            // Si fk_remise_except defini, on lie la remise a la facture
722
            // ce qui la flague comme "consommee".
723
            if ($this->fk_remise_except) {
724
                $discount = new DiscountAbsolute($this->db);
725
                $result = $discount->fetch($this->fk_remise_except);
726
                if ($result >= 0) {
727
                    // Check if discount was found
728
                    if ($result > 0) {
729
                        // Check if discount not already affected to another invoice
730
                        if ($discount->fk_facture_line > 0) {
731
                            if (empty($noerrorifdiscountalreadylinked)) {
732
                                $this->error = $langs->trans("ErrorDiscountAlreadyUsed", $discount->id);
733
                                dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
734
                                $this->db->rollback();
735
                                return -3;
736
                            }
737
                        } else {
738
                            $result = $discount->link_to_invoice($this->rowid, 0);
739
                            if ($result < 0) {
740
                                $this->error = $discount->error;
741
                                dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
742
                                $this->db->rollback();
743
                                return -3;
744
                            }
745
                        }
746
                    } else {
747
                        $this->error = $langs->trans("ErrorADiscountThatHasBeenRemovedIsIncluded");
748
                        dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
749
                        $this->db->rollback();
750
                        return -3;
751
                    }
752
                } else {
753
                    $this->error = $discount->error;
754
                    dol_syslog(get_class($this) . "::insert Error " . $this->error, LOG_ERR);
755
                    $this->db->rollback();
756
                    return -3;
757
                }
758
            }
759
760
            if (!$error && !$notrigger) {
761
                // Call trigger
762
                $result = $this->call_trigger('LINEBILL_SUPPLIER_CREATE', $user);
763
                if ($result < 0) {
764
                    $this->db->rollback();
765
                    return -2;
766
                }
767
                // End call triggers
768
            }
769
770
            $this->db->commit();
771
            return $this->id;
772
        } else {
773
            $this->error = $this->db->error();
774
            $this->db->rollback();
775
            return -2;
776
        }
777
    }
778
779
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
780
    /**
781
     *  Mise a jour de l'objet ligne de commande en base
782
     *
783
     *  @return     int     Return integer <0 si ko, >0 si ok
784
     */
785
    public function update_total()
786
    {
787
		// phpcs:enable
788
        $this->db->begin();
789
790
        // Mise a jour ligne en base
791
        $sql = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det SET";
792
        $sql .= "  total_ht = " . price2num($this->total_ht);
793
        $sql .= ", tva= " . price2num($this->total_tva);
794
        $sql .= ", total_localtax1 = " . price2num($this->total_localtax1);
795
        $sql .= ", total_localtax2 = " . price2num($this->total_localtax2);
796
        $sql .= ", total_ttc = " . price2num($this->total_ttc);
797
        $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

797
        $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...
798
799
        dol_syslog("FactureFournisseurLigne.class.php::update_total", LOG_DEBUG);
800
801
        $resql = $this->db->query($sql);
802
        if ($resql) {
803
            $this->db->commit();
804
            return 1;
805
        } else {
806
            $this->error = $this->db->error();
807
            $this->db->rollback();
808
            return -2;
809
        }
810
    }
811
}
812