add charges to supplier prices

This commit is contained in:
Christophe Battarel 2012-07-19 15:36:25 +02:00
parent 55d547348a
commit 34132de71d
3 changed files with 234 additions and 85 deletions

View File

@ -3,6 +3,7 @@
* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2009-2012 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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
@ -136,105 +137,135 @@ class ProductFournisseur extends Product
* @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 string $charges costs affering to product
* @param Societe $fourn Supplier
* @param int $availability Product availability
* @param string $ref_fourn Supplier ref
* @param float $tva_tx VAT rate
* @return int >0 if KO, >0 if OK
*/
function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx)
function update_buyprice($qty, $buyprice, $charges, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx)
{
global $conf,$mysoc;
// Clean parameter
$buyprice=price2num($buyprice);
$charges=price2num($charges);
$qty=price2num($qty);
if (empty($availability)) $availability=0;
$error=0;
$error=0;
if ($price_base_type == 'TTC')
{
$ttx = get_default_tva($fourn,$mysoc,$this->id);
$buyprice = $buyprice/(1+($ttx/100));
}
$unitBuyPrice = price2num($buyprice/$qty,'MU');
$unitCharges = price2num($charges/$qty,'MU');
$now=dol_now();
$this->db->begin();
if ($this->product_fourn_price_id)
{
// Supprime prix courant du fournisseur pour cette quantite
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE rowid = ".$this->product_fourn_price_id;
$resql=$this->db->query($sql);
if ($resql < 0) $error++;
$sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " SET fk_user = " . $user->id." ,";
$sql.= " price = ".price2num($buyprice).",";
$sql.= " quantity = ".$qty.",";
$sql.= " charges = ".price2num($charges).",";
$sql.= " unitprice = ".$unitBuyPrice.",";
$sql.= " unitcharges = ".$unitCharges.",";
$sql.= " tva_tx = ".$tva_tx.",";
$sql.= " fk_availability = ".$availability.",";
$sql.= " entity = ".$conf->entity;
$sql .= " WHERE rowid = ".$this->product_fourn_price_id;
$resql = $this->db->query($sql) ;
if ($resql)
{
$this->db->commit();
return 0;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -2;
}
}
if (! $error)
{
if ($price_base_type == 'TTC')
{
//$ttx = get_default_tva($fourn,$mysoc,$this->id);
//$buyprice = $buyprice/(1+($ttx/100));
$buyprice = $buyprice/(1+($tva_tx/100));
}
$unitBuyPrice = price2num($buyprice/$qty,'MU');
$now=dol_now();
// Ajoute prix courant du fournisseur pour cette quantite
$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.= " values('".$this->db->idate($now)."',";
$sql.= " ".$this->id.",";
$sql.= " ".$fourn->id.",";
$sql.= " '".$this->db->escape($ref_fourn)."',";
$sql.= " ".$user->id.",";
$sql.= " ".price2num($buyprice).",";
$sql.= " ".$qty.",";
$sql.= " ".$unitBuyPrice.",";
$sql.= " ".$tva_tx.",";
$sql.= " ".$availability.",";
$sql.= $conf->entity;
$sql.=")";
dol_syslog(get_class($this)."::update_buyprice sql=".$sql);
if (! $this->db->query($sql))
{
$error++;
}
/*if (! $error)
{
// Ajoute modif dans table log
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
$sql.= "datec, fk_product_fournisseur,fk_user,price,quantity)";
$sql.= "values('".$this->db->idate($now)."',";
$sql.= " ".$this->product_fourn_id.",";
$sql.= " ".$user->id.",";
$sql.= " ".price2num($buyprice).",";
$sql.= " ".$qty;
$sql.=")";
$resql=$this->db->query($sql);
if (! $resql)
{
$error++;
}
}
*/
if (! $error)
{
$this->db->commit();
return 0;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -1;
}
// Supprime prix courant du fournisseur pour cette quantite
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
$sql.= " WHERE rowid = ".$this->product_fourn_price_id;
$resql=$this->db->query($sql);
if ($resql)
{
// Ajoute prix courant du fournisseur pour cette quantite
$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.= " values('".$this->db->idate($now)."',";
$sql.= " ".$this->id.",";
$sql.= " ".$fourn->id.",";
$sql.= " '".$this->db->escape($ref_fourn)."',";
$sql.= " ".$user->id.",";
$sql.= " ".price2num($buyprice).",";
$sql.= " ".$qty.",";
$sql.= " ".$unitBuyPrice.",";
$sql.= " ".$tva_tx.",";
$sql.= " ".$availability.",";
$sql.= $conf->entity;
$sql.=")";
dol_syslog(get_class($this)."::update_buyprice sql=".$sql);
if (! $this->db->query($sql))
{
$error++;
}
/*if (! $error)
{
// Ajoute modif dans table log
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log(";
$sql.= "datec, fk_product_fournisseur,fk_user,price,quantity)";
$sql.= "values('".$this->db->idate($now)."',";
$sql.= " ".$this->product_fourn_id.",";
$sql.= " ".$user->id.",";
$sql.= " ".price2num($buyprice).",";
$sql.= " ".$qty;
$sql.=")";
$resql=$this->db->query($sql);
if (! $resql)
{
$error++;
}
}
*/
if (! $error)
{
$this->db->commit();
return 0;
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->error()." sql=".$sql;
$this->db->rollback();
return -1;
}
}
}
/**
@ -246,7 +277,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.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product";
$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;
@ -260,8 +291,10 @@ class ProductFournisseur extends Product
$this->product_fourn_price_id = $rowid;
$this->fourn_ref = $obj->ref_fourn;
$this->fourn_price = $obj->price;
$this->fourn_qty = $obj->quantity;
$this->fourn_unitprice = $obj->unitprice;
$this->fourn_charges = $obj->charges;
$this->fourn_qty = $obj->quantity;
$this->fourn_unitprice = $obj->unitprice;
$this->fourn_unitcharges = $obj->unitcharges;
$this->tva_tx = $obj->tva_tx;
$this->product_id = $obj->fk_product; // deprecated
$this->fk_product = $obj->fk_product;
@ -294,7 +327,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";
$sql.= " pfp.price, pfp.quantity, pfp.unitprice, 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).")";
@ -320,6 +353,8 @@ class ProductFournisseur extends Product
$prodfourn->fourn_price = $record["price"];
$prodfourn->fourn_qty = $record["quantity"];
$prodfourn->fourn_unitprice = $record["unitprice"];
$prodfourn->fourn_charges = $record["charges"];
$prodfourn->fourn_unitcharges = $record["unitcharges"];
$prodfourn->fourn_tva_tx = $record["tva_tx"];
$prodfourn->fourn_id = $record["fourn_id"];
$prodfourn->fourn_name = $record["supplier_name"];
@ -374,7 +409,7 @@ class ProductFournisseur extends Product
$sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
$sql.= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
$sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx";
$sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges, pfp.unitcharges";
$sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
$sql.= " AND pfp.fk_product = ".$prodid;
@ -393,6 +428,8 @@ class ProductFournisseur extends Product
$this->fourn_price = $record["price"];
$this->fourn_qty = $record["quantity"];
$this->fourn_unitprice = $record["unitprice"];
$this->fourn_charges = $record["charges"];
$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

@ -0,0 +1,92 @@
<?php
/* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
require("../../main.inc.php");
$prices = array();
$langs->load('stocks');
$sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,";
$sql.= " pfp.ref_fourn,";
$sql.= " pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.unitprice, pfp.charges, pfp.unitcharges,";
$sql.= " s.nom";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = pfp.fk_product";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = pfp.fk_soc";
$sql.= " WHERE pfp.fk_product = ".$_REQUEST['idprod'];
$sql.= " AND p.tobuy = 1";
$sql.= " AND s.fournisseur = 1";
$sql.= " ORDER BY s.nom, pfp.ref_fourn DESC";
dol_syslog("Form::select_product_fourn_price sql=".$sql,LOG_DEBUG);
$result=$db->query($sql);
if ($result)
{
$num = $db->num_rows($result);
if ($num)
{
$i = 0;
while ($i < $num)
{
$objp = $db->fetch_object($result);
$label = $objp->nom.' - '.$objp->ref_fourn.' - ';
if ($objp->quantity == 1)
{
$label.= price($objp->fprice);
$label.= $langs->trans("Currency".$conf->monnaie)."/";
$price = $objp->fprice;
}
$label.= $objp->quantity.' ';
if ($objp->quantity == 1)
{
$label.= strtolower($langs->trans("Unit"));
}
else
{
$label.= strtolower($langs->trans("Units"));
}
if ($objp->quantity > 1)
{
$label.=" - ";
$label.= price($objp->unitprice).$langs->trans("Currency".$conf->monnaie)."/".strtolower($langs->trans("Unit"));
$price = $objp->unitprice;
}
if ($objp->unitcharges > 0 && ($conf->global->MARGIN_TYPE == "2")) {
$label.=" + ";
$label.= price($objp->unitcharges).$langs->trans("Currency".$conf->monnaie);
$price += $objp->unitcharges;
}
if ($objp->duration) $label .= " - ".$objp->duration;
$prices[] = array("id" => $objp->idprodfournprice, "price" => price($price,0,'',0), "label" => $label);
$i++;
}
$db->free($result);
}
}
echo json_encode($prices);
?>

View File

@ -4,6 +4,7 @@
* Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
* Copyright (C) 2005-2012 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
*
* 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
@ -151,7 +152,10 @@ if ($action == 'updateprice' && $_POST["cancel"] <> $langs->trans("Cancel"))
$supplier=new Fournisseur($db);
$result=$supplier->fetch($id_fourn);
$ret=$product->update_buyprice($quantity, $_POST["price"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx);
if (isset($_POST['ref_fourn_price_id']))
$product->fetch_product_fournisseur_price($_POST['ref_fourn_price_id']);
$ret=$product->update_buyprice($quantity, $_POST["price"], $_POST["charges"], $user, $_POST["price_base_type"], $supplier, $_POST["oselDispo"], $ref_fourn, $tva_tx);
if ($ret < 0)
{
$error++;
@ -267,7 +271,7 @@ if ($id || $ref)
print '<table class="border" width="100%">';
print '<tr><td class="fieldrequired">'.$langs->trans("Supplier").'</td><td colspan="3">';
print '<tr><td class="fieldrequired">'.$langs->trans("Supplier").'</td><td colspan="5">';
if ($_GET["rowid"])
{
$supplier=new Fournisseur($db);
@ -292,7 +296,7 @@ if ($id || $ref)
print '</td></tr>';
// Ref supplier
print '<tr><td class="fieldrequired">'.$langs->trans("SupplierRef").'</td><td colspan="3">';
print '<tr><td class="fieldrequired">'.$langs->trans("SupplierRef").'</td><td colspan="5">';
if ($_GET["rowid"])
{
print $product->fourn_ref;
@ -344,6 +348,10 @@ if ($id || $ref)
print '</td>';
print '</tr>';
print '<td>'.$langs->trans("Charges").'</td>';
print '<td><input class="flat" name="charges" size="8" value="'.($_POST["charges"]?$_POST["charges"]:price($product->fourn_charges)).'">';
print '</td>';
print '</table>';
print '<br><center><input class="button" type="submit" value="'.$langs->trans("Save").'">';
@ -389,7 +397,9 @@ if ($id || $ref)
print_liste_field_titre($langs->trans("QtyMin"),$_SERVER["PHP_SELF"],"pfp.quantity","",$param,'align="right"',$sortfield,$sortorder);
print '<td class="liste_titre" align="right">'.$langs->trans("VATRate").'</td>';
print '<td class="liste_titre" align="right">'.$langs->trans("PriceQtyMinHT").'</td>';
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 align="right">'.$langs->trans("UnitCharges").'</td>';
print '<td class="liste_titre"></td>';
print "</tr>\n";
@ -434,12 +444,22 @@ if ($id || $ref)
print $productfourn->fourn_price?price($productfourn->fourn_price):"";
print '</td>';
// Charges
print '<td align="right">';
print $productfourn->fourn_charges?price($productfourn->fourn_charges):"";
print '</td>';
// Unit price
print '<td align="right">';
print price($productfourn->fourn_unitprice);
//print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):"&nbsp;");
print '</td>';
// Unit Charges
print '<td align="right">';
print $productfourn->fourn_unitcharges?price($productfourn->fourn_unitcharges) : ($productfourn->fourn_qty?price($productfourn->fourn_charges/$productfourn->fourn_qty):"&nbsp;");
print '</td>';
// Modify-Remove
print '<td align="center">';
if ($user->rights->produit->creer || $user->rights->service->creer)