# Export Documentation - MidHouse Universe

## Overview
This document describes the export functionality for booking and payment data in the MidHouse Universe system.

## Features
- **PDF Export**: Export individual booking and payment records to PDF
- **Excel/CSV Export**: Export all booking and payment data to CSV format

## API Endpoints

### Booking Exports

#### 1. Export Individual Booking to PDF
```
GET /api/booking/{id}/export-pdf
```
- **Description**: Exports a specific booking record to PDF format
- **Authentication**: Required (auth:api)
- **Parameters**: 
  - `id` (path parameter): Booking ID
- **Response**: PDF file download
- **Example**: `/api/booking/1/export-pdf`

#### 2. Export All Bookings to Excel/CSV
```
GET /api/booking/export-excel
```
- **Description**: Exports all booking records to CSV format
- **Authentication**: Required (auth:api)
- **Parameters**: None
- **Response**: CSV file download
- **Example**: `/api/booking/export-excel`

### Payment Exports

#### 1. Export Individual Payment to PDF
```
GET /api/pay/{id}/export-pdf
```
- **Description**: Exports a specific payment record to PDF format
- **Authentication**: Required (auth:api)
- **Parameters**: 
  - `id` (path parameter): Payment ID
- **Response**: PDF file download
- **Example**: `/api/pay/1/export-pdf`

#### 2. Export All Payments to Excel/CSV
```
GET /api/pay/export-excel
```
- **Description**: Exports all payment records to CSV format
- **Authentication**: Required (auth:api)
- **Parameters**: None
- **Response**: CSV file download
- **Example**: `/api/pay/export-excel`

## Data Included in Exports

### Booking PDF Export Includes:
- Invoice ID
- Booking status
- Check-in date
- Start and end dates
- Quantity
- Total price
- Customer information (name, email)
- Room information (name, property)
- Creation timestamp

### Payment PDF Export Includes:
- Invoice ID
- Payment status
- Bank information (name, account number, account holder)
- Verification status and timestamp
- Customer information (name, email)
- Booking information (check-in date, total amount, room)
- Payment proof file link
- Creation timestamp

### CSV Export Includes:
- All relevant fields from the database
- Formatted data for easy analysis
- Numbered rows for reference
- Proper date formatting
- Currency formatting for prices

## File Naming Convention
- **PDF files**: `booking-{invoice_id}.pdf` or `payment-{invoice_id}.pdf`
- **CSV files**: `bookings_YYYY-MM-DD_HH-MM-SS.csv` or `payments_YYYY-MM-DD_HH-MM-SS.csv`

## Error Handling
- Returns appropriate HTTP status codes
- JSON error responses for API errors
- Graceful handling of missing data
- Proper exception handling for file generation

## Dependencies
- **PDF Generation**: `barryvdh/laravel-dompdf`
- **CSV Generation**: Built-in PHP functions
- **Authentication**: Laravel Sanctum

## Usage Examples

### Frontend Integration (JavaScript)
```javascript
// Export individual booking to PDF
const exportBookingPDF = async (bookingId) => {
    try {
        const response = await fetch(`/api/booking/${bookingId}/export-pdf`, {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Accept': 'application/pdf'
            }
        });
        
        if (response.ok) {
            const blob = await response.blob();
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = `booking-${bookingId}.pdf`;
            a.click();
        }
    } catch (error) {
        console.error('Export failed:', error);
    }
};

// Export all bookings to CSV
const exportAllBookings = async () => {
    try {
        const response = await fetch('/api/booking/export-excel', {
            headers: {
                'Authorization': `Bearer ${token}`,
                'Accept': 'text/csv'
            }
        });
        
        if (response.ok) {
            const blob = await response.blob();
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = `bookings_${new Date().toISOString().slice(0,19).replace(/:/g,'-')}.csv`;
            a.click();
        }
    } catch (error) {
        console.error('Export failed:', error);
    }
};
```

### cURL Examples
```bash
# Export booking PDF
curl -X GET "http://your-domain.com/api/booking/1/export-pdf" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Accept: application/pdf" \
  --output booking-1.pdf

# Export all bookings CSV
curl -X GET "http://your-domain.com/api/booking/export-excel" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Accept: text/csv" \
  --output bookings.csv
```

## Notes
- All exports require authentication
- PDF exports include styled templates with company branding
- CSV exports are optimized for large datasets using chunking
- Files are generated on-demand and not stored on the server
- Proper error handling ensures graceful failure scenarios
