Class: Spree::PurchaseOrders::Create

Inherits:
Workflow
  • Object
show all
Defined in:
app/workflows/spree/purchase_orders/create.rb

Overview

Drafts an order to a supplier. Nothing is on order yet and nothing touches availability — a draft is what the merchant is still deciding to buy.

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

#purchase_order ⇒ Object (readonly)

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



10
11
12
# File 'app/workflows/spree/purchase_orders/create.rb', line 10

def purchase_order
  @purchase_order
end

Instance Method Details

#perform(store:, supplier:, destination_location:, items: [], currency: nil, expected_at: nil, cancel_by: nil, reference: nil, notes: nil, metadata: nil, created_by: nil) ⇒ Object

Parameters:

  • store (Spree::Store)
  • supplier (Spree::Supplier)
  • destination_location (Spree::StockLocation) —

    where the goods are expected

  • items (Array<Hash>) (defaults to: []) —

    [{ variant:, quantity_ordered:, unit_cost: }]

  • currency (String, nil) (defaults to: nil) —

    defaults to the store's; set it for a foreign-currency order

  • expected_at (Date, nil) (defaults to: nil)
  • cancel_by (Date, nil) (defaults to: nil) —

    the date past which the merchant no longer wants the goods, so an order still open after it reads as overdue to cancel rather than merely late

  • reference (String, nil) (defaults to: nil) —

    the supplier's own order number

  • notes (String, nil) (defaults to: nil)
  • metadata (Hash, nil) (defaults to: nil)
  • created_by (Object, nil) (defaults to: nil)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/workflows/spree/purchase_orders/create.rb', line 27

def perform(store:, supplier:, destination_location:, items: [], currency: nil,
            expected_at: nil, cancel_by: nil, reference: nil, notes: nil, metadata: nil, created_by: nil)
  super

  step :build_purchase_order
  run_hooks :validate

  ApplicationRecord.transaction do
    step :save_purchase_order
  end

  run_hooks :after_create
  # The model's lifecycle events already emit `purchase_order.created`.
  success(purchase_order.reload)
end