diff --git a/htdocs/product.class.php b/htdocs/product.class.php index 3754b94e2d0..23948c90cc0 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -249,8 +249,55 @@ class Product } } + /** + * \brief Vérification de l'utilisation du produit en base + * \param id id du produit + */ + function verif_prod_use($id) + { + $sql = "SELECT COUNT(*)"; + $sql.= " FROM ".MAIN_DB_PREFIX."propaldet as p, ".MAIN_DB_PREFIX."commandedet as c"; + $sql.= ", ".MAIN_DB_PREFIX."facturedet as f, ".MAIN_DB_PREFIX."contratdet as ct"; + $sql.= " WHERE p.fk_product = ".$id." AND c.fk_product = ".$id." AND f.fk_product = ".$id." AND ct.fkproduct = ".$id; + $resql = $this->db->query($sql); + if ($resql == 0) + { + return 0 + } + else + { + return -1 + } + } + + /** + * \brief Suppression du produit en base si pas utilisé + * \param id id du produit + */ + function delete($id) + { + global $user; + + if ($user->rights->produit->supprimer) + { + $prod_use = $this->verif_prod_use($id); + if ($prod_use == 0) + { + $sqld = "DELETE from ".MAIN_DB_PREFIX."product "; + $sqld.= " WHERE rowid = ".$id; + $this->db->query($sqld); + return 0; + } + else + { + $this->error .= "Impossible de supprimer le produit.\n"; + return -1 + } + } + } + /** - * \brief : update ou crée les traductions des infos produits + * \brief update ou crée les traductions des infos produits */ function setMultiLangs() { diff --git a/htdocs/product/fiche.php b/htdocs/product/fiche.php index 6e2e3873761..c5feebeea9d 100644 --- a/htdocs/product/fiche.php +++ b/htdocs/product/fiche.php @@ -203,6 +203,30 @@ if ($_GET["action"] == 'clone' && $user->rights->produit->creer) } } +/* + * Suppression d'un produit/service pas encore affecté + */ +if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes' && $user->rights->produit->supprimer) +{ + $product = new Product($db); + $product->fetch($id); + $result = $product->delete($id); + + if ($result == 0) + { + llxHeader(); + print '
'.$langs->trans("ProductDeleted",$product->ref).'
'; + llxFooter(); + exit ; + } + else + { + $reload = 0; + $_GET["action"]=''; + } +} + + /* * Ajout du produit dans une propal */ @@ -495,6 +519,14 @@ if ($_GET["id"] || $_GET["ref"]) $titre=$langs->trans("CardProduct".$product->type); dolibarr_fiche_head($head, $hselected, $titre); + + // Confirmation de la suppression de la facture + if ($_GET["action"] == 'delete') + { + $html = new Form($db); + $html->form_confirm("fiche.php?id=".$product->id,$langs->trans("DeleteProduct"),$langs->trans("ConfirmDeleteProduct"),"confirm_delete"); + print "
\n"; + } print($mesg); @@ -712,6 +744,15 @@ if ($_GET["action"] == '') print ''.$langs->trans("CreateCopy").''; } + if ($product->verif_prod_use($id)) + { + $prod_use = $product->verif_prod_use($id) + } + + if ($user->rights->produit->supprimer && $prod_use == 0) + { + print ''.$langs->trans("Delete").''; + } }