<?php
ini_set('max_execution_time', 5000);
$hostname="localhost";
$username="kimai";
$password="kimai";
$dbname="kimai";
$dbport = "3306";
$html = '';

$conn = mysqli_connect($hostname,$username, $password, $dbname, $dbport) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");


$sql = "SELECT DATE(NOW()) as 'current_date', DATE(NOW()) - INTERVAL 1 WEEK as 'week_start'";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
    $current_date = $row['current_date'];
    $week_start = $row['week_start'];
}

if (isset($_GET['fromdate'])) {
    $fromdate = $_GET['fromdate'];
} else {
    $fromdate = $week_start;
}
if (isset($_GET['todate'])) {
    $todate = $_GET['todate'];
    $todatefront = $_GET['todate'];
    
} else {
    $todate = $current_date;
    $todatefront = $current_date;
}
$date = $todate;
$date1 = str_replace('-', '/', $date);
$todate = date('Y-m-d',strtotime($date1 . "+1 days"));



$sql = "SELECT (5 * (DATEDIFF('$todate', '$fromdate') DIV 7) + MID('0123455501234445012333450122234501101234000123450', 
                              7 * WEEKDAY('$fromdate') + WEEKDAY('$todate') + 1, 1)) * 8.5 AS TotalResolutionTimeBusinessDays ";
$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_array($result)) {
    $corehours = $row['TotalResolutionTimeBusinessDays'];
}
?>
<html>
    <head>
        <link href="css/bootstrap.min.css" rel="stylesheet" media="screen"/>
        <link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen"/>
        <script type="text/javascript" src="js/jquery-1.8.3.min.js" charset="UTF-8"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
        
        <link rel='stylesheet' type='text/css' href='http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css'/>
        <script src="js/jquery.highchartTable.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('table.highchart').highchartTable();
            });
            $(document).ready(function() {
                $("#fromdate").datepicker({ dateFormat: 'yy-mm-dd' });
                $("#todate").datepicker({ dateFormat: 'yy-mm-dd' });
            });
        </script>
        <script src="http://code.highcharts.com/stock/highstock.js"></script>
        <script src="http://code.highcharts.com/stock/modules/exporting.js"></script>
        <style>
            .div {
                -moz-border-radius: 20px;
                -khtml-border-radius: 20px;
                -webkit-border-radius: 20px;
                border-radius: 20px;
                overflow:hidden;
                background:#eee;
                -webkit-box-shadow:0 0 4em rgb(4,6,5);
                -moz-box-shadow:0 0 3em rgb(6,7,8);
                box-shadow:0 0 1em rgb(5,9,2);
                border:20px solid #ddd;
                font-family:Verdana, 20Geneva, sans-serif;
                right: 1%;
                left: 1%;
                position: absolute;
                padding: 2%;
                alignment-adjust: central;
                margin: 25px;

            }
            table {
                border-collapse: collapse;
                border-spacing: 0;
                margin-bottom:30px;
            } 
            th {
                text-align: left;
                font-weight: bold;
            }
            th {
                border-right: 1px solid #999999;
                padding: 5px;
            }
            th:first-child { border-left: 1px solid #999999; }
            td {
                border-right: 1px solid #999999;
                padding: 5px;
            }
            td:first-child { border-left: 1px solid #999999; }
        </style>


    </head>
    <body>
        <?php
        if (!isset($_GET['print'])) {
            $html .=  '<div class="div">';
        }
        ?>
            <legend>Date Selection</legend>
            <form method="get">
        <?php
        if (!isset($_GET['print'])) {
            $html .=  "<a href='time.php?fromdate=$fromdate&todate=$todate&print' style='right: 1%; position: absolute; background: #ccc; text-decoration: none; color: black; padding: 10px;'><div>PRINT</div></a>";
        }
        ?> 
                <label>From Date</label> <input type="text" id="fromdate" name="fromdate" value="<?php $html .=  $fromdate ?>"><br/>
                <label>To Date</label> <input type="text" id="todate" name="todate" value="<?php $html .=  $todatefront ?>"><br/>
                <input type="submit" value="submit">

            </form>
            <br/>
            <legend>Summary Chart</legend>
            <center>
               
            <table border="1px" style="margin: 10px; width: 100%" class="highchart" data-graph-container-before="1" data-graph-type="column" >
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Total Hours</th>
                        <th data-graph-type="line">Core Working Hours</th>
                        <th>Difference</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sql = "select u.usr_alias as 'alias', 
round(sum(e.zef_time) / 3600,2) as time_spent, 
((5 * (DATEDIFF('$todate', '$fromdate') DIV 7) + MID('0123455501234445012333450122234501101234000123450', 7 * WEEKDAY('$fromdate') + WEEKDAY('$todate') + 1, 1)) * 8.5)  AS needed_time, 
round(((sum(e.zef_time) / 3600) - ((5 * (DATEDIFF('$todate', '$fromdate') DIV 7) + MID('0123455501234445012333450122234501101234000123450', 7 * WEEKDAY('$fromdate') + WEEKDAY('$todate') + 1, 1)) * 8.5)),2) as diff 
from kimai_zef e
inner join kimai_usr u on e.zef_usrID=u.usr_ID
inner join kimai_pct p on e.zef_pctID=p.pct_ID
where 
e.zef_in > UNIX_TIMESTAMP(DATE_FORMAT('$fromdate','%Y-%m-%d')) and 
e.zef_in < UNIX_TIMESTAMP(DATE_FORMAT('$todate','%Y-%m-%d'))
group by u.usr_name
                        ";



                    $result = mysqli_query($conn, $sql);

                    while ($row = mysqli_fetch_array($result)) {
                        $name = $row['alias'];
                        $time_spent = $row['time_spent'];
                        $needed_time = $row['needed_time'];
                        $diff = $row['diff'];
                        $html .=  "<tr><td>$name</td><td>$time_spent</td><td>$needed_time</td><td>$diff</td><tr>";
                    }
                    ?>
                </tbody>
            </table>
                </center>
            <br/>
            <legend>Detailed Summary</legend>
            <table border="1px" style="margin: 10px; width: 100%">
                <thead>
                <tr>
                    <th>Project Name</th>



                    <?php
                    $sql = "select u.usr_name as 'name', u.usr_alias as 'alias' from kimai_zef e
inner join kimai_usr u on e.zef_usrID=u.usr_ID
inner join kimai_pct p on e.zef_pctID=p.pct_ID
where 
e.zef_in > UNIX_TIMESTAMP(DATE_FORMAT('$fromdate','%Y-%m-%d')) and 
e.zef_in < UNIX_TIMESTAMP(DATE_FORMAT('$todate','%Y-%m-%d'))
group by u.usr_name
";


                    $result = mysqli_query($conn, $sql);

                    while ($row = mysqli_fetch_array($result)) {

                        $name = $row['name'];
                        $alias = $row['alias'];
                        $html .=  "<th>$alias</th>";
                        $names[] = $name;
                    }
                    ?>
                    <th>TOTAL</th>
                </tr>
                </thead>

                <?php
                $sql = "select p.pct_name as project, ROUND(SUM(e.zef_time) / 3600, 1) as project_total from kimai_zef e
                        inner join kimai_usr u on e.zef_usrID=u.usr_ID
                        inner join kimai_pct p on e.zef_pctID=p.pct_ID
                        where 
                        e.zef_in > UNIX_TIMESTAMP(DATE_FORMAT('$fromdate','%Y-%m-%d')) and 
                        e.zef_in < UNIX_TIMESTAMP(DATE_FORMAT('$todate','%Y-%m-%d'))
                        group by p.pct_name
                        ";



                $result = mysqli_query($conn, $sql);
                $totalforreport = 0;
                while ($row = mysqli_fetch_array($result)) {
                    $projecttotal = $row['project_total'];
                    $project = $row['project'];
                    $html .=  "<tr><td>$project</td>";
                    foreach ($names as $name) {
                        $sqla = "select ROUND(SUM(e.zef_time) / 3600, 1) as 'totaltime'
                            from kimai_zef e
                            inner join kimai_usr u on e.zef_usrID=u.usr_ID
                            inner join kimai_pct p on e.zef_pctID=p.pct_ID
                            where 
                            e.zef_in > UNIX_TIMESTAMP(DATE_FORMAT('$fromdate','%Y-%m-%d')) and 
                            e.zef_in < UNIX_TIMESTAMP(DATE_FORMAT('$todate','%Y-%m-%d'))
                            and p.pct_name = '$project'
                            and u.usr_name = '$name'
                            group by p.pct_name;";
                        $result1 = mysqli_query($conn, $sqla);


                        $total = 0;
                        if (mysqli_num_rows($result1) != 0) {
                            while ($row = mysqli_fetch_array($result1)) {
                                $time = $row['totaltime'];
                                $html .=  "<td>$time</td>";
                            }
                        } else {
                            $html .=  "<td></td>";
                        }
                    }

                    $html .=  "<td>$projecttotal</td>";
                    $html .=  "</tr>";
                    $totalforreport = $totalforreport + $projecttotal;
                }
                ?>

                <tr>
                    <td>TOTAL</td>
                    <?php
                    foreach ($names as $name) {
                        $sql = "select u.usr_alias as name, ROUND(SUM(e.zef_time) / 3600, 1) as 'totaltime'
                            from kimai_zef e
                            inner join kimai_usr u on e.zef_usrID=u.usr_ID
                            inner join kimai_pct p on e.zef_pctID=p.pct_ID
                            where 
                            e.zef_in > UNIX_TIMESTAMP(DATE_FORMAT('$fromdate','%Y-%m-%d')) and 
                            e.zef_in < UNIX_TIMESTAMP(DATE_FORMAT('$todate','%Y-%m-%d'))
                            and u.usr_name = '$name'
                            group by u.usr_name;";
                        $result = mysqli_query($conn, $sql);

                        while ($row = mysqli_fetch_array($result)) {
                            $time = $row['totaltime'];
                            $html .=  "<td>$time</td>";
                        }
                    }
                    $html .=  "<td>$totalforreport</td>";
                    ?>
                </tr>


                <tr>
                    <td>Core Working Hours</td>
                    <?php
                    $totalcorehours = 0;
                    foreach ($names as $name) {
                        $html .=  "<td>$corehours</td>";
                        $totalcorehours = $totalcorehours + $corehours;
                    }
                    $html .=  "<td>$totalcorehours</td>";
                    ?>

                </tr>

                <tr>
                    <td>Difference</td>
                    <?php
                    foreach ($names as $name) {
                        $sql = "select u.usr_alias as name, ROUND(SUM(e.zef_time) / 3600, 2) as 'totaltime'
                            from kimai_zef e
                            inner join kimai_usr u on e.zef_usrID=u.usr_ID
                            inner join kimai_pct p on e.zef_pctID=p.pct_ID
                            where 
                            e.zef_in > UNIX_TIMESTAMP(DATE_FORMAT('$fromdate','%Y-%m-%d')) and 
                            e.zef_in < UNIX_TIMESTAMP(DATE_FORMAT('$todate','%Y-%m-%d'))
                            and u.usr_name = '$name'
                            group by u.usr_name;";
                        $result = mysqli_query($conn, $sql);

                        while ($row = mysqli_fetch_array($result)) {
                            $time = $row['totaltime'] - $corehours;
                            $html .=  "<td>$time</td>";
                        }
                    }
                    $difference = $totalforreport - $totalcorehours;
                    $html .=  "<td>$difference</td>";
                    ?>
                </tr>





            </table>


    <?php
    if (!isset($_GET['print'])) {
        $html .=  '</div>';
    }
    ?>
    <?php if (isset($_GET['print'])) {
            $html .=  '<script type="text/javascript">'
                . '$(document).ready(function(){window.print();window.history.back();});'
                . '</script>';
            } 
            ?>
            
    </body>
</html>