Fix: Fix a serious database default. Table llx_product_fournisseur was

linked with llx_product_fournisseur_price with a link 1-1 so was
useless. I removed it. This reduce a lot of sophisticated code and will
make import of prices easier. This also increase speed.
This commit is contained in:
eldy 2011-10-05 12:51:55 +02:00
parent 47bb2171e0
commit b295a866a5
6 changed files with 119 additions and 96 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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
{

View File

@ -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 = '<div class="ok">'.$langs->trans("PriceRemoved").'.</div>';
}
}
}
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='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'</div>';
}
if (empty($ref_fourn)) // TODO Why not making this optionnal ?
{
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'</div>';
}
if ($id_fourn <= 0)
{
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Supplier")).'</div>';
}
if ($_POST["price"] < 0 || $_POST["price"] == '')
{
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'</div>';
}
$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='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'</div>';
}
if ($id_fourn <= 0)
{
//print "eee".$_POST["id_fourn"];
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Supplier")).'</div>';
}
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='<div class="error">'.$product->error.'</div>';
}
}
else
{
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'</div>';
}
}
else
$ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn);
if ($ret < 0)
{
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'</div>';
$mesg='<div class="error">'.$product->error.'</div>';
}
}
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 '<table class="border" width="100%">';
print '<tr><td>'.$langs->trans("Supplier").'</td><td colspan="3">';
print '<tr><td class="fieldrequired">'.$langs->trans("Supplier").'</td><td colspan="3">';
if ($_GET["rowid"])
{
$supplier=new Fournisseur($db);
$supplier->fetch($_GET["socid"]);
$supplier->fetch($socid);
print $supplier->getNomUrl(1);
print '<input type="hidden" name="id_fourn" value="'.$_GET["socid"].'">';
print '<input type="hidden" name="id_fourn" value="'.$socid.'">';
print '<input type="hidden" name="ref_fourn" value="'.$product->fourn_ref.'">';
print '<input type="hidden" name="ref_fourn_price_id" value="'.$_GET["rowid"].'">';
}
@ -280,14 +277,14 @@ if ($_GET["id"] || $_GET["ref"])
print '</td></tr>';
// Ref supplier
print '<tr><td>'.$langs->trans("SupplierRef").'</td><td colspan="3">';
print '<tr><td class="fieldrequired">'.$langs->trans("SupplierRef").'</td><td colspan="3">';
if ($_GET["rowid"])
{
print $product->fourn_ref;
}
else
{
print '<input class="flat" name="ref_fourn" size="12" value="'.($_POST["ref_fourn"]?$_POST["ref_fourn"]:$product->ref_fourn).'">';
print '<input class="flat" name="ref_fourn" size="12" value="'.($_POST["ref_fourn"]?$_POST["ref_fourn"]:'').'">';
}
print '</td>';
print '</tr>';
@ -302,8 +299,8 @@ if ($_GET["id"] || $_GET["ref"])
// Qty min
print '<tr>';
print '<td>'.$langs->trans("QtyMin").'</td>';
print '<td>';
print '<td class="fieldrequired" width="25%">'.$langs->trans("QtyMin").'</td>';
print '<td width="25%">';
$quantity = $_REQUEST["qty"] ? $_REQUEST["qty"] : "1";
if ($_GET["rowid"])
{
@ -315,8 +312,8 @@ if ($_GET["id"] || $_GET["ref"])
print '<input class="flat" name="qty" size="5" value="'.$quantity.'">';
}
print '</td>';
print '<td>'.$langs->trans("PriceQtyMin").'</td>';
print '<td><input class="flat" name="price" size="8" value="'.($_POST["price"]?$_POST["price"]:price($product->fourn_price)).'">';
print '<td class="fieldrequired" width="25%">'.$langs->trans("PriceQtyMin").'</td>';
print '<td width="25%"><input class="flat" name="price" size="8" value="'.($_POST["price"]?$_POST["price"]:(isset($product->fourn_price)?price($product->fourn_price):'')).'">';
print '&nbsp;';
print $html->select_PriceBaseType(($_POST["price_base_type"]?$_POST["price_base_type"]:$product->price_base_type), "price_base_type");
print '</td>';
@ -339,7 +336,7 @@ if ($_GET["id"] || $_GET["ref"])
print "\n<div class=\"tabsAction\">\n";
if ($_GET["action"] != 'add_price' && $action != 'updateprice')
if ($action != 'add_price' && $action != 'updateprice')
{
if ($user->rights->produit->creer || $user->rights->service->creer)
{