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);
241 $objp = $this->db->fetch_object($result);
244 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
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;
282 $this->
ref = $objp->product_ref;
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;
295 $this->date_start = $this->db->jdate($objp->date_start);
296 $this->date_end = $this->db->jdate($objp->date_end);
298 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
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;
309 $this->db->free($result);
313 $this->error = $this->db->lasterror();
325 public function delete(
User $user, $notrigger = 0)
327 global
$conf, $langs;
331 if (empty($this->
id) && !empty($this->rowid)) {
332 $this->
id = $this->rowid;
336 $sqlCheckShipmentLine =
"SELECT";
337 $sqlCheckShipmentLine .=
" ed.rowid";
338 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
339 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
341 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
342 if (!$resqlCheckShipmentLine) {
344 $this->error = $this->db->lasterror();
345 $this->errors[] = $this->error;
347 $langs->load(
'errors');
348 $num = $this->db->num_rows($resqlCheckShipmentLine);
351 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
352 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
353 $this->errors[] = $this->error;
355 $this->db->free($resqlCheckShipmentLine);
358 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
366 $result = $this->call_trigger(
'LINEORDER_DELETE', $user);
374 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
377 $resql = $this->db->query($sql);
379 $this->error = $this->db->lasterror();
389 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
398 foreach ($this->errors as $errmsg) {
399 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
400 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
402 $this->db->rollback();
413 public function insert($user =
null, $notrigger = 0)
417 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
418 $this->pa_ht = (float) $this->pa_ht;
420 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
423 if (empty($this->tva_tx)) {
426 if (empty($this->localtax1_tx)) {
427 $this->localtax1_tx = 0;
429 if (empty($this->localtax2_tx)) {
430 $this->localtax2_tx = 0;
432 if (empty($this->localtax1_type)) {
433 $this->localtax1_type =
'0';
435 if (empty($this->localtax2_type)) {
436 $this->localtax2_type =
'0';
438 if (empty($this->total_localtax1)) {
439 $this->total_localtax1 = 0;
441 if (empty($this->total_localtax2)) {
442 $this->total_localtax2 = 0;
444 if (empty($this->rang)) {
447 if (empty($this->remise_percent)) {
448 $this->remise_percent = 0;
450 if (empty($this->info_bits)) {
451 $this->info_bits = 0;
453 if (empty($this->special_code)) {
454 $this->special_code = 0;
456 if (empty($this->fk_parent_line)) {
457 $this->fk_parent_line = 0;
459 if (empty($this->ref_ext)) {
464 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
465 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
469 $this->pa_ht = $result;
474 if ($this->product_type < 0) {
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,';
488 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
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');
529 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
530 $resql = $this->db->query($sql);
532 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
533 $this->rowid = $this->id;
540 if (!$error && !$notrigger) {
542 $result = $this->call_trigger(
'LINEORDER_INSERT', $user);
554 foreach ($this->errors as $errmsg) {
555 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
556 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
558 $this->db->rollback();
561 $this->error = $this->db->error();
562 $this->db->rollback();
578 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
581 if (empty($this->tva_tx)) {
584 if (empty($this->localtax1_tx)) {
585 $this->localtax1_tx = 0;
587 if (empty($this->localtax2_tx)) {
588 $this->localtax2_tx = 0;
590 if (empty($this->localtax1_type)) {
591 $this->localtax1_type =
'0';
593 if (empty($this->localtax2_type)) {
594 $this->localtax2_type =
'0';
596 if (empty($this->qty)) {
599 if (empty($this->total_localtax1)) {
600 $this->total_localtax1 = 0;
602 if (empty($this->total_localtax2)) {
603 $this->total_localtax2 = 0;
605 if (empty($this->marque_tx)) {
606 $this->marque_tx = 0;
608 if (empty($this->marge_tx)) {
611 if (empty($this->remise_percent)) {
612 $this->remise_percent = 0;
614 if (empty($this->
price)) {
617 if (empty($this->remise)) {
620 if (empty($this->info_bits)) {
621 $this->info_bits = 0;
623 if (empty($this->special_code)) {
624 $this->special_code = 0;
626 if (empty($this->product_type)) {
627 $this->product_type = 0;
629 if (empty($this->fk_parent_line)) {
630 $this->fk_parent_line = 0;
632 if (empty($this->pa_ht)) {
635 if (empty($this->ref_ext)) {
640 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
641 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
645 $this->pa_ht = $result;
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);
666 $sql .=
" , remise=".price2num($this->remise);
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);
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);
685 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
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);
693 $sql .=
" WHERE rowid = ".((int) $this->rowid);
695 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
696 $resql = $this->db->query($sql);
698 $this->
id = $this->rowid;
704 if (!$error && !$notrigger) {
706 $result = $this->call_trigger(
'LINEORDER_MODIFY', $user);
718 foreach ($this->errors as $errmsg) {
719 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
720 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
722 $this->db->rollback();
725 $this->error = $this->db->error();
726 $this->db->rollback();