/// <summary>
 /// Gets tax rate
 /// </summary>
 /// <param name="calculateTaxRequest">Tax calculation request</param>
 /// <returns>Tax</returns>
 public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest)
 {
     var result = new CalculateTaxResult
     {
         TaxRate = GetTaxRate(calculateTaxRequest.TaxCategoryId)
     };
     return result;
 }
 /// <summary>
 /// Gets tax rate
 /// </summary>
 /// <param name="calculateTaxRequest">Tax calculation request</param>
 /// <returns>Tax</returns>
 public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest)
 {
     var result = new CalculateTaxResult()
     {
          TaxRate = decimal.Zero
     };
     return result;
 }
        /// <summary>
        /// Create request for tax calculation
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="taxCategoryId">Tax category identifier</param>
        /// <param name="customer">Customer</param>
        /// <returns>Package for tax calculation</returns>
        protected virtual CalculateTaxRequest CreateCalculateTaxRequest(Product product, 
            int taxCategoryId, Customer customer)
        {
            var calculateTaxRequest = new CalculateTaxRequest();
            calculateTaxRequest.Customer = customer;
            if (taxCategoryId > 0)
            {
                calculateTaxRequest.TaxCategoryId = taxCategoryId;
            }
            else
            {
                if (product != null)
                    calculateTaxRequest.TaxCategoryId = product.TaxCategoryId;
            }

            var basedOn = _taxSettings.TaxBasedOn;

            if (basedOn == TaxBasedOn.BillingAddress)
            {
                if (customer == null || customer.BillingAddress == null)
                {
                    basedOn = TaxBasedOn.DefaultAddress;
                }
            }
            if (basedOn == TaxBasedOn.ShippingAddress)
            {
                if (customer == null || customer.ShippingAddress == null)
                {
                    basedOn = TaxBasedOn.DefaultAddress;
                }
            }

            Address address = null;

            switch (basedOn)
            {
                case TaxBasedOn.BillingAddress:
                    {
                        address = customer.BillingAddress;
                    }
                    break;
                case TaxBasedOn.ShippingAddress:
                    {
                        address = customer.ShippingAddress;
                    }
                    break;
                case TaxBasedOn.DefaultAddress:
                default:
                    {
                        address = _addressService.GetAddressById(_taxSettings.DefaultTaxAddressId);
                    }
                    break;
            }

            calculateTaxRequest.Address = address;
            return calculateTaxRequest;
        }
        /// <summary>
        /// Gets tax rate
        /// </summary>
        /// <param name="calculateTaxRequest">Tax calculation request</param>
        /// <returns>Tax</returns>
        public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest)
        {
            var result = new CalculateTaxResult();

            if (calculateTaxRequest.Address == null)
            {
                result.Errors.Add("Address is not set");
                return result;
            }

            var taxRates = _taxRateService.GetAllTaxRates(calculateTaxRequest.TaxCategoryId,
                calculateTaxRequest.Address.Country != null ? calculateTaxRequest.Address.Country.Id: 0,
                calculateTaxRequest.Address.StateProvince != null ? calculateTaxRequest.Address.StateProvince.Id : 0,
                calculateTaxRequest.Address.ZipPostalCode);
            if (taxRates.Count > 0)
                result.TaxRate = taxRates[0].Percentage;

            return result;
        }
        /// <summary>
        /// Create request for tax calculation
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="taxCategoryId">Tax category identifier</param>
        /// <param name="customer">Customer</param>
        /// <returns>Package for tax calculation</returns>
        protected virtual CalculateTaxRequest CreateCalculateTaxRequest(Product product, 
            int taxCategoryId, Customer customer)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            var calculateTaxRequest = new CalculateTaxRequest();
            calculateTaxRequest.Customer = customer;
            if (taxCategoryId > 0)
            {
                calculateTaxRequest.TaxCategoryId = taxCategoryId;
            }
            else
            {
                if (product != null)
                    calculateTaxRequest.TaxCategoryId = product.TaxCategoryId;
            }

            var basedOn = _taxSettings.TaxBasedOn;
            //new EU VAT rules starting January 1st 2015
            //find more info at http://ec.europa.eu/taxation_customs/taxation/vat/how_vat_works/telecom/index_en.htm#new_rules
            //EU VAT enabled?
            if (_taxSettings.EuVatEnabled)
            {
                //telecommunications, broadcasting and electronic services?
                if (product != null && product.IsTelecommunicationsOrBroadcastingOrElectronicServices)
                {
                    //January 1st 2015 passed?
                    if (DateTime.UtcNow > new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc))
                    {
                        //Europe Union consumer?
                        if (IsEuConsumer(customer))
                        {
                            //We must charge VAT in the EU country where the customer belongs (not where the business is based)
                            basedOn = TaxBasedOn.BillingAddress;
                        }
                    }
                }
            }
            if (basedOn == TaxBasedOn.BillingAddress && customer.BillingAddress == null)
                basedOn = TaxBasedOn.DefaultAddress;
            if (basedOn == TaxBasedOn.ShippingAddress && customer.ShippingAddress == null)
                basedOn = TaxBasedOn.DefaultAddress;

            Address address;

            switch (basedOn)
            {
                case TaxBasedOn.BillingAddress:
                    {
                        address = customer.BillingAddress;
                    }
                    break;
                case TaxBasedOn.ShippingAddress:
                    {
                        address = customer.ShippingAddress;
                    }
                    break;
                case TaxBasedOn.DefaultAddress:
                default:
                    {
                        address = _addressService.GetAddressById(_taxSettings.DefaultTaxAddressId);
                    }
                    break;
            }

            calculateTaxRequest.Address = address;
            return calculateTaxRequest;
        }
Example #6
0
        /// <summary>
        /// Create request for tax calculation
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="taxCategoryId">Tax category identifier</param>
        /// <param name="customer">Customer</param>
        /// <param name="price">Price</param>
        /// <returns>Package for tax calculation</returns>
        protected virtual CalculateTaxRequest CreateCalculateTaxRequest(Product product,
                                                                        int taxCategoryId, Customer customer, decimal price)
        {
            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            var calculateTaxRequest = new CalculateTaxRequest
            {
                Customer      = customer,
                Product       = product,
                Price         = price,
                TaxCategoryId = taxCategoryId > 0 ? taxCategoryId : (product != null ? product.TaxCategoryId : 0)
            };

            var basedOn = _taxSettings.TaxBasedOn;

            //new EU VAT rules starting January 1st 2015
            //find more info at http://ec.europa.eu/taxation_customs/taxation/vat/how_vat_works/telecom/index_en.htm#new_rules
            var overridenBasedOn = _taxSettings.EuVatEnabled &&                                                         //EU VAT enabled?
                                   product != null && product.IsTelecommunicationsOrBroadcastingOrElectronicServices && //telecommunications, broadcasting and electronic services?
                                   DateTime.UtcNow > new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc) &&             //January 1st 2015 passed?
                                   IsEuConsumer(customer);                                                              //Europe Union consumer?

            if (overridenBasedOn)
            {
                //We must charge VAT in the EU country where the customer belongs (not where the business is based)
                basedOn = TaxBasedOn.BillingAddress;
            }

            //tax is based on pickup point address
            if (!overridenBasedOn && _taxSettings.TaxBasedOnPickupPointAddress && _shippingSettings.AllowPickUpInStore)
            {
                var pickupPoint = customer.GetAttribute <PickupPoint>(SystemCustomerAttributeNames.SelectedPickupPoint, 0);
                if (pickupPoint != null)
                {
                    var country = _countryService.GetCountryByTwoLetterIsoCode(pickupPoint.CountryCode);
                    var state   = _stateProvinceService.GetStateProvinceByAbbreviation(pickupPoint.StateAbbreviation, country?.Id);

                    calculateTaxRequest.Address = new Address
                    {
                        Address1        = pickupPoint.Address,
                        City            = pickupPoint.City,
                        Country         = country,
                        CountryId       = country?.Id ?? 0,
                        StateProvince   = state,
                        StateProvinceId = state?.Id ?? 0,
                        ZipPostalCode   = pickupPoint.ZipPostalCode,
                        CreatedOnUtc    = DateTime.UtcNow
                    };

                    return(calculateTaxRequest);
                }
            }

            //if (basedOn == TaxBasedOn.BillingAddress && customer.BillingAddress == null ||
            //    basedOn == TaxBasedOn.ShippingAddress && customer.ShippingAddress == null)
            //{
            //    basedOn = TaxBasedOn.DefaultAddress;
            //}

            //switch (basedOn)
            //{
            //    case TaxBasedOn.BillingAddress:
            //        calculateTaxRequest.Address = customer.BillingAddress;
            //        break;
            //    case TaxBasedOn.ShippingAddress:
            //        calculateTaxRequest.Address = customer.ShippingAddress;
            //        break;
            //    case TaxBasedOn.DefaultAddress:
            //    default:
            //        calculateTaxRequest.Address = _addressService.GetAddressById(_taxSettings.DefaultTaxAddressId);
            //        break;
            //}

            return(calculateTaxRequest);
        }
Example #7
0
        /// <summary>
        /// Create request for tax calculation
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="taxCategoryId">Tax category identifier</param>
        /// <param name="customer">Customer</param>
        /// <returns>Package for tax calculation</returns>
        protected virtual CalculateTaxRequest CreateCalculateTaxRequest(Product product,
                                                                        int taxCategoryId, Customer customer)
        {
            var calculateTaxRequest = new CalculateTaxRequest();

            calculateTaxRequest.Customer = customer;
            if (taxCategoryId > 0)
            {
                calculateTaxRequest.TaxCategoryId = taxCategoryId;
            }
            else
            {
                if (product != null)
                {
                    calculateTaxRequest.TaxCategoryId = product.TaxCategoryId;
                }
            }

            var basedOn = _taxSettings.TaxBasedOn;

            if (basedOn == TaxBasedOn.BillingAddress)
            {
                if (customer == null || customer.BillingAddress == null)
                {
                    basedOn = TaxBasedOn.DefaultAddress;
                }
            }
            if (basedOn == TaxBasedOn.ShippingAddress)
            {
                if (customer == null || customer.ShippingAddress == null)
                {
                    basedOn = TaxBasedOn.DefaultAddress;
                }
            }

            Address address = null;

            switch (basedOn)
            {
            case TaxBasedOn.BillingAddress:
            {
                address = customer.BillingAddress;
            }
            break;

            case TaxBasedOn.ShippingAddress:
            {
                address = customer.ShippingAddress;
            }
            break;

            case TaxBasedOn.DefaultAddress:
            default:
            {
                address = _addressService.GetAddressById(_taxSettings.DefaultTaxAddressId);
            }
            break;
            }

            calculateTaxRequest.Address = address;
            return(calculateTaxRequest);
        }
        /// <summary>
        /// Create request for tax calculation
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="taxCategoryId">Tax category identifier</param>
        /// <param name="customer">Customer</param>
        /// <returns>Package for tax calculation</returns>
        protected virtual CalculateTaxRequest CreateCalculateTaxRequest(Product product,
                                                                        int taxCategoryId, Customer customer)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }

            var calculateTaxRequest = new CalculateTaxRequest();

            calculateTaxRequest.Customer = customer;
            if (taxCategoryId > 0)
            {
                calculateTaxRequest.TaxCategoryId = taxCategoryId;
            }
            else
            {
                if (product != null)
                {
                    calculateTaxRequest.TaxCategoryId = product.TaxCategoryId;
                }
            }

            var basedOn = _taxSettings.TaxBasedOn;

            //new EU VAT rules starting January 1st 2015
            //find more info at http://ec.europa.eu/taxation_customs/taxation/vat/how_vat_works/telecom/index_en.htm#new_rules
            //EU VAT enabled?
            if (_taxSettings.EuVatEnabled)
            {
                //telecommunications, broadcasting and electronic services?
                if (product != null && product.IsTelecommunicationsOrBroadcastingOrElectronicServices)
                {
                    //January 1st 2015 passed?
                    if (DateTime.UtcNow > new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc))
                    {
                        //Europe Union consumer?
                        if (IsEuConsumer(customer))
                        {
                            //We must charge VAT in the EU country where the customer belongs (not where the business is based)
                            basedOn = TaxBasedOn.BillingAddress;
                        }
                    }
                }
            }
            if (basedOn == TaxBasedOn.BillingAddress && customer.BillingAddress == null)
            {
                basedOn = TaxBasedOn.DefaultAddress;
            }
            if (basedOn == TaxBasedOn.ShippingAddress && customer.ShippingAddress == null)
            {
                basedOn = TaxBasedOn.DefaultAddress;
            }

            Address address = null;

            switch (basedOn)
            {
            case TaxBasedOn.BillingAddress:
            {
                address = customer.BillingAddress;
            }
            break;

            case TaxBasedOn.ShippingAddress:
            {
                address = customer.ShippingAddress;
            }
            break;

            case TaxBasedOn.DefaultAddress:
            default:
            {
                address = _addressService.GetAddressById(_taxSettings.DefaultTaxAddressId);
            }
            break;
            }

            calculateTaxRequest.Address = address;
            return(calculateTaxRequest);
        }
        /// <summary>
        /// Gets tax rate
        /// </summary>
        /// <param name="calculateTaxRequest">Tax calculation request</param>
        /// <returns>Tax</returns>
        public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest)
        {
            var result = new CalculateTaxResult();

            if (calculateTaxRequest.Address == null)
            {
                result.Errors.Add("Address is not set");
                return result;
            }

            //first, load all tax rate records (cached) - loaded only once
            string cacheKey = ModelCacheEventConsumer.ALL_TAX_RATES_MODEL_KEY;
            var allTaxRates = _cacheManager.Get(cacheKey, () =>
                _taxRateService
                .GetAllTaxRates()
                .Select(x => new TaxRateForCaching
                {
                    Id = x.Id,
                    StoreId = x.StoreId,
                    TaxCategoryId = x.TaxCategoryId,
                    CountryId = x.CountryId,
                    StateProvinceId = x.StateProvinceId,
                    Zip = x.Zip,
                    Percentage = x.Percentage,
                }
                )
                .ToList()
                );

            int storeId = _storeContext.CurrentStore.Id;
            int taxCategoryId = calculateTaxRequest.TaxCategoryId;
            int countryId = calculateTaxRequest.Address.Country != null ? calculateTaxRequest.Address.Country.Id : 0;
            int stateProvinceId = calculateTaxRequest.Address.StateProvince != null ? calculateTaxRequest.Address.StateProvince.Id : 0;
            string zip = calculateTaxRequest.Address.ZipPostalCode;


            if (zip == null)
                zip = string.Empty;
            zip = zip.Trim();

            var existingRates = new List<TaxRateForCaching>();
            foreach (var taxRate in allTaxRates)
            {
                if (taxRate.CountryId == countryId && taxRate.TaxCategoryId == taxCategoryId)
                    existingRates.Add(taxRate);
            }

            //filter by store
            var matchedByStore = new List<TaxRateForCaching>();
            //first, find by a store ID
            foreach (var taxRate in existingRates)
                if (storeId == taxRate.StoreId)
                    matchedByStore.Add(taxRate);
            //not found? use the default ones (ID == 0)
            if (!matchedByStore.Any())
                foreach (var taxRate in existingRates)
                    if (taxRate.StoreId == 0)
                        matchedByStore.Add(taxRate);


            //filter by state/province
            var matchedByStateProvince = new List<TaxRateForCaching>();
            //first, find by a state ID
            foreach (var taxRate in matchedByStore)
                if (stateProvinceId == taxRate.StateProvinceId)
                    matchedByStateProvince.Add(taxRate);
            //not found? use the default ones (ID == 0)
            if (!matchedByStateProvince.Any())
                foreach (var taxRate in matchedByStore)
                    if (taxRate.StateProvinceId == 0)
                        matchedByStateProvince.Add(taxRate);


            //filter by zip
            var matchedByZip = new List<TaxRateForCaching>();
            foreach (var taxRate in matchedByStateProvince)
                if ((String.IsNullOrEmpty(zip) && String.IsNullOrEmpty(taxRate.Zip)) ||
                    (zip.Equals(taxRate.Zip, StringComparison.InvariantCultureIgnoreCase)))
                    matchedByZip.Add(taxRate);
            if (!matchedByZip.Any())
                foreach (var taxRate in matchedByStateProvince)
                    if (String.IsNullOrWhiteSpace(taxRate.Zip))
                        matchedByZip.Add(taxRate);

            if (matchedByZip.Any())
                result.TaxRate = matchedByZip[0].Percentage;

            return result;
        }
Example #10
0
        /// <summary>
        /// Create request for tax calculation
        /// </summary>
        /// <param name="product">Product</param>
        /// <param name="taxCategoryId">Tax category identifier</param>
        /// <param name="customer">Customer</param>
        /// <param name="price">Price</param>
        /// <returns>Package for tax calculation</returns>
        protected virtual CalculateTaxRequest CreateCalculateTaxRequest(Product product,
                                                                        int taxCategoryId, Customer customer, decimal price)
        {
            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            var calculateTaxRequest = new CalculateTaxRequest
            {
                Customer       = customer,
                Product        = product,
                Price          = price,
                TaxCategoryId  = taxCategoryId > 0 ? taxCategoryId : product?.TaxCategoryId ?? 0,
                CurrentStoreId = _storeContext.CurrentStore.Id
            };

            var basedOn = _taxSettings.TaxBasedOn;

            //new EU VAT rules starting January 1st 2015
            //find more info at http://ec.europa.eu/taxation_customs/taxation/vat/how_vat_works/telecom/index_en.htm#new_rules
            var overriddenBasedOn =
                //EU VAT enabled?
                _taxSettings.EuVatEnabled &&
                //telecommunications, broadcasting and electronic services?
                product != null && product.IsTelecommunicationsOrBroadcastingOrElectronicServices &&
                //January 1st 2015 passed? Yes, not required anymore
                //DateTime.UtcNow > new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc) &&
                //Europe Union consumer?
                IsEuConsumer(customer);

            if (overriddenBasedOn)
            {
                //We must charge VAT in the EU country where the customer belongs (not where the business is based)
                basedOn = TaxBasedOn.BillingAddress;
            }

            //tax is based on pickup point address
            if (!overriddenBasedOn && _taxSettings.TaxBasedOnPickupPointAddress && _shippingSettings.AllowPickupInStore)
            {
                var pickupPoint = _genericAttributeService.GetAttribute <PickupPoint>(customer,
                                                                                      NopCustomerDefaults.SelectedPickupPointAttribute, _storeContext.CurrentStore.Id);
                if (pickupPoint != null)
                {
                    calculateTaxRequest.Address = LoadPickupPointTaxAddress(pickupPoint);
                    return(calculateTaxRequest);
                }
            }

            if (basedOn == TaxBasedOn.BillingAddress && customer.BillingAddress == null ||
                basedOn == TaxBasedOn.ShippingAddress && customer.ShippingAddress == null)
            {
                basedOn = TaxBasedOn.DefaultAddress;
            }

            switch (basedOn)
            {
            case TaxBasedOn.BillingAddress:
                calculateTaxRequest.Address = PrepareTaxAddress(customer.BillingAddress);
                break;

            case TaxBasedOn.ShippingAddress:
                calculateTaxRequest.Address = PrepareTaxAddress(customer.ShippingAddress);
                break;

            case TaxBasedOn.DefaultAddress:
            default:
                calculateTaxRequest.Address = LoadDefaultTaxAddress();
                break;
            }

            return(calculateTaxRequest);
        }
        /// <summary>
        /// Gets tax rate
        /// </summary>
        /// <param name="calculateTaxRequest">Tax calculation request</param>
        /// <returns>Tax</returns>
        public CalculateTaxResult GetTaxRate(CalculateTaxRequest calculateTaxRequest)
        {
            var result = new CalculateTaxResult();

            var address = calculateTaxRequest.Address;
            if (address == null)
            {
                result.AddError("Address is not set");
                return result;
            }
            if (address.Country == null)
            {
                result.AddError("Country is not set");
                return result;
            }

            //**************************************************************************************************************
            // 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 = _strikeIronTaxSettings.UserId;
            string password = _strikeIronTaxSettings.Password;
            
            decimal taxRate = decimal.Zero;
            if (address.Country.TwoLetterIsoCode.ToLower() == "us")
            {
                if (String.IsNullOrEmpty(address.ZipPostalCode))
                {
                    result.AddError("Zip is not provided");
                    return result;
                }
                string error = "";
                taxRate = GetTaxRateUsa(address.ZipPostalCode, userId, password, ref error);
                if (!String.IsNullOrEmpty(error))
                {
                    result.AddError(error);
                    return result;
                }
            }
            else if (address.Country.TwoLetterIsoCode.ToLower() == "ca")
            {
                if (address.StateProvince == null)
                {
                    result.AddError("Province is not set");
                    return result;
                }
                string error = "";
                taxRate = GetTaxRateCanada(address.StateProvince.Abbreviation, userId, password, ref error);
                if (!String.IsNullOrEmpty(error))
                {
                    result.AddError(error);
                    return result;
                }
            }
            else
            {
                result.AddError("Tax can be calculated only for USA zip or Canada province");
                return result;
            }

            result.TaxRate = taxRate * 100;
            return result;
        }