Class DefaultCartOrderGenerationService<C extends com.broadleafcommerce.cart.client.domain.Cart,​O extends com.broadleafcommerce.order.client.domain.Order>

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean adjustmentShouldBeProrated​(com.broadleafcommerce.order.common.domain.Adjustment adjustment)
      Determines if the given Adjustment needs to be prorated across offer qualifier and target items.
      protected com.broadleafcommerce.order.client.domain.OrderItem buildDependentOrderItemFromCartItem​(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Build the dependent OrderItem from the given dependent CartItem.
      protected List<com.broadleafcommerce.order.client.domain.OrderItem> buildDependentOrderItemsFromCartItems​(List<com.broadleafcommerce.cart.client.domain.CartItem> cartItems)
      Build dependent OrderItems from the given dependent CartItems.
      protected com.broadleafcommerce.order.client.domain.ImageAssetRef buildImageAssetRef​(com.broadleafcommerce.cart.client.domain.CartItem cartItem)  
      protected O buildOrderFromCart​(C cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Build the Order from the given Cart.
      protected com.broadleafcommerce.order.client.domain.OrderItem buildOrderItemFromCartItem​(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Build an OrderItem from the given CartItem.
      protected List<com.broadleafcommerce.order.client.domain.OrderItem> buildOrderItemsFromCart​(C cart, O order)
      Build the OrderItems for the given Cart.
      protected com.broadleafcommerce.order.client.domain.OrderPricing buildOrderPricingFromCart​(C cart)
      Build the OrderPricing for the given Cart.
      O generateOrderFromCart​(C cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Creates and persists a new order from the given cart only if such an order does not already exist.
      protected Map<com.broadleafcommerce.order.client.domain.OrderItem,​List<com.broadleafcommerce.order.common.domain.Adjustment>> getAdjustmentsByTargetItemWithQualifierItemId​(List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems, String qualifierItemId)
      Gets a map of Adjustments by offer target OrderItem whose Adjustment.getQualifierDetails() is referencing the given offer qualifier item id.
      protected String getCartCustomerName​(C cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Get the customer's name from the cart.
      protected OrderOperationServiceOfferUtils getOfferUtils()  
      protected Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> getOrderItemsByOriginalCartItemIds​(List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems)
      Gets a map of OrderItems by their original CartItem.getId().
      protected OrderProvider<O> getOrderProvider()  
      protected String getOriginalCartItemIdFromAttributes​(com.broadleafcommerce.order.client.domain.OrderItem item)  
      protected PaymentProvider getPaymentProvider()  
      protected OrderGenerationProperties getProperties()  
      protected javax.money.MonetaryAmount getQualifierItemDistributedDiscount​(com.broadleafcommerce.order.common.domain.Adjustment adjustment, com.broadleafcommerce.order.client.domain.OrderItem qualifierItem, com.broadleafcommerce.order.client.domain.OrderItem targetItem, Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> orderItemById)
      Gets the proportionally distributed discount for the given offer qualifier item based on its price ratio.
      protected javax.money.MonetaryAmount getQualifierItemsUnadjustedTotal​(com.broadleafcommerce.order.common.domain.Adjustment adjustment, Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> orderItemById)
      Gets the unadjusted item total of all the offer qualifier OrderItems.
      protected javax.money.MonetaryAmount getTargetItemDistributedDiscount​(com.broadleafcommerce.order.common.domain.Adjustment adjustment, com.broadleafcommerce.order.client.domain.OrderItem targetItem, Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> orderItemById)
      Gets the proportionally distributed discount for the given offer target item based on its price ratio.
      protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()  
      protected boolean orderExistsForCart​(C cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Determine whether an Order has already been created for the Cart.
      protected void precalculateRefundAmounts​(O order, List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems)
      Pre-calculates and sets OrderItem.getRefundAmount() based on its OrderItem.getItemAdjustments() and associated offer qualifier and target items.
      void setOfferUtils​(OrderOperationServiceOfferUtils offerUtils)  
      void setPaymentProvider​(PaymentProvider paymentProvider)  
      protected void updateOfferQualifierOrderItemDetails​(List<com.broadleafcommerce.order.client.domain.OrderItem> orderItemsWithDependentOrderItems)
      Updates the offer qualifier OfferItemDetails that were copied from CartItems in OrderItems to use OrderItem.getId() as OfferItemDetail.getItemId().
      protected void updateOrderItemTotalsForVat​(C cart, List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems)  
    • Field Detail

      • orderProvider

        protected final OrderProvider<O extends com.broadleafcommerce.order.client.domain.Order> orderProvider
    • Constructor Detail

      • DefaultCartOrderGenerationService

        public DefaultCartOrderGenerationService​(OrderProvider<O> orderProvider,
                                                 com.broadleafcommerce.common.extension.TypeFactory typeFactory,
                                                 OrderGenerationProperties properties)
    • Method Detail

      • generateOrderFromCart

        @Nullable
        public O generateOrderFromCart​(C cart,
                                       @Nullable
                                       com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Description copied from interface: CartOrderGenerationService
        Creates and persists a new order from the given cart only if such an order does not already exist.
        Specified by:
        generateOrderFromCart in interface CartOrderGenerationService<C extends com.broadleafcommerce.cart.client.domain.Cart,​O extends com.broadleafcommerce.order.client.domain.Order>
        Parameters:
        cart - The cart to create an order from
        contextInfo - the context around sandboxing and multitenant state
        Returns:
        The order created from the cart, or null if an order for the cart already exists
      • orderExistsForCart

        protected boolean orderExistsForCart​(C cart,
                                             @Nullable
                                             com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Determine whether an Order has already been created for the Cart.
        Parameters:
        cart - The cart to check for an already created order
        contextInfo - the context around sandboxing and multitenant state
        Returns:
        Whether an order has already been created for the cart
      • buildOrderFromCart

        protected O buildOrderFromCart​(C cart,
                                       @Nullable
                                       com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Build the Order from the given Cart.
        Parameters:
        cart - The cart to build the order from
        Returns:
        The built order
      • getCartCustomerName

        @Nullable
        protected String getCartCustomerName​(C cart,
                                             @Nullable
                                             com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Get the customer's name from the cart. If there is no customer, use a name from the first payment's billing address.
        Parameters:
        cart - The cart from which to get the customer's name
        Returns:
        The name of the customer for the cart
      • buildOrderPricingFromCart

        @Nullable
        protected com.broadleafcommerce.order.client.domain.OrderPricing buildOrderPricingFromCart​(C cart)
        Build the OrderPricing for the given Cart.
        Parameters:
        cart - The cart to build order pricing from
        Returns:
        The build order pricing
      • buildOrderItemsFromCart

        protected List<com.broadleafcommerce.order.client.domain.OrderItem> buildOrderItemsFromCart​(C cart,
                                                                                                    O order)
        Build the OrderItems for the given Cart.
        Parameters:
        cart - The cart containing items to build the order items from
        order - The order to which the order items should belong
        Returns:
        The built order items
      • updateOrderItemTotalsForVat

        protected void updateOrderItemTotalsForVat​(C cart,
                                                   List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems)
      • updateOfferQualifierOrderItemDetails

        protected void updateOfferQualifierOrderItemDetails​(List<com.broadleafcommerce.order.client.domain.OrderItem> orderItemsWithDependentOrderItems)
        Updates the offer qualifier OfferItemDetails that were copied from CartItems in OrderItems to use OrderItem.getId() as OfferItemDetail.getItemId().
        Parameters:
        orderItemsWithDependentOrderItems - a list of OrderItems to update offer qualifier order item details for
      • precalculateRefundAmounts

        protected void precalculateRefundAmounts​(O order,
                                                 List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems)
        Pre-calculates and sets OrderItem.getRefundAmount() based on its OrderItem.getItemAdjustments() and associated offer qualifier and target items. Any custom refund calculation logic should be added here.

        By default, the refund amount is the unadjusted item price minus the proportionally distributed discount for each item based on their price ratio, using these formulas:

        price ratio = (unadjusted price of the item / total price of qualifier and target items before offer adjustment)

        proportionally distributed discount = price ratio * total discount

        refund amount = item unadjusted price - distributed discount.

        Note that the offer discount distribution is only done for Adjustments whose offer has DefaultOfferProrationType.TARGET_AND_QUALIFIER as OfferRef.getProrationType().

        For Adjustments with DefaultOfferProrationType.TARGET_ONLY as OfferRef.getProrationType(), the refund amount for qualifiers would be the price of the item, and for targets would be the price of the item minus the discount.

        Parameters:
        order - the Order that the OrderItems belong to
        orderItems - a list of OrderItems to calculate and set the refund amounts for
        See Also:
        DefaultOfferProrationType
      • getAdjustmentsByTargetItemWithQualifierItemId

        protected Map<com.broadleafcommerce.order.client.domain.OrderItem,​List<com.broadleafcommerce.order.common.domain.Adjustment>> getAdjustmentsByTargetItemWithQualifierItemId​(List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems,
                                                                                                                                                                                          String qualifierItemId)
        Gets a map of Adjustments by offer target OrderItem whose Adjustment.getQualifierDetails() is referencing the given offer qualifier item id.

        Note that any OrderItems with Adjustments are considered offer "targets".

        Parameters:
        orderItems - the OrderItems to build the map for
        qualifierItemId - the qualifier OfferItemDetail.getItemId() to filter the Adjustment.getQualifierDetails() with
        Returns:
        a map of Adjustments by target OrderItem whose Adjustment.getQualifierDetails() is referencing the given qualifier item id
      • getTargetItemDistributedDiscount

        protected javax.money.MonetaryAmount getTargetItemDistributedDiscount​(com.broadleafcommerce.order.common.domain.Adjustment adjustment,
                                                                              com.broadleafcommerce.order.client.domain.OrderItem targetItem,
                                                                              Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> orderItemById)
        Gets the proportionally distributed discount for the given offer target item based on its price ratio.
        Parameters:
        adjustment - the Adjustment to prorate
        targetItem - the target OrderItem receiving the discount
        orderItemById - a map of OrderItem by OrderItem.getId()
        Returns:
        the proportionally distributed discount for the given offer target item based on its price ratio
      • getQualifierItemDistributedDiscount

        protected javax.money.MonetaryAmount getQualifierItemDistributedDiscount​(com.broadleafcommerce.order.common.domain.Adjustment adjustment,
                                                                                 com.broadleafcommerce.order.client.domain.OrderItem qualifierItem,
                                                                                 com.broadleafcommerce.order.client.domain.OrderItem targetItem,
                                                                                 Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> orderItemById)
        Gets the proportionally distributed discount for the given offer qualifier item based on its price ratio.
        Parameters:
        adjustment - the Adjustment to prorate
        qualifierItem - the qualifier OrderItem that the discount should be prorated to
        targetItem - the target OrderItem that was originally targeted for the discount
        orderItemById - a map of OrderItem by OrderItem.getId()
        Returns:
        the proportionally distributed discount for the given offer qualifier item based on its price ratio
      • adjustmentShouldBeProrated

        protected boolean adjustmentShouldBeProrated​(com.broadleafcommerce.order.common.domain.Adjustment adjustment)
        Determines if the given Adjustment needs to be prorated across offer qualifier and target items.
        Parameters:
        adjustment - the Adjustment to check against
        Returns:
        true if the given Adjustment needs to be prorated across qualifier and target items, otherwise false
      • getQualifierItemsUnadjustedTotal

        protected javax.money.MonetaryAmount getQualifierItemsUnadjustedTotal​(com.broadleafcommerce.order.common.domain.Adjustment adjustment,
                                                                              Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> orderItemById)
        Gets the unadjusted item total of all the offer qualifier OrderItems.

        This is useful to calculate the price ratio when prorating offer discounts.

        Parameters:
        adjustment - the Adjustment to get the qualifier items from
        orderItemById - a map of OrderItem by OrderItem.getId()
        Returns:
        the unadjusted item total of all the offer qualifier OrderItems
      • getOrderItemsByOriginalCartItemIds

        protected Map<String,​com.broadleafcommerce.order.client.domain.OrderItem> getOrderItemsByOriginalCartItemIds​(List<com.broadleafcommerce.order.client.domain.OrderItem> orderItems)
        Gets a map of OrderItems by their original CartItem.getId().
        Parameters:
        orderItems - the list of OrderItems to build the map from
        Returns:
        a map from originally-received-cart-item-ids to OrderItems
      • getOriginalCartItemIdFromAttributes

        protected String getOriginalCartItemIdFromAttributes​(com.broadleafcommerce.order.client.domain.OrderItem item)
      • buildOrderItemFromCartItem

        protected com.broadleafcommerce.order.client.domain.OrderItem buildOrderItemFromCartItem​(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
        Build an OrderItem from the given CartItem.
        Parameters:
        cartItem - The cart item to build an order item from
        Returns:
        The built order item
      • buildImageAssetRef

        @Nullable
        protected com.broadleafcommerce.order.client.domain.ImageAssetRef buildImageAssetRef​(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      • buildDependentOrderItemsFromCartItems

        protected List<com.broadleafcommerce.order.client.domain.OrderItem> buildDependentOrderItemsFromCartItems​(List<com.broadleafcommerce.cart.client.domain.CartItem> cartItems)
        Build dependent OrderItems from the given dependent CartItems.
        Parameters:
        cartItems - The dependent cart items to build dependent order items from
        Returns:
        The built dependent order items
      • buildDependentOrderItemFromCartItem

        protected com.broadleafcommerce.order.client.domain.OrderItem buildDependentOrderItemFromCartItem​(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
        Build the dependent OrderItem from the given dependent CartItem.
        Parameters:
        cartItem - The dependent cart item to build a dependent order item from
        Returns:
        The built dependent order item
      • getTypeFactory

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

        @Autowired
        public void setPaymentProvider​(PaymentProvider paymentProvider)