diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index 0ee31784a16..283a0b9a3d0 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -1856,7 +1856,11 @@ class FactureFournisseur extends CommonInvoice $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("InvoiceValidatedInDolibarr", $num)); } if ($result < 0) { - $error++; + $this->error = $mouvP->error; + if (count($mouvP->errors)) { + $this->errors = $mouvP->errors; + } + return -2; } } } @@ -1935,9 +1939,10 @@ class FactureFournisseur extends CommonInvoice * * @param User $user Object user that modify * @param int $idwarehouse Id warehouse to use for stock change. + * @param int $notrigger 1=Does not execute triggers, 0= execute triggers * @return int <0 if KO, >0 if OK */ - public function setDraft($user, $idwarehouse = -1) + public function setDraft($user, $idwarehouse = -1, $notrigger = 0) { // phpcs:enable global $conf, $langs; diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php index 7a17f161383..8f086be1154 100644 --- a/htdocs/fourn/facture/card.php +++ b/htdocs/fourn/facture/card.php @@ -261,12 +261,42 @@ if (empty($reshook)) { $isErasable = $object->is_erasable(); if (($usercandelete && $isErasable > 0) || ($usercancreate && $isErasable == 1)) { - $result = $object->delete($user); - if ($result > 0) { - header('Location: list.php?restore_lastsearch_values=1'); - exit; - } else { - setEventMessages($object->error, $object->errors, 'errors'); + $revertstock = GETPOST('revertstock'); + + if ($revertstock) { + $idwarehouse = GETPOST('idwarehouse'); + + $qualified_for_stock_change = 0; + if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) { + $qualified_for_stock_change = $object->hasProductsOrServices(2); + } else { + $qualified_for_stock_change = $object->hasProductsOrServices(1); + } + + // Check parameters + if (isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) && $qualified_for_stock_change) { + $langs->load("stocks"); + if (!$idwarehouse || $idwarehouse == -1) { + $error++; + setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Warehouse")), null, 'errors'); + $action = 'delete'; + } else { + $result = $object->setDraft($user, $idwarehouse); + if ($result < 0) { + $error++; + } + } + } + } + + if (!$error) { + $result = $object->delete($user); + if ($result > 0) { + header('Location: list.php?restore_lastsearch_values=1'); + exit; + } else { + setEventMessages($object->error, $object->errors, 'errors'); + } } } } elseif ($action == 'confirm_deleteline' && $confirm == 'yes' && $usercancreate) { @@ -3013,7 +3043,53 @@ if ($action == 'create') { // Confirmation de la suppression de la facture fournisseur if ($action == 'delete') { - $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteBill'), $langs->trans('ConfirmDeleteBill'), 'confirm_delete', '', 0, 1); + $formquestion = array(); + + $qualified_for_stock_change = 0; + if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) { + $qualified_for_stock_change = $object->hasProductsOrServices(2); + } else { + $qualified_for_stock_change = $object->hasProductsOrServices(1); + } + + if (isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) && $qualified_for_stock_change) { + $langs->load("stocks"); + require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php'; + $formproduct = new FormProduct($db); + $warehouse = new Entrepot($db); + $warehouse_array = $warehouse->list_array(); + + $selectwarehouse = ''; + + $selectyesno = array(0 => $langs->trans('No'), 1 => $langs->trans('Yes')); + + print ''; + + $formquestion = array( + array('type' => 'select', 'name' => 'revertstock', 'label' => $langs->trans("RevertProductsToStock"), 'select_show_empty' => 0, 'values' => $selectyesno), + array('type' => 'other', 'name' => 'idwarehouse', 'label' => $label, 'value' => $selectwarehouse, 'tdclass' => 'questionrevertstock hidden') + ); + } + + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteBill'), $langs->trans('ConfirmDeleteBill'), 'confirm_delete', $formquestion, 1, 1); } if ($action == 'deletepayment') { $payment_id = GETPOST('paiement_id'); diff --git a/htdocs/langs/en_US/stocks.lang b/htdocs/langs/en_US/stocks.lang index 961a9590f2b..d7987f322b2 100644 --- a/htdocs/langs/en_US/stocks.lang +++ b/htdocs/langs/en_US/stocks.lang @@ -119,6 +119,7 @@ PersonalStock=Personal stock %s ThisWarehouseIsPersonalStock=This warehouse represents personal stock of %s %s SelectWarehouseForStockDecrease=Choose warehouse to use for stock decrease SelectWarehouseForStockIncrease=Choose warehouse to use for stock increase +RevertProductsToStock=Revert products to stock ? NoStockAction=No stock action DesiredStock=Desired Stock DesiredStockDesc=This stock amount will be the value used to fill the stock by replenishment feature. diff --git a/htdocs/langs/fr_FR/stocks.lang b/htdocs/langs/fr_FR/stocks.lang index da863fc7dd1..d86d504476a 100644 --- a/htdocs/langs/fr_FR/stocks.lang +++ b/htdocs/langs/fr_FR/stocks.lang @@ -119,6 +119,7 @@ PersonalStock=Stock personnel %s ThisWarehouseIsPersonalStock=Cet entrepôt représente le stock personnel de %s %s SelectWarehouseForStockDecrease=Sélectionner l'entrepôt à utiliser pour la décrémentation du stock SelectWarehouseForStockIncrease=Sélectionner l'entrepôt à utiliser pour l'incrémentation du stock +RevertProductsToStock=Remettre les produits en stock ? NoStockAction=Pas d'action sur l'entrepôt DesiredStock=Stock désiré optimal DesiredStockDesc=Le stock optimal sera la valeur utilisée pour remplir le stock avec la fonction de réapprovisionnement