#1249 ticket.invoice
gitea/salix/dev This commit looks good Details

This commit is contained in:
Gerard 2019-04-05 12:24:39 +02:00
parent 5ae58f1d70
commit ebe269f41e
6 changed files with 154 additions and 1 deletions

View File

@ -77,5 +77,7 @@
"The extension must be unique": "La extensión debe ser unica",
"The secret can't be blank": "La contraseña no puede estar en blanco",
"EXTENSION_INVALID_FORMAT": "La extensión es invalida",
"We weren't able to send this SMS": "No hemos podido enviar el SMS"
"We weren't able to send this SMS": "No hemos podido enviar el SMS",
"This client can't be invoiced": "Este cliente no puede ser facturado",
"This ticket can't be invoiced": "Este ticket no puede ser facturado"
}

View File

@ -0,0 +1,34 @@
module.exports = function(Self) {
Self.remoteMethodCtx('canBeInvoiced', {
description: 'Change property isEqualizated in all client addresses',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'string',
required: true,
description: 'Client id',
http: {source: 'path'}
}
],
returns: {
arg: 'data',
type: 'boolean',
root: true
},
http: {
path: `/:id/canBeInvoiced`,
verb: 'get'
}
});
Self.canBeInvoiced = async id => {
let $ = Self.app.models;
let client = await $.Client.findById(id, {fields: ['id', 'isTaxDataChecked', 'hasToInvoice']});
if (client.isTaxDataChecked && client.hasToInvoice)
return true;
return false;
};
};

View File

@ -19,6 +19,7 @@ module.exports = Self => {
require('../methods/client/updateFiscalData')(Self);
require('../methods/client/getTransactions')(Self);
require('../methods/client/confirmTransaction')(Self);
require('../methods/client/canBeInvoiced')(Self);
// Validations

View File

@ -0,0 +1,41 @@
module.exports = function(Self) {
Self.remoteMethodCtx('canBeInvoiced', {
description: 'Change property isEqualizated in all client addresses',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'string',
required: true,
description: 'Client id',
http: {source: 'path'}
}
],
returns: {
arg: 'data',
type: 'boolean',
root: true
},
http: {
path: `/:id/canBeInvoiced`,
verb: 'get'
}
});
Self.canBeInvoiced = async id => {
let ticket = await Self.app.models.Ticket.findById(id, {fields: ['id', 'refFk', 'shipped']});
let ticketTotal = await Self.app.models.Ticket.getTotal(id);
let query = `SELECT vn.hasSomeNegativeBase(?) AS hasSomeNegativeBase`;
let [result] = await Self.rawSql(query, [id]);
let hasSomeNegativeBase = result.hasSomeNegativeBase;
let today = new Date();
let shipped = new Date(ticket.shipped);
if (ticket.refFk || ticketTotal == 0 || shipped.getTime() > today.getTime() || hasSomeNegativeBase)
return false;
return true;
};
};

View File

@ -0,0 +1,73 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = function(Self) {
Self.remoteMethodCtx('makeInvoice', {
description: 'Change property isEqualizated in all client addresses',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'string',
required: true,
description: 'Client id',
http: {source: 'path'}
}
],
returns: {
arg: 'data',
type: 'boolean',
root: true
},
http: {
path: `/:id/makeInvoice`,
verb: 'POST'
}
});
Self.makeInvoice = async(ctx, id) => {
let userId = ctx.req.accessToken.userId;
let $ = Self.app.models;
let options = {};
options.transaction = await Self.beginTransaction({});
try {
let ticket = await $.Ticket.findById(id, {fields: ['id', 'clientFk', 'companyFk']});
let clientCanBeInvoiced = await $.Client.canBeInvoiced(ticket.clientFk);
if (!clientCanBeInvoiced)
throw new UserError(`This client can't be invoiced`);
let ticketCanBeInvoiced = await $.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;
query = `CALL vn.invoiceFromTicket(?)`;
await Self.rawSql(query, [ticket.id], options);
query = `CALL vn.invoiceOutMake(?, ?, @invoiceId);
SELECT @invoiceId AS invoiceId;`;
result = await Self.rawSql(query, [serial, null], options);
let invoice = result[1].invoiceId;
if (serial != 'R' && invoice) {
query = `CALL vn.invoiceOutBooking(?)`;
await Self.rawSql(query, [invoice], options);
}
let user = await Self.app.models.Worker.findOne({where: {userFk: userId}});
query = `INSERT INTO vn2008.Colas(Id_Informe,Cola,Id_Trabajador) VALUES (?, ?, ?)`;
await Self.rawSql(query, [3, invoice, user.id], options);
await options.transaction.commit();
} catch (e) {
options.transaction.rollback();
throw e;
}
};
};

View File

@ -18,4 +18,6 @@ module.exports = Self => {
require('../methods/ticket/getLanded')(Self);
require('../methods/ticket/filter')(Self);
require('../methods/ticket/getPossibleStowaways')(Self);
require('../methods/ticket/canBeInvoiced')(Self);
require('../methods/ticket/makeInvoice')(Self);
};