Skip to content

Commit

Permalink
feat(vouchers): filter on account
Browse files Browse the repository at this point in the history
This commit implements filtering by account on the voucher search modal.
Contributes to #2412.
  • Loading branch information
jniles committed Dec 16, 2017
1 parent ced42e0 commit 01ac57e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 53 deletions.
9 changes: 9 additions & 0 deletions client/src/modules/vouchers/modals/search.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
<bh-clear on-clear="$ctrl.clear('user_id')"></bh-clear>
</bh-user-select>

<bh-account-select
account-id="$ctrl.searchQueries.account_id"
name="account_id"
on-select-callback="$ctrl.onSelectAccount(account)"
exclude-title-accounts="true"
required="false">
<bh-clear on-clear="$ctrl.clear('account_id')"></bh-clear>
</bh-account-select>

<bh-transaction-type-select
on-change="$ctrl.onTransactionTypesChange(transactionTypes)"
transaction-type-ids="$ctrl.searchQueries.type_ids">
Expand Down
65 changes: 33 additions & 32 deletions client/src/modules/vouchers/modals/search.modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ angular.module('bhima.controllers')
.controller('VoucherRegistrySearchModalController', VoucherRegistrySearchModalController);

VoucherRegistrySearchModalController.$inject = [
'$uibModalInstance', 'filters', 'NotifyService', 'moment', 'PeriodService', 'Store', 'util',
'TransactionTypeService', '$translate', 'VoucherService',
'$uibModalInstance', 'filters', 'NotifyService', 'PeriodService', 'Store',
'util', 'VoucherService',
];

/**
Expand All @@ -14,50 +14,40 @@ VoucherRegistrySearchModalController.$inject = [
* returning it as a JSON object to the parent controller. The data can be
* preset by passing in a filters object using filtersProvider().
*/
function VoucherRegistrySearchModalController(ModalInstance, filters, Notify, moment, Periods, Store, util,
TransactionTypes, $translate, Vouchers) {
function VoucherRegistrySearchModalController(
ModalInstance, filters, Notify, Periods, Store, util,
Vouchers
) {
var vm = this;
var changes = new Store({ identifier : 'key' });
var searchQueryOptions = [
'reference', 'description', 'user_id', 'type_ids',
'reference', 'description', 'user_id', 'type_ids', 'account_id',
];

// displayValues will be an id:displayValue pair
var displayValues = {};
var lastDisplayValues = Vouchers.filters.getDisplayValueMap();

vm.filters = filters;
// searchQueries is the same id:value pair
vm.searchQueries = {};

vm.defaultQueries = {};

var lastDisplayValues = Vouchers.filters.getDisplayValueMap();

// displayValues will be an id:displayValue pair
var displayValues = {};

// assign already defined custom filters to searchQueries object
vm.searchQueries = util.maskObjectFromKeys(filters, searchQueryOptions);

// load all Transaction types
TransactionTypes.read()
.then(function (types) {
types.forEach(function (item) {
item.typeText = $translate.instant(item.text);
});
vm.transactionTypes = types;
})
.catch(Notify.handleError);

if (filters.limit) {
vm.defaultQueries.limit = filters.limit;
}

vm.onTransactionTypesChange = function onTransactionTypesChange(transactionTypes) {
vm.searchQueries.type_ids = transactionTypes;
var typeText = '/';
vm.searchQueries.type_ids = transactionTypes;

transactionTypes.forEach(function (typeId) {
vm.transactionTypes.forEach(function (type) {
if (typeId === type.id) {
typeText += type.typeText + ' / ';
typeText += type.typeText.concat(' / ');
}
});
});
Expand All @@ -81,13 +71,19 @@ function VoucherRegistrySearchModalController(ModalInstance, filters, Notify, mo
};

// default filter limit - directly write to changes list
vm.onSelectLimit = function onSelectLimit(value) {
vm.onSelectLimit = function onSelectLimit(_value) {
// input is type value, this will only be defined for a valid number
if (angular.isDefined(value)) {
changes.post({ key : 'limit', value : value });
if (angular.isDefined(_value)) {
changes.post({ key : 'limit', value : _value });
}
};

// custom filter account_id - assign the value to the searchQueries object
vm.onSelectAccount = function onSelectAccount(account) {
vm.searchQueries.account_id = account.id;
displayValues.account_id = String(account.number).concat(' - ', account.label);
};

// deletes a filter from the custom filter object, this key will no longer be written to changes on exit
vm.clear = function clear(key) {
delete vm.searchQueries[key];
Expand All @@ -97,21 +93,26 @@ function VoucherRegistrySearchModalController(ModalInstance, filters, Notify, mo

// submit the filter object to the parent controller.
vm.submit = function submit(form) {
var _displayValue;
var loggedChanges;

if (form.$invalid) { return; }

// delete type_ids if there is no transaction type sent
if (vm.searchQueries.type_ids && vm.searchQueries.type_ids.length === 0) {
vm.clear('type_ids');
}

// push all searchQuery values into the changes array to be applied
angular.forEach(vm.searchQueries, function (value, key) {
if (angular.isDefined(value)) {
angular.forEach(vm.searchQueries, function (_value, _key) {
if (angular.isDefined(_value)) {
// default to the original value if no display value is defined
var displayValue = displayValues[key] || lastDisplayValues[key] || value;
changes.post({ key: key, value: value, displayValue: displayValue });
}
_displayValue = displayValues[_key] || lastDisplayValues[_key] || _value;
changes.post({ key : _key, value : _value, displayValue : _displayValue });
}
});

var loggedChanges = changes.getAll();
loggedChanges = changes.getAll();

// return values to the voucher controller
return ModalInstance.close(loggedChanges);
Expand Down
43 changes: 22 additions & 21 deletions client/src/modules/vouchers/vouchers.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ angular.module('bhima.services')
VoucherService.$inject = [
'PrototypeApiService', 'TransactionTypeStoreService', '$uibModal',
'FilterService', 'PeriodService', 'LanguageService', '$httpParamSerializer',
'appcache', 'bhConstants', 'TransactionService', '$translate'
'appcache', 'bhConstants', 'TransactionService', '$translate',
];

/**
Expand Down Expand Up @@ -41,14 +41,16 @@ function VoucherService(
voucherFilters.registerDefaultFilters(bhConstants.defaultFilters);

voucherFilters.registerCustomFilters([
{ key: 'user_id', label: 'FORM.LABELS.USER' },
{ key: 'reference', label: 'FORM.LABELS.REFERENCE' },
{ key: 'reversed', label: 'FORM.INFO.ANNULLED' },
{ key: 'description', label: 'FORM.LABELS.DESCRIPTION' },
{ key: 'entity_uuid', label: 'FORM.LABELS.ENTITY' },
{ key: 'cash_uuid', label: 'FORM.INFO.PAYMENT' },
{ key: 'invoice_uuid', label: 'FORM.LABELS.INVOICE' },
{ key: 'type_ids', label: 'FORM.LABELS.TRANSACTION_TYPE' }]);
{ key : 'user_id', label : 'FORM.LABELS.USER' },
{ key : 'reference', label : 'FORM.LABELS.REFERENCE' },
{ key : 'reversed', label : 'FORM.INFO.ANNULLED' },
{ key : 'description', label : 'FORM.LABELS.DESCRIPTION' },
{ key : 'entity_uuid', label : 'FORM.LABELS.ENTITY' },
{ key : 'account_id', label : 'FORM.LABELS.ACCOUNT' },
{ key : 'cash_uuid', label : 'FORM.INFO.PAYMENT' },
{ key : 'invoice_uuid', label : 'FORM.LABELS.INVOICE' },
{ key : 'type_ids', label : 'FORM.LABELS.TRANSACTION_TYPE' },
]);


if (filterCache.filters) {
Expand Down Expand Up @@ -134,7 +136,7 @@ function VoucherService(
return sum + row.debit;
}, 0);

return Api.create.call(service, { voucher: v });
return Api.create.call(service, { voucher : v });
}

/**
Expand All @@ -161,14 +163,13 @@ function VoucherService(
item.hrText = $translate.instant(item.text);
return item;
});
})
;
});
}

// downloads a type of report based on the
function download(type) {
var filterOpts = voucherFilters.formatHTTP();
var defaultOpts = { renderer: type, lang: Languages.key };
var defaultOpts = { renderer : type, lang: Languages.key };

// combine options
var options = angular.merge(defaultOpts, filterOpts);
Expand All @@ -184,14 +185,14 @@ function VoucherService(
*/
function openSearchModal(filters) {
return Modal.open({
templateUrl: 'modules/vouchers/modals/search.modal.html',
size: 'md',
animation: false,
keyboard: false,
backdrop: 'static',
controller: 'VoucherRegistrySearchModalController as $ctrl',
resolve: {
filters: function filtersProvider() { return filters; },
templateUrl : 'modules/vouchers/modals/search.modal.html',
size : 'md',
animation : false,
keyboard : false,
backdrop : 'static',
controller : 'VoucherRegistrySearchModalController as $ctrl',
resolve : {
filters : function filtersProvider() { return filters; },
},
}).result;
}
Expand Down

0 comments on commit 01ac57e

Please sign in to comment.