# W1-BE-A2 [FIN] — admission encounters bill to the stay folio, not a stray order (BE)

Read _COMMON.md first, then W1-BE-A's finding: /home/moonui3/public_html/knowledge-base/plans/visit-shell-completion/reports/W1-BE-A.md (section I9).

## The bug
On admission, `EncounterService::open()` fires `EncounterStarted` → listener `OpenServiceOrderForEncounter` creates an empty open ServiceOrder; then `Folio::open()` creates the real stay bill as a second order. `EncounterServiceOrder::findForEncounter()` takes the lowest id (`->first()`) — used by DecideClinicalIntent and the immediate lab/rad paths — so every lab/rad/service ordered on an admission encounter lands on the stray order, not the stay bill. `EncounterResource::billingOrderId()` (id desc → folio) and `GET encounters/{e}/order` (`->first()` → stray) disagree. Dev: 7 of 8 admissions have stray orders (68, 71, 73, 75, 77, 79, 81).

## Fix (orchestrator recommendation — implement, then a Fable [FIN] review follows)
1. ONE resolver: `EncounterServiceOrder::findForEncounter()` returns the admission's folio order (`admissions.service_order_id` or however Folio links it) when the encounter has an admission; otherwise today's behaviour. `GET encounters/{e}/order`, `billingOrderId()`, DecideClinicalIntent, the immediate lab/rad paths and anything else that picks "the encounter's order" must all go through it (grep for every place that finds an encounter's service order).
2. The listener does not open an order for an encounter that belongs to an admission (or opens nothing when Folio will) — check ordering: if the encounter is created before the admission row is linked, make Folio::open reuse the encounter's empty order instead of creating a second one. Pick whichever is robust for both orders of events; explain.
3. Repair command `clinic:repair-admission-orders {--company=} {--dry-run}`: for each admission encounter with a stray order: if the stray order has NO payments/receipts/allocations/invoices/journal postings, move its lines to the folio (keep every line's links — lab requests, rad orders, intents, revenue split rows, insurance coverage rows, whatever references the line or order id — move them consistently), recompute both orders' totals with the existing totals service, and cancel the empty stray (status the app already uses for empty/void orders). If it has any money movement → don't touch, report it. Idempotent. Log per order.
4. Run `--dry-run` on dev, paste the output in your report, then run it for real on dev company 1 and paste the result.
5. Tests: admission encounter → immediate lab order lands on the folio; intent decided on an admission → folio; outpatient unchanged; repair moves lines + links and is idempotent; repair skips an order with a payment.

Money rules: decimal(12,3); never recompute money by hand — use the existing services; no change for outpatient visits.

## Out of scope
FE; lookup / vitals / history files (other agents).
