# Shipping Rates & Memcached System Comparison
## Classic Central (ops.go-parts.com) vs New Central (opstest.go-parts.com/central-new)

## Executive Summary

Both systems implement shipping rate caching, but with significantly different approaches:
- **Classic Central**: Uses direct Memcached with a shared global instance
- **New Central**: Uses Laravel's cache abstraction (currently file-based, not Memcached)
- **Key Finding**: They do NOT share the same cache storage currently

## 1. Shipping Rates Caching Architecture

### Classic Central System
- **Cache Backend**: Direct Memcached (127.0.0.1:11211)
- **Cache Duration**: 2 days (172800 seconds) for successful rates
- **Error Cache**: 2 hours for failed API calls
- **Cache Key Format**: `{warehouse}__md5({request_data})`
- **Key Prefix**: Uses `ph-memc-` prefix for all keys
- **Implementation**: Direct PHP Memcached calls

### New Central System  
- **Cache Backend**: Laravel file cache (storage/framework/cache)
- **Cache Duration**: 10 days (864000 seconds) for successful rates
- **Error Cache**: 2 hours for failed API calls
- **Cache Key Format**: Same as classic - `{warehouse}__md5({request_data})`
- **Implementation**: Laravel Cache facade with abstraction layer

### Key Differences
1. **Storage Location**: Different - Classic uses Memcached, New uses file cache
2. **Cache Duration**: New system caches 5x longer (10 days vs 2 days)
3. **Key Prefix**: Classic adds `ph-memc-` prefix, New doesn't
4. **Abstraction**: New uses Laravel's cache abstraction allowing easy switching

## 2. Memcached Usage Analysis

### Evidence from Testing
Running `view_memcached.php` shows:
- 860+ keys currently in Memcached
- Keys are prefixed with `ph-memc-` (Classic Central format)
- Sample keys: `ph-memc-GA__fb11b1f1b123d3152e816c2bbfe10397`
- All carrier rate entries follow Classic Central's structure

### Shared Memcached Instance?
**NO** - They don't share the same cache storage:
- Classic Central writes to Memcached with `ph-memc-` prefix
- New Central writes to file cache at `/home/opstest/public_html/central-new/storage/framework/cache`
- Config shows New Central has `CACHE_DRIVER=file` not `memcached`

### Migration Path
To share the same Memcached:
1. Change New Central's `.env`: `CACHE_DRIVER=memcached`
2. Configure Memcached connection in `config/cache.php`
3. Add key prefix matching Classic Central (`ph-memc-`)
4. Ensure cache key generation logic matches exactly

## 3. Shipping Rate Calculation Logic

### Common Elements
Both systems:
- Calculate rates via ShipStation API
- Use UPS as primary carrier
- Apply 10% markup to UPS rates
- Cache based on destination + dimensions (NOT per order)
- Fall back to default rates on API failure ($12.03 for UPS)

### Supplier-Specific Handling

| Supplier | Classic Central | New Central | Match? |
|----------|----------------|-------------|---------|
| **ELT** | 10% of price as handling (min $6) | Same | ✅ Yes |
| **DEPO** | Carrier handling from API | Same | ✅ Yes |
| **EXPRESS** | 30% markup on shipping | Same | ✅ Yes |
| **TYC** | Carrier handling from API | Same | ✅ Yes |
| **PBI** | Carrier handling ($9 built into price) | Same | ✅ Yes |
| **KEYSTONE** | Uses DB values when available | Same | ✅ Yes |
| **MEYER** | Uses DB values | Same | ✅ Yes |
| **PERFRAD** | Uses DB values | Same | ✅ Yes |
| **USAUTO** | Uses DB values | Not explicitly coded | ⚠️ Partial |

## 4. Supplier Badge & Display Logic

### New Central Implementation
Located in `/resources/views/filament/tables/columns/item-actions.blade.php`:

**Badge Display Hierarchy**:
1. **Auto Fulfill Section** (lines 64-106)
   - Shows for items with "Needs Order" status
   - Includes dropdown with preferred suppliers (USAVA, EXPRESS, KEYSTONE, PA)
   - "No Part" button for cancellation
   - "Show Log" link for autofulfill history

2. **Supplier Display** (lines 108-112)
   - Shows assigned supplier code in green (#059669)
   - Displays above action buttons
   - Hidden if no supplier assigned

3. **Action Buttons** (lines 114-140)
   - Update Supplier (green if assigned)
   - Additional Cost (green if exists)
   - Supplier Credit (green if exists)
   - Carrier Credit (green if exists)

4. **Fulfillment Order Badge** (lines 142-148)
   - Blue badge showing fulfillment order ID
   - Only displays if fulfillment_order_id exists

### Classic Central Implementation
Based on URL structure and integration points:
- Similar badge system but different UI framework
- Supplier badges likely inline with order items
- Less sophisticated auto-fulfill dropdown

## 5. Auto Fulfill Logic Comparison

### New Central Features
```javascript
// From item-actions.blade.php lines 557-594
autoFulfillItem(itemId, orderId, supplierCode)
- Preferred suppliers: USAVA, EXPRESS, KEYSTONE, PA
- API endpoint: /api/items/auto-fulfill
- Confirms before processing
- Reloads page after success
```

### Supplier Selection Priority
1. **Default**: USAVA (US Auto Virginia)
2. **Dropdown Options**: 
   - EXPRESS (Express suppliers)
   - KEYSTONE (Keystone Automotive)
   - PA (Parts Authority)

### Integration Points
- New Central can call Classic Central via `call_old_central.php`
- Used for email notifications (confirmation, tracking)
- HTTP calls to `http://localhost/central/` endpoints

## 6. Key Findings & Recommendations

### Current State Issues
1. **Cache Fragmentation**: Two separate cache systems
2. **Duration Mismatch**: 10 days vs 2 days cache TTL
3. **No Shared Data**: Systems can't benefit from each other's cached rates

### Recommendations for Unification

#### Short Term (Immediate)
1. **Enable Memcached in New Central**:
   ```env
   CACHE_DRIVER=memcached
   MEMCACHED_HOST=127.0.0.1
   MEMCACHED_PORT=11211
   ```

2. **Match Cache Keys**:
   - Add `ph-memc-` prefix to New Central keys
   - Ensure MD5 hash generation matches exactly

3. **Synchronize TTL**:
   - Standardize on 2-day cache duration
   - Keep 2-hour error cache

#### Long Term (Strategic)
1. **Unified Cache Service**:
   - Create shared caching microservice
   - Both systems call same API
   - Single source of truth for rates

2. **Database Consolidation**:
   - Migrate to single database
   - Shared supplier configuration
   - Unified shipping rules

3. **API Gateway**:
   - Single point for external API calls
   - Centralized rate limiting
   - Better error handling

## 7. Testing Verification

### Commands to Verify Cache Sharing
```bash
# Check New Central cache driver
grep CACHE_DRIVER /home/opstest/public_html/central-new/.env

# View Memcached contents
php /home/opstest/public_html/central-new/test-scripts/view_memcached.php

# Test cache key generation
php -r "echo md5(json_encode(['packageCode'=>'package','fromPostalCode'=>'30093']));"
```

### Expected Results After Unification
- Same cache keys in both systems
- Shared shipping rate data
- Reduced API calls to ShipStation
- Consistent shipping costs across systems

## Conclusion

While both systems implement similar business logic for shipping rates and supplier handling, they currently operate on completely separate cache infrastructures. The New Central system is well-architected to support switching to Memcached, requiring only configuration changes to achieve cache sharing with Classic Central. The supplier badge and auto-fulfill logic shows good feature parity with some enhancements in the New Central system.