2020-01-16 12:39:32 +00:00
|
|
|
module.exports = Self => {
|
2021-03-30 08:28:44 +00:00
|
|
|
Self.remoteMethod('getWasteByWorker', {
|
2020-10-14 05:18:39 +00:00
|
|
|
description: 'Returns the details of losses by worker',
|
2020-01-16 12:39:32 +00:00
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [],
|
|
|
|
returns: {
|
|
|
|
type: ['Object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2021-03-30 08:28:44 +00:00
|
|
|
path: `/getWasteByWorker`,
|
2020-01-16 12:39:32 +00:00
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-30 08:28:44 +00:00
|
|
|
Self.getWasteByWorker = async() => {
|
|
|
|
const wastes = await Self.rawSql(`
|
|
|
|
SELECT *, 100 * dwindle / total AS percentage
|
|
|
|
FROM (
|
|
|
|
SELECT buyer,
|
|
|
|
ws.family,
|
|
|
|
sum(ws.saleTotal) AS total,
|
|
|
|
sum(ws.saleWaste) AS dwindle
|
|
|
|
FROM bs.waste ws
|
|
|
|
WHERE year = YEAR(TIMESTAMPADD(WEEK,-1,CURDATE()))
|
|
|
|
AND week = WEEK(TIMESTAMPADD(WEEK,-1,CURDATE()), 1)
|
|
|
|
GROUP BY buyer, family
|
|
|
|
) sub
|
|
|
|
ORDER BY percentage DESC`);
|
2020-01-16 12:39:32 +00:00
|
|
|
|
|
|
|
const details = [];
|
|
|
|
|
|
|
|
for (let waste of wastes) {
|
|
|
|
const buyerName = waste.buyer;
|
|
|
|
|
|
|
|
let buyerDetail = details.find(waste => {
|
|
|
|
return waste.buyer == buyerName;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!buyerDetail) {
|
|
|
|
buyerDetail = {
|
|
|
|
buyer: buyerName,
|
|
|
|
lines: []
|
|
|
|
};
|
|
|
|
details.push(buyerDetail);
|
|
|
|
}
|
|
|
|
|
|
|
|
buyerDetail.lines.push(waste);
|
|
|
|
}
|
|
|
|
|
|
|
|
return details;
|
|
|
|
};
|
|
|
|
};
|