public ActionResult Address()
 {
     if (ModelState.IsValid)
     {
         using(CustomerAddressEntities caEntities = new CustomerAddressEntities())
         {
             int customerID = Convert.ToInt32(Session["customerID"]);
             ViewBag.customerAdddressinfo = caEntities.CustomerAddresses.Where(x => x.customer_ID == customerID).ToList();
         }
     }
     return View();
 }
 public ActionResult Address(CustomerAddress customerAddress)
 {
     if (ModelState.IsValid)
     {
         using (CustomerAddressEntities caEntities = new CustomerAddressEntities())
         {
             caEntities.Entry(customerAddress).State = EntityState.Modified;
             caEntities.SaveChanges();
             return RedirectToAction("Address");
         }
     }
     return View();
 }
 public ActionResult Register(Customer customer, CustomerAddress customerAddress, CustomerLogin customerLogin)
 {
     string viewChange = "";
     try
     {
         if (ModelState.IsValid)
         {
             //encrypt the password
             customerLogin.password = HashKey.GetHashKey(customerLogin.password);
             //inserting into customers table
             using (CustomerEntities cEntityy = new CustomerEntities())
             {
                 cEntityy.Customers.Add(customer);
                 cEntityy.SaveChanges();
             }
             using (CustomerAddressEntities caEntity = new CustomerAddressEntities())
             {
                 customerAddress.customer_ID = customer.customer_ID;
                 //inserting into customer address table
                 caEntity.CustomerAddresses.Add(customerAddress);
                 caEntity.SaveChanges();
             }
             using (CustomerLoginEntities clEntity = new CustomerLoginEntities())
             {
                 customerLogin.customer_ID = customer.customer_ID;
                 customerLogin.email = customer.email;
                 customerLogin.dateCreated = DateTime.Now;
                 //inserting into customer login table
                 clEntity.CustomerLogins.Add(customerLogin);
                 clEntity.SaveChanges();
                 return RedirectToAction("Index", "Customer");
             }
         }
         else
         {
             viewChange = "LoginRegister";
         }
     }
     catch(EntryPointNotFoundException ex){}
     catch (Exception ex) { viewChange = "LoginRegister"; }
     return View(viewChange);
 }
        public ActionResult ProcessCheckout(ShippingAddress shippingaddress,ShippingMethod shippingMethod,CustomerBilling customerBilling, Order order, OrderDetail orderDetail, OrderStatu orderstatus)
        {
            int customerID = Convert.ToInt32(Session["customerID"]);
            var customerEmail = Session["customerEmail"].ToString();
            try
            {
                using (ShippingAddressEntities shippingEntities = new ShippingAddressEntities())
                {
                    CustomerAddressEntities cAddentity = new CustomerAddressEntities();

                    List<CustomerAddress> customeraddress = cAddentity.CustomerAddresses.Where(x => x.customerAddress_ID == customerID).ToList();
                    shippingaddress.customer_ID = customerID;
                    shippingaddress.CustomerAddress_ID = customeraddress[0].customerAddress_ID;
                    shippingEntities.ShippingAddresses.Add(shippingaddress);
                    shippingEntities.SaveChanges();
                }
                using(CustomerBillingEntities customerBillingentity = new CustomerBillingEntities())
                {
                    customerBilling.customer_ID = customerID;
                    customerBillingentity.CustomerBillings.Add(customerBilling);
                    customerBillingentity.SaveChanges();
                }
                //loop through products in the cart session
                //the information is used to populate database
                foreach (Item item in (List<Item>)Session["cart"])
                {
                    using (OrderDetailsEntities orderDetailsEntities = new OrderDetailsEntities())
                    {
                        orderDetail.product_ID = item.Pr.product_ID;
                        orderDetail.quantity = item.Pr.quantity;
                        orderDetail.total = item.Pr.quantity * item.Pr.price;
                        orderDetailsEntities.OrderDetails.Add(orderDetail);
                        orderDetailsEntities.SaveChanges();
                    }
                    using (OrdersEntities ordersEntity = new OrdersEntities())
                    {
                        decimal total = item.Pr.quantity * item.Pr.price;
                        order.orderDetails_ID = orderDetail.orderDetails_ID;
                        order.customer_ID = customerID;
                        order.orderStatus_ID = 1; //pending order look at table OrderStatus
                        order.customerBilling_ID = customerBilling.customerBilling_ID;
                        order.shippingAddress_ID = shippingaddress.shippingAddress_ID;
                        order.shippingMethod_ID = shippingMethod.shippingMethod_ID;
                        order.customerBilling_ID = customerBilling.customerBilling_ID;
                        order.orderDate = DateTime.Now;
                        order.tax = total  * Convert.ToDecimal(.07);
                        order.grandTotal = order.tax + total;
                        ordersEntity.Orders.Add(order);
                        ordersEntity.SaveChanges();
                    }
                }
                return RedirectToAction("Sent");
            }
            catch(SmtpException ex)
            {
                return View("Checkout");
            }
            catch (Exception ex)
            {
                return View("Checkout");
            }
            //possibly write an if statement if they are not logged in make them login
            //else go to the dashboard
            return RedirectToAction("Index","Home");
        }