# Digital products

The `Digital` module delivers download variants (`FulfilmentType::Download`). Tables: `digital_products` (per variant:
`download_limit`, `valid_days`), `digital_files` (versions on the `private` disk under `digital/{variant}`; the latest
is served), `digital_downloads` (one entitlement per order line: a secret `token`, the count, the limit and expiry
copied at grant time, `revoked_at`).

`Digital\Contracts\Downloads`: `grant(orderId)` (idempotent; only when the order is paid, or delivered for cash on
delivery, and either `orders.digital_consent_at` is set or the 14 days after placing passed; lines held by a return are
skipped) fires `DownloadsGranted`; `release()` runs daily as `digital:release` for orders that waited out the period;
`revoke()` on cancellation or return; `forOrder()`, `forCustomer()`, `holders(variantId)`; `open(token)` counts a
download under a lock and returns the file, `known()` tells a used-up link from an unknown one. The listener
`DeliverDownloads` reacts to `OrderStatusChanged`. Admin: `DigitalFileController` (`admin.digital.files`,
`catalog.manage`); a new version with "notify" fires `DigitalFileUpdated`.

The storefront serves `store.{locale}.download` (`DownloadController`, throttled, `no-store`, `noindex`), the account's
`account.downloads`, the order page section, and `Mail\DownloadsMail` (`ready`, `updated`) from
`Listeners\SendDownloadsMail` (queued).

Checkout: `VariantCard::$fulfilment` and `OrderLineDraft::$fulfilment` (`order_lines.fulfilment_type`) carry the type;
`CheckoutDetails::$digitalConsent` becomes `orders.digital_consent_at` when the order has digital lines. A cart with no
physical lines gets the single `DeliveryMethod::Digital` option (no shipping) and no cash on delivery
(`Checkout::paymentMethods($cart)`); `OrderStatus::Paid` may go to `Delivered` for such orders, and `Picking` is not
offered. `Returns` leaves digital lines out of the returnable units when consent was given (OUG 34/2014 art. 16 lit. m).

Tests: `tests/Feature/Digital/DownloadsTest.php`.

## Gift cards

`gift_cards` (code `citext`, `initial_amount`, `balance`, currency, the order and line that bought it, `expires_at`,
`disabled_at`) and `gift_card_redemptions` (order, amount, `reversed_at`). `Digital\Contracts\GiftCards`: `usable()`,
`redeem()` (under a lock; `GiftCardRefused` when the balance no longer covers), `reverse()`, `issueFor()` (idempotent,
under a lock on the order lines, one card per unit at the unit price, 12 months), `voidFor()` (only whole cards),
`issue()` by staff, `disable()`, `forOrder()`. `SettleGiftCards` follows `OrderStatusChanged` (paid: issue; cancelled:
reverse and void; returned: void), `GiftCardsIssued` sends `Mail\GiftCardsMail`.

In the totals, a gift card line is `exempt` at 0% (`DatabaseCarts`), the cart keeps `gift_card_code`, and
`Totals\ApplyGiftCard` sits in the new last slot `TotalsStages::GIFT_CARDS`, after VAT: it sets
`Totals::$giftCardAmount` without touching VAT, and `Totals::due()` is what the payment, the courier's cash on delivery
and the bank transfer collect. A cart whose card covers everything (delivery priced) gets only
`PaymentMethod::GiftCard`; `DefaultCheckout::place()` redeems the card inside the order's transaction
(`CheckoutRefused('gift_card')` when the balance moved) and marks such an order paid.

Tests: `tests/Feature/Digital/GiftCardsTest.php`.

## Member access

`Contracts\Memberships` (`Access\DatabaseMemberships`): `access_plans` (variant → `customer_group_id`, `days` or null
for good; `Http\Controllers\AccessPlanController`, `admin.digital.access`, catalog.manage) and `memberships` (citext
`email`, group, order and order line, the variant, `starts_at`, `ends_at`, `revoked_at`). `grant(orderId)` once the
order's money is in, one row per unit, each starting where the latest unrevoked one for the e-mail and group ends;
`revoke(orderId, ?lineIds)`; `activeGroups(email)`; `forEmail(email)`. `Listeners\DeliverAccess` follows
`OrderStatusChanged`, `OrderPaymentCollected` and a refunded `ReturnStatusChanged`. Access is not exempt from
withdrawal (`OrderLineDraft::isDigital()` stays downloads and keys).

The groups take effect through `B2B\Contracts\BuyerContext::prices()` (`DatabaseCompanies` adds the active groups of
the signed-in shopper's confirmed e-mail to the `PriceContext`, cached for the request) and `CompanyCatalogVisitor`
(a viewer with groups but no company: `CatalogViewer(company: false, groupIds)`; `isPublic()` is now no company and no
groups, and `Audience` lets only company viewers see company-only rows).

Member-only pages: `pages.customer_group_id` (Content); `Pages::footer()` and `sitemap()` leave them out, `PageView::$groupId`
carries it, and the storefront `PageController` answers 403 with `store.members-only` unless the group is in the
shopper's `PriceContext::$groupIds`, never indexed either way.
