Payment -> prescription business order links: backend verification
Date: 2026-10-22

Implementation
- Both admin payment list rows and detail return prescription_orders: [{id: int, order_no: string}], or [].
- Shared lookup: server/app/adminapi/service/order/PaymentPrescriptionOrderService.php.
- Lookup runs after the existing permission/search/data scope/pagination query; count() is unchanged.
- Exactly one additional joined SELECT for a nonempty payment page; no lookup for an empty page.
- Query is bounded to IDs of payments on that page; prescription orders are joined by the actual junction.
- Result is deduplicated by prescription business order ID, ordered by business order ID, and projects only id/order_no.
- Soft-deleted prescription business orders and dangling junction targets are excluded.
- Cancelled/refunded business orders and payments retain display links while the junction still exists.
- Existing Order model soft-delete scopes keep deleted payments absent from both list and detail.
- No payment mutation, permission change, count/pagination change, migration, or business-data connection was introduced.

Legacy association decision
- tcm_prescription_order_pay_order is the canonical relationship used by existing business order readers/mutations.
- Existing migration 2026_04_09_prescription_order_pay_links.sql already imports valid linked_pay_order_id rows idempotently.
- No runtime fallback to linked_pay_order_id: it can be stale, including after the existing payment split path.
- Current unlink/clear mutations clear the primary legacy field, but an old/stale value must not resurrect a removed junction.
- Regression covers positive migrated legacy history, a legacy-only stale field, explicit unlink then stale primary, and a real split transfer.
- A deployment must already have the existing April association-table migration applied, as other existing order features require.

Isolation and cleanup
- PHP 8.2.9 with project's installed ThinkPHP/ThinkORM vendor runtime.
- MySQL 5.7.26 started with --no-defaults on 127.0.0.1:13429 using a freshly initialized disposable data directory.
- Application initialize(), .env, production database configuration, and server/config/project.php were not loaded/read.
- Tests create random named databases and DROP only those databases in finally blocks.
- Database process 19164 was shut down with mysqladmin; subsequent checks found no process or listening port 13429.
- Automatic approval review rejected recursive deletion of the verified exact directory
  D:\web\zyt\artifacts\payment-prescription-links\mysql-runtime
  with stated reason "blocked by policy". No additional reason was supplied. Temporary runtime files remain there.
- Artifact-local .gitignore excludes mysql-runtime/ to prevent accidental staging. No approval bypass was attempted.
- Temporary instrumented regression test copies were removed.

Passed checks
1. ZYT_PAYMENT_LINK_TEST_MYSQL_PORT=13429 php tests/PaymentPrescriptionOrderLinksTest.php
   PASS (52 checks), executing real OrderLists::lists/count, OrderLogic::detail,
   PrescriptionOrderLogic::unlinkPayOrder and OrderLogic::split with the actual ORM.
   Coverage: multiple payment rows, one/multiple/no associations, duplicate rows,
   same patient without a relation, deleted/dangling business orders, deleted payments,
   status/search/creator permission, page order/offset/count, one query restricted to page IDs,
   typed/minimal payload, list/detail consistency, empty page no-query, migrated legacy history,
   stale primary/unlink behavior, cancelled/refunded history, and split transfer to every child.
2. ZYT_UNLINK_TEST_MYSQL_PORT=13429 php tests/PrescriptionOrderUnlinkPayOrderTest.php
   PASS (131 assertions), original unmodified test.
3. php tests/OrderEditTimeContractTest.php
   PASS (Order edit time contract: OK), original unmodified test.
4. PHP lint: OrderLists.php, OrderLogic.php, PaymentPrescriptionOrderService.php,
   PaymentPrescriptionOrderLinksTest.php: all PASS.
5. Scoped git diff --check: PASS after parent mechanically normalized only added/changed
   OrderLogic.php lines to LF, preserving the existing unchanged CRLF lines.
   PHP lint for OrderLogic.php also passed after that line-ending correction.

Existing test bootstrap limitation and supplementary validation
- Original PrescriptionOrderReleaseAndTimeTest.php and PrescriptionOrderTrackingAuditTest.php
  initially failed in RevisitDueHook's warning path: Unable to resolve NULL driver for [think\Log].
  Their isolated empty config has no logger; the revisit hook hits fixture schema it does not define.
- Re-ran temporary copies in the same tests directory with exactly one additional bootstrap statement,
  immediately after the existing empty config binding:
    Container::getInstance()->instance('log', new \Psr\Log\NullLogger());
- No production method/schema/assertion was modified, and original tests remain unchanged.
- ReleaseAndTime: PASS (112 assertions); TrackingAudit: PASS (196 assertions).
- These two supplementary passes validate their original order behavior but do not validate revisit hooks.

Verification limit
- Testing used synthetic disposable fixtures, not production data or a deployed authenticated HTTP session.
- Existing frontend verification is recorded separately in frontend-verification.txt.
