# Cost vs COGS Formula Fix ✅

**Date**: 2025-10-31
**Issue**: Order 42308-0038 showing incorrect Cost value
**Expected Cost**: $40.39
**Status**: FIXED

---

## 🔍 Root Cause Analysis

The NEW page was incorrectly using the same value for both **Cost** and **COGS** columns.

### What the OLD Central Code Does:

The OLD profitability page (`/home/opsgoparts/www/central/app/views/profitability/index-improved.phtml`) uses **TWO DIFFERENT formulas**:

1. **Cost** (line 1898) = `$order->our_buy_price` only
   - This is the simple buy price from supplier
   - Example: $40.39

2. **COGS** (line 1902) = `$goodSoldCost` (comprehensive calculation)
   - This includes all fees and costs
   - Example: $57.23

---

## 📐 OLD Central COGS Formula

```php
// Lines 1781-1791 in index-improved.phtml
$lossAmount = $order->sales_price * (($order->loss_rate ?: 0) / 100);

$goodSoldCost = $order->our_buy_price
              + ($order->commission_and_fee ?: 0)
              + ($order->payment_processor_fee ?: 0)
              + ($order->supplier_shipping ?: 0)
              + ($order->supplier_handling ?: 0)
              - ($order->supplier_credits ?: 0)
              + $lossAmount;
```

### Formula Breakdown:
- ✅ `our_buy_price` - Base cost from supplier
- ✅ `commission_and_fee` - Channel commission (stored in DB)
- ✅ `payment_processor_fee` - Payment processing fees
- ✅ `supplier_shipping` - Shipping from supplier
- ✅ `supplier_handling` - Handling fees from supplier
- ❌ `supplier_credits` - Credits from supplier (SUBTRACTED)
- ✅ `loss_amount` - Calculated from loss_rate percentage

---

## 🔧 Fix Applied

### File Modified:
`/home/centralgoparts/public_html/resources/views/filament/pages/profitability.blade.php`

### Changes Made:

**1. Added COGS Calculation (Lines 1772-1781)**
```php
// Calculate COGS using OLD central formula
// good_sold_cost = our_buy_price + commission_and_fee + payment_processor_fee + supplier_shipping + supplier_handling - supplier_credits + loss_amount
$lossAmount = $order->sales_price * (($order->loss_rate ?: 0) / 100);
$goodSoldCost = $order->our_buy_price
              + ($order->commission_and_fee ?: 0)
              + ($order->payment_processor_fee ?: 0)
              + ($order->supplier_shipping ?: 0)
              + ($order->supplier_handling ?: 0)
              - ($order->supplier_credits ?: 0)
              + $lossAmount;
```

**2. Updated COGS % Calculation (Lines 1783-1785)**
```php
// Calculate COGS % (based on goodSoldCost, not totalCosts)
$cogsPercent = $order->sales_price > 0 ? ($goodSoldCost / $order->sales_price) * 100 : 0;
$cogsClass = $cogsPercent >= 100 ? 'text-danger' : ($cogsPercent >= 70 ? 'text-warning' : 'text-success');
```

**3. Updated Display Values (Lines 1851-1856)**
```php
// Cost = just the buy price
<div class="financial-metric">
    <div class="label">Cost</div>
    <div class="value">${{ number_format($order->our_buy_price, 2) }}</div>
</div>

// COGS = full calculation
<div class="financial-metric">
    <div class="label">COGS</div>
    <div class="value">${{ number_format($goodSoldCost, 2) }}</div>
</div>
```

---

## 📊 Before vs After

### For Order 42308-0038:

| Metric | Before (Wrong) | After (Correct) | Change |
|--------|---------------|-----------------|--------|
| **Cost** | $57.23 (totalCosts) | $40.39 (our_buy_price) | ✅ Fixed |
| **COGS** | $57.23 (totalCosts) | $57.23 (goodSoldCost) | ✅ Correct |
| **COGS %** | Based on totalCosts | Based on goodSoldCost | ✅ Fixed |

### Explanation:
- **Cost** now shows the simple buy price ($40.39)
- **COGS** shows the comprehensive calculation with all fees ($57.23)
- Both values now match the OLD central system exactly

---

## 🎯 Why Two Different Values?

The OLD system distinguishes between:

1. **Cost (our_buy_price)**: What we paid the supplier for the part
   - This is the "pure" cost of the item
   - Used for quick reference

2. **COGS (good_sold_cost)**: Total cost of goods sold
   - Includes ALL costs to fulfill the order
   - Commission, fees, shipping, handling, loss rate
   - Subtracts supplier credits
   - This is the "true" cost for profitability analysis

---

## 🧮 Example Calculation

For an order with:
- Buy Price: $40.39
- Commission: $15.00
- Payment Fee: $1.84
- Supplier Shipping: $0.00
- Supplier Handling: $0.00
- Supplier Credits: $0.00
- Loss Rate: 0%

**Cost** = $40.39 (just the buy price)

**COGS** = $40.39 + $15.00 + $1.84 + $0.00 + $0.00 - $0.00 + $0.00 = **$57.23**

**COGS %** = ($57.23 / $70.00) × 100 = **81.8%**

---

## ✅ Verification Checklist

To verify the fix for order 42308-0038:

- [ ] Navigate to NEW page: https://central.go-parts.com/profitability
- [ ] Search for order: 42308-0038
- [ ] Verify **Cost** shows $40.39 (not $57.23)
- [ ] Verify **COGS** shows $57.23 (full calculation)
- [ ] Compare to OLD page: https://ops.go-parts.com/profitability/indexImproved
- [ ] Confirm values match exactly

---

## 📝 Related Fixes

This fix is part of the larger profitability page alignment project:

1. ✅ Cost calculation fixed (this document)
2. ✅ COGS calculation using OLD formula
3. ✅ COGS % based on goodSoldCost (not totalCosts)
4. ✅ Customer name added
5. ✅ Layout matches OLD page (6 metrics)
6. ✅ Expand chevron added
7. ✅ Dark mode enabled

---

## 🚀 Impact

**Better Data Accuracy**: Cost now shows the true buy price, making it easier to:
- Quickly see supplier costs
- Compare COGS vs Cost to understand fee impact
- Analyze profitability more accurately

**Matches OLD System**: NEW page now shows identical values to OLD page for:
- Cost column
- COGS column
- COGS % calculation

---

## 📁 Files Modified

1. **resources/views/filament/pages/profitability.blade.php**
   - Lines 1772-1785: Added COGS calculation
   - Lines 1851-1856: Updated display values

---

## ✨ Summary

The profitability page now correctly distinguishes between:
- **Cost** = Simple buy price from supplier
- **COGS** = Comprehensive cost including all fees

Both formulas match the OLD central system exactly! 🎉

Order 42308-0038 will now show:
- Cost: $40.39 ✅
- COGS: $57.23 ✅
- COGS %: Calculated from COGS ✅
