       
        <div class="CSSTableGenerator" >

            <p>
                <?php
                try {
                    $con = new PDO('mysql:host=localhost;port=3306;dbname=kimai', "kimai", "kimai");
                    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $query = "
SET @fromdate = '2015-01-01';
SET @todate = '2015-02-01';
SET @summaryfromdate = '2015-01-01';
SET @summarytodate = '2015-01-31';
SELECT u.usr_alias as 'User',
case when ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) * 1.5 > 0 then ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) else 0 end as 'Time_Extra',
case when ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) * 1.5 > 0 then ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) * 1.5 else 0 end as 'Extra_Time_Worth',
CASE WHEN (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) >0 then (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) else 0 end as 'Sunday_Times',
CASE WHEN (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) >0 then (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) * 2 else 0 end as 'Sunday_Times_Worth',
(case when ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) * 1.5 > 0 then ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) * 1.5 else 0 end + CASE WHEN (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) >0 then (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) * 2 else 0 end) as 'Total_Worth',
CASE WHEN (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and zef.zef_evtID = 158 group by u.usr_name) is null then 0 else (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and zef.zef_evtID = 158 group by u.usr_name) end as 'Flexi Time Taken',
(case when ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) * 1.5 > 0 then ((SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) != 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) - (TOTAL_WEEKDAYS(@summaryfromdate, @summarytodate) * 8.5)) * 1.5 else 0 end + CASE WHEN (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) >0 then (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and DAYNAME(FROM_UNIXTIME(zef.zef_in)) = 'Sunday' and zef.zef_evtID != 158 group by u.usr_name) * 2 else 0 end) 
- CASE WHEN (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and zef.zef_evtID = 158 group by u.usr_name) is null then 0 else (SELECT (round(sum(zef.zef_time) / 3600,2 )) FROM kimai15_zef zef INNER JOIN kimai15_usr usr on zef.zef_usrID = usr.usr_ID inner join kimai15_pct pct on zef.zef_pctID=pct.pct_ID WHERE usr.usr_alias = u.usr_alias and zef.zef_in > UNIX_TIMESTAMP(DATE_FORMAT(@fromdate,'%Y-%m-%d')) and zef.zef_in < UNIX_TIMESTAMP(DATE_FORMAT(@todate,'%Y-%m-%d')) and zef.zef_evtID = 158 group by u.usr_name) end  as 'Total_Available'
from kimai15_zef e 
inner join kimai15_usr u on e.zef_usrID=u.usr_ID 
inner join kimai15_pct p on e.zef_pctID=p.pct_ID 
where
u.usr_active = 1

group by u.usr_name";
                    $query = "SELECT * from kimai15_flexitime";

                    //first pass just gets the column names
                    echo '<table id="myTable" border="1" cellpadding="3" cellspacing="0">';
                    $result = $con->query($query);
                    //return only the first row (we only need field names)
                    $row = $result->fetch(PDO::FETCH_ASSOC);
                    echo " <tr> \n";
                    foreach ($row as $field => $value) {
                        echo " <th>$field</th> \n";
                    } // end foreach
                    echo " </tr> \n";
                    //second query gets the data
                    $data = $con->query($query);
                    $data->setFetchMode(PDO::FETCH_ASSOC);
                    foreach ($data as $row) {
                        echo " <tr> \n";
                        foreach ($row as $name => $value) {
                            echo " <td>$value</td> \n";
                        } // end field loop
                        echo " </tr> \n";
                    } // end record loop
                    echo "</table> \n";
                } catch (PDOException $e) {
                    echo 'ERROR: ' . $e->getMessage();
                } // end try
                ?>
            </p>
                </div>
</div>

