2479-factura-ticket #521
|
@ -1 +0,0 @@
|
||||||
12271-wisemen
|
|
|
@ -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');
|
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ('FixedPrice', '*', '*', 'ALLOW', 'ROLE', 'buyer');
|
|
@ -1,5 +1,4 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
||||||
|
|
||||||
module.exports = function(Self) {
|
module.exports = function(Self) {
|
||||||
Self.remoteMethodCtx('makeInvoice', {
|
Self.remoteMethodCtx('makeInvoice', {
|
||||||
|
@ -26,52 +25,54 @@ module.exports = function(Self) {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.makeInvoice = async(ctx, id) => {
|
Self.makeInvoice = async(ctx, id) => {
|
||||||
const conn = Self.dataSource.connector;
|
const userId = ctx.req.accessToken.userId;
|
||||||
let userId = ctx.req.accessToken.userId;
|
const models = Self.app.models;
|
||||||
let models = Self.app.models;
|
const tx = await Self.beginTransaction({});
|
||||||
let tx = await Self.beginTransaction({});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
|
|
||||||
let filter = {fields: ['id', 'clientFk', 'companyFk']};
|
const filter = {fields: ['id', 'clientFk', 'companyFk']};
|
||||||
let ticket = await models.Ticket.findById(id, filter, options);
|
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)
|
if (!clientCanBeInvoiced)
|
||||||
throw new UserError(`This client can't be invoiced`);
|
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)
|
if (!ticketCanBeInvoiced)
|
||||||
throw new UserError(`This ticket can't be invoiced`);
|
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`;
|
await Self.rawSql('CALL invoiceFromTicket(?)', [id], options);
|
||||||
let [result] = await Self.rawSql(query, [ticket.clientFk, ticket.companyFk, 'R'], options);
|
await Self.rawSql('CALL invoiceOut_new(?, CURDATE(), null, @invoiceId)', [serial], options);
|
||||||
let serial = result.serial;
|
|
||||||
let stmts = [];
|
|
||||||
|
|
||||||
stmt = new ParameterizedSQL('CALL vn.invoiceOut_newFromTicket(?, ?, ?, @invoiceId)', [
|
const [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], options);
|
||||||
ticket.id,
|
|
||||||
serial,
|
|
||||||
null
|
|
||||||
]);
|
|
||||||
stmts.push(stmt);
|
|
||||||
|
|
||||||
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) {
|
if (serial != 'R' && invoiceId) {
|
||||||
query = `CALL vn.invoiceOutBooking(?)`;
|
await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], options);
|
||||||
await Self.rawSql(query, [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();
|
await tx.commit();
|
||||||
|
|
||||||
return {invoiceFk: invoiceId, serial};
|
return {invoiceFk: invoiceId, serial};
|
||||||
|
|
|
@ -17,6 +17,9 @@
|
||||||
"Packaging": {
|
"Packaging": {
|
||||||
"dataSource": "vn"
|
"dataSource": "vn"
|
||||||
},
|
},
|
||||||
|
"PrintServerQueue": {
|
||||||
|
"dataSource": "vn"
|
||||||
|
},
|
||||||
"Sale": {
|
"Sale": {
|
||||||
"dataSource": "vn"
|
"dataSource": "vn"
|
||||||
},
|
},
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue