Class: Dorsale::BillingMachine::Quotation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Dorsale::BillingMachine::Quotation
- Defined in:
- app/models/dorsale/billing_machine/quotation.rb
Constant Summary collapse
- STATES =
%w(pending accepted refused canceled)
Instance Method Summary collapse
- #apply_vat_rate_to_lines ⇒ Object
- #assign_default_values ⇒ Object
- #assign_tracking_id ⇒ Object
- #assign_unique_index ⇒ Object
- #balance ⇒ Object
- #create_copy! ⇒ Object
-
#initialize ⇒ Quotation
constructor
A new instance of Quotation.
- #pdf ⇒ Object
- #to_new_invoice ⇒ Object
- #update_totals ⇒ Object
- #vat_rate ⇒ Object
- #vat_rate=(value) ⇒ Object
Constructor Details
Instance Method Details
#apply_vat_rate_to_lines ⇒ Object
119 120 121 122 123 124 125 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 119 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
53 54 55 56 57 58 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 53 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
49 50 51 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 49 def assign_tracking_id self.tracking_id = date.year.to_s + "-" + unique_index.to_s.rjust(2, "0") end |
#assign_unique_index ⇒ Object
43 44 45 46 47 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 43 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
93 94 95 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 93 def balance self.total_including_taxes end |
#create_copy! ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 133 def create_copy! new_quotation = self.dup self.lines.each do |line| new_quotation.lines << line.dup end new_quotation.unique_index = nil new_quotation.created_at = nil new_quotation.updated_at = nil new_quotation.date = Date.today new_quotation.state = Quotation::STATES.first new_quotation.save! self..each do || = .dup .attachable = new_quotation .file = File.open(.file.path) .save! end new_quotation end |
#pdf ⇒ Object
127 128 129 130 131 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 127 def pdf pdf = ::Dorsale::BillingMachine.quotation_pdf_model.new(self) pdf.build pdf end |
#to_new_invoice ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 158 def to_new_invoice new_invoice = Dorsale::BillingMachine::Invoice.new self.attributes.each do |k, v| next if k.to_s == "id" next if k.to_s.match /index|tracking|_at/ if new_invoice.respond_to?("#{k}=") new_invoice.public_send("#{k}=", v) end end self.lines.each do |line| new_line = new_invoice.lines.new line.attributes.each do |k, v| next if k.to_s == "id" next if k.to_s.match /index|tracking|_at/ if new_line.respond_to?("#{k}=") new_line.public_send("#{k}=", v) end end end new_invoice.date = Date.today new_invoice end |
#update_totals ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 62 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 97 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
113 114 115 |
# File 'app/models/dorsale/billing_machine/quotation.rb', line 113 def vat_rate=(value) @vat_rate = value end |