338 $sql =
'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.description, ed.fk_unit, ed.fk_elementdet, ed.element_type, ed.qty, ed.rang, ed.extraparams';
339 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as ed';
340 $sql .=
' WHERE ed.rowid = '.((int) $rowid);
341 $result = $this->db->query($sql);
343 $objp = $this->db->fetch_object($result);
344 $this->
id = $objp->rowid;
345 $this->fk_expedition = $objp->fk_expedition;
346 $this->entrepot_id = $objp->fk_entrepot;
348 $this->fk_unit = $objp->fk_unit;
349 $this->fk_elementdet = $objp->fk_elementdet;
350 $this->element_type = $objp->element_type;
351 $this->qty = $objp->qty;
352 $this->rang = $objp->rang;
354 $this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
356 $this->db->free($result);
360 $this->errors[] = $this->db->lasterror();
361 $this->error = $this->db->lasterror();
373 public function insert($user =
null, $notrigger = 0)
378 $skip_check_parameters =
false;
381 if (!empty($this->element_type)
382 && !empty($this->fk_elementdet)
383 && $this->element_type ==
'commande') {
384 $objectsrc_line =
new OrderLine($this->db);
385 $objectsrc_line->fetch($this->fk_elementdet);
386 $skip_check_parameters = $objectsrc_line->special_code == SUBTOTALS_SPECIAL_CODE;
390 $origin_id = $this->origin_id;
391 if ($origin_id > 0) {
392 if ((empty($this->fk_expedition)
393 || (empty($this->fk_elementdet) && empty($this->fk_parent))
394 || !is_numeric($this->qty))
395 && !$skip_check_parameters) {
396 $langs->load(
'errors');
397 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
401 if (empty($this->fk_expedition) || !is_numeric($this->qty)) {
402 $langs->load(
'errors');
403 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
409 if (empty($this->rang)) {
414 $ranktouse = $this->rang;
415 if ($ranktouse == -1) {
416 $rangmax = $this->
line_max($this->fk_expedition);
417 $ranktouse = $rangmax + 1;
420 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expeditiondet (";
421 $sql .=
"fk_expedition";
422 $sql .=
", fk_entrepot";
423 $sql .=
", fk_elementdet";
424 $sql .=
", fk_parent";
425 $sql .=
", fk_product";
426 $sql .=
", element_type";
429 $sql .=
", description";
431 $sql .=
") VALUES (";
432 $sql .= $this->fk_expedition;
433 $sql .=
", ".(empty($this->entrepot_id) ?
'NULL' : $this->entrepot_id);
434 $sql .=
", ".(empty($this->fk_elementdet) ?
'NULL' : $this->fk_elementdet);
435 $sql .=
", ".(empty($this->fk_parent) ?
'NULL' : $this->fk_parent);
436 $sql .=
", ".(empty($this->fk_product) ?
'NULL' : $this->fk_product);
437 $sql .=
", '".(empty($this->element_type) ?
'order' : $this->db->escape($this->element_type)).
"'";
438 $sql .=
", ".price2num($this->qty,
'MS');
439 $sql .=
", ".((int) $this->fk_unit);
441 $sql .=
", ".((int) $ranktouse);
444 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
445 $resql = $this->db->query($sql);
447 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"expeditiondet");
456 if (!$error && !$notrigger) {
458 $result = $this->call_trigger(
'LINESHIPPING_INSERT', $user);
466 foreach ($this->errors as $errmsg) {
468 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
476 $this->db->rollback();
496 $sql =
"SELECT ed.rowid as child_line_id";
498 $sql .=
", ed.fk_product";
499 $sql .=
", ed.fk_parent";
500 $sql .=
", " . $this->db->ifsql(
'eb.rowid IS NULL',
'ed.qty',
'eb.qty') .
" as qty";
501 $sql .=
", " . $this->db->ifsql(
'eb.rowid IS NULL',
'ed.fk_entrepot',
'eb.fk_warehouse') .
" as fk_warehouse";
502 $sql .=
", eb.batch, eb.eatby, eb.sellby";
504 $sql .=
" FROM " . $this->db->prefix() . $this->table_element .
" as ed";
505 $sql .=
" LEFT JOIN " . $this->db->prefix() .
"expeditiondet_batch as eb ON eb.fk_expeditiondet = " . ((int) $line_id);
506 $sql .=
" WHERE ed.fk_parent = " . ((int) $line_id);
507 $sql .= $this->db->order(
'ed.fk_product,ed.rowid',
'ASC,ASC');
509 $resql = $this->db->query($sql);
511 while ($obj = $this->db->fetch_object($resql)) {
512 $child_line_id = (int) $obj->child_line_id;
513 if (!isset($list[$line_id])) {
514 $list[$line_id] = array();
518 $list[$line_id][] = $child_line_id;
519 } elseif ($mode == 1) {
521 $line_obj->rowid = $child_line_id;
522 $line_obj->fk_product = $obj->fk_product;
523 $line_obj->fk_parent = $obj->fk_parent;
524 $line_obj->qty = $obj->qty;
525 $line_obj->fk_warehouse = $obj->fk_warehouse;
526 $line_obj->batch = $obj->batch;
527 $line_obj->eatby = $obj->eatby;
528 $line_obj->sellby = $obj->sellby;
529 $line_obj->iskit = 0;
530 $line_obj->incdec = 0;
531 $list[$line_id][] = $line_obj;
536 $this->db->free($resql);
538 $this->error = $this->db->lasterror();
539 $this->errors[] = $this->error;
540 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
554 public function delete($user =
null, $notrigger = 0)
561 if (
getDolGlobalInt(
'PRODUIT_SOUSPRODUITS') && !($this->fk_parent > 0)) {
563 $line_id_list = array();
564 $result = $this->
findAllChild($this->
id, $line_id_list);
566 $child_line_id_list = array_reverse($line_id_list,
true);
567 foreach ($child_line_id_list as $child_line_id_arr) {
568 foreach ($child_line_id_arr as $child_line_id) {
571 $sql =
"DELETE FROM " . $this->db->prefix() .
"expeditiondet_batch";
572 $sql .=
" WHERE fk_expeditiondet = " . ((int) $child_line_id);
573 if (!$this->db->query($sql)) {
575 $this->errors[] = $this->db->lasterror() .
" - sql=$sql";
579 $sql =
"DELETE FROM " . $this->db->prefix() .
"expeditiondet";
580 $sql .=
" WHERE rowid = " . ((int) $child_line_id);
581 if (!$this->db->query($sql)) {
583 $this->errors[] = $this->db->lasterror() .
" - sql=$sql";
602 $sql =
"DELETE FROM ".$this->db->prefix().
"expeditiondet_batch";
603 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
605 if (!$this->db->query($sql)) {
606 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
611 $sql =
"DELETE FROM ".$this->db->prefix().
"expeditiondet";
612 $sql .=
" WHERE rowid = ".((int) $this->
id);
614 if (!$error && $this->db->query($sql)) {
619 $this->errors[] = $this->error;
623 if (!$error && !$notrigger) {
625 $result = $this->call_trigger(
'LINESHIPPING_DELETE', $user);
627 $this->errors[] = $this->error;
633 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
642 foreach ($this->errors as $errmsg) {
643 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
644 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
646 $this->db->rollback();
658 public function update($user =
null, $notrigger = 0)
663 dol_syslog(get_class($this).
"::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
668 if (empty($this->qty)) {
672 $fk_unit = $this->fk_unit;
676 $expedition_batch_id = 0;
677 $origin_id = $this->origin_id;
678 if ($origin_id > 0) {
679 if (is_array($this->detail_batch)) {
680 if (count($this->detail_batch) > 1) {
681 dol_syslog(get_class($this).
'::update only possible for one batch', LOG_ERR);
682 $this->errors[] =
'ErrorBadParameters';
685 $batch = $this->detail_batch[0]->batch;
686 $batch_id = $this->detail_batch[0]->fk_origin_stock;
687 $expedition_batch_id = $this->detail_batch[0]->id;
688 if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
689 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
690 $this->errors[] =
'ErrorBadParameters';
693 $qty =
price2num($this->detail_batch[0]->qty);
695 } elseif (!empty($this->detail_batch)) {
696 $batch = $this->detail_batch->batch;
697 $batch_id = $this->detail_batch->fk_origin_stock;
698 $expedition_batch_id = $this->detail_batch->id;
699 if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
700 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
701 $this->errors[] =
'ErrorBadParameters';
704 $qty =
price2num($this->detail_batch->qty);
708 if (!isset($this->
id) || !isset($this->entrepot_id)) {
709 dol_syslog(get_class($this).
'::update missing line id and/or warehouse id', LOG_ERR);
710 $langs->load(
'errors');
711 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
718 $batch_id_str = $batch_id ??
'null';
719 dol_syslog(get_class($this).
"::update expedition batch id=$expedition_batch_id, batch_id=$batch_id_str, batch=$batch");
721 if (empty($batch_id) || empty($this->fk_product)) {
722 dol_syslog(get_class($this).
'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
723 $langs->load(
'errors');
724 $this->errors[] = $langs->trans(
'ErrorMandatoryParametersNotProvided');
730 $lotArray = $shipmentlinebatch->fetchAll($this->
id);
731 if (!$error && $lotArray < 0) {
732 $this->errors[] = $this->db->lasterror().
" - ExpeditionLineBatch::fetchAll";
736 foreach ($lotArray as $lot) {
737 if ($expedition_batch_id != $lot->id) {
738 $remainingQty += $lot->qty;
741 $qty += $remainingQty;
745 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
747 if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
748 $this->errors = array_merge($this->errors, $lot->errors);
751 if (!$error && !empty($expedition_batch_id)) {
753 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
754 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
755 $sql .=
" AND rowid = ".((int) $expedition_batch_id);
757 if (!$this->db->query($sql)) {
758 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
762 if (!$error && $this->detail_batch->qty > 0) {
764 if (isset($lot->id)) {
766 $shipmentLot->batch = $lot->batch;
767 $shipmentLot->eatby = $lot->eatby;
768 $shipmentLot->sellby = $lot->sellby;
769 $shipmentLot->fk_warehouse = $this->detail_batch->entrepot_id;
770 $shipmentLot->qty = $this->detail_batch->qty;
771 $shipmentLot->fk_origin_stock = (int) $batch_id;
772 if ($shipmentLot->create($this->id) < 0) {
773 $this->errors = $shipmentLot->errors;
783 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
784 $sql .=
" fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id :
'null');
785 $sql .=
" , qty = ".((float)
price2num($qty,
'MS'));
786 $sql .=
" , fk_unit = ".((int) $this->fk_unit);
787 $sql .=
" WHERE rowid = ".((int) $this->
id);
789 if (!$this->db->query($sql)) {
790 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
798 $this->errors[] = $this->error;
803 if (!$error && !$notrigger) {
805 $result = $this->call_trigger(
'LINESHIPPING_MODIFY', $user);
807 $this->errors[] = $this->error;
816 foreach ($this->errors as $errmsg) {
817 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
818 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
820 $this->db->rollback();