Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * BroadleafCommerce Framework
   * %%
   * Copyright (C) 2009 - 2013 Broadleaf Commerce
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */
 package org.broadleafcommerce.core.pricing.service.module;
 
 
 import java.util.Map;

Simple factor-based tax module that can be configured by adding rates for specific postalCodes, city, state, or country. Through configuration, this module can be used to set a specific tax rate for items and shipping for a given postal code, city, state, or country. Utilizes the fulfillment group's address to determine the tax location. Useful for those with very simple tax needs.

Author(s):
jfischer, brian polster
 
 public class SimpleTaxModule implements TaxModule {
 
     public static final String MODULENAME = "simpleTaxModule";
 
     protected String name = ;
 
     protected Map<StringDoubleitemPostalCodeTaxRateMap;
     protected Map<StringDoubleitemCityTaxRateMap;
     protected Map<StringDoubleitemStateTaxRateMap;
     protected Map<StringDoubleitemCountryTaxRateMap;
 
 
 
     protected Double defaultItemTaxRate;
 
     protected boolean taxFees;
 
     @Override
     public Order calculateTaxForOrder(Order orderthrows TaxException {
         for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
             
             // Set taxes on the fulfillment group items
             for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
                 if (isItemTaxable(fgItem)) {
                     Double factor = determineItemTaxRate(fulfillmentGroup.getAddress());
                     if (factor != null && factor.compareTo(0d) != 0) {
                         TaxDetail tax;
                         checkDetail: {
                             for (TaxDetail detail : fgItem.getTaxes()) {
                                 if (detail.getType().equals(.)) {
                                     tax = detail;
                                     break checkDetail;
                                 }
                             }
                             tax = new TaxDetailImpl();
                             tax.setType(.);
                             fgItem.getTaxes().add(tax);
                         }
                         tax.setRate(new BigDecimal(factor));
                         tax.setAmount(fgItem.getTotalItemTaxableAmount().multiply(factor));
                     }
                 }
             }
 
            for (FulfillmentGroupFee fgFee : fulfillmentGroup.getFulfillmentGroupFees()) {
                if (isFeeTaxable(fgFee)) {
                    Double factor = determineItemTaxRate(fulfillmentGroup.getAddress());
                    if (factor != null && factor.compareTo(0d) != 0) {
                        TaxDetail tax;
                        checkDetail: {
                            for (TaxDetail detail : fgFee.getTaxes()) {
                                if (detail.getType().equals(.)) {
                                    tax = detail;
                                    break checkDetail;
                                }
                            }
                            tax = new TaxDetailImpl();
                            tax.setType(.);
                            fgFee.getTaxes().add(tax);
                        }
                        tax.setRate(new BigDecimal(factor));
                        tax.setAmount(fgFee.getAmount().multiply(factor));
                    }
                }
            }
            Double factor = determineTaxRateForFulfillmentGroup(fulfillmentGroup);
            if (factor != null && factor.compareTo(0d) != 0) {
                TaxDetail tax;
                checkDetail: {
                    for (TaxDetail detail : fulfillmentGroup.getTaxes()) {
                        if (detail.getType().equals(.)) {
                            tax = detail;
                            break checkDetail;
                        }
                    }
                    tax = new TaxDetailImpl();
                    tax.setType(.);
                    fulfillmentGroup.getTaxes().add(tax);
                }
                tax.setRate(new BigDecimal(factor));
                tax.setAmount(fulfillmentGroup.getFulfillmentPrice().multiply(factor));
            }
        }
        return order;
    }
    @Override
    public String getName() {
        return ;
    }
    @Override
    public void setName(String name) {
        this. = name;
    }


    
Returns the taxAmount for the passed in postal code or null if no match is found.

Parameters:
postalCode
Returns:
    public Double lookupPostalCodeRate(Map<String,DoublepostalCodeTaxRateMapString postalCode) {
        if (postalCodeTaxRateMap != null && postalCode != null) {
            return postalCodeTaxRateMap.get(postalCode);
        }
        return null;
    }

    
Changes the city to upper case before checking the configuration. Return null if no match is found.

Parameters:
cityTaxRateMap, city
Returns:
    public Double lookupCityRate(Map<String,DoublecityTaxRateMapString city) {
        if (cityTaxRateMap != null && city != null) {
            city = city.toUpperCase();
            return cityTaxRateMap.get(city);
        }
        return null;
    }

    
Returns the taxAmount for the passed in state or null if no match is found. First checks the abbreviation (uppercase) followed by the name (uppercase).

Parameters:
stateTaxRateMap, state
Returns:
    public Double lookupStateRate(Map<String,DoublestateTaxRateMapState state) {
        if (stateTaxRateMap != null && state != null && state.getAbbreviation() != null) {
            String stateAbbr = state.getAbbreviation().toUpperCase();
            Double rate = stateTaxRateMap.get(stateAbbr);
            if (rate == null && state.getName() != null) {
                String stateName = state.getName().toUpperCase();
                return stateTaxRateMap.get(stateName);
            } else {
                return rate;
            }
        }
        return null;
    }

    
Returns the taxAmount for the passed in country or null if no match is found. First checks the abbreviation (uppercase) followed by the name (uppercase).

Parameters:
countryTaxRateMap, state
Returns:
    public Double lookupCountryRate(Map<String,DoublecountryTaxRateMapCountry country) {
        if (countryTaxRateMap != null && country != null && country.getAbbreviation() != null) {
            String cntryAbbr = country.getAbbreviation().toUpperCase();
            Double rate = countryTaxRateMap.get(cntryAbbr);
            if (rate == null && country.getName() != null) {
                String countryName = country.getName().toUpperCase();
                return countryTaxRateMap.get(countryName);
            } else {
                return rate;
            }
        }
        return null;
    }
    protected boolean isItemTaxable(FulfillmentGroupItem item) {
        return item.getOrderItem().isTaxable();
    }
    protected boolean isFeeTaxable(FulfillmentGroupFee fee) {
        return fee.isTaxable();
    }


    
Uses the passed in address to determine if the item is taxable. Checks the configured maps in order - (postal code, city, state, country)

Parameters:
address
Returns:
    public Double determineItemTaxRate(Address address) {
        if (address != null) {
            Double postalCodeRate = lookupPostalCodeRate(address.getPostalCode());
            if (postalCodeRate != null) {
                return postalCodeRate;
            }
            Double cityCodeRate = lookupCityRate(address.getCity());
            if (cityCodeRate != null) {
                return cityCodeRate;
            }
            Double stateCodeRate = lookupStateRate(address.getState());
            if (stateCodeRate != null) {
                return stateCodeRate;
            }
            Double countryCodeRate = lookupCountryRate(address.getCountry());
            if (countryCodeRate != null) {
                return countryCodeRate;
            }
        }
        if ( != null) {
            return ;
        } else {
            return 0d;
        }
    }

    
Uses the passed in address to determine if the item is taxable. Checks the configured maps in order - (postal code, city, state, country)

Parameters:
fulfillmentGroup
Returns:
    public Double determineTaxRateForFulfillmentGroup(FulfillmentGroup fulfillmentGroup) {
        boolean isTaxable = true;
        if (fulfillmentGroup.isShippingPriceTaxable() != null) {
            isTaxable = fulfillmentGroup.isShippingPriceTaxable();
        }
        if (isTaxable) {
            Address address = fulfillmentGroup.getAddress();
            if (address != null) {
                Double postalCodeRate = lookupPostalCodeRate(address.getPostalCode());
                if (postalCodeRate != null) {
                    return postalCodeRate;
                }
                Double cityCodeRate = lookupCityRate(address.getCity());
                if (cityCodeRate != null) {
                    return cityCodeRate;
                }
                Double stateCodeRate = lookupStateRate(address.getState());
                if (stateCodeRate != null) {
                    return stateCodeRate;
                }
                Double countryCodeRate = lookupCountryRate(address.getCountry());
                if (countryCodeRate != null) {
                    return countryCodeRate;
                }
            }
            if ( != null) {
                return ;
            }
        }
        return 0d;
    }
        return ;
    }
    public void setItemPostalCodeTaxRateMap(Map<StringDoubleitemPostalCodeTaxRateMap) {
        this. = itemPostalCodeTaxRateMap;
    }
    public Map<StringDoublegetItemCityTaxRateMap() {
        return ;
    }
    public void setItemCityTaxRateMap(Map<StringDoubleitemCityTaxRateMap) {
        this. = itemCityTaxRateMap;
    }
    public Map<StringDoublegetItemStateTaxRateMap() {
        return ;
    }
    public void setItemStateTaxRateMap(Map<StringDoubleitemStateTaxRateMap) {
        this. = itemStateTaxRateMap;
    }
    public Map<StringDoublegetItemCountryTaxRateMap() {
        return ;
    }
    public void setItemCountryTaxRateMap(Map<StringDoubleitemCountryTaxRateMap) {
        this. = itemCountryTaxRateMap;
    }
    }
    public void setFulfillmentGroupPostalCodeTaxRateMap(Map<StringDoublefulfillmentGroupPostalCodeTaxRateMap) {
        this. = fulfillmentGroupPostalCodeTaxRateMap;
    }
        return ;
    }
    public void setFulfillmentGroupCityTaxRateMap(Map<StringDoublefulfillmentGroupCityTaxRateMap) {
        this. = fulfillmentGroupCityTaxRateMap;
    }
        return ;
    }
    public void setFulfillmentGroupStateTaxRateMap(Map<StringDoublefulfillmentGroupStateTaxRateMap) {
        this. = fulfillmentGroupStateTaxRateMap;
    }
        return ;
    }
    public void setFulfillmentGroupCountryTaxRateMap(Map<StringDoublefulfillmentGroupCountryTaxRateMap) {
        this. = fulfillmentGroupCountryTaxRateMap;
    }
    public Double getDefaultItemTaxRate() {
        return ;
    }
    public void setDefaultItemTaxRate(Double defaultItemTaxRate) {
        this. = defaultItemTaxRate;
    }
        return ;
    }
    public void setDefaultFulfillmentGroupTaxRate(Double defaultFulfillmentGroupTaxRate) {
        this. = defaultFulfillmentGroupTaxRate;
    }

    
Use getDefaultItemTaxRate instead.

Deprecated:
Returns:
    @Deprecated
    public Double getFactor() {
        return getDefaultItemTaxRate();
    }

    
Use setDefaultItemTaxRate instead.

Deprecated:
Returns:
    @Deprecated
    public void setFactor(Double factor) {
        setDefaultItemTaxRate(factor);
    }
New to GrepCode? Check out our FAQ X