Files
thetool/application/TimerecordingEmployeeWorkingHour/TimerecordingEmployeeWorkingHourModel.php
Spitzer Daniel cc00f70cd6 Zeiterfassung Update
* Soll zeiten nun vom Startdatum abhängig
 * Neue Summierung der Sollzeiten in der Personalverwaltung
2024-03-04 11:51:19 +01:00

247 lines
8.4 KiB
PHP

<?php
class TimerecordingEmployeeWorkingHourModel
{
private $user_id;
private $day;
private $start;
private $end;
public static $days_definition = array(1 => "Montag", 2 => "Dienstag", 3 => "Mittwoch", 4 => "Donnerstag", 5 => "Freitag", 6 => "Samstag", 0 => "Sonntag");
public static $days_short = array(0 => "So", 1 => "Mo", 2 => "Di", 3 => "Mi", 4 => "Do", 5 => "Fr", 6 => "Sa");
public static function cleardays($days)
{
$days_short = TimerecordingEmployeeWorkingHourModel::$days_short;
foreach ($days_short as $day_short) {
$days = str_replace($day_short . " - " . $day_short, $day_short, $days);
}
return $days;
}
public static function find($data)
{
}
public static function create(array $data)
{
$model = new TimerecordingEmployeeWorkingHour();
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
if (substr($field, 0, 5) == "vlan_" && !$value) {
$model->$field = null;
continue;
}
$model->$field = $value;
}
}
$me = mfValuecache::singleton()->get("me");
if (!$me) {
$me = new User();
$me->loadMe();
mfValuecache::singleton()->set("me", $me);
}
if ($model->create_by === null) {
$model->create_by = $me->id;
}
if ($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getOne($id)
{
if (!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "id=$id LIMIT 1");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new TimerecordingEmployeeWorkingHour($data);
}
return $item;
}
public static function getAll()
{
$items = [];
$db = FronkDB::singleton();
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "1=1");
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new TimerecordingEmployeeWorkingHour($data);
}
}
return $items;
}
public static function getAllArray()
{
$items = [];
$daysshort = TimerecordingEmployeeWorkingHourModel::$days_short;
$db = FronkDB::singleton();
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "1=1 ORDER by `user_id`,`day`");
if ($db->num_rows($res)) {
$olduser = 0;
$counter = 0;
$oldend = "";
$oldstart = "";
$oldday = "";
$datetimetext = "";
$secondcounter = 0;
while ($data = $db->fetch_array($res)) {
$dayHours[$data['user_id']][$data['day']][] = $data['start'] . " - " . $data['end'];
if ($olduser != $data['user_id']) {
if ($counter > 0) {
$secondcounter = 0;
$datetimetext .= " - " . $daysshort[$oldday] . " " . $oldstart . " - " . $oldend;
$datetimetext = TimerecordingEmployeeWorkingHourModel::cleardays($datetimetext);
$items[$olduser]['datetimetext'] = $datetimetext;
}
$datetimetext = "";
unset($oldstart);
unset($oldend);
unset($oldday);
$items[$data['user_id']] = $data;
}
$secondcounter = $secondcounter + strtotime(date("Y-m-d " . $data['end'] . ":00")) - strtotime(date("Y-m-d " . $data['start'] . ":00"));
if (!$datetimetext) {
$datetimetext = $daysshort[$data['day']];
}
if (($oldstart != $data['start'] || $oldend != $data['end']) && $oldend) {
$datetimetext .= " - " . $daysshort[$oldday] . " " . $oldstart . " - " . $oldend;
$datetimetext .= "<br> " . $daysshort[$data['day']];
}
$items[$data['user_id']]['datetimetext'] = $datetimetext;
$items[$data['user_id']]['secondcounter'] = $secondcounter;
$olduser = $data['user_id'];
$oldstart = $data['start'];
$oldend = $data['end'];
$oldday = $data['day'];
$counter++;
}
$datetimetext .= " - " . $daysshort[$oldday] . " " . $oldstart . " - " . $oldend;
$datetimetext = TimerecordingEmployeeWorkingHourModel::cleardays($datetimetext);
$items[$olduser]['datetimetext'] = $datetimetext;
}
foreach ($dayHours as $key => $dayHour) {
foreach ($dayHour as $key2 => $dayHour2) {
$dayhours[$key][$key2] = implode("<br>", $dayHour2);
}
}
foreach ($dayhours as $key => $dayhour) {
$oldday = false;
$oldstring = false;
$counter = 0;
$oldkey = false;
$oldkey2 = false;
foreach ($dayhour as $key2 => $dayhour2) {
if ($oldday != $key2) {
if ($oldstring != $dayhour2 || $key2!=$oldkey2+1) {
if ($oldkey2) {
$daysitems[$key][$counter]['end'] = $oldkey2;
$counter++;
}
$daysitems[$key][$counter]['start'] = $key2;
}
$daysitems[$key][$counter]['string'] = $dayhour2;
}
$oldkey2 = $key2;
$oldkey = $key;
$oldstring = $dayhour2;
}
$daysitems[$key][$counter]['end'] = $key2;
}
foreach ($daysitems as $key => $daysitem) {
$datetimetext = "";
foreach ($daysitem as $key2 => $daysItem) {
if ($daysItem['start'] != $daysItem['end']) {
$datetimetext .= '<div class="d-table border-bottom border-dark"><div class="d-table-cell w-50 align-middle pr-2 ">' . $daysshort[$daysItem['start']] . "-" . $daysshort[$daysItem['end']] . '</div><div class="d-table-cell">' . $daysItem['string'] . '</div></div>';
} else {
$datetimetext .= '<div class="d-table border-bottom border-dark"><div class="d-table-cell w-50 align-middle pr-2 ">' . $daysshort[$daysItem['start']] . '</div><div class="d-table-cell">' . $daysItem['string'] . '</div></div>';
}
}
$items[$key]['datetimetext'] = $datetimetext;
}
return $items;
}
public static function deleteUserHours($userid)
{
$db = FronkDB::singleton();
$sql = "DELETE FROM `TimerecordingEmployeeWorkingHour` WHERE `user_id`='" . $userid . "'";
$res = $db->query($sql);
}
public static function getFirst()
{
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "$where ");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new TimerecordingEmployeeWorkingHour($data);
if ($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter)
{
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "$where ORDER by `day`,`start`");
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new TimerecordingEmployeeWorkingHour($data);
}
}
return $items;
}
private static function getSqlFilter($filter)
{
$where = "1=1 ";
if (array_key_exists("user_id", $filter)) {
$userid = $filter['user_id'];
if (is_numeric($userid)) {
$where .= " AND user_id=$userid";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}