Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

9.8 KB
Raw Blame

B2B Customer PO Numbers

Status: Implemented 2026-08-30 (all four migration-path phases) Target: Spree 6.0 Depends on: Cart/Order split (6.0-cart-order-split.md), B2B Companies & Catalogs (6.0-b2b-companies-and-catalogs.md), Service Workflows (6.0-service-workflows.md — Carts::Complete validations) Author: Damian + Claude Last updated: 2026-08-30

Summary

For corporate buyers their own purchase-order number is as important as the merchant's order number — it is how their accounting reconciles the order, the invoice and the payment years later. Spree doesn't need a procurement system for this; it needs to treat the buyer's PO as a first-class, permanent, searchable reference on the order. This plan adds po_number on cart + order (offered at B2B checkout and on draft orders, carried everywhere the order goes), an optional PO document upload (no platform ships this natively on orders — the only native attachment precedents anywhere are on quotes), and a per-company required flag. The field follows the market's best shape: always available and optional at B2B checkout (the biggest platform's approach — theirs has no configuration at all and can't even be made required natively), with requiredness hung on the company (the granularity both platforms that control it use), never on a payment method (two platforms tie the PO to an offline payment method, which couples a reference to a tender choice — deliberately not copied).

Not to be confused with Spree::PurchaseOrder from 6.0-inventory-operations.md — that is the merchant's procurement PO to suppliers, the opposite direction, its own model. The buyer's reference is a column named po_number, the industry-exact field name.

Key Decisions (do not deviate without discussion)

  • po_number is a plain column on cart and order (2026-08-30) — copied at completion like the other purchase fields, exposed under the same name on store and admin serializers (read/write symmetry). Never a metafield, never payment-method data. The buyer sets it pre-completion; staff may set or correct it after placement (a plain attribute write — it is not money, and "the buyer forgot" is the common case both reference platforms allow for).
  • The field is always offered on B2B carts, optional by default; po_number_required is a boolean on Spree::Company (2026-08-30, interactive review, after the platform research): where the market controls this at all it does so per company, it is the merchant's literal ask, and it is a property of the buyer's own procurement process, not of the commercial agreement — so it is deliberately NOT a catalog term. Not a governance field (it gates order data, not member powers), so the no-governance-on-OSS-companies constraint holds. Enforced in Carts::Complete as a validation (code: 'po_number_required') for customer-initiated checkouts; staff-created orders skip it.
  • PO document upload is optional and order-native — po_document as a private-storage attachment on cart + order (direct upload via signed blob; spoofing-protected content-type validation per the seller-submission precedent), the same blob re-attached to the order at completion (blob sharing, the media-library precedent). A genuine first — no competitor has it outside quote attachments.
  • Searchable everywhere a reference is looked up. po_number joins the order's ransackable attributes, and the dashboard's order search matches order number OR PO number — an accountant holding either reference finds the same transaction. Surfacing: admin order page, customer order history, order confirmation email (a line when present — cheap differentiation: the biggest platform's own emails can't show it), and every future order-facing document (invoices, packing lists) must render it when present.
  • Quotes read this field, not their own. The Enterprise quote plan's customer_reference and quote-level attachment are replaced by the underlying draft order's po_number + po_document (amended 2026-08-30) — one reference, no duplication, and "quotation → buyer issues PO → order" is just filling the field before acceptance.
  • Buyer-side PO permissions are Enterprise governance — "only an authorised buyer may submit the company's PO" is a capability on company roles, the existing Enterprise plan's territory; nothing here prepares or blocks it. Invoice-number linkage rides the Enterprise terms/invoicing roadmap; OSS delivers the searchable reference and the order↔payment linkage that already exists.

Design Details

  • Schema: po_number string on spree_carts + spree_orders; po_number_required boolean (default false, null: false) on spree_companies; Active Storage attachments (no Spree columns).
  • Writes: store cart update permits po_number + po_document (signed blob id); admin order create/update permit both (draft orders — "a PO arrives by email and the merchant keys the order in" is just the existing draft flow plus these fields); admin company form + serializer gain the flag.
  • Completion: po_number copies with the purchase fields; create_draft_order!'s copy attaches the cart's blob to the order. The Carts::Complete validation reads resolved_company.
  • Dashboard: PO field on the draft-order create form and order page (editable, showing both references side by side; document download/replace); orders list gains a PO column option + the combined number-or-PO search; company page gets the requiredness toggle.
  • Storefront reference: PO input (+ optional file) at checkout for B2B carts, marked required when the company demands it; order history shows the reference.

Migration Path

  1. Schema + model (columns, attachment, validation, ransackable). Done.
  2. API surfaces (store cart, admin orders, admin companies) + SDK types. Done.
  3. Carts::Complete validation + email line. Done.
  4. Dashboard surfaces. Done. Storefront reference app — pending, in the spree/storefront repo.

No data migration; empty fields are today's behavior.

What Was Built (2026-08-30)

  • Spree::Purchase::PurchaseOrder, included by Cart and Order: the po_number normalizer, the private po_document attachment (spoofing protection + 10 MB cap), and po_number_required? reading through resolved_company. Covered by one shared-example battery run against both hosts.
  • The requirement is a Checkout::DefaultRequirements entry, not a bespoke Carts::Complete step — a deliberate refinement of the plan's wording. Carts::Complete already gates on Checkout::Requirements, and that evaluator already derives code: "#{field}_required", so the plan's po_number_required code falls out exactly as specified while the storefront also sees the requirement before completion, which a completion-only validation could not offer. Staff-created carts are exempt via created_by_id.
  • Completion copies the number with the other purchase fields and re-attaches the same blob to the order; the cart keeps its own attachment so a rolled-back completion leaves the buyer's upload intact. SplitBySeller carries the number onto sibling orders.
  • Search: po_number joins Order.search beside the order number, so one query finds either reference, plus the ransackable list.
  • Store API: po_number + po_document writable on the cart; a nested carts/:id/po_document resource presigns (private storage only), streams and detaches. Presigning is nested under the cart deliberately — a general store upload endpoint would let any publishable-key holder mint blobs.
  • Admin API: both fields on order create/update, a streamed GET /orders/:id/po_document mapped to the read scope and the :show ability, and po_number_required on the company.
  • Emails: rendered from the shared order-summary section (HTML) and a shared _po_number text partial, so confirmation, cancellation and the store-owner notification all carry it, and future documents get it by rendering the partial.
  • Dashboard: a purchase-order card on the order page (number and document both editable after placement, with an authenticated download), the same pair on the draft-order form — the case the field exists for, where a PO arrives by email and the merchant keys the order in — an off-by-default PO column plus the combined search on the orders table, and the requirement toggle on the company page. FileUploadField gained a private option for this, and reports when an upload is in flight so a save cannot outrun it.

Constraints on Current Work

  • Never model the buyer's PO as a payment method or couple it to tender choice.
  • po_number (buyer reference) and Spree::PurchaseOrder (merchant procurement) never mix — no shared endpoints, serializers, or UI vocabulary between them.
  • Any new order-facing document or email renders po_number when present.
  • Post-placement edits to po_number are staff-only plain writes — never route them through the order-change substrate, and never let them touch money.

Open Questions

None for this scope. (Per-channel/store requiredness defaults can arrive later as a nullable tri-state on the company if merchants ask; noted, not built.)

References

  • Merchant feedback, 2026-08 (PO number + optional document, per-company required flag, audit-trail searchability, PO-starts-an-order).
  • Platform research 2026-08-30 (field granularity, per-company control, the payment-method anti-pattern, the upload gap) — private, not in this repo.
  • docs/plans/6.0-inventory-operations.md — the other purchase order.
  • ee/docs/plans/b2b-quotes.md — amended to consume this field.
  • Attachment precedents: seller requirement submissions (spoofing protection), media library (blob sharing).