Skip to content

Commit

Permalink
fix(vouchers): filter by default by the current day (#1464)
Browse files Browse the repository at this point in the history
This commit enhances the performance of the voucher registry by making the default filter today.
  • Loading branch information
mbayopanda authored and jniles committed Apr 4, 2017
1 parent a47ca96 commit 9b88b1e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions client/src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ function constantConfig() {
CREDIT_NOTE : 10,
INCOME : 'income',
EXPENSE : 'expense',
OTHER : 'other',
},
reports : {
AGED_DEBTOR : 'AGED_DEBTOR',
Expand Down
13 changes: 11 additions & 2 deletions client/src/js/services/VoucherService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ angular.module('bhima.services')
.service('VoucherService', VoucherService);

VoucherService.$inject = [
'PrototypeApiService', '$http', 'util', 'TransactionTypeStoreService', '$uibModal'
'PrototypeApiService', '$http', 'util', 'TransactionTypeStoreService', '$uibModal',
'FilterService',
];

/**
Expand All @@ -13,11 +14,13 @@ VoucherService.$inject = [
* This service manages posting data to the database via the /vouchers/ URL. It also
* includes some utilities that are useful for voucher pages.
*/
function VoucherService(Api, $http, util, TransactionTypeStore, Modal) {
function VoucherService(Api, $http, util, TransactionTypeStore, Modal,
Filters) {
var service = new Api('/vouchers/');

// @todo - remove this reference to baseUrl
var baseUrl = '/journal/';
var filter = new Filters();

service.create = create;
service.reverse = reverse;
Expand Down Expand Up @@ -106,13 +109,19 @@ function VoucherService(Api, $http, util, TransactionTypeStore, Modal) {
{ field: 'dateTo', displayName: 'FORM.LABELS.DATE', comparitor: '<', ngFilter: 'date' },
{ field: 'reversed', displayName: 'FORM.INFO.ANNULLED' },
{ field: 'description', displayname: 'FORM.LABELS.DESCRIPTION' },
{ field: 'defaultPeriod', displayName : 'TABLE.COLUMNS.PERIOD', ngFilter : 'translate' },
];

// returns columns from filters
return columns.filter(function (column) {
var value = params[column.field];
if (angular.isDefined(value)) {
column.value = value;

if (column.field === 'defaultPeriod') {
column.value = filter.lookupPeriod(value).label;
}

return true;
} else {
return false;
Expand Down
3 changes: 2 additions & 1 deletion client/src/partials/templates/grid/voucherType.tmpl.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<div class="ui-grid-cell-contents">
<span class="label" ng-class="{
'label-success': row.entity._isIncome,
'label-warning': row.entity._isExpense
'label-warning': row.entity._isExpense,
'label-danger': row.entity._isOther
}">
<span translate>{{ row.entity._type }}</span>
<span ng-if="row.groupHeader">{{ COL_FIELD }}</span>
Expand Down
1 change: 1 addition & 0 deletions client/src/partials/vouchers/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

<a
href
ng-if="VoucherCtrl.filter.customFiltersApplied(VoucherCtrl.filters)"
ng-click="VoucherCtrl.clearFilters()"
class="text-danger"
data-method="clear">
Expand Down
35 changes: 25 additions & 10 deletions client/src/partials/vouchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ angular.module('bhima.controllers')
// dependencies injection
VoucherController.$inject = [
'VoucherService', 'NotifyService', 'GridFilteringService', 'uiGridGroupingConstants', 'uiGridConstants',
'bhConstants', 'ReceiptModal', 'GridSortingService', '$state', 'appcache'
'bhConstants', 'ReceiptModal', 'GridSortingService', '$state', 'appcache',
'FilterService',
];

/**
Expand All @@ -13,16 +14,21 @@ VoucherController.$inject = [
* @description
* This controller is responsible for display all vouchers in the voucher table.
*/
function VoucherController(Vouchers, Notify, Filtering, uiGridGroupingConstants, uiGridConstants, bhConstants, Receipts, Sorting, $state, AppCache) {
function VoucherController(Vouchers, Notify, Filtering, uiGridGroupingConstants,
uiGridConstants, bhConstants, Receipts, Sorting, $state, AppCache,
Filters) {
var vm = this;
var filter = new Filters();
var filtering;
var FILTER_BAR_HEIGHT;
var cache = new AppCache('VoucherRegistry');

var INCOME = bhConstants.transactionType.INCOME;
var EXPENSE = bhConstants.transactionType.EXPENSE;
var OTHER = bhConstants.transactionType.OTHER;

/* global variables */
vm.filter = filter;
vm.filterEnabled = false;
vm.transactionTypes = {};
vm.gridApi = {};
Expand All @@ -45,8 +51,6 @@ function VoucherController(Vouchers, Notify, Filtering, uiGridGroupingConstants,
flatEntityAccess : true,
fastWatch : true,
enableFiltering : vm.filterEnabled,
fastWatch : true,
flatEntityAccess : true,
rowTemplate : '/partials/templates/grid/voucher.row.html',
};

Expand Down Expand Up @@ -139,6 +143,11 @@ function VoucherController(Vouchers, Notify, Filtering, uiGridGroupingConstants,
Vouchers.openSearchModal(vm.filters)
.then(function (parameters) {
if (!parameters) { return; }

if (parameters.defaultPeriod) {
delete parameters.defaultPeriod;
}

cacheFilters(parameters);
return load(vm.filters);
});
Expand All @@ -149,20 +158,25 @@ function VoucherController(Vouchers, Notify, Filtering, uiGridGroupingConstants,
Receipts.voucher(uuid);
}

function isEmpty(object) {
return Object.keys(object).length === 0;
}

function load(parameters) {
// flush error and loading states
vm.hasError = false;
toggleLoadingIndicator();

Vouchers.read(null, parameters)
.then(function (vouchers) {
Vouchers.read(null, parameters).then(function (vouchers) {
vm.gridOptions.data = vouchers;

// loop through the vouchers and precompute the voucher type tags
vouchers.forEach(function (voucher) {
voucher._isIncome = (voucher.type_id === INCOME);
voucher._isExpense = (voucher.type_id === EXPENSE);
voucher._type = get(voucher.type_id).text;
var transaction = get(voucher.type_id);
voucher._isIncome = (transaction.type === INCOME);
voucher._isExpense = (transaction.type === EXPENSE);
voucher._isOther = (transaction.type === OTHER);
voucher._type = transaction.text;
});

vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
Expand All @@ -180,6 +194,7 @@ function VoucherController(Vouchers, Notify, Filtering, uiGridGroupingConstants,

// save the parameters to use later. Formats the parameters in filtersFmt for the filter toolbar.
function cacheFilters(filters) {
filters = filter.applyDefaults(filters);
vm.filters = cache.filters = filters;
vm.filtersFmt = Vouchers.formatFilterParameters(filters);

Expand All @@ -197,7 +212,7 @@ function VoucherController(Vouchers, Notify, Filtering, uiGridGroupingConstants,
// clears the filters by forcing a cache of an empty array
function clearFilters() {
cacheFilters({});
load();
load(vm.filters);
}

/**
Expand Down
1 change: 1 addition & 0 deletions server/controllers/finance/vouchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function find(options) {

filters.dateFrom('dateFrom', 'date');
filters.dateTo('dateTo', 'date');
filters.period('defaultPeriod', 'date');

const referenceStatement = `CONCAT_WS('.', '${entityIdentifier}', p.abbr, v.reference) = ?`;
filters.custom('reference', referenceStatement);
Expand Down

0 comments on commit 9b88b1e

Please sign in to comment.