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

83 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-10-10 10:55:04 +00:00
module.exports = Self => {
Self.remoteMethod('summary', {
description: 'Returns a summary for a given order id',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'order id',
http: {source: 'path'}
}],
returns: {
type: [this.modelName],
root: true
},
http: {
path: `/:id/summary`,
verb: 'GET'
}
});
Self.summary = async orderId => {
let models = Self.app.models;
let summary = await getOrderData(Self, orderId);
summary.subTotal = getSubTotal(summary.rows);
summary.VAT = await models.Order.getVAT(orderId);
summary.total = await models.Order.getTotal(orderId);
return summary;
};
async function getOrderData(Self, orderId) {
let filter = {
include: [
{relation: 'agencyMode', scope: {fields: ['name']}},
{
relation: 'client',
scope: {
fields: ['salesPersonFk', 'name'],
include: {
relation: 'salesPerson',
fields: ['firstName', 'name']
}
}
},
{
relation: 'address',
scope: {
fields: ['street', 'city', 'provinceFk', 'phone', 'nickname'],
include: {
relation: 'province',
scope: {
fields: ['name']
}
}
}
},
{
relation: 'rows',
scope: {
include: {
2018-11-06 09:49:38 +00:00
relation: 'item'
2018-10-10 10:55:04 +00:00
}
}
}
],
where: {id: orderId}
};
return await Self.findOne(filter);
}
function getSubTotal(rows) {
let subTotal = 0.00;
rows().forEach(row => {
subTotal += row.quantity * row.price;
});
return subTotal;
}
};