public DiscountCode get_by_id(string id = "")
        {
            // Create the post to return
            DiscountCode post = DiscountCode.GetOneById(id);

            // Return the post
            return(post);
        } // End of the get_by_id method
Exemplo n.º 2
0
        public ActionResult edit(string id = "", string returnUrl = "")
        {
            // Get the current domain
            Domain currentDomain = Tools.GetCurrentDomain();
            ViewBag.CurrentDomain = currentDomain;

            // Get query parameters
            ViewBag.QueryParams = new QueryParams(returnUrl);

            // Check if the administrator is authorized
            if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true)
            {
                ViewBag.AdminSession = true;
            }
            else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true)
            {
                ViewBag.AdminSession = true;
                ViewBag.AdminErrorCode = 1;
                ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC");
                return View("index");
            }
            else
            {
                // Redirect the user to the start page
                return RedirectToAction("index", "admin_login");
            }

            // Add data to the view
            ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC");
            ViewBag.Languages = Language.GetAll(currentDomain.back_end_language, "name", "ASC");
            ViewBag.DiscountCode = DiscountCode.GetOneById(id);
            ViewBag.ReturnUrl = returnUrl;

            // Create a new empty discount code post if the discount code does not exist
            if (ViewBag.DiscountCode == null)
            {
                // Add data to the view
                ViewBag.DiscountCode = new DiscountCode();
            }

            // Return the edit view
            return View("edit");

        } // End of the edit method
        public HttpResponseMessage update(DiscountCode post)
        {
            // Check for errors
            if (post == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null"));
            }
            else if (Language.MasterPostExists(post.language_id) == false)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist"));
            }
            else if (Currency.MasterPostExists(post.currency_code) == false)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The currency does not exist"));
            }

            // Make sure that the data is valid
            post.id                  = AnnytabDataValidation.TruncateString(post.id, 50);
            post.discount_value      = AnnytabDataValidation.TruncateDecimal(post.discount_value, 0, 9.999M);
            post.end_date            = AnnytabDataValidation.TruncateDateTime(post.end_date);
            post.minimum_order_value = AnnytabDataValidation.TruncateDecimal(post.minimum_order_value, 0, 999999999999.99M);

            // Get the saved post
            DiscountCode savedPost = DiscountCode.GetOneById(post.id);

            // Check if the post exists
            if (savedPost == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The record does not exist"));
            }

            // Update the post
            DiscountCode.Update(post);

            // Return the success response
            return(Request.CreateResponse <string>(HttpStatusCode.OK, "The update was successful"));
        } // End of the update method
Exemplo n.º 4
0
    } // End of the ClearShoppingCart method

    /// <summary>
    /// Set the discount code and modify the shopping cart
    /// </summary>
    /// <param name="discountCodeId">The discount code id as a string</param>
    public static void SetDiscountCode(string discountCodeId)
    {
        // Get the current domain
        Domain domain = Tools.GetCurrentDomain();

        // Get the discount code
        DiscountCode discountCode = DiscountCode.GetOneById(discountCodeId);

        // Get the currency
        Currency currency = Currency.GetOneById(domain.currency);

        // Calculate the decimal multiplier
        Int32 decimalMultiplier = (Int32)Math.Pow(10, currency.decimals);

        // Check if there is errors with the discount code
        if (discountCode == null)
        {
            // The discount code does not exist
            HttpContext.Current.Session["CodeError"] = "invalid_discount_code";
            return;
        }
        else if (DateTime.UtcNow.AddDays(-1) > discountCode.end_date)
        {
            // The discount code is not valid anymore
            HttpContext.Current.Session["CodeError"] = "invalid_discount_code";
            return;
        }
        else if (discountCode.language_id != domain.front_end_language)
        {
            // The discount code is not valid for the language
            HttpContext.Current.Session["CodeError"] = "invalid_discount_code";
            return;
        }
        else if (discountCode.currency_code != currency.currency_code)
        {
            // The discount code is not valid for the currency
            HttpContext.Current.Session["CodeError"] = "invalid_discount_code";
            return;
        }
        else
        {
            // Get the signed-in customer
            Customer customer = Customer.GetSignedInCustomer();

            // Check if the discount code already is used by the customer
            if (discountCode.once_per_customer == true && customer == null)
            {
                // The discount code does not exist
                HttpContext.Current.Session["CodeError"] = "customer_not_signed_in";
                return;
            }
            else if (discountCode.once_per_customer == true && Order.GetOneByDiscountCodeAndCustomerId(discountCodeId, customer.id) != null)
            {
                // The discount code is already used
                HttpContext.Current.Session["CodeError"] = "invalid_discount_code";
                return;
            }

            // Get the shopping cart
            Dictionary <string, CartItem> shoppingCart = (Dictionary <string, CartItem>)HttpContext.Current.Session["ShoppingCart"];

            // Make sure that the shopping cart not is null
            if (shoppingCart == null)
            {
                // Shopping cart is empty
                HttpContext.Current.Session["CodeError"] = "empty_shopping_cart";
                return;
            }

            // Calculate the order sum
            decimal net_sum = 0;
            foreach (KeyValuePair <string, CartItem> post in shoppingCart)
            {
                // Get the cart item
                CartItem itemInCart = post.Value;

                // Add to the net sum
                net_sum += itemInCart.quantity * itemInCart.unit_price;
            }

            // Check the order minimum
            if (net_sum < discountCode.minimum_order_value)
            {
                HttpContext.Current.Session["CodeError"] = "invalid_discount_code";
                return;
            }

            // Loop the shopping cart
            foreach (KeyValuePair <string, CartItem> post in shoppingCart)
            {
                // Get the cart item
                CartItem itemInCart = post.Value;

                // Check if we should remove the freight
                if (discountCode.free_freight == true)
                {
                    // Remove the freight
                    itemInCart.unit_freight = 0;
                }

                // Get the product
                Product product = Product.GetOneById(itemInCart.product_id, domain.front_end_language);

                // Apply the discount
                if (discountCode.exclude_products_on_sale == false || (discountCode.exclude_products_on_sale == true && product.discount <= 0M))
                {
                    itemInCart.unit_price = Math.Round(itemInCart.unit_price * (1 - discountCode.discount_value) * decimalMultiplier, MidpointRounding.AwayFromZero) / decimalMultiplier;
                }
            }

            // Set the discount code
            HttpContext.Current.Session["DiscountCodeId"] = discountCodeId;
            HttpContext.Current.Session.Remove("CodeError");
        }
    } // End of the SetDiscountCode method
Exemplo n.º 5
0
        public ActionResult edit(FormCollection collection)
        {
            // Get the current domain
            Domain currentDomain = Tools.GetCurrentDomain();
            ViewBag.CurrentDomain = currentDomain;

            // Get the return url
            string returnUrl = collection["returnUrl"];
            ViewBag.QueryParams = new QueryParams(returnUrl);

            // Check if the administrator is authorized
            if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true)
            {
                ViewBag.AdminSession = true;
            }
            else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true)
            {
                ViewBag.AdminSession = true;
                ViewBag.AdminErrorCode = 1;
                ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC");
                return View("index");
            }
            else
            {
                // Redirect the user to the start page
                return RedirectToAction("index", "admin_login");
            }

            // Get all the form values
            string id = collection["txtId"];
            Int32 language_id = Convert.ToInt32(collection["selectLanguage"]);
            string currency_code = collection["selectCurrency"];
            decimal discount_value = 0;
            decimal.TryParse(collection["txtDiscountValue"].Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture, out discount_value);
            bool free_freight = Convert.ToBoolean(collection["cbFreeFreight"]);
            bool once_per_customer = Convert.ToBoolean(collection["cbOncePerCustomer"]);
            bool exclude_products_on_sale = Convert.ToBoolean(collection["cbExcludeProductsOnSale"]);
            DateTime end_date = Convert.ToDateTime(collection["txtEndDate"]);
            decimal minimum_order_value = 0;
            decimal.TryParse(collection["txtMinimumOrderValue"].Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture, out minimum_order_value);

            // Get translated texts
            KeyStringList tt = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC");

            // Get the discount code
            DiscountCode discountCode = DiscountCode.GetOneById(id);
            bool postExists = true;

            // Check if the discount code exists
            if (discountCode == null)
            {
                // Create an empty discount code
                discountCode = new DiscountCode();
                postExists = false;
            }

            // Update values
            discountCode.id = id;
            discountCode.language_id = language_id;
            discountCode.currency_code = currency_code;
            discountCode.discount_value = discount_value;
            discountCode.free_freight = free_freight;
            discountCode.once_per_customer = once_per_customer;
            discountCode.exclude_products_on_sale = exclude_products_on_sale;
            discountCode.end_date = AnnytabDataValidation.TruncateDateTime(end_date);
            discountCode.minimum_order_value = minimum_order_value;

            // Create a error message
            string errorMessage = string.Empty;

            if (discountCode.id.Length == 0 || discountCode.id.Length > 50)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_certain_length"), tt.Get("id"), "1", "50") + "<br/>";
            }
            if (discountCode.language_id == 0)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_select_value"), tt.Get("language").ToLower()) + "<br/>";
            }
            if (discountCode.currency_code == "")
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_select_value"), tt.Get("currency").ToLower()) + "<br/>";
            }
            if (discountCode.discount_value < 0 || discountCode.discount_value > 9.999M)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_range"), tt.Get("discount"), "9.999") + "<br/>";
            }
            if (discountCode.minimum_order_value < 0 || discountCode.minimum_order_value > 999999999999.99M)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_range"), tt.Get("minimum_order_value"), "999 999 999 999.99") + "<br/>";
            }

            // Check if there is errors
            if (errorMessage == string.Empty)
            {
                // Check if we should add or update the discount code
                if (postExists == false)
                {
                    // Add the discount code
                    DiscountCode.Add(discountCode);
                }
                else
                {
                    // Update the discount code
                    DiscountCode.Update(discountCode);
                }

                // Redirect the user to the list
                return Redirect("/admin_discount_codes" + returnUrl);
            }
            else
            {
                // Set form values
                ViewBag.ErrorMessage = errorMessage;
                ViewBag.TranslatedTexts = tt;
                ViewBag.Languages = Language.GetAll(currentDomain.back_end_language, "id", "ASC");
                ViewBag.DiscountCode = discountCode;
                ViewBag.ReturnUrl = returnUrl;

                // Return the edit view
                return View("edit");
            }

        } // End of the edit method