1800)) { header("Location: logout.php"); exit(); } $_SESSION['last_activity'] = time(); require_once 'db_connection.php'; $pageTitle = "Base Setup"; // Handle form submissions if ($_SERVER["REQUEST_METHOD"] == "POST") { try { // Add Buyer if (isset($_POST['add_buyer'])) { $stmt = $conn->prepare("INSERT INTO buyers (name, phone, rate_tilapia, rate_small_fish, rate_big_fish, delivery_rate, harvesting_fee_tilapia, harvesting_fee_small_fish, harvesting_fee_big_fish) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("ssddddddd", $_POST['name'], $_POST['phone'], $_POST['rate_tilapia'], $_POST['rate_small_fish'], $_POST['rate_big_fish'], $_POST['delivery_rate'], $_POST['harvesting_fee_tilapia'], $_POST['harvesting_fee_small_fish'], $_POST['harvesting_fee_big_fish'] ); $stmt->execute(); $stmt->close(); $_SESSION['success'] = "Buyer added successfully!"; header("Location: base.php"); exit(); } // Add Expense Category if (isset($_POST['add_expense_category'])) { $stmt = $conn->prepare("INSERT INTO expense_categories (name) VALUES (?)"); $stmt->bind_param("s", $_POST['category_name']); $stmt->execute(); $stmt->close(); $_SESSION['success'] = "Expense category added successfully!"; header("Location: base.php"); exit(); } // Add Expense Head if (isset($_POST['add_expense_head'])) { $stmt = $conn->prepare("INSERT INTO expense_heads (name, category_id) VALUES (?, ?)"); $stmt->bind_param("si", $_POST['head_name'], $_POST['category_id']); $stmt->execute(); $stmt->close(); $_SESSION['success'] = "Expense head added successfully!"; header("Location: base.php"); exit(); } // Update Initial Values if (isset($_POST['update_initial_values'])) { // Check if initial values exist $checkQuery = "SELECT COUNT(*) as count FROM initial_values"; $result = $conn->query($checkQuery); $count = $result->fetch_assoc()['count']; if ($count > 0) { $stmt = $conn->prepare("UPDATE initial_values SET cash_in_hand = ?, madhu_balance = ?, rathna_balance = ?, jambukkutti_balance = ? WHERE id = 1"); } else { $stmt = $conn->prepare("INSERT INTO initial_values (cash_in_hand, madhu_balance, rathna_balance, jambukkutti_balance) VALUES (?, ?, ?, ?)"); } $stmt->bind_param("dddd", $_POST['cash_in_hand'], $_POST['madhu_balance'], $_POST['rathna_balance'], $_POST['jambukkutti_balance'] ); $stmt->execute(); $stmt->close(); $_SESSION['success'] = "Initial values updated successfully!"; header("Location: base.php"); exit(); } } catch (Exception $e) { $_SESSION['error'] = "An error occurred. Please try again."; error_log("Base.php error: " . $e->getMessage()); } } // Fetch all buyers $buyersQuery = "SELECT * FROM buyers ORDER BY name"; $buyers = $conn->query($buyersQuery)->fetch_all(MYSQLI_ASSOC); // Fetch all expense categories with their heads $categoriesQuery = "SELECT * FROM expense_categories ORDER BY name"; $categories = $conn->query($categoriesQuery)->fetch_all(MYSQLI_ASSOC); // Fetch initial values $initialQuery = "SELECT * FROM initial_values LIMIT 1"; $initialResult = $conn->query($initialQuery); $initialValues = $initialResult->fetch_assoc(); if (!$initialValues) { $initialValues = [ 'cash_in_hand' => 0, 'madhu_balance' => 0, 'rathna_balance' => 0, 'jambukkutti_balance' => 0 ]; } // Generate buyer rows $buyerRows = ''; foreach ($buyers as $buyer) { $buyerRows .= " " . htmlspecialchars($buyer['name']) . " " . htmlspecialchars($buyer['phone']) . " ₹" . number_format($buyer['rate_tilapia'], 2) . " ₹" . number_format($buyer['rate_small_fish'], 2) . " ₹" . number_format($buyer['rate_big_fish'], 2) . "
Edit Delete
"; } // Generate expense categories and heads $expenseCategoriesHtml = ''; foreach ($categories as $category) { $headsQuery = "SELECT * FROM expense_heads WHERE category_id = " . $category['id'] . " ORDER BY name"; $heads = $conn->query($headsQuery)->fetch_all(MYSQLI_ASSOC); $headsHtml = ''; foreach ($heads as $head) { $headsHtml .= "
  • {$head['name']}
  • "; } $expenseCategoriesHtml .= "

    {$category['name']}

    Delete Category
    "; } $content = <<

    Initial Values

    Add New Buyer

    Buyers List

    Export CSV
    $buyerRows
    Name Phone Tilapia Rate Small Fish Rate Big Fish Rate Actions

    Add Expense Category

    Add Expense Head