Skip to content

Commit

Permalink
fix(grid): transition journal to grid services
Browse files Browse the repository at this point in the history
Closes #376.
Closes #586.

This commit completes the migration of specific posting journal services
to grid services.  It also removes the ability to paginate the posting
journal as this increases the complexity of the process several fold.

In addition to migrating the service, the GridFiltering service now
provides the option to toggle (inline) filtering on and off.  This could be
useful for power users wanting to filter the view quickly.
  • Loading branch information
Jonathan Niles committed Jul 21, 2016
1 parent 447bc8a commit fc883df
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 151 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"angular-messages": "^1.5.6",
"angular-bootstrap": "^2.0.0",
"angular-ui-router": "^0.2.18",
"angular-ui-grid": "^3.1.1",
"angular-ui-grid": "^3.2.1",
"angular-translate": "^2.11.0",
"angular-translate-loader-static-files": "^2.11.0",
"angular-translate-loader-url": "^2.11.0",
Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion client/src/js/services/grid/GridColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ GridColumnService.$inject = [
*/
function GridColumnService(uiGridConstants, AppCache, Modal) {

/** @const cache alias for this service */
var serviceKey = '-Columns';

/**
* @method cacheDefaultColumnVisibility
*
Expand Down Expand Up @@ -82,7 +85,7 @@ function GridColumnService(uiGridConstants, AppCache, Modal) {
function Columns(gridOptions, cacheKey) {

// set up local storage for selected grid columns
this.cache = AppCache(cacheKey);
this.cache = AppCache(cacheKey + serviceKey);

// bind access to the gridOptions
this.gridOptions = gridOptions;
Expand Down
69 changes: 69 additions & 0 deletions client/src/js/services/grid/GridFiltering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
angular.module('bhima.services')
.service('GridFilteringService', GridFilteringService);

GridFilteringService.$inject = ['appcache', 'uiGridConstants'];

/**
* Grid Filter Service
*
* This service is responsible for defining the global configuration for
* filtering for ui-grids.
*/
function GridFilteringService(AppCache, uiGridConstants) {

/** @const service key */
var serviceKey = '-Filtering';

function GridFiltering(gridOptions, cacheKey) {
this.gridOptions = gridOptions;

var cache = this.cache = AppCache(cacheKey + serviceKey);

// global filtering configuration
cache.enableFiltering = cache.enableFiltering || true;
gridOptions.enableFiltering = cache.enableFiltering;

gridOptions.onRegisterApi = function onRegisterApi(api) {
this.gridApi = api;
}.bind(this);
}

/**
* @method filterByDate
*
* @description
* Matches the date string provided in the string.
*/
GridFiltering.prototype.filterByDate = function filterByDate(searchValue, cellValue) {
var cellDate = new Date(cellValue);

var month = cellDate.getMonth();
var date = cellDate.getDate();
var year = cellDate.getFullYear();

var cellMonth = (month < 9) ? '0' + (month + 1) : (month + 1);
var cellDateLong = (date < 10) ? '0' + date : date;
var cellDateString = year + '-' + cellMonth + '-' + cellDateLong;

searchValue = searchValue.replace(/\\/g, '');

return cellDateString.indexOf(searchValue) !== -1;
};

/**
* @method toggleInlineFiltering
*
* @description
* This method toggles the inline grid filters on the column headers of a grid.
*/
GridFiltering.prototype.toggleInlineFiltering = function toggleInlineFiltering() {
if (!this.gridOptions) { return; }

this.gridOptions.enableFiltering = !this.gridOptions.enableFiltering;
this.cache.enableFiltering = this.gridOptions.enableFiltering;

this.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
};

return GridFiltering;
}
1 change: 0 additions & 1 deletion client/src/js/services/grid/GridGrouping.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ function GridGroupingService(uiGridGroupingConstants, $filter, Session, $timeout
// used to render amounts in the aggregate columns
// TODO - this should render the currency from the row set.
customTreeAggregationFinalizerFn : function amountRenderer(aggregation) {
//console.log('aggregation:', aggregation);
aggregation.rendered = $currency(aggregation.value, currencyId);
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
angular.module('bhima.services')
.service('JournalPaginationService', JournalPaginationService);
.service('GridPaginationService', GridPaginationService);

JournalPaginationService.$inject = [];
GridPaginationService.$inject = [];

/**
* Posting Journal Pagination Service
* Grid Pagination Service
*
* This service contains pagination configuration and utility methods for the
* posting journal module. Custom pagination rules and functions are defined to
* ensure that transactions are respected across pages (when grouped).
* This service contains pagination configuration and utility methods for any
* ui-grids. Custom pagination rules and functions are defined to ensure that
* transactions are respected across pages (when grouped).
*
* @todo Discuss if pagination should be encapsulated by TransactionService; this
* logic could be incorperation whilst loading transactions however this may result
* logic could be incorporated whilst loading transactions however this may result
* in too much responsibility.
*
* @todo Discuss if pagination is required in an accountants workflow - how many
* rows are we expecting etc.
*/
function JournalPaginationService() {
function GridPaginationService() {
var service = this;

/**
Expand Down Expand Up @@ -103,7 +103,7 @@ function JournalPaginationService() {
// bind external pagination method
gridApi.pagination.on.paginationChanged(null, fetchPage);

// Settup initial page
// Setup initial page
fetchPage(paginationOptions.pageNumber);
}

Expand Down
34 changes: 0 additions & 34 deletions client/src/js/services/grid/JournalFiltering.js

This file was deleted.

92 changes: 3 additions & 89 deletions client/src/js/services/grid/TransactionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@ TransactionService.$inject = ['$http', 'Store', 'uuid'];
*
* This service is responsible for fetching transactions from the posting journal
* and providing a number of utility methods for manipulating and framing this
* information. Data can be served in one go or using a custom pagincation view
* information. Data can be served in one go or using a custom pagination view
* serving transactions in pages.
*
* @todo Discuss as a team how pages would be most logically demonstrated with
* respect to transactions
* @todo Service is designed to be called once per page - this use case should
* be discussed
* @todo Update service to use the latest posting journal interface/ API
*/
function TransactionService($http, Store, uuid) {
var service = this;

// @todo update service to use latest posting jounral interface/ API
var source = '/journal';

// model to contain transactions - storing this information in a store
// allows us to perform gets/puts based on a transactions UUID locally
var transactionModel = new Store({
identifier : 'uuid'
});

transactionModel.setData([]);

/**
Expand All @@ -40,94 +37,11 @@ function TransactionService($http, Store, uuid) {
* in pagination logic
*/
function fetchTransactions() {
$http.get(source)
return $http.get(source)
.then(function (response) {
var transactions = response.data;

transactions.map(function (item) { return transactionModel.post(item); });
});
}

/** DEVELOPMENT UTILITIES --------------------------------------------------*/

/**
* Mock transactions that would normally be returned from the server, this method
* is primarily designed to help test core journal features before all of the
* additional core finance pieces are completed. It will be depricated as soon
* as 2.x is feature complete.
*/
function mockTransactions() {

// configure mock configuration
var numberOfTransactions = 100;
var transactionPrefix = 'TRANS';
var descriptionPrefix = 'Mock transaction for ID: ';
var currencyId = 1;
var accounts = [100, 101, 102, 200, 202, 203, 400, 500, 600];

var transactions = [];

// each iteration will create a new transaction, a transaction can contain any
// number of rows
for (var i = 0; i < numberOfTransactions; i++) {
var currentTransaction = buildMockTransaction(i);
transactions = transactions.concat(currentTransaction);
}

function buildMockTransaction(id) {
var transaction;
var upperLines = 10;
var upperCost = 10000;
var numberOfLines = Math.round(Math.random() * upperLines);
var cost = selectEvenNumber(upperCost);
var date = selectDate();
var transactionId = transactionPrefix.concat(id);

// array of (n) undefined elements
transaction = Array.apply(null, { length : numberOfLines });

return transaction.map(function (row) {

return {
uuid : uuid(),
trans_id : transactionId,
trans_date : date,
description : descriptionPrefix.concat(transactionId),
reference : id,
currency_id : currencyId,
account_number : selectAccount(),
account : selectEvenNumber,

// @todo to verify aggregation these values will have to sum according
// to double entry accounting (balance)
debit_equiv : selectEvenNumber(cost),
credit_equiv : selectEvenNumber(cost),
};
});
}

function selectDate() {
var start = new Date(2014, 0, 1);
var end = new Date();

return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}

function selectEvenNumber(max) {
var number = (Math.random() * max);
return number - number % 2;
}

function selectAccount() {
return accounts[Math.round(Math.random() * (accounts.length - 1))];
}

transactions.map(function (item) { return transactionModel.post(item); });
}

// set up service default state - populate with default data
mockTransactions();

// Expose the transactionModel
service.list = transactionModel;
}
3 changes: 1 addition & 2 deletions client/src/partials/journal/journal.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
ui-grid-resize-columns
ui-grid-move-columns
ui-grid-selection
ui-grid-grouping
ui-grid-pagination>
ui-grid-grouping>
</div>
</div>
</div>
Expand Down
26 changes: 12 additions & 14 deletions client/src/partials/journal/journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ angular.module('bhima.controllers')
.controller('JournalController', JournalController);

JournalController.$inject = [
'TransactionService', 'JournalService', 'GridSortingService', 'GridGroupingService',
'JournalPaginationService', 'JournalFilteringService', 'GridColumnService', 'JournalConfigService',
'JournalService', 'GridSortingService', 'GridGroupingService',
'GridFilteringService', 'GridColumnService', 'JournalConfigService',
'NotifyService'
];

Expand All @@ -28,26 +28,25 @@ JournalController.$inject = [
*
* @module bhima/controllers/JournalController
*/
function JournalController(Transactions, Journal, Sorting, Grouping, Pagination, Filtering, Columns, Config, Notify) {
function JournalController(Journal, Sorting, Grouping, Filtering, Columns, Config, Notify) {
var vm = this;

// Journal utilities
var sorting, grouping, pagination, filtering, columnConfig;
var sorting, grouping, filtering, columnConfig;

/** @const the cache alias for this controller */
var cacheKey = 'Journal';

// gridOptions is bound to the UI Grid and used to configure many of the
// options, it is also used by the grid to expose the API
vm.gridOptions = {};

// Initialise each of the journal utilites, providing them access to the journal
// Initialise each of the journal utilities, providing them access to the journal
// configuration options
sorting = new Sorting(vm.gridOptions);
filtering = new Filtering(vm.gridOptions);
grouping = new Grouping(vm.gridOptions);
pagination = new Pagination(vm.gridOptions, Transactions.list.data);
columnConfig = new Columns(vm.gridOptions, 'JournalColumns');

console.log('Grouping:', grouping);

sorting = new Sorting(vm.gridOptions);
filtering = new Filtering(vm.gridOptions, cacheKey);
grouping = new Grouping(vm.gridOptions);
columnConfig = new Columns(vm.gridOptions, cacheKey);

// Populate the grid with posting journal data
Journal.read()
Expand All @@ -62,7 +61,6 @@ function JournalController(Transactions, Journal, Sorting, Grouping, Pagination,
* providing them access to the journal
* configuration options :
* sorting = new Sorting(vm.gridOptions);
* pagination = new Pagination(vm.gridOptions, Transactions.list.data);
* grouping = new Grouping(vm.gridOptions);
* filtering = new Filtering(vm.gridOptions);
*
Expand Down

0 comments on commit fc883df

Please sign in to comment.