Class: Spree::Returns::Create

Inherits:
Workflow
  • Object
show all
Includes:
Spree::Refunds::TaxCredit, ReturnableQuantity
Defined in:
app/workflows/spree/returns/create.rb

Overview

Opens a return request for fulfilled items on a completed order.

The validate hook is the seam for return-eligibility policy — 30-day windows, final-sale categories, per-customer-group rules, regional consumer rights. That policy is the most-customized rule in commerce and deliberately does not live in core.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Workflow

#call, define_argument_readers, #errors, #halt!, hooks, inherited, model_name, #read_attribute_for_validation, #reject!, workflow_key

Methods included from ServiceModule::Base

#call, prepended

Methods included from Workflow::ControlFlow

#failure

Instance Attribute Details

#return_record ⇒ Object (readonly)

The created return — hook handlers read it (nil while :validate runs).



16
17
18
# File 'app/workflows/spree/returns/create.rb', line 16

def return_record
  @return_record
end

Instance Method Details

#perform(order:, items:, stock_location: nil, reason: nil, memo: nil, created_by: nil) ⇒ Object

Parameters:

  • order (Spree::Order) —

    a completed order with fulfilled items

  • items (Array<Hash>) —

    [{ fulfillment_item:, quantity: }]

  • stock_location (Spree::StockLocation, nil) (defaults to: nil) —

    where the goods come back to; defaults to the location that shipped them

  • reason (Spree::ReturnReason, nil) (defaults to: nil)
  • memo (String, nil) (defaults to: nil) —

    customer- or staff-supplied note

  • created_by (Object, nil) (defaults to: nil) —

    who is opening it (see Spree.actor_classes); nil for customer self-service



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/workflows/spree/returns/create.rb', line 26

def perform(order:, items:, stock_location: nil, reason: nil, memo: nil, created_by: nil)
  super

  step :ensure_returnable
  step :normalize_items

  # Veto point — return-window and eligibility policy. Before anything
  # is written.
  run_hooks :validate

  # The tax is worked out with the return rather than after it, so no
  # return ever offers a refund whose tax nobody calculated.
  ApplicationRecord.transaction do
    step :build_return
    step :calculate_tax
  end

  run_hooks :after_create
  return_record.publish_event('return.requested')
  success(return_record.reload)
end