Compare commits

...

2 Commits

Author SHA1 Message Date
Pablo Natek 5f3b1ff601 Merge pull request 'fix: refs #7404 filter an rounding issues' (!3196) from 7404-fix-stockBoughtDetailFilter into master
gitea/salix/pipeline/head This commit looks good Details
Reviewed-on: #3196
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
2024-11-13 11:24:18 +00:00
Pablo Natek b025a8b804 fix: refs #7404 filter an rounding issues
gitea/salix/pipeline/pr-master This commit looks good Details
2024-11-13 09:10:18 +01:00
3 changed files with 26 additions and 12 deletions

View File

@ -59,7 +59,7 @@ proc: BEGIN
INSERT INTO stockBought(workerFk, bought, dated) INSERT INTO stockBought(workerFk, bought, dated)
SELECT tb.workerFk, SELECT tb.workerFk,
ROUND(GREATEST(tb.bought - IFNULL(ts.sold, 0), 0), 1), ROUND(GREATEST(tb.bought - IFNULL(ts.sold, 0), 0), 2),
vDated vDated
FROM tStockBought tb FROM tStockBought tb
LEFT JOIN tStockSold ts ON ts.workerFk = tb.workerFk; LEFT JOIN tStockSold ts ON ts.workerFk = tb.workerFk;

View File

@ -6,6 +6,9 @@ module.exports = Self => {
arg: 'workerFk', arg: 'workerFk',
type: 'number', type: 'number',
description: 'The id for a buyer', description: 'The id for a buyer',
}, {
arg: 'filter',
type: 'object',
}, },
{ {
arg: 'dated', arg: 'dated',
@ -23,7 +26,7 @@ module.exports = Self => {
} }
}); });
Self.getStockBought = async(workerFk, dated = Date.vnNew()) => { Self.getStockBought = async(workerFk, filter, dated = Date.vnNew()) => {
const models = Self.app.models; const models = Self.app.models;
const today = Date.vnNew(); const today = Date.vnNew();
dated.setHours(0, 0, 0, 0); dated.setHours(0, 0, 0, 0);
@ -31,7 +34,7 @@ module.exports = Self => {
await models.StockBought.rawSql(`CALL vn.stockBought_calculate(?)`, [dated]); await models.StockBought.rawSql(`CALL vn.stockBought_calculate(?)`, [dated]);
const filter = { const defaultFilter = {
where: {dated}, where: {dated},
include: [ include: [
{ {
@ -53,6 +56,6 @@ module.exports = Self => {
if (workerFk) filter.where.workerFk = workerFk; if (workerFk) filter.where.workerFk = workerFk;
return models.StockBought.find(filter); return models.StockBought.find({...filter, ...defaultFilter});
}; };
}; };

View File

@ -1,3 +1,4 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('getStockBoughtDetail', { Self.remoteMethod('getStockBoughtDetail', {
description: 'Returns the detail of stock bought for a given date and a worker', description: 'Returns the detail of stock bought for a given date and a worker',
@ -12,6 +13,9 @@ module.exports = Self => {
type: 'string', type: 'string',
description: 'The date to filter', description: 'The date to filter',
required: true, required: true,
}, {
arg: 'filter',
type: 'object',
} }
], ],
returns: { returns: {
@ -24,11 +28,10 @@ module.exports = Self => {
} }
}); });
Self.getStockBoughtDetail = async(workerFk, dated) => { Self.getStockBoughtDetail = async(workerFk, dated, filter, options) => {
const models = Self.app.models; const conn = Self.dataSource.connector;
const myOptions = {}; const myOptions = {};
let tx; let tx;
let result;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -39,8 +42,10 @@ module.exports = Self => {
} }
try { try {
await models.StockBought.rawSql(`CALL vn.item_calculateStock(?)`, [dated], myOptions); const stmts = [];
result = await Self.rawSql( stmts.push(new ParameterizedSQL(`CALL vn.item_calculateStock(?)`, [dated]));
const query = new ParameterizedSQL(
`SELECT b.entryFk entryFk, `SELECT b.entryFk entryFk,
i.id itemFk, i.id itemFk,
i.name itemName, i.name itemName,
@ -61,11 +66,17 @@ module.exports = Self => {
JOIN volumeConfig vc JOIN volumeConfig vc
WHERE ic.display WHERE ic.display
AND w.id = ?`, AND w.id = ?`,
[workerFk], myOptions [workerFk]
); );
await Self.rawSql(`DROP TEMPORARY TABLE tmp.item, tmp.buyUltimate;`, [], myOptions);
stmts.push(query.merge(conn.makeSuffix(filter)));
stmts.push(new ParameterizedSQL(`DROP TEMPORARY TABLE tmp.item, tmp.buyUltimate`));
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql, myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();
return result; return result[1];
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();
throw e; throw e;