diff --git a/htdocs/fourn/class/fournisseur.class.php b/htdocs/fourn/class/fournisseur.class.php index 8d94a59f270..22b6af10af0 100644 --- a/htdocs/fourn/class/fournisseur.class.php +++ b/htdocs/fourn/class/fournisseur.class.php @@ -165,7 +165,7 @@ class Fournisseur extends Societe { $prod = new ProductFournisseur($this->db); $prod->fetch($fk_product); - $prod->fetch_fourn_data($this->id); + //$prod->fetch_fourn_data($this->id); $commf->fetch($idc); $commf->addline("Toto",120,1,$prod->tva, $prod->id, 0, $prod->ref_fourn); diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index 7582969cd99..61b7b48ccd7 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -240,7 +240,7 @@ class ProductFournisseur extends Product * @param fournid Supplier ID * @return int < 0 if error, > 0 if ok */ - function fetch_fourn_data($fournid) + /*function fetch_fourn_data($fournid) { global $conf; @@ -268,7 +268,7 @@ class ProductFournisseur extends Product dol_syslog(get_class($this)."::fetch_fourn_data error=".$this->error, LOG_ERR); return -1; } - } + }*/ /** * Loads the price information of a provider @@ -325,17 +325,17 @@ class ProductFournisseur extends Product global $conf; // Suppliers list - $sql = "SELECT s.nom as supplier_name, "; + $sql = "SELECT s.nom as supplier_name,"; $sql.= " s.rowid as fourn_id,"; $sql.= " pfp.ref_fourn,"; - $sql.= " pfp.rowid as product_fourn_pri_id, "; + $sql.= " pfp.rowid as product_fourn_pri_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_price as pfp"; $sql.= " ON pfp.fk_soc = s.rowid"; $sql.= " WHERE s.entity = ".$conf->entity; $sql.= " AND pfp.fk_product = ".$prodid; - $sql.= " ORDER BY s.nom, pfp.quantity"; + $sql.= " ORDER BY s.nom, pfp.quantity, pfp.price"; dol_syslog(get_class($this)."::list_product_fournisseur_price sql=".$sql, LOG_DEBUG); diff --git a/htdocs/install/mysql/migration/3.1.0-3.2.0.sql b/htdocs/install/mysql/migration/3.1.0-3.2.0.sql index a60961dee63..653a2203341 100755 --- a/htdocs/install/mysql/migration/3.1.0-3.2.0.sql +++ b/htdocs/install/mysql/migration/3.1.0-3.2.0.sql @@ -57,7 +57,8 @@ ALTER TABLE llx_product_fournisseur_price ADD COLUMN entity integer DEFAULT 1 NO UPDATE llx_product_fournisseur_price as a, llx_product_fournisseur as b SET a.fk_product = b.fk_product, a.fk_soc = b.fk_soc, a.ref_fourn = b.ref_fourn, a.entity = b.entity WHERE a.fk_product_fournisseur = b.rowid AND (a.fk_product IS NULL OR a.fk_soc IS NULL OR a.fk_product = 0 OR a.fk_soc = 0); -ALTER TABLE llx_product_fournisseur_price ADD UNIQUE INDEX uk_product_fournisseur_price_ref (ref_fourn, fk_soc, entity); +ALTER TABLE llx_product_fournisseur_price DROP INDEX uk_product_fournisseur_price_ref; +ALTER TABLE llx_product_fournisseur_price ADD UNIQUE INDEX uk_product_fournisseur_price_ref (ref_fourn, fk_soc, quantity, entity); ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fourn_price_fk_product (fk_product, entity); ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fourn_price_fk_soc (fk_soc, entity); ALTER TABLE llx_product_fournisseur_price ADD CONSTRAINT fk_product_fournisseur_price_fk_product FOREIGN KEY (fk_product) REFERENCES llx_product (rowid); diff --git a/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql b/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql index 7d3dc962152..a625efcfbbc 100644 --- a/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql +++ b/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql @@ -26,7 +26,7 @@ ALTER TABLE llx_product_fournisseur_price ADD CONSTRAINT fk_product_fournisseur_ -- Added to remove table llx_product_fournisseur -ALTER TABLE llx_product_fournisseur_price ADD UNIQUE INDEX uk_product_fournisseur_price_ref (ref_fourn, fk_soc, entity); +ALTER TABLE llx_product_fournisseur_price ADD UNIQUE INDEX uk_product_fournisseur_price_ref (ref_fourn, fk_soc, quantity, entity); ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fourn_price_fk_product (fk_product, entity); ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fourn_price_fk_soc (fk_soc, entity); diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index e5d0e712536..e6bb32deb36 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -1760,47 +1760,78 @@ class Product extends CommonObject /** - * Add a supplier reference for the product + * Add a supplier price for the product. + * Note: Duplicate ref is accepted for different quantity only or for different companies. * - * @param user User that make link - * @param id_fourn Supplier id - * @param ref_fourn Supplier ref - * @return int < 0 if KO, 0 if link already exists for this product, > 0 if OK + * @param User $user User that make link + * @param int $id_fourn Supplier id + * @param string $ref_fourn Supplier ref + * @param float $quantity Quantity minimum for price + * @return int < 0 if KO, 0 if link already exists for this product, > 0 if OK */ - function add_fournisseur($user, $id_fourn, $ref_fourn) + function add_fournisseur($user, $id_fourn, $ref_fourn, $quantity) { global $conf; $now=dol_now(); - $sql = "SELECT rowid, fk_product"; + if ($ref_fourn) + { + $sql = "SELECT rowid, fk_product"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql.= " WHERE fk_soc = ".$id_fourn; + $sql.= " AND ref_fourn = '".$ref_fourn."'"; + $sql.= " AND fk_product != ".$this->id; + $sql.= " AND entity = ".$conf->entity; + + dol_syslog(get_class($this)."::add_fournisseur sql=".$sql); + $resql=$this->db->query($sql); + if ($resql) + { + $obj = $this->db->fetch_object($resql); + if ($obj) + { + // If the supplier ref already exists but for another product (duplicate ref is accepted for different quantity only or different companies) + $this->product_id_already_linked = $obj->fk_product; + return -3; + } + $this->db->free($resql); + } + } + + $sql = "SELECT rowid"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; $sql.= " WHERE fk_soc = ".$id_fourn; - $sql.= " AND ref_fourn = '".$ref_fourn."'"; + if ($ref_fourn) $sql.= " AND ref_fourn = '".$ref_fourn."'"; + else $sql.= " AND (ref_fourn = '' OR ref_fourn IS NULL)"; + $sql.= " AND quantity = '".$quantity."'"; + $sql.= " AND fk_product = ".$this->id; $sql.= " AND entity = ".$conf->entity; - dol_syslog("Product::add_fournisseur sql=".$sql); + dol_syslog(get_class($this)."::add_fournisseur sql=".$sql); $resql=$this->db->query($sql); if ($resql) { - $obj = $this->db->fetch_object($resql); + $obj = $this->db->fetch_object($resql); - // The reference supplier does not exist, we create it for this product. + // The reference supplier does not exist, we create it for this product. if (! $obj) { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price ("; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price("; $sql.= "datec"; $sql.= ", entity"; $sql.= ", fk_product"; $sql.= ", fk_soc"; $sql.= ", ref_fourn"; + $sql.= ", quantity"; $sql.= ", fk_user"; $sql.= ") VALUES ("; - $sql.= $this->db->idate($now); + $sql.= "'".$this->db->idate($now)."'"; $sql.= ", ".$conf->entity; $sql.= ", ".$this->id; $sql.= ", ".$id_fourn; $sql.= ", '".$ref_fourn."'"; + $sql.= ", ".$quantity; $sql.= ", ".$user->id; $sql.= ")"; @@ -1813,22 +1844,16 @@ class Product extends CommonObject else { $this->error=$this->db->lasterror(); - dol_syslog("Product::add_fournisseur ".$this->error, LOG_ERR); + dol_syslog(get_class($this)."::add_fournisseur ".$this->error, LOG_ERR); return -1; } } - // If the supplier ref already exists for this product - else if ($obj->fk_product == $this->id) + // If the supplier price already exists for this product and quantity + else { $this->product_fourn_price_id = $obj->rowid; return 0; } - // If the supplier ref already exists but for another product - else - { - $this->product_id_already_linked = $obj->fk_product; - return -3; - } } else { diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index 95647c1ca4f..a3b9ccfc9d8 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -35,6 +35,10 @@ $langs->load("suppliers"); $langs->load("bills"); $action=GETPOST('action'); +$id=GETPOST('id','int'); +$ref=GETPOST('ref'); +$socid=GETPOST('socid'); +$error=0; $mesg = ''; // If socid provided by ajax company selector if (! empty($_REQUEST['search_fourn_id'])) @@ -45,10 +49,8 @@ if (! empty($_REQUEST['search_fourn_id'])) } // Security check -$id = GETPOST('id')?GETPOST('id'):GETPOST('ref'); -$fieldid = isset($_GET["ref"])?'ref':'rowid'; if ($user->societe_id) $socid=$user->societe_id; -$result=restrictedArea($user,'produit|service&fournisseur',$id,'product','','',$fieldid); +$result=restrictedArea($user, 'produit|service&fournisseur', ($id>0?$id:$ref), 'product', '', '', ($id>0?'rowid':'ref')); $sortfield = GETPOST("sortfield",'alpha'); $sortorder = GETPOST("sortorder",'alpha'); @@ -56,56 +58,70 @@ $sortorder = GETPOST("sortorder",'alpha'); if (! $sortfield) $sortfield="s.nom"; if (! $sortorder) $sortorder="ASC"; -$mesg = ''; /* * Actions */ -if ($_GET["action"] == 'remove_pf') +if ($action == 'remove_pf') { $product = new ProductFournisseur($db); - if ($product->fetch($_GET["id"]) > 0) + if ($product->fetch($id) > 0) { if ($_GET["rowid"]) { $result=$product->remove_product_fournisseur_price($_GET["rowid"]); - $_GET["action"] = ''; + $action = ''; $mesg = '
'.$langs->trans("PriceRemoved").'.
'; } } } -if ($_POST["action"] == 'updateprice' && $_POST["cancel"] <> $langs->trans("Cancel")) +if ($action == 'updateprice' && $_POST["cancel"] <> $langs->trans("Cancel")) { $id_fourn=GETPOST("id_fourn"); if (empty($id_fourn)) $id_fourn=GETPOST("search_id_fourn"); $ref_fourn=GETPOST("ref_fourn"); if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn"); + $quantity=GETPOST("qty"); + + if (empty($quantity)) + { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'
'; + } + if (empty($ref_fourn)) // TODO Why not making this optionnal ? + { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'
'; + } + if ($id_fourn <= 0) + { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Supplier")).'
'; + } + if ($_POST["price"] < 0 || $_POST["price"] == '') + { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'
'; + } $product = new ProductFournisseur($db); - $result=$product->fetch(GETPOST("id")); - if ($result > 0) + $result=$product->fetch($id); + if ($result <= 0) { - $db->begin(); + $error++; + $mesg=$product->error; + } - $error=0; - if (! $ref_fourn) - { - $error++; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'
'; - } - if ($id_fourn <= 0) - { - //print "eee".$_POST["id_fourn"]; - $error++; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Supplier")).'
'; - } + if (! $error) + { + $db->begin(); if (! $error) { - $ret=$product->add_fournisseur($user, $id_fourn, $ref_fourn); + $ret=$product->add_fournisseur($user, $id_fourn, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice if ($ret == -3) { $error++; @@ -124,44 +140,27 @@ if ($_POST["action"] == 'updateprice' && $_POST["cancel"] <> $langs->trans("Canc if (! $error) { - if ($_POST["qty"]) - { - if ($_POST["price"] >= 0) - { - $supplier=new Fournisseur($db); - $result=$supplier->fetch($id_fourn); + $supplier=new Fournisseur($db); + $result=$supplier->fetch($id_fourn); - $ret=$product->update_buyprice($_POST["qty"], $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn); - if ($ret < 0) - { - $error++; - $mesg='
'.$product->error.'
'; - } - } - else - { - $error++; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'
'; - } - } - else + $ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn); + if ($ret < 0) { $error++; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'
'; + $mesg='
'.$product->error.'
'; } } if (! $error) { $db->commit(); - $_POST['action']=''; $action=''; } else { $db->rollback(); } - } + } } if ($_POST["cancel"] == $langs->trans("Cancel")) @@ -179,21 +178,19 @@ if ($_POST["cancel"] == $langs->trans("Cancel")) $html = new Form($db); -if ($_GET["id"] || $_GET["ref"]) +if ($id || $ref) { - if ($_GET["action"] <> 're-edit') + if ($action <> 're-edit') { $product = new ProductFournisseur($db); - $result = $product->fetch($_GET["id"],$_GET["ref"]); - $result = $product->fetch_fourn_data($_REQUEST["id_fourn"]); - //print 'eeeee'.$_GET["socid"];exit; + $result = $product->fetch($id,$ref); + //$result = $product->fetch_fourn_data($_REQUEST["id_fourn"]); llxHeader("","",$langs->trans("CardProduct".$product->type)); } - if ( $result ) + if ($result) { - - if ($_GET["action"] <> 'edit' && $_GET["action"] <> 're-edit') + if ($action <> 'edit' && $action <> 're-edit') { /* * En mode visu @@ -242,7 +239,7 @@ if ($_GET["id"] || $_GET["ref"]) // Form to add or update a price - if (($_GET["action"] == 'add_price' || $_POST["action"] == 'updateprice' ) && ($user->rights->produit->creer || $user->rights->service->creer)) + if (($action == 'add_price' || $action == 'updateprice' ) && ($user->rights->produit->creer || $user->rights->service->creer)) { $langs->load("suppliers"); @@ -262,13 +259,13 @@ if ($_GET["id"] || $_GET["ref"]) print ''; - print ''; // Ref supplier - print ''; print ''; @@ -302,8 +299,8 @@ if ($_GET["id"] || $_GET["ref"]) // Qty min print ''; - print ''; - print ''; + print ''; - print ''; - print ''; + print ''; @@ -339,7 +336,7 @@ if ($_GET["id"] || $_GET["ref"]) print "\n
\n"; - if ($_GET["action"] != 'add_price' && $action != 'updateprice') + if ($action != 'add_price' && $action != 'updateprice') { if ($user->rights->produit->creer || $user->rights->service->creer) {
'.$langs->trans("Supplier").''; + print '
'.$langs->trans("Supplier").''; if ($_GET["rowid"]) { $supplier=new Fournisseur($db); - $supplier->fetch($_GET["socid"]); + $supplier->fetch($socid); print $supplier->getNomUrl(1); - print ''; + print ''; print ''; print ''; } @@ -280,14 +277,14 @@ if ($_GET["id"] || $_GET["ref"]) print '
'.$langs->trans("SupplierRef").''; + print '
'.$langs->trans("SupplierRef").''; if ($_GET["rowid"]) { print $product->fourn_ref; } else { - print 'ref_fourn).'">'; + print ''; } print '
'.$langs->trans("QtyMin").''; + print ''.$langs->trans("QtyMin").''; $quantity = $_REQUEST["qty"] ? $_REQUEST["qty"] : "1"; if ($_GET["rowid"]) { @@ -315,8 +312,8 @@ if ($_GET["id"] || $_GET["ref"]) print ''; } print ''.$langs->trans("PriceQtyMin").'fourn_price)).'">'; + print ''.$langs->trans("PriceQtyMin").'fourn_price)?price($product->fourn_price):'')).'">'; print ' '; print $html->select_PriceBaseType(($_POST["price_base_type"]?$_POST["price_base_type"]:$product->price_base_type), "price_base_type"); print '