Skip to content

Commit

Permalink
Merge pull request #187 from OrifInformatique:fix_item_modification
Browse files Browse the repository at this point in the history
Fix entity change validations on item_groups and stocking_places
  • Loading branch information
DidierViret committed Jun 28, 2023
2 parents 896514b + d3fb614 commit 8a0e61d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
14 changes: 11 additions & 3 deletions orif/stock/Controllers/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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')
]
];

Expand Down Expand Up @@ -732,15 +736,16 @@ 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");
}

if ( ! empty($_POST)) {
// 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 = [
Expand All @@ -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')
]
];

Expand Down
2 changes: 2 additions & 0 deletions orif/stock/Language/fr/stock_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
70 changes: 64 additions & 6 deletions orif/stock/Validations/CustomRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions orif/stock/Views/admin/item_groups/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<option value="<?=$entity['entity_id']?>" <?=isset($item_group['fk_entity_id'])&&$item_group['fk_entity_id']==$entity['entity_id']?'selected=true':''?>><?=$entity['name']?></option>
<?php endforeach;?>
</select>
<span class="text-danger"><?= $validation->showError('fk_entity_id'); ?></span>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions orif/stock/Views/admin/stocking_places/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<option value="<?=$entity['entity_id']?>" <?=isset($stocking_place['fk_entity_id'])&&$stocking_place['fk_entity_id']==$entity['entity_id']?'selected=true':''?>><?=$entity['name']?></option>
<?php endforeach;?>
</select>
<span class="text-danger"><?= $validation->showError('fk_entity_id'); ?></span>
</div>
</div>
</div>
Expand Down

0 comments on commit 8a0e61d

Please sign in to comment.