private void BindConvertionGrid()
        {
            ShippingConvertionBiz biz = new ShippingConvertionBiz();

            gvConvertion.DataSource = biz.GetAllConvertionByType(ShippingConvertionType.DHL);
            gvConvertion.DataBind();
        }
        protected void gvConvertion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = (int)gvConvertion.DataKeys[e.RowIndex]["ID"];
            ShippingConvertionBiz biz = new ShippingConvertionBiz();

            biz.DeleteConvertion(id);
            BindConvertionGrid();
        }
        protected void gvConvertion_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateConvertion")
            {
                int         index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row   = gvConvertion.Rows[index];

                HiddenField hdID            = row.FindControl("hdID") as HiddenField;
                TextBox     txtFrom         = row.FindControl("txtFrom") as TextBox;
                TextBox     txtTo           = row.FindControl("txtTo") as TextBox;
                TextBox     txtChargeWeight = row.FindControl("txtChargeWeight") as TextBox;

                int id, priceFrom, priceTo, chargeWeight;
                if (!Int32.TryParse(hdID.Value, out id))
                {
                    return;
                }
                if (!Int32.TryParse(txtFrom.Text, out priceFrom))
                {
                    return;
                }
                if (!Int32.TryParse(txtTo.Text, out priceTo))
                {
                    return;
                }
                if (!Int32.TryParse(txtChargeWeight.Text, out chargeWeight))
                {
                    return;
                }

                ShippingConvertionBiz biz = new ShippingConvertionBiz();
                if (biz.UpdateConvertion(id, priceFrom, priceTo, chargeWeight, ShippingConvertionType.DHL))
                {
                    BindConvertionGrid();
                }
            }
        }
        protected void btnAddConvertion_Click(object sender, EventArgs e)
        {
            ShippingConvertionBiz convertionBiz = new ShippingConvertionBiz();
            int priceFrom, priceTo, chargeWeight;

            if (!string.IsNullOrEmpty(txtPriceFrom.Text))
            {
                if (!Int32.TryParse(txtPriceFrom.Text, out priceFrom))
                {
                    return;
                }
            }
            else
            {
                priceFrom = 0;
            }

            if (!string.IsNullOrEmpty(txtPriceTo.Text))
            {
                if (!Int32.TryParse(txtPriceTo.Text, out priceTo))
                {
                    return;
                }
            }
            else
            {
                priceTo = Int32.MaxValue;
            }

            if (!Int32.TryParse(txtChargeWeight.Text, out chargeWeight))
            {
                return;
            }
            convertionBiz.AddConvertion(priceFrom, priceTo, chargeWeight, ShippingConvertionType.DHL);
            ResetConvertionAdd();
            BindConvertionGrid();
        }
        string CreateRequest(string siteid, string password, string cityFrom, string divisionFrom, ShipmentPackage shipmentPackage)
        {
            var usedMeasureWeight = MeasureManager.GetMeasureWeightBySystemKeyword(MEASUREWEIGHTSYSTEMKEYWORD);

            if (usedMeasureWeight == null)
            {
                throw new NopException(string.Format("UPS shipping service. Could not load \"{0}\" measure weight", MEASUREWEIGHTSYSTEMKEYWORD));
            }

            var usedMeasureDimension = MeasureManager.GetMeasureDimensionBySystemKeyword(MEASUREDIMENSIONSYSTEMKEYWORD);

            if (usedMeasureDimension == null)
            {
                throw new NopException(string.Format("UPS shipping service. Could not load \"{0}\" measure dimension", MEASUREDIMENSIONSYSTEMKEYWORD));
            }

            int     length = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(shipmentPackage.GetTotalLength(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
            int     height = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(shipmentPackage.GetTotalHeight(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
            int     width  = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(shipmentPackage.GetTotalWidth(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
            int     weight;
            decimal totalPrice = decimal.Zero;

            foreach (ShoppingCartItem item in shipmentPackage.Items)
            {
                totalPrice += PriceHelper.GetSubTotal(item, true);
            }
            ShippingConvertionBiz convertionBiz = new ShippingConvertionBiz();

            weight = convertionBiz.GetWeightFromTotalPrice(totalPrice, ShippingConvertionType.DHL);

            if (length < 1)
            {
                length = 1;
            }
            if (height < 1)
            {
                height = 1;
            }
            if (width < 1)
            {
                width = 1;
            }
            if (weight < 1)
            {
                weight = 1;
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.Append("<req:ShipmentBookRatingRequest xmlns:req=\"http://www.dhl.com\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.dhl.com ship-book-rate-req.xsd\">");

            // header
            sb.Append("<Request>");
            sb.Append("<ServiceHeader>");
            sb.Append(string.Format("<MessageTime>{0}</MessageTime>", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz")));
            sb.Append(string.Format("<MessageReference>{0}</MessageReference>", Guid.NewGuid().ToString().Replace("-", "")));
            sb.Append(string.Format("<SiteID>{0}</SiteID>", siteid));
            sb.Append(string.Format("<Password>{0}</Password>", password));
            sb.Append("</ServiceHeader>");
            sb.Append("</Request>");

            //Shipper
            sb.Append("<Shipper>");
            sb.Append(string.Format("<City>{0}</City>", cityFrom));
            sb.Append(string.Format("<Division>{0}</Division>", divisionFrom));
            sb.Append(string.Format("<PostalCode>{0}</PostalCode>", shipmentPackage.ZipPostalCodeFrom));
            sb.Append(string.Format("<CountryCode>{0}</CountryCode>", shipmentPackage.CountryFrom.TwoLetterIsoCode));
            sb.Append("</Shipper>");

            //Consignee
            sb.Append("<Consignee>");
            sb.Append(string.Format("<City>{0}</City>", shipmentPackage.ShippingAddress.City));
            sb.Append(string.Format("<Division>{0}</Division>", shipmentPackage.ShippingAddress.StateProvince != null ? shipmentPackage.ShippingAddress.StateProvince.Name : string.Empty));
            sb.Append(string.Format("<PostalCode>{0}</PostalCode>", shipmentPackage.ShippingAddress.ZipPostalCode));
            sb.Append(string.Format("<CountryCode>{0}</CountryCode>", shipmentPackage.ShippingAddress.Country.TwoLetterIsoCode));
            sb.Append("</Consignee>");

            //Shipment Details
            sb.Append("<ShipmentDetails>");
            sb.Append("<NumberOfPieces>1</NumberOfPieces>");
            sb.Append("<Pieces>");
            sb.Append("<Piece>");
            sb.Append("<PieceID>1</PieceID>");
            sb.Append("<PackageType>OD</PackageType>");
            sb.Append(string.Format("<Weight>{0}</Weight>", weight));
            sb.Append("<DimWeight>1</DimWeight>");
            sb.Append(string.Format("<Width>{0}</Width>", width));
            sb.Append(string.Format("<Height>{0}</Height>", height));
            sb.Append(string.Format("<Depth>{0}</Depth>", length));
            sb.Append("</Piece>");
            sb.Append("</Pieces>");
            sb.Append("<WeightUnit>L</WeightUnit>");
            sb.Append("<DimensionUnit>I</DimensionUnit>");
            sb.Append(string.Format("<Weight>{0}</Weight>", weight));
            sb.Append(string.Format("<ProductCode>{0}</ProductCode>", ProductCodeToken));
            sb.Append("</ShipmentDetails>");


            sb.Append("</req:ShipmentBookRatingRequest>");
            return(sb.ToString());
        }
        private string CreateRequest(string AccessKey, string Username, string Password,
                                     ShipmentPackage ShipmentPackage, UPSCustomerClassification customerClassification,
                                     UPSPickupType pickupType, UPSPackagingType packagingType)
        {
            var usedMeasureWeight = MeasureManager.GetMeasureWeightBySystemKeyword(MEASUREWEIGHTSYSTEMKEYWORD);

            if (usedMeasureWeight == null)
            {
                throw new NopException(string.Format("UPS shipping service. Could not load \"{0}\" measure weight", MEASUREWEIGHTSYSTEMKEYWORD));
            }

            var usedMeasureDimension = MeasureManager.GetMeasureDimensionBySystemKeyword(MEASUREDIMENSIONSYSTEMKEYWORD);

            if (usedMeasureDimension == null)
            {
                throw new NopException(string.Format("UPS shipping service. Could not load \"{0}\" measure dimension", MEASUREDIMENSIONSYSTEMKEYWORD));
            }

            int length = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(ShipmentPackage.GetTotalLength(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
            int height = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(ShipmentPackage.GetTotalHeight(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));
            int width  = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertDimension(ShipmentPackage.GetTotalWidth(), MeasureManager.BaseDimensionIn, usedMeasureDimension)));

            #region modify as customer request: convert total items price to weight to calculate shipping fee
            int     weight;// = Convert.ToInt32(Math.Ceiling(MeasureManager.ConvertWeight(ShippingManager.GetShoppingCartTotalWeigth(ShipmentPackage.Items, ShipmentPackage.Customer), MeasureManager.BaseWeightIn, usedMeasureWeight)));
            decimal totalPrice = decimal.Zero;
            foreach (ShoppingCartItem item in ShipmentPackage.Items)
            {
                totalPrice += PriceHelper.GetSubTotal(item, true);
            }
            ShippingConvertionBiz convertionBiz = new ShippingConvertionBiz();
            weight = convertionBiz.GetWeightFromTotalPrice(totalPrice, ShippingConvertionType.UPS);
            #endregion

            if (length < 1)
            {
                length = 1;
            }
            if (height < 1)
            {
                height = 1;
            }
            if (width < 1)
            {
                width = 1;
            }
            if (weight < 1)
            {
                weight = 1;
            }

            string zipPostalCodeFrom = ShipmentPackage.ZipPostalCodeFrom;
            string zipPostalCodeTo   = ShipmentPackage.ShippingAddress.ZipPostalCode;
            string countryCodeFrom   = ShipmentPackage.CountryFrom.TwoLetterIsoCode;
            string countryCodeTo     = ShipmentPackage.ShippingAddress.Country.TwoLetterIsoCode;

            var sb = new StringBuilder();
            sb.Append("<?xml version='1.0'?>");
            sb.Append("<AccessRequest xml:lang='en-US'>");
            sb.AppendFormat("<AccessLicenseNumber>{0}</AccessLicenseNumber>", AccessKey);
            sb.AppendFormat("<UserId>{0}</UserId>", Username);
            sb.AppendFormat("<Password>{0}</Password>", Password);
            sb.Append("</AccessRequest>");
            sb.Append("<?xml version='1.0'?>");
            sb.Append("<RatingServiceSelectionRequest xml:lang='en-US'>");
            sb.Append("<Request>");
            sb.Append("<TransactionReference>");
            sb.Append("<CustomerContext>Bare Bones Rate Request</CustomerContext>");
            sb.Append("<XpciVersion>1.0001</XpciVersion>");
            sb.Append("</TransactionReference>");
            sb.Append("<RequestAction>Rate</RequestAction>");
            sb.Append("<RequestOption>Shop</RequestOption>");
            sb.Append("</Request>");
            sb.Append("<PickupType>");
            sb.AppendFormat("<Code>{0}</Code>", GetPickupTypeCode(pickupType));
            sb.Append("</PickupType>");
            sb.Append("<CustomerClassification>");
            sb.AppendFormat("<Code>{0}</Code>", GetCustomerClassificationCode(customerClassification));
            sb.Append("</CustomerClassification>");
            sb.Append("<Shipment>");
            sb.Append("<Shipper>");
            sb.Append("<Address>");
            sb.AppendFormat("<PostalCode>{0}</PostalCode>", zipPostalCodeFrom);
            sb.AppendFormat("<CountryCode>{0}</CountryCode>", countryCodeFrom);
            sb.Append("</Address>");
            sb.Append("</Shipper>");
            sb.Append("<ShipTo>");
            sb.Append("<Address>");
            sb.Append("<ResidentialAddressIndicator/>");
            sb.AppendFormat("<PostalCode>{0}</PostalCode>", zipPostalCodeTo);
            sb.AppendFormat("<CountryCode>{0}</CountryCode>", countryCodeTo);
            sb.Append("</Address>");
            sb.Append("</ShipTo>");
            sb.Append("<ShipFrom>");
            sb.Append("<Address>");
            sb.AppendFormat("<PostalCode>{0}</PostalCode>", zipPostalCodeFrom);
            sb.AppendFormat("<CountryCode>{0}</CountryCode>", countryCodeFrom);
            sb.Append("</Address>");
            sb.Append("</ShipFrom>");
            sb.Append("<Service>");
            sb.Append("<Code>03</Code>");
            sb.Append("</Service>");


            if ((!IsPackageTooHeavy(weight)) && (!IsPackageTooLarge(length, height, width)))
            {
                sb.Append("<Package>");
                sb.Append("<PackagingType>");
                sb.AppendFormat("<Code>{0}</Code>", GetPackagingTypeCode(packagingType));
                sb.Append("</PackagingType>");
                sb.Append("<Dimensions>");
                sb.AppendFormat("<Length>{0}</Length>", length);
                sb.AppendFormat("<Width>{0}</Width>", width);
                sb.AppendFormat("<Height>{0}</Height>", height);
                sb.Append("</Dimensions>");
                sb.Append("<PackageWeight>");
                sb.AppendFormat("<Weight>{0}</Weight>", weight);
                sb.Append("</PackageWeight>");
                sb.Append("</Package>");
            }
            else
            {
                int totalPackages        = 1;
                int totalPackagesDims    = 1;
                int totalPackagesWeights = 1;
                if (IsPackageTooHeavy(weight))
                {
                    totalPackagesWeights = Convert.ToInt32(Math.Ceiling((decimal)weight / (decimal)MAXPACKAGEWEIGHT));
                }
                if (IsPackageTooLarge(length, height, width))
                {
                    totalPackagesDims = Convert.ToInt32(Math.Ceiling((decimal)TotalPackageSize(length, height, width) / (decimal)108));
                }
                totalPackages = totalPackagesDims > totalPackagesWeights ? totalPackagesDims : totalPackagesWeights;
                if (totalPackages == 0)
                {
                    totalPackages = 1;
                }

                int weight2 = weight / totalPackages;
                int height2 = height / totalPackages;
                int width2  = width / totalPackages;
                int length2 = length / totalPackages;
                if (weight2 < 1)
                {
                    weight2 = 1;
                }
                if (height2 < 1)
                {
                    height2 = 1;
                }
                if (width2 < 1)
                {
                    width2 = 1;
                }
                if (length2 < 1)
                {
                    length2 = 1;
                }

                for (int i = 0; i < totalPackages; i++)
                {
                    sb.Append("<Package>");
                    sb.Append("<PackagingType>");
                    sb.AppendFormat("<Code>{0}</Code>", GetPackagingTypeCode(packagingType));
                    sb.Append("</PackagingType>");
                    sb.Append("<Dimensions>");
                    sb.AppendFormat("<Length>{0}</Length>", length2);
                    sb.AppendFormat("<Width>{0}</Width>", width2);
                    sb.AppendFormat("<Height>{0}</Height>", height2);
                    sb.Append("</Dimensions>");
                    sb.Append("<PackageWeight>");
                    sb.AppendFormat("<Weight>{0}</Weight>", weight2);
                    sb.Append("</PackageWeight>");
                    sb.Append("</Package>");
                }
            }


            sb.Append("</Shipment>");
            sb.Append("</RatingServiceSelectionRequest>");
            string requestString = sb.ToString();
            return(requestString);
        }