diff --git a/resources/Stock/StockController.js b/resources/Stock/StockController.js index 5ffca13..60bf74e 100644 --- a/resources/Stock/StockController.js +++ b/resources/Stock/StockController.js @@ -206,7 +206,7 @@ export const createOrUpdateInventory = async (req, res) => { const userId = req.user._id; try { const { products } = req.body; // products: [{ productid, Stock }] - console.log(products); + // Fetch all products in the system const allProducts = await Product.find({}, "_id SKU"); // Fetch only _id and SKU fields const allProductIds = allProducts.map((p) => p._id.toString()); @@ -225,17 +225,20 @@ export const createOrUpdateInventory = async (req, res) => { return { ...existingProduct, openingInventory: productInRequest - ? productInRequest.Stock + ? productInRequest.openingInventory : existingProduct.openingInventory, + Stock: productInRequest ? productInRequest.openingInventory : 0, }; } else { // New product, set both stock and opening inventory to the same value - console.log("came here "); + return { productid: productId, SKU: allProducts.find((p) => p._id.toString() === productId).SKU, - openingInventory: productInRequest ? productInRequest.Stock : 0, - Stock: productInRequest ? productInRequest.Stock : 0, + openingInventory: productInRequest + ? productInRequest.openingInventory + : 0, + Stock: productInRequest ? productInRequest.openingInventory : 0, }; } }); diff --git a/resources/Stock/StockRoute.js b/resources/Stock/StockRoute.js index 1ccbc07..4fb9fae 100644 --- a/resources/Stock/StockRoute.js +++ b/resources/Stock/StockRoute.js @@ -1,6 +1,7 @@ import express from "express"; import { createOrUpdateInventory, + getAllUsersWithStock, getProductsAndStockByPD, getProductsAndStockByRD, getStockPD,