# Network features: setup notes

## 1. Upgrade an existing install
```bash
mysql -u root -p billing < database/migrations/20261110_network_features.sql
crontab -e
* * * * *   php /var/www/billing/cron/expire.php
* * * * *   php /var/www/billing/cron/monitor.php     # router health + offline alerts
*/5 * * * * php /var/www/billing/cron/usage.php       # FUP usage polling
```
Fresh installs: `schema.sql` already includes the migration. Existing non-Admin roles do not get the new permissions automatically: tick `sessions.*` and `onu.*` under **Staff & roles**.

## 2. FUP / data caps
- On a plan set **Data cap (GB)** and **Speed after cap** (e.g. `1M/1M`). Saving the plan creates the normal profile and a `<profile>-fup` twin on the router.
- `cron/usage.php` reads cumulative byte counters (PPPoE `<pppoe-user>` interface, hotspot user bytes, static queue bytes), adds the delta to the customer's usage, and when the cap is reached switches them to the FUP profile/queue and drops their session so it applies immediately.
- A new payment (or **Extend**) resets usage and restores full speed. **Reset speed** on the customer row does it manually.
- Accuracy: polled every 5 min; traffic between the last poll and a session ending can be missed. Suitable for fair-use, not metered billing.
- No "speed after cap" value means usage is tracked and shown but nobody is throttled.

## 3. Static IP plans (simple queues)
- Plan type **Static IP**. On install, enter the customer's IPv4; the system creates `/queue simple` (`target=<ip>/32`, `max-limit=<plan speed>`).
- Cut-off uses a firewall address-list, so add this rule **once per router** (above your accept rules):
```
/ip firewall filter add chain=forward src-address-list=billing-expired action=drop comment="billing: expired"
/ip firewall filter add chain=forward dst-address-list=billing-expired action=drop comment="billing: expired"
```
  (Optionally add a NAT redirect for `billing-expired` to a "please pay" page.) Without the rule, expired static customers keep their queue and are NOT blocked.
- Static customers can pay through `/api/stk.php` with `account=<username>`, like PPPoE.

## 4. Who's online (Live sessions)
**Who's online** page: pick a router, see PPPoE and hotspot active sessions, **Disconnect** one (needs `sessions.manage`; every disconnect is audit-logged). Reads live from the router on each page load.

## 5. Router health
- `cron/monitor.php` stores CPU %, memory %, and WAN rx/tx (set the router's **WAN interface**, e.g. `ether1`) every minute; 7 days are kept.
- **Router health** page draws 1h-7d graphs. Two missed checks in a row = **offline**: a red alert on the dashboard plus an email to the **Alert email** in Settings; recovery resolves the alert and sends a second email. Email uses PHP `mail()`, so configure a local MTA/SMTP relay.
- Checks run sequentially with a 3 s timeout, so many dead routers can stretch a run past one minute; at that scale split monitoring into a worker.

## 6. GenieACS ONU monitoring
- Settings: NBI URL (usually `http://<host>:7557`), optional user/password, **RX power parameter path** and **RX scale**. RX paths differ by vendor/data model (the default is the TR-098 `WANPONInterfaceConfig.RXPower`; Huawei/ZTE often differ, and some report in 1/100 dBm, so use scale `0.01`). Check a real device in GenieACS to confirm the path.
- Put each customer's **ONU serial** on their record; the **ONUs (FTTH)** page matches ACS devices to customers, shows online/offline (last inform < 15 min), RX power (amber ≤ −25 dBm, red ≤ −27 dBm), and offers **Refresh** and **Reboot** (needs `onu.manage`).
- The ACS must be reachable from this server (VPN/private IP is fine). Loopback, link-local (cloud metadata) and reserved addresses are rejected for routers and the ACS as an SSRF guard.

## Customer accounts (creditors and debtors)
`customers.balance` = credits minus debits. Positive means the customer has paid in advance (creditor); negative means they owe (debtor). Every change is a row in `ledger_entries` with the resulting balance, so a statement always reconciles.
- **Credit**: cash, M-Pesa (manual), bank. Counts as income on the dashboard. Optionally renew the plan straight away.
- **Debit**: charge (installation, equipment) or refund. Refunds cannot take the balance below zero and are deducted from income.
- **Write-off**: forgives a debt; not income.
- **Renew from balance**: pays one period of the plan from the balance. Setting *Credit limit* (`max_debt`, default 0) allows the balance to go negative down to that amount.
- Permissions: `accounts.view`, `accounts.manage`.

## ONU signal, WiFi and passwords (GenieACS)
- ONU list and detail show RX and TX power (weak ≤ -27 dBm, marginal ≤ -25 dBm).
- WiFi name/password for 2.4 and 5 GHz and the ONU admin password are sent as TR-069 `setParameterValues`. A device fault is reported; an offline ONU shows "queued". Passwords are validated (WPA 8-63 characters) and never written to the audit log.
- Settings: pick a preset (generic TR-098, Huawei, ZTE, TR-181) and override any path. Open an ONU and press **Detect parameters** to list the paths that device actually has and save the right ones. The admin-password path is vendor specific and starts empty on purpose.
- Permission: `onu.manage` for changes; `settings.manage` to save detected paths.
