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



75
76
77
# File 'app/models/effective/order_item.rb', line 75

def amount_owing
  total
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



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

def price=(value)
  raise 'expected price to be an Integer representing the number of cents.' unless value.kind_of?(Integer)
  super
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



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

def qb_item_name
  raise('expected Effective Quickbooks gem') unless defined?(EffectiveQbSync) || defined?(EffectiveQbOnline)
  (qb_order_item || build_qb_order_item(name: purchasable&.qb_item_name)).name
end

#quantityObject



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

def quantity
  self[:quantity] || 1
end

#subtotalObject



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

def subtotal
  price * quantity
end

#taxObject



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

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

#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



79
80
81
82
83
# File 'app/models/effective/order_item.rb', line 79

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