# Receipt API - cURL Examples

This document provides cURL examples for the Receipt Controller endpoints that match the PHP implementation.

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

## Authentication
All endpoints require JWT authentication in the Authorization header:
```
Authorization: Bearer <JWT_TOKEN>
```

---

## 1. Insert Receipt (POST)

### Endpoint
```
POST /receipt
```

### cURL Example
```bash
curl -X POST http://localhost:3000/receipt \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImNvbXBhbnlJZCI6OCwidWlkIjoxfSwiaWF0IjoxNzA1MjM0NTY3LCJleHAiOjE3MDUzMjA5Njd9.example" \
  -d '{
    "loan_id": "DL2025001",
    "receipt_no": "REC001",
    "receipt_date": "2025-01-15",
    "cust_id": 123456,
    "cus_name": "John Doe",
    "cus_mobile": "9876543210",
    "no_of_dues": 60,
    "due_amt": 200.00,
    "paid_amt": 200.00,
    "paid_dues": 1,
    "balance_dues": 59,
    "pending_dues": 0,
    "next_date": "2025-01-16",
    "remark": "Payment received successfully",
    "agent": "Agent Name",
    "ledgerName": "Cash A/c",
    "principal_per_due": 167.00,
    "interest_per_due": 33.00,
    "account_close": "",
    "latitude": 12.9716,
    "longitude": 77.5946,
    "accuracy": 10.5,
    "altitude": 920.5,
    "address": "123 Main Street, City, State"
  }'
```

### Form Data (POST - matching PHP $_POST)
```bash
curl -X POST http://localhost:3000/receipt \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d "loan_id=DL2025001" \
  -d "receipt_no=REC001" \
  -d "receipt_date=2025-01-15" \
  -d "cust_id=123456" \
  -d "cus_name=John Doe" \
  -d "cus_mobile=9876543210" \
  -d "no_of_dues=60" \
  -d "due_amt=200.00" \
  -d "paid_amt=200.00" \
  -d "paid_dues=1" \
  -d "balance_dues=59" \
  -d "pending_dues=0" \
  -d "next_date=2025-01-16" \
  -d "remark=Payment received successfully" \
  -d "agent=Agent Name" \
  -d "ledgerName=Cash A/c" \
  -d "principal_per_due=167.00" \
  -d "interest_per_due=33.00" \
  -d "account_close=" \
  -d "latitude=12.9716" \
  -d "longitude=77.5946" \
  -d "address=123 Main Street, City, State"
```

### Success Response
```json
{
  "success": true,
  "message": "Loan inserted successfully",
  "loan_id": "DL2025001",
  "customer_id": 123456,
  "receipt_id": 1,
  "location_tracked": true,
  "location_track_id": 1
}
```

---

## 2. Update Receipt (PUT)

### Endpoint
```
PUT /receipt
```

### cURL Example
```bash
curl -X PUT http://localhost:3000/receipt \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "loan_id": "DL2025001",
    "receipt_no": "REC001",
    "receipt_date": "2025-01-15",
    "cust_id": 123456,
    "cus_name": "John Doe Updated",
    "cus_mobile": "9876543210",
    "no_of_dues": 60,
    "due_amt": 250.00,
    "paid_amt": 250.00,
    "paid_dues": 1,
    "balance_dues": 59,
    "pending_dues": 0,
    "next_date": "2025-01-16",
    "remark": "Updated payment",
    "agent": "Agent Name",
    "ledgerName": "Cash A/c",
    "principal_per_due": 200.00,
    "interest_per_due": 50.00,
    "account_close": ""
  }'
```

### Success Response
```json
{
  "success": true,
  "message": "Loan updated successfully",
  "loan_id": "DL2025001",
  "receipt_no": "REC001"
}
```

---

## 3. Delete Receipt (DELETE)

### Endpoint
```
DELETE /receipt
```

### cURL Example
```bash
curl -X DELETE http://localhost:3000/receipt \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "loan_id": "DL2025001",
    "receipt_no": "REC001"
  }'
```

### Success Response
```json
{
  "success": true,
  "message": "Loan deleted successfully"
}
```

---

## Complete Example Script

### Bash Script with All Operations
```bash
#!/bin/bash

# Configuration
BASE_URL="http://localhost:3000"
JWT_TOKEN="YOUR_JWT_TOKEN_HERE"

echo "=== 1. Insert Receipt ==="
curl -X POST "${BASE_URL}/receipt" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${JWT_TOKEN}" \
  -d '{
    "loan_id": "DL2025001",
    "receipt_no": "REC001",
    "receipt_date": "2025-01-15",
    "cust_id": 123456,
    "cus_name": "John Doe",
    "cus_mobile": "9876543210",
    "no_of_dues": 60,
    "due_amt": 200.00,
    "paid_amt": 200.00,
    "paid_dues": 1,
    "balance_dues": 59,
    "pending_dues": 0,
    "next_date": "2025-01-16",
    "remark": "Payment received",
    "agent": "Agent Name",
    "ledgerName": "Cash A/c",
    "principal_per_due": 167.00,
    "interest_per_due": 33.00,
    "account_close": "",
    "latitude": 12.9716,
    "longitude": 77.5946,
    "address": "123 Main Street"
  }' | jq '.'

echo -e "\n=== 2. Update Receipt ==="
curl -X PUT "${BASE_URL}/receipt" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${JWT_TOKEN}" \
  -d '{
    "loan_id": "DL2025001",
    "receipt_no": "REC001",
    "receipt_date": "2025-01-15",
    "cust_id": 123456,
    "cus_name": "John Doe Updated",
    "cus_mobile": "9876543210",
    "no_of_dues": 60,
    "due_amt": 250.00,
    "paid_amt": 250.00,
    "paid_dues": 1,
    "balance_dues": 59,
    "pending_dues": 0,
    "next_date": "2025-01-16",
    "remark": "Updated payment",
    "agent": "Agent Name",
    "ledgerName": "Cash A/c",
    "principal_per_due": 200.00,
    "interest_per_due": 50.00,
    "account_close": ""
  }' | jq '.'

echo -e "\n=== 3. Delete Receipt ==="
curl -X DELETE "${BASE_URL}/receipt" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${JWT_TOKEN}" \
  -d '{
    "loan_id": "DL2025001",
    "receipt_no": "REC001"
  }' | jq '.'
```

---

## JWT Token Generation

To generate a JWT token for testing, you can use Node.js:

```javascript
const jwt = require('jsonwebtoken');

const secret = '1ED495A77B65435D317B3E6582E4C';
const payload = {
  data: {
    companyId: 8,
    uid: 1
  }
};

const token = jwt.sign(payload, secret, { expiresIn: '24h' });
console.log(token);
```

Or using a simple script:

```bash
node -e "
const jwt = require('jsonwebtoken');
const secret = '1ED495A77B65435D317B3E6582E4C';
const payload = { data: { companyId: 8, uid: 1 } };
console.log(jwt.sign(payload, secret, { expiresIn: '24h' }));
"
```

---

## Error Responses

### Missing Authorization
```json
{
  "success": false,
  "message": "Authorization header missing"
}
```

### Invalid JWT
```json
{
  "success": false,
  "message": "JWT Error: invalid token"
}
```

### Missing Required Fields
```json
{
  "success": false,
  "message": "loan_id and receipt_no are required"
}
```

### Receipt Not Found (Update/Delete)
```json
{
  "success": false,
  "message": "Receipt not found"
}
```

---

## Notes

1. **JWT Token**: Replace `YOUR_JWT_TOKEN` with an actual JWT token containing `companyId` and `uid` in the payload.

2. **Location Tracking**: Location fields (`latitude`, `longitude`, `address`, etc.) are optional. If provided, they will be automatically tracked.

3. **Account Closing**: Set `account_close` to `"Closed"` to close the loan account.

4. **Ledger Entries**: Principal and Interest ledger entries are automatically created/updated in `ledger_day_wise` table.

5. **Transactions**: All operations use database transactions to ensure data integrity.

6. **Form Data**: The POST endpoint accepts both JSON and form data (matching PHP `$_POST` behavior).

