# Money, VAT, exchange rates and addresses

The building blocks every price, order and invoice is made from. None of them touches a float.

## Money

An amount is a `Brick\Money\Money`: an exact decimal plus its currency. In the database it is two columns, an
integer of minor units (bani, cents) named `*_amount` and a currency code. The `AsMoney` cast joins them:

```php
use SyncShop\Casts\AsMoney;

protected function casts(): array
{
    return [
        'price' => AsMoney::class.':price_amount,currency',  // currency from a column
        'deposit' => AsMoney::class.':deposit_amount,RON',   // always in one currency
    ];
}
```

The cast takes only `Money`. A float, a string or a bare integer throws, and so does a fixed-currency amount
given in another currency.

To show an amount, use `SyncShop\Modules\Currency\Contracts\MoneyFormatter`: `1.249,90 lei` in Romanian,
`1,249.90 lei` in English, and `12,50 EUR` for any currency other than RON. A no-break space keeps the unit
next to the number. Views add the `ss-num` class for tabular figures.

## VAT

Products point at a **VAT class**, and each class has **dated rates** (`vat_classes`, `vat_rates`, in the Pricing
module). A rate applies from its `valid_from` day until the next rate of the same class, so a change in the law
is one new row and every earlier date keeps its rate.

| Class | Until 31 Jul 2025 | From 1 Aug 2025 | e-Factura category |
| --- | --- | --- | --- |
| `standard` (the default) | 19 % | 21 % | S |
| `reduced` | 9 % | 11 % | S |
| `exempt` | 0 % | 0 % | E |

Rates are seeded from 1 January 2017. The old 5 % rate is not modelled, because it merged into 11 %. Orders and
invoices store the VAT they were issued with, so old documents never depend on this table.

```php
$rate = resolve(VatRates::class)->rate('standard', $order->placed_at); // the rate in force that day, or null
$line = $rate->fromGross(Money::of('1249.90', 'RON'));                // retail prices include VAT
$line->net;   // RON 1032.98
$line->vat;   // RON 216.92

$b2b = $rate->fromNet(Money::of('61.90', 'RON'));                     // B2B prices are net
$b2b->gross;  // RON 74.90
```

- The day is read on the shop's clock: an order at 00:30 in Bucharest on 1 August 2025 takes the new rate, even
  when the timestamp is stored in UTC.
- VAT is worked out once per line and rounded half up to the minor unit. Negative amounts round away from zero,
  so a credit note mirrors its invoice. `net + vat = gross` always holds.
- `VatBreakdown::plus()` adds up lines at the same rate, for the per-rate summary on an invoice. It refuses
  lines at different rates.

## Exchange rates

RON is the base currency. `currency:fetch-rates` stores BNR's reference rates at 13:30 on banking days (see
[Scheduled tasks](../admin/scheduled-tasks.md)). Each run reads the last ten banking days, so a missed run fills
itself in. `php artisan currency:fetch-rates --year=2025` loads a whole year from BNR's archive.

`SyncShop\Modules\Currency\Contracts\ExchangeRates` has two lookups, and they are not interchangeable:

- `latest('EUR')` is the newest rate, for **showing** a price in another currency.
- `on('EUR', $issueDate)` is the BNR rate **in force on a day**: the last one published on or before it, so a
  weekend or a holiday takes the previous banking day. Fiscal documents use this one. Cod fiscal art. 319 wants
  an invoice in another currency to show its VAT in RON at the rate of the issue date.

`convert(Money $money, 'HUF', $date)` goes through RON and rounds once, half up, to the target currency's minor
unit. Each lookup returns `null` when no rate is known, and the caller decides what to show instead of guessing.

## Addresses

Platform loads Romania's 42 counties and 13,755 localities from the official SIRUTA register (INS, CC BY 4.0;
see `packages/core/src/Modules/Platform/Database/Data/addresses/README.md`). Every release, the installer and `composer setup`
run `php artisan addresses:sync`. It upserts by official code, so running it again changes nothing.

Other modules store a county id (INS code; București is 40) and a locality id (SIRUTA code). They read names
through `SyncShop\Modules\Platform\Contracts\Addresses`:

```php
$addresses = resolve(Addresses::class);
$cluj = $addresses->countyByCode('CJ');
$addresses->search($cluj->id, 'flore');   // case and diacritics ignored; names that start with the term first
$addresses->byPostcode('400335');          // [Cluj-Napoca]
$addresses->findLocalities([54984, 179141]);
```

A locality also carries the commune or town it belongs to, which tells apart the nine villages called Popești in
Vâlcea. It has one postcode, which for a city with a postcode per street is only the main one. Streets and
street-level postcodes come later, from the courier nomenclatures.
