Class: Effective::OrderItem

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

Overview

An Order Item

Instance Method Summary collapse

Instance Method Details

#amount_owingObject



87
88
89
# File 'app/models/effective/order_item.rb', line 87

def amount_owing
  total
end

#archived?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/effective/order_item.rb', line 113

def archived?
  purchasable.try(:archived?) == true && price.to_i == 0
end

#assign_purchasable_attributesObject

This method is called in a before_validation in order.assign_order_values()



39
40
41
42
43
44
45
# File 'app/models/effective/order_item.rb', line 39

def assign_purchasable_attributes
  return unless purchasable.present?

  self.name ||= purchasable.purchasable_name
  self.price ||= purchasable.price
  self.tax_exempt = purchasable.tax_exempt if self.tax_exempt.nil?
end

#build_purchasable(atts = {}) ⇒ Object



53
54
55
# File 'app/models/effective/order_item.rb', line 53

def build_purchasable(atts = {})
  (self.purchasable ||= Effective::Product.new).tap { |purchasable| purchasable.assign_attributes(atts) }
end

#price=(value) ⇒ Object



97
98
99
100
# File 'app/models/effective/order_item.rb', line 97

def price=(value)
  raise 'expected price to be an Integer representing the number of cents.' unless value.kind_of?(Integer)
  super
end

#price_to_fObject



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

def price_to_f
  ((price|| 0) / 100.0).to_f
end

#purchased_download_urlObject



57
58
59
# File 'app/models/effective/order_item.rb', line 57

def purchased_download_url
  purchasable&.purchased_download_url
end

#qb_item_nameObject

first or build



103
104
105
106
107
108
109
110
111
# File 'app/models/effective/order_item.rb', line 103

def qb_item_name
  raise('expected Effective Quickbooks gem') unless defined?(EffectiveQbSync) || defined?(EffectiveQbOnline)

  if defined?(EffectiveQbSync)
    (qb_order_item || build_qb_order_item(name: purchasable.try(:qb_item_name))).name
  else
    purchasable.try(:qb_item_name)
  end
end

#quantityObject



73
74
75
# File 'app/models/effective/order_item.rb', line 73

def quantity
  self[:quantity] || 1
end

#subtotalObject



65
66
67
# File 'app/models/effective/order_item.rb', line 65

def subtotal
  price * quantity
end

#subtotal_to_fObject



69
70
71
# File 'app/models/effective/order_item.rb', line 69

def subtotal_to_f
  ((subtotal || 0) / 100.0).to_f
end

#taxObject



77
78
79
80
81
# File 'app/models/effective/order_item.rb', line 77

def tax
  return 0 if tax_exempt?
  raise 'parent Effective::Order must have a tax_rate to compute order item tax' unless order.try(:tax_rate).present?
  (subtotal * order.tax_rate / 100.0).round(0).to_i
end

#tax_to_fObject



83
84
85
# File 'app/models/effective/order_item.rb', line 83

def tax_to_f
  ((tax || 0) / 100.0).to_f
end

#to_sObject



34
35
36
# File 'app/models/effective/order_item.rb', line 34

def to_s
  ((quantity || 0) > 1 ? "#{quantity}x #{name}" : name) || 'order item'
end

#totalObject



91
92
93
94
95
# File 'app/models/effective/order_item.rb', line 91

def total
  return subtotal if tax_exempt?
  raise 'parent Effective::Order must have a tax_rate to compute order item total' unless order.try(:tax_rate).present?
  subtotal + tax
end

#update_purchasable_attributesObject



47
48
49
50
51
# File 'app/models/effective/order_item.rb', line 47

def update_purchasable_attributes
  if purchasable.present? && !marked_for_destruction?
    assign_attributes(name: purchasable.purchasable_name, price: purchasable.price, tax_exempt: purchasable.tax_exempt)
  end
end