# Testing Collection Details Calculation

This guide explains how to test the enhanced collection calculation logic.

## Quick Test

### Option 1: Using Existing Auth Token

If you already have an auth token from a previous login:

```bash
# Using environment variable
AUTH_TOKEN=your_auth_token_here node test_collection_calculation_simple.js

# Or as command line argument
node test_collection_calculation_simple.js your_auth_token_here
```

### Option 2: Full Test Flow

1. **Generate OTP:**
```bash
curl -X POST "http://localhost:3000/?generate_otp" \
  -H "Content-Type: application/json" \
  -d '{"mobile_no": "9876543210"}'
```

2. **Verify OTP and Get Auth Token:**
```bash
curl -X POST "http://localhost:3000/?verify_otp" \
  -H "Content-Type: application/json" \
  -d '{
    "mobile_no": "9876543210",
    "otp": "123456",
    "device_id": "test-device-123"
  }'
```

3. **Test Collection Details:**
```bash
# Using the auth_token from step 2
curl -X GET "http://localhost:3000/collection_details" \
  -H "Authorization: Bearer YOUR_AUTH_TOKEN" \
  -H "Content-Type: application/json"
```

4. **Run Test Script:**
```bash
AUTH_TOKEN=your_auth_token_here node test_collection_calculation_simple.js
```

## What the Test Validates

The test script validates:

1. **Calculation Accuracy:**
   - `total_collection_amount` = `previous_unpaid_amount` + `today_expected_amount`
   - `remaining_pending` = `total_pending_amount` - `collected_today_amount`

2. **Amount Tracking:**
   - Previous unpaid amounts (overdue)
   - Today's expected amount
   - Future paid amounts (advance payments)
   - Partial future paid amounts
   - Total collected amounts
   - Collected today amounts

3. **Payment Types:**
   - PREVIOUS_UNPAID
   - FUTURE_PAID
   - PARTIAL_FUTURE_PAID
   - FULL_PAID
   - NONE

4. **Collection Status:**
   - FULLY_PAID
   - PARTIALLY_PAID
   - UNPAID

## Expected Response Structure

```json
{
  "status": "success",
  "data": {
    "summary": {
      "total_customers": 10,
      "total_collection_amount": 2000.00,      // Total needed today
      "collected_today_amount": 1500.00,      // Collected today
      "total_collected_amount": 5000.00,      // Total collected (all time)
      "total_pending_amount": 500.00,        // Total pending
      "remaining_pending_amount": 0.00,       // Remaining after today
      "total_previous_unpaid": 300.00,
      "total_future_paid": 100.00,
      "total_partial_future_paid": 50.00,
      "collection_status": {
        "fully_paid": 5,
        "partially_paid": 3,
        "unpaid": 2
      },
      "payment_types": {
        "previous_unpaid": 2,
        "future_paid": 1,
        "partial_future_paid": 1,
        "full_paid": 5,
        "none": 1
      },
      "collection_rate": 50.00,
      "today_collection_rate": 75.00
    },
    "customers": [
      {
        "loan_id": "DL2025001",
        "collection_details": {
          "previous_unpaid_amount": 200.00,
          "today_expected_amount": 100.00,
          "total_collection_amount": 300.00,
          "collected_today_amount": 300.00,
          "total_collected_amount": 1500.00,
          "total_pending_amount": 0.00,
          "remaining_pending": 0.00,
          "collection_status": "FULLY_PAID",
          "payment_type": "FULL_PAID",
          "cumulative_paid_dues": 15
        }
      }
    ]
  }
}
```

## Testing with Postman

1. Import the Postman collection from `postman/DayLoan_API.postman_collection.json`
2. Set up environment variables:
   - `base_url`: `http://localhost:3000`
   - `auth_token`: Your auth token
3. Run the "Get Collection Details" request
4. Verify the response matches the expected structure

## Manual Testing Steps

1. **Check Summary Totals:**
   - Verify `total_collection_amount` matches sum of all customer `total_collection_amount`
   - Verify `collected_today_amount` matches sum of all customer `collected_today_amount`
   - Verify `total_pending_amount` matches sum of all customer `total_pending_amount`

2. **Check Individual Customer Calculations:**
   - For each customer, verify:
     - `total_collection_amount` = `previous_unpaid_amount` + `today_expected_amount`
     - `remaining_pending` = `total_pending_amount` - `collected_today_amount`
     - Payment type matches the payment status

3. **Check Iterative Calculation:**
   - Verify that `due_amnt` in receipts matches iteratively calculated amounts
   - Verify cumulative paid dues are calculated correctly
   - Verify pending dues are calculated correctly

## Troubleshooting

### Server Not Running
```bash
# Start the server
npm start
# or
npm run dev
```

### Authentication Errors
- Make sure you're using a valid auth token
- Check if the token has expired
- Verify the agent exists in the database

### Calculation Errors
- Check the console logs for detailed calculation information
- Verify loan entries have correct `per_due_amt` values
- Verify receipts have correct `due_amnt` and `paid_amnt` values
- Check that `loan_type` is set correctly (Daily, Weekly, Monthly)

## Example Test Output

```
🧪 Testing Collection Details Calculation
============================================================
Base URL: http://localhost:3000
Auth Token: abc123def456...
============================================================

📊 COLLECTION DETAILS SUMMARY
============================================================
Total Customers: 10
Current Date: 2025-01-15
Areas: Area1, Area2

💰 AMOUNT BREAKDOWN
============================================================
Total Collection Amount (Needed Today): ₹2000.00
Collected Today Amount: ₹1500.00
Total Collected Amount (All Time): ₹5000.00
Total Pending Amount: ₹500.00
Remaining Pending Amount: ₹0.00

🔍 VALIDATION CHECKS
============================================================
✅ All 20 validation checks passed!

👤 SAMPLE CUSTOMER DETAILS
============================================================
Customer 1:
  Loan ID: DL2025001
  Customer: John Doe
  Previous Unpaid: ₹200.00 (2 dues)
  Today Expected: ₹100.00
  Total Collection Amount: ₹300.00
  Collected Today: ₹300.00
  Status: FULLY_PAID
  Payment Type: FULL_PAID
```

