import ngModule from '../../module'; import Dialog from 'core/components/dialog'; class Controller extends Dialog { constructor($element, $, $transclude, vnReport, vnEmail) { super($element, $, $transclude); this.vnReport = vnReport; this.vnEmail = vnEmail; this.receipt = {}; } set payed(value) { this.receipt.payed = value; } set amountPaid(value) { this.receipt.amountPaid = value; this.amountToReturn = this.deliveredAmount - value; } get amountPaid() { return this.receipt.amountPaid; } set clientFk(value) { if (!value) return; this.receipt.clientFk = value; const filter = { fields: ['email'], where: { id: value } }; this.getAmountPaid(); this.$http.get(`Clients/findOne`, {filter}) .then(res => { this.receipt.email = res.data.email; }); } get clientFk() { return this.receipt.clientFk; } get companyFk() { if (!this.receipt.companyFk) this.receipt.companyFk = this.vnConfig.companyFk; return this.receipt.companyFk; } set companyFk(value) { this.receipt.companyFk = value; } set description(value) { this.receipt.description = value; this.originalDescription = value; } get description() { return this.receipt.description; } get bankSelection() { return this._bankSelection; } set bankSelection(value) { this._bankSelection = value; if (value) { const accountingType = value.accountingType; this.receipt.description = []; this.viewReceipt = accountingType.code == 'cash'; if (accountingType.code == 'compensation') this.receipt.description = ''; else { if (accountingType.receiptDescription != null && accountingType.receiptDescription != '') this.receipt.description.push(accountingType.receiptDescription); if (this.originalDescription) this.receipt.description.push(this.originalDescription); this.receipt.description = this.receipt.description.join(', '); } this.maxAmount = accountingType && accountingType.maxAmount; this.receipt.payed = Date.vnNew(); if (accountingType.daysInFuture) this.receipt.payed.setDate(this.receipt.payed.getDate() + accountingType.daysInFuture); } } set deliveredAmount(value) { this._deliveredAmount = value; this.amountToReturn = value - this.receipt.amountPaid; } get amountToReturn() { return this._amountToReturn; } set amountToReturn(value) { if (isNaN(value)) return; value = value.toFixed(2); if (Number.isInteger(value)) value = parseInt(value); else value = parseFloat(value); this._amountToReturn = value; } get deliveredAmount() { return this._deliveredAmount; } get bankFk() { if (!this.receipt.bankFk) this.receipt.bankFk = this.vnConfig.bankFk; return this.receipt.bankFk; } set bankFk(value) { this.receipt.bankFk = value; } accountShortToStandard(value) { if (value) { this.receipt.compensationAccount = value.replace('.', '0'.repeat(11 - value.length)); const params = {bankAccount: this.receipt.compensationAccount}; this.$http.get(`Clients/getClientOrSupplierReference`, {params}) .then(res => { if (res.data.clientId) { this.receipt.description = this.$t('Client Compensation Reference', { clientId: res.data.clientId, clientName: res.data.clientName }); } else { this.receipt.description = this.$t('Supplier Compensation Reference', { supplierId: res.data.supplierId, supplierName: res.data.supplierName }); } }); } else this.receipt.description = ''; } getAmountPaid() { const filter = { where: { clientFk: this.$params.id ?? this.clientFk, companyFk: this.receipt.companyFk } }; this.$http.get(`ClientRisks`, {filter}).then(res => { this.receipt.amountPaid = (res.data.length && res.data[0].amount) || null; }); } responseHandler(response) { if (response !== 'accept') return super.responseHandler(response); const exceededAmount = this.receipt.amountPaid > this.maxAmount; const isCash = this.bankSelection.accountingType.code == 'cash'; if (isCash && exceededAmount) return this.vnApp.showError(this.$t('Amount exceeded', {maxAmount: this.maxAmount})); if (isCash && this.sendEmail && !this.receipt.email) return this.vnApp.showError(this.$t('There is no assigned email for this client')); let receiptId; return this.$http.post(`Clients/${this.clientFk}/createReceipt`, this.receipt) .then(res => { receiptId = res.data.id; super.responseHandler(response); }) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) .then(() => { if (!this.sendEmail || !isCash) return; const params = { recipient: this.receipt.email }; this.vnEmail.send(`Receipts/${receiptId}/receipt-email`, params); }) .then(() => { if (this.viewReceipt) this.vnReport.show(`Receipts/${receiptId}/receipt-pdf`); }); } bankSearchFunc($search) { return /^\d+$/.test($search) ? {id: $search} : {bank: {like: '%' + $search + '%'}}; } } Controller.$inject = ['$element', '$scope', '$transclude', 'vnReport', 'vnEmail']; ngModule.vnComponent('vnClientBalanceCreate', { slotTemplate: require('./index.html'), controller: Controller, bindings: { payed: '