diff --git a/src/services/invoiceOut.service.js b/src/services/invoiceOut.service.js deleted file mode 100644 index 17f710cb5..000000000 --- a/src/services/invoiceOut.service.js +++ /dev/null @@ -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; diff --git a/src/stores/invoiceOutGlobal.js b/src/stores/invoiceOutGlobal.js index d449a577d..7b763df63 100644 --- a/src/stores/invoiceOutGlobal.js +++ b/src/stores/invoiceOutGlobal.js @@ -1,8 +1,8 @@ import { defineStore } from 'pinia'; import { useUserConfig } from 'src/composables/useUserConfig'; -import invoiceOutService from 'src/services/invoiceOut.service'; import useNotify from 'src/composables/useNotify.js'; import { exportFile } from 'quasar'; +import axios from 'axios'; const { notify } = useNotify(); @@ -60,18 +60,29 @@ export const useInvoiceOutGlobalStore = defineStore({ }, async fetchInvoiceOutConfig(companyFk) { - this.formInitialData.companyFk = companyFk; - const params = { companyFk: companyFk }; - const { issued } = await invoiceOutService.getInvoiceDate(params); - const stringDate = issued.substring(0, 10); - this.minInvoicingDate = stringDate; - this.formInitialData.invoiceDate = stringDate; + try { + this.formInitialData.companyFk = companyFk; + const params = { companyFk: companyFk }; + + const { data } = await axios.get('InvoiceOuts/getInvoiceDate', { + 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() { const filter = { fields: ['parallelism'] }; - const { parallelism } = await invoiceOutService.getFindOne(filter); - this.parallelism = parallelism; + const { data } = await axios.get('InvoiceOutConfigs/findOne', { + filter, + }); + this.parallelism = data.parallelism; }, async makeInvoice(formData, clientsToInvoice) { @@ -90,11 +101,12 @@ export const useInvoiceOutGlobalStore = defineStore({ if (clientsToInvoice == 'all') params.clientId = undefined; - const addressesResponse = await invoiceOutService.getClientsToInvoice( + const addressesResponse = await await axios.post( + 'InvoiceOuts/clientsToInvoice', params ); - this.addresses = addressesResponse; + this.addresses = addressesResponse.data; if (!this.addresses || !this.addresses.length > 0) { notify( @@ -151,31 +163,45 @@ export const useInvoiceOutGlobalStore = defineStore({ }, 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.nRequests) return; - this.invoicing = false; - this.status = 'done'; - return; - } + if (this.status === 'stopping') { + if (this.nRequests) return; + this.invoicing = false; + this.status = 'done'; + return; + } - const params = { - clientId: address.clientId, - addressId: address.id, - invoiceDate: new Date(formData.invoiceDate), - maxShipped: new Date(formData.maxShipped), - companyFk: formData.companyFk, - }; + const params = { + clientId: address.clientId, + addressId: address.id, + invoiceDate: new Date(formData.invoiceDate), + maxShipped: new Date(formData.maxShipped), + companyFk: formData.companyFk, + }; - this.status = 'invoicing'; - this.invoicing = true; + this.status = 'invoicing'; + this.invoicing = true; - const invoiceResponse = await invoiceOutService.invoiceClient(params); + const invoiceResponse = await axios.post( + 'InvoiceOuts/invoiceClient', + params + ); - if (invoiceResponse.data.error) { - if (invoiceResponse.status >= 400 && invoiceResponse.status < 500) { - this.invoiceClientError(address, invoiceResponse); + if (invoiceResponse.data) { + this.makePdfAndNotify(invoiceResponse.data, address); + } + + this.isInvoicing = false; + } catch (err) { + if ( + err && + err.response && + err.response.status >= 400 && + err.response.status < 500 + ) { + this.invoiceClientError(address, err.response); this.addressIndex++; return; } else { @@ -187,11 +213,6 @@ export const useInvoiceOutGlobalStore = defineStore({ ); 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.totalPdfs++; const params = { printerFk: this.printer }; - await invoiceOutService.makePdfAndNotify(invoiceId, params); + await axios.post(`InvoiceOuts/${invoiceId}/makePdfAndNotify`, params); this.nPdfs++; this.nRequests--; } catch (err) { @@ -222,11 +243,14 @@ export const useInvoiceOutGlobalStore = defineStore({ async getNegativeBasesCsv(from, to) { try { 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', mimeType: 'text/csv;charset=windows-1252;', }); @@ -236,6 +260,7 @@ export const useInvoiceOutGlobalStore = defineStore({ } } catch (err) { notify('invoiceOut.negativeBases.errors.downloadCsvFailed', 'negative'); + throw new Error(); } },