/// <summary>
        /// Occurs when the User Types Grid View Selected item is deleted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvUserTypes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                lblServerSideError.Text = "&nbsp";
                lblServerSideErrorBottom.Text = "";

                UserTypeDelete myResult = new UserTypesLogic().DeleteUserType(Convert.ToInt32(e.Keys[0]));

                if (myResult == UserTypeDelete.LockedUserType)
                {
                    lblServerSideError.Text = "User Type is Locked and Cannot be Deleted";
                }
                else if (myResult == UserTypeDelete.HasUsers)
                {
                    lblServerSideError.Text = "User Type is Bound to One or More Users and Cannot be Deleted";
                }
                else
                {
                    gvUserTypes.SelectedIndex = -1;
                    btnAdd.Visible = true;
                    btnUpdate.Visible = false;
                    txtUserType.Text = "";
                    hdnID.Value = "";
                    gvUserTypes.DataSource = new UserTypesLogic().RetrieveAllUserTypes();
                    gvUserTypes.DataBind();
                }
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        public string UpdateUserType(string UserTypeID, string UserType)
        {
            try
            {
                if (Context.User.IsInRole("Administrator"))
                {
                    int myUserTypeID = Convert.ToInt32(UserTypeID.Trim());
                    string myUserType = UserType.Trim();

                    UserTypeUpdate myResult = new UserTypesLogic().UpdateUserType(myUserTypeID, myUserType);

                    if (myResult == UserTypeUpdate.LockedUserType)
                    {
                        return "User Type is Locked and Cannot be Modified";
                    }
                    else if (myResult == UserTypeUpdate.SameUserType)
                    {
                        return "User Type Already Exists";
                    }
                    else
                    {
                        return "";
                    }
                }
                else
                {
                    return "";
                }
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        public void AddItemToList(string ProductID, string Price, string Quantity)
        {
            try
            {
                if (Context.User.IsInRole("Administrator"))
                {
                    Guid myProductID = Guid.Parse(ProductID);
                    //double myPrice = Convert.ToDouble(Price.Replace('€', ' ').Trim());
                    int myQuantity = Convert.ToInt32(Quantity);

                    double myPrice = 0;

                    UserType myUserType = new UserTypesLogic().RetrieveUserTypeByName("Wholesaler");
                    UserTypeProduct myPriceType = new PriceTypesLogic().RetrievePriceTypeByID(myUserType.Id, myProductID);

                    if (myPriceType != null)
                    {
                        myPrice = myPriceType.Price;
                        double? NewPrice = 0;

                        if ((myPriceType.DiscountDateFrom != null) && (myPriceType.DiscountDateTo != null) && (myPriceType.DiscountPercentage != null))
                        {
                            if ((DateTime.Now >= myPriceType.DiscountDateFrom) && (DateTime.Now <= myPriceType.DiscountDateTo))
                            {
                                NewPrice = myPriceType.Price - ((myPriceType.DiscountPercentage / 100) * myPriceType.Price);
                                myPrice = Convert.ToDouble(NewPrice);
                            }
                        }
                    }

                    List<OrderItem> myList = (List<OrderItem>)Session["SupplierOrder"];

                    OrderItem myOrderItem = new OrderItem();

                    myOrderItem.Id = myProductID;
                    myOrderItem.Price = myPrice;
                    myOrderItem.Quantity = myQuantity;

                    myList.Add(myOrderItem);

                    Session["SupplierOrder"] = myList;
                }
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        public string RetrieveProductPrice(string ProductID)
        {
            try
            {
                if (Context.User.IsInRole("Administrator"))
                {
                    Guid myProductID = Guid.Parse(ProductID);

                    UserType myUserType = new UserTypesLogic().RetrieveUserTypeByName("Wholesaler");
                    UserTypeProduct myPriceType = new PriceTypesLogic().RetrievePriceTypeByID(myUserType.Id, myProductID);

                    string Price = "";

                    if (myPriceType != null)
                    {
                        Price = myPriceType.Price.ToString("F");
                        double? NewPrice = 0;

                        if ((myPriceType.DiscountDateFrom != null) && (myPriceType.DiscountDateTo != null) && (myPriceType.DiscountPercentage != null))
                        {
                            if ((DateTime.Now >= myPriceType.DiscountDateFrom) && (DateTime.Now <= myPriceType.DiscountDateTo))
                            {
                                NewPrice = myPriceType.Price - ((myPriceType.DiscountPercentage / 100) * myPriceType.Price);

                                string myDisplayedNewPrice = Convert.ToDouble(NewPrice).ToString("F");

                                Price = myDisplayedNewPrice + " : " + myPriceType.DiscountPercentage + " % Off";
                            }
                        }
                    }

                    return Price;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        public string RetrieveItems()
        {
            try
            {
                if (Context.User.IsInRole("Administrator"))
                {
                    string HTML = "";

                    HTML += "<table id=\"Order\" cellpadding=\"4\">";

                    List<OrderItem> myList = (List<OrderItem>)Session["SupplierOrder"];

                    foreach (OrderItem myOrderItem in myList)
                    {
                        UserType myUserType = new UserTypesLogic().RetrieveUserTypeByName("Wholesaler");
                        UserTypeProduct myPriceType = new PriceTypesLogic().RetrievePriceTypeByID(myUserType.Id, myOrderItem.Id);

                        string PriceOutput = "";

                        if (myPriceType != null)
                        {
                            PriceOutput = myPriceType.Price.ToString("F");
                            double? NewPrice = 0;

                            if ((myPriceType.DiscountDateFrom != null) && (myPriceType.DiscountDateTo != null) && (myPriceType.DiscountPercentage != null))
                            {
                                if ((DateTime.Now >= myPriceType.DiscountDateFrom) && (DateTime.Now <= myPriceType.DiscountDateTo))
                                {
                                    NewPrice = myPriceType.Price - ((myPriceType.DiscountPercentage / 100) * myPriceType.Price);

                                    string myDisplayedNewPrice = Convert.ToDouble(NewPrice).ToString("F");

                                    PriceOutput = myDisplayedNewPrice + " : " + myPriceType.DiscountPercentage + " % Off";
                                }
                            }
                        }

                        HTML += "<tr class=\"GridViewTuple\">";

                        HTML += "<td>";
                        HTML += new ProductsLogic().RetrieveProductByID(myOrderItem.Id.ToString()).Name;
                        HTML += "</td>";

                        HTML += "<td>";
                        HTML += "x " + myOrderItem.Quantity.ToString();
                        HTML += "</td>";

                        HTML += "<td> at </td>";

                        HTML += "<td>";
                        HTML += "€ " + PriceOutput;
                        HTML += "</td>";

                        HTML += "<td>";
                        HTML += "<input type=\"image\" class=\"ProductButton\" alt=\"\" productid=\"" + myOrderItem.Id.ToString() + "\" src=\"/images/Remove.jpg\" onclick=\"return false;\" />";
                        HTML += "</td>";

                        HTML += "</tr>";
                    }

                    double myTotalPrice = new CalculateTotalPrice().CalculateTotal(myList);

                    HTML += "<tr class=\"GridViewHeader\"><td></td><td>Total</td><td>=</td><td><div class=\"MiniFontBlueLeft\">€ " + myTotalPrice.ToString("F") + "<div></td></tr>";

                    HTML += "<table>";

                    return HTML;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        public string RetrievePossibleTypes(string UserType)
        {
            try
            {
                if (Context.User.IsInRole("Administrator"))
                {
                    string myUserType = UserType.Trim();

                    List<UserType> myUserTypes = new UserTypesLogic().RetrieveAllUserTypes().ToList();
                    string HTML = "";

                    foreach (UserType myType in myUserTypes)
                    {
                        if (myType.Type != myUserType)
                        {
                            HTML += "<option value=\"" + myType.Id + "\">" + myType.Type + "</option>";
                        }
                    }

                    return HTML;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        public string PopulateUserTypes()
        {
            try
            {
                if (Context.User.IsInRole("Administrator"))
                {
                    List<UserType> myUserTypes = new UserTypesLogic().RetrieveAllUserTypes().ToList();
                    string HTML = "<option value=\"0\"> Select </option>";

                    foreach (UserType myType in myUserTypes)
                    {
                        HTML += "<option value=\"" + myType.Id + "\">" + myType.Type + "</option>";
                    }

                    return HTML;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Context.User.Identity.IsAuthenticated)
                {
                    string myOrderID = Request.QueryString[0].ToString();
                    Guid myOrderGuid;

                    if (Guid.TryParse(myOrderID, out myOrderGuid))
                    {
                        Order myOrder = new OrdersLogic().RetrieveOrderByID(myOrderGuid);
                        IQueryable<OrderProduct> myOrderItems = new OrdersLogic().RetrieveItemsByOrderID(myOrderGuid);

                        bool HasAccess = false;

                        if(Context.User.IsInRole("Administrator"))
                        {
                            HasAccess = true;
                        }
                        else
                        {
                            if (myOrder.UserFK == new UsersLogic().RetrieveUserByUsername(Context.User.Identity.Name).Id)
                            {
                                HasAccess = true;
                            }
                        }

                        //User has access to the order
                        if (HasAccess)
                        {
                            string HTML = "<table style=\"font-family: Arial;\"  cellpadding=\"6\">";

                            HTML += "<tr>";
                                HTML += "<td>";
                                HTML += "Order ID: ";
                                HTML += "</td>";
                                HTML += "<td>";
                                HTML += myOrder.Id;
                                HTML += "</td>";
                            HTML += "</tr>";

                            if(myOrder.SupplierFK == null)
                            {
                                HTML += "<tr>";
                                HTML += "<td>";
                                HTML += "Name: ";
                                HTML += "</td>";
                                HTML += "<td>";
                                HTML += myOrder.User.Name + " " + myOrder.User.Surname;
                                HTML += "</td>";
                                HTML += "</tr>";

                                string[] Address = myOrder.User.StreetAddress.Split('|');

                                HTML += "<tr>";
                                HTML += "<td>";
                                HTML += "Address: ";
                                HTML += "</td>";
                                HTML += "<td>";
                                HTML += Address[0];
                                HTML += "</td>";
                                HTML += "</tr>";

                                HTML += "<tr>";
                                HTML += "<td>";
                                HTML += "</td>";
                                HTML += "<td>";
                                HTML += Address[1];
                                HTML += "</td>";
                                HTML += "</tr>";

                                HTML += "<tr>";
                                HTML += "<td>";
                                HTML += "Town: ";
                                HTML += "</td>";
                                HTML += "<td>";
                                HTML += myOrder.User.Town.Town1;
                                HTML += "</td>";
                                HTML += "</tr>";

                                HTML += "<tr>";
                                HTML += "<td>";
                                HTML += "Country: ";
                                HTML += "</td>";
                                HTML += "<td>";
                                HTML += myOrder.User.Town.Country.Country1;
                                HTML += "</td>";
                                HTML += "</tr>";
                            }
                            else
                            {
                                HTML += "<tr>";
                                HTML += "<td>";
                                HTML += "Supplier: ";
                                HTML += "</td>";
                                HTML += "<td>";
                                HTML += myOrder.Supplier.Supplier1;
                                HTML += "</td>";
                                HTML += "</tr>";
                            }

                            HTML += "<tr>";
                            HTML += "<td>";
                            HTML += "Status: ";
                            HTML += "</td>";
                            HTML += "<td>";
                            HTML += myOrder.OrderStatus.Status;
                            HTML += "</td>";

                            HTML += "</tr>";
                            HTML += "<tr>";
                            HTML += "<td>";
                            HTML += "Date Placed: ";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += myOrder.OrderDate;
                            HTML += "</td>";

                            HTML += "</tr>";

                            HTML += "</table>";

                            HTML += "<br/>";

                            HTML += "<table style=\"font-family: Arial;\"  cellpadding=\"6\">";

                            HTML += "<tr>";

                            HTML += "<td>";
                            HTML += "Product";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "Quantity";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "Price";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "VAT Rate";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "Discount (Incl.)";
                            HTML += "</td>";

                            HTML += "</tr>";

                            double TotalPrice = 0;
                            double TotalVat = 0;

                            foreach (OrderProduct myOrderItem in myOrderItems)
                            {
                                Product myProduct = new ProductsLogic().RetrieveProductByID(myOrderItem.ProductFK.ToString());

                                User myUser = null;
                                UserTypeProduct myPriceType = null;

                                if (myOrder.UserFK != null)
                                {
                                    myUser = new UsersLogic().RetrieveUserByID(Guid.Parse(myOrder.UserFK.ToString()));
                                    myPriceType = new PriceTypesLogic().RetrievePriceTypeByID(myUser.UserTypeFK, myProduct.Id);
                                }
                                else
                                {
                                    UserType myUserType = new UserTypesLogic().RetrieveUserTypeByName("Wholesaler");
                                    myPriceType = new PriceTypesLogic().RetrievePriceTypeByID(myUserType.Id, myProduct.Id);
                                }

                                HTML += "<tr>";

                                HTML += "<td>";
                                HTML += myProduct.Name;
                                HTML += "</td>";

                                HTML += "<td>";
                                HTML += " x " + myOrderItem.Quantity;
                                HTML += "</td>";

                                HTML += "<td>";
                                HTML += " at € " + myOrderItem.Price.ToString("F");
                                HTML += "</td>";

                                HTML += "<td>";
                                HTML += myProduct.Vatrate.Vatrate1 + "% VAT";
                                HTML += "</td>";

                                TotalPrice += myOrderItem.Price * myOrderItem.Quantity;
                                TotalVat += ((myProduct.Vatrate.Vatrate1 / 100) * (myOrderItem.Price * myOrderItem.Quantity));

                                HTML += "<td>";

                                if((myOrder.OrderDate >= myPriceType.DiscountDateFrom) && (myOrder.OrderDate <= myPriceType.DiscountDateTo))
                                {
                                    HTML += myPriceType.DiscountPercentage + "% Discount";
                                }

                                HTML += "</td>";

                                HTML += "</tr>";
                            }

                            HTML += "<tr>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "Subtotal : ";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "€ " + (TotalPrice - TotalVat).ToString("F");
                            HTML += "</td>";

                            HTML += "<tr>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "VAT : ";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "€ " + TotalVat.ToString("F");
                            HTML += "</td>";

                            HTML += "</tr>";

                            HTML += "<tr>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "Total : ";
                            HTML += "</td>";

                            HTML += "<td>";
                            HTML += "€ " + TotalPrice.ToString("F");
                            HTML += "</td>";

                            HTML += "</tr>";
                            HTML += "</table>";

                            lblOutput.Text = HTML;
                        }
                        else
                        {
                            Response.Redirect("~/default.aspx");
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/default.aspx");
                }

            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        /// <summary>
        /// Occurs when the UserTypes selected index is Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvUserTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblServerSideError.Text = "&nbsp";
                lblServerSideErrorBottom.Text = "";
                btnAdd.Visible = false;
                btnUpdate.Visible = true;

                UserType myUserType = new UserTypesLogic().RetrieveUserTypeByID(Convert.ToInt32(gvUserTypes.SelectedValue));

                hdnID.Value = myUserType.Id.ToString();
                txtUserType.Text = myUserType.Type;
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }