4
Answers

Count Item Quantity and Critical Stock on the dashboard

Hello Team I want to count item quantity and critical stock from my mvc controller and pass to dashboard, kindly assist. 

public JsonResult GetDashboardData()
        {
            ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
            var currentDate = DateTime.Now.Date.ToString("dd-MMM-yyyy");
        
            return Json(JsonRequestBehavior.AllowGet);
        }

Answers (4)

1
Photo of Vishal Joshi
244 7.8k 135.4k 1y

Hello Emmanuel,

You can try the below code. you need to sum up the quantity to get the item quantity count and get the critical stock needed group by stock item and sum up. if the total is less than the critical count consider it as critical stock count.

public JsonResult GetDashboardData()
{
  ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
  var currentDate = DateTime.Now.Date.ToString("dd-MMM-yyyy");
  var totalQty = db.tblProductStock.Sum(a = >a.Quantity);
  var criticalStockCount = db.tblProductStock.GroupBy(a=>a.productStockId).Where(a 
  =>a.Sum(b=>b.Quantity) <= 0).Count();
  return Json(new{totalQty = totalQty ,criticalStockCount = 
  criticalStockCount},JsonRequestBehavior.AllowGet);
}

Vishal Joshi

Thanks

Accepted
0
Photo of Aravind  Govindaraj
312 5.9k 332.2k 1y

Hi Emmanuel. Typically I would like to say this is aggregated data, so instead of computing from a transaction table. I suggest creating another data product which supposed to act as aggregated data that you are planning to display in the dashboard.

 

Thanks

Aravind Govindaraj

0
Photo of Emmmanuel FIADUFE
673 1.4k 73k 1y

Thank you team,

Everything is working now.

0
Photo of Prasad Raveendran
233 8.3k 1.9m 1y

try something like this

public class DashboardData
{
    public int ItemQuantity { get; set; }
    public int CriticalStock { get; set; }
}

public JsonResult GetDashboardData()
{
    using (ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities())
    {
        var currentDate = DateTime.Now.Date;

        // Assuming you have a table named "Items" with fields for quantity and critical stock
        int totalItems = db.Items.Sum(item => item.Quantity); // Calculate total quantity of items
        int criticalItems = db.Items.Count(item => item.Stock < item.CriticalThreshold); // Count items that are critical

        DashboardData dashboardData = new DashboardData
        {
            ItemQuantity = totalItems,
            CriticalStock = criticalItems
        };

        return Json(dashboardData, JsonRequestBehavior.AllowGet);
    }
}

Replace the entity names and fields (Items, Quantity, Stock, CriticalThreshold, etc.) with the appropriate names from your database.

Once you've retrieved the required data (total quantity and critical stock count), you create an instance of a class (DashboardData) with the retrieved values and return it as JSON in the response.

Make sure to handle errors or exceptions appropriately and ensure that your Entity Framework model matches your database schema for this code to work correctly.

Finally, in your frontend code (JavaScript or whatever you're using), you can fetch this JSON data from your MVC Controller and render it on your dashboard as needed.