diff --git a/htdocs/fourn/commande/fiche.php b/htdocs/fourn/commande/fiche.php index a384556b7a6..a63ec907122 100644 --- a/htdocs/fourn/commande/fiche.php +++ b/htdocs/fourn/commande/fiche.php @@ -90,7 +90,7 @@ if ($_REQUEST['action'] == 'setremisepercent' && $user->rights->fournisseur->com */ if ($_POST['action'] == 'addligne' && $user->rights->fournisseur->commande->creer) { - if ($_POST['qty'] && (($_POST['pu'] && $_POST['desc']) || $_POST['idprod'])) + if ($_POST['qty'] && (($_POST['pu'] && $_POST['desc']) || $_POST['idprod'])) { $commande = new CommandeFournisseur($db); $ret=$commande->fetch($_POST["id"]); @@ -105,7 +105,7 @@ if ($_POST['action'] == 'addligne' && $user->rights->fournisseur->commande->cree // Ecrase $pu par celui du produit // Ecrase $desc par celui du produit - // Ecrase $txtva par celui du produit + // Ecrase $txtva par celui du produit if ($_POST["idprod"] > 0) { $prod = new Product($db, $_POST['idprod']); @@ -113,7 +113,7 @@ if ($_POST['action'] == 'addligne' && $user->rights->fournisseur->commande->cree $libelle = $prod->libelle; - // La description de la ligne est celle saisie ou + // La description de la ligne est celle saisie ou // celle du produit si (non saisi + PRODUIT_CHANGE_PROD_DESC défini) // \todo Ne faut-il pas rendre $conf->global->PRODUIT_CHANGE_PROD_DESC toujours a on $desc=$_POST['np_desc']; @@ -137,7 +137,8 @@ if ($_POST['action'] == 'addligne' && $user->rights->fournisseur->commande->cree $_POST['qty'], $tva_tx, $_POST['idprod'], - $_POST['remise_percent'] + $_POST['remise_percent'], + 'HT' ); if ($result > 0) @@ -535,7 +536,7 @@ else */ if ($_GET["action"] == 'commande') { - $date_com = mktime(0,0,0,$_POST["remonth"],$_POST["reday"],$_POST["reyear"]); + $date_com = dolibarr_mktime(0,0,0,$_POST["remonth"],$_POST["reday"],$_POST["reyear"]); $html->form_confirm("fiche.php?id=".$commande->id."&datecommande=".$date_com."&methode=".$_POST["methodecommande"], $langs->trans("MakeOrder"),$langs->trans("ConfirmMakeOrder",dolibarr_print_date($date_com,'day')),"confirm_commande"); print '
'; @@ -657,7 +658,7 @@ else print ' - '.$objp->label; if ($objp->description) print '
'.nl2br($objp->description); print ""; - print ''.$objp->tva_tx.'%'; + print ''.vatrate($objp->tva_tx).'%'; print ''.$objp->qty.''; if ($objp->remise_percent > 0) { diff --git a/htdocs/fourn/fournisseur.class.php b/htdocs/fourn/fournisseur.class.php index d11412df47a..aec0dfc2cdf 100644 --- a/htdocs/fourn/fournisseur.class.php +++ b/htdocs/fourn/fournisseur.class.php @@ -29,7 +29,7 @@ require_once(DOL_DOCUMENT_ROOT."/societe.class.php"); require_once(DOL_DOCUMENT_ROOT."/fourn/fournisseur.commande.class.php"); -require_once(DOL_DOCUMENT_ROOT."/product.class.php"); +require_once(DOL_DOCUMENT_ROOT."/fourn/fournisseur.product.class.php"); /** @@ -169,7 +169,7 @@ class Fournisseur extends Societe if ($idc > 0) { - $prod = new Product($this->db); + $prod = new ProductFournisseur($this->db); $prod->fetch($fk_product); $prod->fetch_fourn_data($this->id); diff --git a/htdocs/fourn/fournisseur.commande.class.php b/htdocs/fourn/fournisseur.commande.class.php index b1c36c8bb28..cf21451c74e 100644 --- a/htdocs/fourn/fournisseur.commande.class.php +++ b/htdocs/fourn/fournisseur.commande.class.php @@ -637,100 +637,106 @@ class CommandeFournisseur extends Commande } } - /** - * \brief Ajoute une ligne de commande - * \param desc Description - * \param pu Prix unitaire - * \param qty Quantité - * \param txtva Taux tva - * \param fk_product Id produit - * \param remise_percent Remise - * \param int <0 si ko, >0 si ok - */ - function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) - { - global $langs; - - $qty = price2num($qty); - $pu = price2num($pu); - $desc = trim($desc); - $remise_percent = price2num($remise_percent); - - dolibarr_syslog("Fournisseur.Commande.class::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); + /** + * \brief Ajoute une ligne de commande + * \param desc Description + * \param pu Prix unitaire + * \param qty Quantité + * \param txtva Taux tva + * \param fk_product Id produit + * \param remise_percent Remise + * \param price_base_type HT or TTC + * \param int <0 si ko, >0 si ok + */ + function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $price_base_type='HT') + { + global $langs; + + // Clean parameters + $qty = price2num($qty); + $pu = price2num($pu); + $desc = trim($desc); + $remise_percent = price2num($remise_percent); + + dolibarr_syslog("Fournisseur.Commande.class::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent"); - if ($qty < 1 && ! $fk_product) - { - $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Product")); - return -1; - } - - if ($this->brouillon) - { - $this->db->begin(); - - if ($fk_product > 0) - { - $prod = new Product($this->db, $fk_product); - if ($prod->fetch($fk_product) > 0) - { - $result=$prod->get_buyprice($this->fourn_id,$qty); - if ($result > 0) - { - $label = $prod->libelle; - $desc = $prod->description; - $txtva = $prod->tva_tx; - $pu = $prod->fourn_pu; - $ref = $prod->ref; - } - if ($result == 0 || $result == -1) - { - $this->error="Aucun tarif trouvé pour cette quantité. Quantité saisie insuffisante ?"; - $this->db->rollback(); - dolibarr_syslog("Fournisseur.commande.class::addline result=".$result." - ".$this->error); - return -1; - } - if ($result < -1) - { - $this->error=$prod->error; - $this->db->rollback(); - dolibarr_syslog("Fournisseur.commande.class::addline result=".$result." - ".$this->error); - return -1; - } - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - - $remise = 0; - $price = price2num($pu); - $subprice = $price; - if ($remise_percent > 0) - { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet (fk_commande,label,description,fk_product, price, qty, tva_tx, remise_percent, subprice, remise, ref)"; - $sql .= " VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',".$fk_product.",".price2num($price).", '$qty', $txtva, $remise_percent,'".price2num($subprice)."','".price2num($remise)."','".$ref."') ;"; - dolibarr_syslog('Fournisseur.commande.class::addline sql='.$sql); - $resql=$this->db->query($sql); - if ($resql) - { - $this->update_price(); + if ($qty < 1 && ! $fk_product) + { + $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Product")); + return -1; + } + + if ($this->brouillon) + { + $this->db->begin(); + + if ($fk_product > 0) + { + $prod = new Product($this->db, $fk_product); + if ($prod->fetch($fk_product) > 0) + { + $result=$prod->get_buyprice($this->fourn_id,$qty); + if ($result > 0) + { + $label = $prod->libelle; + $desc = $prod->description; + $txtva = $prod->tva_tx; + $pu = $prod->fourn_pu; + $ref = $prod->ref; + } + if ($result == 0 || $result == -1) + { + $this->error="Aucun tarif trouvé pour cette quantité. Quantité saisie insuffisante ?"; + $this->db->rollback(); + dolibarr_syslog("Fournisseur.commande.class::addline result=".$result." - ".$this->error); + return -1; + } + if ($result < -1) + { + $this->error=$prod->error; + $this->db->rollback(); + dolibarr_syslog("Fournisseur.commande.class::addline result=".$result." - ".$this->error); + return -1; + } + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + + $subprice = price2num($pu,'MU'); + + // Champ obsolete + $remise = 0; + $price = $subprice; + if ($remise_percent > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet (fk_commande,label,description,fk_product, price, qty, tva_tx, remise_percent, subprice, remise, ref)"; + $sql .= " VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',".$fk_product.",".price2num($price,'MU').", '$qty', $txtva, $remise_percent,'".price2num($subprice,'MU')."','".price2num($remise)."','".$ref."') ;"; + dolibarr_syslog('Fournisseur.commande.class::addline sql='.$sql); + $resql=$this->db->query($sql); + if ($resql) + { + $this->update_price(); - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - return -1; - } - } - } + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -1; + } + } + } + + /** * Dispatch un element de la commande dans un stock * diff --git a/htdocs/fourn/fournisseur.product.class.php b/htdocs/fourn/fournisseur.product.class.php index 51a39d94922..c23f454a118 100644 --- a/htdocs/fourn/fournisseur.product.class.php +++ b/htdocs/fourn/fournisseur.product.class.php @@ -27,89 +27,281 @@ \version $Revision$ */ +require_once DOL_DOCUMENT_ROOT."/product.class.php"; +require_once DOL_DOCUMENT_ROOT."/fourn/fournisseur.class.php"; + /** \class Product \brief Classe permettant la gestion des produits prédéfinis */ - -require_once DOL_DOCUMENT_ROOT."/product.class.php"; -require_once DOL_DOCUMENT_ROOT."/fourn/fournisseur.product.class.php"; - class ProductFournisseur extends Product { - var $db ; + var $db ; - var $id ; - var $fourn_ref; - var $fourn; - - function ProductFournisseur($db) - { - $this->db = $db; - - $this->fourn = new Fournisseur($this->db); - } - - function fetch ($id, $id_fourn) - { - Product::Fetch($id); - $this->fourn->fetch($id_fourn); + var $id ; + var $fourn_ref; + var $fourn; - $sql = "SELECT ref_fourn"; - $sql .= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql .=" WHERE fk_soc = ".$this->fourn->id; - $sql .= " AND fk_product = ".$this->id; + function ProductFournisseur($db) + { + $this->db = $db; + } - $resql = $this->db->query($sql) ; - - if ( $resql ) - { - $result = $this->db->fetch_array($resql); - $this->fourn_ref = $result["ref_fourn"]; - $this->db->free($resql); - } - else - { - print "Errir"; - } + /** + * \brief Délie un fournisseur au produit/service + * \param user utilisateur qui défait le lien + * \param id_fourn id du fournisseur + * \return int < 0 si erreur, > 0 si ok + */ + function remove_fournisseur($id_fourn) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur "; + $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn; - return 0; - } + dolibarr_syslog("ProductFournisseur::remove_fournisseur sql=".$sql); + $resql=$this->db->query($sql); + if ($resql) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + return -1; + } + } - /* - * - * - */ - function get_buyprice($qty) - { - Product::get_buyprice($this->fourn->id, $qty); - } - /* - * - * - */ - function update($ref, $qty, $buyprice, $user) - { - $this->fourn_ref = $ref; + function remove_product_fournisseur($rowid) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE rowid = ".$rowid; - /* Mise à jour du prix */ + dolibarr_syslog("ProductFournisseur::remove_product_fournisseur sql=".$sql); + $resql = $this->db->query($sql); + if ($resql) + { + return 1; + } + else + { + return -1; + } + } - Product::update_buyprice($this->fourn->id, $qty, $buyprice, $user); + /* + * \return int <0 si KO, 0 si non trouve, >0 si efface + */ + function remove_product_fournisseur_price($rowid) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql.= " WHERE rowid = ".$rowid; - /* Mise à jour de la référence */ + dolibarr_syslog("ProductFournisseur::remove_product_fournisseur_price sql=".$sql); + $resql = $this->db->query($sql); + if ($resql) + { + if ($this->db->affected_rows() > 0) return 1; + else return 0; + } + else + { + return -1; + } + } + + + /* + * + * + */ + function update($ref, $qty, $buyprice, $user) + { + $this->fourn_ref = $ref; - $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur "; - $sql .= " SET ref_fourn = '" . $this->fourn_ref ."'"; - $sql .= " WHERE fk_product = " . $this->id; - $sql .=" AND fk_soc = ".$this->fourn->id; + /* Mise à jour du prix */ - $resql = $this->db->query($sql) ; - } + $this->update_buyprice($this->fourn->id, $qty, $buyprice, $user); + /* Mise à jour de la référence */ + + $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur "; + $sql .= " SET ref_fourn = '" . $this->fourn_ref ."'"; + $sql .= " WHERE fk_product = " . $this->id; + $sql .=" AND fk_soc = ".$this->fourn->id; + + $resql = $this->db->query($sql) ; + } + + + /** + * \brief Modifie le prix d'achat pour un fournisseur + * \param id_fourn Id du fournisseur + * \param qty Quantite pour lequel le prix est valide + * \param buyprice Prix d'achat pour la quantité + * \param user Objet user de l'utilisateur qui modifie + * \param price_base_type HT or TTC + */ + function update_buyprice($id_fourn, $qty, $buyprice, $user, $price_base_type='HT') + { + $error=0; + $this->db->begin(); + + // Supprime prix courant du fournisseur pour cette quantité + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price "; + $sql .= " WHERE "; + $sql .= " fk_product = ".$this->id; + $sql .= " AND fk_soc = ".$id_fourn; + $sql .= " AND quantity = ".$qty; + + if ($this->db->query($sql)) + { + // Ajoute prix courant du fournisseur pour cette quantité + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql .= " SET datec = now()"; + $sql .= " ,fk_product = ".$this->id; + $sql .= " ,fk_soc = ".$id_fourn; + $sql .= " ,fk_user = ".$user->id; + $sql .= " ,price = ".price2num($buyprice); + $sql .= " ,quantity = ".$qty; + $sql .= " ,unitprice = ".price2num($buyprice/$qty,'MU'); + + dolibarr_syslog("ProductFournisseur::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 .= " SET datec = now()"; + $sql .= " ,fk_product = ".$this->id; + $sql .= " ,fk_soc = ".$id_fourn; + $sql .= " ,fk_user = ".$user->id; + $sql .= " ,price = ".price2num($buyprice); + $sql .= " ,quantity = ".$qty; + + if (! $this->db->query($sql)) + { + $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; + } + } + + + /** + \brief Modifie le prix d'achat pour un fournisseur par la référecne du produit chez le fournisseur + \param id_fourn Id du fournisseur + \param product_fourn_ref Ref du produit chez le fournisseur + \param qty Quantite pour lequel le prix est valide + \param buyprice Prix d'achat pour la quantité + \param user Objet user de l'utilisateur qui modifie + \return int <0 si KO, >0 si OK + */ + function UpdateBuyPriceByFournRef($id_fourn, $product_fourn_ref, $qty, $buyprice, $user, $price_base_type='HT') + { + $result=0; + + // Recherche id produit pour cette ref et fournisseur + $sql = "SELECT fk_product"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; + $sql.= " WHERE fk_soc ='".$id_fourn."'"; + $sql.= " AND ref_fourn='".$product_fourn_ref."'"; + + if ($this->db->query($sql)) + { + if ($obj = $this->db->fetch_object($resql)) + { + // Met a jour prix pour la qté + $this->id = $obj->fk_product; + $result = $this->update_buyprice($id_fourn, $qty, $buyprice, $user, $price_base_type); + } + } + + return $result; + } + + + /** + * \brief Charge les informations relatives à un fournisseur + * \param fournid id du fournisseur + * \return int < 0 si erreur, > 0 si ok + */ + function fetch_fourn_data($fournid) + { + $sql = "SELECT rowid, ref_fourn"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur "; + $sql.= " WHERE fk_product = ".$this->id; + $sql.= " AND fk_soc = ".$fournid; + + dolibarr_syslog("Product::fetch_fourn_data sql=".$sql); + $result = $this->db->query($sql) ; + if ($result) + { + $result = $this->db->fetch_array(); + $this->ref_fourn = $result["ref_fourn"]; + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("Product::fetch_fourn_data error=".$this->error); + return -1; + } + } + + /** + * \brief Charge les informations relatives à un prix de fournisseur + * \param rowid id ligne + * \return int < 0 si erreur, > 0 si ok + */ + function fetch_product_fournisseur_price($rowid) + { + $sql = "SELECT rowid, fk_soc, ref_fourn, price, quantity, unitprice"; + $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql.= " WHERE rowid = ".$rowid; + + dolibarr_syslog("Product::fetch_product_fournisseur_price sql=".$sql); + $resql = $this->db->query($sql) ; + if ($resql) + { + $obj = $this->db->fetch_object($resql); + $this->fourn_ref = $obj->ref_fourn; + $this->fourn_price = $obj->price; + $this->fourn_qty = $obj->qty; + $this->fourn_unitprice = $obj->unitprice; + return 1; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog("Product::fetch_product_fournisseur_price error=".$this->error); + return -1; + } + } } ?> diff --git a/htdocs/fourn/product/fiche.php b/htdocs/fourn/product/fiche.php index 07cff1f35f4..c4c8c39d035 100644 --- a/htdocs/fourn/product/fiche.php +++ b/htdocs/fourn/product/fiche.php @@ -150,7 +150,7 @@ if ($_POST["action"] == 'add_fourn' && $_POST["cancel"] <> $langs->trans("Cancel } if ($_GET["action"] == 'remove_fourn') { - $product = new Product($db); + $product = new ProductFournisseur($db); if( $product->fetch($_GET["id"]) ) { if ($product->remove_fournisseur($user, $_GET["id_fourn"]) > 0) diff --git a/htdocs/fourn/product/fourn.php b/htdocs/fourn/product/fourn.php index 4ea0d43e318..b3a4df31a95 100644 --- a/htdocs/fourn/product/fourn.php +++ b/htdocs/fourn/product/fourn.php @@ -60,8 +60,7 @@ if ($_GET["id"]) { $product = new ProductFournisseur($db); $result = $product->fetch($_GET["id"], $_GET["id_fourn"]); - - $product->get_buyprice(1); + $product->get_buyprice($_GET["id_fourn"],1); } if ( $result == 0) diff --git a/htdocs/html.form.class.php b/htdocs/html.form.class.php index 52d7d696da0..4c36e5621b5 100644 --- a/htdocs/html.form.class.php +++ b/htdocs/html.form.class.php @@ -1114,11 +1114,9 @@ class Form { global $langs,$conf; - $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration"; - //$sql.= ", pf.ref_fourn"; - $sql.= ", pfp.price as fprice, pfp.quantity"; + $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,"; + $sql.= " pfp.price as fprice, pfp.quantity, pfp.unitprice"; $sql.= " FROM ".MAIN_DB_PREFIX."product as p"; - //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur as pf ON p.rowid = pf.fk_product"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp ON p.rowid = pfp.fk_product"; $sql.= " WHERE p.envente = 1"; if ($socid) $sql.= " AND pfp.fk_soc = ".$socid; @@ -1167,12 +1165,12 @@ class Form $opt.= dolibarr_trunc($objp->label,18).' - '; if ($objp->fprice != '') { - $opt.= $objp->fprice; + $opt.= price($objp->fprice); $opt.= $langs->trans("Currency".$conf->monnaie)."/".$objp->quantity.$langs->trans("Units"); if ($objp->quantity > 1) { $opt.=" - "; - $opt.= round($objp->fprice/$objp->quantity,4).$langs->trans("Currency".$conf->monnaie)."/".$langs->trans("Unit"); + $opt.= price($objp->unitprice).$langs->trans("Currency".$conf->monnaie)."/".$langs->trans("Unit"); } if ($objp->duration) $opt .= " - ".$objp->duration; } diff --git a/htdocs/product.class.php b/htdocs/product.class.php index cb9350dcff8..bfe02b4d65b 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -659,7 +659,7 @@ class Product function get_buyprice($fourn_id, $qty) { $result = 0; - $sql = "SELECT pf.price as price, pf.quantity as quantity"; + $sql = "SELECT pf.rowid, pf.price as price, pf.quantity as quantity"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pf"; $sql.= " WHERE pf.fk_soc = ".$fourn_id; $sql.= " AND pf.fk_product =" .$this->id; @@ -719,112 +719,6 @@ class Product } - /** - * \brief Modifie le prix d'achat pour un fournisseur - * \param id_fourn Id du fournisseur - * \param qty Quantite pour lequel le prix est valide - * \param buyprice Prix d'achat pour la quantité - * \param user Objet user de l'utilisateur qui modifie - * \param price_base_type HT or TTC - */ - function update_buyprice($id_fourn, $qty, $buyprice, $user, $price_base_type='HT') - { - $error=0; - $this->db->begin(); - - // Supprime prix courant du fournisseur pour cette quantité - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price "; - $sql .= " WHERE "; - $sql .= " fk_product = ".$this->id; - $sql .= " AND fk_soc = ".$id_fourn; - $sql .= " AND quantity = ".$qty; - - if ($this->db->query($sql)) - { - // Ajoute prix courant du fournisseur pour cette quantité - $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price"; - $sql .= " SET datec = now()"; - $sql .= " ,fk_product = ".$this->id; - $sql .= " ,fk_soc = ".$id_fourn; - $sql .= " ,fk_user = ".$user->id; - $sql .= " ,price = ".price2num($buyprice); - $sql .= " ,quantity = ".$qty; - - if (! $this->db->query($sql)) - { - $error++; - } - - if (! $error) { - // Ajoute modif dans table log - $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_fournisseur_price_log "; - $sql .= " SET datec = now()"; - $sql .= " ,fk_product = ".$this->id; - $sql .= " ,fk_soc = ".$id_fourn; - $sql .= " ,fk_user = ".$user->id; - $sql .= " ,price = ".price2num($buyprice); - $sql .= " ,quantity = ".$qty; - - if (! $this->db->query($sql)) - { - $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; - } - } - - - /** - \brief Modifie le prix d'achat pour un fournisseur par la référecne du produit chez le fournisseur - \param id_fourn Id du fournisseur - \param product_fourn_ref Ref du produit chez le fournisseur - \param qty Quantite pour lequel le prix est valide - \param buyprice Prix d'achat pour la quantité - \param user Objet user de l'utilisateur qui modifie - \return int <0 si KO, >0 si OK - */ - function UpdateBuyPriceByFournRef($id_fourn, $product_fourn_ref, $qty, $buyprice, $user, $price_base_type='HT') - { - $result=0; - - // Recherche id produit pour cette ref et fournisseur - $sql = "SELECT fk_product"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur"; - $sql.= " WHERE fk_soc ='".$id_fourn."'"; - $sql.= " AND ref_fourn='".$product_fourn_ref."'"; - - if ($this->db->query($sql)) - { - if ($obj = $this->db->fetch_object($resql)) - { - // Met a jour prix pour la qté - $this->id = $obj->fk_product; - $result = $this->update_buyprice($id_fourn, $qty, $buyprice, $user, $price_base_type); - } - } - - return $result; - } - - /** \brief Modifie le prix d'un produit/service \param id Id du produit/service à modifier @@ -919,7 +813,8 @@ class Product if ($id) $sql.= " WHERE rowid = '".$id."'"; if ($ref) $sql.= " WHERE ref = '".addslashes($ref)."'"; - $result = $this->db->query($sql) ; + dolibarr_syslog("Product::fetch sql=".$sql); + $result = $this->db->query($sql); if ( $result ) { $result = $this->db->fetch_array(); @@ -1742,28 +1637,6 @@ class Product } } - /** - * \brief Délie un fournisseur au produit/service - * \param user utilisateur qui défait le lien - * \param id_fourn id du fournisseur - * \return int < 0 si erreur, > 0 si ok - */ - function remove_fournisseur($user, $id_fourn) - { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur "; - $sql.= " WHERE fk_product = $this->id AND fk_soc = $id_fourn;"; - - if ($this->db->query($sql) ) - { - return 1; - } - else - { - dolibarr_print_error($this->db); - return -1; - } - } - /** * \brief Recopie les prix d'un produit/service sur un autre * \param fromId Id produit source @@ -2245,30 +2118,6 @@ class Product } } - /** - * \brief Charge les informations relatives à un fournisseur - * \param fournid id du fournisseur - * \return int < 0 si erreur, > 0 si ok - */ - function fetch_fourn_data($fournid) - { - $sql = "SELECT rowid, ref_fourn"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur "; - $sql.= " WHERE fk_product = ".$this->id; - $sql.= " AND fk_soc = ".$fournid; - $result = $this->db->query($sql) ; - - if ($result) - { - $result = $this->db->fetch_array(); - $this->ref_fourn = $result["ref_fourn"]; - return 1; - } - else { - return -1; - } - } - /** * \brief Déplace fichier uploadé sous le nom $files dans le répertoire sdir diff --git a/htdocs/product/fournisseurs.php b/htdocs/product/fournisseurs.php index fffef74de0b..5a2721a4769 100644 --- a/htdocs/product/fournisseurs.php +++ b/htdocs/product/fournisseurs.php @@ -33,7 +33,7 @@ require("./pre.inc.php"); require_once(DOL_DOCUMENT_ROOT."/lib/product.lib.php"); require_once(DOL_DOCUMENT_ROOT."/propal.class.php"); require_once(DOL_DOCUMENT_ROOT."/facture.class.php"); -require_once(DOL_DOCUMENT_ROOT."/product.class.php"); +require_once DOL_DOCUMENT_ROOT."/fourn/fournisseur.product.class.php"; $langs->load("products"); $langs->load("suppliers"); @@ -57,24 +57,19 @@ if ($conf->use_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT && $_POST['id * Actions */ -if ($_GET["action"] == 'remove_fourn') +if ($_GET["action"] == 'remove_pf') { - $product = new Product($db); - if( $product->fetch($_GET["id"]) ) + $product = new ProductFournisseur($db); + if ($product->fetch($_GET["id"]) > 0) { - if ($_GET["qty"]) { // On supprime une quantité - if ($product->remove_price($user, $_GET["id_fourn"], $_GET["qty"]) > 0) - { - $_GET["action"] = ''; - $mesg = '
'.$langs->trans("PriceRemoved").'.
'; - } - else - { - $_GET["action"] = ''; - } - } - else { // On supprime un fournisseur - if ($product->remove_fournisseur($user, $_GET["id_fourn"]) > 0) + if ($_GET["rowid"] && $product->remove_product_fournisseur_price($_GET["rowid"]) > 0) + { + $_GET["action"] = ''; + $mesg = '
'.$langs->trans("PriceRemoved").'.
'; + } + else + { + if ($product->remove_fournisseur($_GET["socid"]) > 0) { $_GET["action"] = ''; $mesg = '
'.$langs->trans("SupplierRemoved").'.
'; @@ -90,8 +85,9 @@ if ($_GET["action"] == 'remove_fourn') if ($_POST["action"] == 'updateprice' && $_POST["cancel"] <> $langs->trans("Cancel")) { - $product = new Product($db); - if( $product->fetch($_GET["id"]) ) + $product = new ProductFournisseur($db); + $result=$product->fetch($_REQUEST["id"]); + if ($result) { $db->begin(); @@ -105,45 +101,47 @@ if ($_POST["action"] == 'updateprice' && $_POST["cancel"] <> $langs->trans("Canc $error++; $mesg='
'.$product->error.'
'; } + + if ($_POST["qty"]) + { + if ($_POST["price"] >= 0) + { + $ret=$product->update_buyprice($_POST["id_fourn"], $_POST["qty"], $_POST["price"], $user); + if ($ret < 0) + { + $error++; + $mesg='
'.$product->error.'
'; + if ($ret == -2) + { + $mesg='
'.$langs->trans("ProductHasAlreadyReferenceInThisSupplier").'
'; + } + } + } + else + { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'
'; + } + } + else + { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'
'; + } } else { $error++; $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref")).'
'; } - - if (! $error && $_POST["qty"]) - { - if ($_POST["price"] >= 0) - { - $ret=$product->update_buyprice($_POST["id_fourn"], $_POST["qty"], $_POST["price"], $user); - if ($ret < 0) - { - $error++; - $mesg='
'.$product->error.'
'; - if ($ret == -2) - { - $mesg='
'.$langs->trans("ProductHasAlreadyReferenceInThisSupplier").'
'; - } - } - } - else - { - $error++; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'
'; - } - } - else - { - $error++; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'
'; - } + if (! $error) { $db->commit(); } - else { + else + { $db->rollback(); } } @@ -166,9 +164,10 @@ if ($_GET["id"] || $_GET["ref"]) { if ($_GET["action"] <> 're-edit') { - $product = new Product($db); + $product = new ProductFournisseur($db); if ($_GET["ref"]) $result = $product->fetch('',$_GET["ref"]); - if ($_GET["id"]) $result = $product->fetch($_GET["id"]); + if ($_GET["id"]) $result = $product->fetch($_GET["id"]); + $product->fetch_fourn_data($_GET["socid"]); llxHeader("","",$langs->trans("CardProduct".$product->type)); } @@ -223,7 +222,7 @@ if ($_GET["id"] || $_GET["ref"]) { $langs->load("suppliers"); - if ($_GET["id_fourn"]) { + if ($_GET["rowid"]) { print_fiche_titre($langs->trans("ChangeSupplierPrice")); } else { print_fiche_titre($langs->trans("AddSupplierPrice")); @@ -232,25 +231,45 @@ if ($_GET["id"] || $_GET["ref"]) print '
'; print ''; - if ($_GET["id_fourn"]) { - print ''; - $product->fetch_fourn_data($_GET["id_fourn"]); + if ($_GET["rowid"]) + { + print ''; print ''; - } else { + } + else + { print ''.$langs->trans("Supplier").''; $html=new Form($db); $html->select_societes('','id_fourn','fournisseur=1'); print ''; } - print ''.$langs->trans("Ref").''; - print ''; + print ''.$langs->trans("SupplierRef").''; + if ($_GET["rowid"]) + { + print $product->ref_fourn; + } + else + { + print ''; + } print ''; print ''.$langs->trans("QtyMin").''; $quantity = $_GET["qty"] ? $_GET["qty"] : "1"; - print ''; + print ''; + if ($_GET["rowid"]) + { + print ''; + print $quantity; + } + else + { + print ''; + } + print ''; print ''.$langs->trans("PriceQtyHT").''; - print ''; + $product->fetch_product_fournisseur_price($_GET["rowid"]); + print ''; print ''; print '   '; @@ -272,7 +291,7 @@ if ($_GET["id"] || $_GET["ref"]) if ($user->rights->produit->creer) { print ''; - print $langs->trans("AddSupplier").''; + print $langs->trans("AddSupplierPrice").''; } } @@ -281,14 +300,15 @@ if ($_GET["id"] || $_GET["ref"]) print '
'; - if ($user->rights->fournisseur->lire) { + if ($user->rights->fournisseur->lire) + { // Titre liste des fournisseurs print ''; if ($product->isproduct()) $nblignefour=4; else $nblignefour=4; print ''; - print ''; + print ''; print ''; print ''; print ''; @@ -297,7 +317,8 @@ if ($_GET["id"] || $_GET["ref"]) // Liste des fournisseurs $sql = "SELECT s.nom, s.rowid as socid,"; - $sql.= "pf.ref_fourn, pfp.price, pfp.quantity"; + $sql.= " pf.ref_fourn,"; + $sql.= " pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice"; $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur as pf"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " ON pf.fk_soc = pfp.fk_soc AND pf.fk_product = pfp.fk_product"; @@ -335,15 +356,15 @@ if ($_GET["id"] || $_GET["ref"]) // Prix unitaire print ''; // Modifier-Supprimer print ''; diff --git a/mysql/migration/2.1.0-2.2.0.sql b/mysql/migration/2.1.0-2.2.0.sql index a14ed5bd3fa..3266247016a 100644 --- a/mysql/migration/2.1.0-2.2.0.sql +++ b/mysql/migration/2.1.0-2.2.0.sql @@ -742,6 +742,9 @@ drop table if exists `llx_accountingsystem_det`; update llx_bank set label='(InitialBankBalance)' where fk_type='SOLD' and label in ('Balance','(Balance)','Solde','(Solde)'); +alter table llx_product_fournisseur_price add unitprice double(16,8); +alter table llx_product_fournisseur_price add ref_fourn varchar(30) after fk_soc; +update llx_product_fournisseur_price set unitprice = ROUND(price/quantity,8) where unitprice IS NULL; update llx_fichinter set tms=datec where tms < datec; update llx_fichinter set tms=date_valid where tms < date_valid; diff --git a/mysql/tables/llx_product_fournisseur_price.sql b/mysql/tables/llx_product_fournisseur_price.sql index f121510f524..e25d8987c8a 100644 --- a/mysql/tables/llx_product_fournisseur_price.sql +++ b/mysql/tables/llx_product_fournisseur_price.sql @@ -27,8 +27,10 @@ create table llx_product_fournisseur_price tms timestamp, fk_product integer, fk_soc integer, -- lien sur llx_societe + ref_fourn varchar(30), price double(16,8), quantity double, + unitprice double(16,8), fk_user integer )type=innodb;
'; print $langs->trans("Suppliers").''.$langs->trans("Ref").''.$langs->trans("SupplierRef").''.$langs->trans("QtyMin").''.$langs->trans("PriceQtyHT").''.$langs->trans("UnitPriceHT").''; - print $objp->quantity?price($objp->price / $objp->quantity):" "; + print $objp->unitprice? price($objp->unitprice) : ($objp->quantity?price($objp->price/$objp->quantity):" "); print ''; - if ($user->rights->produit->creer) { - print ''.img_edit().""; - print ''; - print img_disable($langs->trans("Remove")).''; + if ($user->rights->produit->creer) + { + print ''.img_edit().""; + print ''.img_disable($langs->trans("Remove")).''; } print '