# Dual File Upload System for Special Banks

## Overview
Some banks require **TWO separate loan files** to be uploaded for each reporting period before CECL processing can begin. This system provides a streamlined interface for uploading both files.

## Banks Requiring Dual File Upload

### 1. **fsbrosemount** (First State Bank of Rosemount)
- **File 1:** Regular Loans → `fsbrosemount-shortdate.csv`
- **File 2:** Student Loans → `fsbrosemount-StudentLoans-shortdate.csv`
- **Detection:** File 2 is identified by having "student" in the filename

### 2. **eurekasavings** (Eureka Savings Bank)
- **File 1:** Regular Loans → `eurekasavings-shortdate.csv`
- **File 2:** CECLML Loans → `eurekasavings-shortdateb.csv`
- **Detection:** File 2 is identified by having "ceclml" in the filename

### 3. **logansportsavings** (Logansport Savings Bank)
- **File 1:** Regular Loans → `logansportsavings-shortdate.csv`
- **File 2:** CECLMTG Loans → `logansportsavings-shortdateb.csv`
- **Detection:** File 2 is identified by having "ceclmtg" in the filename

## How It Works

### User Interface - Sequential Upload with Preview
When a special bank logs in, they see a **4-step sequential process**:

#### **Step 1: Upload Regular Loans File**
- Single upload zone with drag-drop or file picker
- Accepts any CSV file (no specific naming required)

#### **Step 2: Preview & Confirm Regular Loans**
- Shows preview of first 5 rows in a table format
- Displays filename
- User can:
  - **Confirm & Continue** → Uploads as regular file and proceeds to Step 3
  - **Cancel & Re-upload** → Returns to Step 1 to select a different file

#### **Step 3: Upload Special Loans File**
- Second upload zone appears (green-themed)
- Label shows specific file type needed:
  - "Student Loans File" (fsbrosemount)
  - "CECLML Loans File" (eurekasavings)
  - "CECLMTG Loans File" (logansportsavings)
- Accepts any CSV file

#### **Step 4: Preview & Confirm Special Loans**
- Shows preview of first 5 rows in a table format
- Displays filename
- User can:
  - **Confirm & Process** → Uploads as special file and starts CECL processing
  - **Cancel & Re-upload** → Returns to Step 3 to select a different file

### Upload Process Flow
1. **User selects File 1** (drag-drop or file picker)
2. **System reads file** → Displays first 5 rows for preview
3. **User confirms** → File uploaded to `uploads/shortname-shortdate.csv`
4. **User selects File 2** (drag-drop or file picker)
5. **System reads file** → Displays first 5 rows for preview
6. **User confirms** → File uploaded to `uploads/shortname-StudentLoans-shortdate.csv` (or `shortdateb.csv`)
7. **System redirects** to validation page
8. **System validates** column titles (validateTitlesTest.php)
9. **System copies BOTH files** from `uploads/` to `BankData/`
10. **System triggers Python processing** (loan_data_processor.py)

### File Naming Logic
The system uses the `fileType` parameter to determine naming:
- **fileType='regular'**: Always named as `shortname-shortdate.csv`
- **fileType='special'**: Named based on bank:
  - `fsbrosemount` → `shortname-StudentLoans-shortdate.csv`
  - `eurekasavings` → `shortname-shortdateb.csv`
  - `logansportsavings` → `shortname-shortdateb.csv`

**Users can name their files anything** - the system names them correctly based on upload order and fileType.

### Code Flow

#### 1. Frontend (`cecl-portal.php`)
```javascript
// Detect special bank
$isSpecialBank = in_array($shortname, ['fsbrosemount', 'eurekasavings', 'logansportsavings']);

// Show sequential 4-step interface if special bank
if ($isSpecialBank) {
    // Step 1: Single upload zone for file 1
    // Step 2: Preview & confirm file 1
    // Step 3: Single upload zone for file 2
    // Step 4: Preview & confirm file 2
}

// JavaScript functions:
previewFile(file, fileNum) {
    // Read first 5 rows of CSV
    // Display in table format
    // Show confirm/cancel buttons
}

confirmFile1() {
    // Upload with fileType='regular' and skipProcessing=1
    // Show step 3
}

confirmFile2() {
    // Upload with fileType='special'
    // Redirect to validation
}
```

#### 2. Upload Handler (`upload_handler_new.php`)
```php
$fileType = $_POST['fileType'] ?? 'regular'; // From frontend

// Determine correct filename based on fileType parameter
if ($isSpecialBank && $fileType === 'special') {
    if ($shortname === 'fsbrosemount') {
        $newFilename = $shortname . '-StudentLoans-' . $shortdate . '.csv';
    } elseif ($shortname === 'eurekasavings') {
        $newFilename = $shortname . '-' . $shortdate . 'b.csv';
    }
} else {
    $newFilename = $shortname . '-' . $shortdate . '.csv';
}

// Save to uploads/ directory
// Don't trigger processing if skipProcessing=1
```

#### 3. Validation (`validateTitlesTest.php`)
```php
// Check if special bank
if (in_array($shortname, $specialBanks)) {
    // Copy BOTH files from uploads/ to BankData/
    copy($sourceFile1, $targetFile1);
    copy($sourceFile2, $targetFile2);
}

// Then trigger loan_data_processor.py
```

## Key Features

### File Preview Before Upload
Users see the actual data before confirming:
- **First 5 rows displayed** in a formatted table
- **Header row highlighted** for easy identification
- Allows users to verify they selected the correct file
- Prevents upload mistakes

### Sequential Guided Process
Step-by-step workflow ensures:
- Clear instructions at each stage
- No confusion about which file goes where
- Visual progress (Step 1 of 2, Step 2 of 2)
- Can't proceed until each step is confirmed

### No Filename Requirements
Users can name their files ANYTHING:
- `loans.csv`
- `data_oct_2024.csv`
- `my_file_123.csv`
- System doesn't care about original filename
- System names files correctly based on upload order

### Skip Processing Flag
When uploading the first file, `skipProcessing=1` is sent to prevent premature processing. Only after the second file is confirmed and uploaded does the system trigger validation and processing.

### Visual Feedback
- Step badges show progress (Step 1 of 2, Step 2 of 2)
- Color coding (blue for file 1, green for file 2)
- Preview shows actual data from file
- Success messages after each upload
- Clear error messages if upload fails

## Error Handling

### Missing Second File
If validation is triggered but the second file is missing from `uploads/`:
```
Error: Second file not found in uploads directory. Both files must be uploaded.
```

### Upload Failure
If either file fails to upload:
- The error is displayed
- The other file (if uploaded) remains in `uploads/`
- User can retry the failed file
- Upload button is re-enabled

### File Copy Failure
If either file fails to copy to `BankData/`:
- Processing stops immediately
- Error message displayed
- Files remain in `uploads/` for retry

## Testing

To test the dual file upload system:
1. Log in as one of the special banks
2. Select a file date
3. Upload File 1 (regular loans)
4. Upload File 2 (special loan type with appropriate keyword in filename)
5. Click "Upload Both Files"
6. Verify both files appear in `uploads/` and then `BankData/`
7. Verify processing starts automatically

## Maintenance

To add a new bank requiring dual files:
1. Add shortname to `$specialBanks` array in:
   - `cecl-portal.php`
   - `upload_handler_new.php`
   - `validateTitlesTest.php`
2. Add file naming logic in `upload_handler_new.php`
3. Add UI labels in `cecl-portal.php`
4. Test thoroughly!

