167 $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,';
168 $sql .=
' cd.remise, cd.remise_percent, cd.fk_remise_except, cd.subprice, cd.ref_ext,';
169 $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,';
170 $sql .=
' cd.fk_unit,';
171 $sql .=
' cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc,';
172 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc, p.tobatch as product_tobatch,';
173 $sql .=
' cd.date_start, cd.date_end, cd.vat_src_code';
174 $sql .=
' FROM '.MAIN_DB_PREFIX.
'commandedet as cd';
175 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON cd.fk_product = p.rowid';
176 $sql .=
' WHERE cd.rowid = '.((int) $rowid);
177 $result = $this->db->query($sql);
179 $objp = $this->db->fetch_object($result);
182 $this->error =
'OrderLine with id '. $rowid .
' not found sql='.$sql;
186 $this->rowid = $objp->rowid;
187 $this->
id = $objp->rowid;
188 $this->fk_commande = $objp->fk_commande;
189 $this->fk_parent_line = $objp->fk_parent_line;
190 $this->label = $objp->custom_label;
191 $this->desc = $objp->description;
192 $this->qty = $objp->qty;
193 $this->
price = $objp->price;
194 $this->subprice = $objp->subprice;
195 $this->ref_ext = $objp->ref_ext;
196 $this->vat_src_code = $objp->vat_src_code;
197 $this->tva_tx = $objp->tva_tx;
198 $this->localtax1_tx = $objp->localtax1_tx;
199 $this->localtax2_tx = $objp->localtax2_tx;
200 $this->remise = $objp->remise;
201 $this->remise_percent = $objp->remise_percent;
202 $this->fk_remise_except = $objp->fk_remise_except;
203 $this->fk_product = $objp->fk_product;
204 $this->product_type = $objp->product_type;
205 $this->info_bits = $objp->info_bits;
206 $this->special_code = $objp->special_code;
207 $this->total_ht = $objp->total_ht;
208 $this->total_tva = $objp->total_tva;
209 $this->total_localtax1 = $objp->total_localtax1;
210 $this->total_localtax2 = $objp->total_localtax2;
211 $this->total_ttc = $objp->total_ttc;
212 $this->fk_fournprice = $objp->fk_fournprice;
213 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
214 $this->pa_ht = $marginInfos[0];
215 $this->marge_tx = $marginInfos[1];
216 $this->marque_tx = $marginInfos[2];
217 $this->special_code = $objp->special_code;
218 $this->rang = $objp->rang;
220 $this->
ref = $objp->product_ref;
222 $this->product_ref = $objp->product_ref;
223 $this->product_label = $objp->product_label;
224 $this->product_desc = $objp->product_desc;
225 $this->product_tobatch = $objp->product_tobatch;
226 $this->fk_unit = $objp->fk_unit;
228 $this->date_start = $this->db->jdate($objp->date_start);
229 $this->date_end = $this->db->jdate($objp->date_end);
231 $this->fk_multicurrency = $objp->fk_multicurrency;
232 $this->multicurrency_code = $objp->multicurrency_code;
233 $this->multicurrency_subprice = $objp->multicurrency_subprice;
234 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
235 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
236 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
240 $this->db->free($result);
244 $this->error = $this->db->lasterror();
256 public function delete(
User $user, $notrigger = 0)
258 global
$conf, $langs;
262 if (empty($this->
id) && !empty($this->rowid)) {
263 $this->
id = $this->rowid;
267 $sqlCheckShipmentLine =
"SELECT";
268 $sqlCheckShipmentLine .=
" ed.rowid";
269 $sqlCheckShipmentLine .=
" FROM " . MAIN_DB_PREFIX .
"expeditiondet ed";
270 $sqlCheckShipmentLine .=
" WHERE ed.fk_elementdet = " . ((int) $this->
id);
272 $resqlCheckShipmentLine = $this->db->query($sqlCheckShipmentLine);
273 if (!$resqlCheckShipmentLine) {
275 $this->error = $this->db->lasterror();
276 $this->errors[] = $this->error;
278 $langs->load(
'errors');
279 $num = $this->db->num_rows($resqlCheckShipmentLine);
282 $objCheckShipmentLine = $this->db->fetch_object($resqlCheckShipmentLine);
283 $this->error = $langs->trans(
'ErrorRecordAlreadyExists') .
' : ' . $langs->trans(
'ShipmentLine') .
' ' . $objCheckShipmentLine->rowid;
284 $this->errors[] = $this->error;
286 $this->db->free($resqlCheckShipmentLine);
289 dol_syslog(__METHOD__ .
'Error ; ' . $this->error, LOG_ERR);
297 $result = $this->
call_trigger(
'LINEORDER_DELETE', $user);
305 $sql =
'DELETE FROM ' . MAIN_DB_PREFIX .
"commandedet WHERE rowid = " . ((int) $this->
id);
308 $resql = $this->db->query($sql);
310 $this->error = $this->db->lasterror();
320 dol_syslog(get_class($this) .
"::delete error -4 " . $this->error, LOG_ERR);
329 foreach ($this->errors as $errmsg) {
330 dol_syslog(get_class($this) .
"::delete " . $errmsg, LOG_ERR);
331 $this->error .= ($this->error ?
', ' . $errmsg : $errmsg);
333 $this->db->rollback();
344 public function insert($user =
null, $notrigger = 0)
348 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
349 $this->pa_ht = (float) $this->pa_ht;
351 dol_syslog(get_class($this).
"::insert rang=".$this->rang);
354 if (empty($this->tva_tx)) {
357 if (empty($this->localtax1_tx)) {
358 $this->localtax1_tx = 0;
360 if (empty($this->localtax2_tx)) {
361 $this->localtax2_tx = 0;
363 if (empty($this->localtax1_type)) {
364 $this->localtax1_type =
'0';
366 if (empty($this->localtax2_type)) {
367 $this->localtax2_type =
'0';
369 if (empty($this->total_localtax1)) {
370 $this->total_localtax1 = 0;
372 if (empty($this->total_localtax2)) {
373 $this->total_localtax2 = 0;
375 if (empty($this->rang)) {
378 if (empty($this->remise_percent)) {
379 $this->remise_percent = 0;
381 if (empty($this->info_bits)) {
382 $this->info_bits = 0;
384 if (empty($this->special_code)) {
385 $this->special_code = 0;
387 if (empty($this->fk_parent_line)) {
388 $this->fk_parent_line = 0;
390 if (empty($this->ref_ext)) {
395 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
396 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
400 $this->pa_ht = $result;
405 if ($this->product_type < 0) {
412 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'commandedet';
413 $sql .=
' (fk_commande, fk_parent_line, label, description, qty, ref_ext,';
414 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
415 $sql .=
' fk_product, product_type, remise_percent, subprice, price, fk_remise_except,';
416 $sql .=
' special_code, rang, fk_product_fournisseur_price, buy_price_ht,';
417 $sql .=
' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, date_start, date_end,';
419 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
421 $sql .=
" VALUES (".$this->fk_commande.
",";
422 $sql .=
" ".($this->fk_parent_line > 0 ?
"'".$this->db->escape($this->fk_parent_line).
"'" :
"null").
",";
423 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
424 $sql .=
" '".$this->db->escape($this->desc).
"',";
425 $sql .=
" '".price2num($this->qty).
"',";
426 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
427 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
428 $sql .=
" '".price2num($this->tva_tx).
"',";
429 $sql .=
" '".price2num($this->localtax1_tx).
"',";
430 $sql .=
" '".price2num($this->localtax2_tx).
"',";
431 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
432 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
433 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
434 $sql .=
" '".$this->db->escape($this->product_type).
"',";
435 $sql .=
" '".price2num($this->remise_percent).
"',";
436 $sql .=
" ".(price2num($this->subprice) !==
'' ?
price2num($this->subprice) :
"null").
",";
437 $sql .=
" ".($this->price !=
'' ?
"'".price2num($this->
price).
"'" :
"null").
",";
438 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
439 $sql .=
' '.((int) $this->special_code).
',';
440 $sql .=
' '.((int) $this->rang).
',';
441 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
442 $sql .=
' '.price2num($this->pa_ht).
',';
443 $sql .=
" ".((int) $this->info_bits).
",";
444 $sql .=
" ".price2num($this->total_ht,
'MT').
",";
445 $sql .=
" ".price2num($this->total_tva,
'MT').
",";
446 $sql .=
" ".price2num($this->total_localtax1,
'MT').
",";
447 $sql .=
" ".price2num($this->total_localtax2,
'MT').
",";
448 $sql .=
" ".price2num($this->total_ttc,
'MT').
",";
449 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
',';
450 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
',';
451 $sql .=
' '.(!$this->fk_unit ?
'NULL' : ((int) $this->fk_unit));
452 $sql .=
", ".(!empty($this->fk_multicurrency) ? ((int) $this->fk_multicurrency) :
'NULL');
453 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
454 $sql .=
", ".price2num($this->multicurrency_subprice,
'CU');
455 $sql .=
", ".price2num($this->multicurrency_total_ht,
'CT');
456 $sql .=
", ".price2num($this->multicurrency_total_tva,
'CT');
457 $sql .=
", ".price2num($this->multicurrency_total_ttc,
'CT');
460 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
461 $resql = $this->db->query($sql);
463 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'commandedet');
464 $this->rowid = $this->id;
473 if (!$error && !$notrigger) {
475 $result = $this->
call_trigger(
'LINEORDER_INSERT', $user);
487 foreach ($this->errors as $errmsg) {
488 dol_syslog(get_class($this).
"::insert ".$errmsg, LOG_ERR);
489 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
491 $this->db->rollback();
494 $this->error = $this->db->error();
495 $this->db->rollback();
511 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
514 if (empty($this->tva_tx)) {
517 if (empty($this->localtax1_tx)) {
518 $this->localtax1_tx = 0;
520 if (empty($this->localtax2_tx)) {
521 $this->localtax2_tx = 0;
523 if (empty($this->localtax1_type)) {
524 $this->localtax1_type =
'0';
526 if (empty($this->localtax2_type)) {
527 $this->localtax2_type =
'0';
529 if (empty($this->qty)) {
532 if (empty($this->total_localtax1)) {
533 $this->total_localtax1 = 0;
535 if (empty($this->total_localtax2)) {
536 $this->total_localtax2 = 0;
538 if (empty($this->marque_tx)) {
539 $this->marque_tx = 0;
541 if (empty($this->marge_tx)) {
544 if (empty($this->remise_percent)) {
545 $this->remise_percent = 0;
547 if (empty($this->remise)) {
550 if (empty($this->info_bits)) {
551 $this->info_bits = 0;
553 if (empty($this->special_code)) {
554 $this->special_code = 0;
556 if (empty($this->product_type)) {
557 $this->product_type = 0;
559 if (empty($this->fk_parent_line)) {
560 $this->fk_parent_line = 0;
562 if (empty($this->pa_ht)) {
565 if (empty($this->ref_ext)) {
570 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
571 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
575 $this->pa_ht = $result;
582 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
583 $sql .=
" description='".$this->db->escape($this->desc).
"'";
584 $sql .=
" , label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
585 $sql .=
" , vat_src_code=".(!empty($this->vat_src_code) ?
"'".$this->db->escape($this->vat_src_code).
"'" :
"''");
586 $sql .=
" , tva_tx=".price2num($this->tva_tx);
587 $sql .=
" , localtax1_tx=".price2num($this->localtax1_tx);
588 $sql .=
" , localtax2_tx=".price2num($this->localtax2_tx);
589 $sql .=
" , localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
590 $sql .=
" , localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
591 $sql .=
" , qty=".price2num($this->qty);
592 $sql .=
" , ref_ext='".$this->db->escape($this->ref_ext).
"'";
593 $sql .=
" , subprice=".price2num($this->subprice);
594 $sql .=
" , remise_percent=".price2num($this->remise_percent);
595 $sql .=
" , price=".price2num($this->
price);
596 $sql .=
" , remise=".price2num($this->remise);
597 if (empty($this->skip_update_total)) {
598 $sql .=
" , total_ht=".price2num($this->total_ht);
599 $sql .=
" , total_tva=".price2num($this->total_tva);
600 $sql .=
" , total_ttc=".price2num($this->total_ttc);
601 $sql .=
" , total_localtax1=".price2num($this->total_localtax1);
602 $sql .=
" , total_localtax2=".price2num($this->total_localtax2);
604 $sql .=
" , fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null");
605 $sql .=
" , buy_price_ht='".price2num($this->pa_ht).
"'";
606 $sql .=
" , info_bits=".((int) $this->info_bits);
607 $sql .=
" , special_code=".((int) $this->special_code);
608 $sql .=
" , date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
609 $sql .=
" , date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
610 $sql .=
" , product_type=".$this->product_type;
611 $sql .=
" , fk_parent_line=".(!empty($this->fk_parent_line) ? $this->fk_parent_line :
"null");
612 if (!empty($this->rang)) {
613 $sql .=
", rang=".((int) $this->rang);
615 $sql .=
" , fk_unit=".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
618 $sql .=
" , multicurrency_subprice=".price2num($this->multicurrency_subprice);
619 $sql .=
" , multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
620 $sql .=
" , multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
621 $sql .=
" , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
623 $sql .=
" WHERE rowid = ".((int) $this->rowid);
625 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
626 $resql = $this->db->query($sql);
629 $this->
id = $this->rowid;
636 if (!$error && !$notrigger) {
638 $result = $this->
call_trigger(
'LINEORDER_MODIFY', $user);
650 foreach ($this->errors as $errmsg) {
651 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
652 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
654 $this->db->rollback();
657 $this->error = $this->db->error();
658 $this->db->rollback();
676 if (empty($this->total_localtax1)) {
677 $this->total_localtax1 = 0;
679 if (empty($this->total_localtax2)) {
680 $this->total_localtax2 = 0;
684 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"commandedet SET";
685 $sql .=
" total_ht='".price2num($this->total_ht).
"'";
686 $sql .=
",total_tva='".price2num($this->total_tva).
"'";
687 $sql .=
",total_localtax1='".price2num($this->total_localtax1).
"'";
688 $sql .=
",total_localtax2='".price2num($this->total_localtax2).
"'";
689 $sql .=
",total_ttc='".price2num($this->total_ttc).
"'";
690 $sql .=
" WHERE rowid = ".((int) $this->rowid);
692 dol_syslog(
"OrderLine::update_total", LOG_DEBUG);
694 $resql = $this->db->query($sql);
699 $this->error = $this->db->error();
700 $this->db->rollback();