176 lines
4.7 KiB
PHP
176 lines
4.7 KiB
PHP
<?php
|
|
|
|
class TimerecordingCarModel
|
|
{
|
|
private $user_id;
|
|
private $type;
|
|
private $number_plate;
|
|
private $brand;
|
|
private $model;
|
|
private $mileage;
|
|
private $mileage_now;
|
|
private $mileage_timestamp;
|
|
private $initial_approval;
|
|
private $override_approval;
|
|
private $first_approval;
|
|
private $timerecording;
|
|
private $retired;
|
|
private $service_interval;
|
|
private $tires_at;
|
|
private $summer_tires;
|
|
private $winter_tires;
|
|
private $oil_type;
|
|
private $service_station;
|
|
private $retired_date;
|
|
private $edit_by;
|
|
|
|
|
|
public static $carTypes = [
|
|
"1" => "PKW",
|
|
"2" => "Anhänger"
|
|
];
|
|
|
|
|
|
|
|
public static function find($data)
|
|
{
|
|
|
|
}
|
|
|
|
public static function create(array $data)
|
|
{
|
|
$model = new TimerecordingCar();
|
|
|
|
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("TimerecordingCar", "*", "id=$id LIMIT 1");
|
|
if ($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new TimerecordingCar($data);
|
|
}
|
|
return $item;
|
|
}
|
|
|
|
public static function calcMileage()
|
|
{
|
|
$TimerecordingCars = self::getAll();
|
|
foreach ($TimerecordingCars as $TimerecordingCar) {
|
|
$timerecording = TimerecordingModel::search(["timerecordingCar_id" => $TimerecordingCar->id]);
|
|
if (!$timerecording) {
|
|
if ($TimerecordingCar->mileage_now != $TimerecordingCar->mileage) {
|
|
$TimerecordingCar->mileage_now = $TimerecordingCar->mileage;
|
|
$TimerecordingCar->save();
|
|
}
|
|
} else {
|
|
if (!$TimerecordingCar->mileage_now || $TimerecordingCar->mileage_now < $timerecording[0]->mileage_end) {
|
|
$TimerecordingCar->mileage_now = $timerecording[0]->mileage_end;
|
|
$TimerecordingCar->save();
|
|
} else if ($timerecording[0]->mileage_end < $TimerecordingCar->mileage) {
|
|
$TimerecordingCar->mileage_now = $TimerecordingCar->mileage;
|
|
$TimerecordingCar->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static function getAll()
|
|
{
|
|
$items = [];
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
$res = $db->select("TimerecordingCar", "*", "1=1");
|
|
if ($db->num_rows($res)) {
|
|
while ($data = $db->fetch_object($res)) {
|
|
$items[] = new TimerecordingCar($data);
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function getFirst()
|
|
{
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$res = $db->select("TimerecordingCar", "*", "$where ");
|
|
if ($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new TimerecordingCar($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("TimerecordingCar", "*", "$where");
|
|
if ($db->num_rows($res)) {
|
|
while ($data = $db->fetch_object($res)) {
|
|
$items[] = new TimerecordingCar($data);
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter)
|
|
{
|
|
$where = "1=1 ";
|
|
|
|
//var_dump($filter);exit;
|
|
if (array_key_exists("timerecording", $filter)) {
|
|
$timerecording = $filter['timerecording'];
|
|
if (is_numeric($timerecording)) {
|
|
$where .= " AND timerecording=$timerecording";
|
|
}
|
|
}
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
}
|