query($query); return $result->fetch_all(MYSQLI_ASSOC); } function getSales($conn, $page = 1, $per_page = 20) { $offset = ($page - 1) * $per_page; $query = "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"; $result = $conn->query($query); return $result->fetch_all(MYSQLI_ASSOC); } function getTotalSales($conn) { $query = "SELECT COUNT(*) as total FROM sales"; $result = $conn->query($query); return $result->fetch_assoc()['total']; } if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST["add_sale"])) { $date = $conn->real_escape_string($_POST['date']); $buyer_id = $conn->real_escape_string($_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 = !empty($_POST['final_amount_override']) ? floatval($_POST['final_amount_override']) : floatval($_POST['final_amount']); $harvesting_charges = floatval($_POST['harvesting_charges']); $query = "INSERT INTO sales (date, buyer_id, weight_tilapia, weight_small_fish, weight_big_fish, include_delivery, final_amount, harvesting_charges) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($query); $stmt->bind_param("sidddidd", $date, $buyer_id, $weight_tilapia, $weight_small_fish, $weight_big_fish, $include_delivery, $final_amount, $harvesting_charges); if ($stmt->execute()) { $updatedSales = getSales($conn); echo json_encode(['success' => true, 'salesRows' => generateSalesRows($updatedSales)]); } else { echo json_encode(['success' => false, 'error' => $stmt->error]); } exit; } elseif (isset($_POST['delete_sale'])) { $sale_id = $conn->real_escape_string($_POST['sale_id']); $query = "DELETE FROM sales WHERE id = $sale_id"; if ($conn->query($query) === TRUE) { $updatedSales = getSales($conn); echo json_encode(['success' => true, 'salesRows' => generateSalesRows($updatedSales)]); } else { echo json_encode(['success' => false, 'error' => $conn->error]); } exit; } elseif (isset($_POST['export_csv'])) { $query = "SELECT s.date, b.name as buyer_name, s.weight_tilapia, s.weight_small_fish, s.weight_big_fish, s.include_delivery, s.final_amount, s.harvesting_charges FROM sales s JOIN buyers b ON s.buyer_id = b.id ORDER BY s.date DESC"; $result = $conn->query($query); $filename = "sales_export_" . date('Y-m-d') . ".csv"; header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="' . $filename . '"'); $output = fopen('php://output', 'w'); fputcsv($output, ['Date', 'Buyer', 'Tilapia (kg)', 'Small Fish (kg)', 'Big Fish (kg)', 'Delivery Included', 'Final Amount', 'Harvesting Charges']); while ($row = $result->fetch_assoc()) { $row['include_delivery'] = $row['include_delivery'] ? 'Yes' : 'No'; fputcsv($output, $row); } fclose($output); exit; } elseif (isset($_POST['get_buyer_rates'])) { $buyer_id = $conn->real_escape_string($_POST['buyer_id']); $query = "SELECT rate_tilapia, rate_small_fish, rate_big_fish, delivery_rate, harvesting_fee_tilapia, harvesting_fee_small_fish, harvesting_fee_big_fish FROM buyers WHERE id = $buyer_id"; $result = $conn->query($query); $buyer = $result->fetch_assoc(); echo json_encode($buyer); exit; } } $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $per_page = 20; $total_sales = getTotalSales($conn); $total_pages = ceil($total_sales / $per_page); $buyers = getBuyers($conn); $sales = getSales($conn, $page, $per_page); function generateBuyerOptions($buyers) { $options = ''; foreach ($buyers as $buyer) { $options .= ""; } return $options; } function generateSalesRows($sales) { $rows = ''; foreach ($sales as $sale) { $deliveryIncluded = $sale['include_delivery'] ? 'Yes' : 'No'; // Format amounts with commas for better readability $formattedAmount = '₹' . number_format($sale['final_amount'], 2); $formattedHarvestCharges = '₹' . number_format($sale['harvesting_charges'], 2); $rows .= " " . date('Y-m-d', strtotime($sale['date'])) . " {$sale['buyer_name']} " . number_format($sale['weight_tilapia'], 2) . " " . number_format($sale['weight_small_fish'], 2) . " " . number_format($sale['weight_big_fish'], 2) . " {$deliveryIncluded} {$formattedAmount} {$formattedHarvestCharges} "; } return $rows; } function generatePagination($current_page, $total_pages) { // Keep pagination manageable by showing limited page numbers $display_range = 5; // Number of pages to show before and after current page $pagination = ''; // Add page info $pagination .= '
Page ' . $current_page . ' of ' . $total_pages . '
'; return $pagination; } $buyerOptions = generateBuyerOptions($buyers); $salesRows = generateSalesRows($sales); $pagination = generatePagination($page, $total_pages); $content = <<

Sales Management

Add Sale

0.00

Sales List

$salesRows
Date Buyer Tilapia (kg) Small Fish (kg) Big Fish (kg) Delivery Final Amount Harvesting Charges Actions
$pagination
HTML; include 'main_layout.php'; ?>