dolibarr 25.0.0-alpha
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 * Copyright (C) 2026 Lionel Vessiller <lvessiller@open-dsi.fr>
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32 */
33
40require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
41require_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php'; // Because the CommonOrderLine is still in commonorder.class.php file
42require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
43
44
49{
53 public $element = 'commandedet';
54
58 public $table_element = 'commandedet';
59
63 public $oldline;
64
69 public $fk_commande;
70
77 public $commande_id;
78
82 public $fk_parent_line;
83
87 public $fk_facture;
88
92 public $ref_ext;
93
97 public $fk_remise_except;
98
102 public $rang = 0;
103
107 public $fk_fournprice;
108
113 public $pa_ht;
114
118 public $marge_tx;
119
123 public $marque_tx;
124
130 public $remise;
131
136 public $date_start;
137
142 public $date_end;
143
148 public $skip_update_total;
149
153 public $packaging;
154
155
156 // BEGIN MODULEBUILDER PROPERTIES
160 public $fields = array(
161 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
162 'fk_commande' => array('type' => 'integer:Commande:commande/class/commande.class.php', 'label' => 'Order', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 15),
163 'fk_parent_line' => array('type' => 'integer:OrderLine:commande/class/orderline.class.php', 'label' => 'ParentLine', 'enabled' => 1, 'visible' => -1, 'position' => 20),
164 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'ProductOrService', 'enabled' => "isModEnabled('product') || isModEnabled('service')", 'visible' => -1, 'position' => 25),
165 'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => -1, 'position' => 30),
166 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'visible' => -1, 'position' => 35, 'searchall' => 1),
167 'vat_src_code' => array('type' => 'varchar(10)', 'label' => 'VatSrcCode', 'enabled' => 1, 'visible' => -1, 'position' => 40),
168 'tva_tx' => array('type' => 'double(7,4)', 'label' => 'VATRate', 'enabled' => 1, 'visible' => -1, 'position' => 45),
169 'localtax1_tx' => array('type' => 'double(7,4)', 'label' => 'LocalTax1Rate', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 50),
170 'localtax1_type' => array('type' => 'varchar(10)', 'label' => 'LocalTax1Type', 'enabled' => 1, 'visible' => -1, 'position' => 55),
171 'localtax2_tx' => array('type' => 'double(7,4)', 'label' => 'LocalTax2Rate', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 60),
172 'localtax2_type' => array('type' => 'varchar(10)', 'label' => 'LocalTax2Type', 'enabled' => 1, 'visible' => -1, 'position' => 65),
173 'qty' => array('type' => 'real', 'label' => 'Qty', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 70),
174 'remise_percent' => array('type' => 'real', 'label' => 'ReductionPercent', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 75),
175 'remise' => array('type' => 'real', 'label' => 'Remise', 'enabled' => 0, 'visible' => -2, 'default' => '0', 'position' => 80),
176 'fk_remise_except' => array('type' => 'integer:DiscountAbsolute:core/class/discount.class.php', 'label' => 'DiscountAbsolute', 'enabled' => 1, 'visible' => -1, 'position' => 85),
177 'price' => array('type' => 'real', 'label' => 'Price', 'enabled' => 0, 'visible' => -2, 'position' => 90),
178 'subprice' => array('type' => 'double(24,8)', 'label' => 'SubPrice', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 95),
179 'subprice_ttc' => array('type' => 'double(24,8)', 'label' => 'SubPriceTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 100),
180 'total_ht' => array('type' => 'double(24,8)', 'label' => 'TotalHT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 105, 'isameasure' => 1),
181 'total_tva' => array('type' => 'double(24,8)', 'label' => 'VAT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 110, 'isameasure' => 1),
182 'total_localtax1' => array('type' => 'double(24,8)', 'label' => 'LocalTax1', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 115, 'isameasure' => 1),
183 'total_localtax2' => array('type' => 'double(24,8)', 'label' => 'LocalTax2', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 120, 'isameasure' => 1),
184 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'TotalTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 125, 'isameasure' => 1),
185 'product_type' => array('type' => 'integer', 'label' => 'TypeOfLineServiceOrProduct', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 130),
186 'date_start' => array('type' => 'datetime', 'label' => 'DateStart', 'enabled' => 1, 'visible' => -1, 'position' => 135),
187 'date_end' => array('type' => 'datetime', 'label' => 'DateEnd', 'enabled' => 1, 'visible' => -1, 'position' => 140),
188 'info_bits' => array('type' => 'integer', 'label' => 'InfoBits', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 145),
189 'buy_price_ht' => array('type' => 'double(24,8)', 'label' => 'BuyingPrice', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 150),
190 'fk_product_fournisseur_price' => array('type' => 'integer:ProductFournisseur:fourn/class/fournisseur.product.class.php', 'label' => 'SupplierPriceReference', 'enabled' => 1, 'visible' => -1, 'position' => 155),
191 'special_code' => array('type' => 'integer', 'label' => 'SpecialCode', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 160),
192 'rang' => array('type' => 'integer', 'label' => 'Rank', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 165),
193 'fk_unit' => array('type' => 'integer:CUnits:core/class/cunits.class.php', 'label' => 'Unit', 'enabled' => 1, 'visible' => -1, 'position' => 170),
194 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 175),
195 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 180),
196 'fk_commandefourndet' => array('type' => 'integer:CommandeFournisseurLigne:fourn/class/fournisseur.commande.class.php', 'label' => 'SupplierOrderLine', 'enabled' => 1, 'visible' => -1, 'position' => 185),
197 'fk_multicurrency' => array('type' => 'integer:MultiCurrency:multicurrency/class/multicurrency.class.php', 'label' => 'Currency', 'enabled' => 1, 'visible' => -1, 'position' => 190),
198 'multicurrency_code' => array('type' => 'varchar(3)', 'label' => 'CurrencyCode', 'enabled' => 1, 'visible' => -1, 'position' => 195),
199 'multicurrency_subprice' => array('type' => 'double(24,8)', 'label' => 'CurrencyUnitPrice', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 200),
200 'multicurrency_subprice_ttc' => array('type' => 'double(24,8)', 'label' => 'CurrencyUnitPriceTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 205),
201 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalHT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 210),
202 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalVAT', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 215),
203 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'CurrencyTotalTTC', 'enabled' => 1, 'visible' => -1, 'default' => '0', 'position' => 220),
204 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 225),
205 );
206 // END MODULEBUILDER PROPERTIES
207
208
214 public function __construct($db)
215 {
216 $this->db = $db;
217 }
218
225 public function fetch($rowid)
226 {
227 $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,';
228 $sql .= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.subprice_ttc, cd.ref_ext,';
229 $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,';
230 $sql .= ' cd.fk_unit,';
231 $sql .= ' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
232 $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,';
233 $sql .= ' p.customcode, p.fk_country as country_id, c.code as country_code,';
234 $sql .= ' p.packaging,';
235 $sql .= ' cd.date_start, cd.date_end, cd.vat_src_code, cd.extraparams';
236 $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as cd';
237 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
238 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON c.rowid = p.fk_country';
239 $sql .= ' WHERE cd.rowid = '.((int) $rowid);
240 $result = $this->db->query($sql);
241 if ($result) {
242 $objp = $this->db->fetch_object($result);
243
244 if (!$objp) {
245 $this->error = 'OrderLine with id '. $rowid .' not found sql='.$sql;
246 return 0;
247 }
248
249 $this->rowid = $objp->rowid;
250 $this->id = $objp->rowid;
251 $this->fk_commande = $objp->fk_commande;
252 $this->fk_parent_line = $objp->fk_parent_line;
253 $this->label = $objp->custom_label;
254 $this->desc = $objp->description;
255 $this->qty = $objp->qty;
256 $this->price = $objp->price;
257 $this->subprice = $objp->subprice;
258 $this->subprice_ttc = $objp->subprice_ttc;
259 $this->ref_ext = $objp->ref_ext;
260 $this->vat_src_code = $objp->vat_src_code;
261 $this->tva_tx = $objp->tva_tx;
262 $this->localtax1_tx = $objp->localtax1_tx;
263 $this->localtax2_tx = $objp->localtax2_tx;
264 $this->remise = $objp->remise;
265 $this->remise_percent = $objp->remise_percent;
266 $this->fk_remise_except = $objp->fk_remise_except;
267 $this->fk_product = $objp->fk_product;
268 $this->product_type = $objp->product_type;
269 $this->info_bits = $objp->info_bits;
270 $this->special_code = $objp->special_code;
271 $this->total_ht = $objp->total_ht;
272 $this->total_tva = $objp->total_tva;
273 $this->total_localtax1 = $objp->total_localtax1;
274 $this->total_localtax2 = $objp->total_localtax2;
275 $this->total_ttc = $objp->total_ttc;
276 $this->fk_fournprice = $objp->fk_fournprice;
277 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
278 $this->pa_ht = $marginInfos[0];
279 $this->marge_tx = $marginInfos[1];
280 $this->marque_tx = $marginInfos[2];
281 $this->special_code = $objp->special_code;
282 $this->rang = $objp->rang;
283
284 $this->ref = $objp->product_ref; // deprecated
285
286 $this->product_ref = $objp->product_ref;
287 $this->product_label = $objp->product_label;
288 $this->product_desc = $objp->product_desc;
289 $this->product_tobatch = $objp->product_tobatch;
290 $this->product_barcode = $objp->product_barcode;
291 $this->product_custom_code = $objp->customcode;
292 $this->product_custom_country_id = $objp->country_id;
293 $this->product_custom_country_code = $objp->country_code;
294 $this->fk_unit = $objp->fk_unit;
295 $this->packaging = $objp->packaging;
296
297 $this->date_start = $this->db->jdate($objp->date_start);
298 $this->date_end = $this->db->jdate($objp->date_end);
299
300 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
301
302 $this->fk_multicurrency = $objp->fk_multicurrency;
303 $this->multicurrency_code = $objp->multicurrency_code;
304 $this->multicurrency_subprice = $objp->multicurrency_subprice;
305 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
306 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
307 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
308
309 $this->fetch_optionals();
310
311 $this->db->free($result);
312
313 return 1;
314 } else {
315 $this->error = $this->db->lasterror();
316 return -1;
317 }
318 }
319
327 public function delete(User $user, $notrigger = 0)
328 {
329 global $conf, $langs;
330
331 $error = 0;
332
333 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
334 $this->id = $this->rowid;
335 }
336
337 // check if order line is not in a shipment line before deleting
338 $sqlCheckShipmentLine = "SELECT";
339 $sqlCheckShipmentLine .= " ed.rowid";
340 $sqlCheckShipmentLine .= " FROM " . MAIN_DB_PREFIX . "expeditiondet ed";
341 $sqlCheckShipmentLine .= " WHERE ed.fk_elementdet = " . ((int) $this->id);
342
343 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
344 if (!$resqlCheckShipmentLine) {
345 $error++;
346 $this->error = $this->db->lasterror();
347 $this->errors[] = $this->error;
348 } else {
349 $langs->load('errors');
350 $num = $this->db->num_rows($resqlCheckShipmentLine);
351 if ($num > 0) {
352 $error++;
353 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
354 $this->error = $langs->trans('ErrorRecordAlreadyExists') . ' : ' . $langs->trans('ShipmentLine') . ' ' . $objCheckShipmentLine->rowid;
355 $this->errors[] = $this->error;
356 }
357 $this->db->free($resqlCheckShipmentLine);
358 }
359 if ($error) {
360 dol_syslog(__METHOD__ . 'Error ; ' . $this->error, LOG_ERR);
361 return -1;
362 }
363
364 $this->db->begin();
365
366 if (!$notrigger) {
367 // Call trigger
368 $result = $this->call_trigger('LINEORDER_DELETE', $user);
369 if ($result < 0) {
370 $error++;
371 }
372 // End call triggers
373 }
374
375 if (!$error) {
376 $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . "commandedet WHERE rowid = " . ((int) $this->id);
377
378 dol_syslog("OrderLine::delete", LOG_DEBUG);
379 $resql = $this->db->query($sql);
380 if (!$resql) {
381 $this->error = $this->db->lasterror();
382 $error++;
383 }
384 }
385
386 // Remove extrafields
387 if (!$error) {
388 $result = $this->deleteExtraFields();
389 if ($result < 0) {
390 $error++;
391 dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
392 }
393 }
394
395 if (!$error) {
396 $this->db->commit();
397 return 1;
398 }
399
400 foreach ($this->errors as $errmsg) {
401 dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
402 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
403 }
404 $this->db->rollback();
405 return -1 * $error;
406 }
407
415 public function insert($user = null, $notrigger = 0)
416 {
417 $error = 0;
418
419 $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'.
420 $this->pa_ht = (float) $this->pa_ht; // convert to float but after checking if value is empty
421
422 dol_syslog(get_class($this)."::insert rang=".$this->rang);
423
424 // Clean parameters
425 if (empty($this->tva_tx)) {
426 $this->tva_tx = 0;
427 }
428 if (empty($this->localtax1_tx)) {
429 $this->localtax1_tx = 0;
430 }
431 if (empty($this->localtax2_tx)) {
432 $this->localtax2_tx = 0;
433 }
434 if (empty($this->localtax1_type)) {
435 $this->localtax1_type = '0';
436 }
437 if (empty($this->localtax2_type)) {
438 $this->localtax2_type = '0';
439 }
440 if (empty($this->total_localtax1)) {
441 $this->total_localtax1 = 0;
442 }
443 if (empty($this->total_localtax2)) {
444 $this->total_localtax2 = 0;
445 }
446 if (empty($this->rang)) {
447 $this->rang = 0;
448 }
449 if (empty($this->remise_percent)) {
450 $this->remise_percent = 0;
451 }
452 if (empty($this->subprice_ttc)) {
453 $this->subprice_ttc = 0;
454 }
455 if (empty($this->info_bits)) {
456 $this->info_bits = 0;
457 }
458 if (empty($this->special_code)) {
459 $this->special_code = 0;
460 }
461 if (empty($this->fk_parent_line)) {
462 $this->fk_parent_line = 0;
463 }
464 if (empty($this->ref_ext)) {
465 $this->ref_ext = '';
466 }
467
468 // if buy price not defined (if = ''), we set the buyprice as configured in margin admin setup
469 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
470 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
471 if ($result < 0) {
472 return $result;
473 } else {
474 $this->pa_ht = $result;
475 }
476 }
477
478 // Check parameters
479 if ($this->product_type < 0) {
480 return -1;
481 }
482
483 $this->db->begin();
484
485 // Insert line into database
486 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet';
487 $sql .= ' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
488 $sql .= ' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
489 $sql .= ' fk_product, product_type, remise_percent, subprice, subprice_ttc, price, fk_remise_except,';
490 $sql .= ' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
491 $sql .= ' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
492 $sql .= ' fk_unit,';
493 $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
494 $sql .= ')';
495 $sql .= " VALUES (".((int) $this->fk_commande).",";
496 $sql .= " ".($this->fk_parent_line > 0 ? "'".$this->db->escape((string) $this->fk_parent_line)."'" : "null").",";
497 $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
498 $sql .= " '".$this->db->escape($this->desc)."',";
499 $sql .= " '".price2num($this->qty)."',";
500 $sql .= " '".$this->db->escape($this->ref_ext)."',";
501 $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
502 $sql .= " '".price2num($this->tva_tx)."',";
503 $sql .= " '".price2num($this->localtax1_tx)."',";
504 $sql .= " '".price2num($this->localtax2_tx)."',";
505 $sql .= " '".$this->db->escape($this->localtax1_type)."',";
506 $sql .= " '".$this->db->escape($this->localtax2_type)."',";
507 $sql .= ' '.((!empty($this->fk_product) && $this->fk_product > 0) ? ((int) $this->fk_product) : "null").',';
508 $sql .= " ".((int) $this->product_type).",";
509 $sql .= " '".price2num($this->remise_percent)."',";
510 $sql .= " ".(price2num($this->subprice) !== '' ? price2num($this->subprice) : "null").",";
511 $sql .= " ".price2num($this->subprice_ttc).",";
512 $sql .= " ".($this->price != '' ? "'".price2num($this->price)."'" : "null").",";
513 $sql .= ' '.(!empty($this->fk_remise_except) ? ((int) $this->fk_remise_except) : "null").',';
514 $sql .= ' '.((int) $this->special_code).',';
515 $sql .= ' '.((int) $this->rang).',';
516 $sql .= ' '.(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "null").',';
517 $sql .= ' '.price2num($this->pa_ht).',';
518 $sql .= " ".((int) $this->info_bits).",";
519 $sql .= " ".price2num($this->total_ht, 'MT').",";
520 $sql .= " ".price2num($this->total_tva, 'MT').",";
521 $sql .= " ".price2num($this->total_localtax1, 'MT').",";
522 $sql .= " ".price2num($this->total_localtax2, 'MT').",";
523 $sql .= " ".price2num($this->total_ttc, 'MT').",";
524 $sql .= " ".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null").',';
525 $sql .= " ".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null").',';
526 $sql .= ' '.(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
527 $sql .= ", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL');
528 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
529 $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
530 $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
531 $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
532 $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
533 $sql .= ')';
534
535 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
536 $resql = $this->db->query($sql);
537 if ($resql) {
538 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commandedet');
539 $this->rowid = $this->id;
540
541 $result = $this->insertExtraFields();
542 if ($result < 0) {
543 $error++;
544 }
545
546 if (!$error && !$notrigger) {
547 // Call trigger
548 $result = $this->call_trigger('LINEORDER_INSERT', $user);
549 if ($result < 0) {
550 $error++;
551 }
552 // End call triggers
553 }
554
555 if (!$error) {
556 $this->db->commit();
557 return $this->id;
558 }
559
560 foreach ($this->errors as $errmsg) {
561 dol_syslog(get_class($this)."::insert ".$errmsg, LOG_ERR);
562 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
563 }
564 $this->db->rollback();
565 return -1 * $error;
566 } else {
567 $this->error = $this->db->error();
568 $this->db->rollback();
569 return -2;
570 }
571 }
572
580 public function update(User $user, $notrigger = 0)
581 {
582 $error = 0;
583
584 $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'.
585
586 // Clean parameters
587 if (empty($this->tva_tx)) {
588 $this->tva_tx = 0;
589 }
590 if (empty($this->localtax1_tx)) {
591 $this->localtax1_tx = 0;
592 }
593 if (empty($this->localtax2_tx)) {
594 $this->localtax2_tx = 0;
595 }
596 if (empty($this->localtax1_type)) {
597 $this->localtax1_type = '0';
598 }
599 if (empty($this->localtax2_type)) {
600 $this->localtax2_type = '0';
601 }
602 if (empty($this->qty)) {
603 $this->qty = 0;
604 }
605 if (empty($this->subprice_ttc)) {
606 $this->subprice_ttc = 0;
607 }
608 if (empty($this->total_localtax1)) {
609 $this->total_localtax1 = 0;
610 }
611 if (empty($this->total_localtax2)) {
612 $this->total_localtax2 = 0;
613 }
614 if (empty($this->marque_tx)) {
615 $this->marque_tx = 0;
616 }
617 if (empty($this->marge_tx)) {
618 $this->marge_tx = 0;
619 }
620 if (empty($this->remise_percent)) {
621 $this->remise_percent = 0;
622 }
623 if (empty($this->price)) {
624 $this->price = 0;
625 }
626 if (empty($this->remise)) {
627 $this->remise = 0;
628 }
629 if (empty($this->info_bits)) {
630 $this->info_bits = 0;
631 }
632 if (empty($this->special_code)) {
633 $this->special_code = 0;
634 }
635 if (empty($this->product_type)) {
636 $this->product_type = 0;
637 }
638 if (empty($this->fk_parent_line)) {
639 $this->fk_parent_line = 0;
640 }
641 if (empty($this->pa_ht)) {
642 $this->pa_ht = 0;
643 }
644 if (empty($this->ref_ext)) {
645 $this->ref_ext = '';
646 }
647
648 // if buy price not defined, define buyprice as configured in margin admin
649 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
650 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
651 if ($result < 0) {
652 return $result;
653 } else {
654 $this->pa_ht = $result;
655 }
656 }
657
658 $this->db->begin();
659
660 // Update line in database
661 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
662 $sql .= " description='".$this->db->escape($this->desc)."'";
663 $sql .= " , label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
664 $sql .= " , vat_src_code=".(!empty($this->vat_src_code) ? "'".$this->db->escape($this->vat_src_code)."'" : "''");
665 $sql .= " , tva_tx=".price2num($this->tva_tx);
666 $sql .= " , localtax1_tx=".price2num($this->localtax1_tx);
667 $sql .= " , localtax2_tx=".price2num($this->localtax2_tx);
668 $sql .= " , localtax1_type='".$this->db->escape($this->localtax1_type)."'";
669 $sql .= " , localtax2_type='".$this->db->escape($this->localtax2_type)."'";
670 $sql .= " , qty=".price2num($this->qty);
671 $sql .= " , ref_ext='".$this->db->escape($this->ref_ext)."'";
672 $sql .= " , subprice=".price2num($this->subprice);
673 $sql .= " , subprice_ttc=".price2num($this->subprice_ttc);
674 $sql .= " , remise_percent=".price2num($this->remise_percent);
675 $sql .= " , price=".price2num($this->price); // TODO A virer
676 $sql .= " , remise=".price2num($this->remise); // TODO A virer
677 if (empty($this->skip_update_total)) {
678 $sql .= " , total_ht=".price2num($this->total_ht);
679 $sql .= " , total_tva=".price2num($this->total_tva);
680 $sql .= " , total_ttc=".price2num($this->total_ttc);
681 $sql .= " , total_localtax1=".price2num($this->total_localtax1);
682 $sql .= " , total_localtax2=".price2num($this->total_localtax2);
683 }
684 $sql .= " , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? ((int) $this->fk_fournprice) : "null");
685 $sql .= " , buy_price_ht='".price2num($this->pa_ht)."'";
686 $sql .= " , info_bits=".((int) $this->info_bits);
687 $sql .= " , special_code=".((int) $this->special_code);
688 $sql .= " , date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
689 $sql .= " , date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
690 $sql .= " , product_type = ".((int) $this->product_type);
691 $sql .= " , fk_parent_line=".(!empty($this->fk_parent_line) ? ((int) $this->fk_parent_line) : "null");
692 if (!empty($this->rang)) {
693 $sql .= ", rang=".((int) $this->rang);
694 }
695 $sql .= " , fk_unit=".(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
696
697 // Multicurrency
698 $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice);
699 $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
700 $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
701 $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
702
703 $sql .= " WHERE rowid = ".((int) $this->rowid);
704
705 dol_syslog(get_class($this)."::update", LOG_DEBUG);
706 $resql = $this->db->query($sql);
707 if ($resql) {
708 $this->id = $this->rowid;
709 $result = $this->insertExtraFields();
710 if ($result < 0) {
711 $error++;
712 }
713
714 if (!$error && !$notrigger) {
715 // Call trigger
716 $result = $this->call_trigger('LINEORDER_MODIFY', $user);
717 if ($result < 0) {
718 $error++;
719 }
720 // End call triggers
721 }
722
723 if (!$error) {
724 $this->db->commit();
725 return 1;
726 }
727
728 foreach ($this->errors as $errmsg) {
729 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
730 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
731 }
732 $this->db->rollback();
733 return -1 * $error;
734 } else {
735 $this->error = $this->db->error();
736 $this->db->rollback();
737 return -2;
738 }
739 }
740
741 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
748 public function update_total()
749 {
750 // phpcs:enable
751 $this->db->begin();
752
753 // Clean parameters
754 if (empty($this->total_localtax1)) {
755 $this->total_localtax1 = 0;
756 }
757 if (empty($this->total_localtax2)) {
758 $this->total_localtax2 = 0;
759 }
760
761 // Update line in database
762 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
763 $sql .= " total_ht='".price2num($this->total_ht)."'";
764 $sql .= ",total_tva='".price2num($this->total_tva)."'";
765 $sql .= ",total_localtax1='".price2num($this->total_localtax1)."'";
766 $sql .= ",total_localtax2='".price2num($this->total_localtax2)."'";
767 $sql .= ",total_ttc='".price2num($this->total_ttc)."'";
768 $sql .= " WHERE rowid = ".((int) $this->rowid);
769
770 dol_syslog("OrderLine::update_total", LOG_DEBUG);
771
772 $resql = $this->db->query($sql);
773 if ($resql) {
774 $this->db->commit();
775 return 1;
776 } else {
777 $this->error = $this->db->error();
778 $this->db->rollback();
779 return -2;
780 }
781 }
782}
$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.