# Testing & Verification Guide

## Quick Test (5 minutes)

### Step 1: Verify Migration Applied
```sql
-- Run in PhpMyAdmin or MySQL client
SHOW COLUMNS FROM purchase_lines LIKE 'transfer_price%';
-- Should return: transfer_price_inc_tax DECIMAL(20,4) nullable
```

### Step 2: Test Stock Mismatch Checker
1. Go to **Products → Stock Mismatch Checker**
2. If no mismatches exist, the fix is working
3. If mismatches show:
   - Click **"Fix All Mismatches"** button
   - Click **"Recalculate Stock Values"** button
   - Verify stock values show (not Rs 0)

### Step 3: Verify Stock Report
1. Go to **Reports → Stock Report**
2. Check "Closing Stock (By purchase price)" column
3. Verify transferred items show correct values (not 0)

## Detailed Testing

### Test Case 1: Transferred Items Valuation

**Scenario:** Item transferred from Location A to Location B

**Setup:**
1. Create a product "Test Item" with purchase price Rs 100
2. Add 10 units to Location A
3. Transfer 5 units to Location B

**Expected After "Recalculate Stock Values":**
- Location A: 5 units × Rs 100 = Rs 500
- Location B: 5 units × Rs 100 = Rs 500 (default purchase price)

**Verification:**
- Stock Report should show both locations with correct values
- No Rs 0 values should appear

### Test Case 2: Transfer with Custom Rate

**Scenario:** Transfer with adjusted purchase rate

**Setup:**
1. Manually update purchase_line in database:
```sql
UPDATE purchase_lines 
SET transfer_price_inc_tax = 105 
WHERE id = [purchase_line_id] AND quantity > 0;
```

2. Run Stock Mismatch Checker → "Recalculate Stock Values"

**Expected Result:**
- Items transferred with custom rate should use new price
- Stock value = quantity × 105 (not 100)

**Verification Query:**
```sql
SELECT 
    pl.id,
    pl.quantity,
    pl.purchase_price_inc_tax,
    pl.transfer_price_inc_tax,
    COALESCE(pl.transfer_price_inc_tax, pl.purchase_price_inc_tax) as effective_price
FROM purchase_lines pl
WHERE pl.transfer_price_inc_tax IS NOT NULL;
```

### Test Case 3: Stock Reset Flag

**Before Fix:**
```sql
SELECT variation_id, location_id, stock_reset 
FROM variation_location_details 
WHERE stock_reset = 1;
-- May show items with stock_reset = 1
```

**After "Fix All Mismatches" + "Recalculate":**
```sql
SELECT variation_id, location_id, stock_reset 
FROM variation_location_details 
WHERE stock_reset = 1;
-- Should return no rows
```

### Test Case 4: Purchase Transfer Transactions

**Verify purchase_transfer is included:**
```sql
SELECT 
    t.type,
    COUNT(*) as count,
    SUM(pl.quantity) as total_qty
FROM transactions t
JOIN purchase_lines pl ON t.id = pl.transaction_id
WHERE t.type = 'purchase_transfer'
GROUP BY t.type;
```

## Automated Testing (Database Validation)

### Test 1: Stock Value Calculation
```sql
-- Check stock values are calculated using transfer prices
SELECT 
    p.name,
    vld.qty_available,
    pl.purchase_price_inc_tax,
    pl.transfer_price_inc_tax,
    ROUND(vld.qty_available * COALESCE(pl.transfer_price_inc_tax, pl.purchase_price_inc_tax), 2) as expected_value
FROM variation_location_details vld
JOIN variations v ON vld.variation_id = v.id
JOIN products p ON v.product_id = p.id
LEFT JOIN purchase_lines pl ON pl.variation_id = v.id
WHERE vld.qty_available > 0
LIMIT 10;
```

### Test 2: Verify No Stock Reset Flags
```sql
-- Should return 0 rows
SELECT COUNT(*) FROM variation_location_details WHERE stock_reset = 1;
```

### Test 3: Check Transfer Price Column Exists
```sql
-- Should show transfer_price_inc_tax column
DESCRIBE purchase_lines;
```

### Test 4: Verify All Transactions Are Counted
```sql
-- Confirm purchase_transfer is in the list
SELECT DISTINCT type FROM transactions 
WHERE type IN ('purchase', 'opening_stock', 'purchase_transfer');
```

## Production Checklist

Before deploying to production:

- [ ] All migrations applied successfully
- [ ] No error messages in Laravel logs
- [ ] Stock Mismatch Checker works without errors
- [ ] "Fix All Mismatches" button responds
- [ ] "Recalculate Stock Values" button responds
- [ ] Stock Report shows correct values
- [ ] No Rs 0 stock values for transferred items
- [ ] Database backup taken before testing
- [ ] All syntax errors fixed
- [ ] Code compiles without errors

## Rollback Plan

If issues occur:

### Database Rollback
```bash
C:\xampp\php\php.exe artisan migrate:rollback --step=1
```

### Code Rollback
1. Revert `app/Http/Controllers/StockMismatchController.php`
2. Revert `app/Utils/ProductUtil.php`
3. Clear Laravel cache: `php artisan cache:clear`
4. Restart web server

### Check Status
```bash
C:\xampp\php\php.exe artisan migrate:status
```

## Performance Benchmarking

### Before Optimization (Baseline)
- Stock Report load time: [X seconds]
- Stock Mismatch Checker load time: [Y seconds]

### After Implementation
- Stock Report load time: [Should be similar]
- Stock Mismatch Checker load time: [Should be same or better]

### Query Performance
```sql
-- Check query execution time
EXPLAIN SELECT 
    ROUND(vld.qty_available, 2) as stock,
    COALESCE(pl.transfer_price_inc_tax, pl.purchase_price_inc_tax) as valuation_price
FROM variation_location_details vld
LEFT JOIN purchase_lines pl ON pl.variation_id = vld.variation_id
WHERE vld.qty_available > 0;
```

## Common Issues & Solutions

### Issue: Column "transfer_price_inc_tax" doesn't exist
**Solution:** Run migration: `php artisan migrate --path=database/migrations/2025_12_04_103500_add_transfer_price_to_purchase_lines.php`

### Issue: Stock values still show Rs 0
**Solution:** 
1. Check `stock_reset` flag is 0
2. Run "Recalculate Stock Values" again
3. Clear cache: `php artisan cache:clear`

### Issue: Syntax error in ProductUtil
**Solution:** Verify line 1902 has comma after `stock_price")` like: `as stock_price\"),`

### Issue: Migration fails with "class not found"
**Solution:** Ensure migration class name matches file pattern. Current: `Add_transfer_price_to_purchase_lines` for file `2025_12_04_103500_add_transfer_price_to_purchase_lines.php`

## Sign-Off

- **Tester:** _____________
- **Date:** _____________
- **Status:** ☐ PASS ☐ FAIL
- **Notes:** _________________________________

