308 lines
11 KiB
PHP
308 lines
11 KiB
PHP
<?php
|
|
|
|
class ConstructionConsentOwnerController extends mfBaseController
|
|
{
|
|
|
|
protected function init(): void
|
|
{
|
|
$this->needlogin = true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->me = $me;
|
|
$this->layout()->set("me", $me);
|
|
|
|
if ($this->me->is("Admin")) $this->constructionConsentProjects = array_column(ConstructionConsentProject::getAll(), 'id');
|
|
else {
|
|
$constructionConsentProjects = json_decode((new WorkerFlag($this->me->id, "constructionConsent_projects"))->value() ?? '[]');
|
|
empty($constructionConsentProjects) ? $this->redirect("Dashboard") : $this->constructionConsentProjects = $constructionConsentProjects;
|
|
}
|
|
}
|
|
|
|
protected function uploadDocumentAction() {
|
|
$owner_id = $this->request->owner_id;
|
|
$owner = new ConstructionConsentOwner($owner_id);
|
|
if(!$owner->id) {
|
|
$this->layout()->setFlash("Besitzer nicht gefunden!", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$constructionConsent = new ConstructionConsent($owner->constructionconsent_id);
|
|
$filename = "ZU_KG" . $constructionConsent->kg . "_EZ" . $constructionConsent->ez . "_GST" . $constructionConsent->gst . "_" . $constructionConsent->name . "_" . $owner->lastname . ".pdf";
|
|
$_FILES['consentOwnerUpload']['name'] = $filename;
|
|
|
|
if(is_array($_FILES) && array_key_exists("consentOwnerUpload", $_FILES) && !$_FILES['consentOwnerUpload']['error']) {
|
|
try {
|
|
// returns File object or throws Exception on error
|
|
$file = mfUpload::handleFormUpload("consentOwnerUpload", false, TT_CONSTRUCTIONCONSENT_FILE_UPLOAD_SUBFOLDER);
|
|
} catch (Exception $ex) {
|
|
$this->layout()->setFlash("Fehler beim Hochladen: " . $ex->getMessage(), "warning");
|
|
return $this->editAction();
|
|
}
|
|
|
|
$ccof = ConstructionConsentOwnerFile::create([
|
|
'constructionconsentowner_id' => $owner->id,
|
|
'file_id' => $file->id,
|
|
'filename' => $filename,
|
|
]);
|
|
|
|
if(!$ccof->save()) {
|
|
$this->layout()->setFlash("Fehler beim Hochladen", "error");
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $owner->constructionconsent_id]);
|
|
}
|
|
|
|
$this->layout()->setFlash("Datei erfolgreich hochgeladen", "success");
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $owner->constructionconsent_id]);
|
|
}
|
|
|
|
$this->layout()->setFlash("Keine Datei ausgewählt", "info");
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $owner->constructionconsent_id]);
|
|
}
|
|
|
|
protected function saveAction()
|
|
{
|
|
$r = $this->request;
|
|
//var_dump($r->get());exit;
|
|
$id = $r->id;
|
|
if (is_numeric($id) && $id > 0) {
|
|
$mode = "edit";
|
|
$item = new ConstructionConsentOwner($id);
|
|
if (!$item->id) {
|
|
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
} else {
|
|
$id = false;
|
|
$mode = "add";
|
|
}
|
|
|
|
$cc_id = $r->constructionconsent_id;
|
|
$cc = new ConstructionConsent($cc_id);
|
|
if(!$cc_id || !$cc->id) {
|
|
$this->layout()->setFlash("Beim Speichern ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
if (!in_array($cc->constructionconsentproject_id, $this->constructionConsentProjects)) {
|
|
$this->layout()->setFlash("Sie sind nicht berechtigt, diese Zustimmungserklärung zu bearbeiten", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$data = [];
|
|
$data["constructionconsent_id"] = $cc_id;
|
|
$data["title"] = $r->title;
|
|
$data["firstname"] = $r->firstname;
|
|
$data["company"] = $r->company;
|
|
$data["lastname"] = $r->lastname;
|
|
$data["street"] = $r->street;
|
|
$data["zip"] = $r->zip;
|
|
$data["city"] = $r->city;
|
|
$data["country"] = $r->country;
|
|
$data["phone"] = $r->phone;
|
|
$data["phone2"] = $r->phone2;
|
|
$data["fax"] = $r->fax;
|
|
$data["email"] = $r->email;
|
|
$data["birthdate"] = null;
|
|
|
|
if($r->birthdate) {
|
|
try {
|
|
$birthdate = DateTime::createFromFormat("d.m.Y", $r->birthdate, new DateTimeZone("Europe/Vienna"));
|
|
$data["birthdate"] = $birthdate->format("Y-m-d");
|
|
} catch(Exception $e) {
|
|
$this->layout()->setFlash("Ungültiges Geburtsdateum", "warning");
|
|
}
|
|
}
|
|
|
|
if($mode == "add") {
|
|
$data["status"] = "new";
|
|
$item = ConstructionConsentOwner::create($data);
|
|
} else {
|
|
$item->update($data);
|
|
}
|
|
|
|
if(!$item->save()) {
|
|
$this->layout()->setFlash("Beim Speichern ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.", "error");
|
|
} else {
|
|
if($mode == "add") {
|
|
$journal = ConstructionConsentJournal::create([
|
|
"constructionconsent_id" => $cc->id,
|
|
"text" => "Eigentümer " . ($item->company ? $item->company : $item->firstname . " " . $item->lastname) . " wurde hinzugefügt"
|
|
]);
|
|
$journal->save();
|
|
}
|
|
|
|
$this->layout()->setFlash("Besitzer wurde erfolgreich gespeichert.", "success");
|
|
}
|
|
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $cc_id]);
|
|
|
|
}
|
|
|
|
protected function deleteAction() {
|
|
$id = $this->request->id;
|
|
|
|
$owner = new ConstructionConsentOwner($id);
|
|
if(!$owner->id) {
|
|
$this->layout()->setFlash("Besitzer nicht gefunden!", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$consent = $owner->consent;
|
|
|
|
if (!in_array($consent->constructionconsentproject_id, $this->constructionConsentProjects)) {
|
|
$this->layout()->setFlash("Sie sind nicht berechtigt, diese Zustimmungserklärung zu bearbeiten", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
foreach($owner->files as $file) {
|
|
if ($file->file) $file->file->delete();
|
|
$file->delete();
|
|
}
|
|
|
|
$journal = ConstructionConsentJournal::create([
|
|
"constructionconsent_id" => $consent->id,
|
|
"text" =>
|
|
$owner->company ?
|
|
"Eigentümer $owner->company wurde gelöscht" :
|
|
"Eigentümer $owner->firstname $owner->lastname wurde gelöscht"
|
|
]);
|
|
$journal->save();
|
|
|
|
$owner->delete();
|
|
|
|
$this->layout()->setFlash("Besitzer gelöscht!", "success");
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $consent->id]);
|
|
|
|
}
|
|
|
|
protected function deleteFileAction() {
|
|
// display errors
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
$id = $this->request->id;
|
|
|
|
$file = new ConstructionConsentOwnerFile($id);
|
|
if(!$file->id) {
|
|
$this->layout()->setFlash("Datei nicht gefunden!", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$owner = new ConstructionConsentOwner($file->constructionconsentowner_id);
|
|
if(!$owner->id) {
|
|
$this->layout()->setFlash("Besitzer nicht gefunden!", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$consent = $owner->consent;
|
|
if(!$consent->id) {
|
|
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden!", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$project = new ConstructionConsentProject($consent->constructionconsentproject_id);
|
|
|
|
if(!$project->id) {
|
|
$this->layout()->setFlash("Zustimmungserklärungsprojekt nicht gefunden!", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
if (!in_array($project->id, $this->constructionConsentProjects)) {
|
|
$this->layout()->setFlash("Sie sind nicht berechtigt, diese Zustimmungserklärung zu bearbeiten", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$file->file->delete();
|
|
$file->delete();
|
|
|
|
$this->layout()->setFlash("Datei gelöscht!", "success");
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $owner->constructionconsent_id]);
|
|
}
|
|
|
|
protected function apiAction() {
|
|
$do = $this->request->do;
|
|
$data = [];
|
|
|
|
switch($do) {
|
|
case "updateStatus":
|
|
$return = $this->updateStatusApi();
|
|
break;
|
|
case "updateResult":
|
|
$return = $this->updateResultApi();
|
|
break;
|
|
default:
|
|
$this->log->warn(__METHOD__ . ": Called API function '$do' does not exist");
|
|
$return = false;
|
|
}
|
|
|
|
if(!is_array($return) || !count($return)) {
|
|
$data = ["status" => "error"];
|
|
$this->returnJson($data);
|
|
}
|
|
$data['status'] = "OK";
|
|
$data['result'] = $return;
|
|
$this->returnJson($data);
|
|
}
|
|
|
|
private function updateStatusApi() {
|
|
$owner_id = trim($this->request->id);
|
|
$new_status = trim($this->request->status);
|
|
|
|
$owner = new ConstructionConsentOwner($owner_id);
|
|
if(!$owner->id) {
|
|
return false;
|
|
}
|
|
|
|
if(!in_array($new_status, ['new', 'sent', 'returned', 'outstanding'])) {
|
|
return false;
|
|
}
|
|
|
|
$owner->status = $new_status;
|
|
if(!$owner->save()) {
|
|
return false;
|
|
}
|
|
|
|
return ["message" => "Status saved successfully", "update" => ["id" => $owner->id, "status" => $owner->status, "status_text" => __($owner->status, "consent")]];
|
|
}
|
|
|
|
private function updateResultApi() {
|
|
$owner_id = trim($this->request->id);
|
|
$new_result = trim($this->request->result);
|
|
|
|
$owner = new ConstructionConsentOwner($owner_id);
|
|
if(!$owner->id) {
|
|
return false;
|
|
}
|
|
|
|
if(!in_array($new_result, ['open', 'accepted', 'denied', 'unresolvable', 'moved'])) {
|
|
return false;
|
|
}
|
|
|
|
$owner->result = $new_result;
|
|
if(!$owner->save()) {
|
|
return false;
|
|
}
|
|
|
|
return ["message" => "Result saved successfully", "update" => ["id" => $owner->id, "result" => $owner->result, "result_text" => __($owner->result, "consent")]];
|
|
|
|
}
|
|
|
|
protected function searchOwnerAction() {
|
|
$search = $this->request->search;
|
|
|
|
$results = PreorderModel::search(['add-where' => " AND firstname LIKE '%$search%' OR lastname LIKE '%$search%'"]);
|
|
self::returnJson(array_map(function($result) {
|
|
return [
|
|
'id' => $result->id,
|
|
'firstname' => $result->firstname,
|
|
'lastname' => $result->lastname,
|
|
'street' => $result->street,
|
|
'zip' => $result->zip,
|
|
'city' => $result->city,
|
|
'phone' => $result->phone,
|
|
'email' => $result->email,
|
|
'text' => $result->firstname . " " . $result->lastname . " (" . $result->street . ", " . $result->zip . " " . $result->city . ") [" . $result->phone . " | " . $result->email . "]"
|
|
];
|
|
}, $results));
|
|
}
|
|
}
|