public ActionResult CreateBillCompany(BillCompany Bill)
        {
         

            Bill.CompanyId =Convert.ToInt16(TempData["CompanyId"]);
            Bill.FirstName = TempData["CompanyName"].ToString();
            if (ModelState.IsValid)
            {
                DateTime date = DateTime.Now.AddHours(10);
                int hours = date.Hour;
                int minutes = date.Minute;
                int secondes = date.Second;
                Bill.OrderDate=Bill.OrderDate.AddHours(hours).AddMinutes(minutes).AddSeconds(secondes);
                db.BillCompany.Add(Bill);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = Bill.BillCompanyId, CompanyId=Bill.CompanyId });
            }



            //ViewBag.BillClientId = new SelectList(db.BillClient, "BillClientId", "BillType", orderDetails.BillClientId);
            return View(Bill);


        }
        public ActionResult Create(ICollection<Senef> Seneflist)
        {
              
            int CompanyId = 0;
       
            bool checke = false;
            decimal total = 0;
            List<Senef> Temp = new List<Senef>();
            foreach (Senef se in Seneflist)
            {
           
                Senef tempee = new Senef();
                checke = se.needthis;
                if (checke == true)
                {
                    Senef Senoof = db.Senef.Where(s => s.FirstName.Equals(se.FirstName)).FirstOrDefault();
                    if (Senoof == null)
                    {
                        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

                    }
                    tempee.SenefId = Senoof.SenefId;
                    tempee.FirstName = se.FirstName;
                    tempee.Quantity = se.Quantity;
                    if (se.ChangePrice == true)
                    {
                        tempee.Price = se.NewPrice;
                        total = total + (se.NewPrice * se.Quantity);
                    }
                    else
                    {


                        tempee.Price = Senoof.Price;
                        total = total + (Senoof.Price * se.Quantity);
                    }
                    
              
                    Temp.Add(tempee);
                    CompanyId = se.ClientID;

                }

            }


            if (CompanyId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var company = db.Company.Find(CompanyId);
            if (company == null)
            {
                return HttpNotFound();
            }


            var order = new BillCompany();
            TryUpdateModel(order);

            try
            {

                order.MoneyType = Seneflist.FirstOrDefault().MoneyType;
                order.cashtype = Seneflist.FirstOrDefault().cashtype;
                order.BillType = Seneflist.FirstOrDefault().BillType;
                order.FirstName = company.FirstName;
                

                order.Phone = company.Phone;
                

                order.CompanyId = company.CompanyId;
                order.OrderDate = DateTime.Now.AddHours(10);

                order.Total = total;
                ////Save Order
                db.BillCompany.Add(order);
                db.SaveChanges();
                ////Process the order


                int idds = CreateOrder(Temp, order);

                return RedirectToAction("Index", "OrderDetailsCompanies", new { Id = order.BillCompanyId, ClientId = CompanyId });

            }
            catch
            {
                //Invalid - redisplay with errors
                return View(order);
            }








            //if (ModelState.IsValid)
            //{

            //    db.BillClient.Add(billClient);
            //    db.SaveChanges();
            //    return RedirectToAction("Index");
            //}

            //ViewBag.ClientId = new SelectList(db.Client, "ClientId", "Title", billClient.ClientId);
            //return View();
        }
        private int CreateOrder(List<Senef> Temp, BillCompany order)
        {
            ApplicationDbContext storeDB = new ApplicationDbContext();
            decimal orderTotal = 0;


            //  var cartItems = GetCartItems();
            // Iterate over the items in the cart, 
            // adding the order details for each
            foreach (var item in Temp)
            {



                var orderDetail = new OrderDetailsCompany
                {

                    BillCompanyId=order.BillCompanyId,
                    ProductId = item.SenefId,
                    productName = item.FirstName,
                    CompanyId = order.CompanyId,
                    UnitPrice = item.Price,
                    Quantity = item.Quantity,
                    Total = item.Quantity * item.Price,
                    OrderDate = DateTime.Now.AddHours(10)




                };
                // Set the order total of the shopping cart
                orderTotal += (item.Quantity * item.Price);


                storeDB.OrderDetailsCompany.Add(orderDetail);

            }
            // Set the order's total to the orderTotal count
            order.Total = orderTotal;

            // Save the order
            storeDB.SaveChanges();

            // Return the OrderId as the confirmation number
            return order.BillCompanyId;

        }
        public ActionResult CreatEysal(Senef Seneflist)
        {
            int clientId = 0;
            decimal total = 0;
            List<Senef> Temp = new List<Senef>();
            Senef tempee = new Senef();

            Senef Senoof = db.Senef.Where(s => s.FirstName.Equals(Seneflist.FirstName)).FirstOrDefault();
            if (Senoof == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            }
            tempee.SenefId = Senoof.SenefId;
            tempee.FirstName = Seneflist.BillType;
            tempee.Quantity = Seneflist.Quantity;
            tempee.Price = Senoof.Price;
            total = Seneflist.Quantity;

            Temp.Add(tempee);
            clientId = Seneflist.ClientID;


            if (clientId == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var client = db.Company.Find(clientId);
            if (client == null)
            {
                return HttpNotFound();
            }


            var order = new BillCompany();
            TryUpdateModel(order);

            try
            {

                order.MoneyType = Seneflist.MoneyType;
                order.cashtype = "منه / مدين";
                order.BillType = "ايصال";
                order.FirstName = client.FirstName;
               

                order.Phone = client.Phone;


                order.CompanyId = client.CompanyId;
                order.OrderDate = DateTime.Now.AddHours(10);

                order.Total = total;
                ////Save Order
                db.BillCompany.Add(order);
                db.SaveChanges();
                ////Process the order


                int idds = CreateOrder(Temp, order);

                return RedirectToAction("Index", "OrderDetailsCompanies", new { Id = order.BillCompanyId, ClientId = client.CompanyId });

            }
            catch
            {
                //Invalid - redisplay with errors
                return View(order);
            }
        }