131 lines
4.3 KiB
JavaScript
131 lines
4.3 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
|
|
};
|
|
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});
|
|
}
|
|
|
|
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 index = this.$.data.findIndex(element => element.id == clientAndAddress.clientId);
|
|
return this.$http.post(`InvoiceOuts/invoiceClient`, params)
|
|
.then(() => {
|
|
this.$.data[index].status = 'ok';
|
|
}).catch(() => {
|
|
this.$.data[index].status = 'error';
|
|
}).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.fromClientId || !this.invoice.toClientId)
|
|
throw new Error('Choose a valid clients range');
|
|
|
|
this.$.invoiceButton.disabled = true;
|
|
this.packageInvoicing = true;
|
|
|
|
this.$http.post(`InvoiceOuts/clientsToInvoice`, this.invoice)
|
|
.then(res => {
|
|
this.packageInvoicing = false;
|
|
const invoice = res.data.invoice;
|
|
|
|
const clientsIds = [];
|
|
for (const clientAndAddress of res.data.clientsAndAddresses)
|
|
clientsIds.push(clientAndAddress.clientId);
|
|
const dataArr = new Set(clientsIds);
|
|
const clientsIdsNoRepeat = [...dataArr];
|
|
const clients = clientsIdsNoRepeat.map(clientId => {
|
|
return {
|
|
id: clientId,
|
|
status: 'waiting'
|
|
};
|
|
});
|
|
this.$.data = clients;
|
|
|
|
const clientsAndAddresses = res.data.clientsAndAddresses;
|
|
if (!clientsAndAddresses.length) throw new UserError(`There aren't clients to invoice`);
|
|
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;
|
|
}
|
|
}
|
|
|
|
clean() {
|
|
this.$.data = this.$.data.filter(client => client.status == 'error');
|
|
}
|
|
}
|
|
|
|
Controller.$inject = ['$element', '$scope', '$transclude'];
|
|
|
|
ngModule.vnComponent('vnInvoiceOutGlobalInvoicing', {
|
|
template: require('./index.html'),
|
|
controller: Controller
|
|
});
|