salix/services/loopback/common/methods/ticket/summary.js

127 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-04-10 05:48:04 +00:00
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);
2018-04-17 12:49:55 +00:00
summaryObj.subTotal = getSubTotal(summaryObj.sales);
summaryObj.totalTax = await getTotalTax(models.Ticket, ticketFk);
summaryObj.total = await models.Ticket.getTotal(ticketFk);
2018-04-10 05:48:04 +00:00
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',
fields: ['firstName', 'name']
}
}
},
{relation: 'address', scope: {fields: ['street', 'phone']}},
{
relation: 'notes',
scope: {
fields: ['id', 'observationTypeFk', 'description'],
include: {
relation: 'observationType',
fields: ['description']
}
}
},
{
relation: 'ticketTracking',
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: 'itemFk ASC',
2018-04-10 05:48:04 +00:00
include: [{
relation: 'item',
scope: {
include: {
relation: 'tags',
2018-04-10 05:48:04 +00:00
scope: {
fields: ['tagFk', 'value'],
include: {
relation: 'tag',
scope: {
fields: ['name']
}
},
limit: 6
}
},
fields: ['itemFk', 'name']
}
}]
};
return await Sale.find(filter);
}
2018-04-17 12:49:55 +00:00
function getSubTotal(sales) {
let subTotal = 0.00;
sales.forEach(sale => {
subTotal += sale.quantity * sale.price;
2018-04-17 12:49:55 +00:00
});
return subTotal;
}
2018-04-10 05:48:04 +00:00
2018-04-17 12:49:55 +00:00
async function getTotalTax(ticket, ticketFk) {
let totalTax = 0.00;
let taxes = await ticket.getTaxes(ticketFk);
taxes.forEach(tax => {
totalTax += tax.tax;
});
return totalTax;
}
2018-04-10 05:48:04 +00:00
};