Class: Caboose::Order

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

Constant Summary collapse

STATUS_CART =
'cart'
STATUS_PENDING =
'pending'
STATUS_CANCELED =
'canceled'
STATUS_SHIPPED =
'shipped'
STATUS_TESTING =
'testing'

Instance Method Summary collapse

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/caboose/order.rb', line 107

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

#calculateObject



123
124
125
126
127
128
129
130
# File 'app/models/caboose/order.rb', line 123

def calculate
  self.update_column(:subtotal , self.calculate_subtotal )
  self.update_column(:tax      , self.calculate_tax      )
  self.update_column(:shipping , self.calculate_shipping )
  self.update_column(:handling , self.calculate_handling )
  self.update_column(:discount , self.calculate_discount )
  self.update_column(:total    , self.calculate_total    )
end

#calculate_discountObject



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

def calculate_discount      
  return 0.0 if self.discounts.nil? || self.discounts.count == 0
  x = 0.0
  self.discounts.each{ |d| x = x + d.amount }
  x = x + self.custom_discount if self.custom_discount
  return x
end

#calculate_handlingObject



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

def calculate_handling
  return 0.0 if self.site.nil? || self.site.store_config.nil?      
  self.subtotal * self.site.store_config.handling_percentage.to_f
end

#calculate_shippingObject



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

def calculate_shipping      
  return 0.0 if self.order_packages.nil? || self.order_packages.count == 0
  x = 0.0
  self.order_packages.each{ |op| x = x + op.total }
  return x
end

#calculate_subtotalObject



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

def calculate_subtotal
  return 0.0 if self.line_items.empty?
  x = 0.0      
  self.line_items.each{ |li| x = x + li.variant.price }
  return x
end

#calculate_taxObject



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

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

#calculate_totalObject



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

def calculate_total
  return (self.subtotal + self.tax + self.shipping + self.handling) - self.discount
end

#captureObject



111
112
113
# File 'app/models/caboose/order.rb', line 111

def capture
  PaymentProcessor.capture(self)
end

#decrement_quantitiesObject



79
80
81
82
83
84
85
86
87
# File 'app/models/caboose/order.rb', line 79

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

#has_empty_shipping_methods?Boolean

Returns:

  • (Boolean)


187
188
189
190
191
192
193
194
# File 'app/models/caboose/order.rb', line 187

def has_empty_shipping_methods?
  return true if self.order_packages.nil?
  return true if self.order_packages.count == 0
  self.order_packages.each do |op|
    return true if op.shipping_method_id.nil?
  end
  return false
end

#increment_quantitiesObject



89
90
91
92
93
94
95
96
97
# File 'app/models/caboose/order.rb', line 89

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

#item_countObject



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

def item_count
  count = 0
  self.line_items.each{ |li| count = count + li.quantity } if self.line_items
  return count
end

#packagesObject



75
76
77
# File 'app/models/caboose/order.rb', line 75

def packages
  self.order_packages
end

#refunedObject



115
116
117
# File 'app/models/caboose/order.rb', line 115

def refuned
  PaymentProcessor.refund(self)
end

#resend_confirmationObject



99
100
101
# File 'app/models/caboose/order.rb', line 99

def resend_confirmation
  OrdersMailer.configure_for_site(self.site_id).customer_new_order(self).deliver
end

#shipping_and_handlingObject



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

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

#take_gift_card_fundsObject



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

def take_gift_card_funds
  return if self.discounts.nil? || self.discounts.count == 0
  self.discounts.each do |d|        
    gc = d.gift_card
    gc.balance = gc.balance - d.amount
    gc.save
  end
end

#test?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/caboose/order.rb', line 103

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

#voidObject



119
120
121
# File 'app/models/caboose/order.rb', line 119

def void
  PaymentProcessor.void(self)
end