class Panier::Domain::LineItem

A line item is a value object representing a single line of an order or receipt.

Constants

TAX_ROUNDING_VALUE

The fractional value to which tax rounding calculations are made.

Attributes

description[R]
product[R]
quantity[R]
tax_classes[R]
unit_amount[R]

Public Class Methods

new(product, quantity) click to toggle source

Initializes the line such that it represents the given quantity of products.

@param product [Product] The product represented in the line item. @param quantity [Integer] The number of products represented.

# File lib/panier/domain/line_item.rb, line 24
def initialize(product, quantity)
  @product = product
  self.quantity = quantity
  @rounding_strategy = RoundUpRounding.new(TAX_ROUNDING_VALUE)
  @description = product.name
  @unit_amount = product.price
  @tax_classes = product.tax_classes.dup
end

Public Instance Methods

total_amount() click to toggle source

Calculates the total value of the line item.

# File lib/panier/domain/line_item.rb, line 36
def total_amount
  unit_amount * quantity
end
total_amount_inc_tax() click to toggle source

Calculates the total value of the line item including tax.

@return [Money] The total value of the line item including tax.

# File lib/panier/domain/line_item.rb, line 51
def total_amount_inc_tax
  unit_amount_inc_tax * quantity
end
total_tax() click to toggle source

Calculates the total tax included in the line item.

# File lib/panier/domain/line_item.rb, line 43
def total_tax
  unit_tax * quantity
end
unit_amount_inc_tax() click to toggle source

Calculates the value of a single unit including tax.

# File lib/panier/domain/line_item.rb, line 58
def unit_amount_inc_tax
  unit_amount + unit_tax
end
unit_tax() click to toggle source

Calculates the tax applicable to one unit of the line item.

# File lib/panier/domain/line_item.rb, line 65
def unit_tax
  tax = Money.new(0)
  tax_classes.each do |tax_class|
    class_tax = @rounding_strategy.round(tax_class.rate * unit_amount)
    tax += class_tax
  end
  tax
end

Private Instance Methods

quantity=(quantity) click to toggle source
# File lib/panier/domain/line_item.rb, line 76
def quantity=(quantity)
  unless quantity.is_a? Integer
    fail ArgumentError, ':quantity must be a whole number'
  end
  fail ArgumentError, ':quantity must be non-negative' if quantity < 0
  @quantity = quantity
end