salix/modules/ticket/back/methods/sale/priceDifference.js

67 lines
1.9 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
}
});
2018-11-06 09:49:38 +00:00
Self.priceDifference = async (ticketFk, data) => {
2018-04-10 05:48:04 +00:00
let filter = {
where: {
ticketFk: ticketFk
},
order: 'concept ASC',
include: [{
2018-11-06 09:49:38 +00:00
relation: 'item'
2018-04-10 05:48:04 +00:00
}]
};
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
};
};