import ngModule from '../../module'; import './style.scss'; class Controller { constructor($stateParams, $translate, $scope, vnToken, $http, vnConfig) { this.$http = $http; this.$ = $scope; this.$stateParams = $stateParams; this.$translate = $translate; this.accessToken = vnToken.token; this.vnConfig = vnConfig; 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(); } getData() { return this.$.model.applyFilter(null, { clientId: this.$stateParams.id, companyId: this.companyId }).then(() => this.$.riskModel.applyFilter({ where: { clientFk: this.$stateParams.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.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); } }); } openCreateDialog() { this.$.balanceCreateDialog.companyFk = this.companyId; this.$.balanceCreateDialog.onResponse = () => this.getData(); this.$.balanceCreateDialog.show(); } showWorkerDescriptor(event, workerFk) { if (event.defaultPrevented) return; event.preventDefault(); event.stopPropagation(); this.selectedWorker = workerFk; this.$.workerDescriptor.parent = event.target; this.$.workerDescriptor.show(); } showInvoiceOutDescriptor(event, balance) { if (!balance.isInvoice) return; if (event.defaultPrevented) return; event.preventDefault(); event.stopPropagation(); this.selectedInvoiceOut = balance.id; this.$.invoiceOutDescriptor.parent = event.target; this.$.invoiceOutDescriptor.show(); } } Controller.$inject = ['$stateParams', '$translate', '$scope', 'vnToken', '$http', 'vnConfig']; ngModule.component('vnClientBalanceIndex', { template: require('./index.html'), controller: Controller, });