<?php
class functions
{
    function calculate_horizontal_cylinder_volume($radius, $liquid_level_from_top, $length)
    {

        $Step_1 = pi() * pow($radius, 2);
        $Step_2 = pow($radius, 2) * acos(($radius - $liquid_level_from_top) / $radius);
        $Step_3 = ($radius - $liquid_level_from_top) * sqrt((2 * $radius * $liquid_level_from_top) - pow($liquid_level_from_top, 2));
        $liquid_inside = round((($Step_1 - $Step_2 + $Step_3) * $length) / 1000000);
        $total_volume = round((pi() * pow($radius, 2) * $length) / 1000000);
        $percentage_filled = ($liquid_inside / $total_volume) * 100;
        return array(
            'fluid_volume_liters' => round($liquid_inside),
            'total_volume_liters' => round($total_volume),
            'percentage_filled' => round($percentage_filled)
        );
    }
    function calculate_vertical_cylinder_volume($radius_m, $liquid_level_from_top_m, $height_m)
    {
        // Calculate liquid height
        $liquid_height_m = $height_m - $liquid_level_from_top_m;
        if ($liquid_height_m < 0)
            $liquid_height_m = 0; // prevent negative

        // Volumes in cubic meters
        $liquid_volume_m3 = pi() * pow($radius_m, 2) * $liquid_height_m;
        $total_volume_m3 = pi() * pow($radius_m, 2) * $height_m;

        // Convert cubic meters → liters
        $liquid_volume_liters = $liquid_volume_m3 * 1000;
        $total_volume_liters = $total_volume_m3 * 1000;

        // Percentage filled
        $percentage_filled = ($total_volume_liters > 0)
            ? ($liquid_volume_liters / $total_volume_liters) * 100
            : 0;

        return array(
            'fluid_volume_liters' => round($liquid_volume_liters, 2),
            'total_volume_liters' => round($total_volume_liters, 2),
            'percentage_filled' => round($percentage_filled, 2)
        );
    }


    function calculate_horizontal_oval_tank_volume($radius1_mm, $radius2_mm, $depth_mm, $tank_length_mm)
    {
        // Convert measurements to meters
        $radius1 = $radius1_mm / 1000; // Convert millimeters to meters
        $radius2 = $radius2_mm / 1000; // Convert millimeters to meters
        $depth = $depth_mm / 1000; // Convert millimeters to meters
        $tank_length = $tank_length_mm / 1000; // Convert millimeters to meters

        // Calculate the angle of the segment
        $theta = acos((($radius1 - $depth) / $radius1));

        // Calculate the volume of the fluid
        $volume_fluid = ($theta * $radius1 * $radius2) * $tank_length;

        // Convert volume to liters
        $volume_fluid_liters = $volume_fluid * 1000; // Convert cubic meters to liters

        // Calculate the total volume of the tank
        $volume_total = (M_PI * $radius1 * $radius2) * $tank_length;

        // Convert volumes to liters
        $volume_total_liters = $volume_total * 1000; // Convert cubic meters to liters

        // Calculate the percentage of the tank filled
        $percentage_filled = ($volume_fluid_liters / $volume_total_liters) * 100;

        return array(
            'fluid_volume_liters' => round($volume_fluid_liters),
            'total_volume_liters' => round($volume_total_liters),
            'percentage_filled' => round($percentage_filled)
        );
    }


    function get_tank_name($tank_id)
    {
        $db = new db();
        $tank_res = $db->query("tanks", "SELECT `name` FROM tanks WHERE record_id = $tank_id");
        return $tank_res->fetch_assoc()['name'];
    }

    function get_username($user_id)
    {
        $db = new db();
        $user_res = $db->query("users", "SELECT `username` FROM users WHERE record_id = $user_id");
        return $user_res->fetch_assoc()['username'];
    }

    function get_client_name($client_id)
    {
        $db = new db();
        $client_res = $db->query("clients", "SELECT `client_name` FROM clients WHERE record_id = $client_id");
        if ($client_res->num_rows == 0)
            return "MANUAL OVERRIDE";
        return $client_res->fetch_assoc()['client_name'];
    }

    function get_site_name($site_id)
    {
        $db = new db();
        $site_res = $db->query("sites", "SELECT `name` FROM sites WHERE record_id = $site_id");
        return $site_res->fetch_assoc()['name'];
    }

    function get_cash_type($cash_type_id)
    {
        if ($cash_type_id == 0) {
            return "N/A";
        } else if ($cash_type_id == 1) {
            return "CASH";
        } else if ($cash_type_id == 2) {
            return "CARD";
        } else if ($cash_type_id == 3) {
            return "EFT";
        }
    }

    function total_liters_for_tank($tank_id)
    {
        $db = new db();
        $date = date("Y-m-d");
        $tank_res = $db->query("tanks", "SELECT SUM(amount) as total FROM fuel_movement as fm LEFT JOIN clients as c ON fm.client_id = c.record_id WHERE (fm.date_time_closed BETWEEN '$date 00:00' AND '$date 23:59' AND fm.tank_id = $tank_id AND fm.company_id = {$_SESSION['company_id']} AND c.company_id = {$_SESSION['company_id']} AND c.exclude_from_reports != 1) OR (fm.date_time_closed BETWEEN '$date 00:00' AND '$date 23:59' AND fm.tank_id = $tank_id AND fm.company_id = {$_SESSION['company_id']} AND fm.status = 'MANUAL OVERRIDE' AND fm.client_id = 0)");
        return $tank_res->fetch_assoc()['total'] / 1000;
    }


    function total_liters_for_today()
    {
        $db = new db();
        $date = date("Y-m-d");
        $tank_res = $db->query("tanks", "SELECT SUM(amount) as total FROM fuel_movement as fm LEFT JOIN clients as c ON fm.client_id = c.record_id WHERE (fm.tank_id IN ({$_SESSION['tanks']}) AND fm.date_time_closed BETWEEN '$date 00:00' AND '$date 23:59' AND fm.company_id = {$_SESSION['company_id']} AND c.company_id = {$_SESSION['company_id']} AND fm.status = 'RFID' AND c.exclude_from_reports != 1)OR( fm.tank_id IN ({$_SESSION['tanks']}) AND fm.date_time_closed BETWEEN '$date 00:00' AND '$date 23:59' AND fm.company_id = {$_SESSION['company_id']} AND  fm.status = 'MANUAL OVERRIDE' AND fm.client_id = 0)");
        // echo "SELECT SUM(amount) as total FROM fuel_movement WHERE date_time_closed BETWEEN '$date 00:00' AND '$date 23:59' AND company_id = {$_SESSION['company_id']}";
        return $tank_res->fetch_assoc()['total'] / 1000;
    }


    function total_liters_for_today_excluded_tags()
    {
        $db = new db();
        $date = date("Y-m-d");
        $tank_res = $db->query("tanks", "SELECT SUM(amount) as total FROM fuel_movement as fm LEFT JOIN clients as c ON fm.client_id = c.record_id WHERE fm.tank_id IN ({$_SESSION['tanks']}) AND fm.date_time_closed BETWEEN '$date 00:00' AND '$date 23:59' AND fm.company_id = {$_SESSION['company_id']} AND c.company_id = {$_SESSION['company_id']} AND c.exclude_from_reports = 1");
        // echo "SELECT SUM(amount) as total FROM fuel_movement WHERE date_time_closed BETWEEN '$date 00:00' AND '$date 23:59' AND company_id = {$_SESSION['company_id']}";
        return $tank_res->fetch_assoc()['total'] / 1000;
    }
}