{
  "ursId": "URS-016",
  "title": "Auto-decrement Inventory After Order Submission",
  "timestamp": "2026-08-03T23:02:02.369Z",
  "durationMs": 134842,
  "config": {
    "inboxUrl": "http://localhost:34059",
    "dbHost": "localhost",
    "dbPort": 40721,
    "dbName": "cc_repinbox_dev"
  },
  "setup": {
    "status": "pass"
  },
  "scenarios": [
    {
      "name": "Step 1: Initial inventory",
      "status": "pass",
      "description": "Step 1: Initial inventory",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-01-logged-in.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-01-initial-inventory.png"
      ],
      "explanation": "Logs in as a sales rep (Blair Bennett) and navigates to TRUNK-36 to record the starting quantities for NovaPatch and LiraLock. This establishes the known baseline so subsequent steps can prove the correct delta was applied after each order.",
      "expectedAuditActions": [
        "user_log:user:login"
      ],
      "startedAt": "2026-08-03T23:02:11.978Z",
      "finishedAt": "2026-08-03T23:02:15.815Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/videos/step-01-initial-inventory.webm"
    },
    {
      "name": "Step 2: First order",
      "status": "pass",
      "description": "Step 2: First order",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-02-account-selected.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-02-devices-selected.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-02-documents.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-02-review.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-02-order-submitted.png"
      ],
      "explanation": "Creates and submits a bill-only order for 3 NovaPatch and 2 LiraLock devices from trunk inventory. This exercises the full order-submission flow that should trigger an automatic inventory decrement in the database.",
      "expectedEmailTemplates": [
        "New Bill-Only Order"
      ],
      "expectedAuditActions": [
        "decision:bill_only_order.inventory_items_decrement"
      ],
      "startedAt": "2026-08-03T23:02:29.442Z",
      "finishedAt": "2026-08-03T23:02:52.107Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/videos/step-02-first-order.webm"
    },
    {
      "name": "Step 3: Verify first decrement",
      "status": "pass",
      "description": "Step 3: Verify first decrement",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-03-inventory-after-first-order.png"
      ],
      "explanation": "Navigates to TRUNK-36 immediately after the first order and confirms that NovaPatch and LiraLock quantities have each decreased by the ordered amount. This proves the inventory auto-decrement fires on order submission.",
      "startedAt": "2026-08-03T23:02:58.307Z",
      "finishedAt": "2026-08-03T23:02:58.307Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/videos/step-03-verify-decrement.webm"
    },
    {
      "name": "Step 4: Second order",
      "status": "pass",
      "description": "Step 4: Second order",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-04-second-order-submitted.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-04-inventory-after-second-order.png"
      ],
      "explanation": "Submits a second bill-only order and then re-checks the inventory to verify that quantities reflect both orders cumulatively. This proves the decrement is applied consistently across multiple successive submissions.",
      "expectedEmailTemplates": [
        "New Bill-Only Order"
      ],
      "expectedAuditActions": [
        "decision:bill_only_order.inventory_items_decrement"
      ],
      "startedAt": "2026-08-03T23:03:33.604Z",
      "finishedAt": "2026-08-03T23:03:36.475Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/videos/step-04-second-order.webm"
    },
    {
      "name": "Step 5: Shortage edge case",
      "status": "pass",
      "description": "Step 5: Shortage edge case",
      "screenshots": [
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-05-shortage-warning.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-05-review-with-shortage.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-05-shortage-result.png",
        "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/step-05-final-inventory.png"
      ],
      "explanation": "Orders more NovaPatch units than are available in trunk stock. The system permits submission in best-effort mode and consumes all remaining units, bringing the count to zero. This proves the system handles inventory shortages without blocking the order while still tracking the discrepancy.",
      "expectedEmailTemplates": [
        "New Bill-Only Order"
      ],
      "startedAt": "2026-08-03T23:04:02.400Z",
      "finishedAt": "2026-08-03T23:04:13.507Z",
      "videoPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/videos/step-05-shortage-edge-case.webm"
    }
  ],
  "dbValidations": [
    {
      "name": "Inventory items exist at trunk",
      "query": "SELECT ii.id, ii.di, ii.lot, ii.quantity_ready, ii.quantity_backordered, ii.quantity_in_transit\n      FROM inventory_items ii\n      WHERE ii.real_world_location_id = $1\n        AND ii.lot IN ($2, $3)\n      ORDER BY ii.lot",
      "status": "pass",
      "rows": [
        {
          "id": "b2ac20b4-7847-451a-ab44-8df797fe697e",
          "di": "07649988202247",
          "lot": "URS016-FL-001",
          "quantity_ready": 12,
          "quantity_backordered": 0,
          "quantity_in_transit": 0
        },
        {
          "id": "66a853cb-377b-4851-a3a9-32e9cac716c9",
          "di": "07649988202230",
          "lot": "URS016-SP-001",
          "quantity_ready": 0,
          "quantity_backordered": 0,
          "quantity_in_transit": 0
        }
      ],
      "assertion": "Both test inventory items (NovaPatch and LiraLock) should exist at TRUNK-36"
    },
    {
      "name": "Inventory quantities decremented",
      "query": "SELECT ii.lot, ii.quantity_ready,\n        CASE\n          WHEN ii.lot = $2 THEN 20\n          WHEN ii.lot = $3 THEN 15\n        END as initial_quantity,\n        CASE\n          WHEN ii.lot = $2 THEN 20 - ii.quantity_ready\n          WHEN ii.lot = $3 THEN 15 - ii.quantity_ready\n        END as total_decremented\n      FROM inventory_items ii\n      WHERE ii.real_world_location_id = $1\n        AND ii.lot IN ($2, $3)\n      ORDER BY ii.lot",
      "status": "pass",
      "rows": [
        {
          "lot": "URS016-FL-001",
          "quantity_ready": 12,
          "initial_quantity": 15,
          "total_decremented": 3
        },
        {
          "lot": "URS016-SP-001",
          "quantity_ready": 0,
          "initial_quantity": 20,
          "total_decremented": 20
        }
      ],
      "assertion": "Both inventory items should have quantity_ready less than their initial values"
    },
    {
      "name": "Recent billing orders created",
      "query": "SELECT bo.id, bo.order_number, bo.status, bo.procedure_date,\n        bo.created_at, bo.created_by_user_id\n      FROM billing_orders bo\n      WHERE bo.created_by_user_id = $1\n        AND bo.created_at > NOW() - INTERVAL '30 minutes'\n      ORDER BY bo.created_at DESC\n      LIMIT 5",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "order_number": "BO-3",
          "status": "submitted",
          "procedure_date": "2026-08-03T00:00:00.000Z",
          "created_at": "2026-08-03T23:04:07.275Z",
          "created_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456"
        },
        {
          "id": "019fc9de-3688-7643-b07f-cf1cb081ad88",
          "order_number": "BO-2",
          "status": "submitted",
          "procedure_date": "2026-08-03T00:00:00.000Z",
          "created_at": "2026-08-03T23:03:31.146Z",
          "created_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456"
        },
        {
          "id": "019fc9dd-93a6-7041-a007-6be6b642e70c",
          "order_number": "BO-1",
          "status": "submitted",
          "procedure_date": "2026-08-03T00:00:00.000Z",
          "created_at": "2026-08-03T23:02:49.431Z",
          "created_by_user_id": "17b8c9d0-e1f2-3456-1234-567890123456"
        }
      ],
      "assertion": "At least one billing order should have been created by Blair Bennett in the last 30 minutes"
    },
    {
      "name": "Billing order items reference correct products",
      "query": "SELECT boi.id, boi.billing_order_id, op.title as product_name,\n        boi.lot_number, boi.serial_number, boi.location_id,\n        boi.inventory_discrepancy\n      FROM billing_order_items boi\n      JOIN org_products op ON boi.product_id = op.id\n      JOIN billing_orders bo ON boi.billing_order_id = bo.id\n      WHERE bo.created_by_user_id = $1\n        AND bo.created_at > NOW() - INTERVAL '30 minutes'\n      ORDER BY bo.created_at DESC, op.title",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-c3a0-7842-80d7-9b1ce21df8fa",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b24b77ec17e",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b253d0dab98",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b26308dc49c",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b27419fc970",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b285b74d29f",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b291d0ca060",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b2a42533610",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b2ba05c37c9",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b18be441e90",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b19389e8ccc",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b1ad3f92fd1",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b1b5554a0bf",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": true
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b1dd886a5b4",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b1e7e837479",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b1f136c82fa",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b2091739c44",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b2144b67639",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b22ed88bc18",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-c3a0-7842-80d7-9b2381b0ff63",
          "billing_order_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-3681-7d1d-9386-18fc9475dd53",
          "billing_order_id": "019fc9de-3688-7643-b07f-cf1cb081ad88",
          "product_name": "LiraLock® Instrument SN",
          "lot_number": "URS016-FL-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-3684-71d9-bcff-6cb7601ebb19",
          "billing_order_id": "019fc9de-3688-7643-b07f-cf1cb081ad88",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9de-3684-71d9-bcff-6cb6099092d3",
          "billing_order_id": "019fc9de-3688-7643-b07f-cf1cb081ad88",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9dd-939e-7c5c-99bf-7d64ce3b7d1f",
          "billing_order_id": "019fc9dd-93a6-7041-a007-6be6b642e70c",
          "product_name": "LiraLock® Instrument SN",
          "lot_number": "URS016-FL-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9dd-939f-7c33-87de-85fe219416c0",
          "billing_order_id": "019fc9dd-93a6-7041-a007-6be6b642e70c",
          "product_name": "LiraLock® Instrument SN",
          "lot_number": "URS016-FL-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9dd-93a1-7e1a-a1b0-4d634be021bb",
          "billing_order_id": "019fc9dd-93a6-7041-a007-6be6b642e70c",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9dd-93a1-7e1a-a1b0-4d62a72ecd8f",
          "billing_order_id": "019fc9dd-93a6-7041-a007-6be6b642e70c",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        },
        {
          "id": "019fc9dd-93a1-7e1a-a1b0-4d64775ad23b",
          "billing_order_id": "019fc9dd-93a6-7041-a007-6be6b642e70c",
          "product_name": "NovaPatch® PET",
          "lot_number": "URS016-SP-001",
          "serial_number": null,
          "location_id": "a6e7f8a9-b0c1-2345-0123-456789012345",
          "inventory_discrepancy": false
        }
      ],
      "assertion": "Billing order items should reference NovaPatch and/or LiraLock products"
    },
    {
      "name": "Inventory history records bill_only_order removals",
      "query": "SELECT ih.id, ih.item_id, ih.quantity, ih.status, ih.info,\n        ih.created_at, ih.inventory_id\n      FROM inventory_history ih\n      WHERE ih.item_id IN (\n        SELECT id FROM inventory_items\n        WHERE real_world_location_id = $1\n          AND lot IN ($2, $3)\n      )\n      AND ih.created_at > NOW() - INTERVAL '30 minutes'\n      AND ih.info->>'action' = 'removed'\n      AND ih.info->>'reason' = 'bill_only_order'\n      ORDER BY ih.created_at DESC\n      LIMIT 20",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-c3b0-7003-be17-e7f4a1609abb",
          "item_id": "66a853cb-377b-4851-a3a9-32e9cac716c9",
          "quantity": 15,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
            "shortageQuantity": 0,
            "requestedQuantity": 15
          },
          "created_at": "2026-08-03T23:04:07.275Z",
          "inventory_id": "fa000001-0000-4000-8000-000000000001"
        },
        {
          "id": "019fc9de-c3b1-74da-8372-26d54305e815",
          "item_id": "66a853cb-377b-4851-a3a9-32e9cac716c9",
          "quantity": 5,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
            "shortageQuantity": 5,
            "requestedQuantity": 20
          },
          "created_at": "2026-08-03T23:04:07.275Z",
          "inventory_id": "fa000001-0000-4000-8000-000000000001"
        },
        {
          "id": "019fc9de-3698-754b-b6b9-b75ae66a3304",
          "item_id": "b2ac20b4-7847-451a-ab44-8df797fe697e",
          "quantity": 1,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019fc9de-3688-7643-b07f-cf1cb081ad88",
            "shortageQuantity": 0,
            "requestedQuantity": 1
          },
          "created_at": "2026-08-03T23:03:31.146Z",
          "inventory_id": "fa000001-0000-4000-8000-000000000001"
        },
        {
          "id": "019fc9de-3698-754b-b6b9-b75b7b89746e",
          "item_id": "66a853cb-377b-4851-a3a9-32e9cac716c9",
          "quantity": 2,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019fc9de-3688-7643-b07f-cf1cb081ad88",
            "shortageQuantity": 0,
            "requestedQuantity": 2
          },
          "created_at": "2026-08-03T23:03:31.146Z",
          "inventory_id": "fa000001-0000-4000-8000-000000000001"
        },
        {
          "id": "019fc9dd-93d0-7c48-bcf3-12ec93f8e63d",
          "item_id": "b2ac20b4-7847-451a-ab44-8df797fe697e",
          "quantity": 2,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019fc9dd-93a6-7041-a007-6be6b642e70c",
            "shortageQuantity": 0,
            "requestedQuantity": 2
          },
          "created_at": "2026-08-03T23:02:49.431Z",
          "inventory_id": "fa000001-0000-4000-8000-000000000001"
        },
        {
          "id": "019fc9dd-93d0-7c48-bcf3-12ed2ef96ea1",
          "item_id": "66a853cb-377b-4851-a3a9-32e9cac716c9",
          "quantity": 3,
          "status": "ready",
          "info": {
            "action": "removed",
            "reason": "bill_only_order",
            "bestEffort": true,
            "billingOrderId": "019fc9dd-93a6-7041-a007-6be6b642e70c",
            "shortageQuantity": 0,
            "requestedQuantity": 3
          },
          "created_at": "2026-08-03T23:02:49.431Z",
          "inventory_id": "fa000001-0000-4000-8000-000000000001"
        }
      ],
      "assertion": "Inventory history should contain records with action=removed and reason=bill_only_order"
    },
    {
      "name": "Inventory transactions created",
      "query": "SELECT it.id, it.source_type, it.source_id, it.created_at,\n        it.distributor_organization_id, it.manufacturer_organization_id\n      FROM inventory_transactions it\n      WHERE it.source_type = 'bill_only_order'\n        AND it.created_at > NOW() - INTERVAL '30 minutes'\n        AND (it.distributor_organization_id = $1 OR it.manufacturer_organization_id = $1)\n      ORDER BY it.created_at DESC\n      LIMIT 10",
      "status": "pass",
      "rows": [
        {
          "id": "019fc9de-c3ad-751b-9472-129573618d8c",
          "source_type": "bill_only_order",
          "source_id": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
          "created_at": "2026-08-03T23:04:07.275Z",
          "distributor_organization_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
          "manufacturer_organization_id": null
        },
        {
          "id": "019fc9de-3694-7735-aebc-196855c07bc5",
          "source_type": "bill_only_order",
          "source_id": "019fc9de-3688-7643-b07f-cf1cb081ad88",
          "created_at": "2026-08-03T23:03:31.146Z",
          "distributor_organization_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
          "manufacturer_organization_id": null
        },
        {
          "id": "019fc9dd-93b2-7126-9b1c-9ea1db1140df",
          "source_type": "bill_only_order",
          "source_id": "019fc9dd-93a6-7041-a007-6be6b642e70c",
          "created_at": "2026-08-03T23:02:49.431Z",
          "distributor_organization_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
          "manufacturer_organization_id": null
        }
      ],
      "assertion": "Inventory transactions should exist with source_type = bill_only_order"
    },
    {
      "name": "No negative inventory quantities",
      "query": "SELECT ii.id, ii.lot, ii.quantity_ready\n      FROM inventory_items ii\n      WHERE ii.real_world_location_id = $1\n        AND ii.lot IN ($2, $3)\n        AND ii.quantity_ready < 0",
      "status": "pass",
      "rows": [],
      "assertion": "Inventory quantities should never go below zero (CHECK constraint enforced)"
    }
  ],
  "overallStatus": "pass",
  "outputDir": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z",
  "auditStartTime": "2026-08-03T23:02:00.689Z",
  "emailEvidence": [
    {
      "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-1",
      "template": "New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-1",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/emails/2026-08-03T23-02-50-400Z-New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-1.png"
    },
    {
      "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-2",
      "template": "New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-2",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/emails/2026-08-03T23-03-33-584Z-New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-2.png"
    },
    {
      "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-3",
      "template": "New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-3",
      "screenshotPath": "/home/runner/_work/code/code/validation_test_results/urs-016-inventory-decrement/2026-08-03T23-02-02-369Z/screenshots/emails/2026-08-03T23-04-08-712Z-New_Bill-Only_Order_-_8_3_2026_-_Vantis_BO-3.png"
    }
  ],
  "auditEventEvidence": [
    {
      "createdAt": "2026-08-03T23:02:09.758Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:19.480Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:02:49.431Z",
      "eventType": "billing_order",
      "action": "status_change",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9dd-93a6-7041-a007-6be6b642e70c",
      "secondaryObjectId": null,
      "payload": {
        "reason": null,
        "toStatus": "submitted",
        "fromStatus": null,
        "automatedAction": false
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:49.431Z",
      "eventType": "decision",
      "action": "bill_only.link_purchase_order",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9dd-93a6-7041-a007-6be6b642e70c",
      "secondaryObjectId": null,
      "payload": {
        "reason": "no_purchase_order_selected",
        "performed": false,
        "entityType": "billing_order",
        "salesOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:49.431Z",
      "eventType": "decision",
      "action": "bill_only_order.inventory_items_decrement",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9dd-93a6-7041-a007-6be6b642e70c",
      "secondaryObjectId": null,
      "payload": {
        "reason": "inventory_items_decremented",
        "itemCount": 5,
        "performed": true,
        "entityType": "billing_order",
        "inventorySource": "inventory_items"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:49.431Z",
      "eventType": "decision",
      "action": "bill_only_order.direct_po_import_on_create",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9dd-93a6-7041-a007-6be6b642e70c",
      "secondaryObjectId": null,
      "payload": {
        "reason": "No uploaded PO documents",
        "performed": false,
        "entityType": "billing_order",
        "fileSourceOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "hasSelectedPurchaseOrderId": false,
        "purchaseOrderDocumentCount": 0
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:50.440Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9dd-93a6-7041-a007-6be6b642e70c",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "blair.bennett@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9dd-972f-7121-ac88-36350d1c1efe/",
        "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-1",
        "messageId": "dev-console-log",
        "relatedEntityType": "billing_order"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:02:55.232Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:03:01.598Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:03:31.146Z",
      "eventType": "decision",
      "action": "bill_only.link_purchase_order",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9de-3688-7643-b07f-cf1cb081ad88",
      "secondaryObjectId": null,
      "payload": {
        "reason": "no_purchase_order_selected",
        "performed": false,
        "entityType": "billing_order",
        "salesOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:31.146Z",
      "eventType": "decision",
      "action": "bill_only_order.inventory_items_decrement",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-3688-7643-b07f-cf1cb081ad88",
      "secondaryObjectId": null,
      "payload": {
        "reason": "inventory_items_decremented",
        "itemCount": 3,
        "performed": true,
        "entityType": "billing_order",
        "inventorySource": "inventory_items"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:31.146Z",
      "eventType": "decision",
      "action": "bill_only_order.direct_po_import_on_create",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-3688-7643-b07f-cf1cb081ad88",
      "secondaryObjectId": null,
      "payload": {
        "reason": "No uploaded PO documents",
        "performed": false,
        "entityType": "billing_order",
        "fileSourceOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "hasSelectedPurchaseOrderId": false,
        "purchaseOrderDocumentCount": 0
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:31.146Z",
      "eventType": "billing_order",
      "action": "status_change",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-3688-7643-b07f-cf1cb081ad88",
      "secondaryObjectId": null,
      "payload": {
        "reason": null,
        "toStatus": "submitted",
        "fromStatus": null,
        "automatedAction": false
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:33.604Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9de-3688-7643-b07f-cf1cb081ad88",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "blair.bennett@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9de-3fd7-7a05-b883-c8e77e2afd30/",
        "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-2",
        "messageId": "dev-console-log",
        "relatedEntityType": "billing_order"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:03:39.609Z",
      "eventType": "user_log",
      "action": "user:login",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": null,
      "secondaryObjectId": null,
      "payload": {
        "email": "blair.bennett@corvetasurgical.com"
      },
      "route": "/login",
      "traceId": "00000000000000000000000000000000"
    },
    {
      "createdAt": "2026-08-03T23:04:07.275Z",
      "eventType": "decision",
      "action": "bill_only_order.direct_po_import_on_create",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
      "secondaryObjectId": null,
      "payload": {
        "reason": "No uploaded PO documents",
        "performed": false,
        "entityType": "billing_order",
        "fileSourceOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "hasSelectedPurchaseOrderId": false,
        "purchaseOrderDocumentCount": 0
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:04:07.275Z",
      "eventType": "billing_order",
      "action": "status_change",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
      "secondaryObjectId": null,
      "payload": {
        "reason": null,
        "toStatus": "submitted",
        "fromStatus": null,
        "automatedAction": false
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:04:07.275Z",
      "eventType": "decision",
      "action": "bill_only_order.inventory_items_decrement",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Vantis",
      "organizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "objectId": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
      "secondaryObjectId": null,
      "payload": {
        "reason": "inventory_items_decremented",
        "itemCount": 20,
        "performed": true,
        "entityType": "billing_order",
        "inventorySource": "inventory_items"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:04:07.275Z",
      "eventType": "decision",
      "action": "bill_only.link_purchase_order",
      "userEmail": "blair.bennett@corvetasurgical.com",
      "userId": "17b8c9d0-e1f2-3456-1234-567890123456",
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
      "secondaryObjectId": null,
      "payload": {
        "reason": "no_purchase_order_selected",
        "performed": false,
        "entityType": "billing_order",
        "salesOrganizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
        "manufacturerOrganizationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      },
      "route": null,
      "traceId": null
    },
    {
      "createdAt": "2026-08-03T23:04:08.765Z",
      "eventType": "transactional_email",
      "action": "new_bill_only",
      "userEmail": null,
      "userId": null,
      "organizationName": "Corveta Surgical Group",
      "organizationId": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
      "objectId": "019fc9de-c3a3-790b-b8e9-2ca0e500b424",
      "secondaryObjectId": null,
      "payload": {
        "to": [
          "blair.bennett@corvetasurgical.com"
        ],
        "s3Path": "email-audit/b2c3d4e5-f6a7-8901-bcde-f12345678901/019fc9de-c913-7e2c-bcf7-d5fdd5a004f7/",
        "subject": "New Bill-Only Order - 8/3/2026 - Vantis BO-3",
        "messageId": "dev-console-log",
        "relatedEntityType": "billing_order"
      },
      "route": null,
      "traceId": null
    }
  ],
  "auditQuery": "SELECT\n    ae.created_at,\n    ae.event_type,\n    ae.action,\n    ae.user_id,\n    u.email AS user_email,\n    ae.organization_id,\n    o.name AS organization_name,\n    ae.object_id,\n    ae.secondary_object_id,\n    ae.payload,\n    ae.route,\n    ae.trace_id\n  FROM audit_events ae\n  LEFT JOIN users u ON u.id = ae.user_id\n  LEFT JOIN organizations o ON o.id = ae.organization_id\n  WHERE ae.created_at >= $1\n    AND ae.organization_id = ANY($2::uuid[])\n  ORDER BY ae.created_at ASC",
  "auditAssertions": [
    {
      "stepName": "Step 1: Initial inventory",
      "expectedAction": "user_log:user:login",
      "found": true
    },
    {
      "stepName": "Step 2: First order",
      "expectedAction": "decision:bill_only_order.inventory_items_decrement",
      "found": true
    },
    {
      "stepName": "Step 4: Second order",
      "expectedAction": "decision:bill_only_order.inventory_items_decrement",
      "found": true
    }
  ],
  "emailAssertions": [
    {
      "stepName": "Step 2: First order",
      "expectedTemplate": "New Bill-Only Order",
      "found": true
    },
    {
      "stepName": "Step 4: Second order",
      "expectedTemplate": "New Bill-Only Order",
      "found": true
    },
    {
      "stepName": "Step 5: Shortage edge case",
      "expectedTemplate": "New Bill-Only Order",
      "found": true
    }
  ]
}