Class: Dorsale::BillingMachine::Invoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Dorsale::BillingMachine::Invoice
show all
- Defined in:
- app/models/dorsale/billing_machine/invoice.rb
Defined Under Namespace
Classes: Copy, Statistics
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Invoice
Returns a new instance of Invoice.
21
22
23
24
25
26
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 21
def initialize(*args)
super
assign_default_values
self.due_date = 30.days.from_now if due_date.nil?
self.date = Time.zone.now.to_date if date.nil?
end
|
Instance Method Details
#apply_vat_rate_to_lines ⇒ Object
97
98
99
100
101
102
103
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 97
def apply_vat_rate_to_lines
return if ::Dorsale::BillingMachine.vat_mode == :multiple
lines.each do |line|
line.vat_rate = vat_rate
end
end
|
#assign_default_values ⇒ Object
42
43
44
45
46
47
48
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 42
def assign_default_values
self.advance = 0.0 if advance.nil?
self.vat_amount = 0.0 if vat_amount.nil?
self.commercial_discount = 0.0 if commercial_discount.nil?
self.total_excluding_taxes = 0.0 if total_excluding_taxes.nil?
self.paid = false if paid.nil?
end
|
#assign_tracking_id ⇒ Object
38
39
40
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 38
def assign_tracking_id
self.tracking_id = date.year.to_s + "-" + unique_index.to_s.rjust(2, "0")
end
|
#assign_unique_index ⇒ Object
32
33
34
35
36
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 32
def assign_unique_index
if unique_index.nil?
self.unique_index = self.class.all.pluck(:unique_index).max.to_i.next
end
end
|
#payment_status ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 105
def payment_status
if paid?
return :paid
elsif due_date == nil
return :on_alert
elsif Time.zone.now.to_date >= due_date + 15
return :on_alert
elsif Time.zone.now.to_date > due_date
return :late
else
return :pending
end
end
|
#t(*args) ⇒ Object
119
120
121
122
123
124
125
126
127
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 119
def t(*args)
return super if args.any?
if balance && balance < 0
super(:credit_note)
else
self.class.t
end
end
|
#update_totals ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 52
def update_totals
assign_default_values
lines_sum = lines.map(&:total).sum
self.total_excluding_taxes = lines_sum - commercial_discount
if commercial_discount.nonzero? && lines_sum.nonzero?
discount_rate = commercial_discount / lines_sum
else
discount_rate = 0.0
end
self.vat_amount = 0.0
lines.each do |line|
line_total = line.total - (line.total * discount_rate)
self.vat_amount += (line_total * line.vat_rate / 100.0)
end
self.total_including_taxes = total_excluding_taxes + vat_amount
self.balance = total_including_taxes - advance
end
|
#vat_rate ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 75
def vat_rate
if ::Dorsale::BillingMachine.vat_mode == :multiple
raise "Invoice#vat_rate is not available in multiple vat mode"
end
return @vat_rate if @vat_rate
vat_rates = lines.map(&:vat_rate).uniq
if vat_rates.length > 1
raise "Invoice has multiple vat rates"
end
vat_rates.first || ::Dorsale::BillingMachine::DEFAULT_VAT_RATE
end
|
#vat_rate=(value) ⇒ Object
91
92
93
|
# File 'app/models/dorsale/billing_machine/invoice.rb', line 91
def vat_rate=(value)
@vat_rate = value
end
|