Ajout valorisation PMP pour un produit et pour un entrepot

This commit is contained in:
Rodolphe Quiedeville 2006-12-13 14:20:14 +00:00
parent 93741bb995
commit 28d1f68f57

View File

@ -48,7 +48,7 @@ class MouvementStock
function _create($user, $fk_product, $entrepot_id, $qty, $type, $price=0)
{
$error = 0;
dolibarr_syslog("mouvementstock.class.php::create $user, $fk_product, $entrepot_id, $qty, $type");
dolibarr_syslog("MouvementStock::_Create $user->id, $fk_product, $entrepot_id, $qty, $type, $price");
$this->db->begin();
@ -57,9 +57,9 @@ class MouvementStock
$sql.= " VALUES (now(), $fk_product, $entrepot_id, $qty, $type, $user->id";
$sql.= ",'".ereg_replace(",",".",$price)."');";
if ($this->db->query($sql))
if ($resql = $this->db->query($sql))
{
$mvid = $this->db->last_insert_id($resql);
}
else
{
@ -112,6 +112,17 @@ class MouvementStock
}
if ($error === 0)
{
$valo_mouvement = 0;
$error = $this->CalculateValoPmp($mvid, $fk_product, $qty, $price, $valo_mouvement);
}
if ($error === 0)
{
$error = $this->CalculateEntrepotValoPmp($user, $entrepot_id, $valo_mouvement);
}
if ($error === 0)
{
$this->db->commit();
@ -121,10 +132,196 @@ class MouvementStock
{
$this->db->rollback();
$this->error=$this->db->error() . " - $sql";
dolibarr_syslog("MouvementStock::_Create ERROR : $error");
dolibarr_syslog("MouvementStock::_Create ROLLBACK");
return -2;
}
}
/**
* \brief Crée un mouvement en base
* \return int <0 si ko, >0 si ok
*/
function CalculateEntrepotValoPmp($user, $entrepot_id, $valo_mouvement)
{
$error = 0;
dolibarr_syslog("MouvementStock::CalculateEntrepotValoPmp $user->id, $entrepot_id, $valo_mouvement");
if ( $valo_mouvement <> 0 )
{
$entrepot_value_pmp = 0;
if ($error === 0)
{
$sql = "SELECT valo_pmp,".$this->db->pdate("date_calcul")." FROM ".MAIN_DB_PREFIX."entrepot_valorisation";
$sql.= " WHERE fk_entrepot = $entrepot_id ORDER BY date_calcul DESC LIMIT 1;";
if ($this->db->query($sql))
{
while ($row = $this->db->fetch_row($resql) )
{
$entrepot_value_pmp = $row[0];
$entrepot_value_date = $row[1];
}
$this->db->free($resql);
}
else
{
$error = -26;
dolibarr_syslog("MouvementStock::CalculateEntrepotValoPmp ERRORSQL[$error]");
}
}
$new_value = $entrepot_value_pmp + $valo_mouvement;
$now = time();
if ($error === 0)
{
if ( strftime('%Y%m%d',$entrepot_value_date) == strftime('%Y%m%d',$now) )
{
$sql = "UPDATE ".MAIN_DB_PREFIX."entrepot_valorisation";
$sql.= " SET valo_pmp='".ereg_replace(",",".",$new_value)."'";
$sql.= " WHERE fk_entrepot = $entrepot_id ";
$sql.= " AND ".$this->db->pdate("date_calcul")."='".$entrepot_value_date."';";
}
else
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."entrepot_valorisation";
$sql.= " (date_calcul, fk_entrepot, valo_pmp)";
$sql.= " VALUES (now(), $entrepot_id";
$sql.= ",'".ereg_replace(",",".",$new_value)."');";
}
if ($this->db->query($sql))
{
}
else
{
$error = -27;
dolibarr_syslog("MouvementStock::CalculateEntrepotValoPmp ERRORSQL[$error]");
}
}
if ($error === 0)
{
return 0;
}
else
{
dolibarr_syslog("MouvementStock::CalculateEntrepotValoPmp RETURN IN ERROR[$error]");
return $error;
}
}
else
{
return 0;
}
}
/**
* \brief Crée un mouvement en base
* \param mvid int Id du mouvement
* \param fk_product int Id produit
* \param qty float Quantité
* \param price float Prix unitaire du produit
* \param value_ope float Valeur du mouvement en retour
* \return int <0 si ko, 0 si ok
*/
function CalculateValoPmp($mvid, $fk_product, $qty, $price=0, &$value_ope)
{
$error = 0;
dolibarr_syslog("MouvementStock::CalculateValoPmp $user->id, $fk_product, $qty, $price");
if ( $qty <> 0 )
{
$price_pmp = 0;
$qty_stock = 0;
$stock_value_pmp = 0;
if ($error === 0)
{
$sql = "SELECT price_pmp, qty_stock, valo_pmp FROM ".MAIN_DB_PREFIX."stock_valorisation";
$sql.= " WHERE fk_product = $fk_product ORDER BY date_valo DESC LIMIT 1;";
if ($this->db->query($sql))
{
while ($row = $this->db->fetch_row($resql) )
{
$price_pmp = $row [0];
$qty_stock = $row [1];
$stock_value_pmp = $row [2];
}
$this->db->free($resql);
}
else
{
dolibarr_syslog("MouvementStock::CalculateValoPmp ERRORSQL[1] ".$this->error);
$error = -16;
}
}
/*
* Calcul
*/
if ($qty > 0)
{
// on stock
$new_pmp = ( ($qty * $price) + ($qty_stock * $price_pmp ) ) / ($qty + $qty_stock);
$value_ope = $qty * $price;
$new_stock_qty = $qty_stock + $qty;
$new_stock_value_pmp = $stock_value_pmp + $value_ope;
}
else
{
// on destock
$new_pmp = $price_pmp;
$price = $price_pmp;
$value_ope = $qty * $price_pmp;
}
$new_stock_qty = $qty_stock + $qty;
$new_stock_value_pmp = $stock_value_pmp + $value_ope;
/*
* Fin calcul
*/
if ($error === 0)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."stock_valorisation";
$sql.= " (date_valo, fk_product, fk_stock_mouvement, qty_ope, price_ope, valo_ope, price_pmp, qty_stock, valo_pmp)";
$sql.= " VALUES (now(), $fk_product, $mvid";
$sql.= ",'".ereg_replace(",",".",$qty)."'";
$sql.= ",'".ereg_replace(",",".",$price)."'";
$sql.= ",'".ereg_replace(",",".",$value_ope)."'";
$sql.= ",'".ereg_replace(",",".",$new_pmp)."'";
$sql.= ",'".ereg_replace(",",".",$new_stock_qty)."'";
$sql.= ",'".ereg_replace(",",".",$new_stock_value_pmp)."');";
if ($this->db->query($sql))
{
}
else
{
dolibarr_syslog("MouvementStock::CalculateValoPmp ERRORSQL[2] insert ".$this->error);
$error = -17;
}
}
if ($error === 0)
{
return 0;
}
else
{
dolibarr_syslog("MouvementStock::CalculateValoPmp ERROR : $error");
return -21;
}
}
else
{
return 0;
}
}
/*
*
*