Better update of database cleaning non expected records.

This commit is contained in:
Laurent Destailleur 2016-04-30 21:58:00 +02:00
parent 50323afe94
commit f324c662c2
2 changed files with 15 additions and 13 deletions

View File

@ -408,8 +408,8 @@ class Productbatch extends CommonObject
* Find first detail record that match eather eat-by or sell-by or batch within given warehouse
*
* @param int $fk_product_stock id product_stock for objet
* @param date $eatby eat-by date for objet
* @param date $sellby sell-by date for objet
* @param date $eatby eat-by date for objet - deprecated: a search must be done on batch number
* @param date $sellby sell-by date for objet - deprecated: a search must be done on batch number
* @param string $batch_number batch number for objet
* @return int <0 if KO, >0 if OK
*/

View File

@ -222,7 +222,7 @@ class MouvementStock extends CommonObject
$oldqtywarehouse=0;
// Test if there is already a record for couple (warehouse / product)
$num = 0;
$alreadyarecord = 0;
if (! $error)
{
$sql = "SELECT rowid, reel FROM ".MAIN_DB_PREFIX."product_stock";
@ -235,7 +235,7 @@ class MouvementStock extends CommonObject
$obj = $this->db->fetch_object($resql);
if ($obj)
{
$num = 1;
$alreadyarecord = 1;
$oldqtywarehouse = $obj->reel;
$fk_product_stock = $obj->rowid;
}
@ -281,7 +281,7 @@ class MouvementStock extends CommonObject
// Update stock quantity
if (! $error)
{
if ($num > 0)
if ($alreadyarecord > 0)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."product_stock SET reel = reel + ".$qty;
$sql.= " WHERE fk_entrepot = ".$entrepot_id." AND fk_product = ".$fk_product;
@ -315,18 +315,14 @@ class MouvementStock extends CommonObject
if ($result<0) $error++;
}
// If stock is now 0, we can remove entry into llx_stock_product, but only if there is no child lines into llx_product_batch (detail of batch, because we can imagine
// having a lot=1, qty=X and lot=2, qty=-X, so 0 but we must not loose repartition of different lot.
// TODO We should do this but not so important because there is already a filter on !=0 on product stock lists.
// Update PMP and denormalized value of stock qty at product level
if (! $error)
{
//$sql = "UPDATE ".MAIN_DB_PREFIX."product SET pmp = ".$newpmp.", stock = ".$this->db->ifsql("stock IS NULL", 0, "stock") . " + ".$qty;
//$sql.= " WHERE rowid = ".$fk_product;
// $sql = "UPDATE ".MAIN_DB_PREFIX."product SET pmp = ".$newpmp.", stock = ".$this->db->ifsql("stock IS NULL", 0, "stock") . " + ".$qty;
// $sql.= " WHERE rowid = ".$fk_product;
// Update pmp + denormalized fields because we change content of produt_stock. Warning: Do not use "SET p.stock", does not works with pgsql
$sql = "UPDATE ".MAIN_DB_PREFIX."product as p SET p.pmp = ".$newpmp.", ";
$sql.= " stock=(SELECT SUM(ps.reel) FROM llx_product_stock ps WHERE ps.fk_product = p.rowid)";
$sql.= " stock=(SELECT SUM(ps.reel) FROM ".MAIN_DB_PREFIX."product_stock ps WHERE ps.fk_product = p.rowid)";
$sql.= " WHERE rowid = ".$fk_product;
print $sql;
dol_syslog(get_class($this)."::_create", LOG_DEBUG);
@ -337,6 +333,12 @@ class MouvementStock extends CommonObject
$error = -4;
}
}
// If stock is now 0, we can remove entry into llx_stock_product, but only if there is no child lines into llx_product_batch (detail of batch, because we can imagine
// having a lot1/qty=X and lot2/qty=-X, so 0 but we must not loose repartition of different lot.
$sql="DELETE FROM ".MAIN_DB_PREFIX."product_stock WHERE reel = 0 AND rowid NOT IN (SELECT fk_product_stock FROM ".MAIN_DB_PREFIX."product_batch as pb)";
$resql=$this->db->query($sql);
// We do not test error, it can fails if there is child in batch details
}
// Add movement for sub products (recursive call)
@ -528,7 +530,7 @@ class MouvementStock extends CommonObject
* Create or update batch record (update table llx_product_batch)
*
* @param array|int $dluo Could be either
* - int if id of product_batch
* - int if row id of product_batch table
* - or complete array('fk_product_stock'=>, 'eatby'=>, 'sellby'=> , 'batchnumber'=>)
* @param int $qty Quantity of product with batch number. May be a negative amount.
* @return int <0 if KO, else return productbatch id