From 4cdbb85e30bad4b6eb11bb3f21c66547f3f47633 Mon Sep 17 00:00:00 2001 From: jgallego Date: Wed, 20 Jan 2021 13:53:49 +0100 Subject: [PATCH 1/4] primer comit --- .../ticket/back/methods/ticket/makeInvoice.js | 14 +++++++ .../back/models/print-server-queue.json | 39 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 modules/ticket/back/models/print-server-queue.json diff --git a/modules/ticket/back/methods/ticket/makeInvoice.js b/modules/ticket/back/methods/ticket/makeInvoice.js index 226eebf97..fabb2a582 100644 --- a/modules/ticket/back/methods/ticket/makeInvoice.js +++ b/modules/ticket/back/methods/ticket/makeInvoice.js @@ -51,6 +51,20 @@ module.exports = function(Self) { let serial = result.serial; let stmts = []; + await Self.rawSql('CALL invoiceFromTicket(?)', [id], options); + await Self.rawSql('CALL invoiceOut_new(?, CURDATE(), null, @invoiceId)', [serial], options); + + + let [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', options); + let invoiceId = resultInvoice.id; + + if serial != 'R' && invoiceId && invoiceId > 0 + { + await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], options); + + INSERT INTO printServerQueue(priorityFk, reportFk, param1, workerFk) VALUES(1, 3, vInvoiceId, account.myUser_getId()); + + stmt = new ParameterizedSQL('CALL vn.invoiceOut_newFromTicket(?, ?, ?, @invoiceId)', [ ticket.id, serial, diff --git a/modules/ticket/back/models/print-server-queue.json b/modules/ticket/back/models/print-server-queue.json new file mode 100644 index 000000000..4dcb58b62 --- /dev/null +++ b/modules/ticket/back/models/print-server-queue.json @@ -0,0 +1,39 @@ +{ + "name": "PrintServerQueue", + "description": "Print server queue", + "base": "VnModel", + "options": { + "mysql": { + "table": "printServerQueue" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + }, + "priorityFk": { + "type": "number" + }, + "reportFk": { + "type": "number", + "required": true + }, + "param1": { + "type": "number" + }, + "workerFk": { + "type": "number", + + } + }, + "acls": [ + { + "accessType": "READ", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + } + ] +} \ No newline at end of file -- 2.40.1 From 3dc50e8a16964304aa6cdf537dd1ce0c2e6e9122 Mon Sep 17 00:00:00 2001 From: jgallego Date: Thu, 21 Jan 2021 13:39:40 +0100 Subject: [PATCH 2/4] test ok --- db/changes/10271-wisemen/.keep | 2 +- db/changes/10271-wisemen/00-ACL.sql | 2 + .../ticket/back/methods/ticket/makeInvoice.js | 49 +++++++------------ modules/ticket/back/model-config.json | 3 ++ .../back/models/print-server-queue.json | 22 +++------ 5 files changed, 31 insertions(+), 47 deletions(-) create mode 100644 db/changes/10271-wisemen/00-ACL.sql diff --git a/db/changes/10271-wisemen/.keep b/db/changes/10271-wisemen/.keep index d955e6ec8..49ab40216 100644 --- a/db/changes/10271-wisemen/.keep +++ b/db/changes/10271-wisemen/.keep @@ -1 +1 @@ -12271-wisemen \ No newline at end of file +10271-wisemen \ No newline at end of file diff --git a/db/changes/10271-wisemen/00-ACL.sql b/db/changes/10271-wisemen/00-ACL.sql new file mode 100644 index 000000000..5c81ab91b --- /dev/null +++ b/db/changes/10271-wisemen/00-ACL.sql @@ -0,0 +1,2 @@ +INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) + VALUES ('printServerQueue', '*', 'WRITE', 'ALLOW', 'ROLE', 'employee'); diff --git a/modules/ticket/back/methods/ticket/makeInvoice.js b/modules/ticket/back/methods/ticket/makeInvoice.js index fabb2a582..45e7a615c 100644 --- a/modules/ticket/back/methods/ticket/makeInvoice.js +++ b/modules/ticket/back/methods/ticket/makeInvoice.js @@ -1,5 +1,4 @@ const UserError = require('vn-loopback/util/user-error'); -const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; module.exports = function(Self) { Self.remoteMethodCtx('makeInvoice', { @@ -26,7 +25,6 @@ module.exports = function(Self) { }); Self.makeInvoice = async(ctx, id) => { - const conn = Self.dataSource.connector; let userId = ctx.req.accessToken.userId; let models = Self.app.models; let tx = await Self.beginTransaction({}); @@ -45,47 +43,36 @@ module.exports = function(Self) { if (!ticketCanBeInvoiced) throw new UserError(`This ticket can't be invoiced`); - let query = `SELECT vn.invoiceSerial(?, ?, ?) AS serial`; let [result] = await Self.rawSql(query, [ticket.clientFk, ticket.companyFk, 'R'], options); let serial = result.serial; - let stmts = []; await Self.rawSql('CALL invoiceFromTicket(?)', [id], options); await Self.rawSql('CALL invoiceOut_new(?, CURDATE(), null, @invoiceId)', [serial], options); - - - let [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', options); + + let [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], options); + let invoiceId = resultInvoice.id; - if serial != 'R' && invoiceId && invoiceId > 0 - { - await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], options); - - INSERT INTO printServerQueue(priorityFk, reportFk, param1, workerFk) VALUES(1, 3, vInvoiceId, account.myUser_getId()); - + let ticketInvoice = await models.Ticket.findById(id, {fields: ['refFk']}, options); - stmt = new ParameterizedSQL('CALL vn.invoiceOut_newFromTicket(?, ?, ?, @invoiceId)', [ - ticket.id, - serial, - null - ]); - stmts.push(stmt); + await models.TicketLog.create({ + originFk: ticket.id, + userFk: userId, + action: 'insert', + changedModel: 'Ticket', + changedModelId: ticket.id, + newInstance: ticketInvoice + }, options); - let invoiceIndex = stmts.push(`SELECT @invoiceId AS invoiceId`) - 1; - - let sql = ParameterizedSQL.join(stmts, ';'); - result = await conn.executeStmt(sql); - let invoiceId = result[invoiceIndex][0].invoiceId; if (serial != 'R' && invoiceId) { - query = `CALL vn.invoiceOutBooking(?)`; - await Self.rawSql(query, [invoiceId], options); + await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], options); + await models.PrintServerQueue.create({ + reportFk: 3, // Tarea #2734 (Nueva): crear informe facturas + param1: invoiceId, + workerFk: userId + }, options); } - - let user = await models.Worker.findOne({where: {userFk: userId}}, options); - - query = `INSERT INTO printServerQueue(reportFk, param1, workerFk) VALUES (?, ?, ?)`; - await Self.rawSql(query, [3, invoiceId, user.id], options); await tx.commit(); return {invoiceFk: invoiceId, serial}; diff --git a/modules/ticket/back/model-config.json b/modules/ticket/back/model-config.json index ac6f4f100..5d5f08694 100644 --- a/modules/ticket/back/model-config.json +++ b/modules/ticket/back/model-config.json @@ -17,6 +17,9 @@ "Packaging": { "dataSource": "vn" }, + "PrintServerQueue": { + "dataSource": "vn" + }, "Sale": { "dataSource": "vn" }, diff --git a/modules/ticket/back/models/print-server-queue.json b/modules/ticket/back/models/print-server-queue.json index 4dcb58b62..fc1a972f0 100644 --- a/modules/ticket/back/models/print-server-queue.json +++ b/modules/ticket/back/models/print-server-queue.json @@ -9,31 +9,23 @@ }, "properties": { "id": { - "type": "number", + "type": "Number", "id": true, "description": "Identifier" }, "priorityFk": { - "type": "number" + "type": "Number" }, "reportFk": { - "type": "number", + "type": "Number", "required": true }, "param1": { - "type": "number" + "type": "Number" }, "workerFk": { - "type": "number", - + "type": "Number", + "required": true } - }, - "acls": [ - { - "accessType": "READ", - "principalType": "ROLE", - "principalId": "$everyone", - "permission": "ALLOW" - } - ] + } } \ No newline at end of file -- 2.40.1 From 7bdfb36f25f0e7cbc6b7f9f58342be846728092f Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 22 Jan 2021 14:30:46 +0100 Subject: [PATCH 3/4] minor refactor --- db/changes/10271-wisemen/00-ACL.sql | 2 +- .../ticket/back/methods/ticket/makeInvoice.js | 28 +++++++++---------- .../back/models/print-server-queue.json | 10 +++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/db/changes/10271-wisemen/00-ACL.sql b/db/changes/10271-wisemen/00-ACL.sql index 1c1a64a30..e4b84a3e4 100644 --- a/db/changes/10271-wisemen/00-ACL.sql +++ b/db/changes/10271-wisemen/00-ACL.sql @@ -1,3 +1,3 @@ INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) - VALUES ('printServerQueue', '*', 'WRITE', 'ALLOW', 'ROLE', 'employee'); + VALUES ('PrintServerQueue', '*', 'WRITE', 'ALLOW', 'ROLE', 'employee'); INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ('FixedPrice', '*', '*', 'ALLOW', 'ROLE', 'buyer'); diff --git a/modules/ticket/back/methods/ticket/makeInvoice.js b/modules/ticket/back/methods/ticket/makeInvoice.js index 45e7a615c..29099e379 100644 --- a/modules/ticket/back/methods/ticket/makeInvoice.js +++ b/modules/ticket/back/methods/ticket/makeInvoice.js @@ -25,36 +25,36 @@ module.exports = function(Self) { }); Self.makeInvoice = async(ctx, id) => { - let userId = ctx.req.accessToken.userId; - let models = Self.app.models; - let tx = await Self.beginTransaction({}); + const userId = ctx.req.accessToken.userId; + const models = Self.app.models; + const tx = await Self.beginTransaction({}); try { - let options = {transaction: tx}; + const options = {transaction: tx}; - let filter = {fields: ['id', 'clientFk', 'companyFk']}; - let ticket = await models.Ticket.findById(id, filter, options); + const filter = {fields: ['id', 'clientFk', 'companyFk']}; + const ticket = await models.Ticket.findById(id, filter, options); - let clientCanBeInvoiced = await models.Client.canBeInvoiced(ticket.clientFk); + const clientCanBeInvoiced = await models.Client.canBeInvoiced(ticket.clientFk); if (!clientCanBeInvoiced) throw new UserError(`This client can't be invoiced`); - let ticketCanBeInvoiced = await models.Ticket.canBeInvoiced(ticket.id); + const ticketCanBeInvoiced = await models.Ticket.canBeInvoiced(ticket.id); if (!ticketCanBeInvoiced) throw new UserError(`This ticket can't be invoiced`); - let query = `SELECT vn.invoiceSerial(?, ?, ?) AS serial`; - let [result] = await Self.rawSql(query, [ticket.clientFk, ticket.companyFk, 'R'], options); - let serial = result.serial; + const query = `SELECT vn.invoiceSerial(?, ?, ?) AS serial`; + const [result] = await Self.rawSql(query, [ticket.clientFk, ticket.companyFk, 'R'], options); + const serial = result.serial; await Self.rawSql('CALL invoiceFromTicket(?)', [id], options); await Self.rawSql('CALL invoiceOut_new(?, CURDATE(), null, @invoiceId)', [serial], options); - let [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], options); + const [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], options); - let invoiceId = resultInvoice.id; + const invoiceId = resultInvoice.id; - let ticketInvoice = await models.Ticket.findById(id, {fields: ['refFk']}, options); + const ticketInvoice = await models.Ticket.findById(id, {fields: ['refFk']}, options); await models.TicketLog.create({ originFk: ticket.id, diff --git a/modules/ticket/back/models/print-server-queue.json b/modules/ticket/back/models/print-server-queue.json index fc1a972f0..fef7f3b3e 100644 --- a/modules/ticket/back/models/print-server-queue.json +++ b/modules/ticket/back/models/print-server-queue.json @@ -9,22 +9,22 @@ }, "properties": { "id": { - "type": "Number", + "type": "number", "id": true, "description": "Identifier" }, "priorityFk": { - "type": "Number" + "type": "number" }, "reportFk": { - "type": "Number", + "type": "number", "required": true }, "param1": { - "type": "Number" + "type": "number" }, "workerFk": { - "type": "Number", + "type": "number", "required": true } } -- 2.40.1 From e1711af265a5da31133373854c076f615f3b4004 Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 22 Jan 2021 14:32:35 +0100 Subject: [PATCH 4/4] sin keep --- db/changes/10271-wisemen/.keep | 1 - 1 file changed, 1 deletion(-) delete mode 100644 db/changes/10271-wisemen/.keep diff --git a/db/changes/10271-wisemen/.keep b/db/changes/10271-wisemen/.keep deleted file mode 100644 index 49ab40216..000000000 --- a/db/changes/10271-wisemen/.keep +++ /dev/null @@ -1 +0,0 @@ -10271-wisemen \ No newline at end of file -- 2.40.1