query($query); while ($row = $result->fetch_assoc()) { // Use override amount if set, otherwise use final_amount $display_amount = ($row['final_amount_override'] != null && $row['final_amount_override'] > 0) ? $row['final_amount_override'] : $row['final_amount']; fputcsv($output, array( $row['date'], $row['buyer_name'], number_format($row['weight_tilapia'], 2), number_format($row['weight_small_fish'], 2), number_format($row['weight_big_fish'], 2), $row['include_delivery'] ? 'Yes' : 'No', number_format($display_amount, 2) )); } fclose($output); $conn->close(); } catch (Exception $e) { header('Content-Type: text/plain'); echo "Export error"; error_log("Sales export error: " . $e->getMessage()); } exit(); ?>