From fe276431525ef5cac39657d8a48b893810093826 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 20 Aug 2011 23:56:03 +0000 Subject: [PATCH] New: task #11243: Add best supplier price --- ChangeLog | 5 +- .../fourn/class/fournisseur.product.class.php | 997 ++++++++++-------- htdocs/fourn/commande/fiche.php | 24 +- htdocs/langs/en_US/suppliers.lang | 1 + htdocs/langs/fr_FR/suppliers.lang | 1 + .../product/canvas/default/tpl/list.tpl.php | 4 +- htdocs/product/fournisseurs.php | 86 +- htdocs/product/liste.php | 34 +- htdocs/user/class/user.class.php | 8 +- 9 files changed, 665 insertions(+), 495 deletions(-) diff --git a/ChangeLog b/ChangeLog index fab8fbb40ef1f..1e2a7c68edb3d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,6 @@ -------------------------------------------------------------- English Dolibarr ChangeLog -$Id: ChangeLog,v 1.426 2011/08/17 16:21:22 simnandez Exp $ +$Id: ChangeLog,v 1.427 2011/08/20 23:57:09 eldy Exp $ -------------------------------------------------------------- ***** ChangeLog for 3.2 compared to 3.1 ***** @@ -9,9 +9,12 @@ For users: - New: task #11243: Show quantity into stocks for each subproducts into the subproduct tab - New: Task #10500: Option to choose if professionnal id are unique - New: Add hide option FOURN_PRODUCT_AVAILABILITY +- New: task #11123: Add best supplier price For developers: - Qual: Removed no more used external libraries +- Qual: Clean a lot of dead code + ***** ChangeLog for 3.1 compared to 3.0 ***** WARNING: IE6 browser is no more supported in this version. diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index 2c1e5a768d291..c4e1436416569 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -1,8 +1,8 @@ - * Copyright (C) 2006 Laurent Destailleur - * Copyright (C) 2009 Regis Houssin - * Copyright (C) 2011 Juanjo Menent +/* Copyright (C) 2005 Rodolphe Quiedeville + * Copyright (C) 2006-2011 Laurent Destailleur + * Copyright (C) 2009 Regis Houssin + * Copyright (C) 2011 Juanjo Menent * * 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 @@ -22,7 +22,7 @@ * \file htdocs/fourn/class/fournisseur.product.class.php * \ingroup produit * \brief File of class to manage predefined suppliers products - * \version $Id: fournisseur.product.class.php,v 1.9 2011/08/18 08:01:07 simnandez Exp $ + * \version $Id: fournisseur.product.class.php,v 1.10 2011/08/20 23:56:03 eldy Exp $ */ require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php"; @@ -35,432 +35,573 @@ */ class ProductFournisseur extends Product { - var $db ; - - var $id ; - var $fourn_ref; - var $fourn; - var $fourn_qty; - var $product_fourn_id; - var $product_fourn_price_id; - var $fk_availability; - - function ProductFournisseur($db) - { - $this->db = $db; - } - - - - /** - * Remove all prices for this couple supplier-product - * @param id_fourn Supplier Id - * @return int < 0 if error, > 0 if ok - */ - function remove_fournisseur($id_fourn) - { - $ok=1; - - $this->db->begin(); - - // Search all links - $sql = "SELECT rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn; - - dol_syslog("ProductFournisseur::remove_fournisseur sql=".$sql); - $resql=$this->db->query($sql); - if ($resql) - { - // For each link, delete price line - while ($obj=$this->db->fetch_object($resql)) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; - $sql.= " WHERE fk_product_fournisseur = ".$obj->rowid; - - dol_syslog("ProductFournisseur::remove_fournisseur sql=".$sql); - $resql2=$this->db->query($sql); - if (! $resql2) - { - $this->error=$this->db->lasterror(); - dol_syslog("ProductFournisseur::remove_fournisseur ".$this->error, LOG_ERR); - $ok=0; - } - } - - // Now delete all link supplier-product (they have no more childs) - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn; - - dol_syslog("ProductFournisseur::remove_fournisseur sql=".$sql); - $resql=$this->db->query($sql); - if (! $resql) - { - $this->error=$this->db->lasterror(); - dol_syslog("ProductFournisseur::remove_fournisseur ".$this->error, LOG_ERR); - $ok=0; - } - - if ($ok) - { - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - return -1; - } - } - else - { - $this->db->rollback(); - dol_print_error($this->db); - return -2; - } - } - - - /** - * Remove supplier product - * @param rowid Product id - * @return int < 0 if error, > 0 if ok - */ - function remove_product_fournisseur($rowid) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE rowid = ".$rowid; - - dol_syslog("ProductFournisseur::remove_product_fournisseur sql=".$sql); - $resql = $this->db->query($sql); - if ($resql) - { - return 1; - } - else - { - return -1; - } - } - - /** - * Remove a price for a couple supplier-product - * @param rowid Line id of price - * @return int <0 if KO, >0 if OK - */ - function remove_product_fournisseur_price($rowid) - { - global $conf; - - $this->db->begin(); - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; - $sql.= " WHERE rowid = ".$rowid; - - dol_syslog("ProductFournisseur::remove_product_fournisseur_price sql=".$sql); - $resql = $this->db->query($sql); - if ($resql) - { - // Remove all entries with no childs - $sql = "SELECT pf.rowid"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur as pf"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON pfp.fk_product_fournisseur = pf.rowid"; - $sql.= " WHERE pfp.rowid IS NULL"; - $sql.= " AND pf.entity = ".$conf->entity; - - dol_syslog("ProductFournisseur::remove_product_fournisseur_price sql=".$sql); - $resql = $this->db->query($sql); - if ($resql) - { - $ok=1; - - while ($obj=$this->db->fetch_object($resql)) - { - $rowidpf=$obj->rowid; - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE rowid = ".$rowidpf; - - dol_syslog("ProductFournisseur::remove_product_fournisseur_price sql=".$sql); - $resql2 = $this->db->query($sql); - if (! $resql2) - { - $this->error=$this->db->lasterror(); - dol_syslog("ProductFournisseur::remove_product_fournisseur_price ".$this->error,LOG_ERR); - $ok=0; - } - } - - if ($ok) - { - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - return -3; - } - } - else - { - $this->error=$this->db->lasterror(); - dol_syslog("ProductFournisseur::remove_product_fournisseur_price ".$this->error,LOG_ERR); - $this->db->rollback(); - return -2; - } - } - else - { - $this->error=$this->db->lasterror(); - dol_syslog("ProductFournisseur::remove_product_fournisseur_price ".$this->error,LOG_ERR); - $this->db->rollback(); - return -1; - } - } - - - /** - * Modify the purchase price for a supplier - * @param ref Supplier ref - * @param qty Min quantity for which price is valid - * @param buyprice Purchase price for the quantity min - * @param user Object user user made changes - */ - function update($ref, $qty, $buyprice, $user) - { - $this->fourn_ref = $ref; - - /* Mise a jour du prix */ - - $this->update_buyprice($qty, $buyprice, $user); - - /* Mise a jour de la reference */ - - $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur "; - $sql .= " SET ref_fourn = '" . $this->fourn_ref ."'"; - $sql .= " WHERE fk_product = " . $this->id; - $sql .=" AND fk_soc = ".$this->fourn->id; - - $resql = $this->db->query($sql) ; - } - - - /** - * Modify the purchase price for a supplier - * @param qty Min quantity for which price is valid - * @param buyprice Purchase price for the quantity min - * @param user Object user user made changes - * @param price_base_type HT or TTC - * @param fourn Supplier - * @param availability Product availability - */ - function update_buyprice($qty, $buyprice, $user, $price_base_type='HT', $fourn,$availability) - { - global $mysoc; - - $buyprice=price2num($buyprice); - $qty=price2num($qty); - - $error=0; - $this->db->begin(); - - // Supprime prix courant du fournisseur pour cette quantite - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; - if ($this->product_fourn_price_id) - { - $sql.= " WHERE rowid = ".$this->product_fourn_price_id; - } - else - { - $sql.= " WHERE fk_product_fournisseur = ".$this->product_fourn_id; - $sql.= " AND quantity = ".$qty; - } - - $resql=$this->db->query($sql); - if ($resql) - { - if ($price_base_type == 'TTC') - { - $ttx = get_default_tva($fourn,$mysoc,$this->id); - $buyprice = $buyprice/(1+($ttx/100)); - } - $unitBuyPrice = price2num($buyprice/$qty,'MU'); - - $now=dol_now(); - - // Ajoute prix courant du fournisseur pour cette quantite - $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price("; - $sql.= "datec, fk_product_fournisseur, fk_user, price, quantity, unitprice, fk_availability)"; - $sql.= " values('".$this->db->idate($now)."',"; - $sql.= " ".$this->product_fourn_id.","; - $sql.= " ".$user->id.","; - $sql.= " ".price2num($buyprice).","; - $sql.= " ".$qty.","; - $sql.= " ".$unitBuyPrice.","; - $sql.= " ".$availability; + var $db; + var $error; + + var $product_fourn_price_id; // id of ligne product-supplier + + var $id; // product id + var $fourn_ref; // ref supplier + var $fourn_qty; // quantity for price + var $fourn_price; // price for quantity + var $product_fourn_id; // supplier id + var $fk_availability; // availability delay + var $fourn_unitprice; + + + /** + * Constructor + */ + function ProductFournisseur($db) + { + $this->db = $db; + } + + + + /** + * Remove all prices for this couple supplier-product + * + * @param id_fourn Supplier Id + * @return int < 0 if error, > 0 if ok + */ + function remove_fournisseur($id_fourn) + { + $ok=1; + + $this->db->begin(); + + // Search all links + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn; + + dol_syslog("ProductFournisseur::remove_fournisseur sql=".$sql); + $resql=$this->db->query($sql); + if ($resql) + { + // For each link, delete price line + while ($obj=$this->db->fetch_object($resql)) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql.= " WHERE fk_product_fournisseur = ".$obj->rowid; + + dol_syslog("ProductFournisseur::remove_fournisseur sql=".$sql); + $resql2=$this->db->query($sql); + if (! $resql2) + { + $this->error=$this->db->lasterror(); + dol_syslog("ProductFournisseur::remove_fournisseur ".$this->error, LOG_ERR); + $ok=0; + } + } + + // Now delete all link supplier-product (they have no more childs) + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn; + + dol_syslog("ProductFournisseur::remove_fournisseur sql=".$sql); + $resql=$this->db->query($sql); + if (! $resql) + { + $this->error=$this->db->lasterror(); + dol_syslog("ProductFournisseur::remove_fournisseur ".$this->error, LOG_ERR); + $ok=0; + } + + if ($ok) + { + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -1; + } + } + else + { + $this->db->rollback(); + dol_print_error($this->db); + return -2; + } + } + + + /** + * Remove supplier product + * + * @param rowid Product id + * @return int < 0 if error, > 0 if ok + */ + function remove_product_fournisseur($rowid) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE rowid = ".$rowid; + + dol_syslog("ProductFournisseur::remove_product_fournisseur sql=".$sql); + $resql = $this->db->query($sql); + if ($resql) + { + return 1; + } + else + { + return -1; + } + } + + /** + * Remove a price for a couple supplier-product + * + * @param rowid Line id of price + * @return int <0 if KO, >0 if OK + */ + function remove_product_fournisseur_price($rowid) + { + global $conf; + + $this->db->begin(); + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql.= " WHERE rowid = ".$rowid; + + dol_syslog("ProductFournisseur::remove_product_fournisseur_price sql=".$sql); + $resql = $this->db->query($sql); + if ($resql) + { + // Remove all entries with no childs + $sql = "SELECT pf.rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur as pf"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON pfp.fk_product_fournisseur = pf.rowid"; + $sql.= " WHERE pfp.rowid IS NULL"; + $sql.= " AND pf.entity = ".$conf->entity; + + dol_syslog("ProductFournisseur::remove_product_fournisseur_price sql=".$sql); + $resql = $this->db->query($sql); + if ($resql) + { + $ok=1; + + while ($obj=$this->db->fetch_object($resql)) + { + $rowidpf=$obj->rowid; + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE rowid = ".$rowidpf; + + dol_syslog("ProductFournisseur::remove_product_fournisseur_price sql=".$sql); + $resql2 = $this->db->query($sql); + if (! $resql2) + { + $this->error=$this->db->lasterror(); + dol_syslog("ProductFournisseur::remove_product_fournisseur_price ".$this->error,LOG_ERR); + $ok=0; + } + } + + if ($ok) + { + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -3; + } + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog("ProductFournisseur::remove_product_fournisseur_price ".$this->error,LOG_ERR); + $this->db->rollback(); + return -2; + } + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog("ProductFournisseur::remove_product_fournisseur_price ".$this->error,LOG_ERR); + $this->db->rollback(); + return -1; + } + } + + + /** + * Modify the purchase price for a supplier + * + * @param qty Min quantity for which price is valid + * @param buyprice Purchase price for the quantity min + * @param user Object user user made changes + * @param price_base_type HT or TTC + * @param fourn Supplier + * @param availability Product availability + */ + function update_buyprice($qty, $buyprice, $user, $price_base_type='HT', $fourn, $availability) + { + global $mysoc; + + // Clean parameter + $buyprice=price2num($buyprice); + $qty=price2num($qty); + if (empty($availability)) $availability=0; + + $error=0; + $this->db->begin(); + + // Supprime prix courant du fournisseur pour cette quantite + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + if ($this->product_fourn_price_id) + { + $sql.= " WHERE rowid = ".$this->product_fourn_price_id; + } + else + { + $sql.= " WHERE fk_product_fournisseur = ".$this->product_fourn_id; + $sql.= " AND quantity = ".$qty; + } + + $resql=$this->db->query($sql); + if ($resql) + { + if ($price_base_type == 'TTC') + { + $ttx = get_default_tva($fourn,$mysoc,$this->id); + $buyprice = $buyprice/(1+($ttx/100)); + } + $unitBuyPrice = price2num($buyprice/$qty,'MU'); + + $now=dol_now(); + + // Ajoute prix courant du fournisseur pour cette quantite + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price("; + $sql.= "datec, fk_product_fournisseur, fk_user, price, quantity, unitprice, fk_availability)"; + $sql.= " values('".$this->db->idate($now)."',"; + $sql.= " ".$this->product_fourn_id.","; + $sql.= " ".$user->id.","; + $sql.= " ".price2num($buyprice).","; + $sql.= " ".$qty.","; + $sql.= " ".$unitBuyPrice.","; + $sql.= " ".$availability; $sql.=")"; - dol_syslog("ProductFournisseur::update_buyprice sql=".$sql); - if (! $this->db->query($sql)) - { - $error++; - } - - if (! $error) - { - // Ajoute modif dans table log - $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log("; - $sql.= "datec, fk_product_fournisseur,fk_user,price,quantity)"; - $sql.= "values('".$this->db->idate($now)."',"; - $sql.= " ".$this->product_fourn_id.","; - $sql.= " ".$user->id.","; - $sql.= " ".price2num($buyprice).","; - $sql.= " ".$qty; + dol_syslog("ProductFournisseur::update_buyprice sql=".$sql); + if (! $this->db->query($sql)) + { + $error++; + } + + if (! $error) + { + // Ajoute modif dans table log + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log("; + $sql.= "datec, fk_product_fournisseur,fk_user,price,quantity)"; + $sql.= "values('".$this->db->idate($now)."',"; + $sql.= " ".$this->product_fourn_id.","; + $sql.= " ".$user->id.","; + $sql.= " ".price2num($buyprice).","; + $sql.= " ".$qty; $sql.=")"; - $resql=$this->db->query($sql); - if (! $resql) - { - $error++; - } - } - - if (! $error) - { - $this->db->commit(); - return 0; - } - else - { - $this->error=$this->db->error()." sql=".$sql; - $this->db->rollback(); - return -2; - } - } - else - { - $this->error=$this->db->error()." sql=".$sql; - $this->db->rollback(); - return -1; - } - } - - - /** - * Changes the purchase price for a supplier of the product in the reference supplier - * @param id_fourn Supplier ID - * @param product_fourn_ref Supplier ref product - * @param qty Amount for which the price is valid - * @param buyprice Purchase price for the quantity - * @param user Object user user made changes - * @return int <0 if KO, >0 if OK - */ - function UpdateBuyPriceByFournRef($id_fourn, $product_fourn_ref, $qty, $buyprice, $user, $price_base_type='HT') - { - global $conf; - - $result=0; - - // Recherche id produit pour cette ref et fournisseur - $sql = "SELECT fk_product"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE fk_soc = '".$id_fourn."'"; - $sql.= " AND ref_fourn = '".$product_fourn_ref."'"; - $sql.= " AND entity = ".$conf->entity; - - if ($this->db->query($sql)) - { - if ($obj = $this->db->fetch_object($resql)) - { - // Met a jour prix pour la qte - $this->id = $obj->fk_product; - $result = $this->update_buyprice($id_fourn, $qty, $buyprice, $user, $price_base_type); - } - } - - return $result; - } - - - /** - * Load information about a provider - * @param fournid Supplier ID - * @return int < 0 if error, > 0 if ok - */ - function fetch_fourn_data($fournid) - { - global $conf; - - // Check parameters - if (empty($fournid)) return -1; - - $sql = "SELECT rowid, ref_fourn"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur "; - $sql.= " WHERE fk_product = ".$this->id; - $sql.= " AND fk_soc = ".$fournid; - $sql.= " AND entity = ".$conf->entity; - - dol_syslog("Product::fetch_fourn_data sql=".$sql); - $result = $this->db->query($sql) ; - if ($result) - { - $result = $this->db->fetch_array($result); - $this->ref_fourn = $result["ref_fourn"]; - $this->product_fourn_id = $result["rowid"]; - return 1; - } - else - { - $this->error=$this->db->error(); - dol_syslog("ProductFournisseur::fetch_fourn_data error=".$this->error, LOG_ERR); - return -1; - } - } - - /** - * Loads the price information of a provider - * @param rowid line id - * @return int < 0 if KO, 0 if OK but not found, > 0 if OK - */ - function fetch_product_fournisseur_price($rowid) - { - $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.fk_availability"; - $sql.= ", pf.rowid as product_fourn_id, pf.fk_soc, pf.ref_fourn, pf.fk_product"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; - $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur as pf"; - $sql.= " WHERE pfp.rowid = ".$rowid; - $sql.= " AND pf.rowid = pfp.fk_product_fournisseur"; - - dol_syslog("ProductFournisseur::fetch_product_fournisseur_price sql=".$sql, LOG_DEBUG); - $resql = $this->db->query($sql) ; - if ($resql) - { - $obj = $this->db->fetch_object($resql); - if ($obj) - { - $this->product_fourn_price_id = $rowid; - $this->product_fourn_id = $obj->product_fourn_id; - $this->fourn_ref = $obj->ref_fourn; - $this->fourn_price = $obj->price; - $this->fourn_qty = $obj->quantity; - $this->fourn_unitprice = $obj->unitprice; - $this->product_id = $obj->fk_product; // deprecated - $this->fk_product = $obj->fk_product; - $this->fk_availability = $obj->fk_availability; - return 1; - } - else - { - return 0; - } - } - else - { - $this->error=$this->db->error(); - dol_syslog("ProductFournisseur::fetch_product_fournisseur_price error=".$this->error, LOG_ERR); - return -1; - } - } + $resql=$this->db->query($sql); + if (! $resql) + { + $error++; + } + } + + if (! $error) + { + $this->db->commit(); + return 0; + } + else + { + $this->error=$this->db->error()." sql=".$sql; + $this->db->rollback(); + return -2; + } + } + else + { + $this->error=$this->db->error()." sql=".$sql; + $this->db->rollback(); + return -1; + } + } + + + /** + * Changes the purchase price for a supplier of the product in the reference supplier + * + * @param id_fourn Supplier ID + * @param product_fourn_ref Supplier ref product + * @param qty Amount for which the price is valid + * @param buyprice Purchase price for the quantity + * @param user Object user user made changes + * @return int <0 if KO, >0 if OK + */ + function UpdateBuyPriceByFournRef($id_fourn, $product_fourn_ref, $qty, $buyprice, $user, $price_base_type='HT') + { + global $conf; + + $result=0; + + // Recherche id produit pour cette ref et fournisseur + $sql = "SELECT fk_product"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE fk_soc = '".$id_fourn."'"; + $sql.= " AND ref_fourn = '".$product_fourn_ref."'"; + $sql.= " AND entity = ".$conf->entity; + + $resql=$this->db->query($sql); + if ($resql) + { + if ($obj = $this->db->fetch_object($resql)) + { + // Met a jour prix pour la qte + $this->id = $obj->fk_product; + $result = $this->update_buyprice($id_fourn, $qty, $buyprice, $user, $price_base_type); + } + } + + return $result; + } + + + /** + * Load information about a provider + * + * @param fournid Supplier ID + * @return int < 0 if error, > 0 if ok + */ + function fetch_fourn_data($fournid) + { + global $conf; + + // Check parameters + if (empty($fournid)) return -1; + + $sql = "SELECT rowid, ref_fourn"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur "; + $sql.= " WHERE fk_product = ".$this->id; + $sql.= " AND fk_soc = ".$fournid; + $sql.= " AND entity = ".$conf->entity; + + dol_syslog("Product::fetch_fourn_data sql=".$sql); + $result = $this->db->query($sql) ; + if ($result) + { + $result = $this->db->fetch_array($result); + $this->ref_fourn = $result["ref_fourn"]; + $this->product_fourn_id = $result["rowid"]; + return 1; + } + else + { + $this->error=$this->db->error(); + dol_syslog("ProductFournisseur::fetch_fourn_data error=".$this->error, LOG_ERR); + return -1; + } + } + + /** + * Loads the price information of a provider + * + * @param rowid line id + * @return int < 0 if KO, 0 if OK but not found, > 0 if OK + */ + function fetch_product_fournisseur_price($rowid) + { + $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.fk_availability"; + $sql.= ", pf.rowid as product_fourn_id, pf.fk_soc, pf.ref_fourn, pf.fk_product"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; + $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur as pf"; + $sql.= " WHERE pfp.rowid = ".$rowid; + $sql.= " AND pf.rowid = pfp.fk_product_fournisseur"; + + dol_syslog("ProductFournisseur::fetch_product_fournisseur_price sql=".$sql, LOG_DEBUG); + $resql = $this->db->query($sql) ; + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj) + { + $this->product_fourn_price_id = $rowid; + $this->product_fourn_id = $obj->product_fourn_id; + $this->fourn_ref = $obj->ref_fourn; + $this->fourn_price = $obj->price; + $this->fourn_qty = $obj->quantity; + $this->fourn_unitprice = $obj->unitprice; + $this->product_id = $obj->fk_product; // deprecated + $this->fk_product = $obj->fk_product; + $this->fk_availability = $obj->fk_availability; + return 1; + } + else + { + return 0; + } + } + else + { + $this->error=$this->db->error(); + dol_syslog("ProductFournisseur::fetch_product_fournisseur_price error=".$this->error, LOG_ERR); + return -1; + } + } + + + /** + * List all supplier prices of a product + * + * @param rowid id du produit + * @return table table de ProductFournisseur + */ + function fetch_product_fournisseur($prodid) + { + global $conf; + + // Suppliers list + $sql = "SELECT s.nom as supplier_name, "; + $sql.= " s.rowid as fourn_id,"; + $sql.= " pf.ref_fourn,"; + $sql.= " pfp.rowid as product_fourn_pri_id, "; + $sql.= " pf.rowid as product_fourn_id, "; + $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.fk_availability"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; + $sql.= " INNER JOIN ".MAIN_DB_PREFIX."product_fournisseur as pf ON pf.fk_soc = s.rowid "; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; + $sql.= " ON pf.rowid = pfp.fk_product_fournisseur"; + $sql.= " WHERE s.entity = ".$conf->entity; + $sql.= " AND pf.fk_product = ".$prodid; + $sql.= " ORDER BY s.nom, pfp.quantity"; + + dol_syslog("ProductFournisseur::fetch_product_fournisseur sql=".$sql, LOG_DEBUG); + + $resql = $this->db->query($sql); + + if ($resql) + { + $prod_fourn = array(); + + while ($record = $this->db->fetch_array ($resql)) + { + //define base attribute + $prodfourn = new ProductFournisseur($this->db); + + $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"]; + $prodfourn->product_fourn_id = $record["product_fourn_id"]; + $prodfourn->fourn_ref = $record["ref_fourn"]; + $prodfourn->fourn_price = $record["price"]; + $prodfourn->fourn_qty = $record["quantity"]; + $prodfourn->fourn_unitprice = $record["unitprice"]; + $prodfourn->fourn_id = $record["fourn_id"]; + $prodfourn->fourn_name = $record["supplier_name"]; + $prodfourn->fk_availability = $record["fk_availability"]; + $prodfourn->id = $prodid; + + if (!isset($prodfourn->fourn_unitprice)) + { + if ($prodfourn->fourn_qty!=0) + { + $prodfourn->fourn_unitprice = $prodfourn->fourn_price/$prodfourn->fourn_qty; + } + else + { + $prodfourn->fourn_unitprice=""; + } + } + + $prod_fourn[]=$prodfourn; + } + + $this->db->free($resql); + return $prod_fourn; + } + else + { + $this->error=$this->db->error(); + dol_syslog("ProductFournisseur::fetch_product_fournisseur error=".$this->error, LOG_ERR); + return -1; + } + } + + /** + * Load properties for minimum price + * + * @param rowid Product id + * @return int <0 if KO, >0 if OK + */ + function find_min_price_product_fournisseur($prodid) + { + global $conf; + + $sql = "SELECT s.nom as supplier_name, "; + $sql.= " s.rowid as fourn_id,"; + $sql.= " pf.ref_fourn,"; + $sql.= " pfp.rowid as product_fourn_pri_id, "; + $sql.= " pf.rowid as product_fourn_id, "; + $sql.= " pfp.price, pfp.quantity, pfp.unitprice"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; + $sql.= " INNER JOIN ".MAIN_DB_PREFIX."product_fournisseur as pf ON pf.fk_soc = s.rowid "; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; + $sql.= " ON pf.rowid = pfp.fk_product_fournisseur"; + $sql.= " WHERE s.entity = ".$conf->entity; + $sql.= " AND pf.fk_product = ".$prodid; + $sql.= " ORDER BY pfp.unitprice"; + + dol_syslog(get_class($this)."::find_min_price_product_fournisseur sql=".$sql, LOG_DEBUG); + + $resql = $this->db->query($sql); + if ($resql) + { + $record = $this->db->fetch_array ($resql); + $this->product_fourn_price_id = $record["product_fourn_pri_id"]; + $this->product_fourn_id = $record["product_fourn_id"]; + $this->fourn_ref = $record["ref_fourn"]; + $this->fourn_price = $record["price"]; + $this->fourn_qty = $record["quantity"]; + $this->fourn_unitprice = $record["unitprice"]; + $this->fourn_id = $record["fourn_id"]; + $this->fourn_name = $record["supplier_name"]; + $this->id = $prodid; + $this->db->free($resql); + return 1; + } + else + { + $this->error=$this->db->error(); + dol_syslog(get_class($this)."::find_min_price_product_fournisseur error=".$this->error, LOG_ERR); + return -1; + } + } + + /** + * + */ + function getSocNomUrl($withpicto=0) + { + $cust = new Fournisseur($this->db); + $cust->fetch($this->fourn_id); + + return $cust->getNomUrl($withpicto); + } + + /** + * + */ + function display_price_product_fournisseur() + { + global $langs; + $langs->load("suppliers"); + $out=price($this->fourn_unitprice).'   ('.$langs->trans("Supplier").': '.$this->getSocNomUrl(1).' / '.$langs->trans("SupplierRef").': '.$this->fourn_ref.')'; + return $out; + } + } + ?> \ No newline at end of file diff --git a/htdocs/fourn/commande/fiche.php b/htdocs/fourn/commande/fiche.php index cd4894128458a..6fa6f56e97b41 100644 --- a/htdocs/fourn/commande/fiche.php +++ b/htdocs/fourn/commande/fiche.php @@ -25,7 +25,7 @@ * \file htdocs/fourn/commande/fiche.php * \ingroup supplier, order * \brief Card supplier order - * \version $Id: fiche.php,v 1.233 2011/08/04 21:46:51 eldy Exp $ + * \version $Id: fiche.php,v 1.234 2011/08/20 23:56:04 eldy Exp $ */ require("../../main.inc.php"); @@ -141,8 +141,8 @@ { $qty = $_POST['qty'] ? $_POST['qty'] : $_POST['pqty']; - $product = new ProductFournisseur($db); - $idprod=$product->get_buyprice($_POST['idprodfournprice'], $qty); + $productsupplier = new ProductFournisseur($db); + $idprod=$productsupplier->get_buyprice($_POST['idprodfournprice'], $qty); //$societe=''; if ($object->socid) @@ -153,20 +153,20 @@ if ($idprod > 0) { - $res=$product->fetch($idprod); + $res=$productsupplier->fetch($idprod); // cas special pour lequel on a les meme reference que le fournisseur // $label = '['.$nv_prod->ref.'] - '. $nv_prod->libelle; - $label = $product->libelle; + $label = $productsupplier->libelle; - $desc = $product->description; - $desc.= $product->description && $_POST['np_desc'] ? "\n" : ""; + $desc = $productsupplier->description; + $desc.= $productsupplier->description && $_POST['np_desc'] ? "\n" : ""; $desc.= $_POST['np_desc']; $remise_percent = $_POST["remise_percent"] ? $_POST["remise_percent"] : $_POST["p_remise_percent"]; - $tva_tx = get_default_tva($societe,$mysoc,$product->id); - $type = $product->type; + $tva_tx = get_default_tva($societe,$mysoc,$productsupplier->id); + $type = $productsupplier->type; // Local Taxes $localtax1_tx= get_localtax($tva_tx, 1, $societe); @@ -179,9 +179,9 @@ $tva_tx, $localtax1_tx, $localtax2_tx, - $product->id, + $productsupplier->id, $_POST['idprodfournprice'], - $product->fourn_ref, + $productsupplier->fourn_ref, $remise_percent, 'HT', $type @@ -1542,5 +1542,5 @@ $db->close(); -llxFooter('$Date: 2011/08/04 21:46:51 $ - $Revision: 1.233 $'); +llxFooter('$Date: 2011/08/20 23:56:04 $ - $Revision: 1.234 $'); ?> diff --git a/htdocs/langs/en_US/suppliers.lang b/htdocs/langs/en_US/suppliers.lang index 096c59717d562..6497d599273d2 100644 --- a/htdocs/langs/en_US/suppliers.lang +++ b/htdocs/langs/en_US/suppliers.lang @@ -12,6 +12,7 @@ ListOfSuppliers=List of suppliers ShowSupplier=Show supplier OrderDate=Order date BuyingPrice=Buying price +BuyingPriceMin=Minimum buying price AddSupplierPrice=Add supplier price ChangeSupplierPrice=Change supplier price ErrorQtyTooLowForThisSupplier=Quantity too low for this supplier or no price defined on this product for this supplier diff --git a/htdocs/langs/fr_FR/suppliers.lang b/htdocs/langs/fr_FR/suppliers.lang index 9e0ca68fd8048..765a9623688d3 100644 --- a/htdocs/langs/fr_FR/suppliers.lang +++ b/htdocs/langs/fr_FR/suppliers.lang @@ -12,6 +12,7 @@ ListOfSuppliers=Liste des fournisseurs ShowSupplier=Afficher fournisseur OrderDate=Date commande BuyingPrice=Prix d'achat +BuyingPriceMin=Prix d'achat minimum AddSupplierPrice=Ajouter prix fournisseur ChangeSupplierPrice=Modifier prix fournisseur ErrorSupplierCountryIsNotDefined=Le pays de ce fournisseur n'est pas défini. Corriger sur sa fiche. diff --git a/htdocs/product/canvas/default/tpl/list.tpl.php b/htdocs/product/canvas/default/tpl/list.tpl.php index 25e03e40b582c..be85d6f2f1e97 100644 --- a/htdocs/product/canvas/default/tpl/list.tpl.php +++ b/htdocs/product/canvas/default/tpl/list.tpl.php @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . * - * $Id: list.tpl.php,v 1.4 2011/07/31 23:19:26 eldy Exp $ + * $Id: list.tpl.php,v 1.5 2011/08/20 23:56:03 eldy Exp $ */ ?> @@ -83,7 +83,7 @@ > diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index 9751c51a048f3..2067a4b55b9e5 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -23,13 +23,13 @@ * \file htdocs/product/fournisseurs.php * \ingroup product * \brief Page of tab suppliers for products - * \version $Id: fournisseurs.php,v 1.99 2011/08/17 16:44:38 simnandez Exp $ + * \version $Id: fournisseurs.php,v 1.100 2011/08/20 23:56:04 eldy Exp $ */ require("../main.inc.php"); require_once(DOL_DOCUMENT_ROOT."/lib/product.lib.php"); require_once(DOL_DOCUMENT_ROOT."/comm/propal/class/propal.class.php"); -require_once DOL_DOCUMENT_ROOT."/fourn/class/fournisseur.product.class.php"; +require_once(DOL_DOCUMENT_ROOT."/fourn/class/fournisseur.product.class.php"); $langs->load("products"); $langs->load("suppliers"); @@ -228,9 +228,20 @@ print ''; print ''; - // Libelle + // Label print ''.$langs->trans("Label").''.$product->libelle.''; + // Minimum Price + print ''.$langs->trans("BuyingPriceMin").''; + print ''; + $product_fourn = new ProductFournisseur($db); + if ($product_fourn->find_min_price_product_fournisseur($product->id) > 0) + { + if (isset($product_fourn->fourn_unitprice)) print $product_fourn->display_price_product_fournisseur(); + else print $langs->trans("NotDefined"); + } + print ''; + // Status (to buy) print ''.$langs->trans("Status").' ('.$langs->trans("Buy").')'.''; print $product->getLibStatut(2,1); @@ -277,6 +288,7 @@ } print ''; + // Ref supplier print ''.$langs->trans("SupplierRef").''; if ($_GET["rowid"]) { @@ -288,14 +300,16 @@ } print ''; print ''; - - //Availability + + // Availability if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) { print ''.$langs->trans("Availability").''; $html->select_availability($product->fk_availability,"oselDispo",1); print ''."\n"; } + + // Qty min print ''; print ''.$langs->trans("QtyMin").''; print ''; @@ -353,8 +367,8 @@ print ''; if ($product->isproduct()) $nblignefour=4; else $nblignefour=4; - - $param="&id=".$product->id; + + $param="&id=".$product->id; print ''; print_liste_field_titre($langs->trans("Suppliers"),$_SERVER["PHP_SELF"],"s.nom","",$param,"",$sortfield,$sortorder); print ''; @@ -366,78 +380,60 @@ print ''; print "\n"; - // Suppliers list - $sql = "SELECT s.nom, s.rowid as socid,"; - $sql.= " pf.ref_fourn,"; - $sql.= " pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.fk_availability"; - $sql.= " FROM ".MAIN_DB_PREFIX."societe as s"; - $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur as pf"; - $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; - $sql.= " ON pf.rowid = pfp.fk_product_fournisseur"; - $sql.= " WHERE pf.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; - $sql.= " AND pf.fk_product = ".$product->id; - //$sql.= " ORDER BY s.nom, pfp.quantity"; - $sql.= $db->order($sortfield,$sortorder); - $resql=$db->query($sql); - if ($resql) + $product_fourn = new ProductFournisseur($db); + $product_fourn_list = $product_fourn->fetch_product_fournisseur($product->id); + + if (sizeof($product_fourn_list)>0) { - $num = $db->num_rows($resql); - $i = 0; + $var=true; - $var=True; - while ($i < $num) + foreach($product_fourn_list as $productfourn) { - $objp = $db->fetch_object($resql); $var=!$var; - print ""; - print ''; + print ""; + + print ''; // Supplier - print ''; - + print ''; + //Availability if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) { $html->load_cache_availability(); - $availability= $html->cache_availability[$objp->fk_availability]['label']; + $availability= $html->cache_availability[$productfourn->fk_availability]['label']; print ''; } + // Quantity print ''; // Price quantity print ''; // Unit price print ''; // Modify-Remove print ''; print ''; - - $i++; } - - $db->free($resql); - } - else { - dol_print_error($db); } print '
'.$langs->trans("SupplierRef").'
'.img_object($langs->trans("ShowCompany"),'company').' '.$objp->nom.'
'.$productfourn->getSocNomUrl(1).''.$objp->ref_fourn.''.$productfourn->fourn_ref.''.$availability.''; - print $objp->quantity; + print $productfourn->fourn_qty; print ''; - print $objp->price?price($objp->price):""; + print $productfourn->fourn_price?price($productfourn->fourn_price):""; print ''; - print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):" "); + print price($productfourn->fourn_unitprice); + //print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):" "); print ''; if ($user->rights->produit->creer || $user->rights->service->creer) { - print ''.img_edit().""; - print ''.img_picto($langs->trans("Remove"),'disable.png').''; + print ''.img_edit().""; + print ''.img_picto($langs->trans("Remove"),'disable.png').''; } print '
'; @@ -453,5 +449,5 @@ $db->close(); -llxFooter('$Date: 2011/08/17 16:44:38 $ - $Revision: 1.99 $'); +llxFooter('$Date: 2011/08/20 23:56:04 $ - $Revision: 1.100 $'); ?> \ No newline at end of file diff --git a/htdocs/product/liste.php b/htdocs/product/liste.php index 771722dd166ca..5f53bc97a5375 100644 --- a/htdocs/product/liste.php +++ b/htdocs/product/liste.php @@ -21,7 +21,7 @@ * \file htdocs/product/liste.php * \ingroup produit * \brief Page to list products and services - * \version $Id: liste.php,v 1.152 2011/07/31 23:19:25 eldy Exp $ + * \version $Id: liste.php,v 1.153 2011/08/20 23:56:04 eldy Exp $ */ require("../main.inc.php"); @@ -296,6 +296,7 @@ if ($conf->service->enabled && $type != 0) print_liste_field_titre($langs->trans("Duration"), $_SERVER["PHP_SELF"], "p.duration",$param,"",'align="center"',$sortfield,$sortorder); if (empty($conf->global->PRODUIT_MULTIPRICES)) print_liste_field_titre($langs->trans("SellingPrice"), $_SERVER["PHP_SELF"], "p.price",$param,"",'align="right"',$sortfield,$sortorder); if ($conf->stock->enabled && $user->rights->stock->lire && $type != 1) print ''.$langs->trans("PhysicalStock").''; + if ($conf->fournisseur->enabled && $user->rights->fournisseur->lire && $type != 1) print ''.$langs->trans("BuyingPriceMin").''; print_liste_field_titre($langs->trans("Sell"), $_SERVER["PHP_SELF"], "p.tosell",$param,"",'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Buy"), $_SERVER["PHP_SELF"], "p.tobuy",$param,"",'align="right"',$sortfield,$sortorder); print "\n"; @@ -341,6 +342,14 @@ print ' '; print ''; } + + // Minimum buying Price + if ($conf->fournisseur->enabled && $user->rights->fournisseur->lire && $type != 1) + { + print ''; + print ' '; + print ''; + } print ''; print ' '; @@ -355,7 +364,7 @@ $product_static=new Product($db); - $var=True; + $var=true; while ($i < min($num,$limit)) { $objp = $db->fetch_object($resql); @@ -438,6 +447,25 @@ print ' '; } } + + // MinimumPrice + if ($conf->fournisseur->enabled && $user->rights->fournisseur->lire && $type != 1) + { + if ($objp->fk_product_type != 1) + { + require_once(DOL_DOCUMENT_ROOT."/fourn/class/fournisseur.product.class.php"); + + $product_fourn = new ProductFournisseur($db); + if ($product_fourn->find_min_price_product_fournisseur($objp->rowid)) + { + print ''.$product_fourn->display_price_product_fournisseur().''; + } + } + else + { + print ' '; + } + } // Status (to buy) print ''.$product_static->LibStatut($objp->tosell,5,0).''; @@ -475,5 +503,5 @@ $db->close(); -llxFooter('$Date: 2011/07/31 23:19:25 $ - $Revision: 1.152 $'); +llxFooter('$Date: 2011/08/20 23:56:04 $ - $Revision: 1.153 $'); ?> diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index fd8f8971098a3..48bf073eb9135 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -25,7 +25,7 @@ /** * \file htdocs/user/class/user.class.php * \brief Fichier de la classe utilisateur - * \version $Id: user.class.php,v 1.50 2011/08/20 16:59:18 eldy Exp $ + * \version $Id: user.class.php,v 1.51 2011/08/20 23:56:03 eldy Exp $ */ require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php"); @@ -118,6 +118,7 @@ function User($DB) /** * Load a user from database with its id or ref (login) + * * @param id Si defini, id a utiliser pour recherche * @param login Si defini, login a utiliser pour recherche * @param sid Si defini, sid a utiliser pour recherche @@ -145,7 +146,7 @@ function fetch($id='', $login='',$sid='',$loadpersonalconf=1) $sql.= " u.openid as openid"; $sql.= " FROM ".MAIN_DB_PREFIX."user as u"; - if($conf->multicompany->enabled && $conf->entity == 1 && ($conf->global->MULTICOMPANY_TRANSVERSE_MODE || ($user->admin && ! $user->entity))) + if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && ($conf->global->MULTICOMPANY_TRANSVERSE_MODE || ($user->admin && ! $user->entity))) { $sql.= " WHERE u.entity IS NOT NULL"; } @@ -154,12 +155,11 @@ function fetch($id='', $login='',$sid='',$loadpersonalconf=1) $sql.= " WHERE u.entity IN (0,".$conf->entity.")"; } - if ($sid) + if ($sid) // permet une recherche du user par son SID ActiveDirectory ou Samba { $sql.= " AND (u.ldap_sid = '".$sid."' OR u.login = '".$this->db->escape($login)."') LIMIT 1"; } else if ($login) - // permet une recherche du user par son SID ActiveDirectory ou Samba { $sql.= " AND u.login = '".$this->db->escape($login)."'"; }