# Update 2026-11-19: settlements and refunds (phase 6)

Stores and driver companies are now paid by **settlements**: one payout per cycle instead of withdraw requests. Customers can now ask for a **refund** of a delivered order. The refund is money only, paid into their wallet.

Who has work to do:
- **Customer app:** the new refund request (section 1).
- **Restaurant app:** settlements replace withdrawals (section 2).
- **Driver app:** no change. Drivers still use withdraw requests.

All responses use the usual wrapper `{ message, data, total? }`. Every text is translated by the `lang` header.

---

## 1. Customer app: refund request

### Rules
- **Only a delivered order** (`status = DELIVERED`) can be refunded.
- **Time window:** the customer can ask within the days the admin set after `deliveredAt`. Setting `refundRequestDays` defaults to 3; empty means no limit.
- **Feature switch:** the admin can turn refund requests off (setting `refundRequest`).
- **One open request per order.** An order is refunded once at most.
- **Admin review:** the admin approves with an amount, or rejects with a reason.
- **On approval:**
  - The amount is added to the customer's **wallet**. The wallet history shows an `ORDER_REFUND` line with reference `refund:{id}`.
  - The order becomes `REFUND`.
  - Nothing is picked up from the customer.

### Refund reasons (existing)
`GET /refund/reason`: the list of reasons to choose from (`id`, `reason`).

### Ask for a refund
`POST /refund/requests` with role `CUSTOMER`, sent as **multipart/form-data**:

| Field | Type | Required | Notes |
|---|---|---|---|
| `orderId` | int | yes | The customer's own delivered order |
| `reasonId` | int | no | From `GET /refund/reason` |
| `note` | string | no | Up to 2000 characters |
| `image` | file | no | A photo |

Response `201` with the request: `{ id, orderId, status: "PENDING", ... }`.

Errors (`400`, translated message):

| Key | When |
|---|---|
| `refund_requests_closed` | The admin turned refund requests off |
| `refund_order_not_delivered` | The order isn't `DELIVERED` |
| `refund_window_passed` | More than N days since delivery |
| `refund_nothing_paid` | The order total was 0 |
| `refund_request_pending` | A request for this order is already waiting |
| `refund_already_done` | The order was already refunded |
| `refund_reason_not_found` | Wrong `reasonId` |

### My refund requests
- `GET /refund/requests?status=&orderId=&page=&limit=`: the customer sees their own requests only.
- `GET /refund/requests/:id`

Each item:

```json
{
  "id": 12,
  "orderId": 1593,
  "status": "PENDING | APPROVED | REJECTED",
  "source": "CUSTOMER | ADMIN",
  "reason": "Damaged item",
  "note": "…",
  "imagePath": "uploads/…",
  "amount": "27.00",
  "rejectReason": null,
  "createdAt": "…",
  "reviewedAt": null,
  "paid": 27
}
```

- `amount` is set when the request is approved.
- `paid` is the most the order can give back: what the customer paid by card or cash, plus wallet.
- `source: ADMIN` means the admin refunded the order without a request (after a phone complaint, for example).

### Push
`data.type = "REFUND_STATUS"`, together with `orderId`, `refundRequestId` and `status` (`APPROVED` / `REJECTED`). On `APPROVED`, refresh the wallet.

### UI suggestion
On a delivered order, show a **"Request a refund"** button while the window is open. If the order has a request, show its status instead. A refunded order shows status `REFUND`.

---

## 2. Restaurant app: settlements replace withdrawals

### What changed
- **`POST /withdraw` returns `403`** (`withdraw_closed_use_settlements`) for a store. Remove the "new withdraw" button. The old requests stay readable at `GET /withdraw`.
- **Automatic settlements:** every cycle (daily, weekly or monthly, set by the admin), the store's unsettled balance becomes one settlement.
  - The admin transfers it to the store's **default bank account** and records the reference and a proof file.
  - A **weekly** cycle ends on **Sunday 00:00** (Asia/Riyadh time).
  - A main store is settled together with its branches. A branch user only sees the branch's own part.
- **Negative net:** if the net is 0 or below (for example, cash pickups), no settlement is made. The debt carries over to the next one.
- **No bank account:** if there is no bank account, no settlement is made and the store gets a notice to add one.

### Endpoints (role VENDOR / MANAGER)

| Method | Path | What |
|---|---|---|
| GET | `/settlements?status=&page=&limit=` | Settlements list |
| GET | `/settlements/summary` | `{ due, owed, net, held, paid, cycle, cycleIsCustom, payoutAccount }` |
| GET | `/settlements/:id` | One settlement, with `Lines` (one per branch) |
| GET | `/settlements/:id/entries?page=&limit=` | The ledger rows it covers, each with its `Order` |
| PATCH | `/store-bank/:id/default` | Make a bank account the payout one |

Summary fields:
- `due`: what's owed to the store (not settled yet).
- `owed`: what the store owes (not settled yet).
- `net` = `due` − `owed`.
- `held`: in settlements waiting for the transfer.
- `paid`: already transferred.
- `payoutAccount`: the default bank account, or the only one if there's just one.

Settlement fields:
- `status`: `DRAFT` (waiting for the transfer) or `TRANSFERRED` (done).
- `periodStart`, `periodEnd`, `dueTotal`, `owedTotal`, `amount`, `ordersCount`.
- The bank copy: `bankName`, `accountOwnerName`, `accountIban`.
- The transfer: `transferReference`, `transferProofPath`, `transferredAt`.

### Bank accounts
`GET /store-bank` now returns `isDefault` on each account. The first account added becomes the default.

### Refunds
- When the admin refunds an order, the store's share is taken off its balance as ledger type `REFUND`.
- The store gets a notice.
- If that order's settlement was already paid, the share is deducted from the next settlement.

### New ledger types
`SETTLEMENT_HOLD`, `SETTLEMENT_PAID` and `REFUND`.
