349 $sql =
'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_product, ed.fk_parent, ed.description, ed.fk_unit, ed.fk_elementdet, ed.element_type, ed.qty, ed.rang, ed.extraparams';
350 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as ed';
351 $sql .=
' WHERE ed.rowid = '.((int) $rowid);
352 $result = $this->db->query($sql);
354 $objp = $this->db->fetch_object($result);
355 $this->
id = $objp->rowid;
356 $this->fk_expedition = $objp->fk_expedition;
357 $this->entrepot_id = $objp->fk_entrepot;
358 $this->fk_entrepot = $objp->fk_entrepot;
359 $this->fk_product = $objp->fk_product;
360 $this->fk_parent = $objp->fk_parent;
362 $this->fk_unit = $objp->fk_unit;
363 $this->fk_elementdet = $objp->fk_elementdet;
364 $this->element_type = $objp->element_type;
365 $this->qty = $objp->qty;
366 $this->rang = $objp->rang;
368 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
370 $this->db->free($result);
374 $this->errors[] = $this->db->lasterror();
375 $this->error = $this->db->lasterror();
387 public function insert($user =
null, $notrigger = 0)
392 $skip_check_parameters =
false;
395 if (!empty($this->element_type)
396 && !empty($this->fk_elementdet)
397 && $this->element_type ==
'commande') {
398 $objectsrc_line =
new OrderLine($this->db);
399 $objectsrc_line->fetch($this->fk_elementdet);
400 $skip_check_parameters = $objectsrc_line->special_code == SUBTOTALS_SPECIAL_CODE;
404 $origin_id = $this->origin_id;
405 if ($origin_id > 0) {
406 if ((empty($this->fk_expedition)
407 || (empty($this->fk_elementdet) && empty($this->fk_parent))
408 || !is_numeric($this->qty))
409 && !$skip_check_parameters) {
410 $langs->load(
'errors');
411 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
415 if (empty($this->fk_expedition) || !is_numeric($this->qty)) {
416 $langs->load(
'errors');
417 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
423 if (
getDolGlobalString(
'STOCK_EXPEDITION_NO_MORE_THAN_ORDER') && !empty($this->fk_elementdet) && !empty($this->fk_expedition)) {
425 if (!empty($this->error)) {
426 $this->db->rollback();
431 if (empty($this->rang)) {
436 $ranktouse = $this->rang;
437 if ($ranktouse == -1) {
438 $rangmax = $this->
line_max($this->fk_expedition);
439 $ranktouse = $rangmax + 1;
442 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expeditiondet (";
443 $sql .=
"fk_expedition";
444 $sql .=
", fk_entrepot";
445 $sql .=
", fk_elementdet";
446 $sql .=
", fk_parent";
447 $sql .=
", fk_product";
448 $sql .=
", element_type";
451 $sql .=
", description";
453 $sql .=
") VALUES (";
454 $sql .= ((int) $this->fk_expedition);
455 $sql .=
", ".(empty($this->entrepot_id) ?
'NULL' : ((int) $this->entrepot_id));
456 $sql .=
", ".(empty($this->fk_elementdet) ?
'NULL' : ((int) $this->fk_elementdet));
457 $sql .=
", ".(empty($this->fk_parent) ?
'NULL' : ((int) $this->fk_parent));
458 $sql .=
", ".(empty($this->fk_product) ?
'NULL' : ((int) $this->fk_product));
459 $sql .=
", '".(empty($this->element_type) ?
'order' : $this->db->escape($this->element_type)).
"'";
460 $sql .=
", ".price2num($this->qty,
'MS');
461 $sql .=
", ".((int) $this->fk_unit);
463 $sql .=
", ".((int) $ranktouse);
466 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
467 $resql = $this->db->query($sql);
469 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"expeditiondet");
478 if (!$error && !$notrigger) {
480 $result = $this->call_trigger(
'LINESHIPPING_INSERT', $user);
488 foreach ($this->errors as $errmsg) {
490 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
498 $this->db->rollback();
518 $sql =
"SELECT ed.rowid as child_line_id";
520 $sql .=
", ed.fk_product";
521 $sql .=
", ed.fk_parent";
522 $sql .=
", " . $this->db->ifsql(
'eb.rowid IS NULL',
'ed.qty',
'eb.qty') .
" as qty";
523 $sql .=
", " . $this->db->ifsql(
'eb.rowid IS NULL',
'ed.fk_entrepot',
'eb.fk_warehouse') .
" as fk_warehouse";
524 $sql .=
", eb.batch, eb.eatby, eb.sellby";
526 $sql .=
" FROM " . $this->db->prefix() . $this->table_element .
" as ed";
527 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"expeditiondet_batch as eb ON eb.fk_expeditiondet = " . ((int) $line_id);
528 $sql .=
" WHERE ed.fk_parent = " . ((int) $line_id);
529 $sql .= $this->db->order(
'ed.fk_product,ed.rowid',
'ASC,ASC');
531 $resql = $this->db->query($sql);
533 while ($obj = $this->db->fetch_object($resql)) {
534 $child_line_id = (int) $obj->child_line_id;
535 if (!isset($list[$line_id])) {
536 $list[$line_id] = array();
540 $list[$line_id][] = $child_line_id;
541 } elseif ($mode == 1) {
543 $line_obj->rowid = $child_line_id;
544 $line_obj->fk_product = $obj->fk_product;
545 $line_obj->fk_parent = $obj->fk_parent;
546 $line_obj->qty = $obj->qty;
547 $line_obj->fk_warehouse = $obj->fk_warehouse;
548 $line_obj->batch = $obj->batch;
549 $line_obj->eatby = $obj->eatby;
550 $line_obj->sellby = $obj->sellby;
551 $line_obj->iskit = 0;
552 $line_obj->incdec = 0;
553 $list[$line_id][] = $line_obj;
558 $this->db->free($resql);
560 $this->error = $this->db->lasterror();
561 $this->errors[] = $this->error;
562 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
576 public function delete($user =
null, $notrigger = 0)
584 if ((
getDolGlobalInt(
'PRODUIT_SOUSPRODUITS') || $this->element_type ==
'shipping') && !($this->fk_parent > 0)) {
586 $line_id_list = array();
587 $result = $this->
findAllChild($this->
id, $line_id_list);
589 $child_line_id_list = array_reverse($line_id_list,
true);
590 foreach ($child_line_id_list as $child_line_id_arr) {
591 foreach ($child_line_id_arr as $child_line_id) {
594 $sql =
"DELETE FROM " . $this->db->prefix() .
"expeditiondet_batch";
595 $sql .=
" WHERE fk_expeditiondet = " . ((int) $child_line_id);
596 if (!$this->db->query($sql)) {
598 $this->errors[] = $this->db->lasterror() .
" - sql=$sql";
602 $sql =
"DELETE FROM " . $this->db->prefix() .
"expeditiondet";
603 $sql .=
" WHERE rowid = " . ((int) $child_line_id);
604 if (!$this->db->query($sql)) {
606 $this->errors[] = $this->db->lasterror() .
" - sql=$sql";
625 $sql =
"DELETE FROM ".$this->db->prefix().
"expeditiondet_batch";
626 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
628 if (!$this->db->query($sql)) {
629 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
634 $sql =
"DELETE FROM ".$this->db->prefix().
"expeditiondet";
635 $sql .=
" WHERE rowid = ".((int) $this->
id);
637 if (!$error && $this->db->query($sql)) {
642 $this->errors[] = $this->error;
646 if (!$error && !$notrigger) {
648 $result = $this->call_trigger(
'LINESHIPPING_DELETE', $user);
650 $this->errors[] = $this->error;
656 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
665 foreach ($this->errors as $errmsg) {
666 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
667 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
669 $this->db->rollback();
681 public function update($user =
null, $notrigger = 0)
686 dol_syslog(get_class($this).
"::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
690 if (!empty($this->
id) && empty($this->fk_elementdet)) {
691 $sql =
"SELECT fk_elementdet FROM ".MAIN_DB_PREFIX.
"expeditiondet";
692 $sql .=
" WHERE rowid = ".((int) $this->
id);
693 $resql = $this->db->query($sql);
695 $obj = $this->db->fetch_object($resql);
697 $this->fk_elementdet = $obj->fk_elementdet;
702 if (
getDolGlobalString(
'STOCK_EXPEDITION_NO_MORE_THAN_ORDER') && !empty($this->fk_elementdet)) {
703 $qty_to_check = $this->qty;
704 if (!empty($this->detail_batch)) {
705 if (is_array($this->detail_batch)) {
706 $qty_to_check = array_sum(array_column($this->detail_batch,
'qty'));
708 $qty_to_check = $this->detail_batch->qty;
712 if (!empty($this->error)) {
713 $this->db->rollback();
719 if (empty($this->qty)) {
723 $fk_unit = $this->fk_unit;
727 $expedition_batch_id = 0;
728 $origin_id = $this->origin_id;
729 if ($origin_id > 0) {
730 if (is_array($this->detail_batch)) {
731 if (count($this->detail_batch) > 1) {
732 dol_syslog(get_class($this).
'::update only possible for one batch', LOG_ERR);
733 $this->errors[] =
'ErrorBadParameters';
736 $batch = $this->detail_batch[0]->batch;
737 $batch_id = $this->detail_batch[0]->fk_origin_stock;
738 $expedition_batch_id = $this->detail_batch[0]->id;
739 if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
740 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
741 $this->errors[] =
'ErrorBadParameters';
744 $qty =
price2num($this->detail_batch[0]->qty);
746 } elseif (!empty($this->detail_batch)) {
747 $batch = $this->detail_batch->batch;
748 $batch_id = $this->detail_batch->fk_origin_stock;
749 $expedition_batch_id = $this->detail_batch->id;
750 if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
751 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
752 $this->errors[] =
'ErrorBadParameters';
755 $qty =
price2num($this->detail_batch->qty);
759 if (!isset($this->
id) || !isset($this->entrepot_id)) {
760 dol_syslog(get_class($this).
'::update missing line id and/or warehouse id', LOG_ERR);
761 $langs->load(
'errors');
762 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
769 $batch_id_str = $batch_id ??
'null';
770 dol_syslog(get_class($this).
"::update expedition batch id=$expedition_batch_id, batch_id=$batch_id_str, batch=$batch");
772 if (empty($batch_id) || empty($this->fk_product)) {
773 dol_syslog(get_class($this).
'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
774 $langs->load(
'errors');
775 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
781 $lotArray = $shipmentlinebatch->fetchAll($this->
id);
782 if (!$error && $lotArray < 0) {
783 $this->errors[] = $this->db->lasterror().
" - ExpeditionLineBatch::fetchAll";
787 foreach ($lotArray as $lot) {
788 if ($expedition_batch_id != $lot->id) {
789 $remainingQty += $lot->qty;
792 $qty += $remainingQty;
796 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
798 if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
799 $this->errors = array_merge($this->errors, $lot->errors);
802 if (!$error && !empty($expedition_batch_id)) {
804 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
805 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
806 $sql .=
" AND rowid = ".((int) $expedition_batch_id);
808 if (!$this->db->query($sql)) {
809 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
813 if (!$error && $this->detail_batch->qty > 0) {
815 if (isset($lot->id)) {
817 $shipmentLot->batch = $lot->batch;
818 $shipmentLot->eatby = $lot->eatby;
819 $shipmentLot->sellby = $lot->sellby;
820 $shipmentLot->fk_warehouse = $this->detail_batch->entrepot_id;
821 $shipmentLot->qty = $this->detail_batch->qty;
822 $shipmentLot->fk_origin_stock = (int) $batch_id;
823 if ($shipmentLot->create($this->id) < 0) {
824 $this->errors = $shipmentLot->errors;
834 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
835 $sql .=
" fk_entrepot = ".($this->entrepot_id > 0 ? ((int) $this->entrepot_id) :
'null');
836 $sql .=
" , qty = ".((float)
price2num($qty,
'MS'));
837 $sql .=
" , fk_unit = ".((int) $this->fk_unit);
838 $sql .=
" WHERE rowid = ".((int) $this->
id);
840 if (!$this->db->query($sql)) {
841 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
849 $this->errors[] = $this->error;
854 if (!$error && !$notrigger) {
856 $result = $this->call_trigger(
'LINESHIPPING_MODIFY', $user);
858 $this->errors[] = $this->error;
867 foreach ($this->errors as $errmsg) {
868 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
869 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
871 $this->db->rollback();
890 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
892 if ($orderline->fetch($fk_elementdet) <= 0) {
895 if ($orderline->product_type == 9) {
898 $qty_ordered = (float) $orderline->qty;
901 $sql =
"SELECT COALESCE(SUM(ed.qty), 0) as qty_shipped";
902 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expeditiondet as ed";
903 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"expedition as e ON e.rowid = ed.fk_expedition";
904 $sql .=
" WHERE ed.fk_elementdet = ".((int) $fk_elementdet);
905 $sql .=
" AND e.fk_statut >= 0";
906 if ($exclude_line_id > 0) {
907 $sql .=
" AND ed.rowid != ".((int) $exclude_line_id);
910 $resql = $this->db->query($sql);
914 $obj = $this->db->fetch_object($resql);
915 $qty_already_shipped = $obj ? (float) $obj->qty_shipped : 0;
917 $qty_remaining = $qty_ordered - $qty_already_shipped;
919 if ($qty > $qty_remaining) {
920 $langs->load(
'errors');
921 $product_ref = !empty($orderline->product_ref) ? $orderline->product_ref : $orderline->desc;
922 $this->error = $langs->trans(
'ErrorExpeditionQtyTooHigh', $qty, max(0, $qty_remaining), $product_ref);