From 907ae36cdffde5b34f2651252bba69bf7e72907d Mon Sep 17 00:00:00 2001 From: fappels Date: Sun, 15 Oct 2017 17:58:22 +0200 Subject: [PATCH 01/17] Add insert, update and delete methods to line class Cleanup properties Add insert, line and delete methods --- htdocs/expedition/class/expedition.class.php | 356 ++++++++++++++++++- 1 file changed, 346 insertions(+), 10 deletions(-) diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index cf1c7afad9c..fee155a59de 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -2235,13 +2235,30 @@ class Expedition extends CommonObject */ class ExpeditionLigne extends CommonObjectLine { - var $db; + public $element='expeditiondet'; + public $table_element='expeditiondet'; + + public $fk_origin_line; + + /** + * Id of shipment + * @var int + */ + public $fk_expedition; + + var $db; // From llx_expeditiondet var $qty; var $qty_shipped; var $fk_product; var $detail_batch; + /** + * Id of warehouse + * @var int + */ + public $entrepot_id; + // From llx_commandedet or llx_propaldet var $qty_asked; @@ -2258,10 +2275,7 @@ class ExpeditionLigne extends CommonObjectLine var $total_localtax1; // Total Local tax 1 var $total_localtax2; // Total Local tax 2 - public $element='expeditiondet'; - public $table_element='expeditiondet'; - - public $fk_origin_line; + // Deprecated /** @@ -2280,15 +2294,337 @@ class ExpeditionLigne extends CommonObjectLine */ var $libelle; - /** - * Constructor - * - * @param DoliDB $db Database handler - */ + /** + * Constructor + * + * @param DoliDB $db Database handler + */ function __construct($db) { $this->db=$db; } + /** + * Insert line into database + * + * @param User $user User that modify + * @param int $notrigger 1 = disable triggers + * @return int <0 if KO, line id >0 if OK + */ + function insert($user=null, $notrigger=0) + { + global $langs, $conf; + + $error=0; + + // Check parameters + if (empty($this->fk_expedition) || empty($this->fk_origin_line) || empty($this->qty)) + { + $this->errors[] = 'ErrorMandatoryParametersNotProvided'; + return -1; + } + // Clean parameters + if (empty($this->entrepot_id)) $this->entrepot_id='null'; + + $this->db->begin(); + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet ("; + $sql.= "fk_expedition"; + $sql.= ", fk_entrepot"; + $sql.= ", fk_origin_line"; + $sql.= ", qty"; + $sql.= ") VALUES ("; + $sql.= $this->id; + $sql.= ", ".$this->entrepot_id; + $sql.= ", ".$this->origin_line_id; + $sql.= ", ".$this->qty; + $sql.= ")"; + + dol_syslog(get_class($this)."::insert", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet"); + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used + { + $this->id=$this->rowid; + $result=$this->insertExtraFields(); + if ($result < 0) + { + $error++; + } + } + + if (! $error && ! $notrigger) + { + // Call trigger + $result=$this->call_trigger('LINESHIPMENT_INSERT',$user); + if ($result < 0) $error++; + // End call triggers + } + + if (!$error) { + $this->db->commit(); + return $this->id; + } + + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; + } + else + { + $error++; + } + + if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options)>0) // For avoid conflicts if trigger used + { + $expeditionline = new ExpeditionLigne($this->db); + $expeditionline->array_options=$array_options; + $expeditionline->id= $this->db->last_insert_id(MAIN_DB_PREFIX.$expeditionline->table_element); + $result=$expeditionline->insertExtraFields(); + if ($result < 0) + { + $this->error[]=$expeditionline->error; + $error++; + } + } + + if (! $error) return $line_id; + else return -1; + + // //////////////////// + + dol_syslog(get_class($this)."::insert", LOG_DEBUG); + $resql=$this->db->query($sql); + if ($resql) + { + $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'commandedet'); + + + } + else + { + $this->error=$this->db->error(); + $this->db->rollback(); + return -2; + } + } + + /** + * Delete shipment line. + * + * @return int >0 if OK, <0 if KO + */ + function delete() + { + global $conf; + + $this->db->begin(); + + // delete batch expedition line + if ($conf->productbatch->enabled) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch"; + $sql.= " WHERE fk_expeditiondet = ".$this->id; + + if (!$this->db->query($sql)) + { + $this->errors[]=$this->db->lasterror()." - sql=$sql"; + $this->db->rollback(); + return -2; + } + } + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet"; + $sql.= " WHERE rowid = ".$this->id; + + if ( $this->db->query($sql)) + { + // Remove extrafields + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used + { + $result=$this->deleteExtraFields(); + if ($result < 0) + { + $this->errors[]=$this->error; + $this->db->rollback(); + return -4; + } + else + { + $this->db->commit(); + return 1; + } + } + else + { + $this->db->commit(); + return 1; + } + } + else + { + $this->errors[]=$this->db->lasterror()." - sql=$sql"; + $this->db->rollback(); + return -3; + } + } + + /** + * Update a line in database + * + * @return int < 0 if KO, > 0 if OK + */ + function update() + { + global $conf; + + dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty"); + + // Add a protection to refuse deleting if shipment is not in draft status + if (! isset($this->id) || ! isset($this->entrepot_id)) + { + dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR); + $this->errors[]='ErrorBadParameters'; + return -1; + } + + $this->db->begin(); + + // Clean parameters + if (empty($this->qty)) $this->qty=0; + $qty=price2num($this->qty); + $remainingQty = 0; + $batch = null; + $batch_id = null; + if (is_array($this->detail_batch)) + { + if (count($this->detail_batch) > 1) + { + dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR); + $this->errors[]='ErrorBadParameters'; + return -7; + } + else + { + $batch = $this->detail_batch[0]->batch; + $batch_id = $this->detail_batch[0]->fk_origin_stock; + } + } + else + { + $batch = $this->detail_batch->batch; + $batch_id = $this->detail_batch->fk_origin_stock; + } + + // update lot + + if (!empty($batch) && $conf->productbatch->enabled) + { + if (empty($batch_id) || empty($this->fk_product)) { + dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR); + $this->errors[]='ErrorBadParameters'; + return -8; + } + + // fetch remaining lot qty + require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionbatch.class.php'; + if (($lotArray = ExpeditionLineBatch::fetchAll($this->db, $this->id)) < 0) + { + $this->errors[]=$this->db->lasterror()." - ExpeditionLineBatch::fetchAll"; + $this->db->rollback(); + return -4; + } + foreach ($lotArray as $lot) + { + if ($batch != $lot->batch) + { + $remainingQty += $lot->dluo_qty; + } + } + + //fetch lot details + + // fetch from product_lot + require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php'; + $lot = new Productlot($this->db); + if ($lot->fetch(0,$this->fk_product,$batch) < 0) + { + $this->errors[] = $lot->errors; + return -3; + } + + // delete lot expedition line + $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch"; + $sql.= " WHERE fk_expeditiondet = ".$this->id; + $sql.= " AND batch = '".$this->db->escape($batch)."'"; + + if (!$this->db->query($sql)) + { + $this->errors[]=$this->db->lasterror()." - sql=$sql"; + $this->db->rollback(); + return -2; + } + + if ($qty > 0) { + if (isset($lot->id)) + { + $shipmentLot = new ExpeditionLineBatch($this->db); + $shipmentLot->batch = $lot->batch; + $shipmentLot->eatby = $lot->eatby; + $shipmentLot->sellby = $lot->sellby; + $shipmentLot->entrepot_id = $this->entrepot_id; + $shipmentLot->dluo_qty = $qty; + $shipmentLot->fk_origin_stock = $batch_id; + if ($shipmentLot->create($this->id) < 0) + { + $this->errors[]=$shipmentLot->errors; + $this->db->rollback(); + return -6; + } + } + } + } + + // update line + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql.= " fk_entrepot = ".$this->entrepot_id; + $sql.= " , qty = ".($qty + $remainingQty); + $sql.= " WHERE rowid = ".$this->id; + + if (!$this->db->query($sql)) + { + $this->errors[]=$this->db->lasterror()." - sql=$sql"; + $this->db->rollback(); + return -5; + } + + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used + { + $this->id=$this->rowid; + $result=$this->insertExtraFields(); + if ($result < 0) + { + $this->errors[]=$this->error; + $this->db->rollback(); + return -4; + } + else + { + $this->db->commit(); + return 1; + } + } + else + { + $this->db->commit(); + return 1; + } + } } From 2ea687a60b380e3f58fdb3f1865382d693f1f799 Mon Sep 17 00:00:00 2001 From: fappels Date: Wed, 18 Oct 2017 17:01:17 +0200 Subject: [PATCH 02/17] Add Line triggers, Improve line error handling, ... Add triggers, Improve line error handling, use line->insert in create_line --- htdocs/expedition/class/expedition.class.php | 426 +++++++++---------- 1 file changed, 202 insertions(+), 224 deletions(-) diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index fee155a59de..05f6c9e57d3 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -7,7 +7,7 @@ * Copyright (C) 2013 Florian Henry * Copyright (C) 2014 Cedric GROSS * Copyright (C) 2014-2015 Marcos García - * Copyright (C) 2014-2015 Francis Appels + * Copyright (C) 2014-2017 Francis Appels * Copyright (C) 2015 Claudio Aschieri * Copyright (C) 2016 Ferran Marcet * @@ -373,48 +373,18 @@ class Expedition extends CommonObject */ function create_line($entrepot_id, $origin_line_id, $qty,$array_options=0) { - global $conf; - $error = 0; - $line_id = 0; + $expeditionline = new ExpeditionLigne($this->db); + $expeditionline->fk_expedition = $this->id; + $expeditionline->entrepot_id = $entrepot_id; + $expeditionline->fk_origin_line = $origin_line_id; + $expeditionline->qty = $qty; + $expeditionline->$array_options = $array_options; - $sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet ("; - $sql.= "fk_expedition"; - $sql.= ", fk_entrepot"; - $sql.= ", fk_origin_line"; - $sql.= ", qty"; - $sql.= ") VALUES ("; - $sql.= $this->id; - $sql.= ", ".($entrepot_id?$entrepot_id:'null'); - $sql.= ", ".$origin_line_id; - $sql.= ", ".$qty; - $sql.= ")"; - - dol_syslog(get_class($this)."::create_line", LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) + if (($lineId = $expeditionline->insert()) < 0) { - $line_id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet"); + $this->error[]=$expeditionline->error; } - else - { - $error++; - } - - if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options)>0) // For avoid conflicts if trigger used - { - $expeditionline = new ExpeditionLigne($this->db); - $expeditionline->array_options=$array_options; - $expeditionline->id= $this->db->last_insert_id(MAIN_DB_PREFIX.$expeditionline->table_element); - $result=$expeditionline->insertExtraFields(); - if ($result < 0) - { - $this->error[]=$expeditionline->error; - $error++; - } - } - - if (! $error) return $line_id; - else return -1; + return $lineId; } @@ -2307,7 +2277,7 @@ class ExpeditionLigne extends CommonObjectLine /** * Insert line into database * - * @param User $user User that modify + * @param User $user User that modify * @param int $notrigger 1 = disable triggers * @return int <0 if KO, line id >0 if OK */ @@ -2323,10 +2293,10 @@ class ExpeditionLigne extends CommonObjectLine $this->errors[] = 'ErrorMandatoryParametersNotProvided'; return -1; } - // Clean parameters - if (empty($this->entrepot_id)) $this->entrepot_id='null'; + // Clean parameters + if (empty($this->entrepot_id)) $this->entrepot_id='null'; - $this->db->begin(); + $this->db->begin(); $sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet ("; $sql.= "fk_expedition"; @@ -2334,9 +2304,9 @@ class ExpeditionLigne extends CommonObjectLine $sql.= ", fk_origin_line"; $sql.= ", qty"; $sql.= ") VALUES ("; - $sql.= $this->id; + $sql.= $this->fk_expedition; $sql.= ", ".$this->entrepot_id; - $sql.= ", ".$this->origin_line_id; + $sql.= ", ".$this->fk_origin_line; $sql.= ", ".$this->qty; $sql.= ")"; @@ -2347,7 +2317,6 @@ class ExpeditionLigne extends CommonObjectLine $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet"); if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used { - $this->id=$this->rowid; $result=$this->insertExtraFields(); if ($result < 0) { @@ -2355,75 +2324,50 @@ class ExpeditionLigne extends CommonObjectLine } } - if (! $error && ! $notrigger) - { - // Call trigger - $result=$this->call_trigger('LINESHIPMENT_INSERT',$user); - if ($result < 0) $error++; - // End call triggers - } + if (! $error && ! $notrigger) + { + // Call trigger + $result=$this->call_trigger('LINESHIPPING_INSERT',$user); + if ($result < 0) + { + $this->errors[]=$this->error; + $error++; + } + // End call triggers + } - if (!$error) { - $this->db->commit(); - return $this->id; - } + if (! $error) { + $this->db->commit(); + return $this->id; + } - foreach($this->errors as $errmsg) - { - dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); - $this->error.=($this->error?', '.$errmsg:$errmsg); - } - $this->db->rollback(); - return -1*$error; + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; } else { $error++; } - - if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && is_array($array_options) && count($array_options)>0) // For avoid conflicts if trigger used - { - $expeditionline = new ExpeditionLigne($this->db); - $expeditionline->array_options=$array_options; - $expeditionline->id= $this->db->last_insert_id(MAIN_DB_PREFIX.$expeditionline->table_element); - $result=$expeditionline->insertExtraFields(); - if ($result < 0) - { - $this->error[]=$expeditionline->error; - $error++; - } - } - - if (! $error) return $line_id; - else return -1; - - // //////////////////// - - dol_syslog(get_class($this)."::insert", LOG_DEBUG); - $resql=$this->db->query($sql); - if ($resql) - { - $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'commandedet'); - - - } - else - { - $this->error=$this->db->error(); - $this->db->rollback(); - return -2; - } - } + } /** * Delete shipment line. * + * @param User $user User that modify + * @param int $notrigger 0=launch triggers after, 1=disable triggers * @return int >0 if OK, <0 if KO */ - function delete() + function delete($user = null, $notrigger = 0) { global $conf; + $error=0; + $this->db->begin(); // delete batch expedition line @@ -2435,15 +2379,14 @@ class ExpeditionLigne extends CommonObjectLine if (!$this->db->query($sql)) { $this->errors[]=$this->db->lasterror()." - sql=$sql"; - $this->db->rollback(); - return -2; + $error++; } } $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet"; $sql.= " WHERE rowid = ".$this->id; - if ( $this->db->query($sql)) + if (! $error && $this->db->query($sql)) { // Remove extrafields if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used @@ -2452,26 +2395,40 @@ class ExpeditionLigne extends CommonObjectLine if ($result < 0) { $this->errors[]=$this->error; - $this->db->rollback(); - return -4; + $error++; } - else - { - $this->db->commit(); - return 1; - } - } - else + } + if (! $error && ! $notrigger) { - $this->db->commit(); - return 1; + // Call trigger + $result=$this->call_trigger('LINESHIPPING_DELETE',$user); + if ($result < 0) + { + $this->errors[]=$this->error; + $error++; + } + // End call triggers } } else { $this->errors[]=$this->db->lasterror()." - sql=$sql"; + $error++; + } + + if (! $error) { + $this->db->commit(); + return 1; + } + else + { + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } $this->db->rollback(); - return -3; + return -1*$error; } } @@ -2480,151 +2437,172 @@ class ExpeditionLigne extends CommonObjectLine * * @return int < 0 if KO, > 0 if OK */ - function update() - { - global $conf; + function update($user = null, $notrigger = 0) + { + global $conf; + + $error=0; - dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty"); - - // Add a protection to refuse deleting if shipment is not in draft status + dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty"); + + // check parameters if (! isset($this->id) || ! isset($this->entrepot_id)) { dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR); - $this->errors[]='ErrorBadParameters'; + $this->errors[]='ErrorMandatoryParametersNotProvided'; + $error++; return -1; } - - $this->db->begin(); - // Clean parameters - if (empty($this->qty)) $this->qty=0; - $qty=price2num($this->qty); - $remainingQty = 0; - $batch = null; - $batch_id = null; - if (is_array($this->detail_batch)) - { - if (count($this->detail_batch) > 1) - { - dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR); - $this->errors[]='ErrorBadParameters'; - return -7; - } - else - { - $batch = $this->detail_batch[0]->batch; - $batch_id = $this->detail_batch[0]->fk_origin_stock; - } - } - else - { - $batch = $this->detail_batch->batch; - $batch_id = $this->detail_batch->fk_origin_stock; - } + $this->db->begin(); + + // Clean parameters + if (empty($this->qty)) $this->qty=0; + $qty=price2num($this->qty); + $remainingQty = 0; + $batch = null; + $batch_id = null; + if (is_array($this->detail_batch)) + { + if (count($this->detail_batch) > 1) + { + dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR); + $this->errors[]='ErrorBadParameters'; + $error++; + } + else + { + $batch = $this->detail_batch[0]->batch; + $batch_id = $this->detail_batch[0]->fk_origin_stock; + } + } + else + { + $batch = $this->detail_batch->batch; + $batch_id = $this->detail_batch->fk_origin_stock; + } // update lot - if (!empty($batch) && $conf->productbatch->enabled) + if (! empty($batch) && $conf->productbatch->enabled) { if (empty($batch_id) || empty($this->fk_product)) { dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR); - $this->errors[]='ErrorBadParameters'; - return -8; + $this->errors[]='ErrorMandatoryParametersNotProvided'; + $error++; } - + // fetch remaining lot qty require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionbatch.class.php'; if (($lotArray = ExpeditionLineBatch::fetchAll($this->db, $this->id)) < 0) { $this->errors[]=$this->db->lasterror()." - ExpeditionLineBatch::fetchAll"; - $this->db->rollback(); - return -4; - } - foreach ($lotArray as $lot) + $error++; + } + else { - if ($batch != $lot->batch) + foreach ($lotArray as $lot) { - $remainingQty += $lot->dluo_qty; - } - } - - //fetch lot details - - // fetch from product_lot - require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php'; - $lot = new Productlot($this->db); - if ($lot->fetch(0,$this->fk_product,$batch) < 0) - { - $this->errors[] = $lot->errors; - return -3; - } - - // delete lot expedition line - $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch"; - $sql.= " WHERE fk_expeditiondet = ".$this->id; - $sql.= " AND batch = '".$this->db->escape($batch)."'"; - - if (!$this->db->query($sql)) - { - $this->errors[]=$this->db->lasterror()." - sql=$sql"; - $this->db->rollback(); - return -2; - } - - if ($qty > 0) { - if (isset($lot->id)) - { - $shipmentLot = new ExpeditionLineBatch($this->db); - $shipmentLot->batch = $lot->batch; - $shipmentLot->eatby = $lot->eatby; - $shipmentLot->sellby = $lot->sellby; - $shipmentLot->entrepot_id = $this->entrepot_id; - $shipmentLot->dluo_qty = $qty; - $shipmentLot->fk_origin_stock = $batch_id; - if ($shipmentLot->create($this->id) < 0) + if ($batch != $lot->batch) { - $this->errors[]=$shipmentLot->errors; - $this->db->rollback(); - return -6; + $remainingQty += $lot->dluo_qty; + } + } + + //fetch lot details + + // fetch from product_lot + require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php'; + $lot = new Productlot($this->db); + if ($lot->fetch(0,$this->fk_product,$batch) < 0) + { + $this->errors[] = $lot->errors; + $error++; + } + else + { + // delete lot expedition line + $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch"; + $sql.= " WHERE fk_expeditiondet = ".$this->id; + $sql.= " AND batch = '".$this->db->escape($batch)."'"; + + if (!$this->db->query($sql)) + { + $this->errors[]=$this->db->lasterror()." - sql=$sql"; + $error++; + } + else if ($qty > 0) + { + if (isset($lot->id)) + { + $shipmentLot = new ExpeditionLineBatch($this->db); + $shipmentLot->batch = $lot->batch; + $shipmentLot->eatby = $lot->eatby; + $shipmentLot->sellby = $lot->sellby; + $shipmentLot->entrepot_id = $this->entrepot_id; + $shipmentLot->dluo_qty = $qty; + $shipmentLot->fk_origin_stock = $batch_id; + if ($shipmentLot->create($this->id) < 0) + { + $this->errors[]=$shipmentLot->errors; + $error++; + } + } } } } } - - // update line - $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; - $sql.= " fk_entrepot = ".$this->entrepot_id; - $sql.= " , qty = ".($qty + $remainingQty); - $sql.= " WHERE rowid = ".$this->id; - - if (!$this->db->query($sql)) + if (! $error) { - $this->errors[]=$this->db->lasterror()." - sql=$sql"; - $this->db->rollback(); - return -5; - } - - if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used - { - $this->id=$this->rowid; - $result=$this->insertExtraFields(); - if ($result < 0) + // update line + $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; + $sql.= " fk_entrepot = ".$this->entrepot_id; + $sql.= " , qty = ".($qty + $remainingQty); + $sql.= " WHERE rowid = ".$this->id; + + if (!$this->db->query($sql)) { - $this->errors[]=$this->error; - $this->db->rollback(); - return -4; + $this->errors[]=$this->db->lasterror()." - sql=$sql"; + $error++; } else { - $this->db->commit(); - return 1; + if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used + { + $result=$this->insertExtraFields(); + if ($result < 0) + { + $this->errors[]=$this->error; + $error++; + } + } } - } - else + } + if (! $error && ! $notrigger) { + // Call trigger + $result=$this->call_trigger('LINESHIPPING_UPDATE',$user); + if ($result < 0) + { + $this->errors[]=$this->error; + $error++; + } + // End call triggers + } + if (!$error) { $this->db->commit(); return 1; } + else + { + foreach($this->errors as $errmsg) + { + dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR); + $this->error.=($this->error?', '.$errmsg:$errmsg); + } + $this->db->rollback(); + return -1*$error; + } } } From 146f17faa6bacf55066d4a222582fb97eed784b4 Mon Sep 17 00:00:00 2001 From: fappels Date: Wed, 18 Oct 2017 18:18:17 +0200 Subject: [PATCH 03/17] Add delete line to expedition card --- htdocs/expedition/card.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index 9fa609d9d76..bc647c31999 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -69,6 +69,7 @@ $id = $origin_id; if (empty($origin_id)) $origin_id = GETPOST('origin_id','int'); // Id of order or propal if (empty($origin_id)) $origin_id = GETPOST('object_id','int'); // Id of order or propal $ref=GETPOST('ref','alpha'); +$line_id = GETPOST('lineid','int')?GETPOST('lineid','int'):''; // Security check $socid=''; @@ -596,6 +597,18 @@ if (empty($reshook)) } } + elseif ($action == 'deleteline' && ! empty($line_id)) + { + $object->fetch($id); + $line = new ExpeditionLigne($db); + $line->id = $line_id; + $result = $line->delete($user); + if($result >= 0) { + header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id); + exit(); + } + } + include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php'; // Actions to send emails @@ -1769,6 +1782,8 @@ else if ($id || $ref) print ''.$langs->trans("Batch").''; } + print ''; + print ''; print "\n"; $var=false; @@ -1992,6 +2007,18 @@ else if ($id || $ref) print ''; } } + + // edit-delete buttons + print ''; + print 'id . '">'; + print img_edit(); + print ''; + print ''; + print ''; + print 'id . '">'; + print img_delete(); + print ''; + print ''; print ""; // Display lines extrafields @@ -2003,8 +2030,6 @@ else if ($id || $ref) print $line->showOptionals($extrafieldsline, 'view', array('style'=>$bc[$var], 'colspan'=>$colspan),$indiceAsked); print ''; } - - } // TODO Show also lines ordered but not delivered From 6571083a78960f76d7bb52fdc67dfaac55356ae0 Mon Sep 17 00:00:00 2001 From: fappels Date: Mon, 23 Oct 2017 22:16:00 +0200 Subject: [PATCH 04/17] Add line id to detail_entrepot object Add line id to detail_entrepot object. Add expedition batch id to update batch in line update --- htdocs/expedition/class/expedition.class.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index 05f6c9e57d3..f97de9b8b5a 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -1336,6 +1336,7 @@ class Expedition extends CommonObject $detail_entrepot = new stdClass; $detail_entrepot->entrepot_id = $obj->fk_entrepot; $detail_entrepot->qty_shipped = $obj->qty_shipped; + $detail_entrepot->line_id = $obj->line_id; $line->details_entrepot[] = $detail_entrepot; $line->line_id = $obj->line_id; @@ -2462,6 +2463,7 @@ class ExpeditionLigne extends CommonObjectLine $remainingQty = 0; $batch = null; $batch_id = null; + $expedition_batch_id = null; if (is_array($this->detail_batch)) { if (count($this->detail_batch) > 1) @@ -2474,19 +2476,23 @@ class ExpeditionLigne extends CommonObjectLine { $batch = $this->detail_batch[0]->batch; $batch_id = $this->detail_batch[0]->fk_origin_stock; + $expedition_batch_id = $this->detail_batch[0]->id; } } else { $batch = $this->detail_batch->batch; $batch_id = $this->detail_batch->fk_origin_stock; + $expedition_batch_id = $this->detail_batch->id; } // update lot if (! empty($batch) && $conf->productbatch->enabled) { - if (empty($batch_id) || empty($this->fk_product)) { + dol_syslog(get_class($this)."::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch"); + + if (empty($batch_id) || empty($expedition_batch_id) || empty($this->fk_product)) { dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR); $this->errors[]='ErrorMandatoryParametersNotProvided'; $error++; @@ -2503,7 +2509,7 @@ class ExpeditionLigne extends CommonObjectLine { foreach ($lotArray as $lot) { - if ($batch != $lot->batch) + if ($expedition_batch_id != $lot->id) { $remainingQty += $lot->dluo_qty; } @@ -2524,7 +2530,7 @@ class ExpeditionLigne extends CommonObjectLine // delete lot expedition line $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch"; $sql.= " WHERE fk_expeditiondet = ".$this->id; - $sql.= " AND batch = '".$this->db->escape($batch)."'"; + $sql.= " AND rowid = ".$expedition_batch_id; if (!$this->db->query($sql)) { From 5342785a4973c1e3d35be59704ec77f399876e1f Mon Sep 17 00:00:00 2001 From: fappels Date: Mon, 23 Oct 2017 22:16:40 +0200 Subject: [PATCH 05/17] Add entrepot id to expedition batch fetchAll --- .../class/expeditionbatch.class.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/htdocs/expedition/class/expeditionbatch.class.php b/htdocs/expedition/class/expeditionbatch.class.php index 5253156e32a..c0a2decd93d 100644 --- a/htdocs/expedition/class/expeditionbatch.class.php +++ b/htdocs/expedition/class/expeditionbatch.class.php @@ -177,15 +177,18 @@ class ExpeditionLineBatch extends CommonObject */ static function fetchAll($db,$id_line_expdet) { - $sql="SELECT rowid,"; - $sql.= "fk_expeditiondet"; - $sql.= ", sellby"; - $sql.= ", eatby"; - $sql.= ", batch"; - $sql.= ", qty"; - $sql.= ", fk_origin_stock"; - $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element; - $sql.= " WHERE fk_expeditiondet=".(int) $id_line_expdet; + $sql="SELECT t.rowid"; + $sql.= ", t.fk_expeditiondet"; + $sql.= ", t.sellby"; + $sql.= ", t.eatby"; + $sql.= ", t.batch"; + $sql.= ", t.qty"; + $sql.= ", t.fk_origin_stock"; + $sql.= ", ps.fk_entrepot"; + $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element." t"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_batch pb ON t.fk_origin_stock = pb.rowid"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock ps ON pb.fk_product_stock = ps.rowid"; + $sql.= " WHERE t.fk_expeditiondet=".(int) $id_line_expdet; dol_syslog(__METHOD__ ."", LOG_DEBUG); $resql=$db->query($sql); @@ -207,6 +210,7 @@ class ExpeditionLineBatch extends CommonObject $tmp->fk_origin_stock = $obj->fk_origin_stock; $tmp->fk_expeditiondet = $obj->fk_expeditiondet; $tmp->dluo_qty = $obj->qty; + $tmp->entrepot_id = $obj->fk_entrepot; $ret[]=$tmp; $i++; From fe2acb2cb6a31e1b9e0a9ecf1adb18879745dfcf Mon Sep 17 00:00:00 2001 From: fappels Date: Mon, 23 Oct 2017 22:17:24 +0200 Subject: [PATCH 06/17] Add form select for lot numbers --- .../product/class/html.formproduct.class.php | 124 +++++++++++++++++- 1 file changed, 123 insertions(+), 1 deletion(-) diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index 8d4ddc4705d..96a7d35f62b 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -34,6 +34,7 @@ class FormProduct // Cache arrays var $cache_warehouses=array(); + var $cache_lot=array(); /** @@ -329,5 +330,126 @@ class FormProduct return $return; } -} + /** + * Return list of lot numbers (stock from product_batch) with stock location and stock qty + * + * @param int $selected Id of preselected lot stock id ('' for no value, 'ifone'=select value if one value otherwise no value) + * @param string $htmlname Name of html select html + * @param string $filterstatus lot status filter, following comma separated filter options can be used + * @param int $empty 1=Can be empty, 0 if not + * @param int $disabled 1=Select is disabled + * @param int $fk_product show lot numbers of product with id fk_product. All if 0. + * @param int $fk_entrepot show lot numbers in warehouse with id fk_entrepot. All if 0. + * @param string $empty_label Empty label if needed (only if $empty=1) + * @param int $forcecombo 1=Force combo iso ajax select2 + * @param array $events Events to add to select2 + * @param string $morecss Add more css classes to HTML select + * + * @return string HTML select + */ + function selectLotStock($selected='',$htmlname='batch_id',$filterstatus='',$empty=0,$disabled=0,$fk_product=0,$fk_entrepot=0,$empty_label='', $forcecombo=0, $events=array(), $morecss='minwidth200') + { + global $langs; + dol_syslog(get_class($this)."::selectLot $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $fk_entrepot, $empty_label, $showstock, $forcecombo, $morecss",LOG_DEBUG); + + $out=''; + + $nboflot = $this->loadLotStock($fk_product, $fk_entrepot); + + if ($conf->use_javascript_ajax && ! $forcecombo) + { + include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php'; + $comboenhancement = ajax_combobox($htmlname, $events); + $out.= $comboenhancement; + } + + $out.=''; + if ($disabled) $out.=''; + + return $out; + } + + /** + * Load in cache array list of lot available in stock + * If fk_product is not 0, we do not use cache + * + * @param int $fk_product load lot of id fk_product, all if 0. + * @param string $fk_entrepot load lot in fk_entrepot all if 0. + * + * @return int Nb of loaded lines, 0 if already loaded, <0 if KO + */ + function loadLotStock($fk_product = 0, $fk_entrepot = 0) + { + global $conf, $langs; + + if (empty($fk_product) && empty($fk_product) && count($this->cache_lot)) + { + return count($this->cache_lot); // Cache already loaded and we do not want a list with information specific to a product or warehouse + } + else + { + // clear cache; + $this->cache_lot = array(); + } + + $sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.label"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_batch as pb"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as ps on ps.rowid = pb.fk_product_stock"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (".getEntity('stock').")"; + if (!empty($fk_product) || !empty($fk_entrepot)) + { + $sql.= " WHERE"; + if (!empty($fk_product)) + { + $sql.= " ps.fk_product = '".$fk_product."'"; + if (!empty($fk_entrepot)) + { + $sql.= " AND"; + } + } + if (!empty($fk_entrepot)) + { + $sql.= " ps.fk_entrepot = '".$fk_entrepot."'"; + } + } + $sql.= " ORDER BY e.label, pb.batch"; + + dol_syslog(get_class($this).'::loadLotStock', LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + $this->cache_lot[$obj->rowid]['id'] =$obj->rowid; + $this->cache_lot[$obj->rowid]['batch']=$obj->batch; + $this->cache_lot[$obj->rowid]['entrepot_id']=$obj->fk_entrepot; + $this->cache_lot[$obj->rowid]['entrepot_label']=$obj->label; + $this->cache_lot[$obj->rowid]['qty'] = $obj->qty; + $i++; + } + + return $num; + } + else + { + dol_print_error($this->db); + return -1; + } + } +} \ No newline at end of file From b2cd2290e1cf0d3fb844bae97694dd6ca5c62900 Mon Sep 17 00:00:00 2001 From: fappels Date: Mon, 23 Oct 2017 22:18:15 +0200 Subject: [PATCH 07/17] Update card for line update --- htdocs/expedition/card.php | 452 +++++++++++++++++++++++++++++-------- 1 file changed, 364 insertions(+), 88 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index bc647c31999..e33176bf391 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -46,7 +46,10 @@ require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php'; if (! empty($conf->product->enabled) || ! empty($conf->service->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; if (! empty($conf->propal->enabled)) require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; if (! empty($conf->commande->enabled)) require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; -if (! empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; +if (! empty($conf->productbatch->enabled)) { + require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; + require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productstockentrepot.class.php'; +} if (! empty($conf->projet->enabled)) { require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php'; @@ -597,16 +600,184 @@ if (empty($reshook)) } } + /* + * delete a line + */ elseif ($action == 'deleteline' && ! empty($line_id)) { $object->fetch($id); + $lines = $object->lines; $line = new ExpeditionLigne($db); - $line->id = $line_id; - $result = $line->delete($user); - if($result >= 0) { + + $num_prod = count($lines); + for ($i = 0 ; $i < $num_prod ; $i++) + { + if (count($lines[$i]->details_entrepot) > 1) + { + // delete multi warehouse lines + foreach ($lines[$i]->details_entrepot as $details_entrepot) { + $line->id = GETPOST("lineid".$details_entrepot->line_id); + if (!error && $line->delete($user) < 0) + { + $error++; + } + } + } + else if ($lines[$i] == $line_id) + { + // delete single warehouse line + $line->id = $line_id; + if (!error && $line->delete($user) < 0) + { + $error++; + } + } + } + + if(! $error) { header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id); exit(); } + else + { + setEventMessages($line->error, $line->errors, 'errors'); + } + } + + /* + * Update a line + */ + else if ($action == 'updateline' && $user->rights->expedition->creer && GETPOST('save')) + { + // Clean parameters + + $qty=0; + $entrepot_id = 0; + $batch_id = 0; + + $lines = $object->lines; + $num_prod = count($lines); + for ($i = 0 ; $i < $num_prod ; $i++) + { + if ($lines[$i]->id == $line_id) + { + // line to update + $line = new ExpeditionLigne($db); + + $line->fk_product = $lines[$i]->fk_product; + if (is_array($lines[$i]->detail_batch) && count($lines[$i]->detail_batch) > 0) + { + // line with lot + foreach ($lines[$i]->detail_batch as $detail_batch) + { + $lotStock = new Productbatch($db); + $batch="batchl".$detail_batch->fk_expeditiondet."_".$detail_batch->fk_origin_stock; + $qty = "qtyl".$detail_batch->fk_expeditiondet.'_'.$detail_batch->id; + $batch_id = GETPOST($batch,'int'); + if (! empty($batch_id)) + { + if ($lotStock->fetch($batch_id) > 0) + { + $line->id = $detail_batch->fk_expeditiondet; + $line->detail_batch->fk_origin_stock = $batch_id; + $line->detail_batch->batch = $lotStock->batch; + $line->detail_batch->id = $detail_batch->id; + $line->entrepot_id = $lotStock->warehouseid; + $line->qty = GETPOST($qty, 'int'); + if ($line->update($user) < 0) { + setEventMessages($line->error, $line->errors, 'errors'); + $error++; + } + } + else + { + setEventMessages($lotStock->error, $lotStock->errors, 'errors'); + $error++; + } + } + } + } + else + { + // line without lot + if ($lines[$i]->entrepot_id > 0) + { + // single warehouse shipment line + $stockLocation="entl".$line_id; + $qty = "qtyl".$line_id; + $line->id = $line_id; + $line->entrepot_id = GETPOST($stockLocation,'int'); + $line->qty = GETPOST($qty, 'int'); + if ($line->update($user) < 0) { + setEventMessages($line->error, $line->errors, 'errors'); + $error++; + } + } + else if (count($lines[$i]->details_entrepot) > 1) + { + // multi warehouse shipment lines + foreach ($lines[$i]->details_entrepot as $detail_entrepot) + { + if (! $error) { + $stockLocation="entl".$detail_entrepot->line_id; + $qty = "qtyl".$detail_entrepot->line_id; + $warehouse = GETPOST($stockLocation,'int'); + if (!empty ($warehouse)) + { + $line->id = $detail_entrepot->line_id; + $line->entrepot_id = $warehouse; + $line->qty = GETPOST($qty, 'int'); + if ($line->update($user) < 0) { + setEventMessages($line->error, $line->errors, 'errors'); + $error++; + } + } + } + } + } + } + } + } + + // Extrafields Lines + $extrafieldsline = new ExtraFields($db); + $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line); + $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline); + // Unset extrafield POST Data + if (is_array($extralabelsline)) { + foreach ($extralabelsline as $key => $value) { + unset($_POST["options_" . $key]); + } + } + + if (! $error) { + if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) { + // Define output language + $outputlangs = $langs; + $newlang = ''; + if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) + $newlang = GETPOST('lang_id','aZ09'); + if ($conf->global->MAIN_MULTILANGS && empty($newlang)) + $newlang = $object->thirdparty->default_lang; + if (! empty($newlang)) { + $outputlangs = new Translate("", $conf); + $outputlangs->setDefaultLang($newlang); + } + + $ret = $object->fetch($object->id); // Reload to get new records + $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref); + } + } + else + { + header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $object->id); // Pour reaffichage de la fiche en cours d'edition + exit(); + } + } + + else if ($action == 'updateline' && $user->rights->expedition->creer && GETPOST('cancel','alpha') == $langs->trans('Cancel')) { + header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $object->id); // Pour reaffichage de la fiche en cours d'edition + exit(); } include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php'; @@ -1743,6 +1914,15 @@ else if ($id || $ref) /* * Lines of products */ + if ($action == 'editline') + { + print '
+ + + + + '; + } print '
'; print '
'; @@ -1754,36 +1934,62 @@ else if ($id || $ref) } print ''.$langs->trans("Products").''; print ''.$langs->trans("QtyOrdered").''; - if ($object->statut <= 1) - { - print ''.$langs->trans("QtyToShip").''; - } - else - { - print ''.$langs->trans("QtyShipped").''; - } - if ($origin && $origin_id > 0) { - print ''.$langs->trans("QtyInOtherShipments").''; + print ''.$langs->trans("QtyInOtherShipments").''; + } + if ($action == 'editline') + { + $editColspan = 3; + if (empty($conf->stock->enabled)) $editColspan--; + if (empty($conf->productbatch->enabled)) $editColspan--; + print ''; + if ($object->statut <= 1) + { + print $langs->trans("QtyToShip").' - '; + } + else + { + print $langs->trans("QtyShipped").' - '; + } + if (! empty($conf->stock->enabled)) + { + print $langs->trans("WarehouseSource").' - '; + } + if (! empty($conf->productbatch->enabled)) + { + print $langs->trans("Batch"); + } + print ''; + } + else + { + if ($object->statut <= 1) + { + print ''.$langs->trans("QtyToShip").''; + } + else + { + print ''.$langs->trans("QtyShipped").''; + } + if (! empty($conf->stock->enabled)) + { + print ''.$langs->trans("WarehouseSource").''; + } + + if (! empty($conf->productbatch->enabled)) + { + print ''.$langs->trans("Batch").''; + } } - print ''.$langs->trans("CalculatedWeight").''; print ''.$langs->trans("CalculatedVolume").''; //print ''.$langs->trans("Size").''; - - if (! empty($conf->stock->enabled)) + if ($object->statut == 0) { - print ''.$langs->trans("WarehouseSource").''; + print ''; + print ''; } - - if (! empty($conf->productbatch->enabled)) - { - print ''.$langs->trans("Batch").''; - } - - print ''; - print ''; print "\n"; $var=false; @@ -1907,9 +2113,6 @@ else if ($id || $ref) // Qty ordered print ''.$lines[$i]->qty_asked.''; - // Qty to ship or shipped - print ''.$lines[$i]->qty_shipped.''; - // Qty in other shipments (with shipment and warehouse used) if ($origin && $origin_id > 0) { @@ -1941,6 +2144,111 @@ else if ($id || $ref) } print ''; + if ($action == 'editline' && $lines[$i]->id == $line_id) + { + // edit mode + print ''; + if (is_array($lines[$i]->detail_batch) && count($lines[$i]->detail_batch) > 0) + { + foreach ($lines[$i]->detail_batch as $detail_batch) + { + print ''; + // Qty to ship or shipped + print ''; + // Batch number managment + print ''; + print ''; + } + } + else if (! empty($conf->stock->enabled)) + { + if ($lines[$i]->entrepot_id > 0) + { + print ''; + // Qty to ship or shipped + print ''; + // Warehouse source + print ''; + // Batch number managment + print ''; + print ''; + } + else if (count($lines[$i]->details_entrepot) > 1) + { + foreach ($lines[$i]->details_entrepot as $detail_entrepot) + { + print ''; + // Qty to ship or shipped + print ''; + // Warehouse source + print ''; + // Batch number managment + print ''; + print ''; + } + } + } + print '
' . '' . '' . $formproduct->selectLotStock($detail_batch->fk_origin_stock, 'batchl'.$detail_batch->fk_expeditiondet.'_'.$detail_batch->fk_origin_stock, '', 1, 0, $lines[$i]->fk_product, 0, '', 0). '
' . '' . '' . $formproduct->selectWarehouses($lines[$i]->entrepot_id, 'entl'.$line_id, '', 1, 0, $lines[$i]->fk_product, '', 1). ' - ' . $langs->trans("NA") . '
' . '' . '' . $formproduct->selectWarehouses($detail_entrepot->entrepot_id, 'entl'.$detail_entrepot->line_id, '', 1, 0, $lines[$i]->fk_product, '', 1) . ' - ' . $langs->trans("NA") . '
'; + } + else + { + // Qty to ship or shipped + print ''.$lines[$i]->qty_shipped.''; + + // Warehouse source + if (! empty($conf->stock->enabled)) + { + print ''; + if ($lines[$i]->entrepot_id > 0) + { + $entrepot = new Entrepot($db); + $entrepot->fetch($lines[$i]->entrepot_id); + print $entrepot->getNomUrl(1); + } + else if (count($lines[$i]->details_entrepot) > 1) + { + $detail = ''; + foreach ($lines[$i]->details_entrepot as $detail_entrepot) + { + if ($detail_entrepot->entrepot_id > 0) + { + $entrepot = new Entrepot($db); + $entrepot->fetch($detail_entrepot->entrepot_id); + $detail.= $langs->trans("DetailWarehouseFormat",$entrepot->libelle,$detail_entrepot->qty_shipped).'
'; + print ''; + } + } + print $form->textwithtooltip(img_picto('', 'object_stock').' '.$langs->trans("DetailWarehouseNumber"),$detail); + } + print ''; + } + + // Batch number managment + if (! empty($conf->productbatch->enabled)) + { + if (isset($lines[$i]->detail_batch)) + { + print ''; + if ($lines[$i]->product_tobatch) + { + $detail = ''; + foreach ($lines[$i]->detail_batch as $dbatch) + { + $detail.= $langs->trans("DetailBatchFormat",$dbatch->batch,dol_print_date($dbatch->eatby,"day"),dol_print_date($dbatch->sellby,"day"),$dbatch->dluo_qty).'
'; + } + print $form->textwithtooltip(img_picto('', 'object_barcode').' '.$langs->trans("DetailBatchNumber"),$detail); + } + else + { + print $langs->trans("NA"); + } + print ''; + } else { + print ''; + } + } + } + // Weight print ''; if ($lines[$i]->fk_product_type == 0) print $lines[$i]->weight*$lines[$i]->qty_shipped.' '.measuring_units_string($lines[$i]->weight_units,"weight"); @@ -1956,69 +2264,30 @@ else if ($id || $ref) // Size //print ''.$lines[$i]->volume*$lines[$i]->qty_shipped.' '.measuring_units_string($lines[$i]->volume_units,"volume").''; - // Warehouse source - if (! empty($conf->stock->enabled)) + if ($action == 'editline' && $lines[$i]->id == $line_id) { - print ''; - if ($lines[$i]->entrepot_id > 0) - { - $entrepot = new Entrepot($db); - $entrepot->fetch($lines[$i]->entrepot_id); - print $entrepot->getNomUrl(1); - } - else if (count($lines[$i]->details_entrepot) > 1) - { - $detail = ''; - foreach ($lines[$i]->details_entrepot as $detail_entrepot) - { - if ($detail_entrepot->entrepot_id > 0) - { - $entrepot = new Entrepot($db); - $entrepot->fetch($detail_entrepot->entrepot_id); - $detail.= $langs->trans("DetailWarehouseFormat",$entrepot->libelle,$detail_entrepot->qty_shipped).'
'; - } - } - print $form->textwithtooltip(img_picto('', 'object_stock').' '.$langs->trans("DetailWarehouseNumber"),$detail); - } + print ''; + print '
'; + print '
'; + } + else if ($object->statut == 0) + { + // edit-delete buttons + print ''; + print 'id . '">' . img_edit() . ''; + print ''; + print ''; + print 'id . '">' . img_delete() . ''; print ''; - } - // Batch number managment - if (! empty($conf->productbatch->enabled)) - { - if (isset($lines[$i]->detail_batch)) + // Display lines extrafields + if (! empty($rowExtrafieldsStart)) { - print ''; - if ($lines[$i]->product_tobatch) - { - $detail = ''; - foreach ($lines[$i]->detail_batch as $dbatch) - { - $detail.= $langs->trans("DetailBatchFormat",$dbatch->batch,dol_print_date($dbatch->eatby,"day"),dol_print_date($dbatch->sellby,"day"),$dbatch->dluo_qty).'
'; - } - print $form->textwithtooltip(img_picto('', 'object_barcode').' '.$langs->trans("DetailBatchNumber"),$detail); - } - else - { - print $langs->trans("NA"); - } - print ''; - } else { - print ''; + print $rowExtrafieldsStart; + print $rowExtrafieldsView; + print $rowEnd; } } - - // edit-delete buttons - print ''; - print 'id . '">'; - print img_edit(); - print ''; - print ''; - print ''; - print 'id . '">'; - print img_delete(); - print ''; - print ''; print ""; // Display lines extrafields @@ -2027,7 +2296,14 @@ else if ($id || $ref) $line = new ExpeditionLigne($db); $line->fetch_optionals($lines[$i]->id,$extralabelslines); print ''; - print $line->showOptionals($extrafieldsline, 'view', array('style'=>$bc[$var], 'colspan'=>$colspan),$indiceAsked); + if ($lines[$i]->id == $line_id) + { + print $line->showOptionals($extrafieldsline, 'edit', array('style'=>$bc[$var], 'colspan'=>$colspan),$indiceAsked); + } + else + { + print $line->showOptionals($extrafieldsline, 'view', array('style'=>$bc[$var], 'colspan'=>$colspan),$indiceAsked); + } print ''; } } @@ -2137,7 +2413,7 @@ else if ($id || $ref) * Documents generated */ - if ($action != 'presend') + if ($action != 'presend' && $action != 'editline') { print '
'; From ba189277ee88a2272cf76260b61c66406821a56e Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 24 Oct 2017 10:53:11 +0200 Subject: [PATCH 08/17] Fix update extrafield --- htdocs/expedition/card.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index e33176bf391..caf7d7b79a2 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -621,6 +621,7 @@ if (empty($reshook)) { $error++; } + unset($_POST["lineid".$details_entrepot->line_id]); } } else if ($lines[$i] == $line_id) @@ -631,6 +632,7 @@ if (empty($reshook)) { $error++; } + unset($_POST["lineid"]); } } @@ -663,7 +665,16 @@ if (empty($reshook)) { // line to update $line = new ExpeditionLigne($db); - + // Extrafields Lines + $extrafieldsline = new ExtraFields($db); + $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line); + $line->array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline); + // Unset extrafield POST Data + if (is_array($extralabelsline)) { + foreach ($extralabelsline as $key => $value) { + unset($_POST["options_" . $key]); + } + } $line->fk_product = $lines[$i]->fk_product; if (is_array($lines[$i]->detail_batch) && count($lines[$i]->detail_batch) > 0) { @@ -695,6 +706,8 @@ if (empty($reshook)) $error++; } } + unset($_POST[$batch]); + unset($_POST[$qty]); } } else @@ -712,6 +725,8 @@ if (empty($reshook)) setEventMessages($line->error, $line->errors, 'errors'); $error++; } + unset($_POST[$stockLocation]); + unset($_POST[$qty]); } else if (count($lines[$i]->details_entrepot) > 1) { @@ -732,6 +747,8 @@ if (empty($reshook)) $error++; } } + unset($_POST[$stockLocation]); + unset($_POST[$qty]); } } } @@ -739,16 +756,7 @@ if (empty($reshook)) } } - // Extrafields Lines - $extrafieldsline = new ExtraFields($db); - $extralabelsline = $extrafieldsline->fetch_name_optionals_label($object->table_element_line); - $array_options = $extrafieldsline->getOptionalsFromPost($extralabelsline); - // Unset extrafield POST Data - if (is_array($extralabelsline)) { - foreach ($extralabelsline as $key => $value) { - unset($_POST["options_" . $key]); - } - } + unset($_POST["lineid"]); if (! $error) { if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) { @@ -2296,7 +2304,7 @@ else if ($id || $ref) $line = new ExpeditionLigne($db); $line->fetch_optionals($lines[$i]->id,$extralabelslines); print ''; - if ($lines[$i]->id == $line_id) + if ($action == 'editline' && $lines[$i]->id == $line_id) { print $line->showOptionals($extrafieldsline, 'edit', array('style'=>$bc[$var], 'colspan'=>$colspan),$indiceAsked); } From 0c38944daa80ceb838f89346f62c8c13e487dfb2 Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 24 Oct 2017 11:37:17 +0200 Subject: [PATCH 09/17] Fix delete multi warehouse line --- htdocs/expedition/card.php | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index 623fe1f0426..c258f7beef6 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -612,28 +612,30 @@ if (empty($reshook)) $num_prod = count($lines); for ($i = 0 ; $i < $num_prod ; $i++) { - if (count($lines[$i]->details_entrepot) > 1) + if ($lines[$i]->id == $line_id) { - // delete multi warehouse lines - foreach ($lines[$i]->details_entrepot as $details_entrepot) { - $line->id = GETPOST("lineid".$details_entrepot->line_id); - if (!error && $line->delete($user) < 0) + if (count($lines[$i]->details_entrepot) > 1) + { + // delete multi warehouse lines + foreach ($lines[$i]->details_entrepot as $details_entrepot) { + $line->id = $details_entrepot->line_id; + if (! $error && $line->delete($user) < 0) + { + $error++; + } + } + } + else + { + // delete single warehouse line + $line->id = $line_id; + if (! $error && $line->delete($user) < 0) { $error++; } - unset($_POST["lineid".$details_entrepot->line_id]); } } - else if ($lines[$i] == $line_id) - { - // delete single warehouse line - $line->id = $line_id; - if (!error && $line->delete($user) < 0) - { - $error++; - } - unset($_POST["lineid"]); - } + unset($_POST["lineid"]); } if(! $error) { @@ -2223,7 +2225,6 @@ else if ($id || $ref) $entrepot = new Entrepot($db); $entrepot->fetch($detail_entrepot->entrepot_id); $detail.= $langs->trans("DetailWarehouseFormat",$entrepot->libelle,$detail_entrepot->qty_shipped).'
'; - print ''; } } print $form->textwithtooltip(img_picto('', 'object_stock').' '.$langs->trans("DetailWarehouseNumber"),$detail); From 914b8c1c7119e6f527af8cb633573c04c6b0323a Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 24 Oct 2017 11:45:11 +0200 Subject: [PATCH 10/17] cleanup --- htdocs/expedition/card.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index c258f7beef6..83898b5bf19 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -7,7 +7,7 @@ * Copyright (C) 2013 Florian Henry * Copyright (C) 2013 Marcos García * Copyright (C) 2014 Cedric GROSS - * Copyright (C) 2014-2015 Francis Appels + * Copyright (C) 2014-2017 Francis Appels * Copyright (C) 2015 Claudio Aschieri * Copyright (C) 2016 Ferran Marcet * Copyright (C) 2016 Yasser Carreón @@ -46,10 +46,7 @@ require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php'; if (! empty($conf->product->enabled) || ! empty($conf->service->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; if (! empty($conf->propal->enabled)) require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; if (! empty($conf->commande->enabled)) require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; -if (! empty($conf->productbatch->enabled)) { - require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; - require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productstockentrepot.class.php'; -} +if (! empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; if (! empty($conf->projet->enabled)) { require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php'; From eaf3958be7e1b31732614eeb9f478559e6428988 Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 24 Oct 2017 15:45:49 +0200 Subject: [PATCH 11/17] Revert "Add entrepot id to expedition batch fetchAll" This reverts commit 5342785a4973c1e3d35be59704ec77f399876e1f. --- .../class/expeditionbatch.class.php | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/htdocs/expedition/class/expeditionbatch.class.php b/htdocs/expedition/class/expeditionbatch.class.php index c0a2decd93d..5253156e32a 100644 --- a/htdocs/expedition/class/expeditionbatch.class.php +++ b/htdocs/expedition/class/expeditionbatch.class.php @@ -177,18 +177,15 @@ class ExpeditionLineBatch extends CommonObject */ static function fetchAll($db,$id_line_expdet) { - $sql="SELECT t.rowid"; - $sql.= ", t.fk_expeditiondet"; - $sql.= ", t.sellby"; - $sql.= ", t.eatby"; - $sql.= ", t.batch"; - $sql.= ", t.qty"; - $sql.= ", t.fk_origin_stock"; - $sql.= ", ps.fk_entrepot"; - $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element." t"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_batch pb ON t.fk_origin_stock = pb.rowid"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock ps ON pb.fk_product_stock = ps.rowid"; - $sql.= " WHERE t.fk_expeditiondet=".(int) $id_line_expdet; + $sql="SELECT rowid,"; + $sql.= "fk_expeditiondet"; + $sql.= ", sellby"; + $sql.= ", eatby"; + $sql.= ", batch"; + $sql.= ", qty"; + $sql.= ", fk_origin_stock"; + $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element; + $sql.= " WHERE fk_expeditiondet=".(int) $id_line_expdet; dol_syslog(__METHOD__ ."", LOG_DEBUG); $resql=$db->query($sql); @@ -210,7 +207,6 @@ class ExpeditionLineBatch extends CommonObject $tmp->fk_origin_stock = $obj->fk_origin_stock; $tmp->fk_expeditiondet = $obj->fk_expeditiondet; $tmp->dluo_qty = $obj->qty; - $tmp->entrepot_id = $obj->fk_entrepot; $ret[]=$tmp; $i++; From 6bf57962dcd639a15bf1e969bd8c67048dc13582 Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 24 Oct 2017 15:48:22 +0200 Subject: [PATCH 12/17] Fix travis --- htdocs/expedition/class/expedition.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index afe9a368f27..87581cea1ac 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -2438,7 +2438,9 @@ class ExpeditionLigne extends CommonObjectLine /** * Update a line in database - * + * + * @param User $user User that modify + * @param int $notrigger 1 = disable triggers * @return int < 0 if KO, > 0 if OK */ function update($user = null, $notrigger = 0) From cd5c678534c204c8e24593d7f460ccaceb7b6076 Mon Sep 17 00:00:00 2001 From: fappels Date: Sun, 29 Oct 2017 19:01:42 +0100 Subject: [PATCH 13/17] Only update new lot number --- htdocs/expedition/card.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index 83898b5bf19..838e34e44d0 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -684,7 +684,7 @@ if (empty($reshook)) $batch="batchl".$detail_batch->fk_expeditiondet."_".$detail_batch->fk_origin_stock; $qty = "qtyl".$detail_batch->fk_expeditiondet.'_'.$detail_batch->id; $batch_id = GETPOST($batch,'int'); - if (! empty($batch_id)) + if (! empty($batch_id) && $batch_id != $detail_batch->fk_origin_stock) { if ($lotStock->fetch($batch_id) > 0) { @@ -2163,10 +2163,10 @@ else if ($id || $ref) // Qty to ship or shipped print '' . '' . ''; // Batch number managment - print '' . $formproduct->selectLotStock($detail_batch->fk_origin_stock, 'batchl'.$detail_batch->fk_expeditiondet.'_'.$detail_batch->fk_origin_stock, '', 1, 0, $lines[$i]->fk_product, 0, '', 0). ''; + print '' . $formproduct->selectLotStock($detail_batch->fk_origin_stock, 'batchl'.$detail_batch->fk_expeditiondet.'_'.$detail_batch->fk_origin_stock, '', 1, 0, $lines[$i]->fk_product). ''; print ''; } - } + } else if (! empty($conf->stock->enabled)) { if ($lines[$i]->entrepot_id > 0) From a9ceb46f978a61bdcab70b4648e59e5326707580 Mon Sep 17 00:00:00 2001 From: fappels Date: Sun, 29 Oct 2017 20:40:56 +0100 Subject: [PATCH 14/17] Add parameter to load all lot from lines of object in cache If you want use the lot stock selector in mutiple lines you can add lines array. If you don't give a product id or a lines array, no lot numbers will be loaded. --- .../product/class/html.formproduct.class.php | 153 +++++++++++------- 1 file changed, 93 insertions(+), 60 deletions(-) diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index 96a7d35f62b..02c7bcddd0d 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -1,6 +1,6 @@ - * Copyright (C) 2016 Francis Appels + * Copyright (C) 2015-2017 Francis Appels * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -338,8 +338,9 @@ class FormProduct * @param string $filterstatus lot status filter, following comma separated filter options can be used * @param int $empty 1=Can be empty, 0 if not * @param int $disabled 1=Select is disabled - * @param int $fk_product show lot numbers of product with id fk_product. All if 0. - * @param int $fk_entrepot show lot numbers in warehouse with id fk_entrepot. All if 0. + * @param int $fk_product show lot numbers of product with id fk_product. All from objectLines if 0. + * @param int $fk_entrepot filter lot numbers for warehouse with id fk_entrepot. All if 0. + * @param array $objectLines Only cache lot numbers for products in lines of object. If no lines only for fk_product. If no fk_product, all. * @param string $empty_label Empty label if needed (only if $empty=1) * @param int $forcecombo 1=Force combo iso ajax select2 * @param array $events Events to add to select2 @@ -347,15 +348,26 @@ class FormProduct * * @return string HTML select */ - function selectLotStock($selected='',$htmlname='batch_id',$filterstatus='',$empty=0,$disabled=0,$fk_product=0,$fk_entrepot=0,$empty_label='', $forcecombo=0, $events=array(), $morecss='minwidth200') + function selectLotStock($selected='',$htmlname='batch_id',$filterstatus='',$empty=0,$disabled=0,$fk_product=0,$fk_entrepot=0,$objectLines = array(),$empty_label='', $forcecombo=0, $events=array(), $morecss='minwidth200') { global $langs; dol_syslog(get_class($this)."::selectLot $selected, $htmlname, $filterstatus, $empty, $disabled, $fk_product, $fk_entrepot, $empty_label, $showstock, $forcecombo, $morecss",LOG_DEBUG); $out=''; + $productIdArray = array(); + if (! is_array($objectLines) || ! count($objectLines)) + { + if (! empty($fk_product)) $productIdArray[] = $fk_product; + } + else + { + foreach ($objectLines as $line) { + if ($line->fk_product) $productIdArray[] = $line->fk_product; + } + } - $nboflot = $this->loadLotStock($fk_product, $fk_entrepot); + $nboflot = $this->loadLotStock($productIdArray); if ($conf->use_javascript_ajax && ! $forcecombo) { @@ -366,15 +378,33 @@ class FormProduct $out.=''; if ($disabled) $out.=''; @@ -386,70 +416,73 @@ class FormProduct * Load in cache array list of lot available in stock * If fk_product is not 0, we do not use cache * - * @param int $fk_product load lot of id fk_product, all if 0. + * @param array $productIdArray array of product id's from who to get lot numbers. A * @param string $fk_entrepot load lot in fk_entrepot all if 0. * - * @return int Nb of loaded lines, 0 if already loaded, <0 if KO + * @return int Nb of loaded lines, 0 if nothing loaded, <0 if KO */ - function loadLotStock($fk_product = 0, $fk_entrepot = 0) + private function loadLotStock($productIdArray = array()) { global $conf, $langs; - if (empty($fk_product) && empty($fk_product) && count($this->cache_lot)) + $cacheLoaded = false; + if (empty($productIdArray)) { - return count($this->cache_lot); // Cache already loaded and we do not want a list with information specific to a product or warehouse + // only Load lot stock for given products + $this->cache_lot = array(); + return 0; + } + if (count($productIdArray) && count($this->cache_lot)) + { + // check cache already loaded for product id's + foreach ($productIdArray as $productId) + { + $cacheLoaded = ! empty($this->cache_lot[$productId]) ? true : false; + } + } + if ($cacheLoaded) + { + return count($this->cache_lot); } else { - // clear cache; + // clear cache $this->cache_lot = array(); - } - - $sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.label"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_batch as pb"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as ps on ps.rowid = pb.fk_product_stock"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (".getEntity('stock').")"; - if (!empty($fk_product) || !empty($fk_entrepot)) - { - $sql.= " WHERE"; - if (!empty($fk_product)) + $productIdList = implode(',', $productIdArray); + $sql = "SELECT pb.batch, pb.rowid, ps.fk_entrepot, pb.qty, e.label, ps.fk_product"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_batch as pb"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_stock as ps on ps.rowid = pb.fk_product_stock"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."entrepot as e on e.rowid = ps.fk_entrepot AND e.entity IN (".getEntity('stock').")"; + if (!empty($productIdList)) { - $sql.= " ps.fk_product = '".$fk_product."'"; - if (!empty($fk_entrepot)) + $sql.= " WHERE ps.fk_product IN (".$productIdList.")"; + } + $sql.= " ORDER BY e.label, pb.batch"; + + dol_syslog(get_class($this).'::loadLotStock', LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) { - $sql.= " AND"; + $obj = $this->db->fetch_object($resql); + $this->cache_lot[$obj->fk_product][$obj->rowid]['id'] =$obj->rowid; + $this->cache_lot[$obj->fk_product][$obj->rowid]['batch']=$obj->batch; + $this->cache_lot[$obj->fk_product][$obj->rowid]['entrepot_id']=$obj->fk_entrepot; + $this->cache_lot[$obj->fk_product][$obj->rowid]['entrepot_label']=$obj->label; + $this->cache_lot[$obj->fk_product][$obj->rowid]['qty'] = $obj->qty; + $i++; } + + return $num; } - if (!empty($fk_entrepot)) + else { - $sql.= " ps.fk_entrepot = '".$fk_entrepot."'"; + dol_print_error($this->db); + return -1; } } - $sql.= " ORDER BY e.label, pb.batch"; - - dol_syslog(get_class($this).'::loadLotStock', LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) - { - $num = $this->db->num_rows($resql); - $i = 0; - while ($i < $num) - { - $obj = $this->db->fetch_object($resql); - $this->cache_lot[$obj->rowid]['id'] =$obj->rowid; - $this->cache_lot[$obj->rowid]['batch']=$obj->batch; - $this->cache_lot[$obj->rowid]['entrepot_id']=$obj->fk_entrepot; - $this->cache_lot[$obj->rowid]['entrepot_label']=$obj->label; - $this->cache_lot[$obj->rowid]['qty'] = $obj->qty; - $i++; - } - - return $num; - } - else - { - dol_print_error($this->db); - return -1; - } } } \ No newline at end of file From 08f931b73c7b0cbdd8fd065ac41fbc65c5e42a82 Mon Sep 17 00:00:00 2001 From: fappels Date: Mon, 30 Oct 2017 12:39:06 +0100 Subject: [PATCH 15/17] Only show show lot from same src warehouse. When shipping from multiple warehouse, only allow lot from same src warehouse. (expeditiondet_batch is on to many from expeditiondet). --- htdocs/expedition/card.php | 23 ++++-- htdocs/expedition/class/expedition.class.php | 76 ++++++++++++++++---- 2 files changed, 81 insertions(+), 18 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index 838e34e44d0..52c780e1680 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -684,16 +684,21 @@ if (empty($reshook)) $batch="batchl".$detail_batch->fk_expeditiondet."_".$detail_batch->fk_origin_stock; $qty = "qtyl".$detail_batch->fk_expeditiondet.'_'.$detail_batch->id; $batch_id = GETPOST($batch,'int'); - if (! empty($batch_id) && $batch_id != $detail_batch->fk_origin_stock) + $batch_qty = GETPOST($qty, 'int'); + if (! empty($batch_id) && ($batch_id != $detail_batch->fk_origin_stock || $batch_qty != $detail_batch->dluo_qty)) { - if ($lotStock->fetch($batch_id) > 0) + if ($lotStock->fetch($batch_id) > 0 && $line->fetch($detail_batch->fk_expeditiondet) > 0) { - $line->id = $detail_batch->fk_expeditiondet; + if ($lines[$i]->entrepot_id != 0) + { + // allow update line entrepot_id if not multi warehouse shipping + $line->entrepot_id = $lotStock->warehouseid; + } $line->detail_batch->fk_origin_stock = $batch_id; $line->detail_batch->batch = $lotStock->batch; $line->detail_batch->id = $detail_batch->id; - $line->entrepot_id = $lotStock->warehouseid; - $line->qty = GETPOST($qty, 'int'); + $line->detail_batch->entrepot_id = $lotStock->warehouseid; + $line->detail_batch->dluo_qty = $batch_qty; if ($line->update($user) < 0) { setEventMessages($line->error, $line->errors, 'errors'); $error++; @@ -2157,13 +2162,19 @@ else if ($id || $ref) print ''; if (is_array($lines[$i]->detail_batch) && count($lines[$i]->detail_batch) > 0) { + $line = new ExpeditionLigne($db); foreach ($lines[$i]->detail_batch as $detail_batch) { print ''; // Qty to ship or shipped print ''; // Batch number managment - print ''; + if ($lines[$i]->entrepot_id == 0) + { + // only show lot numbers from src warehouse when shipping from multiple warehouses + $line->fetch($detail_batch->fk_expeditiondet); + } + print ''; print ''; } } diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index 87581cea1ac..ecd56ed9d70 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -2278,6 +2278,40 @@ class ExpeditionLigne extends CommonObjectLine $this->db=$db; } + /** + * Load line expedition + * + * @param int $rowid Id line order + * @return int <0 if KO, >0 if OK + */ + function fetch($rowid) + { + $sql = 'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_origin_line, ed.qty, ed.rang'; + $sql.= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as ed'; + $sql.= ' WHERE ed.rowid = '.$rowid; + $result = $this->db->query($sql); + if ($result) + { + $objp = $this->db->fetch_object($result); + $this->id = $objp->rowid; + $this->fk_expedition = $objp->fk_expedition; + $this->entrepot_id = $objp->fk_entrepot; + $this->fk_origin_line = $objp->fk_origin_line; + $this->qty = $objp->qty; + $this->rang = $objp->rang; + + $this->db->free($result); + + return 1; + } + else + { + $this->errors[] = $this->db->lasterror(); + $this->error = $this->db->lasterror(); + return -1; + } + } + /** * Insert line into database * @@ -2451,14 +2485,7 @@ class ExpeditionLigne extends CommonObjectLine dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty"); - // check parameters - if (! isset($this->id) || ! isset($this->entrepot_id)) - { - dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR); - $this->errors[]='ErrorMandatoryParametersNotProvided'; - $error++; - return -1; - } + $this->db->begin(); @@ -2482,13 +2509,36 @@ class ExpeditionLigne extends CommonObjectLine $batch = $this->detail_batch[0]->batch; $batch_id = $this->detail_batch[0]->fk_origin_stock; $expedition_batch_id = $this->detail_batch[0]->id; + if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) + { + dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR); + $this->errors[]='ErrorBadParameters'; + $error++; + } + $qty = price2num($this->detail_batch[0]->dluo_qty); } } - else + else if (! empty($this->detail_batch)) { $batch = $this->detail_batch->batch; $batch_id = $this->detail_batch->fk_origin_stock; $expedition_batch_id = $this->detail_batch->id; + if ($this->entrepot_id != $this->detail_batch->entrepot_id) + { + dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR); + $this->errors[]='ErrorBadParameters'; + $error++; + } + $qty = price2num($this->detail_batch->dluo_qty); + } + + // check parameters + if (! isset($this->id) || ! isset($this->entrepot_id)) + { + dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR); + $this->errors[]='ErrorMandatoryParametersNotProvided'; + $error++; + return -1; } // update lot @@ -2512,6 +2562,7 @@ class ExpeditionLigne extends CommonObjectLine } else { + // caculate new total line qty foreach ($lotArray as $lot) { if ($expedition_batch_id != $lot->id) @@ -2519,6 +2570,7 @@ class ExpeditionLigne extends CommonObjectLine $remainingQty += $lot->dluo_qty; } } + $qty += $remainingQty; //fetch lot details @@ -2550,8 +2602,8 @@ class ExpeditionLigne extends CommonObjectLine $shipmentLot->batch = $lot->batch; $shipmentLot->eatby = $lot->eatby; $shipmentLot->sellby = $lot->sellby; - $shipmentLot->entrepot_id = $this->entrepot_id; - $shipmentLot->dluo_qty = $qty; + $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id; + $shipmentLot->dluo_qty = $this->detail_batch->dluo_qty; $shipmentLot->fk_origin_stock = $batch_id; if ($shipmentLot->create($this->id) < 0) { @@ -2568,7 +2620,7 @@ class ExpeditionLigne extends CommonObjectLine // update line $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET"; $sql.= " fk_entrepot = ".$this->entrepot_id; - $sql.= " , qty = ".($qty + $remainingQty); + $sql.= " , qty = ".$qty; $sql.= " WHERE rowid = ".$this->id; if (!$this->db->query($sql)) From f7df9e1d1f930c645c9cb36c5dc1129b0fa005be Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 31 Oct 2017 16:49:39 +0100 Subject: [PATCH 16/17] Fix travis --- htdocs/product/class/html.formproduct.class.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/htdocs/product/class/html.formproduct.class.php b/htdocs/product/class/html.formproduct.class.php index 02c7bcddd0d..68e80454b22 100644 --- a/htdocs/product/class/html.formproduct.class.php +++ b/htdocs/product/class/html.formproduct.class.php @@ -413,13 +413,11 @@ class FormProduct } /** - * Load in cache array list of lot available in stock - * If fk_product is not 0, we do not use cache + * Load in cache array list of lot available in stock from a given list of products * * @param array $productIdArray array of product id's from who to get lot numbers. A - * @param string $fk_entrepot load lot in fk_entrepot all if 0. * - * @return int Nb of loaded lines, 0 if nothing loaded, <0 if KO + * @return int Nb of loaded lines, 0 if nothing loaded, <0 if KO */ private function loadLotStock($productIdArray = array()) { From 3e58f7c609aca046df72425afcf8f5c10e31c683 Mon Sep 17 00:00:00 2001 From: fappels Date: Tue, 31 Oct 2017 17:20:29 +0100 Subject: [PATCH 17/17] Fix create line extra fields --- htdocs/expedition/class/expedition.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index 4209c0b71e4..3e341e6d5be 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -378,7 +378,7 @@ class Expedition extends CommonObject $expeditionline->entrepot_id = $entrepot_id; $expeditionline->fk_origin_line = $origin_line_id; $expeditionline->qty = $qty; - $expeditionline->$array_options = $array_options; + $expeditionline->array_options = $array_options; if (($lineId = $expeditionline->insert()) < 0) {
' . '' . '' . $formproduct->selectLotStock($detail_batch->fk_origin_stock, 'batchl'.$detail_batch->fk_expeditiondet.'_'.$detail_batch->fk_origin_stock, '', 1, 0, $lines[$i]->fk_product). '' . $formproduct->selectLotStock($detail_batch->fk_origin_stock, 'batchl'.$detail_batch->fk_expeditiondet.'_'.$detail_batch->fk_origin_stock, '', 1, 0, $lines[$i]->fk_product, $line->entrepot_id). '