# Admin API: Get Recent Collection Details

## Endpoint
`GET /admin/recent-collection-details`

## Description
Returns a simplified list of recent collection details with:
- Customer name and mobile
- Agent name and mobile
- Total amount till today
- Total collected amount
- Collected today amount
- Total pending amount
- Remaining pending amount
- Collection status and payment type

This endpoint provides a clean, simplified view of collection data for dashboard or reporting purposes.

## Authentication
Requires admin authentication via Bearer token (JWT or auth_token).

## Query Parameters
- `date` (optional): Target date for collection details (YYYY-MM-DD). Defaults to today
- `agent_id` (optional): Filter by specific agent ID
- `area` (optional): Filter by specific area(s). Comma-separated for multiple areas (e.g., "Area1,Area2")
- `limit` (optional): Maximum number of records to return. Defaults to 100

## cURL Examples

### 1. Get Recent Collection Details (Default - Today, All Agents)
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 2. Get Recent Collection Details for Specific Date
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details?date=2025-12-12" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 3. Get Recent Collection Details for Specific Agent
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details?agent_id=3" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 4. Get Recent Collection Details with Area Filter
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details?area=Kollampalayam,Mollapalayam" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 5. Get Recent Collection Details with Limit
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details?limit=50" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 6. Combined Filters
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details?date=2025-12-12&agent_id=3&limit=50" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

## Response Format

### Success Response (200 OK)
```json
{
  "status": "success",
  "message": "Recent collection details retrieved successfully",
  "data": {
    "filters": {
      "date": "2025-12-12",
      "agent_id": null,
      "area": null,
      "limit": 100
    },
    "summary": {
      "total_customers": 3,
      "total_amount_till_today": 5300.00,
      "total_collected_amount": 2600.00,
      "collected_today_amount": 200.00,
      "total_pending_amount": 2700.00,
      "remaining_pending": 300.00
    },
    "customers": [
      {
        "customer_name": "Priya",
        "customer_mobile": "9797686575",
        "agent_name": "Arshaf",
        "agent_mobile": "9087863189",
        "loan_id": "DL2025001",
        "area": "Kollampalayam",
        "total_amount_till_today": 1400.00,
        "total_collected_amount": 1400.00,
        "collected_today_amount": 200.00,
        "total_pending_amount": 0.00,
        "remaining_pending": 0.00,
        "collection_status": "FULLY_PAID",
        "payment_type": "FULL_PAID",
        "next_due_date": "2025-12-13",
        "per_due_amount": 200.00
      },
      {
        "customer_name": "Nandhini",
        "customer_mobile": "9878678675",
        "agent_name": "Arshaf",
        "agent_mobile": "9087863189",
        "loan_id": "DL2025002",
        "area": "Mollapalayam",
        "total_amount_till_today": 1500.00,
        "total_collected_amount": 1200.00,
        "collected_today_amount": 0.00,
        "total_pending_amount": 300.00,
        "remaining_pending": 300.00,
        "collection_status": "UNPAID",
        "payment_type": "NONE",
        "next_due_date": "2025-12-12",
        "per_due_amount": 300.00
      },
      {
        "customer_name": "Bala",
        "customer_mobile": "9789867875",
        "agent_name": "Arshaf",
        "agent_mobile": "9087863189",
        "loan_id": "DL2025003",
        "area": "P.S.Park",
        "total_amount_till_today": 2400.00,
        "total_collected_amount": 0.00,
        "collected_today_amount": 0.00,
        "total_pending_amount": 0.00,
        "remaining_pending": 0.00,
        "collection_status": "UNPAID",
        "payment_type": "NONE",
        "next_due_date": "2025-12-15",
        "per_due_amount": 1200.00
      }
    ]
  }
}
```

### Success Response - No Customers Found
```json
{
  "status": "success",
  "message": "No agents found",
  "data": {
    "filters": {
      "date": "2025-12-12",
      "agent_id": null,
      "area": null,
      "limit": 100
    },
    "customers": []
  }
}
```

### Error Response (401 Unauthorized)
```json
{
  "status": "error",
  "message": "Admin authentication required"
}
```

### Error Response (500 Internal Server Error)
```json
{
  "status": "error",
  "message": "Failed to fetch recent collection details",
  "error": "Error message details"
}
```

## Response Fields

### Customer Object Fields
- `customer_name`: Name of the customer
- `customer_mobile`: Mobile number of the customer
- `agent_name`: Name of the collection agent
- `agent_mobile`: Mobile number of the collection agent
- `loan_id`: Unique loan identifier
- `area`: Area assigned to the customer
- `total_amount_till_today`: Total amount due from loan start to today
- `total_collected_amount`: Total amount collected (all time)
- `collected_today_amount`: Amount collected today
- `total_pending_amount`: Total pending amount (overdue + today)
- `remaining_pending`: Remaining pending after today's collection
- `collection_status`: Payment status (`FULLY_PAID`, `PARTIALLY_PAID`, `UNPAID`)
- `payment_type`: Payment type (`FULL_PAID`, `PARTIAL_FUTURE_PAID`, `PREVIOUS_UNPAID`, `NONE`)
- `next_due_date`: Next due date for the loan
- `per_due_amount`: Amount per due payment

### Summary Fields
- `total_customers`: Total number of customers in the result
- `total_amount_till_today`: Sum of all customers' total amount till today
- `total_collected_amount`: Sum of all customers' total collected amount
- `collected_today_amount`: Sum of all customers' collected today amount
- `total_pending_amount`: Sum of all customers' total pending amount
- `remaining_pending`: Sum of all customers' remaining pending

## Notes

1. **Authentication**: Replace `YOUR_ADMIN_JWT_TOKEN` with a valid admin JWT token obtained from `/admin/login` or `/?admin_login=true`.

2. **Date Format**: Use `YYYY-MM-DD` format for dates (e.g., `2025-12-12`). Defaults to today if not provided.

3. **Filtering**: 
   - If `agent_id` is provided, only returns customers for that specific agent
   - If `area` is provided, only returns customers from those areas
   - Multiple areas can be specified as comma-separated values

4. **Limit**: The `limit` parameter controls the maximum number of customer records returned. Default is 100. Results are sorted by agent name, then customer name.

5. **Company Filtering**: The endpoint automatically filters by the admin's company_id, ensuring admins can only access data from their own company.

6. **Sorting**: Results are sorted by agent name first, then by customer name alphabetically.

## Example with Real Server URL

```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details?date=2025-12-12&limit=50" \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
```

## Example for Today's Date Only

### cURL Command (Today - No date parameter needed, defaults to today)
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details" \
  -H "accept: application/json" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVpZCI6NCwiY29tcGFueUlkIjoxNSwiY29tcGFueV9pZCI6MTUsInVzZXJuYW1lIjoiYXlhYW4iLCJ1c2VyX3R5cGUiOiJhZG1pbiIsInJlZ19pZCI6IjYifSwiaWF0IjoxNzY1NTM0NTA0LCJleHAiOjE3NjYxMzkzMDQsImF1ZCI6Imh0dHA6Ly94ZXNzdGVjaGxpbmsuY29tIiwiaXNzIjoiaHR0cDovL2thbmluZm90ZWNoLmluIn0.uQnzCk7UiAVTAxkuC8FyKZM24n8ZRchBXiPq1abs3ks"
```

### Alternative: Explicitly specify today's date
```bash
curl -X GET "https://dayloanapp.xesstechlink.com/admin/recent-collection-details?date=$(date +%Y-%m-%d)" \
  -H "accept: application/json" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVpZCI6NCwiY29tcGFueUlkIjoxNSwiY29tcGFueV9pZCI6MTUsInVzZXJuYW1lIjoiYXlhYW4iLCJ1c2VyX3R5cGUiOiJhZG1pbiIsInJlZ19pZCI6IjYifSwiaWF0IjoxNzY1NTM0NTA0LCJleHAiOjE3NjYxMzkzMDQsImF1ZCI6Imh0dHA6Ly94ZXNzdGVjaGxpbmsuY29tIiwiaXNzIjoiaHR0cDovL2thbmluZm90ZWNoLmluIn0.uQnzCk7UiAVTAxkuC8FyKZM24n8ZRchBXiPq1abs3ks"
```

### Response Example (Today's Data)
```json
{
  "status": "success",
  "message": "Recent collection details retrieved successfully",
  "data": {
    "filters": {
      "date": "2025-12-12",
      "agent_id": null,
      "area": null,
      "limit": 100
    },
    "summary": {
      "total_customers": 3,
      "total_amount_till_today": 5300.00,
      "total_collected_amount": 2600.00,
      "collected_today_amount": 200.00,
      "total_pending_amount": 2700.00,
      "remaining_pending": 300.00
    },
    "customers": [
      {
        "customer_name": "Priya",
        "customer_mobile": "9797686575",
        "agent_name": "Arshaf",
        "agent_mobile": "9087863189",
        "loan_id": "DL2025001",
        "area": "Kollampalayam",
        "total_amount_till_today": 1400.00,
        "total_collected_amount": 1400.00,
        "collected_today_amount": 200.00,
        "total_pending_amount": 0.00,
        "remaining_pending": 0.00,
        "collection_status": "FULLY_PAID",
        "payment_type": "FULL_PAID",
        "next_due_date": "2025-12-13",
        "per_due_amount": 200.00
      },
      {
        "customer_name": "Nandhini",
        "customer_mobile": "9878678675",
        "agent_name": "Arshaf",
        "agent_mobile": "9087863189",
        "loan_id": "DL2025002",
        "area": "Mollapalayam",
        "total_amount_till_today": 1500.00,
        "total_collected_amount": 1200.00,
        "collected_today_amount": 0.00,
        "total_pending_amount": 300.00,
        "remaining_pending": 300.00,
        "collection_status": "UNPAID",
        "payment_type": "NONE",
        "next_due_date": "2025-12-12",
        "per_due_amount": 300.00
      },
      {
        "customer_name": "Bala",
        "customer_mobile": "9789867875",
        "agent_name": "Arshaf",
        "agent_mobile": "9087863189",
        "loan_id": "DL2025003",
        "area": "P.S.Park",
        "total_amount_till_today": 2400.00,
        "total_collected_amount": 0.00,
        "collected_today_amount": 0.00,
        "total_pending_amount": 0.00,
        "remaining_pending": 0.00,
        "collection_status": "UNPAID",
        "payment_type": "NONE",
        "next_due_date": "2025-12-15",
        "per_due_amount": 1200.00
      }
    ]
  }
}
```

