Class: BillTrap::Invoice
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- BillTrap::Invoice
show all
- Extended by:
- Helpers
- Defined in:
- lib/billtrap/models.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helpers
format_date, format_money, is_i?
Class Method Details
.completed ⇒ Object
52
53
54
|
# File 'lib/billtrap/models.rb', line 52
def self.completed
exclude(:sent => nil).all.select { |i| i.paid? }
end
|
.current ⇒ Object
76
77
78
79
|
# File 'lib/billtrap/models.rb', line 76
def self.current
last = Meta.find(:key => 'current_invoice')
Invoice[last.value]
end
|
.current=(id) ⇒ Object
70
71
72
73
74
|
# File 'lib/billtrap/models.rb', line 70
def self.current= id
last = Meta.find_or_create(:key => 'current_invoice')
last.value = id
last.save
end
|
.get(inv) ⇒ Object
Retrieve invoice If numeric, assumes it’s an ID If string, returns first match inv == name Returns nil otherwise / if no match was found
60
61
62
63
64
65
66
67
68
|
# File 'lib/billtrap/models.rb', line 60
def self.get inv
if is_i? inv
Invoice[inv]
elsif inv.is_a? String
Invoice.find(:name => inv)
else
nil
end
end
|
.open ⇒ Object
48
49
50
|
# File 'lib/billtrap/models.rb', line 48
def self.open
all.select { |i| !i.paid? }
end
|
Instance Method Details
#currency ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/billtrap/models.rb', line 81
def currency
if client_id
client.currency
else
BillTrap::Config['currency']
end
end
|
#due_date ⇒ Object
131
132
133
|
# File 'lib/billtrap/models.rb', line 131
def due_date
created + BillTrap::Config['due_date']
end
|
#overdue? ⇒ Boolean
127
128
129
|
# File 'lib/billtrap/models.rb', line 127
def overdue?
due_date > Date.today
end
|
#paid? ⇒ Boolean
103
104
105
106
107
108
109
110
|
# File 'lib/billtrap/models.rb', line 103
def paid?
if sent.nil?
return false
end
puts "received = #{received_amount}, total = #{total}"
return received_amount == total
end
|
#rate ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/billtrap/models.rb', line 112
def rate
rate = if client.nil?
BillTrap::Config['default_rate']
else
client.rate
end
Money.parse(rate, currency)
end
|
#received_amount ⇒ Object
122
123
124
125
|
# File 'lib/billtrap/models.rb', line 122
def received_amount
cents = Payment.where(:invoice_id => id).sum(:cents)
Money.new(cents, currency)
end
|
#set_attr(k, v) ⇒ Object
89
90
91
92
|
# File 'lib/billtrap/models.rb', line 89
def set_attr k,v
attributes[k] = v
save
end
|
#total ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'lib/billtrap/models.rb', line 94
def total
sum = Money.new(0, currency)
entries = InvoiceEntry.filter(:invoice_id=>id)
entries.each do |entry|
sum += entry.total
end
return sum
end
|