dolibarr 24.0.1
orderline.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
6 * Copyright (C) 2010-2020 Juanjo Menent <jmenent@2byte.es>
7 * Copyright (C) 2011 Jean Heimburger <jean@tiaris.info>
8 * Copyright (C) 2012-2014 Christophe Battarel <christophe.battarel@altairis.fr>
9 * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
10 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
11 * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
12 * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
13 * Copyright (C) 2016-2022 Ferran Marcet <fmarcet@2byte.es>
14 * Copyright (C) 2021-2025 Frédéric France <frederic.france@free.fr>
15 * Copyright (C) 2022 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
16 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
17 * Copyright (C) 2024 William Mead <william.mead@manchenumerique.fr>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 */
32
39require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
40require_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php'; // Because the CommonOrderLine is still in commonorder.class.php file
41require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
42
43
48{
52 public $element = 'commandedet';
53
57 public $table_element = 'commandedet';
58
62 public $oldline;
63
68 public $fk_commande;
69
76 public $commande_id;
77
81 public $fk_parent_line;
82
86 public $fk_facture;
87
91 public $ref_ext;
92
96 public $fk_remise_except;
97
101 public $rang = 0;
102
106 public $fk_fournprice;
107
112 public $pa_ht;
113
117 public $marge_tx;
118
122 public $marque_tx;
123
129 public $remise;
130
135 public $date_start;
136
141 public $date_end;
142
147 public $skip_update_total;
148
152 public $packaging;
153
154
155 // BEGIN MODULEBUILDER PROPERTIES
159 public $fields = array(
160 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
161 'fk_commande' => array('type' => 'integer:Commande:commande/class/commande.class.php', 'label' => 'Order', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15),
162 'fk_parent_line' => array('type' => 'integer:OrderLine:commande/class/orderline.class.php', 'label' => 'ParentLine', 'enabled' => 1, 'visible' => -1, 'position' => 20),
163 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'ProductOrService', 'enabled' => "isModEnabled('product') || isModEnabled('service')", 'visible' => -1, 'position' => 25),
164 'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'position' => 30),
165 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => -1, 'position' => 35, 'searchall' => 1),
166 'vat_src_code' => array('type' => 'varchar(10)', 'label' => 'VatSrcCode', 'enabled' => 1, 'visible' => -1, 'position' => 40),
167 'tva_tx' => array('type' => 'double(7,4)', 'label' => 'VATRate', 'enabled' => 1, 'visible' => -1, 'position' => 45),
168 'localtax1_tx' => array('type' => 'double(7,4)', 'label' => 'LocalTax1Rate', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 50),
169 'localtax1_type' => array('type' => 'varchar(10)', 'label' => 'LocalTax1Type', 'enabled' => 1, 'visible' => -1, 'position' => 55),
170 'localtax2_tx' => array('type' => 'double(7,4)', 'label' => 'LocalTax2Rate', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 60),
171 'localtax2_type' => array('type' => 'varchar(10)', 'label' => 'LocalTax2Type', 'enabled' => 1, 'visible' => -1, 'position' => 65),
172 'qty' => array('type' => 'real', 'label' => 'Qty', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
173 'remise_percent' => array('type' => 'real', 'label' => 'ReductionPercent', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 75),
174 'remise' => array('type' => 'real', 'label' => 'Remise', 'enabled' => 0, 'visible' => -2, 'default' => '0', 'position' => 80),
175 'fk_remise_except' => array('type' => 'integer:DiscountAbsolute:core/class/discount.class.php', 'label' => 'DiscountAbsolute', 'enabled' => 1, 'visible' => -1, 'position' => 85),
176 'price' => array('type' => 'real', 'label' => 'Price', 'enabled' => 0, 'visible' => -2, 'position' => 90),
177 'subprice' => array('type' => 'double(24,8)', 'label' => 'SubPrice', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 95),
178 'subprice_ttc' => array('type' => 'double(24,8)', 'label' => 'SubPriceTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 100),
179 'total_ht' => array('type' => 'double(24,8)', 'label' => 'TotalHT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 105, 'isameasure' => 1),
180 'total_tva' => array('type' => 'double(24,8)', 'label' => 'VAT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 110, 'isameasure' => 1),
181 'total_localtax1' => array('type' => 'double(24,8)', 'label' => 'LocalTax1', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 115, 'isameasure' => 1),
182 'total_localtax2' => array('type' => 'double(24,8)', 'label' => 'LocalTax2', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 120, 'isameasure' => 1),
183 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'TotalTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 125, 'isameasure' => 1),
184 'product_type' => array('type' => 'integer', 'label' => 'TypeOfLineServiceOrProduct', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 130),
185 'date_start' => array('type' => 'datetime', 'label' => 'DateStart', 'enabled' => 1, 'visible' => -1, 'position' => 135),
186 'date_end' => array('type' => 'datetime', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => -1, 'position' => 140),
187 'info_bits' => array('type' => 'integer', 'label' => 'InfoBits', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 145),
188 'buy_price_ht' => array('type' => 'double(24,8)', 'label' => 'BuyingPrice', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 150),
189 'fk_product_fournisseur_price' => array('type' => 'integer:ProductFournisseur:fourn/class/fournisseur.product.class.php', 'label' => 'SupplierPriceReference', 'enabled' => 1, 'visible' => -1, 'position' => 155),
190 'special_code' => array('type' => 'integer', 'label' => 'SpecialCode', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 160),
191 'rang' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 165),
192 'fk_unit' => array('type' => 'integer:CUnits:core/class/cunits.class.php', 'label' => 'Unit', 'enabled' => 1, 'visible' => -1, 'position' => 170),
193 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 175),
194 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 180),
195 'fk_commandefourndet' => array('type' => 'integer:CommandeFournisseurLigne:fourn/class/fournisseur.commande.class.php', 'label' => 'SupplierOrderLine', 'enabled' => 1, 'visible' => -1, 'position' => 185),
196 'fk_multicurrency' => array('type' => 'integer:MultiCurrency:multicurrency/class/multicurrency.class.php', 'label' => 'Currency', 'enabled' => 1, 'visible' => -1, 'position' => 190),
197 'multicurrency_code' => array('type' => 'varchar(3)', 'label' => 'CurrencyCode', 'enabled' => 1, 'visible' => -1, 'position' => 195),
198 'multicurrency_subprice' => array('type' => 'double(24,8)', 'label' => 'CurrencyUnitPrice', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 200),
199 'multicurrency_subprice_ttc' => array('type' => 'double(24,8)', 'label' => 'CurrencyUnitPriceTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 205),
200 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalHT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 210),
201 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalVAT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 215),
202 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 220),
203 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 225),
204 );
205 // END MODULEBUILDER PROPERTIES
206
207
213 public function __construct($db)
214 {
215 $this->db = $db;
216 }
217
224 public function fetch($rowid)
225 {
226 $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_parent_line, cd.fk_product, cd.product_type, cd.label as custom_label, cd.description, cd.price, cd.qty, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx,';
227 $sql .= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
228 $sql .= ' cd.info_bits, cd.total_ht, cd.total_tva, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.fk_product_fournisseur_price as fk_fournprice, cd.buy_price_ht as pa_ht, cd.rang, cd.special_code,';
229 $sql .= ' cd.fk_unit,';
230 $sql .= ' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
231 $sql .= ' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,p.barcode as product_barcode,';
232 $sql .= ' p.customcode, p.fk_country as country_id, c.code as country_code,';
233 $sql .= ' p.packaging,';
234 $sql .= ' cd.date_start, cd.date_end, cd.vat_src_code, cd.extraparams';
235 $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as cd';
236 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
237 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON c.rowid = p.fk_country';
238 $sql .= ' WHERE cd.rowid = '.((int) $rowid);
239 $result = $this->db->query($sql);
240 if ($result) {
241 $objp = $this->db->fetch_object($result);
242
243 if (!$objp) {
244 $this->error = 'OrderLine with id '. $rowid .' not found sql='.$sql;
245 return 0;
246 }
247
248 $this->rowid = $objp->rowid;
249 $this->id = $objp->rowid;
250 $this->fk_commande = $objp->fk_commande;
251 $this->fk_parent_line = $objp->fk_parent_line;
252 $this->label = $objp->custom_label;
253 $this->desc = $objp->description;
254 $this->qty = $objp->qty;
255 $this->price = $objp->price;
256 $this->subprice = $objp->subprice;
257 $this->ref_ext = $objp->ref_ext;
258 $this->vat_src_code = $objp->vat_src_code;
259 $this->tva_tx = $objp->tva_tx;
260 $this->localtax1_tx = $objp->localtax1_tx;
261 $this->localtax2_tx = $objp->localtax2_tx;
262 $this->remise = $objp->remise;
263 $this->remise_percent = $objp->remise_percent;
264 $this->fk_remise_except = $objp->fk_remise_except;
265 $this->fk_product = $objp->fk_product;
266 $this->product_type = $objp->product_type;
267 $this->info_bits = $objp->info_bits;
268 $this->special_code = $objp->special_code;
269 $this->total_ht = $objp->total_ht;
270 $this->total_tva = $objp->total_tva;
271 $this->total_localtax1 = $objp->total_localtax1;
272 $this->total_localtax2 = $objp->total_localtax2;
273 $this->total_ttc = $objp->total_ttc;
274 $this->fk_fournprice = $objp->fk_fournprice;
275 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
276 $this->pa_ht = $marginInfos[0];
277 $this->marge_tx = $marginInfos[1];
278 $this->marque_tx = $marginInfos[2];
279 $this->special_code = $objp->special_code;
280 $this->rang = $objp->rang;
281
282 $this->ref = $objp->product_ref; // deprecated
283
284 $this->product_ref = $objp->product_ref;
285 $this->product_label = $objp->product_label;
286 $this->product_desc = $objp->product_desc;
287 $this->product_tobatch = $objp->product_tobatch;
288 $this->product_barcode = $objp->product_barcode;
289 $this->product_custom_code = $objp->customcode;
290 $this->product_custom_country_id = $objp->country_id;
291 $this->product_custom_country_code = $objp->country_code;
292 $this->fk_unit = $objp->fk_unit;
293 $this->packaging = $objp->packaging;
294
295 $this->date_start = $this->db->jdate($objp->date_start);
296 $this->date_end = $this->db->jdate($objp->date_end);
297
298 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
299
300 $this->fk_multicurrency = $objp->fk_multicurrency;
301 $this->multicurrency_code = $objp->multicurrency_code;
302 $this->multicurrency_subprice = $objp->multicurrency_subprice;
303 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
304 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
305 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
306
307 $this->fetch_optionals();
308
309 $this->db->free($result);
310
311 return 1;
312 } else {
313 $this->error = $this->db->lasterror();
314 return -1;
315 }
316 }
317
325 public function delete(User $user, $notrigger = 0)
326 {
327 global $conf, $langs;
328
329 $error = 0;
330
331 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
332 $this->id = $this->rowid;
333 }
334
335 // check if order line is not in a shipment line before deleting
336 $sqlCheckShipmentLine = "SELECT";
337 $sqlCheckShipmentLine .= " ed.rowid";
338 $sqlCheckShipmentLine .= " FROM " . MAIN_DB_PREFIX . "expeditiondet ed";
339 $sqlCheckShipmentLine .= " WHERE ed.fk_elementdet = " . ((int) $this->id);
340
341 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
342 if (!$resqlCheckShipmentLine) {
343 $error++;
344 $this->error = $this->db->lasterror();
345 $this->errors[] = $this->error;
346 } else {
347 $langs->load('errors');
348 $num = $this->db->num_rows($resqlCheckShipmentLine);
349 if ($num > 0) {
350 $error++;
351 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
352 $this->error = $langs->trans('ErrorRecordAlreadyExists') . ' : ' . $langs->trans('ShipmentLine') . ' ' . $objCheckShipmentLine->rowid;
353 $this->errors[] = $this->error;
354 }
355 $this->db->free($resqlCheckShipmentLine);
356 }
357 if ($error) {
358 dol_syslog(__METHOD__ . 'Error ; ' . $this->error, LOG_ERR);
359 return -1;
360 }
361
362 $this->db->begin();
363
364 if (!$notrigger) {
365 // Call trigger
366 $result = $this->call_trigger('LINEORDER_DELETE', $user);
367 if ($result < 0) {
368 $error++;
369 }
370 // End call triggers
371 }
372
373 if (!$error) {
374 $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . "commandedet WHERE rowid = " . ((int) $this->id);
375
376 dol_syslog("OrderLine::delete", LOG_DEBUG);
377 $resql = $this->db->query($sql);
378 if (!$resql) {
379 $this->error = $this->db->lasterror();
380 $error++;
381 }
382 }
383
384 // Remove extrafields
385 if (!$error) {
386 $result = $this->deleteExtraFields();
387 if ($result < 0) {
388 $error++;
389 dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
390 }
391 }
392
393 if (!$error) {
394 $this->db->commit();
395 return 1;
396 }
397
398 foreach ($this->errors as $errmsg) {
399 dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
400 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
401 }
402 $this->db->rollback();
403 return -1 * $error;
404 }
405
413 public function insert($user = null, $notrigger = 0)
414 {
415 $error = 0;
416
417 $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'.
418 $this->pa_ht = (float) $this->pa_ht; // convert to float but after checking if value is empty
419
420 dol_syslog(get_class($this)."::insert rang=".$this->rang);
421
422 // Clean parameters
423 if (empty($this->tva_tx)) {
424 $this->tva_tx = 0;
425 }
426 if (empty($this->localtax1_tx)) {
427 $this->localtax1_tx = 0;
428 }
429 if (empty($this->localtax2_tx)) {
430 $this->localtax2_tx = 0;
431 }
432 if (empty($this->localtax1_type)) {
433 $this->localtax1_type = '0';
434 }
435 if (empty($this->localtax2_type)) {
436 $this->localtax2_type = '0';
437 }
438 if (empty($this->total_localtax1)) {
439 $this->total_localtax1 = 0;
440 }
441 if (empty($this->total_localtax2)) {
442 $this->total_localtax2 = 0;
443 }
444 if (empty($this->rang)) {
445 $this->rang = 0;
446 }
447 if (empty($this->remise_percent)) {
448 $this->remise_percent = 0;
449 }
450 if (empty($this->info_bits)) {
451 $this->info_bits = 0;
452 }
453 if (empty($this->special_code)) {
454 $this->special_code = 0;
455 }
456 if (empty($this->fk_parent_line)) {
457 $this->fk_parent_line = 0;
458 }
459 if (empty($this->ref_ext)) {
460 $this->ref_ext = '';
461 }
462
463 // if buy price not defined (if = ''), we set the buyprice as configured in margin admin setup
464 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
465 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
466 if ($result < 0) {
467 return $result;
468 } else {
469 $this->pa_ht = $result;
470 }
471 }
472
473 // Check parameters
474 if ($this->product_type < 0) {
475 return -1;
476 }
477
478 $this->db->begin();
479
480 // Insert line into database
481 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet';
482 $sql .= ' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
483 $sql .= ' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
484 $sql .= ' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
485 $sql .= ' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
486 $sql .= ' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
487 $sql .= ' fk_unit,';
488 $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
489 $sql .= ')';
490 $sql .= " VALUES (".((int) $this->fk_commande).",";
491 $sql .= " ".($this->fk_parent_line > 0 ? "'".$this->db->escape((string) $this->fk_parent_line)."'" : "null").",";
492 $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
493 $sql .= " '".$this->db->escape($this->desc)."',";
494 $sql .= " '".price2num($this->qty)."',";
495 $sql .= " '".$this->db->escape($this->ref_ext)."',";
496 $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
497 $sql .= " '".price2num($this->tva_tx)."',";
498 $sql .= " '".price2num($this->localtax1_tx)."',";
499 $sql .= " '".price2num($this->localtax2_tx)."',";
500 $sql .= " '".$this->db->escape($this->localtax1_type)."',";
501 $sql .= " '".$this->db->escape($this->localtax2_type)."',";
502 $sql .= ' '.((!empty($this->fk_product) && $this->fk_product > 0) ? ((int) $this->fk_product) : "null").',';
503 $sql .= " ".((int) $this->product_type).",";
504 $sql .= " '".price2num($this->remise_percent)."',";
505 $sql .= " ".(price2num($this->subprice) !== '' ? price2num($this->subprice) : "null").",";
506 $sql .= " ".($this->price != '' ? "'".price2num($this->price)."'" : "null").",";
507 $sql .= ' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) : "null").',';
508 $sql .= ' '.((int) $this->special_code).',';
509 $sql .= ' '.((int) $this->rang).',';
510 $sql .= ' '.(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "null").',';
511 $sql .= ' '.price2num($this->pa_ht).',';
512 $sql .= " ".((int) $this->info_bits).",";
513 $sql .= " ".price2num($this->total_ht, 'MT').",";
514 $sql .= " ".price2num($this->total_tva, 'MT').",";
515 $sql .= " ".price2num($this->total_localtax1, 'MT').",";
516 $sql .= " ".price2num($this->total_localtax2, 'MT').",";
517 $sql .= " ".price2num($this->total_ttc, 'MT').",";
518 $sql .= " ".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null").',';
519 $sql .= " ".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null").',';
520 $sql .= ' '.(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
521 $sql .= ", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL');
522 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
523 $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
524 $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
525 $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
526 $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
527 $sql .= ')';
528
529 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
530 $resql = $this->db->query($sql);
531 if ($resql) {
532 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commandedet');
533 $this->rowid = $this->id;
534
535 $result = $this->insertExtraFields();
536 if ($result < 0) {
537 $error++;
538 }
539
540 if (!$error && !$notrigger) {
541 // Call trigger
542 $result = $this->call_trigger('LINEORDER_INSERT', $user);
543 if ($result < 0) {
544 $error++;
545 }
546 // End call triggers
547 }
548
549 if (!$error) {
550 $this->db->commit();
551 return $this->id;
552 }
553
554 foreach ($this->errors as $errmsg) {
555 dol_syslog(get_class($this)."::insert ".$errmsg, LOG_ERR);
556 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
557 }
558 $this->db->rollback();
559 return -1 * $error;
560 } else {
561 $this->error = $this->db->error();
562 $this->db->rollback();
563 return -2;
564 }
565 }
566
574 public function update(User $user, $notrigger = 0)
575 {
576 $error = 0;
577
578 $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'.
579
580 // Clean parameters
581 if (empty($this->tva_tx)) {
582 $this->tva_tx = 0;
583 }
584 if (empty($this->localtax1_tx)) {
585 $this->localtax1_tx = 0;
586 }
587 if (empty($this->localtax2_tx)) {
588 $this->localtax2_tx = 0;
589 }
590 if (empty($this->localtax1_type)) {
591 $this->localtax1_type = '0';
592 }
593 if (empty($this->localtax2_type)) {
594 $this->localtax2_type = '0';
595 }
596 if (empty($this->qty)) {
597 $this->qty = 0;
598 }
599 if (empty($this->total_localtax1)) {
600 $this->total_localtax1 = 0;
601 }
602 if (empty($this->total_localtax2)) {
603 $this->total_localtax2 = 0;
604 }
605 if (empty($this->marque_tx)) {
606 $this->marque_tx = 0;
607 }
608 if (empty($this->marge_tx)) {
609 $this->marge_tx = 0;
610 }
611 if (empty($this->remise_percent)) {
612 $this->remise_percent = 0;
613 }
614 if (empty($this->price)) {
615 $this->price = 0;
616 }
617 if (empty($this->remise)) {
618 $this->remise = 0;
619 }
620 if (empty($this->info_bits)) {
621 $this->info_bits = 0;
622 }
623 if (empty($this->special_code)) {
624 $this->special_code = 0;
625 }
626 if (empty($this->product_type)) {
627 $this->product_type = 0;
628 }
629 if (empty($this->fk_parent_line)) {
630 $this->fk_parent_line = 0;
631 }
632 if (empty($this->pa_ht)) {
633 $this->pa_ht = 0;
634 }
635 if (empty($this->ref_ext)) {
636 $this->ref_ext = '';
637 }
638
639 // if buy price not defined, define buyprice as configured in margin admin
640 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
641 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
642 if ($result < 0) {
643 return $result;
644 } else {
645 $this->pa_ht = $result;
646 }
647 }
648
649 $this->db->begin();
650
651 // Update line in database
652 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
653 $sql .= " description='".$this->db->escape($this->desc)."'";
654 $sql .= " , label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
655 $sql .= " , vat_src_code=".(!empty($this->vat_src_code) ? "'".$this->db->escape($this->vat_src_code)."'" : "''");
656 $sql .= " , tva_tx=".price2num($this->tva_tx);
657 $sql .= " , localtax1_tx=".price2num($this->localtax1_tx);
658 $sql .= " , localtax2_tx=".price2num($this->localtax2_tx);
659 $sql .= " , localtax1_type='".$this->db->escape($this->localtax1_type)."'";
660 $sql .= " , localtax2_type='".$this->db->escape($this->localtax2_type)."'";
661 $sql .= " , qty=".price2num($this->qty);
662 $sql .= " , ref_ext='".$this->db->escape($this->ref_ext)."'";
663 $sql .= " , subprice=".price2num($this->subprice);
664 $sql .= " , remise_percent=".price2num($this->remise_percent);
665 $sql .= " , price=".price2num($this->price); // TODO A virer
666 $sql .= " , remise=".price2num($this->remise); // TODO A virer
667 if (empty($this->skip_update_total)) {
668 $sql .= " , total_ht=".price2num($this->total_ht);
669 $sql .= " , total_tva=".price2num($this->total_tva);
670 $sql .= " , total_ttc=".price2num($this->total_ttc);
671 $sql .= " , total_localtax1=".price2num($this->total_localtax1);
672 $sql .= " , total_localtax2=".price2num($this->total_localtax2);
673 }
674 $sql .= " , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "null");
675 $sql .= " , buy_price_ht='".price2num($this->pa_ht)."'";
676 $sql .= " , info_bits=".((int) $this->info_bits);
677 $sql .= " , special_code=".((int) $this->special_code);
678 $sql .= " , date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
679 $sql .= " , date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
680 $sql .= " , product_type = ".((int) $this->product_type);
681 $sql .= " , fk_parent_line=".(!empty($this->fk_parent_line) ? ((int) $this->fk_parent_line) : "null");
682 if (!empty($this->rang)) {
683 $sql .= ", rang=".((int) $this->rang);
684 }
685 $sql .= " , fk_unit=".(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
686
687 // Multicurrency
688 $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice);
689 $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
690 $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
691 $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
692
693 $sql .= " WHERE rowid = ".((int) $this->rowid);
694
695 dol_syslog(get_class($this)."::update", LOG_DEBUG);
696 $resql = $this->db->query($sql);
697 if ($resql) {
698 $this->id = $this->rowid;
699 $result = $this->insertExtraFields();
700 if ($result < 0) {
701 $error++;
702 }
703
704 if (!$error && !$notrigger) {
705 // Call trigger
706 $result = $this->call_trigger('LINEORDER_MODIFY', $user);
707 if ($result < 0) {
708 $error++;
709 }
710 // End call triggers
711 }
712
713 if (!$error) {
714 $this->db->commit();
715 return 1;
716 }
717
718 foreach ($this->errors as $errmsg) {
719 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
720 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
721 }
722 $this->db->rollback();
723 return -1 * $error;
724 } else {
725 $this->error = $this->db->error();
726 $this->db->rollback();
727 return -2;
728 }
729 }
730
731 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
738 public function update_total()
739 {
740 // phpcs:enable
741 $this->db->begin();
742
743 // Clean parameters
744 if (empty($this->total_localtax1)) {
745 $this->total_localtax1 = 0;
746 }
747 if (empty($this->total_localtax2)) {
748 $this->total_localtax2 = 0;
749 }
750
751 // Update line in database
752 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
753 $sql .= " total_ht='".price2num($this->total_ht)."'";
754 $sql .= ",total_tva='".price2num($this->total_tva)."'";
755 $sql .= ",total_localtax1='".price2num($this->total_localtax1)."'";
756 $sql .= ",total_localtax2='".price2num($this->total_localtax2)."'";
757 $sql .= ",total_ttc='".price2num($this->total_ttc)."'";
758 $sql .= " WHERE rowid = ".((int) $this->rowid);
759
760 dol_syslog("OrderLine::update_total", LOG_DEBUG);
761
762 $resql = $this->db->query($sql);
763 if ($resql) {
764 $this->db->commit();
765 return 1;
766 } else {
767 $this->error = $this->db->error();
768 $this->db->rollback();
769 return -2;
770 }
771 }
772}
$object ref
Definition info.php:90
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Superclass for orders classes.
Class to manage order lines.
insert($user=null, $notrigger=0)
Insert line into database.
update(User $user, $notrigger=0)
Update the line object into db.
update_total()
Update DB line fields total_xxx Used by migration.
__construct($db)
Constructor.
fetch($rowid)
Load line order.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.