


<?php
session_start();
include '../../assets/constant/config.php';

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// connect author at mdkhairnar92@gmail.com 
// Function to fetch data from the database based on date range and branch
function fetchData($fromdate, $todate, $branch_name,$conn) {
    // Your database query here to fetch relevant data based on date range and branch
    
$stmt=$conn->prepare("SELECT * FROM branch WHERE date BETWEEN $fromdate AND $todate AND branch_name = $branch_name");
  $stmt->bindParam(':fromdate', $fromdate);
        $stmt->bindParam(':todate', $todate);
        $stmt->bindParam(':branch_name', $branch_name);
     $stmt->execute();
      return $stmt->fetchAll(PDO::FETCH_ASSOC);
    // Execute the query and return the fetched data
}

// Function to calculate contribution, pledge, income, payroll, and expense values
function calculateValues($data) {
    $contribution = 0;
    $pledge = 0;
    $income = 0;
    $payroll = 0;
    $expense = 0;
    
    foreach ($data as $row) {
        // Perform calculations based on your data structure
        // Update contribution, pledge, income, payroll, and expense accordingly
    }
    
    return array(
        'contribution' => $contribution,
        'pledge' => $pledge,
        'income' => $income,
        'payroll' => $payroll,
        'expense' => $expense
    );
}

// Process form submission
if (isset($_POST['search'])) {
    $startDate = $_POST["fromdate"];
    $endDate = $_POST["todate"];
    $branch_name = $_POST["branch"];

    // Fetch data from the database
    $data = fetchData($startDate, $endDate, $branch_name);

    // Calculate values
    $values = calculateValues($data);

    // if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "Contribution: " . $values['contribution'] . "<br>";
    echo "Pledge: " . $values['pledge'] . "<br>";
    echo "Income: " . $values['income'] . "<br>";
    echo "Payroll: " . $values['payroll'] . "<br>";
    echo "Expense: " . $values['expense'] . "<br>";
}

} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>