Class: Caboose::Order

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

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object

Methods



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

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)


131
132
133
# File 'app/models/caboose/order.rb', line 131

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

#calculateObject



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

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



178
179
180
181
# File 'app/models/caboose/order.rb', line 178

def calculate_handling
  return 0 if !Caboose::store_handling_percentage
  self.shipping * Caboose::store_handling_percentage
end

#calculate_shippingObject



173
174
175
176
# File 'app/models/caboose/order.rb', line 173

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

#calculate_subtotalObject



163
164
165
166
# File 'app/models/caboose/order.rb', line 163

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



168
169
170
171
# File 'app/models/caboose/order.rb', line 168

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

#calculate_totalObject



183
184
185
# File 'app/models/caboose/order.rb', line 183

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

#captureObject



135
136
137
# File 'app/models/caboose/order.rb', line 135

def capture
  PaymentProcessor.capture(self)
end

#decrement_quantitiesObject



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

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



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

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



147
148
149
# File 'app/models/caboose/order.rb', line 147

def line_item_added(line_item)
  self.calculate
end

#line_item_removed(line_item) ⇒ Object



151
152
153
# File 'app/models/caboose/order.rb', line 151

def line_item_removed(line_item)
  self.calculate
end

#refunedObject



139
140
141
# File 'app/models/caboose/order.rb', line 139

def refuned
  PaymentProcessor.refund(self)
end

#resend_confirmationObject



123
124
125
# File 'app/models/caboose/order.rb', line 123

def resend_confirmation
  OrdersMailer.customer_new_order(self).deliver
end

#shipping_and_handlingObject



187
188
189
# File 'app/models/caboose/order.rb', line 187

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

#test?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/caboose/order.rb', line 127

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

#voidObject



143
144
145
# File 'app/models/caboose/order.rb', line 143

def void
  PaymentProcessor.void(self)
end