# Statistics Export Script - Fix Summary

## What I Found

I analyzed your Statistics export script and found **multiple critical issues** preventing proper export for several tabs. I've created:

1. **`EXPORT_ISSUES_ANALYSIS.md`** - Detailed technical analysis of all issues
2. **`export_script_CORRECTED.py`** - Fixed version of your script

## Quick Summary of Issues

### ✅ Working Tabs (8/13)
- Scanner Details (tab 1)
- Sorter Summary (tab 2)
- Sorter Details (tab 3) - has minor typo but works
- Lane Details (tab 4)
- Hourly Induct (tab 5)
- Hourly Scanner (tab 6)
- Hourly Sorter Summary (tab 7)
- Hourly Sorter Details (tab 8)

### ❌ Broken Tabs (5/13)

1. **Induct Details (tab 0)** - CRITICAL
   - Query doesn't include single/double carrier fields
   - Script tries to access non-existent fields
   - **FIX:** Simplified to only export Total fields (carrier breakdown removed)

2. **Hourly Lane (tab 9)** - MODERATE
   - Wrong field names for filtered results
   - **FIX:** Updated to use correct field names

3. **Lane Total Full (tab 10)** - CRITICAL
   - Script had Dumper Cycles logic instead of Total Full logic
   - Completely wrong data structure
   - **FIX:** Rewrote entire section with correct fields

4. **Jam by Area (tab 11)** - MINOR
   - Case sensitivity issue: `Total_Jam` should be `Total_jam`
   - **FIX:** Changed to lowercase

5. **Dumper cycles (tab 12)** - NEEDS VERIFICATION
   - Wrong number of lanes (3 vs 8)
   - Wrong lane names (ULGL vs ULC)
   - **FIX:** Updated to ULC1-8, but needs testing

## How to Apply the Fix

### Option 1: Replace Entire Script (Recommended)
Copy the contents of `export_script_CORRECTED.py` and replace your current export script.

### Option 2: Manual Fixes
If you prefer to fix the original script manually, apply these changes:

#### Fix 1: Induct Details (Tab 0) - Remove Carrier Fields
Replace lines 15-40 with simplified version that only exports Total fields (see corrected script).

#### Fix 2: Sorter Details (Tab 3) - Fix Typo
Line ~155 (in else block):
```python
# WRONG:
data.append([..., result['sorted_count'], result['sorted_count'], ...])

# CORRECT:
data.append([..., result['sorted_count'], result['assigned_count'], ...])
```

#### Fix 3: Hourly Lane (Tab 9) - Fix Field Names
Replace the filtered results section (lines ~264-280) to use the same field structure as unfiltered data.

#### Fix 4: Lane Total Full (Tab 10) - Complete Rewrite
Replace lines ~285-310 with:
```python
headers = ["Lane", "Total (#/% /pph)", "DestFull (#/% /pph)"]
# Access: result['Lane'], result['Total_count'], result['DestFull_count']
```

#### Fix 5: Jam by Area (Tab 11) - Fix Case
Change `result['Total_Jam']` to `result['Total_jam']` (lowercase 'j')

#### Fix 6: Dumper Cycles (Tab 12) - Update Lane Names
Change from ULGL1/2/3 to ULC1-8 (8 lanes total)

## Important Notes

### Induct Details - Missing Carrier Data
The `Statistics/Induct Details` query **does not return single/double carrier data**. To restore this functionality:

**Option A:** Update the query to include carrier fields (use `TestQ/query.sql` as template)
**Option B:** Keep the simplified version (recommended for now)

### Dumper Cycles - Needs Verification
The Dumper Cycles fix is based on the view configuration, but the actual query field names need verification:
- Check if fields are `ulc1`, `ulc2`, etc. or `Cycles of ULC1`, `Cycles of ULC2`, etc.
- Test the export and adjust if needed

### Data Sources Not in Statistics Folder
- **Jam Area**: Uses query `Jam_Area/Jam` (different project folder)
- **Dumper Cycles**: Uses queries `Dumper/Dumper Lane Count/Percent/Rate` (different project folder)

## Testing Checklist

After applying the fix, test each tab:

- [ ] Tab 0: Induct Details (simplified - no carrier breakdown)
- [ ] Tab 1: Scanner Details (should work)
- [ ] Tab 2: Sorter Summary (should work)
- [ ] Tab 3: Sorter Details (typo fixed)
- [ ] Tab 4: Lane Details (should work)
- [ ] Tab 5: Hourly Induct (should work)
- [ ] Tab 6: Hourly Scanner (should work)
- [ ] Tab 7: Hourly Sorter Summary (should work)
- [ ] Tab 8: Hourly Sorter Details (should work)
- [ ] Tab 9: Hourly Lane (field names fixed)
- [ ] Tab 10: Lane Total Full (completely rewritten)
- [ ] Tab 11: Jam by Area (case fixed)
- [ ] Tab 12: Dumper Cycles (needs verification)

## Next Steps

1. Review the corrected script (`export_script_CORRECTED.py`)
2. Apply the fix (replace your current script)
3. Test each tab's export functionality
4. If Dumper Cycles fails, check the actual query field names and adjust
5. If you need carrier breakdown in Induct Details, update the query first

## Need Help?

- See `EXPORT_ISSUES_ANALYSIS.md` for detailed technical analysis
- Check the inline comments in `export_script_CORRECTED.py` for specific changes
- Test with Count mode first, then Percentage and Rate modes

