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 = new ProductFournisseur($this->db);
$prod->fetch($fk_product); $prod->fetch($fk_product);
$prod->fetch_fourn_data($this->id); //$prod->fetch_fourn_data($this->id);
$commf->fetch($idc); $commf->fetch($idc);
$commf->addline("Toto",120,1,$prod->tva, $prod->id, 0, $prod->ref_fourn); $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 * @param fournid Supplier ID
* @return int < 0 if error, > 0 if ok * @return int < 0 if error, > 0 if ok
*/ */
function fetch_fourn_data($fournid) /*function fetch_fourn_data($fournid)
{ {
global $conf; global $conf;
@ -268,7 +268,7 @@ class ProductFournisseur extends Product
dol_syslog(get_class($this)."::fetch_fourn_data error=".$this->error, LOG_ERR); dol_syslog(get_class($this)."::fetch_fourn_data error=".$this->error, LOG_ERR);
return -1; return -1;
} }
} }*/
/** /**
* Loads the price information of a provider * Loads the price information of a provider
@ -335,7 +335,7 @@ class ProductFournisseur extends Product
$sql.= " ON pfp.fk_soc = s.rowid"; $sql.= " ON pfp.fk_soc = s.rowid";
$sql.= " WHERE s.entity = ".$conf->entity; $sql.= " WHERE s.entity = ".$conf->entity;
$sql.= " AND pfp.fk_product = ".$prodid; $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); 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); 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_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 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); 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 -- 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_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 INDEX idx_product_fourn_price_fk_soc (fk_soc, entity);

View File

@ -1760,26 +1760,55 @@ 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 User $user User that make link
* @param id_fourn Supplier id * @param int $id_fourn Supplier id
* @param ref_fourn Supplier ref * @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 * @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; global $conf;
$now=dol_now(); $now=dol_now();
if ($ref_fourn)
{
$sql = "SELECT rowid, fk_product"; $sql = "SELECT rowid, fk_product";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE fk_soc = ".$id_fourn; $sql.= " WHERE fk_soc = ".$id_fourn;
$sql.= " AND ref_fourn = '".$ref_fourn."'"; $sql.= " AND ref_fourn = '".$ref_fourn."'";
$sql.= " AND fk_product != ".$this->id;
$sql.= " AND entity = ".$conf->entity; $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);
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;
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(get_class($this)."::add_fournisseur sql=".$sql);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -1794,13 +1823,15 @@ class Product extends CommonObject
$sql.= ", fk_product"; $sql.= ", fk_product";
$sql.= ", fk_soc"; $sql.= ", fk_soc";
$sql.= ", ref_fourn"; $sql.= ", ref_fourn";
$sql.= ", quantity";
$sql.= ", fk_user"; $sql.= ", fk_user";
$sql.= ") VALUES ("; $sql.= ") VALUES (";
$sql.= $this->db->idate($now); $sql.= "'".$this->db->idate($now)."'";
$sql.= ", ".$conf->entity; $sql.= ", ".$conf->entity;
$sql.= ", ".$this->id; $sql.= ", ".$this->id;
$sql.= ", ".$id_fourn; $sql.= ", ".$id_fourn;
$sql.= ", '".$ref_fourn."'"; $sql.= ", '".$ref_fourn."'";
$sql.= ", ".$quantity;
$sql.= ", ".$user->id; $sql.= ", ".$user->id;
$sql.= ")"; $sql.= ")";
@ -1813,22 +1844,16 @@ class Product extends CommonObject
else else
{ {
$this->error=$this->db->lasterror(); $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; return -1;
} }
} }
// If the supplier ref already exists for this product // If the supplier price already exists for this product and quantity
else if ($obj->fk_product == $this->id) else
{ {
$this->product_fourn_price_id = $obj->rowid; $this->product_fourn_price_id = $obj->rowid;
return 0; return 0;
} }
// If the supplier ref already exists but for another product
else
{
$this->product_id_already_linked = $obj->fk_product;
return -3;
}
} }
else else
{ {

View File

@ -35,6 +35,10 @@ $langs->load("suppliers");
$langs->load("bills"); $langs->load("bills");
$action=GETPOST('action'); $action=GETPOST('action');
$id=GETPOST('id','int');
$ref=GETPOST('ref');
$socid=GETPOST('socid');
$error=0; $mesg = '';
// If socid provided by ajax company selector // If socid provided by ajax company selector
if (! empty($_REQUEST['search_fourn_id'])) if (! empty($_REQUEST['search_fourn_id']))
@ -45,10 +49,8 @@ if (! empty($_REQUEST['search_fourn_id']))
} }
// Security check // Security check
$id = GETPOST('id')?GETPOST('id'):GETPOST('ref');
$fieldid = isset($_GET["ref"])?'ref':'rowid';
if ($user->societe_id) $socid=$user->societe_id; 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'); $sortfield = GETPOST("sortfield",'alpha');
$sortorder = GETPOST("sortorder",'alpha'); $sortorder = GETPOST("sortorder",'alpha');
@ -56,56 +58,70 @@ $sortorder = GETPOST("sortorder",'alpha');
if (! $sortfield) $sortfield="s.nom"; if (! $sortfield) $sortfield="s.nom";
if (! $sortorder) $sortorder="ASC"; if (! $sortorder) $sortorder="ASC";
$mesg = '';
/* /*
* Actions * Actions
*/ */
if ($_GET["action"] == 'remove_pf') if ($action == 'remove_pf')
{ {
$product = new ProductFournisseur($db); $product = new ProductFournisseur($db);
if ($product->fetch($_GET["id"]) > 0) if ($product->fetch($id) > 0)
{ {
if ($_GET["rowid"]) if ($_GET["rowid"])
{ {
$result=$product->remove_product_fournisseur_price($_GET["rowid"]); $result=$product->remove_product_fournisseur_price($_GET["rowid"]);
$_GET["action"] = ''; $action = '';
$mesg = '<div class="ok">'.$langs->trans("PriceRemoved").'.</div>'; $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"); $id_fourn=GETPOST("id_fourn");
if (empty($id_fourn)) $id_fourn=GETPOST("search_id_fourn"); if (empty($id_fourn)) $id_fourn=GETPOST("search_id_fourn");
$ref_fourn=GETPOST("ref_fourn"); $ref_fourn=GETPOST("ref_fourn");
if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn"); if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn");
$quantity=GETPOST("qty");
$product = new ProductFournisseur($db); if (empty($quantity))
$result=$product->fetch(GETPOST("id"));
if ($result > 0)
{ {
$db->begin(); $error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'</div>';
$error=0; }
if (! $ref_fourn) if (empty($ref_fourn)) // TODO Why not making this optionnal ?
{ {
$error++; $error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'</div>'; $mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("RefSupplier")).'</div>';
} }
if ($id_fourn <= 0) if ($id_fourn <= 0)
{ {
//print "eee".$_POST["id_fourn"];
$error++; $error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Supplier")).'</div>'; $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($id);
if ($result <= 0)
{
$error++;
$mesg=$product->error;
}
if (! $error) if (! $error)
{ {
$ret=$product->add_fournisseur($user, $id_fourn, $ref_fourn); $db->begin();
if (! $error)
{
$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) if ($ret == -3)
{ {
$error++; $error++;
@ -123,38 +139,21 @@ if ($_POST["action"] == 'updateprice' && $_POST["cancel"] <> $langs->trans("Canc
} }
if (! $error) if (! $error)
{
if ($_POST["qty"])
{
if ($_POST["price"] >= 0)
{ {
$supplier=new Fournisseur($db); $supplier=new Fournisseur($db);
$result=$supplier->fetch($id_fourn); $result=$supplier->fetch($id_fourn);
$ret=$product->update_buyprice($_POST["qty"], $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn); $ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn);
if ($ret < 0) if ($ret < 0)
{ {
$error++; $error++;
$mesg='<div class="error">'.$product->error.'</div>'; $mesg='<div class="error">'.$product->error.'</div>';
} }
} }
else
{
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'</div>';
}
}
else
{
$error++;
$mesg='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'</div>';
}
}
if (! $error) if (! $error)
{ {
$db->commit(); $db->commit();
$_POST['action']='';
$action=''; $action='';
} }
else else
@ -179,21 +178,19 @@ if ($_POST["cancel"] == $langs->trans("Cancel"))
$html = new Form($db); $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); $product = new ProductFournisseur($db);
$result = $product->fetch($_GET["id"],$_GET["ref"]); $result = $product->fetch($id,$ref);
$result = $product->fetch_fourn_data($_REQUEST["id_fourn"]); //$result = $product->fetch_fourn_data($_REQUEST["id_fourn"]);
//print 'eeeee'.$_GET["socid"];exit;
llxHeader("","",$langs->trans("CardProduct".$product->type)); llxHeader("","",$langs->trans("CardProduct".$product->type));
} }
if ($result) if ($result)
{ {
if ($action <> 'edit' && $action <> 're-edit')
if ($_GET["action"] <> 'edit' && $_GET["action"] <> 're-edit')
{ {
/* /*
* En mode visu * En mode visu
@ -242,7 +239,7 @@ if ($_GET["id"] || $_GET["ref"])
// Form to add or update a price // 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"); $langs->load("suppliers");
@ -262,13 +259,13 @@ if ($_GET["id"] || $_GET["ref"])
print '<table class="border" width="100%">'; 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"]) if ($_GET["rowid"])
{ {
$supplier=new Fournisseur($db); $supplier=new Fournisseur($db);
$supplier->fetch($_GET["socid"]); $supplier->fetch($socid);
print $supplier->getNomUrl(1); 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" value="'.$product->fourn_ref.'">';
print '<input type="hidden" name="ref_fourn_price_id" value="'.$_GET["rowid"].'">'; print '<input type="hidden" name="ref_fourn_price_id" value="'.$_GET["rowid"].'">';
} }
@ -280,14 +277,14 @@ if ($_GET["id"] || $_GET["ref"])
print '</td></tr>'; print '</td></tr>';
// Ref supplier // 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"]) if ($_GET["rowid"])
{ {
print $product->fourn_ref; print $product->fourn_ref;
} }
else 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 '</td>';
print '</tr>'; print '</tr>';
@ -302,8 +299,8 @@ if ($_GET["id"] || $_GET["ref"])
// Qty min // Qty min
print '<tr>'; print '<tr>';
print '<td>'.$langs->trans("QtyMin").'</td>'; print '<td class="fieldrequired" width="25%">'.$langs->trans("QtyMin").'</td>';
print '<td>'; print '<td width="25%">';
$quantity = $_REQUEST["qty"] ? $_REQUEST["qty"] : "1"; $quantity = $_REQUEST["qty"] ? $_REQUEST["qty"] : "1";
if ($_GET["rowid"]) if ($_GET["rowid"])
{ {
@ -315,8 +312,8 @@ if ($_GET["id"] || $_GET["ref"])
print '<input class="flat" name="qty" size="5" value="'.$quantity.'">'; print '<input class="flat" name="qty" size="5" value="'.$quantity.'">';
} }
print '</td>'; print '</td>';
print '<td>'.$langs->trans("PriceQtyMin").'</td>'; print '<td class="fieldrequired" width="25%">'.$langs->trans("PriceQtyMin").'</td>';
print '<td><input class="flat" name="price" size="8" value="'.($_POST["price"]?$_POST["price"]:price($product->fourn_price)).'">'; 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 '&nbsp;';
print $html->select_PriceBaseType(($_POST["price_base_type"]?$_POST["price_base_type"]:$product->price_base_type), "price_base_type"); print $html->select_PriceBaseType(($_POST["price_base_type"]?$_POST["price_base_type"]:$product->price_base_type), "price_base_type");
print '</td>'; print '</td>';
@ -339,7 +336,7 @@ if ($_GET["id"] || $_GET["ref"])
print "\n<div class=\"tabsAction\">\n"; 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) if ($user->rights->produit->creer || $user->rights->service->creer)
{ {