diff --git a/htdocs/product.class.php b/htdocs/product.class.php index cd016dc284f..28a0abc47c1 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -840,60 +840,118 @@ class Product } - /** - * \brief Supprime un tarif fournisseur - * \param user utilisateur qui défait le lien - * \param id_fourn id du fournisseur - * \param qty quantité - * \return int < 0 si erreur, > 0 si ok - */ - - function remove_price($user, $id_fourn, $qty) + /** + * \brief Supprime un tarif fournisseur + * \param user utilisateur qui défait le lien + * \param id_fourn id du fournisseur + * \param qty quantit + * \return int < 0 si erreur, > 0 si ok + */ + function remove_price($user, $id_fourn, $qty) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; - $sql.= " WHERE fk_product = $this->id AND fk_soc = $id_fourn and quantity = '".$qty."';"; - - if ($this->db->query($sql) ) - { - return 1; - } - else - { - dolibarr_print_error($this->db); - return -1; - } + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price"; + $sql.= " WHERE fk_product = $this->id AND fk_soc = $id_fourn and quantity = '".$qty."';"; + + if ($this->db->query($sql) ) + { + return 1; + } + else + { + dolibarr_print_error($this->db); + return -1; + } } - /** - * \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) + /** + * \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; - } + $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 + * \param toId Id produit cible + * \return int < 0 si erreur, > 0 si ok + */ + function clone_price($fromId, $toId) + { + global $db; + + $db->begin(); + + // les prix + $sql = "insert " .MAIN_DB_PREFIX."product_price (" + . " fk_product, date_price, price, tva_tx, fk_user_author, envente )" + . " select ".$toId . ", date_price, price, tva_tx, fk_user_author, envente " + . " from ".MAIN_DB_PREFIX."product_price " + . " where fk_product = ". $fromId . ";" ; + if ( ! $db->query($sql ) ) { + $db->rollback(); + return -1; + } + $db->commit(); + return 1; + } + + /** + * \brief Recopie les fournisseurs et prix fournisseurs d'un produit/service sur un autre + * \param fromId Id produit source + * \param toId Id produit cible + * \return int < 0 si erreur, > 0 si ok + */ + function clone_fournisseurs($fromId, $toId) + { + global $db; + + $db->begin(); + + // les fournisseurs + $sql = "insert ".MAIN_DB_PREFIX."product_fournisseur (" + . " datec, fk_product, fk_soc, ref_fourn, fk_user_author )" + . " select now(), ".$toId.", fk_soc, ref_fourn, fk_user_author" + . " from ".MAIN_DB_PREFIX."product_fournisseur " + . " where fk_product = ".$fromId .";" ; + if ( ! $db->query($sql ) ) { + $db->rollback(); + return -1; + } + // les prix de fournisseurs. + $sql = "insert ".MAIN_DB_PREFIX."product_fournisseur_price (" + . " datec, fk_product, fk_soc, price, quantity, fk_user )" + . " select now(), ".$toId. ", fk_soc, price, quantity, fk_user" + . " from ".MAIN_DB_PREFIX."product_fournisseur_price" + . " where fk_product = ".$fromId.";"; + if ( ! $db->query($sql ) ) { + $db->rollback(); + return -1; + } + $db->commit(); + return 1; } - /** * \brief Entre un nombre de piece du produit en stock dans un entrepôt * \param id_entrepot id de l'entrepot * \param nbpiece nombre de pieces */ - function create_stock($id_entrepot, $nbpiece) { @@ -920,7 +978,6 @@ class Product * \param nbpiece nombre de pieces * \param mouvement 0 = ajout, 1 = suppression */ - function correct_stock($user, $id_entrepot, $nbpiece, $mouvement) { diff --git a/htdocs/product/fiche.php b/htdocs/product/fiche.php index 4e7622807db..0689e58e188 100644 --- a/htdocs/product/fiche.php +++ b/htdocs/product/fiche.php @@ -20,7 +20,6 @@ * * $Id$ * $Source$ - * */ /** @@ -45,7 +44,6 @@ if (!$user->rights->produit->lire) accessforbidden(); $types[0] = $langs->trans("Product"); $types[1] = $langs->trans("Service"); - /* * */ @@ -103,7 +101,6 @@ if ($_POST["action"] == 'update' && if ($product->fetch($_POST["id"])) { $product->ref = $_POST["ref"]; - $product->libelle = $_POST["libelle"]; $product->price = $_POST["price"]; $product->tva_tx = $_POST["tva_tx"]; $product->description = $_POST["desc"]; @@ -136,19 +133,67 @@ if ($_POST["action"] == 'update' && } } +// clone d'un produit +if ($_GET["action"] == 'clone' && $user->rights->produit->creer) +{ + $db->begin(); + + $product = new Product($db); + $originalId = $_GET["id"]; + if ($product->fetch($_GET["id"]) > 0) + { + $product->ref = "Clone ".$product->ref; + $product->envente = 0; + $product->id = null; + + if ($product->check()) + { + $id = $product->create($user); + if ($id > 0) + { + // $product->clone_fournisseurs($originalId, $id); + + $db->commit(); + + Header("Location: fiche.php?id=$id"); + } + else if ($id == -3) + { + $db->rollback(); + + $_error = 1; + $_GET["action"] = ""; + dolibarr_print_error($product->db); + } + else + { + $db->rollback(); + + dolibarr_print_error($product->db); + } + } + } + else + { + $db->rollback(); + + dolibarr_print_error($product->db); + } +} if ($_POST["action"] == 'addinpropal') { - $propal = New Propal($db); - $propal->fetch($_POST["propalid"]); + $propal = New Propal($db); + $propal->fetch($_POST["propalid"]); - $result = $propal->insert_product($_GET["id"], $_POST["qty"], $_POST["remise_percent"]); - if ( $result < 0) + $result = $propal->insert_product($_GET["id"], $_POST["qty"], $_POST["remise_percent"]); + if ( $result < 0) { - $mesg = $langs->trans("ErrorUnknown").": $result"; + $mesg = $langs->trans("ErrorUnknown").": $result"; } - Header("Location: ../comm/propal.php?propalid=".$propal->id); + Header("Location: ../comm/propal.php?propalid=".$propal->id); + exit; } /* @@ -156,47 +201,48 @@ if ($_POST["action"] == 'addinpropal') */ if ($_POST["action"] == 'addinfacture' && $user->rights->facture->creer) { - $product = new Product($db); - $result = $product->fetch($_GET["id"]); + $product = new Product($db); + $result = $product->fetch($_GET["id"]); - $facture = New Facture($db); + $facture = New Facture($db); - $facture->fetch($_POST["factureid"]); + $facture->fetch($_POST["factureid"]); - $facture->addline($_POST["factureid"], - addslashes($product->libelle), - $product->price, - $_POST["qty"], - $product->tva_tx, - $product->id, - $_POST["remise_percent"]); - - Header("Location: ../compta/facture.php?facid=".$facture->id); + $facture->addline($_POST["factureid"], + addslashes($product->libelle), + $product->price, + $_POST["qty"], + $product->tva_tx, + $product->id, + $_POST["remise_percent"]); + Header("Location: ../compta/facture.php?facid=".$facture->id); + exit; } if ($_POST["action"] == 'add_fourn' && $_POST["cancel"] <> $langs->trans("Cancel")) { - $product = new Product($db); - if( $product->fetch($_GET["id"]) ) + $product = new Product($db); + if( $product->fetch($_GET["id"]) ) { - if ($product->add_fournisseur($user, $_POST["id_fourn"], $_POST["ref_fourn"]) > 0) - { - $action = ''; - $mesg = $langs->trans("SupplierAdded"); - } - else - { - $action = ''; - } + if ($product->add_fournisseur($user, $_POST["id_fourn"], $_POST["ref_fourn"]) > 0) + { + $action = ''; + $mesg = $langs->trans("SupplierAdded"); + } + else + { + $action = ''; + } } } if ($_POST["cancel"] == $langs->trans("Cancel")) { - $action = ''; - Header("Location: fiche.php?id=".$_POST["id"]); + $action = ''; + Header("Location: fiche.php?id=".$_POST["id"]); + exit; } @@ -353,17 +399,18 @@ if ($_GET["id"]) $head[$h][1] = $langs->trans('Statistics'); $h++; - //erics: pour créer des produits composés de x 'sous' produits - $head[$h][0] = DOL_URL_ROOT."/product/pack.php?id=".$product->id; - $head[$h][1] = $langs->trans('Packs'); - $h++; + //erics: pour créer des produits composés de x 'sous' produits + $head[$h][0] = DOL_URL_ROOT."/product/pack.php?id=".$product->id; + $head[$h][1] = $langs->trans('Packs'); + $h++; $head[$h][0] = DOL_URL_ROOT."/product/stats/facture.php?id=".$product->id; $head[$h][1] = $langs->trans('Referers'); $h++; - $head[$h][0] = DOL_URL_ROOT.'/product/document.php?id='.$product->id; - $head[$h][1] = $langs->trans('Documents'); - $h++; + + $head[$h][0] = DOL_URL_ROOT.'/product/document.php?id='.$product->id; + $head[$h][1] = $langs->trans('Documents'); + $h++; dolibarr_fiche_head($head, $hselected, $langs->trans("CardProduct".$product->type).' : '.$product->ref); @@ -561,6 +608,8 @@ if ($_GET["action"] == '') if ( $user->rights->produit->creer) { print ''.$langs->trans("Edit").''; + + print ''.$langs->trans("CreateCopy").''; } }