# POS Return-Only Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Allow a cashier to finalize returned receipt items without adding a replacement product.

**Architecture:** `SellPosController::store()` detects a return-only request before the normal sale-products gate and delegates it to a focused transaction method. That method reuses existing sell-return, payment, customer-credit, cash-register, and receipt infrastructure so the return is atomic and normal exchanges remain unchanged.

**Tech Stack:** Laravel/PHP, PHPUnit, jQuery POS form, MySQL transaction models.

## Global Constraints

- Do not create a replacement `sell` transaction for return-only requests.
- Preserve the current exchange-with-replacement workflow.
- Use the existing `exchange_refund_method` values `cash` and `store_credit`.
- Store credit requires a named customer.
- All database mutations must commit or roll back together.
- This workspace is not recognized as a usable Git worktree, so verification replaces commit steps.

---

### Task 1: Return-only regression contract

**Files:**
- Create: `tests/Unit/PosReturnOnlyWorkflowTest.php`
- Modify: `app/Http/Controllers/SellPosController.php`

**Interfaces:**
- Consumes: POS fields `exchange_parent_id`, `exchange_returns`, `exchange_refund_method`, and `status`.
- Produces: `isPosReturnOnly(array $input): bool` and `processPosReturnOnly(Request $request, array $input, int $business_id): array`.

- [x] **Step 1: Write the failing test**

```php
public function test_return_only_is_processed_before_the_products_gate(): void
{
    $controller = file_get_contents(__DIR__.'/../../app/Http/Controllers/SellPosController.php');
    $returnOnly = strpos($controller, 'if ($this->isPosReturnOnly($input))');
    $products = strpos($controller, "if (!empty(\$input['products']))");

    $this->assertNotFalse($returnOnly);
    $this->assertLessThan($products, $returnOnly);
    $this->assertStringContainsString('processPosReturnOnly', $controller);
}
```

- [x] **Step 2: Run the focused test and confirm RED**

Run: `vendor/bin/phpunit tests/Unit/PosReturnOnlyWorkflowTest.php`

Expected: failure because `isPosReturnOnly` and `processPosReturnOnly` do not exist.

- [x] **Step 3: Add the return-only dispatcher and processor**

```php
if ($this->isPosReturnOnly($input)) {
    $output = $this->processPosReturnOnly($request, $input, $business_id);
} elseif (!empty($input['products'])) {
    // existing sale and exchange workflow
}
```

The processor must validate `sell.exchange_replacement`, load the finalized parent sale, reject quantities above the remaining sold quantity, call `addSellReturn`, issue the full refund through `createOrUpdatePaymentLines`, update either customer credit or the cash register, update payment status, render the receipt, and commit.

- [x] **Step 4: Run the focused test and confirm GREEN**

Run: `vendor/bin/phpunit tests/Unit/PosReturnOnlyWorkflowTest.php`

Expected: all return-only contract assertions pass.

### Task 2: Sell-return receipt and complete verification

**Files:**
- Modify: `app/Http/Controllers/SellPosController.php`
- Modify: `app/Http/Controllers/ManagerApprovalController.php`
- Modify: `app/Utils/TransactionUtil.php`
- Modify: `app/Utils/ProductSerialUtil.php`
- Modify: `public/js/pos.js`
- Modify: `config/constants.php`
- Copy: `08122026_pos_exchange_return_serial_edit_fix/app/Http/Controllers/SellPosController.php`
- Copy: `08122026_pos_exchange_return_serial_edit_fix/app/Http/Controllers/ManagerApprovalController.php`
- Copy: `08122026_pos_exchange_return_serial_edit_fix/app/Utils/TransactionUtil.php`
- Copy: `08122026_pos_exchange_return_serial_edit_fix/app/Utils/ProductSerialUtil.php`
- Copy: `08122026_pos_exchange_return_serial_edit_fix/public/js/pos.js`
- Copy: `08122026_pos_exchange_return_serial_edit_fix/config/constants.php`

**Interfaces:**
- Consumes: sell-return transaction type from `Transaction::find($transaction_id)`.
- Produces: POS response with `success`, `msg`, and a receipt rendered with `sell_return.receipt`.

- [x] **Step 1: Extend the regression test for receipt and refund effects**

```php
$this->assertStringContainsString("'sell_return.receipt'", $controller);
$this->assertStringContainsString('updateRefundPayments', $processor);
$this->assertStringContainsString('updateContactBalance', $processor);
$this->assertStringContainsString('updatePaymentStatus', $processor);
```

- [x] **Step 2: Run the focused test and confirm RED for missing receipt selection**

Run: `vendor/bin/phpunit tests/Unit/PosReturnOnlyWorkflowTest.php`

Expected: failure until the POS receipt builder selects the sell-return view.

- [x] **Step 3: Render the correct receipt and bump the asset version**

```php
$layout = $transaction && $transaction->type === 'sell_return'
    ? 'sell_return.receipt'
    : (!empty($receipt_details->design) ? 'sale_pos.receipts.'.$receipt_details->design : 'sale_pos.receipts.classic');
```

Increase `config/constants.php` asset version from `631` to `632` so deployed clients receive current POS assets.

- [x] **Step 4: Run focused and complete checks**

Run: `vendor/bin/phpunit tests/Unit/PosReturnOnlyWorkflowTest.php`

Run: `vendor/bin/phpunit`

Run: `node --test tests/js/pos_exchange_return_serial.test.js`

Expected: all tests pass without failures.

- [x] **Step 5: Refresh and verify deployment files**

Copy the modified controller and constants file into `08122026_pos_exchange_return_serial_edit_fix`, preserving their project-relative paths, then compare SHA-256 hashes against the workspace originals. Expected: each source/copy hash pair is identical.
