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

132 lines
3.4 KiB
JavaScript

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'],
},
},
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;
return this._companyFk;
}
set companyFk(id) {
this._companyFk = id;
}
setOrder(value) {
this.params.params.companyFk = value;
this.filter.where.companyFk = value;
this.refresh();
}
refresh() {
this.$.model.refresh();
this.$.riskModel.refresh();
}
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;
});
return currentBalance.amount;
}
getBalances() {
this.balances.forEach((balance, index) => {
if (index === 0)
balance.balance = this.getCurrentBalance();
if (index > 0) {
let previousBalance = this.balances[index - 1];
balance.balance = previousBalance.balance - (previousBalance.debit - previousBalance.credit);
}
});
}
openCreateDialog() {
this.$.balanceCreateDialog.companyFk = this.companyFk;
this.$.balanceCreateDialog.onResponse = () => {
this.refresh();
};
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,
});