2018-10-19 06:40:32 +00:00
|
|
|
import ngModule from '../../module';
|
2020-03-17 10:17:50 +00:00
|
|
|
import Section from 'salix/components/section';
|
2018-10-19 06:40:32 +00:00
|
|
|
|
2020-03-17 10:17:50 +00:00
|
|
|
class Controller extends Section {
|
|
|
|
constructor($element, $) {
|
|
|
|
super($element, $);
|
2018-10-19 06:40:32 +00:00
|
|
|
this.filter = {
|
|
|
|
include: {
|
2018-11-14 09:55:15 +00:00
|
|
|
relation: 'company',
|
2018-10-19 06:40:32 +00:00
|
|
|
scope: {
|
2018-11-14 09:55:15 +00:00
|
|
|
fields: ['code'],
|
|
|
|
},
|
2020-01-02 13:44:51 +00:00
|
|
|
}
|
2018-11-14 09:55:15 +00:00
|
|
|
};
|
|
|
|
}
|
2019-10-15 11:18:56 +00:00
|
|
|
|
2020-01-03 11:29:13 +00:00
|
|
|
get companyId() {
|
|
|
|
if (!this._companyId)
|
|
|
|
this.companyId = this.vnConfig.companyFk;
|
2019-10-15 11:18:56 +00:00
|
|
|
|
2020-01-03 11:29:13 +00:00
|
|
|
return this._companyId;
|
2019-10-15 11:18:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 11:29:13 +00:00
|
|
|
set companyId(value) {
|
|
|
|
this._companyId = value;
|
2019-02-05 15:46:02 +00:00
|
|
|
|
2020-01-03 11:29:13 +00:00
|
|
|
if (value) this.getData();
|
2018-11-14 09:55:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 07:38:19 +00:00
|
|
|
get balances() {
|
|
|
|
return this._balances;
|
|
|
|
}
|
|
|
|
|
|
|
|
set balances(value) {
|
|
|
|
this._balances = value;
|
|
|
|
|
|
|
|
const riskModel = this.$.riskModel;
|
|
|
|
if (value && riskModel.data)
|
|
|
|
this.getBalances();
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:44:51 +00:00
|
|
|
getData() {
|
|
|
|
return this.$.model.applyFilter(null, {
|
2020-03-17 10:17:50 +00:00
|
|
|
clientId: this.$params.id,
|
2020-01-03 11:29:13 +00:00
|
|
|
companyId: this.companyId
|
2020-01-02 13:44:51 +00:00
|
|
|
}).then(() => this.$.riskModel.applyFilter({
|
|
|
|
where: {
|
2020-03-17 10:17:50 +00:00
|
|
|
clientFk: this.$params.id,
|
2020-01-03 11:29:13 +00:00
|
|
|
companyFk: this.companyId
|
2018-11-14 09:55:15 +00:00
|
|
|
}
|
2020-01-02 13:44:51 +00:00
|
|
|
})).then(() => this.getBalances());
|
2018-11-14 09:55:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 11:20:34 +00:00
|
|
|
getCurrentBalance() {
|
2020-01-02 13:44:51 +00:00
|
|
|
const clientRisks = this.$.riskModel.data;
|
2020-01-03 11:29:13 +00:00
|
|
|
const selectedCompany = this.companyId;
|
2020-01-02 13:44:51 +00:00
|
|
|
const currentBalance = clientRisks.find(balance => {
|
|
|
|
return balance.companyFk === selectedCompany;
|
2019-04-05 11:20:34 +00:00
|
|
|
});
|
|
|
|
|
2020-08-27 09:16:36 +00:00
|
|
|
return currentBalance && currentBalance.amount;
|
2019-04-05 11:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getBalances() {
|
2020-01-02 13:44:51 +00:00
|
|
|
const balances = this.$.model.data;
|
|
|
|
balances.forEach((balance, index) => {
|
2019-04-05 11:20:34 +00:00
|
|
|
if (index === 0)
|
|
|
|
balance.balance = this.getCurrentBalance();
|
|
|
|
if (index > 0) {
|
2020-01-02 13:44:51 +00:00
|
|
|
let previousBalance = balances[index - 1];
|
2019-04-05 11:20:34 +00:00
|
|
|
balance.balance = previousBalance.balance - (previousBalance.debit - previousBalance.credit);
|
|
|
|
}
|
|
|
|
});
|
2018-10-19 06:40:32 +00:00
|
|
|
}
|
2020-06-08 05:52:23 +00:00
|
|
|
|
|
|
|
showInvoiceOutDescriptor(event, balance) {
|
|
|
|
if (!balance.isInvoice) return;
|
|
|
|
if (event.defaultPrevented) return;
|
|
|
|
|
|
|
|
this.$.invoiceOutDescriptor.show(event.target, balance.id);
|
|
|
|
}
|
2020-08-27 06:24:21 +00:00
|
|
|
|
|
|
|
changeDescription(balance) {
|
|
|
|
const params = {description: balance.description};
|
|
|
|
const endpoint = `Receipts/${balance.id}`;
|
|
|
|
this.$http.patch(endpoint, params)
|
|
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
|
|
|
|
}
|
2018-10-19 06:40:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 10:17:50 +00:00
|
|
|
Controller.$inject = ['$element', '$scope'];
|
2018-10-19 06:40:32 +00:00
|
|
|
|
2020-07-24 12:22:30 +00:00
|
|
|
ngModule.vnComponent('vnClientBalanceIndex', {
|
2018-10-19 06:40:32 +00:00
|
|
|
template: require('./index.html'),
|
2018-11-14 09:55:15 +00:00
|
|
|
controller: Controller,
|
2018-10-19 06:40:32 +00:00
|
|
|
});
|