Class: Croatia::Invoice::LineItem

Inherits:
Object
  • Object
show all
Includes:
Enum
Defined in:
lib/croatia/invoice/line_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enum

included

Constructor Details

#initialize(**options) ⇒ LineItem

Returns a new instance of LineItem.



12
13
14
15
16
17
18
# File 'lib/croatia/invoice/line_item.rb', line 12

def initialize(**options)
  self.description = options[:description]
  self.quantity = options.fetch(:quantity, 1)
  self.unit = options[:unit]
  self.unit_price = options.fetch(:unit_price, 0.0)
  self.taxes = options.fetch(:taxes, {})
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/croatia/invoice/line_item.rb', line 9

def description
  @description
end

#discount_rateObject

Returns the value of attribute discount_rate.



10
11
12
# File 'lib/croatia/invoice/line_item.rb', line 10

def discount_rate
  @discount_rate
end

#quantityObject

Returns the value of attribute quantity.



10
11
12
# File 'lib/croatia/invoice/line_item.rb', line 10

def quantity
  @quantity
end

#taxesObject

Returns the value of attribute taxes.



9
10
11
# File 'lib/croatia/invoice/line_item.rb', line 9

def taxes
  @taxes
end

#unitObject

Returns the value of attribute unit.



9
10
11
# File 'lib/croatia/invoice/line_item.rb', line 9

def unit
  @unit
end

#unit_priceObject

Returns the value of attribute unit_price.



10
11
12
# File 'lib/croatia/invoice/line_item.rb', line 10

def unit_price
  @unit_price
end

Instance Method Details

#add_tax(tax = nil, **options, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/croatia/invoice/line_item.rb', line 93

def add_tax(tax = nil, **options, &block)
  if tax.nil?
    tax = Croatia::Invoice::Tax.new(**options)
  end

  tax.tap(&block) if block_given?

  taxes[tax.type] = tax
  tax
end

#discountObject



63
64
65
66
67
68
69
70
71
# File 'lib/croatia/invoice/line_item.rb', line 63

def discount
  if @discount
    @discount
  elsif @discount_rate
    (gross * @discount_rate).round(2, BigDecimal::ROUND_HALF_UP)
  else
    BigDecimal("0.0")
  end
end

#discount=(value) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/croatia/invoice/line_item.rb', line 50

def discount=(value)
  if value.nil?
    @discount = nil
    return
  end

  unless value.is_a?(Numeric) && value >= 0
    raise ArgumentError, "Discount must be a non-negative number"
  end

  @discount = value.to_d.round(2, BigDecimal::ROUND_HALF_UP)
end

#grossObject



77
78
79
# File 'lib/croatia/invoice/line_item.rb', line 77

def gross
  (quantity * unit_price).round(2, BigDecimal::ROUND_HALF_UP)
end

#reverseObject



73
74
75
# File 'lib/croatia/invoice/line_item.rb', line 73

def reverse
  self.quantity *= -1
end

#subtotalObject



81
82
83
# File 'lib/croatia/invoice/line_item.rb', line 81

def subtotal
  gross - discount
end

#taxObject



85
86
87
# File 'lib/croatia/invoice/line_item.rb', line 85

def tax
  taxes.values.sum { |tax_obj| (subtotal * tax_obj.rate).round(2, BigDecimal::ROUND_HALF_UP) }
end

#totalObject



89
90
91
# File 'lib/croatia/invoice/line_item.rb', line 89

def total
  (subtotal + tax).round(2, BigDecimal::ROUND_HALF_UP)
end