Class: Einvoicing::LineItem

Inherits:
Data
  • Object
show all
Defined in:
lib/einvoicing/line_item.rb

Overview

A single line on an invoice.

Examples:

Einvoicing::LineItem.new(
  description: "Software consulting",
  quantity: 5,
  unit_price: 150.00,
  vat_rate: 0.20
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description:, quantity:, unit_price:, vat_rate: 0.20, unit: "C62", category: nil) ⇒ LineItem

Returns a new instance of LineItem.



17
18
19
20
21
22
23
24
25
26
# File 'lib/einvoicing/line_item.rb', line 17

def initialize(description:, quantity:, unit_price:, vat_rate: 0.20, unit: "C62", category: nil)
  super(
    description: description,
    quantity:    BigDecimal(quantity.to_s),
    unit_price:  BigDecimal(unit_price.to_s),
    vat_rate:    vat_rate,
    unit:        unit,
    category:    category
  )
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category

Returns:

  • (Object)

    the current value of category



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

def category
  @category
end

#descriptionObject (readonly)

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



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

def description
  @description
end

#quantityObject (readonly)

Returns the value of attribute quantity

Returns:

  • (Object)

    the current value of quantity



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

def quantity
  @quantity
end

#unitObject (readonly)

Returns the value of attribute unit

Returns:

  • (Object)

    the current value of unit



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

def unit
  @unit
end

#unit_priceObject (readonly)

Returns the value of attribute unit_price

Returns:

  • (Object)

    the current value of unit_price



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

def unit_price
  @unit_price
end

#vat_rateObject (readonly)

Returns the value of attribute vat_rate

Returns:

  • (Object)

    the current value of vat_rate



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

def vat_rate
  @vat_rate
end

Instance Method Details

#gross_amountObject

Gross line total (including VAT).



39
40
41
# File 'lib/einvoicing/line_item.rb', line 39

def gross_amount
  (net_amount + vat_amount).round(2, :half_up)
end

#net_amountObject

Net line total (excluding VAT).



29
30
31
# File 'lib/einvoicing/line_item.rb', line 29

def net_amount
  (quantity * unit_price).round(2, :half_up)
end

#tax_category_codeObject

CII/UBL tax category code — delegates to shared Tax logic.



50
51
52
# File 'lib/einvoicing/line_item.rb', line 50

def tax_category_code
  Tax.category_code_for(rate: vat_rate, category: category)
end

#vat_amountObject

VAT amount for this line.



34
35
36
# File 'lib/einvoicing/line_item.rb', line 34

def vat_amount
  (net_amount * BigDecimal(vat_rate.to_s)).round(2, :half_up)
end

#vat_rate_percentObject



43
44
45
46
47
# File 'lib/einvoicing/line_item.rb', line 43

def vat_rate_percent
  return BigDecimal("0") if category == :reverse_charge

  (BigDecimal(vat_rate.to_s) * 100).round(2)
end