Class: Spree::Order
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Spree::Order
show all
- Extended by:
- DisplayMoney
- Includes:
- Checkout, Payments
- Defined in:
- app/models/spree/order.rb,
app/models/spree/order/checkout.rb,
app/models/spree/order/payments.rb
Defined Under Namespace
Modules: Checkout, Payments
Classes: CannotRebuildShipments, InsufficientStock
Constant Summary
collapse
- ORDER_NUMBER_LENGTH =
9
- ORDER_NUMBER_LETTERS =
false
- ORDER_NUMBER_PREFIX =
'R'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
money_methods
Methods included from Checkout
included
Methods inherited from Base
#initialize_preference_defaults, page, preference
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#coupon_code ⇒ Object
Returns the value of attribute coupon_code.
33
34
35
|
# File 'app/models/spree/order.rb', line 33
def coupon_code
@coupon_code
end
|
#temporary_address ⇒ Object
Returns the value of attribute temporary_address.
34
35
36
|
# File 'app/models/spree/order.rb', line 34
def temporary_address
@temporary_address
end
|
#temporary_credit_card ⇒ Object
Returns the value of attribute temporary_credit_card.
34
35
36
|
# File 'app/models/spree/order.rb', line 34
def temporary_credit_card
@temporary_credit_card
end
|
#use_billing ⇒ Object
Returns the value of attribute use_billing.
90
91
92
|
# File 'app/models/spree/order.rb', line 90
def use_billing
@use_billing
end
|
Class Method Details
.by_customer(customer) ⇒ Object
130
131
132
|
# File 'app/models/spree/order.rb', line 130
def self.by_customer(customer)
joins(:user).where("#{Spree.user_class.table_name}.email" => customer)
end
|
.by_number(number) ⇒ Object
115
116
117
|
# File 'app/models/spree/order.rb', line 115
def by_number(number)
where(number: number)
end
|
.by_state(state) ⇒ Object
134
135
136
|
# File 'app/models/spree/order.rb', line 134
def self.by_state(state)
where(state: state)
end
|
.complete ⇒ Object
138
139
140
|
# File 'app/models/spree/order.rb', line 138
def self.complete
where.not(completed_at: nil)
end
|
.incomplete ⇒ Object
142
143
144
|
# File 'app/models/spree/order.rb', line 142
def self.incomplete
where(completed_at: nil)
end
|
.register_line_item_comparison_hook(hook) ⇒ Object
Use this method in other gems that wish to register their own custom logic that should be called when determining if two line items are equal.
154
155
156
|
# File 'app/models/spree/order.rb', line 154
def self.register_line_item_comparison_hook(hook)
self.line_item_comparison_hooks.add(hook)
end
|
.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#update
148
149
150
|
# File 'app/models/spree/order.rb', line 148
def self.register_update_hook(hook)
self.update_hooks.add(hook)
end
|
Instance Method Details
#add_store_credit_payments ⇒ Object
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
# File 'app/models/spree/order.rb', line 611
def add_store_credit_payments
payments.store_credits.checkout.each(&:invalidate!)
authorized_total = payments.pending.sum(:amount)
remaining_total = outstanding_balance - authorized_total
if user && user.store_credits.any?
payment_method = Spree::PaymentMethod::StoreCredit.first
user.store_credits.order_by_priority.each do |credit|
break if remaining_total.zero?
next if credit.amount_remaining.zero?
amount_to_take = [credit.amount_remaining, remaining_total].min
payments.create!(source: credit,
payment_method: payment_method,
amount: amount_to_take,
state: 'checkout',
response_code: credit.generate_authorization_code)
remaining_total -= amount_to_take
end
end
other_payments = payments.checkout.not_store_credits
if remaining_total.zero?
other_payments.each(&:invalidate!)
elsif other_payments.size == 1
other_payments.first.update_attributes!(amount: remaining_total)
end
payments.reset
if payments.where(state: %w(checkout pending)).sum(:amount) != total
errors.add(:base, Spree.t("store_credit.errors.unable_to_fund")) and return false
end
end
|
#all_inventory_units_returned? ⇒ Boolean
235
236
237
|
# File 'app/models/spree/order.rb', line 235
def all_inventory_units_returned?
inventory_units.all? { |inventory_unit| inventory_unit.returned? }
end
|
#allow_cancel? ⇒ Boolean
230
231
232
233
|
# File 'app/models/spree/order.rb', line 230
def allow_cancel?
return false unless completed? and state != 'canceled'
shipment_state.nil? || %w{ready backorder pending}.include?(shipment_state)
end
|
#amount ⇒ Object
For compatiblity with Calculator::PriceSack
159
160
161
|
# File 'app/models/spree/order.rb', line 159
def amount
line_items.inject(0.0) { |sum, li| sum + li.amount }
end
|
#approved? ⇒ Boolean
561
562
563
|
# File 'app/models/spree/order.rb', line 561
def approved?
!!self.approved_at
end
|
#assign_billing_to_shipping_address ⇒ Object
225
226
227
228
|
# File 'app/models/spree/order.rb', line 225
def assign_billing_to_shipping_address
self.ship_address = bill_address if bill_address
true
end
|
#associate_user!(user, override_email = true) ⇒ Object
Associates the specified user with the order.
252
253
254
255
256
257
258
259
260
261
262
263
264
|
# File 'app/models/spree/order.rb', line 252
def associate_user!(user, override_email = true)
self.user = user
attrs_to_set = { user_id: user.try(:id) }
attrs_to_set[:email] = user.try(:email) if override_email
attrs_to_set[:created_by_id] = user.try(:id) if self.created_by.blank?
if persisted?
self.class.unscoped.where(id: id).update_all(attrs_to_set)
end
assign_attributes(attrs_to_set)
end
|
#available_payment_methods ⇒ Object
411
412
413
414
415
416
|
# File 'app/models/spree/order.rb', line 411
def available_payment_methods
@available_payment_methods ||= (
PaymentMethod.available(:front_end, store: store) +
PaymentMethod.available(:both, store: store)
).uniq
end
|
#backordered? ⇒ Boolean
202
203
204
|
# File 'app/models/spree/order.rb', line 202
def backordered?
shipments.any?(&:backordered?)
end
|
#can_add_coupon? ⇒ Boolean
#can_approve? ⇒ Boolean
565
566
567
|
# File 'app/models/spree/order.rb', line 565
def can_approve?
!approved?
end
|
#can_ship? ⇒ Boolean
359
360
361
|
# File 'app/models/spree/order.rb', line 359
def can_ship?
self.complete? || self.resumed? || self.awaiting_return? || self.returned?
end
|
#canceled_by(user) ⇒ Object
551
552
553
554
555
556
557
558
559
|
# File 'app/models/spree/order.rb', line 551
def canceled_by(user)
self.transaction do
cancel!
self.update_columns(
canceler_id: user.id,
canceled_at: Time.current,
)
end
end
|
#cancellations ⇒ Object
247
248
249
|
# File 'app/models/spree/order.rb', line 247
def cancellations
@cancellations ||= Spree::OrderCancellations.new(self)
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.
188
189
190
|
# File 'app/models/spree/order.rb', line 188
def checkout_allowed?
line_items.count > 0
end
|
#completed? ⇒ Boolean
180
181
182
|
# File 'app/models/spree/order.rb', line 180
def completed?
completed_at.present?
end
|
#confirmation_required? ⇒ Boolean
197
198
199
|
# File 'app/models/spree/order.rb', line 197
def confirmation_required?
true
end
|
#contains?(variant, options = {}) ⇒ Boolean
291
292
293
|
# File 'app/models/spree/order.rb', line 291
def contains?(variant, options = {})
find_line_item_by_variant(variant, options).present?
end
|
#contents ⇒ Object
239
240
241
|
# File 'app/models/spree/order.rb', line 239
def contents
@contents ||= Spree::OrderContents.new(self)
end
|
#covered_by_store_credit? ⇒ Boolean
Also known as:
covered_by_store_credit
653
654
655
656
|
# File 'app/models/spree/order.rb', line 653
def covered_by_store_credit?
return false unless user
user.total_available_store_credit >= total
end
|
#create_proposed_shipments ⇒ Object
488
489
490
491
492
493
494
495
496
497
498
499
500
|
# File 'app/models/spree/order.rb', line 488
def create_proposed_shipments
return self.shipments if unreturned_exchange?
if completed?
raise CannotRebuildShipments.new(Spree.t(:cannot_rebuild_shipments_order_completed))
elsif shipments.any? { |s| !s.pending? }
raise CannotRebuildShipments.new(Spree.t(:cannot_rebuild_shipments_shipments_not_pending))
else
adjustments.shipping.destroy_all
shipments.destroy_all
self.shipments = Spree::Stock::Coordinator.new(self).shipments
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.
326
327
328
329
330
331
|
# File 'app/models/spree/order.rb', line 326
def create_tax_charge!
order_tax_zone = self.tax_zone
Spree::TaxRate.adjust(order_tax_zone, line_items)
Spree::TaxRate.adjust(order_tax_zone, shipments) if shipments.any?
end
|
#credit_cards ⇒ Object
363
364
365
366
|
# File 'app/models/spree/order.rb', line 363
def credit_cards
credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
#currency ⇒ Object
168
169
170
|
# File 'app/models/spree/order.rb', line 168
def currency
self[:currency] || Spree::Config[:currency]
end
|
#deliver_order_confirmation_email ⇒ Object
401
402
403
404
|
# File 'app/models/spree/order.rb', line 401
def deliver_order_confirmation_email
OrderMailer.confirm_email(self).deliver_later
update_column(:confirmation_delivered, true)
end
|
#display_store_credit_remaining_after_capture ⇒ Object
680
681
682
|
# File 'app/models/spree/order.rb', line 680
def display_store_credit_remaining_after_capture
Spree::Money.new(total_available_store_credit - total_applicable_store_credit, { currency: currency })
end
|
#display_total_applicable_store_credit ⇒ Object
676
677
678
|
# File 'app/models/spree/order.rb', line 676
def display_total_applicable_store_credit
Spree::Money.new(-total_applicable_store_credit, { currency: currency })
end
|
#empty! ⇒ Object
439
440
441
442
443
444
445
446
447
|
# File 'app/models/spree/order.rb', line 439
def empty!
line_items.destroy_all
updater.update_item_count
adjustments.destroy_all
shipments.destroy_all
update_totals
persist_totals
end
|
#ensure_line_item_variants_are_not_deleted ⇒ Object
Check to see if any line item variants are soft, deleted. If so add error and restart checkout.
425
426
427
428
429
430
431
432
433
|
# File 'app/models/spree/order.rb', line 425
def ensure_line_item_variants_are_not_deleted
if line_items.any? { |li| li.variant.paranoia_destroyed? }
errors.add(:base, Spree.t(:deleted_variants_present))
restart_checkout_flow
false
else
true
end
end
|
#ensure_shipping_address ⇒ Object
482
483
484
485
486
|
# File 'app/models/spree/order.rb', line 482
def ensure_shipping_address
unless ship_address && ship_address.valid?
errors.add(:base, Spree.t(:ship_address_required)) and return false
end
end
|
#ensure_updated_shipments ⇒ Object
Clean shipments and make order back to address state
At some point the might need to force the order to transition from address to delivery again so that proper updated shipments are created. e.g. customer goes back from payment step and changes order items
514
515
516
517
518
519
520
521
|
# File 'app/models/spree/order.rb', line 514
def ensure_updated_shipments
if !completed? && shipments.all?(&:pending?)
self.shipments.destroy_all
self.update_column(:shipment_total, 0)
restart_checkout_flow
end
end
|
#finalize! ⇒ Object
Finalizes an in progress order after checkout is complete. Called after transition to complete state when payments will have been processed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# File 'app/models/spree/order.rb', line 375
def finalize!
all_adjustments.each(&:finalize!)
updater.update_payment_state
shipments.each do |shipment|
shipment.update!(self)
shipment.finalize!
end
updater.update_shipment_state
save!
updater.run_hooks
touch :completed_at
deliver_order_confirmation_email unless confirmation_delivered?
end
|
#find_line_item_by_variant(variant, options = {}) ⇒ Object
300
301
302
303
304
305
|
# File 'app/models/spree/order.rb', line 300
def find_line_item_by_variant(variant, options = {})
line_items.detect { |line_item|
line_item.variant_id == variant.id &&
line_item_options_match(line_item, options)
}
end
|
#fulfill! ⇒ Object
395
396
397
398
399
|
# File 'app/models/spree/order.rb', line 395
def fulfill!
shipments.each { |shipment| shipment.update!(self) if shipment.persisted? }
updater.update_shipment_state
save!
end
|
#fully_discounted? ⇒ Boolean
Also known as:
fully_discounted
Deprecated.
Do not use this method. Behaviour is unreliable.
589
590
591
|
# File 'app/models/spree/order.rb', line 589
def fully_discounted?
adjustment_total + line_items.map(&:final_amount).sum == 0.0
end
|
#generate_order_number(options = {}) ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'app/models/spree/order.rb', line 266
def generate_order_number(options = {})
options[:length] ||= ORDER_NUMBER_LENGTH
options[:letters] ||= ORDER_NUMBER_LETTERS
options[:prefix] ||= ORDER_NUMBER_PREFIX
possible = (0..9).to_a
possible += ('A'..'Z').to_a if options[:letters]
self.number ||= loop do
random = "#{options[:prefix]}#{(0...options[:length]).map { possible.sample }.join}"
if self.class.exists?(number: random)
options[:length] += 1 if self.class.count > (10 ** options[:length] / 2)
else
break random
end
end
end
|
578
579
580
581
|
# File 'app/models/spree/order.rb', line 578
def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.offset_payment.exists? end
|
#has_step?(step) ⇒ Boolean
449
450
451
|
# File 'app/models/spree/order.rb', line 449
def has_step?(step)
checkout_steps.include?(step)
end
|
#insufficient_stock_lines ⇒ Object
418
419
420
|
# File 'app/models/spree/order.rb', line 418
def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
|
#is_risky? ⇒ Boolean
547
548
549
|
# File 'app/models/spree/order.rb', line 547
def is_risky?
self.payments.risky.count > 0
end
|
#line_item_options_match(line_item, options) ⇒ Object
This method enables extensions to participate in the “Are these line items equal” decision.
When adding to cart, an extension would send something like: params=…
and would provide:
def product_customizations_match
316
317
318
319
320
321
322
|
# File 'app/models/spree/order.rb', line 316
def line_item_options_match(line_item, options)
return true unless options
self.line_item_comparison_hooks.all? { |hook|
self.send(hook, line_item, options)
}
end
|
#merge!(*args) ⇒ Object
435
436
437
|
# File 'app/models/spree/order.rb', line 435
def merge!(*args)
Spree::Config.order_merger_class.new(self).merge!(*args)
end
|
#name ⇒ Object
353
354
355
356
357
|
# File 'app/models/spree/order.rb', line 353
def name
if (address = bill_address || ship_address)
"#{address.firstname} #{address.lastname}"
end
end
|
#order_total_after_store_credit ⇒ Object
664
665
666
|
# File 'app/models/spree/order.rb', line 664
def order_total_after_store_credit
total - total_applicable_store_credit
end
|
#outstanding_balance ⇒ Object
333
334
335
336
337
338
339
340
341
342
343
|
# File 'app/models/spree/order.rb', line 333
def outstanding_balance
adjusted_payment_total = payment_total + refund_total
if state == 'canceled'
-1 * adjusted_payment_total
else
total - adjusted_payment_total
end
end
|
#outstanding_balance? ⇒ Boolean
345
346
347
|
# File 'app/models/spree/order.rb', line 345
def outstanding_balance?
self.outstanding_balance != 0
end
|
#paid? ⇒ Boolean
Helper methods for checkout steps
407
408
409
|
# File 'app/models/spree/order.rb', line 407
def paid?
%w(paid credit_owed).include?(payment_state)
end
|
#payment_required? ⇒ Boolean
Is this a free order in which case the payment step should be skipped
193
194
195
|
# File 'app/models/spree/order.rb', line 193
def payment_required?
total.to_f > 0.0
end
|
#payments_attributes=(attributes) ⇒ Object
684
685
686
687
|
# File 'app/models/spree/order.rb', line 684
def payments_attributes=(attributes)
validate_payments_attributes(attributes)
super(attributes)
end
|
#pre_tax_item_amount ⇒ Object
Sum of all line item amounts pre-tax
164
165
166
|
# File 'app/models/spree/order.rb', line 164
def pre_tax_item_amount
line_items.to_a.sum(&:pre_tax_amount)
end
|
#quantity ⇒ Object
574
575
576
|
# File 'app/models/spree/order.rb', line 574
def quantity
line_items.sum(:quantity)
end
|
#quantity_of(variant, options = {}) ⇒ Object
295
296
297
298
|
# File 'app/models/spree/order.rb', line 295
def quantity_of(variant, options = {})
line_item = find_line_item_by_variant(variant, options)
line_item ? line_item.quantity : 0
end
|
#refresh_shipment_rates ⇒ Object
533
534
535
|
# File 'app/models/spree/order.rb', line 533
def refresh_shipment_rates
shipments.map(&:refresh_rates)
end
|
#refund_total ⇒ Object
349
350
351
|
# File 'app/models/spree/order.rb', line 349
def refund_total
payments.flat_map(&:refunds).sum(&:amount)
end
|
#reload(options = nil) ⇒ Object
569
570
571
572
|
# File 'app/models/spree/order.rb', line 569
def reload(options=nil)
remove_instance_variable(:@tax_zone) if defined?(@tax_zone)
super
end
|
#restart_checkout_flow ⇒ Object
523
524
525
526
527
528
529
530
531
|
# File 'app/models/spree/order.rb', line 523
def restart_checkout_flow
return if self.state == 'cart'
self.update_columns(
state: 'cart',
updated_at: Time.current,
)
self.next! if self.line_items.size > 0
end
|
#set_shipments_cost ⇒ Object
541
542
543
544
545
|
# File 'app/models/spree/order.rb', line 541
def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
persist_totals
end
|
#shipped? ⇒ Boolean
478
479
480
|
# File 'app/models/spree/order.rb', line 478
def shipped?
%w(partial shipped).include?(shipment_state)
end
|
#shipped_shipments ⇒ Object
287
288
289
|
# File 'app/models/spree/order.rb', line 287
def shipped_shipments
shipments.shipped
end
|
#shipping ⇒ Object
243
244
245
|
# File 'app/models/spree/order.rb', line 243
def shipping
@shipping ||= Spree::OrderShipping.new(self)
end
|
#shipping_discount ⇒ Object
172
173
174
|
# File 'app/models/spree/order.rb', line 172
def shipping_discount
shipment_adjustments.eligible.sum(:amount) * - 1
end
|
#shipping_eq_billing_address? ⇒ Boolean
537
538
539
|
# File 'app/models/spree/order.rb', line 537
def shipping_eq_billing_address?
bill_address == ship_address
end
|
#state_changed(name) ⇒ Object
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# File 'app/models/spree/order.rb', line 453
def state_changed(name)
state = "#{name}_state"
if persisted?
old_state = self.send("#{state}_was")
new_state = self.send(state)
unless old_state == new_state
self.state_changes.create(
previous_state: old_state,
next_state: new_state,
name: name,
user_id: self.user_id
)
end
end
end
|
#tax_address ⇒ Object
Returns the address for taxation based on configuration
213
214
215
|
# File 'app/models/spree/order.rb', line 213
def tax_address
Spree::Config[:tax_using_ship_address] ? ship_address : bill_address
end
|
#tax_total ⇒ Object
607
608
609
|
# File 'app/models/spree/order.rb', line 607
def tax_total
additional_tax_total + included_tax_total
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
208
209
210
|
# File 'app/models/spree/order.rb', line 208
def tax_zone
@tax_zone ||= Zone.match(tax_address) || Zone.default_tax
end
|
#to_param ⇒ Object
176
177
178
|
# File 'app/models/spree/order.rb', line 176
def to_param
number.to_s.to_url.upcase
end
|
#token ⇒ Object
583
584
585
586
|
# File 'app/models/spree/order.rb', line 583
def token
ActiveSupport::Deprecation.warn("Spree::Order#token is DEPRECATED, please use #guest_token instead.", caller)
guest_token
end
|
#total_applicable_store_credit ⇒ Object
668
669
670
671
672
673
674
|
# File 'app/models/spree/order.rb', line 668
def total_applicable_store_credit
if confirm? || complete?
payments.store_credits.valid.sum(:amount)
else
[total, (user.try(:total_available_store_credit) || 0.0)].min
end
end
|
#total_available_store_credit ⇒ Object
659
660
661
662
|
# File 'app/models/spree/order.rb', line 659
def total_available_store_credit
return 0.0 unless user
user.total_available_store_credit
end
|
#unreturned_exchange? ⇒ Boolean
595
596
597
598
599
600
601
602
603
604
605
|
# File 'app/models/spree/order.rb', line 595
def unreturned_exchange?
shipment = self.shipments.first
shipment.present? ? (shipment.created_at < self.created_at - 1) : false
end
|
#update! ⇒ Object
221
222
223
|
# File 'app/models/spree/order.rb', line 221
def update!
updater.update
end
|
#updater ⇒ Object
217
218
219
|
# File 'app/models/spree/order.rb', line 217
def updater
@updater ||= OrderUpdater.new(self)
end
|
#valid_credit_cards ⇒ Object
368
369
370
371
|
# File 'app/models/spree/order.rb', line 368
def valid_credit_cards
credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
#validate_payments_attributes(attributes) ⇒ Object
689
690
691
692
693
694
695
696
697
698
699
|
# File 'app/models/spree/order.rb', line 689
def validate_payments_attributes(attributes)
attributes = Array.wrap(attributes)
payment_methods = Spree::PaymentMethod.where(id: available_payment_methods)
attributes.each do |payment_attributes|
payment_method_id = payment_attributes[:payment_method_id]
payment_methods.find(payment_method_id) if payment_method_id
end
end
|