2479-factura-ticket #521

Merged
jgallego merged 6 commits from 2479-factura-ticket into dev 2021-01-22 15:15:50 +00:00
5 changed files with 69 additions and 33 deletions

View File

@ -1 +0,0 @@
12271-wisemen

View File

@ -1 +1,3 @@
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');

View File

@ -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};

View File

@ -17,6 +17,9 @@
"Packaging": {
"dataSource": "vn"
},
"PrintServerQueue": {
"dataSource": "vn"
},
"Sale": {
"dataSource": "vn"
},

View File

@ -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
}
}
}