Class: Spree::Order
- Inherits:
-
Object
show all
- Extended by:
- DisplayMoney
- Includes:
- MemoizedData, Metadata, MultiSearchable, NumberAsParam, NumberIdentifier, AddressBook, Checkout, CurrencyUpdater, Digital, Emails, GiftCard, Payments, StoreCredit, Webhooks, Security::Orders, SingleStoreResource, VendorConcern
- Defined in:
- app/models/spree/order.rb,
app/models/spree/order/emails.rb,
app/models/spree/order/digital.rb,
app/models/spree/order/checkout.rb,
app/models/spree/order/payments.rb,
app/models/spree/order/webhooks.rb,
app/models/spree/order/gift_card.rb,
app/models/spree/order/address_book.rb,
app/models/spree/order/store_credit.rb,
app/models/spree/order/currency_updater.rb
Defined Under Namespace
Modules: AddressBook, Checkout, CurrencyUpdater, Digital, Emails, GiftCard, Payments, StoreCredit, Webhooks
Constant Summary
collapse
- PAYMENT_STATES =
%w(balance_due credit_owed failed paid void)
- SHIPMENT_STATES =
%w(backorder canceled partial pending ready shipped)
- LINE_ITEM_REMOVABLE_STATES =
%w(cart address delivery payment confirm resumed)
- MEMOIZED_METHODS =
%w(tax_zone)
- MONEY_THRESHOLD =
100_000_000
- MONEY_VALIDATION =
{
presence: true,
numericality: {
greater_than: -MONEY_THRESHOLD,
less_than: MONEY_THRESHOLD,
allow_blank: true
},
format: { with: /\A-?\d+(?:\.\d{1,2})?\z/, allow_blank: true }
}.freeze
- POSITIVE_MONEY_VALIDATION =
MONEY_VALIDATION.deep_dup.tap do |validation|
validation.fetch(:numericality)[:greater_than_or_equal_to] = 0
end.freeze
- NEGATIVE_MONEY_VALIDATION =
MONEY_VALIDATION.deep_dup.tap do |validation|
validation.fetch(:numericality)[:less_than_or_equal_to] = 0
end.freeze
- ASSOCIATED_USER_ATTRIBUTES =
[:user_id, :email, :created_by_id, :bill_address_id, :ship_address_id]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
money_methods
Methods included from GiftCard
#apply_gift_card, #gift_card_total, #recalculate_gift_card, #redeem_gift_card, #remove_gift_card
Methods included from Webhooks
#send_order_canceled_webhook, #send_order_placed_webhook, #send_order_resumed_webhook
Methods included from Emails
#deliver_order_confirmation_email, #deliver_store_owner_order_notification_email, #deliver_store_owner_order_notification_email?, #send_cancel_email
#bill_address_attributes=, #bill_address_id=, #clone_billing_address, #clone_shipping_address, #ship_address_attributes=, #ship_address_id=
#add_store_credit_payments, #available_store_credits, #could_use_store_credit?, #covered_by_store_credit?, #display_order_total_after_store_credit, #display_store_credit_remaining_after_capture, #display_total_applicable_store_credit, #display_total_applied_store_credit, #display_total_available_store_credit, #display_total_minus_store_credits, #order_total_after_store_credit, #remove_store_credit_payments, #total_applicable_store_credit, #total_applied_store_credit, #total_available_store_credit, #total_minus_store_credits, #using_store_credit?
Methods included from Digital
#create_digital_links, #digital?, #digital_line_items, #digital_links, #some_digital?, #with_digital_assets?
#price_from_line_item, #update_line_item_currencies!, #update_line_item_price!
Methods included from Checkout
included
Instance Attribute Details
#coupon_code ⇒ Object
Returns the value of attribute coupon_code.
89
90
91
|
# File 'app/models/spree/order.rb', line 89
def coupon_code
@coupon_code
end
|
#temporary_address ⇒ Object
Returns the value of attribute temporary_address.
90
91
92
|
# File 'app/models/spree/order.rb', line 90
def temporary_address
@temporary_address
end
|
#temporary_credit_card ⇒ Object
Returns the value of attribute temporary_credit_card.
90
91
92
|
# File 'app/models/spree/order.rb', line 90
def temporary_credit_card
@temporary_credit_card
end
|
#use_billing ⇒ Object
Returns the value of attribute use_billing.
159
160
161
|
# File 'app/models/spree/order.rb', line 159
def use_billing
@use_billing
end
|
#use_shipping ⇒ Object
Returns the value of attribute use_shipping.
159
160
161
|
# File 'app/models/spree/order.rb', line 159
def use_shipping
@use_shipping
end
|
Class Method Details
.multi_search(query) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'app/models/spree/order.rb', line 212
def self.multi_search(query)
sanitized_query = sanitize_query_for_multi_search(query)
return none if query.blank?
query_pattern = "%#{sanitized_query}%"
conditions = []
conditions << arel_table[:number].lower.matches(query_pattern)
conditions << multi_search_condition(Spree::Address, :firstname, sanitized_query)
conditions << multi_search_condition(Spree::Address, :lastname, sanitized_query)
full_name = NameOfPerson::PersonName.full(sanitized_query)
if full_name.first.present? && full_name.last.present?
conditions << multi_search_condition(Spree::Address, :firstname, full_name.first)
conditions << multi_search_condition(Spree::Address, :lastname, full_name.last)
end
left_joins(:bill_address).where(email: sanitized_query).or(where(conditions.reduce(:or)))
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
236
237
238
|
# File 'app/models/spree/order.rb', line 236
def self.register_update_hook(hook)
update_hooks.add(hook)
end
|
Instance Method Details
#all_inventory_units_returned? ⇒ Boolean
374
375
376
|
# File 'app/models/spree/order.rb', line 374
def all_inventory_units_returned?
inventory_units.all?(&:returned?)
end
|
#all_line_items ⇒ Object
859
860
861
|
# File 'app/models/spree/order.rb', line 859
def all_line_items
line_items
end
|
#all_line_items_invalid? ⇒ Boolean
Checks if all line items cannot be shipped
670
671
672
|
# File 'app/models/spree/order.rb', line 670
def all_line_items_invalid?
line_items_without_shipping_rates.size == line_items.count
end
|
#allow_cancel? ⇒ Boolean
368
369
370
371
372
|
# File 'app/models/spree/order.rb', line 368
def allow_cancel?
return false if !completed? || canceled?
shipment_state.nil? || %w{ready backorder pending canceled}.include?(shipment_state)
end
|
#amount ⇒ Object
For compatibility with Calculator::PriceSack
241
242
243
|
# File 'app/models/spree/order.rb', line 241
def amount
line_items.inject(0.0) { |sum, li| sum + li.amount }
end
|
#analytics_subtotal ⇒ Float
Returns the subtotal used for analytics integrations It’s a sum of the item total and the promo total
258
259
260
|
# File 'app/models/spree/order.rb', line 258
def analytics_subtotal
(item_total + line_items.sum(:promo_total)).to_f
end
|
Applies user promotions when login after filling the cart
#approve! ⇒ Object
782
783
784
|
# File 'app/models/spree/order.rb', line 782
def approve!
update_column(:considered_risky, false)
end
|
#approved? ⇒ Boolean
757
758
759
|
# File 'app/models/spree/order.rb', line 757
def approved?
!!approved_at
end
|
#approved_by(user) ⇒ Object
747
748
749
750
751
752
753
754
755
|
# File 'app/models/spree/order.rb', line 747
def approved_by(user)
transaction do
approve!
update_columns(
approver_id: user.id,
approved_at: Time.current
)
end
end
|
#associate_user!(user, override_email = true) ⇒ Object
Associates the specified user with the order.
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
# File 'app/models/spree/order.rb', line 379
def associate_user!(user, override_email = true)
self.user = user
self.email = user.email if override_email
self.created_by ||= user if user.is_a?(Spree.admin_user_class)
self.bill_address ||= user.bill_address
self.ship_address ||= user.ship_address
changes = slice(*ASSOCIATED_USER_ATTRIBUTES)
ActiveRecord::Base.connected_to(role: :writing) do
self.class.unscoped.where(id: self).update_all(changes)
end
end
|
#available_payment_methods(store = nil) ⇒ Object
543
544
545
546
547
548
549
550
|
# File 'app/models/spree/order.rb', line 543
def available_payment_methods(store = nil)
Spree::Deprecation.warn('`Order#available_payment_methods` is deprecated and will be removed in Spree 6. Use `collect_frontend_payment_methods` instead.')
if store.present?
Spree::Deprecation.warn('The `store` parameter is deprecated and will be removed in Spree 5. Order is already associated with Store')
end
@available_payment_methods ||= collect_payment_methods(store)
end
|
#backordered? ⇒ Boolean
315
316
317
|
# File 'app/models/spree/order.rb', line 315
def backordered?
shipments.any?(&:backordered?)
end
|
#backordered_variants ⇒ Array<Spree::Variant>
Returns the backordered variants for the order
480
481
482
483
484
485
|
# File 'app/models/spree/order.rb', line 480
def backordered_variants
variants.
where(track_inventory: true).
joins(:stock_items, :product).
where(Spree::StockItem.table_name => { count_on_hand: ..0, backorderable: true })
end
|
#can_add_coupon? ⇒ Boolean
#can_approve? ⇒ Boolean
761
762
763
|
# File 'app/models/spree/order.rb', line 761
def can_approve?
!approved?
end
|
#can_be_deleted? ⇒ Boolean
770
771
772
|
# File 'app/models/spree/order.rb', line 770
def can_be_deleted?
!completed? && payments.completed.empty?
end
|
#can_be_destroyed? ⇒ Boolean
765
766
767
768
|
# File 'app/models/spree/order.rb', line 765
def can_be_destroyed?
Spree::Deprecation.warn('Spree::Order#can_be_destroyed? is deprecated and will be removed in the next major version. Use Spree::Order#can_be_deleted? instead.')
can_be_deleted?
end
|
#can_ship? ⇒ Boolean
487
488
489
|
# File 'app/models/spree/order.rb', line 487
def can_ship?
complete? || resumed? || awaiting_return? || returned?
end
|
#canceled_by(user, canceled_at = nil) ⇒ Object
735
736
737
738
739
740
741
742
743
744
745
|
# File 'app/models/spree/order.rb', line 735
def canceled_by(user, canceled_at = nil)
canceled_at ||= Time.current
transaction do
update_columns(
canceler_id: user.id,
canceled_at: canceled_at
)
cancel!
end
end
|
Returns item and whole order discount amount for Order without Shipment disccounts (eg. Free Shipping)
838
839
840
841
842
|
# File 'app/models/spree/order.rb', line 838
def cart_promo_total
all_adjustments.eligible.nonzero.promotion.
where.not(adjustable_type: 'Spree::Shipment').
sum(:amount)
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.
287
288
289
|
# File 'app/models/spree/order.rb', line 287
def checkout_allowed?
line_items.exists?
end
|
#collect_backend_payment_methods ⇒ Object
799
800
801
|
# File 'app/models/spree/order.rb', line 799
def collect_backend_payment_methods
store.payment_methods.active.available_on_back_end.select { |pm| pm.available_for_order?(self) }
end
|
#collect_frontend_payment_methods ⇒ Object
803
804
805
|
# File 'app/models/spree/order.rb', line 803
def collect_frontend_payment_methods
store.payment_methods.active.available_on_front_end.select { |pm| pm.available_for_order?(self) }
end
|
#completed? ⇒ Boolean
266
267
268
|
# File 'app/models/spree/order.rb', line 266
def completed?
completed_at.present?
end
|
#confirmation_required? ⇒ Boolean
If true, causes the confirmation step to happen during the checkout process
302
303
304
305
306
307
308
309
|
# File 'app/models/spree/order.rb', line 302
def confirmation_required?
Spree::Config[:always_include_confirm_step] ||
payments.valid.map(&:payment_method).compact.any?(&:payment_profiles_supported?) ||
confirm?
end
|
#consider_risk ⇒ Object
774
775
776
|
# File 'app/models/spree/order.rb', line 774
def consider_risk
considered_risky! if is_risky? && !approved?
end
|
#considered_risky! ⇒ Object
778
779
780
|
# File 'app/models/spree/order.rb', line 778
def considered_risky!
update_column(:considered_risky, true)
end
|
#create_proposed_shipments ⇒ Object
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
# File 'app/models/spree/order.rb', line 634
def create_proposed_shipments
all_adjustments.shipping.delete_all
shipment_ids = shipments.map(&:id)
StateChange.where(stateful_type: 'Spree::Shipment', stateful_id: shipment_ids).delete_all
ShippingRate.where(shipment_id: shipment_ids).delete_all
shipments.delete_all
inventory_units.on_hand_or_backordered.delete_all
self.shipments = Spree::Stock::Coordinator.new(self).shipments
end
|
#create_shipment_tax_charge! ⇒ Object
422
423
424
|
# File 'app/models/spree/order.rb', line 422
def create_shipment_tax_charge!
Spree::TaxRate.adjust(self, shipments) if shipments.any?
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.
417
418
419
420
|
# File 'app/models/spree/order.rb', line 417
def create_tax_charge!
Spree::TaxRate.adjust(self, line_items)
Spree::TaxRate.adjust(self, shipments) if shipments.any?
end
|
#credit_cards ⇒ Object
495
496
497
498
|
# File 'app/models/spree/order.rb', line 495
def credit_cards
credit_card_ids = payments.from_credit_card.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
#delivery_required? ⇒ Boolean
Does this order require a delivery (physical or digital).
292
293
294
|
# File 'app/models/spree/order.rb', line 292
def delivery_required?
true end
|
#disassociate_user! ⇒ Object
397
398
399
400
401
|
# File 'app/models/spree/order.rb', line 397
def disassociate_user!
nullified_attributes = ASSOCIATED_USER_ATTRIBUTES.index_with(nil)
update!(nullified_attributes)
end
|
#email_required? ⇒ Boolean
311
312
313
|
# File 'app/models/spree/order.rb', line 311
def email_required?
require_email
end
|
#empty! ⇒ Object
579
580
581
582
583
584
|
# File 'app/models/spree/order.rb', line 579
def empty!
raise Spree.t(:cannot_empty_completed_order) if completed?
result = Spree::Dependencies.cart_empty_service.constantize.call(order: self)
result.value
end
|
#ensure_line_item_variants_are_not_discontinued ⇒ Object
Check to see if any line item variants are discontinued. If so add error and restart checkout.
559
560
561
562
563
564
565
566
567
|
# File 'app/models/spree/order.rb', line 559
def ensure_line_item_variants_are_not_discontinued
if line_items.any? { |li| !li.variant || li.variant.discontinued? }
restart_checkout_flow
errors.add(:base, Spree.t(:discontinued_variants_present))
false
else
true
end
end
|
#ensure_line_items_are_in_stock ⇒ Object
569
570
571
572
573
574
575
576
577
|
# File 'app/models/spree/order.rb', line 569
def ensure_line_items_are_in_stock
if insufficient_stock_lines.present?
restart_checkout_flow
errors.add(:base, Spree.t(:insufficient_stock_lines_present))
false
else
true
end
end
|
#ensure_store_presence ⇒ Object
364
365
366
|
# File 'app/models/spree/order.rb', line 364
def ensure_store_presence
self.store ||= Spree::Store.default
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
691
692
693
694
695
696
697
|
# File 'app/models/spree/order.rb', line 691
def ensure_updated_shipments
if shipments.any? && !completed?
shipments.destroy_all
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
# File 'app/models/spree/order.rb', line 507
def finalize!
all_adjustments.each(&:close)
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?
deliver_store_owner_order_notification_email if deliver_store_owner_order_notification_email?
send_order_placed_webhook
consider_risk
end
|
#find_line_item_by_variant(variant, options = {}) ⇒ Object
408
409
410
411
412
413
|
# File 'app/models/spree/order.rb', line 408
def find_line_item_by_variant(variant, options = {})
line_items.detect do |line_item|
line_item.variant_id == variant.id &&
Spree::Dependencies.cart_compare_line_items_service.constantize.new.call(order: self, line_item: line_item, options: options).value
end
end
|
#fulfill! ⇒ Object
532
533
534
535
536
|
# File 'app/models/spree/order.rb', line 532
def fulfill!
shipments.each { |shipment| shipment.update!(self) if shipment.persisted? }
updater.update_shipment_state
save!
end
|
#full_name ⇒ Object
455
456
457
458
459
460
461
|
# File 'app/models/spree/order.rb', line 455
def full_name
@full_name ||= if user.present? && user.name.present?
user.name.full
else
billing_address&.full_name || email
end
end
|
#fully_discounted? ⇒ Boolean
Also known as:
fully_discounted
determines whether the inventory is fully discounted
Returns
812
813
814
|
# File 'app/models/spree/order.rb', line 812
def fully_discounted?
adjustment_total + line_items.map(&:final_amount).sum == 0.0
end
|
#fully_shipped? ⇒ Boolean
630
631
632
|
# File 'app/models/spree/order.rb', line 630
def fully_shipped?
shipments.shipped.size == shipments.size
end
|
#has_free_shipping? ⇒ Boolean
844
845
846
847
848
849
|
# File 'app/models/spree/order.rb', line 844
def has_free_shipping?
shipment_adjustments.
joins(:promotion_action).
where(spree_adjustments: { eligible: true, source_type: 'Spree::PromotionAction' },
spree_promotion_actions: { type: 'Spree::Promotion::Actions::FreeShipping' }).exists?
end
|
794
795
796
797
|
# File 'app/models/spree/order.rb', line 794
def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.offset_payment.exists? end
|
#has_step?(step) ⇒ Boolean
590
591
592
|
# File 'app/models/spree/order.rb', line 590
def has_step?(step)
checkout_steps.include?(step)
end
|
#insufficient_stock_lines ⇒ Object
552
553
554
|
# File 'app/models/spree/order.rb', line 552
def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
|
#is_risky? ⇒ Boolean
731
732
733
|
# File 'app/models/spree/order.rb', line 731
def is_risky?
!payments.risky.empty?
end
|
#line_items_without_shipping_rates ⇒ Array<Spree::LineItem>
Returns line items that have no shipping rates
661
662
663
664
665
|
# File 'app/models/spree/order.rb', line 661
def line_items_without_shipping_rates
@line_items_without_shipping_rates ||= shipments.map do |shipment|
shipment.manifest.map(&:line_item) if shipment.shipping_rates.blank?
end.flatten.compact
end
|
#log_state_changes(state_name:, old_state:, new_state:) ⇒ Object
605
606
607
608
609
610
611
612
|
# File 'app/models/spree/order.rb', line 605
def log_state_changes(state_name:, old_state:, new_state:)
state_changes.create(
previous_state: old_state,
next_state: new_state,
name: state_name,
user_id: user_id
)
end
|
#merger ⇒ Object
360
361
362
|
# File 'app/models/spree/order.rb', line 360
def merger
@merger ||= Spree::OrderMerger.new(self)
end
|
#name ⇒ Object
449
450
451
452
453
|
# File 'app/models/spree/order.rb', line 449
def name
if (address = bill_address || ship_address)
address.full_name
end
end
|
#order_refunded? ⇒ Boolean
270
271
272
|
# File 'app/models/spree/order.rb', line 270
def order_refunded?
(payment_state == 'void' && refunds.sum(:amount).positive?) || refunds.sum(:amount) == total
end
|
#outstanding_balance ⇒ Object
433
434
435
436
437
438
439
|
# File 'app/models/spree/order.rb', line 433
def outstanding_balance
if canceled?
-1 * payment_total
else
total - (payment_total + reimbursement_paid_total)
end
end
|
#outstanding_balance? ⇒ Boolean
445
446
447
|
# File 'app/models/spree/order.rb', line 445
def outstanding_balance?
outstanding_balance != 0
end
|
#paid? ⇒ Boolean
Helper methods for checkout steps
539
540
541
|
# File 'app/models/spree/order.rb', line 539
def paid?
payments.valid.completed.size == payments.valid.size && payments.valid.sum(:amount) >= total
end
|
#partially_refunded? ⇒ Boolean
274
275
276
277
278
279
280
281
|
# File 'app/models/spree/order.rb', line 274
def partially_refunded?
return false if refunds.empty?
amount_not_refunded = additional_tax_total.abs + total_applied_store_credit
refunds.sum(:amount) < total - amount_not_refunded
end
|
Returns the payment method for the order
466
467
468
|
# File 'app/models/spree/order.rb', line 466
def payment_method
payments.valid.not_store_credits.first&.payment_method
end
|
#payment_required? ⇒ Boolean
Is this a free order in which case the payment step should be skipped
297
298
299
|
# File 'app/models/spree/order.rb', line 297
def payment_required?
total.to_f > 0.0
end
|
Returns the payment source for the order
473
474
475
|
# File 'app/models/spree/order.rb', line 473
def payment_source
payments.valid.not_store_credits.first&.source
end
|
#pre_tax_item_amount ⇒ Object
Sum of all line item amounts pre-tax
246
247
248
|
# File 'app/models/spree/order.rb', line 246
def pre_tax_item_amount
line_items.sum(:pre_tax_amount)
end
|
#pre_tax_total ⇒ Object
Sum of all line item and shipment pre-tax
251
252
253
|
# File 'app/models/spree/order.rb', line 251
def pre_tax_total
pre_tax_item_amount + shipments.sum(:pre_tax_amount)
end
|
817
818
819
|
# File 'app/models/spree/order.rb', line 817
def promo_code
Spree::CouponCode.find_by(order: self, promotion: promotions).try(:code) || promotions.pluck(:code).compact.first
end
|
#quantity ⇒ Object
790
791
792
|
# File 'app/models/spree/order.rb', line 790
def quantity
line_items.sum(:quantity)
end
|
#quantity_of(variant, options = {}) ⇒ Object
403
404
405
406
|
# File 'app/models/spree/order.rb', line 403
def quantity_of(variant, options = {})
line_item = find_line_item_by_variant(variant, options)
line_item ? line_item.quantity : 0
end
|
#quick_checkout? ⇒ Boolean
Check if the shipping address is a quick checkout address quick checkout addresses are incomplete as wallet providers like Apple Pay and Google Pay do not provide all the address fields until the checkout is completed (confirmed) on their side
323
324
325
|
# File 'app/models/spree/order.rb', line 323
def quick_checkout?
shipping_address.present? && shipping_address.quick_checkout?
end
|
#quick_checkout_available? ⇒ Boolean
Check if quick checkout is available for this order Either fully digital or not digital at all
330
331
332
|
# File 'app/models/spree/order.rb', line 330
def quick_checkout_available?
payment_required? && shipments.count <= 1 && (digital? || !some_digital? || !delivery_required?)
end
|
#quick_checkout_require_address? ⇒ Boolean
Check if quick checkout requires an address collection If the order is digital or not delivery required, then we don’t need to collect an address
337
338
339
|
# File 'app/models/spree/order.rb', line 337
def quick_checkout_require_address?
!digital? && delivery_required?
end
|
#refresh_shipment_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END) ⇒ Object
707
708
709
|
# File 'app/models/spree/order.rb', line 707
def refresh_shipment_rates(shipping_method_filter = ShippingMethod::DISPLAY_ON_FRONT_END)
shipments.map { |s| s.refresh_rates(shipping_method_filter) }
end
|
#reimbursement_paid_total ⇒ Object
441
442
443
|
# File 'app/models/spree/order.rb', line 441
def reimbursement_paid_total
reimbursements.sum(&:paid_amount)
end
|
#requires_ship_address? ⇒ Boolean
863
864
865
|
# File 'app/models/spree/order.rb', line 863
def requires_ship_address?
!digital?
end
|
#restart_checkout_flow ⇒ Object
699
700
701
702
703
704
705
|
# File 'app/models/spree/order.rb', line 699
def restart_checkout_flow
update_columns(
state: 'cart',
updated_at: Time.current
)
next! unless line_items.empty?
end
|
#set_shipments_cost ⇒ Object
715
716
717
718
719
720
|
# File 'app/models/spree/order.rb', line 715
def set_shipments_cost
shipments.each(&:update_amounts)
updater.update_shipment_total
updater.update_adjustment_total
persist_totals
end
|
#shipped? ⇒ Boolean
626
627
628
|
# File 'app/models/spree/order.rb', line 626
def shipped?
%w(partial shipped).include?(shipment_state)
end
|
#shipping_discount ⇒ Object
262
263
264
|
# File 'app/models/spree/order.rb', line 262
def shipping_discount
shipment_adjustments.non_tax.eligible.sum(:amount) * - 1
end
|
#shipping_eq_billing_address? ⇒ Boolean
711
712
713
|
# File 'app/models/spree/order.rb', line 711
def shipping_eq_billing_address?
bill_address == ship_address
end
|
#shipping_method ⇒ Object
722
723
724
725
726
727
728
729
|
# File 'app/models/spree/order.rb', line 722
def shipping_method
Spree::ShippingMethod.
where(id: shipments.with_selected_shipping_method.limit(1).pluck(:shipping_method_id)).
limit(1).
first
end
|
#state_changed(name) ⇒ Object
594
595
596
597
598
599
600
601
602
603
|
# File 'app/models/spree/order.rb', line 594
def state_changed(name)
state = "#{name}_state"
if persisted?
old_state = send("#{state}_was")
new_state = send(state)
unless old_state == new_state
log_state_changes(state_name: name, old_state: old_state, new_state: new_state)
end
end
end
|
#tax_address ⇒ Object
Returns the address for taxation based on configuration
348
349
350
|
# File 'app/models/spree/order.rb', line 348
def tax_address
Spree::Config[:tax_using_ship_address] ? ship_address : bill_address
end
|
#tax_total ⇒ Object
786
787
788
|
# File 'app/models/spree/order.rb', line 786
def tax_total
included_tax_total + additional_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
343
344
345
|
# File 'app/models/spree/order.rb', line 343
def tax_zone
@tax_zone ||= Zone.match(tax_address) || Zone.default_tax
end
|
#to_csv(_store = nil) ⇒ Object
851
852
853
854
855
856
857
|
# File 'app/models/spree/order.rb', line 851
def to_csv(_store = nil)
csv_lines = []
all_line_items.each_with_index do |line_item, index|
csv_lines << Spree::CSV::OrderLineItemPresenter.new(self, line_item, index).call
end
csv_lines
end
|
#total_weight ⇒ BigDecimal
Returns the total weight of the inventory units in the order This is used to calculate the shipping rates for the order
654
655
656
|
# File 'app/models/spree/order.rb', line 654
def total_weight
@total_weight ||= line_items.joins(:variant).includes(:variant).map(&:item_weight).sum
end
|
#uneditable? ⇒ Boolean
491
492
493
|
# File 'app/models/spree/order.rb', line 491
def uneditable?
complete? || canceled? || returned?
end
|
#update_line_item_prices! ⇒ Object
426
427
428
429
430
431
|
# File 'app/models/spree/order.rb', line 426
def update_line_item_prices!
transaction do
line_items.reload.each(&:update_price)
save!
end
end
|
#update_with_updater! ⇒ Object
356
357
358
|
# File 'app/models/spree/order.rb', line 356
def update_with_updater!
updater.update
end
|
#updater ⇒ Object
352
353
354
|
# File 'app/models/spree/order.rb', line 352
def updater
@updater ||= OrderUpdater.new(self)
end
|
#use_all_coupon_codes ⇒ Object
829
830
831
832
833
|
# File 'app/models/spree/order.rb', line 829
def valid_coupon_promotions
promotions.
where(id: valid_promotion_ids).
coupons
end
|
#valid_credit_cards ⇒ Object
500
501
502
503
|
# File 'app/models/spree/order.rb', line 500
def valid_credit_cards
credit_card_ids = payments.from_credit_card.valid.pluck(:source_id).uniq
CreditCard.where(id: credit_card_ids)
end
|
825
826
827
|
# File 'app/models/spree/order.rb', line 825
def valid_promotion_ids
all_adjustments.eligible.nonzero.promotion.map { |a| a.source.promotion_id }.uniq
end
|
821
822
823
|
# File 'app/models/spree/order.rb', line 821
def valid_promotions
order_promotions.where(promotion_id: valid_promotion_ids).uniq(&:promotion_id)
end
|