Class: Dorsale::BillingMachine::Invoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Dorsale::BillingMachine::Invoice
- Defined in:
- app/models/dorsale/billing_machine/invoice.rb
Class Method Summary collapse
- .french_number(amount) ⇒ Object
- .to_csv(options = { :force_quotes => true, :col_sep => ";" }) ⇒ Object
Instance Method Summary collapse
- #apply_vat_rate_to_lines ⇒ Object
- #assign_default_values ⇒ Object
- #assign_tracking_id ⇒ Object
- #assign_unique_index ⇒ Object
-
#initialize(*args) ⇒ Invoice
constructor
A new instance of Invoice.
- #payment_status ⇒ Object
- #pdf ⇒ Object
- #t(*args) ⇒ Object
- #update_totals ⇒ Object
- #vat_rate ⇒ Object
- #vat_rate=(value) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Invoice
Returns a new instance of Invoice.
22 23 24 25 26 27 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 22 def initialize(*args) super assign_default_values self.due_date = 30.days.from_now if due_date.nil? self.date = Date.today if date.nil? end |
Class Method Details
.french_number(amount) ⇒ Object
192 193 194 195 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 192 def self.french_number amount extend ActionView::Helpers::NumberHelper number_with_precision(amount, :delimiter => '', :separator => ",", :precision => 2) end |
.to_csv(options = { :force_quotes => true, :col_sep => ";" }) ⇒ Object
148 149 150 151 152 153 154 155 156 157 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 186 187 188 189 190 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 148 def self.to_csv( = { :force_quotes => true, :col_sep => ";" }) CSV.generate() do |csv| column_names = [ "Date", "Numéro", "Objet", "Client", "Adresse 1", "Adresse 2", "Code postal", "Ville", "Pays", "Remise commerciale", "Montant HT", "Montant TVA", "Montant TTC", "Acompte", "Solde à payer" ] csv << column_names all.each do |invoice| csv << [ invoice.date, invoice.tracking_id, invoice.label, invoice.customer.try(:name), invoice.customer.try(:address).try(:street), invoice.customer.try(:address).try(:street_bis), invoice.customer.try(:address).try(:zip), invoice.customer.try(:address).try(:city), invoice.customer.try(:address).try(:country), french_number(invoice.commercial_discount), french_number(invoice.total_excluding_taxes), french_number(invoice.vat_amount), french_number(invoice.total_including_taxes), french_number(invoice.advance), french_number(invoice.balance) ] end end end |
Instance Method Details
#apply_vat_rate_to_lines ⇒ Object
104 105 106 107 108 109 110 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 104 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
43 44 45 46 47 48 49 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 43 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
39 40 41 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 39 def assign_tracking_id self.tracking_id = date.year.to_s + "-" + unique_index.to_s.rjust(2, "0") end |
#assign_unique_index ⇒ Object
33 34 35 36 37 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 33 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
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 112 def payment_status if paid? return :paid elsif due_date == nil return :on_alert elsif Date.today >= due_date + 15 return :on_alert elsif Date.today > due_date return :late else return :pending end end |
#pdf ⇒ Object
76 77 78 79 80 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 76 def pdf pdf = ::Dorsale::BillingMachine.invoice_pdf_model.new(self) pdf.build pdf end |
#t(*args) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 126 def t(*args) return super if args.any? if balance && balance < 0 super(:credit_note) else self.class.t end end |
#update_totals ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 53 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 82 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
98 99 100 |
# File 'app/models/dorsale/billing_machine/invoice.rb', line 98 def vat_rate=(value) @vat_rate = value end |