diff --git a/orif/stock/Controllers/Item.php b/orif/stock/Controllers/Item.php index ce27f2d5..b343063a 100644 --- a/orif/stock/Controllers/Item.php +++ b/orif/stock/Controllers/Item.php @@ -98,9 +98,6 @@ public function index($page = 1) { $output['item_conditions'] = $this->item_condition_model->dropdown('name'); - $output['item_groups'] = $this->item_group_model->dropdown('name'); - - $output['stocking_places'] = $this->stocking_place_model->dropdown('name'); $output['sort_order'] = array(lang('MY_application.sort_order_name'), lang('MY_application.sort_order_stocking_place_id'), lang('MY_application.sort_order_date'), @@ -141,6 +138,16 @@ public function index($page = 1) { } } + $filters = $_GET; + + $filters['e'] = $this->getEFilter($filters); + + $where = isset($filters['e']) && $filters['e'] !== 0 ? "fk_entity_id = {$filters['e']}" : "fk_entity_id IS NULL"; + + $output['item_groups'] = $this->dropdown($this->item_group_model->select(["item_group_id", "name"])->where($where)->findAll(), 'item_group_id'); + + $output['stocking_places'] = $this->dropdown($this->stocking_place_model->select(["stocking_place_id", "name"])->where($where)->findAll(), 'stocking_place_id'); + $output['entities_has_items'] = $this->has_items(false); // Send the data to the View @@ -162,19 +169,7 @@ private function load_list($page = 1) $filters['c'] = array($this->config->functional_item_condition); } - if (!isset($filters['e'])) { - if (isset($_SESSION['user_id'])) { - $entityId = $this->user_entity_model->where('fk_user_id', $_SESSION['user_id'])->first()['fk_entity_id'] ?? 0; - } else { - $entityId = 0; - } - - if ($entityId !== 0) { - $filters['e'] = $entityId; - } else { - $filters['e'] = $this->entity_model->first()['entity_id'] ?? 0; - } - } + $filters['e'] = $this->getEFilter($filters); // Sanitize $page parameter if (empty($page) || !is_numeric($page) || $page<1) { @@ -1132,6 +1127,8 @@ private function set_validation_rules() { $validation->setRule("name", lang('MY_application.field_item_name'), 'required'); $validation->setRule("inventory_prefix", lang('MY_application.field_inventory_number'), 'required'); + $validation->setRule("item_group_id", lang('MY_application.field_group'), 'required'); + $validation->setRule("stocking_place_id", lang('MY_application.field_stocking_place'), 'required'); return $validation; } @@ -1170,4 +1167,45 @@ public function return_loan() { } + /** + * Transform the array for dropdown display + * + * @param array $array + * @param string $id = column name of the id + * @return array + */ + private function dropdown($array, $id) { + $result = array(); + + foreach ($array as $row) { + $result[$row[$id]] = $row['name']; + } + + return $result; + } + + /** + * Get e filter or set the default one + * + * @param array $filters = $_GET variable + * @return int + */ + private function getEFilter($filters): int { + if (isset($filters['e'])) { + return $filters['e']; + } + + if (isset($_SESSION['user_id'])) { + $entityId = $this->user_entity_model->where('fk_user_id', $_SESSION['user_id'])->first()['fk_entity_id'] ?? 0; + } else { + $entityId = 0; + } + + if ($entityId !== 0) { + return $entityId; + } + + return $this->entity_model->first()['entity_id'] ?? 0; + } + } diff --git a/orif/stock/Views/item/list.php b/orif/stock/Views/item/list.php index b6026a4d..406a15e9 100644 --- a/orif/stock/Views/item/list.php +++ b/orif/stock/Views/item/list.php @@ -164,12 +164,20 @@ class="btn btn-success mb-3"> { + // Load page 1 with new filters + load_items(1, getFilters()).finally(() => { + location.reload(); + }); + }); + // Reload items list on filter update - $("input[type=checkbox], input[type=radio]").change(function(){ + $("input[type=checkbox], input[type=radio]").change(function() { // Load page 1 with new filters load_items(1, getFilters()); }); - $("#text_search").on("change blur", function(){ + $("#text_search").on("change blur", function() { // Load page 1 with new filters load_items(1, getFilters()); }); @@ -181,7 +189,7 @@ class="btn btn-success mb-3">"+ "/" + page+filters; - $.ajax({ + await $.ajax({ url: url, type: "get", success: function (response) { diff --git a/public/.htaccess b/public/.htaccess index 712c9017..0777f441 100644 --- a/public/.htaccess +++ b/public/.htaccess @@ -31,7 +31,7 @@ Options All -Indexes # request to the front controller, index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA] + RewriteRule ^([\s\S]*)$ index.php?/$1 [L,NC,QSA] # Ensure Authorization header is passed along RewriteCond %{HTTP:Authorization} .