B2B
The B2B module holds company accounts (companies: CUI citext, register details, customer_group_id,
payment_terms_days, credit_limit, verified_at) and their buyers (company_members: one company per customer,
CompanyRole admin | buyer | viewer).
B2B\Contracts\Companies::membershipOf(customerId)andB2B\Contracts\BuyerContext(prices(),membership()) are one request-scoped service (DatabaseCompanies), so the membership is looked up once per request.prices()gives aPriceContextwith the company's group, its id andpricesIncludeVatfrom the group (Pricing\Contracts\CustomerGroups); otherwise retail.- The storefront asks
StoreContext::priceContext()/netPrices()/membership(): product pages, listings (Shelf), the cart and checkout price with it. The product page builds its JSON-LD offers from the retail context whatever the buyer. DefaultCheckoutprices withBuyerContext, refuses a viewer (CheckoutRefused('viewer')) and recordsOrderDraft::$companyId(orders.company_id); the checkout form is prefilled with the company.Platform\Contracts\CompanyRegister(AnafCompanyRegister, lifted from Harmoniq) looks a CUI up in ANAF's public VAT register, cached a day (not when ANAF is down); the admin uses it to fill the form and to mark a company checked and its VAT status.- Admin:
CompanyController(admin.companies.*), buyers added throughCustomerAccounts::findByEmail().
Only buyers with a confirmed e-mail are added, and membershipOf() ignores an unconfirmed one.
Payment on terms: PaymentMethod::Terms (not prepaid), offered by Checkout::paymentMethods($cart) when the
membership has paymentTermsDays and Checkout::termsBlocked($cart) is null (no overdue order, within
creditLimit in RON, from OrderBook::termsBalance()); orders.payment_due_at is set at placing and orders.paid_at
when the money comes in (OrderBook::markCollected(), once under a row lock, then OrderPaymentCollected; also
set by any move to Paid). Placing an order on terms takes a per-company advisory lock (pg_advisory_xact_lock) and
checks the balance again inside the transaction, so two orders placed together cannot both fit under the limit. An
overdue order in any currency stops terms.
What is handed over only when paid asks PlacedOrder::moneyIn(): cash on delivery once delivered, on terms once
collected, otherwise from Paid on. Downloads, licence keys and gift cards bought on terms wait for the collection
(their listeners also handle OrderPaymentCollected), and the invoice is marked paid only then. A digital order on
terms goes Confirmed → Delivered.
Order rules: product_variants.b2b_min_quantity / b2b_pack_size (on VariantCard and VariantChoice,
b2bStart()); DatabaseCarts gives company lines (PriceContext::$companyId) the problems min_quantity and pack,
which block checkout like any line problem. companies.min_order_amount (bani, RON, goods with VAT after discounts)
is checked by DefaultCheckout::place() (CheckoutRefused('min_order')).
Catalog visibility: products.audience and categories.audience (Catalog\Data\CatalogAudience: everyone |
companies | groups, with product_audience_groups / category_audience_groups). Catalog\Storefront\Audience holds
the SQL: a category's audience covers its subtree (ltree @>), a product needs its own and that of every category
above its primary one. The viewer comes from Catalog\Contracts\CatalogVisitor (the catalog binds PublicVisitor
with bindIf; B2B binds CompanyCatalogVisitor from BuyerContext). DatabaseCatalogBrowser (menu, category,
product, listings, brands, archived), Facets (values) and DatabaseVariantDirectory (sellable) apply it; the
sitemap always uses CatalogViewer::public(). ProductPage::$public / CategoryPage::$public turn off indexing,
JSON-LD and the Markdown alternate. Search documents carry public (Typesense field, product_search.is_public);
SearchQuery::$publicOnly is off only for a company buyer. Changing a category's audience re-queues its products.
Fast ordering: Storefront\Actions\AddLinesToCart adds several variants through Carts::add() and reports each
refusal (CartErrors::message()); QuickOrderController (account.quick, company members only) reads typed lines or a
CSV upload with QuickOrderInput (separators , ; tab space, header skipped, repeats added up, max 200 lines,
Windows-1250 converted) and finds variants with VariantDirectory::variantsBySku(); ReorderController
(order.reorder, by the order's private token) re-adds an order's non-gift lines.
Order lists: B2B\Contracts\OrderLists (DatabaseOrderLists; tables order_lists, order_list_items), every call
scoped by company id; save() locks the company row, replaces a list of the same name (case-insensitive) and keeps at
most MAX_LISTS. Storefront\Http\Controllers\OrderListController (account.lists*): any member sees and adds to
cart, only canOrder() roles save or delete.
Quotes (RFQ): B2B\Contracts\Quotes (Quotes\DatabaseQuotes; tables quotes, quote_lines), every storefront
call scoped by company id. QuoteStatus: requested → offered → accepted → ordered, or declined (buyer), refused (shop),
expired. A quote keeps its currency and prices_include_vat from the buyer's price context when asked; line prices
(list_unit, unit) are minor units in that convention. request() locks the company row and allows MAX_OPEN
requested or offered at once. Staff answer with Actions\OfferQuote (only requested or offered; quantity 0 drops a
line) and Actions\RefuseQuote (Http\Controllers\QuoteController, admin.quotes.*, quotes.manage); both raise
QuoteOffered, and request() raises QuoteRequested (Storefront\Listeners\SendQuoteMail: the shop's orders address,
then the buyer who asked). accept() locks the quote and makes Cart\Contracts\Carts::agreed(): a fixed cart
(purpose quote:{id}, agreed true, owned by the accepting buyer) with each line's price converted to gross with the
product's VAT rate; the same buyer asking again gets the same cart, another buyer of the company takes the quote over
and the first cart is cleared, so a quote has one cart at a time. The storefront puts the token where the checkout
looks for a subscription's cart (SubscriptionController::SESSION). Totals::$agreed makes the promotions stage skip
the cart, and a fixed price on a net buyer's line is shown net (VatRate::fromGross). Listeners\MarkQuoteOrdered
(OrderPlaced, source quote:{id}, same company, status accepted) closes it with the order id; b2b:expire-quotes
(Actions\ExpireQuotes, hourly) expires offered and accepted quotes past valid_until and clears their carts.
Privacy\QuotePersonalData exports a customer's requests and erases their link and note. Storefront:
Storefront\Http\Controllers\QuoteController (account.quotes*), any member sees, canOrder() roles ask, accept and
decline; impersonating staff cannot (not in NotWhileImpersonating::ALLOWED_CHANGES).
Approval flows: companies.approval_limit (bani, RON) on CompanyMembership::$approvalLimit;
CompanyMembership::needsApproval(Money $total) is true for a buyer role over the limit, or in another currency.
DefaultCheckout drops card payment from paymentMethods($cart) when the cart needs approval and places the order with
OrderDraft::$awaitingApproval (see the orders guide for the hold). Companies::adminIds(companyId) gives the
administrators; Storefront\Listeners\SendApprovalMail writes to them on OrderPlaced (pending only) and to the buyer on
OrderApprovalDecided. Storefront\Http\Controllers\ApprovalController (account.approvals*) is for the company's
admin role and calls OrderBook::awaitingApproval() / decideApproval().
Tests: tests/Feature/B2B/ApprovalsTest.php, tests/Feature/B2B/QuotesTest.php, tests/Feature/B2B/CompaniesTest.php, tests/Feature/B2B/CatalogVisibilityTest.php, tests/Feature/B2B/FastOrderingTest.php, tests/Feature/B2B/OrderListsTest.php; companyBuyer() in
tests/Support/b2b.php.