# DSP Report - Troubleshooting Guide

## Problem: Error Bergantian Setelah Upload ke Hosting

### Deskripsi Masalah
Setelah di-upload ke hosting, sistem PGN dan RWL mengalami error bergantian:
- Sebelumnya: PGN berfungsi, RWL error
- Setelah diperbaiki: RWL berfungsi, PGN error
- Di localhost: Kedua sistem berfungsi dengan baik

### Penyebab Utama
1. **Hosting Environment**: Perbedaan konfigurasi PHP antara localhost dan hosting
2. **Output Buffering**: Hosting memiliki aturan berbeda untuk output buffering
3. **Header Management**: Hosting lebih ketat dalam menangani HTTP headers
4. **Resource Limits**: Hosting memiliki batasan timeout dan memory

### Solusi yang Telah Diterapkan

#### 1. Enhanced AJAX Handlers
- **File**: `pgn/module/report/ajax_shipment_detail.php`
- **File**: `rwl/module/report/ajax_shipment_detail.php`
- **Perbaikan**:
  - Robust error handling dengan try-catch
  - Proper output buffering management
  - Header checking dengan `!headers_sent()`
  - Extended timeout (30s login, 60s query)
  - Clean JSON response handling

#### 2. Improved JavaScript Functions
- **File**: `pgn/module/report/shipment_detail.php`
- **File**: `rwl/module/report/shipment_detail.php`
- **Perbaikan**:
  - Absolute URL paths untuk hosting
  - Enhanced error handling dalam AJAX calls
  - Proper timeout management (30 detik)
  - Comprehensive error logging

#### 3. Diagnostic Tools
- **File**: `diagnostic.php` - Sistem diagnostik lengkap
- **File**: `quick_fix.php` - Perbaikan otomatis
- **File**: `monitoring.php` - Monitoring real-time

### Langkah Troubleshooting

#### Step 1: Upload File Diagnostic
1. Upload `diagnostic.php` ke root folder hosting
2. Akses `http://yourdomain.com/dsp-report/diagnostic.php`
3. Periksa hasil diagnostic:
   - PHP version dan extensions
   - File permissions
   - Database connections
   - AJAX endpoint tests

#### Step 2: Jalankan Quick Fix
1. Upload `quick_fix.php` ke root folder hosting
2. Akses `http://yourdomain.com/dsp-report/quick_fix.php`
3. File ini akan otomatis:
   - Memperbaiki AJAX handlers
   - Update JavaScript functions
   - Implementasi hosting-compatible code

#### Step 3: Monitor System
1. Upload `monitoring.php` ke root folder hosting
2. Akses `http://yourdomain.com/dsp-report/monitoring.php`
3. Gunakan untuk:
   - Test PGN dan RWL endpoints
   - Monitor error log real-time
   - Auto-refresh monitoring

### Perbedaan Konfigurasi

#### Localhost vs Hosting
| Aspect | Localhost | Hosting |
|--------|-----------|---------|
| PHP Version | 7.4+ | Varies |
| Output Buffering | Flexible | Strict |
| Error Reporting | Enabled | Disabled |
| Timeout | Unlimited | Limited |
| Memory Limit | High | Restricted |

#### Database Connections
- **PGN**: 103.133.223.211 (AS/111222)
- **RWL**: 103.133.223.212 (dspreport/R3wuluindah)

### File Structure
```
dsp-report/
├── diagnostic.php          # Diagnostic tool
├── quick_fix.php          # Auto-fix tool
├── monitoring.php         # Real-time monitoring
├── pgn/
│   └── module/
│       └── report/
│           ├── shipment_detail.php
│           └── ajax_shipment_detail.php
└── rwl/
    └── module/
        └── report/
            ├── shipment_detail.php
            └── ajax_shipment_detail.php
```

### Troubleshooting Checklist

#### ✅ PHP Configuration
- [ ] PHP version 7.4 atau lebih baru
- [ ] SQLSRV extension enabled
- [ ] JSON extension enabled
- [ ] Output buffering enabled
- [ ] Error reporting configured

#### ✅ File Permissions
- [ ] All PHP files readable (644)
- [ ] Folders accessible (755)
- [ ] No permission errors in logs

#### ✅ Database Connectivity
- [ ] PGN database connection works
- [ ] RWL database connection works
- [ ] Query execution successful
- [ ] No timeout errors

#### ✅ AJAX Functionality
- [ ] PGN AJAX endpoint responds
- [ ] RWL AJAX endpoint responds
- [ ] JSON responses valid
- [ ] Error handling works

### Common Issues & Solutions

#### Issue 1: Headers Already Sent
**Error**: `Cannot modify header information - headers already sent`
**Solution**: Implemented `!headers_sent()` check in AJAX handlers

#### Issue 2: Output Buffer Issues
**Error**: Blank responses or corrupted JSON
**Solution**: Proper output buffer management with `ob_start()` and `ob_clean()`

#### Issue 3: Database Timeout
**Error**: Connection timeout or query timeout
**Solution**: Extended timeout settings (30s login, 60s query)

#### Issue 4: JavaScript Errors
**Error**: AJAX calls fail silently
**Solution**: Enhanced error handling and logging in JavaScript

### Monitoring Commands

#### Test PGN System
```bash
curl -X POST http://yourdomain.com/dsp-report/pgn/module/report/ajax_shipment_detail.php \
  -d "get_detail=1&shipment_id=TEST123"
```

#### Test RWL System
```bash
curl -X POST http://yourdomain.com/dsp-report/rwl/module/report/ajax_shipment_detail.php \
  -d "get_detail=1&shipment_id=TEST123"
```

### Error Log Analysis

#### Common Error Patterns
1. **500 Internal Server Error**: PHP syntax atau configuration issue
2. **404 Not Found**: File path atau permission issue
3. **Empty Response**: Output buffering atau header issue
4. **JSON Parse Error**: Invalid JSON response

#### Log Locations
- Server error log: `/var/log/apache2/error.log`
- PHP error log: `/var/log/php_errors.log`
- Custom monitoring: `monitoring.php`

### Maintenance Tasks

#### Daily Checks
1. Access monitoring.php
2. Test both PGN and RWL endpoints
3. Check error logs for patterns
4. Verify database connections

#### Weekly Tasks
1. Run diagnostic.php
2. Review performance metrics
3. Check for new errors
4. Update documentation

### Contact Information
- **Developer**: GitHub Copilot
- **System**: DSP Report Tracking
- **Version**: 2.0 (Hosting Compatible)
- **Last Update**: 2024-01-20

### Notes
- Kedua sistem (PGN dan RWL) sekarang identik dalam struktur
- Perbedaan hanya pada database credentials
- Semua file telah di-optimasi untuk hosting environment
- Monitoring tools tersedia untuk troubleshooting real-time
