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

153 lines
4.7 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('summary', {
description: 'Returns a ticket summary',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'ticket id',
http: {source: 'path'}
}],
returns: {
type: [this.modelName],
root: true
},
http: {
path: `/:id/summary`,
verb: 'GET'
}
});
Self.summary = async ticketFk => {
let models = Self.app.models;
let summaryObj = await getTicketData(Self, ticketFk);
summaryObj.sales = await getSales(models.Sale, ticketFk);
summaryObj.subtotal = await models.Ticket.subtotal(ticketFk);
summaryObj.vat = await models.Ticket.getVAT(ticketFk);
summaryObj.total = summaryObj.subtotal + summaryObj.vat;
summaryObj.packagings = await models.TicketPackaging.find({
where: {ticketFk: ticketFk},
include: [{relation: 'packaging',
scope: {
fields: ['itemFk', 'name'],
include: {
relation: 'item'
}
}
}]
});
summaryObj.requests = await getRequests(Self, ticketFk);
summaryObj.services = await models.TicketService.find({
where: {ticketFk: ticketFk},
include: [{relation: 'taxClass'}]
});
return summaryObj;
};
async function getTicketData(Self, ticketFk) {
let filter = {
include: [
{relation: 'warehouse', scope: {fields: ['name']}},
{relation: 'agencyMode', scope: {fields: ['name']}},
{
relation: 'client',
scope: {
fields: ['salesPersonFk', 'name'],
include: {
relation: 'salesPerson',
scope: {
fields: ['userFk'],
include: {
relation: 'user',
scope: {
fields: ['nickname']
}
}
}
}
}
},
{
relation: 'address',
scope: {
fields: ['street', 'city', 'provinceFk', 'phone'],
include: {
relation: 'province',
scope: {
fields: ['name']
}
}
}
},
{
relation: 'notes',
scope: {
fields: ['id', 'observationTypeFk', 'description'],
include: {
relation: 'observationType',
fields: ['description']
}
}
},
{
relation: 'state',
scope: {
fields: ['stateFk'],
include: {
relation: 'state',
fields: ['name']
}
}
}
],
where: {id: ticketFk}
};
return await Self.findOne(filter);
}
async function getSales(Sale, ticketFk) {
let filter = {
where: {
ticketFk: ticketFk
},
order: 'concept',
include: [
{relation: 'item'},
{relation: 'claimBeginning'}
]
};
return await Sale.find(filter);
}
async function getRequests(Self, ticketFk) {
let filter = {
where: {
ticketFk: ticketFk
},
include: [
{
relation: 'requester',
scope: {
include: {
relation: 'user'
}
}
}, {
relation: 'atender',
scope: {
include: {
relation: 'user'
}
}
}, {
relation: 'sale'
}
]
};
return await Self.app.models.TicketRequest.find(filter);
}
};