# Pricing

`SyncShop\Modules\Pricing\Contracts\Prices` answers one question: what does this buyer pay for this variant, at this
quantity, in their currency? Promotions, vouchers, shipping and the SGR deposit come later in the totals pipeline.

```php
$price = resolve(Prices::class)->price($variantId, PriceContext::retail(), quantity: 3);
$price->unit;        // what to show: gross for retail, net for B2B
$price->unitNet;     // without VAT
$price->unitGross;   // with VAT
$price->listGross;   // the retail list price in this currency, before any price list
$price->priceListId; // the list that set it, or null
```

## The buyer: `PriceContext`

A retail visitor (`PriceContext::retail()`), a retail customer, or a buyer inside a company account:

| Field | Meaning |
| --- | --- |
| `groupIds` | the customer groups the buyer is in |
| `companyId` | their company, for B2B |
| `currency` | the currency prices are shown and charged in |
| `pricesIncludeVat` | retail sees prices with VAT; B2B sees net prices and VAT is added in the cart |
| `at` | the moment the price is for (now by default) |

## Price lists

A price list has one currency and says whether its prices include VAT. It applies to:

- a **customer group** (for example *Distribuitori*, with net prices);
- one **company** (the B2B module links it);
- or, with neither, **everyone** buying in its currency. That is how a product gets a fixed EUR price instead of a
  converted one.

A list can be switched off and can have start and end dates. Each item sets either a **fixed price**, in the list's
currency and on its VAT basis, or a **percentage off** the list price, from a **minimum quantity**, which gives
tiers ("10+ pieces: −12 %").

The resolver starts from the variant's retail price in RON. In another currency it asks
`Currency\Contracts\Currencies::toDisplay()`, which uses the manual rate or the newest BNR rate, adds the
currency's markup and applies its rounding, unless a list gives a fixed price in that currency. A currency that
isn't enabled gives no price. Then it takes the **lowest** price among the
items of every list that applies to the buyer and the quantity. Fixed net prices stay exact, and VAT is added on top;
gross prices are split per line as described in [Money, VAT and exchange rates](money-and-vat.md). In a currency
with no known rate, `price()` returns null rather than guess.

## Price history (Omnibus)

An announced price cut has to show the lowest price of the 30 days before it (Directive 98/6/EC art. 6a). Pricing
keeps one row per variant and day in `price_history`: the lowest retail price (RON, VAT included) that applied that
day.

- When a variant's price is set or changed, Catalog dispatches `Catalog\Data\VariantPriceChanged`, and Pricing
  records it in the same request. A second change on the same day keeps the lower price.
- `pricing:snapshot-prices` records every variant at 00:05, so each day has its row even when nothing changed.
- `Prices::lowestBefore($variantId, $since)` returns the lowest price of the 30 days before `$since`, leaving out the
  day of the cut itself. The storefront shows it beside every strikethrough price, and computes the discount
  percentage against it.

Promotions, when they come, record their reduced prices in the same history.

