# Perfex CRM 2.9.1 - Partially Paid Invoices HTTP 500 Error Fix
## Complete Solution for PHP 8.2 Compatibility

### Root Cause Identified

The HTTP 500 error when viewing Partially Paid invoices is **NOT** caused by the invoice code itself. The real issue is a **Carbon library compatibility problem with PHP 8.2**.

**Error Message:**
```
Carbon\Carbon::setLastErrors(): Argument #1 ($lastErrors) must be of type array, bool given
```

This error occurs in `/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php` at line 97, where the Carbon library tries to pass a boolean value to a method that expects an array in PHP 8.2's strict type checking.

### The Complete Fix

This package contains **3 files** that need to be deployed:

1. **Invoices.php** (Original, unmodified) - Restored to original state
2. **invoice_preview_template.php** (Original, unmodified) - Restored to original state
3. **Creator.php** (Fixed) - Carbon library PHP 8.2 compatibility patch

### Installation Steps

#### Step 1: Backup Current Files
```bash
# Backup the current files on your server
cp /public_html/application/controllers/admin/Invoices.php /public_html/application/controllers/admin/Invoices.php.backup
cp /public_html/application/views/admin/invoices/invoice_preview_template.php /public_html/application/views/admin/invoices/invoice_preview_template.php.backup
cp /public_html/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php /public_html/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php.backup
```

#### Step 2: Extract the Fix Package
1. Download the ZIP file: `perfex-crm-2.9.1-php8.2-fix.zip`
2. Extract it in your `/public_html/` directory
3. The directory structure will be preserved automatically

**Command line extraction:**
```bash
cd /public_html
unzip perfex-crm-2.9.1-php8.2-fix.zip
```

#### Step 3: Verify File Permissions
```bash
chmod 644 /public_html/application/controllers/admin/Invoices.php
chmod 644 /public_html/application/views/admin/invoices/invoice_preview_template.php
chmod 644 /public_html/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php
```

#### Step 4: Clear Application Cache
```bash
# Remove all cache files
rm -rf /public_html/application/cache/*

# Or via admin panel: Settings → Tools → Clear Cache
```

#### Step 5: Clear Browser Cache
- Press **Ctrl+Shift+Delete** (Windows/Linux) or **Cmd+Shift+Delete** (Mac)
- Select "All time" and clear all browsing data
- Or use **Ctrl+F5** for hard refresh

#### Step 6: Test the Fix
1. Log in to Perfex CRM admin panel
2. Navigate to **Accounting → Invoices**
3. Click on a **Partially Paid invoice**
4. The detail panel should now display correctly ✓

### What Was Fixed

**File: Creator.php** (Line 97-103)

**Before (Broken):**
```php
static::setLastErrors(parent::getLastErrors());
```

**After (Fixed):**
```php
// PHP 8.2 Compatibility Fix: Ensure getLastErrors returns an array
$lastErrors = parent::getLastErrors();
if (is_array($lastErrors)) {
    static::setLastErrors($lastErrors);
} else {
    static::setLastErrors([]);
}
```

This fix ensures that the `setLastErrors()` method always receives an array, even if `getLastErrors()` returns a boolean value in PHP 8.2.

### Verification Checklist

After installation, verify the following:

- [ ] Partially Paid invoices now display correctly
- [ ] Paid invoices still display correctly
- [ ] Unpaid invoices still display correctly
- [ ] Overdue invoices still display correctly
- [ ] Draft invoices still display correctly
- [ ] Cancelled invoices still display correctly
- [ ] No JavaScript errors in browser console (F12)
- [ ] No PHP errors in `/application/logs/`
- [ ] All invoice buttons are functional
- [ ] Invoice merge functionality works
- [ ] Payment recording works
- [ ] Credits can be applied to applicable invoices

### Troubleshooting

#### Issue: Still getting HTTP 500 error

**Solution:**
1. Verify all three files were extracted correctly
2. Check file permissions are set to 644
3. Ensure `/public_html/application/cache/` is completely empty
4. Hard refresh browser: Ctrl+F5
5. Check `/application/logs/` for any remaining errors

#### Issue: Other pages showing errors

**Solution:**
1. The Carbon library fix affects all date/time operations
2. If other pages show errors, verify the Creator.php file was extracted correctly
3. Check that the file path is exactly: `/public_html/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php`

#### Issue: Invoices page still blank

**Solution:**
1. Open browser Developer Tools (F12)
2. Go to Console tab
3. Check for any JavaScript errors
4. Go to Network tab and check the AJAX request status
5. If still showing 500 error, check `/application/logs/` for detailed error messages

### Rollback Instructions

If you need to revert to the original files:

```bash
# Restore from backups
cp /public_html/application/controllers/admin/Invoices.php.backup /public_html/application/controllers/admin/Invoices.php
cp /public_html/application/views/admin/invoices/invoice_preview_template.php.backup /public_html/application/views/admin/invoices/invoice_preview_template.php
cp /public_html/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php.backup /public_html/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php

# Clear cache
rm -rf /public_html/application/cache/*

# Hard refresh browser (Ctrl+F5)
```

### Why This Fix Works

The Carbon library's `setLastErrors()` method has strict type checking in PHP 8.2. When `parent::getLastErrors()` returns a boolean value (which can happen in certain conditions), PHP 8.2 throws a type error because the method expects an array.

By checking the type before passing it to `setLastErrors()`, we ensure compatibility with PHP 8.2's strict type system while maintaining backward compatibility with earlier PHP versions.

### Technical Details

**Affected File:** `/application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php`

**Affected Line:** 97

**PHP Version:** 8.2 (and potentially 8.1+)

**Carbon Library Version:** Included with Perfex CRM 2.9.1

**Fix Type:** Type compatibility patch

**Risk Level:** LOW - Only adds type checking, no logic changes

**Performance Impact:** Negligible (< 1ms)

### Files Included

1. `application/controllers/admin/Invoices.php` - Original file (restored)
2. `application/views/admin/invoices/invoice_preview_template.php` - Original file (restored)
3. `application/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php` - Fixed file (PHP 8.2 compatible)
4. `INSTALLATION_INSTRUCTIONS.md` - This file

### Support

If you encounter any issues:

1. Check the troubleshooting section above
2. Verify all files were extracted correctly
3. Ensure file permissions are set to 644
4. Clear application cache completely
5. Hard refresh your browser
6. Check `/application/logs/` for detailed error messages

### Summary

This fix resolves the HTTP 500 error that occurs when viewing Partially Paid invoices by:

1. **Restoring original invoice files** to their unmodified state
2. **Patching the Carbon library** for PHP 8.2 compatibility
3. **Adding type checking** to prevent type mismatch errors
4. **Maintaining backward compatibility** with earlier PHP versions

The fix is minimal, safe, and addresses the root cause of the issue.
