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);
223 $objp = $this->db->fetch_object($result);
226 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
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;
264 $this->
ref = $objp->product_ref;
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;
277 $this->date_start = $this->db->jdate($objp->date_start);
278 $this->date_end = $this->db->jdate($objp->date_end);
280 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
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;
291 $this->db->free($result);
295 $this->error = $this->db->lasterror();
307 public function delete(
User $user, $notrigger = 0)
309 global
$conf, $langs;
313 if (empty($this->
id) && !empty($this->rowid)) {
314 $this->
id = $this->rowid;
318 $sqlCheckShipmentLine =
"SELECT";
319 $sqlCheckShipmentLine .=
" ed.rowid";
320 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
321 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
323 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
324 if (!$resqlCheckShipmentLine) {
326 $this->error = $this->db->lasterror();
327 $this->errors[] = $this->error;
329 $langs->load(
'errors');
330 $num = $this->db->num_rows($resqlCheckShipmentLine);
333 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
334 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
335 $this->errors[] = $this->error;
337 $this->db->free($resqlCheckShipmentLine);
340 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
348 $result = $this->call_trigger(
'LINEORDER_DELETE', $user);
356 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
359 $resql = $this->db->query($sql);
361 $this->error = $this->db->lasterror();
371 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
380 foreach ($this->errors as $errmsg) {
381 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
382 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
384 $this->db->rollback();
395 public function insert($user =
null, $notrigger = 0)
399 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
400 $this->pa_ht = (float) $this->pa_ht;
402 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
405 if (empty($this->tva_tx)) {
408 if (empty($this->localtax1_tx)) {
409 $this->localtax1_tx = 0;
411 if (empty($this->localtax2_tx)) {
412 $this->localtax2_tx = 0;
414 if (empty($this->localtax1_type)) {
415 $this->localtax1_type =
'0';
417 if (empty($this->localtax2_type)) {
418 $this->localtax2_type =
'0';
420 if (empty($this->total_localtax1)) {
421 $this->total_localtax1 = 0;
423 if (empty($this->total_localtax2)) {
424 $this->total_localtax2 = 0;
426 if (empty($this->rang)) {
429 if (empty($this->remise_percent)) {
430 $this->remise_percent = 0;
432 if (empty($this->info_bits)) {
433 $this->info_bits = 0;
435 if (empty($this->special_code)) {
436 $this->special_code = 0;
438 if (empty($this->fk_parent_line)) {
439 $this->fk_parent_line = 0;
441 if (empty($this->ref_ext)) {
446 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
447 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
451 $this->pa_ht = $result;
456 if ($this->product_type < 0) {
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,';
470 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
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');
511 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
512 $resql = $this->db->query($sql);
514 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
515 $this->rowid = $this->id;
522 if (!$error && !$notrigger) {
524 $result = $this->call_trigger(
'LINEORDER_INSERT', $user);
536 foreach ($this->errors as $errmsg) {
537 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
538 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
540 $this->db->rollback();
543 $this->error = $this->db->error();
544 $this->db->rollback();
560 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
563 if (empty($this->tva_tx)) {
566 if (empty($this->localtax1_tx)) {
567 $this->localtax1_tx = 0;
569 if (empty($this->localtax2_tx)) {
570 $this->localtax2_tx = 0;
572 if (empty($this->localtax1_type)) {
573 $this->localtax1_type =
'0';
575 if (empty($this->localtax2_type)) {
576 $this->localtax2_type =
'0';
578 if (empty($this->qty)) {
581 if (empty($this->total_localtax1)) {
582 $this->total_localtax1 = 0;
584 if (empty($this->total_localtax2)) {
585 $this->total_localtax2 = 0;
587 if (empty($this->marque_tx)) {
588 $this->marque_tx = 0;
590 if (empty($this->marge_tx)) {
593 if (empty($this->remise_percent)) {
594 $this->remise_percent = 0;
596 if (empty($this->
price)) {
599 if (empty($this->remise)) {
602 if (empty($this->info_bits)) {
603 $this->info_bits = 0;
605 if (empty($this->special_code)) {
606 $this->special_code = 0;
608 if (empty($this->product_type)) {
609 $this->product_type = 0;
611 if (empty($this->fk_parent_line)) {
612 $this->fk_parent_line = 0;
614 if (empty($this->pa_ht)) {
617 if (empty($this->ref_ext)) {
622 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
623 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
627 $this->pa_ht = $result;
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);
648 $sql .=
" , remise=".price2num($this->remise);
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);
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);
667 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
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);
675 $sql .=
" WHERE rowid = ".((int) $this->rowid);
677 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
678 $resql = $this->db->query($sql);
680 $this->
id = $this->rowid;
686 if (!$error && !$notrigger) {
688 $result = $this->call_trigger(
'LINEORDER_MODIFY', $user);
700 foreach ($this->errors as $errmsg) {
701 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
702 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
704 $this->db->rollback();
707 $this->error = $this->db->error();
708 $this->db->rollback();
726 if (empty($this->total_localtax1)) {
727 $this->total_localtax1 = 0;
729 if (empty($this->total_localtax2)) {
730 $this->total_localtax2 = 0;
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);
742 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
744 $resql = $this->db->query($sql);
749 $this->error = $this->db->error();
750 $this->db->rollback();