Class: Spree::Commissions::CommissionOrder

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/commissions/commission_order.rb

Overview

Writes an order's commission lines: one per sold item, plus one per fulfillment when the rate charges delivery too.

Runs once, at placement, and is idempotent — an order that already carries commission is left exactly as it is. Both matter because the lines are a settlement record: recomputing them later would let a rate edit rewrite what a seller was already told they owed, and a replayed placement would charge them twice.

Lines are grouped by the seller each item was bought from, which is line_item.seller_id — the snapshot frozen when the order was placed, not whoever owns the product today. Items with no seller are the marketplace's own and are not commissioned.

It runs inside the customer's checkout, so everything it reads is loaded up front: the sellers, their billing addresses (the tax question is asked against those), the products behind each line, and the order's fulfillments — each once for the whole order rather than once per line.

Swap through Spree.commissions_commission_order_service.

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(order:) ⇒ Array<Spree::CommissionLine>

Returns the lines written, empty when there was nothing to commission.

Parameters:

Returns:



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
# File 'app/services/spree/commissions/commission_order.rb', line 31

def call(order:)
  if order.commission_lines.exists?
    # Refreshed rather than assumed: a replayed placement can arrive
    # between the winner writing its rows and stamping the columns, and
    # returning here without re-summing would leave the order reporting
    # no commission on a sale that was charged for.
    refresh_commission_totals(order)
    return success([])
  end

  commissioned = order.line_items.
                 includes(seller: :billing_address, variant: { product: :categories }).
                 select(&:seller_id)
  return success([]) if commissioned.empty?

  rates = Spree::Commissions::ResolveRate.candidates_for(order.store)
  categories = Spree::Commissions::ResolveRate.categories_for(
    commissioned.map { |line_item| line_item.variant&.product }
  )
  deliveries = deliveries_by_seller(order)
  lines = []

  Spree::CommissionLine.transaction do
    commissioned.group_by(&:seller_id).each_value do |line_items|
      lines.concat(
        commission_seller(
          order: order, seller: line_items.first.seller, line_items: line_items,
          rates: rates, categories: categories, deliveries: deliveries
        )
      )
    end
  end

  refresh_commission_totals(order)

  success(lines)
rescue ActiveRecord::RecordNotUnique
  # Another delivery of the same event got there first. The unique index
  # on line item and fulfillment is what makes that safe: the winner's
  # rows are the record, and the loser reports them rather than failing a
  # checkout over work already done.
  refresh_commission_totals(order)

  success(order.commission_lines.reload.to_a)
end