Class LineItem

java.lang.Object
de.hybris.order.calculation.domain.LineItem
All Implemented Interfaces:
Taxable
Direct Known Subclasses:
NumberedLineItem

public class LineItem extends Object implements Taxable
Represents one entry (or element) for an Order. Central objects that holds all line items related data required for amount calculation. A LineItem is Taxable which means getTotal(Order) is used for tax calculation. This line item holds the basePrice and the numberOfUnits. Additionally the giveAwayCount can be set (10 items are bought, 5 of them are free) and this line item holds a ordered List of LineItemDiscounts and LineItemCharges. The line item must use the same Currency as the linked Order or a CurrenciesAreNotEqualException is thrown.
  • Constructor Details

    • LineItem

      public LineItem(Money basePrice)
      Creates a new line item with the given base price and one unit.
    • LineItem

      public LineItem(Money basePrice, int numberOfUnits)
      Creates a new line item with the given base price and number of units.
  • Method Details

    • getTotal

      public Money getTotal(Order context)
      Calculates to line item total. This includes sub total, discounts and charges.
      Specified by:
      getTotal in interface Taxable
      Parameters:
      context - the order within the tax is being calculated
      See Also:
    • getSubTotal

      public Money getSubTotal()
      Calculates the line item sub total. This is just the bas price multiplied by the number of applicable units (see setGiveAwayUnits(int) for how some units may be excluded).
      See Also:
    • getTotalDiscount

      public Money getTotalDiscount()
      Calculates the total of all line item discounts.
      See Also:
    • getTotalCharge

      public Money getTotalCharge()
      Calculates the total of all line item charges.
      See Also:
    • getTotalDiscounts

      public Map<LineItemDiscount,Money> getTotalDiscounts()
      Calculates the totals of all line item discounts separately.
      Returns:
      a map ( LineItemDiscount -> Money )
      See Also:
    • getTotalCharges

      public Map<LineItemCharge,Money> getTotalCharges()
      Calculates totals of all line item charges separately.
      Returns:
      a map ( LineItemCharge -> Money )
      See Also:
    • calculateDiscount

      protected Money calculateDiscount(Money currentValue, LineItemDiscount discount)
    • calculateCharge

      protected Money calculateCharge(Money currentValue, LineItemCharge charge)
    • getNumberOfUnits

      public int getNumberOfUnits()
      Returns the regular number of units within that line item. Please note that some units may be excluded from calculation via setGiveAwayUnits(int).
      See Also:
    • setNumberOfUnits

      public final void setNumberOfUnits(int numberOfUnits)
      Changes the number of regular units within that line item. Please note that some units may be excluded from calculation via setGiveAwayUnits(int).
      See Also:
    • setOrder

      public void setOrder(Order order)
      Sets the Order to for this line item belongs to. Is needed for the calculation because all line item must have the same currency (Order.getCurrency() is used to check it).
    • getOrder

      public Order getOrder()
      Returns:
      the order this line item belongs to. Can be null but no calculation is then possible!
      Throws:
      MissingCalculationDataException - if order is null and this method or any calculation method (getTotal...) is called.
    • getBasePrice

      public Money getBasePrice()
      Returns the base price of this line item.
    • toString

      public String toString()
      {numberOfUnits}x {basePrice} {currency}(free:{giveAwayCount}) discounts:[...] charges:[...]
      Overrides:
      toString in class Object
    • getDiscounts

      public List<LineItemDiscount> getDiscounts()
      Returns all discounts assigned to this line item.
    • addDiscounts

      public void addDiscounts(LineItemDiscount... discounts)
      Adds multiple discounts to this line item.
      Throws:
      CurrenciesAreNotEqualException - in case at least one discount is absolute and uses a different currency than the enclosing order
    • addDiscounts

      public void addDiscounts(List<LineItemDiscount> discounts)
      Adds multiple discounts to this line item.
      Throws:
      CurrenciesAreNotEqualException - in case at least one discount is absolute and uses a different currency than the enclosing order
    • addDiscount

      public void addDiscount(int index, LineItemDiscount discount)
      Adds a single discount to this line item at a specific position.
      Parameters:
      index - the position to add the discount at
      Throws:
      CurrenciesAreNotEqualException - in case at it's a absolute discount and uses a different currency than the enclosing order
    • assertCurrency

      protected void assertCurrency(AbstractAmount amount)
    • addDiscount

      public void addDiscount(LineItemDiscount discount)
      Adds a single discount to this line item.
      Throws:
      CurrenciesAreNotEqualException - in case at it's a absolute discount and uses a different currency than the enclosing order
    • clearDiscounts

      public void clearDiscounts()
      Removes all attached discounts of the current line item.
    • removeDiscount

      public void removeDiscount(LineItemDiscount discount)
      Removes a single discount from this line item.
    • getCharges

      public List<LineItemCharge> getCharges()
      Returns all charges assigned to this line item.
    • addCharges

      public void addCharges(LineItemCharge... lineItemCharges)
      Adds multiple charges to this line item.
      Throws:
      CurrenciesAreNotEqualException - in case there is at least one absolute charge which uses a different currency than the enclosing order
    • addCharges

      public void addCharges(List<LineItemCharge> lineItemCharges)
      Adds multiple charges to this line item.
      Throws:
      CurrenciesAreNotEqualException - in case there is at least one absolute charge which uses a different currency than the enclosing order
    • addCharge

      public void addCharge(LineItemCharge lineItemCharge)
      Adds a single charge to this line item.
      Throws:
      CurrenciesAreNotEqualException - in case at it's a absolute charge and uses a different currency than the enclosing order
    • addCharge

      public void addCharge(int index, LineItemCharge lineItemCharge)
      Adds a single charge to this line item at a specific position.
      Parameters:
      index - the position to add the charge at
      Throws:
      CurrenciesAreNotEqualException - in case at it's a absolute charge and uses a different currency than the enclosing order
    • clearCharges

      public void clearCharges()
      Removes all attached charges of the current line item.
    • removeCharge

      public void removeCharge(LineItemCharge charge)
      Removes a charge from this line item.
    • getNumberOfUnitsForCalculation

      public int getNumberOfUnitsForCalculation()
      Returns the number of units that are applicable for calculation. Basically this is getNumberOfUnits() - getGiveAwayUnits().
    • getGiveAwayUnits

      public int getGiveAwayUnits()
      Tells the number of units to be 'for free' meaning that they're excluded from calculation completely. In consequence for calculation this line item appears to hold <numberOfUnits> - <giveAwayCount> units. In case the number of give-away units is greater than the actual available number of units the line item appear to have zero units.
      See Also:
    • setGiveAwayUnits

      public final void setGiveAwayUnits(int giveAwayCount)
      Changes the number of units to be 'for free' meaning that they're excluded from calculation completely. In consequence for calculation this line item appears to hold <numberOfUnits> - <giveAwayCount> units. In case the number of give-away units is greater than the actual available number of units the line item appear to have zero units.
      See Also:
    • getTaxes

      public Collection<Tax> getTaxes()
      Returns all taxes which are targeting this line item.
      See Also: