From 9948b0d04601a5b089d578134afc80f02664b09a Mon Sep 17 00:00:00 2001 From: catdesu Date: Tue, 6 Jun 2023 10:14:03 +0200 Subject: [PATCH 1/3] Fixed item_group validations when changing entity --- orif/stock/Controllers/Admin.php | 8 ++++-- orif/stock/Language/fr/stock_lang.php | 1 + orif/stock/Validations/CustomRules.php | 28 +++++++++++++++++++++ orif/stock/Views/admin/item_groups/form.php | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/orif/stock/Controllers/Admin.php b/orif/stock/Controllers/Admin.php index 438a5aab..b3b636cc 100644 --- a/orif/stock/Controllers/Admin.php +++ b/orif/stock/Controllers/Admin.php @@ -732,7 +732,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 +740,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 +750,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..0d1b95c0 100644 --- a/orif/stock/Language/fr/stock_lang.php +++ b/orif/stock/Language/fr/stock_lang.php @@ -122,6 +122,7 @@ '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' => 'Le changement de site pour ce groupe d\'objets n\'est pas autorisé, car il est actuellement utilisé par d\'autres objets.', /** BTN */ 'excel_export_btn' => 'Exporter', diff --git a/orif/stock/Validations/CustomRules.php b/orif/stock/Validations/CustomRules.php index 52c47cac..9ca04ab4 100644 --- a/orif/stock/Validations/CustomRules.php +++ b/orif/stock/Validations/CustomRules.php @@ -102,4 +102,32 @@ 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 + * + * @param string $short_name = short name to check + * @param string $params = contains every parameters needed separated with a comma + * @return boolean = TRUE if the $entity_id equals fk_entity_id from the query is unique, 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; + } } \ 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'); ?> From 37445e4176eb2e12d7bcd0283ba12b19a7d0edb5 Mon Sep 17 00:00:00 2001 From: catdesu Date: Tue, 6 Jun 2023 10:33:31 +0200 Subject: [PATCH 2/3] Fixed stocking_place validations when changing entity --- orif/stock/Controllers/Admin.php | 6 ++- orif/stock/Language/fr/stock_lang.php | 1 + orif/stock/Validations/CustomRules.php | 40 ++++++++++++++++--- .../Views/admin/stocking_places/form.php | 1 + 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/orif/stock/Controllers/Admin.php b/orif/stock/Controllers/Admin.php index b3b636cc..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') ] ]; diff --git a/orif/stock/Language/fr/stock_lang.php b/orif/stock/Language/fr/stock_lang.php index 0d1b95c0..927fde27 100644 --- a/orif/stock/Language/fr/stock_lang.php +++ b/orif/stock/Language/fr/stock_lang.php @@ -123,6 +123,7 @@ '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' => 'Le changement de site pour ce groupe d\'objets n\'est pas autorisé, car il est actuellement utilisé par d\'autres objets.', +'msg_err_stocking_place_has_same_entity'=> 'Le changement de site pour ce lieu de stockage n\'est pas autorisé, car il est actuellement utilisé par d\'autres objets.', /** BTN */ 'excel_export_btn' => 'Exporter', diff --git a/orif/stock/Validations/CustomRules.php b/orif/stock/Validations/CustomRules.php index 9ca04ab4..8f9250f7 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, 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, FALSE otherwise */ public function is_unique_group_short_name_by_entity(string $short_name, string $params) : bool { @@ -64,7 +64,7 @@ public function is_unique_group_short_name_by_entity(string $short_name, string * * @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, FALSE otherwise */ public function is_unique_place_name_by_entity(string $name, string $params) : bool { @@ -86,7 +86,7 @@ public function is_unique_place_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, FALSE otherwise */ public function is_unique_place_short_name_by_entity(string $short_name, string $params) : bool { @@ -106,9 +106,9 @@ public function is_unique_place_short_name_by_entity(string $short_name, string /** * Checks that an entity change on an item_group does not affect any item * - * @param string $short_name = short name to check + * @param string $entity_id = entity id to check * @param string $params = contains every parameters needed separated with a comma - * @return boolean = TRUE if the $entity_id equals fk_entity_id from the query is unique, FALSE otherwise + * @return boolean = TRUE if the $entity_id equals fk_entity_id from the query or no result was found, FALSE otherwise */ public function item_group_has_same_entity(string $entity_id, string $params) : bool { @@ -130,4 +130,32 @@ public function item_group_has_same_entity(string $entity_id, string $params) : return $result ? $result->fk_entity_id === $entity_id : true; } + + /** + * Checks that an entity change on a stocking_place does not affect any item + * + * @param string $entity_id = entity id to check + * @param string $params = contains every parameters needed separated with a comma + * @return boolean = TRUE if the $entity_id equals fk_entity_id from the query or no result was found, 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/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'); ?> From d3fb6140a1f0be61b149646b6e5241ab765ec278 Mon Sep 17 00:00:00 2001 From: Didier Viret Date: Wed, 28 Jun 2023 15:47:12 +0200 Subject: [PATCH 3/3] Rework entity change rules comments --- orif/stock/Language/fr/stock_lang.php | 4 ++-- orif/stock/Validations/CustomRules.php | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/orif/stock/Language/fr/stock_lang.php b/orif/stock/Language/fr/stock_lang.php index 927fde27..10ae4b07 100644 --- a/orif/stock/Language/fr/stock_lang.php +++ b/orif/stock/Language/fr/stock_lang.php @@ -122,8 +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' => 'Le changement de site pour ce groupe d\'objets n\'est pas autorisé, car il est actuellement utilisé par d\'autres objets.', -'msg_err_stocking_place_has_same_entity'=> 'Le changement de site pour ce lieu de stockage n\'est pas autorisé, car il est actuellement utilisé par d\'autres objets.', +'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 8f9250f7..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 name 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 short_name 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 name 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 short_name 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 { @@ -104,11 +104,12 @@ public function is_unique_place_short_name_by_entity(string $short_name, string } /** - * Checks that an entity change on an item_group does not affect any item + * 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 the $entity_id equals fk_entity_id from the query or no result was found, FALSE otherwise + * @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 { @@ -133,10 +134,11 @@ public function item_group_has_same_entity(string $entity_id, string $params) : /** * 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 the $entity_id equals fk_entity_id from the query or no result was found, FALSE otherwise + * @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 {