Class: Panier::Domain::LineItem

Inherits:
Object
  • Object
show all
Defined in:
lib/panier/domain/line_item.rb

Overview

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

Constant Summary collapse

TAX_ROUNDING_VALUE =

The fractional value to which tax rounding calculations are made.

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product, quantity) ⇒ LineItem

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

Parameters:

  • product (Product)

    The product represented in the line item.

  • quantity (Integer)

    The number of products represented.



24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



16
17
18
# File 'lib/panier/domain/line_item.rb', line 16

def description
  @description
end

#productObject (readonly)

Returns the value of attribute product.



16
17
18
# File 'lib/panier/domain/line_item.rb', line 16

def product
  @product
end

#quantityObject

Returns the value of attribute quantity.



16
17
18
# File 'lib/panier/domain/line_item.rb', line 16

def quantity
  @quantity
end

#tax_classesObject (readonly)

Returns the value of attribute tax_classes.



16
17
18
# File 'lib/panier/domain/line_item.rb', line 16

def tax_classes
  @tax_classes
end

#unit_amountObject (readonly)

Returns the value of attribute unit_amount.



16
17
18
# File 'lib/panier/domain/line_item.rb', line 16

def unit_amount
  @unit_amount
end

Instance Method Details

#total_amountObject

Calculates the total value of the line item.



36
37
38
# File 'lib/panier/domain/line_item.rb', line 36

def total_amount
  unit_amount * quantity
end

#total_amount_inc_taxMoney

Calculates the total value of the line item including tax.

Returns:

  • (Money)

    The total value of the line item including tax.



51
52
53
# File 'lib/panier/domain/line_item.rb', line 51

def total_amount_inc_tax
  unit_amount_inc_tax * quantity
end

#total_taxObject

Calculates the total tax included in the line item.



43
44
45
# File 'lib/panier/domain/line_item.rb', line 43

def total_tax
  unit_tax * quantity
end

#unit_amount_inc_taxObject

Calculates the value of a single unit including tax.



58
59
60
# File 'lib/panier/domain/line_item.rb', line 58

def unit_amount_inc_tax
  unit_amount + unit_tax
end

#unit_taxObject

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



65
66
67
68
69
70
71
72
# 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