# Receipt Download Functionality

## Overview
The receipt download feature allows customers to download a formatted payment receipt for their records. The receipt is generated as an HTML file that is print-ready and can be saved as PDF from the browser.

## Implementation Details

### Backend (Laravel)

**Controller**: `afinet-portal-backend/app/Http/Controllers/API/PaymentController.php`

**Route**: `GET /api/payments/{id}/receipt`

**Method**: `downloadReceipt($id)`

The method:
1. Authenticates the user
2. Fetches the payment with related records (invoice, sales order, quotation, customer)
3. Generates HTML from the Blade template
4. Returns the HTML with download headers

**Template**: `afinet-portal-backend/resources/views/receipts/payment-receipt.blade.php`

The template includes:
- Payment amount (large, centered)
- Payment information (reference, date, method, status)
- Customer information (name, email, phone, ID)
- Related records (invoice, order, quotation)
- Professional styling with purple gradient header
- Print-ready CSS

### Frontend (Next.js)

**Payment Detail Page**: `afinet-portal/src/app/(portal)/payments/[id]/page.js`
- Download button with loading state
- Downloads as `.html` file
- Shows spinner and "Generating Receipt..." text while downloading

**Payments List Page**: `afinet-portal/src/app/(portal)/payments/page.js`
- Download icon button for each payment
- Shows spinner while downloading
- Toast notifications for success/failure

## File Format

The receipt is downloaded as an **HTML file** (not PDF) because:
1. No external dependencies required (DomPDF was not properly installed)
2. HTML is print-ready with proper CSS
3. Users can open in browser and save as PDF using browser's print function
4. Faster generation and download
5. No server-side PDF processing overhead

## Usage

### From Payment Detail Page
1. Navigate to a payment detail page
2. Click "Download Receipt" button
3. Button shows loading state with spinner
4. HTML file downloads automatically
5. Open in browser and use Print > Save as PDF if needed

### From Payments List
1. Navigate to payments page
2. Click download icon next to any payment
3. Icon shows spinner while downloading
4. HTML file downloads automatically
5. Toast notification confirms success

## Styling

The receipt uses:
- Purple gradient header (#667eea to #764ba2)
- Clean, professional layout
- Responsive design
- Print-optimized CSS
- Company branding (AFINET logo and colors)

## Security

- Only authenticated users can download receipts
- Users can only download their own payment receipts
- Payment ID is validated against customer ID
- 404 error if payment not found or doesn't belong to user

## Error Handling

- 404: Payment not found or unauthorized
- 500: Server error during generation
- Frontend shows user-friendly error messages
- Backend logs errors for debugging

## Future Enhancements

If PDF generation is required in the future:
1. Install DomPDF: `composer require barryvdh/laravel-dompdf`
2. Run: `composer dump-autoload`
3. Add import: `use Barryvdh\DomPDF\Facade\Pdf;`
4. Update method to use: `Pdf::loadHTML($html)->download('receipt.pdf')`
5. Update frontend to download as `.pdf` instead of `.html`
