Skip to content

Commit

Permalink
feat(filter): Add a filter service into the stocj service
Browse files Browse the repository at this point in the history
  • Loading branch information
DedrickEnc committed Sep 26, 2017
1 parent ca42be8 commit 1cb3398
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 29 deletions.
35 changes: 35 additions & 0 deletions client/src/js/services/StockService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down
78 changes: 49 additions & 29 deletions client/src/modules/stock/inventories/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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();
}

0 comments on commit 1cb3398

Please sign in to comment.