示例#1
0
        protected void NewLevelImageButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int     quantity = 0;
            decimal amount   = 0m;

            if (!int.TryParse(QuantityTextBox.Text, out quantity))
            {
                MessageBox1.ShowError("Quantity must be numeric.");
            }

            if (!decimal.TryParse(PriceTextBox.Text, out amount))
            {
                MessageBox1.ShowError("Price must be a monetary amount.");
            }

            List <ProductVolumeDiscount> volumeDiscounts = MTApp.CatalogServices.VolumeDiscounts.FindByProductId((string)ViewState["id"]);
            ProductVolumeDiscount        volumeDiscount  = null;

            foreach (ProductVolumeDiscount item in volumeDiscounts)
            {
                if (item.Qty == quantity)
                {
                    volumeDiscount = item;
                }
            }
            if (volumeDiscount == null)
            {
                volumeDiscount = new ProductVolumeDiscount();
            }

            volumeDiscount.DiscountType = ProductVolumeDiscountType.Amount;
            volumeDiscount.Amount       = amount;
            volumeDiscount.Qty          = quantity;
            volumeDiscount.ProductId    = (string)ViewState["id"];

            bool result = false;

            if (volumeDiscount.Bvin == string.Empty)
            {
                result = MTApp.CatalogServices.VolumeDiscounts.Create(volumeDiscount);
            }
            else
            {
                result = MTApp.CatalogServices.VolumeDiscounts.Update(volumeDiscount);
            }
            if (result)
            {
                MessageBox1.ShowOk("Volume Discount Updated");
                QuantityTextBox.Text = "";
                PriceTextBox.Text    = "";
            }
            else
            {
                MessageBox1.ShowError("Error occurred while inserting new volume discount");
            }
            BindGridViews();
        }
        /// <summary>
        ///     Allows the REST API to create or update a product volume discount
        /// </summary>
        /// <param name="parameters">
        ///     Parameters passed in the URL of the REST API call. If there is a first parameter found in the
        ///     URL, the method will assume it is the product volume discount ID (bvin) and that this is an update, otherwise it
        ///     assumes to create a product volume discount.
        /// </param>
        /// <param name="querystring">
        ///     Name/value pairs from the REST API call querystring. This method does not expect any
        ///     querystrings.
        /// </param>
        /// <param name="postdata">Serialized (JSON) version of the ProductVolumeDiscountDTO object</param>
        /// <returns>ProductVolumeDiscountDTO - Serialized (JSON) version of the product volume discount</returns>
        public override string PostAction(string parameters, NameValueCollection querystring, string postdata)
        {
            var data     = string.Empty;
            var bvin     = FirstParameter(parameters);
            var response = new ApiResponse <ProductVolumeDiscountDTO>();

            ProductVolumeDiscountDTO postedItem = null;

            try
            {
                postedItem = Json.ObjectFromJson <ProductVolumeDiscountDTO>(postdata);
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                return(Json.ObjectToJson(response));
            }

            // return an error if the posted item isn't there
            if (postedItem == null)
            {
                response.Errors.Add(new ApiError("EXCEPTION", "Post data is missing or null"));
                return(Json.ObjectToJson(response));
            }

            var item = new ProductVolumeDiscount();

            item.FromDto(postedItem);

            if (bvin == string.Empty)
            {
                if (HccApp.CatalogServices.VolumeDiscounts.Create(item))
                {
                    bvin = item.Bvin;
                }
            }
            else
            {
                HccApp.CatalogServices.VolumeDiscounts.Update(item);
            }
            var resultItem = HccApp.CatalogServices.VolumeDiscounts.Find(bvin);

            if (resultItem != null)
            {
                response.Content = resultItem.ToDto();
            }

            data = Json.ObjectToJson(response);
            return(data);
        }
        // Create or Update
        public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata)
        {
            string data = string.Empty;
            string bvin = FirstParameter(parameters);
            ApiResponse <ProductVolumeDiscountDTO> response = new ApiResponse <ProductVolumeDiscountDTO>();

            ProductVolumeDiscountDTO postedItem = null;

            try
            {
                postedItem = MerchantTribe.Web.Json.ObjectFromJson <ProductVolumeDiscountDTO>(postdata);
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                return(MerchantTribe.Web.Json.ObjectToJson(response));
            }

            ProductVolumeDiscount item = new ProductVolumeDiscount();

            item.FromDto(postedItem);

            if (bvin == string.Empty)
            {
                if (MTApp.CatalogServices.VolumeDiscounts.Create(item))
                {
                    bvin = item.Bvin;
                }
            }
            else
            {
                MTApp.CatalogServices.VolumeDiscounts.Update(item);
            }
            ProductVolumeDiscount resultItem = MTApp.CatalogServices.VolumeDiscounts.Find(bvin);

            if (resultItem != null)
            {
                response.Content = resultItem.ToDto();
            }

            data = MerchantTribe.Web.Json.ObjectToJson(response);
            return(data);
        }
        // List or Find Single
        public override string GetAction(string parameters, System.Collections.Specialized.NameValueCollection querystring)
        {
            string data = string.Empty;

            // Find One Specific Category
            ApiResponse <ProductVolumeDiscountDTO> response = new ApiResponse <ProductVolumeDiscountDTO>();
            string bvin = FirstParameter(parameters);
            ProductVolumeDiscount item = MTApp.CatalogServices.VolumeDiscounts.Find(bvin);

            if (item == null)
            {
                response.Errors.Add(new ApiError("NULL", "Could not locate that item. Check bvin and try again."));
            }
            else
            {
                response.Content = item.ToDto();
            }
            data = MerchantTribe.Web.Json.ObjectToJson(response);

            return(data);
        }
        protected void btnNewLevel_Click(object sender, EventArgs e)
        {
            var quantity = 0;
            var amount   = 0m;
            var hasError = false;

            if (!int.TryParse(txtQuantity.Text, out quantity))
            {
                ucMessageBox.ShowError(Localization.GetString("QuantityError"));
                hasError = true;
            }

            if (!decimal.TryParse(txtPrice.Text, out amount))
            {
                ucMessageBox.ShowError(Localization.GetString("PriceError"));
                hasError = true;
            }

            if (!hasError)
            {
                var volumeDiscounts =
                    HccApp.CatalogServices.VolumeDiscounts.FindByProductId(ProductId);
                ProductVolumeDiscount volumeDiscount = null;
                foreach (var item in volumeDiscounts)
                {
                    if (item.Qty == quantity)
                    {
                        volumeDiscount = item;
                    }
                }
                if (volumeDiscount == null)
                {
                    volumeDiscount = new ProductVolumeDiscount();
                }

                volumeDiscount.DiscountType = ProductVolumeDiscountType.Amount;
                volumeDiscount.Amount       = Money.RoundCurrency(amount);
                volumeDiscount.Qty          = quantity;
                volumeDiscount.ProductId    = ProductId;

                var result = false;
                if (string.IsNullOrEmpty(volumeDiscount.Bvin))
                {
                    result = HccApp.CatalogServices.VolumeDiscounts.Create(volumeDiscount);
                }
                else
                {
                    result = HccApp.CatalogServices.VolumeDiscounts.Update(volumeDiscount);
                }
                if (result)
                {
                    ucMessageBox.ShowOk(Localization.GetString("VolumeDiscountAdded"));
                    txtQuantity.Text = string.Empty;
                    txtPrice.Text    = string.Empty;
                }
                else
                {
                    ucMessageBox.ShowError(Localization.GetString("AddError"));
                }
                BindGridViews();
            }
        }