dolibarr 24.0.0-beta
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 'ref' => array('type' => 'varchar(50)', 'label' => 'Ref', 'enabled' => 1, 'visible' => -1, 'showoncombobox' => 1, 'position' => 15, 'searchall' => 1),
162 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'visible' => 0, 'position' => 20),
163 'ref_customer' => array('type' => 'varchar(50)', 'label' => 'RefCustomer', 'enabled' => 1, 'visible' => -1, 'position' => 25, 'searchall' => 1),
164 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 30, 'index' => 1),
165 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 35),
166 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 40),
167 'fk_commande' => array('type' => 'integer:Commande:commande/class/commande.class.php', 'label' => 'Order', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 70),
168 'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Project', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 75),
169 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:1', 'label' => 'ProductOrService', 'enabled' => "isModEnabled('product') || isModEnabled('service')", 'visible' => -1, 'position' => 76),
170 //'fk_commercial_signature' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative Signature', 'enabled' => 1, 'visible' => -1, 'position' => 80),
171 //'fk_commercial_suivi' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'SaleRepresentative follower', 'enabled' => 1, 'visible' => -1, 'position' => 85),
172 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 90),
173 'total_ht' => array('type' => 'double(24,8)', 'label' => 'TotalHT', 'enabled' => 1, 'visible' => -1, 'position' => 125, 'isameasure' => 1),
174 'total_tva' => array('type' => 'double(24,8)', 'label' => 'VAT', 'enabled' => 1, 'visible' => -1, 'position' => 130, 'isameasure' => 1),
175 'localtax1' => array('type' => 'double(24,8)', 'label' => 'LocalTax1', 'enabled' => 1, 'visible' => -1, 'position' => 135, 'isameasure' => 1),
176 'localtax2' => array('type' => 'double(24,8)', 'label' => 'LocalTax2', 'enabled' => 1, 'visible' => -1, 'position' => 140, 'isameasure' => 1),
177 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'TotalTTC', 'enabled' => 1, 'visible' => -1, 'position' => 145, 'isameasure' => 1),
178 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 105, 'searchall' => 1),
179 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 110, 'searchall' => 1),
180 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 115),
181 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'position' => 120),
182 'extraparams' => array('type' => 'varchar(255)', 'label' => 'Extraparams', 'enabled' => 1, 'visible' => -1, 'position' => 125),
183 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 135),
184 'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'Last main doc', 'enabled' => 1, 'visible' => -1, 'position' => 140),
185 'statut' => array('type' => 'smallint(6)', 'label' => 'Statut', 'enabled' => 1, 'visible' => -1, 'position' => 500, 'notnull' => 1, 'arrayofkeyval' => array(0 => 'Draft', 1 => 'Validated', 2 => 'Closed'))
186 );
187 // END MODULEBUILDER PROPERTIES
188
189
195 public function __construct($db)
196 {
197 $this->db = $db;
198 }
199
206 public function fetch($rowid)
207 {
208 $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,';
209 $sql .= ' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
210 $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,';
211 $sql .= ' cd.fk_unit,';
212 $sql .= ' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
213 $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,';
214 $sql .= ' p.customcode, p.fk_country as country_id, c.code as country_code,';
215 $sql .= ' p.packaging,';
216 $sql .= ' cd.date_start, cd.date_end, cd.vat_src_code, cd.extraparams';
217 $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as cd';
218 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
219 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_country as c ON c.rowid = p.fk_country';
220 $sql .= ' WHERE cd.rowid = '.((int) $rowid);
221 $result = $this->db->query($sql);
222 if ($result) {
223 $objp = $this->db->fetch_object($result);
224
225 if (!$objp) {
226 $this->error = 'OrderLine with id '. $rowid .' not found sql='.$sql;
227 return 0;
228 }
229
230 $this->rowid = $objp->rowid;
231 $this->id = $objp->rowid;
232 $this->fk_commande = $objp->fk_commande;
233 $this->fk_parent_line = $objp->fk_parent_line;
234 $this->label = $objp->custom_label;
235 $this->desc = $objp->description;
236 $this->qty = $objp->qty;
237 $this->price = $objp->price;
238 $this->subprice = $objp->subprice;
239 $this->ref_ext = $objp->ref_ext;
240 $this->vat_src_code = $objp->vat_src_code;
241 $this->tva_tx = $objp->tva_tx;
242 $this->localtax1_tx = $objp->localtax1_tx;
243 $this->localtax2_tx = $objp->localtax2_tx;
244 $this->remise = $objp->remise;
245 $this->remise_percent = $objp->remise_percent;
246 $this->fk_remise_except = $objp->fk_remise_except;
247 $this->fk_product = $objp->fk_product;
248 $this->product_type = $objp->product_type;
249 $this->info_bits = $objp->info_bits;
250 $this->special_code = $objp->special_code;
251 $this->total_ht = $objp->total_ht;
252 $this->total_tva = $objp->total_tva;
253 $this->total_localtax1 = $objp->total_localtax1;
254 $this->total_localtax2 = $objp->total_localtax2;
255 $this->total_ttc = $objp->total_ttc;
256 $this->fk_fournprice = $objp->fk_fournprice;
257 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
258 $this->pa_ht = $marginInfos[0];
259 $this->marge_tx = $marginInfos[1];
260 $this->marque_tx = $marginInfos[2];
261 $this->special_code = $objp->special_code;
262 $this->rang = $objp->rang;
263
264 $this->ref = $objp->product_ref; // deprecated
265
266 $this->product_ref = $objp->product_ref;
267 $this->product_label = $objp->product_label;
268 $this->product_desc = $objp->product_desc;
269 $this->product_tobatch = $objp->product_tobatch;
270 $this->product_barcode = $objp->product_barcode;
271 $this->product_custom_code = $objp->customcode;
272 $this->product_custom_country_id = $objp->country_id;
273 $this->product_custom_country_code = $objp->country_code;
274 $this->fk_unit = $objp->fk_unit;
275 $this->packaging = $objp->packaging;
276
277 $this->date_start = $this->db->jdate($objp->date_start);
278 $this->date_end = $this->db->jdate($objp->date_end);
279
280 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
281
282 $this->fk_multicurrency = $objp->fk_multicurrency;
283 $this->multicurrency_code = $objp->multicurrency_code;
284 $this->multicurrency_subprice = $objp->multicurrency_subprice;
285 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
286 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
287 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
288
289 $this->fetch_optionals();
290
291 $this->db->free($result);
292
293 return 1;
294 } else {
295 $this->error = $this->db->lasterror();
296 return -1;
297 }
298 }
299
307 public function delete(User $user, $notrigger = 0)
308 {
309 global $conf, $langs;
310
311 $error = 0;
312
313 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
314 $this->id = $this->rowid;
315 }
316
317 // check if order line is not in a shipment line before deleting
318 $sqlCheckShipmentLine = "SELECT";
319 $sqlCheckShipmentLine .= " ed.rowid";
320 $sqlCheckShipmentLine .= " FROM " . MAIN_DB_PREFIX . "expeditiondet ed";
321 $sqlCheckShipmentLine .= " WHERE ed.fk_elementdet = " . ((int) $this->id);
322
323 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
324 if (!$resqlCheckShipmentLine) {
325 $error++;
326 $this->error = $this->db->lasterror();
327 $this->errors[] = $this->error;
328 } else {
329 $langs->load('errors');
330 $num = $this->db->num_rows($resqlCheckShipmentLine);
331 if ($num > 0) {
332 $error++;
333 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
334 $this->error = $langs->trans('ErrorRecordAlreadyExists') . ' : ' . $langs->trans('ShipmentLine') . ' ' . $objCheckShipmentLine->rowid;
335 $this->errors[] = $this->error;
336 }
337 $this->db->free($resqlCheckShipmentLine);
338 }
339 if ($error) {
340 dol_syslog(__METHOD__ . 'Error ; ' . $this->error, LOG_ERR);
341 return -1;
342 }
343
344 $this->db->begin();
345
346 if (!$notrigger) {
347 // Call trigger
348 $result = $this->call_trigger('LINEORDER_DELETE', $user);
349 if ($result < 0) {
350 $error++;
351 }
352 // End call triggers
353 }
354
355 if (!$error) {
356 $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . "commandedet WHERE rowid = " . ((int) $this->id);
357
358 dol_syslog("OrderLine::delete", LOG_DEBUG);
359 $resql = $this->db->query($sql);
360 if (!$resql) {
361 $this->error = $this->db->lasterror();
362 $error++;
363 }
364 }
365
366 // Remove extrafields
367 if (!$error) {
368 $result = $this->deleteExtraFields();
369 if ($result < 0) {
370 $error++;
371 dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
372 }
373 }
374
375 if (!$error) {
376 $this->db->commit();
377 return 1;
378 }
379
380 foreach ($this->errors as $errmsg) {
381 dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
382 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
383 }
384 $this->db->rollback();
385 return -1 * $error;
386 }
387
395 public function insert($user = null, $notrigger = 0)
396 {
397 $error = 0;
398
399 $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'.
400 $this->pa_ht = (float) $this->pa_ht; // convert to float but after checking if value is empty
401
402 dol_syslog(get_class($this)."::insert rang=".$this->rang);
403
404 // Clean parameters
405 if (empty($this->tva_tx)) {
406 $this->tva_tx = 0;
407 }
408 if (empty($this->localtax1_tx)) {
409 $this->localtax1_tx = 0;
410 }
411 if (empty($this->localtax2_tx)) {
412 $this->localtax2_tx = 0;
413 }
414 if (empty($this->localtax1_type)) {
415 $this->localtax1_type = '0';
416 }
417 if (empty($this->localtax2_type)) {
418 $this->localtax2_type = '0';
419 }
420 if (empty($this->total_localtax1)) {
421 $this->total_localtax1 = 0;
422 }
423 if (empty($this->total_localtax2)) {
424 $this->total_localtax2 = 0;
425 }
426 if (empty($this->rang)) {
427 $this->rang = 0;
428 }
429 if (empty($this->remise_percent)) {
430 $this->remise_percent = 0;
431 }
432 if (empty($this->info_bits)) {
433 $this->info_bits = 0;
434 }
435 if (empty($this->special_code)) {
436 $this->special_code = 0;
437 }
438 if (empty($this->fk_parent_line)) {
439 $this->fk_parent_line = 0;
440 }
441 if (empty($this->ref_ext)) {
442 $this->ref_ext = '';
443 }
444
445 // if buy price not defined (if = ''), we set the buyprice as configured in margin admin setup
446 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
447 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
448 if ($result < 0) {
449 return $result;
450 } else {
451 $this->pa_ht = $result;
452 }
453 }
454
455 // Check parameters
456 if ($this->product_type < 0) {
457 return -1;
458 }
459
460 $this->db->begin();
461
462 // Insert line into database
463 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet';
464 $sql .= ' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
465 $sql .= ' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
466 $sql .= ' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
467 $sql .= ' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
468 $sql .= ' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
469 $sql .= ' fk_unit,';
470 $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
471 $sql .= ')';
472 $sql .= " VALUES (".$this->fk_commande.",";
473 $sql .= " ".($this->fk_parent_line > 0 ? "'".$this->db->escape((string) $this->fk_parent_line)."'" : "null").",";
474 $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
475 $sql .= " '".$this->db->escape($this->desc)."',";
476 $sql .= " '".price2num($this->qty)."',";
477 $sql .= " '".$this->db->escape($this->ref_ext)."',";
478 $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
479 $sql .= " '".price2num($this->tva_tx)."',";
480 $sql .= " '".price2num($this->localtax1_tx)."',";
481 $sql .= " '".price2num($this->localtax2_tx)."',";
482 $sql .= " '".$this->db->escape($this->localtax1_type)."',";
483 $sql .= " '".$this->db->escape($this->localtax2_type)."',";
484 $sql .= ' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product : "null").',';
485 $sql .= " ".((int) $this->product_type).",";
486 $sql .= " '".price2num($this->remise_percent)."',";
487 $sql .= " ".(price2num($this->subprice) !== '' ? price2num($this->subprice) : "null").",";
488 $sql .= " ".($this->price != '' ? "'".price2num($this->price)."'" : "null").",";
489 $sql .= ' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except : "null").',';
490 $sql .= ' '.((int) $this->special_code).',';
491 $sql .= ' '.((int) $this->rang).',';
492 $sql .= ' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice : "null").',';
493 $sql .= ' '.price2num($this->pa_ht).',';
494 $sql .= " ".((int) $this->info_bits).",";
495 $sql .= " ".price2num($this->total_ht, 'MT').",";
496 $sql .= " ".price2num($this->total_tva, 'MT').",";
497 $sql .= " ".price2num($this->total_localtax1, 'MT').",";
498 $sql .= " ".price2num($this->total_localtax2, 'MT').",";
499 $sql .= " ".price2num($this->total_ttc, 'MT').",";
500 $sql .= " ".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null").',';
501 $sql .= " ".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null").',';
502 $sql .= ' '.(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
503 $sql .= ", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL');
504 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
505 $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
506 $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
507 $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
508 $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
509 $sql .= ')';
510
511 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
512 $resql = $this->db->query($sql);
513 if ($resql) {
514 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commandedet');
515 $this->rowid = $this->id;
516
517 $result = $this->insertExtraFields();
518 if ($result < 0) {
519 $error++;
520 }
521
522 if (!$error && !$notrigger) {
523 // Call trigger
524 $result = $this->call_trigger('LINEORDER_INSERT', $user);
525 if ($result < 0) {
526 $error++;
527 }
528 // End call triggers
529 }
530
531 if (!$error) {
532 $this->db->commit();
533 return $this->id;
534 }
535
536 foreach ($this->errors as $errmsg) {
537 dol_syslog(get_class($this)."::insert ".$errmsg, LOG_ERR);
538 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
539 }
540 $this->db->rollback();
541 return -1 * $error;
542 } else {
543 $this->error = $this->db->error();
544 $this->db->rollback();
545 return -2;
546 }
547 }
548
556 public function update(User $user, $notrigger = 0)
557 {
558 $error = 0;
559
560 $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'.
561
562 // Clean parameters
563 if (empty($this->tva_tx)) {
564 $this->tva_tx = 0;
565 }
566 if (empty($this->localtax1_tx)) {
567 $this->localtax1_tx = 0;
568 }
569 if (empty($this->localtax2_tx)) {
570 $this->localtax2_tx = 0;
571 }
572 if (empty($this->localtax1_type)) {
573 $this->localtax1_type = '0';
574 }
575 if (empty($this->localtax2_type)) {
576 $this->localtax2_type = '0';
577 }
578 if (empty($this->qty)) {
579 $this->qty = 0;
580 }
581 if (empty($this->total_localtax1)) {
582 $this->total_localtax1 = 0;
583 }
584 if (empty($this->total_localtax2)) {
585 $this->total_localtax2 = 0;
586 }
587 if (empty($this->marque_tx)) {
588 $this->marque_tx = 0;
589 }
590 if (empty($this->marge_tx)) {
591 $this->marge_tx = 0;
592 }
593 if (empty($this->remise_percent)) {
594 $this->remise_percent = 0;
595 }
596 if (empty($this->price)) {
597 $this->price = 0;
598 }
599 if (empty($this->remise)) {
600 $this->remise = 0;
601 }
602 if (empty($this->info_bits)) {
603 $this->info_bits = 0;
604 }
605 if (empty($this->special_code)) {
606 $this->special_code = 0;
607 }
608 if (empty($this->product_type)) {
609 $this->product_type = 0;
610 }
611 if (empty($this->fk_parent_line)) {
612 $this->fk_parent_line = 0;
613 }
614 if (empty($this->pa_ht)) {
615 $this->pa_ht = 0;
616 }
617 if (empty($this->ref_ext)) {
618 $this->ref_ext = '';
619 }
620
621 // if buy price not defined, define buyprice as configured in margin admin
622 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
623 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
624 if ($result < 0) {
625 return $result;
626 } else {
627 $this->pa_ht = $result;
628 }
629 }
630
631 $this->db->begin();
632
633 // Update line in database
634 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
635 $sql .= " description='".$this->db->escape($this->desc)."'";
636 $sql .= " , label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
637 $sql .= " , vat_src_code=".(!empty($this->vat_src_code) ? "'".$this->db->escape($this->vat_src_code)."'" : "''");
638 $sql .= " , tva_tx=".price2num($this->tva_tx);
639 $sql .= " , localtax1_tx=".price2num($this->localtax1_tx);
640 $sql .= " , localtax2_tx=".price2num($this->localtax2_tx);
641 $sql .= " , localtax1_type='".$this->db->escape($this->localtax1_type)."'";
642 $sql .= " , localtax2_type='".$this->db->escape($this->localtax2_type)."'";
643 $sql .= " , qty=".price2num($this->qty);
644 $sql .= " , ref_ext='".$this->db->escape($this->ref_ext)."'";
645 $sql .= " , subprice=".price2num($this->subprice);
646 $sql .= " , remise_percent=".price2num($this->remise_percent);
647 $sql .= " , price=".price2num($this->price); // TODO A virer
648 $sql .= " , remise=".price2num($this->remise); // TODO A virer
649 if (empty($this->skip_update_total)) {
650 $sql .= " , total_ht=".price2num($this->total_ht);
651 $sql .= " , total_tva=".price2num($this->total_tva);
652 $sql .= " , total_ttc=".price2num($this->total_ttc);
653 $sql .= " , total_localtax1=".price2num($this->total_localtax1);
654 $sql .= " , total_localtax2=".price2num($this->total_localtax2);
655 }
656 $sql .= " , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice : "null");
657 $sql .= " , buy_price_ht='".price2num($this->pa_ht)."'";
658 $sql .= " , info_bits=".((int) $this->info_bits);
659 $sql .= " , special_code=".((int) $this->special_code);
660 $sql .= " , date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
661 $sql .= " , date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
662 $sql .= " , product_type = ".((int) $this->product_type);
663 $sql .= " , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line : "null");
664 if (!empty($this->rang)) {
665 $sql .= ", rang=".((int) $this->rang);
666 }
667 $sql .= " , fk_unit=".(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
668
669 // Multicurrency
670 $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice);
671 $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
672 $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
673 $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
674
675 $sql .= " WHERE rowid = ".((int) $this->rowid);
676
677 dol_syslog(get_class($this)."::update", LOG_DEBUG);
678 $resql = $this->db->query($sql);
679 if ($resql) {
680 $this->id = $this->rowid;
681 $result = $this->insertExtraFields();
682 if ($result < 0) {
683 $error++;
684 }
685
686 if (!$error && !$notrigger) {
687 // Call trigger
688 $result = $this->call_trigger('LINEORDER_MODIFY', $user);
689 if ($result < 0) {
690 $error++;
691 }
692 // End call triggers
693 }
694
695 if (!$error) {
696 $this->db->commit();
697 return 1;
698 }
699
700 foreach ($this->errors as $errmsg) {
701 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
702 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
703 }
704 $this->db->rollback();
705 return -1 * $error;
706 } else {
707 $this->error = $this->db->error();
708 $this->db->rollback();
709 return -2;
710 }
711 }
712
713 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
720 public function update_total()
721 {
722 // phpcs:enable
723 $this->db->begin();
724
725 // Clean parameters
726 if (empty($this->total_localtax1)) {
727 $this->total_localtax1 = 0;
728 }
729 if (empty($this->total_localtax2)) {
730 $this->total_localtax2 = 0;
731 }
732
733 // Update line in database
734 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
735 $sql .= " total_ht='".price2num($this->total_ht)."'";
736 $sql .= ",total_tva='".price2num($this->total_tva)."'";
737 $sql .= ",total_localtax1='".price2num($this->total_localtax1)."'";
738 $sql .= ",total_localtax2='".price2num($this->total_localtax2)."'";
739 $sql .= ",total_ttc='".price2num($this->total_ttc)."'";
740 $sql .= " WHERE rowid = ".((int) $this->rowid);
741
742 dol_syslog("OrderLine::update_total", LOG_DEBUG);
743
744 $resql = $this->db->query($sql);
745 if ($resql) {
746 $this->db->commit();
747 return 1;
748 } else {
749 $this->error = $this->db->error();
750 $this->db->rollback();
751 return -2;
752 }
753 }
754}
$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.