331 lines
14 KiB
PHP
331 lines
14 KiB
PHP
<?php
|
|
// RMLWorkorderCompanyController.php
|
|
|
|
class RMLWorkorderCompanyController extends TTCrud
|
|
{
|
|
protected string $headerTitle = 'Meine Arbeitsaufträge';
|
|
protected bool $createText = false;
|
|
|
|
protected array $columns = [
|
|
['key' => 'id', 'text' => 'Auftrag-Nr.', 'table' => ['sortable' => true]],
|
|
['key' => 'preorderInfo', 'text' => 'Kunde / Projekt', 'modal' => false, 'table' => ['sortable' => false, 'filter' => 'search']],
|
|
['key' => 'status', 'text' => 'Status', 'modal' => false, 'table' => ['filter' => 'iconSelect', 'filterOptions' => [
|
|
['value' => 'new', 'text' => 'Neu', 'icon' => 'fas fa-star text-primary'],
|
|
['value' => 'assigned', 'text' => 'Zugewiesen', 'icon' => 'fas fa-user-check text-info'],
|
|
['value' => 'scheduled', 'text' => 'Terminiert', 'icon' => 'fas fa-calendar-check text-warning'],
|
|
['value' => 'correction_requested', 'text' => 'Korrektur angefordert', 'icon' => 'fas fa-exclamation-triangle text-danger'],
|
|
['value' => 'documented', 'text' => 'Dokumentiert', 'icon' => 'fas fa-file-alt text-success'],
|
|
['value' => 'completed', 'text' => 'Abgeschlossen', 'icon' => 'fas fa-check-double text-secondary'],
|
|
]]],
|
|
['key' => 'deadlineDate', 'text' => 'Deadline', 'modal' => false, 'table' => ['filter' => 'date']],
|
|
['key' => 'appointmentDate', 'text' => 'Termin', 'modal' => false, 'table' => ['filter' => 'date']],
|
|
];
|
|
|
|
protected array $additionalJSVariables = ['COMPANY_ID' => '0'];
|
|
|
|
protected function prepareCrudConfig() {
|
|
$company = RMLWorkorderCompanyModel::getFirst(['addressId' => $this->user->address_id]);
|
|
if ($company) {
|
|
$this->additionalJSVariables['COMPANY_ID'] = $company->id;
|
|
} else {
|
|
$this->sendError('Access Denied. You are not associated with a registered RML company.', 403);
|
|
}
|
|
}
|
|
|
|
protected function indexAction()
|
|
{
|
|
Helper::renderVue($this, 'RMLWorkorderCompany', $this->headerTitle, [
|
|
"CRUD_CONFIG" => $this->getCrudConfig(),
|
|
"TABLE_URL" => $this::getUrl("RMLWorkorderCompany/get"),
|
|
"COMPANY_ID" => $this->additionalJSVariables['COMPANY_ID'],
|
|
]);
|
|
}
|
|
|
|
protected function getAction()
|
|
{
|
|
$json = json_decode(file_get_contents('php://input'), true);
|
|
$pagination = $json['pagination'] ?? ['page' => 1, 'per_page' => 10];
|
|
$filters = $json['filters'] ?? [];
|
|
$order = $json['order'] ?? ['key' => 'deadlineDate', 'order' => 'ASC'];
|
|
|
|
$company = RMLWorkorderCompanyModel::getFirst(['addressId' => $this->user->address_id]);
|
|
if (!$company) {
|
|
self::sendError("Company not found for user.", 403);
|
|
}
|
|
|
|
// Get paginated workorders and total count from the new model methods
|
|
$workorders = RMLWorkorderModel::getCompanyWorkorders($filters, $pagination['per_page'], ($pagination['page'] - 1) * $pagination['per_page'], $order, $company->id);
|
|
$totalCount = RMLWorkorderModel::countCompanyWorkorders($filters, $company->id);
|
|
|
|
// Format rows for the frontend
|
|
$rows = array_map(function($workorder) {
|
|
$row = (array)$workorder;
|
|
|
|
$anschlussadresse = "{$row['street']} {$row['hausnummer']}";
|
|
if ($row['stiege']) $anschlussadresse .= "/{$row['stiege']}";
|
|
if ($row['apartment']) $anschlussadresse .= " / WE: {$row['apartment']}";
|
|
$anschlussadresse .= ", {$row['plz']} {$row['city']}";
|
|
|
|
$kunde = $row['customerCompany'] ?: $row['customerName'];
|
|
|
|
$row['preorderInfo'] = "<strong>Kunde:</strong> {$kunde}<br>" .
|
|
"<strong>Anschluss:</strong> {$anschlussadresse}<br>" .
|
|
"<strong>Kontakt:</strong> {$row['phone']} / {$row['email']}<br>" .
|
|
"<strong>OAID:</strong> <span class='text-pink'>{$row['oaid']}</span>";
|
|
|
|
// Clean up unnecessary fields before sending
|
|
unset($row['customerName'], $row['customerCompany'], $row['street'], $row['hausnummer'], $row['stiege'], $row['oaid'], $row['apartment'], $row['plz'], $row['city'], $row['phone'], $row['email']);
|
|
|
|
return $row;
|
|
}, $workorders);
|
|
|
|
self::returnJson([
|
|
'rows' => $rows,
|
|
'pagination' => [
|
|
'page' => $pagination['page'],
|
|
'per_page' => $pagination['per_page'],
|
|
'total_rows' => $totalCount,
|
|
'total_pages' => ceil($totalCount / $pagination['per_page']),
|
|
'filtered_available' => $totalCount
|
|
]
|
|
]);
|
|
}
|
|
public function getWorkorderByIdAction() {
|
|
$id = $this->request->id;
|
|
if(!$id) self::sendError("ID missing");
|
|
|
|
$workorder = RMLWorkorderModel::get($id);
|
|
if(!$workorder) self::sendError("Workorder not found");
|
|
|
|
$workorder->preorderInfo = $this->getPreorderInfoText($workorder->preorderId);
|
|
|
|
self::returnJson((array) $workorder);
|
|
}
|
|
|
|
private function getPreorderInfoText($preorderId) {
|
|
$preorder = new Preorder($preorderId);
|
|
$anschlussadresse = 'N/A';
|
|
if ($preorder->adb_hausnummer_id) {
|
|
$hn = $preorder->adb_hausnummer;
|
|
$anschlussadresse = "{$hn->strasse->name} {$hn->hausnummer}";
|
|
if ($hn->stiege) $anschlussadresse .= "/{$hn->stiege}";
|
|
if ($preorder->adb_wohneinheit_id) $anschlussadresse .= " / WE: {$preorder->adb_wohneinheit->bezeichner}";
|
|
$anschlussadresse .= ", {$hn->plz->plz} {$hn->ortschaft->name}";
|
|
}
|
|
|
|
$kunde = ($preorder->company) ?: "{$preorder->firstname} {$preorder->lastname}";
|
|
|
|
return "<strong>Kunde:</strong> {$kunde}<br>" .
|
|
"<strong>Anschluss:</strong> {$anschlussadresse}<br>" .
|
|
"<strong>Kontakt:</strong> {$preorder->phone} / {$preorder->email}<br>" .
|
|
"<strong>OAID:</strong> <span class='text-pink'>{$preorder->oaid}</span>";
|
|
}
|
|
|
|
protected function scheduleAppointmentAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['workorderId']) || empty($post['appointmentDate'])) self::sendError("Required fields are missing.");
|
|
|
|
$workorder = RMLWorkorderModel::get($post['workorderId']);
|
|
if(!$workorder) self::sendError("Workorder not found");
|
|
|
|
$workorder->appointmentDate = $post['appointmentDate'];
|
|
$workorder->status = 'scheduled';
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Termin erfolgreich gespeichert.']);
|
|
}
|
|
|
|
protected function uploadDocumentationAction()
|
|
{
|
|
if (empty($_FILES['files']) || empty($_POST['workorderId'])) {
|
|
self::returnJson(['error' => 'Required data is missing.']);
|
|
return;
|
|
}
|
|
|
|
$workorderId = $_POST['workorderId'];
|
|
$description = $_POST['description'] ?? '';
|
|
$documentType = $_POST['documentType'] ?? 'general';
|
|
$files = $_FILES['files'];
|
|
$uploadCount = 0;
|
|
|
|
foreach ($files['name'] as $index => $name) {
|
|
if ($files['error'][$index] === UPLOAD_ERR_OK) {
|
|
$_FILES['file'] = [
|
|
'name' => $files['name'][$index],
|
|
'type' => $files['type'][$index],
|
|
'tmp_name' => $files['tmp_name'][$index],
|
|
'error' => $files['error'][$index],
|
|
'size' => $files['size'][$index]
|
|
];
|
|
|
|
try {
|
|
$uploaded = mfUpload::handleFormUpload("file", false, "/RMLWorkorder");
|
|
RMLWorkorderDocumentationModel::create([
|
|
'workorderId' => $workorderId,
|
|
'fileId' => $uploaded->id,
|
|
'description' => $description,
|
|
'documentType' => $documentType,
|
|
'create' => time(),
|
|
'createBy' => $this->user->id
|
|
]);
|
|
$uploadCount++;
|
|
} catch (Exception $e) {
|
|
error_log("File upload failed for $name: " . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
$workorder = RMLWorkorderModel::get($workorderId);
|
|
if ($workorder->status === 'correction_requested') {
|
|
$workorder->status = 'assigned';
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
$workorder = RMLWorkorderModel::get($workorderId);
|
|
}
|
|
|
|
$formattedDocs = $this->getFormattedDocs($workorderId);
|
|
|
|
self::returnJson([
|
|
'success' => true,
|
|
'message' => "$uploadCount Datei(en) erfolgreich hochgeladen.",
|
|
'docs' => $formattedDocs,
|
|
'workorder' => (array)$workorder
|
|
]);
|
|
}
|
|
|
|
private function getFormattedDocs($workorderId) {
|
|
$docs = RMLWorkorderDocumentationModel::getAll(['workorderId' => $workorderId], null, 0, ['key' => 'create', 'order' => 'ASC']);
|
|
$responseDocs = [];
|
|
$typeCounts = [];
|
|
|
|
$translationMap = [
|
|
'photo_before' => 'Foto vorher',
|
|
'photo_during' => 'Foto währenddessen',
|
|
'photo_after' => 'Foto nachher',
|
|
'measurement_protocol' => 'Messprotokoll',
|
|
'customer_signature' => 'Kundenunterschrift',
|
|
];
|
|
|
|
foreach($docs as $doc) {
|
|
$file = new File($doc->fileId);
|
|
$documentTypeKey = $doc->documentType;
|
|
if (!isset($typeCounts[$documentTypeKey])) {
|
|
$typeCounts[$documentTypeKey] = 1;
|
|
} else {
|
|
$typeCounts[$documentTypeKey]++;
|
|
}
|
|
|
|
$originalFilename = $file->orig_filename ?? $file->filename;
|
|
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
|
|
$translatedType = $translationMap[$documentTypeKey] ?? $documentTypeKey;
|
|
$newFilename = "{$translatedType}_{$typeCounts[$documentTypeKey]}." . strtolower($extension);
|
|
|
|
$responseDocs[] = [
|
|
'id' => $doc->id,
|
|
'fileId' => $doc->fileId,
|
|
'fileName' => $newFilename,
|
|
'documentType' => $documentTypeKey,
|
|
'mimetype' => $file->mimetype,
|
|
];
|
|
}
|
|
return $responseDocs;
|
|
}
|
|
|
|
|
|
protected function getDocumentationAction() {
|
|
if(empty($this->request->workorderId)) self::sendError("Workorder ID missing.");
|
|
|
|
$docs = $this->getFormattedDocs($this->request->workorderId);
|
|
|
|
$journals = array_map(
|
|
function ($j) {
|
|
$j->createByName = UserModel::getOne($j->createBy)->getAbbrName();
|
|
return (array)$j;
|
|
},
|
|
RMLWorkorderJournalModel::getAll(['workorderId' => $this->request->workorderId], null, 0, ['key' => 'create', 'order' => 'DESC'])
|
|
);
|
|
|
|
self::returnJson(['docs' => $docs, 'journals' => $journals]);
|
|
}
|
|
protected function completeWorkorderAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if(empty($post['workorderId'])) self::sendError("Workorder ID missing.");
|
|
|
|
$workorder = RMLWorkorderModel::get($post['workorderId']);
|
|
if(!$workorder) self::sendError("Workorder not found.");
|
|
|
|
$workorder->status = 'documented';
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Auftrag abgeschlossen.']);
|
|
}
|
|
|
|
protected function deleteDocumentationAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['id'])) self::sendError("Document ID missing.");
|
|
|
|
$doc = RMLWorkorderDocumentationModel::get($post['id']);
|
|
if (!$doc) self::sendError("Document not found.");
|
|
$workorderId = $doc->workorderId;
|
|
|
|
RMLWorkorderDocumentationModel::delete($post['id']);
|
|
|
|
$formattedDocs = $this->getFormattedDocs($workorderId);
|
|
|
|
self::returnJson([
|
|
'success' => true,
|
|
'message' => 'Dokument gelöscht.',
|
|
'docs' => $formattedDocs
|
|
]);
|
|
}
|
|
|
|
protected function updateDocumentationAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['id'])) self::sendError("Document ID missing.");
|
|
|
|
$doc = RMLWorkorderDocumentationModel::get($post['id']);
|
|
if (!$doc) self::sendError("Dokument nicht gefunden.");
|
|
|
|
if (isset($post['documentType'])) {
|
|
$doc->documentType = $post['documentType'];
|
|
}
|
|
|
|
RMLWorkorderDocumentationModel::update((array)$doc);
|
|
|
|
$formattedDocs = $this->getFormattedDocs($doc->workorderId);
|
|
|
|
self::returnJson([
|
|
'success' => true,
|
|
'message' => 'Dokument aktualisiert.',
|
|
'docs' => $formattedDocs
|
|
]);
|
|
}
|
|
|
|
protected function addJournalAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (empty($post['workorderId']) || empty(trim($post['text']))) {
|
|
self::sendError("Workorder ID and text are required.");
|
|
}
|
|
|
|
RMLWorkorderJournalModel::create([
|
|
'workorderId' => $post['workorderId'],
|
|
'text' => $post['text'],
|
|
'createBy' => $this->user->id,
|
|
'create' => time(),
|
|
]);
|
|
|
|
$journals = array_map(
|
|
function ($j) {
|
|
$j->createByName = UserModel::getOne($j->createBy)->getAbbrName();
|
|
return (array)$j;
|
|
},
|
|
RMLWorkorderJournalModel::getAll(['workorderId' => $post['workorderId']], null, 0, ['key' => 'create', 'order' => 'DESC'])
|
|
);
|
|
|
|
self::returnJson([
|
|
'success' => true,
|
|
'message' => 'Journal-Eintrag hinzugefügt.',
|
|
'journals' => $journals
|
|
]);
|
|
}
|
|
} |