diff --git a/htdocs/fourn/commande/fiche.php b/htdocs/fourn/commande/fiche.php index 254d63e8143..96a9aae3642 100644 --- a/htdocs/fourn/commande/fiche.php +++ b/htdocs/fourn/commande/fiche.php @@ -43,9 +43,11 @@ if ($user->societe_id > 0) $socidp = $user->societe_id; } +$mesg=''; + /* - * + * Actions */ if ($_POST["action"] == 'classin') { @@ -97,8 +99,16 @@ if ($_POST["action"] == 'addligne' && $user->rights->fournisseur->commande->cree 0, $_POST["remise_percent"]); } - Header("Location: fiche.php?id=".$_GET["id"]); - exit; + + if ($result >= 0) + { + Header("Location: fiche.php?id=".$_GET["id"]); + exit; + } + else + { + $mesg='
| '.img_object($langs->trans("ShowProduct"),'product').' '.$objp->ref.' | '; + print ''.img_object($langs->trans("ShowProduct"),'product').' '.$objp->ref.' | '; if ($objp->fk_product > 0) { print ''; diff --git a/htdocs/fourn/fournisseur.commande.class.php b/htdocs/fourn/fournisseur.commande.class.php index d681444cdaf..fae2a736024 100644 --- a/htdocs/fourn/fournisseur.commande.class.php +++ b/htdocs/fourn/fournisseur.commande.class.php @@ -62,66 +62,65 @@ class CommandeFournisseur } - /** - * Lit une commande - * - */ - function fetch ($id) + /** + * Lit une commande + */ + function fetch ($id) { - $sql = "SELECT c.rowid, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva"; - $sql .= ", ".$this->db->pdate("c.date_commande")." as date_commande, c.fk_projet, c.remise_percent, c.source, c.fk_methode_commande "; - $sql .= ", c.note"; - - $sql .= ", cm.libelle as methode_commande"; - - $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c"; -$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_methode_commande_fournisseur as cm ON cm.rowid = c.fk_methode_commande"; - - $sql .= " WHERE c.rowid = ".$id; - $resql = $this->db->query($sql) ; - - if ( $resql ) - { - $obj = $this->db->fetch_object($resql); - - $this->id = $obj->rowid; - $this->ref = $obj->ref; - $this->soc_id = $obj->fk_soc; - $this->fourn_id = $obj->fk_soc; - $this->statut = $obj->fk_statut; - $this->user_author_id = $obj->fk_user_author; - $this->total_ht = $obj->total_ht; - $this->total_tva = $obj->tva; - $this->total_ttc = $obj->total_ttc; - $this->date_commande = $obj->date_commande; // date à laquelle la commande a été transmise - $this->remise_percent = $obj->remise_percent; - $this->methode_commande_id = $obj->fk_methode_commande; - $this->methode_commande = $obj->methode_commande; - - $this->source = $obj->source; - $this->facturee = $obj->facture; - $this->projet_id = $obj->fk_projet; - $this->note = stripslashes($obj->note); - - $this->db->free($resql); - - if ($this->statut == 0) - { - $this->brouillon = 1; - } - - $result = 0; - - } - else - { - dolibarr_syslog("CommandeFournisseur::Fetch Error $sql"); - dolibarr_syslog("CommandeFournisseur::Fetch Error ".$this->db->error()); - $result = -1; - } - - return $result ; + $sql = "SELECT c.rowid, c.date_creation, c.ref, c.fk_soc, c.fk_user_author, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva"; + $sql .= ", ".$this->db->pdate("c.date_commande")." as date_commande, c.fk_projet, c.remise_percent, c.source, c.fk_methode_commande "; + $sql .= ", c.note"; + + $sql .= ", cm.libelle as methode_commande"; + + $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_methode_commande_fournisseur as cm ON cm.rowid = c.fk_methode_commande"; + + $sql .= " WHERE c.rowid = ".$id; + $resql = $this->db->query($sql) ; + if ($resql) + { + $obj = $this->db->fetch_object($resql); + + $this->id = $obj->rowid; + $this->ref = $obj->ref; + $this->soc_id = $obj->fk_soc; + $this->fourn_id = $obj->fk_soc; + $this->statut = $obj->fk_statut; + $this->user_author_id = $obj->fk_user_author; + $this->total_ht = $obj->total_ht; + $this->total_tva = $obj->tva; + $this->total_ttc = $obj->total_ttc; + $this->date_commande = $obj->date_commande; // date à laquelle la commande a été transmise + $this->remise_percent = $obj->remise_percent; + $this->methode_commande_id = $obj->fk_methode_commande; + $this->methode_commande = $obj->methode_commande; + + $this->source = $obj->source; + $this->facturee = $obj->facture; + $this->projet_id = $obj->fk_projet; + $this->note = stripslashes($obj->note); + + $this->db->free($resql); + + if ($this->statut == 0) + { + $this->brouillon = 1; + } + + $result = 0; + + } + else + { + dolibarr_syslog("CommandeFournisseur::Fetch Error $sql"); + dolibarr_syslog("CommandeFournisseur::Fetch Error ".$this->db->error()); + $result = -1; + } + + return $result ; + } /** @@ -467,47 +466,59 @@ $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_methode_commande_fournisseur as cm ON cm */ function addline($desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) { - $qty = ereg_replace(",",".",$qty); - $pu = ereg_replace(",",".",$pu); - $desc=trim($desc); - + global $langs; + + $qty = price2num($qty); + $pu = price2num($pu); + $desc = trim($desc); + $remise_percent = price2num($remise_percent); + dolibarr_syslog("Fournisseur_Commande.class.php::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 (strlen(trim($qty))==0) - { - $qty=1; - } - if ($fk_product > 0) { $prod = new Product($this->db, $fk_product); if ($prod->fetch($fk_product) > 0) { - $prod->get_buyprice($this->fourn_id,$qty); - - $desc = $prod->libelle; - $txtva = $prod->tva_tx; - $pu = $prod->buyprice; - $ref = $prod->ref; + $result=$prod->get_buyprice($this->fourn_id,$qty); + if ($result) + { + $desc = $prod->libelle; + $txtva = $prod->tva_tx; + $pu = $prod->fourn_pu; + $ref = $prod->ref; + } + else + { + $this->error="Aucun tarif trouvé pour cette quantité. Quantité saisie insuffisante ?"; + $this->db->rollback(); + dolibarr_syslog($this->error); + return -1; + } } } $remise = 0; - $price = round(ereg_replace(",",".",$pu), 2); + $price = price2num($pu); $subprice = $price; - if (trim(strlen($remise_percent)) > 0) + 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($desc) . "','" . addslashes($desc) . "',$fk_product,".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."','".$ref."') ;"; - + $sql .= " VALUES ($this->id, '" . addslashes($desc) . "','" . addslashes($desc) . "',".$fk_product.",".price2num($price).", '$qty', $txtva, $remise_percent,'".price2num($subprice)."','".price2num($remise)."','".$ref."') ;"; if ( $this->db->query( $sql) ) { $this->update_price(); diff --git a/htdocs/html.form.class.php b/htdocs/html.form.class.php index af84cbe5084..40a22e5bfe4 100644 --- a/htdocs/html.form.class.php +++ b/htdocs/html.form.class.php @@ -620,7 +620,7 @@ class Form { global $langs,$conf; - $sql = "SELECT p.rowid, p.label, p.ref, p.price, pf.quantity, p.duration"; + $sql = "SELECT p.rowid, p.label, p.ref, p.price as price, pf.price as fprice, pf.quantity, p.duration"; $sql.= " FROM ".MAIN_DB_PREFIX."product as p "; $sql .= " , ".MAIN_DB_PREFIX."product_fournisseur_price as pf "; $sql.= " WHERE p.rowid = pf.fk_product AND pf.fk_soc = ".$socid; @@ -641,11 +641,11 @@ class Form $objp = $this->db->fetch_object($result); $opt = '\n"; diff --git a/htdocs/product.class.php b/htdocs/product.class.php index b26aa912ec8..4a393f224ab 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -18,7 +18,6 @@ * * $Id$ * $Source$ - * */ /** @@ -193,62 +192,60 @@ class Product } - /** - * \brief Mise à jour du produit en base - * \param id id du produit - * \param user utilisateur qui effectue l'insertion - * \return int 1 si ok, -1 si ref deja existante, -2 autre erreur - */ - - function update($id, $user) - { - global $langs; - $langs->load("main"); - $langs->load("products"); - - if (! $this->libelle) $this->libelle = 'LIBELLE MANQUANT'; - - $this->ref = trim(sanitize_string($this->ref)); - $this->libelle = trim($this->libelle); - $this->description = trim($this->description); - $this->note = trim($this->note); - - $sql = "UPDATE ".MAIN_DB_PREFIX."product "; - $sql .= " SET label = '" . addslashes($this->libelle) ."'"; - if ($this->ref) $sql .= ",ref = '" . $this->ref ."'"; - $sql .= ",tva_tx = " . $this->tva_tx ; - $sql .= ",envente = " . $this->envente ; - $sql .= ",seuil_stock_alerte = " . $this->seuil_stock_alerte ; - $sql .= ",description = '" . addslashes($this->description) ."'"; - $sql .= ",note = '" . addslashes($this->note) ."'"; - $sql .= ",duration = '" . $this->duration_value . $this->duration_unit ."'"; - $sql .= " WHERE rowid = " . $id; - - if ( $this->db->query($sql) ) + /** + * \brief Mise à jour du produit en base + * \param id id du produit + * \param user utilisateur qui effectue l'insertion + * \return int 1 si ok, -1 si ref deja existante, -2 autre erreur + */ + function update($id, $user) { - return 1; - } - else - { - if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') + global $langs; + $langs->load("main"); + $langs->load("products"); + + if (! $this->libelle) $this->libelle = 'LIBELLE MANQUANT'; + + $this->ref = trim(sanitize_string($this->ref)); + $this->libelle = trim($this->libelle); + $this->description = trim($this->description); + $this->note = trim($this->note); + + $sql = "UPDATE ".MAIN_DB_PREFIX."product "; + $sql .= " SET label = '" . addslashes($this->libelle) ."'"; + if ($this->ref) $sql .= ",ref = '" . $this->ref ."'"; + $sql .= ",tva_tx = " . $this->tva_tx ; + $sql .= ",envente = " . $this->envente ; + $sql .= ",seuil_stock_alerte = " . $this->seuil_stock_alerte ; + $sql .= ",description = '" . addslashes($this->description) ."'"; + $sql .= ",note = '" . addslashes($this->note) ."'"; + $sql .= ",duration = '" . $this->duration_value . $this->duration_unit ."'"; + $sql .= " WHERE rowid = " . $id; + + if ( $this->db->query($sql) ) { - $this->error=$langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref); - return -1; + return 1; } else { - $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql; - return -2; + if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') + { + $this->error=$langs->trans("Error")." : ".$langs->trans("ErrorProductAlreadyExists",$this->ref); + return -1; + } + else + { + $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql; + return -2; + } } } - } - /** - * \brief Ajoute un changement de prix en base dans l'historique des prix - * \param user utilisateur qui modifie le prix - */ - + /** + * \brief Ajoute un changement de prix en base dans l'historique des prix + * \param user utilisateur qui modifie le prix + */ function _log_price($user) { // On supprimme ligne existante au cas ou @@ -278,28 +275,43 @@ class Product } - /** - * \brief Lit le prix d'achat pour un fournisseur - * \param fourn_id Id du fournisseur - * \param qty Quantite pour lequel le prix est valide - * \return int Renvoi prix - */ - function get_buyprice($fourn_id, $qty) + /** + * \brief Lit le prix pratiqué par un fournisseur + * \param fourn_id Id du fournisseur + * \param qty Quantite pour lequel le prix est valide + * \return int <0 si ko, 0 si ok mais rien trouvé, 1 si ok et trouvé + */ + function get_buyprice($fourn_id, $qty) { $result = 0; - $sql = "SELECT pf.price"; - $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pf "; + $sql = "SELECT 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; $sql.= " AND quantity <= ".$qty; $sql.= " ORDER BY quantity DESC"; $sql.= " LIMIT 1"; + dolibarr_syslog("Product::get_buyprice $fourn_id,$qty sql=$sql"); + $resql = $this->db->query($sql); if ($resql) { - $row = $this->db->fetch_row($resql); - $this->buyprice = $row[0]; + $obj = $this->db->fetch_object($resql); + if ($obj && $obj->quantity > 0) + { + $this->buyprice = $obj->price; // \deprecated + $this->fourn_pu = $obj->price / $obj->quantity; // Prix unitaire du produit pour le fournisseur $fourn_id + return 1; + } + else + { + return 0; + } + } + else + { + return -1; } return $result; } |