2018-10-24 12:10:48 +00:00
|
|
|
import ngModule from '../../module';
|
2020-03-30 15:30:03 +00:00
|
|
|
import Dialog from 'core/components/dialog';
|
|
|
|
|
|
|
|
class Controller extends Dialog {
|
2023-02-09 09:26:55 +00:00
|
|
|
constructor($element, $, $transclude, vnReport, vnEmail) {
|
2020-03-30 15:30:03 +00:00
|
|
|
super($element, $, $transclude);
|
2021-06-22 09:57:43 +00:00
|
|
|
this.vnReport = vnReport;
|
2023-02-09 09:26:55 +00:00
|
|
|
this.vnEmail = vnEmail;
|
2022-05-17 10:24:35 +00:00
|
|
|
this.receipt = {};
|
2019-02-05 15:46:02 +00:00
|
|
|
}
|
2018-10-24 12:10:48 +00:00
|
|
|
|
2019-02-05 15:46:02 +00:00
|
|
|
set payed(value) {
|
|
|
|
this.receipt.payed = value;
|
|
|
|
}
|
2018-10-24 12:10:48 +00:00
|
|
|
|
2019-02-05 15:46:02 +00:00
|
|
|
set amountPaid(value) {
|
|
|
|
this.receipt.amountPaid = value;
|
2021-01-21 09:25:25 +00:00
|
|
|
this.amountToReturn = this.deliveredAmount - value;
|
2019-02-05 15:46:02 +00:00
|
|
|
}
|
2018-10-24 12:10:48 +00:00
|
|
|
|
2019-04-25 05:57:26 +00:00
|
|
|
get amountPaid() {
|
|
|
|
return this.receipt.amountPaid;
|
|
|
|
}
|
|
|
|
|
|
|
|
set clientFk(value) {
|
2024-09-05 12:50:36 +00:00
|
|
|
if (!value) return;
|
2019-04-25 05:57:26 +00:00
|
|
|
this.receipt.clientFk = value;
|
2023-02-09 09:26:55 +00:00
|
|
|
|
|
|
|
const filter = {
|
|
|
|
fields: ['email'],
|
|
|
|
where: {
|
|
|
|
id: value
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.$http.get(`Clients/findOne`, {filter})
|
|
|
|
.then(res => {
|
|
|
|
this.receipt.email = res.data.email;
|
|
|
|
});
|
2019-04-25 05:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get clientFk() {
|
|
|
|
return this.receipt.clientFk;
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:57:43 +00:00
|
|
|
get companyFk() {
|
|
|
|
if (!this.receipt.companyFk)
|
|
|
|
this.receipt.companyFk = this.vnConfig.companyFk;
|
|
|
|
return this.receipt.companyFk;
|
|
|
|
}
|
|
|
|
|
2019-02-05 15:46:02 +00:00
|
|
|
set companyFk(value) {
|
|
|
|
this.receipt.companyFk = value;
|
2024-09-11 11:26:40 +00:00
|
|
|
this.getAmountPaid();
|
2018-10-24 12:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 05:57:26 +00:00
|
|
|
set description(value) {
|
|
|
|
this.receipt.description = value;
|
2021-10-26 08:20:35 +00:00
|
|
|
this.originalDescription = value;
|
2019-04-25 05:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get description() {
|
|
|
|
return this.receipt.description;
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:24:21 +00:00
|
|
|
get bankSelection() {
|
|
|
|
return this._bankSelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
set bankSelection(value) {
|
|
|
|
this._bankSelection = value;
|
|
|
|
|
|
|
|
if (value) {
|
|
|
|
const accountingType = value.accountingType;
|
2023-02-06 07:07:50 +00:00
|
|
|
|
|
|
|
this.receipt.description = [];
|
2023-01-27 07:31:14 +00:00
|
|
|
this.viewReceipt = accountingType.code == 'cash';
|
2023-02-14 11:41:36 +00:00
|
|
|
if (accountingType.code == 'compensation')
|
|
|
|
this.receipt.description = '';
|
|
|
|
else {
|
2023-02-17 12:33:34 +00:00
|
|
|
if (accountingType.receiptDescription != null && accountingType.receiptDescription != '')
|
|
|
|
this.receipt.description.push(accountingType.receiptDescription);
|
|
|
|
if (this.originalDescription)
|
|
|
|
this.receipt.description.push(this.originalDescription);
|
2023-02-23 14:52:47 +00:00
|
|
|
this.receipt.description = this.receipt.description.join(', ');
|
2023-02-14 11:41:36 +00:00
|
|
|
}
|
2021-11-08 07:59:42 +00:00
|
|
|
this.maxAmount = accountingType && accountingType.maxAmount;
|
2022-05-17 10:24:35 +00:00
|
|
|
|
2023-01-16 14:18:24 +00:00
|
|
|
this.receipt.payed = Date.vnNew();
|
2022-05-20 11:16:47 +00:00
|
|
|
if (accountingType.daysInFuture)
|
|
|
|
this.receipt.payed.setDate(this.receipt.payed.getDate() + accountingType.daysInFuture);
|
2020-08-27 06:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 07:33:08 +00:00
|
|
|
set deliveredAmount(value) {
|
|
|
|
this._deliveredAmount = value;
|
|
|
|
this.amountToReturn = value - this.receipt.amountPaid;
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:57:43 +00:00
|
|
|
get amountToReturn() {
|
|
|
|
return this._amountToReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
set amountToReturn(value) {
|
2021-12-22 08:46:18 +00:00
|
|
|
if (isNaN(value)) return;
|
2021-06-22 09:57:43 +00:00
|
|
|
|
|
|
|
value = value.toFixed(2);
|
|
|
|
|
|
|
|
if (Number.isInteger(value))
|
|
|
|
value = parseInt(value);
|
|
|
|
else value = parseFloat(value);
|
|
|
|
|
|
|
|
this._amountToReturn = value;
|
|
|
|
}
|
|
|
|
|
2020-12-16 07:33:08 +00:00
|
|
|
get deliveredAmount() {
|
|
|
|
return this._deliveredAmount;
|
|
|
|
}
|
|
|
|
|
2021-01-19 08:27:15 +00:00
|
|
|
get bankFk() {
|
|
|
|
if (!this.receipt.bankFk)
|
|
|
|
this.receipt.bankFk = this.vnConfig.bankFk;
|
|
|
|
|
|
|
|
return this.receipt.bankFk;
|
|
|
|
}
|
|
|
|
|
|
|
|
set bankFk(value) {
|
|
|
|
this.receipt.bankFk = value;
|
|
|
|
}
|
|
|
|
|
2020-12-16 07:33:08 +00:00
|
|
|
accountShortToStandard(value) {
|
2023-02-14 11:41:36 +00:00
|
|
|
if (value) {
|
|
|
|
this.receipt.compensationAccount = value.replace('.', '0'.repeat(11 - value.length));
|
2023-02-15 12:14:52 +00:00
|
|
|
const params = {bankAccount: this.receipt.compensationAccount};
|
|
|
|
this.$http.get(`Clients/getClientOrSupplierReference`, {params})
|
2023-02-14 11:41:36 +00:00
|
|
|
.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 = '';
|
2020-12-16 07:33:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 15:46:02 +00:00
|
|
|
getAmountPaid() {
|
2020-03-30 15:30:03 +00:00
|
|
|
const filter = {
|
2018-10-24 12:10:48 +00:00
|
|
|
where: {
|
2024-09-05 12:50:36 +00:00
|
|
|
clientFk: this.$params.id ?? this.clientFk,
|
2018-10-24 12:10:48 +00:00
|
|
|
companyFk: this.receipt.companyFk
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-30 15:30:03 +00:00
|
|
|
this.$http.get(`ClientRisks`, {filter}).then(res => {
|
2018-10-24 12:10:48 +00:00
|
|
|
this.receipt.amountPaid = (res.data.length && res.data[0].amount) || null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:30:03 +00:00
|
|
|
responseHandler(response) {
|
|
|
|
if (response !== 'accept')
|
|
|
|
return super.responseHandler(response);
|
2018-10-24 12:10:48 +00:00
|
|
|
|
2021-11-09 13:36:39 +00:00
|
|
|
const exceededAmount = this.receipt.amountPaid > this.maxAmount;
|
2023-02-09 09:26:55 +00:00
|
|
|
const isCash = this.bankSelection.accountingType.code == 'cash';
|
|
|
|
if (isCash && exceededAmount)
|
2021-11-08 07:59:42 +00:00
|
|
|
return this.vnApp.showError(this.$t('Amount exceeded', {maxAmount: this.maxAmount}));
|
|
|
|
|
2023-02-09 09:26:55 +00:00
|
|
|
if (isCash && this.sendEmail && !this.receipt.email)
|
|
|
|
return this.vnApp.showError(this.$t('There is no assigned email for this client'));
|
|
|
|
|
2021-06-22 11:57:32 +00:00
|
|
|
let receiptId;
|
2021-02-01 09:51:17 +00:00
|
|
|
return this.$http.post(`Clients/${this.clientFk}/createReceipt`, this.receipt)
|
2021-06-22 11:57:32 +00:00
|
|
|
.then(res => {
|
|
|
|
receiptId = res.data.id;
|
|
|
|
super.responseHandler(response);
|
|
|
|
})
|
2021-06-22 09:57:43 +00:00
|
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
|
2023-02-09 09:26:55 +00:00
|
|
|
.then(() => {
|
|
|
|
if (!this.sendEmail || !isCash) return;
|
|
|
|
const params = {
|
|
|
|
recipient: this.receipt.email
|
|
|
|
};
|
|
|
|
this.vnEmail.send(`Receipts/${receiptId}/receipt-email`, params);
|
|
|
|
})
|
2021-06-22 09:57:43 +00:00
|
|
|
.then(() => {
|
2022-10-24 07:49:51 +00:00
|
|
|
if (this.viewReceipt)
|
|
|
|
this.vnReport.show(`Receipts/${receiptId}/receipt-pdf`);
|
2021-06-22 09:57:43 +00:00
|
|
|
});
|
2018-10-24 12:10:48 +00:00
|
|
|
}
|
2021-09-03 12:00:47 +00:00
|
|
|
|
|
|
|
bankSearchFunc($search) {
|
|
|
|
return /^\d+$/.test($search)
|
|
|
|
? {id: $search}
|
|
|
|
: {bank: {like: '%' + $search + '%'}};
|
|
|
|
}
|
2018-10-24 12:10:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-09 09:26:55 +00:00
|
|
|
Controller.$inject = ['$element', '$scope', '$transclude', 'vnReport', 'vnEmail'];
|
2021-06-22 09:57:43 +00:00
|
|
|
|
2020-03-30 15:30:03 +00:00
|
|
|
ngModule.vnComponent('vnClientBalanceCreate', {
|
2020-04-25 09:50:04 +00:00
|
|
|
slotTemplate: require('./index.html'),
|
2019-02-05 15:46:02 +00:00
|
|
|
controller: Controller,
|
|
|
|
bindings: {
|
|
|
|
payed: '<?',
|
|
|
|
bankFk: '<?',
|
|
|
|
amountPaid: '<?',
|
2024-09-05 12:50:36 +00:00
|
|
|
clientFk: '<?',
|
2019-04-25 05:57:26 +00:00
|
|
|
companyFk: '<?',
|
|
|
|
description: '<?',
|
2019-02-05 15:46:02 +00:00
|
|
|
}
|
2018-10-24 12:10:48 +00:00
|
|
|
});
|