refactor client.balance
gitea/salix/1964-client_balance This commit looks good Details

This commit is contained in:
Bernat Exposito Domenech 2020-01-02 14:44:51 +01:00
parent 8a38ca7055
commit 68f437071f
6 changed files with 84 additions and 94 deletions

View File

@ -1,4 +1,5 @@
module.exports = { const crudModel = {
_data: [1, 2, 3],
data: [], data: [],
filter: {}, filter: {},
order: {}, order: {},
@ -31,7 +32,28 @@ module.exports = {
} }
}; };
}, },
refresh: () => {}, refresh: () => {
addFilter: () => {}, return {
applyFilter: () => {}, then: callback => {
return callback({data: {id: 1234}});
}
};
},
addFilter: () => {
return {
then: callback => {
return callback({data: {id: 1234}});
}
};
},
applyFilter: () => {
crudModel.data = crudModel._data;
return {
then: callback => {
return callback({data: {id: 1234}});
}
};
},
}; };
module.exports = crudModel;

View File

@ -11,10 +11,15 @@ module.exports = Self => {
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
http: {source: 'query'} http: {source: 'query'}
}, { }, {
arg: 'params', arg: 'clientFk',
type: 'Object', type: 'Number',
description: 'clientFk', description: 'The client id',
http: {source: 'query'} required: true,
}, {
arg: 'companyFk',
type: 'Number',
description: 'The company id',
required: true,
} }
], ],
returns: { returns: {
@ -27,7 +32,7 @@ module.exports = Self => {
} }
}); });
Self.filter = async(filter, params) => { Self.filter = async(filter, clientId, companyId) => {
let stmt = new ParameterizedSQL( let stmt = new ParameterizedSQL(
`SELECT * FROM ( `SELECT * FROM (
SELECT SELECT
@ -72,10 +77,10 @@ module.exports = Self => {
ORDER BY payed DESC, created DESC ORDER BY payed DESC, created DESC
) t ) t
ORDER BY payed DESC, created DESC`, [ ORDER BY payed DESC, created DESC`, [
params.clientFk, clientId,
params.companyFk, companyId,
params.clientFk, clientId,
params.companyFk, companyId,
] ]
); );

View File

@ -2,12 +2,10 @@ const app = require('vn-loopback/server/server');
describe('receipt filter()', () => { describe('receipt filter()', () => {
it('should return the receipts', async() => { it('should return the receipts', async() => {
let filter = {limit: 20}; const filter = {limit: 20};
let params = { const clientId = 101;
clientFk: 101, const companyId = 442;
companyFk: 442 let result = await app.models.Receipt.filter(filter, clientId, companyId);
};
let result = await app.models.Receipt.filter(filter, params);
expect(result.length).toBeGreaterThan(0); expect(result.length).toBeGreaterThan(0);
}); });

View File

@ -1,10 +1,8 @@
<vn-crud-model <vn-crud-model
vn-id="model" vn-id="model"
url="receipts/filter" url="receipts/filter"
params="$ctrl.params"
limit="20" limit="20"
data="$ctrl.balances" data="$ctrl.balances">
auto-load="true">
</vn-crud-model> </vn-crud-model>
<vn-crud-model <vn-crud-model
vn-id="riskModel" vn-id="riskModel"
@ -22,7 +20,6 @@
vn-id="company" vn-id="company"
class="dense" class="dense"
ng-model="$ctrl.companyFk" ng-model="$ctrl.companyFk"
on-change="$ctrl.setOrder(value)"
url="Companies" url="Companies"
show-field="code" show-field="code"
value-field="id" value-field="id"

View File

@ -16,74 +16,54 @@ class Controller {
scope: { scope: {
fields: ['code'], fields: ['code'],
}, },
}, }
where: {
clientFk: $stateParams.id,
companyFk: this.companyFk
},
};
this.params = {
params: {
clientFk: this.$stateParams.id,
companyFk: this.companyFk,
},
}; };
} }
get companyFk() { get companyFk() {
if (!this._companyFk) if (!this._companyFk)
return this.vnConfig.companyFk; this.companyFk = this.vnConfig.companyFk;
return this._companyFk; return this._companyFk;
} }
set companyFk(id) { set companyFk(id) {
this._companyFk = id; this._companyFk = id;
}
setOrder(value) { if (id) this.getData();
this.params.params.companyFk = value;
this.filter.where.companyFk = value;
this.refresh();
} }
refresh() { getData() {
this.$.model.refresh(); console.log('1st log');
this.$.riskModel.refresh(); return this.$.model.applyFilter(null, {
} clientFk: this.$stateParams.id,
companyFk: this.companyFk
set balances(value) { }).then(() => this.$.riskModel.applyFilter({
this._balances = value; where: {
clientFk: this.$stateParams.id,
if (!value) return; companyFk: this.companyFk
const params = {filter: this.filter};
this.$http.get(`ClientRisks`, {params}).then(response => {
if (response.data) {
this.clientRisks = response.data;
this.getBalances();
} }
}); })).then(() => this.getBalances());
} }
get balances() {
return this._balances;
}
getCurrentBalance() { getCurrentBalance() {
const selectedCompany = this.$.company.selection; const clientRisks = this.$.riskModel.data;
const currentBalance = this.clientRisks.find(balance => { const selectedCompany = this.companyFk;
return balance.companyFk === selectedCompany.id; const currentBalance = clientRisks.find(balance => {
return balance.companyFk === selectedCompany;
}); });
return currentBalance.amount; return currentBalance.amount;
} }
getBalances() { getBalances() {
this.balances.forEach((balance, index) => { const balances = this.$.model.data;
balances.forEach((balance, index) => {
if (index === 0) if (index === 0)
balance.balance = this.getCurrentBalance(); balance.balance = this.getCurrentBalance();
if (index > 0) { if (index > 0) {
let previousBalance = this.balances[index - 1]; let previousBalance = balances[index - 1];
balance.balance = previousBalance.balance - (previousBalance.debit - previousBalance.credit); balance.balance = previousBalance.balance - (previousBalance.debit - previousBalance.credit);
} }
@ -93,9 +73,7 @@ class Controller {
openCreateDialog() { openCreateDialog() {
this.$.balanceCreateDialog.companyFk = this.companyFk; this.$.balanceCreateDialog.companyFk = this.companyFk;
this.$.balanceCreateDialog.onResponse = () => { this.$.balanceCreateDialog.onResponse = () => this.getData();
this.refresh();
};
this.$.balanceCreateDialog.show(); this.$.balanceCreateDialog.show();
} }

View File

@ -3,41 +3,31 @@ import './index';
describe('Client', () => { describe('Client', () => {
describe('Component vnClientBalanceIndex', () => { describe('Component vnClientBalanceIndex', () => {
let $componentController; let $componentController;
let $scope;
let $httpBackend;
let $httpParamSerializer;
let controller; let controller;
beforeEach(ngModule('client')); beforeEach(ngModule('client'));
beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { beforeEach(angular.mock.inject((_$componentController_, $rootScope) => {
$componentController = _$componentController_; $componentController = _$componentController_;
$httpBackend = _$httpBackend_; let $scope = $rootScope.$new();
$httpParamSerializer = _$httpParamSerializer_;
$scope = $rootScope.$new();
controller = $componentController('vnClientBalanceIndex', {$scope}); controller = $componentController('vnClientBalanceIndex', {$scope});
controller._companyFk = 442;
controller.$stateParams.id = 101;
controller.$.model = {applyFilter: () => {}};
controller.$.riskModel = {applyFilter: () => {}};
})); }));
describe('balances() setter', () => { describe('getData()', () => {
it('should calculate the balance for each line from the oldest date to the newest', () => { it('should apply the filters on he models and get the client balance', () => {
controller.getCurrentBalance = jasmine.createSpy(controller, 'getCurrentBalance').and.returnValue(1000); spyOn(controller, 'getBalances');
let balances = [ spyOn(controller.$.model, 'applyFilter').and.returnValue(Promise.resolve());
{credit: -100, debit: 0}, spyOn(controller.$.riskModel, 'applyFilter').and.returnValue(Promise.resolve());
{credit: 0, debit: 300}, controller.getData().then(() => {
{credit: 100, debit: 0}, expect(controller.$.model.applyFilter).toHaveBeenCalledWith(null, {'clientFk': 101, 'companyFk': 442});
{credit: 0, debit: -300} expect(controller.$.riskModel.applyFilter).toHaveBeenCalledWith({'where': {'clientFk': 101, 'companyFk': 442}});
]; expect(controller.getBalances).toHaveBeenCalledWith();
const params = {filter: controller.filter}; });
let serializedParams = $httpParamSerializer(params);
$httpBackend.when('GET', `ClientRisks?${serializedParams}`).respond(balances);
$httpBackend.expect('GET', `ClientRisks?${serializedParams}`);
controller.balances = balances;
$httpBackend.flush();
expect(controller.balances[0].balance).toEqual(1000);
expect(controller.balances[1].balance).toEqual(900);
expect(controller.balances[2].balance).toEqual(600);
expect(controller.balances[3].balance).toEqual(700);
}); });
}); });
}); });