Class: Dorsale::BillingMachine::Quotation

Inherits:
ApplicationRecord 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

Methods inherited from ApplicationRecord

last_created

Instance Method Details

#after_attachments_changesObject



122
123
124
# File 'app/models/dorsale/billing_machine/quotation.rb', line 122

def after_attachments_changes
  Dorsale::BillingMachine::PdfFileGenerator.(self)
end

#apply_vat_rate_to_linesObject



114
115
116
117
118
119
120
# File 'app/models/dorsale/billing_machine/quotation.rb', line 114

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_valuesObject



46
47
48
49
50
51
52
53
# File 'app/models/dorsale/billing_machine/quotation.rb', line 46

def assign_default_values
  assign_default :state,                  STATES.first
  assign_default :date,                   Time.zone.now.to_date
  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_idObject



42
43
44
# File 'app/models/dorsale/billing_machine/quotation.rb', line 42

def assign_tracking_id
  self.tracking_id = date.year.to_s + "-" + unique_index.to_s.rjust(2, "0")
end

#assign_unique_indexObject



36
37
38
39
40
# File 'app/models/dorsale/billing_machine/quotation.rb', line 36

def assign_unique_index
  if unique_index.nil?
    self.unique_index = self.class.all.pluck(:unique_index).max.to_i.next
  end
end

#balanceObject



88
89
90
# File 'app/models/dorsale/billing_machine/quotation.rb', line 88

def balance
  self.total_including_taxes
end

#update_totalsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/dorsale/billing_machine/quotation.rb', line 57

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_rateObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/dorsale/billing_machine/quotation.rb', line 92

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



108
109
110
# File 'app/models/dorsale/billing_machine/quotation.rb', line 108

def vat_rate=(value)
  @vat_rate = value
end