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);
|
|
|
|
|
2021-08-10 09:48:29 +00:00
|
|
|
this.isInvoicing = false;
|
2021-08-02 11:35:38 +00:00
|
|
|
this.invoice = {
|
|
|
|
maxShipped: new Date()
|
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseHandler(response) {
|
|
|
|
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-06 13:19:55 +00:00
|
|
|
if (!this.invoice.fromClientId)
|
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-10 10:40:10 +00:00
|
|
|
return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice)
|
2022-10-11 08:04:13 +00:00
|
|
|
.then(async res => {
|
2022-10-11 10:24:06 +00:00
|
|
|
const clientAndAddressIds = res.data.clientAndAddressIds;
|
|
|
|
if (!clientAndAddressIds) return super.responseHandler(response);
|
|
|
|
this.lastClientId = clientAndAddressIds[clientAndAddressIds.length - 1][0];
|
2022-10-11 08:04:13 +00:00
|
|
|
this.isInvoicing = true;
|
2022-10-11 10:24:06 +00:00
|
|
|
for (let clientAndAddresId of clientAndAddressIds) {
|
|
|
|
this.currentClientId = clientAndAddresId[0];
|
|
|
|
res.data.clientId = clientAndAddresId[0];
|
|
|
|
res.data.addressId = clientAndAddresId[1];
|
|
|
|
await this.$http.post(`InvoiceOuts/globalInvoicing`, res.data);
|
2022-10-11 08:04:13 +00:00
|
|
|
}
|
2022-10-10 10:40:10 +00:00
|
|
|
})
|
2022-10-11 08:04:13 +00:00
|
|
|
.then(() => super.responseHandler(response))
|
2021-08-10 09:48:29 +00:00
|
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
|
|
|
|
.finally(() => this.isInvoicing = false);
|
2021-08-02 11:35:38 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.vnApp.showError(this.$t(e.message));
|
2021-08-10 09:48:29 +00:00
|
|
|
this.isInvoicing = false;
|
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: '<?'
|
|
|
|
}
|
|
|
|
});
|