salix/services/loopback/common/methods/sale/priceDifference.js

83 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-04-10 05:48:04 +00:00
module.exports = Self => {
Self.remoteMethod('priceDifference', {
description: 'Returns sales with price difference',
2018-04-10 05:48:04 +00:00
accessType: 'READ',
accepts: [{
arg: 'ticketFk',
type: 'number',
required: true,
description: 'ticket id',
http: {source: 'path'}
}, {
arg: 'data',
type: 'Object',
required: true,
description: 'landed, addressFk, agencyModeFk',
http: {source: 'body'}
2018-04-10 05:48:04 +00:00
}],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/:ticketFk/priceDifference`,
verb: 'post'
2018-04-10 05:48:04 +00:00
}
});
Self.priceDifference = async (ticketFk, data) => {
2018-04-10 05:48:04 +00:00
let filter = {
where: {
ticketFk: ticketFk
},
order: 'concept ASC',
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']
}
}]
};
let salesObj = {};
salesObj.items = await Self.find(filter);
salesObj.totalUnitPrice = 0.00;
salesObj.totalNewPrice = 0.00;
salesObj.totalDifference = 0.00;
let query = `CALL vn.ticketComponentPriceDifference(?, ?, ?, ?, ?)`;
let [differences] = await Self.rawSql(query, [
ticketFk,
data.landed,
data.addressFk,
data.agencyModeFk,
data.warehouseFk
]);
salesObj.items.forEach(sale => {
differences.forEach(difference => {
if (sale.id == difference.saleFk)
sale.component = difference;
});
salesObj.totalUnitPrice += sale.price;
salesObj.totalNewPrice += sale.component.newPrice;
salesObj.totalDifference += sale.component.difference;
});
2018-04-10 05:48:04 +00:00
return salesObj;
2018-04-10 05:48:04 +00:00
};
};