141 lines
4.7 KiB
JavaScript
141 lines
4.7 KiB
JavaScript
import ngModule from '../module';
|
|
import Section from 'salix/components/section';
|
|
import UserError from 'core/lib/user-error';
|
|
import './style.scss';
|
|
|
|
class Controller extends Section {
|
|
constructor($element, $, $transclude) {
|
|
super($element, $, $transclude);
|
|
this.invoice = {
|
|
maxShipped: new Date(),
|
|
companyFk: this.vnConfig.companyFk
|
|
};
|
|
}
|
|
|
|
$onInit() {
|
|
this.getMinClientId();
|
|
this.getMaxClientId();
|
|
}
|
|
|
|
getMinClientId() {
|
|
this.getClientId('min')
|
|
.then(res => this.invoice.fromClientId = res.data.id);
|
|
}
|
|
|
|
getMaxClientId() {
|
|
this.getClientId('max')
|
|
.then(res => this.invoice.toClientId = res.data.id);
|
|
}
|
|
|
|
getClientId(func) {
|
|
const order = func == 'min' ? 'ASC' : 'DESC';
|
|
const params = {
|
|
filter: {
|
|
order: 'id ' + order,
|
|
limit: 1
|
|
}
|
|
};
|
|
return this.$http.get('Clients/findOne', {params});
|
|
}
|
|
|
|
getPercentage() {
|
|
this.percentage = (this.currentClient * 100) / this.clients.length;
|
|
}
|
|
|
|
restartValues() {
|
|
this.$.invoiceButton.disabled = false;
|
|
this.packageInvoicing = false;
|
|
}
|
|
|
|
invoiceOut(invoice, clientsAndAddresses) {
|
|
const [clientAndAddress] = clientsAndAddresses;
|
|
if (!clientAndAddress) return;
|
|
this.currentClientId = clientAndAddress.clientId;
|
|
this.currentClient = ++this.currentClient;
|
|
this.getPercentage();
|
|
const params = {
|
|
clientId: clientAndAddress.clientId,
|
|
addressId: clientAndAddress.addressId,
|
|
invoiceDate: invoice.invoiceDate,
|
|
maxShipped: invoice.maxShipped,
|
|
companyFk: invoice.companyFk,
|
|
minShipped: invoice.minShipped,
|
|
printerFk: this.invoice.printerFk,
|
|
|
|
};
|
|
|
|
this.$http.get(`Addresses/${clientAndAddress.addressId}`)
|
|
.then(res => {
|
|
this.address = res.data;
|
|
|
|
this.$.data.unshift({
|
|
id: clientAndAddress.clientId,
|
|
address: this.address,
|
|
status: 'waiting'
|
|
});
|
|
|
|
const index = this.$.data.findIndex(
|
|
client => client.id == clientAndAddress.clientId && client.address.id == clientAndAddress.addressId
|
|
);
|
|
|
|
return this.$http.post(`InvoiceOuts/invoiceClient`, params)
|
|
.then(() => {
|
|
this.$.data[index].status = 'ok';
|
|
this.$.data.shift();
|
|
}).catch(res => {
|
|
this.$.data[index].status = 'error';
|
|
this.$.data[index].error = res.data.error.message;
|
|
}).finally(() => {
|
|
clientsAndAddresses.shift();
|
|
return this.invoiceOut(invoice, clientsAndAddresses);
|
|
});
|
|
});
|
|
}
|
|
|
|
makeInvoice() {
|
|
try {
|
|
if (!this.invoice.invoiceDate || !this.invoice.maxShipped)
|
|
throw new Error('Invoice date and the max date should be filled');
|
|
|
|
if (!this.invoice.companyFk)
|
|
throw new Error('Choose a valid company');
|
|
|
|
if (!this.invoice.printerFk)
|
|
throw new Error('Choose a valid printer');
|
|
|
|
this.$.invoiceButton.disabled = true;
|
|
this.$.data = [];
|
|
this.packageInvoicing = true;
|
|
|
|
this.$http.post(`InvoiceOuts/clientsToInvoice`, this.invoice)
|
|
.then(res => {
|
|
this.packageInvoicing = false;
|
|
const invoice = res.data.invoice;
|
|
const clientsAndAddresses = res.data.clientsAndAddresses;
|
|
if (!clientsAndAddresses.length) throw new UserError(`There aren't clients to invoice`);
|
|
|
|
this.clients = [];
|
|
for (const clientAndAddress of clientsAndAddresses)
|
|
this.clients.push(clientAndAddress.clientId);
|
|
this.currentClient = 0;
|
|
this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId;
|
|
|
|
return this.invoiceOut(invoice, clientsAndAddresses);
|
|
})
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
|
|
.finally(() => this.restartValues());
|
|
} catch (e) {
|
|
this.vnApp.showError(this.$t(e.message));
|
|
this.restartValues();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
Controller.$inject = ['$element', '$scope', '$transclude'];
|
|
|
|
ngModule.vnComponent('vnInvoiceOutGlobalInvoicing', {
|
|
template: require('./index.html'),
|
|
controller: Controller
|
|
});
|