Files
thetool/application/Timerecording/TimerecordingController.php
Daniel Spitzer df2dd9c596 Kalender Anpassungen
- Anpassungen bei neuen Kalenderbenutzern
2025-06-06 09:29:25 +02:00

1298 lines
56 KiB
PHP

<?php
class TimerecordingController extends mfBaseController
{
protected function init()
{
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if (!$me->is(["employee"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction()
{
$this->updatePlushours($this->me->id);
$this->updateHolidays($this->me->id);
$timerecordingBilling = TimerecordingBillingModel::getLast();
$timerecordingCategoriess = TimerecordingCategoryModel::getAll();
$timerecordingCars = TimerecordingCarModel::search(['timerecording' => 1]);
$this->layout()->set("timerecordingCategoriess", $timerecordingCategoriess);
$this->layout()->set("timerecordingBilling", $timerecordingBilling);
$this->layout()->set("timerecordingCars", $timerecordingCars);
$this->layout()->setTemplate("Timerecording/Index");
}
protected function apiAction()
{
$do = $this->request->do;
$datatype = $this->request->datatype;
$dataweek = $this->request->dataweek;
$datamonth = $this->request->datamonth;
$datayear = $this->request->datayear;
$ajax = $this->request->ajax;
$data = [];
switch ($do) {
case "getTimerecordings":
$return = $this->getTimerecordingsApi($datatype, $dataweek, $datamonth, $datayear);
break;
case "fillWorkinghours":
$return = $this->fillWorkinghours($dataweek);
break;
case "checkWorkinghours":
$return = $this->checkWorkingHours($ajax);
break;
default:
$return = false;
}
if (!is_array($return) || !count($return)) {
$data = ["status" => "error"];
$this->returnJson($data);
}
$data['status'] = "OK";
$data['result'] = $return;
$this->returnJson($data);
}
protected function addAction()
{
$timerecordingCategoriess = TimerecordingCategoryModel::getAll();
$this->layout()->set("timerecordingCategoriess", $timerecordingCategoriess);
$this->layout()->setTemplate("Timerecording/Form");
}
protected function editAction()
{
$id = $this->request->id;
if (!is_numeric($id) || !$id) {
$this->layout()->setFlash("Buchung nicht gefunden", "error");
$this->redirect("Timerecording");
}
$timerecordings = new Timerecording($id);
if ($timerecordings->id != $id) {
$this->layout()->setFlash("Buchung nicht gefunden", "error");
$this->redirect("Timerecording");
}
$this->layout()->set("timerecordings", $timerecordings);
return $this->addAction();
}
protected function checkTimerecording($starttime, $endtime, $id = NULL)
{
$r = $this->request;
if ($r->user_id) {
$userid = $r->user_id;
} else {
$userid = $this->me->id;
}
if (!$endtime) {
$endtime = strtotime(date("Y-m-d", $starttime) . " 23:59:00");
}
$searchArray = ['user_id' => $userid, 'starttime' => $starttime, 'endtime' => $endtime];
if ($id) {
$searchArray['id'] = $id;
}
$timerecordings = TimerecordingModel::search($searchArray);
if ($timerecordings) {
$result['state'] = "error";
$result['error'] = "Zeitüberschneidung mit anderer Buchung";
} else {
$result['state'] = "success";
}
return $result;
}
protected function updateOpenTimerecording()
{
$r = $this->request;
$date = strtotime($r->date . " 23:59:00");
if ($r->user_id) {
$userid = $r->user_id;
} else {
$userid = $this->me->id;
}
$searchArray = ['user_id' => $userid, 'type' => 'opentimerecording'];
$timerecordings = TimerecordingModel::search($searchArray);
$timerecordingssave = new Timerecording($timerecordings[0]->id);
$data = [];
$data['end'] = $date;
$timerecordingssave->update($data);
$id = $timerecordingssave->save();
if ($timerecordingssave->id) {
if ($r->ajax == 1) {
$message = "Buchung erfolgreich angelegt";
$result['state'] = "success";
$result['message'] = "$message";
echo json_encode($result);
die();
}
} else {
if ($r->ajax == 1) {
$message = "Buchung konnte nicht angelegt werden";
$result['state'] = "error";
$result['error'] = "$message";
echo json_encode($result);
die();
}
}
}
protected function checkWorkingHours($ajax = 0)
{
$r = $this->request;
$starttime = strtotime($r->date . " " . $r->start . ":00");
$endtime = strtotime($r->date . " " . $r->end . ":00");
$date = $r->date;
if ($r->user_id) {
$userid = $r->user_id;
} else {
$userid = $this->me->id;
}
$workinghours = TimerecordingEmployeeWorkingHourModel::search(['user_id' => $userid]);
$realHolidays = TimerecordingHolidayModel::getAll();
foreach ($realHolidays as $realHoliday) {
$realholiDay[date('Y-m-d', $realHoliday->timestamp)] = $realHoliday->timestamp;
}
if ($realholiDay[$date]) {
$result['state'] = "error";
$result['error'] = "Kein Zeitausgleich an einem Feiertag möglich";
echo json_encode($result);
die();
}
foreach ($workinghours as $workinghour) {
$whstart = strtotime(date('Y-m-d', time()) . " " . $workinghour->start . ":00");
$whend = strtotime(date('Y-m-d', time()) . " " . $workinghour->end . ":00");
if (!$workingHours[$workinghour->day]) {
$workingHours[$workinghour->day] = $whend - $whstart;
} else {
$workingHours[$workinghour->day] = $workingHours[$workinghour->day] + $whend - $whstart;
}
}
if (!$workingHours[date("w", $starttime)]) {
$result['state'] = "error";
$result['error'] = "Kein Zeitausgleich außerhalb der Arbeitszeiten möglich";
echo json_encode($result);
die();
}
$maxTime = $workingHours[date("w", $starttime)];
$isTime = $endtime - $starttime;
$cleanTime = $isTime;
if ($maxTime < $isTime) {
$result['state'] = "error";
$result['error'] = "Zeitausgleich kann maximal " . sprintf('%02dh:%02dm', floor($maxTime / 3600), floor($maxTime / 60 % 60)) . " an diesem Tag betragen.";
echo json_encode($result);
die();
}
$employee = TimerecordingEmployeeModel::search(['user_id' => $userid]);
$plushours = $employee[0]->plushours_now * 1.25;
$overtime = $employee[0]->overtime_now * 1.5;
$overtimesum = $plushours + $overtime;
if (!$this->me->can('Fibu')):
if ($overtimesum < $isTime) {
if ($overtimesum < 0) {
$overtimesum = 0;
}
$result['state'] = "error";
$result['error'] = "Maximal verfügbarer ZA: " . sprintf('%02dh:%02dm', floor($overtimesum / 3600), floor($overtimesum / 60 % 60)) . ".";
echo json_encode($result);
die();
}
endif;
if ($plushours >= $isTime) {
$isTime = $isTime * 0.8;
$return['hours'] = $isTime;
} elseif ($plushours == 0) {
$isTime = $isTime * 0.66666666666666666666666666666667;
$return ['hours_overtime'] = $isTime;
} elseif ($plushours <= 0 && $overtime <= 0) {
$return['hours'] = $isTime;
} else {
$isTime25 = $plushours;
$isTime50 = $isTime - $plushours;
$isTime25 = $isTime25 * 0.8;
$isTime50 = $isTime50 * 0.66666666666666666666666666666667;
$isTime = $isTime25 + $isTime50;
$return['hours'] = $isTime25;
$return['hours_overtime'] = $isTime50;
}
if ($return['hours_overtime'] && $return['hours_overtime'] > $overtime) {
$excess = $return['hours_overtime'] - $overtime;
$return['hours_overtime'] = $overtime;
// überschüssige Sekunden 1:1 zu normalen Stunden
$return['hours'] += $excess * 1.5;
}
if ($ajax == 1) {
if ($r->ajax == 1) {
if ($isTime < 0) {
$isTime = 0;
}
if ($cleanTime < 0) {
$cleanTime = 0;
}
$result['state'] = "success";
$result['message'] = "<span class='text-bold'>Abzug:</span> " . sprintf('%02dh:%02dm', floor($isTime / 3600), floor($isTime / 60 % 60)) . " <span class='text-bold ml-1'>Zeitwert:</span> " . sprintf('%02dh:%02dm', floor($cleanTime / 3600), floor($cleanTime / 60 % 60));
echo json_encode($result);
die();
}
} else {
return $return;
}
}
protected function saveAction()
{
$r = $this->request;
$id = $r->id;
$oldbpseconds = 0;
$enddate = $r->enddate;
$hourday = $r->hourday;
if ($hourday == 4) {
$this->updateOpenTimerecording();
}
$data = [];
if ($hourday == 1 || $hourday == 6 || $hourday == 7) {
$starttime = strtotime($r->date . " " . $r->start . ":00");
$endtime = strtotime($r->date . " " . $r->end . ":00");
if ($hourday == 6) {
$data = $this->checkWorkingHours();
}
} elseif ($hourday == 2) {
$starttime = strtotime($r->date . " 00:00:00");
$endtime = strtotime($enddate . " 23:59:00");
} else if ($hourday == 3) {
$starttime = strtotime($r->date . " 00:00:00");
$endtime = NULL;
} else if ($hourday == 5 || $hourday == 9) {
$starttime = strtotime($r->date . " 00:00:00");
$endtime = strtotime($r->date . " 23:59:00");
} else if ($hourday == 8) {
$starttime = strtotime($r->date . " 00:00:00");
$endtime = strtotime($r->date . " 23:59:00");
}
if ($hourday != 5 && $hourday != 7 && $hourday != 8 && $hourday != 9) {
$result = $this->checkTimerecording($starttime, $endtime, $id);
}
if ($result['state'] == "error") {
if ($r->ajax == 1) {
echo json_encode($result);
die();
}
$this->layout()->setFlash($result['error'], "error");
$this->redirect("Timerecording");
}
if (is_numeric($id) && $id > 0) {
$mode = "edit";
$timerecordings = new Timerecording($id);
$oldbpseconds = $timerecordings->hours_bpa;
if (!$timerecordings->id) {
$this->layout()->setFlash("Buchungen nicht gefunden", "error");
$this->redirect("Timerecording");
}
} else {
$mode = "add";
}
if ($r->user_id) {
$data['user_id'] = $r->user_id;
} else {
$data['user_id'] = $this->me->id;
}
$data['start'] = $starttime;
$data['end'] = $endtime;
$data['timerecordingCategory_id'] = trim($r->timerecordingCategory_id);
$data['comment'] = trim($r->comment);
$data['businesstrip'] = $r->businesstrip;
$data['businesstrip_info'] = $r->businesstrip_info;
$data['homeoffice'] = $r->homeoffice;
if ($hourday == 8) {
$data['days'] = 0;
$data['hours_bpa'] = $r->days * 60 * 60;
} else if ($hourday == 9) {
$data['days'] = 0;
$data['hours'] = $r->days * 60 * 60;
$data['hours'] = 0 - $data['hours'];
} else {
$data['days'] = $r->days;
}
$data['timerecordingCar_id'] = $r->timerecordingCar_id;
$data['mileage_start'] = $r->mileage_start;
$data['mileage_end'] = $r->mileage_end;
if (!$data['timerecordingCar_id']) {
$data['timerecordingCar_id'] = NULL;
}
if (!$data['mileage_start']) {
$data['mileage_start'] = NULL;
}
if (!$data['mileage_end']) {
$data['mileage_end'] = NULL;
}
if (!$data['businesstrip'] || $data['businesstrip'] == "false") {
$data['businesstrip'] = 0;
}
if (!$data['homeoffice'] || $data['homeoffice'] == "false") {
$data['homeoffice'] = 0;
}
if (!$data['days'] || $data['days'] == "false") {
$data['days'] = 0;
}
if ($r->businesstrip == 1 && !$r->businesstrip_info) {
$result['state'] = "error";
$result['error'] = "Dienstreiseort darf nicht leer sein";
echo json_encode($result);
die();
}
if (!$data['businesstrip_info']) {
$data['businesstrip_info'] = NULL;
}
if (!$data['user_id']) {
if ($r->ajax == 1) {
$result['state'] = "error";
$result['error'] = "Benutzer darf nicht leer sein";
echo json_encode($result);
die();
}
$this->layout()->setFlash("Benutzer darf nicht leer sein", "error");
$this->redirect("Timerecording");
}
if ($data['start'] < 1577833200) {
if ($r->ajax == 1) {
$result['state'] = "error";
$result['error'] = "Ungültige Startzeit";
echo json_encode($result);
die();
}
$this->layout()->setFlash("Ungültige Startzeit", "error");
$this->redirect("Timerecording");
}
if ($data['end'] && $data['end'] < 1577833200) {
if ($r->ajax == 1) {
$result['state'] = "error";
$result['error'] = "Ungültige Endzeit";
echo json_encode($result);
die();
}
$this->layout()->setFlash("Ungültige Endzeit", "error");
$this->redirect("Timerecording");
}
if (!$data['timerecordingCategory_id']) {
$data['timerecordingCategory_id'] = NULL;
}
if (!$data['comment']) {
$data['comment'] = NULL;
}
if ($mode == "edit") {
$timerecordings->update($data);
} else {
$timerecordings = TimerecordingModel::create($data);
}
// var_dump($filestore);
// exit;
$id = $timerecordings->save();
if (!$id) {
$message = "Buchung konnte nicht angelegt werden";
if ($r->ajax == 1) {
$result['state'] = "error";
$result['error'] = $message;
echo json_encode($result);
die();
}
$this->layout()->setFlash($message, "error");
$this->redirect("Timerecording");
}
if ($id) {
$timerecordingCategoriess = TimerecordingCategoryModel::search(['id' => $data['timerecordingCategory_id']]);
if ($timerecordingCategoriess[0]->approval == "1" && !$r->user_id) {
$body = 'Beantrag von: ' . $this->me->name . '
';
$body .= 'Buchungsart: ' . $timerecordingCategoriess[0]->name . '
';
if ($timerecordingCategoriess[0]->hourday == "1") {
$body .= 'von: ' . date("d.m.Y H:i", $data['start']) . ' bis: ' . date("H:i", $data['end']);
} else if ($timerecordingCategoriess[0]->hourday == "2") {
$body .= 'von: ' . date("d.m.Y", $data['start']) . ' bis: ' . date("d.m.Y", $data['end']);
} else if ($timerecordingCategoriess[0]->hourday == "6") {
$body .= 'von: ' . date("d.m.Y H:i", $data['start']) . ' bis: ' . date("H:i", $data['end']);
}
$email = new Emailnotification();
$email->setSubject('Antrag für ' . $timerecordingCategoriess[0]->name . ' erstellt');
$email->setBody($body);
$email->setFrom(TT_TIMERECORDING_EMAIL, TT_TIMERECORDING_EMAIL_NAME);
$email->setTo($this->me->email);
$email->send();
$email = new Emailnotification();
$email->setSubject('Antrag für ' . $timerecordingCategoriess[0]->name . ' erstellt (' . $this->me->name . ')');
$email->setBody($body);
$email->setFrom(TT_TIMERECORDING_EMAIL, TT_TIMERECORDING_EMAIL_NAME);
$email->setTo(TT_TIMERECORDING_EMAIL);
$email->send();
}
if ($data['timerecordingCategory_id'] == "3" || $timerecordingCategoriess[0]->hourday == "5") {
$this->updateHolidays($data['user_id']);
}
$this->updatePlushours($data['user_id']);
TimerecordingCarModel::calcMileage();
if ($data['hours_bpa']) {
$this->updateBpHours($data['user_id'], $data['hours_bpa'], $oldbpseconds);
}
}
if ($mode == "edit") {
$message = "Buchung erfolgreich geändert";
} else if ($mode = "add") {
$message = "Buchung erfolgreich angelegt";
}
if ($r->ajax == 1) {
$result['state'] = "success";
$result['message'] = "$message";
echo json_encode($result);
die();
}
$this->layout()->setFlash($message, "success");
$this->redirect("Timerecording");
}
public function updateHolidays($userid)
{
$employee = TimerecordingEmployeeModel::search(['user_id' => $userid]);
if ($employee) {
$employee = $employee[0];
$holidays = $employee->holidays;
$holidays_now = $employee->holidays_now;
$holidays_timestamp = $employee->holidays_timestamp;
$workinghours = TimerecordingEmployeeWorkingHourModel::search(['user_id' => $userid]);
$realHolidays = TimerecordingHolidayModel::getAll();
foreach ($realHolidays as $realHoliday) {
$realholiDay[date('Y-m-d', $realHoliday->timestamp)] = $realHoliday->timestamp;
}
if (!$holidays_timestamp) {
//$holidays_timestamp = $employee->startdate;
$holidays_timestamp = strtotime('2024-01-01 00:00:00');
$holidays_now = $holidays;
}
$timerecordings = TimerecordingModel::search(['user_id' => $userid, 'start' => $holidays_timestamp, 'timerecordingCategory_id' => 3]);
$timerecordingscorrections = TimerecordingModel::search(['user_id' => $userid, 'start' => $holidays_timestamp, 'days' => 1]);
foreach ($timerecordings as $timerecording) {
$daycounter = ($timerecording->end - $timerecording->start) / 86400;
$daycounter = intval(round($daycounter, 0, PHP_ROUND_HALF_DOWN));
$daycounter = $daycounter * 86400;
if (is_int($daycounter)) {
for ($i = 86400; $i <= $daycounter; $i = $i + 86400) {
$holidayDays[date("Y-m-d", $timerecording->start + $i - 86400)] = 1;
}
}
}
foreach ($workinghours as $workinghour) {
$whstart = strtotime(date('Y-m-d', time()) . " " . $workinghour->start . ":00");
$whend = strtotime(date('Y-m-d', time()) . " " . $workinghour->end . ":00");
if (!$workingHours[$workinghour->day]) {
$workingHours[$workinghour->day] = $whend - $whstart;
} else {
$workingHours[$workinghour->day] = $workingHours[$workinghour->day] + $whend - $whstart;
}
}
$workinghourshistory = TimerecordingEmployeeWorkingHourHistoryModel::search(['user_id' => $userid]);
if ($workinghourshistory) {
$workingHoursHistory[2147483500] = $workingHours;
foreach ($workinghourshistory as $workinghourhistory) {
$whenddate = $workinghourhistory->enddate;
$workinghourhistoryhours = json_decode($workinghourhistory->workinghours, true);
foreach ($workinghourhistoryhours as $workinghourhistoryhour) {
$whstart = strtotime(date('Y-m-d', time()) . " " . $workinghourhistoryhour['start'] . ":00");
$whend = strtotime(date('Y-m-d', time()) . " " . $workinghourhistoryhour['end'] . ":00");
if (!$workingHoursHistory[$whenddate][$workinghourhistoryhour['day']]) {
$workingHoursHistory[$whenddate][$workinghourhistoryhour['day']] = $whend - $whstart;
} else {
$workingHoursHistory[$whenddate][$workinghourhistoryhour['day']] = $workingHoursHistory[$whenddate][$workinghourhistoryhour['day']] + $whend - $whstart;
}
}
}
}
//check if holiday is already in the list
foreach ($holidayDays as $key => $holidayDay) {
if ($workingHoursHistory) {
foreach ($workingHoursHistory as $whkey => $whdata) {
$whtimestamp = strtotime(date('Y-m-d 23:59:59', $whkey));
$timestamp = strtotime($key);
if ($whtimestamp >= $timestamp) {
$workingHours = $whdata;
}
}
}
if (($realholiDay[$key])) {
} else if ($workingHours[date('w', strtotime($key))]) {
$holidays_now--;
}
}
foreach ($timerecordingscorrections as $timerecordingscorrection) {
$holidays_now = $holidays_now + $timerecordingscorrection->days;
}
if ($holidays_now != $employee->holidays_now) {
$employeeupdate = new TimerecordingEmployee($employee->id);
$data = [];
$data['holidays_now'] = $holidays_now;
$employeeupdate->update($data);
$employeeupdate->save();
}
}
}
public function updateBpHours($userid, $seconds, $oldseconds = 0)
{
$employee = TimerecordingEmployeeModel::search(['user_id' => $userid]);
if (!$employee) {
return;
}
$employee = $employee[0];
$bpHours = $employee->bpahours;
$employeeupdate = new TimerecordingEmployee($employee->id);
$data = [];
$data['bpahours'] = $bpHours + $seconds - $oldseconds;
$employeeupdate->update($data);
$employeeupdate->save();
}
public function updatePlushours($userid, $enddate = null)
{
$employee = TimerecordingEmployeeModel::search(['user_id' => $userid]);
if ($employee) {
$employee = $employee[0];
$plushours = $employee->plushours;
$plushours_now = $employee->plushours_now;
$plushours_timestamp = $employee->plushours_timestamp;
$overtime = $employee->overtime;
$overtime_now = $employee->overtime_now;
$overtime_timestamp = $employee->overtime_timestamp;
if (!$plushours_timestamp) {
$plushours_timestamp = $employee->startdate;
$plushours_now = $plushours;
}
if (!$overtime_timestamp) {
$overtime_timestamp = $employee->startdate;
$overtime_now = $overtime;
}
if (!$enddate) {
if ($employee->enddate && $employee->enddate < time() - 86400) {
$endtime = strtotime(date("Y-m-d", $employee->enddate) . " 23:59:00");
} else {
$endtime = time() - 86400;
}
} else {
if ($employee->enddate && $employee->enddate < $enddate) {
$endtime = strtotime(date("Y-m-d", $employee->enddate) . " 23:59:00");
} else {
$endtime = $enddate;
}
}
$endtime = date("Y-m-d", $endtime);
$endtime = strtotime($endtime . " 23:59:00");
if ($plushours_timestamp > 0) {
$return = $this->getTimerecordingsApi(5, null, null, null, $plushours_timestamp, $endtime, $userid);
// echo $return['is'] ." ". $return['must'].PHP_EOL;
$diffTime = $return['is'] - $return['must'];
$plushours_now = $plushours_now + $diffTime;
} else {
$diffTime = 0;
}
$minushours = 0;
$minushoursovertime = 0;
$timerecordings = TimerecordingModel::search(['user_id' => $userid, 'start' => $plushours_timestamp, 'hours' => 1]);
foreach ($timerecordings as $timerecording) {
if ($timerecording->hours) {
$timediff = $timerecording->hours;
$minushours = $minushours + $timediff;
}
if ($timerecording->hours_overtime) {
$timediffovertime = $timerecording->hours_overtime;
$minushoursovertime = $minushoursovertime + $timediffovertime;
}
}
$plushours_now = $plushours_now - $minushours;
$diffTime = $diffTime - $minushours;
$overtime_now = $overtime_now - $minushoursovertime;
if (!$enddate) {
if ($employee->plushours_now != $plushours_now || $employee->overtime_now != $overtime_now) {
$employeeupdate = new TimerecordingEmployee($employee->id);
$data = [];
$data['plushours_now'] = $plushours_now;
$data['overtime_now'] = $overtime_now;
$employeeupdate->update($data);
$employeeupdate->save();
}
} else {
$response['plushours'] = $plushours_now;
$response['overtime'] = $overtime_now;
return $response;
}
}
}
public function getTimerecordingsApi($datatype, $dataweek, $datamonth, $datayear, $startime = null, $endtime = null, $userid = null, $ajax = 1)
{
$holidayrows = [];
$mustSeconds = 0;
$isSeconds = 0;
$holiDays = 0;
$plusHours = 0;
$startdate = time();
$allhours = 0;
if (!$userid) {
$userid = $this->me->id;
}
$rows = [];
$employee = TimerecordingEmployeeModel::search(['user_id' => $userid]);
if ($employee) {
$holiDays = $employee[0]->holidays_now;
$plusHours = $employee[0]->plushours_now;
$auto_workinghours = $employee[0]->auto_workinghours;
$startdate = $employee[0]->startdate;
if ($employee[0]->enddate) {
$enddate = strtotime(date('Y-m-d', $employee[0]->enddate) . " 23:59:59");
} else {
$enddate = 2208985200;
}
$overtime = $employee[0]->overtime_now;
if ($employee[0]->only_admin) {
$getAllHours = TimerecordingModel::getAllHours($userid);
$getAllHours = $getAllHours[0]->gesamt_summe;
}
}
$workinghours = TimerecordingEmployeeWorkingHourModel::search(['user_id' => $userid]);
$holidays = TimerecordingHolidayModel::getAll();
foreach ($workinghours as $workinghour) {
$whstart = strtotime(date('Y-m-d', time()) . " " . $workinghour->start . ":00");
$whend = strtotime(date('Y-m-d', time()) . " " . $workinghour->end . ":00");
if (!$workingHours[$workinghour->day]) {
$workingHours[$workinghour->day] = $whend - $whstart;
} else {
$workingHours[$workinghour->day] = $workingHours[$workinghour->day] + $whend - $whstart;
}
}
$workinghourshistory = TimerecordingEmployeeWorkingHourHistoryModel::search(['user_id' => $userid]);
if ($workinghourshistory) {
$workingHoursHistory[2147483500] = $workingHours;
foreach ($workinghourshistory as $workinghourhistory) {
$whenddate = $workinghourhistory->enddate;
$workinghourhistoryhours = json_decode($workinghourhistory->workinghours, true);
foreach ($workinghourhistoryhours as $workinghourhistoryhour) {
$whstart = strtotime(date('Y-m-d', time()) . " " . $workinghourhistoryhour['start'] . ":00");
$whend = strtotime(date('Y-m-d', time()) . " " . $workinghourhistoryhour['end'] . ":00");
if (!$workingHoursHistory[$whenddate][$workinghourhistoryhour['day']]) {
$workingHoursHistory[$whenddate][$workinghourhistoryhour['day']] = $whend - $whstart;
} else {
$workingHoursHistory[$whenddate][$workinghourhistoryhour['day']] = $workingHoursHistory[$whenddate][$workinghourhistoryhour['day']] + $whend - $whstart;
}
}
}
}
foreach ($holidays as $holiday) {
$holiDay[date('Y-m-d', $holiday->timestamp)] = $holiday->description;
}
if ($datatype == 1) {
$kw = date('W', $dataweek);
$year = date('o', $dataweek);
$timestamp_montag = strtotime("{$year}-W{$kw}");
$timestamp_sonntag = strtotime("{$year}-W{$kw}-7");
$firstdate = strtotime(date("Y-m-d", $timestamp_montag) . " 00:00:00");
$lastdate = strtotime(date("Y-m-d", $timestamp_sonntag) . ' 23:59:59');
$searchArray = ['user_id' => $userid, 'start' => $timestamp_montag, 'end' => $lastdate];
$daycounter = '0';
$timestamp = $timestamp_montag;
for ($i = 1; $i <= 7; $i++) {
$WintertimeCompensation = 0;
if (date('I', $timestamp) == 0) {
$WintertimeCompensation = 3600;
}
$dDate = date('Y-m-d', $timestamp + $WintertimeCompensation);
$dDay = date('w', $timestamp + $WintertimeCompensation);
if ($workingHoursHistory) {
foreach ($workingHoursHistory as $whkey => $whdata) {
$whtimestamp = strtotime(date('Y-m-d 23:59:59', $whkey));
if ($whtimestamp >= $timestamp) {
unset($workingHours);
$workingHours = $whdata;
}
}
}
if (!$holiDay[$dDate] && $dDate >= date('Y-m-d', $startdate) && $dDate <= date('Y-m-d', $enddate)) {
$mustSeconds = $mustSeconds + $workingHours[$dDay];
} elseif ($holiDay[$dDate]) {
$holidayrows[$timestamp] = $holiDay[$dDate];
}
$timestamp = $timestamp + 86400;
}
} else if ($datatype == 2) {
$firstdate = strtotime(date("Y-m-01", $datamonth));
$lastdate = strtotime(date("Y-m-t", $datamonth));
$daycount = date("t", $datamonth);
$lastdate = strtotime(date("Y-m-d", $lastdate) . ' 23:59:59');
$searchArray = ['user_id' => $userid, 'start' => $firstdate, 'end' => $lastdate];
$timestamp = $firstdate;
for ($i = 1; $i <= $daycount; $i++) {
$WintertimeCompensation = 0;
if (date('I', $timestamp) == 0) {
$WintertimeCompensation = 3600;
}
$dDate = date('Y-m-d', $timestamp + $WintertimeCompensation);
$dDay = date('w', $timestamp + $WintertimeCompensation);
if ($workingHoursHistory) {
foreach ($workingHoursHistory as $whkey => $whdata) {
$whtimestamp = strtotime(date('Y-m-d 23:59:59', $whkey));
if ($whtimestamp >= $timestamp) {
unset($workingHours);
$workingHours = $whdata;
}
}
}
if (!$holiDay[$dDate] && $dDate >= date('Y-m-d', $startdate) && $dDate <= date('Y-m-d', $enddate)) {
$mustSeconds = $mustSeconds + $workingHours[$dDay];
} elseif ($holiDay[$dDate]) {
$holidayrows[$timestamp] = $holiDay[$dDate];
}
$timestamp = $timestamp + 86400;
}
} else if ($datatype == 3) {
$firstdate = strtotime(date("Y-01-01", $datayear));
$lastdate = strtotime(date("Y-12-31 23:59:59", $datayear));
$daycount = date("t", $datamonth);
$lastdate = strtotime(date("Y-m-d", $lastdate) . ' 23:59:59');
$searchArray = ['user_id' => $userid, 'start' => $firstdate, 'end' => $lastdate];
$timestamp = $firstdate;
for ($i = 1; $i <= 366; $i++) {
$WintertimeCompensation = 0;
if (date('I', $timestamp) == 0) {
$WintertimeCompensation = 3600;
}
$dDate = date('Y-m-d', $timestamp + $WintertimeCompensation);
$dDay = date('w', $timestamp + $WintertimeCompensation);
if ($workingHoursHistory) {
foreach ($workingHoursHistory as $whkey => $whdata) {
$whtimestamp = strtotime(date('Y-m-d 23:59:59', $whkey));
if ($whtimestamp >= $timestamp) {
unset($workingHours);
$workingHours = $whdata;
}
}
}
if (!$holiDay[$dDate] && $dDate >= date('Y-m-d', $startdate) && $dDate <= date('Y-m-d', $enddate)) {
$mustSeconds = $mustSeconds + $workingHours[$dDay];
} elseif ($holiDay[$dDate]) {
$holidayrows[$timestamp] = $holiDay[$dDate];
}
$timestamp = $timestamp + 86400;
}
} else if ($datatype == 5) {
$firstdate = $startime;
$lastdate = $endtime;
$timediff = $lastdate - $firstdate;
$daycount = $timediff / 86400;
$daycount = round($daycount, 0, PHP_ROUND_HALF_DOWN);
$searchArray = ['user_id' => $userid, 'start' => $firstdate, 'end' => $lastdate];
$timestamp = $firstdate;
for ($i = 1; $i <= $daycount; $i++) {
$WintertimeCompensation = 0;
if (date('I', $timestamp) == 0) {
$WintertimeCompensation = 3600;
}
if ($workingHoursHistory) {
foreach ($workingHoursHistory as $whkey => $whdata) {
$whtimestamp = strtotime(date('Y-m-d 23:59:59', $whkey));
if ($whtimestamp >= $timestamp) {
unset($workingHours);
$workingHours = $whdata;
// echo date('Y-m-d 23:59:59', $whkey).PHP_EOL;
}
}
}
$dDate = date('Y-m-d', $timestamp + $WintertimeCompensation);
$dDay = date('w', $timestamp + $WintertimeCompensation);
if (!$holiDay[$dDate]) {
$mustSeconds = $mustSeconds + $workingHours[$dDay];
}
$timestamp = $timestamp + 86400;
}
}
$daysgerm = array("So", "Mo", "Di", "Mi", "Do", "Fr", "Sa");
$timerecordingcategories = TimerecordingCategoryModel::getAll();
$timerecordings = TimerecordingModel::search($searchArray);
$responsecount = count($timerecordings);
foreach ($timerecordings as $timerecording):
$state = "";
$enddate = "";
$sum = "-";
$day = "";
$orderdate = $timerecording->start;
if ($timerecording->timerecordingCategory->hourday == 1) {
$date = date("d.m.Y", $timerecording->start);
$datadate = date("Y-m-d", $timerecording->start);
$start = date("H:i", $timerecording->start);
$end = date("H:i", $timerecording->end);
$seconds = $timerecording->end - $timerecording->start;
$minutes = floor(($seconds % 3600) / 60);
$hours = floor($seconds / 3600);
$sum = sprintf("%02d", $hours) . ":" . sprintf("%02d", $minutes);
$day = $daysgerm[date("w", $timerecording->start)];
$isSeconds = $isSeconds + $seconds;
if ($ajax == 0) {
$workdaycheck[] = $datadate;
}
} else if ($timerecording->timerecordingCategory->hourday == 2 || ($timerecording->timerecordingCategory->hourday == 3 && $timerecording->end)) {
$date = date("d.m.", $timerecording->start) . " - " . $daysgerm[date("w", $timerecording->end)] . " " . date("d.m.Y", $timerecording->end);
$datadate = date("Y-m-d", $timerecording->start);
$enddate = date("Y-m-d", $timerecording->end);
$start = "-";
$end = "-";
$day = $daysgerm[date("w", $timerecording->start)];
if ($lastdate < $timerecording->end) {
$endtimecalc = $lastdate;
} else {
$endtimecalc = $timerecording->end;
}
if ($firstdate > $timerecording->start) {
$starttimecalc = $firstdate;
} else {
$starttimecalc = $timerecording->start;
}
$summcounter = 0;
$savecounter = 0;
$sumdays = 0;
for ($i = $starttimecalc; $i <= $endtimecalc; $i = $i + 86400) {
if ($workingHoursHistory) {
foreach ($workingHoursHistory as $whkey => $whdata) {
$whtimestamp = strtotime(date('Y-m-d 23:59:59', $whkey));
if ($whtimestamp >= $i) {
unset($workingHours);
$workingHours = $whdata;
}
}
}
$holidaycounter = $workingHours[date("w", $i)];
$daycheck = date("Y-m-d", $i);
if ($ajax == 0) {
$workdaycheck[] = $daycheck;
}
if (!$holiDay[$daycheck]) {
if ($holidaycounter) {
$isSeconds = $isSeconds + $holidaycounter;
$summcounter = $summcounter + $holidaycounter;
$sumdays++;
}
}
if ($savecounter == 1000) {
echo $savecounter;
die();
}
$savecounter++;
}
$seconds = $summcounter;
$minutes = floor(($seconds % 3600) / 60);
$hours = floor($seconds / 3600);
// $sum = sprintf("%02d", $hours) . ":" . sprintf("%02d", $minutes);
if ($sumdays == 1 || $sumdays == 0) {
$sum = $sumdays . " Tag";
} else if ($sumdays > 1) {
$sum = $sumdays . " Tage";
}
} else if ($timerecording->timerecordingCategory->hourday == 3 && !$timerecording->end) {
$date = date("d.m.Y", $timerecording->start) . " - " . $daysgerm[date("w", time())] . " " . date("d.m.Y", time());;
$datadate = date("Y-m-d", $timerecording->start);
$enddatetemp = date("Y-m-d", time());
$enddatetemp = strtotime($enddatetemp . " 23:59:59");
$enddate = date("Y-m-d", $enddatetemp + 7200);
$start = "-";
$end = "-";
$day = $daysgerm[date("w", $timerecording->start)];
if ($lastdate < time()) {
$endtimecalc = $lastdate;
} else {
$endtimecalc = time();
}
$summcounter = 0;
if ($firstdate > $timerecording->start) {
$starttimecalc = $firstdate;
} else {
$starttimecalc = $timerecording->start;
}
$summcounter = 0;
$savecounter = 0;
// echo $starttimecalc."<br>";
for ($i = $starttimecalc; $i <= $endtimecalc; $i = $i + 86400) {
if ($workingHoursHistory) {
foreach ($workingHoursHistory as $whkey => $whdata) {
$whtimestamp = strtotime(date('Y-m-d 23:59:59', $whkey));
if ($whtimestamp >= $i) {
unset($workingHours);
$workingHours = $whdata;
}
}
}
if ($ajax == 0) {
$daycheck = date("Y-m-d", $i);
$workdaycheck[] = $daycheck;
}
$holidaycounter = $workingHours[$timerecording->user_id][date("w", $i)];
$daycheck = date("Y-m-d", $i);
if (!$holiDay[$daycheck]) {
$isSeconds = $isSeconds + $holidaycounter;
$summcounter = $summcounter + $holidaycounter;;
}
if ($savecounter == 1000) {
echo $savecounter;
die();
}
$savecounter++;
}
$seconds = $summcounter;
$minutes = floor(($seconds % 3600) / 60);
$hours = floor($seconds / 3600);
$sum = sprintf("%02d", $hours) . ":" . sprintf("%02d", $minutes);
} else if ($timerecording->timerecordingCategory->hourday == 5) {
$date = date("d.m.Y", $timerecording->start);
$start = "-";
$end = "-";
$day = $daysgerm[date("w", $timerecording->start)];
if ($timerecording->days > 0) {
if ($timerecording->days == 1) {
$sum = "+" . $timerecording->days . " Tag";
} else {
$sum = "+" . $timerecording->days . " Tage";
}
} else {
if ($timerecording->days == -1) {
$sum = $timerecording->days . " Tag";
} else {
$sum = $timerecording->days . " Tage";
}
}
} else if ($timerecording->timerecordingCategory->hourday == 6) {
$date = date("d.m.Y", $timerecording->start);
$datadate = date("Y-m-d", $timerecording->start);
$start = date("H:i", $timerecording->start);
$end = date("H:i", $timerecording->end);
// $seconds = $timerecording->hours;
$seconds = $timerecording->end - $timerecording->start;
$minutes = floor(($seconds % 3600) / 60);
$hours = floor($seconds / 3600);
$sum = "-" . sprintf("%02d", $hours) . ":" . sprintf("%02d", $minutes);
$day = $daysgerm[date("w", $timerecording->start)];
$isSeconds = $isSeconds + $seconds;
if ($ajax == 0) {
$workdaycheck[] = $datadate;
}
} else if ($timerecording->timerecordingCategory->hourday == 7) {
$date = date("d.m.Y", $timerecording->start);
$datadate = date("Y-m-d", $timerecording->start);
$start = date("H:i", $timerecording->start);
$end = date("H:i", $timerecording->end);
$seconds = $timerecording->end - $timerecording->start;
$minutes = floor(($seconds % 3600) / 60);
$hours = floor($seconds / 3600);
$sum = sprintf("%02d", $hours) . ":" . sprintf("%02d", $minutes);
$day = $daysgerm[date("w", $timerecording->start)];
}
if ($timerecording->timerecordingCategory->approval == 1 && $timerecording->approved == 0) {
$state = '<i class="fa-regular fa-clock mr-1"></i>';
} else if ($timerecording->timerecordingCategory->approval == 1 && $timerecording->approved == 1) {
$state = '<i class="fa-regular fa-circle-check mr-1"></i>';
}
$edit = "";
if ($timerecording->timerecordingCategory->hourday == 7) {
$distance = $timerecording->mileage_end - $timerecording->mileage_start;
$category = "<span>" . $timerecording->timerecordingCategory->name . "</span><span class='text-bold ml-1'>(" . $timerecording->timerecordingCar->number_plate . " " . $distance . "KM) (Zielort: " . $timerecording->businesstrip_info . ")</span>";
} else if ($timerecording->businesstrip == 1) {
if ($timerecording->timerecordingCar) {
$distance = $timerecording->mileage_end - $timerecording->mileage_start;
$car = " (" . $timerecording->timerecordingCar->number_plate . " " . $distance . "KM)";
} else {
$car = "";
}
$category = "<span>" . $timerecording->timerecordingCategory->name . "</span><span class='text-bold ml-1'>$car (Dienstreise: " . $timerecording->businesstrip_info . ")</span>";
} else if ($timerecording->homeoffice == 1) {
$category = "<span>" . $timerecording->timerecordingCategory->name . "</span><span class='text-bold ml-1'> (Homeoffice)</span>";
} else {
$category = $timerecording->timerecordingCategory->name;
}
if ($timerecording->timerecordingCategory->hourday == 3 && !$timerecording->end) {
$category = $category . "<span class='text-bold ml-2'>(offen)</span>";
}
if ($timerecording->completed == 0 && $timerecording->timerecordingCategory->only_admin == 0):
if ($timerecording->approved == 0) :
$edit = '<i class="far fa-edit edit-button" data-id="' . $timerecording->id . '"
data-date="' . $datadate . '"
data-category="' . $timerecording->timerecordingCategory->id . '"
data-start="' . $start . '"
data-end="' . $end . '"
data-enddate="' . $enddate . '"
data-comment="' . $timerecording->comment . '"
data-businesstrip="' . $timerecording->businesstrip . '"
data-businesstripinfo="' . $timerecording->businesstrip_info . '"
data-homeoffice="' . $timerecording->homeoffice . '"
data-car="' . $timerecording->timerecordingCar_id . '"
data-mileagestart="' . $timerecording->mileage_start . '"
data-mileageend="' . $timerecording->mileage_end . '"
title="Bearbeiten"></i>';
else :
$edit .= '<div class="edit-placeholder"></div>';
endif;
$edit .= '<i data-id="' . $timerecording->id . '" class="fas fa-trash text-danger delete-item" ></i>';
endif;
if ($datatype == 3 && ($timerecording->timerecordingCategory->hourday == 1 || $timerecording->timerecordingCategory->hourday == 7 || $timerecording->timerecordingCategory->hourday == 5)) {
} else if ($timerecording->timerecordingCategory->hourday != 8) {
$rows[] = array(
'date' => array('date' => $state . $day . " " . $date, 'order' => $orderdate),
'start' => array('start' => $start, 'order' => $start),
'end' => array('end' => $end, 'order' => $end),
'sum' => array('sum' => $sum, 'order' => $sum),
'category' => array('category' => $category, 'order' => $timerecording->timerecordingCategory->name),
'comment' => array('comment' => $timerecording->comment, 'order' => $timerecording->comment),
'edit' => array('edit' => $edit, 'order' => $edit),
);
}
endforeach;
foreach ($holidayrows as $key => $holidayrow) {
$day = $daysgerm[date("w", $key)];
$rows[] = array(
'date' => array('date' => '<span class="text-bold holiday-text">' . $day . " " . date("d.m.Y", $key) . '</span>', 'order' => $key),
'start' => array('start' => "-", 'order' => "-"),
'end' => array('end' => "-", 'order' => "-"),
'sum' => array('sum' => "-", 'order' => "-"),
'category' => array('category' => '<span class="text-bold holiday-text">Feiertag <i class="far fa-fw fa-umbrella-beach "></i></span>', 'order' => 'Feiertag'),
'comment' => array('comment' => '<span class="text-bold holiday-text">' . $holidayrow . '</span>', 'order' => $holidayrow),
'edit' => array('edit' => "", 'order' => ""),
);
}
if ($datatype == 5) {
$response['is'] = $isSeconds;
$response['must'] = $mustSeconds;
return $response;
} else {
$plusHours = $overtime + $plusHours;
if ($plusHours < 0) {
$plusHours = $plusHours * -1;
$plusHours = "-" . sprintf('%02dh:%02dm', floor($plusHours / 3600), floor($plusHours / 60 % 60));
} else {
$plusHours = sprintf('%02dh:%02dm', floor($plusHours / 3600), floor($plusHours / 60 % 60));
}
if ($isSeconds < 0) {
$isSeconds = $isSeconds * -1;
$isSeconds = "-" . sprintf('%02dh:%02dm', floor($isSeconds / 3600), floor($isSeconds / 60 % 60));
} else {
$isSeconds = sprintf('%02dh:%02dm', floor($isSeconds / 3600), floor($isSeconds / 60 % 60));
}
$json['success'] = true;
$json['time']['auto_workinghours'] = $auto_workinghours;
$json['time']['is'] = $isSeconds;
$json['time']['must'] = sprintf('%02dh:%02dm', floor($mustSeconds / 3600), floor($mustSeconds / 60 % 60));
$json['time']['holidays'] = $holiDays;
$json['time']['plushours'] = $plusHours;
$json['time']['AllHours'] = sprintf('%02dh:%02dm', floor($getAllHours / 3600), floor($getAllHours / 60 % 60));;
$json['data'] = $rows;
$json['recordsFiltered'] = $responsecount;
$json['recordsTotal'] = $responsecount;
if ($ajax == 1) {
$json = json_encode($json);
echo trim($json);
die();
} else {
$json['workcheck'] = $workdaycheck;
return $json;
}
}
}
protected function fillWorkinghours($dataweek)
{
$employee = TimerecordingEmployeeModel::search(['user_id' => $this->me->id]);
if ($employee) {
$auto_workinghours = $employee[0]->auto_workinghours;
}
if ($auto_workinghours == 0) {
$result['state'] = "error";
$result['error'] = "Automatische Arbeitszeiten sind deaktiviert";
echo json_encode($result);
die();
}
$workinghours = TimerecordingEmployeeWorkingHourModel::search(['user_id' => $this->me->id]);
$holidays = TimerecordingHolidayModel::getAll();
foreach ($workinghours as $workinghour) {
$workingHours[$workinghour->day][] = array('start' => $workinghour->start, 'end' => $workinghour->end);
}
foreach ($holidays as $holiday) {
$holiDay[date('Y-m-d', $holiday->timestamp)] = $holiday->timestamp;
}
$kw = date('W', $dataweek);
$year = date('o', $dataweek);
$timestamp_montag = strtotime("{$year}-W{$kw}");
$timestamp_sonntag = strtotime("{$year}-W{$kw}-7");
$firstdate = strtotime(date("Y-m-d", $timestamp_montag) . " 00:00:00");
$lastdate = strtotime(date("Y-m-d", $timestamp_sonntag) . ' 23:59:59');
$searchArray = ['user_id' => $this->me->id, 'start' => $firstdate, 'end' => $lastdate];
$timerecording = TimerecordingModel::search($searchArray);
$daycounter = '0';
$update = false;
$timestamp = $timestamp_montag;
for ($i = 1; $i <= 7; $i++) {
$WintertimeCompensation = 0;
if (date('I', $timestamp) == 0) {
$WintertimeCompensation = 3600;
}
$dDate = date('Y-m-d', $timestamp + $WintertimeCompensation);
$dDay = date('w', $timestamp + $WintertimeCompensation);
if (!$holiDay[$dDate]) {
if ($workingHours[$dDay]) {
foreach ($workingHours[$dDay] as $workingHour) {
$starttime = strtotime($dDate . " " . $workingHour['start'] . ":00");
$endtime = strtotime($dDate . " " . $workingHour['end'] . ":00");
$check = $this->checkTimerecording($starttime, $endtime);
if ($employee[0]->startdate <= $starttime) {
if ($check['state'] == "success") {
$update = 1;
$data = [];
$data['user_id'] = $this->me->id;
$data['start'] = $starttime;
$data['end'] = $endtime;
$data['timerecordingCategory_id'] = 1;
$data['comment'] = "Automatisch eingetragen";
$data['businesstrip'] = 0;
$data['businesstrip_info'] = NULL;
$timerecordings = TimerecordingModel::create($data);
$id = $timerecordings->save();
}
}
}
}
}
$timestamp = $timestamp + 86400;;
}
if ($update) {
$this->updatePlushours($this->me->id);
$this->updateHolidays($this->me->id);
}
$result['message'] = "Ok";
return $result;
}
protected function deleteAction()
{
$id = $this->request->id;
$timerecordings = new Timerecording($id);
$userid = $timerecordings->user_id;
$oldbpseconds = $timerecordings->hours_bpa;
if (!$timerecordings->id || $timerecordings->id != $id) {
$this->layout()->setFlash("Buchung nicht gefunden.", "error");
$this->redirect("Timerecording");
}
if ($timerecordings->timerecordingCategory->hourday == 8) {
$this->updateBpHours($userid, 0, $oldbpseconds);
}
$timerecordings->delete();
$this->updateHolidays($userid);
$this->updatePlushours($userid);
if ($this->request->ajax == 1) {
die();
}
$this->redirect("Timerecording");
}
}