Merge pull request #429 from atm-maxime/mko559

Task # 559 : add discount on supplier prices
This commit is contained in:
Laurent Destailleur 2012-10-01 13:31:17 -07:00
commit eec2a7b34e
8 changed files with 60 additions and 19 deletions

View File

@ -1408,7 +1408,7 @@ class Form
$langs->load('stocks');
$sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,";
$sql.= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.unitprice,";
$sql.= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.remise_percent, pfp.remise, pfp.unitprice,";
$sql.= " s.nom";
$sql.= " FROM ".MAIN_DB_PREFIX."product as p";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
@ -1435,7 +1435,7 @@ class Form
$sql .= " OR p.barcode LIKE '".$filterkey."'";
}
}
$sql.= " ORDER BY pfp.ref_fourn DESC";
$sql.= " ORDER BY pfp.ref_fourn DESC, pfp.quantity ASC";
// Build output string
$outselect='';
@ -1461,6 +1461,7 @@ class Form
$outref=$objp->ref;
$outval='';
$outqty=1;
$outdiscount=0;
$opt = '<option value="'.$objp->idprodfournprice.'"';
if ($selected && $selected == $objp->idprodfournprice) $opt.= ' selected="selected"';
@ -1489,6 +1490,7 @@ class Form
$opt.= price($objp->fprice).' '.$currencytext."/".$objp->quantity;
$outval.= price($objp->fprice).' '.$currencytextnoent."/".$objp->quantity;
$outqty=$objp->quantity;
$outdiscount=$objp->remise_percent;
if ($objp->quantity == 1)
{
$opt.= strtolower($langs->trans("Unit"));
@ -1503,6 +1505,11 @@ class Form
{
$opt.=" (".price($objp->unitprice).' '.$currencytext."/".strtolower($langs->trans("Unit")).")";
$outval.=" (".price($objp->unitprice).' '.$currencytextnoent."/".strtolower($langs->transnoentities("Unit")).")";
}
if ($objp->remise_percent >= 1)
{
$opt.=" - ".$langs->trans("Discount")." : ".vatrate($objp->remise_percent).' %';
$outval.=" - ".$langs->transnoentities("Discount")." : ".vatrate($objp->remise_percent).' %';
}
if ($objp->duration)
{
@ -1526,7 +1533,7 @@ class Form
// "key" value of json key array is used by jQuery automatically as selected value
// "label" value of json key array is used by jQuery automatically as text for combo box
$outselect.=$opt;
array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'disabled'=>(empty($objp->idprodfournprice)?true:false)));
array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'qty'=>$outqty, 'discount'=>$outdiscount, 'disabled'=>(empty($objp->idprodfournprice)?true:false)));
$i++;
}

View File

@ -44,6 +44,8 @@ class ProductFournisseur extends Product
var $fourn_ref; // ref supplier
var $fourn_qty; // quantity for price
var $fourn_price; // price for quantity
var $fourn_remise_percent; // discount for quantity (percent)
var $fourn_remise; // discount for quantity (amount)
var $product_fourn_id; // supplier id
var $fk_availability; // availability delay
var $fourn_unitprice;
@ -133,18 +135,20 @@ class ProductFournisseur extends Product
/**
* Modify the purchase price for a supplier
*
* @param int $qty Min quantity for which price is valid
* @param float $buyprice Purchase price for the quantity min
* @param User $user Object user user made changes
* @param int $qty Min quantity for which price is valid
* @param float $buyprice Purchase price for the quantity min
* @param User $user Object user user made changes
* @param string $price_base_type HT or TTC
* @param Societe $fourn Supplier
* @param int $availability Product availability
* @param string $ref_fourn Supplier ref
* @param float $tva_tx VAT rate
* @param string $charges costs affering to product
* @param float $remise_percent Discount regarding qty (percent)
* @param float $remise Discount regarding qty (amount)
* @return int >0 if KO, >0 if OK
*/
function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0)
function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0)
{
global $conf,$mysoc;
@ -177,6 +181,8 @@ class ProductFournisseur extends Product
$sql.= " SET fk_user = " . $user->id." ,";
$sql.= " price = ".price2num($buyprice).",";
$sql.= " quantity = ".$qty.",";
$sql.= " remise_percent = ".$remise_percent.",";
$sql.= " remise = ".$remise.",";
$sql.= " unitprice = ".$unitBuyPrice.",";
$sql.= " unitcharges = ".$unitCharges.",";
$sql.= " tva_tx = ".$tva_tx.",";
@ -211,7 +217,7 @@ class ProductFournisseur extends Product
{
// Add price for this quantity to supplier
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price(";
$sql.= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, unitprice, tva_tx, fk_availability, entity)";
$sql.= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, fk_availability, entity)";
$sql.= " values('".$this->db->idate($now)."',";
$sql.= " ".$this->id.",";
$sql.= " ".$fourn->id.",";
@ -219,6 +225,8 @@ class ProductFournisseur extends Product
$sql.= " ".$user->id.",";
$sql.= " ".price2num($buyprice).",";
$sql.= " ".$qty.",";
$sql.= " ".$remise_percent.",";
$sql.= " ".$remise.",";
$sql.= " ".$unitBuyPrice.",";
$sql.= " ".$tva_tx.",";
$sql.= " ".$availability.",";
@ -280,7 +288,7 @@ class ProductFournisseur extends Product
*/
function fetch_product_fournisseur_price($rowid)
{
$sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.fk_availability,";
$sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability,";
$sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product, pfp.charges, pfp.unitcharges";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " WHERE pfp.rowid = ".$rowid;
@ -297,6 +305,8 @@ class ProductFournisseur extends Product
$this->fourn_price = $obj->price;
$this->fourn_charges = $obj->charges;
$this->fourn_qty = $obj->quantity;
$this->fourn_remise_percent = $obj->remise_percent;
$this->fourn_remise = $obj->remise;
$this->fourn_unitprice = $obj->unitprice;
$this->fourn_unitcharges = $obj->unitcharges;
$this->tva_tx = $obj->tva_tx;
@ -331,7 +341,7 @@ class ProductFournisseur extends Product
$sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
$sql.= " pfp.rowid as product_fourn_pri_id, pfp.ref_fourn, pfp.fk_product as product_fourn_id,";
$sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.unitcharges";
$sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.unitcharges";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= ", ".MAIN_DB_PREFIX."societe as s";
$sql.= " WHERE pfp.entity IN (".getEntity('product', 1).")";
@ -356,6 +366,8 @@ class ProductFournisseur extends Product
$prodfourn->fourn_ref = $record["ref_fourn"];
$prodfourn->fourn_price = $record["price"];
$prodfourn->fourn_qty = $record["quantity"];
$prodfourn->fourn_remise_percent = $record["remise_percent"];
$prodfourn->fourn_remise = $record["remise"];
$prodfourn->fourn_unitprice = $record["unitprice"];
$prodfourn->fourn_charges = $record["charges"];
$prodfourn->fourn_unitcharges = $record["unitcharges"];
@ -406,6 +418,8 @@ class ProductFournisseur extends Product
$this->fourn_ref = '';
$this->fourn_price = '';
$this->fourn_qty = '';
$this->fourn_remise_percent = '';
$this->fourn_remise = '';
$this->fourn_unitprice = '';
$this->fourn_id = '';
$this->fourn_name = '';
@ -431,9 +445,11 @@ class ProductFournisseur extends Product
$this->fourn_ref = $record["ref_fourn"];
$this->fourn_price = $record["price"];
$this->fourn_qty = $record["quantity"];
$this->fourn_remise_percent = $record["remise_percent"];
$this->fourn_remise = $record["remise"];
$this->fourn_unitprice = $record["unitprice"];
$this->fourn_charges = $record["charges"];
$this->fourn_unitcharges = $record["unitcharges"];
$this->fourn_unitcharges = $record["unitcharges"];
$this->fourn_tva_tx = $record["tva_tx"];
$this->fourn_id = $record["fourn_id"];
$this->fourn_name = $record["supplier_name"];

View File

@ -1533,7 +1533,7 @@ if ($id > 0 || ! empty($ref))
print '<td colspan="3">';
$ajaxoptions=array(
'update' => array('pqty' => 'qty'),
'update' => array('pqty' => 'qty', 'p_remise_percent' => 'discount'),
'option_disabled' => 'addPredefinedProductButton',
'error' => $langs->trans("NoPriceDefinedForThisSupplier")
);
@ -1554,7 +1554,7 @@ if ($id > 0 || ! empty($ref))
print '</td>';
print '<td align="right"><input type="text" size="2" id="pqty" name="pqty" value="'.(GETPOST('pqty')?GETPOST('pqty'):'1').'"></td>';
print '<td align="right" nowrap="nowrap"><input type="text" size="1" name="p_remise_percent" value="'.(GETPOST('p_remise_percent')?GETPOST('p_remise_percent'):$soc->remise_client).'">%</td>';
print '<td align="right" nowrap="nowrap"><input type="text" size="1" id="p_remise_percent" name="p_remise_percent" value="'.(GETPOST('p_remise_percent')?GETPOST('p_remise_percent'):$soc->remise_client).'">%</td>';
print '<td align="center" colspan="4"><input type="submit" id="addPredefinedProductButton" class="button" value="'.$langs->trans('Add').'"></td>';
print '</tr>';

View File

@ -1819,7 +1819,7 @@ else
print '<td colspan="4">';
$ajaxoptions=array(
'update' => array('pqty' => 'qty'),
'update' => array('pqty' => 'qty', 'p_remise_percent' => 'discount'),
'disabled' => 'addPredefinedProductButton',
'error' => $langs->trans("NoPriceDefinedForThisSupplier")
);
@ -1840,7 +1840,7 @@ else
print '</td>';
print '<td align="right"><input type="text" id="pqty" name="qty" value="1" size="1"></td>';
print '<td align="right" nowrap="nowrap"><input size="1" name="remise_percent" type="text" value="'.(GETPOST('remise_percent')?GETPOST('remise_percent'):'0').'">%</td>';
print '<td align="right" nowrap="nowrap"><input size="1" id="p_remise_percent" name="remise_percent" type="text" value="'.(GETPOST('remise_percent')?GETPOST('remise_percent'):'0').'">%</td>';
print '<td>&nbsp;</td>';
print '<td>&nbsp;</td>';
print '<td align="center" valign="middle" colspan="2"><input type="submit" id="addPredefinedProductButton" class="button" value="'.$langs->trans("Add").'"></td>';

View File

@ -742,3 +742,7 @@ insert into llx_accountingaccount (rowid, fk_pcg_version, pcg_type, pcg_subtype,
insert into llx_accountingaccount (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label, active) VALUES (436,'PCG99-BASE','PROD', 'XXXXXX', '791','79', 'Transferts de charges d''exploitation ', '1');
insert into llx_accountingaccount (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label, active) VALUES (437,'PCG99-BASE','PROD', 'XXXXXX', '796','79', 'Transferts de charges financières', '1');
insert into llx_accountingaccount (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label, active) VALUES (438,'PCG99-BASE','PROD', 'XXXXXX', '797','79', 'Transferts de charges exceptionnelles', '1');
-- Add discount in product supplier price
ALTER TABLE llx_product_fournisseur_price ADD COLUMN remise_percent DOUBLE NOT NULL DEFAULT 0 AFTER quantity;
ALTER TABLE llx_product_fournisseur_price ADD COLUMN remise DOUBLE NOT NULL DEFAULT 0 AFTER remise_percent;

View File

@ -146,6 +146,7 @@ ProductSpecial=Special
QtyMin=Quantity minimum
PriceQty=Price for this quantity
PriceQtyMin=Price quantity min.
DiscountQtyMin=Discount quantity min.
NoPriceDefinedForThisSupplier=No price/qty defined for this supplier/product
NoSupplierPriceDefinedForThisProduct=No supplier price/qty defined for this product
RecordedProducts=Products recorded

View File

@ -146,6 +146,7 @@ ProductSpecial=Special
QtyMin=Quantité minimum
PriceQty=Prix pour la quantité
PriceQtyMin=Prix quantité min.
DiscountQtyMin=Remise quantité min.
NoPriceDefinedForThisSupplier=Aucun prix/qté défini pour ce fournisseur/produit
NoSupplierPriceDefinedForThisProduct=Aucun prix/qté fournisseur défini pour ce produit
RecordedProducts=Produits en vente

View File

@ -94,6 +94,7 @@ if ($action == 'updateprice' && GETPOST('cancel') <> $langs->trans("Cancel"))
$ref_fourn=GETPOST("ref_fourn");
if (empty($ref_fourn)) $ref_fourn=GETPOST("search_ref_fourn");
$quantity=GETPOST("qty");
$remise_percent=price2num(GETPOST('remise_percent','alpha'));
$tva_tx=price2num(GETPOST('tva_tx','alpha'));
if (empty($quantity))
@ -153,10 +154,10 @@ if ($action == 'updateprice' && GETPOST('cancel') <> $langs->trans("Cancel"))
$supplier=new Fournisseur($db);
$result=$supplier->fetch($id_fourn);
if (isset($_POST['ref_fourn_price_id']))
$product->fetch_product_fournisseur_price($_POST['ref_fourn_price_id']);
if (isset($_POST['ref_fourn_price_id']))
$product->fetch_product_fournisseur_price($_POST['ref_fourn_price_id']);
$ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx, $_POST["charges"]);
$ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx, $_POST["charges"], $remise_percent);
if ($ret < 0)
{
$error++;
@ -346,7 +347,12 @@ if ($id || $ref)
print '<td><input class="flat" name="price" size="8" value="'.(GETPOST('price')?price(GETPOST('price')):(isset($product->fourn_price)?price($product->fourn_price):'')).'">';
print '&nbsp;';
print $form->select_PriceBaseType((GETPOST('price_base_type')?GETPOST('price_base_type'):$product->price_base_type), "price_base_type");
print '</td>';
print '</td>';
// Discount qty min
print '<td>'.$langs->trans("DiscountQtyMin").'</td>';
print '<td><input class="flat" name="remise_percent" size="8" value="'.(GETPOST('remise_percent')?vatrate(GETPOST('remise_percent')):(isset($product->fourn_remise_percent)?vatrate($product->fourn_remise_percent):'')).'"> %';
print '</td>';
print '</tr>';
// Charges ????
@ -407,6 +413,7 @@ if ($id || $ref)
// Charges ????
if (! empty($conf->margin->enabled)) print '<td align="right">'.$langs->trans("Charges").'</td>';
print_liste_field_titre($langs->trans("UnitPriceHT"),$_SERVER["PHP_SELF"],"pfp.unitprice","",$param,'align="right"',$sortfield,$sortorder);
print '<td class="liste_titre" align="right">'.$langs->trans("DiscountQtyMin").'</td>';
// Charges ????
if (! empty($conf->margin->enabled)) print '<td align="right">'.$langs->trans("UnitCharges").'</td>';
print '<td class="liste_titre"></td>';
@ -466,6 +473,11 @@ if ($id || $ref)
print price($productfourn->fourn_unitprice);
//print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):"&nbsp;");
print '</td>';
// Discount
print '<td align="right">';
print vatrate($productfourn->fourn_remise_percent, $langs);
print '</td>';
// Unit Charges ???
if (! empty($conf->margin->enabled))