From 0d785695fe311fecf198891e7efe8be0a375211e Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 11 Oct 2022 12:24:06 +0200 Subject: [PATCH] feat: invoicing by (clientFk, addressFk) --- .../methods/invoiceOut/clientToInvoice.js | 18 ++++--- .../methods/invoiceOut/globalInvoicing.js | 54 ++++++++++++++++--- .../front/index/global-invoicing/index.js | 13 +++-- 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js index 16c999e73a..5466f22ace 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js +++ b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js @@ -53,7 +53,6 @@ module.exports = Self => { myOptions.transaction = tx; } - const clientToInvoideIds = []; let query; try { query = ` @@ -97,14 +96,21 @@ module.exports = Self => { if (!invoiceableClients.length) return; - const clientToInvoiceIds = invoiceableClients.map(invoiceableClient => invoiceableClient.id); - const dataArr = new Set(clientToInvoiceIds); - const clientNotRepeatedIds = [...dataArr]; + const clientAndAddress = invoiceableClients.map( + invoiceableClient => [invoiceableClient.id, invoiceableClient.addressFk] + ); if (tx) await tx.commit(); - console.log(invoiceableClients, clientToInvoiceIds, clientNotRepeatedIds); - return clientNotRepeatedIds; + return { + clientAndAddressIds: clientAndAddress, + invoiceDate: args.invoiceDate, + maxShipped: args.maxShipped, + fromClientId: args.fromClientId, + toClientId: args.toClientId, + companyFk: args.companyFk, + minShipped: minShipped + }; } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index bee79746a3..a90a986c38 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -7,6 +7,43 @@ module.exports = Self => { type: 'number', description: 'The client id to invoice', required: true + }, + { + arg: 'addressId', + type: 'number', + description: 'The address id to invoice', + required: true + }, + { + arg: 'invoiceDate', + type: 'date', + description: 'The invoice date' + }, + { + arg: 'maxShipped', + type: 'date', + description: 'The maximum shipped date' + }, + { + arg: 'fromClientId', + type: 'number', + description: 'The minimum client id' + }, + { + arg: 'toClientId', + type: 'number', + description: 'The maximum client id', + required: false + }, + { + arg: 'companyFk', + type: 'number', + description: 'The company id to invoice' + }, + { + arg: 'minShipped', + type: 'date', + description: 'The company id to invoice' }], returns: { type: 'object', @@ -18,7 +55,8 @@ module.exports = Self => { } }); - Self.globalInvoicing = async(ctx, clientId, options) => { + Self.globalInvoicing = async(ctx, options) => { + const args = ctx.args; const models = Self.app.models; const myOptions = {}; let tx; @@ -34,15 +72,16 @@ module.exports = Self => { const invoicesIds = []; const failedClients = []; try { - const client = await models.Client.findById(clientId, { - fields: ['id', 'hasToInvoiceByAddress', 'addressFk'] + // const addresses = await models.Address.find({where: {clientFk: args.clientId}}, myOptions); + const client = await models.Client.findById(args.clientId, { + fields: ['id', 'hasToInvoiceByAddress'] }, myOptions); try { if (client.hasToInvoiceByAddress) { await Self.rawSql('CALL ticketToInvoiceByAddress(?, ?, ?, ?)', [ - minShipped, + args.minShipped, args.maxShipped, - client.addressFk, + args.addressId, args.companyFk ], myOptions); } else { @@ -59,7 +98,7 @@ module.exports = Self => { // Validates ticket nagative base const hasAnyNegativeBase = await getNegativeBase(myOptions); if (hasAnyNegativeBase && isSpanishCompany) - return notifyFailures(ctx, failedClients, myOptions); // continue + return tx.rollback(); // continue query = `SELECT invoiceSerial(?, ?, ?) AS serial`; const [invoiceSerial] = await Self.rawSql(query, [ @@ -89,9 +128,8 @@ module.exports = Self => { id: client.id, stacktrace: e }); - return notifyFailures(ctx, failedClients, myOptions); // continue + await notifyFailures(ctx, failedClients, myOptions); // continue } - // } if (tx) await tx.commit(); } catch (e) { diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index fea1094ce5..30d965e9f2 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -59,12 +59,15 @@ class Controller extends Dialog { return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) .then(async res => { - this.lastClientId = res.data[res.data.length - 1]; + const clientAndAddressIds = res.data.clientAndAddressIds; + if (!clientAndAddressIds) return super.responseHandler(response); + this.lastClientId = clientAndAddressIds[clientAndAddressIds.length - 1][0]; this.isInvoicing = true; - for (let clientId of res.data) { - this.currentClientId = clientId; - const params = {clientId: clientId}; - await this.$http.post(`InvoiceOuts/globalInvoicing`, params); + for (let clientAndAddresId of clientAndAddressIds) { + this.currentClientId = clientAndAddresId[0]; + res.data.clientId = clientAndAddresId[0]; + res.data.addressId = clientAndAddresId[1]; + await this.$http.post(`InvoiceOuts/globalInvoicing`, res.data); } }) .then(() => super.responseHandler(response))