Class DefaultTaxService<F extends com.broadleafcommerce.cart.client.domain.FulfillmentGroup,​TREQ extends com.broadleafcommerce.tax.domain.TaxRequest,​TRES extends com.broadleafcommerce.tax.domain.TaxResponse,​TI extends com.broadleafcommerce.tax.domain.TaxItem>

  • All Implemented Interfaces:
    TaxService

    public class DefaultTaxService<F extends com.broadleafcommerce.cart.client.domain.FulfillmentGroup,​TREQ extends com.broadleafcommerce.tax.domain.TaxRequest,​TRES extends com.broadleafcommerce.tax.domain.TaxResponse,​TI extends com.broadleafcommerce.tax.domain.TaxItem>
    extends Object
    implements TaxService
    The default TaxService which integrates with TaxProviders. This service connects to an actualTaxProvider for accurate actual tax amounts and an optional estimatedTaxProvider for quick estimated tax amounts. Callers of this class can choose to get taxes for either or both actual and estimated. A typical scenario would be to use a quick synchronous call for estimated taxes when performing cart operations and then get the actual taxes through a 3rd party provider once the cart is finalized in checkout and ready for the customer to review final pricing.
    Author:
    Chad Harchar (charchar)
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultTaxService​(com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> estimatedTaxProvider, com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> actualTaxProvider, com.broadleafcommerce.common.extension.TypeFactory typeFactory)  
      DefaultTaxService​(com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> estimatedTaxProvider, com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> actualTaxProvider, com.broadleafcommerce.common.extension.TypeFactory typeFactory, List<TaxAddressSourceHandler> handlers)  
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      protected void addFulfillmentItemTaxDetails​(com.broadleafcommerce.tax.domain.TaxResponse taxResponse, com.broadleafcommerce.tax.domain.TaxInfo taxInfo, com.broadleafcommerce.cart.client.domain.FulfillmentItem fulfillmentItem)
      Add FulfillmentItemTaxDetails to the fulfillment item.
      void applyTaxes​(com.broadleafcommerce.cart.client.domain.Cart cart, boolean estimated, boolean actual, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Calculates and applies taxes to a cart.
      protected void applyTaxesForGroup​(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, F fulfillmentGroup, @NonNull com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      protected void applyTaxesForGroups​(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull List<F> fulfillmentGroups, @NonNull com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Calculates and sets FulfillmentItem.getMerchandiseTotalTax() and FulfillmentGroup.getTotalTax().
      protected void applyTaxesInternal​(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Performs the bulk of the tax calculations and application for a given TaxProvider.
      protected void applyTaxResponse​(TRES taxResponse, F fulfillmentGroup, javax.money.CurrencyUnit expectedCurrency)
      protected void applyTaxResponses​(List<TRES> taxResponses, List<F> fulfillmentGroups, javax.money.CurrencyUnit expectedCurrency)  
      protected TI buildTaxItemForLineItem​(F fulfillmentGroup, @NonNull com.broadleafcommerce.cart.client.domain.FulfillmentItem fulfillmentItem)
      Deprecated.
      Since 1.2.
      protected List<TI> buildTaxItemsForFulfillment​(F fulfillmentGroup)  
      protected List<TI> buildTaxItemsForLineItems​(F fulfillmentGroup)
      Deprecated.
      Since 1.2.
      protected Collection<? extends TI> buildTaxItemsForLineItems​(F fulfillmentGroup, com.broadleafcommerce.cart.client.domain.Cart cart)  
      protected void calculateActualTaxesForCart​(com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)  
      protected void calculateEstimatedTaxesForCart​(com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)  
      protected void calculateIncludedTaxes​(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, javax.money.CurrencyUnit expectedCurrency, List<TRES> taxResponse)
      Set VAT properties on CartPricing.
      boolean canCalculateTaxes​(com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Whether the cart contains the information required to calculate taxes.
      protected TaxAddressSourceHandler findTaxAddressSourceHandler​(com.broadleafcommerce.cart.client.domain.Cart cart, F fulfillmentGroup)  
      protected com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> getActualTaxProvider()  
      protected com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> getEstimatedTaxProvider()  
      protected List<F> getFulfillmentGroups​(com.broadleafcommerce.cart.client.domain.Cart cart)  
      protected <T> T getInstance​(Class<?> clazz)  
      protected String getProductDescription​(@NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Retrieves the description of the product represented by the CartItem.
      protected List<TaxAddressSourceHandler> getTaxAddressSourceHandlers()  
      protected String getTaxItemIdForFulfillment​(F fulfillmentGroup)
      Extension point to modify the TaxItem.getItemId() for fulfillment tax items.
      protected String getTaxItemIdForLineItem​(F fulfillmentGroup, @NonNull com.broadleafcommerce.cart.client.domain.FulfillmentItem fulfillmentItem)  
      protected com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> getTaxProvider​(com.broadleafcommerce.cart.client.domain.Cart cart, boolean estimated, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Determine which tax provider to use for calculating taxes for the cart
      protected TREQ getTaxRequest​(com.broadleafcommerce.cart.client.domain.Cart cart, F fulfillmentGroup, TaxAddressSourceHandler handler, List<TI> taxItems, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)  
      protected Map<String,​TRES> getTaxResponseMap​(List<TRES> taxResponses)
      Returns a map of tax responses, with the key as the TaxResponse.getFulfillmentGroupReferenceNumber()
      protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()  
      protected boolean isEstimatedProvider​(com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider, com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Determine if the given provider calculates estimated taxes or actual taxes.
      protected void setAllTaxToZero​(com.broadleafcommerce.cart.client.domain.Cart cart)  
      protected boolean shouldCalculateItemTaxes​(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart)  
      protected boolean shouldTaxFulfillment​(F fulfillmentGroup, javax.money.CurrencyUnit currencyUnit)  
    • Constructor Detail

      • DefaultTaxService

        public DefaultTaxService​(@Nullable
                                 com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> estimatedTaxProvider,
                                 com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> actualTaxProvider,
                                 com.broadleafcommerce.common.extension.TypeFactory typeFactory,
                                 List<TaxAddressSourceHandler> handlers)
      • DefaultTaxService

        public DefaultTaxService​(com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> estimatedTaxProvider,
                                 com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> actualTaxProvider,
                                 com.broadleafcommerce.common.extension.TypeFactory typeFactory)
    • Method Detail

      • canCalculateTaxes

        public boolean canCalculateTaxes​(com.broadleafcommerce.cart.client.domain.Cart cart,
                                         @Nullable
                                         com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Description copied from interface: TaxService
        Whether the cart contains the information required to calculate taxes.
        Specified by:
        canCalculateTaxes in interface TaxService
        Parameters:
        cart - The cart to determine whether taxes can be calculated for.
        contextInfo - Additional sandbox and tenant info
        Returns:
        Whether the cart contains the information required to calculate taxes.
      • applyTaxes

        public void applyTaxes​(com.broadleafcommerce.cart.client.domain.Cart cart,
                               boolean estimated,
                               boolean actual,
                               @Nullable
                               com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Description copied from interface: TaxService
        Calculates and applies taxes to a cart.
        Specified by:
        applyTaxes in interface TaxService
        Parameters:
        cart - The cart to apply taxes against.
        estimated - Whether to provide an estimate
        actual - Whether to provide the actual
        contextInfo - Additional sandbox and tenant info
      • calculateActualTaxesForCart

        protected void calculateActualTaxesForCart​(com.broadleafcommerce.cart.client.domain.Cart cart,
                                                   @Nullable
                                                   com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • calculateEstimatedTaxesForCart

        protected void calculateEstimatedTaxesForCart​(com.broadleafcommerce.cart.client.domain.Cart cart,
                                                      @Nullable
                                                      com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • applyTaxesInternal

        protected void applyTaxesInternal​(@NonNull
                                          @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                          @NonNull
                                          @NonNull com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider,
                                          @Nullable
                                          com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Performs the bulk of the tax calculations and application for a given TaxProvider.
        Parameters:
        cart - Cart to be taxed
        taxProvider - The TaxProvider to facilitate tax calculations
      • applyTaxesForGroups

        protected void applyTaxesForGroups​(@NonNull
                                           @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                           @NonNull
                                           @NonNull List<F> fulfillmentGroups,
                                           @NonNull
                                           @NonNull com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider,
                                           @Nullable
                                           com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Calculates and sets FulfillmentItem.getMerchandiseTotalTax() and FulfillmentGroup.getTotalTax().
        Parameters:
        cart - The cart with items to be taxed
        fulfillmentGroups - The group to be taxed with items to be taxed
        taxProvider - The TaxProvider that calculates the tax
      • calculateIncludedTaxes

        protected void calculateIncludedTaxes​(@NonNull
                                              @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                              javax.money.CurrencyUnit expectedCurrency,
                                              List<TRES> taxResponse)
        Set VAT properties on CartPricing. Determine how much of the total tax amount is included in the subtotal.
        Parameters:
        cart - Cart to be taxed
        expectedCurrency - The currency to use
        taxResponse - The TaxResponse response containing calculated taxes.
      • buildTaxItemsForLineItems

        protected Collection<? extends TI> buildTaxItemsForLineItems​(F fulfillmentGroup,
                                                                     com.broadleafcommerce.cart.client.domain.Cart cart)
      • getProductDescription

        protected String getProductDescription​(@NonNull
                                               @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem)
        Retrieves the description of the product represented by the CartItem.
        Parameters:
        cartItem - the CartItem
        Returns:
        The description of the product that was added to the cart.
      • getTaxRequest

        protected TREQ getTaxRequest​(com.broadleafcommerce.cart.client.domain.Cart cart,
                                     F fulfillmentGroup,
                                     TaxAddressSourceHandler handler,
                                     List<TI> taxItems,
                                     @Nullable
                                     com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • applyTaxResponses

        protected void applyTaxResponses​(List<TRES> taxResponses,
                                         List<F> fulfillmentGroups,
                                         javax.money.CurrencyUnit expectedCurrency)
      • addFulfillmentItemTaxDetails

        protected void addFulfillmentItemTaxDetails​(com.broadleafcommerce.tax.domain.TaxResponse taxResponse,
                                                    com.broadleafcommerce.tax.domain.TaxInfo taxInfo,
                                                    com.broadleafcommerce.cart.client.domain.FulfillmentItem fulfillmentItem)
        Add FulfillmentItemTaxDetails to the fulfillment item.
        Parameters:
        taxResponse - The tax response
        taxInfo - The tax info
        fulfillmentItem - The fulfillment item
      • getTaxResponseMap

        protected Map<String,​TRES> getTaxResponseMap​(List<TRES> taxResponses)
        Returns a map of tax responses, with the key as the TaxResponse.getFulfillmentGroupReferenceNumber()
        Parameters:
        taxResponses - A list of tax responses
        Returns:
        A map of tax responses.
      • applyTaxesForGroup

        @Deprecated
        protected void applyTaxesForGroup​(@NonNull
                                          @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                          @NonNull
                                          F fulfillmentGroup,
                                          @NonNull
                                          @NonNull com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider,
                                          com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Calculates and sets FulfillmentItem.getMerchandiseTotalTax() and FulfillmentGroup.getTotalTax().
        Parameters:
        cart - The cart with items to be taxed
        fulfillmentGroup - The group to be taxed with items to be taxed
        taxProvider - The TaxProvider that facilitates calculating the tax
      • getTaxItemIdForLineItem

        protected String getTaxItemIdForLineItem​(@NonNull
                                                 F fulfillmentGroup,
                                                 @NonNull
                                                 @NonNull com.broadleafcommerce.cart.client.domain.FulfillmentItem fulfillmentItem)
      • buildTaxItemForLineItem

        @Deprecated
        protected TI buildTaxItemForLineItem​(@NonNull
                                             F fulfillmentGroup,
                                             @NonNull
                                             @NonNull com.broadleafcommerce.cart.client.domain.FulfillmentItem fulfillmentItem)
        Parameters:
        fulfillmentGroup -
        fulfillmentItem -
        Returns:
      • buildTaxItemsForFulfillment

        protected List<TI> buildTaxItemsForFulfillment​(@NonNull
                                                       F fulfillmentGroup)
      • getTaxItemIdForFulfillment

        protected String getTaxItemIdForFulfillment​(@NonNull
                                                    F fulfillmentGroup)
        Extension point to modify the TaxItem.getItemId() for fulfillment tax items. For some TaxProviders, there may be a affix like FR (for freight) to add to the FulfillmentGroup.getReferenceNumber().
        Parameters:
        fulfillmentGroup - Fulfillment group from which to derive an id
        Returns:
        TaxItem.getItemId() for fulfillment tax items
      • applyTaxResponse

        @Deprecated
        protected void applyTaxResponse​(@NonNull
                                        TRES taxResponse,
                                        @NonNull
                                        F fulfillmentGroup,
                                        javax.money.CurrencyUnit expectedCurrency)
        Takes the TaxResponse and applies the results to the FulfillmentGroup and its items.
        Parameters:
        taxResponse - Tax response to apply
        fulfillmentGroup - Group to which to apply the response
      • setAllTaxToZero

        protected void setAllTaxToZero​(com.broadleafcommerce.cart.client.domain.Cart cart)
      • shouldCalculateItemTaxes

        protected boolean shouldCalculateItemTaxes​(@NonNull
                                                   @NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
      • shouldTaxFulfillment

        protected boolean shouldTaxFulfillment​(F fulfillmentGroup,
                                               javax.money.CurrencyUnit currencyUnit)
      • findTaxAddressSourceHandler

        @Nullable
        protected TaxAddressSourceHandler findTaxAddressSourceHandler​(com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                      F fulfillmentGroup)
      • getInstance

        protected <T> T getInstance​(Class<?> clazz)
      • getFulfillmentGroups

        protected List<F> getFulfillmentGroups​(com.broadleafcommerce.cart.client.domain.Cart cart)
      • getTaxProvider

        @Nullable
        protected com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> getTaxProvider​(com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                                        boolean estimated,
                                                                                        @Nullable
                                                                                        com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Determine which tax provider to use for calculating taxes for the cart
        Parameters:
        cart - The cart to be taxed.
        estimated - Whether the calculated tax is an estimate.
        contextInfo - Context information around sandbox and multitenant state.
        Returns:
        A TaxProvider that can be used to calculate taxes for the cart.
      • isEstimatedProvider

        protected boolean isEstimatedProvider​(com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> taxProvider,
                                              com.broadleafcommerce.cart.client.domain.Cart cart,
                                              @Nullable
                                              com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Determine if the given provider calculates estimated taxes or actual taxes.
        Parameters:
        taxProvider - The TaxProvider used to calculate taxes for the cart.
        cart - The cart to be taxed.
        contextInfo - Context information around sandbox and multitenant state.
        Returns:
        True if the given TaxProvider calculates estimated taxes.
      • getEstimatedTaxProvider

        @Nullable
        protected com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> getEstimatedTaxProvider()
      • getActualTaxProvider

        protected com.broadleafcommerce.tax.TaxProvider<TREQ,​TRES> getActualTaxProvider()
      • getTypeFactory

        protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()