forked from verdnatura/salix-front
Eliminar invoiceOut.service y delegar integraciones al store
This commit is contained in:
parent
a52e75f51c
commit
40c0edfeb3
|
@ -1,45 +0,0 @@
|
||||||
import axios from 'axios';
|
|
||||||
|
|
||||||
const request = async (method, url, params = {}) => {
|
|
||||||
try {
|
|
||||||
let response;
|
|
||||||
|
|
||||||
if (method === 'GET') {
|
|
||||||
response = await axios.get(url, { params });
|
|
||||||
} else if (method === 'POST') {
|
|
||||||
response = await axios.post(url, params);
|
|
||||||
}
|
|
||||||
return response.data;
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`Error with ${method} request to ${url}`, err);
|
|
||||||
return err.response;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const invoiceOutService = {
|
|
||||||
getNegativeBasesCsv: async (params) => {
|
|
||||||
return await request('GET', 'InvoiceOuts/negativeBasesCsv', params);
|
|
||||||
},
|
|
||||||
|
|
||||||
getInvoiceDate: async (params) => {
|
|
||||||
return await request('GET', 'InvoiceOuts/getInvoiceDate', params);
|
|
||||||
},
|
|
||||||
|
|
||||||
getFindOne: async (params) => {
|
|
||||||
return await request('GET', 'InvoiceOutConfigs/findOne', params);
|
|
||||||
},
|
|
||||||
|
|
||||||
getClientsToInvoice: async (params) => {
|
|
||||||
return await request('POST', 'InvoiceOuts/clientsToInvoice', params);
|
|
||||||
},
|
|
||||||
|
|
||||||
invoiceClient: async (params) => {
|
|
||||||
return await request('POST', 'InvoiceOuts/invoiceClient', params);
|
|
||||||
},
|
|
||||||
|
|
||||||
makePdfAndNotify: async (invoiceId, params) => {
|
|
||||||
return await request('POST', `InvoiceOuts/${invoiceId}/makePdfAndNotify`, params);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default invoiceOutService;
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { useUserConfig } from 'src/composables/useUserConfig';
|
import { useUserConfig } from 'src/composables/useUserConfig';
|
||||||
import invoiceOutService from 'src/services/invoiceOut.service';
|
|
||||||
import useNotify from 'src/composables/useNotify.js';
|
import useNotify from 'src/composables/useNotify.js';
|
||||||
import { exportFile } from 'quasar';
|
import { exportFile } from 'quasar';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
const { notify } = useNotify();
|
const { notify } = useNotify();
|
||||||
|
|
||||||
|
@ -60,18 +60,29 @@ export const useInvoiceOutGlobalStore = defineStore({
|
||||||
},
|
},
|
||||||
|
|
||||||
async fetchInvoiceOutConfig(companyFk) {
|
async fetchInvoiceOutConfig(companyFk) {
|
||||||
this.formInitialData.companyFk = companyFk;
|
try {
|
||||||
const params = { companyFk: companyFk };
|
this.formInitialData.companyFk = companyFk;
|
||||||
const { issued } = await invoiceOutService.getInvoiceDate(params);
|
const params = { companyFk: companyFk };
|
||||||
const stringDate = issued.substring(0, 10);
|
|
||||||
this.minInvoicingDate = stringDate;
|
const { data } = await axios.get('InvoiceOuts/getInvoiceDate', {
|
||||||
this.formInitialData.invoiceDate = stringDate;
|
params,
|
||||||
|
});
|
||||||
|
|
||||||
|
const stringDate = data.issued.substring(0, 10);
|
||||||
|
this.minInvoicingDate = stringDate;
|
||||||
|
this.formInitialData.invoiceDate = stringDate;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching invoice out global initial data');
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async fetchParallelism() {
|
async fetchParallelism() {
|
||||||
const filter = { fields: ['parallelism'] };
|
const filter = { fields: ['parallelism'] };
|
||||||
const { parallelism } = await invoiceOutService.getFindOne(filter);
|
const { data } = await axios.get('InvoiceOutConfigs/findOne', {
|
||||||
this.parallelism = parallelism;
|
filter,
|
||||||
|
});
|
||||||
|
this.parallelism = data.parallelism;
|
||||||
},
|
},
|
||||||
|
|
||||||
async makeInvoice(formData, clientsToInvoice) {
|
async makeInvoice(formData, clientsToInvoice) {
|
||||||
|
@ -90,11 +101,12 @@ export const useInvoiceOutGlobalStore = defineStore({
|
||||||
|
|
||||||
if (clientsToInvoice == 'all') params.clientId = undefined;
|
if (clientsToInvoice == 'all') params.clientId = undefined;
|
||||||
|
|
||||||
const addressesResponse = await invoiceOutService.getClientsToInvoice(
|
const addressesResponse = await await axios.post(
|
||||||
|
'InvoiceOuts/clientsToInvoice',
|
||||||
params
|
params
|
||||||
);
|
);
|
||||||
|
|
||||||
this.addresses = addressesResponse;
|
this.addresses = addressesResponse.data;
|
||||||
|
|
||||||
if (!this.addresses || !this.addresses.length > 0) {
|
if (!this.addresses || !this.addresses.length > 0) {
|
||||||
notify(
|
notify(
|
||||||
|
@ -151,31 +163,45 @@ export const useInvoiceOutGlobalStore = defineStore({
|
||||||
},
|
},
|
||||||
|
|
||||||
async invoiceClient(address, formData) {
|
async invoiceClient(address, formData) {
|
||||||
if (this.nRequests === this.parallelism || this.isInvoicing) return;
|
try {
|
||||||
|
if (this.nRequests === this.parallelism || this.isInvoicing) return;
|
||||||
|
|
||||||
if (this.status === 'stopping') {
|
if (this.status === 'stopping') {
|
||||||
if (this.nRequests) return;
|
if (this.nRequests) return;
|
||||||
this.invoicing = false;
|
this.invoicing = false;
|
||||||
this.status = 'done';
|
this.status = 'done';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
clientId: address.clientId,
|
clientId: address.clientId,
|
||||||
addressId: address.id,
|
addressId: address.id,
|
||||||
invoiceDate: new Date(formData.invoiceDate),
|
invoiceDate: new Date(formData.invoiceDate),
|
||||||
maxShipped: new Date(formData.maxShipped),
|
maxShipped: new Date(formData.maxShipped),
|
||||||
companyFk: formData.companyFk,
|
companyFk: formData.companyFk,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.status = 'invoicing';
|
this.status = 'invoicing';
|
||||||
this.invoicing = true;
|
this.invoicing = true;
|
||||||
|
|
||||||
const invoiceResponse = await invoiceOutService.invoiceClient(params);
|
const invoiceResponse = await axios.post(
|
||||||
|
'InvoiceOuts/invoiceClient',
|
||||||
|
params
|
||||||
|
);
|
||||||
|
|
||||||
if (invoiceResponse.data.error) {
|
if (invoiceResponse.data) {
|
||||||
if (invoiceResponse.status >= 400 && invoiceResponse.status < 500) {
|
this.makePdfAndNotify(invoiceResponse.data, address);
|
||||||
this.invoiceClientError(address, invoiceResponse);
|
}
|
||||||
|
|
||||||
|
this.isInvoicing = false;
|
||||||
|
} catch (err) {
|
||||||
|
if (
|
||||||
|
err &&
|
||||||
|
err.response &&
|
||||||
|
err.response.status >= 400 &&
|
||||||
|
err.response.status < 500
|
||||||
|
) {
|
||||||
|
this.invoiceClientError(address, err.response);
|
||||||
this.addressIndex++;
|
this.addressIndex++;
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
@ -187,11 +213,6 @@ export const useInvoiceOutGlobalStore = defineStore({
|
||||||
);
|
);
|
||||||
throw new Error('Critical invoicing error, process stopped');
|
throw new Error('Critical invoicing error, process stopped');
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.isInvoicing = false;
|
|
||||||
if (invoiceResponse.data) {
|
|
||||||
this.makePdfAndNotify(invoiceResponse.data, address);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -200,7 +221,7 @@ export const useInvoiceOutGlobalStore = defineStore({
|
||||||
this.nRequests++;
|
this.nRequests++;
|
||||||
this.totalPdfs++;
|
this.totalPdfs++;
|
||||||
const params = { printerFk: this.printer };
|
const params = { printerFk: this.printer };
|
||||||
await invoiceOutService.makePdfAndNotify(invoiceId, params);
|
await axios.post(`InvoiceOuts/${invoiceId}/makePdfAndNotify`, params);
|
||||||
this.nPdfs++;
|
this.nPdfs++;
|
||||||
this.nRequests--;
|
this.nRequests--;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -222,11 +243,14 @@ export const useInvoiceOutGlobalStore = defineStore({
|
||||||
async getNegativeBasesCsv(from, to) {
|
async getNegativeBasesCsv(from, to) {
|
||||||
try {
|
try {
|
||||||
const params = { from: from, to: to };
|
const params = { from: from, to: to };
|
||||||
const CSVResponse = await invoiceOutService.getNegativeBasesCsv(params);
|
|
||||||
|
|
||||||
if (CSVResponse.data && CSVResponse.data.error) throw new Error();
|
const { data } = await axios.get('InvoiceOuts/negativeBasesCsv', {
|
||||||
|
params,
|
||||||
|
});
|
||||||
|
|
||||||
const status = exportFile('negativeBases.csv', CSVResponse, {
|
if (data.data && data.data.error) throw new Error();
|
||||||
|
|
||||||
|
const status = exportFile('negativeBases.csv', data, {
|
||||||
encoding: 'windows-1252',
|
encoding: 'windows-1252',
|
||||||
mimeType: 'text/csv;charset=windows-1252;',
|
mimeType: 'text/csv;charset=windows-1252;',
|
||||||
});
|
});
|
||||||
|
@ -236,6 +260,7 @@ export const useInvoiceOutGlobalStore = defineStore({
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
notify('invoiceOut.negativeBases.errors.downloadCsvFailed', 'negative');
|
notify('invoiceOut.negativeBases.errors.downloadCsvFailed', 'negative');
|
||||||
|
throw new Error();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue