<?php

include_once("classes/mysql.php");
include_once("classes/globals.php");
?>

<html>
    <head>
        <style>
            body
            {
                font-size:12px;
                font-family:Verdana;
                margin:20px auto;
                width:1000px;
                border:solid 1px #cccccc;
                padding:10px;
            } 
            .header
            {
                background-color:black;
                color:white;
                font-weight:bold;
            }
            .data_even
            {
                background-color:#FFDDDD;
            }
            .data_odd
            {
                background-color:#FFFFFF;
            }
            .user_James
            {
                background-color:#EEFF77;
            }
            .user_Jonty
            {
                background-color:#CCCCFF;
            }
            .user_Brad
            {
                background-color:#CCFFCC;
            }
            .user_Wayne
            {
                background-color:#ffbbff;
            }
            .user_Tim
            {
                background-color:#ff1188;
            }
            tr.header a
            {
                color:white;
            }
            td
            {
                border:solid 1px #000000;
                font-size:11px;
                padding:2px;
                vertical-align:top;
            }
            p.small
            {
                font-size:9px;
            }
            td.daytotal
            {
                font-weight:bold;
                font-style:italic;
                background-color:#FF9944;
                border:none;
                border-bottom:solid 1px black;
            }
            tr:hover
            {
                background-color:#FFFF00;
            }
            td.spacer
            {
                border:none;
            }
            table#maintable
            {
                width:1000px;
                border:solid 1px #888888;
                border-collapse:collapse;
            }
            td.project-1 
            {
            	background:color:#f00;
            	}
            .UPDATED
            {
                background-color:#8fea8f !important;
            }
            textarea
            {
                width:200px;
            }
        </style>
        <!--
        <script src="jquery.js" type="text/javascript" charset="utf-8"></script>
        <script src="jquery.jeditable.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" charset="utf-8">
             $(document).ready(function() {
                $(".edit").editable("test.php", {
                 indicatior: "Saving...",
                 tooltip: "Click to edit...",
                 type: "textarea",
                 submit: "OK"
                });
            });
        </script>
        -->
     </head>
    <body>

<?php

if($_GET["order"])
{
    $sort = $_GET["order"];
} else {
    $sort = "nr_timesheet.date";
}
if($_GET["month"])
{
    $month = $_GET["month"];
} else {
    $month = date("m",mktime(0,0,0,date("m")-1,1,date("y")));
}
if($_GET["year"])
{
    $year = $_GET["year"];
} else {
    $year = date("Y");
}
if($_GET["client"])
{
    $client = " and nr_timesheet.client_id=\"".$_GET["client"]."\"";
} else {
    $client = "";
}

if($_GET["project"]&&$_GET["project"]!=0)
{
    $project = " and nr_timesheet.project_work=\"".$_GET["project"]."\"";
} elseif($_GET["project"]=="0") {
    $project = " and nr_timesheet.project_work=\"".$_GET["project"]."\"";
} else
{
    $project ="";
}

if($_GET["user"]&&$_GET["user"]!="all")
{
    $user = " and nr_users.firstname=\"".$_GET["user"]."\"";
} else {
    $user = "";
}

if($_POST["id"])
{
    $id = $_POST["id"];
    $link = new mySQL($__host, $__user, $__password, $__database);
    if(isset($_POST["project"]))
    {
        $link->query("UPDATE nr_timesheet SET project_work=1 WHERE ID=$id");
    } else {
        $link->query("UPDATE nr_timesheet SET project_work=0 WHERE ID=$id");
    }
}


$link = new mySQL($__host, $__user, $__password, $__database);
$link->query("select nr_users.firstname,nr_clients.name,nr_timesheet.* from nr_users, nr_timesheet,nr_clients where nr_users.id = nr_timesheet.user and nr_clients.id = nr_timesheet.client_id and date like \"$year$month%\"$user$project$client order by $sort asc");
$user = $_GET["user"]; //restore user from the sql stuff to just the name
$project = $_GET["project"];
$client = $_GET["client"];

?>
<table style="border:none;" width="700">
    <tr>
        <td style="border:none;"><img src="logosmall.jpg" height="50" /></td>
        <td style="border:none;" align="center"><h1>Hours for <?php echo $month;?>-<?php echo $year;?>&nbsp;&nbsp;<?php echo "(User:$user,Project:$project)";?></h1></td>
</tr>
</table>
<p>
    Click Date, Client or Engineer to sort by these fields. Days are broken up by a larger space in between them.
</p>
<p class="small">
    <a href="?month=01&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Jan</a>&nbsp;|&nbsp;
    <a href="?month=02&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Feb</a>&nbsp;|&nbsp;
    <a href="?month=03&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Mar</a>&nbsp;|&nbsp;
    <a href="?month=04&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Apr</a>&nbsp;|&nbsp;
    <a href="?month=05&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">May</a>&nbsp;|&nbsp;
    <a href="?month=06&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Jun</a>&nbsp;|&nbsp;
    <a href="?month=07&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Jul</a>&nbsp;|&nbsp;
    <a href="?month=08&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Aug</a>&nbsp;|&nbsp;
    <a href="?month=09&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Sep</a>&nbsp;|&nbsp;
    <a href="?month=10&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Oct</a>&nbsp;|&nbsp;
    <a href="?month=11&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Nov</a>&nbsp;|&nbsp;
    <a href="?month=12&user=<?php echo $user;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Dec</a>&nbsp;|&nbsp;    
</p>

<p class="small">
    <a href="?user=brad&month=<?php echo $month;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Brad</a>&nbsp;|&nbsp;
    <a href="?user=james&month=<?php echo $month;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">James</a>&nbsp;|&nbsp;
    <a href="?user=jonty&month=<?php echo $month;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Jonty</a>&nbsp;|&nbsp;
    <a href="?user=wayne&month=<?php echo $month;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Wayne</a>&nbsp;|&nbsp;
    <a href="?user=tim&month=<?php echo $month;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">Tim</a>&nbsp;|&nbsp;    
    <a href="?user=all&month=<?php echo $month;?>&year=<?php echo $year;?>&project=<?php echo $project;?>&client=<?php echo $client;?>">All</a>&nbsp;|&nbsp;
&nbsp;&nbsp;&nbsp;
    <a href="?year=2005&user=<?php echo $user;?>&month=<?php echo $month;?>&client=<?php echo $client;?>">2005</a>&nbsp;|&nbsp;
    <a href="?year=2006&user=<?php echo $user;?>&month=<?php echo $month;?>&client=<?php echo $client;?>">2006</a>&nbsp;|&nbsp;
    <a href="?year=2007&user=<?php echo $user;?>&month=<?php echo $month;?>&client=<?php echo $client;?>">2007</a>&nbsp;|&nbsp;
    <a href="?year=2008&user=<?php echo $user;?>&month=<?php echo $month;?>&client=<?php echo $client;?>">2008</a>&nbsp;|&nbsp;
&nbsp;&nbsp;&nbsp;
    <a href="?project=1&year=<?php echo $year;?>&user=<?php echo $user;?>&month=<?php echo $month;?>&client=<?php echo $client;?>">Project Only</a>&nbsp;|&nbsp;
    <a href="?project=0&year=<?php echo $year;?>&user=<?php echo $user;?>&month=<?php echo $month;?>&client=<?php echo $client;?>">Standard Only</a>&nbsp;|&nbsp;
    <a href="?project=all&year=<?php echo $year;?>&user=<?php echo $user;?>&month=<?php echo $month;?>&client=<?php echo $client;?>">All Hours</a>&nbsp;|&nbsp;
    &nbsp;&nbsp;&nbsp;
    <?php
        $client_link = new mysql($__host, $__user, $__password, $__database);
        $client_link->query("SELECT id,name FROM nr_clients ORDER BY name ASC");
        echo '<select name="client" onchange="location.href=location.href+\'?&client=\'+this[this.selectedIndex].value;"';
        while($row = $client_link->getRow())
        {
            echo '<option value="'.$row["id"].'">'.$row["name"].'</option>';
        }
        echo '</select>';
    ?>
    <a href="?year=<?php echo $year;?>&user=<?php echo $user;?>&month=<?php echo $month;?>&project=<?php echo $project;?>">All Clients</a>&nbsp;|&nbsp;
</p>

<table id="maintable">
    <tr class="header">
        <td></td>
        <td width="10%"><a href="?order=nr_timesheet.date&month=<?php echo $month;?>">Date</a></td>
        <td width="20%"><a href="?order=nr_clients.name&month=<?php echo $month;?>">Client</a></td>
        <td width="7%"><a href="?order=nr_users.firstname&month=<?php echo $month;?>">Engineer</a></td>
        <td width="5%">Hours</td>
        <td width="8%">Time In</td>
        <td width="8%">Time Out</td>
        <td>Details</td>
        <td>P?</td>
    </tr>
<?php
$i = 0; $overallhours = 0; $count = 1; $daytotal = 0;
while($row = $link->getRow())
{
    if($count==1) { $prevdate = $row["date"];}

    $date = substr($row["date"],6,2)."/".substr($row["date"],4,2)."/".substr($row["date"],0,4);
    
    $hours = substr($row["time_out"],0,2)-substr($row["time_in"],0,2);
    $mins = (substr($row["time_out"],2,2)-substr($row["time_in"],2,2))/60;
    $total += $hours+$mins;

    $daytotal += $total; $thisjob = $hours+$mins;
    $overallhours += $total;

    //echo $date.": total for job $count is $thisjob, day total so far is $daytotal & overal hours is $overallhours<br />";
        
    if(++$i%2==0)
    {
        $col = "_even";
    } else {
        $col = "_odd";
    }
    
    if($row["ID"]==$id) { $col .= " UPDATED"; }
    
    if($prevdate != $row["date"]&&$count!=1)
    {
        $pdate = substr($prevdate,6,2)."/".substr($prevdate,4,2)."/".substr($prevdate,0,4);
        $epochdate = mktime(0,0,0,substr($prevdate,4,2),substr($prevdate,6,2),substr($prevdate,0,4));
        $dayname = date("l", $epochdate);
    
        $daytotal -= $thisjob;
        //echo "caught cross over, job $count has total $thisjob, new daytotal is $daytotal<br /><br />";
        
        echo "<tr><td colspan=\"4\" class=\"daytotal\">$dayname: $pdate</td>".
             "<td class=\"daytotal\">$daytotal</td><td colspan=\"4\" class=\"daytotal\">&nbsp;</td></tr>".
             "<tr><td colspan=\"9\" class=\"spacer\">&nbsp;</td></tr>";
        
        $prevdate = $row["date"];
        $daytotal = $thisjob; //restore the total for the current day
        $count = 1; //reset count for the day
    }
  
    
    $nicetotal = fmod($total, 1.0);
    switch($nicetotal)
    {
        case 0:
            $nicetotal = "";
            break;
        case 0.25:
            $nicetotal = "&frac14;";
            break;
        case 0.5:
            $nicetotal = "&frac12;";
            break;
        case 0.75:
            $nicetotal = "&frac34;";
            break;
    }
    if (floor($total)!=0)
    {
        $h = floor($total).$nicetotal;
    } else {
        $h = $nicetotal;
    }
        
    $checked = ($row["project_work"]) ? " checked" : "";
    $projectflag = '<form name="updateproject" method="POST"><input type="hidden" name="id" value="'.$row["ID"].'"><input onchange="submit();" name="project" type="checkbox"'.$checked.'></form>';
    
    echo "<tr class=\"data$col\">\n";
    echo "<td>#".$count."</td>\n";
    echo "<td>".$date."</td>\n";
    echo "<td>".$row["name"]."</td>\n";
    echo "<td class=\"user_".$row["firstname"]."\">".$row["firstname"]."</td>\n";
    echo "<td>".$h."</td>\n";
    echo "<td>".$row["time_in"]."</td>\n";
    echo "<td>".$row["time_out"]."</td>\n";
    echo "<td><div class='edit'>";
    if(strlen($row["details"])>200)
    {
        echo substr($row["details"],0,200)."...";
    } else {
        echo $row["details"];
    }
    echo "</div></td>\n";
    echo '<td>'.$projectflag.'</td>';
    echo "</tr>";
   
    $total = 0;
    $count++;
}

$pdate = substr($prevdate,6,2)."/".substr($prevdate,4,2)."/".substr($prevdate,0,4);
$epochdate = mktime(0,0,0,substr($prevdate,4,2),substr($prevdate,6,2),substr($prevdate,0,4));
$dayname = date("l", $epochdate);

echo "<tr><td colspan=\"4\" class=\"daytotal\">$dayname: $date</td>".
     "<td class=\"daytotal\">$daytotal</td><td colspan=\"4\" class=\"daytotal\">&nbsp;</td></tr>".
     "<tr><td colspan=\"9\" class=\"spacer\">P</td></tr>";

?>
         </table>
        <h2>Overall hours: <?php echo $overallhours;?></h2>
    </body>
</html>


syntax highlighted by Code2HTML, v. 0.9.1