// GET: Sales/Create
        public ActionResult Create()
        {
            //List Item
            List <ViewSalesItem> viewSalesItems = new List <ViewSalesItem>();
            List <Item>          items          = db.items.Where(a => a.InActive == false).ToList();

            if (items.Count > 0)
            {
                foreach (Item item in items)
                {
                    ViewSalesItem viewSalesItem = new ViewSalesItem();
                    viewSalesItem.ItemId    = item.ItemCode;
                    viewSalesItem.Name      = item.ItemName;
                    viewSalesItem.ImageFile = item.ImageFile;
                    viewSalesItem.Price     = item.Price;
                    viewSalesItems.Add(viewSalesItem);
                }
            }

            ViewBag.ItemList = viewSalesItems.ToList();

            Sale sale = new Sale();

            return(View(sale));
        }
        // GET: Sales/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Sale sale = db.sales.Find(id);

            if (sale == null)
            {
                return(HttpNotFound());
            }
            //List Item
            List <ViewSalesItem> viewSalesItems = new List <ViewSalesItem>();
            List <Item>          items          = db.items.Where(a => a.InActive == false).ToList();

            if (items.Count > 0)
            {
                foreach (Item item in items)
                {
                    ViewSalesItem viewSalesItem = new ViewSalesItem();
                    viewSalesItem.ItemId    = item.ItemCode;
                    viewSalesItem.Name      = item.ItemName;
                    viewSalesItem.ImageFile = item.ImageFile;
                    viewSalesItem.Price     = item.Price;
                    viewSalesItem.SalesId   = sale.SalesId;
                    viewSalesItems.Add(viewSalesItem);

                    if (sale.SalesItemLists.Count > 0)
                    {
                        var salesItem = sale.SalesItemLists.FirstOrDefault(a => a.ItemId == item.ItemCode);
                        if (salesItem != null)
                        {
                            viewSalesItem.Quantity    = salesItem.Quantity;
                            viewSalesItem.SalesItemId = salesItem.SalesItemId;
                        }
                    }
                }
            }

            ViewBag.ItemList = viewSalesItems.ToList();

            ViewBag.EmployeeId = new SelectList(db.employees, "Id", "EmpFullName");
            return(View("Create", sale));
        }
        public ActionResult Create(Sale sale)
        {
            if (ModelState.IsValid)
            {
                Employee employee = db.employees.Find(sale.EmployeeId);

                if (employee == null)
                {
                    sale.EmployeeId = null;
                }

                if (sale.SalesId == 0)
                {
                    db.sales.Add(sale);
                    db.SaveChanges();
                }
                else
                {
                    //List<SalesItemList> salesItemLists = db.salesItemLists.Where(a => a.SalesId == sale.SalesId).ToList();
                    //db.salesItemLists.RemoveRange(salesItemLists);
                    db.Entry(sale).State = EntityState.Modified;
                    Array.ForEach(sale.SalesItemLists.ToArray(), a => db.Entry(a).State = EntityState.Modified);

                    db.SaveChanges();
                }


                return(RedirectToAction("Details", new { id = sale.SalesId }));
            }

            //List Item
            List <ViewSalesItem> viewSalesItems = new List <ViewSalesItem>();
            List <Item>          items          = db.items.Where(a => a.InActive == false).ToList();

            if (items.Count > 0)
            {
                foreach (Item item in items)
                {
                    ViewSalesItem viewSalesItem = new ViewSalesItem();
                    viewSalesItem.ItemId    = item.ItemCode;
                    viewSalesItem.Name      = item.ItemName;
                    viewSalesItem.ImageFile = item.ImageFile;
                    viewSalesItem.Price     = item.Price;
                    viewSalesItems.Add(viewSalesItem);

                    if (sale.SalesItemLists.Count > 0)
                    {
                        var salesItem = sale.SalesItemLists.FirstOrDefault(a => a.ItemId == item.ItemCode);
                        if (salesItem != null)
                        {
                            viewSalesItem.Quantity = salesItem.Quantity;
                        }
                    }
                }
            }

            ViewBag.ItemList = viewSalesItems.ToList();

            ViewBag.EmployeeId = new SelectList(db.employees, "Id", "EmpFullName");
            return(View(sale));
        }