/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <param name="Error">Error</param> /// <returns>Tax</returns> public decimal GetTaxRate(CalculateTaxRequest calculateTaxRequest, ref string Error) { if (calculateTaxRequest.Address == null) { Error = "Address is not set"; return 0; } decimal taxRate = decimal.Zero; int taxClassID = 0; if (calculateTaxRequest.TaxClassID > 0) { taxClassID = calculateTaxRequest.TaxClassID; } else { ProductVariant productVariant = calculateTaxRequest.Item; if (productVariant != null) { taxClassID = productVariant.TaxCategoryID; } } taxRate = GetTaxRate(calculateTaxRequest.Address, taxClassID); return taxRate; }
/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <param name="Error">Error</param> /// <returns>Tax</returns> public decimal GetTaxRate(CalculateTaxRequest calculateTaxRequest, ref string Error) { Address address = calculateTaxRequest.Address; if (address == null) { Error = "Billing address is not set"; return 0; } if (address.Country == null) { Error = "Country is not set"; return 0; } //************************************************************************************************************** // As a Registered StrikeIron user, you can authenticate to a StrikeIron web service with either a // UserID/Password combination or a License Key. If you wish to use a License Key, // assign this value to the UserID field and set the Password field to null. //************************************************************************************************************** string UserID = SettingManager.GetSettingValue("Tax.TaxProvider.StrikeIron.UserID"); string Password = SettingManager.GetSettingValue("Tax.TaxProvider.StrikeIron.Password"); //if (Password == " " || !String.IsNullOrEmpty(Password)) // Password = String.Empty; //************************************************************************************************************** decimal taxRate = decimal.Zero; if (address.Country.TwoLetterISOCode.ToLower() == "us") { if (String.IsNullOrEmpty(address.ZipPostalCode)) { Error = "Zip is not provided"; return 0; } taxRate = GetTaxRateUSA(address.ZipPostalCode, UserID, Password, ref Error); } else if (address.Country.TwoLetterISOCode.ToLower() == "ca") { if (address.StateProvince == null) { Error = "Province is not set"; return 0; } taxRate = GetTaxRateCanada(address.StateProvince.Abbreviation, UserID, Password, ref Error); } else { Error = "Tax can be calculated only for USA zip or Canada province"; return 0; } if (String.IsNullOrEmpty(Error)) { return taxRate * 100; } else { return 0; } }
/// <summary> /// Create request for tax calculation /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="taxClassId">Tax class identifier</param> /// <param name="customer">Customer</param> /// <returns>Package for tax calculation</returns> protected static CalculateTaxRequest CreateCalculateTaxRequest(ProductVariant productVariant, int taxClassId, Customer customer) { var calculateTaxRequest = new CalculateTaxRequest(); calculateTaxRequest.Customer = customer; calculateTaxRequest.Item = productVariant; calculateTaxRequest.TaxClassId = taxClassId; var basedOn = TaxManager.TaxBasedOn; if (basedOn == TaxBasedOnEnum.BillingAddress) { if (customer == null || customer.BillingAddress == null) { basedOn = TaxBasedOnEnum.DefaultAddress; } } if (basedOn == TaxBasedOnEnum.ShippingAddress) { if (customer == null || customer.ShippingAddress == null) { basedOn = TaxBasedOnEnum.DefaultAddress; } } Address address = null; switch (basedOn) { case TaxBasedOnEnum.BillingAddress: { address = customer.BillingAddress; } break; case TaxBasedOnEnum.ShippingAddress: { address = customer.ShippingAddress; } break; case TaxBasedOnEnum.DefaultAddress: { address = TaxManager.DefaultTaxAddress; } break; case TaxBasedOnEnum.ShippingOrigin: { address = ShippingManager.ShippingOrigin; } break; } calculateTaxRequest.Address = address; return(calculateTaxRequest); }
/// <summary> /// Create request for tax calculation /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="TaxClassID">Tax class identifier</param> /// <param name="customer">Customer</param> /// <returns>Package for tax calculation</returns> protected static CalculateTaxRequest CreateCalculateTaxRequest(ProductVariant productVariant, int TaxClassID, Customer customer) { CalculateTaxRequest calculateTaxRequest = new CalculateTaxRequest(); calculateTaxRequest.Customer = customer; calculateTaxRequest.Item = productVariant; calculateTaxRequest.TaxClassID = TaxClassID; TaxBasedOnEnum basedOn = TaxManager.TaxBasedOn; if (basedOn == TaxBasedOnEnum.BillingAddress) { if (customer == null || customer.BillingAddress == null) { basedOn = TaxBasedOnEnum.DefaultAddress; } } if (basedOn == TaxBasedOnEnum.ShippingAddress) { if (customer == null || customer.ShippingAddress == null) { basedOn = TaxBasedOnEnum.DefaultAddress; } } Address address = null; switch (basedOn) { case TaxBasedOnEnum.BillingAddress: { address = customer.BillingAddress; } break; case TaxBasedOnEnum.ShippingAddress: { address = customer.ShippingAddress; } break; case TaxBasedOnEnum.DefaultAddress: { address = TaxManager.DefaultTaxAddress; } break; case TaxBasedOnEnum.ShippingOrigin: { address = ShippingManager.ShippingOrigin; } break; } calculateTaxRequest.Address = address; return calculateTaxRequest; }
/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <param name="error">Error</param> /// <returns>Tax</returns> public decimal GetTaxRate(CalculateTaxRequest calculateTaxRequest, ref string error) { decimal taxRate = decimal.Zero; int taxClassID = 0; if (calculateTaxRequest.TaxClassId > 0) { taxClassID = calculateTaxRequest.TaxClassId; } else { var productVariant = calculateTaxRequest.Item; if (productVariant != null) { taxClassID = productVariant.TaxCategoryId; } } taxRate = GetTaxRate(taxClassID); return taxRate; }
/// <summary> /// Gets tax rate /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="TaxClassID">Tax class identifier</param> /// <param name="customer">Customer</param> /// <param name="Error">Error</param> /// <returns>Tax rate</returns> public static decimal GetTaxRate(ProductVariant productVariant, int TaxClassID, Customer customer, ref string Error) { bool isFreeTax = IsFreeTax(productVariant, customer); if (isFreeTax) { return(decimal.Zero); } CalculateTaxRequest calculateTaxRequest = CreateCalculateTaxRequest(productVariant, TaxClassID, customer); TaxProvider activeTaxProvider = TaxManager.ActiveTaxProvider; if (activeTaxProvider == null) { throw new NopException("Tax provider could not be loaded"); } ITaxProvider iTaxProvider = Activator.CreateInstance(Type.GetType(activeTaxProvider.ClassName)) as ITaxProvider; decimal taxRate = iTaxProvider.GetTaxRate(calculateTaxRequest, ref Error); return(taxRate); }
/// <summary> /// Gets tax rate /// </summary> /// <param name="calculateTaxRequest">Tax calculation request</param> /// <param name="Error">Error</param> /// <returns>Tax</returns> public decimal GetTaxRate(CalculateTaxRequest calculateTaxRequest, ref string Error) { return decimal.Zero; }
/// <summary> /// Create request for tax calculation /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="taxClassId">Tax class identifier</param> /// <param name="customer">Customer</param> /// <returns>Package for tax calculation</returns> protected CalculateTaxRequest CreateCalculateTaxRequest(ProductVariant productVariant, int taxClassId, Customer customer) { var calculateTaxRequest = new CalculateTaxRequest(); calculateTaxRequest.Customer = customer; calculateTaxRequest.Item = productVariant; calculateTaxRequest.TaxClassId = taxClassId; var basedOn = this.TaxBasedOn; if (basedOn == TaxBasedOnEnum.BillingAddress) { if (customer == null || customer.BillingAddress == null) { basedOn = TaxBasedOnEnum.DefaultAddress; } } if (basedOn == TaxBasedOnEnum.ShippingAddress) { if (customer == null || customer.ShippingAddress == null) { basedOn = TaxBasedOnEnum.DefaultAddress; } } Address address = null; switch (basedOn) { case TaxBasedOnEnum.BillingAddress: { address = customer.BillingAddress; } break; case TaxBasedOnEnum.ShippingAddress: { address = customer.ShippingAddress; } break; case TaxBasedOnEnum.DefaultAddress: { address = this.DefaultTaxAddress; } break; case TaxBasedOnEnum.ShippingOrigin: { address = IoC.Resolve<IShippingService>().ShippingOrigin; } break; } calculateTaxRequest.Address = address; return calculateTaxRequest; }