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

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-04-05 10:24:39 +00:00
module.exports = function(Self) {
Self.remoteMethodCtx('canBeInvoiced', {
description: 'Whether the ticket can or not be invoiced',
2019-04-05 10:24:39 +00:00
accessType: 'READ',
accepts: [
{
2021-06-30 14:10:26 +00:00
arg: 'ticketsIds',
description: 'The tickets id',
type: ['number'],
required: true
2019-04-05 10:24:39 +00:00
}
],
returns: {
arg: 'data',
type: 'boolean',
root: true
},
http: {
2021-06-30 14:10:26 +00:00
path: `/canBeInvoiced`,
2019-04-05 10:24:39 +00:00
verb: 'get'
}
});
2021-06-30 14:10:26 +00:00
Self.canBeInvoiced = async(ticketsIds, options) => {
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const tickets = await Self.find({
where: {
id: {inq: ticketsIds}
},
2021-03-11 14:14:08 +00:00
fields: ['id', 'refFk', 'shipped', 'totalWithVat']
2021-06-30 14:10:26 +00:00
}, myOptions);
2019-04-05 10:24:39 +00:00
2021-06-30 14:10:26 +00:00
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
);
2019-04-05 10:24:39 +00:00
2021-06-30 14:10:26 +00:00
const today = new Date();
2019-04-05 10:24:39 +00:00
2021-06-30 14:10:26 +00:00
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;
});
2019-04-05 10:24:39 +00:00
2021-06-30 14:10:26 +00:00
return !(invalidTickets || hasSomeNegativeBase);
2019-04-05 10:24:39 +00:00
};
};