import ngModule from '../../module'; import Section from 'salix/components/section'; class Controller extends Section { constructor($element, $) { super($element, $); this.filter = { include: { relation: 'company', scope: { fields: ['code'], }, } }; } get companyId() { if (!this._companyId) this.companyId = this.vnConfig.companyFk; return this._companyId; } set companyId(value) { this._companyId = value; if (value) this.getData(); } get balances() { return this._balances; } set balances(value) { this._balances = value; const riskModel = this.$.riskModel; if (value && riskModel.data) this.getBalances(); } get isAdministrative() { return this.aclService.hasAny(['administrative']); } getData() { return this.$.model.applyFilter(null, { clientId: this.$params.id, companyId: this.companyId }).then(() => this.$.riskModel.applyFilter({ where: { clientFk: this.$params.id, companyFk: this.companyId } })).then(() => this.getBalances()); } getCurrentBalance() { const clientRisks = this.$.riskModel.data; const selectedCompany = this.companyId; const currentBalance = clientRisks.find(balance => { return balance.companyFk === selectedCompany; }); return currentBalance && currentBalance.amount; } getBalances() { const balances = this.$.model.data; balances.forEach((balance, index) => { if (index === 0) balance.balance = this.getCurrentBalance(); if (index > 0) { let previousBalance = balances[index - 1]; balance.balance = previousBalance.balance - (previousBalance.debit - previousBalance.credit); } }); } showInvoiceOutDescriptor(event, balance) { if (!balance.isInvoice) return; if (event.defaultPrevented) return; this.$.invoiceOutDescriptor.show(event.target, balance.id); } 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!'))); } } Controller.$inject = ['$element', '$scope']; ngModule.vnComponent('vnClientBalanceIndex', { template: require('./index.html'), controller: Controller, });