dolibarr 22.0.5
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-2024 Frédéric France <frederic.france@free.fr>
15 * Copyright (C) 2022 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
16 * Copyright (C) 2024-2025 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,';
214 $sql .= ' p.packaging,';
215 $sql .= ' cd.date_start, cd.date_end, cd.vat_src_code, cd.extraparams';
216 $sql .= ' FROM '.MAIN_DB_PREFIX.'commandedet as cd';
217 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON cd.fk_product = p.rowid';
218 $sql .= ' WHERE cd.rowid = '.((int) $rowid);
219 $result = $this->db->query($sql);
220 if ($result) {
221 $objp = $this->db->fetch_object($result);
222
223 if (!$objp) {
224 $this->error = 'OrderLine with id '. $rowid .' not found sql='.$sql;
225 return 0;
226 }
227
228 $this->rowid = $objp->rowid;
229 $this->id = $objp->rowid;
230 $this->fk_commande = $objp->fk_commande;
231 $this->fk_parent_line = $objp->fk_parent_line;
232 $this->label = $objp->custom_label;
233 $this->desc = $objp->description;
234 $this->qty = $objp->qty;
235 $this->price = $objp->price;
236 $this->subprice = $objp->subprice;
237 $this->ref_ext = $objp->ref_ext;
238 $this->vat_src_code = $objp->vat_src_code;
239 $this->tva_tx = $objp->tva_tx;
240 $this->localtax1_tx = $objp->localtax1_tx;
241 $this->localtax2_tx = $objp->localtax2_tx;
242 $this->remise = $objp->remise;
243 $this->remise_percent = $objp->remise_percent;
244 $this->fk_remise_except = $objp->fk_remise_except;
245 $this->fk_product = $objp->fk_product;
246 $this->product_type = $objp->product_type;
247 $this->info_bits = $objp->info_bits;
248 $this->special_code = $objp->special_code;
249 $this->total_ht = $objp->total_ht;
250 $this->total_tva = $objp->total_tva;
251 $this->total_localtax1 = $objp->total_localtax1;
252 $this->total_localtax2 = $objp->total_localtax2;
253 $this->total_ttc = $objp->total_ttc;
254 $this->fk_fournprice = $objp->fk_fournprice;
255 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
256 $this->pa_ht = $marginInfos[0];
257 $this->marge_tx = $marginInfos[1];
258 $this->marque_tx = $marginInfos[2];
259 $this->special_code = $objp->special_code;
260 $this->rang = $objp->rang;
261
262 $this->ref = $objp->product_ref; // deprecated
263
264 $this->product_ref = $objp->product_ref;
265 $this->product_label = $objp->product_label;
266 $this->product_desc = $objp->product_desc;
267 $this->product_tobatch = $objp->product_tobatch;
268 $this->fk_unit = $objp->fk_unit;
269 $this->packaging = $objp->packaging;
270
271 $this->date_start = $this->db->jdate($objp->date_start);
272 $this->date_end = $this->db->jdate($objp->date_end);
273
274 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
275
276 $this->fk_multicurrency = $objp->fk_multicurrency;
277 $this->multicurrency_code = $objp->multicurrency_code;
278 $this->multicurrency_subprice = $objp->multicurrency_subprice;
279 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
280 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
281 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
282
283 $this->fetch_optionals();
284
285 $this->db->free($result);
286
287 return 1;
288 } else {
289 $this->error = $this->db->lasterror();
290 return -1;
291 }
292 }
293
301 public function delete(User $user, $notrigger = 0)
302 {
303 global $conf, $langs;
304
305 $error = 0;
306
307 if (empty($this->id) && !empty($this->rowid)) { // For backward compatibility
308 $this->id = $this->rowid;
309 }
310
311 // check if order line is not in a shipment line before deleting
312 $sqlCheckShipmentLine = "SELECT";
313 $sqlCheckShipmentLine .= " ed.rowid";
314 $sqlCheckShipmentLine .= " FROM " . MAIN_DB_PREFIX . "expeditiondet ed";
315 $sqlCheckShipmentLine .= " WHERE ed.fk_elementdet = " . ((int) $this->id);
316
317 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
318 if (!$resqlCheckShipmentLine) {
319 $error++;
320 $this->error = $this->db->lasterror();
321 $this->errors[] = $this->error;
322 } else {
323 $langs->load('errors');
324 $num = $this->db->num_rows($resqlCheckShipmentLine);
325 if ($num > 0) {
326 $error++;
327 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
328 $this->error = $langs->trans('ErrorRecordAlreadyExists') . ' : ' . $langs->trans('ShipmentLine') . ' ' . $objCheckShipmentLine->rowid;
329 $this->errors[] = $this->error;
330 }
331 $this->db->free($resqlCheckShipmentLine);
332 }
333 if ($error) {
334 dol_syslog(__METHOD__ . 'Error ; ' . $this->error, LOG_ERR);
335 return -1;
336 }
337
338 $this->db->begin();
339
340 if (!$notrigger) {
341 // Call trigger
342 $result = $this->call_trigger('LINEORDER_DELETE', $user);
343 if ($result < 0) {
344 $error++;
345 }
346 // End call triggers
347 }
348
349 if (!$error) {
350 $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . "commandedet WHERE rowid = " . ((int) $this->id);
351
352 dol_syslog("OrderLine::delete", LOG_DEBUG);
353 $resql = $this->db->query($sql);
354 if (!$resql) {
355 $this->error = $this->db->lasterror();
356 $error++;
357 }
358 }
359
360 // Remove extrafields
361 if (!$error) {
362 $result = $this->deleteExtraFields();
363 if ($result < 0) {
364 $error++;
365 dol_syslog(get_class($this) . "::delete error -4 " . $this->error, LOG_ERR);
366 }
367 }
368
369 if (!$error) {
370 $this->db->commit();
371 return 1;
372 }
373
374 foreach ($this->errors as $errmsg) {
375 dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
376 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
377 }
378 $this->db->rollback();
379 return -1 * $error;
380 }
381
389 public function insert($user = null, $notrigger = 0)
390 {
391 $error = 0;
392
393 $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'.
394 $this->pa_ht = (float) $this->pa_ht; // convert to float but after checking if value is empty
395
396 dol_syslog(get_class($this)."::insert rang=".$this->rang);
397
398 // Clean parameters
399 if (empty($this->tva_tx)) {
400 $this->tva_tx = 0;
401 }
402 if (empty($this->localtax1_tx)) {
403 $this->localtax1_tx = 0;
404 }
405 if (empty($this->localtax2_tx)) {
406 $this->localtax2_tx = 0;
407 }
408 if (empty($this->localtax1_type)) {
409 $this->localtax1_type = '0';
410 }
411 if (empty($this->localtax2_type)) {
412 $this->localtax2_type = '0';
413 }
414 if (empty($this->total_localtax1)) {
415 $this->total_localtax1 = 0;
416 }
417 if (empty($this->total_localtax2)) {
418 $this->total_localtax2 = 0;
419 }
420 if (empty($this->rang)) {
421 $this->rang = 0;
422 }
423 if (empty($this->remise_percent)) {
424 $this->remise_percent = 0;
425 }
426 if (empty($this->info_bits)) {
427 $this->info_bits = 0;
428 }
429 if (empty($this->special_code)) {
430 $this->special_code = 0;
431 }
432 if (empty($this->fk_parent_line)) {
433 $this->fk_parent_line = 0;
434 }
435 if (empty($this->ref_ext)) {
436 $this->ref_ext = '';
437 }
438
439 // if buy price not defined (if = ''), we set the buyprice as configured in margin admin setup
440 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
441 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
442 if ($result < 0) {
443 return $result;
444 } else {
445 $this->pa_ht = $result;
446 }
447 }
448
449 // Check parameters
450 if ($this->product_type < 0) {
451 return -1;
452 }
453
454 $this->db->begin();
455
456 // Insertion dans base de la ligne
457 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'commandedet';
458 $sql .= ' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
459 $sql .= ' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
460 $sql .= ' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
461 $sql .= ' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
462 $sql .= ' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
463 $sql .= ' fk_unit,';
464 $sql .= ' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
465 $sql .= ')';
466 $sql .= " VALUES (".$this->fk_commande.",";
467 $sql .= " ".($this->fk_parent_line > 0 ? "'".$this->db->escape((string) $this->fk_parent_line)."'" : "null").",";
468 $sql .= " ".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
469 $sql .= " '".$this->db->escape($this->desc)."',";
470 $sql .= " '".price2num($this->qty)."',";
471 $sql .= " '".$this->db->escape($this->ref_ext)."',";
472 $sql .= " ".(empty($this->vat_src_code) ? "''" : "'".$this->db->escape($this->vat_src_code)."'").",";
473 $sql .= " '".price2num($this->tva_tx)."',";
474 $sql .= " '".price2num($this->localtax1_tx)."',";
475 $sql .= " '".price2num($this->localtax2_tx)."',";
476 $sql .= " '".$this->db->escape($this->localtax1_type)."',";
477 $sql .= " '".$this->db->escape($this->localtax2_type)."',";
478 $sql .= ' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product : "null").',';
479 $sql .= " ".((int) $this->product_type).",";
480 $sql .= " '".price2num($this->remise_percent)."',";
481 $sql .= " ".(price2num($this->subprice) !== '' ? price2num($this->subprice) : "null").",";
482 $sql .= " ".($this->price != '' ? "'".price2num($this->price)."'" : "null").",";
483 $sql .= ' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except : "null").',';
484 $sql .= ' '.((int) $this->special_code).',';
485 $sql .= ' '.((int) $this->rang).',';
486 $sql .= ' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice : "null").',';
487 $sql .= ' '.price2num($this->pa_ht).',';
488 $sql .= " ".((int) $this->info_bits).",";
489 $sql .= " ".price2num($this->total_ht, 'MT').",";
490 $sql .= " ".price2num($this->total_tva, 'MT').",";
491 $sql .= " ".price2num($this->total_localtax1, 'MT').",";
492 $sql .= " ".price2num($this->total_localtax2, 'MT').",";
493 $sql .= " ".price2num($this->total_ttc, 'MT').",";
494 $sql .= " ".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null").',';
495 $sql .= " ".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null").',';
496 $sql .= ' '.(!$this->fk_unit ? 'NULL' : ((int) $this->fk_unit));
497 $sql .= ", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) : 'NULL');
498 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
499 $sql .= ", ".price2num($this->multicurrency_subprice, 'CU');
500 $sql .= ", ".price2num($this->multicurrency_total_ht, 'CT');
501 $sql .= ", ".price2num($this->multicurrency_total_tva, 'CT');
502 $sql .= ", ".price2num($this->multicurrency_total_ttc, 'CT');
503 $sql .= ')';
504
505 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
506 $resql = $this->db->query($sql);
507 if ($resql) {
508 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'commandedet');
509 $this->rowid = $this->id;
510
511 if (!$error) {
512 $result = $this->insertExtraFields();
513 if ($result < 0) {
514 $error++;
515 }
516 }
517
518 if (!$error && !$notrigger) {
519 // Call trigger
520 $result = $this->call_trigger('LINEORDER_INSERT', $user);
521 if ($result < 0) {
522 $error++;
523 }
524 // End call triggers
525 }
526
527 if (!$error) {
528 $this->db->commit();
529 return $this->id;
530 }
531
532 foreach ($this->errors as $errmsg) {
533 dol_syslog(get_class($this)."::insert ".$errmsg, LOG_ERR);
534 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
535 }
536 $this->db->rollback();
537 return -1 * $error;
538 } else {
539 $this->error = $this->db->error();
540 $this->db->rollback();
541 return -2;
542 }
543 }
544
552 public function update(User $user, $notrigger = 0)
553 {
554 $error = 0;
555
556 $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'.
557
558 // Clean parameters
559 if (empty($this->tva_tx)) {
560 $this->tva_tx = 0;
561 }
562 if (empty($this->localtax1_tx)) {
563 $this->localtax1_tx = 0;
564 }
565 if (empty($this->localtax2_tx)) {
566 $this->localtax2_tx = 0;
567 }
568 if (empty($this->localtax1_type)) {
569 $this->localtax1_type = '0';
570 }
571 if (empty($this->localtax2_type)) {
572 $this->localtax2_type = '0';
573 }
574 if (empty($this->qty)) {
575 $this->qty = 0;
576 }
577 if (empty($this->total_localtax1)) {
578 $this->total_localtax1 = 0;
579 }
580 if (empty($this->total_localtax2)) {
581 $this->total_localtax2 = 0;
582 }
583 if (empty($this->marque_tx)) {
584 $this->marque_tx = 0;
585 }
586 if (empty($this->marge_tx)) {
587 $this->marge_tx = 0;
588 }
589 if (empty($this->remise_percent)) {
590 $this->remise_percent = 0;
591 }
592 if (empty($this->remise)) {
593 $this->remise = 0;
594 }
595 if (empty($this->info_bits)) {
596 $this->info_bits = 0;
597 }
598 if (empty($this->special_code)) {
599 $this->special_code = 0;
600 }
601 if (empty($this->product_type)) {
602 $this->product_type = 0;
603 }
604 if (empty($this->fk_parent_line)) {
605 $this->fk_parent_line = 0;
606 }
607 if (empty($this->pa_ht)) {
608 $this->pa_ht = 0;
609 }
610 if (empty($this->ref_ext)) {
611 $this->ref_ext = '';
612 }
613
614 // if buy price not defined, define buyprice as configured in margin admin
615 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
616 $result = $this->defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
617 if ($result < 0) {
618 return $result;
619 } else {
620 $this->pa_ht = $result;
621 }
622 }
623
624 $this->db->begin();
625
626 // Mise a jour ligne en base
627 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
628 $sql .= " description='".$this->db->escape($this->desc)."'";
629 $sql .= " , label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
630 $sql .= " , vat_src_code=".(!empty($this->vat_src_code) ? "'".$this->db->escape($this->vat_src_code)."'" : "''");
631 $sql .= " , tva_tx=".price2num($this->tva_tx);
632 $sql .= " , localtax1_tx=".price2num($this->localtax1_tx);
633 $sql .= " , localtax2_tx=".price2num($this->localtax2_tx);
634 $sql .= " , localtax1_type='".$this->db->escape($this->localtax1_type)."'";
635 $sql .= " , localtax2_type='".$this->db->escape($this->localtax2_type)."'";
636 $sql .= " , qty=".price2num($this->qty);
637 $sql .= " , ref_ext='".$this->db->escape($this->ref_ext)."'";
638 $sql .= " , subprice=".price2num($this->subprice);
639 $sql .= " , remise_percent=".price2num($this->remise_percent);
640 $sql .= " , price=".price2num($this->price); // TODO A virer
641 $sql .= " , remise=".price2num($this->remise); // TODO A virer
642 if (empty($this->skip_update_total)) {
643 $sql .= " , total_ht=".price2num($this->total_ht);
644 $sql .= " , total_tva=".price2num($this->total_tva);
645 $sql .= " , total_ttc=".price2num($this->total_ttc);
646 $sql .= " , total_localtax1=".price2num($this->total_localtax1);
647 $sql .= " , total_localtax2=".price2num($this->total_localtax2);
648 }
649 $sql .= " , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice : "null");
650 $sql .= " , buy_price_ht='".price2num($this->pa_ht)."'";
651 $sql .= " , info_bits=".((int) $this->info_bits);
652 $sql .= " , special_code=".((int) $this->special_code);
653 $sql .= " , date_start=".(!empty($this->date_start) ? "'".$this->db->idate($this->date_start)."'" : "null");
654 $sql .= " , date_end=".(!empty($this->date_end) ? "'".$this->db->idate($this->date_end)."'" : "null");
655 $sql .= " , product_type = ".((int) $this->product_type);
656 $sql .= " , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line : "null");
657 if (!empty($this->rang)) {
658 $sql .= ", rang=".((int) $this->rang);
659 }
660 $sql .= " , fk_unit=".(!$this->fk_unit ? 'NULL' : $this->fk_unit);
661
662 // Multicurrency
663 $sql .= " , multicurrency_subprice=".price2num($this->multicurrency_subprice);
664 $sql .= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
665 $sql .= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
666 $sql .= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
667
668 $sql .= " WHERE rowid = ".((int) $this->rowid);
669
670 dol_syslog(get_class($this)."::update", LOG_DEBUG);
671 $resql = $this->db->query($sql);
672 if ($resql) {
673 if (!$error) {
674 $this->id = $this->rowid;
675 $result = $this->insertExtraFields();
676 if ($result < 0) {
677 $error++;
678 }
679 }
680
681 if (!$error && !$notrigger) {
682 // Call trigger
683 $result = $this->call_trigger('LINEORDER_MODIFY', $user);
684 if ($result < 0) {
685 $error++;
686 }
687 // End call triggers
688 }
689
690 if (!$error) {
691 $this->db->commit();
692 return 1;
693 }
694
695 foreach ($this->errors as $errmsg) {
696 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
697 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
698 }
699 $this->db->rollback();
700 return -1 * $error;
701 } else {
702 $this->error = $this->db->error();
703 $this->db->rollback();
704 return -2;
705 }
706 }
707
708 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
715 public function update_total()
716 {
717 // phpcs:enable
718 $this->db->begin();
719
720 // Clean parameters
721 if (empty($this->total_localtax1)) {
722 $this->total_localtax1 = 0;
723 }
724 if (empty($this->total_localtax2)) {
725 $this->total_localtax2 = 0;
726 }
727
728 // Mise a jour ligne en base
729 $sql = "UPDATE ".MAIN_DB_PREFIX."commandedet SET";
730 $sql .= " total_ht='".price2num($this->total_ht)."'";
731 $sql .= ",total_tva='".price2num($this->total_tva)."'";
732 $sql .= ",total_localtax1='".price2num($this->total_localtax1)."'";
733 $sql .= ",total_localtax2='".price2num($this->total_localtax2)."'";
734 $sql .= ",total_ttc='".price2num($this->total_ttc)."'";
735 $sql .= " WHERE rowid = ".((int) $this->rowid);
736
737 dol_syslog("OrderLine::update_total", LOG_DEBUG);
738
739 $resql = $this->db->query($sql);
740 if ($resql) {
741 $this->db->commit();
742 return 1;
743 } else {
744 $this->error = $this->db->error();
745 $this->db->rollback();
746 return -2;
747 }
748 }
749}
$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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
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.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79