diff --git a/resources/Stock/StockController.js b/resources/Stock/StockController.js index 60bf74e..53bbf9a 100644 --- a/resources/Stock/StockController.js +++ b/resources/Stock/StockController.js @@ -69,18 +69,22 @@ export const getProductsAndStockByPD = async (req, res) => { ]), ]); - // Create a stock map for easy lookup + // Create a stock map for easy lookup from userStock const stockMap = {}; if (userStock && userStock.products) { userStock.products.forEach((product) => { - stockMap[product.productid.toString()] = product.Stock; + stockMap[product.productid.toString()] = { + Stock: product.Stock, + openingInventory: product.openingInventory, + }; }); } - // Combine products with their respective stock + // Combine products with their respective stock and opening inventory const productsWithStock = products.map((product) => ({ ...product, - stock: stockMap[product._id.toString()] || 0, + stock: stockMap[product._id.toString()]?.Stock || 0, + openingInventory: stockMap[product._id.toString()]?.openingInventory || 0, })); // Get total count for pagination purposes