337 $sql =
'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_elementdet, ed.element_type, ed.qty, ed.rang, ed.extraparams';
338 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as ed';
339 $sql .=
' WHERE ed.rowid = '.((int) $rowid);
340 $result = $this->db->query($sql);
342 $objp = $this->db->fetch_object($result);
343 $this->
id = $objp->rowid;
344 $this->fk_expedition = $objp->fk_expedition;
345 $this->entrepot_id = $objp->fk_entrepot;
346 $this->fk_elementdet = $objp->fk_elementdet;
347 $this->element_type = $objp->element_type;
348 $this->qty = $objp->qty;
349 $this->rang = $objp->rang;
351 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
353 $this->db->free($result);
357 $this->errors[] = $this->db->lasterror();
358 $this->error = $this->db->lasterror();
370 public function insert($user, $notrigger = 0)
375 $skip_check_parameters =
false;
378 if (!empty($this->element_type)
379 && !empty($this->fk_elementdet)
380 && $this->element_type ==
'commande') {
381 $objectsrc_line =
new OrderLine($this->db);
382 $objectsrc_line->fetch($this->fk_elementdet);
383 $skip_check_parameters = $objectsrc_line->special_code == SUBTOTALS_SPECIAL_CODE;
387 if ((empty($this->fk_expedition)
388 || (empty($this->fk_elementdet) && empty($this->fk_parent))
389 || !is_numeric($this->qty))
390 && !$skip_check_parameters) {
391 $langs->load(
'errors');
392 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
398 if (empty($this->rang)) {
403 $ranktouse = $this->rang;
404 if ($ranktouse == -1) {
405 $rangmax = $this->
line_max($this->fk_expedition);
406 $ranktouse = $rangmax + 1;
409 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expeditiondet (";
410 $sql .=
"fk_expedition";
411 $sql .=
", fk_entrepot";
412 $sql .=
", fk_elementdet";
413 $sql .=
", fk_parent";
414 $sql .=
", fk_product";
415 $sql .=
", element_type";
418 $sql .=
") VALUES (";
419 $sql .= $this->fk_expedition;
420 $sql .=
", ".(empty($this->entrepot_id) ?
'NULL' : $this->entrepot_id);
421 $sql .=
", ".(empty($this->fk_elementdet) ?
'NULL' : $this->fk_elementdet);
422 $sql .=
", ".(empty($this->fk_parent) ?
'NULL' : $this->fk_parent);
423 $sql .=
", ".(empty($this->fk_product) ?
'NULL' : $this->fk_product);
424 $sql .=
", '".(empty($this->element_type) ?
'order' : $this->db->escape($this->element_type)).
"'";
425 $sql .=
", ".price2num($this->qty,
'MS');
426 $sql .=
", ".((int) $ranktouse);
429 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
430 $resql = $this->db->query($sql);
432 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"expeditiondet");
441 if (!$error && !$notrigger) {
443 $result = $this->
call_trigger(
'LINESHIPPING_INSERT', $user);
451 foreach ($this->errors as $errmsg) {
453 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
461 $this->db->rollback();
481 $sql =
"SELECT ed.rowid as child_line_id";
483 $sql .=
", ed.fk_product";
484 $sql .=
", ed.fk_parent";
485 $sql .=
", " . $this->db->ifsql(
'eb.rowid IS NULL',
'ed.qty',
'eb.qty') .
" as qty";
486 $sql .=
", " . $this->db->ifsql(
'eb.rowid IS NULL',
'ed.fk_entrepot',
'eb.fk_warehouse') .
" as fk_warehouse";
487 $sql .=
", eb.batch, eb.eatby, eb.sellby";
489 $sql .=
" FROM " . $this->db->prefix() . $this->table_element .
" as ed";
490 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"expeditiondet_batch as eb ON eb.fk_expeditiondet = " . ((int) $line_id);
491 $sql .=
" WHERE ed.fk_parent = " . ((int) $line_id);
492 $sql .= $this->db->order(
'ed.fk_product,ed.rowid',
'ASC,ASC');
494 $resql = $this->db->query($sql);
496 while ($obj = $this->db->fetch_object($resql)) {
497 $child_line_id = (int) $obj->child_line_id;
498 if (!isset($list[$line_id])) {
499 $list[$line_id] = array();
503 $list[$line_id][] = $child_line_id;
504 } elseif ($mode == 1) {
506 $line_obj->rowid = $child_line_id;
507 $line_obj->fk_product = $obj->fk_product;
508 $line_obj->fk_parent = $obj->fk_parent;
509 $line_obj->qty = $obj->qty;
510 $line_obj->fk_warehouse = $obj->fk_warehouse;
511 $line_obj->batch = $obj->batch;
512 $line_obj->eatby = $obj->eatby;
513 $line_obj->sellby = $obj->sellby;
514 $line_obj->iskit = 0;
515 $line_obj->incdec = 0;
516 $list[$line_id][] = $line_obj;
521 $this->db->free($resql);
523 $this->error = $this->db->lasterror();
524 $this->errors[] = $this->error;
525 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
539 public function delete($user =
null, $notrigger = 0)
546 if (
getDolGlobalInt(
'PRODUIT_SOUSPRODUITS') && !($this->fk_parent > 0)) {
548 $line_id_list = array();
549 $result = $this->
findAllChild($this->
id, $line_id_list);
551 $child_line_id_list = array_reverse($line_id_list,
true);
552 foreach ($child_line_id_list as $child_line_id_arr) {
553 foreach ($child_line_id_arr as $child_line_id) {
555 if (isModEnabled(
'productbatch')) {
556 $sql =
"DELETE FROM " . $this->db->prefix() .
"expeditiondet_batch";
557 $sql .=
" WHERE fk_expeditiondet = " . ((int) $child_line_id);
558 if (!$this->db->query($sql)) {
560 $this->errors[] = $this->db->lasterror() .
" - sql=$sql";
564 $sql =
"DELETE FROM " . $this->db->prefix() .
"expeditiondet";
565 $sql .=
" WHERE rowid = " . ((int) $child_line_id);
566 if (!$this->db->query($sql)) {
568 $this->errors[] = $this->db->lasterror() .
" - sql=$sql";
586 if (isModEnabled(
'productbatch')) {
587 $sql =
"DELETE FROM ".$this->db->prefix().
"expeditiondet_batch";
588 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
590 if (!$this->db->query($sql)) {
591 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
596 $sql =
"DELETE FROM ".$this->db->prefix().
"expeditiondet";
597 $sql .=
" WHERE rowid = ".((int) $this->
id);
599 if (!$error && $this->db->query($sql)) {
604 $this->errors[] = $this->error;
608 if (!$error && !$notrigger) {
610 $result = $this->
call_trigger(
'LINESHIPPING_DELETE', $user);
612 $this->errors[] = $this->error;
618 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
627 foreach ($this->errors as $errmsg) {
628 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
629 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
631 $this->db->rollback();
643 public function update($user =
null, $notrigger = 0)
648 dol_syslog(get_class($this).
"::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
653 if (empty($this->qty)) {
660 $expedition_batch_id = 0;
661 if (is_array($this->detail_batch)) {
662 if (count($this->detail_batch) > 1) {
663 dol_syslog(get_class($this).
'::update only possible for one batch', LOG_ERR);
664 $this->errors[] =
'ErrorBadParameters';
667 $batch = $this->detail_batch[0]->batch;
668 $batch_id = $this->detail_batch[0]->fk_origin_stock;
669 $expedition_batch_id = $this->detail_batch[0]->id;
670 if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
671 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
672 $this->errors[] =
'ErrorBadParameters';
675 $qty =
price2num($this->detail_batch[0]->qty);
677 } elseif (!empty($this->detail_batch)) {
678 $batch = $this->detail_batch->batch;
679 $batch_id = $this->detail_batch->fk_origin_stock;
680 $expedition_batch_id = $this->detail_batch->id;
681 if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
682 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
683 $this->errors[] =
'ErrorBadParameters';
686 $qty =
price2num($this->detail_batch->qty);
690 if (!isset($this->
id) || !isset($this->entrepot_id)) {
691 dol_syslog(get_class($this).
'::update missing line id and/or warehouse id', LOG_ERR);
692 $langs->load(
'errors');
693 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
700 if (!empty($batch) && isModEnabled(
'productbatch')) {
701 $batch_id_str = $batch_id ??
'null';
702 dol_syslog(get_class($this).
"::update expedition batch id=$expedition_batch_id, batch_id=$batch_id_str, batch=$batch");
704 if (empty($batch_id) || empty($this->fk_product)) {
705 dol_syslog(get_class($this).
'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
706 $langs->load(
'errors');
707 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
713 $lotArray = $shipmentlinebatch->fetchAll($this->
id);
714 if (!$error && $lotArray < 0) {
715 $this->errors[] = $this->db->lasterror().
" - ExpeditionLineBatch::fetchAll";
719 foreach ($lotArray as $lot) {
720 if ($expedition_batch_id != $lot->id) {
721 $remainingQty += $lot->qty;
724 $qty += $remainingQty;
729 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
731 if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
732 $this->errors = array_merge($this->errors, $lot->errors);
735 if (!$error && !empty($expedition_batch_id)) {
737 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
738 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
739 $sql .=
" AND rowid = ".((int) $expedition_batch_id);
741 if (!$this->db->query($sql)) {
742 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
746 if (!$error && $this->detail_batch->qty > 0) {
748 if (isset($lot->id)) {
750 $shipmentLot->batch = $lot->batch;
751 $shipmentLot->eatby = $lot->eatby;
752 $shipmentLot->sellby = $lot->sellby;
753 $shipmentLot->fk_warehouse = $this->detail_batch->entrepot_id;
754 $shipmentLot->qty = $this->detail_batch->qty;
755 $shipmentLot->fk_origin_stock = (int) $batch_id;
756 if ($shipmentLot->create($this->id) < 0) {
757 $this->errors = $shipmentLot->errors;
766 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
767 $sql .=
" fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id :
'null');
768 $sql .=
" , qty = ".((float)
price2num($qty,
'MS'));
769 $sql .=
" WHERE rowid = ".((int) $this->
id);
771 if (!$this->db->query($sql)) {
772 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
780 $this->errors[] = $this->error;
785 if (!$error && !$notrigger) {
787 $result = $this->
call_trigger(
'LINESHIPPING_MODIFY', $user);
789 $this->errors[] = $this->error;
798 foreach ($this->errors as $errmsg) {
799 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
800 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
802 $this->db->rollback();