# Partial Bill Quantity Correction

Allows **ADMIN** to reduce bill line quantities or remove unsold lines on consumed bills via the existing cost-correction path (`PATCH /bills/{id}`).

Full API details: [api/bills.md](./api/bills.md#cost-correction-consumed-inventory).

## Rules (v1)

| Allowed | Blocked |
|---------|---------|
| Qty decrease when `new_qty >= sold_qty` (per line) | Qty increase after any consumption |
| Remove line when `sold_qty = 0` (`_remove: true` or `qty: 0`) | Remove line with sales or backorder clearance |
| Price/tax/memo changes | `item_id` change |
| Reduce unconsumed lines while other lines sold | Remove all lines (at least one must remain) |
| Add new lines (append) — see below | Qty change on backorder-cleared lines |
| | Changing an existing line while appending new lines |

## Adding new lines (append)

**ADMIN** can add the items that were left off a consumed bill in the same `PATCH /bills/{id}` request by including the existing lines (by `id`, unchanged) plus one or more rows **without** an `id`. This routes to **line append**: the new rows are receipted into inventory on their own FIFO layers at the bill's original `txn_date`, and a single balanced delta journal entry posts Inventory Dr / Input VAT Dr / Accounts Payable Cr for the appended amount. Existing lines and sold invoice COGS are untouched.

Constraints: `OPEN`, unpaid, `txn_date` not in a closed period; existing lines must be present and unchanged (change them in a separate cost-correction request). Appended layers are added at the receipt date and are not retroactively ordered ahead of earlier sales. Full API details: [api/bills.md](./api/bills.md#appending-lines-consumed-inventory).

**Cost correction** requires `lines[].id` on every row (from `GET /bills/{id}`). Array order is ignored; each payload row is matched to the bill line with that id.

`sold_qty` = sum of `(qty_purchased - qty_remaining)` on FIFO layers for that `bill_line_id`.

## Example — qty reduction

Bill: Item A ×10, Item B ×10, Item C ×10 → receive → invoice Item A ×5 → PATCH bill with Item C qty 5:

- Item A line locked at qty 10 (5 sold)
- Item C reduced to 5 (0 sold) — inventory and A/P decrease

## Example — line removal

Same bill, remove mistaken Item C line (0 sold) while Item A has sales:

```json
{
  "lines": [
    { "id": 40, "item_id": "<A>", "qty": 10, "unit_price": 50 },
    { "id": 41, "item_id": "<B>", "qty": 10, "unit_price": 50 },
    { "id": 42, "_remove": true, "item_id": "<C>" }
  ]
}
```

- Item C line deleted; `qty_on_hand` for Item C returns to 0 (no 0.0001 dust)
- Bill total and vendor A/P decrease by Item C line total

## Implementation

- `BillEditModeResolver` — single source of truth mapping a PATCH payload to `DRAFT_UPDATE` / `AMEND` / `COST_CORRECTION` / `APPEND` (shared by controller, request, preview)
- `BillCorrectionLineResolver` — validates id-set and indexes payload by bill line id
- `BillLineConsumptionService` — per-line sold/on-hand math
- `BillLineRemovalService` — delete unconsumed FIFO layers and bill lines
- `BillLineQtyValidator` — request validation
- `BillCostCorrectionService` — shrinks FIFO layers, removes lines, posts delta GL
- `BillLineAppendService` — inserts new lines, receipts them into inventory (reusing `BillProcessingService::receiveInventoryForLines`), posts one balanced A/P delta GL
- `BillCostCorrectionImpactCalculator` — preview with `line_impacts` (`action: removed` for correction, `action: added` for append)
