319 $sql =
'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_elementdet, ed.element_type, ed.qty, ed.rang';
320 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as ed';
321 $sql .=
' WHERE ed.rowid = '.((int) $rowid);
322 $result = $this->db->query($sql);
324 $objp = $this->db->fetch_object($result);
325 $this->
id = $objp->rowid;
326 $this->fk_expedition = $objp->fk_expedition;
327 $this->entrepot_id = $objp->fk_entrepot;
328 $this->fk_elementdet = $objp->fk_elementdet;
329 $this->element_type = $objp->element_type;
330 $this->qty = $objp->qty;
331 $this->rang = $objp->rang;
333 $this->db->free($result);
337 $this->errors[] = $this->db->lasterror();
338 $this->error = $this->db->lasterror();
350 public function insert($user, $notrigger = 0)
355 if (empty($this->fk_expedition) || empty($this->fk_elementdet) || !is_numeric($this->qty)) {
356 $this->error =
'ErrorMandatoryParametersNotProvided';
362 if (empty($this->rang)) {
367 $ranktouse = $this->rang;
368 if ($ranktouse == -1) {
369 $rangmax = $this->
line_max($this->fk_expedition);
370 $ranktouse = $rangmax + 1;
373 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"expeditiondet (";
374 $sql .=
"fk_expedition";
375 $sql .=
", fk_entrepot";
376 $sql .=
", fk_elementdet";
377 $sql .=
", element_type";
380 $sql .=
") VALUES (";
381 $sql .= $this->fk_expedition;
382 $sql .=
", ".(empty($this->entrepot_id) ?
'NULL' : $this->entrepot_id);
383 $sql .=
", ".((int) $this->fk_elementdet);
384 $sql .=
", '".(empty($this->element_type) ?
'order' : $this->db->escape($this->element_type)).
"'";
385 $sql .=
", ".price2num($this->qty,
'MS');
386 $sql .=
", ".((int) $ranktouse);
389 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
390 $resql = $this->db->query($sql);
392 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"expeditiondet");
401 if (!$error && !$notrigger) {
403 $result = $this->
call_trigger(
'LINESHIPPING_INSERT', $user);
411 foreach ($this->errors as $errmsg) {
412 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
413 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
421 $this->db->rollback();
436 public function delete($user =
null, $notrigger = 0)
443 if (isModEnabled(
'productbatch')) {
444 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
445 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
447 if (!$this->db->query($sql)) {
448 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
453 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet";
454 $sql .=
" WHERE rowid = ".((int) $this->
id);
456 if (!$error && $this->db->query($sql)) {
461 $this->errors[] = $this->error;
465 if (!$error && !$notrigger) {
467 $result = $this->
call_trigger(
'LINESHIPPING_DELETE', $user);
469 $this->errors[] = $this->error;
475 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
483 foreach ($this->errors as $errmsg) {
484 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
485 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
487 $this->db->rollback();
499 public function update($user =
null, $notrigger = 0)
503 dol_syslog(get_class($this).
"::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
508 if (empty($this->qty)) {
515 $expedition_batch_id =
null;
516 if (is_array($this->detail_batch)) {
517 if (count($this->detail_batch) > 1) {
518 dol_syslog(get_class($this).
'::update only possible for one batch', LOG_ERR);
519 $this->errors[] =
'ErrorBadParameters';
522 $batch = $this->detail_batch[0]->batch;
523 $batch_id = $this->detail_batch[0]->fk_origin_stock;
524 $expedition_batch_id = $this->detail_batch[0]->id;
525 if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
526 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
527 $this->errors[] =
'ErrorBadParameters';
530 $qty =
price2num($this->detail_batch[0]->qty);
532 } elseif (!empty($this->detail_batch)) {
533 $batch = $this->detail_batch->batch;
534 $batch_id = $this->detail_batch->fk_origin_stock;
535 $expedition_batch_id = $this->detail_batch->id;
536 if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
537 dol_syslog(get_class($this).
'::update only possible for batch of same warehouse', LOG_ERR);
538 $this->errors[] =
'ErrorBadParameters';
541 $qty =
price2num($this->detail_batch->qty);
545 if (!isset($this->
id) || !isset($this->entrepot_id)) {
546 dol_syslog(get_class($this).
'::update missing line id and/or warehouse id', LOG_ERR);
547 $this->errors[] =
'ErrorMandatoryParametersNotProvided';
554 if (!empty($batch) && isModEnabled(
'productbatch')) {
555 $batch_id_str = $batch_id ??
'null';
556 dol_syslog(get_class($this).
"::update expedition batch id=$expedition_batch_id, batch_id=$batch_id_str, batch=$batch");
558 if (empty($batch_id) || empty($this->fk_product)) {
559 dol_syslog(get_class($this).
'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
560 $this->errors[] =
'ErrorMandatoryParametersNotProvided';
566 $lotArray = $shipmentlinebatch->fetchAll($this->
id);
567 if (!$error && $lotArray < 0) {
568 $this->errors[] = $this->db->lasterror().
" - ExpeditionLineBatch::fetchAll";
572 foreach ($lotArray as $lot) {
573 if ($expedition_batch_id != $lot->id) {
574 $remainingQty += $lot->qty;
577 $qty += $remainingQty;
582 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/productlot.class.php';
584 if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
585 $this->errors = array_merge($this->errors, $lot->errors);
588 if (!$error && !empty($expedition_batch_id)) {
590 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"expeditiondet_batch";
591 $sql .=
" WHERE fk_expeditiondet = ".((int) $this->
id);
592 $sql .=
" AND rowid = ".((int) $expedition_batch_id);
594 if (!$this->db->query($sql)) {
595 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
599 if (!$error && $this->detail_batch->qty > 0) {
601 if (isset($lot->id)) {
603 $shipmentLot->batch = $lot->batch;
604 $shipmentLot->eatby = $lot->eatby;
605 $shipmentLot->sellby = $lot->sellby;
606 $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id;
607 $shipmentLot->qty = $this->detail_batch->qty;
608 $shipmentLot->fk_origin_stock = $batch_id;
609 if ($shipmentLot->create($this->id) < 0) {
610 $this->errors = $shipmentLot->errors;
619 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element.
" SET";
620 $sql .=
" fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id :
'null');
621 $sql .=
" , qty = ".((float)
price2num($qty,
'MS'));
622 $sql .=
" WHERE rowid = ".((int) $this->
id);
624 if (!$this->db->query($sql)) {
625 $this->errors[] = $this->db->lasterror().
" - sql=$sql";
633 $this->errors[] = $this->error;
638 if (!$error && !$notrigger) {
640 $result = $this->
call_trigger(
'LINESHIPPING_MODIFY', $user);
642 $this->errors[] = $this->error;
651 foreach ($this->errors as $errmsg) {
652 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
653 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
655 $this->db->rollback();