salix/modules/invoiceOut/front/global-invoicing/index.js

135 lines
4.2 KiB
JavaScript
Raw Normal View History

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, {
maxShipped: new Date(date.getFullYear(), date.getMonth(), 0),
clientsToInvoice: 'all',
});
2021-08-02 11:35:38 +00:00
2023-02-23 08:25:23 +00:00
this.$http.get('UserConfigs/getUserConfig')
.then(res => {
this.companyFk = res.data.companyFk;
const params = {
year: this.maxShipped.getFullYear(),
companyFk: this.companyFk
};
return this.$http.get('InvoiceOuts/getInvoiceDate', {params});
})
.then(res => {
this.minInvoicingDate = new Date(res.data.issued);
this.invoiceDate = this.minInvoicingDate;
});
2021-08-02 11:35:38 +00:00
}
2023-02-23 08:25:23 +00:00
stopInvoicing() {
this.status = 'stopping';
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');
if (this.invoiceDate.getTime() < this.minInvoicingDate.getTime())
throw new UserError('Exists an invoice with a previous date');
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`);
this.addressIndex = 0;
return this.invoiceOut();
})
.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-02-23 08:25:23 +00:00
invoiceOut() {
if (this.addressIndex == this.addresses.length || this.status == 'stopping') {
this.invoicing = false;
this.status = 'done';
return;
}
2023-02-23 08:25:23 +00:00
this.status = 'invoicing';
const address = this.addresses[this.addressIndex];
this.currentAddress = address;
const params = {
2023-02-23 08:25:23 +00:00
clientId: address.clientId,
addressId: address.id,
invoiceDate: this.invoiceDate,
maxShipped: this.maxShipped,
companyFk: this.companyFk,
printerFk: this.printerFk,
};
2023-02-23 08:25:23 +00:00
this.$http.post(`InvoiceOuts/invoiceClient`, params)
.catch(res => {
this.errors.unshift({
address,
message: res.data.error.message
});
})
.finally(() => {
this.addressIndex++;
this.invoiceOut();
});
}
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
});