diff --git a/htdocs/fourn/fournisseur.commande.class.php b/htdocs/fourn/fournisseur.commande.class.php index db2841adf45..f8d8ff4f850 100644 --- a/htdocs/fourn/fournisseur.commande.class.php +++ b/htdocs/fourn/fournisseur.commande.class.php @@ -607,6 +607,8 @@ class CommandeFournisseur extends Commande */ function refuse($user) { + global $conf, $langs; + dol_syslog("CommandeFournisseur::Refuse"); $result = 0; if ($user->rights->fournisseur->commande->approuver) @@ -615,21 +617,21 @@ class CommandeFournisseur extends Commande $sql .= " WHERE rowid = ".$this->id; if ($this->db->query($sql) ) - { - $result = 0; - $this->log($user, 9, time()); + { + $result = 0; + $this->log($user, 9, time()); - $subject = $langs->trans("EMailTextOrderRefused",$this->ref); - $message = $langs->trans("Hello").",\n\n"; - $message .= $langs->trans("EMailTextOrderRefusedBy",$this->ref,$user->fullname); + $subject = $langs->trans("EMailTextOrderRefused",$this->ref); + $message = $langs->trans("Hello").",\n\n"; + $message .= $langs->trans("EMailTextOrderRefusedBy",$this->ref,$user->fullname); - $this->_NotifyCreator($user, $subject, $message); - } - else - { - dol_syslog("CommandeFournisseur::Refuse Error -1"); - $result = -1; - } + $this->_NotifyCreator($user, $subject, $message); + } + else + { + dol_syslog("CommandeFournisseur::Refuse Error -1"); + $result = -1; + } } else { diff --git a/htdocs/langs/en_US/stocks.lang b/htdocs/langs/en_US/stocks.lang index 83c46e5361d..a469593374f 100644 --- a/htdocs/langs/en_US/stocks.lang +++ b/htdocs/langs/en_US/stocks.lang @@ -62,4 +62,7 @@ StockUpShort=Stock up IdWarehouse=Id warehouse DescWareHouse=Description warehouse LieuWareHouse=Localisation warehouse -WarehousesAndProducts=Warehouses and products \ No newline at end of file +WarehousesAndProducts=Warehouses and products +AverageUnitPricePMP=Average price +EstimatedStockValueForProduct=Estimated value for product's stock +EstimatedStockValue=Estimated value in stock \ No newline at end of file diff --git a/htdocs/langs/fr_FR/stocks.lang b/htdocs/langs/fr_FR/stocks.lang index 76b633c30ec..1df90c25c72 100644 --- a/htdocs/langs/fr_FR/stocks.lang +++ b/htdocs/langs/fr_FR/stocks.lang @@ -62,4 +62,7 @@ StockUpShort=Stock max IdWarehouse=Id entrepôt DescWareHouse=Description entrepôt LieuWareHouse=Lieu entrepôt -WarehousesAndProducts=Entrepôts et produits \ No newline at end of file +WarehousesAndProducts=Entrepôts et produits +AverageUnitPricePMP=Prix moyen pondéré (PMP) +EstimatedStockValueForProduct=Valeur estimée du produit en stock +EstimatedStockValue=Valeur estimée du stock \ No newline at end of file diff --git a/htdocs/product.class.php b/htdocs/product.class.php index fcb873f95cc..ec7645f58da 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -38,6 +38,8 @@ class Product extends CommonObject { var $db; var $error; + //! Numero de l'erreur + var $errno = 0; var $element='product'; var $table_element='product'; @@ -63,7 +65,12 @@ class Product extends CommonObject //! Type 0 for regular product, 1 for service, 2 for assembly kit, 3 for stock kit var $type; var $typestring; + + //! Stock var $stock_reel; + //! Stock value (PMP) + var $pmp; + var $seuil_stock_alerte; //! Duree de validite du service var $duration_value; @@ -97,9 +104,6 @@ class Product extends CommonObject var $imgWidth; var $imgHeight; - //! Numero de l'erreur - //! Numero d'erreur Plage 0256-0511 - var $errno = 0; //! Canevas a utiliser si le produit n'est pas un produit generique var $canvas; @@ -438,9 +442,9 @@ class Product extends CommonObject { $num = $this->db->num_rows($result); if ($num != 0) - { - $sqr++; - } + { + $sqr++; + } } $sql = "SELECT rowid"; @@ -452,9 +456,9 @@ class Product extends CommonObject { $num = $this->db->num_rows($result); if ($num != 0) - { - $sqr++; - } + { + $sqr++; + } } @@ -467,9 +471,9 @@ class Product extends CommonObject { $num = $this->db->num_rows($result); if ($num != 0) - { - $sqr++; - } + { + $sqr++; + } } @@ -482,9 +486,9 @@ class Product extends CommonObject { $num = $this->db->num_rows($result); if ($num != 0) - { - $sqr++; - } + { + $sqr++; + } } if ($sqr == 0) @@ -875,10 +879,10 @@ class Product extends CommonObject /** - * \brief Charge le produit/service en memoire - * \param id Id du produit/service a charger - * \param ref Ref du produit/service a charger - * \return int <0 si ko, >0 si ok + * \brief Load a product in memory from database + * \param id Id of product/service to load + * \param ref Ref of product/service to load + * \return int <0 if KO, >0 if OK */ function fetch($id='',$ref='') { @@ -898,7 +902,9 @@ class Product extends CommonObject $sql = "SELECT rowid, ref, label, description, note, price, price_ttc,"; $sql.= " price_min, price_min_ttc, price_base_type, tva_tx, envente,"; $sql.= " fk_product_type, duration, seuil_stock_alerte,canvas,"; - $sql.= " stock_loc, weight, weight_units, volume, volume_units, barcode, fk_barcode_type, finished"; + $sql.= " stock_loc, weight, weight_units, volume, volume_units, barcode, fk_barcode_type, finished,"; + $sql.= " stock, pmp,"; + $sql.= " import_key"; $sql.= " FROM ".MAIN_DB_PREFIX."product"; if ($id) $sql.= " WHERE rowid = '".$id."'"; if ($ref) $sql.= " WHERE ref = '".addslashes($ref)."'"; @@ -936,9 +942,12 @@ class Product extends CommonObject $this->barcode = $result["barcode"]; $this->barcode_type = $result["fk_barcode_type"]; - $this->next_prev_filter = 'entity = '.$conf->entity; + $this->stock_reel = $result["stock"]; + $this->pmp = $result["pmp"]; - $this->label_url = ''.$this->libelle.''; + $this->import_key = $result["import_key"]; + + $this->next_prev_filter = 'entity = '.$conf->entity; $this->db->free(); @@ -958,10 +967,10 @@ class Product extends CommonObject $sql = "SELECT code, libelle, coder"; $sql.= " FROM ".MAIN_DB_PREFIX."c_barcode_type"; $sql.= " WHERE rowid = ".$this->barcode_type; - $result = $this->db->query($sql); - if ($result) + $resql = $this->db->query($sql); + if ($resql) { - $result = $this->db->fetch_array(); + $result = $this->db->fetch_array($resql); $this->barcode_type_code = $result["code"]; $this->barcode_type_label = $result["libelle"]; $this->barcode_type_coder = $result["coder"]; @@ -985,10 +994,10 @@ class Product extends CommonObject $sql.= " where price_level=".$i." and"; $sql.= " fk_product = '".$this->id."'"; $sql.= " order by date_price DESC limit 1"; - $result = $this->db->query($sql) ; - if ( $result ) + $resql = $this->db->query($sql) ; + if ($resql) { - $result = $this->db->fetch_array(); + $result = $this->db->fetch_array($resql); $this->multiprices[$i]=$result["price"]; $this->multiprices_ttc[$i]=$result["price_ttc"]; @@ -1034,7 +1043,7 @@ class Product extends CommonObject if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE p.rowid = pd.fk_propal"; $sql.= " AND p.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND s.entity = ".$conf->entity; $sql.= " AND pd.fk_product = ".$this->id; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; //$sql.= " AND pr.fk_statut != 0"; @@ -1076,7 +1085,7 @@ class Product extends CommonObject if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE c.rowid = cd.fk_commande"; $sql.= " AND c.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND s.entity = ".$conf->entity; $sql.= " AND cd.fk_product = ".$this->id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; if ($socid > 0) $sql.= " AND c.fk_soc = ".$socid; @@ -1117,7 +1126,7 @@ class Product extends CommonObject if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE c.rowid = cd.fk_commande"; $sql.= " AND c.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND s.entity = ".$conf->entity; $sql.= " AND cd.fk_product = ".$this->id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; if ($socid > 0) $sql.= " AND c.fk_soc = ".$socid; @@ -1159,7 +1168,7 @@ class Product extends CommonObject if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE e.rowid = ed.fk_expedition"; $sql.= " AND e.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND s.entity = ".$conf->entity; $sql.= " AND ed.fk_origin_line = cd.rowid"; $sql.= " AND cd.fk_product = ".$this->id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND e.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; @@ -1201,7 +1210,7 @@ class Product extends CommonObject if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE c.rowid = cd.fk_contrat"; $sql.= " AND c.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND s.entity = ".$conf->entity; $sql.= " AND cd.fk_product = ".$this->id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; //$sql.= " AND c.statut != 0"; @@ -1242,7 +1251,7 @@ class Product extends CommonObject if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE f.rowid = fd.fk_facture"; $sql.= " AND f.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND s.entity = ".$conf->entity; $sql.= " AND fd.fk_product = ".$this->id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; //$sql.= " AND f.fk_statut != 0"; @@ -1283,7 +1292,7 @@ class Product extends CommonObject if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql.= " WHERE f.rowid = fd.fk_facture_fourn"; $sql.= " AND f.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND s.entity = ".$conf->entity; $sql.= " AND fd.fk_product = ".$this->id; if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; //$sql.= " AND f.fk_statut != 0"; @@ -1852,13 +1861,13 @@ class Product extends CommonObject ".$compl_path.stripslashes($nom_pere)." (".$desc_pere[1].") ".$trueValue."   ".$this->stock_entrepot[1]." ".$img."", - $desc_pere[0], + $desc_pere[0], 'fullpath' => $compl_path.$nom_pere); } else { $this->res[]= array($compl_path.$nom_pere." (".$desc_pere[1].")", - $desc_pere[0], + $desc_pere[0], 'fullpath' => $compl_path.$nom_pere); } } @@ -1909,7 +1918,7 @@ class Product extends CommonObject { if (is_array($desc_pere)) $this->fetch_prod_arbo($desc_pere,"",$multiply); } -// dol_sort($this->res,); + // dol_sort($this->res,); } return $this->res; } diff --git a/htdocs/product/stock/mouvementstock.class.php b/htdocs/product/stock/mouvementstock.class.php index 050e4904911..94e344ad043 100644 --- a/htdocs/product/stock/mouvementstock.class.php +++ b/htdocs/product/stock/mouvementstock.class.php @@ -39,19 +39,32 @@ class MouvementStock /** * \brief Add a movement in stock (in one direction only) - * \param type Direction of movement: 2=output (stock decrease), 3=input (stock increase) - * \return int <0 if KO, >0 if OK + * \param user User object + * \param fk_product Id of product + * \param entrepot_id Id of warehouse + * \param qty Qty of movement + * \param type Direction of movement: 2=output (stock decrease), 3=input (stock increase) + * \param type Unit price HT of product + * \return int <0 if KO, >0 if OK */ function _create($user, $fk_product, $entrepot_id, $qty, $type, $price=0) { global $conf; $error = 0; - dol_syslog("MouvementStock::_create $user->id, $fk_product, $entrepot_id, qty=$qty, type=$type, $price"); + dol_syslog("MouvementStock::_create start userid=$user->id, fk_product=$fk_product, warehouse=$entrepot_id, qty=$qty, type=$type, price=$price"); $this->db->begin(); - if (1 == 1) // Always change stock for current product, change for subproduct done after + $product = new Product($this->db); + $result=$product->fetch($fk_product); + if (! $result > 0) + { + dol_print_error('',"Failed to fetch product"); + return -1; + } + + if (1 == 1) // Always change stock for current product, change for subproduct is done after { $sql = "INSERT INTO ".MAIN_DB_PREFIX."stock_mouvement"; $sql.= " (datem, fk_product, fk_entrepot, value, type_mouvement, fk_user_author, price)"; @@ -65,98 +78,117 @@ class MouvementStock } else { + $this->error=$this->db->lasterror(); dol_syslog("MouvementStock::_create ".$this->error, LOG_ERR); $error = -1; } + // Define current values for qty and pmp + $oldqty=$product->stock_reel; + $oldqtywarehouse=0; + $oldpmp=$product->pmp; + $oldpmpwarehouse=0; + // Test if there is already a record for couple (warehouse / product) $num = 0; - if ($error == 0) + if (! $error) { - $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_stock"; + $sql = "SELECT rowid, reel, pmp FROM ".MAIN_DB_PREFIX."product_stock"; $sql.= " WHERE fk_entrepot = ".$entrepot_id." AND fk_product = ".$fk_product; + dol_syslog("MouvementStock::_create sql=".$sql); if ($this->db->query($sql)) { - $num = $this->db->num_rows($resql); + $obj = $this->db->fetch_object($resql); + if ($obj) + { + $num = 1; + $oldqtywarehouse = $obj->reel; + $oldpmpwarehouse = $obj->pmp; + } $this->db->free($resql); } else { + $this->error=$this->db->lasterror(); dol_syslog("MouvementStock::_create echec update ".$this->error, LOG_ERR); $error = -2; } } + // Calculate new PMP + if (! $error) + { + $newpmp=0; + $newpmpwarehouse=0; + if ($price > 0) + { + if ($oldqty > 0 && $oldpmp > 0) $newpmp=($oldqty * $oldpmp + $qty * $price) / ($oldqty + $qty); + else $newpmp=$price; + if ($oldqtywarehouse > 0 && $oldpmpwarehouse > 0) $newpmpwarehouse=($oldqtywarehouse * $oldpmpwarehouse + $qty * $price) / ($oldqtywarehouse + $qty); + else $newpmpwarehouse=$price; + } + else + { + $newpmp = $oldpmp; + $newpmpwarehouse = $oldpmpwarehouse; + } + } + // Update denormalized value of stock in product_stock and product - if ($error == 0) + if (! $error) { if ($num > 0) { - $sql = "UPDATE ".MAIN_DB_PREFIX."product_stock SET reel = reel + ".$qty; + $sql = "UPDATE ".MAIN_DB_PREFIX."product_stock SET pmp = ".$newpmpwarehouse.", reel = reel + ".$qty; $sql.= " WHERE fk_entrepot = ".$entrepot_id." AND fk_product = ".$fk_product; } else { $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_stock"; - $sql.= " (reel, fk_entrepot, fk_product) VALUES "; - $sql.= " (".$qty.",".$entrepot_id.",".$fk_product.")"; + $sql.= " (pmp, reel, fk_entrepot, fk_product) VALUES "; + $sql.= " (".$newpmpwarehouse.", ".$qty.", ".$entrepot_id.", ".$fk_product.")"; } - dol_syslog("MouvementStock::_create sql=".$sql, LOG_DEBUG); - if ($this->db->query($sql)) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."product SET stock = stock + ".$qty; - $sql.= " WHERE rowid = ".$fk_product; - - dol_syslog("MouvementStock::_create sql=".$sql, LOG_DEBUG); - if ($this->db->query($sql)) - { - - // TODO - // Update value of PMP in product_stock - } - else - { - dol_syslog("MouvementStock::_create ".$this->error, LOG_ERR); - $error = -4; - } - - } - else + dol_syslog("MouvementStock::_create sql=".$sql); + $resql=$this->db->query($sql); + if (! $resql) { + $this->error=$this->db->lasterror(); dol_syslog("MouvementStock::_create ".$this->error, LOG_ERR); $error = -3; } } - /* - if ($error == 0) + if (! $error) { - $valo_mouvement = 0; - $error = $this->CalculateValoPmp($mvid, $fk_product, $qty, $price, $valo_mouvement); - } + $sql = "UPDATE ".MAIN_DB_PREFIX."product SET pmp = ".$newpmp.", stock = stock + ".$qty; + $sql.= " WHERE rowid = ".$fk_product; - if ($error == 0) - { - $error = $this->CalculateEntrepotValoPmp($user, $entrepot_id, $valo_mouvement); + dol_syslog("MouvementStock::_create sql=".$sql); + $resql=$this->db->query($sql); + if (! $resql) + { + $this->error=$this->db->lasterror(); + dol_syslog("MouvementStock::_create ".$this->error, LOG_ERR); + $error = -4; + } } - */ } // Add movement for sub products - if ($error == 0 && $conf->global->PRODUIT_SOUSPRODUITS) + if (! $error && $conf->global->PRODUIT_SOUSPRODUITS) { - $error = $this->_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, $price=0); + $error = $this->_createSubProduct($user, $fk_product, $entrepot_id, $qty, $type, 0); // pmp is not change for subproduct } // composition module - if ($error == 0 && $qty < 0 && $conf->global->MAIN_MODULE_COMPOSITION) + if (! $error && $qty < 0 && $conf->global->MAIN_MODULE_COMPOSITION) { - $error = $this->_createProductComposition($user, $fk_product, $entrepot_id, $qty, $type, $price=0); + $error = $this->_createProductComposition($user, $fk_product, $entrepot_id, $qty, $type, 0); // pmp is not change for subproduct } - if ($error == 0) + if (! $error) { $this->db->commit(); return 1; @@ -164,9 +196,8 @@ class MouvementStock else { $this->db->rollback(); - $this->error=$this->db->lasterror(); - dol_syslog("MouvementStock::_create ".$this->error, LOG_ERR); - return -2; + dol_syslog("MouvementStock::_create error code=".$error, LOG_ERR); + return -6; } } @@ -215,7 +246,7 @@ class MouvementStock /** - * \brief Crée un mouvement en base pour toutes les compositions de produits + * \brief Cr�e un mouvement en base pour toutes les compositions de produits * \return int <0 si ko, 0 si ok */ function _createProductComposition($user, $fk_product, $entrepot_id, $qty, $type, $price=0) @@ -293,211 +324,5 @@ class MouvementStock return $nbSP; } - /** - * \brief Calcul ??? - * \return int <0 si ko, >0 si ok - */ - /* - function CalculateEntrepotValoPmp($user, $entrepot_id, $valo_mouvement) - { - $error = 0; - dol_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; - dol_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='".price2num($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 (".$this->db->idate(mktime()).", ".$entrepot_id; - $sql.= ",'".price2num($new_value)."')"; - } - - if ($this->db->query($sql)) - { - - } - else - { - $error = -27; - dol_syslog("MouvementStock::CalculateEntrepotValoPmp ERRORSQL[$error]"); - } - } - - if ($error === 0) - { - $sql = "UPDATE ".MAIN_DB_PREFIX."entrepot"; - $sql.= " SET valo_pmp='".price2num($new_value)."'"; - $sql.= " WHERE rowid = $entrepot_id "; - - if ($this->db->query($sql)) - { - - } - else - { - $error = -28; - dol_syslog("MouvementStock::CalculateEntrepotValoPmp ERRORSQL[$error]"); - } - } - - if ($error === 0) - { - return 0; - } - else - { - dol_syslog("MouvementStock::CalculateEntrepotValoPmp RETURN IN ERROR[$error]"); - return $error; - } - } - else - { - return 0; - } - } - */ - - /** - * \brief ??? - * \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; - dol_syslog("MouvementStock::CalculateValoPmp $mvid, $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 - { - dol_syslog("MouvementStock::CalculateValoPmp ERRORSQL[1] ".$this->error, LOG_ERR); - $error = -16; - } - } - - // Calcul - if ($qty > 0) - { - // on stock - if (($qty + $qty_stock) <> 0) - $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 (".$this->db->idate(mktime()).", $fk_product, $mvid"; - $sql.= ",'".price2num($qty)."'"; - $sql.= ",'".price2num($price)."'"; - $sql.= ",'".price2num($value_ope)."'"; - $sql.= ",'".price2num($new_pmp)."'"; - $sql.= ",'".price2num($new_stock_qty)."'"; - $sql.= ",'".price2num($new_stock_value_pmp)."')"; - - if ($this->db->query($sql)) - { - - } - else - { - dol_syslog("MouvementStock::CalculateValoPmp ERRORSQL[2] insert ".$this->error, LOG_ERR); - $error = -17; - } - } - - if ($error === 0) - { - return 0; - } - else - { - dol_syslog("MouvementStock::CalculateValoPmp ERROR : $error"); - return -21; - } - } - else - { - return 0; - } - } - */ } ?> diff --git a/htdocs/product/stock/product.php b/htdocs/product/stock/product.php index e81f11ad60d..76d6cf214c5 100644 --- a/htdocs/product/stock/product.php +++ b/htdocs/product/stock/product.php @@ -1,6 +1,6 @@ - * Copyright (C) 2004-2008 Laurent Destailleur + * Copyright (C) 2004-2009 Laurent Destailleur * Copyright (C) 2004 Eric Seigne * Copyright (C) 2005 Simon TOSSER * Copyright (C) 2005-2009 Regis Houssin @@ -22,8 +22,8 @@ /** * \file htdocs/product/stock/product.php - * \ingroup product - * \brief Page de la fiche stock d'un produit + * \ingroup product, stock + * \brief Page to list detailed stock of a product * \version $Id$ */ @@ -350,9 +350,12 @@ print ''; */ print '
'; print ''; -print ''; +print ''; +print ''; +print ''; +print ''; -$sql = "SELECT e.rowid, e.label, ps.reel"; +$sql = "SELECT e.rowid, e.label, ps.reel, ps.pmp"; $sql.= " FROM ".MAIN_DB_PREFIX."entrepot as e"; $sql.= ", ".MAIN_DB_PREFIX."product_stock as ps"; $sql.= " WHERE ps.reel != 0"; @@ -362,6 +365,8 @@ $sql.= " AND ps.fk_product = ".$product->id; $sql.= " ORDER BY lower(e.label)"; $entrepotstatic=new Entrepot($db); +$total=0; +$totalvalue=0; $resql=$db->query($sql); if ($resql) @@ -376,13 +381,20 @@ if ($resql) print ''; print ''; print ''; + print ''; + print ''; print ''; ; $total = $total + $obj->reel; + $totalvalue = $totalvalue + price2num($obj->pmp,'MU')*$obj->reel; $i++; $var=!$var; } } -print '"; +print ''; +print ''; +print ''; +print ''; +print ""; print "
'.$langs->trans("Warehouse").''.$langs->trans("NumberOfUnit").'
'.$langs->trans("NumberOfUnit").''.$langs->trans("AverageUnitPricePMP").''.$langs->trans("EstimatedStockValue").'
'.$entrepotstatic->getNomUrl(1).''.$obj->reel.''.price2num($obj->pmp,'MU').''.price(price2num($obj->pmp,'MU')*$obj->reel).'
'.$langs->trans("Total").':'.$total."
'.$langs->trans("Total").':'.$total.' '.price($totalvalue).'
"; diff --git a/mysql/migration/2.6.0-2.7.0.sql b/mysql/migration/2.6.0-2.7.0.sql index c67fea64a93..431f75be50e 100644 --- a/mysql/migration/2.6.0-2.7.0.sql +++ b/mysql/migration/2.6.0-2.7.0.sql @@ -6,6 +6,9 @@ -- when current version is 2.6.0 or higher. -- +drop table llx_stock_valorisation; +drop table llx_entrepot_valorisation; + update llx_actioncomm set datep = datec where datep is null and datec is not null; -- Create new table for import module diff --git a/mysql/tables/llx_entrepot_valorisation.sql b/mysql/tables/llx_entrepot_valorisation.sql deleted file mode 100644 index d5c4c4472fc..00000000000 --- a/mysql/tables/llx_entrepot_valorisation.sql +++ /dev/null @@ -1,29 +0,0 @@ --- ============================================================================ --- Copyright (C) 2006 Rodolphe Quiedeville --- --- 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 --- the Free Software Foundation; either version 2 of the License, or --- (at your option) any later version. --- --- This program is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License --- along with this program; if not, write to the Free Software --- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --- --- $Id$ --- ============================================================================ - -create table llx_entrepot_valorisation -( - rowid integer AUTO_INCREMENT PRIMARY KEY, - tms timestamp, -- date technique mise à jour automatiquement - date_calcul date, -- date auquel a ete calcule la valeur - fk_entrepot integer UNSIGNED NOT NULL, - valo_pmp float(12,4) -- valoristaion du stock en PMP -)type=innodb; - diff --git a/mysql/tables/llx_stock_valorisation.key.sql b/mysql/tables/llx_stock_valorisation.key.sql deleted file mode 100644 index 049d8419b24..00000000000 --- a/mysql/tables/llx_stock_valorisation.key.sql +++ /dev/null @@ -1,24 +0,0 @@ --- ============================================================================ --- Copyright (C) 2002-2004 Rodolphe Quiedeville --- Copyright (C) 2004-2005 Laurent Destailleur --- --- 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 --- the Free Software Foundation; either version 2 of the License, or --- (at your option) any later version. --- --- This program is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License --- along with this program; if not, write to the Free Software --- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --- --- $Id$ --- ============================================================================ - - -ALTER TABLE llx_stock_valorisation ADD INDEX idx_stock_valorisation_fk_product (fk_product); - diff --git a/mysql/tables/llx_stock_valorisation.sql b/mysql/tables/llx_stock_valorisation.sql deleted file mode 100644 index 1dbe72b591c..00000000000 --- a/mysql/tables/llx_stock_valorisation.sql +++ /dev/null @@ -1,35 +0,0 @@ --- ============================================================================ --- Copyright (C) 2006 Rodolphe Quiedeville --- --- 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 --- the Free Software Foundation; either version 2 of the License, or --- (at your option) any later version. --- --- This program is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License --- along with this program; if not, write to the Free Software --- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --- --- $Id$ --- ============================================================================ - -create table llx_stock_valorisation -( - rowid integer AUTO_INCREMENT PRIMARY KEY, - tms timestamp, -- date technique mise à jour automatiquement - date_valo datetime, -- date de valorisation - fk_product integer NOT NULL, -- id du produit concerne par l'operation - qty_ope float(9,3), -- quantité de l'operation - price_ope float(12,4), -- prix unitaire du produit concerne par l'operation - valo_ope float(12,4), -- valorisation de l'operation - price_pmp float(12,4), -- valeur PMP de l'operation - qty_stock float(9,3) DEFAULT 0, -- qunatite en stock - valo_pmp float(12,4), -- valorisation du stock en PMP - fk_stock_mouvement integer -- id du mouvement de stock -)type=innodb; -