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-02 11:35:38 +00:00
|
|
|
constructor($element, $, $transclude) {
|
|
|
|
super($element, $, $transclude);
|
|
|
|
this.invoice = {
|
2022-12-27 11:21:04 +00:00
|
|
|
maxShipped: new Date(),
|
|
|
|
companyFk: this.vnConfig.companyFk
|
2021-08-02 11:35:38 +00:00
|
|
|
};
|
2021-08-10 11:57:03 +00:00
|
|
|
}
|
2021-08-02 11:35:38 +00:00
|
|
|
|
2021-08-10 11:57:03 +00:00
|
|
|
$onInit() {
|
2021-08-02 11:35:38 +00:00
|
|
|
this.getMinClientId();
|
|
|
|
this.getMaxClientId();
|
|
|
|
}
|
|
|
|
|
|
|
|
getMinClientId() {
|
2021-08-10 11:57:03 +00:00
|
|
|
this.getClientId('min')
|
|
|
|
.then(res => this.invoice.fromClientId = res.data.id);
|
2021-08-02 11:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getMaxClientId() {
|
2021-08-10 11:57:03 +00:00
|
|
|
this.getClientId('max')
|
|
|
|
.then(res => this.invoice.toClientId = res.data.id);
|
2021-08-02 11:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getClientId(func) {
|
|
|
|
const order = func == 'min' ? 'ASC' : 'DESC';
|
|
|
|
const params = {
|
|
|
|
filter: {
|
|
|
|
order: 'id ' + order,
|
|
|
|
limit: 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return this.$http.get('Clients/findOne', {params});
|
|
|
|
}
|
|
|
|
|
2023-01-24 08:45:52 +00:00
|
|
|
getPercentage() {
|
|
|
|
this.percentage = (this.currentClient * 100) / this.clients.length;
|
|
|
|
}
|
|
|
|
|
2022-10-17 07:49:47 +00:00
|
|
|
restartValues() {
|
|
|
|
this.$.invoiceButton.disabled = false;
|
2023-01-11 10:57:52 +00:00
|
|
|
this.packageInvoicing = false;
|
2022-10-17 07:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 11:59:57 +00:00
|
|
|
invoiceOut(invoice, clientsAndAddresses) {
|
2022-10-20 05:58:38 +00:00
|
|
|
const [clientAndAddress] = clientsAndAddresses;
|
2022-10-19 11:59:57 +00:00
|
|
|
if (!clientAndAddress) return;
|
2023-01-25 07:15:50 +00:00
|
|
|
|
2023-01-26 12:12:53 +00:00
|
|
|
this.currentClientId = clientAndAddress.clientId;
|
2023-01-24 08:45:52 +00:00
|
|
|
this.currentClient = ++this.currentClient;
|
|
|
|
this.getPercentage();
|
2023-01-25 07:15:50 +00:00
|
|
|
|
2022-10-19 11:59:57 +00:00
|
|
|
const params = {
|
|
|
|
clientId: clientAndAddress.clientId,
|
|
|
|
addressId: clientAndAddress.addressId,
|
|
|
|
invoiceDate: invoice.invoiceDate,
|
|
|
|
maxShipped: invoice.maxShipped,
|
|
|
|
companyFk: invoice.companyFk,
|
|
|
|
minShipped: invoice.minShipped,
|
2023-01-17 09:46:16 +00:00
|
|
|
printerFk: this.invoice.printerFk,
|
2022-10-19 11:59:57 +00:00
|
|
|
|
|
|
|
};
|
2022-12-29 07:15:04 +00:00
|
|
|
this.$http.get(`Addresses/${clientAndAddress.addressId}`)
|
|
|
|
.then(res => {
|
|
|
|
this.address = res.data;
|
|
|
|
return this.$http.post(`InvoiceOuts/invoiceClient`, params)
|
2023-01-26 12:12:53 +00:00
|
|
|
.catch(res => {
|
|
|
|
this.$.data.unshift({
|
|
|
|
id: clientAndAddress.clientId,
|
|
|
|
address: this.address,
|
|
|
|
status: 'error',
|
|
|
|
error: res.data.error.message
|
|
|
|
});
|
2022-12-29 07:15:04 +00:00
|
|
|
}).finally(() => {
|
|
|
|
clientsAndAddresses.shift();
|
|
|
|
return this.invoiceOut(invoice, clientsAndAddresses);
|
|
|
|
});
|
2022-10-19 11:59:57 +00:00
|
|
|
});
|
2022-10-18 12:46:40 +00:00
|
|
|
}
|
|
|
|
|
2022-12-27 11:21:04 +00:00
|
|
|
makeInvoice() {
|
2021-08-02 11:35:38 +00:00
|
|
|
try {
|
2021-08-10 09:48:29 +00:00
|
|
|
if (!this.invoice.invoiceDate || !this.invoice.maxShipped)
|
|
|
|
throw new Error('Invoice date and the max date should be filled');
|
2021-08-02 11:35:38 +00:00
|
|
|
|
2023-01-11 10:57:52 +00:00
|
|
|
if (!this.invoice.companyFk)
|
|
|
|
throw new Error('Choose a valid company');
|
|
|
|
|
2023-01-18 06:26:57 +00:00
|
|
|
if (!this.invoice.printerFk)
|
|
|
|
throw new Error('Choose a valid printer');
|
|
|
|
|
2022-10-14 07:30:09 +00:00
|
|
|
this.$.invoiceButton.disabled = true;
|
2022-12-27 12:50:10 +00:00
|
|
|
this.$.data = [];
|
2022-10-21 08:06:34 +00:00
|
|
|
this.packageInvoicing = true;
|
2022-10-26 08:36:41 +00:00
|
|
|
|
2022-12-27 11:21:04 +00:00
|
|
|
this.$http.post(`InvoiceOuts/clientsToInvoice`, this.invoice)
|
2022-10-19 11:59:57 +00:00
|
|
|
.then(res => {
|
2022-10-21 08:06:34 +00:00
|
|
|
this.packageInvoicing = false;
|
2022-10-13 13:01:15 +00:00
|
|
|
const invoice = res.data.invoice;
|
2023-01-26 12:12:53 +00:00
|
|
|
this.currentClient = 0;
|
|
|
|
|
2022-10-17 11:02:03 +00:00
|
|
|
const clientsAndAddresses = res.data.clientsAndAddresses;
|
2022-12-27 11:21:04 +00:00
|
|
|
if (!clientsAndAddresses.length) throw new UserError(`There aren't clients to invoice`);
|
2023-01-24 08:45:52 +00:00
|
|
|
|
|
|
|
this.clients = [];
|
|
|
|
for (const clientAndAddress of clientsAndAddresses)
|
|
|
|
this.clients.push(clientAndAddress.clientId);
|
|
|
|
|
2022-10-19 11:59:57 +00:00
|
|
|
return this.invoiceOut(invoice, clientsAndAddresses);
|
2022-10-10 10:40:10 +00:00
|
|
|
})
|
2021-08-10 09:48:29 +00:00
|
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
|
2022-10-17 07:49:47 +00:00
|
|
|
.finally(() => this.restartValues());
|
2021-08-02 11:35:38 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.vnApp.showError(this.$t(e.message));
|
2022-10-17 11:02:03 +00:00
|
|
|
this.restartValues();
|
2021-08-02 11:35:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller.$inject = ['$element', '$scope', '$transclude'];
|
|
|
|
|
|
|
|
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
|
|
|
});
|