diff --git a/htdocs/core/actions_addupdatedelete.inc.php b/htdocs/core/actions_addupdatedelete.inc.php index cf3b9268ee4..ae161009ba8 100644 --- a/htdocs/core/actions_addupdatedelete.inc.php +++ b/htdocs/core/actions_addupdatedelete.inc.php @@ -444,7 +444,13 @@ if ($action == 'confirm_deleteline' && $confirm == 'yes' && !empty($permissionto // Action validate object if ($action == 'confirm_validate' && $confirm == 'yes' && $permissiontoadd) { - $result = $object->validate($user); + if ($object->element == 'inventory' && !empty($include_sub_warehouse)) { + //Case of the conf INVENTORY_INCLUDE_SUB_WAREHOUSE + $result = $object->validate($user, false, $include_sub_warehouse); + } else { + $result = $object->validate($user); + } + if ($result >= 0) { // Define output language if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) { diff --git a/htdocs/langs/en_US/stocks.lang b/htdocs/langs/en_US/stocks.lang index 176857c0c6b..cf2ae8ca41f 100644 --- a/htdocs/langs/en_US/stocks.lang +++ b/htdocs/langs/en_US/stocks.lang @@ -321,5 +321,8 @@ BatchNotFound=Lot / serial not found for this product StockMovementWillBeRecorded=Stock movement will be recorded StockMovementNotYetRecorded=Stock movement will not be affected by this step WarningThisWIllAlsoDeleteStock=Warning, this will also destroy all quantities in stock in the warehouse +ValidateInventory=Inventory validation +IncludeSubWarehouse=Include sub-warehouse ? +IncludeSubWarehouseExplanation=Check this box if you want to include all sub-warehouses of the associated warehouse in the inventory DeleteBatch=Delete lot/serial -ConfirmDeleteBatch=Are you sure you want to delete lot/serial ? \ No newline at end of file +ConfirmDeleteBatch=Are you sure you want to delete lot/serial ? diff --git a/htdocs/langs/fr_FR/stocks.lang b/htdocs/langs/fr_FR/stocks.lang index 099b5b8d3a3..0d534602675 100644 --- a/htdocs/langs/fr_FR/stocks.lang +++ b/htdocs/langs/fr_FR/stocks.lang @@ -318,6 +318,7 @@ StockTransferRightRead=Lire les transferts de stocks StockTransferRightCreateUpdate=Créer/Mettre à jour les transferts de stocks StockTransferRightDelete=Supprimer les transferts de stocks BatchNotFound=Lot/série introuvable pour ce produit +IncludeSubWarehouse=Inclure les sous-entrepôts ? StockMovementWillBeRecorded=Les mouvements de stock seront enregistrés StockMovementNotYetRecorded=Le mouvement des stocks ne sera pas affecté par cette étape WarningThisWIllAlsoDeleteStock=Attention, cela détruira également toutes les quantités en stock dans l'entrepôt diff --git a/htdocs/product/inventory/card.php b/htdocs/product/inventory/card.php index 7db7f5af6a4..c99183008a4 100644 --- a/htdocs/product/inventory/card.php +++ b/htdocs/product/inventory/card.php @@ -40,6 +40,7 @@ $confirm = GETPOST('confirm', 'alpha'); $cancel = GETPOST('cancel', 'aZ09'); $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'inventorycard'; // To manage different context of search $backtopage = GETPOST('backtopage', 'alpha'); +$include_sub_warehouse = !empty(GETPOST('include_sub_warehouse')) ? GETPOST('include_sub_warehouse') : 0; if (empty($conf->global->MAIN_USE_ADVANCED_PERMS)) { $result = restrictedArea($user, 'stock', $id); @@ -289,6 +290,18 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('XXX'), $text, 'confirm_xxx', $formquestion, 0, 1, 220); } + + if ($action == 'validate') { + $form = new Form($db); + $formquestion = ''; + if ($conf->global->INVENTORY_INCLUDE_SUB_WAREHOUSE && !empty($object->fk_warehouse)) { + $formquestion = array( + array('type' => 'checkbox', 'name' => 'include_sub_warehouse', 'label' => $langs->trans("IncludeSubWarehouse"), 'value' => 1, 'size' => '10'), + ); + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ValidateInventory'), $langs->trans('IncludeSubWarehouseExplanation'), 'confirm_validate', $formquestion, '', 1); + } + } + // Call Hook formConfirm $parameters = array('formConfirm' => $formconfirm); $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook @@ -412,7 +425,11 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea // Validate if ($object->status == $object::STATUS_DRAFT || $object->status == $object::STATUS_CANCELED) { if ($permissiontoadd) { - print ''.$langs->trans("Validate").' ('.$langs->trans("ToStart").')'; + if ($conf->global->INVENTORY_INCLUDE_SUB_WAREHOUSE && !empty($object->fk_warehouse)) { + print ''.$langs->trans("Validate").' ('.$langs->trans("Start").')'; + } else { + print ''.$langs->trans("Validate").' ('.$langs->trans("Start").')'; + } } } diff --git a/htdocs/product/inventory/class/inventory.class.php b/htdocs/product/inventory/class/inventory.class.php index 97ae607e70b..51446544c60 100644 --- a/htdocs/product/inventory/class/inventory.class.php +++ b/htdocs/product/inventory/class/inventory.class.php @@ -260,7 +260,7 @@ class Inventory extends CommonObject * @param bool $notrigger false=launch triggers after, true=disable triggers * @return int <0 if KO, Id of created object if OK */ - public function validate(User $user, $notrigger = false) + public function validate(User $user, $notrigger = false, $include_sub_warehouse = 0) { global $conf; $this->db->begin(); @@ -292,7 +292,12 @@ class Inventory extends CommonObject $sql .= " AND ps.fk_product = ".((int) $this->fk_product); } if ($this->fk_warehouse > 0) { - $sql .= " AND ps.fk_entrepot = ".((int) $this->fk_warehouse); + $sql .= " AND (ps.fk_entrepot = ".((int) $this->fk_warehouse); + if (!empty($include_sub_warehouse) && $conf->global->INVENTORY_INCLUDE_SUB_WAREHOUSE) { + $this->getchildWarehouse($this->fk_warehouse, $TChildWarehouses); + $sql .= " OR ps.fk_entrepot IN (".filter_var(implode(',', $TChildWarehouses), FILTER_SANITIZE_STRING).")"; + } + $sql .= ')'; } if (!empty($this->categories_product)) { $sql .= " AND EXISTS ("; @@ -745,6 +750,29 @@ class Inventory extends CommonObject $this->initAsSpecimenCommon(); $this->title = ''; } + + /** + * Return the child warehouse of the current one + * @param int $id Id of warehouse + * @param $TChildWarehouse child warehouses + * @return int <0 if KO, >0 if OK + */ + public function getchildWarehouse($id, &$TChildWarehouse) + { + $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'entrepot'; + $sql.= ' WHERE fk_parent='.(int) $id; + $sql.= ' ORDER BY rowid'; + $resql = $this->db->query($sql); + if ($resql && $this->db->num_rows($resql)>0) { + while ($obj = $this->db->fetch_object($resql)) { + $TChildWarehouse[] = $obj->rowid; + $this->getchildWarehouse($obj->rowid, $TChildWarehouse); + } + return 1; + } else { + return -1; + } + } } /** diff --git a/htdocs/product/inventory/inventory.php b/htdocs/product/inventory/inventory.php index 8f595abcc53..ff78e3f6dcc 100644 --- a/htdocs/product/inventory/inventory.php +++ b/htdocs/product/inventory/inventory.php @@ -468,6 +468,17 @@ if ($action == 'confirm_cancel') { $action = 'view'; } +if ($action == 'validate') { + $form = new Form($db); + $formquestion = ''; + if ($conf->global->INVENTORY_INCLUDE_SUB_WAREHOUSE && !empty($object->fk_warehouse)) { + $formquestion = array( + array('type' => 'checkbox', 'name' => 'include_sub_warehouse', 'label' => $langs->trans("IncludeSubWarehouse"), 'value' => 1, 'size' => '10'), + ); + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ValidateInventory'), $langs->trans('IncludeSubWarehouseExplanation'), 'confirm_validate', $formquestion, '', 1); + } +} + // Call Hook formConfirm $parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid); $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook @@ -573,7 +584,11 @@ if ($action != 'record') { if (empty($reshook)) { if ($object->status == Inventory::STATUS_DRAFT) { if ($permissiontoadd) { - print ''.$langs->trans("Validate").' ('.$langs->trans("Start").')'."\n"; + if ($conf->global->INVENTORY_INCLUDE_SUB_WAREHOUSE && !empty($object->fk_warehouse)) { + print ''.$langs->trans("Validate").' ('.$langs->trans("Start").')'; + } else { + print ''.$langs->trans("Validate").' ('.$langs->trans("Start").')'; + } } else { print ''.$langs->trans('Validate').' ('.$langs->trans("Start").')'."\n"; }