130 lines
4.0 KiB
JavaScript
130 lines
4.0 KiB
JavaScript
import ngModule from '../../module';
|
|
import Dialog from 'core/components/dialog';
|
|
import './style.scss';
|
|
|
|
class Controller extends Dialog {
|
|
constructor($element, $, $transclude) {
|
|
super($element, $, $transclude);
|
|
this.invoice = {
|
|
maxShipped: new Date()
|
|
};
|
|
this.clientsNumber = 'allClients';
|
|
}
|
|
|
|
$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});
|
|
}
|
|
|
|
get companyFk() {
|
|
return this.invoice.companyFk;
|
|
}
|
|
|
|
set companyFk(value) {
|
|
this.invoice.companyFk = value;
|
|
}
|
|
|
|
restartValues() {
|
|
this.lastClientId = null;
|
|
this.$.invoiceButton.disabled = false;
|
|
}
|
|
|
|
cancelRequest() {
|
|
this.canceler = this.$q.defer();
|
|
return {timeout: this.canceler.promise};
|
|
}
|
|
|
|
invoiceOut(invoice, clientsAndAddresses) {
|
|
const [clientAndAddress] = clientsAndAddresses;
|
|
if (!clientAndAddress) return;
|
|
this.currentClientId = clientAndAddress.clientId;
|
|
const params = {
|
|
clientId: clientAndAddress.clientId,
|
|
addressId: clientAndAddress.addressId,
|
|
invoiceDate: invoice.invoiceDate,
|
|
maxShipped: invoice.maxShipped,
|
|
companyFk: invoice.companyFk,
|
|
minShipped: invoice.minShipped,
|
|
|
|
};
|
|
|
|
const options = this.cancelRequest();
|
|
|
|
return this.$http.post(`InvoiceOuts/invoiceClient`, params, options)
|
|
.then(() => {
|
|
clientsAndAddresses.shift();
|
|
return this.invoiceOut(invoice, clientsAndAddresses);
|
|
});
|
|
}
|
|
|
|
responseHandler(response) {
|
|
try {
|
|
if (response !== 'accept')
|
|
return super.responseHandler(response);
|
|
|
|
if (!this.invoice.invoiceDate || !this.invoice.maxShipped)
|
|
throw new Error('Invoice date and the max date should be filled');
|
|
|
|
if (!this.invoice.fromClientId || !this.invoice.toClientId)
|
|
throw new Error('Choose a valid clients range');
|
|
|
|
this.on('close', () => {
|
|
if (this.canceler) this.canceler.resolve();
|
|
this.vnApp.showSuccess(this.$t('Data saved!'));
|
|
});
|
|
|
|
this.$.invoiceButton.disabled = true;
|
|
this.packageInvoicing = true;
|
|
const options = this.cancelRequest();
|
|
|
|
this.$http.post(`InvoiceOuts/clientsToInvoice`, this.invoice, options)
|
|
.then(res => {
|
|
this.packageInvoicing = false;
|
|
const invoice = res.data.invoice;
|
|
const clientsAndAddresses = res.data.clientsAndAddresses;
|
|
if (!clientsAndAddresses.length) return super.responseHandler(response);
|
|
this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId;
|
|
return this.invoiceOut(invoice, clientsAndAddresses);
|
|
})
|
|
.then(() => super.responseHandler(response))
|
|
.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', {
|
|
slotTemplate: require('./index.html'),
|
|
controller: Controller,
|
|
bindings: {
|
|
companyFk: '<?'
|
|
}
|
|
});
|