Class: Einvoicing::Tax

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

Overview

VAT breakdown entry for a single tax rate.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate:, taxable_amount:, tax_amount:, category: nil) ⇒ Tax

Returns a new instance of Tax.

Parameters:

  • rate (Numeric)

    e.g. 0.20 for 20% VAT; 0 for zero-rated or reverse charge

  • taxable_amount (Numeric)

    net amount subject to this rate

  • tax_amount (Numeric)

    VAT amount for this rate

  • category (Symbol, nil) (defaults to: nil)

    nil for standard/zero, :reverse_charge for AE

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/einvoicing/tax.rb', line 13

def initialize(rate:, taxable_amount:, tax_amount:, category: nil)
  raise ArgumentError, Einvoicing::I18n.t("tax.invalid_rate", rate: rate) if rate.to_f.negative?

  super
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category

Returns:

  • (Object)

    the current value of category



8
9
10
# File 'lib/einvoicing/tax.rb', line 8

def category
  @category
end

#rateObject (readonly)

Returns the value of attribute rate

Returns:

  • (Object)

    the current value of rate



8
9
10
# File 'lib/einvoicing/tax.rb', line 8

def rate
  @rate
end

#tax_amountObject (readonly)

Returns the value of attribute tax_amount

Returns:

  • (Object)

    the current value of tax_amount



8
9
10
# File 'lib/einvoicing/tax.rb', line 8

def tax_amount
  @tax_amount
end

#taxable_amountObject (readonly)

Returns the value of attribute taxable_amount

Returns:

  • (Object)

    the current value of taxable_amount



8
9
10
# File 'lib/einvoicing/tax.rb', line 8

def taxable_amount
  @taxable_amount
end

Class Method Details

.category_code_for(rate:, category: nil) ⇒ Object

Shared lookup used by both Tax and LineItem.



20
21
22
23
24
25
26
# File 'lib/einvoicing/tax.rb', line 20

def self.category_code_for(rate:, category: nil)
  if rate.to_f == 0.0
    category == :reverse_charge ? "AE" : "Z"
  else
    "S"
  end
end

Instance Method Details

#category_codeObject



28
29
30
# File 'lib/einvoicing/tax.rb', line 28

def category_code
  Tax.category_code_for(rate: rate, category: category)
end

#rate_percentObject



32
33
34
35
36
# File 'lib/einvoicing/tax.rb', line 32

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

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