<?php
$curYear = date("Y", time());
$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);

if ($curQuarter == 1) {
$fromdate = "$curYear" . "-01-01";
$summaryfromdate = "$curYear" . "-01-01";
}
if ($curQuarter == 2) {
$fromdate = "$curYear" . "-03-01";
$summaryfromdate = "$curYear" . "-03-01";
}
if ($curQuarter == 3) {
$fromdate = "$curYear" . "-06-01";
$summaryfromdate = "$curYear" . "-06-01";
}
if ($curQuarter == 4) {
$fromdate = "$curYear" . "-09-01";
$summaryfromdate = "$curYear" . "-09-01";
}

$todate = new DateTime(date("Y-m-d", time()));
$todate->modify('+1 day');
$todate = $todate->format('Y-m-d');
$summarytodate = new DateTime(date("Y-m-d", time()));
$summarytodate = $summarytodate->format('Y-m-d');



echo "\nfromdate " .$fromdate;
echo "\ntodate " . $todate;
echo "\nsummaryfromdate " .$summaryfromdate;
echo "\nsummarytodate " .$summarytodate;
echo "\n";

try {
                    $con = new PDO('mysql:host=localhost;dbname=kimai', "kimai", "kimai");
                    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $query = "
SET @fromdate = '$fromdate';
SET @todate = '$todate';
SET @summaryfromdate = '$summaryfromdate';
SET @summarytodate = '$summarytodate';
Drop  table  IF EXISTS kimai15_flexitime;
Create Table kimai15_flexitime
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";
$result = $con->query($query);
} catch (PDOException $e) {
                    echo 'ERROR: ' . $e->getMessage();
                } 

?>