/// <summary>
        /// Insert or update stock based on purchase Items
        /// </summary>
        /// <param name="vm"></param>
        public void InsertOrUpdateInventory(PurchaseItem vm)
        {
            using (MyContext db = new MyContext())
            {
                _stock = new Stock();

                //Initialize new stock with vm inserted values
                _stock.ItemID = vm.ItemID;
                _stock.BatchNo = vm.Batch;
                _stock.CostPrice = vm.CostPrice;
                _stock.SellingPrice = vm.SellingPrice;
                _stock.ExpiryDate = vm.Expiry;
                _stock.PurchaseID = vm.PurchaseID;

                //Get list of all the inserted item in Stock table
                List<Stock> _checkItem = (from s in db.Stocks
                                          where s.ItemID == vm.ItemID && s.BatchNo == vm.Batch
                                          select s).ToList();

                //count the number of exixting record on inserted item
                int countStock = _checkItem.Count();

                //Add new record if record is not found
                if (countStock == 0)
                {
                    //Add new item with new Initial qty
                    _stock.Qty = vm.Qty;
                    _stock.InitialQty = _stock.Qty;
                    db.Stocks.Add(_stock);
                    db.SaveChanges();
                }
                else
                {
                    //to check how many times loop executes completely 
                    int loopCount = 0;
                    //Check and Add or update
                    foreach (Stock stock in _checkItem)
                    {
                        if (stock.CostPrice == vm.CostPrice)
                        {
                            //Update qty and InitialQty 
                            stock.Qty += vm.Qty;
                            stock.InitialQty += vm.Qty;
                            db.SaveChanges();
                            break;
                        }
                        loopCount++;
                    }
                    if (loopCount == _checkItem.Count())
                    {
                        //Add new record with Qty and intial Qty
                        _stock.InitialQty += vm.Qty;
                        db.Stocks.Add(_stock);
                        db.SaveChanges();
                    }
                }
            }
        }
Esempio n. 2
0
 public void UpdateStock(string[] _stockID, string[] _qty)
 {            
     for (int i = 0, y = _stockID.Count(); i < y; i++)
     {
         int getStockID = Convert.ToInt32(_stockID[i]);
         int getQty = Convert.ToInt32(_qty[i]);
         Stock stock = new Stock();
         repo.UpdateStock(getStockID, getQty);                
     }        
 }
        /*
        public bool UpdateStock(string[] _stockID, string[] _qty)
        {
            using (MyContext db = new MyContext())
            {
                for (int i = 0, y = _stockID.Count(); i < y; i++)
                {
                    int getStockID = Convert.ToInt32(_stockID[i]);
                    int getQty = Convert.ToInt32(_qty[i]);
                    Stock stock = new Stock();
                    stock = db.Stocks.Find(getStockID);
                   
                    stock.Qty = stock.Qty - getQty;
                   // db.Stocks.Add(stock);
                    db.SaveChanges();
                }                
            }
            return true;
        }*/

        public void UpdateStock(int getStockID, int getQty)
        {
            Stock stock = new Stock();
            stock = db.Stocks.Find(getStockID);
            stock.Qty = stock.Qty - getQty;
            db.SaveChanges();
        }