Files
thetool/application/TimerecordingCar/TimerecordingCarController.php
Daniel Spitzer 7d176779c9 Zeiterfassung neues Feature:
Features für Project 7832:
* Das Pickerldatum Zusatzfeld
* Auftrennen PKW und Anhänger
* Dokumente Upload
* Standardsortierung
* Ausgeschieden Flag mit Datum
* zusätzliche migration
2025-03-10 08:04:56 +01:00

306 lines
10 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;
$mimetypes = TimerecordingCarDocumentsModel::$mimetypes;
$timerecordingcarDokuments = TimerecordingCarDocumentsModel::search(["timerecordingCar_id" => $timerecordingcarid]);
$this->layout()->set("mimetypes", $mimetypes);
$this->layout()->set("timerecordingcarDokuments", $timerecordingcarDokuments);
$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;
$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['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['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") {
$timerecordingcars->update($data);
} else {
$timerecordingcars = TimerecordingCarModel::create($data);
}
// var_dump($filestore);
// exit;
$id = $timerecordingcars->save();
$returnUrl = "TimerecordingCar";
$returnAction = "Index";
$returnVariables = array();
$returnAnker = "";
if ($this->request->returnto == "detail") {
$returnUrl = "TimerecordingCar";
$returnAction = "detail";
$returnVariables = array("id" => $id);
}
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();
}
}