diff --git a/client/src/js/services/StockService.js b/client/src/js/services/StockService.js index e031d7b493..d557873180 100644 --- a/client/src/js/services/StockService.js +++ b/client/src/js/services/StockService.js @@ -28,11 +28,15 @@ function StockService(Api, Filters, AppCache, Periods, $httpParamSerializer, Lan // Filter service var StockLotFilters = new Filters(); var StockMovementFilters = new Filters(); + var StockInventoryFilters = new Filters(); + var filterMovementCache = new AppCache('stock-movement-filters'); var filterLotCache = new AppCache('stock-lot-filters'); + var filterInventoryCache = new AppCache('stock-inventory-filters'); StockLotFilters.registerDefaultFilters(bhConstants.defaultFilters); StockMovementFilters.registerDefaultFilters(bhConstants.defaultFilters); + StockInventoryFilters.registerDefaultFilters(bhConstants.defaultFilters); StockLotFilters.registerCustomFilters([ { key : 'depot_uuid', label : 'STOCK.DEPOT' }, @@ -54,6 +58,12 @@ function StockService(Api, Filters, AppCache, Periods, $httpParamSerializer, Lan { key : 'dateTo', label : 'FORM.LABELS.DATE', comparitor: '<', valueFilter : 'date' }, ]); + StockInventoryFilters.registerCustomFilters([ + { key : 'depot_uuid', label : 'STOCK.DEPOT' }, + { key : 'inventory_uuid', label : 'STOCK.INVENTORY' }, + { key : 'status', label : 'STOCK.STATUS.LABEL' }, + ]); + if (filterLotCache.filters) { StockLotFilters.loadCache(filterLotCache.filters); @@ -63,20 +73,27 @@ function StockService(Api, Filters, AppCache, Periods, $httpParamSerializer, Lan StockMovementFilters.loadCache(filterMovementCache.filters); } + if (filterInventoryCache.filters) { + StockInventoryFilters.loadCache(filterInventoryCache.filters); + } + // once the cache has been loaded - ensure that default filters are provided appropriate values assignLotDefaultFilters(); assignMovementDefaultFilters(); + assignInventoryDefaultFilters(); // creating an object of filter to avoid method duplication var stockFilter = { lot : StockLotFilters, movement : StockMovementFilters, + inventory : StockInventoryFilters }; // creating an object of filter object to avoid method duplication var filterCache = { lot : filterLotCache, movement : filterMovementCache, + inventory : filterInventoryCache }; function assignLotDefaultFilters() { @@ -115,6 +132,24 @@ function StockService(Api, Filters, AppCache, Periods, $httpParamSerializer, Lan } } + function assignInventoryDefaultFilters() { + // get the keys of filters already assigned - on initial load this will be empty + var assignedKeys = Object.keys(StockInventoryFilters.formatHTTP()); + + // assign default period filter + var periodDefined = + inventories.util.arrayIncludes(assignedKeys, ['period']); + + if (!periodDefined) { + StockInventoryFilters.assignFilters(Periods.defaultFilters()); + } + + // assign default limit filter + if (assignedKeys.indexOf('limit') === -1) { + StockInventoryFilters.assignFilter('limit', 100); + } + } + function removeFilter(filterKey, valueKey) { stockFilter[filterKey].resetFilterState(valueKey); } diff --git a/client/src/modules/stock/inventories/registry.js b/client/src/modules/stock/inventories/registry.js index b542ae6792..f5ba8f3c7d 100644 --- a/client/src/modules/stock/inventories/registry.js +++ b/client/src/modules/stock/inventories/registry.js @@ -5,24 +5,23 @@ StockInventoriesController.$inject = [ 'StockService', 'NotifyService', 'uiGridConstants', '$translate', 'StockModalService', 'SearchFilterFormatService', 'LanguageService', 'SessionService', - 'GridGroupingService', 'bhConstants', 'GridStateService', '$state' + 'GridGroupingService', 'bhConstants', 'GridStateService', '$state', 'GridColumnService' ]; /** * Stock Inventory Controller * This module is a registry page for stock inventories */ -function StockInventoriesController(Stock, Notify, - uiGridConstants, $translate, Modal, - SearchFilterFormat, Languages, Session, Grouping, bhConstants, GridStateService, $state) { +function StockInventoriesController( + Stock, Notify, uiGridConstants, $translate, Modal, + SearchFilterFormat, Languages, Session, Grouping, bhConstants, GridState, $state, Columns +) { var vm = this; - + var filterKey = 'inventory'; + var StockInventoryFilters = Stock.filter.inventory; var cacheKey = 'stock-inventory-grid'; - - // global variables - // vm.filters = { lang: Languages.key }; - // vm.formatedFilters = []; - vm.enterprise = Session.enterprise; + var state; + var gridColumns; var columns = [ { field : 'depot_text', @@ -103,24 +102,31 @@ function StockInventoriesController(Stock, Notify, cellTemplate : 'modules/stock/inventories/templates/appro.cell.html' }, ]; - // options for the UI grid + vm.enterprise = Session.enterprise; + vm.gridApi = {}; + vm.gridOptions = { appScopeProvider : vm, enableColumnMenus : false, columnDefs : columns, enableSorting : true, - // showColumnFooter : true, fastWatch : true, flatEntityAccess : true, + onRegisterApi : onRegisterApi, }; vm.grouping = new Grouping(vm.gridOptions, true, 'depot_text', vm.grouped, true); + state = new GridState(vm.gridOptions, cacheKey); + + function onRegisterApi(gridApi) { + vm.gridApi = gridApi; + } - // expose to the view - vm.search = search; - vm.onRemoveFilter = onRemoveFilter; - vm.clearFilters = clearFilters; - vm.setStatusFlag = setStatusFlag; + // expose view logic + // vm.search = search; + // vm.onRemoveFilter = onRemoveFilter; + // vm.clearFilters = clearFilters; + // vm.setStatusFlag = setStatusFlag; function setStatusFlag(item) { item.isSoldOut = item.status === bhConstants.stockStatus.IS_SOLD_OUT; @@ -142,21 +148,24 @@ function StockInventoriesController(Stock, Notify, // load stock lots in the grid function load(filters) { + vm.hasError = false; vm.loading = true; - Stock.inventories.read(null, filters).then(function (rows) { - vm.loading = false; + Stock.inventories.read(null, filters) + .then(function (rows) { + // set status flags + rows.forEach(function (item) { + setStatusFlag(item); + }); - // set status flags - rows.forEach(function (item) { - setStatusFlag(item); - }); + vm.gridOptions.data = rows; - vm.gridOptions.data = rows; - - vm.grouping.unfoldAllGroups(); - }) - .catch(Notify.handleError); + vm.grouping.unfoldAllGroups(); + }) + .catch(Notify.handleError) + .finally(function () { + vm.loading = false; + }); } // search modal @@ -176,5 +185,16 @@ function StockInventoriesController(Stock, Notify, load(filters.identifiers); } - load(); + function startup (){ + if ($state.params.filters) { + var changes = [{ key : $state.params.filters.key, value : $state.params.filters.value }] + stockMovementFilters.replaceFilters(changes); + Stock.cacheFilters(filterKey); + } + + load(StockInventoryFilters.formatHTTP(true)); + vm.latestViewFilters = StockInventoryFilters.formatView(); + } + + startup(); }