shiftcalc/public/index.php
2025-07-21 22:07:04 +02:00

238 lines
8.3 KiB
PHP

<html><head><meta name="viewport" content="width=device-width, initial-scale=1"><title>Shift Calc</title><style>html, body { margin:0; padding: 0; } body { padding: 1rem; } table { border-collapse: collapse; } table, td, th { border: solid 1px #000000; } tr.vacation { background-color: #ffffe0; } tr.homeoffice { background-color: #fff5ee;} tr.office { background-color: #f0ffff; } tr.sickday { background-color: #ffe4e1;} tr.businesstrip { background-color: #e6e6fa; } tr.holiday { background-color: #f0fff0; }
tr.current { background-color: #008080; } </style></head><body><?php
// require the vendor autoload file
require '../vendor/autoload.php';
// define working context to prevent data breaching
define("SHIFTCALC", 1);
// use the application namespace
use Nischcodes\Shiftcalc\Application;
use Nischcodes\Shiftcalc\HTTP;
//ini_set('display_errors', '1');
//ini_set('display_startup_errors', '1');
//error_reporting(E_ALL);
// initilize the application
Application::init();
// run the application
Application::run();
function transferHoursToDecimal($time) {
$parts = explode(':', $time);
//var_dump($parts);
return round(($parts[0] + ($parts[1] / 60)), 2);
}
// load login data and encode it
$encodedLogin = base64_encode($_ENV['LOGIN']);
$breakSmall = 0.5;
$breakLarge = 0.75;
$breakLimit = 9;
//print_r($login);
//print_r($encodedLogin);
$currentYear = date("Y");
$holidays = HTTP::fetchJSON(str_replace('$currentYear', $currentYear, $_ENV['HOLIDAY_SERVER']));
//print_r($holidays);
// create a connection via curl
$respData = HTTP::fetchJSON($_ENV['SERVER'], [
"Authorization: Basic [$encodedLogin]",
"Content-Type: application/json",
"OCS-APIRequest: true"
]);
//print_r($respData);
$dates = [];
// add holidays
foreach($holidays['feiertage'] as $holyday) {
//print_r($holyday['date']);
$date = $holyday['date'];
$description = $holyday['fname'];
$dates[] = ['date' => $date, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Holiday', 'desc' => $description];
}
// convert all entries to date/start/end-arrays
foreach($respData as $rpentry) {
$edate = '';
$etime = '';
$etype = 0;
$homeoffice = false;
// extract date, time and type
foreach($rpentry['data'] as $i=>$rpdata) {
if($rpdata['columnId'] == 9) $edate = $rpdata['value'];
else if ($rpdata['columnId'] == 10) $etime = $rpdata['value'];
else if ($rpdata['columnId'] == 12) $etype = $rpdata['value'];
else if ($rpdata['columnId'] == 13) $homeoffice = $rpdata['value'];
}
//print_r($edate);
//print_r($etime);
//print_r($etype);
//print_r($homeoffice);
$found = false;
foreach($dates as $index=>$date) {
//var_dump($date['date']);
//var_dump($edate);
//var_dump($date['date'] == $edate);
if($date['date'] == $edate) {
//var_dump($date);
if($etype == 0) $dates[$index]['start'] = $etime;
else if($etype == 1) {
// add end time to entry
$dates[$index]['end'] = $etime;
// calc shift in decimal
$dates[$index]['sdec'] = transferHoursToDecimal($dates[$index]['start']);
$dates[$index]['edec'] = transferHoursToDecimal($dates[$index]['end']);
$dates[$index]['worktime'] = $dates[$index]['edec'] - $dates[$index]['sdec'];
$dates[$index]['break'] = ($dates[$index]['worktime'] > $breakLimit ? $breakLarge : $breakSmall);
$dates[$index]['worktime_total'] = $dates[$index]['worktime'] - $dates[$index]['break'];
}
$found = true;
break;
}
}
if(!$found) {
if($etype == 0) $dates[] = ['date' => $edate, 'start' => $etime, 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 0, 'break' => 0.5, 'worktime_total' => 0, 'info' => ($homeoffice ? 'Homeoffice' : 'Office'), 'desc' => ''];
if($etype == 2) $dates[] = ['date' => $edate, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Vacation', 'desc' => ''];
if($etype == 3) $dates[] = ['date' => $edate, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Sickday', 'desc' => ''];
if($etype == 4) $dates[] = ['date' => $edate, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Businesstrip', 'desc' => ''];
}
}
// sort all dates
usort($dates, function($a, $b) {
return strcmp($a['date'], $b['date']);
});
// print_r($dates);
$groupedDates = [];
// group dates by week
foreach($dates as $entry) {
$date = new DateTime($entry['date']);
$year = $date->format('o');
$week = $date->format('W');
$key = "$year-CW$week";
if(!isset($groupedDates[$key])) {
$groupedDates[$key] = [];
}
$groupedDates[$key][] = $entry;
}
//print_r($groupedDates);
$homeofficedays = 0;
$sickdays = 0;
$vacations = 0;
$workdays = 0;
$currentWeek = date("W");
$currentWeekIndex = "$currentYear-CW$currentWeek";
// build the current week
echo "<h2>Current Week</h2>";
echo "<table style=\"width:-webkit-fill-available;\" >";
echo "<tr class=\"calenderweek\"><th colspan=\"7\" style=\"text-align:left;\">$currentWeekIndex</th></tr>";
echo "<tr class=\"cwheader\"><td width=\"10%\">Date</td><td>Info</th><td width=\"10%\">Start</td><td width=\"10%\">End</td><td width=\"10%\">Worktime</td><td width=\"10%\">Break</td><td width=\"10%\">Worktime Total</td></tr>";
$currentWeekTotal = 0;
foreach($groupedDates[$currentWeekIndex] as $id=>$data) {
$date = $data['date'];
$info = $data['info'];
$start = $data['start'];
$end = $data['end'];
$worktime = $data['worktime'];
$break = $data['break'];
$worktimeTotal = $data['worktime_total'];
$description = ($data['desc'] ? ' - '.$data['desc'] : '');
$class = strtolower($info);
echo "<tr class=\"$class\"><td>$date</td><td>$info$description</td><td>$start</td><td>$end</td><td>$worktime</td><td>$break</td><td>$worktimeTotal</td></tr>";
$currentWeekTotal += $worktimeTotal;
}
echo "<tr class=\"cwtotal\" style=\"font-weight: bold;\"><td colspan=\"6\" style=\"text-align:right;\">Total: </td><td>$currentWeekTotal</td></tr>";
echo '</table>';
echo "<p>&nbsp;</p>";
echo "<hr />";
echo "<h2>Full year table</h2>";
// build the full year table
echo "<table style=\"width:-webkit-fill-available;\" >";
foreach($groupedDates as $index=>$group) {
$isCurrentWeek = (strcmp($index, $currentWeekIndex) === 0 ? true : false);
if($isCurrentWeek) echo "<tr class=\"current\"><td colspan=\"7\">&nbsp;</td></tr>";
echo "<tr class=\"calenderweek\"><th colspan=\"7\" style=\"text-align:left;\">$index</th></tr>";
echo "<tr class=\"cwheader\"><td width=\"10%\">Date</td><td>Info</th><td width=\"10%\">Start</td><td width=\"10%\">End</td><td width=\"10%\">Worktime</td><td width=\"10%\">Break</td><td width=\"10%\">Worktime Total</td></tr>";
$weekTotal = 0;
foreach($group as $id=>$data) {
$date = $data['date'];
$info = $data['info'];
$start = $data['start'];
$end = $data['end'];
$worktime = $data['worktime'];
$break = $data['break'];
$worktimeTotal = $data['worktime_total'];
$description = ($data['desc'] ? ' - '.$data['desc'] : '');
$class = strtolower($info);
echo "<tr class=\"$class\"><td>$date</td><td>$info$description</td><td>$start</td><td>$end</td><td>$worktime</td><td>$break</td><td>$worktimeTotal</td></tr>";
$weekTotal += $worktimeTotal;
if(strcmp($info, 'Sickday') === 0) $sickdays++;
if(strcmp($info, 'Homeoffice') === 0) $homeofficedays++;
if(strcmp($info, 'Vacation') === 0) $vacations++;
if(strcmp($info, 'Vacation') !== 0 && strcmp($info, 'Holiday') !== 0) $workdays++;
}
echo "<tr class=\"cwtotal\" style=\"font-weight: bold;\"><td colspan=\"6\" style=\"text-align:right;\">Total: </td><td>$weekTotal</td></tr>";
if($isCurrentWeek) echo "<tr class=\"current\"><td colspan=\"7\">&nbsp;</td></tr>";
}
echo "<tr><td colspan=\"7\">&nbsp;</td></tr>";
//$workdaystotal = count($dates);
echo "<tr class=\"summary\" style=\"font-weight: bold;\"><td>Sickdays: </td><td>$sickdays</td><td>Vacationdays: </td><td>$vacations/30</td><td>Homeoffice: </td><td>$homeofficedays</th><td>$workdays</td></tr>";
echo '</table>';
?></body></html>