# Revert Purchase Implementation Plan

**Goal:** Cancel an incorrect purchase and reverse its inventory and recorded payments while preserving its history.

**Architecture:** PurchaseRevertUtil performs an atomic reversal. PurchaseController supplies the authorized POST endpoint and read-only cancelled list behavior. PurchaseLocationUtil exposes the existing stock guard for reversal; payment reversal uses TransactionPayment::deletePayment and its listeners.

**Tech stack:** Laravel 12, PHP 8.3, jQuery, PHPUnit 11, SQLite test fixtures.

## Constraints

- Preserve the user-selected cancellation and payment reversal behavior.
- Keep cancelled records visible and exclude them from active purchase accounting using cancelled_purchase type.
- No actual production transactions are reverted during implementation.
- No database migration. Repository metadata is unusable, so do not commit or create a worktree.

## Task 1: Atomic reversal

Files: app/Utils/PurchaseRevertUtil.php, app/Utils/PurchaseLocationUtil.php, app/Transaction.php, tests/Feature/PurchaseLocationEditTest.php.

- [x] Add endpoint tests using existing purchase fixtures: POST /purchases/{id}/revert with reason, assert cancelled_purchase/cancelled, old quantity 12 becomes 7, destination remains 3 and repeat request has no second deduction. Add payment, parent allocation, stock, serial, authorization and rollback cases.
- [x] Run `php vendor/phpunit/phpunit/phpunit tests/Feature/PurchaseLocationEditTest.php --filter revert --no-progress` and confirm missing endpoint failures.
- [x] Add PurchaseRevertUtil::revert(Transaction $purchase, string $reason): void. Called inside DB::transaction after receipt lock, it validates business-owned payments/parents, returns and stock; deletes payments through existing helper; reverses received stock/serials; releases PO quantities; archives type/status; records reason and safe snapshots. Expose PurchaseLocationUtil::validateReversal(Transaction $purchase, string $field = 'purchase_id'): void.
- [x] Run tests, fix failures, and retain existing purchase location behavior.

## Task 2: Endpoint and user flow

Files: routes/web.php, app/Http/Controllers/PurchaseController.php, app/Http/Controllers/TransactionPaymentController.php, app/Utils/TransactionUtil.php, resources/views/purchase/index.blade.php, public/js/purchase_revert.js, lang/en/purchase.php, tests/js/purchase_revert.test.js.

- [x] Register authenticated POST /purchases/{id}/revert. Validate reason, enforce purchase.delete and permitted location; enforce delete_purchase_payment inside reversal if payments exist.
- [x] Include archived purchases in getListPurchases only. Retain original total for reference but show cancelled rows with zero active total contribution/due and no modifying actions. Add cancelled filter and prevent edit, status, delete and payment endpoints from modifying archived records.
- [x] Add Actions > Revert Purchase with a confirmation modal, required reason and explicit stock/payment explanation. Submit POST with CSRF token, disable while saving, display server error and allow retry, reload table on success.
- [x] Exercise JavaScript submit, cancel, retry and duplicate-click behavior with node's test runner using the actual script and a small jQuery stub. Compile/lint affected Blade files and lint PHP/JS.

## Task 3: Verify and deliver

- [x] Run reversal/location tests plus edit-window, inventory-count and serial regression suites. Request read-only code review and fix relevant findings.
- [x] Package changed production files with the prior edit/location fix and README deployment steps. Hash-check copied files and list ZIP entries. Report local test results and clarify hosted site still requires upload.

