/// <summary>
 /// Adds a Price Type
 /// Level: Data
 /// </summary>
 /// <param name="myPriceType">The Price Type to Add</param>
 public void AddPriceType(UserTypeProduct myPriceType)
 {
     try
     {
         Entities.AddToUserTypeProducts(myPriceType);
         Entities.SaveChanges();
     }
     catch (Exception Exception)
     {
         throw Exception;
     }
 }
        /// <summary>
        /// Removes a Specific Price Type
        /// Level: Data
        /// </summary>
        /// <param name="UserTypeFK">The User Type ID</param>
        /// <param name="ProductFK">The Product ID</param>
        public void RemoveSpecificPriceType(int UserTypeFK, Guid ProductFK)
        {
            try
            {
                UserTypeProduct myPriceType = RetrievePriceTypeByID(UserTypeFK, ProductFK);

                Entities.DeleteObject(myPriceType);
                Entities.SaveChanges();
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        /// <summary>
        /// Updates a Price Type
        /// Level: Data
        /// </summary>
        /// <param name="UserTypeFK">The User Type ID</param>
        /// <param name="ProductFK">The Product ID</param>
        /// <param name="Price">The Price</param>
        /// <param name="DiscountBegins">The Discount Start Date</param>
        /// <param name="DiscountEnds">The Discount End Date</param>
        /// <param name="DiscountPercent">The Discount Percentage</param>
        public void UpdatePriceType(int UserTypeFK, Guid ProductFK, double Price,
                                    DateTime?DiscountBegins, DateTime?DiscountEnds, double?DiscountPercent)
        {
            try
            {
                UserTypeProduct myPriceType = RetrievePriceTypeByID(UserTypeFK, ProductFK);

                myPriceType.Price              = Price;
                myPriceType.DiscountDateFrom   = DiscountBegins;
                myPriceType.DiscountDateTo     = DiscountEnds;
                myPriceType.DiscountPercentage = DiscountPercent;

                Entities.SaveChanges();
            }
            catch (Exception Exception)
            {
                throw Exception;
            }
        }
        /// <summary>
        /// Adds a new price type
        /// Level: Logic
        /// </summary>
        /// <param name="UserTypeFK">The UserTypeID</param>
        /// <param name="ProductFK">The Product ID</param>
        /// <param name="Price">The Price</param>
        /// <param name="DiscountBegins">Discount Start Date</param>
        /// <param name="DiscountEnds">Discount End Date</param>
        /// <param name="DiscountPercent">Discount Percentage</param>
        public void AddPriceType(int UserTypeFK, Guid ProductFK, double Price,
                                 DateTime?DiscountBegins, DateTime?DiscountEnds, double?DiscountPercent)
        {
            try
            {
                UserTypeProduct myPriceType = new UserTypeProduct();

                myPriceType.UserTypeFK         = UserTypeFK;
                myPriceType.ProductFK          = ProductFK;
                myPriceType.Price              = Price;
                myPriceType.DiscountDateFrom   = DiscountBegins;
                myPriceType.DiscountDateTo     = DiscountEnds;
                myPriceType.DiscountPercentage = DiscountPercent;

                new PriceTypesRepository().AddPriceType(myPriceType);
            }
            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;
            }
        }