Loyalty
The Loyalty module keeps points per customer (PLAN › Loyalty).
loyalty_entries(Models\LoyaltyEntry):customer_id, signedpoints,kind(Data\LoyaltyKind: earned, redeemed, refunded, reversed, expired, adjusted),order_id,available_at,note,user_id. The balance is the sum of entries withavailable_atnow or earlier; later ones are pending. Every change for one customer runs underpg_advisory_xact_lock(DatabaseLoyalty::lock()).Settings\LoyaltySettings(loyalty.*):enabled,points_per_leu(decimal string),point_value(bani),min_redeem,max_share(percent),hold_days,expire_months.Contracts\Loyalty(Ledger\DatabaseLoyalty):programme(),balance(),history(),redeem(customerId, points, orderId)(in the order's transaction;Data\LoyaltyRefusedwhen the balance no longer covers it) andadjust()(never below zero).- Totals:
Totals::$loyaltyPointscomes fromcarts.loyalty_points(Carts::useLoyalty(), cleared with the cart).Totals\ApplyLoyaltyin theloyaltyslot caps the points at the balance, atmax_shareof the goods after earlier discounts and at whole points' worth, refuses fewer thanmin_redeem(a notice), works only for a signed-in shopper, in RON, outside company accounts, and adds anAdjustmentwithout lines (VAT spread over the rates) whosereferenceis the number of points.DefaultCheckoutreads it back and callsredeem()next to vouchers and gift cards;CheckoutRefused('loyalty')sends the shopper to the cart. Ledger\SettleOrderPointsthroughListeners\FollowOrder(OrderStatusChanged): delivered →earn()(points =OrderRevenue::netProducts()×points_per_leu, rounded down; not for company or marketplace orders, guests or when off; once per order), cancelled →refund()(the points spent come back now) andreverse(), returned →reverse()(a still-pending earning is matched by a reversal on the same date, so it never becomes spendable).loyalty:expire(02:20 daily): customers whose newest entry is older thanexpire_monthslose their balance.- Storefront:
Storefront\Http\Controllers\LoyaltyController(cart.loyalty,cart.loyalty.remove,account.loyalty); the cart shows the offer for a signed-in shopper outside a company with at leastmin_redeem. Admin:Http\Controllers\LoyaltyController(admin.settings.loyalty.*,admin.loyalty.adjust,loyalty.manage); the customer page card reads the contract. Privacy\LoyaltyPersonalDataexports and erases the entries (the account's deletion cascades too).
Cards: loyalty_cards (Models\LoyaltyCard: number unique upper-case, customer_id, expires_on, is_active,
linked_at, code_hash: HMAC of the optional activation code), Contracts\LoyaltyCards
(Ledger\DatabaseLoyaltyCards: forCustomer(), usableFor(), link(customerId, number, code) → linked | unknown
(no card, off, or a wrong or missing code) | taken | one (the account already has a usable card); numbers and codes
normalised without spaces or dashes). Settings card_discount (percent string) and
card_free_shipping_from (bani). Totals\ApplyLoyaltyCard, registered in the loyalty slot before the points, takes the
percent off what is left of each line (not exempt gift card lines) as an Adjustment naming its lines (reference
card:···1234, so the checkout's points reader skips it) and sets withFreeShipping() from the threshold (RON). Admin:
Http\Controllers\LoyaltyCardController (admin.loyalty.cards.*: list, add, CSV import up to 5,000 rows, switch);
storefront: account.loyalty.card (confirmed accounts only, 10 per 10 minutes per address and 5 failures a day per
account, counted before the try through RateLimiter::hit() so simultaneous requests cannot slip under it, the same answer for unknown and taken numbers). Holders' names and e-mails, and linking by
e-mail in the admin, need customers.view. The
privacy export lists the cards; erasure unlinks them.
Tests: tests/Feature/Loyalty/LoyaltyTest.php.