Class: Croatia::Invoice

Inherits:
Object
  • Object
show all
Includes:
Enum, EInvoicable, Fiscalizable, Payable
Defined in:
lib/croatia/invoice.rb

Defined Under Namespace

Modules: EInvoicable, Fiscalizable, Payable Classes: LineItem, Party, Tax

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Payable

included

Methods included from Fiscalizable

included

Methods included from Enum

included

Constructor Details

#initialize(**options) ⇒ Invoice

Returns a new instance of Invoice.



32
33
34
35
36
37
38
39
40
# File 'lib/croatia/invoice.rb', line 32

def initialize(**options)
  self.line_items = options.delete(:line_items) { [] }
  self.payment_method = :card
  self.sequential_by = :register

  options.each do |key, value|
    public_send("#{key}=", value)
  end
end

Instance Attribute Details

#business_location_identifierObject

Returns the value of attribute business_location_identifier.



21
22
23
# File 'lib/croatia/invoice.rb', line 21

def business_location_identifier
  @business_location_identifier
end

#buyer(&block) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/croatia/invoice.rb', line 83

def buyer(&block)
  if block_given?
    self.buyer = Party.new.tap(&block)
  else
    @buyer
  end
end

#currencyObject

Returns the value of attribute currency.



21
22
23
# File 'lib/croatia/invoice.rb', line 21

def currency
  @currency
end

#due_dateObject

Returns the value of attribute due_date.



19
20
21
# File 'lib/croatia/invoice.rb', line 19

def due_date
  @due_date
end

#issue_dateObject

Returns the value of attribute issue_date.



19
20
21
# File 'lib/croatia/invoice.rb', line 19

def issue_date
  @issue_date
end

#issuer(&block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/croatia/invoice.rb', line 75

def issuer(&block)
  if block_given?
    self.issuer = Party.new.tap(&block)
  else
    @issuer
  end
end

#line_itemsObject

Returns the value of attribute line_items.



21
22
23
# File 'lib/croatia/invoice.rb', line 21

def line_items
  @line_items
end

#register_identifierObject

Returns the value of attribute register_identifier.



21
22
23
# File 'lib/croatia/invoice.rb', line 21

def register_identifier
  @register_identifier
end

#seller(&block) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/croatia/invoice.rb', line 91

def seller(&block)
  if block_given?
    self.seller = Party.new.tap(&block)
  else
    @seller
  end
end

#sequential_numberObject

Returns the value of attribute sequential_number.



21
22
23
# File 'lib/croatia/invoice.rb', line 21

def sequential_number
  @sequential_number
end

#unique_invoice_identifierObject

Returns the value of attribute unique_invoice_identifier.



21
22
23
# File 'lib/croatia/invoice.rb', line 21

def unique_invoice_identifier
  @unique_invoice_identifier
end

Instance Method Details

#add_line_item(line_item = nil, &block) ⇒ Object



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

def add_line_item(line_item = nil, &block)
  if line_item.nil? && block.nil?
    raise ArgumentError, "You must provide a line item or a block"
  end

  line_item ||= LineItem.new.tap(&block)

  self.line_items ||= []
  line_items << line_item

  line_item
end

#numberObject



42
43
44
# File 'lib/croatia/invoice.rb', line 42

def number
  [ sequential_number, business_location_identifier, register_identifier ].join("/")
end

#subtotalObject



46
47
48
# File 'lib/croatia/invoice.rb', line 46

def subtotal
  line_items.sum(&:subtotal).to_d
end

#taxObject



50
51
52
# File 'lib/croatia/invoice.rb', line 50

def tax
  line_items.sum(&:tax).to_d
end

#totalObject



54
55
56
# File 'lib/croatia/invoice.rb', line 54

def total
  line_items.sum(&:total).to_d
end

#total_centsObject



58
59
60
# File 'lib/croatia/invoice.rb', line 58

def total_cents
  (total * 100).to_i
end