Files
thetool/application/TimerecordingCar/TimerecordingCarController.php
Daniel Spitzer 93987b4341 Zeiterfassung Fahrzeugverwaltung neue Features:
* KM-Stand History
* Journal
* Neue Felder
2025-03-13 21:27:48 +01:00

377 lines
13 KiB
PHP

<?php
class TimerecordingCarController extends mfBaseController
{
protected function init()
{
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if (!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function apiAction()
{
$do = $this->request->do;
switch ($do) {
case "uploadDocuments":
$return = $this->uploadDocument();
break;
case "getDocuments":
$return = $this->getDocuments();
break;
case "deleteDocument":
$return = $this->deleteDocument();
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 indexAction()
{
$this->layout()->setTemplate("TimerecordingCar/Index");
$timerecordingcars = TimerecordingCarModel::getAll();
$this->layout()->set("timerecordingcars", $timerecordingcars);
}
protected function detailAction()
{
$timerecordingcarid = $this->request->id;
$timeRecordingCarMileageHistory = TimerecordingCarMileageHistoryModel::search(["timerecordingCar_id" => $timerecordingcarid]);
$timerecordingCarJournal=TimerecordingCarJournalModel::search(["timerecordingCar_id" => $timerecordingcarid]);
$mimetypes = TimerecordingCarDocumentsModel::$mimetypes;
$timerecordingcarDokuments = TimerecordingCarDocumentsModel::search(["timerecordingCar_id" => $timerecordingcarid]);
$this->layout()->set("mimetypes", $mimetypes);
$this->layout()->set("timerecordingcarDokuments", $timerecordingcarDokuments);
$this->layout()->set("timeRecordingCarMileageHistory", $timeRecordingCarMileageHistory);
$this->layout()->set("timerecordingCarJournal", $timerecordingCarJournal);
$this->layout()->setTemplate("TimerecordingCar/Detail");
$timerecordingcar = TimerecordingCarModel::getOne($timerecordingcarid);
$this->layout()->set("timerecordingcar", $timerecordingcar);
$timerecordings = TimerecordingModel::search(["timerecordingCar_id_all" => $timerecordingcarid]);
$this->layout()->set("timerecordings", $timerecordings);
}
protected function addAction()
{
$timerecordingusers = UserModel::search(['employee' => 'true']);
$this->layout()->set("timerecordingusers", $timerecordingusers);
$this->layout()->setTemplate("TimerecordingCar/Form");
}
protected function editAction()
{
$id = $this->request->id;
if (!is_numeric($id) || !$id) {
$this->layout()->setFlash("Auto nicht gefunden", "error");
$this->redirect("TimerecordingCar");
}
$timerecordingcars = new TimerecordingCar($id);
$mimetypes = TimerecordingCarDocumentsModel::$mimetypes;
$timerecordingcarDokuments = TimerecordingCarDocumentsModel::search(["timerecordingCar_id" => $id]);
if ($timerecordingcars->id != $id) {
$this->layout()->setFlash("Auto nicht gefunden", "error");
$this->redirect("TimerecordingCar");
}
$this->layout()->set("mimetypes", $mimetypes);
$this->layout()->set("timerecordingcarDokuments", $timerecordingcarDokuments);
$this->layout()->set("timerecordingcars", $timerecordingcars);
return $this->addAction();
}
protected function retireAction()
{
$r = $this->request;
$id = $r->id;
$retired_date = $r->retired_date;
if (is_numeric($id) && $id > 0) {
$timerecordingcars = new TimerecordingCar($id);
if (!$timerecordingcars->id) {
$this->layout()->setFlash("Fahrzeug nicht gefunden", "error");
$this->redirect("TimerecordingCar");
}
} else {
$this->layout()->setFlash("Fahrzeug nicht gefunden", "error");
$this->redirect("TimerecordingCar");
}
$data = [];
$data['retired'] = 1;
$data['retired_date'] = strtotime($retired_date);
$data['edit_by'] = $this->me->id;
$timerecordingcars->update($data);
$timerecordingcars->save();
$this->layout()->setFlash("Fahrzeug erfolgreich ausgeschieden", "success");
$this->redirect("TimerecordingCar");
}
protected function saveAction()
{
$r = $this->request;
$journal = trim($r->journal);
$id = $r->id;
//var_dump($r->get());exit;
if (is_numeric($id) && $id > 0) {
$mode = "edit";
$timerecordingcars = new TimerecordingCar($id);
if (!$timerecordingcars->id) {
$this->layout()->setFlash("Autos nicht gefunden", "error");
$this->redirect("TimerecordingCar");
}
} else {
$mode = "add";
}
$data = [];
$data['number_plate'] = trim($r->number_plate);
$data['user_id'] = trim($r->user_id);
$data['service_interval'] = trim($r->service_interval);
$data['tires_at'] = trim($r->tires_at);
$data['summer_tires'] = trim($r->summer_tires);
$data['winter_tires'] = trim($r->winter_tires);
$data['oil_type'] = trim($r->oil_type);
$data['service_station'] = trim($r->service_station);
$data['type'] = trim($r->type);
$data['brand'] = trim($r->brand);
$data['model'] = trim($r->model);
$data['mileage'] = trim($r->mileage);
$data['initial_approval'] = strtotime($r->initial_approval);
$data['timerecording'] = $r->timerecording;
$data['first_approval'] = trim($r->first_approval);
$data['override_approval'] = strtotime(trim($r->override_approval));
$data['edit_by'] = $this->me->id;
if (!$data['user_id'] || $data['user_id'] == "-") {
$data['user_id'] = null;
}
if (!$data['service_interval']) {
$data['service_interval'] = null;
}
if (!$data['tires_at']) {
$data['tires_at'] = null;
}
if (!$data['summer_tires']) {
$data['summer_tires'] = null;
}
if (!$data['winter_tires']) {
$data['winter_tires'] = null;
}
if (!$data['oil_type']) {
$data['oil_type'] = null;
}
if (!$data['service_station']) {
$data['service_station'] = null;
}
if (!$data['initial_approval']) {
$data['initial_approval'] = null;
}
if (!$data['override_approval']) {
$data['override_approval'] = null;
}
if (!$data['timerecording']) {
$data['timerecording'] = 0;
}
if (!$data['mileage']) {
$data['mileage'] = null;
}
if (!$data['first_approval'] || trim($data['first_approval']) == "") {
$data['first_approval'] = null;
}
if (!$data['number_plate']) {
$this->layout()->setFlash("Kennzeichen darf nicht leer sein", "error");
$this->redirect("TimerecordingCar");
}
if (!$data['brand']) {
$this->layout()->setFlash("Marke darf nicht leer sein", "error");
$this->redirect("TimerecordingCar");
}
if (!$data['model']) {
$this->layout()->setFlash("Model/Typ darf nicht leer sein", "error");
$this->redirect("TimerecordingCar");
}
// var_dump($_FILES);
// var_dump($upload);
// exit;
if ($mode == "edit") {
if ($timerecordingcars->mileage_now != $data['mileage']) {
$mileage_timestamp = $r->mileage_timestamp;
if (!$mileage_timestamp) {
$data['mileage_timestamp'] = time();
} else {
$data['mileage_timestamp'] = strtotime($mileage_timestamp);
}
$datamileageHistory = [];
$datamileageHistory['timerecordingCar_id'] = $timerecordingcars->id;
$datamileageHistory['mileage'] = $timerecordingcars->mileage_now;
if ($timerecordingcars->mileage_timestamp) {
$datamileageHistory['mileage_timestamp'] = $timerecordingcars->mileage_timestamp;
} else {
$datamileageHistory['mileage_timestamp'] = $timerecordingcars->edit;
}
$timerecordingcarsMilageHistory = TimerecordingCarMileageHistoryModel::create($datamileageHistory);
$timerecordingcarsMilageHistory->save();
}
$timerecordingcars->update($data);
} else {
if ($data['mileage'] != null) {
$data['mileage_timestamp'] = time();
}
$timerecordingcars = TimerecordingCarModel::create($data);
}
// var_dump($filestore);
// exit;
$id = $timerecordingcars->save();
if ($journal) {
$journal_timestamp = $r->journal_timestamp;
$dataJournal = [];
$dataJournal['timerecordingCar_id'] = $id;
$dataJournal['journal'] = $journal;
if ($journal_timestamp) {
$dataJournal['timestamp'] = strtotime($journal_timestamp);
} else {
$dataJournal['timestamp'] = time();
}
$timerecordingcarJournal = TimerecordingCarJournalModel::create($dataJournal);
$timerecordingcarJournal->save();
}
$returnUrl = "TimerecordingCar";
$returnAction = "Index";
$returnVariables = array();
$returnAnker = "";
if ($this->request->returnto == "detail") {
$returnUrl = "TimerecordingCar";
$returnAction = "detail";
$returnVariables = array("id" => $id);
if ($this->request->hash) {
$returnVariables['hash'] = $this->request->hash;
}
} else {
if ($this->request->hash) {
$returnAnker = $this->request->hash;
}
}
TimerecordingCarModel::calcMileage();
if (!$id) {
$this->layout()->setFlash("Auto konnte nicht angelegt werden", "error");
$this->redirect("TimerecordingCar");
}
if ($mode == "edit") {
$this->layout()->setFlash("Auto erfolgreich geändert", "success");
} else if ($mode = "add") {
$this->layout()->setFlash("Auto erfolgreich angelegt", "success");
}
$this->redirect($returnUrl, $returnAction, $returnVariables, $returnAnker);
}
protected function deleteAction()
{
$id = $this->request->id;
$timerecordingcars = new TimerecordingCar($id);
if (!$timerecordingcars->id || $timerecordingcars->id != $id) {
$this->layout()->setFlash("Auto nicht gefunden.", "error");
$this->redirect("TimerecordingCar");
}
$timerecordingcars->delete();
$this->redirect("TimerecordingCar");
}
protected function uploadDocument()
{
$r = $this->request;
if (array_key_exists("timeRecordingCar", $_FILES) && !$_FILES['timeRecordingCar']['error']) {
$upload_error = false;
$upload = new mfUpload("timeRecordingCar");
$upload->setSavepath(MFUPLOAD_FILE_SAVE_PATH . "/timeRecordingCar");
if (!$upload->getSize()) {
die();
}
$upload->save();
$file_data = [];
$file_data['name'] = $upload->getOriginalFilename();
$file_data['filename'] = ($r->file_filename) ? $r->file_filename : $upload->getOriginalFilename();
$file_data['subfolder'] = "timeRecordingCar";
$file_data['store_filename'] = $upload->getFilename();
$file_data['orig_filename'] = $upload->getOriginalFilename();
$file = FileModel::create($file_data);
$file_id = $file->save();
if (!$file_id) {
die();
} else {
$data = [];
$data['file_id'] = $file_id;
$data['file_size'] = $upload->getSize();
$data['timerecordingCar_id'] = $r->timerecordingCar_id;
$data['name'] = $upload->getOriginalFilename();
$timerecordingcardocumentss = TimerecordingCarDocumentsModel::create($data);
$id = $timerecordingcardocumentss->save();
}
$result['success'] = true;
$result['data']['file_id'] = $file_id;
$result['data']['id'] = $id;
} else {
echo "error";
}
echo json_encode($result);
die();
}
protected function getDocuments()
{
$r = $this->request;
$file_name = $r->file_name;
$timerecordingCarDokument = TimerecordingCarDocumentsModel::search(["file_name" => $file_name]);
return $timerecordingCarDokument->file_id;
}
protected function deleteDocument()
{
$r = $this->request;
$id = $r->id;
$timerecordingCarDokument = new TimerecordingCarDocuments($id);
$timerecordingCarDokument->file->delete();
$timerecordingCarDokument->delete();
}
}