refactor client.balance
gitea/salix/1964-client_balance This commit looks good
Details
gitea/salix/1964-client_balance This commit looks good
Details
This commit is contained in:
parent
8a38ca7055
commit
68f437071f
|
@ -1,4 +1,5 @@
|
|||
module.exports = {
|
||||
const crudModel = {
|
||||
_data: [1, 2, 3],
|
||||
data: [],
|
||||
filter: {},
|
||||
order: {},
|
||||
|
@ -31,7 +32,28 @@ module.exports = {
|
|||
}
|
||||
};
|
||||
},
|
||||
refresh: () => {},
|
||||
addFilter: () => {},
|
||||
applyFilter: () => {},
|
||||
refresh: () => {
|
||||
return {
|
||||
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;
|
||||
|
|
|
@ -11,10 +11,15 @@ module.exports = Self => {
|
|||
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
arg: 'params',
|
||||
type: 'Object',
|
||||
description: 'clientFk',
|
||||
http: {source: 'query'}
|
||||
arg: 'clientFk',
|
||||
type: 'Number',
|
||||
description: 'The client id',
|
||||
required: true,
|
||||
}, {
|
||||
arg: 'companyFk',
|
||||
type: 'Number',
|
||||
description: 'The company id',
|
||||
required: true,
|
||||
}
|
||||
],
|
||||
returns: {
|
||||
|
@ -27,7 +32,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.filter = async(filter, params) => {
|
||||
Self.filter = async(filter, clientId, companyId) => {
|
||||
let stmt = new ParameterizedSQL(
|
||||
`SELECT * FROM (
|
||||
SELECT
|
||||
|
@ -72,10 +77,10 @@ module.exports = Self => {
|
|||
ORDER BY payed DESC, created DESC
|
||||
) t
|
||||
ORDER BY payed DESC, created DESC`, [
|
||||
params.clientFk,
|
||||
params.companyFk,
|
||||
params.clientFk,
|
||||
params.companyFk,
|
||||
clientId,
|
||||
companyId,
|
||||
clientId,
|
||||
companyId,
|
||||
]
|
||||
);
|
||||
|
||||
|
|
|
@ -2,12 +2,10 @@ const app = require('vn-loopback/server/server');
|
|||
|
||||
describe('receipt filter()', () => {
|
||||
it('should return the receipts', async() => {
|
||||
let filter = {limit: 20};
|
||||
let params = {
|
||||
clientFk: 101,
|
||||
companyFk: 442
|
||||
};
|
||||
let result = await app.models.Receipt.filter(filter, params);
|
||||
const filter = {limit: 20};
|
||||
const clientId = 101;
|
||||
const companyId = 442;
|
||||
let result = await app.models.Receipt.filter(filter, clientId, companyId);
|
||||
|
||||
expect(result.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
<vn-crud-model
|
||||
vn-id="model"
|
||||
url="receipts/filter"
|
||||
params="$ctrl.params"
|
||||
limit="20"
|
||||
data="$ctrl.balances"
|
||||
auto-load="true">
|
||||
data="$ctrl.balances">
|
||||
</vn-crud-model>
|
||||
<vn-crud-model
|
||||
vn-id="riskModel"
|
||||
|
@ -22,7 +20,6 @@
|
|||
vn-id="company"
|
||||
class="dense"
|
||||
ng-model="$ctrl.companyFk"
|
||||
on-change="$ctrl.setOrder(value)"
|
||||
url="Companies"
|
||||
show-field="code"
|
||||
value-field="id"
|
||||
|
|
|
@ -16,74 +16,54 @@ class Controller {
|
|||
scope: {
|
||||
fields: ['code'],
|
||||
},
|
||||
},
|
||||
where: {
|
||||
clientFk: $stateParams.id,
|
||||
companyFk: this.companyFk
|
||||
},
|
||||
};
|
||||
this.params = {
|
||||
params: {
|
||||
clientFk: this.$stateParams.id,
|
||||
companyFk: this.companyFk,
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
get companyFk() {
|
||||
if (!this._companyFk)
|
||||
return this.vnConfig.companyFk;
|
||||
this.companyFk = this.vnConfig.companyFk;
|
||||
|
||||
return this._companyFk;
|
||||
}
|
||||
|
||||
set companyFk(id) {
|
||||
this._companyFk = id;
|
||||
}
|
||||
setOrder(value) {
|
||||
this.params.params.companyFk = value;
|
||||
this.filter.where.companyFk = value;
|
||||
this.refresh();
|
||||
|
||||
if (id) this.getData();
|
||||
}
|
||||
|
||||
refresh() {
|
||||
this.$.model.refresh();
|
||||
this.$.riskModel.refresh();
|
||||
getData() {
|
||||
console.log('1st log');
|
||||
return this.$.model.applyFilter(null, {
|
||||
clientFk: this.$stateParams.id,
|
||||
companyFk: this.companyFk
|
||||
}).then(() => this.$.riskModel.applyFilter({
|
||||
where: {
|
||||
clientFk: this.$stateParams.id,
|
||||
companyFk: this.companyFk
|
||||
}
|
||||
})).then(() => this.getBalances());
|
||||
}
|
||||
|
||||
set balances(value) {
|
||||
this._balances = value;
|
||||
|
||||
if (!value) return;
|
||||
const params = {filter: this.filter};
|
||||
this.$http.get(`ClientRisks`, {params}).then(response => {
|
||||
if (response.data) {
|
||||
this.clientRisks = response.data;
|
||||
|
||||
this.getBalances();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
get balances() {
|
||||
return this._balances;
|
||||
}
|
||||
|
||||
getCurrentBalance() {
|
||||
const selectedCompany = this.$.company.selection;
|
||||
const currentBalance = this.clientRisks.find(balance => {
|
||||
return balance.companyFk === selectedCompany.id;
|
||||
const clientRisks = this.$.riskModel.data;
|
||||
const selectedCompany = this.companyFk;
|
||||
const currentBalance = clientRisks.find(balance => {
|
||||
return balance.companyFk === selectedCompany;
|
||||
});
|
||||
|
||||
return currentBalance.amount;
|
||||
}
|
||||
|
||||
getBalances() {
|
||||
this.balances.forEach((balance, index) => {
|
||||
const balances = this.$.model.data;
|
||||
balances.forEach((balance, index) => {
|
||||
if (index === 0)
|
||||
balance.balance = this.getCurrentBalance();
|
||||
if (index > 0) {
|
||||
let previousBalance = this.balances[index - 1];
|
||||
let previousBalance = balances[index - 1];
|
||||
|
||||
balance.balance = previousBalance.balance - (previousBalance.debit - previousBalance.credit);
|
||||
}
|
||||
|
@ -93,9 +73,7 @@ class Controller {
|
|||
|
||||
openCreateDialog() {
|
||||
this.$.balanceCreateDialog.companyFk = this.companyFk;
|
||||
this.$.balanceCreateDialog.onResponse = () => {
|
||||
this.refresh();
|
||||
};
|
||||
this.$.balanceCreateDialog.onResponse = () => this.getData();
|
||||
this.$.balanceCreateDialog.show();
|
||||
}
|
||||
|
||||
|
|
|
@ -3,41 +3,31 @@ import './index';
|
|||
describe('Client', () => {
|
||||
describe('Component vnClientBalanceIndex', () => {
|
||||
let $componentController;
|
||||
let $scope;
|
||||
let $httpBackend;
|
||||
let $httpParamSerializer;
|
||||
let controller;
|
||||
|
||||
beforeEach(ngModule('client'));
|
||||
|
||||
beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
|
||||
beforeEach(angular.mock.inject((_$componentController_, $rootScope) => {
|
||||
$componentController = _$componentController_;
|
||||
$httpBackend = _$httpBackend_;
|
||||
$httpParamSerializer = _$httpParamSerializer_;
|
||||
$scope = $rootScope.$new();
|
||||
let $scope = $rootScope.$new();
|
||||
controller = $componentController('vnClientBalanceIndex', {$scope});
|
||||
controller._companyFk = 442;
|
||||
controller.$stateParams.id = 101;
|
||||
|
||||
controller.$.model = {applyFilter: () => {}};
|
||||
controller.$.riskModel = {applyFilter: () => {}};
|
||||
}));
|
||||
|
||||
describe('balances() setter', () => {
|
||||
it('should calculate the balance for each line from the oldest date to the newest', () => {
|
||||
controller.getCurrentBalance = jasmine.createSpy(controller, 'getCurrentBalance').and.returnValue(1000);
|
||||
let balances = [
|
||||
{credit: -100, debit: 0},
|
||||
{credit: 0, debit: 300},
|
||||
{credit: 100, debit: 0},
|
||||
{credit: 0, debit: -300}
|
||||
];
|
||||
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);
|
||||
describe('getData()', () => {
|
||||
it('should apply the filters on he models and get the client balance', () => {
|
||||
spyOn(controller, 'getBalances');
|
||||
spyOn(controller.$.model, 'applyFilter').and.returnValue(Promise.resolve());
|
||||
spyOn(controller.$.riskModel, 'applyFilter').and.returnValue(Promise.resolve());
|
||||
controller.getData().then(() => {
|
||||
expect(controller.$.model.applyFilter).toHaveBeenCalledWith(null, {'clientFk': 101, 'companyFk': 442});
|
||||
expect(controller.$.riskModel.applyFilter).toHaveBeenCalledWith({'where': {'clientFk': 101, 'companyFk': 442}});
|
||||
expect(controller.getBalances).toHaveBeenCalledWith();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue