2022-12-27 11:21:04 +00:00
|
|
|
import ngModule from '../module';
|
|
|
|
import Section from 'salix/components/section';
|
|
|
|
import UserError from 'core/lib/user-error';
|
2021-08-02 11:35:38 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2022-12-27 11:21:04 +00:00
|
|
|
class Controller extends Section {
|
2021-08-10 11:57:03 +00:00
|
|
|
$onInit() {
|
2023-02-23 08:25:23 +00:00
|
|
|
const date = Date.vnNew();
|
|
|
|
Object.assign(this, {
|
2023-06-22 09:10:56 +00:00
|
|
|
maxShipped: new Date(date.getFullYear(), date.getMonth(), 0),
|
|
|
|
clientsToInvoice: 'all',
|
2023-06-23 09:42:30 +00:00
|
|
|
companyFk: this.vnConfig.companyFk
|
2023-02-23 08:25:23 +00:00
|
|
|
});
|
2021-08-02 11:35:38 +00:00
|
|
|
|
2023-06-23 09:42:30 +00:00
|
|
|
const params = {companyFk: this.companyFk};
|
2023-06-22 09:10:56 +00:00
|
|
|
this.$http.get('InvoiceOuts/getInvoiceDate', {params})
|
|
|
|
.then(res => {
|
|
|
|
this.minInvoicingDate = res.data.issued ? new Date(res.data.issued) : null;
|
|
|
|
this.invoiceDate = this.minInvoicingDate;
|
|
|
|
});
|
2021-08-02 11:35:38 +00:00
|
|
|
|
2023-06-23 09:42:30 +00:00
|
|
|
const filter = {fields: ['parallelism']};
|
|
|
|
this.$http.get('InvoiceOutConfigs/findOne', {filter})
|
|
|
|
.then(res => {
|
|
|
|
this.parallelism = res.data.parallelism || 1;
|
|
|
|
});
|
2021-08-02 11:35:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 08:25:23 +00:00
|
|
|
makeInvoice() {
|
|
|
|
this.invoicing = true;
|
|
|
|
this.status = 'packageInvoicing';
|
|
|
|
this.errors = [];
|
|
|
|
this.addresses = null;
|
2021-08-02 11:35:38 +00:00
|
|
|
|
2023-02-23 08:25:23 +00:00
|
|
|
try {
|
|
|
|
if (this.clientsToInvoice == 'one' && !this.clientId)
|
|
|
|
throw new UserError('Choose a valid client');
|
|
|
|
if (!this.invoiceDate || !this.maxShipped)
|
|
|
|
throw new UserError('Invoice date and the max date should be filled');
|
|
|
|
if (this.invoiceDate < this.maxShipped)
|
|
|
|
throw new UserError('Invoice date can\'t be less than max date');
|
2023-06-15 07:50:46 +00:00
|
|
|
if (this.minInvoicingDate && this.invoiceDate.getTime() < this.minInvoicingDate.getTime())
|
2023-06-21 07:40:14 +00:00
|
|
|
throw new UserError('Exists an invoice with a future date');
|
2023-02-23 08:25:23 +00:00
|
|
|
if (!this.companyFk)
|
|
|
|
throw new UserError('Choose a valid company');
|
|
|
|
if (!this.printerFk)
|
|
|
|
throw new UserError('Choose a valid printer');
|
|
|
|
|
|
|
|
if (this.clientsToInvoice == 'all')
|
|
|
|
this.clientId = undefined;
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
invoiceDate: this.invoiceDate,
|
|
|
|
maxShipped: this.maxShipped,
|
|
|
|
clientId: this.clientId,
|
|
|
|
companyFk: this.companyFk
|
|
|
|
};
|
|
|
|
this.$http.post(`InvoiceOuts/clientsToInvoice`, params)
|
|
|
|
.then(res => {
|
|
|
|
this.addresses = res.data;
|
|
|
|
if (!this.addresses.length)
|
|
|
|
throw new UserError(`There aren't tickets to invoice`);
|
|
|
|
|
2023-06-23 09:42:30 +00:00
|
|
|
this.nRequests = 0;
|
|
|
|
this.nPdfs = 0;
|
|
|
|
this.totalPdfs = 0;
|
2023-02-23 08:25:23 +00:00
|
|
|
this.addressIndex = 0;
|
2023-06-23 09:42:30 +00:00
|
|
|
this.invoiceClient();
|
2023-02-23 08:25:23 +00:00
|
|
|
})
|
|
|
|
.catch(err => this.handleError(err));
|
|
|
|
} catch (err) {
|
|
|
|
this.handleError(err);
|
|
|
|
}
|
2023-01-24 08:45:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 08:25:23 +00:00
|
|
|
handleError(err) {
|
|
|
|
this.invoicing = false;
|
|
|
|
this.status = null;
|
|
|
|
throw err;
|
2022-10-17 07:49:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 09:42:30 +00:00
|
|
|
invoiceClient() {
|
|
|
|
if (this.nRequests == this.parallelism || this.isInvoicing) return;
|
|
|
|
|
|
|
|
if (this.addressIndex >= this.addresses.length || this.status == 'stopping') {
|
|
|
|
if (this.nRequests) return;
|
2023-02-23 08:25:23 +00:00
|
|
|
this.invoicing = false;
|
|
|
|
this.status = 'done';
|
2023-01-30 11:13:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-25 07:15:50 +00:00
|
|
|
|
2023-02-23 08:25:23 +00:00
|
|
|
this.status = 'invoicing';
|
|
|
|
const address = this.addresses[this.addressIndex];
|
|
|
|
this.currentAddress = address;
|
2023-06-23 09:42:30 +00:00
|
|
|
this.isInvoicing = true;
|
2023-01-25 07:15:50 +00:00
|
|
|
|
2022-10-19 11:59:57 +00:00
|
|
|
const params = {
|
2023-02-23 08:25:23 +00:00
|
|
|
clientId: address.clientId,
|
|
|
|
addressId: address.id,
|
|
|
|
invoiceDate: this.invoiceDate,
|
|
|
|
maxShipped: this.maxShipped,
|
2023-06-23 09:42:30 +00:00
|
|
|
companyFk: this.companyFk
|
2022-10-19 11:59:57 +00:00
|
|
|
};
|
2023-02-23 08:25:23 +00:00
|
|
|
|
|
|
|
this.$http.post(`InvoiceOuts/invoiceClient`, params)
|
2023-06-23 09:42:30 +00:00
|
|
|
.then(res => {
|
|
|
|
this.isInvoicing = false;
|
|
|
|
if (res.data)
|
|
|
|
this.makePdfAndNotify(res.data, address);
|
|
|
|
this.invoiceNext();
|
|
|
|
})
|
2023-02-23 08:25:23 +00:00
|
|
|
.catch(res => {
|
2023-06-23 09:42:30 +00:00
|
|
|
this.isInvoicing = false;
|
2023-04-04 13:00:28 +00:00
|
|
|
if (res.status >= 400 && res.status < 500) {
|
2023-06-23 09:42:30 +00:00
|
|
|
this.invoiceError(address, res);
|
2023-04-04 13:00:28 +00:00
|
|
|
this.invoiceNext();
|
|
|
|
} else {
|
|
|
|
this.invoicing = false;
|
|
|
|
this.status = 'done';
|
|
|
|
throw new UserError(`Critical invoicing error, proccess stopped`);
|
|
|
|
}
|
2023-06-22 09:10:56 +00:00
|
|
|
});
|
2023-04-04 13:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
invoiceNext() {
|
|
|
|
this.addressIndex++;
|
2023-06-23 09:42:30 +00:00
|
|
|
this.invoiceClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
makePdfAndNotify(invoiceId, address) {
|
|
|
|
this.nRequests++;
|
|
|
|
this.totalPdfs++;
|
|
|
|
const params = {printerFk: this.printerFk};
|
|
|
|
this.$http.post(`InvoiceOuts/${invoiceId}/makePdfAndNotify`, params)
|
|
|
|
.catch(res => {
|
|
|
|
this.invoiceError(address, res, true);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.nPdfs++;
|
|
|
|
this.nRequests--;
|
|
|
|
this.invoiceClient();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
invoiceError(address, res, isWarning) {
|
|
|
|
const message = res.data?.error?.message || res.message;
|
|
|
|
this.errors.unshift({address, message, isWarning});
|
2022-10-18 12:46:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 08:25:23 +00:00
|
|
|
get nAddresses() {
|
|
|
|
if (!this.addresses) return 0;
|
|
|
|
return this.addresses.length;
|
|
|
|
}
|
2023-01-24 08:45:52 +00:00
|
|
|
|
2023-02-23 08:25:23 +00:00
|
|
|
get addressNumber() {
|
|
|
|
return Math.min(this.addressIndex + 1, this.nAddresses);
|
|
|
|
}
|
2023-01-24 08:45:52 +00:00
|
|
|
|
2023-02-23 08:25:23 +00:00
|
|
|
get percentage() {
|
|
|
|
const len = this.nAddresses;
|
|
|
|
return Math.min(this.addressIndex, len) / len;
|
2021-08-02 11:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngModule.vnComponent('vnInvoiceOutGlobalInvoicing', {
|
2022-12-27 11:21:04 +00:00
|
|
|
template: require('./index.html'),
|
|
|
|
controller: Controller
|
2021-08-02 11:35:38 +00:00
|
|
|
});
|