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);
221 $objp = $this->db->fetch_object($result);
224 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
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;
262 $this->
ref = $objp->product_ref;
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;
271 $this->date_start = $this->db->jdate($objp->date_start);
272 $this->date_end = $this->db->jdate($objp->date_end);
274 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
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;
285 $this->db->free($result);
289 $this->error = $this->db->lasterror();
301 public function delete(
User $user, $notrigger = 0)
303 global
$conf, $langs;
307 if (empty($this->
id) && !empty($this->rowid)) {
308 $this->
id = $this->rowid;
312 $sqlCheckShipmentLine =
"SELECT";
313 $sqlCheckShipmentLine .=
" ed.rowid";
314 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
315 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
317 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
318 if (!$resqlCheckShipmentLine) {
320 $this->error = $this->db->lasterror();
321 $this->errors[] = $this->error;
323 $langs->load(
'errors');
324 $num = $this->db->num_rows($resqlCheckShipmentLine);
327 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
328 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
329 $this->errors[] = $this->error;
331 $this->db->free($resqlCheckShipmentLine);
334 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
342 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
350 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
353 $resql = $this->db->query($sql);
355 $this->error = $this->db->lasterror();
365 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
374 foreach ($this->errors as $errmsg) {
375 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
376 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
378 $this->db->rollback();
389 public function insert($user =
null, $notrigger = 0)
393 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
394 $this->pa_ht = (float) $this->pa_ht;
396 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
399 if (empty($this->tva_tx)) {
402 if (empty($this->localtax1_tx)) {
403 $this->localtax1_tx = 0;
405 if (empty($this->localtax2_tx)) {
406 $this->localtax2_tx = 0;
408 if (empty($this->localtax1_type)) {
409 $this->localtax1_type =
'0';
411 if (empty($this->localtax2_type)) {
412 $this->localtax2_type =
'0';
414 if (empty($this->total_localtax1)) {
415 $this->total_localtax1 = 0;
417 if (empty($this->total_localtax2)) {
418 $this->total_localtax2 = 0;
420 if (empty($this->rang)) {
423 if (empty($this->remise_percent)) {
424 $this->remise_percent = 0;
426 if (empty($this->info_bits)) {
427 $this->info_bits = 0;
429 if (empty($this->special_code)) {
430 $this->special_code = 0;
432 if (empty($this->fk_parent_line)) {
433 $this->fk_parent_line = 0;
435 if (empty($this->ref_ext)) {
440 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
441 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
445 $this->pa_ht = $result;
450 if ($this->product_type < 0) {
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,';
464 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
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');
505 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
506 $resql = $this->db->query($sql);
508 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
509 $this->rowid = $this->id;
518 if (!$error && !$notrigger) {
520 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
532 foreach ($this->errors as $errmsg) {
533 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
534 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
536 $this->db->rollback();
539 $this->error = $this->db->error();
540 $this->db->rollback();
556 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
559 if (empty($this->tva_tx)) {
562 if (empty($this->localtax1_tx)) {
563 $this->localtax1_tx = 0;
565 if (empty($this->localtax2_tx)) {
566 $this->localtax2_tx = 0;
568 if (empty($this->localtax1_type)) {
569 $this->localtax1_type =
'0';
571 if (empty($this->localtax2_type)) {
572 $this->localtax2_type =
'0';
574 if (empty($this->qty)) {
577 if (empty($this->total_localtax1)) {
578 $this->total_localtax1 = 0;
580 if (empty($this->total_localtax2)) {
581 $this->total_localtax2 = 0;
583 if (empty($this->marque_tx)) {
584 $this->marque_tx = 0;
586 if (empty($this->marge_tx)) {
589 if (empty($this->remise_percent)) {
590 $this->remise_percent = 0;
592 if (empty($this->remise)) {
595 if (empty($this->info_bits)) {
596 $this->info_bits = 0;
598 if (empty($this->special_code)) {
599 $this->special_code = 0;
601 if (empty($this->product_type)) {
602 $this->product_type = 0;
604 if (empty($this->fk_parent_line)) {
605 $this->fk_parent_line = 0;
607 if (empty($this->pa_ht)) {
610 if (empty($this->ref_ext)) {
615 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
616 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
620 $this->pa_ht = $result;
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);
641 $sql .=
" , remise=".price2num($this->remise);
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);
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);
660 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
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);
668 $sql .=
" WHERE rowid = ".((int) $this->rowid);
670 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
671 $resql = $this->db->query($sql);
674 $this->
id = $this->rowid;
681 if (!$error && !$notrigger) {
683 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
695 foreach ($this->errors as $errmsg) {
696 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
697 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
699 $this->db->rollback();
702 $this->error = $this->db->error();
703 $this->db->rollback();
721 if (empty($this->total_localtax1)) {
722 $this->total_localtax1 = 0;
724 if (empty($this->total_localtax2)) {
725 $this->total_localtax2 = 0;
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);
737 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
739 $resql = $this->db->query($sql);
744 $this->error = $this->db->error();
745 $this->db->rollback();