Class: Dorsale::BillingMachine::Quotation
Defined Under Namespace
Classes: Copy, Statistics, ToInvoice
Constant Summary
collapse
- STATES =
%w(draft pending accepted refused canceled)
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#vat_rate ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 98
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
|
Instance Method Details
#apply_vat_rate_to_lines ⇒ Object
116
117
118
119
120
121
122
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 116
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
50
51
52
53
54
55
56
57
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 50
def assign_default_values
assign_default :state, STATES.first
assign_default :date, Date.current
assign_default :expires_at, date + 1.month
assign_default :commercial_discount, 0
assign_default :vat_amount, 0
assign_default :total_excluding_taxes, 0
end
|
#assign_tracking_id ⇒ Object
46
47
48
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 46
def assign_tracking_id
self.tracking_id = date.year.to_s + "-" + unique_index.to_s.rjust(2, "0")
end
|
#assign_unique_index ⇒ Object
40
41
42
43
44
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 40
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
rubocop:enable Style/SingleLineMethods
94
95
96
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 94
def balance
total_including_taxes
end
|
#document_type ⇒ Object
32
33
34
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 32
def document_type
:quotation
end
|
#on_attachment_action(_attachment, _action) ⇒ Object
#update_totals ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'app/models/dorsale/billing_machine/quotation.rb', line 59
def update_totals
assign_default_values
lines.each(&:update_total)
apply_vat_rate_to_lines
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)
line_vat = (line_total * line.vat_rate / 100.0)
line_vat = line_vat.round(2) if Dorsale::BillingMachine.vat_round_by_line
self.vat_amount += line_vat
end
self.vat_amount = vat_amount.round(2)
self.total_including_taxes = total_excluding_taxes + vat_amount
end
|