# Mutual Fund — Buy Fund Request

**Controller:** `App\Http\Controllers\MfBuyFundRequestController`  
**Source tables:** `mf_orders`, `mf_funds`, `mf_amcs`, `mf_investor_amc_profiles`, `mf_payment_transactions`, `mf_sip_plans`, `mf_bank_accounts`  
**Middleware:** `api` only (no JWT)

Back office reviews **INIT buy** orders (`order_type` is `LUMPSUM` or `SIP`, never `SELL`) and accepts or rejects them.

---

## Processable or not

Lookup: `mf_orders.fund_id` → `mf_funds.amc_id` + `mf_orders.fintr_customer_id` → `mf_investor_amc_profiles`.

| `can_process` | When |
|---------------|------|
| `true` | Profile exists, `is_onboarded = 1`, and `amc_investor_code` is set, and order is still `INIT` |
| `false` | No profile for that AMC, or not onboarded / no investor code, or order is already processed |

Accept is **409** when `can_process` is false — onboard the investor first (`mf-investor-amc-profile-accept`). Reject is still allowed on `INIT`.

---

## Lumpsum vs SIP

| | Lumpsum | SIP |
|--|---------|-----|
| `order_type` | `LUMPSUM` | `SIP` |
| Extra block | `payment` from `mf_payment_transactions` (+ `mf_orders.payment_receipt`) | `sip_plan` from `mf_sip_plans` via `mf_orders.sip_plan_id` |
| Not included | `sip_plan` is `null` | `payment` is `null` |
| Accept | `mf_orders.status = DONE` and that order's payments `PENDING` → `SUCCESS` | `mf_orders.status = DONE` only |
| Reject | `mf_orders.status = FAILED` (payment unchanged) | same |

`mf_orders.status` is `INIT` / `VERIFIED` / `DONE` / `FAILED`. The reject API writes `FAILED`.

---

## Endpoints

| Action | Method | Path |
|--------|--------|------|
| List | GET | `/api/mf-buy-fund-request/mf-buy-fund-request-index` |
| Show | GET | `/api/mf-buy-fund-request/mf-buy-fund-request-show/{id}` |
| Accept | POST | `/api/mf-buy-fund-request/mf-buy-fund-request-accept/{id}` |
| Reject | POST | `/api/mf-buy-fund-request/mf-buy-fund-request-reject/{id}` |

`{id}` is `mf_orders.id`. Accept / reject are **POST**, not PUT.

---

## Query params (index)

| Param | Default | Description |
|-------|---------|-------------|
| `per_page` | 10 | 1–100 |
| `page` | 1 | Laravel paginator |
| `order_type` | — | `LUMPSUM` or `SIP` |
| `amc_id` | — | Fund's AMC |
| `fund_id` | — | Fund |
| `request_status` | `pending` | `pending` (`INIT`), `accepted` (`DONE`), `failed`, `all` |
| `can_process` | — | `1` only onboarded INIT rows, `0` blocked INIT rows |
| `q` | — | Customer code, investor code, reference, fund name, BO id, user name |

---

## Accept / reject body

| Field | Required | Notes |
|-------|----------|--------|
| `users_id` | no | Back-office user |
| `remark` | no | Reject only, stored nowhere yet (accepted in the body for the UI) |

Already-processed rows return **409**.

On accept, if the order has no `amc_investor_code`, it is copied from the onboarded profile.
