Commit f8c11843 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(accounting): COGS could never post — sale_items has no total_cost column

onSaleCompleted summed sale_items.total_cost. That column does not exist; the
table stores a per-unit cost_price alongside quantity. Confirmed with SHOW
COLUMNS on the live database.

Every sale therefore threw "Unknown column" inside the sale.completed listener,
which is wrapped in a try/catch that only writes to the log. So inventory was
relieved in the stock ledger while the general ledger kept carrying it, and no
cost of sales was ever recognised — the gross margin on every sale was overstated
by its entire cost.

Now SUM(cost_price * quantity) over non-refunded lines. This also un-breaks
onSaleVoided, which reverses the 'sale_cogs' entry and could never find one.
Co-Authored-By: 's avatarClaude Opus 5 (1M context) <noreply@anthropic.com>
parent e06900e1
......@@ -235,9 +235,15 @@ final class AccountingIntegrationService
return;
}
// Calculate total cost from sale items
// Cost of the goods on this sale. sale_items stores a per-unit `cost_price`
// and a `quantity`; there is no `total_cost` column, so the previous query
// threw "Unknown column" on every sale. The listener is wrapped in a
// try/catch that only logs, so COGS silently never posted for any sale —
// inventory was relieved in the stock ledger while the GL kept carrying it.
$costRow = $db->selectOne(
"SELECT COALESCE(SUM(total_cost), 0) as total_cost FROM sale_items WHERE sale_id = ?",
"SELECT COALESCE(SUM(cost_price * quantity), 0) AS total_cost
FROM sale_items
WHERE sale_id = ? AND is_refunded = 0",
[$saleId]
);
$totalCost = (string) ($costRow['total_cost'] ?? '0.00');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment