Class: Dorsale::BillingMachine::Quotation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Dorsale::BillingMachine::Quotation
show all
- Defined in:
- app/models/dorsale/billing_machine/quotation.rb
Defined Under Namespace
Classes: Copy, Statistics, ToInvoice
Constant Summary
collapse
- STATES =
%w(pending accepted refused canceled)
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Quotation.
31
32
33
34
35
36
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 31
def initialize(*)
super
self.state = STATES.first if state.nil?
self.date = Time.zone.now.to_date if date.nil?
assign_default_values
end
|
Instance Method Details
#apply_vat_rate_to_lines ⇒ Object
118
119
120
121
122
123
124
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 118
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
52
53
54
55
56
57
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 52
def assign_default_values
self.expires_at = date + 1.month if expires_at.nil?
self.commercial_discount = 0 if commercial_discount.nil?
self.vat_amount = 0 if vat_amount.nil?
self.total_excluding_taxes = 0 if total_excluding_taxes
end
|
#assign_tracking_id ⇒ Object
48
49
50
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 48
def assign_tracking_id
self.tracking_id = date.year.to_s + "-" + unique_index.to_s.rjust(2, "0")
end
|
#assign_unique_index ⇒ Object
42
43
44
45
46
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 42
def assign_unique_index
if unique_index.nil?
self.unique_index = self.class.all.pluck(:unique_index).max.to_i.next
end
end
|
#balance ⇒ Object
92
93
94
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 92
def balance
self.total_including_taxes
end
|
#update_totals ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 61
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
end
|
#vat_rate ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 96
def vat_rate
if ::Dorsale::BillingMachine.vat_mode == :multiple
raise "Quotation#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 "Quotation has multiple vat rates"
end
vat_rates.first || ::Dorsale::BillingMachine::DEFAULT_VAT_RATE
end
|
#vat_rate=(value) ⇒ Object
112
113
114
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 112
def vat_rate=(value)
@vat_rate = value
end
|