# Admin Daily Loans API - cURL Examples

Quick reference for admins to view daily loans with cURL.

## Base URL
```
http://192.168.1.3:3000
```

## Authentication
Replace `YOUR_ADMIN_JWT_TOKEN` with your actual admin JWT token.

---

## Endpoint
```
GET /admin/loans/daily
```

## Query Parameters
- `date` (optional): Specific date (YYYY-MM-DD) - returns loans for that day
- `start_date` (optional): Start date filter (YYYY-MM-DD) - for date range
- `end_date` (optional): End date filter (YYYY-MM-DD) - for date range
- `loan_type` (optional): Filter by loan type
- `status` (optional): Filter by loan status
- `area` (optional): Filter by area
- `limit` (optional): Maximum number of records (default: 1000)

---

## cURL Examples

### 1. Get Today's Loans
```bash
TODAY=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=${TODAY}" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 2. Get Loans for Specific Date
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=2025-01-15" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 3. Get Loans Using Date Range
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?start_date=2025-01-01&end_date=2025-01-31" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 4. Get Today's Loans with Pretty Print
```bash
TODAY=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=${TODAY}" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 5. Get Loans for Specific Date with Loan Type Filter
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=2025-01-15&loan_type=Weekly" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 6. Get Loans for Specific Date with Status Filter
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=2025-01-15&status=active" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 7. Get Loans for Specific Date with Area Filter
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=2025-01-15&area=Erode" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 8. Get Loans with Multiple Filters
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=2025-01-15&loan_type=Weekly&status=active&area=Erode" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 9. Get Loans with Limit
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=2025-01-15&limit=100" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

### 10. Get All Loans (No Date Filter)
```bash
curl -X GET "http://192.168.1.3:3000/admin/loans/daily" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json"
```

---

## Success Response Format

```json
{
  "status": "success",
  "message": "Daily loans retrieved successfully",
  "data": {
    "filters": {
      "date": "2025-01-15",
      "start_date": null,
      "end_date": null,
      "loan_type": null,
      "status": null,
      "area": null,
      "limit": null
    },
    "summary": {
      "total_loans": 25,
      "total_loan_amount": 250000.00,
      "total_collected": 75000.00,
      "total_balance": 175000.00,
      "total_receipts": 75,
      "collection_rate": 30.00
    },
    "by_loan_type": {
      "Weekly": {
        "count": 15,
        "total_amount": 150000.00,
        "collected": 50000.00
      },
      "Monthly": {
        "count": 10,
        "total_amount": 100000.00,
        "collected": 25000.00
      }
    },
    "by_status": {
      "active": {
        "count": 20,
        "total_amount": 200000.00
      },
      "closed": {
        "count": 5,
        "total_amount": 50000.00
      }
    },
    "loans": [
      {
        "id": 123,
        "loan_id": "DL2025001",
        "loan_date": "2025-01-15T00:00:00.000Z",
        "customer_name": "John Doe",
        "customer_phone": "9876543210",
        "customer": {
          "cust_id": "CUST001",
          "name": "John Doe",
          "mobile": "9876543210",
          "address": "123 Main Street, Erode",
          "aadhaar_no": "1234 5678 9012"
        },
        "loan_details": {
          "loan_type": "Weekly",
          "loan_amount": 10000.00,
          "total_amount": 12000.00,
          "interest": 2.00,
          "interest_amount": 2000.00,
          "per_due_amount": 1000.00,
          "dues": 12,
          "area": "Erode",
          "ledger": "Ledger 1",
          "status": "active"
        },
        "payment_details": {
          "total_collected": 3000.00,
          "balance_amount": 9000.00,
          "receipt_count": 3,
          "last_payment_date": "2025-01-18",
          "next_due_date": "2025-01-20",
          "last_due_date": "2025-01-13"
        },
        "created_on": "2025-01-15T10:30:00.000Z"
      }
    ]
  }
}
```

---

## Response Fields Explained

### Summary
- `total_loans`: Total number of loans for the day/period
- `total_loan_amount`: Sum of all loan amounts
- `total_collected`: Sum of all collected payments
- `total_balance`: Remaining balance (total_amount - collected)
- `total_receipts`: Total number of receipts/payments
- `collection_rate`: Percentage of loan amount collected

### Loan Object
- `id`: Loan entry ID
- `loan_id`: Unique loan identifier
- `loan_date`: Date when loan was issued
- `customer_name`: Customer name (top level for easy access)
- `customer_phone`: Customer phone number (top level for easy access)
- `customer`: Full customer information object
- `loan_details`: Loan terms and details
- `payment_details`: Payment and collection information
- `created_on`: When the loan record was created

---

## Common Use Cases

### 1. View Today's Loans
```bash
TODAY=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=${TODAY}" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 2. View Yesterday's Loans
```bash
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=${YESTERDAY}" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 3. View This Month's Loans
```bash
MONTH_START=$(date +%Y-%m-01)
MONTH_END=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?start_date=${MONTH_START}&end_date=${MONTH_END}" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 4. View Today's Weekly Loans Only
```bash
TODAY=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=${TODAY}&loan_type=Weekly" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

### 5. View Today's Active Loans
```bash
TODAY=$(date +%Y-%m-%d)
curl -X GET "http://192.168.1.3:3000/admin/loans/daily?date=${TODAY}&status=active" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Accept: application/json" | python3 -m json.tool
```

---

## Error Responses

### Authentication Error
```json
{
  "status": "error",
  "message": "Admin authentication required"
}
```

### Invalid Date Format
```json
{
  "status": "error",
  "message": "Invalid date format. Use YYYY-MM-DD"
}
```

---

## Notes

1. **Single Date**: Use `date` parameter for a specific day (e.g., `date=2025-01-15`)
2. **Date Range**: Use `start_date` and `end_date` for a range of days
3. **Date Format**: All dates should be in `YYYY-MM-DD` format
4. **Customer Info**: Each loan includes `customer_name` and `customer_phone` at the top level
5. **Collection Rate**: Calculated as (total_collected / total_loan_amount) * 100
6. **Balance Amount**: Calculated as total_amount - total_collected
7. **Receipts**: Includes all receipts for each loan to calculate collected amounts
8. **Default Limit**: 1000 records if not specified
9. **Sorting**: Loans are sorted by loan_date (newest first), then by created_on

---

## PowerShell Examples

### Get Today's Loans
```powershell
$today = (Get-Date).ToString("yyyy-MM-dd")

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1"

Invoke-WebRequest -UseBasicParsing `
  -Uri "http://192.168.1.3:3000/admin/loans/daily?date=${today}" `
  -WebSession $session `
  -Headers @{
    "Accept"="application/json"
    "Authorization"="Bearer YOUR_ADMIN_JWT_TOKEN"
  } `
  -ContentType "application/json"
```

---

## Comparison: Daily vs Weekly

| Feature | Daily Loans | Weekly Loans |
|---------|-------------|--------------|
| **Endpoint** | `/admin/loans/daily` | `/admin/loans/weekly` |
| **Primary Filter** | `date` (single day) | `week_start` & `week_end` |
| **Use Case** | View loans for a specific day | View loans for a week |
| **Response Format** | Same structure | Same structure |
| **Customer Info** | ✅ Included | ✅ Included |

Both endpoints return the same data structure with customer name and phone number included!

