From 31aafc071945c2d6e84760d92b1173d7495790a3 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 10 Aug 2022 09:11:38 +0200 Subject: [PATCH 01/40] feat: add inserts in printQueue --- db/dump/fixtures.sql | 4 ++ .../methods/invoiceOut/globalInvoicing.js | 41 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index e7232edd3..638f191f3 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2642,3 +2642,7 @@ INSERT INTO `vn`.`workerTimeControlConfig` (`id`, `dayBreak`, `dayBreakDriver`, INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`) VALUES (1, 9); + +INSERT INTO `vn`.`report` (`id`, `name`) + VALUES + (3, 'invoice'); \ No newline at end of file diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 7f2cbb442..14d6913cb 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -79,6 +79,13 @@ module.exports = Self => { const minShipped = new Date(); minShipped.setFullYear(minShipped.getFullYear() - 1); + if (args.fromClientId = args.toClientId) { + minShipped.setFullYear(2021); + minShipped.setMonth(1); + minShipped.setDate(1); + minShipped.setHours(0, 0, 0, 0); + } + // Packaging liquidation const vIsAllInvoiceable = false; const clientsWithPackaging = await getClientsWithPackaging(ctx, myOptions); @@ -138,8 +145,34 @@ module.exports = Self => { if (newInvoice.id) { await Self.rawSql('CALL invoiceOutBooking(?)', [newInvoice.id], myOptions); - query = `INSERT IGNORE INTO invoiceOut_queue(invoiceFk) VALUES(?)`; - await Self.rawSql(query, [newInvoice.id], myOptions); + // query = `INSERT IGNORE INTO invoiceOut_queue(invoiceFk) VALUES(?)`; + // await Self.rawSql(query, [newInvoice.id], myOptions); + + query = `SELECT id FROM vn.report WHERE name ='invoice'`; + const [reportFk] = await Self.rawSql(query, null, myOptions); + + query = `SELECT ref FROM vn.invoiceOut WHERE id = ?`; + const [invoiceRef] = await Self.rawSql(query, [newInvoice.id], myOptions); + + // Print invoice + const args = { + invoiceOutFk: newInvoice.id, + refFk: invoiceRef.ref, + hasToForcePdf: true, + email: '' + }; + + const userId = ctx.req.accessToken.userId; + + query = `INSERT INTO vn.printQueue (priorityFk, reportFk, workerFk, printerFk) VALUES (?, ?, ?, NULL)`; + await Self.rawSql(query, [2, reportFk.id, userId], myOptions); // La prioridad de donde se la saca? 'adPriorityBelowNormal' + + const [lastInsertedPrintQueue] = await Self.rawSql('SELECT LAST_INSERT_ID() AS id', null, myOptions); + + for (let key in args) { + query = `INSERT INTO vn.printQueueArgs (printQueueFk, name, value) VALUES (?, ?, ?)`; + await Self.rawSql(query, [lastInsertedPrintQueue.id, key, args[key]], myOptions); + } invoicesIds.push(newInvoice.id); } @@ -192,8 +225,10 @@ module.exports = Self => { const query = `SELECT DISTINCT clientFk AS id FROM ticket t JOIN ticketPackaging tp ON t.id = tp.ticketFk + JOIN client c ON c.id = t.clientFk WHERE t.shipped BETWEEN '2017-11-21' AND ? - AND t.clientFk BETWEEN ? AND ?`; + AND t.clientFk BETWEEN ? AND ? + AND c.isActive`; return models.InvoiceOut.rawSql(query, [ args.maxShipped, args.fromClientId, From f3df373f70aded877b2cbeae261b75ace5cacba8 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 10 Aug 2022 13:23:50 +0200 Subject: [PATCH 02/40] delete: unnecessary code --- .../methods/invoiceOut/globalInvoicing.js | 30 ++----------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 14d6913cb..dba05ed85 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -145,34 +145,8 @@ module.exports = Self => { if (newInvoice.id) { await Self.rawSql('CALL invoiceOutBooking(?)', [newInvoice.id], myOptions); - // query = `INSERT IGNORE INTO invoiceOut_queue(invoiceFk) VALUES(?)`; - // await Self.rawSql(query, [newInvoice.id], myOptions); - - query = `SELECT id FROM vn.report WHERE name ='invoice'`; - const [reportFk] = await Self.rawSql(query, null, myOptions); - - query = `SELECT ref FROM vn.invoiceOut WHERE id = ?`; - const [invoiceRef] = await Self.rawSql(query, [newInvoice.id], myOptions); - - // Print invoice - const args = { - invoiceOutFk: newInvoice.id, - refFk: invoiceRef.ref, - hasToForcePdf: true, - email: '' - }; - - const userId = ctx.req.accessToken.userId; - - query = `INSERT INTO vn.printQueue (priorityFk, reportFk, workerFk, printerFk) VALUES (?, ?, ?, NULL)`; - await Self.rawSql(query, [2, reportFk.id, userId], myOptions); // La prioridad de donde se la saca? 'adPriorityBelowNormal' - - const [lastInsertedPrintQueue] = await Self.rawSql('SELECT LAST_INSERT_ID() AS id', null, myOptions); - - for (let key in args) { - query = `INSERT INTO vn.printQueueArgs (printQueueFk, name, value) VALUES (?, ?, ?)`; - await Self.rawSql(query, [lastInsertedPrintQueue.id, key, args[key]], myOptions); - } + query = `INSERT IGNORE INTO invoiceOut_queue(invoiceFk) VALUES(?)`; + await Self.rawSql(query, [newInvoice.id], myOptions); invoicesIds.push(newInvoice.id); } From 994cf7ccf0e3d55116188b84bbb80cdb3b5d2687 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 4 Oct 2022 15:04:27 +0200 Subject: [PATCH 03/40] fix: '==' by '=' --- modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index dba05ed85..6279f90b6 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -41,7 +41,6 @@ module.exports = Self => { Self.globalInvoicing = async(ctx, options) => { const args = ctx.args; - let tx; const myOptions = {}; @@ -79,7 +78,7 @@ module.exports = Self => { const minShipped = new Date(); minShipped.setFullYear(minShipped.getFullYear() - 1); - if (args.fromClientId = args.toClientId) { + if (args.fromClientId == args.toClientId) { minShipped.setFullYear(2021); minShipped.setMonth(1); minShipped.setDate(1); From d5592d6a0f4a22f4cc3dfa1ad36712d63810ff55 Mon Sep 17 00:00:00 2001 From: jgallego Date: Thu, 6 Oct 2022 15:19:55 +0200 Subject: [PATCH 04/40] sin toClientId --- .../methods/invoiceOut/globalInvoicing.js | 24 ++++++++++--------- .../front/index/global-invoicing/index.js | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 4a20b81a2..71a10b1b8 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -21,7 +21,8 @@ module.exports = Self => { { arg: 'toClientId', type: 'number', - description: 'The maximum client id' + description: 'The maximum client id', + required: false }, { arg: 'companyFk', @@ -77,13 +78,10 @@ module.exports = Self => { const minShipped = new Date(); minShipped.setFullYear(minShipped.getFullYear() - 1); - - if (args.fromClientId == args.toClientId) { - minShipped.setFullYear(2021); - minShipped.setMonth(1); - minShipped.setDate(1); - minShipped.setHours(0, 0, 0, 0); - } + minShipped.setMonth(1); + minShipped.setDate(1); + minShipped.setHours(0, 0, 0, 0); + // Packaging liquidation const vIsAllInvoiceable = false; @@ -200,11 +198,13 @@ module.exports = Self => { JOIN ticketPackaging tp ON t.id = tp.ticketFk JOIN client c ON c.id = t.clientFk WHERE t.shipped BETWEEN '2017-11-21' AND ? - AND t.clientFk BETWEEN ? AND ? + AND t.clientFk >= ? + AND (t.clientFk <= ? OR ? IS NULL) AND c.isActive`; return models.InvoiceOut.rawSql(query, [ args.maxShipped, args.fromClientId, + args.toClientId, args.toClientId ], options); } @@ -233,15 +233,17 @@ module.exports = Self => { LEFT JOIN ticketService ts ON ts.ticketFk = t.id JOIN address a ON a.id = t.addressFk JOIN client c ON c.id = t.clientFk - WHERE ISNULL(t.refFk) AND c.id BETWEEN ? AND ? + WHERE ISNULL(t.refFk) AND c.id >= ? + AND (t.clientFk <= ? OR ? IS NULL) AND t.shipped BETWEEN ? AND util.dayEnd(?) AND t.companyFk = ? AND c.hasToInvoice - AND c.isTaxDataChecked + AND c.isTaxDataChecked AND c.isActive GROUP BY c.id, IF(c.hasToInvoiceByAddress,a.id,TRUE) HAVING sumAmount > 0`; return models.InvoiceOut.rawSql(query, [ args.fromClientId, args.toClientId, + args.toClientId, minShipped, args.maxShipped, args.companyFk diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 5e522f23d..1b1ae5ec9 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -54,7 +54,7 @@ class Controller extends Dialog { 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) + if (!this.invoice.fromClientId) throw new Error('Choose a valid clients range'); this.isInvoicing = true; From 3da948f3f9f1873c54f940e9960cf1b1b0f3cf04 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 7 Oct 2022 09:17:14 +0200 Subject: [PATCH 05/40] refactor: delete unnecessary lines --- modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 71a10b1b8..dae6a0c33 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -81,7 +81,6 @@ module.exports = Self => { minShipped.setMonth(1); minShipped.setDate(1); minShipped.setHours(0, 0, 0, 0); - // Packaging liquidation const vIsAllInvoiceable = false; From 8130cf9e6a7d2583f10af4ed46c5d1cf733e0fac Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 10 Oct 2022 12:40:10 +0200 Subject: [PATCH 06/40] feat: separate globalInvoicing in 2 parts --- db/changes/10491-august/00-aclInvoiceOut.sql | 3 + .../methods/invoiceOut/clientToInvoice.js | 173 ++++++++++++ .../methods/invoiceOut/globalInvoicing.js | 253 +++++------------- modules/invoiceOut/back/models/invoice-out.js | 1 + .../front/index/global-invoicing/index.html | 2 + .../front/index/global-invoicing/index.js | 14 +- 6 files changed, 255 insertions(+), 191 deletions(-) create mode 100644 db/changes/10491-august/00-aclInvoiceOut.sql create mode 100644 modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js diff --git a/db/changes/10491-august/00-aclInvoiceOut.sql b/db/changes/10491-august/00-aclInvoiceOut.sql new file mode 100644 index 000000000..b129b7201 --- /dev/null +++ b/db/changes/10491-august/00-aclInvoiceOut.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('InvoiceOut', 'clientToInvoice', 'WRITE', 'ALLOW', 'ROLE', 'invoicing'); \ No newline at end of file diff --git a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js new file mode 100644 index 000000000..16c999e73 --- /dev/null +++ b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js @@ -0,0 +1,173 @@ +module.exports = Self => { + Self.remoteMethodCtx('clientToInvoice', { + description: 'Get the clients to make global invoicing', + accessType: 'WRITE', + accepts: [ + { + 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' + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: '/clientToInvoice', + verb: 'POST' + } + }); + + Self.clientToInvoice = async(ctx, options) => { + const args = ctx.args; + let tx; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + const clientToInvoideIds = []; + let query; + try { + query = ` + SELECT MAX(issued) issued + FROM vn.invoiceOut io + JOIN vn.time t ON t.dated = io.issued + WHERE io.serial = 'A' + AND t.year = YEAR(?) + AND io.companyFk = ?`; + const [maxIssued] = await Self.rawSql(query, [ + args.invoiceDate, + args.companyFk + ], myOptions); + + const maxSerialDate = maxIssued.issued || args.invoiceDate; + if (args.invoiceDate < maxSerialDate) + args.invoiceDate = maxSerialDate; + + if (args.invoiceDate < args.maxShipped) + args.maxShipped = args.invoiceDate; + + const minShipped = new Date(); + minShipped.setFullYear(minShipped.getFullYear() - 1); + minShipped.setMonth(1); + minShipped.setDate(1); + minShipped.setHours(0, 0, 0, 0); + + // Packaging liquidation + const vIsAllInvoiceable = false; + const clientsWithPackaging = await getClientsWithPackaging(ctx, myOptions); + for (let client of clientsWithPackaging) { + await Self.rawSql('CALL packageInvoicing(?, ?, ?, ?, @newTicket)', [ + client.id, + args.invoiceDate, + args.companyFk, + vIsAllInvoiceable + ], myOptions); + } + + const invoiceableClients = await getInvoiceableClients(ctx, myOptions); + + if (!invoiceableClients.length) return; + + const clientToInvoiceIds = invoiceableClients.map(invoiceableClient => invoiceableClient.id); + const dataArr = new Set(clientToInvoiceIds); + const clientNotRepeatedIds = [...dataArr]; + + if (tx) await tx.commit(); + + console.log(invoiceableClients, clientToInvoiceIds, clientNotRepeatedIds); + return clientNotRepeatedIds; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; + + async function getClientsWithPackaging(ctx, options) { + const models = Self.app.models; + const args = ctx.args; + const query = `SELECT DISTINCT clientFk AS id + FROM ticket t + JOIN ticketPackaging tp ON t.id = tp.ticketFk + JOIN client c ON c.id = t.clientFk + WHERE t.shipped BETWEEN '2017-11-21' AND ? + AND t.clientFk >= ? + AND (t.clientFk <= ? OR ? IS NULL) + AND c.isActive`; + return models.InvoiceOut.rawSql(query, [ + args.maxShipped, + args.fromClientId, + args.toClientId, + args.toClientId + ], options); + } + + async function getInvoiceableClients(ctx, options) { + const models = Self.app.models; + const args = ctx.args; + const minShipped = new Date(); + minShipped.setFullYear(minShipped.getFullYear() - 1); + + const query = `SELECT + c.id, + SUM(IFNULL + ( + s.quantity * + s.price * (100-s.discount)/100, + 0) + + IFNULL(ts.quantity * ts.price,0) + ) AS sumAmount, + c.hasToInvoiceByAddress, + c.email, + c.isToBeMailed, + a.id addressFk + FROM ticket t + LEFT JOIN sale s ON s.ticketFk = t.id + LEFT JOIN ticketService ts ON ts.ticketFk = t.id + JOIN address a ON a.id = t.addressFk + JOIN client c ON c.id = t.clientFk + WHERE ISNULL(t.refFk) AND c.id >= ? + AND (t.clientFk <= ? OR ? IS NULL) + AND t.shipped BETWEEN ? AND util.dayEnd(?) + AND t.companyFk = ? AND c.hasToInvoice + AND c.isTaxDataChecked AND c.isActive + GROUP BY c.id, IF(c.hasToInvoiceByAddress,a.id,TRUE) HAVING sumAmount > 0`; + + return models.InvoiceOut.rawSql(query, [ + args.fromClientId, + args.toClientId, + args.toClientId, + minShipped, + args.maxShipped, + args.companyFk + ], options); + } +}; diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index dae6a0c33..bee79746a 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -1,35 +1,13 @@ module.exports = Self => { Self.remoteMethodCtx('globalInvoicing', { - description: 'Make a global invoice', + description: 'Make a global invoice of a client', accessType: 'WRITE', - accepts: [ - { - 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' - } - ], + accepts: [{ + arg: 'clientId', + type: 'number', + description: 'The client id to invoice', + required: true + }], returns: { type: 'object', root: true @@ -40,10 +18,10 @@ module.exports = Self => { } }); - Self.globalInvoicing = async(ctx, options) => { - const args = ctx.args; - let tx; + Self.globalInvoicing = async(ctx, clientId, options) => { + const models = Self.app.models; const myOptions = {}; + let tx; if (typeof options == 'object') Object.assign(myOptions, options); @@ -55,108 +33,65 @@ module.exports = Self => { const invoicesIds = []; const failedClients = []; - let query; try { - query = ` - SELECT MAX(issued) issued - FROM vn.invoiceOut io - JOIN vn.time t ON t.dated = io.issued - WHERE io.serial = 'A' - AND t.year = YEAR(?) - AND io.companyFk = ?`; - const [maxIssued] = await Self.rawSql(query, [ - args.invoiceDate, - args.companyFk - ], myOptions); - - const maxSerialDate = maxIssued.issued || args.invoiceDate; - if (args.invoiceDate < maxSerialDate) - args.invoiceDate = maxSerialDate; - - if (args.invoiceDate < args.maxShipped) - args.maxShipped = args.invoiceDate; - - const minShipped = new Date(); - minShipped.setFullYear(minShipped.getFullYear() - 1); - minShipped.setMonth(1); - minShipped.setDate(1); - minShipped.setHours(0, 0, 0, 0); - - // Packaging liquidation - const vIsAllInvoiceable = false; - const clientsWithPackaging = await getClientsWithPackaging(ctx, myOptions); - for (let client of clientsWithPackaging) { - await Self.rawSql('CALL packageInvoicing(?, ?, ?, ?, @newTicket)', [ - client.id, - args.invoiceDate, - args.companyFk, - vIsAllInvoiceable - ], myOptions); - } - - const invoiceableClients = await getInvoiceableClients(ctx, myOptions); - - if (!invoiceableClients.length) return; - - for (let client of invoiceableClients) { - try { - if (client.hasToInvoiceByAddress) { - await Self.rawSql('CALL ticketToInvoiceByAddress(?, ?, ?, ?)', [ - minShipped, - args.maxShipped, - client.addressFk, - args.companyFk - ], myOptions); - } else { - await Self.rawSql('CALL invoiceFromClient(?, ?, ?)', [ - args.maxShipped, - client.id, - args.companyFk - ], myOptions); - } - - // Make invoice - const isSpanishCompany = await getIsSpanishCompany(args.companyFk, myOptions); - - // Validates ticket nagative base - const hasAnyNegativeBase = await getNegativeBase(myOptions); - if (hasAnyNegativeBase && isSpanishCompany) - continue; - - query = `SELECT invoiceSerial(?, ?, ?) AS serial`; - const [invoiceSerial] = await Self.rawSql(query, [ + const client = await models.Client.findById(clientId, { + fields: ['id', 'hasToInvoiceByAddress', 'addressFk'] + }, myOptions); + try { + if (client.hasToInvoiceByAddress) { + await Self.rawSql('CALL ticketToInvoiceByAddress(?, ?, ?, ?)', [ + minShipped, + args.maxShipped, + client.addressFk, + args.companyFk + ], myOptions); + } else { + await Self.rawSql('CALL invoiceFromClient(?, ?, ?)', [ + args.maxShipped, client.id, - args.companyFk, - 'G' + args.companyFk ], myOptions); - const serialLetter = invoiceSerial.serial; - - query = `CALL invoiceOut_new(?, ?, NULL, @invoiceId)`; - await Self.rawSql(query, [ - serialLetter, - args.invoiceDate - ], myOptions); - - const [newInvoice] = await Self.rawSql(`SELECT @invoiceId id`, null, myOptions); - if (newInvoice.id) { - await Self.rawSql('CALL invoiceOutBooking(?)', [newInvoice.id], myOptions); - - query = `INSERT IGNORE INTO invoiceOutQueue(invoiceFk) VALUES(?)`; - await Self.rawSql(query, [newInvoice.id], myOptions); - - invoicesIds.push(newInvoice.id); - } - } catch (e) { - failedClients.push({ - id: client.id, - stacktrace: e - }); - continue; } - } - if (failedClients.length > 0) - await notifyFailures(ctx, failedClients, myOptions); + // Make invoice + const isSpanishCompany = await getIsSpanishCompany(args.companyFk, myOptions); + + // Validates ticket nagative base + const hasAnyNegativeBase = await getNegativeBase(myOptions); + if (hasAnyNegativeBase && isSpanishCompany) + return notifyFailures(ctx, failedClients, myOptions); // continue + + query = `SELECT invoiceSerial(?, ?, ?) AS serial`; + const [invoiceSerial] = await Self.rawSql(query, [ + client.id, + args.companyFk, + 'G' + ], myOptions); + const serialLetter = invoiceSerial.serial; + + query = `CALL invoiceOut_new(?, ?, NULL, @invoiceId)`; + await Self.rawSql(query, [ + serialLetter, + args.invoiceDate + ], myOptions); + + const [newInvoice] = await Self.rawSql(`SELECT @invoiceId id`, null, myOptions); + if (newInvoice.id) { + await Self.rawSql('CALL invoiceOutBooking(?)', [newInvoice.id], myOptions); + + query = `INSERT IGNORE INTO invoiceOutQueue(invoiceFk) VALUES(?)`; + await Self.rawSql(query, [newInvoice.id], myOptions); + + invoicesIds.push(newInvoice.id); + } + } catch (e) { + failedClients.push({ + id: client.id, + stacktrace: e + }); + return notifyFailures(ctx, failedClients, myOptions); // continue + } + // } if (tx) await tx.commit(); } catch (e) { @@ -189,66 +124,6 @@ module.exports = Self => { return supplierCompany && supplierCompany.total; } - async function getClientsWithPackaging(ctx, options) { - const models = Self.app.models; - const args = ctx.args; - const query = `SELECT DISTINCT clientFk AS id - FROM ticket t - JOIN ticketPackaging tp ON t.id = tp.ticketFk - JOIN client c ON c.id = t.clientFk - WHERE t.shipped BETWEEN '2017-11-21' AND ? - AND t.clientFk >= ? - AND (t.clientFk <= ? OR ? IS NULL) - AND c.isActive`; - return models.InvoiceOut.rawSql(query, [ - args.maxShipped, - args.fromClientId, - args.toClientId, - args.toClientId - ], options); - } - - async function getInvoiceableClients(ctx, options) { - const models = Self.app.models; - const args = ctx.args; - const minShipped = new Date(); - minShipped.setFullYear(minShipped.getFullYear() - 1); - - const query = `SELECT - c.id, - SUM(IFNULL - ( - s.quantity * - s.price * (100-s.discount)/100, - 0) - + IFNULL(ts.quantity * ts.price,0) - ) AS sumAmount, - c.hasToInvoiceByAddress, - c.email, - c.isToBeMailed, - a.id addressFk - FROM ticket t - LEFT JOIN sale s ON s.ticketFk = t.id - LEFT JOIN ticketService ts ON ts.ticketFk = t.id - JOIN address a ON a.id = t.addressFk - JOIN client c ON c.id = t.clientFk - WHERE ISNULL(t.refFk) AND c.id >= ? - AND (t.clientFk <= ? OR ? IS NULL) - AND t.shipped BETWEEN ? AND util.dayEnd(?) - AND t.companyFk = ? AND c.hasToInvoice - AND c.isTaxDataChecked AND c.isActive - GROUP BY c.id, IF(c.hasToInvoiceByAddress,a.id,TRUE) HAVING sumAmount > 0`; - - return models.InvoiceOut.rawSql(query, [ - args.fromClientId, - args.toClientId, - args.toClientId, - minShipped, - args.maxShipped, - args.companyFk - ], options); - } - async function notifyFailures(ctx, failedClients, options) { const models = Self.app.models; const userId = ctx.req.accessToken.userId; diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index 5af64de2b..7ac1246cf 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -7,6 +7,7 @@ module.exports = Self => { require('../methods/invoiceOut/book')(Self); require('../methods/invoiceOut/createPdf')(Self); require('../methods/invoiceOut/createManualInvoice')(Self); + require('../methods/invoiceOut/clientToInvoice')(Self); require('../methods/invoiceOut/globalInvoicing')(Self); require('../methods/invoiceOut/refund')(Self); require('../methods/invoiceOut/invoiceEmail')(Self); diff --git a/modules/invoiceOut/front/index/global-invoicing/index.html b/modules/invoiceOut/front/index/global-invoicing/index.html index 3d245b8d8..dbb780b7c 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.html +++ b/modules/invoiceOut/front/index/global-invoicing/index.html @@ -20,6 +20,8 @@ Adding invoices to queue... + {{$ctrl.lastClientId}} + {{::$ctrl.lastClientId || 'Vacío'}} diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 1b1ae5ec9..66c2e698a 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -58,8 +58,18 @@ class Controller extends Dialog { throw new Error('Choose a valid clients range'); this.isInvoicing = true; - return this.$http.post(`InvoiceOuts/globalInvoicing`, this.invoice) - .then(() => super.responseHandler(response)) + return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) + .then(res => { + this.lastClientId = res.data[res.data.length - 1]; + console.log(this.lastClientId); + }) + // .then(() => { + // for (let clientId of res.data) { + // const params = {clientId: clientId}; + // this.$http.post(`InvoiceOuts/globalInvoicing`, params); + // } + // }) + // .then(() => super.responseHandler(response)) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) .finally(() => this.isInvoicing = false); } catch (e) { From 53f78a98d274ac1f74d2d20a54676343a6c0087c Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 11 Oct 2022 10:04:13 +0200 Subject: [PATCH 07/40] feat: update message while globalInvoicing --- .../front/index/global-invoicing/index.html | 4 +--- .../front/index/global-invoicing/index.js | 18 ++++++++---------- .../front/index/global-invoicing/locale/es.yml | 3 ++- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.html b/modules/invoiceOut/front/index/global-invoicing/index.html index dbb780b7c..d7b4c3604 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.html +++ b/modules/invoiceOut/front/index/global-invoicing/index.html @@ -19,9 +19,7 @@ ng-if="$ctrl.isInvoicing"> - Adding invoices to queue... - {{$ctrl.lastClientId}} - {{::$ctrl.lastClientId || 'Vacío'}} + {{$ctrl.currentClientId}} {{$ctrl.$t('of')}} {{$ctrl.lastClientId}} diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 66c2e698a..fea1094ce 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -57,19 +57,17 @@ class Controller extends Dialog { if (!this.invoice.fromClientId) throw new Error('Choose a valid clients range'); - this.isInvoicing = true; return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) - .then(res => { + .then(async res => { this.lastClientId = res.data[res.data.length - 1]; - console.log(this.lastClientId); + this.isInvoicing = true; + for (let clientId of res.data) { + this.currentClientId = clientId; + const params = {clientId: clientId}; + await this.$http.post(`InvoiceOuts/globalInvoicing`, params); + } }) - // .then(() => { - // for (let clientId of res.data) { - // const params = {clientId: clientId}; - // this.$http.post(`InvoiceOuts/globalInvoicing`, params); - // } - // }) - // .then(() => super.responseHandler(response)) + .then(() => super.responseHandler(response)) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) .finally(() => this.isInvoicing = false); } catch (e) { diff --git a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml index 1a6e15656..208974307 100644 --- a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml +++ b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml @@ -6,4 +6,5 @@ Invoice date: Fecha de factura From client: Desde el cliente To client: Hasta el cliente Invoice date and the max date should be filled: La fecha de factura y la fecha límite deben rellenarse -Choose a valid clients range: Selecciona un rango válido de clientes \ No newline at end of file +Choose a valid clients range: Selecciona un rango válido de clientes +of: de \ No newline at end of file From 0d785695fe311fecf198891e7efe8be0a375211e Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 11 Oct 2022 12:24:06 +0200 Subject: [PATCH 08/40] 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 16c999e73..5466f22ac 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 bee79746a..a90a986c3 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 fea1094ce..30d965e9f 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)) From 50ec41decefb975e0900cafbe6596eb44596f042 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 13 Oct 2022 15:01:15 +0200 Subject: [PATCH 09/40] refator: cancel request --- .../methods/invoiceOut/clientToInvoice.js | 41 +++++++++------ .../methods/invoiceOut/globalInvoicing.js | 18 ++++--- .../front/index/global-invoicing/index.html | 10 ++-- .../front/index/global-invoicing/index.js | 50 ++++++++++++++----- .../index/global-invoicing/locale/es.yml | 3 +- 5 files changed, 82 insertions(+), 40 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js index 5466f22ac..cb2bfece7 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js +++ b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js @@ -30,10 +30,14 @@ module.exports = Self => { description: 'The company id to invoice' } ], - returns: { - type: 'object', - root: true + returns: [{ + arg: 'clientsAndAddresses', + type: ['object'] }, + { + arg: 'invoice', + type: 'object' + }], http: { path: '/clientToInvoice', verb: 'POST' @@ -94,23 +98,30 @@ module.exports = Self => { const invoiceableClients = await getInvoiceableClients(ctx, myOptions); - if (!invoiceableClients.length) return; + if (!invoiceableClients) return; - const clientAndAddress = invoiceableClients.map( - invoiceableClient => [invoiceableClient.id, invoiceableClient.addressFk] + const clientsAndAddresses = invoiceableClients.map(invoiceableClient => { + return { + clientId: invoiceableClient.id, + addressId: invoiceableClient.addressFk + + }; + } ); if (tx) await tx.commit(); - return { - clientAndAddressIds: clientAndAddress, - invoiceDate: args.invoiceDate, - maxShipped: args.maxShipped, - fromClientId: args.fromClientId, - toClientId: args.toClientId, - companyFk: args.companyFk, - minShipped: minShipped - }; + return [ + clientsAndAddresses, + { + 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 a90a986c3..8728a5278 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -1,6 +1,6 @@ module.exports = Self => { Self.remoteMethodCtx('globalInvoicing', { - description: 'Make a global invoice of a client', + description: 'Make a invoice of a client', accessType: 'WRITE', accepts: [{ arg: 'clientId', @@ -17,17 +17,20 @@ module.exports = Self => { { arg: 'invoiceDate', type: 'date', - description: 'The invoice date' + description: 'The invoice date', + required: true }, { arg: 'maxShipped', type: 'date', - description: 'The maximum shipped date' + description: 'The maximum shipped date', + required: true }, { arg: 'fromClientId', type: 'number', - description: 'The minimum client id' + description: 'The minimum client id', + required: true }, { arg: 'toClientId', @@ -38,12 +41,14 @@ module.exports = Self => { { arg: 'companyFk', type: 'number', - description: 'The company id to invoice' + description: 'The company id to invoice', + required: true }, { arg: 'minShipped', type: 'date', - description: 'The company id to invoice' + description: 'The company id to invoice', + required: true }], returns: { type: 'object', @@ -72,7 +77,6 @@ module.exports = Self => { const invoicesIds = []; const failedClients = []; try { - // const addresses = await models.Address.find({where: {clientFk: args.clientId}}, myOptions); const client = await models.Client.findById(args.clientId, { fields: ['id', 'hasToInvoiceByAddress'] }, myOptions); diff --git a/modules/invoiceOut/front/index/global-invoicing/index.html b/modules/invoiceOut/front/index/global-invoicing/index.html index d7b4c3604..a6c7661f5 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.html +++ b/modules/invoiceOut/front/index/global-invoicing/index.html @@ -16,10 +16,12 @@
+ ng-if="$ctrl.lastClientId"> - - {{$ctrl.currentClientId}} {{$ctrl.$t('of')}} {{$ctrl.lastClientId}} +
+ {{'Id Client' | translate}}: {{$ctrl.currentClientId}} + {{'of' | translate}} {{::$ctrl.lastClientId}} +
@@ -66,5 +68,5 @@ - + {{$ctrl.isInvoicing}} \ No newline at end of file diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 30d965e9f..a74cf7610 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -6,7 +6,7 @@ class Controller extends Dialog { constructor($element, $, $transclude) { super($element, $, $transclude); - this.isInvoicing = false; + this.lastClientId = null; this.invoice = { maxShipped: new Date() }; @@ -46,7 +46,7 @@ class Controller extends Dialog { this.invoice.companyFk = value; } - responseHandler(response) { + async responseHandler(response) { try { if (response !== 'accept') return super.responseHandler(response); @@ -57,25 +57,49 @@ class Controller extends Dialog { if (!this.invoice.fromClientId) throw new Error('Choose a valid clients range'); + this.on('close', () => { + if (this.canceler) this.canceler.resolve(); + this.vnApp.showSuccess(this.$t('Data saved!')); + }); + return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) .then(async res => { - const clientAndAddressIds = res.data.clientAndAddressIds; - if (!clientAndAddressIds) return super.responseHandler(response); - this.lastClientId = clientAndAddressIds[clientAndAddressIds.length - 1][0]; - this.isInvoicing = true; - 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); + const clientsAndAddresses = res.data.clientsAndAddresses; + const invoice = res.data.invoice; + + if (!clientsAndAddresses.length) return super.responseHandler(response); + this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId; + this.$.invoiceButton.setAttribute('disabled', true); + for (let clientAndAddress of clientsAndAddresses) { + this.currentClientId = clientAndAddress.clientId; + const params = { + clientId: clientAndAddress.clientId, + addressId: clientAndAddress.addressId, + invoiceDate: invoice.invoiceDate, + maxShipped: invoice.maxShipped, + fromClientId: invoice.fromClientId, + toClientId: invoice.toClientId, + companyFk: invoice.companyFk, + minShipped: invoice.minShipped, + + }; + this.canceler = this.$q.defer(); + const options = { + timeout: this.canceler.promise + }; + await this.$http.post(`InvoiceOuts/globalInvoicing`, params, options); } }) .then(() => super.responseHandler(response)) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) - .finally(() => this.isInvoicing = false); + .finally(() => { + this.lastClientId = null; + this.$.invoiceButton.removeAttribute('disabled'); + }); } catch (e) { this.vnApp.showError(this.$t(e.message)); - this.isInvoicing = false; + this.lastClientId = null; + this.$.invoiceButton.removeAttribute('disabled'); return false; } } diff --git a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml index 208974307..6245ee208 100644 --- a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml +++ b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml @@ -7,4 +7,5 @@ From client: Desde el cliente To client: Hasta el cliente Invoice date and the max date should be filled: La fecha de factura y la fecha límite deben rellenarse Choose a valid clients range: Selecciona un rango válido de clientes -of: de \ No newline at end of file +of: de +Id Client: Id Cliente \ No newline at end of file From 5918b3e0dfc3bc3c7868664fb71090366ba8820a Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 14 Oct 2022 09:30:09 +0200 Subject: [PATCH 10/40] refactor: juan changes --- .../invoiceOut/front/index/global-invoicing/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index a74cf7610..2dc4de3cb 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -62,6 +62,7 @@ class Controller extends Dialog { this.vnApp.showSuccess(this.$t('Data saved!')); }); + this.$.invoiceButton.disabled = true; return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) .then(async res => { const clientsAndAddresses = res.data.clientsAndAddresses; @@ -69,7 +70,6 @@ class Controller extends Dialog { if (!clientsAndAddresses.length) return super.responseHandler(response); this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId; - this.$.invoiceButton.setAttribute('disabled', true); for (let clientAndAddress of clientsAndAddresses) { this.currentClientId = clientAndAddress.clientId; const params = { @@ -93,13 +93,14 @@ class Controller extends Dialog { .then(() => super.responseHandler(response)) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) .finally(() => { - this.lastClientId = null; - this.$.invoiceButton.removeAttribute('disabled'); + this.lastClientId = null; // unificar en una función + this.$.invoiceButton.disabled = false; // unificar en una función }); } catch (e) { this.vnApp.showError(this.$t(e.message)); - this.lastClientId = null; - this.$.invoiceButton.removeAttribute('disabled'); + this.lastClientId = null; // unificar en una función + this.$.invoiceButton.disabled = false; // unificar en una función + throw e; return false; } } From 15ba6681afd112b91b68ccc8b299b1d0b7d5907b Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 09:49:47 +0200 Subject: [PATCH 11/40] refator: code unified --- .../back/methods/invoiceOut/globalInvoicing.js | 4 ++-- .../front/index/global-invoicing/index.js | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 8728a5278..b9d4b1e32 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -102,7 +102,7 @@ module.exports = Self => { // Validates ticket nagative base const hasAnyNegativeBase = await getNegativeBase(myOptions); if (hasAnyNegativeBase && isSpanishCompany) - return tx.rollback(); // continue + return tx.rollback(); query = `SELECT invoiceSerial(?, ?, ?) AS serial`; const [invoiceSerial] = await Self.rawSql(query, [ @@ -132,7 +132,7 @@ module.exports = Self => { id: client.id, stacktrace: e }); - await notifyFailures(ctx, failedClients, myOptions); // continue + await notifyFailures(ctx, failedClients, myOptions); } if (tx) await tx.commit(); diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 2dc4de3cb..74a4f562f 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -5,8 +5,6 @@ import './style.scss'; class Controller extends Dialog { constructor($element, $, $transclude) { super($element, $, $transclude); - - this.lastClientId = null; this.invoice = { maxShipped: new Date() }; @@ -46,6 +44,11 @@ class Controller extends Dialog { this.invoice.companyFk = value; } + restartValues() { + this.lastClientId = null; + this.$.invoiceButton.disabled = false; + } + async responseHandler(response) { try { if (response !== 'accept') @@ -92,15 +95,10 @@ class Controller extends Dialog { }) .then(() => super.responseHandler(response)) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) - .finally(() => { - this.lastClientId = null; // unificar en una función - this.$.invoiceButton.disabled = false; // unificar en una función - }); + .finally(() => this.restartValues()); } catch (e) { + this.restartValues(); this.vnApp.showError(this.$t(e.message)); - this.lastClientId = null; // unificar en una función - this.$.invoiceButton.disabled = false; // unificar en una función - throw e; return false; } } From 6924b05c8a45dc05044a67e8c3c271ed32875b58 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 10:19:07 +0200 Subject: [PATCH 12/40] feat: add radio button --- .../back/methods/invoiceOut/globalInvoicing.js | 2 +- .../front/index/global-invoicing/index.html | 12 ++++++++++++ .../invoiceOut/front/index/global-invoicing/index.js | 3 +++ .../front/index/global-invoicing/locale/es.yml | 4 +++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index b9d4b1e32..6aa2ecc07 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -36,7 +36,7 @@ module.exports = Self => { arg: 'toClientId', type: 'number', description: 'The maximum client id', - required: false + required: true }, { arg: 'companyFk', diff --git a/modules/invoiceOut/front/index/global-invoicing/index.html b/modules/invoiceOut/front/index/global-invoicing/index.html index a6c7661f5..7f96397f5 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.html +++ b/modules/invoiceOut/front/index/global-invoicing/index.html @@ -37,6 +37,18 @@ + + + + + + { const clientsAndAddresses = res.data.clientsAndAddresses; diff --git a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml index 6245ee208..8ac2cc13a 100644 --- a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml +++ b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml @@ -8,4 +8,6 @@ To client: Hasta el cliente Invoice date and the max date should be filled: La fecha de factura y la fecha límite deben rellenarse Choose a valid clients range: Selecciona un rango válido de clientes of: de -Id Client: Id Cliente \ No newline at end of file +Id Client: Id Cliente +All clients: Todos los clientes +Clients range: Rango de clientes \ No newline at end of file From 33092acee755b5e752400f2ecd9628a2ac735c2a Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 11:42:05 +0200 Subject: [PATCH 13/40] refactor: delete unnecessary code --- .../methods/invoiceOut/globalInvoicing.js | 37 ++++++------------- .../front/index/global-invoicing/index.js | 2 - 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 6aa2ecc07..270d1c2f9 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -26,18 +26,6 @@ module.exports = Self => { description: 'The maximum shipped date', required: true }, - { - arg: 'fromClientId', - type: 'number', - description: 'The minimum client id', - required: true - }, - { - arg: 'toClientId', - type: 'number', - description: 'The maximum client id', - required: true - }, { arg: 'companyFk', type: 'number', @@ -47,7 +35,7 @@ module.exports = Self => { { arg: 'minShipped', type: 'date', - description: 'The company id to invoice', + description: 'The minium shupped date', required: true }], returns: { @@ -74,13 +62,14 @@ module.exports = Self => { myOptions.transaction = tx; } - const invoicesIds = []; - const failedClients = []; + let invoiceId; try { const client = await models.Client.findById(args.clientId, { fields: ['id', 'hasToInvoiceByAddress'] }, myOptions); try { + throw new Error(`Pq lo digo yo`); + if (client.hasToInvoiceByAddress) { await Self.rawSql('CALL ticketToInvoiceByAddress(?, ?, ?, ?)', [ args.minShipped, @@ -125,14 +114,14 @@ module.exports = Self => { query = `INSERT IGNORE INTO invoiceOutQueue(invoiceFk) VALUES(?)`; await Self.rawSql(query, [newInvoice.id], myOptions); - invoicesIds.push(newInvoice.id); + invoiceId = newInvoice.id; } } catch (e) { - failedClients.push({ + const failedClient = { id: client.id, stacktrace: e - }); - await notifyFailures(ctx, failedClients, myOptions); + }; + await notifyFailures(ctx, failedClient, myOptions); } if (tx) await tx.commit(); @@ -141,7 +130,7 @@ module.exports = Self => { throw e; } - return invoicesIds; + return invoiceId; }; async function getNegativeBase(options) { @@ -166,7 +155,7 @@ module.exports = Self => { return supplierCompany && supplierCompany.total; } - async function notifyFailures(ctx, failedClients, options) { + async function notifyFailures(ctx, failedClient, options) { const models = Self.app.models; const userId = ctx.req.accessToken.userId; const $t = ctx.req.__; // $translate @@ -175,10 +164,8 @@ module.exports = Self => { const subject = $t('Global invoicing failed'); let body = $t(`Wasn't able to invoice the following clients`) + ':

'; - for (client of failedClients) { - body += `ID: ${client.id} -
${client.stacktrace}

`; - } + body += `ID: ${failedClient.id} +
${failedClient.stacktrace}

`; await Self.rawSql(` INSERT INTO vn.mail (sender, replyTo, sent, subject, body) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index a7b263fc0..4ef2ed8e0 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -83,8 +83,6 @@ class Controller extends Dialog { addressId: clientAndAddress.addressId, invoiceDate: invoice.invoiceDate, maxShipped: invoice.maxShipped, - fromClientId: invoice.fromClientId, - toClientId: invoice.toClientId, companyFk: invoice.companyFk, minShipped: invoice.minShipped, From e5fd6adfe553a8692846161bf3beacfcd59a908e Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 11:43:01 +0200 Subject: [PATCH 14/40] fix: delete throw error --- .../back/methods/invoiceOut/specs/globalInvoicing.spec.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js index f7546b72e..eeef08e1f 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js @@ -4,6 +4,7 @@ describe('InvoiceOut globalInvoicing()', () => { const userId = 1; const companyFk = 442; const clientId = 1101; + const addressId = 1; const invoicedTicketId = 8; const invoiceSerial = 'A'; const activeCtx = { @@ -22,11 +23,12 @@ describe('InvoiceOut globalInvoicing()', () => { try { ctx.args = { + clientId: clientId, + addressId: addressId, invoiceDate: new Date(), maxShipped: new Date(), - fromClientId: clientId, - toClientId: 1106, - companyFk: companyFk + companyFk: companyFk, + minShipped: new Date() }; const result = await models.InvoiceOut.globalInvoicing(ctx, options); const ticket = await models.Ticket.findById(invoicedTicketId, null, options); From dd10b79441bd688b25564f22a90441303fda31c2 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 11:43:26 +0200 Subject: [PATCH 15/40] delete: throw error --- modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js index 270d1c2f9..d74199799 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js @@ -68,8 +68,6 @@ module.exports = Self => { fields: ['id', 'hasToInvoiceByAddress'] }, myOptions); try { - throw new Error(`Pq lo digo yo`); - if (client.hasToInvoiceByAddress) { await Self.rawSql('CALL ticketToInvoiceByAddress(?, ?, ?, ?)', [ args.minShipped, From 2d1b48280c58b8f0c626ae9b0e611ef5e814dd13 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 11:53:29 +0200 Subject: [PATCH 16/40] fix: backTest --- .../invoiceOut/specs/globalInvoicing.spec.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js index eeef08e1f..bd6a07baa 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js @@ -2,9 +2,14 @@ const models = require('vn-loopback/server/server').models; describe('InvoiceOut globalInvoicing()', () => { const userId = 1; - const companyFk = 442; const clientId = 1101; - const addressId = 1; + const addressId = 121; + const companyFk = 442; + const minShipped = new Date(); + minShipped.setFullYear(minShipped.getFullYear() - 1); + minShipped.setMonth(1); + minShipped.setDate(1); + minShipped.setHours(0, 0, 0, 0); const invoicedTicketId = 8; const invoiceSerial = 'A'; const activeCtx = { @@ -28,12 +33,12 @@ describe('InvoiceOut globalInvoicing()', () => { invoiceDate: new Date(), maxShipped: new Date(), companyFk: companyFk, - minShipped: new Date() + minShipped: minShipped }; const result = await models.InvoiceOut.globalInvoicing(ctx, options); const ticket = await models.Ticket.findById(invoicedTicketId, null, options); - expect(result.length).toBeGreaterThan(0); + expect(result).toBeGreaterThan(0); expect(ticket.refFk).toContain(invoiceSerial); await tx.rollback(); From 86d24ffede6b430e7923d66f77d73244a8dc71d7 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 13:02:03 +0200 Subject: [PATCH 17/40] refactor code --- .../invoiceOut/back/methods/invoiceOut/clientToInvoice.js | 3 +-- modules/invoiceOut/front/index/global-invoicing/index.js | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js index cb2bfece7..a0578897a 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js +++ b/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js @@ -21,8 +21,7 @@ module.exports = Self => { { arg: 'toClientId', type: 'number', - description: 'The maximum client id', - required: false + description: 'The maximum client id' }, { arg: 'companyFk', diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 4ef2ed8e0..e311f6b02 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -58,7 +58,7 @@ class Controller extends Dialog { if (!this.invoice.invoiceDate || !this.invoice.maxShipped) throw new Error('Invoice date and the max date should be filled'); - if (!this.invoice.fromClientId) + if (!this.invoice.fromClientId || !this.invoice.toClientId) throw new Error('Choose a valid clients range'); this.on('close', () => { @@ -71,11 +71,11 @@ class Controller extends Dialog { return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) .then(async res => { - const clientsAndAddresses = res.data.clientsAndAddresses; const invoice = res.data.invoice; - + const clientsAndAddresses = res.data.clientsAndAddresses; if (!clientsAndAddresses.length) return super.responseHandler(response); this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId; + for (let clientAndAddress of clientsAndAddresses) { this.currentClientId = clientAndAddress.clientId; const params = { @@ -98,8 +98,8 @@ class Controller extends Dialog { .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) .finally(() => this.restartValues()); } catch (e) { - this.restartValues(); this.vnApp.showError(this.$t(e.message)); + this.restartValues(); return false; } } From 8ab6a50c73bd93e952ee4e5981d8d9ad3336278c Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 17 Oct 2022 13:02:19 +0200 Subject: [PATCH 18/40] try to fix frontTest --- .../index/global-invoicing/index.spec.js | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.spec.js b/modules/invoiceOut/front/index/global-invoicing/index.spec.js index 916364007..1e5ed3a99 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.spec.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.spec.js @@ -19,6 +19,7 @@ describe('InvoiceOut', () => { } }; controller = $componentController('vnInvoiceOutGlobalInvoicing', {$element, $scope, $transclude}); + controller.$.invoiceButton = {disabled: false}; })); describe('getMinClientId()', () => { @@ -86,14 +87,39 @@ describe('InvoiceOut', () => { it('should make an http POST query and then call to the showSuccess() method', () => { jest.spyOn(controller.vnApp, 'showSuccess'); - + const filter = { + order: 'id DESC', + limit: 1 + }; + const minShipped = new Date(); + minShipped.setFullYear(minShipped.getFullYear() - 1); + minShipped.setMonth(1); + minShipped.setDate(1); + minShipped.setHours(0, 0, 0, 0); controller.invoice = { invoiceDate: new Date(), maxShipped: new Date(), fromClientId: 1101, - toClientId: 1101 + toClientId: 1101, + companyFk: 442, + minShipped: minShipped + }; + const response = { + clientsAndAddresses: [{clientId: 1101, addressId: 121}], + invoice: controller.invoice + }; + const expectedParams = { + clientId: 1101, + addressId: 121, + invoiceDate: new Date(), + maxShipped: new Date(), + companyFk: 442, + minShipped: minShipped }; + const serializedParams = $httpParamSerializer({filter}); + $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112}); + $httpBackend.expect('POST', `InvoiceOuts/clientToInvoice`).respond(response); $httpBackend.expect('POST', `InvoiceOuts/globalInvoicing`).respond({id: 1}); controller.responseHandler('accept'); $httpBackend.flush(); From f95f977b69a4b72f5122107425f603b113aacae9 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 18 Oct 2022 10:04:53 +0200 Subject: [PATCH 19/40] fix: backTest --- .../methods/invoiceOut/specs/globalInvoicing.spec.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js index bd6a07baa..3f359dfb5 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js @@ -10,7 +10,6 @@ describe('InvoiceOut globalInvoicing()', () => { minShipped.setMonth(1); minShipped.setDate(1); minShipped.setHours(0, 0, 0, 0); - const invoicedTicketId = 8; const invoiceSerial = 'A'; const activeCtx = { accessToken: {userId: userId}, @@ -35,11 +34,14 @@ describe('InvoiceOut globalInvoicing()', () => { companyFk: companyFk, minShipped: minShipped }; - const result = await models.InvoiceOut.globalInvoicing(ctx, options); - const ticket = await models.Ticket.findById(invoicedTicketId, null, options); + const invoiceOutId = await models.InvoiceOut.globalInvoicing(ctx, options); + const invoiceOut = await models.InvoiceOut.findById(invoiceOutId, null, options); + const [firstTicket] = await models.Ticket.find({ + where: {refFk: invoiceOut.ref} + }, options); - expect(result).toBeGreaterThan(0); - expect(ticket.refFk).toContain(invoiceSerial); + expect(invoiceOutId).toBeGreaterThan(0); + expect(firstTicket.refFk).toContain(invoiceSerial); await tx.rollback(); } catch (e) { From dc62841c3d72950e12717321d80144c3929a2890 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 18 Oct 2022 14:46:40 +0200 Subject: [PATCH 20/40] refator: trying to delete async and await --- .../front/index/global-invoicing/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index e311f6b02..ef05c7784 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -50,6 +50,14 @@ class Controller extends Dialog { this.$.invoiceButton.disabled = false; } + invoice() { + const clientAndAddress = clientsAndAddresses[this.pos]; + return this.post(`InvoiceOuts/globalInvoicing`, params, options).then(() => { + this.pos++; + this.invoice(); + }); + } + async responseHandler(response) { try { if (response !== 'accept') @@ -69,7 +77,10 @@ class Controller extends Dialog { this.$.invoiceButton.disabled = true; if (this.clientsNumber == 'allClients') this.getMaxClientId(); - return this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) + this.pos = 0; + this.invoice(); + + this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) .then(async res => { const invoice = res.data.invoice; const clientsAndAddresses = res.data.clientsAndAddresses; @@ -91,7 +102,7 @@ class Controller extends Dialog { const options = { timeout: this.canceler.promise }; - await this.$http.post(`InvoiceOuts/globalInvoicing`, params, options); + await this.$http.post(`InvoiceOuts/globalInvoicing`, params, options).then(); } }) .then(() => super.responseHandler(response)) From 9d53360d50881ef016ab82d82753f9a538cf3c43 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 19 Oct 2022 13:59:57 +0200 Subject: [PATCH 21/40] feat: replaced await by recursive function --- .../front/index/global-invoicing/index.js | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index ef05c7784..46732cb66 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -50,15 +50,32 @@ class Controller extends Dialog { this.$.invoiceButton.disabled = false; } - invoice() { + invoiceOut(invoice, clientsAndAddresses) { const clientAndAddress = clientsAndAddresses[this.pos]; - return this.post(`InvoiceOuts/globalInvoicing`, params, options).then(() => { - this.pos++; - this.invoice(); - }); + 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, + + }; + this.canceler = this.$q.defer(); + const options = { + timeout: this.canceler.promise + }; + + return this.$http.post(`InvoiceOuts/globalInvoicing`, params, options) + .then(() => { + this.pos++; + return this.invoiceOut(invoice, clientsAndAddresses); + }); } - async responseHandler(response) { + responseHandler(response) { try { if (response !== 'accept') return super.responseHandler(response); @@ -77,33 +94,14 @@ class Controller extends Dialog { this.$.invoiceButton.disabled = true; if (this.clientsNumber == 'allClients') this.getMaxClientId(); - this.pos = 0; - this.invoice(); - this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) - .then(async res => { + .then(res => { const invoice = res.data.invoice; const clientsAndAddresses = res.data.clientsAndAddresses; if (!clientsAndAddresses.length) return super.responseHandler(response); this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId; - - for (let clientAndAddress of clientsAndAddresses) { - this.currentClientId = clientAndAddress.clientId; - const params = { - clientId: clientAndAddress.clientId, - addressId: clientAndAddress.addressId, - invoiceDate: invoice.invoiceDate, - maxShipped: invoice.maxShipped, - companyFk: invoice.companyFk, - minShipped: invoice.minShipped, - - }; - this.canceler = this.$q.defer(); - const options = { - timeout: this.canceler.promise - }; - await this.$http.post(`InvoiceOuts/globalInvoicing`, params, options).then(); - } + this.pos = 0; + return this.invoiceOut(invoice, clientsAndAddresses); }) .then(() => super.responseHandler(response)) .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))) From abd2d9a849ba76366f3b7c59f5c580d9fc2c43c8 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 19 Oct 2022 15:10:43 +0200 Subject: [PATCH 22/40] fix: e2e --- e2e/helpers/selectors.js | 1 + e2e/paths/09-invoice-out/04_globalInvoice.spec.js | 12 ++++++++++++ .../front/index/global-invoicing/index.spec.js | 8 -------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/e2e/helpers/selectors.js b/e2e/helpers/selectors.js index cd6d39795..a678da583 100644 --- a/e2e/helpers/selectors.js +++ b/e2e/helpers/selectors.js @@ -954,6 +954,7 @@ export default { manualInvoiceTaxArea: 'vn-autocomplete[ng-model="$ctrl.invoice.taxArea"]', saveInvoice: 'button[response="accept"]', globalInvoiceForm: '.vn-invoice-out-global-invoicing', + globalInvoiceClientsRange: 'vn-radio[val="clientsRange"]', globalInvoiceDate: '[ng-model="$ctrl.invoice.invoiceDate"]', globalInvoiceFromClient: '[ng-model="$ctrl.invoice.fromClientId"]', globalInvoiceToClient: '[ng-model="$ctrl.invoice.toClientId"]', diff --git a/e2e/paths/09-invoice-out/04_globalInvoice.spec.js b/e2e/paths/09-invoice-out/04_globalInvoice.spec.js index b62c889db..74efafd2d 100644 --- a/e2e/paths/09-invoice-out/04_globalInvoice.spec.js +++ b/e2e/paths/09-invoice-out/04_globalInvoice.spec.js @@ -33,6 +33,7 @@ describe('InvoiceOut global invoice path', () => { it('should create a global invoice for charles xavier today', async() => { await page.pickDate(selectors.invoiceOutIndex.globalInvoiceDate); + await page.waitToClick(selectors.invoiceOutIndex.globalInvoiceClientsRange); await page.autocompleteSearch(selectors.invoiceOutIndex.globalInvoiceFromClient, 'Petter Parker'); await page.autocompleteSearch(selectors.invoiceOutIndex.globalInvoiceToClient, 'Petter Parker'); await page.waitToClick(selectors.invoiceOutIndex.saveInvoice); @@ -48,4 +49,15 @@ describe('InvoiceOut global invoice path', () => { expect(currentInvoices).toBeGreaterThan(invoicesBefore); }); + + it('should create a global invoice for all clients today', async() => { + await page.waitToClick(selectors.invoiceOutIndex.createInvoice); + await page.waitToClick(selectors.invoiceOutIndex.createGlobalInvoice); + await page.waitForSelector(selectors.invoiceOutIndex.globalInvoiceForm); + await page.pickDate(selectors.invoiceOutIndex.globalInvoiceDate); + await page.waitToClick(selectors.invoiceOutIndex.saveInvoice); + const message = await page.waitForSnackbar(); + + expect(message.text).toContain('Data saved!'); + }); }); diff --git a/modules/invoiceOut/front/index/global-invoicing/index.spec.js b/modules/invoiceOut/front/index/global-invoicing/index.spec.js index 1e5ed3a99..6e3721f9e 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.spec.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.spec.js @@ -108,14 +108,6 @@ describe('InvoiceOut', () => { clientsAndAddresses: [{clientId: 1101, addressId: 121}], invoice: controller.invoice }; - const expectedParams = { - clientId: 1101, - addressId: 121, - invoiceDate: new Date(), - maxShipped: new Date(), - companyFk: 442, - minShipped: minShipped - }; const serializedParams = $httpParamSerializer({filter}); $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112}); From 39f4445172f9f3487e7884c7f120087d760664c1 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 20 Oct 2022 07:23:57 +0200 Subject: [PATCH 23/40] refator: endpoint name --- db/changes/10491-august/00-aclInvoiceOut.sql | 6 +++++- .../invoiceOut/{globalInvoicing.js => invoiceClient.js} | 6 +++--- .../{globalInvoicing.spec.js => invoiceClient.spec.js} | 4 ++-- modules/invoiceOut/back/models/invoice-out.js | 2 +- modules/invoiceOut/front/index/global-invoicing/index.js | 2 +- .../invoiceOut/front/index/global-invoicing/index.spec.js | 2 +- 6 files changed, 13 insertions(+), 9 deletions(-) rename modules/invoiceOut/back/methods/invoiceOut/{globalInvoicing.js => invoiceClient.js} (97%) rename modules/invoiceOut/back/methods/invoiceOut/specs/{globalInvoicing.spec.js => invoiceClient.spec.js} (91%) diff --git a/db/changes/10491-august/00-aclInvoiceOut.sql b/db/changes/10491-august/00-aclInvoiceOut.sql index b129b7201..bc2b87cdb 100644 --- a/db/changes/10491-august/00-aclInvoiceOut.sql +++ b/db/changes/10491-august/00-aclInvoiceOut.sql @@ -1,3 +1,7 @@ INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES - ('InvoiceOut', 'clientToInvoice', 'WRITE', 'ALLOW', 'ROLE', 'invoicing'); \ No newline at end of file + ('InvoiceOut', 'clientToInvoice', 'WRITE', 'ALLOW', 'ROLE', 'invoicing'); + +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('InvoiceOut', 'invoiceClient', 'WRITE', 'ALLOW', 'ROLE', 'invoicing'); \ No newline at end of file diff --git a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js similarity index 97% rename from modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js rename to modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index d74199799..ec3244344 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/globalInvoicing.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -1,5 +1,5 @@ module.exports = Self => { - Self.remoteMethodCtx('globalInvoicing', { + Self.remoteMethodCtx('invoiceClient', { description: 'Make a invoice of a client', accessType: 'WRITE', accepts: [{ @@ -43,12 +43,12 @@ module.exports = Self => { root: true }, http: { - path: '/globalInvoicing', + path: '/invoiceClient', verb: 'POST' } }); - Self.globalInvoicing = async(ctx, options) => { + Self.invoiceClient = async(ctx, options) => { const args = ctx.args; const models = Self.app.models; const myOptions = {}; diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js similarity index 91% rename from modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js rename to modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js index 3f359dfb5..f1f9b7037 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/globalInvoicing.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js @@ -1,6 +1,6 @@ const models = require('vn-loopback/server/server').models; -describe('InvoiceOut globalInvoicing()', () => { +describe('InvoiceOut invoiceClient()', () => { const userId = 1; const clientId = 1101; const addressId = 121; @@ -34,7 +34,7 @@ describe('InvoiceOut globalInvoicing()', () => { companyFk: companyFk, minShipped: minShipped }; - const invoiceOutId = await models.InvoiceOut.globalInvoicing(ctx, options); + const invoiceOutId = await models.InvoiceOut.invoiceClient(ctx, options); const invoiceOut = await models.InvoiceOut.findById(invoiceOutId, null, options); const [firstTicket] = await models.Ticket.find({ where: {refFk: invoiceOut.ref} diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index 7ac1246cf..20d9af939 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -8,7 +8,7 @@ module.exports = Self => { require('../methods/invoiceOut/createPdf')(Self); require('../methods/invoiceOut/createManualInvoice')(Self); require('../methods/invoiceOut/clientToInvoice')(Self); - require('../methods/invoiceOut/globalInvoicing')(Self); + require('../methods/invoiceOut/invoiceClient')(Self); require('../methods/invoiceOut/refund')(Self); require('../methods/invoiceOut/invoiceEmail')(Self); require('../methods/invoiceOut/exportationPdf')(Self); diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 46732cb66..ffd5484e4 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -68,7 +68,7 @@ class Controller extends Dialog { timeout: this.canceler.promise }; - return this.$http.post(`InvoiceOuts/globalInvoicing`, params, options) + return this.$http.post(`InvoiceOuts/invoiceClient`, params, options) .then(() => { this.pos++; return this.invoiceOut(invoice, clientsAndAddresses); diff --git a/modules/invoiceOut/front/index/global-invoicing/index.spec.js b/modules/invoiceOut/front/index/global-invoicing/index.spec.js index 6e3721f9e..04ea99e37 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.spec.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.spec.js @@ -112,7 +112,7 @@ describe('InvoiceOut', () => { const serializedParams = $httpParamSerializer({filter}); $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112}); $httpBackend.expect('POST', `InvoiceOuts/clientToInvoice`).respond(response); - $httpBackend.expect('POST', `InvoiceOuts/globalInvoicing`).respond({id: 1}); + $httpBackend.expect('POST', `InvoiceOuts/invoiceClient`).respond({id: 1}); controller.responseHandler('accept'); $httpBackend.flush(); From 53112995bcf8223955ee1b254ba842ff9c7c7bf3 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 20 Oct 2022 07:58:38 +0200 Subject: [PATCH 24/40] refactor: delete counter --- modules/invoiceOut/front/index/global-invoicing/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index ffd5484e4..1390f52e8 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -51,7 +51,7 @@ class Controller extends Dialog { } invoiceOut(invoice, clientsAndAddresses) { - const clientAndAddress = clientsAndAddresses[this.pos]; + const [clientAndAddress] = clientsAndAddresses; if (!clientAndAddress) return; this.currentClientId = clientAndAddress.clientId; const params = { @@ -70,7 +70,7 @@ class Controller extends Dialog { return this.$http.post(`InvoiceOuts/invoiceClient`, params, options) .then(() => { - this.pos++; + clientsAndAddresses.shift(); return this.invoiceOut(invoice, clientsAndAddresses); }); } @@ -100,7 +100,6 @@ class Controller extends Dialog { const clientsAndAddresses = res.data.clientsAndAddresses; if (!clientsAndAddresses.length) return super.responseHandler(response); this.lastClientId = clientsAndAddresses[clientsAndAddresses.length - 1].clientId; - this.pos = 0; return this.invoiceOut(invoice, clientsAndAddresses); }) .then(() => super.responseHandler(response)) From b06f5b72468622606fa104164d14ce30d038124c Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 21 Oct 2022 10:06:34 +0200 Subject: [PATCH 25/40] feat: cuando seleccionas 'allClients' no tiene en cuenta el 'clientsRange' --- .../front/index/global-invoicing/index.html | 16 ++++++++++++++-- .../front/index/global-invoicing/index.js | 4 ++-- .../front/index/global-invoicing/locale/es.yml | 3 ++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.html b/modules/invoiceOut/front/index/global-invoicing/index.html index 7f96397f5..c2d1c4304 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.html +++ b/modules/invoiceOut/front/index/global-invoicing/index.html @@ -13,7 +13,16 @@ url="Companies" data="companies" order="code"> - + +
+ +
+ {{'Calculating packages to invoice...' | translate}} +
+
+
@@ -40,7 +49,8 @@ + ng-model="$ctrl.clientsNumber" + ng-click="$ctrl.$onInit()"> @@ -62,6 +73,7 @@ url="Clients" label="To client" search-function="{or: [{id: $search}, {name: {like: '%'+$search+'%'}}]}" + order="id" show-field="name" value-field="id" ng-model="$ctrl.invoice.toClientId"> diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 1390f52e8..39dc31137 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -92,10 +92,10 @@ class Controller extends Dialog { }); this.$.invoiceButton.disabled = true; - if (this.clientsNumber == 'allClients') this.getMaxClientId(); - + this.packageInvoicing = true; this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) .then(res => { + this.packageInvoicing = false; const invoice = res.data.invoice; const clientsAndAddresses = res.data.clientsAndAddresses; if (!clientsAndAddresses.length) return super.responseHandler(response); diff --git a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml index 8ac2cc13a..af151684f 100644 --- a/modules/invoiceOut/front/index/global-invoicing/locale/es.yml +++ b/modules/invoiceOut/front/index/global-invoicing/locale/es.yml @@ -10,4 +10,5 @@ Choose a valid clients range: Selecciona un rango válido de clientes of: de Id Client: Id Cliente All clients: Todos los clientes -Clients range: Rango de clientes \ No newline at end of file +Clients range: Rango de clientes +Calculating packages to invoice...: Calculando paquetes a factura... \ No newline at end of file From 0f57a40aac1831c0b3a35b4d4f459a87e030ad09 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 21 Oct 2022 10:50:18 +0200 Subject: [PATCH 26/40] fix: backTest --- .../invoiceOut/front/index/global-invoicing/index.spec.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/modules/invoiceOut/front/index/global-invoicing/index.spec.js b/modules/invoiceOut/front/index/global-invoicing/index.spec.js index 04ea99e37..7dcc51e59 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.spec.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.spec.js @@ -87,10 +87,7 @@ describe('InvoiceOut', () => { it('should make an http POST query and then call to the showSuccess() method', () => { jest.spyOn(controller.vnApp, 'showSuccess'); - const filter = { - order: 'id DESC', - limit: 1 - }; + const minShipped = new Date(); minShipped.setFullYear(minShipped.getFullYear() - 1); minShipped.setMonth(1); @@ -109,8 +106,6 @@ describe('InvoiceOut', () => { invoice: controller.invoice }; - const serializedParams = $httpParamSerializer({filter}); - $httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112}); $httpBackend.expect('POST', `InvoiceOuts/clientToInvoice`).respond(response); $httpBackend.expect('POST', `InvoiceOuts/invoiceClient`).respond({id: 1}); controller.responseHandler('accept'); From 350c516bb12c5589ca9cc9e96a67d4a2f779ca36 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 26 Oct 2022 10:36:41 +0200 Subject: [PATCH 27/40] =?UTF-8?q?feat:=20cancelar=20petici=C3=B3n=20cuando?= =?UTF-8?q?=20esta=20calculando=20el=20packaging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/changes/10491-august/00-aclInvoiceOut.sql | 2 +- .../{clientToInvoice.js => clientsToInvoice.js} | 6 +++--- modules/invoiceOut/back/models/invoice-out.js | 2 +- .../front/index/global-invoicing/index.js | 15 ++++++++++----- .../front/index/global-invoicing/index.spec.js | 2 +- 5 files changed, 16 insertions(+), 11 deletions(-) rename modules/invoiceOut/back/methods/invoiceOut/{clientToInvoice.js => clientsToInvoice.js} (97%) diff --git a/db/changes/10491-august/00-aclInvoiceOut.sql b/db/changes/10491-august/00-aclInvoiceOut.sql index bc2b87cdb..7715045b5 100644 --- a/db/changes/10491-august/00-aclInvoiceOut.sql +++ b/db/changes/10491-august/00-aclInvoiceOut.sql @@ -1,6 +1,6 @@ INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES - ('InvoiceOut', 'clientToInvoice', 'WRITE', 'ALLOW', 'ROLE', 'invoicing'); + ('InvoiceOut', 'clientsToInvoice', 'WRITE', 'ALLOW', 'ROLE', 'invoicing'); INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES diff --git a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js b/modules/invoiceOut/back/methods/invoiceOut/clientsToInvoice.js similarity index 97% rename from modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js rename to modules/invoiceOut/back/methods/invoiceOut/clientsToInvoice.js index a0578897a..d42184ae5 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/clientToInvoice.js +++ b/modules/invoiceOut/back/methods/invoiceOut/clientsToInvoice.js @@ -1,5 +1,5 @@ module.exports = Self => { - Self.remoteMethodCtx('clientToInvoice', { + Self.remoteMethodCtx('clientsToInvoice', { description: 'Get the clients to make global invoicing', accessType: 'WRITE', accepts: [ @@ -38,12 +38,12 @@ module.exports = Self => { type: 'object' }], http: { - path: '/clientToInvoice', + path: '/clientsToInvoice', verb: 'POST' } }); - Self.clientToInvoice = async(ctx, options) => { + Self.clientsToInvoice = async(ctx, options) => { const args = ctx.args; let tx; const myOptions = {}; diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index 20d9af939..d363ab835 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -7,7 +7,7 @@ module.exports = Self => { require('../methods/invoiceOut/book')(Self); require('../methods/invoiceOut/createPdf')(Self); require('../methods/invoiceOut/createManualInvoice')(Self); - require('../methods/invoiceOut/clientToInvoice')(Self); + require('../methods/invoiceOut/clientsToInvoice')(Self); require('../methods/invoiceOut/invoiceClient')(Self); require('../methods/invoiceOut/refund')(Self); require('../methods/invoiceOut/invoiceEmail')(Self); diff --git a/modules/invoiceOut/front/index/global-invoicing/index.js b/modules/invoiceOut/front/index/global-invoicing/index.js index 39dc31137..f772a4936 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.js @@ -50,6 +50,11 @@ class Controller extends Dialog { this.$.invoiceButton.disabled = false; } + cancelRequest() { + this.canceler = this.$q.defer(); + return {timeout: this.canceler.promise}; + } + invoiceOut(invoice, clientsAndAddresses) { const [clientAndAddress] = clientsAndAddresses; if (!clientAndAddress) return; @@ -63,10 +68,8 @@ class Controller extends Dialog { minShipped: invoice.minShipped, }; - this.canceler = this.$q.defer(); - const options = { - timeout: this.canceler.promise - }; + + const options = this.cancelRequest(); return this.$http.post(`InvoiceOuts/invoiceClient`, params, options) .then(() => { @@ -93,7 +96,9 @@ class Controller extends Dialog { this.$.invoiceButton.disabled = true; this.packageInvoicing = true; - this.$http.post(`InvoiceOuts/clientToInvoice`, this.invoice) + const options = this.cancelRequest(); + + this.$http.post(`InvoiceOuts/clientsToInvoice`, this.invoice, options) .then(res => { this.packageInvoicing = false; const invoice = res.data.invoice; diff --git a/modules/invoiceOut/front/index/global-invoicing/index.spec.js b/modules/invoiceOut/front/index/global-invoicing/index.spec.js index 7dcc51e59..e88b0b1d4 100644 --- a/modules/invoiceOut/front/index/global-invoicing/index.spec.js +++ b/modules/invoiceOut/front/index/global-invoicing/index.spec.js @@ -106,7 +106,7 @@ describe('InvoiceOut', () => { invoice: controller.invoice }; - $httpBackend.expect('POST', `InvoiceOuts/clientToInvoice`).respond(response); + $httpBackend.expect('POST', `InvoiceOuts/clientsToInvoice`).respond(response); $httpBackend.expect('POST', `InvoiceOuts/invoiceClient`).respond({id: 1}); controller.responseHandler('accept'); $httpBackend.flush(); From dbd0e77e12c5f5fc0bc620dea0e01d2421d42557 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 9 Nov 2022 09:14:32 +0100 Subject: [PATCH 28/40] feat: send mail with the invoice pdf --- .../back/methods/invoiceOut/invoiceClient.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index ec3244344..c3ad9508a 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -109,9 +109,6 @@ module.exports = Self => { if (newInvoice.id) { await Self.rawSql('CALL invoiceOutBooking(?)', [newInvoice.id], myOptions); - query = `INSERT IGNORE INTO invoiceOutQueue(invoiceFk) VALUES(?)`; - await Self.rawSql(query, [newInvoice.id], myOptions); - invoiceId = newInvoice.id; } } catch (e) { @@ -128,6 +125,21 @@ module.exports = Self => { throw e; } + const invoiceOut = await models.InvoiceOut.findById(invoiceId, { + include: { + relation: 'client' + } + }); + + try { + ctx.args = { + reference: invoiceOut.ref, + recipientId: invoiceOut.clientFk, + recipient: invoiceOut.client().email + }; + await models.InvoiceOut.invoiceEmail(ctx); + } catch (err) {} + return invoiceId; }; From 2ab475791c03a7b53bb187ac36fcc2e44eb3fc05 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 9 Nov 2022 09:16:58 +0100 Subject: [PATCH 29/40] refactor code --- .../back/methods/invoiceOut/invoiceClient.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index c3ad9508a..7c8742caa 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -130,13 +130,12 @@ module.exports = Self => { relation: 'client' } }); - + ctx.args = { + reference: invoiceOut.ref, + recipientId: invoiceOut.clientFk, + recipient: invoiceOut.client().email + }; try { - ctx.args = { - reference: invoiceOut.ref, - recipientId: invoiceOut.clientFk, - recipient: invoiceOut.client().email - }; await models.InvoiceOut.invoiceEmail(ctx); } catch (err) {} From 79e9edcee959f42044b7328932e5a5970a861f8c Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 9 Nov 2022 09:26:28 +0100 Subject: [PATCH 30/40] delete: old code --- .../00-deleteInvoiceOutQueue.sql | 1 + .../back/methods/invoiceOut/sendQueued.js | 133 ------------------ modules/invoiceOut/back/models/invoice-out.js | 1 - 3 files changed, 1 insertion(+), 134 deletions(-) create mode 100644 db/changes/10502-november/00-deleteInvoiceOutQueue.sql delete mode 100644 modules/invoiceOut/back/methods/invoiceOut/sendQueued.js diff --git a/db/changes/10502-november/00-deleteInvoiceOutQueue.sql b/db/changes/10502-november/00-deleteInvoiceOutQueue.sql new file mode 100644 index 000000000..87a3059a0 --- /dev/null +++ b/db/changes/10502-november/00-deleteInvoiceOutQueue.sql @@ -0,0 +1 @@ +DROP TABLE `vn`.`invoiceOutQueue`; \ No newline at end of file diff --git a/modules/invoiceOut/back/methods/invoiceOut/sendQueued.js b/modules/invoiceOut/back/methods/invoiceOut/sendQueued.js deleted file mode 100644 index a1730ac81..000000000 --- a/modules/invoiceOut/back/methods/invoiceOut/sendQueued.js +++ /dev/null @@ -1,133 +0,0 @@ -const {Email, Report, storage} = require('vn-print'); - -module.exports = Self => { - Self.remoteMethod('sendQueued', { - description: 'Send all queued invoices', - accessType: 'WRITE', - accepts: [], - returns: { - type: 'object', - root: true - }, - http: { - path: '/send-queued', - verb: 'POST' - } - }); - - Self.sendQueued = async() => { - const invoices = await Self.rawSql(` - SELECT - io.id, - io.clientFk, - io.issued, - io.ref, - c.email recipient, - c.salesPersonFk, - c.isToBeMailed, - c.hasToInvoice, - co.hasDailyInvoice, - eu.email salesPersonEmail - FROM invoiceOutQueue ioq - JOIN invoiceOut io ON io.id = ioq.invoiceFk - JOIN client c ON c.id = io.clientFk - JOIN province p ON p.id = c.provinceFk - JOIN country co ON co.id = p.countryFk - LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk - WHERE status = ''`); - - let invoiceId; - for (const invoiceOut of invoices) { - try { - const tx = await Self.beginTransaction({}); - const myOptions = {transaction: tx}; - - invoiceId = invoiceOut.id; - - const args = { - reference: invoiceOut.ref, - recipientId: invoiceOut.clientFk, - recipient: invoiceOut.recipient, - replyTo: invoiceOut.salesPersonEmail - }; - - const invoiceReport = new Report('invoice', args); - const stream = await invoiceReport.toPdfStream(); - - const issued = invoiceOut.issued; - const year = issued.getFullYear().toString(); - const month = (issued.getMonth() + 1).toString(); - const day = issued.getDate().toString(); - - const fileName = `${year}${invoiceOut.ref}.pdf`; - - // Store invoice - storage.write(stream, { - type: 'invoice', - path: `${year}/${month}/${day}`, - fileName: fileName - }); - - await Self.rawSql(` - UPDATE invoiceOut - SET hasPdf = true - WHERE id = ?`, - [invoiceOut.id], myOptions); - - const isToBeMailed = invoiceOut.recipient && invoiceOut.salesPersonFk && invoiceOut.isToBeMailed; - - if (isToBeMailed) { - const mailOptions = { - overrideAttachments: true, - attachments: [] - }; - - const invoiceAttachment = { - filename: fileName, - content: stream - }; - - if (invoiceOut.serial == 'E' && invoiceOut.companyCode == 'VNL') { - const exportation = new Report('exportation', args); - const stream = await exportation.toPdfStream(); - const fileName = `CITES-${invoiceOut.ref}.pdf`; - - mailOptions.attachments.push({ - filename: fileName, - content: stream - }); - } - - mailOptions.attachments.push(invoiceAttachment); - - const email = new Email('invoice', args); - await email.send(mailOptions); - } - // Update queue status - const date = new Date(); - await Self.rawSql(` - UPDATE invoiceOutQueue - SET status = "printed", - printed = ? - WHERE invoiceFk = ?`, - [date, invoiceOut.id], myOptions); - - await tx.commit(); - } catch (error) { - await tx.rollback(); - - await Self.rawSql(` - UPDATE invoiceOutQueue - SET status = ? - WHERE invoiceFk = ?`, - [error.message, invoiceId]); - - throw e; - } - } - - return { - message: 'Success' - }; - }; -}; diff --git a/modules/invoiceOut/back/models/invoice-out.js b/modules/invoiceOut/back/models/invoice-out.js index 3648ba849..88adae2ef 100644 --- a/modules/invoiceOut/back/models/invoice-out.js +++ b/modules/invoiceOut/back/models/invoice-out.js @@ -13,7 +13,6 @@ module.exports = Self => { require('../methods/invoiceOut/refund')(Self); require('../methods/invoiceOut/invoiceEmail')(Self); require('../methods/invoiceOut/exportationPdf')(Self); - require('../methods/invoiceOut/sendQueued')(Self); require('../methods/invoiceOut/invoiceCsv')(Self); require('../methods/invoiceOut/invoiceCsvEmail')(Self); }; From 574f2757f4414ac2b062e322f29debd30f42a145 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 9 Nov 2022 10:19:27 +0100 Subject: [PATCH 31/40] refactor code --- .../back/methods/invoiceOut/invoiceClient.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index 7c8742caa..6667b5f0e 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -63,6 +63,7 @@ module.exports = Self => { } let invoiceId; + let invoiceOut; try { const client = await models.Client.findById(args.clientId, { fields: ['id', 'hasToInvoiceByAddress'] @@ -119,25 +120,24 @@ module.exports = Self => { await notifyFailures(ctx, failedClient, myOptions); } + invoiceOut = await models.InvoiceOut.findById(invoiceId, { + include: { + relation: 'client' + } + }, myOptions); + if (tx) await tx.commit(); } catch (e) { if (tx) await tx.rollback(); throw e; } - const invoiceOut = await models.InvoiceOut.findById(invoiceId, { - include: { - relation: 'client' - } - }); ctx.args = { reference: invoiceOut.ref, recipientId: invoiceOut.clientFk, recipient: invoiceOut.client().email }; - try { - await models.InvoiceOut.invoiceEmail(ctx); - } catch (err) {} + await models.InvoiceOut.invoiceEmail(ctx); return invoiceId; }; From 1ea5ffa6d30a33ccfc40d8c05727e6a69de220cf Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 9 Nov 2022 12:04:19 +0100 Subject: [PATCH 32/40] fix: backTest --- modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js | 4 +++- .../back/methods/invoiceOut/specs/invoiceClient.spec.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js index 6667b5f0e..b04747d16 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceClient.js @@ -137,7 +137,9 @@ module.exports = Self => { recipientId: invoiceOut.clientFk, recipient: invoiceOut.client().email }; - await models.InvoiceOut.invoiceEmail(ctx); + try { + await models.InvoiceOut.invoiceEmail(ctx); + } catch (err) {} return invoiceId; }; diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js index f1f9b7037..fdf50cbd4 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js @@ -12,6 +12,9 @@ describe('InvoiceOut invoiceClient()', () => { minShipped.setHours(0, 0, 0, 0); const invoiceSerial = 'A'; const activeCtx = { + getLocale: () => { + return 'en'; + }, accessToken: {userId: userId}, __: value => { return value; From 0bd09ae366dd23b49c19a0f038b420f416357f05 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 9 Nov 2022 12:06:51 +0100 Subject: [PATCH 33/40] fix: delete unnecessary column --- modules/invoiceOut/front/index/index.html | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/invoiceOut/front/index/index.html b/modules/invoiceOut/front/index/index.html index 632dbf90b..e2cf2120a 100644 --- a/modules/invoiceOut/front/index/index.html +++ b/modules/invoiceOut/front/index/index.html @@ -30,7 +30,6 @@ Company Due date - From 7e50f23563ddb9a96051aa98eba71a73585ded93 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 11 Nov 2022 08:55:56 +0100 Subject: [PATCH 34/40] refacor code --- .../{10491-august => 10503-november}/00-aclInvoiceOut.sql | 0 .../00-deleteInvoiceOutQueue.sql | 0 db/dump/fixtures.sql | 4 ---- .../back/methods/invoiceOut/specs/invoiceClient.spec.js | 1 + print/templates/email/invoice/invoice.js | 3 ++- 5 files changed, 3 insertions(+), 5 deletions(-) rename db/changes/{10491-august => 10503-november}/00-aclInvoiceOut.sql (100%) rename db/changes/{10502-november => 10503-november}/00-deleteInvoiceOutQueue.sql (100%) diff --git a/db/changes/10491-august/00-aclInvoiceOut.sql b/db/changes/10503-november/00-aclInvoiceOut.sql similarity index 100% rename from db/changes/10491-august/00-aclInvoiceOut.sql rename to db/changes/10503-november/00-aclInvoiceOut.sql diff --git a/db/changes/10502-november/00-deleteInvoiceOutQueue.sql b/db/changes/10503-november/00-deleteInvoiceOutQueue.sql similarity index 100% rename from db/changes/10502-november/00-deleteInvoiceOutQueue.sql rename to db/changes/10503-november/00-deleteInvoiceOutQueue.sql diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index f7c7526ca..29b75602b 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2699,10 +2699,6 @@ INSERT INTO `util`.`notificationSubscription` (`notificationFk`, `userFk`) INSERT INTO `vn`.`routeConfig` (`id`, `defaultWorkCenterFk`) VALUES (1, 9); - -INSERT INTO `vn`.`report` (`id`, `name`) - VALUES - (3, 'invoice'); INSERT INTO `vn`.`productionConfig` (`isPreviousPreparationRequired`, `ticketPrintedMax`, `ticketTrolleyMax`, `rookieDays`, `notBuyingMonths`, `id`, `isZoneClosedByExpeditionActivated`, `maxNotReadyCollections`, `minTicketsToCloseZone`, `movingTicketDelRoute`, `defaultZone`, `defautlAgencyMode`, `hasUniqueCollectionTime`, `maxCollectionWithoutUser`, `pendingCollectionsOrder`, `pendingCollectionsAge`) VALUES diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js index fdf50cbd4..5f890de26 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/invoiceClient.spec.js @@ -24,6 +24,7 @@ describe('InvoiceOut invoiceClient()', () => { it('should make a global invoicing', async() => { spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true))); + spyOn(models.InvoiceOut, 'invoiceEmail'); const tx = await models.InvoiceOut.beginTransaction({}); const options = {transaction: tx}; diff --git a/print/templates/email/invoice/invoice.js b/print/templates/email/invoice/invoice.js index 8d3dcaea7..4bf0f7d66 100755 --- a/print/templates/email/invoice/invoice.js +++ b/print/templates/email/invoice/invoice.js @@ -4,6 +4,7 @@ module.exports = { name: 'invoice', async serverPrefetch() { this.invoice = await this.fetchInvoice(this.reference); + console.log('this.invoice: ', this.invoice); }, methods: { fetchInvoice(reference) { @@ -15,7 +16,7 @@ module.exports = { }, props: { reference: { - type: Number, + type: String, required: true } } From a30f50ffe5c956c5c1e647b737d156670ae085d4 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 14 Nov 2022 12:20:27 +0100 Subject: [PATCH 35/40] refactor: borrado console.log --- print/templates/email/invoice/invoice.js | 1 - 1 file changed, 1 deletion(-) diff --git a/print/templates/email/invoice/invoice.js b/print/templates/email/invoice/invoice.js index 4bf0f7d66..cb90e461f 100755 --- a/print/templates/email/invoice/invoice.js +++ b/print/templates/email/invoice/invoice.js @@ -4,7 +4,6 @@ module.exports = { name: 'invoice', async serverPrefetch() { this.invoice = await this.fetchInvoice(this.reference); - console.log('this.invoice: ', this.invoice); }, methods: { fetchInvoice(reference) { From 386e304958743c90f9d82ea267023002231cdb28 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 14 Nov 2022 14:52:08 +0100 Subject: [PATCH 36/40] =?UTF-8?q?fix:=20modelo=20incorreto=20para=20la=20t?= =?UTF-8?q?ransacci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../back/methods/invoiceOut/specs/downloadZip.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/specs/downloadZip.spec.js b/modules/invoiceOut/back/methods/invoiceOut/specs/downloadZip.spec.js index 7a9e184ea..08f049783 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/specs/downloadZip.spec.js +++ b/modules/invoiceOut/back/methods/invoiceOut/specs/downloadZip.spec.js @@ -13,7 +13,7 @@ describe('InvoiceOut downloadZip()', () => { }; it('should return part of link to dowloand the zip', async() => { - const tx = await models.Order.beginTransaction({}); + const tx = await models.InvoiceOut.beginTransaction({}); try { const options = {transaction: tx}; @@ -30,7 +30,7 @@ describe('InvoiceOut downloadZip()', () => { }); it('should return an error if the size of the files is too large', async() => { - const tx = await models.Order.beginTransaction({}); + const tx = await models.InvoiceOut.beginTransaction({}); let error; try { From 216108a5760aca6692489feae6fb13ac2da46eca Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 14 Nov 2022 15:01:22 +0100 Subject: [PATCH 37/40] refactor --- modules/invoiceOut/front/index/index.html | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/invoiceOut/front/index/index.html b/modules/invoiceOut/front/index/index.html index 632dbf90b..e2cf2120a 100644 --- a/modules/invoiceOut/front/index/index.html +++ b/modules/invoiceOut/front/index/index.html @@ -30,7 +30,6 @@ Company Due date - From 0637050375af785da6f500dadef4c4edd7c706e5 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 14 Nov 2022 15:11:33 +0100 Subject: [PATCH 38/40] fix: frontTest --- modules/ticket/front/expedition/index.spec.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ticket/front/expedition/index.spec.js b/modules/ticket/front/expedition/index.spec.js index b95d64fa3..5a538b1c8 100644 --- a/modules/ticket/front/expedition/index.spec.js +++ b/modules/ticket/front/expedition/index.spec.js @@ -76,9 +76,10 @@ describe('Ticket', () => { it('should make a query and then call to the $state go() method', () => { jest.spyOn(controller.$state, 'go').mockReturnThis(); + const landed = new Date(); const ticket = { clientFk: 1101, - landed: new Date(), + landed: landed, addressFk: 121, agencyModeFk: 1, warehouseFk: 1 @@ -90,7 +91,7 @@ describe('Ticket', () => { const expectedParams = { clientId: 1101, - landed: new Date(), + landed: landed, warehouseId: 1, addressId: 121, agencyModeId: 1, From 63142a886142e0598cc07e0b7cf56f691ed1aace Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 14 Nov 2022 15:16:21 +0100 Subject: [PATCH 39/40] refactor --- modules/invoiceOut/front/summary/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/invoiceOut/front/summary/index.html b/modules/invoiceOut/front/summary/index.html index a0d050efd..b83775158 100644 --- a/modules/invoiceOut/front/summary/index.html +++ b/modules/invoiceOut/front/summary/index.html @@ -85,7 +85,7 @@ {{ticket.shipped | date: 'dd/MM/yyyy' | dashIfEmpty}} - {{ticket.totalWithVat | currency: 'EUR': 2}} + {{ticket.totalWithVat | currency: 'EUR': 2}} From 174dd1fe5c71753f5188f52f66a86e0124603926 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 15 Nov 2022 10:00:21 +0100 Subject: [PATCH 40/40] feat(invoice): send invoice from stored PDF - closes #4811 --- .../back/methods/invoiceOut/invoiceEmail.js | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js index 83564e3ab..83cb84881 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js @@ -40,19 +40,40 @@ module.exports = Self => { } }); - Self.invoiceEmail = async ctx => { + Self.invoiceEmail = async(ctx, reference) => { const args = Object.assign({}, ctx.args); + const {InvoiceOut} = Self.app.models; const params = { recipient: args.recipient, lang: ctx.req.getLocale() }; + const invoiceOut = await InvoiceOut.findOne({ + where: { + ref: reference + } + }); + delete args.ctx; for (const param in args) params[param] = args[param]; const email = new Email('invoice', params); - return email.send(); + const [stream, ...headers] = await Self.download(ctx, invoiceOut.id); + const name = headers[1]; + const fileName = name.replace(/filename="(.*)"/gm, '$1'); + + const mailOptions = { + overrideAttachments: true, + attachments: [ + { + filename: fileName, + content: stream + } + ] + }; + + return email.send(mailOptions); }; };