92 lines
2.6 KiB
JavaScript
92 lines
2.6 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.isInvoicing = false;
|
|
this.invoice = {
|
|
maxShipped: new Date()
|
|
};
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
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)
|
|
throw new Error('Choose a valid clients range');
|
|
|
|
this.isInvoicing = true;
|
|
return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice)
|
|
.then(res => {
|
|
this.lastClientId = res.data[res.data.length - 1];
|
|
console.log(this.lastClientId);
|
|
})
|
|
// .then(() => {
|
|
// for (let clientId of res.data) {
|
|
// const params = {clientId: clientId};
|
|
// this.$http.post(`InvoiceOuts/globalInvoicing`, params);
|
|
// }
|
|
// })
|
|
// .then(() => super.responseHandler(response))
|
|
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')))
|
|
.finally(() => this.isInvoicing = false);
|
|
} catch (e) {
|
|
this.vnApp.showError(this.$t(e.message));
|
|
this.isInvoicing = false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
Controller.$inject = ['$element', '$scope', '$transclude'];
|
|
|
|
ngModule.vnComponent('vnInvoiceOutGlobalInvoicing', {
|
|
slotTemplate: require('./index.html'),
|
|
controller: Controller,
|
|
bindings: {
|
|
companyFk: '<?'
|
|
}
|
|
});
|