prepare($salesQuery); $salesStmt->bind_param("is", $buyerId, $date); $salesStmt->execute(); $salesResult = $salesStmt->get_result(); $totalSales = $salesResult->fetch_assoc()['total_sales'] ?? 0; // Get total payments $paymentsQuery = "SELECT SUM(amount_paid) as total_payments FROM payments WHERE buyer_id = ? AND date <= ?"; $paymentsStmt = $conn->prepare($paymentsQuery); $paymentsStmt->bind_param("is", $buyerId, $date); $paymentsStmt->execute(); $paymentsResult = $paymentsStmt->get_result(); $totalPayments = $paymentsResult->fetch_assoc()['total_payments'] ?? 0; // Get initial values for special buyers $initialQuery = "SELECT * FROM initial_values LIMIT 1"; $initialResult = $conn->query($initialQuery); $initialValues = $initialResult->fetch_assoc(); // Get buyer name $buyerQuery = "SELECT name FROM buyers WHERE id = ?"; $buyerStmt = $conn->prepare($buyerQuery); $buyerStmt->bind_param("i", $buyerId); $buyerStmt->execute(); $buyerResult = $buyerStmt->get_result(); $buyer = $buyerResult->fetch_assoc(); // Add initial balance if it's one of the special buyers $initialBalance = 0; if ($buyer && $initialValues) { if ($buyer['name'] == 'Madhu') { $initialBalance = $initialValues['madhu_balance'] ?? 0; } elseif ($buyer['name'] == 'Rathna') { $initialBalance = $initialValues['rathna_balance'] ?? 0; } elseif ($buyer['name'] == 'Jambukkutti') { $initialBalance = $initialValues['jambukkutti_balance'] ?? 0; } } // Calculate balance return $totalSales - $totalPayments + $initialBalance; } // Helper function to generate buyer options function generateBuyerOptions($buyers, $selectedBuyerId) { $options = ''; foreach ($buyers as $buyer) { $selected = $buyer['id'] == $selectedBuyerId ? 'selected' : ''; $options .= ""; } return $options; } // Fetch payment data $query = "SELECT * FROM payments WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("i", $paymentId); $stmt->execute(); $result = $stmt->get_result(); $payment = $result->fetch_assoc(); if (!$payment) { $_SESSION['flash_message'] = "Payment not found"; header("Location: payments.php"); exit(); } // Fetch buyers for dropdown $buyersQuery = "SELECT id, name FROM buyers ORDER BY name"; $buyersResult = $conn->query($buyersQuery); $buyers = $buyersResult->fetch_all(MYSQLI_ASSOC); // Handle form submission if ($_SERVER["REQUEST_METHOD"] == "POST") { $date = $_POST["date"]; $buyerId = intval($_POST["buyer_id"]); $amountPaid = floatval($_POST["amount_paid"]); // Update payment $updateQuery = "UPDATE payments SET date = ?, buyer_id = ?, amount_paid = ? WHERE id = ?"; $updateStmt = $conn->prepare($updateQuery); $updateStmt->bind_param("sidi", $date, $buyerId, $amountPaid, $paymentId); if ($updateStmt->execute()) { // Recalculate balance $balance = calculateBalance($conn, $buyerId, $date); $balanceUpdateQuery = "UPDATE payments SET balance = ? WHERE id = ?"; $balanceUpdateStmt = $conn->prepare($balanceUpdateQuery); $balanceUpdateStmt->bind_param("di", $balance, $paymentId); $balanceUpdateStmt->execute(); $_SESSION['flash_message'] = "Payment updated successfully"; header("Location: payments.php"); exit(); } else { $error = "Error updating payment: " . $conn->error; } } // Generate buyer options $buyerOptions = generateBuyerOptions($buyers, $payment['buyer_id']); // Build content $content = <<