Class: CabooseStore::Order

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

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object

Methods



94
95
96
97
98
99
100
# File 'app/models/caboose_store/order.rb', line 94

def as_json(options={})
  self.attributes.merge({
    :line_items => self.line_items,
    :shipping_address => self.shipping_address,
    :billing_address => self.billing_address
  })
end

#authorized?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/caboose_store/order.rb', line 130

def authorized?
  self.financial_status == 'authorized'
end

#calculateObject



154
155
156
157
158
159
160
# File 'app/models/caboose_store/order.rb', line 154

def calculate
  self.update_column(:subtotal, (self.calculate_subtotal * 100).ceil / 100.00)
  self.update_column(:tax, (self.calculate_tax * 100).ceil / 100.00)
  self.update_column(:shipping, (self.calculate_shipping * 100).ceil / 100.00)
  self.update_column(:handling, (self.calculate_handling * 100).ceil / 100.00)
  self.update_column(:total, (self.calculate_total * 100).ceil / 100.00)
end

#calculate_handlingObject



177
178
179
180
# File 'app/models/caboose_store/order.rb', line 177

def calculate_handling
  return 0 if !CabooseStore::handling_percentage
  self.shipping * CabooseStore::handling_percentage
end

#calculate_shippingObject



172
173
174
175
# File 'app/models/caboose_store/order.rb', line 172

def calculate_shipping
  return 0 if !self.shipping_address || !self.shipping_method_code
  ShippingCalculator.rate(self)[:total_price]
end

#calculate_subtotalObject



162
163
164
165
# File 'app/models/caboose_store/order.rb', line 162

def calculate_subtotal
  return 0 if self.line_items.empty?
  self.line_items.collect { |line_item| line_item.price }.inject { |sum, price| sum + price }
end

#calculate_taxObject



167
168
169
170
# File 'app/models/caboose_store/order.rb', line 167

def calculate_tax
  return 0 if !self.shipping_address
  self.subtotal * TaxCalculator.tax_rate(self.shipping_address)
end

#calculate_totalObject



182
183
184
# File 'app/models/caboose_store/order.rb', line 182

def calculate_total
  [self.subtotal, self.tax, self.shipping, self.handling].compact.inject { |sum, price| sum + price }
end

#captureObject



134
135
136
# File 'app/models/caboose_store/order.rb', line 134

def capture
  PaymentProcessor.capture(self)
end

#decrement_quantitiesObject



102
103
104
105
106
107
108
109
110
# File 'app/models/caboose_store/order.rb', line 102

def decrement_quantities
  return false if self.decremented
  
  self.line_items.each do |line_item|
    line_item.variant.update_attribute(:quantity, line_item.variant.quantity_in_stock - line_item.quantity)
  end
  
  self.update_attribute(:decremented, true)
end

#increment_quantitiesObject



112
113
114
115
116
117
118
119
120
# File 'app/models/caboose_store/order.rb', line 112

def increment_quantities
  return false if !self.decremented
  
  self.line_items.each do |line_item|
    line_item.variant.update_attribute(:quantity, line_item.variant.quantity_in_stock - line_item.quantity)
  end
  
  self.update_attribute(:decremented, false)
end

#line_item_added(line_item) ⇒ Object



146
147
148
# File 'app/models/caboose_store/order.rb', line 146

def line_item_added(line_item)
  self.calculate
end

#line_item_removed(line_item) ⇒ Object



150
151
152
# File 'app/models/caboose_store/order.rb', line 150

def line_item_removed(line_item)
  self.calculate
end

#refunedObject



138
139
140
# File 'app/models/caboose_store/order.rb', line 138

def refuned
  PaymentProcessor.refund(self)
end

#resend_confirmationObject



122
123
124
# File 'app/models/caboose_store/order.rb', line 122

def resend_confirmation
  OrdersMailer.customer_new_order(self).deliver
end

#shipping_and_handlingObject



186
187
188
# File 'app/models/caboose_store/order.rb', line 186

def shipping_and_handling
  (self.shipping ? self.shipping : 0.0) + (self.handling ? self.handling : 0.0)      
end

#test?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'app/models/caboose_store/order.rb', line 126

def test?
  self.status == 'testing'
end

#voidObject



142
143
144
# File 'app/models/caboose_store/order.rb', line 142

def void
  PaymentProcessor.void(self)
end