Class: InvoiceItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/invoice_item.rb

Overview

add_index :invoice_items, :invoice_id, :name => ‘invoice_id_idx’

add_index :invoice_items, :invoice_item_type_id, :name => 'invoice_item_type_id_idx'

Instance Method Summary collapse

Instance Method Details

#add_invoiced_record(record) ⇒ Object



82
83
84
# File 'app/models/invoice_item.rb', line 82

def add_invoiced_record(record)
  invoiced_records << InvoicedRecord.new(invoiceable_item: record)
end

#balanceObject



61
62
63
# File 'app/models/invoice_item.rb', line 61

def balance
  (self.total_amount - self.total_payments).round(2)
end

#calculate_tax(ctx = {}) ⇒ Object

calculates tax and save to sales_tax



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/invoice_item.rb', line 66

def calculate_tax(ctx={})
  tax = 0

  # see if anything is taxed
  if invoiced_records.collect { |item| item.taxed? }.include?(true)
    taxation = ErpOrders::Taxation.new

    tax += taxation.calculate_tax(self,
                                  ctx.merge({
                                                amount: (self.unit_price * (self.quantity || 1))
                                            }))
  end

  tax
end

#product_descriptionsObject



90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/invoice_item.rb', line 90

def product_descriptions
  # return an array of product descriptions for this invoice item
  descriptions = []
  invoiced_records.each do |invoiced_record|
    descriptions << invoiced_record.invoiceable_item.description
  end
  if descriptions.count == 0
    descriptions << "No Product Description"
  end
  descriptions
end

#sub_totalObject



51
52
53
54
55
56
57
58
59
# File 'app/models/invoice_item.rb', line 51

def sub_total
  _amount = self.amount

  if _amount.blank?
    _amount = (self.unit_price * self.quantity)
  end

  _amount.round(2)
end

#taxed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/invoice_item.rb', line 39

def taxed?
  self.taxed
end

#to_labelObject



102
103
104
# File 'app/models/invoice_item.rb', line 102

def to_label
  to_s
end

#to_sObject



86
87
88
# File 'app/models/invoice_item.rb', line 86

def to_s
  item_description
end

#total_amountObject



43
44
45
46
47
48
49
# File 'app/models/invoice_item.rb', line 43

def total_amount
  if taxed?
    (sub_total + (self.sales_tax || 0)).round(2)
  else
    sub_total
  end
end