1800)) { header("Location: logout.php"); exit(); } $_SESSION['last_activity'] = time(); require_once 'db_connection.php'; $pageTitle = "Sales"; // Handle form submission if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_sale'])) { try { $date = $_POST['date']; $buyer_id = intval($_POST['buyer_id']); $weight_tilapia = floatval($_POST['weight_tilapia']); $weight_small_fish = floatval($_POST['weight_small_fish']); $weight_big_fish = floatval($_POST['weight_big_fish']); $include_delivery = isset($_POST['include_delivery']) ? 1 : 0; $final_amount = floatval($_POST['final_amount']); $harvesting_charges = floatval($_POST['harvesting_charges']); $stmt = $conn->prepare("INSERT INTO sales (date, buyer_id, weight_tilapia, weight_small_fish, weight_big_fish, include_delivery, final_amount, harvesting_charges) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("sidddidi", $date, $buyer_id, $weight_tilapia, $weight_small_fish, $weight_big_fish, $include_delivery, $final_amount, $harvesting_charges); $stmt->execute(); $stmt->close(); $_SESSION['success'] = "Sale added successfully!"; header("Location: sales.php"); exit(); } catch (Exception $e) { $_SESSION['error'] = "Error adding sale."; error_log("Sales error: " . $e->getMessage()); } } // Get buyers $buyersQuery = "SELECT id, name FROM buyers ORDER BY name"; $buyers = $conn->query($buyersQuery)->fetch_all(MYSQLI_ASSOC); // Pagination $page = isset($_GET['page']) ? intval($_GET['page']) : 1; $per_page = 20; $offset = ($page - 1) * $per_page; // Get total sales count $totalQuery = "SELECT COUNT(*) as total FROM sales"; $totalResult = $conn->query($totalQuery); $totalSales = $totalResult->fetch_assoc()['total']; $totalPages = ceil($totalSales / $per_page); // Get sales $salesQuery = "SELECT s.*, b.name as buyer_name FROM sales s JOIN buyers b ON s.buyer_id = b.id ORDER BY s.date DESC LIMIT $offset, $per_page"; $sales = $conn->query($salesQuery)->fetch_all(MYSQLI_ASSOC); // Generate buyer options $buyerOptions = ''; foreach ($buyers as $buyer) { $buyerOptions .= ""; } // Generate sales rows $salesRows = ''; foreach ($sales as $sale) { $salesRows .= " {$sale['date']} {$sale['buyer_name']} " . number_format($sale['weight_tilapia'], 2) . " kg " . number_format($sale['weight_small_fish'], 2) . " kg " . number_format($sale['weight_big_fish'], 2) . " kg " . ($sale['include_delivery'] ? 'Yes' : 'No') . " ₹" . number_format($sale['final_amount'], 2) . "
"; } // Generate pagination $pagination = ''; if ($totalPages > 1) { $pagination = ''; } $content = <<

Add New Sale

Sales List

Export CSV
$salesRows
Date Buyer Tilapia Small Fish Big Fish Delivery Amount Actions
$pagination
HTML; include 'main_layout.php'; $conn->close(); ?>