File "mark_harvested.php"

Full Path: /home/leadltht/fastlinkinternet.com/administrator/data-provider/admin/mark_harvested.php
File size: 3.46 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// Start the session to capture the user's information
session_start();

// Set the correct timezone
date_default_timezone_set('Asia/Manila'); // Adjust this to your correct timezone

// Database connection
$conn = new mysqli("server329", "leadltht_prazey1982", "prazey1982123456", "leadltht_fastlinkinternet");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['harvested_clients'])) {
    $harvested_clients = $_POST['harvested_clients'];
    $harvested_amounts = $_POST['harvested_amounts'];
    $less_deductions = $_POST['less_deductions'];
    $remarks = $_POST['remarks'];
    $current_date = date('Y-m-d');  // This should now be accurate
    $collected_by = isset($_SESSION['userdata']['username']) ? $_SESSION['userdata']['username'] : null;

    // Debugging: Check if username is correctly set
    if (empty($collected_by)) {
        die("Error: User is not logged in. Please log in first.");
    }

    // Prepare the insert and update statements
    $insert_query = "INSERT INTO harvest_collection (client_id, harvested_date, harvested_amount, less_deductions, client_share, owner_share, collected_by, remarks) 
                     VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    $update_query = "UPDATE clients SET last_harvested_date = ? WHERE id = ?";
    
    $insert_stmt = $conn->prepare($insert_query);
    $update_stmt = $conn->prepare($update_query);

    if (!$insert_stmt || !$update_stmt) {
        die("Error preparing statements: " . $conn->error);
    }

    foreach ($harvested_clients as $client_id) {
        $harvested_amount = isset($harvested_amounts[$client_id]) ? $harvested_amounts[$client_id] : 0;
        $deductions = isset($less_deductions[$client_id]) ? $less_deductions[$client_id] : 0;
        $remark = isset($remarks[$client_id]) ? $remarks[$client_id] : '';

        // Fetch client share percentage from the clients table
        $client_query = "SELECT client_share FROM clients WHERE id = ?";
        $client_stmt = $conn->prepare($client_query);
        $client_stmt->bind_param("i", $client_id);
        $client_stmt->execute();
        $client_stmt->bind_result($client_share_percentage);
        $client_stmt->fetch();
        $client_stmt->close();

        // Calculate client share and owner share
        $client_share_amount = ($deductions * ($client_share_percentage / 100));
        $owner_share_amount = ($deductions - $client_share_amount);

        // Insert into harvest_collection
        $insert_stmt->bind_param("isdddsss", $client_id, $current_date, $harvested_amount, $deductions, $client_share_amount, $owner_share_amount, $collected_by, $remark);
        if ($insert_stmt->execute()) {
            echo "Data successfully inserted for Client ID: $client_id<br>";
        } else {
            echo "Error inserting record for Client ID $client_id: " . $conn->error . "<br>";
        }

        // Update clients table
        $update_stmt->bind_param("si", $current_date, $client_id);
        if ($update_stmt->execute()) {
            echo "Client's last harvested date updated successfully for Client ID $client_id<br>";
        } else {
            echo "Error updating last harvested date for Client ID $client_id: " . $conn->error . "<br>";
        }
    }

    $insert_stmt->close();
    $update_stmt->close();
} else {
    echo "No clients selected for harvesting.";
}

$conn->close();

header("Location: for_harvest.php");
exit();
?>