2014-09-24 63 views
0

我正在一個項目中,我有三個表:庫存,庫存&採購。當庫存數量少於庫存數量時,我只能購買單個產品特定產品的數量。以下代碼的問題阻止購買其他產品。如:庫存/庫存管理

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(); 
               } 
              } 

            } 
           } 
          } 
+0

項目Id是從下面的代碼是從產品編號上面的表模式? – 2ooom 2014-09-24 05:28:43

+0

是的。它是。無法做到關於單個產品的庫存匹配。 – TechView 2014-09-24 05:33:08

回答

0

要檢查您的業務規則(你的產品是否有庫存表數量小於庫存表中的數量)使用LINQ查詢迭代每個代替手動收集。下面是代碼,應該幫助你理解的方法(工作example):

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; 
    } 
}