# Automatic Elderly program IDs

New registrations receive a read-only `AREA-26-00001` style ID from the signed-in user’s `clinic` value:

| Account clinic code | Area |
| --- | --- |
| 71, 72, 81 | HTY |
| 73 | SPT |
| 84 | SDG |
| 74 | TL |

No area is guessed for other clinic codes. The registration form stays available with a configuration message. Set a mapped code on the existing user account, then reopen online. The owner will change `ElderlyEntry@MAM` from its previous clinic code 1; this application patch does not modify users or passwords.

Each area/year has one shared five-digit sequence, starting at 00001 when no numbers exist. It continues above both historical registrations (including deleted rows) and reserved IDs. Existing saved IDs stay unchanged and cannot be edited. Reserved, cancelled and deleted numbers are never recycled; unused reservations can cause gaps. IDs are identifiers, not enrollment counts. Year code remains 26 as requested and is configured explicitly rather than inferred from visit dates.

An authenticated, CSRF-protected reservation endpoint assigns at most ten IDs at once, each tied to a UUID and account. A single new table, `elderly_support_program_ids`, retains these reservations. The endpoint and registration sync use the same row lock on the existing registration sync counter, so parallel users/devices cannot allocate the same number. Reservation does not create a patient record or increment the reporting change feed. Repeating a lost request with the same UUIDs returns the same numbers. A reservation cannot be claimed by another account.

The browser keeps unused numbers in its account-scoped IndexedDB metadata. Only an explicit Save draft or Complete & save action takes a number, after the answers pass validation, in a transaction shared across tabs. Opening, reloading, typing, selecting New draft and reconnecting never take a number or save answers. Enter in an input does not submit the form. Both save buttons are disabled while a save is in progress to prevent duplicate clicks. The area/context is refreshed from the protected MAM session and cached for offline use; unconsumed pools are cleared when its area/year changes. On an explicit save, ten numbers are prepared when the pool is empty online. A browser with no reserved numbers can save an unnumbered offline draft. An exhausted offline browser can still save an unnumbered draft and a linked visit; parent-first sync assigns the registration ID and supplies the same code to the new follow-up. Completion requires a program ID, so sync that draft before completing it. Existing visit identities remain unchanged.

The server owns IDs regardless of submitted field values. It keeps the existing saved ID on edits and assigns a reservation on new creates, including older frozen requests. Operation IDs, original request fingerprints and idempotent receipts remain unchanged. No bulk rewrite of registration/follow-up payloads is performed. IDs reserved for unsaved drafts can be abandoned safely; they are not reused on another person.

General / clinic ID, when supplied in either form, must contain exactly 10, 11 or 12 ASCII digits. It remains optional under the existing completion rules. It is stored and exported as text so leading zeros survive. Signs, spaces, decimal points, exponent notation and letters are rejected by both browser and server. Old records can still be viewed/exported unchanged; an invalid historical ID must be corrected before saving/syncing an edit.

Only migration `2026_10_17_000300_create_elderly_support_program_ids_table.php` is needed, explicitly on `MAM_NAP`. Existing tables are not altered. No environment or service configuration change is required. Apply the migration before enabling updated sync/controller code. The public PWA shells contain no user clinic or reservations; those are read only from the authorized session/API. No reservation response or case data enters Cache Storage.

Verification uses a guarded disposable MySQL socket: `tests/elderly-support-program-ids.php` (including parallel processes), `tests/elderly-support-program-ids-browser.mjs` (mapping, tabs, offline, lost response and exhausted pool), existing registration/follow-up regressions and shared core tests. Production smoke checks use no synthetic case writes or ID allocations. Dictionary version 1.2 describes registration schema 6 and follow-up schema 3.
