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);
242 $objp = $this->db->fetch_object($result);
245 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
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;
284 $this->
ref = $objp->product_ref;
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;
297 $this->date_start = $this->db->jdate($objp->date_start);
298 $this->date_end = $this->db->jdate($objp->date_end);
300 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
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;
311 $this->db->free($result);
315 $this->error = $this->db->lasterror();
327 public function delete(
User $user, $notrigger = 0)
329 global
$conf, $langs;
333 if (empty($this->
id) && !empty($this->rowid)) {
334 $this->
id = $this->rowid;
338 $sqlCheckShipmentLine =
"SELECT";
339 $sqlCheckShipmentLine .=
" ed.rowid";
340 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
341 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
343 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
344 if (!$resqlCheckShipmentLine) {
346 $this->error = $this->db->lasterror();
347 $this->errors[] = $this->error;
349 $langs->load(
'errors');
350 $num = $this->db->num_rows($resqlCheckShipmentLine);
353 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
354 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
355 $this->errors[] = $this->error;
357 $this->db->free($resqlCheckShipmentLine);
360 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
368 $result = $this->call_trigger(
'LINEORDER_DELETE', $user);
376 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
379 $resql = $this->db->query($sql);
381 $this->error = $this->db->lasterror();
391 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
400 foreach ($this->errors as $errmsg) {
401 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
402 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
404 $this->db->rollback();
415 public function insert($user =
null, $notrigger = 0)
419 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
420 $this->pa_ht = (float) $this->pa_ht;
422 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
425 if (empty($this->tva_tx)) {
428 if (empty($this->localtax1_tx)) {
429 $this->localtax1_tx = 0;
431 if (empty($this->localtax2_tx)) {
432 $this->localtax2_tx = 0;
434 if (empty($this->localtax1_type)) {
435 $this->localtax1_type =
'0';
437 if (empty($this->localtax2_type)) {
438 $this->localtax2_type =
'0';
440 if (empty($this->total_localtax1)) {
441 $this->total_localtax1 = 0;
443 if (empty($this->total_localtax2)) {
444 $this->total_localtax2 = 0;
446 if (empty($this->rang)) {
449 if (empty($this->remise_percent)) {
450 $this->remise_percent = 0;
452 if (empty($this->subprice_ttc)) {
453 $this->subprice_ttc = 0;
455 if (empty($this->info_bits)) {
456 $this->info_bits = 0;
458 if (empty($this->special_code)) {
459 $this->special_code = 0;
461 if (empty($this->fk_parent_line)) {
462 $this->fk_parent_line = 0;
464 if (empty($this->ref_ext)) {
469 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
470 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
474 $this->pa_ht = $result;
479 if ($this->product_type < 0) {
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,';
493 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
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');
535 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
536 $resql = $this->db->query($sql);
538 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
539 $this->rowid = $this->id;
546 if (!$error && !$notrigger) {
548 $result = $this->call_trigger(
'LINEORDER_INSERT', $user);
560 foreach ($this->errors as $errmsg) {
561 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
562 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
564 $this->db->rollback();
567 $this->error = $this->db->error();
568 $this->db->rollback();
584 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
587 if (empty($this->tva_tx)) {
590 if (empty($this->localtax1_tx)) {
591 $this->localtax1_tx = 0;
593 if (empty($this->localtax2_tx)) {
594 $this->localtax2_tx = 0;
596 if (empty($this->localtax1_type)) {
597 $this->localtax1_type =
'0';
599 if (empty($this->localtax2_type)) {
600 $this->localtax2_type =
'0';
602 if (empty($this->qty)) {
605 if (empty($this->subprice_ttc)) {
606 $this->subprice_ttc = 0;
608 if (empty($this->total_localtax1)) {
609 $this->total_localtax1 = 0;
611 if (empty($this->total_localtax2)) {
612 $this->total_localtax2 = 0;
614 if (empty($this->marque_tx)) {
615 $this->marque_tx = 0;
617 if (empty($this->marge_tx)) {
620 if (empty($this->remise_percent)) {
621 $this->remise_percent = 0;
623 if (empty($this->
price)) {
626 if (empty($this->remise)) {
629 if (empty($this->info_bits)) {
630 $this->info_bits = 0;
632 if (empty($this->special_code)) {
633 $this->special_code = 0;
635 if (empty($this->product_type)) {
636 $this->product_type = 0;
638 if (empty($this->fk_parent_line)) {
639 $this->fk_parent_line = 0;
641 if (empty($this->pa_ht)) {
644 if (empty($this->ref_ext)) {
649 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
650 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
654 $this->pa_ht = $result;
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);
676 $sql .=
" , remise=".price2num($this->remise);
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);
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);
695 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
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);
703 $sql .=
" WHERE rowid = ".((int) $this->rowid);
705 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
706 $resql = $this->db->query($sql);
708 $this->
id = $this->rowid;
714 if (!$error && !$notrigger) {
716 $result = $this->call_trigger(
'LINEORDER_MODIFY', $user);
728 foreach ($this->errors as $errmsg) {
729 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
730 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
732 $this->db->rollback();
735 $this->error = $this->db->error();
736 $this->db->rollback();