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

125 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-08-02 11:35:38 +00:00
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()
};
2022-10-17 08:19:07 +00:00
this.clientsNumber = 'allClients';
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});
}
get companyFk() {
return this.invoice.companyFk;
}
set companyFk(value) {
this.invoice.companyFk = value;
}
2022-10-17 07:49:47 +00:00
restartValues() {
this.lastClientId = null;
this.$.invoiceButton.disabled = false;
}
invoiceOut(invoice, clientsAndAddresses) {
2022-10-20 05:58:38 +00:00
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,
};
this.canceler = this.$q.defer();
const options = {
timeout: this.canceler.promise
};
2022-10-20 05:23:57 +00:00
return this.$http.post(`InvoiceOuts/invoiceClient`, params, options)
.then(() => {
2022-10-20 05:58:38 +00:00
clientsAndAddresses.shift();
return this.invoiceOut(invoice, clientsAndAddresses);
});
}
responseHandler(response) {
2021-08-02 11:35:38 +00:00
try {
if (response !== 'accept')
return super.responseHandler(response);
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
2022-10-17 11:02:03 +00:00
if (!this.invoice.fromClientId || !this.invoice.toClientId)
2021-08-10 09:48:29 +00:00
throw new Error('Choose a valid clients range');
2021-08-02 11:35:38 +00:00
2022-10-13 13:01:15 +00:00
this.on('close', () => {
if (this.canceler) this.canceler.resolve();
this.vnApp.showSuccess(this.$t('Data saved!'));
});
2022-10-14 07:30:09 +00:00
this.$.invoiceButton.disabled = true;
2022-10-17 08:19:07 +00:00
if (this.clientsNumber == 'allClients') this.getMaxClientId();
this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice)
.then(res => {
2022-10-13 13:01:15 +00:00
const invoice = res.data.invoice;
2022-10-17 11:02:03 +00:00
const clientsAndAddresses = res.data.clientsAndAddresses;
2022-10-13 13:01:15 +00:00
if (!clientsAndAddresses.length) return super.responseHandler(response);
this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId;
return this.invoiceOut(invoice, clientsAndAddresses);
})
.then(() => super.responseHandler(response))
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', {
slotTemplate: require('./index.html'),
controller: Controller,
bindings: {
companyFk: '<?'
}
});