# Admin API: Get All Receipt Details for a Loan

## Endpoint
```
GET /admin/loans/:loan_id/receipts
```

## Description
Retrieves all receipt details and transactions for a specific loan_id. This endpoint provides comprehensive information about all payments made for a loan, including loan details, customer information, and a complete transaction history.

## Authentication
- **Required**: Yes
- **Type**: Admin JWT Token
- **Header**: `Authorization: Bearer YOUR_ADMIN_JWT_TOKEN`

## URL Parameters
- `loan_id` (required): The loan ID to fetch receipts for (e.g., "DL2025001")

## Query Parameters
None

## Request Example

### cURL (Bash/Zsh)
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/DL2025001/receipts" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### cURL (PowerShell)
```powershell
$headers = @{
    "Authorization" = "Bearer YOUR_ADMIN_JWT_TOKEN"
    "Accept" = "application/json"
}

Invoke-RestMethod -Uri "http://192.168.1.3:3000/admin/loans/DL2025001/receipts" `
  -Method GET `
  -Headers $headers
```

## Success Response

### Status Code: 200 OK

```json
{
  "status": "success",
  "message": "Loan receipts retrieved successfully",
  "data": {
    "loan_info": {
      "loan_id": "DL2025001",
      "loan_date": "2025-11-10",
      "loan_type": "Daily",
      "loan_amount": 5000,
      "total_amount": 5500,
      "interest": 10,
      "interest_amount": 500,
      "per_due_amount": 92,
      "total_dues": 60,
      "next_due_date": "2025-12-08",
      "last_due_date": "2026-01-09",
      "status": "Active",
      "area": "Mollapalayam"
    },
    "customer_info": {
      "customer_id": 8,
      "customer_name": "Pavithra",
      "customer_mobile": "5346876764",
      "customer_contact": "5346876764",
      "address": "Erode",
      "address2": "Erode",
      "area": "Mollapalayam",
      "aadhaar_no": "534687676476"
    },
    "receipts_summary": {
      "total_receipts": 2,
      "total_paid_amount": 2484.00,
      "total_paid_dues": 25,
      "first_receipt_date": "2025-12-04",
      "last_receipt_date": "2025-12-07",
      "remaining_dues": 35,
      "balance_amount": 3016.00
    },
    "receipts": [
      {
        "receipt_number": 1,
        "receipt_id": 37,
        "receipt_no": "001",
        "receipt_date": "2025-12-04",
        "customer_id": 8,
        "customer_name": "Pavithra",
        "customer_mobile": "5346876764",
        "payment_details": {
          "num_dues": 60,
          "due_amount": 92,
          "paid_amount": 2208,
          "paid_dues": 24,
          "balance_dues": 60,
          "pending_dues": 24
        },
        "next_due_date": "2025-12-05",
        "remark": "hvhjfgh",
        "collected_by": "Raja",
        "created_at": "2025-12-04T11:20:07.000Z"
      },
      {
        "receipt_number": 2,
        "receipt_id": 38,
        "receipt_no": "REC1765116719895",
        "receipt_date": "2025-12-07",
        "customer_id": 8,
        "customer_name": "Pavithra",
        "customer_mobile": "5346876764",
        "payment_details": {
          "num_dues": 0,
          "due_amount": 276,
          "paid_amount": 276,
          "paid_dues": 1,
          "balance_dues": 0,
          "pending_dues": 0
        },
        "next_due_date": "2025-12-08",
        "remark": "",
        "collected_by": "",
        "created_at": "2025-12-07T14:11:59.000Z"
      }
    ]
  }
}
```

## Response Fields

### loan_info
- `loan_id`: Loan identifier
- `loan_date`: Date when loan was issued
- `loan_type`: Type of loan (Daily, Weekly, Monthly)
- `loan_amount`: Principal loan amount
- `total_amount`: Total amount including interest
- `interest`: Interest percentage
- `interest_amount`: Total interest amount
- `per_due_amount`: Amount per due payment
- `total_dues`: Total number of dues
- `next_due_date`: Next due date
- `last_due_date`: Last due date
- `status`: Loan status (Active, Closed, etc.)
- `area`: Area/location of the loan

### customer_info
- `customer_id`: Customer ID
- `customer_name`: Customer name
- `customer_mobile`: Customer mobile number
- `customer_contact`: Customer contact number
- `address`: Customer address line 1
- `address2`: Customer address line 2
- `area`: Customer area
- `aadhaar_no`: Customer Aadhaar number

### receipts_summary
- `total_receipts`: Total number of receipts
- `total_paid_amount`: Sum of all paid amounts
- `total_paid_dues`: Total number of dues paid
- `first_receipt_date`: Date of first receipt
- `last_receipt_date`: Date of last receipt
- `remaining_dues`: Remaining dues to be paid
- `balance_amount`: Remaining balance amount

### receipts (Array)
Each receipt object contains:
- `receipt_number`: Sequential receipt number (1, 2, 3...)
- `receipt_id`: Database receipt ID
- `receipt_no`: Receipt number
- `receipt_date`: Date of receipt
- `customer_id`: Customer ID
- `customer_name`: Customer name
- `customer_mobile`: Customer mobile
- `payment_details`: Payment information object
  - `num_dues`: Number of dues
  - `due_amount`: Due amount
  - `paid_amount`: Amount paid
  - `paid_dues`: Number of dues paid
  - `balance_dues`: Balance dues
  - `pending_dues`: Pending dues
- `next_due_date`: Next due date after this receipt
- `remark`: Remarks/notes
- `collected_by`: Agent name who collected
- `created_at`: Receipt creation timestamp

## Error Responses

### 400 Bad Request - Missing loan_id
```json
{
  "status": "error",
  "message": "loan_id is required"
}
```

### 401 Unauthorized - Invalid/Missing Token
```json
{
  "status": "error",
  "message": "Admin authentication required"
}
```

### 404 Not Found - Loan Not Found
```json
{
  "status": "error",
  "message": "Loan not found"
}
```

### 500 Internal Server Error
```json
{
  "status": "error",
  "message": "Failed to fetch loan receipts",
  "error": "Error message details"
}
```

## Notes
- Receipts are ordered by receipt date (ascending) and creation time (ascending)
- All amounts are returned as decimal numbers with 2 decimal places
- The endpoint only returns receipts for loans belonging to the admin's company
- If no receipts exist for the loan, the `receipts` array will be empty

## Use Cases
1. **View complete payment history** for a specific loan
2. **Generate payment reports** for a loan
3. **Track collection progress** for a loan
4. **Audit trail** of all transactions for a loan
5. **Customer payment analysis** - see payment patterns and history

