51 lines
1.9 KiB
JavaScript
51 lines
1.9 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethod('getItemsPackaging', {
|
|
description: 'Returns the list of items from the supplier of type packing',
|
|
accessType: 'READ',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The supplier id',
|
|
http: {source: 'path'}
|
|
}, {
|
|
arg: 'entry',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The entry id',
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/getItemsPackaging`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
Self.getItemsPackaging = async(id, entry) => {
|
|
return Self.rawSql(`
|
|
WITH entryTmp AS (
|
|
SELECT i.id, SUM(b.quantity) quantity, SUM(b.printedStickers) printedStickers
|
|
FROM vn.entry e
|
|
JOIN vn.buy b ON b.entryFk = e.id
|
|
JOIN vn.supplier s ON s.id = e.supplierFk
|
|
JOIN vn.item i ON i.id = b.itemFk
|
|
WHERE e.id = ? AND e.supplierFk = ?
|
|
GROUP BY i.id
|
|
) SELECT i.id, i.name, et.quantity, SUM(b.quantity) quantityTotal, et.printedStickers
|
|
FROM vn.buy b
|
|
JOIN vn.item i ON i.id = b.itemFk
|
|
JOIN vn.entry e ON e.id = b.entryFk
|
|
JOIN vn.supplier s ON s.id = e.supplierFk
|
|
JOIN vn.buyConfig bc ON bc.monthsAgo
|
|
JOIN vn.travel t ON t.id = e.travelFk
|
|
LEFT JOIN entryTmp et ON et.id = i.id
|
|
WHERE e.supplierFk = ?
|
|
AND i.family IN ('EMB', 'CONT')
|
|
AND b.created > (util.VN_CURDATE() - INTERVAL bc.monthsAgo MONTH)
|
|
GROUP BY b.itemFk
|
|
ORDER BY et.quantity DESC, quantityTotal DESC`, [entry, id, id]);
|
|
};
|
|
};
|