Class: Spree::OrderGroup

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
Marketplace::OrderGroup, Metadata, NumberIdentifier, Purchase::Addresses, Purchase::StoreCredits, SingleStoreResource
Defined in:
app/models/spree/order_group.rb

Overview

N orders placed together as one customer checkout.

Deliberately domain-neutral. Multi-vendor is its first consumer — a mixed cart splits into one order per seller — but the same container serves split-by-fulfillment-location, split-by-availability and B2B split-by-company-location, none of which involve a seller. The seller therefore lives on the child order and never here (docs/plans/6.0-multi-vendor-marketplace.md, Decision 8).

The group owns what the customer experienced once: the payment they made, the addresses they entered, the contact email. Children own what varies per seller: line items, fulfillments, totals, and their own money lines.

Status is derived across the children rather than stored, because a column would be a second source of truth for something the children already answer, and it would need re-writing on every child transition.

Constant Summary collapse

ROLLED_UP_TOTALS =

What the customer was charged for the whole checkout, and what it was charged for — the sums of what each child independently computed. Never prorated downward: the children are the record, these are the roll-up, and a gateway asking what it is charging for gets the whole basket because the whole basket is what the one charge covers.

Summed in Ruby rather than by SQL aggregate: anything reading these has the children loaded already (a serializer renders them, a gateway call reads four of these in a row), and an aggregate per figure would issue a query each time even against rows sitting in memory.

tax_total is each child's own derivation rather than a column, summed like the rest: asking the children keeps the group's answer the one they gave, where re-deriving it here from the two halves above would be a second way to reach the same figure.

%i[
  total item_total delivery_total additional_tax_total included_tax_total
  tax_total discount_total fee_total
].freeze

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods included from Marketplace::OrderGroup

#includes_first_party?, #seller_count, #sellers

Methods included from Purchase::Addresses

#assign_default_addresses!, #bill_address_attributes=, #bill_address_id=, #can_propose_deliveries?, #clone_billing_address, #clone_shipping_address, #owns_address?, #selectable_address, #selectable_addresses, #ship_address_attributes=, #ship_address_id=, #shipping_address_required?, #shipping_eq_billing_address?, #use_billing=, #use_billing?, #use_shipping?

Methods included from Purchase::StoreCredits

#add_store_credit_payments, #amount_due, #available_store_credits, #could_use_store_credit?, #covered_by_store_credit?, #display_order_total_after_store_credit, #display_store_credit_remaining_after_capture, #display_store_credit_total, #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, #store_credit_total, #total_applicable_store_credit, #total_applied_store_credit, #total_available_store_credit, #total_minus_store_credits, #using_store_credit?

Methods included from Metadata

#private_metadata, #private_metadata=

Instance Method Details

#completed? ⇒ Boolean

A group only ever exists because a checkout finished, so it is completed by construction. Store-credit reporting asks, to tell what was applied from what could still be.

Returns:

  • (Boolean)


299
300
301
# File 'app/models/spree/order_group.rb', line 299

def completed?
  true
end

#forget_derived_money ⇒ Object

Drops the memoized roll-ups. Money moves through the children and their payments, not through this record, so nothing here is dirtied by a write that changes what it reports.



250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/models/spree/order_group.rb', line 250

def forget_derived_money
  @rolled_up_totals = nil
  @payment_total = nil
  # The figures are summed in Ruby off these associations, so dropping the
  # memo alone would recompute from the same stale rows — a payment that
  # settled after they were loaded would go unnoticed, which is the exact
  # case refresh_payment_total! exists to answer.
  return unless persisted?

  association(:payments).reset
  association(:orders).reset
end

#fulfillment_groups ⇒ Array<Spree::FulfillmentGroup>

The parcels this purchase actually ships in — not one per child order. A parcel divided between sellers is counted once, because one box leaves the warehouse however many orders its contents belong to. See Spree::FulfillmentGroup for why the two numbers differ.

Ordered by child, so first-party goods lead and sellers follow in the order the split filed them, and the same purchase reads the same way twice.

Returns:



161
162
163
164
165
166
167
# File 'app/models/spree/order_group.rb', line 161

def fulfillment_groups
  Spree::FulfillmentGroup.build_from(
    fulfillments.
      includes(:stock_location, { order: :seller }, { selected_delivery_rate: :delivery_method }).
      order(:order_id, :id)
  )
end

#fulfillment_status ⇒ String?

Returns the group's fulfillment position, in the same vocabulary a single order uses, rolled up across children.

Returns:

  • (String, nil) —

    the group's fulfillment position, in the same vocabulary a single order uses, rolled up across children



196
197
198
199
200
201
202
203
204
205
# File 'app/models/spree/order_group.rb', line 196

def fulfillment_status
  roll_up(orders.map(&:fulfillment_status)) do |statuses|
    # Canceled siblings do not make the group canceled — a recalled parcel
    # does not describe the purchase while another is still coming.
    live = statuses - ['canceled']
    next 'canceled' if live.empty?

    live.one? ? live.first : 'partial'
  end
end

#gift_card ⇒ Spree::GiftCard?

The gift card this checkout was paid with, if any.

A group holds no card of its own — the children carry it, each copied from the one cart — so it answers with theirs. A payment method asks its owner this when deciding whether it may be used, and the owner of a grouped payment is this record.

Returns:



290
291
292
# File 'app/models/spree/order_group.rb', line 290

def gift_card
  orders.filter_map(&:gift_card).first
end

#gift_card_total ⇒ BigDecimal

What the customer paid with a gift card. Not rolled up from the children: the group claims the payments at completion, so a child's own gift_card_total — which reads its payments — answers zero for every one of them, and the confirmation would omit a deduction its total makes.

Returns:

  • (BigDecimal)


187
188
189
190
191
192
# File 'app/models/spree/order_group.rb', line 187

def gift_card_total
  return 0.to_d if gift_card.nil?

  store_credit_ids = payments.store_credits.valid.pluck(:source_id)
  Spree::StoreCredit.where(id: store_credit_ids, originator: gift_card).sum(:amount)
end

#last_ip_address ⇒ String?

Where the checkout was made from, carried on the children because they are what the checkout produced.

Returns:

  • (String, nil)


128
129
130
# File 'app/models/spree/order_group.rb', line 128

def last_ip_address
  orders.first&.last_ip_address
end

#locale ⇒ String?

Returns:

  • (String, nil)


133
134
135
# File 'app/models/spree/order_group.rb', line 133

def locale
  orders.first&.locale
end

#name ⇒ String?

Who to address the purchase to, read off the addresses the checkout copied onto the group — the same answer Spree::Order#name gives, so a customer-facing document greets them identically either way.

Returns:

  • (String, nil)


147
148
149
# File 'app/models/spree/order_group.rb', line 147

def name
  (bill_address || ship_address)&.full_name
end

#number_store ⇒ Object



303
304
305
# File 'app/models/spree/order_group.rb', line 303

def number_store
  store
end

#outstanding_balance ⇒ BigDecimal

Refunds for goods that came back settle what was owed for them, as on Spree::Order#outstanding_balance, so they do not count as uncollected.

Returns:

  • (BigDecimal) —

    still to collect across the whole checkout



267
268
269
# File 'app/models/spree/order_group.rb', line 267

def outstanding_balance
  total - payment_total - returned_items_refund_total
end

#paid? ⇒ Boolean

Returns:

  • (Boolean)


278
279
280
# File 'app/models/spree/order_group.rb', line 278

def paid?
  total.positive? && payment_total >= total
end

#payment_status ⇒ String?

The payment is shared, but each child reports its own share of it — so this rolls those up rather than reading the payment, and a checkout with one seller shipped and another not says so.

Returns:

  • (String, nil)


212
213
214
# File 'app/models/spree/order_group.rb', line 212

def payment_status
  roll_up(orders.map(&:payment_status))
end

#payment_total ⇒ BigDecimal

Reads the persisted rows rather than filtering the loaded association: a payment still being validated is already held by payments (the association is inverse_of this group), and counting it here would have the group treat money it has not taken yet as settled — which is what max_amount then subtracts from the total.

Returns:

  • (BigDecimal) —

    completed payments on this group, less their refunds



230
231
232
233
234
235
# File 'app/models/spree/order_group.rb', line 230

def payment_total
  @payment_total ||= begin
    settled = payments.completed.to_a
    settled.sum(&:amount) - Spree::Refund.completed_or_unset.where(payment_id: settled.map(&:id)).sum(:amount)
  end
end

#po_number ⇒ String?

Returns:

  • (String, nil)


138
139
140
# File 'app/models/spree/order_group.rb', line 138

def po_number
  orders.first&.po_number
end

#refresh_payment_total! ⇒ BigDecimal

Nothing to persist — payment_total is derived here — but the payment path calls this after settling, so it must exist, and it must forget what it last worked out or a settled payment would go unnoticed.

Returns:

  • (BigDecimal)


242
243
244
245
# File 'app/models/spree/order_group.rb', line 242

def refresh_payment_total!
  forget_derived_money
  payment_total
end

#returned_items_refund_total ⇒ BigDecimal

Returns what refunds for returns, claims and exchanges on this checkout's payments have given back.

Returns:

  • (BigDecimal) —

    what refunds for returns, claims and exchanges on this checkout's payments have given back



273
274
275
# File 'app/models/spree/order_group.rb', line 273

def returned_items_refund_total
  Spree::Refund.for_returned_items.completed_or_unset.where(payment_id: payments.completed.select(:id)).sum(:amount)
end

#unfulfilled_line_items(groups = fulfillment_groups) ⇒ Array<Spree::LineItem>

What was bought that no parcel carries — a downloaded album, a gift card sent by email. Without it a confirmation organised by parcel would list everything the customer is waiting for and silently omit what they already have.

Parameters:

  • groups (Array<Spree::FulfillmentGroup>) (defaults to: fulfillment_groups) —

    the parcels, when the caller has already built them — rebuilding re-reads the fulfillments

Returns:



177
178
179
# File 'app/models/spree/order_group.rb', line 177

def unfulfilled_line_items(groups = fulfillment_groups)
  line_items.to_a - groups.flat_map(&:line_items)
end