Files
thetool/application/Timerecording/TimerecordingModel.php
Spitzer Daniel ee5ab065d8 Zeiterfassung Update
* Neue migration für Homeoffice
 * Soll zeiten nun vom Startdatum abhängig
 * Neue Summierung der Sollzeiten in der Personalverwaltung
 * Urlaubstage werden nun Tage statt Stunden angezeigt (in Buchungen und Auswertungen)
 * Urlaube werden nun bis 1.1.2024 berücksichtigt
 * Homeoffice Flag nun möglich
2024-03-04 13:57:44 +01:00

187 lines
5.6 KiB
PHP

<?php
class TimerecordingModel
{
private $user_id;
private $start;
private $end;
private $timerecordingCategory_id;
private $businesstrip;
private $businesstrip_info;
private $homeoffice;
private $comment;
private $approved;
private $completed;
public static function find($data)
{
}
public static function create(array $data)
{
$model = new Timerecording();
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("Timerecording", "*", "id=$id LIMIT 1");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Timerecording($data);
}
return $item;
}
public static function getAll()
{
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Timerecording", "*", "1=1");
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new Timerecording($data);
}
}
return $items;
}
public static function getAllPermits()
{
$items = [];
$db = FronkDB::singleton();
$sql = "SELECT Timerecording.* FROM `Timerecording`
INNER JOIN `TimerecordingCategory` ON (`Timerecording`.`timerecordingCategory_id` = `TimerecordingCategory`.`id`)
WHERE `TimerecordingCategory`.`approval`='1'";
$res = $db->query($sql);
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new Timerecording($data);
}
}
return $items;
}
public static function getFirst()
{
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Timerecording", "*", "$where ");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Timerecording($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("Timerecording", "*", "$where ");
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new Timerecording($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";
}
}
if (array_key_exists("start", $filter) && array_key_exists("end", $filter)) {
$start = $filter['start'];
$end = $filter['end'];
if (is_numeric($start) && is_numeric($end)) {
$where .= " AND ((`start` >= $start AND `start` <= $end) OR (`end` >= $start AND `end` <= $end) OR `end` is NULL) ORDER by user_id ASC";
}
}
if (array_key_exists("start", $filter) && array_key_exists("timerecordingCategory_id", $filter)) {
$start = $filter['start'];
$timerecordingCategory_id = $filter['timerecordingCategory_id'];
if (is_numeric($start) && is_numeric($timerecordingCategory_id)) {
$where .= " AND `start` >= $start AND `timerecordingCategory_id` = $timerecordingCategory_id ORDER by start ASC";
}
}
if (array_key_exists("starttime", $filter) && array_key_exists("endtime", $filter)) {
$starttime = $filter['starttime'];
$endtime = $filter['endtime'];
$id = $filter['id'];
if (is_numeric($starttime) && is_numeric($endtime)) {
$where .= " AND (((`start` <= $starttime AND `end` > $starttime ) OR (`start` > $endtime AND `end` < $endtime) OR (`start` > $starttime AND `end` > $starttime AND `start` <= $endtime AND `end` < $endtime) OR (`start` > $starttime AND `end` > $starttime AND `start` < $endtime AND `end` > $endtime) OR (`start` = $starttime AND `end` = $endtime )) OR ( `start` <= $starttime AND `end` IS NULL)) ORDER by user_id ASC";
if ($id && is_numeric($id)) {
$where .= " AND `id` != $id";
}
//var_dump($where);exit;
}
}
if (array_key_exists("type", $filter)) {
$type = $filter['type'];
if ($type === "opentimerecording") {
$where .= " AND `end` IS NULL ORDER by start DESC LIMIT 1";
}
}
//
// var_dump($filter, $where);exit;
return $where;
}
}