# Shared

## Decimal arithmetic

Use `Add`, `Subtract`, `Multiply`, and `Divide` from the package root (implemented
in `src/shared/math-operations.ts`) for ordinary application arithmetic. These
functions normalize absent or invalid inputs to zero via `GetNumber` and return
JavaScript numbers. `Divide` throws on division by zero. Keep callers dependent
on these functions rather than importing the underlying decimal library; a
library change can then be made in the shared implementation.

Exact-money, authority, and tax calculations have stricter input and precision
rules. Some of their operations still use `big.js` directly in
`src/accounting/discounts/` and `src/tax/`. Do not replace those operations with
the number-returning helpers unless equivalent exact parsing, comparison, and
rounding APIs are available. The old `src/aggregation/aggregation.ts` helpers
were duplicates and have been removed.

## Money precision

Every money figure stored on a document -- a line amount, a discount, a
`RecDisc`, each tax component on each line -- is rounded exactly once, half away
from zero, to the currency's minor unit (`RoundMoney`); nothing in between is
rounded. Line tax goes through `CalculateLineTaxAmounts`, whichever entry point
reaches it (`CalculateLineTax`, `ApplyRecordDiscountAndTaxes`,
`RecalculateLineTaxes`), so a line's tax does not depend on which screen
recalculated it. The policy and its reasons are in MainAPI
`docs/common/accounting/numbers-and-discounts.md#invoice-money-precision`.

## Plan limits on capabilities

`FeatureModeAllows` and `IsAllowedByPlan` (`src/authorization/capability-plan.ts`)
decide whether an entity's `Config.Features` allows a feature use or a
capability key. MainAPI enforces with them and MainUI hides with them, so the
two cannot disagree. The rule table is not in this package: MainAPI owns it and
MainUI generates it, and both pass it in. A key with no rule entry is denied;
MainAPI exempts its platform keys before calling. See MainAPI
`docs/planning/login-authz-payload.md`.
