Class: Spree::Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Order
- Defined in:
- app/models/spree/order.rb
Instance Attribute Summary collapse
-
#use_billing ⇒ Object
Returns the value of attribute use_billing.
Class Method Summary collapse
-
.register_update_hook(hook) ⇒ Object
Use this method in other gems that wish to register their own custom logic that should be called after Order#updat.
Instance Method Summary collapse
- #add_variant(variant, quantity = 1) ⇒ Object
- #allow_cancel? ⇒ Boolean
- #allow_resume? ⇒ Boolean
-
#amount ⇒ Object
For compatiblity with Calculator::PriceSack.
- #available_payment_methods ⇒ Object
-
#available_shipping_methods(display_on = nil) ⇒ Object
Helper methods for checkout steps.
-
#backordered? ⇒ Boolean
Indicates whether there are any backordered InventoryUnits associated with the Order.
- #billing_firstname ⇒ Object
- #billing_lastname ⇒ Object
-
#checkout_allowed? ⇒ Boolean
Indicates whether or not the user is allowed to proceed to checkout.
- #clone_billing_address ⇒ Object
- #completed? ⇒ Boolean
- #contains?(variant) ⇒ Boolean
-
#create_shipment! ⇒ Object
Creates a new shipment (adjustment is created by shipment model).
-
#create_tax_charge! ⇒ Object
Creates new tax charges if there are any applicable rates.
- #creditcards ⇒ Object
-
#exclude_tax? ⇒ Boolean
Indicates whether tax should be backed out of the price calcualtions in cases where prices include tax but the customer is not required to pay taxes in that case.
-
#finalize! ⇒ Object
Finalizes an in progress order after checkout is complete.
-
#generate_order_number ⇒ Object
FIXME refactor this method and implement validation using validates_* utilities.
- #insufficient_stock_lines ⇒ Object
-
#ip_address ⇒ Object
delegate :ip_address, :to => :checkout.
-
#item_count ⇒ Object
Indicates the number of items in the order.
- #name ⇒ Object
- #outstanding_balance ⇒ Object
- #outstanding_balance? ⇒ Boolean
- #payment ⇒ Object
- #payment_method ⇒ Object
-
#payment_required? ⇒ Boolean
Is this a free order in which case the payment step should be skipped.
-
#price_adjustment_totals ⇒ Object
Array of totals grouped by Adjustment#label.
-
#price_adjustments ⇒ Object
Array of adjustments that are inclusive in the variant price.
- #process_payments! ⇒ Object
- #products ⇒ Object
- #quantity_of(variant) ⇒ Object
- #rate_hash ⇒ Object
- #restore_state ⇒ Object
- #ship_total ⇒ Object
-
#shipment ⇒ Object
convenience method since many stores will not allow user to create multiple shipments.
- #tax_total ⇒ Object
-
#tax_zone ⇒ Object
Returns the relevant zone (if any) to be used for taxation purposes.
- #to_param ⇒ Object
-
#update! ⇒ Object
This is a multi-purpose method for processing logic related to changes in the Order.
Instance Attribute Details
#use_billing ⇒ Object
Returns the value of attribute use_billing.
235 236 237 |
# File 'app/models/spree/order.rb', line 235 def use_billing @use_billing end |
Class Method Details
.register_update_hook(hook) ⇒ Object
Use this method in other gems that wish to register their own custom logic that should be called after Order#updat
55 56 57 |
# File 'app/models/spree/order.rb', line 55 def self.register_update_hook(hook) self.update_hooks.add(hook) end |
Instance Method Details
#add_variant(variant, quantity = 1) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'app/models/spree/order.rb', line 257 def add_variant(variant, quantity = 1) current_item = contains?(variant) if current_item current_item.quantity += quantity current_item.save else current_item = LineItem.new(:quantity => quantity) current_item.variant = variant current_item.price = variant.price self.line_items << current_item end # populate line_items attributes for additional_fields entries # that have populate => [:line_item] Variant.additional_fields.select { |f| !f[:populate].nil? && f[:populate].include?(:line_item) }.each do |field| value = '' if field[:only].nil? || field[:only].include?(:variant) value = variant.send(field[:name].gsub(' ', '_').downcase) elsif field[:only].include?(:product) value = variant.product.send(field[:name].gsub(' ', '_').downcase) end current_item.update_attribute(field[:name].gsub(' ', '_').downcase, value) end current_item end |
#allow_cancel? ⇒ Boolean
246 247 248 249 |
# File 'app/models/spree/order.rb', line 246 def allow_cancel? return false unless completed? and state != 'canceled' %w{ready backorder pending}.include? shipment_state end |
#allow_resume? ⇒ Boolean
251 252 253 254 255 |
# File 'app/models/spree/order.rb', line 251 def allow_resume? # we shouldn't allow resume for legacy orders b/c we lack the information necessary to restore to a previous state return false if state_events.empty? || state_events.last.previous_state.nil? true end |
#amount ⇒ Object
For compatiblity with Calculator::PriceSack
60 61 62 |
# File 'app/models/spree/order.rb', line 60 def amount line_items.map(&:amount).sum end |
#available_payment_methods ⇒ Object
389 390 391 |
# File 'app/models/spree/order.rb', line 389 def available_payment_methods(display_on = nil) PaymentMethod.all(display_on) end |
#available_shipping_methods(display_on = nil) ⇒ Object
Helper methods for checkout steps
384 385 386 387 |
# File 'app/models/spree/order.rb', line 384 def available_shipping_methods(display_on = nil) return [] unless ship_address ShippingMethod.all_available(self, display_on) end |
#backordered? ⇒ Boolean
Indicates whether there are any backordered InventoryUnits associated with the Order.
144 145 146 147 |
# File 'app/models/spree/order.rb', line 144 def backordered? return false unless Spree::Config[:track_inventory_levels] inventory_units.backorder.present? end |
#billing_firstname ⇒ Object
420 421 422 |
# File 'app/models/spree/order.rb', line 420 def billing_firstname bill_address.try(:firstname) end |
#billing_lastname ⇒ Object
424 425 426 |
# File 'app/models/spree/order.rb', line 424 def billing_lastname bill_address.try(:lastname) end |
#checkout_allowed? ⇒ Boolean
Indicates whether or not the user is allowed to proceed to checkout. Currently this is implemented as a check for whether or not there is at least one LineItem in the Order. Feel free to override this logic in your own application if you require additional steps before allowing a checkout.
75 76 77 |
# File 'app/models/spree/order.rb', line 75 def checkout_allowed? line_items.count > 0 end |
#clone_billing_address ⇒ Object
237 238 239 240 241 242 243 244 |
# File 'app/models/spree/order.rb', line 237 def clone_billing_address if bill_address and self.ship_address.nil? self.ship_address = bill_address.clone else self.ship_address.attributes = bill_address.attributes.except('id', 'updated_at', 'created_at') end true end |
#completed? ⇒ Boolean
68 69 70 |
# File 'app/models/spree/order.rb', line 68 def completed? !! completed_at end |
#contains?(variant) ⇒ Boolean
301 302 303 |
# File 'app/models/spree/order.rb', line 301 def contains?(variant) line_items.detect { |line_item| line_item.variant_id == variant.id } end |
#create_shipment! ⇒ Object
Creates a new shipment (adjustment is created by shipment model)
329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'app/models/spree/order.rb', line 329 def create_shipment! shipping_method(true) if shipment.present? shipment.update_attributes!(:shipping_method => shipping_method) else self.shipments << Shipment.create!(:order => self, :shipping_method => shipping_method, :address => self.ship_address, :inventory_units => self.inventory_units) end end |
#create_tax_charge! ⇒ Object
Creates new tax charges if there are any applicable rates. If prices already include taxes then price adjustments are created instead.
320 321 322 323 324 325 326 |
# File 'app/models/spree/order.rb', line 320 def create_tax_charge! # destroy any previous adjustments (eveything is recalculated from scratch) adjustments.tax.each { |e| e.destroy } price_adjustments.each { |p| p.destroy } TaxRate.match(self).each { |rate| rate.adjust(self) } end |
#creditcards ⇒ Object
356 357 358 359 |
# File 'app/models/spree/order.rb', line 356 def creditcards creditcard_ids = payments.from_creditcard.map(&:source_id).uniq Creditcard.scoped(:conditions => { :id => creditcard_ids }) end |
#exclude_tax? ⇒ Boolean
Indicates whether tax should be backed out of the price calcualtions in cases where prices include tax but the customer is not required to pay taxes in that case.
158 159 160 161 |
# File 'app/models/spree/order.rb', line 158 def exclude_tax? return false unless Spree::Config[:prices_inc_tax] return tax_zone != Zone.default_tax end |
#finalize! ⇒ Object
Finalizes an in progress order after checkout is complete. Called after transition to complete state when payments will have been processed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'app/models/spree/order.rb', line 367 def finalize! update_attribute(:completed_at, Time.now) InventoryUnit.assign_opening_inventory(self) # lock any optional adjustments (coupon promotions, etc.) adjustments.optional.each { |adjustment| adjustment.update_attribute('locked', true) } OrderMailer.confirm_email(self).deliver self.state_events.create({ :previous_state => 'cart', :next_state => 'complete', :name => 'order' , :user_id => (User.respond_to?(:current) && User.current.try(:id)) || self.user_id }) end |
#generate_order_number ⇒ Object
FIXME refactor this method and implement validation using validates_* utilities
286 287 288 289 290 291 292 293 294 |
# File 'app/models/spree/order.rb', line 286 def generate_order_number record = true while record random = "R#{Array.new(9){rand(9)}.join}" record = self.class.where(:number => random).first end self.number = random if self.number.blank? self.number end |
#insufficient_stock_lines ⇒ Object
432 433 434 |
# File 'app/models/spree/order.rb', line 432 def insufficient_stock_lines line_items.select &:insufficient_stock? end |
#ip_address ⇒ Object
delegate :ip_address, :to => :checkout
38 39 40 |
# File 'app/models/spree/order.rb', line 38 def ip_address '192.168.1.100' end |
#item_count ⇒ Object
Indicates the number of items in the order
85 86 87 |
# File 'app/models/spree/order.rb', line 85 def item_count line_items.map(&:quantity).sum end |
#name ⇒ Object
350 351 352 353 354 |
# File 'app/models/spree/order.rb', line 350 def name if (address = bill_address || ship_address) "#{address.firstname} #{address.lastname}" end end |
#outstanding_balance ⇒ Object
342 343 344 |
# File 'app/models/spree/order.rb', line 342 def outstanding_balance total - payment_total end |
#outstanding_balance? ⇒ Boolean
346 347 348 |
# File 'app/models/spree/order.rb', line 346 def outstanding_balance? self.outstanding_balance != 0 end |
#payment ⇒ Object
404 405 406 |
# File 'app/models/spree/order.rb', line 404 def payment payments.first end |
#payment_method ⇒ Object
412 413 414 415 416 417 418 |
# File 'app/models/spree/order.rb', line 412 def payment_method if payment and payment.payment_method payment.payment_method else available_payment_methods.first end end |
#payment_required? ⇒ Boolean
Is this a free order in which case the payment step should be skipped
80 81 82 |
# File 'app/models/spree/order.rb', line 80 def payment_required? total.to_f > 0.0 end |
#price_adjustment_totals ⇒ Object
Array of totals grouped by Adjustment#label. Useful for displaying price adjustments on an invoice. For example, you can display tax breakout for cases where tax is included in price.
177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/models/spree/order.rb', line 177 def price_adjustment_totals totals = {} price_adjustments.each do |adjustment| label = adjustment.label totals[label] ||= 0 totals[label] = totals[label] + adjustment.amount end totals end |
#price_adjustments ⇒ Object
Array of adjustments that are inclusive in the variant price. Useful for when prices include tax (ex. VAT) and you need to record the tax amount separately.
165 166 167 168 169 170 171 172 173 |
# File 'app/models/spree/order.rb', line 165 def price_adjustments adjustments = [] line_items.each do |line_item| adjustments.concat line_item.adjustments end adjustments end |
#process_payments! ⇒ Object
361 362 363 |
# File 'app/models/spree/order.rb', line 361 def process_payments! ret = payments.each(&:process!) end |
#products ⇒ Object
428 429 430 |
# File 'app/models/spree/order.rb', line 428 def products line_items.map { |li| li.variant.product } end |
#quantity_of(variant) ⇒ Object
305 306 307 308 |
# File 'app/models/spree/order.rb', line 305 def quantity_of(variant) line_item = line_items.find { |line_item| line_item.variant_id == variant.id } line_item ? line_item.quantity : 0 end |
#rate_hash ⇒ Object
393 394 395 396 397 398 399 400 401 402 |
# File 'app/models/spree/order.rb', line 393 def rate_hash @rate_hash ||= available_shipping_methods(:front_end).collect do |ship_method| next unless cost = ship_method.calculator.compute(self) { :id => ship_method.id, :shipping_method => ship_method, :name => ship_method.name, :cost => cost } end.compact.sort_by { |r| r[:cost] } end |
#restore_state ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/models/spree/order.rb', line 221 def restore_state # pop the resume event so we can see what the event before that was state_events.pop if state_events.last.name == 'resume' update_attribute('state', state_events.last.previous_state) if paid? raise 'do something with inventory' #Spree::InventoryUnit.assign_opening_inventory(self) if inventory_units.empty? #shipment.inventory_units = inventory_units #shipment.ready! end end |
#ship_total ⇒ Object
310 311 312 |
# File 'app/models/spree/order.rb', line 310 def ship_total adjustments.shipping.map(&:amount).sum end |
#shipment ⇒ Object
convenience method since many stores will not allow user to create multiple shipments
297 298 299 |
# File 'app/models/spree/order.rb', line 297 def shipment @shipment ||= shipments.last end |
#tax_total ⇒ Object
314 315 316 |
# File 'app/models/spree/order.rb', line 314 def tax_total adjustments.tax.map(&:amount).sum end |
#tax_zone ⇒ Object
Returns the relevant zone (if any) to be used for taxation purposes. Uses default tax zone unless there is a specific match
151 152 153 154 |
# File 'app/models/spree/order.rb', line 151 def tax_zone zone_address = Spree::Config[:tax_using_ship_address] ? ship_address : bill_address Zone.match(zone_address) || Zone.default_tax end |
#to_param ⇒ Object
64 65 66 |
# File 'app/models/spree/order.rb', line 64 def to_param number.to_s.to_url.upcase end |
#update! ⇒ Object
This is a multi-purpose method for processing logic related to changes in the Order. It is meant to be called from various observers so that the Order is aware of changes that affect totals and other values stored in the Order. This method should never do anything to the Order that results in a save call on the object (otherwise you will end up in an infinite recursion as the associations try to save and then in turn try to call update!
again.)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'app/models/spree/order.rb', line 193 def update! update_totals update_payment_state # give each of the shipments a chance to update themselves shipments.each { |shipment| shipment.update!(self) }#(&:update!) update_shipment_state update_adjustments # update totals a second time in case updated adjustments have an effect on the total update_totals update_attributes_without_callbacks({ :payment_state => payment_state, :shipment_state => shipment_state, :item_total => item_total, :adjustment_total => adjustment_total, :payment_total => payment_total, :total => total }) #ensure checkout payment always matches order total if payment and payment.checkout? and payment.amount != total payment.update_attributes_without_callbacks(:amount => total) end update_hooks.each { |hook| self.send hook } end |