diff --git a/htdocs/fourn/class/fournisseur.product.class.php b/htdocs/fourn/class/fournisseur.product.class.php index 4fbb595c6f8..c7fa32d7835 100644 --- a/htdocs/fourn/class/fournisseur.product.class.php +++ b/htdocs/fourn/class/fournisseur.product.class.php @@ -3,6 +3,7 @@ * Copyright (C) 2006-2011 Laurent Destailleur * Copyright (C) 2009-2012 Regis Houssin * Copyright (C) 2011 Juanjo Menent + * Copyright (C) 2012 Christophe Battarel * * 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"]; diff --git a/htdocs/fourn/product/getSupplierPrices.php b/htdocs/fourn/product/getSupplierPrices.php new file mode 100644 index 00000000000..a521140f7bc --- /dev/null +++ b/htdocs/fourn/product/getSupplierPrices.php @@ -0,0 +1,92 @@ + + * + * 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 . + * + */ +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); + +?> diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index 27a27cb98c1..83178537734 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -4,6 +4,7 @@ * Copyright (C) 2004 Eric Seigne * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2010-2011 Juanjo Menent + * Copyright (C) 2012 Christophe Battarel * * 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 ''; - print ''; // Ref supplier - print ''; print ''; + print ''; + print ''; + print '
'.$langs->trans("Supplier").''; + print '
'.$langs->trans("Supplier").''; if ($_GET["rowid"]) { $supplier=new Fournisseur($db); @@ -292,7 +296,7 @@ if ($id || $ref) print '
'.$langs->trans("SupplierRef").''; + print '
'.$langs->trans("SupplierRef").''; if ($_GET["rowid"]) { print $product->fourn_ref; @@ -344,6 +348,10 @@ if ($id || $ref) print '
'.$langs->trans("Charges").'fourn_charges)).'">'; + print '
'; print '
'; @@ -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 ''.$langs->trans("VATRate").''; print ''.$langs->trans("PriceQtyMinHT").''; + print ''.$langs->trans("Charges").''; print_liste_field_titre($langs->trans("UnitPriceHT"),$_SERVER["PHP_SELF"],"pfp.unitprice","",$param,'align="right"',$sortfield,$sortorder); + print ''.$langs->trans("UnitCharges").''; print ''; print "\n"; @@ -434,12 +444,22 @@ if ($id || $ref) print $productfourn->fourn_price?price($productfourn->fourn_price):""; print ''; + // Charges + print ''; + print $productfourn->fourn_charges?price($productfourn->fourn_charges):""; + print ''; + // Unit price print ''; print price($productfourn->fourn_unitprice); //print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):" "); print ''; + // Unit Charges + print ''; + print $productfourn->fourn_unitcharges?price($productfourn->fourn_unitcharges) : ($productfourn->fourn_qty?price($productfourn->fourn_charges/$productfourn->fourn_qty):" "); + print ''; + // Modify-Remove print ''; if ($user->rights->produit->creer || $user->rights->service->creer)