# Flowchart — `debts`

> Produced by the Reversa **Archaeologist** (phase: excavation) · doc_level: `complete`
> Generated on 2026-09-17

**Confidence scale:** 🟢 CONFIRMED · 🟡 INFERRED · 🔴 GAP

## `DebtController::index` — accounts-receivable overview 🟢

```mermaid
flowchart TD
    A["GET /debts (debts.index)"] --> B["header = 'Danh sách công nợ'"]
    B --> C["base query:<br/>Customer where debt_total &gt; 0<br/>order by debt_total desc"]
    C --> D{"q present?"}
    D -->|yes| E["where(closure):<br/>phone LIKE %q% OR fullname LIKE %q%<br/>🟢 grouped — stays AND-scoped<br/>to debt_total &gt; 0"]
    D -->|no| F["(no extra filter)"]
    E --> G["customers = query-&gt;paginate(30)<br/>✅ fixed — was ->get() (no pagination)"]
    F --> G
    G --> H["view('pages.debts', customers)"]
```

**Notes:**
- The page renders two modals whose forms POST to the **customers** module: `POST /customers/{customer}/repayments` (Thu nợ) and `POST /customers/{customer}/debts` (Ghi nợ tay). Those handlers call `CustomerDebt::record(...)` — see `flowcharts/customers.md`. This controller performs **no writes**. 🟢
- Read-only projection over `customers.debt_total` and the `customer_debts` ledger; introduces no entities of its own.
