salix/modules/ticket/back/methods/ticket/canBeInvoiced.js

60 lines
1.7 KiB
JavaScript

module.exports = function(Self) {
Self.remoteMethodCtx('canBeInvoiced', {
description: 'Whether the ticket can or not be invoiced',
accessType: 'READ',
accepts: [
{
arg: 'ticketsIds',
description: 'The tickets id',
type: ['number'],
required: true
}
],
returns: {
arg: 'data',
type: 'boolean',
root: true
},
http: {
path: `/canBeInvoiced`,
verb: 'get'
}
});
Self.canBeInvoiced = async(ticketsIds, options) => {
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const tickets = await Self.find({
where: {
id: {inq: ticketsIds}
},
fields: ['id', 'refFk', 'shipped', 'totalWithVat']
}, myOptions);
const query = `
SELECT vn.hasSomeNegativeBase(t.id) AS hasSomeNegativeBase
FROM ticket t
WHERE id IN(?)`;
const ticketBases = await Self.rawSql(query, [ticketsIds], myOptions);
const hasSomeNegativeBase = ticketBases.some(
ticketBases => ticketBases.hasSomeNegativeBase
);
const today = new Date();
const invalidTickets = tickets.some(ticket => {
const shipped = new Date(ticket.shipped);
const shippingInFuture = shipped.getTime() > today.getTime();
const isInvoiced = ticket.refFk;
const priceZero = ticket.totalWithVat == 0;
return isInvoiced || priceZero || shippingInFuture;
});
return !(invalidTickets || hasSomeNegativeBase);
};
};