83 lines
2.3 KiB
JavaScript
83 lines
2.3 KiB
JavaScript
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: {
|
|
relation: 'item'
|
|
}
|
|
}
|
|
}
|
|
],
|
|
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;
|
|
}
|
|
};
|