public JsonResult InsertIntoPurchase(PurchaseInvoiceEntryViewModel vm)
        {   
            Purchase _purchase = new Purchase(){
                ID = vm.PurchaseID + "(" + vm.SelectedSupplierValue + ")",
                Date =vm.InvocingDate,
                SupplierID = vm.SelectedSupplierValue,
                Amount =vm.Amount,
                Discount = vm.Discount,
                Tax=vm.Tax,
                GrandTotal =vm.GrandTotal,
                IsPaid =vm.IsPaid,
                Description =vm.Remarks        
             };

            int response = repo.InsertIntoPurchase(_purchase);
            if (response == 1)
            {
                string invoiceId = vm.PurchaseID + "(" + vm.SelectedSupplierValue + ")";
                return Json(invoiceId);
            }
            else
            {
                return Json("Invoice Number Already exists!");
            }
        }
        public void InsertOrUpdateInventory(PurchaseInvoiceEntryViewModel vm)
        {
            using( MyContext db = new MyContext())
            {
                _stock = new Stock();

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

                //Get list of all the inserted item in Stock table
                List<Stock> _checkItem = (from s in db.Stocks
                                          where s.ItemID == vm.SelectedItemvalue && s.BatchNo == vm.BatchNo
                                          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.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 initial 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();
                    }
                }
            }
        }
       // List<PurchaseInvoiceEntryViewModel> tempList = new List<PurchaseInvoiceEntryViewModel>();

        // GET: PurchaseInvoiceEntry
   
        public ActionResult Index()
        {
            var vm = new PurchaseInvoiceEntryViewModel();      
            return View(vm);
        }