salix/modules/client/front/balance/index/index.js

107 lines
2.9 KiB
JavaScript
Raw Normal View History

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 {
2022-10-28 12:49:22 +00:00
constructor($element, $, vnReport, vnEmail) {
2020-03-17 10:17:50 +00:00
super($element, $);
2022-10-28 12:49:22 +00:00
this.vnReport = vnReport;
this.vnEmail = vnEmail;
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
};
}
2020-01-03 11:29:13 +00:00
get companyId() {
if (!this._companyId)
this.companyId = this.vnConfig.companyFk;
2020-01-03 11:29:13 +00:00
return this._companyId;
}
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
}
get balances() {
return this._balances;
}
set balances(value) {
this._balances = value;
const riskModel = this.$.riskModel;
if (value && riskModel.data)
this.getBalances();
}
2020-08-27 09:23:39 +00:00
get isAdministrative() {
return this.aclService.hasAny(['administrative']);
}
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
}
2022-10-28 12:49:22 +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
});
2022-10-28 12:49:22 +00:00
2020-08-27 09:16:36 +00:00
return currentBalance && currentBalance.amount;
2019-04-05 11:20:34 +00:00
}
2022-10-28 12:49:22 +00:00
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)
2022-10-28 12:49:22 +00:00
balance.balance = this.getCurrentBalance();
2019-04-05 11:20:34 +00:00
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
}
2022-10-28 12:49:22 +00:00
2020-06-08 05:52:23 +00:00
showInvoiceOutDescriptor(event, balance) {
if (!balance.isInvoice) return;
if (event.defaultPrevented) return;
2022-10-28 12:49:22 +00:00
2020-06-08 05:52:23 +00:00
this.$.invoiceOutDescriptor.show(event.target, balance.id);
}
2022-10-28 12:49:22 +00:00
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)
2022-10-28 12:49:22 +00:00
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
}
sendEmail(balance) {
return this.vnEmail.send(`Receipts/${balance.id}/deb-balances-comp-email`);
2020-08-27 06:24:21 +00:00
}
2018-10-19 06:40:32 +00:00
}
2022-10-28 12:49:22 +00:00
Controller.$inject = ['$element', '$scope', 'vnReport', 'vnEmail'];
2018-10-19 06:40:32 +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
});