diff --git a/modules/entry/back/methods/stock-bought/getStockBoughtDetail.js b/modules/entry/back/methods/stock-bought/getStockBoughtDetail.js index 2e2ddd79f..3e040d0d3 100644 --- a/modules/entry/back/methods/stock-bought/getStockBoughtDetail.js +++ b/modules/entry/back/methods/stock-bought/getStockBoughtDetail.js @@ -26,12 +26,20 @@ module.exports = Self => { Self.getStockBoughtDetail = async(workerFk, dated) => { const models = Self.app.models; - await models.StockBought.rawSql(`CALL vn.item_calculateStock(?)`, [dated]); - const tx = await models.Collection.beginTransaction({}); - const options = {transaction: tx}; + const myOptions = {}; + let tx; let result; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + try { - await models.StockBought.rawSql(`CALL vn.item_calculateStock(?)`, [dated], options); + await models.StockBought.rawSql(`CALL vn.item_calculateStock(?)`, [dated], myOptions); result = await Self.rawSql( `SELECT b.entryFk entryFk, i.id itemFk, @@ -53,13 +61,14 @@ module.exports = Self => { JOIN volumeConfig vc WHERE ic.display AND w.id = ?`, - [workerFk], options + [workerFk], myOptions ); - await Self.rawSql(`DROP TEMPORARY TABLE tmp.item, tmp.buyUltimate;`, [], options); + await Self.rawSql(`DROP TEMPORARY TABLE tmp.item, tmp.buyUltimate;`, [], myOptions); + if (tx) await tx.commit(); + return result; } catch (e) { await tx.rollback(); throw e; } - return result; }; };