Module: Order::AuthorizationSplittable

Extended by:
ActiveSupport::Concern
Included in:
Order
Defined in:
app/models/order/authorization_splittable.rb

Overview

Lets a split-off child order pull its share of the parent order's
authorization once it has a committed delivery to attach payment to.

Splitting moves line items into the child immediately, but its delivery
(and therefore the unit payments attach to) is committed later. The original
splitter only allocated payment when that delivery happened to exist at split
time; this concern + AuthorizationSplitWorker close the gap by
reacting when the delivery is finally committed.

Belongs to collapse

Has many collapse

Instance Method Summary collapse

Instance Method Details

#childrenActiveRecord::Relation<Order>

Returns:

  • (ActiveRecord::Relation<Order>)

See Also:



19
# File 'app/models/order/authorization_splittable.rb', line 19

has_many :children, class_name: 'Order', foreign_key: :parent_id, inverse_of: :parent, dependent: :nullify

#needs_authorization_split_from_parent?Boolean

True when this is a split-off child that is funded by a parent
authorization that hasn't been allocated to it yet, and whose deliveries
are now committed (so the amount is final and there's something to attach
payment to).

The authoritative idempotency guard — re-checked under the advisory lock in
Order::AuthorizationSplitWorker.

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/order/authorization_splittable.rb', line 31

def needs_authorization_split_from_parent?
  return false if parent_id.blank?

  own_deliveries = deliveries.to_a
  return false if own_deliveries.empty?
  return false if own_deliveries.any?(&:quoting?)      # wait until shipping is finalized
  return false unless balance.positive?                # nothing left to fund
  return false if payments.all_authorized.exists?      # already has its own authorization

  par = parent
  return false unless par

  # A parent authorization is still "unsplit" while its original CC auth is
  # intact (the shared-PI split voids it), or there's an authorized PO/PayPal.
  par.payments.all_authorized
     .where(category: [Payment::CREDIT_CARD, Payment::PO, Payment::VPO, Payment::CHECK, Payment::CASH, Payment::PAYPAL])
     .exists?
end

#parentOrder

Returns:

See Also:



18
# File 'app/models/order/authorization_splittable.rb', line 18

belongs_to :parent, class_name: 'Order', optional: true, inverse_of: :children