Files
thetool/application/Timerecording/TimerecordingModel.php
Daniel Spitzer 085d8facdc Zeiterfassung
Neue Features:
* BP Stunden Aufbuchen/Mindern
* Spezialbuchungen ohne Verechnungstechnische relevanz
2024-12-29 20:24:36 +01:00

222 lines
7.0 KiB
PHP

<?php
class TimerecordingModel
{
private $user_id;
private $start;
private $end;
private $hours;
private $hours_overtime;
private $hours_bpa;
private $days;
private $timerecordingCategory_id;
private $businesstrip;
private $businesstrip_info;
private $timerecordingCar_id;
private $mileage_start;
private $mileage_end;
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,start 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("start", $filter) && array_key_exists("hours", $filter)) {
$start = $filter['start'];
if (is_numeric($start)) {
$where .= " AND `start` >= $start AND (`hours`>0 or `hours_overtime`>0) ORDER by start ASC";
}
}
if (array_key_exists("start", $filter) && array_key_exists("days", $filter)) {
$days = $filter['days'];
$start = $filter['start'];
if ($days === 1) {
$where .= " AND `start` >= $start AND `days` !=0 ORDER by start ASC";
}
}
if (array_key_exists("timerecordingCar_id", $filter)) {
$timerecordingCar = $filter['timerecordingCar_id'];
if (is_numeric($timerecordingCar)) {
$where .= " AND `timerecordingCar_id` = $timerecordingCar ORDER by mileage_end DESC LIMIT 1";
}
}
if (array_key_exists("timerecordingCar_id_all", $filter)) {
$timerecordingCar = $filter['timerecordingCar_id_all'];
if (is_numeric($timerecordingCar)) {
$where .= " AND `timerecordingCar_id` = $timerecordingCar ORDER by mileage_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)) {
if ($id && is_numeric($id)) {
$where .= " AND `id` != $id ";
}
$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)) AND `days` = 0 ORDER by user_id,start ASC";
//echo ($where);die();
}
}
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;
}
}