Class: Taxedo::Receipt

Inherits:
Object
  • Object
show all
Defined in:
lib/taxedo/receipt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region_id, amount) ⇒ Receipt

Returns a new instance of Receipt.



5
6
7
8
9
10
11
# File 'lib/taxedo/receipt.rb', line 5

def initialize(region_id, amount)
  @region_id = region_id
  @amount = amount
  @taxes = []

  load_translations
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



2
3
4
# File 'lib/taxedo/receipt.rb', line 2

def currency
  @currency
end

#equation_typeObject

Returns the value of attribute equation_type.



3
4
5
# File 'lib/taxedo/receipt.rb', line 3

def equation_type
  @equation_type
end

Instance Method Details

#add_tax(id, rate) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/taxedo/receipt.rb', line 13

def add_tax(id, rate)
  tax = Taxedo::Tax.new(id)
  tax.name = tax_name(id)
  tax.rate = rate
  tax.source_amount = get_source_amount

  @taxes << tax
end

#get_source_amountObject



22
23
24
25
26
27
28
# File 'lib/taxedo/receipt.rb', line 22

def get_source_amount
  if @taxes.empty? or self.equation_type == 'separated'
    @amount
  else
    @taxes.last.subtotal
  end
end

#languageObject



30
31
32
# File 'lib/taxedo/receipt.rb', line 30

def language
  defined?(I18n) ? I18n.locale.to_s : 'fr'
end

#subtotalObject



34
35
36
# File 'lib/taxedo/receipt.rb', line 34

def subtotal
  @amount
end

#t(path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/taxedo/receipt.rb', line 38

def t(path)
  text = @translations

  path.split('.').each do |p|
    raise "TAXEDO: No #{language} translation for #{path}." if not text
    text = text[p]
  end

  return text.to_s
end

#taxesObject



49
50
51
# File 'lib/taxedo/receipt.rb', line 49

def taxes
  @taxes
end

#to_hashObject



60
61
62
# File 'lib/taxedo/receipt.rb', line 60

def to_hash
  Taxedo::Builder::Hash.new(self).generate
end

#to_html(options = {}) ⇒ Object



64
65
66
67
68
# File 'lib/taxedo/receipt.rb', line 64

def to_html(options={})
  options = { columns: 0, custom_content: '', template: :table }.merge(options)

  return Taxedo::Builder::Html.new(self).generate(options[:template], options)
end

#to_jsonObject



70
71
72
# File 'lib/taxedo/receipt.rb', line 70

def to_json
  Taxedo::Builder::Json.new(self).generate
end

#to_textObject



74
75
76
# File 'lib/taxedo/receipt.rb', line 74

def to_text
  Taxedo::Builder::Text.new(self).generate
end

#totalObject



53
54
55
56
57
58
# File 'lib/taxedo/receipt.rb', line 53

def total
  @total = subtotal
  @taxes.each { |t| @total += t.amount }

  return @total
end