---
--- 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, see .
---
--- ============================================================================
-
-create table llx_product_fournisseur
-(
- rowid integer AUTO_INCREMENT PRIMARY KEY,
- datec datetime,
- tms timestamp,
- fk_product integer,
- fk_soc integer,
- ref_fourn varchar(30),
- entity integer DEFAULT 1 NOT NULL, -- multi company id
- fk_user_author integer
-)ENGINE=innodb;
-
---
--- List of codes for the field entity
---
--- 1 : first company product
--- 2 : second company product
--- 3 : etc...
---
\ No newline at end of file
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 67ddb3cd150..7d3dc962152 100644
--- a/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql
+++ b/htdocs/install/mysql/tables/llx_product_fournisseur_price.key.sql
@@ -19,9 +19,16 @@
ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fournisseur_price_fk_user (fk_user);
-ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fournisseur_price_fk_product_fournisseur (fk_product_fournisseur);
+--ALTER TABLE llx_product_fournisseur_price ADD INDEX idx_product_fournisseur_price_fk_product_fournisseur (fk_product_fournisseur);
ALTER TABLE llx_product_fournisseur_price ADD CONSTRAINT fk_product_fournisseur_price_fk_user FOREIGN KEY (fk_user) REFERENCES llx_user (rowid);
-ALTER TABLE llx_product_fournisseur_price ADD CONSTRAINT fk_product_fournisseur_price_fk_product_fournisseur FOREIGN KEY (fk_product_fournisseur) REFERENCES llx_product_fournisseur (rowid);
+--ALTER TABLE llx_product_fournisseur_price ADD CONSTRAINT fk_product_fournisseur_price_fk_product_fournisseur FOREIGN KEY (fk_product_fournisseur) REFERENCES llx_product_fournisseur (rowid);
+-- 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 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.sql b/htdocs/install/mysql/tables/llx_product_fournisseur_price.sql
index 0d49d733a28..42f9f836471 100644
--- a/htdocs/install/mysql/tables/llx_product_fournisseur_price.sql
+++ b/htdocs/install/mysql/tables/llx_product_fournisseur_price.sql
@@ -22,11 +22,14 @@ create table llx_product_fournisseur_price
rowid integer AUTO_INCREMENT PRIMARY KEY,
datec datetime,
tms timestamp,
- fk_product_fournisseur integer NOT NULL,
+ fk_product_fournisseur integer NOT NULL, -- deprecated
+ fk_product integer,
+ fk_soc integer,
+ ref_fourn varchar(30),
fk_availability integer,
price double(24,8) DEFAULT 0,
quantity double,
unitprice double(24,8) DEFAULT 0,
- fk_user integer
-
+ fk_user integer,
+ entity integer DEFAULT 1 NOT NULL, -- multi company id
)ENGINE=innodb;
diff --git a/htdocs/product/canvas/service/actions_card_service.class.php b/htdocs/product/canvas/service/actions_card_service.class.php
index 591e220c22b..984edc8002f 100755
--- a/htdocs/product/canvas/service/actions_card_service.class.php
+++ b/htdocs/product/canvas/service/actions_card_service.class.php
@@ -310,7 +310,7 @@ class ActionsCardService extends Product
if ($_GET["fourn_id"] > 0)
{
$fourn_id = $_GET["fourn_id"];
- $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur as pf";
+ $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
}
$sql.= " WHERE p.entity = ".$conf->entity;
if ($search_categ) $sql.= " AND p.rowid = cp.fk_product"; // Join for the needed table to filter by categ
@@ -335,7 +335,7 @@ class ActionsCardService extends Product
}
if ($fourn_id > 0)
{
- $sql.= " AND p.rowid = pf.fk_product AND pf.fk_soc = ".$fourn_id;
+ $sql.= " AND p.rowid = pfp.fk_product AND pfp.fk_soc = ".$fourn_id;
}
// Insert categ filter
if ($search_categ)
diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php
index cb662f912e9..e5d0e712536 100644
--- a/htdocs/product/class/product.class.php
+++ b/htdocs/product/class/product.class.php
@@ -37,7 +37,7 @@ class Product extends CommonObject
public $element='product';
public $table_element='product';
public $fk_element='fk_product';
- public $childtables=array('propaldet','commandedet','facturedet','contratdet','product_fournisseur');
+ public $childtables=array('propaldet','commandedet','facturedet','contratdet','product_fournisseur_price');
protected $isnolinkedbythird = 1; // No field fk_soc
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
@@ -519,11 +519,11 @@ class Product extends CommonObject
$this->db->begin();
// Delete supplier prices log
- if (! $error)
+ /*if (! $error)
{
$sql = 'DELETE pfpl';
- $sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price_log as pfpl, '.MAIN_DB_PREFIX.'product_fournisseur as pf';
- $sql.= ' WHERE pfpl.fk_product_fournisseur = pf.rowid';
+ $sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price_log as pfpl, '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp';
+ $sql.= ' WHERE pfpl.fk_product_fournisseur = pfp.rowid';
$sql.= ' AND pf.fk_product = '.$id;
dol_syslog(get_class($this).'::delete sql='.$sql, LOG_DEBUG);
$result = $this->db->query($sql);
@@ -533,15 +533,14 @@ class Product extends CommonObject
$this->error = $this->db->lasterror();
dol_syslog(get_class($this).'::delete error '.$this->error, LOG_ERR);
}
- }
+ }*/
// Delete supplier prices
if (! $error)
{
$sql = 'DELETE pfp';
- $sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.'product_fournisseur as pf';
- $sql.= ' WHERE pfp.fk_product_fournisseur = pf.rowid';
- $sql.= ' AND pf.fk_product = '.$id;
+ $sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp';
+ $sql.= ' WHERE pfp.fk_product = '.$id;
dol_syslog(get_class($this).'::delete sql='.$sql, LOG_DEBUG);
$result = $this->db->query($sql);
if (! $result)
@@ -819,10 +818,9 @@ class Product extends CommonObject
{
$result = 0;
$sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity,";
- $sql.= " pf.fk_product, pf.ref_fourn";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product_fournisseur as pf";
- $sql.= " WHERE pf.rowid = pfp.fk_product_fournisseur";
- $sql.= " AND pfp.rowid = ".$prodfournprice;
+ $sql.= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc";
+ $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
+ $sql.= " WHERE pfp.rowid = ".$prodfournprice;
$sql.= " AND pfp.quantity <= ".$qty;
dol_syslog("Product::get_buyprice sql=".$sql);
@@ -841,13 +839,12 @@ class Product extends CommonObject
else
{
// On refait le meme select sur la ref et l'id du produit
- $sql = "SELECT pfp.price as price, pfp.quantity as quantity, pf.fk_soc,";
- $sql.= " pf.fk_product, pf.ref_fourn";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp, ".MAIN_DB_PREFIX."product_fournisseur as pf";
- $sql.= " WHERE pf.rowid = pfp.fk_product_fournisseur";
- $sql.= " AND pf.ref_fourn = '".$fourn_ref."'";
- $sql.= " AND pf.fk_product = ".$product_id;
- $sql.= " AND quantity <= ".$qty;
+ $sql = "SELECT pfp.price as price, pfp.quantity as quantity, pfp.fk_soc,";
+ $sql.= " pfp.fk_product, pfp.ref_fourn";
+ $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
+ $sql.= " WHERE pfp.ref_fourn = '".$fourn_ref."'";
+ $sql.= " AND pfp.fk_product = ".$product_id;
+ $sql.= " AND pfp.quantity <= ".$qty;
$sql.= " ORDER BY pfp.quantity DESC";
$sql.= " LIMIT 1";
@@ -1774,8 +1771,10 @@ class Product extends CommonObject
{
global $conf;
+ $now=dol_now();
+
$sql = "SELECT rowid, fk_product";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur";
+ $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE fk_soc = ".$id_fourn;
$sql.= " AND ref_fourn = '".$ref_fourn."'";
$sql.= " AND entity = ".$conf->entity;
@@ -1789,15 +1788,15 @@ class Product extends CommonObject
// The reference supplier does not exist, we create it for this product.
if (! $obj)
{
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur (";
+ $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price (";
$sql.= "datec";
$sql.= ", entity";
$sql.= ", fk_product";
$sql.= ", fk_soc";
$sql.= ", ref_fourn";
- $sql.= ", fk_user_author";
+ $sql.= ", fk_user";
$sql.= ") VALUES (";
- $sql.= $this->db->idate(mktime());
+ $sql.= $this->db->idate($now);
$sql.= ", ".$conf->entity;
$sql.= ", ".$this->id;
$sql.= ", ".$id_fourn;
@@ -1808,7 +1807,7 @@ class Product extends CommonObject
dol_syslog("Product::add_fournisseur sql=".$sql);
if ($this->db->query($sql))
{
- $this->product_fourn_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur");
+ $this->product_fourn_price_id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_fournisseur_price");
return 1;
}
else
@@ -1821,15 +1820,13 @@ class Product extends CommonObject
// If the supplier ref already exists for this product
else if ($obj->fk_product == $this->id)
{
- $this->product_fourn_id = $obj->rowid;
-
+ $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;
}
}
@@ -1852,8 +1849,8 @@ class Product extends CommonObject
$list = array();
- $sql = "SELECT fk_soc";
- $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur as p";
+ $sql = "SELECT p.fk_soc";
+ $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as p";
$sql.= " WHERE p.fk_product = ".$this->id;
$sql.= " AND p.entity = ".$conf->entity;
@@ -1925,16 +1922,16 @@ class Product extends CommonObject
/**
* Recopie les fournisseurs et prix fournisseurs d'un produit/service sur un autre
*
- * @param fromId Id produit source
- * @param toId Id produit cible
- * @return int < 0 si erreur, > 0 si ok
+ * @param int $fromId Id produit source
+ * @param int $toId Id produit cible
+ * @return int < 0 si erreur, > 0 si ok
*/
function clone_fournisseurs($fromId, $toId)
{
$this->db->begin();
// les fournisseurs
- $sql = "INSERT ".MAIN_DB_PREFIX."product_fournisseur ("
+ /*$sql = "INSERT ".MAIN_DB_PREFIX."product_fournisseur ("
. " datec, fk_product, fk_soc, ref_fourn, fk_user_author )"
. " SELECT '".$this->db->idate(mktime())."', ".$toId.", fk_soc, ref_fourn, fk_user_author"
. " FROM ".MAIN_DB_PREFIX."product_fournisseur"
@@ -1944,7 +1941,7 @@ class Product extends CommonObject
{
$this->db->rollback();
return -1;
- }
+ }*/
// les prix de fournisseurs.
$sql = "INSERT ".MAIN_DB_PREFIX."product_fournisseur_price ("
@@ -1953,13 +1950,17 @@ class Product extends CommonObject
. " FROM ".MAIN_DB_PREFIX."product_fournisseur_price"
. " WHERE fk_product = ".$fromId;
- if ( ! $this->db->query($sql ) )
+ $resql=$this->db->query($sql);
+ if (! $resql)
{
$this->db->rollback();
return -1;
}
- $this->db->commit();
- return 1;
+ else
+ {
+ $this->db->commit();
+ return 1;
+ }
}
/**
diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php
index fcc5398b196..95647c1ca4f 100644
--- a/htdocs/product/fournisseurs.php
+++ b/htdocs/product/fournisseurs.php
@@ -34,6 +34,8 @@ $langs->load("products");
$langs->load("suppliers");
$langs->load("bills");
+$action=GETPOST('action');
+
// If socid provided by ajax company selector
if (! empty($_REQUEST['search_fourn_id']))
{
@@ -43,10 +45,7 @@ if (! empty($_REQUEST['search_fourn_id']))
}
// Security check
-if (isset($_GET["id"]) || isset($_GET["ref"]))
-{
- $id = isset($_GET["id"])?$_GET["id"]:(isset($_GET["ref"])?$_GET["ref"]:'');
-}
+$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);
@@ -75,19 +74,6 @@ if ($_GET["action"] == 'remove_pf')
$_GET["action"] = '';
$mesg = ''.$langs->trans("PriceRemoved").'.
';
}
- else
- {
- // Deprecated. Should not occurs
- if ($product->remove_fournisseur($_GET["socid"]) > 0)
- {
- $_GET["action"] = '';
- $mesg = ''.$langs->trans("SupplierRemoved").'.
';
- }
- else
- {
- $_GET["action"] = '';
- }
- }
}
}
@@ -145,7 +131,7 @@ if ($_POST["action"] == 'updateprice' && $_POST["cancel"] <> $langs->trans("Canc
$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"]);
+ $ret=$product->update_buyprice($_POST["qty"], $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn);
if ($ret < 0)
{
$error++;
@@ -169,6 +155,7 @@ if ($_POST["action"] == 'updateprice' && $_POST["cancel"] <> $langs->trans("Canc
{
$db->commit();
$_POST['action']='';
+ $action='';
}
else
{
@@ -259,17 +246,22 @@ if ($_GET["id"] || $_GET["ref"])
{
$langs->load("suppliers");
- if ($_GET["rowid"]) {
+ if ($_GET["rowid"])
+ {
$product->fetch_product_fournisseur_price($_GET["rowid"]);
print_fiche_titre($langs->trans("ChangeSupplierPrice"));
- } else {
+ }
+ else
+ {
print_fiche_titre($langs->trans("AddSupplierPrice"));
}
- print '';
}
/* ************************************************************************** */
@@ -346,14 +339,13 @@ if ($_GET["id"] || $_GET["ref"])
print "\n\n";
- if ($_GET["action"] != 'add_price') {
-
+ if ($_GET["action"] != 'add_price' && $action != 'updateprice')
+ {
if ($user->rights->produit->creer || $user->rights->service->creer)
{
print '
';
print $langs->trans("AddSupplierPrice").'';
}
-
}
print "\n
\n";
@@ -371,16 +363,15 @@ if ($_GET["id"] || $_GET["ref"])
print '';
print_liste_field_titre($langs->trans("Suppliers"),$_SERVER["PHP_SELF"],"s.nom","",$param,"",$sortfield,$sortorder);
print '| '.$langs->trans("SupplierRef").' | ';
- if(!empty($conf->global->FOURN_PRODUCT_AVAILABILITY))
- print_liste_field_titre($langs->trans("Availability"),$_SERVER["PHP_SELF"],"pfp.fk_availability","",$param,"",$sortfield,$sortorder);
- print_liste_field_titre($langs->trans("QtyMin"),$_SERVER["PHP_SELF"],"pfp.quantity","",$param,"",$sortfield,$sortorder);
+ if (!empty($conf->global->FOURN_PRODUCT_AVAILABILITY)) print_liste_field_titre($langs->trans("Availability"),$_SERVER["PHP_SELF"],"pfp.fk_availability","",$param,"",$sortfield,$sortorder);
+ print_liste_field_titre($langs->trans("QtyMin"),$_SERVER["PHP_SELF"],"pfp.quantity","",$param,'align="right"',$sortfield,$sortorder);
print ''.$langs->trans("PriceQtyMinHT").' | ';
print_liste_field_titre($langs->trans("UnitPriceHT"),$_SERVER["PHP_SELF"],"pfp.unitprice","",$param,'align="right"',$sortfield,$sortorder);
print ' | ';
print "
\n";
$product_fourn = new ProductFournisseur($db);
- $product_fourn_list = $product_fourn->fetch_product_fournisseur($product->id);
+ $product_fourn_list = $product_fourn->list_product_fournisseur_price($product->id);
if (count($product_fourn_list)>0)
{
@@ -406,7 +397,7 @@ if ($_GET["id"] || $_GET["ref"])
}
// Quantity
- print '';
+ print ' | ';
print $productfourn->fourn_qty;
print ' | ';
diff --git a/htdocs/product/liste.php b/htdocs/product/liste.php
index 5be0bee72fc..7853db14257 100644
--- a/htdocs/product/liste.php
+++ b/htdocs/product/liste.php
@@ -133,8 +133,8 @@ else
$sql.= ') ';
//if ($fourn_id > 0) // The DISTINCT is used to avoid duplicate from this link
//{
- $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur as pf ON p.rowid = pf.fk_product";
- $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON pf.rowid = pfp.fk_product_fournisseur";
+ //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur as pf ON p.rowid = pf.fk_product";
+ $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
//}
$sql.= ' WHERE p.entity IN (0,'.(! empty($conf->entities['product']) ? $conf->entities['product'] : $conf->entity).')';
if ($search_categ) $sql.= " AND p.rowid = cp.fk_product"; // Join for the needed table to filter by categ
@@ -156,7 +156,7 @@ else
if (dol_strlen($canvas) > 0) $sql.= " AND p.canvas = '".$db->escape($canvas)."'";
if ($catid) $sql.= " AND cp.fk_categorie = ".$catid;
if ($search_categ) $sql.= " AND cp.fk_categorie = ".$search_categ;
- if ($fourn_id > 0) $sql.= " AND pf.fk_soc = ".$fourn_id;
+ if ($fourn_id > 0) $sql.= " AND pfp.fk_soc = ".$fourn_id;
$sql.= " GROUP BY p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type,";
$sql.= " p.fk_product_type, p.tms,";
$sql.= " p.duration, p.tosell, p.tobuy, p.seuil_stock_alerte";
diff --git a/htdocs/product/reassort.php b/htdocs/product/reassort.php
index 93b96132635..e964f9bca59 100644
--- a/htdocs/product/reassort.php
+++ b/htdocs/product/reassort.php
@@ -96,12 +96,6 @@ $sql.= ' SUM(s.reel) as stock_physique';
$sql.= ' FROM ('.MAIN_DB_PREFIX.'product as p';
// We'll need this table joined to the select in order to filter by categ
if ($search_categ) $sql.= ", ".MAIN_DB_PREFIX."categorie_product as cp";
-// We disable this because this create duplicate lines
-/*if ($_GET["fourn_id"] > 0)
-{
- $fourn_id = $_GET["fourn_id"];
- $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur as pf";
-}*/
$sql.= ') LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as s on p.rowid = s.fk_product';
$sql.= " WHERE p.entity = ".$conf->entity;
if ($search_categ) $sql.= " AND p.rowid = cp.fk_product"; // Join for the needed table to filter by categ
@@ -112,9 +106,12 @@ if ($sall)
// if the type is not 1, we show all products (type = 0,2,3)
if (dol_strlen($type))
{
- if ($type==1) {
+ if ($type==1)
+ {
$sql.= " AND p.fk_product_type = '1'";
- } else {
+ }
+ else
+ {
$sql.= " AND p.fk_product_type <> '1'";
}
}