module.exports = Self => { Self.remoteMethod('getStockBoughtDetail', { description: 'Returns the detail of stock bought for a given date and a worker', accessType: 'READ', accepts: [{ arg: 'workerFk', type: 'number', description: 'The worker to filter', required: true, }, { arg: 'dated', type: 'string', description: 'The date to filter', } ], returns: { type: ['object'], root: true }, http: { path: `/getStockBoughtDetail`, verb: 'GET' } }); Self.getStockBoughtDetail = async(workerFk, dated) => { if (!dated) { dated = Date.vnNew(); dated.setHours(0, 0, 0, 0); } return Self.rawSql( `SELECT e.id entryFk, i.id itemFk, i.longName itemName, b.quantity, ROUND((ac.conversionCoefficient * (b.quantity / b.packing) * buy_getVolume(b.id) ) / (vc.trolleyM3 * 1000000), 2 ) volume, b.packagingFk, b.packing FROM entry e JOIN travel t ON t.id = e.travelFk JOIN buy b ON b.entryFk = e.id JOIN item i ON i.id = b.itemFk JOIN itemType it ON it.id = i.typeFk JOIN worker w ON w.id = it.workerFk JOIN auctionConfig ac JOIN volumeConfig vc WHERE t.warehouseInFk = ac.warehouseFk AND it.workerFk = ? AND t.shipped = util.VN_CURDATE()`, [workerFk] ); }; };