Class: Effective::OrderItem

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

Instance Method Summary collapse

Instance Method Details

#price=(value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'app/models/effective/order_item.rb', line 54

def price=(value)
  if value.kind_of?(Integer)
    super
  elsif value.kind_of?(String) && !value.include?('.') # Looks like an integer
    super
  else # Could be Float, BigDecimal, or String like 9.99
    ActiveSupport::Deprecation.warn('order_item.price= was passed a non-integer. Expecting an Integer representing the number of cents.  Continuing with (price * 100.0).round(0).to_i conversion') unless EffectiveOrders.silence_deprecation_warnings
    super((value.to_f * 100.0).to_i)
  end
end

#sellerObject

This is going to return an Effective::Customer object that matches the purchasable.user And is the Customer representing who is selling the product This is really only used for StripeConnect



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

def seller
  @seller ||= Effective::Customer.for_user(purchasable.try(:seller))
end

#stripe_connect_application_feeObject



72
73
74
75
76
77
78
# File 'app/models/effective/order_item.rb', line 72

def stripe_connect_application_fee
  @stripe_connect_application_fee ||= (
    self.instance_exec(self, &EffectiveOrders.stripe_connect_application_fee_method).to_i.tap do |fee|
      raise ArgumentError.new("expected EffectiveOrders.stripe_connect_application_fee_method to return a value between 0 and the order_item total (#{self.total}).  Received #{fee}.") if (fee > total || fee < 0)
    end
  )
end

#subtotalObject



38
39
40
# File 'app/models/effective/order_item.rb', line 38

def subtotal
  price * quantity
end

#taxObject



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

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 #{title}" : title
end

#totalObject



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

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