# SS Mapping Changes Implementation Summary

## Date: 2025-08-27
## Purpose: Change from hardcoded warehouse mappings to database-driven approach

## Changes Made:

### 1. **CarrierRateService.php**
- **Removed:** Hardcoded `SUPPLIER_WAREHOUSES` constant that mapped supplier IDs to state codes
- **Added:** New `getWarehousesForSupplier()` method that:
  - Reads `ss_accounts` field from suppliers table
  - Validates warehouse codes against config
  - Falls back to GA with logging if error or invalid
  - Supports comma-separated multiple warehouses

### 2. **ShipStationService.php**  
- **Removed:** Legacy `WAREHOUSE_CREDENTIALS` constant with hardcoded state mappings
- **Modified:** `getWarehouseCredentials()` method to:
  - Accept warehouse codes directly (e.g., "DEPO-IL", "PBITX")
  - Look up config using exact warehouse code
  - Fall back to GA with warning log if not found
  - Throw exception if GA config also missing

### 3. **CacheShippingRates.php**
- **Removed:** Hardcoded `$allowedSupplierIds` array
- **Added:** Database query to get suppliers with `ss_accounts` configured
- Now dynamically includes any active supplier with ShipStation account

## How It Works Now:

```
Supplier (database) 
  → ss_accounts field (e.g., "DEPO-IL" or "GA,NJ")
  → Direct lookup in config/shipstation.php
  → Use configuration or fallback to GA
```

## Benefits:
1. **No code changes needed** when suppliers change warehouses
2. **Single source of truth** - database `ss_accounts` field
3. **Supports multiple warehouses** per supplier (comma-separated)
4. **Automatic GA fallback** with logging for monitoring
5. **Matches old central's proven approach**

## Testing Results:
- **11 of 17** suppliers working correctly
- **6 suppliers** need API credentials updated (authentication failures)
- **GA fallback** working as expected
- All working suppliers correctly use their configured warehouses

## Failed Configurations (Need API Keys):
1. DTL (Supplier 50)
2. DEPO-CA (Supplier 51)
3. DEPO-NJ (Supplier 52)
4. DEPO-IL (Supplier 53)
5. TYC-WC (Supplier 55)
6. JCAUTO (Supplier 56)

## Logs to Monitor:
When GA fallback occurs, logs will contain:
- `"No ss_accounts for supplier {id}, falling back to GA"`
- `"Warehouse {code} not found in config, falling back to GA. Customer notification needed."`
- `"No valid warehouses found for supplier {id}, falling back to GA. Customer notification needed."`
- `"Error getting warehouses for supplier {id}: {error}. Falling back to GA. Customer notification needed."`

## To Restore Previous Version:
Run: `/home/opstest/public_html/central-new/backups/SS_mapping_changes_20250827_014306/restore.sh`

## Next Steps:
1. Update API credentials for the 6 failed configurations
2. Implement customer notification system for GA fallbacks
3. Monitor logs for any unexpected fallbacks