Class: Croatia::Invoice::LineItem
- Inherits:
-
Object
- Object
- Croatia::Invoice::LineItem
- Includes:
- Enum
- Defined in:
- lib/croatia/invoice/line_item.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#discount_rate ⇒ Object
Returns the value of attribute discount_rate.
-
#quantity ⇒ Object
Returns the value of attribute quantity.
-
#taxes ⇒ Object
Returns the value of attribute taxes.
-
#unit ⇒ Object
Returns the value of attribute unit.
-
#unit_price ⇒ Object
Returns the value of attribute unit_price.
Instance Method Summary collapse
- #add_tax(tax = nil, **options, &block) ⇒ Object
- #discount ⇒ Object
- #discount=(value) ⇒ Object
- #gross ⇒ Object
-
#initialize(**options) ⇒ LineItem
constructor
A new instance of LineItem.
- #reverse ⇒ Object
- #subtotal ⇒ Object
- #tax ⇒ Object
- #total ⇒ Object
Methods included from Enum
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(**) self.description = [:description] self.quantity = .fetch(:quantity, 1) self.unit = [:unit] self.unit_price = .fetch(:unit_price, 0.0) self.taxes = .fetch(:taxes, {}) end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
9 10 11 |
# File 'lib/croatia/invoice/line_item.rb', line 9 def description @description end |
#discount_rate ⇒ Object
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 |
#quantity ⇒ Object
Returns the value of attribute quantity.
10 11 12 |
# File 'lib/croatia/invoice/line_item.rb', line 10 def quantity @quantity end |
#taxes ⇒ Object
Returns the value of attribute taxes.
9 10 11 |
# File 'lib/croatia/invoice/line_item.rb', line 9 def taxes @taxes end |
#unit ⇒ Object
Returns the value of attribute unit.
9 10 11 |
# File 'lib/croatia/invoice/line_item.rb', line 9 def unit @unit end |
#unit_price ⇒ Object
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, **, &block) if tax.nil? tax = Croatia::Invoice::Tax.new(**) end tax.tap(&block) if block_given? taxes[tax.type] = tax tax end |
#discount ⇒ Object
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 |
#gross ⇒ Object
77 78 79 |
# File 'lib/croatia/invoice/line_item.rb', line 77 def gross (quantity * unit_price).round(2, BigDecimal::ROUND_HALF_UP) end |
#reverse ⇒ Object
73 74 75 |
# File 'lib/croatia/invoice/line_item.rb', line 73 def reverse self.quantity *= -1 end |
#subtotal ⇒ Object
81 82 83 |
# File 'lib/croatia/invoice/line_item.rb', line 81 def subtotal gross - discount end |
#tax ⇒ Object
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 |
#total ⇒ Object
89 90 91 |
# File 'lib/croatia/invoice/line_item.rb', line 89 def total (subtotal + tax).round(2, BigDecimal::ROUND_HALF_UP) end |