Skip to content

Commit

Permalink
feat(ui-grid): add reference sorting algorithm
Browse files Browse the repository at this point in the history
This commit adds an algorithm to sort all references generated by the
system.  It parses the new identifier syntax and sorts on the integer
values of the identifier.

Closes #181.
  • Loading branch information
Jonathan Niles committed Jan 21, 2017
1 parent ca955d8 commit 5c3480a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
32 changes: 30 additions & 2 deletions client/src/js/services/grid/GridSorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ function GridSortingService(util) {
} else {

// reference value is passed in the row - simply use this
// FIXME(@jniles) we rarely pass the numeric reference value...
first = Number(rowA.entity.reference);
second = Number(rowB.entity.reference);
}

// This (standard method) casuses transaction groups to be sorted incorrectly - why has not been demonstrated
// Standard integer compare sort: retrun first - second;
// This (standard method) causes transaction groups to be sorted incorrectly - why has not been demonstrated
// Standard integer compare sort: return first - second;
if (first > second) {
return 1;
}
Expand Down Expand Up @@ -89,5 +90,32 @@ function GridSortingService(util) {
}.bind(this));
}

/**
* @function sortByReferece
*
* @description
* Sorts references as if they were numerical values.
*
* @public
*/
function sortByReference(a, b) {

// get the last index of the dot in the reference (plus offset)
var aIdx = a.lastIndexOf('.') + 1,
bIdx = b.lastIndexOf('.') + 1;

// get the numerical value
var aReference = parseInt(a.slice(aIdx)),
bReference = parseInt(b.slice(bIdx));

// return the correct numerical value
return aReference - bReference;
}

// bind all algorithms for public consumption
GridSorting.algorithms = {
sortByReference : sortByReference
};

return GridSorting;
}
8 changes: 5 additions & 3 deletions client/src/partials/cash/payments/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ angular.module('bhima.controllers')
// dependencies injection
CashPaymentRegistryController.$inject = [
'CashService', 'bhConstants', 'NotifyService', 'SessionService', 'uiGridConstants',
'uiGridGroupingConstants', 'LanguageService', 'appcache', 'ReceiptModal', 'ModalService'
'uiGridGroupingConstants', 'LanguageService', 'appcache', 'ReceiptModal', 'ModalService',
'GridSortingService'
];

/**
Expand All @@ -13,7 +14,7 @@ CashPaymentRegistryController.$inject = [
* This controller is responsible to display all cash payment made and provides
* print and search utilities for the registry.`j
*/
function CashPaymentRegistryController(Cash, bhConstants, Notify, Session, uiGridConstants, uiGridGroupingConstants, Languages, AppCache, Receipt, Modal) {
function CashPaymentRegistryController(Cash, bhConstants, Notify, Session, uiGridConstants, uiGridGroupingConstants, Languages, AppCache, Receipt, Modal, Sorting) {
var vm = this;

var cache = AppCache('CashRegistry');
Expand Down Expand Up @@ -51,7 +52,8 @@ function CashPaymentRegistryController(Cash, bhConstants, Notify, Session, uiGri

vm.gridOptions.columnDefs = [{
field : 'reference', displayName : 'TABLE.COLUMNS.REFERENCE',
headerCellFilter: 'translate', aggregationType: uiGridConstants.aggregationTypes.count, aggregationHideLabel : true
headerCellFilter: 'translate', aggregationType: uiGridConstants.aggregationTypes.count,
aggregationHideLabel : true, sortingAlgorithm : Sorting.algorithms.sortByReference
}, {
field : 'date', displayName : 'TABLE.COLUMNS.DATE', headerCellFilter: 'translate', cellFilter : 'date:"mediumDate"',
}, {
Expand Down
7 changes: 4 additions & 3 deletions client/src/partials/patient_invoice/registry/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ angular.module('bhima.controllers')
InvoiceRegistryController.$inject = [
'PatientInvoiceService', 'bhConstants', 'NotifyService',
'SessionService', 'util', 'ReceiptModal', 'appcache',
'uiGridConstants', 'ModalService', 'CashService'
'uiGridConstants', 'ModalService', 'CashService', 'GridSortingService'
];

/**
* Invoice Registry Controller
*
* This module is responsible for the management of Invoice Registry.
*/
function InvoiceRegistryController(Invoices, bhConstants, Notify, Session, util, Receipt, AppCache, uiGridConstants, ModalService, Cash) {
function InvoiceRegistryController(Invoices, bhConstants, Notify, Session, util, Receipt, AppCache, uiGridConstants, ModalService, Cash, Sorting) {
var vm = this;

var cache = AppCache('InvoiceRegistry');
Expand All @@ -39,7 +39,8 @@ function InvoiceRegistryController(Invoices, bhConstants, Notify, Session, util,
headerCellFilter: 'translate',
aggregationType: uiGridConstants.aggregationTypes.count,
aggregationHideLabel : true,
footerCellClass : 'text-center'
footerCellClass : 'text-center',
sortingAlgorithm : Sorting.algorithms.sortByReference
},
{ field : 'date', cellFilter:'date', displayName : 'TABLE.COLUMNS.BILLING_DATE', headerCellFilter : 'translate', type: 'date' },
{ field : 'patientName', displayName : 'TABLE.COLUMNS.PATIENT', headerCellFilter : 'translate' },
Expand Down
8 changes: 5 additions & 3 deletions client/src/partials/patients/registry/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ angular.module('bhima.controllers')
.controller('PatientRegistryController', PatientRegistryController);

PatientRegistryController.$inject = [
'$state', 'PatientService', 'NotifyService', 'AppCache', 'util', 'ReceiptModal', 'uiGridConstants', '$translate', 'GridColumnService'
'$state', 'PatientService', 'NotifyService', 'AppCache', 'util', 'ReceiptModal',
'uiGridConstants', '$translate', 'GridColumnService', 'GridSortingService'
];

/**
* Patient Registry Controller
*
* This module is responsible for the management of Patient Registry.
*/
function PatientRegistryController($state, Patients, Notify, AppCache, util, Receipts, uiGridConstants, $translate, Columns) {
function PatientRegistryController($state, Patients, Notify, AppCache, util, Receipts, uiGridConstants, $translate, Columns, Sorting) {
var vm = this;

var cacheKey = 'PatientRegistry';
Expand Down Expand Up @@ -52,7 +53,8 @@ function PatientRegistryController($state, Patients, Notify, AppCache, util, Rec
displayName : 'TABLE.COLUMNS.REFERENCE',
aggregationType: uiGridConstants.aggregationTypes.count,
aggregationHideLabel : true, headerCellFilter: 'translate',
footerCellClass : 'text-center'
footerCellClass : 'text-center',
sortingAlgorithm : Sorting.algorithms.sortByReference
},
{ field : 'display_name', displayName : 'TABLE.COLUMNS.NAME', headerCellFilter: 'translate' },
{ field : 'patientAge', displayName : 'TABLE.COLUMNS.AGE', headerCellFilter: 'translate', type: 'number' },
Expand Down
18 changes: 11 additions & 7 deletions client/src/partials/vouchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ angular.module('bhima.controllers')
VoucherController.$inject = [
'VoucherService', '$translate', 'NotifyService', 'GridFilteringService',
'uiGridGroupingConstants', 'uiGridConstants', 'ModalService', 'DateService',
'bhConstants', 'ReceiptModal'
'bhConstants', 'ReceiptModal', 'GridSortingService'
];

/**
Expand All @@ -14,7 +14,7 @@ VoucherController.$inject = [
* @description
* This controller is responsible for display all vouchers in the voucher table.
*/
function VoucherController(Vouchers, $translate, Notify, Filtering, uiGridGroupingConstants, uiGridConstants, Modal, Dates, bhConstants, Receipts) {
function VoucherController(Vouchers, $translate, Notify, Filtering, uiGridGroupingConstants, uiGridConstants, Modal, Dates, bhConstants, Receipts, Sorting) {
var vm = this;

/* global variables */
Expand Down Expand Up @@ -60,29 +60,33 @@ function VoucherController(Vouchers, $translate, Notify, Filtering, uiGridGroupi
// grid default options
vm.gridOptions.columnDefs = [
{ field : 'reference', displayName : 'TABLE.COLUMNS.REFERENCE', headerCellFilter: 'translate',
groupingShowAggregationMenu: false,
aggregationType: uiGridConstants.aggregationTypes.count
treeAggregationType: uiGridGroupingConstants.aggregation.COUNT,
sortingAlgorithm : Sorting.algorithms.sortByReference,
treeAggregationLabel: '', footerCellClass : 'text-center',
},
{ field : 'type_id', displayName : 'TABLE.COLUMNS.TYPE', headerCellFilter: 'translate',
sort: { priority: 0, direction : 'asc' },
grouping: { groupPriority: 0},
cellTemplate: 'partials/templates/grid/voucherType.tmpl.html',
treeAggregationType: uiGridGroupingConstants.aggregation.SUM,
customTreeAggregationFinalizerFn: typeAggregation,
treeAggregationLabel : '',
groupingShowAggregationMenu: false
},
{ field : 'date', displayName : 'TABLE.COLUMNS.DATE', headerCellFilter: 'translate',
cellFilter : 'date:"mediumDate"',
cellFilter : 'date',
filter : { condition : filtering.byDate },
customTreeAggregationFinalizerFn: timeAggregation,
treeAggregationLabel : '', type : 'date',
groupingShowAggregationMenu: false
},
{ field : 'description', displayName : 'TABLE.COLUMNS.DESCRIPTION', headerCellFilter: 'translate',
groupingShowAggregationMenu: false
},
{ field : 'amount', displayName : 'TABLE.COLUMNS.AMOUNT', headerCellFilter: 'translate',
treeAggregationType: uiGridGroupingConstants.aggregation.SUM,
groupingShowAggregationMenu: false
treeAggregationLabel : '', footerCellClass : 'text-center',
type: 'number', groupingShowAggregationMenu: false
},
{ field : 'display_name', displayName : 'TABLE.COLUMNS.RESPONSIBLE', headerCellFilter: 'translate',
groupingShowAggregationMenu: false
Expand Down Expand Up @@ -166,7 +170,7 @@ function VoucherController(Vouchers, $translate, Notify, Filtering, uiGridGroupi
Notify.handleError(err);
})
.finally(function () {
vm.loading = false;
toggleLoadingIndicator();
});
}

Expand Down

0 comments on commit 5c3480a

Please sign in to comment.