From ebb4d36fdaaef90d2b9e74c9624a2315d907ad67 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 7 Jan 2025 15:04:45 +0100 Subject: [PATCH 1/2] fix: refs #8264 parallelism --- src/stores/invoiceOutGlobal.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/stores/invoiceOutGlobal.js b/src/stores/invoiceOutGlobal.js index cc8d86ea8..42f0c9db2 100644 --- a/src/stores/invoiceOutGlobal.js +++ b/src/stores/invoiceOutGlobal.js @@ -10,7 +10,6 @@ const { notify } = useNotify(); export const useInvoiceOutGlobalStore = defineStore({ id: 'invoiceOutGlobal', - state: () => ({ initialDataLoading: true, formInitialData: { @@ -33,6 +32,7 @@ export const useInvoiceOutGlobalStore = defineStore({ nRequests: 0, nPdfs: 0, totalPdfs: 0, + formData: null, }), actions: { async init() { @@ -94,7 +94,6 @@ export const useInvoiceOutGlobalStore = defineStore({ async makeInvoice(formData, clientsToInvoice) { this.invoicing = true; - const promises = []; try { this.printer = formData.printer; const params = { @@ -120,10 +119,9 @@ export const useInvoiceOutGlobalStore = defineStore({ throw new Error("There aren't addresses to invoice"); } this.status = 'invoicing'; - for (let index = 0; index < this.parallelism; index++) { - promises.push(this.invoiceClient(formData, index)); - } - await Promise.all(promises); + this.formData = formData; + this.addressIndex = 0; + await this.invoiceClient(this.addressIndex); } catch (err) { this.handleError(err); } @@ -182,8 +180,11 @@ export const useInvoiceOutGlobalStore = defineStore({ } }, - async invoiceClient(formData, index) { + async invoiceClient(index = this.addressIndex++) { + if (this.nRequests >= this.parallelism) return; + const address = this.addresses[index]; + if (!address || !this.status || this.status == 'stopping') { this.status = 'stopping'; this.invoicing = false; @@ -193,17 +194,17 @@ export const useInvoiceOutGlobalStore = defineStore({ const params = { clientId: address.clientId, addressId: address.id, - invoiceDate: new Date(formData.invoiceDate), - maxShipped: new Date(formData.maxShipped), - companyFk: formData.companyFk, - serialType: formData.serialType, + invoiceDate: new Date(this.formData.invoiceDate), + maxShipped: new Date(this.formData.maxShipped), + companyFk: this.formData.companyFk, + serialType: this.formData.serialType, }; this.invoicing = true; const { data } = await axios.post('InvoiceOuts/invoiceClient', params); - if (data) await this.makePdfAndNotify(data, address); + if (data) this.makePdfAndNotify(data, address); this.isInvoicing = false; } catch (err) { if (err?.response?.status >= 400 && err?.response?.status < 500) { @@ -218,9 +219,7 @@ export const useInvoiceOutGlobalStore = defineStore({ throw new Error('Critical invoicing error, process stopped'); } } finally { - this.addressIndex++; - if (this.status != 'stopping') - await this.invoiceClient(formData, this.addressIndex); + await this.invoiceClient(); } }, @@ -231,9 +230,11 @@ export const useInvoiceOutGlobalStore = defineStore({ const params = { printerFk: this.printer }; await axios.post(`InvoiceOuts/${invoiceId}/makePdfAndNotify`, params); this.nPdfs++; - this.nRequests--; } catch (err) { this.invoiceClientError(client, err.response?.data?.error?.message, true); + } finally { + this.nRequests--; + await this.invoiceClient(); // Comprobar que no haya ninguna factura pendiente } }, From 0c30a8244001d4970a7fb9ee13d10f19e08533e0 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 9 Jan 2025 07:55:57 +0100 Subject: [PATCH 2/2] fix(InvoiceOutGlobal): refs #8264 fix invoicing --- src/stores/invoiceOutGlobal.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/stores/invoiceOutGlobal.js b/src/stores/invoiceOutGlobal.js index 42f0c9db2..d8649753f 100644 --- a/src/stores/invoiceOutGlobal.js +++ b/src/stores/invoiceOutGlobal.js @@ -93,7 +93,6 @@ export const useInvoiceOutGlobalStore = defineStore({ }, async makeInvoice(formData, clientsToInvoice) { - this.invoicing = true; try { this.printer = formData.printer; const params = { @@ -118,10 +117,12 @@ export const useInvoiceOutGlobalStore = defineStore({ ); throw new Error("There aren't addresses to invoice"); } + this.invoicing = false; this.status = 'invoicing'; this.formData = formData; this.addressIndex = 0; - await this.invoiceClient(this.addressIndex); + this.errors = []; + await this.invoiceClient(); } catch (err) { this.handleError(err); } @@ -180,10 +181,9 @@ export const useInvoiceOutGlobalStore = defineStore({ } }, - async invoiceClient(index = this.addressIndex++) { - if (this.nRequests >= this.parallelism) return; - - const address = this.addresses[index]; + async invoiceClient() { + if (this.invoicing || this.nRequests >= this.parallelism) return; + const address = this.addresses[this.addressIndex]; if (!address || !this.status || this.status == 'stopping') { this.status = 'stopping'; @@ -191,6 +191,7 @@ export const useInvoiceOutGlobalStore = defineStore({ return; } try { + this.invoicing = true; const params = { clientId: address.clientId, addressId: address.id, @@ -200,26 +201,22 @@ export const useInvoiceOutGlobalStore = defineStore({ serialType: this.formData.serialType, }; - this.invoicing = true; - const { data } = await axios.post('InvoiceOuts/invoiceClient', params); - if (data) this.makePdfAndNotify(data, address); - this.isInvoicing = false; } catch (err) { if (err?.response?.status >= 400 && err?.response?.status < 500) { this.invoiceClientError(address, err.response?.data?.error?.message); return; } else { - this.invoicing = false; notify( 'invoiceOut.globalInvoices.errors.criticalInvoiceError', 'negative' ); - throw new Error('Critical invoicing error, process stopped'); } } finally { - await this.invoiceClient(); + this.invoicing = false; + this.addressIndex++; + this.invoiceClient(); } }, @@ -234,7 +231,7 @@ export const useInvoiceOutGlobalStore = defineStore({ this.invoiceClientError(client, err.response?.data?.error?.message, true); } finally { this.nRequests--; - await this.invoiceClient(); // Comprobar que no haya ninguna factura pendiente + this.invoiceClient(); } },