Class: GnuCash::Invoice

Inherits:
Object
  • Object
show all
Includes:
Timestamps
Defined in:
lib/gnucash/invoice.rb,
lib/gnucash/invoice/entry.rb,
lib/gnucash/invoice/runner.rb,
lib/gnucash/invoice/printer.rb,
lib/gnucash/invoice/version.rb,
lib/gnucash/invoice/currency.rb,
lib/gnucash/invoice/customer.rb

Defined Under Namespace

Modules: Runner Classes: Currency, Customer, Entry, InvoiceNotFound, Printer

Constant Summary collapse

VERSION =
"0.1.1"

Constants included from Timestamps

Timestamps::FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Timestamps

#from_timestamp

Constructor Details

#initialize(data) ⇒ Invoice

Returns a new instance of Invoice.



23
24
25
26
27
28
29
30
# File 'lib/gnucash/invoice.rb', line 23

def initialize data
  @raw        = data

  @id         = data[:id]
  @opened_at  = from_timestamp data[:date_opened]
  @posted_at  = from_timestamp data[:date_posted]
  @notes      = data[:notes]
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



20
21
22
# File 'lib/gnucash/invoice.rb', line 20

def id
  @id
end

#notesObject (readonly)

Returns the value of attribute notes.



20
21
22
# File 'lib/gnucash/invoice.rb', line 20

def notes
  @notes
end

#opened_atObject (readonly)

Returns the value of attribute opened_at.



20
21
22
# File 'lib/gnucash/invoice.rb', line 20

def opened_at
  @opened_at
end

#posted_atObject (readonly)

Returns the value of attribute posted_at.



20
21
22
# File 'lib/gnucash/invoice.rb', line 20

def posted_at
  @posted_at
end

#rawObject (readonly)

Returns the value of attribute raw.



20
21
22
# File 'lib/gnucash/invoice.rb', line 20

def raw
  @raw
end

Class Method Details

.allObject



70
71
72
# File 'lib/gnucash/invoice.rb', line 70

def self.all
  dataset.map{ |data| new(data) }
end

.find(id) ⇒ Object



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

def self.find id
  unless data = dataset.where(:id => id).first
    raise InvoiceNotFound, "ID: #{id}"
  end

  new(data)
end

Instance Method Details

#currencyObject



48
49
50
# File 'lib/gnucash/invoice.rb', line 48

def currency
  @currency ||= Currency.find @raw[:currency]
end

#customerObject



38
39
40
# File 'lib/gnucash/invoice.rb', line 38

def customer
  @customer ||= Customer.find @raw[:owner_guid]
end

#entriesObject



43
44
45
# File 'lib/gnucash/invoice.rb', line 43

def entries
  @entries ||= Entry.find @raw[:guid]
end

#posted?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/gnucash/invoice.rb', line 33

def posted?
  posted_at.is_a? DateTime
end

#to_sObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/gnucash/invoice.rb', line 58

def to_s
  open_date = "(#{opened_at.strftime '%Y/%m/%d'})"
   = ""

  if posted?
     = "[#{posted_at.strftime '%Y/%m/%d'}]"
  end

  "%-16s %-32s %s %s" % [ id, customer.name, open_date,  ]
end

#totalObject



53
54
55
# File 'lib/gnucash/invoice.rb', line 53

def total
  entries.inject(0){ |memo,entry| memo + entry.total }
end