-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathorder.rb
867 lines (710 loc) · 27 KB
/
order.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
require 'spree/core/validators/email'
require 'spree/order/checkout'
require 'spree/order/number_generator'
module Spree
# The customers cart until completed, then acts as permanent record of the transaction.
#
# `Spree::Order` is the heart of the Solidus system, as it acts as the customer's
# cart as they shop. Once an order is complete, it serves as the
# permanent record of their purchase. It has many responsibilities:
#
# * Records and validates attributes like `total` and relationships like
# `Spree::LineItem` as an ActiveRecord model.
#
# * Implements a customizable state machine to manage the lifecycle of an order.
#
# * Implements business logic to provide a single interface for quesitons like
# `checkout_allowed?` or `payment_required?`.
#
# * Implements an interface for mutating the order with methods like
# `empty!` and `fulfill!`.
#
class Order < Spree::Base
ORDER_NUMBER_LENGTH = 9
ORDER_NUMBER_LETTERS = false
ORDER_NUMBER_PREFIX = 'R'
include Spree::Order::Checkout
include Spree::Order::Payments
class InsufficientStock < StandardError; end
class CannotRebuildShipments < StandardError; end
extend Spree::DisplayMoney
money_methods :outstanding_balance, :item_total, :adjustment_total,
:included_tax_total, :additional_tax_total, :tax_total,
:shipment_total, :total, :order_total_after_store_credit, :total_available_store_credit
alias :display_ship_total :display_shipment_total
checkout_flow do
go_to_state :address
go_to_state :delivery
go_to_state :payment, if: ->(order) { order.payment_required? }
go_to_state :confirm
end
self.whitelisted_ransackable_associations = %w[shipments user order_promotions promotions bill_address ship_address line_items]
self.whitelisted_ransackable_attributes = %w[completed_at created_at email number state payment_state shipment_state total store_id]
attr_reader :coupon_code
attr_accessor :temporary_address
attr_accessor :temporary_payment_source
alias_method :temporary_credit_card, :temporary_payment_source
alias_method :temporary_credit_card=, :temporary_payment_source=
deprecate temporary_credit_card: :temporary_payment_source, deprecator: Spree::Deprecation
deprecate :temporary_credit_card= => :temporary_payment_source=, deprecator: Spree::Deprecation
# Customer info
belongs_to :user, class_name: Spree::UserClassHandle.new
belongs_to :bill_address, foreign_key: :bill_address_id, class_name: 'Spree::Address'
alias_attribute :billing_address, :bill_address
belongs_to :ship_address, foreign_key: :ship_address_id, class_name: 'Spree::Address'
alias_attribute :shipping_address, :ship_address
alias_attribute :ship_total, :shipment_total
belongs_to :store, class_name: 'Spree::Store'
# Items
has_many :line_items, -> { order(:created_at, :id) }, dependent: :destroy, inverse_of: :order
has_many :variants, through: :line_items
has_many :products, through: :variants
# Shipping
has_many :inventory_units, inverse_of: :order
has_many :cartons, -> { distinct }, through: :inventory_units
has_many :shipments, dependent: :destroy, inverse_of: :order do
def states
pluck(:state).uniq
end
end
has_many :order_stock_locations, class_name: "Spree::OrderStockLocation"
has_many :stock_locations, through: :order_stock_locations
# Adjustments and promotions
has_many :adjustments, -> { order(:created_at) }, as: :adjustable, inverse_of: :adjustable, dependent: :destroy
has_many :line_item_adjustments, through: :line_items, source: :adjustments
has_many :shipment_adjustments, through: :shipments, source: :adjustments
has_many :all_adjustments,
class_name: 'Spree::Adjustment',
foreign_key: :order_id,
dependent: :destroy,
inverse_of: :order
has_many :order_promotions, class_name: 'Spree::OrderPromotion'
has_many :promotions, through: :order_promotions
# Payments
has_many :payments, dependent: :destroy, inverse_of: :order
# Returns
has_many :return_authorizations, dependent: :destroy, inverse_of: :order
has_many :reimbursements, inverse_of: :order
has_many :refunds, through: :payments
# Logging
has_many :state_changes, as: :stateful
belongs_to :created_by, class_name: Spree::UserClassHandle.new
belongs_to :approver, class_name: Spree::UserClassHandle.new
belongs_to :canceler, class_name: Spree::UserClassHandle.new
accepts_nested_attributes_for :line_items
accepts_nested_attributes_for :bill_address
accepts_nested_attributes_for :ship_address
accepts_nested_attributes_for :payments
accepts_nested_attributes_for :shipments
# Needs to happen before save_permalink is called
before_validation :associate_store
before_validation :set_currency
before_validation :generate_order_number, on: :create
before_validation :assign_billing_to_shipping_address, if: :use_billing?
attr_accessor :use_billing
before_create :create_token
before_create :link_by_email
validates :email, presence: true, if: :require_email
validates :email, email: true, allow_blank: true
validates :guest_token, presence: { allow_nil: true }
validates :number, presence: true, uniqueness: { allow_blank: true }
validates :store_id, presence: true
make_permalink field: :number
delegate :update_totals, :persist_totals, to: :updater
delegate :firstname, :lastname, to: :bill_address, prefix: true, allow_nil: true
alias_method :billing_firstname, :bill_address_firstname
alias_method :billing_lastname, :bill_address_lastname
class_attribute :update_hooks
self.update_hooks = Set.new
class_attribute :line_item_comparison_hooks
self.line_item_comparison_hooks = Set.new
scope :created_between, ->(start_date, end_date) { where(created_at: start_date..end_date) }
scope :completed_between, ->(start_date, end_date) { where(completed_at: start_date..end_date) }
scope :by_store, ->(store) { where(store_id: store.id) }
# shows completed orders first, by their completed_at date, then uncompleted orders by their created_at
scope :reverse_chronological, -> { order('spree_orders.completed_at IS NULL', completed_at: :desc, created_at: :desc) }
def self.by_customer(customer)
joins(:user).where("#{Spree.user_class.table_name}.email" => customer)
end
def self.by_state(state)
where(state: state)
end
def self.complete
where.not(completed_at: nil)
end
def self.incomplete
where(completed_at: nil)
end
# Use this method in other gems that wish to register their own custom logic
# that should be called after Order#update
def self.register_update_hook(hook)
update_hooks.add(hook)
end
# 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.
def self.register_line_item_comparison_hook(hook)
line_item_comparison_hooks.add(hook)
end
# For compatiblity with Calculator::PriceSack
def amount
line_items.map(&:amount).sum
end
# Sum of all line item amounts pre-tax
def pre_tax_item_amount
line_items.to_a.sum(&:pre_tax_amount)
end
# Sum of all line item amounts after promotions, before added tax
def discounted_item_amount
line_items.to_a.sum(&:discounted_amount)
end
def currency
self[:currency] || Spree::Config[:currency]
end
def shipping_discount
shipment_adjustments.eligible.sum(:amount) * - 1
end
def to_param
number
end
def completed?
completed_at.present?
end
# 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.
def checkout_allowed?
line_items.count > 0
end
# Is this a free order in which case the payment step should be skipped
def payment_required?
total > 0
end
def confirmation_required?
true
end
deprecate :confirmation_required?, deprecator: Spree::Deprecation
def backordered?
shipments.any?(&:backordered?)
end
# Returns the relevant zone (if any) to be used for taxation purposes.
# Uses default tax zone unless there is a specific match
def tax_zone
Spree::Zone.match(tax_address) || Spree::Zone.default_tax
end
deprecate tax_zone: "Please use Spree::Order#tax_address instead.",
deprecator: Spree::Deprecation
# Returns the address for taxation based on configuration
def tax_address
if Spree::Config[:tax_using_ship_address]
ship_address
else
bill_address
end || store.default_cart_tax_location
end
def updater
@updater ||= Spree::OrderUpdater.new(self)
end
def update!
updater.update
end
def assign_billing_to_shipping_address
self.ship_address = bill_address if bill_address
true
end
def allow_cancel?
return false unless completed? && state != 'canceled'
shipment_state.nil? || %w{ready backorder pending}.include?(shipment_state)
end
def all_inventory_units_returned?
inventory_units.all?(&:returned?)
end
def contents
@contents ||= Spree::OrderContents.new(self)
end
def shipping
@shipping ||= Spree::OrderShipping.new(self)
end
def cancellations
@cancellations ||= Spree::OrderCancellations.new(self)
end
# Associates the specified user with the order.
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 created_by.blank?
if persisted?
# immediately persist the changes we just made, but don't use save since we might have an invalid address associated
self.class.unscoped.where(id: id).update_all(attrs_to_set)
end
assign_attributes(attrs_to_set)
end
def generate_order_number(options = nil)
if options
Spree::Deprecation.warn \
"Passing options to Order#generate_order_number is deprecated. " \
"Please add your own instance of the order number generator " \
"with your options (#{options.inspect}) and store it as " \
"Spree::Config.order_number_generator in your stores config."
end
self.number ||= Spree::Config.order_number_generator.generate
end
def shipped_shipments
shipments.shipped
end
def contains?(variant, options = {})
find_line_item_by_variant(variant, options).present?
end
def quantity_of(variant, options = {})
line_item = find_line_item_by_variant(variant, options)
line_item ? line_item.quantity : 0
end
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
# 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[:product_customizations]={...}
#
# and would provide:
#
# def product_customizations_match
def line_item_options_match(line_item, options)
return true unless options
line_item_comparison_hooks.all? { |hook|
send(hook, line_item, options)
}
end
# Creates new tax charges if there are any applicable rates. If prices already
# include taxes then price adjustments are created instead.
# @deprecated This now happens during #update!
def create_tax_charge!
Spree::Config.tax_adjuster_class.new(self).adjust!
end
deprecate create_tax_charge!: :update!, deprecator: Spree::Deprecation
def reimbursement_total
reimbursements.sum(:total)
end
def outstanding_balance
# If reimbursement has happened add it back to total to prevent balance_due payment state
# See: https://github.com/spree/spree/issues/6229
if state == 'canceled'
-1 * payment_total
else
total - reimbursement_total - payment_total
end
end
def outstanding_balance?
outstanding_balance != 0
end
def refund_total
payments.flat_map(&:refunds).sum(&:amount)
end
def name
if (address = bill_address || ship_address)
"#{address.firstname} #{address.lastname}"
end
end
def can_ship?
complete? || resumed? || awaiting_return? || returned?
end
def credit_cards
credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
Spree::CreditCard.where(id: credit_card_ids)
end
def valid_credit_cards
credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
Spree::CreditCard.where(id: credit_card_ids)
end
# Finalizes an in progress order after checkout is complete.
# Called after transition to complete state when payments will have been processed
def finalize!
# lock all adjustments (coupon promotions, etc.)
all_adjustments.each(&:finalize!)
# update payment and shipment(s) states, and save
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
def fulfill!
shipments.each { |shipment| shipment.update!(self) if shipment.persisted? }
updater.update_shipment_state
save!
end
def deliver_order_confirmation_email
Spree::OrderMailer.confirm_email(self).deliver_later
update_column(:confirmation_delivered, true)
end
# Helper methods for checkout steps
def paid?
%w(paid credit_owed).include?(payment_state)
end
def available_payment_methods
@available_payment_methods ||= Spree::PaymentMethod
.active
.available_to_store(store)
.available_to_users
.order(:position)
end
def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
##
# Check to see if any line item variants are soft, deleted.
# If so add error and restart checkout.
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
def merge!(*args)
Spree::Config.order_merger_class.new(self).merge!(*args)
end
def empty!
line_items.destroy_all
adjustments.destroy_all
shipments.destroy_all
order_promotions.destroy_all
update!
end
alias_method :has_step?, :has_checkout_step?
deprecate has_step?: :has_checkout_step?, deprecator: Spree::Deprecation
def state_changed(name)
state = "#{name}_state"
if persisted?
old_state = send("#{state}_was")
new_state = send(state)
unless old_state == new_state
state_changes.create(
previous_state: old_state,
next_state: new_state,
name: name,
user_id: user_id
)
end
end
end
deprecate :state_changed, deprecator: Spree::Deprecation
def coupon_code=(code)
@coupon_code = begin
code.strip.downcase
rescue
nil
end
end
def can_add_coupon?
Spree::Promotion.order_activatable?(self)
end
def shipped?
%w(partial shipped).include?(shipment_state)
end
def ensure_shipping_address
unless ship_address && ship_address.valid?
errors.add(:base, Spree.t(:ship_address_required)) && (return false)
end
end
def create_proposed_shipments
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
shipments.destroy_all
self.shipments = Spree::Config.stock.coordinator_class.new(self).shipments
end
end
def apply_free_shipping_promotions
Spree::PromotionHandler::FreeShipping.new(self).activate
update!
end
# 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
def ensure_updated_shipments
if !completed? && shipments.all?(&:pending?)
shipments.destroy_all
update_column(:shipment_total, 0)
restart_checkout_flow
end
end
def restart_checkout_flow
return if state == 'cart'
update_columns(
state: 'cart',
updated_at: Time.current
)
next! if line_items.size > 0
end
def refresh_shipment_rates
shipments.map(&:refresh_rates)
end
def shipping_eq_billing_address?
bill_address == ship_address
end
def set_shipments_cost
shipments.each(&:update_amounts)
update!
end
deprecate set_shipments_cost: :update!, deprecator: Spree::Deprecation
def is_risky?
payments.risky.count > 0
end
def canceled_by(user)
transaction do
cancel!
update_columns(
canceler_id: user.id,
canceled_at: Time.current
)
end
end
def approved?
!!approved_at
end
def can_approve?
!approved?
end
def quantity
line_items.sum(:quantity)
end
def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.offset_payment.exists? # how old versions of spree stored refunds
end
def token
Spree::Deprecation.warn("Spree::Order#token is DEPRECATED, please use #guest_token instead.", caller)
guest_token
end
def tax_total
additional_tax_total + included_tax_total
end
def add_store_credit_payments
return if user.nil?
return if payments.store_credits.checkout.empty? && user.total_available_store_credit.zero?
payments.store_credits.checkout.each(&:invalidate!)
# this can happen when multiple payments are present, auto_capture is
# turned off, and one of the payments fails when the user tries to
# complete the order, which sends the order back to the 'payment' state.
authorized_total = payments.pending.sum(:amount)
remaining_total = outstanding_balance - authorized_total
if 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")) && (return false)
end
end
def covered_by_store_credit?
return false unless user
user.total_available_store_credit >= total
end
alias_method :covered_by_store_credit, :covered_by_store_credit?
def total_available_store_credit
return 0.0 unless user
user.total_available_store_credit
end
def order_total_after_store_credit
total - total_applicable_store_credit
end
def total_applicable_store_credit
if can_complete? || complete?
payments.store_credits.valid.sum(:amount)
else
[total, (user.try(:total_available_store_credit) || 0.0)].min
end
end
def display_total_applicable_store_credit
Spree::Money.new(-total_applicable_store_credit, { currency: currency })
end
def display_store_credit_remaining_after_capture
Spree::Money.new(total_available_store_credit - total_applicable_store_credit, { currency: currency })
end
def bill_address_attributes=(attributes)
self.bill_address = Spree::Address.immutable_merge(bill_address, attributes)
end
def ship_address_attributes=(attributes)
self.ship_address = Spree::Address.immutable_merge(ship_address, attributes)
end
# Assigns a default bill_address and ship_address to the order based on the
# associated user's bill_address and ship_address.
# @note This doesn't persist the change bill_address or ship_address
def assign_default_user_addresses
if user
bill_address = (user.bill_address || user.default_address)
ship_address = (user.ship_address || user.default_address)
# this is one of 2 places still using User#bill_address
self.bill_address ||= bill_address if bill_address.try!(:valid?)
# Skip setting ship address if order doesn't have a delivery checkout step
# to avoid triggering validations on shipping address
self.ship_address ||= ship_address if ship_address.try!(:valid?) && checkout_steps.include?("delivery")
end
end
alias_method :assign_default_user_addresses!, :assign_default_user_addresses
deprecate assign_default_user_addresses!: :assign_default_user_addresses, deprecator: Spree::Deprecation
alias_method :assign_default_addresses!, :assign_default_user_addresses
deprecate assign_default_addresses!: :assign_default_user_addresses, deprecator: Spree::Deprecation
def persist_user_address!
if !temporary_address && user && user.respond_to?(:persist_order_address) && bill_address_id
user.persist_order_address(self)
end
end
def add_payment_sources_to_wallet
Spree::Config.
add_payment_sources_to_wallet_class.new(self).
add_to_wallet
end
alias_method :persist_user_credit_card, :add_payment_sources_to_wallet
deprecate persist_user_credit_card: :add_payment_sources_to_wallet, deprecator: Spree::Deprecation
def add_default_payment_from_wallet
builder = Spree::Config.default_payment_builder_class.new(self)
if payment = builder.build
payments << payment
if bill_address.nil?
# this is one of 2 places still using User#bill_address
self.bill_address = payment.source.try(:address) ||
user.bill_address
end
end
end
alias_method :assign_default_credit_card, :add_default_payment_from_wallet
deprecate assign_default_credit_card: :add_default_payment_from_wallet, deprecator: Spree::Deprecation
private
def process_payments_before_complete
return if !payment_required?
if payments.valid.empty?
errors.add(:base, Spree.t(:no_payment_found))
return false
end
if process_payments!
true
else
saved_errors = errors[:base]
payment_failed!
saved_errors.each { |error| errors.add(:base, error) }
false
end
end
# In case a existing credit card is provided it needs to build the payment
# attributes from scratch so we can set the amount. example payload:
#
# {
# "order": {
# "existing_card": "2"
# }
# }
#
def update_params_payment_source
if @updating_params[:order] && (@updating_params[:order][:payments_attributes] || @updating_params[:order][:existing_card])
@updating_params[:order][:payments_attributes] ||= [{}]
@updating_params[:order][:payments_attributes].first[:amount] = total
end
end
def associate_store
self.store ||= Spree::Store.default
end
def link_by_email
self.email = user.email if user
end
# Determine if email is required (we don't want validation errors before we hit the checkout)
def require_email
true unless new_record? || ['cart', 'address'].include?(state)
end
def ensure_inventory_units
if has_checkout_step?("delivery")
inventory_validator = Spree::Stock::InventoryValidator.new
errors = line_items.map { |line_item| inventory_validator.validate(line_item) }.compact
raise InsufficientStock if errors.any?
end
end
def ensure_promotions_eligible
adjustment_changed = all_adjustments.eligible.promotion.any? do |adjustment|
!adjustment.calculate_eligibility
end
if adjustment_changed
restart_checkout_flow
update!
errors.add(:base, Spree.t(:promotion_total_changed_before_complete))
end
errors.empty?
end
def validate_line_item_availability
availability_validator = Spree::Stock::AvailabilityValidator.new
raise InsufficientStock unless line_items.all? { |line_item| availability_validator.validate(line_item) }
end
def ensure_line_items_present
unless line_items.present?
errors.add(:base, Spree.t(:there_are_no_items_for_this_order)) && (return false)
end
end
def ensure_available_shipping_rates
if shipments.empty? || shipments.any? { |shipment| shipment.shipping_rates.blank? }
# After this point, order redirects back to 'address' state and asks user to pick a proper address
# Therefore, shipments are not necessary at this point.
shipments.destroy_all
errors.add(:base, Spree.t(:items_cannot_be_shipped)) && (return false)
end
end
def after_cancel
shipments.each(&:cancel!)
payments.completed.each(&:cancel!)
payments.store_credits.pending.each(&:void_transaction!)
send_cancel_email
update!
end
def send_cancel_email
Spree::OrderMailer.cancel_email(self).deliver_later
end
def after_resume
shipments.each(&:resume!)
end
def use_billing?
use_billing.in?([true, 'true', '1'])
end
def set_currency
self.currency = Spree::Config[:currency] if self[:currency].nil?
end
def create_token
self.guest_token ||= loop do
random_token = SecureRandom.urlsafe_base64(nil, false)
break random_token unless self.class.exists?(guest_token: random_token)
end
end
end
end