diff --git a/db/changes/10271-wisemen/.keep b/db/changes/10271-wisemen/.keep deleted file mode 100644 index d955e6ec8..000000000 --- a/db/changes/10271-wisemen/.keep +++ /dev/null @@ -1 +0,0 @@ -12271-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 index 40e47b1a3..e4b84a3e4 100644 --- a/db/changes/10271-wisemen/00-ACL.sql +++ b/db/changes/10271-wisemen/00-ACL.sql @@ -1 +1,3 @@ -INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ('FixedPrice', '*', '*', 'ALLOW', 'ROLE', 'buyer'); \ No newline at end of file +INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) + 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 226eebf97..29099e379 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,52 +25,54 @@ 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({}); + 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`); + const query = `SELECT vn.invoiceSerial(?, ?, ?) AS serial`; + const [result] = await Self.rawSql(query, [ticket.clientFk, ticket.companyFk, 'R'], options); + const serial = result.serial; - 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); - stmt = new ParameterizedSQL('CALL vn.invoiceOut_newFromTicket(?, ?, ?, @invoiceId)', [ - ticket.id, - serial, - null - ]); - stmts.push(stmt); + const [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], options); - let invoiceIndex = stmts.push(`SELECT @invoiceId AS invoiceId`) - 1; + const invoiceId = resultInvoice.id; + + const ticketInvoice = await models.Ticket.findById(id, {fields: ['refFk']}, options); + + await models.TicketLog.create({ + originFk: ticket.id, + userFk: userId, + action: 'insert', + changedModel: 'Ticket', + changedModelId: ticket.id, + newInstance: ticketInvoice + }, options); - 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 new file mode 100644 index 000000000..fef7f3b3e --- /dev/null +++ b/modules/ticket/back/models/print-server-queue.json @@ -0,0 +1,31 @@ +{ + "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", + "required": true + } + } +} \ No newline at end of file