prepare($sql); $stmt->bind_param("ss", $date_from, $date_to); $stmt->execute(); $result = $stmt->get_result(); if ($result) { $row = $result->fetch_assoc(); $total_income = $row['net_sales'] ?? 0; } else { $total_income = 0; } echo "
Total Income Debug: "; print_r($row); echo "
"; // 2. COGS (based on quantity × cost_price) $cogs = 0; $sql = " SELECT si.quantity, p.cost_price FROM sales_items si INNER JOIN products p ON si.product_id = p.id INNER JOIN sales_transactions st ON si.transaction_id = st.id WHERE st.status = 'completed' AND st.created_at BETWEEN ? AND ? "; $stmt = $conn->prepare($sql); $stmt->bind_param("ss", $date_from, $date_to); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $cogs += $row['quantity'] * $row['cost_price']; } // 3. Gross Profit $gross_profit = $total_income - $cogs; // 4. Operating Expenses $operating_expenses = 0; $sql = " SELECT SUM(amount) AS total_expense FROM expenses WHERE status = 'approved' AND deleted_at IS NULL AND expense_date BETWEEN ? AND ? "; $stmt = $conn->prepare($sql); $stmt->bind_param("ss", $date_from, $date_to); $stmt->execute(); $result = $stmt->get_result()->fetch_assoc(); $operating_expenses = $result['total_expense'] ?? 0; // 5. Net Profit $net_profit = $gross_profit - $operating_expenses; ?>

Income Statement

Period: to

Total Income (Net of VAT) MWK
Cost of Goods Sold (COGS) MWK
Gross Profit MWK
Operating Expenses MWK
Net Profit MWK