2018-04-10 05:48:04 +00:00
|
|
|
module.exports = Self => {
|
2018-05-04 09:46:03 +00:00
|
|
|
Self.remoteMethod('priceDifference', {
|
2018-05-16 06:13:39 +00:00
|
|
|
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'}
|
2018-05-16 06:13:39 +00:00
|
|
|
}, {
|
|
|
|
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: {
|
2018-05-04 09:46:03 +00:00
|
|
|
path: `/:ticketFk/priceDifference`,
|
2018-05-16 06:13:39 +00:00
|
|
|
verb: 'post'
|
2018-04-10 05:48:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-01 08:07:44 +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
|
|
|
}]
|
|
|
|
};
|
2018-05-16 06:13:39 +00:00
|
|
|
let salesObj = {};
|
|
|
|
salesObj.items = await Self.find(filter);
|
|
|
|
salesObj.totalUnitPrice = 0.00;
|
|
|
|
salesObj.totalNewPrice = 0.00;
|
|
|
|
salesObj.totalDifference = 0.00;
|
|
|
|
|
2018-05-30 08:55:54 +00:00
|
|
|
let query = `CALL vn.ticketComponentPriceDifference(?, ?, ?, ?, ?)`;
|
|
|
|
let [differences] = await Self.rawSql(query, [
|
2018-07-24 10:14:35 +00:00
|
|
|
ticketFk,
|
|
|
|
data.landed,
|
|
|
|
data.addressFk,
|
|
|
|
data.agencyModeFk,
|
2018-05-30 08:55:54 +00:00
|
|
|
data.warehouseFk
|
|
|
|
]);
|
2018-05-16 06:13:39 +00:00
|
|
|
|
|
|
|
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;
|
2019-03-20 07:52:54 +00:00
|
|
|
salesObj.totalUnitPrice = Math.round(salesObj.totalUnitPrice * 100) / 100;
|
|
|
|
salesObj.totalNewPrice = Math.round(salesObj.totalNewPrice * 100) / 100;
|
|
|
|
salesObj.totalDifference = Math.round(salesObj.totalDifference * 100) / 100;
|
2018-05-16 06:13:39 +00:00
|
|
|
});
|
2018-04-10 05:48:04 +00:00
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
return salesObj;
|
2018-04-10 05:48:04 +00:00
|
|
|
};
|
|
|
|
};
|