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!");
            }
        }
 /// <summary>
 /// Add Items to PurchaseItems
 /// </summary>
 /// <param name="pi"></param>
 public void AddPurchaseAndPurchseItems(Purchase p)
 {
     using (MyContext db = new MyContext())
     {
         db.Purchases.Add(p);
         db.SaveChanges();
     }
 }
        /// <summary>
        /// Checks if invoice number already exists in database. 
        /// If no saves the entry
        /// </summary>
        /// <param name="_purchase"></param>
        /// <returns>1 as success, 0 as failure</returns>
        public int InsertIntoPurchase(Purchase _purchase)
        {
            using( MyContext db = new MyContext())
            {
                //Checking if invoice number already exists in database
                List<Purchase> obj = (from p in db.Purchases
                                      where p.ID == _purchase.ID
                                      select p).ToList();

                if(obj.Count()>0)
                {
                    return 0;
                }
                else
                {
                    db.Purchases.Add(_purchase);
                    db.SaveChanges();
                    return 1;
                }
             }
        }
        public ActionResult GenerateExpiryReturns(IEnumerable<int> stocksID)
        {
            List<ExpiryReturnsViewModel> returnList = new List<ExpiryReturnsViewModel>();
            foreach (var id in stocksID)
            {
                Purchase p = new Purchase();

                var stk = (from s in db.Stocks
                            where s.ID == id
                            select new { s.Item.Name, s.BatchNo, s.Qty, s.Item.ID }).SingleOrDefault();

                //get purchaseID using StockID
                var purchaseID = (from s in db.Stocks
                                 where s.ID == id
                                 select s.PurchaseID).SingleOrDefault();

                //Query Purchase Infos using purchaseID
                p = db.Purchases.SqlQuery("Select * from dbo.Purchase where ID='"+purchaseID+"'").SingleOrDefault();
                               
                ExpiryReturnsViewModel vm = new ExpiryReturnsViewModel() { 
                    StockID = id,
                    ItemName = stk.Name,
                    Batch = stk.BatchNo,
                    PurchasedDate = p.Date,
                    QtyExpired = stk.Qty,
                    SupplierName = p.Supplier.Name,
                };
                returnList.Add(vm);
            }
            return View(returnList);
        }
        public JsonResult InsertIntoPurchaseFormColl(string setPurchaseID1, DateTime InvocingDate, int SelectedSupplierValue, Decimal Amount, Decimal Discount, Decimal Tax, Decimal GrandTotal, bool IsPaid, string Remarks)
        {
            Purchase _purchase = new Purchase()
            {
                ID = setPurchaseID1,
                Date = InvocingDate,
                SupplierID = SelectedSupplierValue,
                Amount = Amount,
                Discount = Discount,
                Tax = Tax,
                GrandTotal = GrandTotal,
                IsPaid = IsPaid,
                Description = Remarks
            };


            int response = repo.InsertIntoPurchase(_purchase);
            if (response == 1)
            {
                return Json("Saved :)");
            }
            else
            {
                return Json("Invoice Number Already exists!");
            }
        }
 public void AddPurchaseAndPurchseItems(Purchase p)
 {
     repo.AddPurchaseAndPurchseItems(p);
 }
        public JsonResult SavePurchase(PurchaseEntryVM p)
        {            
            bool status = false;

            if (p != null)
            {
                //new purchase object using the data from the viewmodel : PurchaseEntryVM
                Purchase purchase = new Purchase
                {
                    ID = p.ID,
                    Date = p.Date,
                    SupplierID = p.SupplierID,
                    Amount = p.Amount,
                    Discount = p.Discount,
                    Tax = p.Tax,
                    GrandTotal = p.GrandTotal,
                    IsPaid = p.IsPaid,
                    Description = p.Description,
                    LastUpdated = DateTime.Now
                };

                purchase.PurchaseItems = new List<PurchaseItem>();
                //populating the PurchaseItems from the PurchaseItems within ViewModel : PurchaseEntryVM
                foreach (var i in p.PurchaseItems)
                {                    
                    purchase.PurchaseItems.Add(i);
                }

                //add purchase 
                // finally save changes.
                service.AddPurchaseAndPurchseItems(purchase);
                service.InsertOrUpdateInventory(p.PurchaseItems);

                //if everything is sucessful, set status to true.
                status = true;                
            }     
            // return the status in form of Json
            return new JsonResult { Data = new { status = status } };
        }     
        /// <summary>
        /// Checks if invoice number already exists in database. 
        /// If no saves the entry
        /// </summary>
        /// <param name="_purchase"></param>
        /// <returns>1 as success, 0 as failure</returns>

        public void InsertIntoPurchase(Purchase _purchase)
        {
            using (MyContext db = new MyContext())
            {                
                db.Purchases.Add(_purchase);
                db.SaveChanges();
            }
        }