库存/库存管理

本文关键字:库存管理 库存 | 更新日期: 2023-09-27 18:24:13

我正在做一个项目,我有三个表:库存、库存和;购买当库存表中的数量小于特定产品的库存表中数量时,我只能购买单个产品。以下代码的问题也阻止购买其他产品。类似:

Stock Table:
StoreID- ProductID - Quantity
1 - 1- 50
InventoryTable:
StoreID- ProductID - Quantity
1 - 1- 60 //The product is unable To purchase IT has greater quantity regarding The stock Table
1 - 2- 40 //I can purchase This product as
          // This isn't available in stock Table but The following code blocks IT Too

我正在使用表格提交采购表中的所需产品。所以我需要的是匹配产品的单个库存的库存和库存表,如果IT低于最低库存,那么就可以进行购买。我试过了,但还是被卡住了。有什么想法吗??

以下是我迄今为止尝试过的代码:

            SMPURCHASEEntities dc = new SMPURCHASEEntities();
            var con2 = (from s in dc.StoreInventoryDetails
                        select s).ToList();
            string l = Session["EmployeeID"].ToString();
            int g = Convert.ToInt32(l);
            List<EmployeeDetails> con4 = (from r in dc.EmployeeDetails
                                          where r.EmployeeID == (g)
                                          select r).ToList();

            List<Stock> con3 = (from f in dc.Stock
                                select f).ToList();

                        if (string.IsNullOrEmpty(txtQuantity.Text.Trim()))
                        {
                            lblMsg.Text = "Fields are empty";
                            return;
                        }
                            SMPURCHASEEntities context = new SMPURCHASEEntities();
                            int d = Convert.ToInt32(Session["EmployeeID"]);

                            List<EmployeeDetails> con = (from m in context.EmployeeDetails
                                                         where m.EmployeeID == (d)
                                                         select m).ToList();

                            List<DemandOrderLine> con_02 = (from c in context.DemandOrderLine
                                                            select c).ToList();

                            DemandOrderLine k = new DemandOrderLine();

                        foreach (StoreInventoryDetails s in con2)
                        {
                            foreach (Stock f in con3)
                            {
                                foreach (EmployeeDetails r in con4)
                                {
                                    if (f.ItemID == s.ItemID && s.Quantity > f.Quantity && f.StoreID == r.StoreID && s.UniTypeID == f.UnitTypeID)
                                    {
                                        Label1.Text = "You have The minimum stock!!";
                                    }
                                    else
                                    {
                                                k.OrderID = Session["OrderID"].ToString();
                                                k.CategoryID = Convert.ToInt32(ddlCategoryID.SelectedValue);
                                                k.ItemID = Convert.ToInt32(ddlItemID.SelectedValue);
                                                k.UnitTypeID = Convert.ToInt32(ddlUnitTypeID.SelectedValue);
                                                k.Quantity = Convert.ToDouble(txtQuantity.Text);
                                                k.Status = 0;
                                                k.ApprovalStatus = Convert.ToBoolean(0);
                                                k.StoreID = r.StoreID;
                                                k.TotalQuantity = Convert.ToDouble(txtQuantity.Text);
                                                k.Dissolved = 0;
                                                try
                                                {
                                                    context.DemandOrderLine.Add(k);
                                                    context.SaveChanges();
                                                }
                                                catch (Exception ex)
                                                {
                                                    ex.ToString();
                                                }
                                            }
                                    }
                                }
                            }

库存/库存管理

要检查您的业务规则(您的产品在库存表中的数量是否小于库存表中数量),请使用LINQ查询,而不是手动迭代每个集合。以下代码应该有助于您理解该方法(工作示例):

public class Program
{
    public static void Main(string[] args)
    {
        const int employeeId = 1;
        var storeId = EmployeeList.Single(t => t.EmployeeId == employeeId).StoreId;
        CanAddProduct(1, storeId); // false
        CanAddProduct(2, storeId); // true
    }
    static bool CanAddProduct(int itemId, int storeId)
    {
        var inventory = InventoryList.FirstOrDefault(t => t.ItemId == itemId && t.StoreId == storeId);
        if (inventory == null)
        {
            throw new ApplicationException("No such product in inventory");
        }
        var stock = StockList.Where(st => st.ItemId == itemId &&
            st.Quantity < inventory.Quantity && st.StoreId == storeId);
        if (stock.Any())
        {
            Console.WriteLine("You can't add ItemId={0}. You have The minimum stock", itemId);
            return false;
        }
        Console.WriteLine("You can add ItemId={0}", itemId);
        return true;
    }
}