# Registering an invoice payment for Jonathan (`invoices:register-payment`)

Card #5736. Agents may register a payment on a Nuhet invoice **only when Jonathan asks**, naming the
invoice (number or id) or a card that does, for example "invoice 2026-12345 is paid". Typical case: a
customer on payment_method 2 ("Invoice", in-house invoicing, #254) paid by bank transfer or against a
Visma invoice. The command calls `InvoiceSettlement::registerPayment()`, so the effects equal the admin
"Registrera betalning" button: a `manual` payment row, the invoice marked paid when the payments reach
the total, a package invoice extends `expire_date` and posts the "betalning mottagen" notice, and an
account suspended for non-payment is unsuspended.

## Commands

Run from `/var/www/html`. Never run `config:cache`.

```text
sudo -u www-data -- php8.3 artisan invoices:register-payment --id=<invoices.id> | --number=<invoices.number> [options]
sudo -u www-data -- php8.3 artisan invoices:open [--company=<id>] [--elsewhere] [--format=json|table]
```

| Option | Meaning |
| --- | --- |
| `--id=` / `--number=` | Exactly one. A number that matches more than one invoice is refused (use `--id`). |
| `--amount=` | Kronor including VAT, `123.45` or `123,45`. Default: the remaining amount. A smaller amount is a part payment and leaves the invoice unpaid. |
| `--reference=` | Visma invoice number or bank reference, at most 28 characters. Saved as `invoice_payments.transaction_number` = `cli-<reference>` (without a reference: `cli-<yymmddHHMMSS>`), so a CLI payment is recognisable. Also logged. |
| `--note="Jonathan, card #NNNN"` | Who asked and on which card. Required with `--commit`; logged (not stored on the payment). |
| `--commit` | Register the payment. **Without it the command is a dry run and changes nothing.** |

Output is JSON in both modes: invoice id, number, company, payment_method (and `invoice_elsewhere`),
status, credited, paid, due date, total incl. VAT, paid so far, remaining, the amount to register,
and `result`: `would_register_paid` / `would_register_partial` for a dry run, or `paid` / `partial`
after `--commit`. A commit also shows the company's `status` and `expire_date` before and after; a dry run shows the current values twice and does not preview the extension. `warnings` lists oddities such as a deleted company (the invoice is then marked paid but no account is extended or reactivated).
Exit code 1 means a refusal or bad input (`refused_paid`, `refused_credited`, `refused_amount`,
`not_found`, `ambiguous`, `selector`, `invalid_amount`, `invalid_reference`, `refused_duplicate_reference`, `missing_note`); nothing is
written then.

`invoices:open` is read-only: unpaid, uncredited, ordinary invoices, oldest due date first, with
remaining amount and days overdue. `--elsewhere` limits it to payment_method 2.

## Procedure

1. **Scope.** Confirm the request names the invoice. Run `invoices:open --company=<id>` or `--elsewhere`
   if you must find it, and check the company name against what Jonathan said. If several invoices fit, ask.
2. **Dry run** on the Nuhet host, as the web user, from `/var/www/html`:
   `sudo -u www-data -- php8.3 artisan invoices:register-payment --number=2026-12345`
   Check the company, total and remaining amount against Jonathan's words. Use `--amount` only when he
   named a part payment.
3. **Commit** with the same arguments plus `--note="Jonathan, card #NNNN" --reference="..." --commit`.
   Each production command is approved on the confirm-required host.
4. **Check** the printed `result` (`paid`), and for a package invoice the new `expire_date`.
5. **Card.** Put the invoice number, company id, amount, reference, result and the expire_date before
   and after in the agent comment.

## Notes

- A registered payment **cannot be undone from the command**. Removing a wrong payment (and
  reopening the invoice) needs Jonathan's decision and a manual database change; do not improvise one.
- **Uncertain outcome (for example an SSH timeout).** Do not rerun `--commit` blindly. First rerun the
  same command **without** `--commit`: the dry run shows `paid_so_far`, `remaining` and, when
  `--reference` was given and that payment exists, `would_refuse_duplicate_reference` with the existing
  payment's sum and time. Always pass `--reference` on a commit: a retried commit with the same reference
  on the same invoice is refused (`refused_duplicate_reference`, nothing written), so it is safe. Without a
  reference a retry of a part payment registers a second payment. A repeated full payment is refused as
  `refused_paid` either way, so the account is never extended twice.
- An unpaid invoice whose payments already cover the total is refused as `refused_amount` (nothing is left to register); it needs Jonathan, since the cron or an admin settles it.
- `--reference` accepts printable ASCII only.
- The commands are not scheduled.
