diff --git a/orif/stock/Controllers/Admin.php b/orif/stock/Controllers/Admin.php index 438a5aab..3d024b35 100644 --- a/orif/stock/Controllers/Admin.php +++ b/orif/stock/Controllers/Admin.php @@ -321,7 +321,8 @@ public function modify_stocking_place($id = NULL) // VALIDATION $validationRules = [ 'name' => 'required|min_length[3]|max_length[45]|is_unique_place_name_by_entity[' . $id . ',' . $_POST['fk_entity_id'] . ']', - 'short' => 'required|max_length['.$short_max_length.']|is_unique_place_short_name_by_entity[' . $id . ',' . $_POST['fk_entity_id'] . ']' + 'short' => 'required|max_length['.$short_max_length.']|is_unique_place_short_name_by_entity[' . $id . ',' . $_POST['fk_entity_id'] . ']', + 'fk_entity_id' => 'required|stocking_place_has_same_entity[' . $id . ']' ]; $validationErrors = [ @@ -330,6 +331,9 @@ public function modify_stocking_place($id = NULL) ], 'short' => [ 'is_unique_place_short_name_by_entity' => lang('stock_lang.msg_err_unique_short_name') + ], + 'fk_entity_id' => [ + 'stocking_place_has_same_entity' => lang('stock_lang.msg_err_stocking_place_has_same_entity') ] ]; @@ -732,7 +736,7 @@ public function modify_item_group($id = NULL) { $output = []; - if(is_null($this->item_group_model->withDeleted()->find($id))) { + if (is_null($this->item_group_model->withDeleted()->find($id))) { return redirect()->to("/stock/admin/view_item_groups"); } @@ -740,7 +744,8 @@ public function modify_item_group($id = NULL) // VALIDATION $validationRules = [ 'name' => 'required|min_length[2]|max_length[45]|is_unique_group_name_by_entity[' . $id . ',' . $_POST['fk_entity_id'] . ']', - 'short_name' => 'required|max_length['.config('\Stock\Config\StockConfig')->stocking_short_max_length.']|is_unique_group_short_name_by_entity[' . $id . ',' . $_POST['fk_entity_id'] . ']' + 'short_name' => 'required|max_length['.config('\Stock\Config\StockConfig')->stocking_short_max_length.']|is_unique_group_short_name_by_entity[' . $id . ',' . $_POST['fk_entity_id'] . ']', + 'fk_entity_id' => 'required|item_group_has_same_entity[' . $id . ']' ]; $validationErrors = [ @@ -749,6 +754,9 @@ public function modify_item_group($id = NULL) ], 'short_name' => [ 'is_unique_group_short_name_by_entity' => lang('stock_lang.msg_err_unique_short_name') + ], + 'fk_entity_id' => [ + 'item_group_has_same_entity' => lang('stock_lang.msg_err_item_group_has_same_entity') ] ]; diff --git a/orif/stock/Language/fr/stock_lang.php b/orif/stock/Language/fr/stock_lang.php index f66355e6..10ae4b07 100644 --- a/orif/stock/Language/fr/stock_lang.php +++ b/orif/stock/Language/fr/stock_lang.php @@ -122,6 +122,8 @@ 'unauthorized_entity_list' => 'Vous n\'êtes pas autorisé à consulter le(s) sites demandés', 'msg_err_unique_name' => 'Ce nom est déjà utilisé dans ce site', 'msg_err_unique_short_name' => 'Ce nom court est déjà utilisé dans ce site', +'msg_err_item_group_has_same_entity' => 'Ce groupe d\'objets ne peut pas être transféré sur un autre site, car des objets lui sont liés.', +'msg_err_stocking_place_has_same_entity'=> 'Ce lieu de stockage ne peut pas être transféré sur un autre site, car des objets lui sont liés.', /** BTN */ 'excel_export_btn' => 'Exporter', diff --git a/orif/stock/Validations/CustomRules.php b/orif/stock/Validations/CustomRules.php index 52c47cac..c5b0bdc6 100644 --- a/orif/stock/Validations/CustomRules.php +++ b/orif/stock/Validations/CustomRules.php @@ -20,7 +20,7 @@ class CustomRules * * @param string $name = name to check * @param string $params = contains every parameters needed separated with a comma - * @return boolean = TRUE if the username is unique, FALSE otherwise + * @return boolean = TRUE if the name is unique in the given entity, FALSE otherwise */ public function is_unique_group_name_by_entity(string $name, string $params) : bool { @@ -42,7 +42,7 @@ public function is_unique_group_name_by_entity(string $name, string $params) : b * * @param string $short name = short name to check * @param string $params = contains every parameters needed separated with a comma - * @return boolean = TRUE if the username is unique, FALSE otherwise + * @return boolean = TRUE if the short_name is unique in the given entity, FALSE otherwise */ public function is_unique_group_short_name_by_entity(string $short_name, string $params) : bool { @@ -60,11 +60,11 @@ public function is_unique_group_short_name_by_entity(string $short_name, string } /** - * Checks that a name doesn't already exist in an entity + * Checks that a stocking place's name doesn't already exist in an entity * * @param string $name = name to check * @param string $params = contains every parameters needed separated with a comma - * @return boolean = TRUE if the username is unique, FALSE otherwise + * @return boolean = TRUE if the name is unique in the given entity, FALSE otherwise */ public function is_unique_place_name_by_entity(string $name, string $params) : bool { @@ -82,11 +82,11 @@ public function is_unique_place_name_by_entity(string $name, string $params) : b } /** - * Checks that a short_name doesn't already exist in an entity + * Checks that a stocking place's short_name doesn't already exist in an entity * * @param string $short_name = short name to check * @param string $params = contains every parameters needed separated with a comma - * @return boolean = TRUE if the username is unique, FALSE otherwise + * @return boolean = TRUE if the short_name is unique in the given entity, FALSE otherwise */ public function is_unique_place_short_name_by_entity(string $short_name, string $params) : bool { @@ -102,4 +102,62 @@ public function is_unique_place_short_name_by_entity(string $short_name, string return is_null($stocking_place); } + + /** + * Checks that an entity change on an item_group does not affect any item. + * The stocking place is also linked with the entity and we have to avoid data inconsistency. + * + * @param string $entity_id = entity id to check + * @param string $params = contains every parameters needed separated with a comma + * @return boolean = TRUE if no item linked to a stocking place in the current entity is affected by the change. FALSE otherwise. + */ + public function item_group_has_same_entity(string $entity_id, string $params) : bool + { + // Separate the 2 parameters + $params = explode(',', $params); + + $item_group_model = new Item_group_model(); + + $item_group_entity_id = $item_group_model->where('item_group_id', $params[0])->findColumn('fk_entity_id'); + + $result = $item_group_model->select('stocking_place.fk_entity_id') + ->join('item', 'item.item_group_id = item_group.item_group_id', 'inner') + ->join('stocking_place', 'stocking_place.stocking_place_id = item.stocking_place_id', 'inner') + ->where('item_group.item_group_id', $params[0]) + ->where('item_group.fk_entity_id', $item_group_entity_id) + ->distinct() + ->get() + ->getRow(); + + return $result ? $result->fk_entity_id === $entity_id : true; + } + + /** + * Checks that an entity change on a stocking_place does not affect any item + * The item group is also linked with the entity and we have to avoid data inconsistency. + * + * @param string $entity_id = entity id to check + * @param string $params = contains every parameters needed separated with a comma + * @return boolean = TRUE if no item linked to an item group in the current entity is affected by the change. FALSE otherwise. + */ + public function stocking_place_has_same_entity(string $entity_id, string $params) : bool + { + // Separate the 2 parameters + $params = explode(',', $params); + + $stocking_place_model = new Stocking_place_model(); + + $stocking_place_entity_id = $stocking_place_model->where('stocking_place_id', $params[0])->findColumn('fk_entity_id'); + + $result = $stocking_place_model->select('item_group.fk_entity_id') + ->join('item', 'item.stocking_place_id = stocking_place.stocking_place_id', 'inner') + ->join('item_group', 'item_group.item_group_id = item.item_group_id', 'inner') + ->where('stocking_place.stocking_place_id', $params[0]) + ->where('stocking_place.fk_entity_id', $stocking_place_entity_id) + ->distinct() + ->get() + ->getRow(); + + return $result ? $result->fk_entity_id === $entity_id : true; + } } \ No newline at end of file diff --git a/orif/stock/Views/admin/item_groups/form.php b/orif/stock/Views/admin/item_groups/form.php index 5c764416..e7b242b2 100644 --- a/orif/stock/Views/admin/item_groups/form.php +++ b/orif/stock/Views/admin/item_groups/form.php @@ -32,6 +32,7 @@ + showError('fk_entity_id'); ?> diff --git a/orif/stock/Views/admin/stocking_places/form.php b/orif/stock/Views/admin/stocking_places/form.php index 212efd74..14a55c25 100644 --- a/orif/stock/Views/admin/stocking_places/form.php +++ b/orif/stock/Views/admin/stocking_places/form.php @@ -32,6 +32,7 @@ + showError('fk_entity_id'); ?>