2024-10-01 09:30:34 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('getStockBought', {
|
|
|
|
description: 'Returns the stock bought for a given date',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'workerFk',
|
|
|
|
type: 'number',
|
|
|
|
description: 'The id for a buyer',
|
2024-11-13 08:10:18 +00:00
|
|
|
}, {
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'object',
|
2024-10-01 09:30:34 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'dated',
|
|
|
|
type: 'date',
|
|
|
|
description: 'The date to filter',
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/getStockBought`,
|
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-11-13 08:10:18 +00:00
|
|
|
Self.getStockBought = async(workerFk, filter, dated = Date.vnNew()) => {
|
2024-10-01 09:30:34 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const today = Date.vnNew();
|
|
|
|
dated.setHours(0, 0, 0, 0);
|
|
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
|
|
|
|
await models.StockBought.rawSql(`CALL vn.stockBought_calculate(?)`, [dated]);
|
|
|
|
|
2024-11-13 08:10:18 +00:00
|
|
|
const defaultFilter = {
|
2024-10-01 09:30:34 +00:00
|
|
|
where: {dated},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
fields: ['workerFk', 'reserve', 'bought'],
|
|
|
|
relation: 'worker',
|
|
|
|
scope: {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'user',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
if (workerFk) filter.where.workerFk = workerFk;
|
|
|
|
|
2024-11-13 08:10:18 +00:00
|
|
|
return models.StockBought.find({...filter, ...defaultFilter});
|
2024-10-01 09:30:34 +00:00
|
|
|
};
|
|
|
|
};
|