Files
thetool/application/RMLWorkorderCompany/RMLWorkorderCompanyController.php
2025-07-23 20:44:25 +02:00

244 lines
10 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' => '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']],
['key' => 'actions', 'text' => 'Aktionen', 'modal' => false, 'table' => ['filter' => false, 'sortable' => false]],
];
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' => 'id', 'order' => 'DESC'];
$company = RMLWorkorderCompanyModel::getFirst(['addressId' => $this->user->address_id]);
if(!$company) self::sendError("Company not found for user.", 403);
$filters['companyId'] = $company->id;
if (!empty($filters['preorderInfo'])) {
$searchTerm = $filters['preorderInfo'];
//todo: fix this preordermodel search shit
$preorders = PreorderModel::getAll(['firstname|lastname|company|oaid' => $searchTerm]);
$preorderIds = array_map(fn($p) => $p->id, $preorders);
if (!empty($preorderIds)) {
$filters['preorderId'] = $preorderIds;
} else {
$filters['id'] = -1;
}
}
unset($filters['preorderInfo']);
// only show workorders that are assigned to the company and have the status assigned or scheduled
$filters['status'] = ['assigned', 'scheduled'];
$filters['companyId'] = $company->id;
$workorders = RMLWorkorderModel::getAll($filters, $pagination['per_page'], ($pagination['page'] - 1) * $pagination['per_page'], $order);
$totalCount = RMLWorkorderModel::count($filters);
$rows = [];
foreach($workorders as $workorder) {
$row = (array)$workorder;
$row['preorderInfo'] = $this->getPreorderInfoText($workorder->preorderId);
$rows[] = $row;
}
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) {
var_dump($e->getMessage());exit;
// Log error but continue with other files
error_log("File upload failed for $name: " . $e->getMessage());
}
}
}
self::returnJson(['success' => true, 'message' => "$uploadCount Datei(en) erfolgreich hochgeladen."]);
}
protected function getDocumentationAction() {
if(empty($this->request->workorderId)) self::sendError("Workorder ID missing.");
// Order by creation date to ensure consistent numbering (_1, _2, etc.)
$docs = RMLWorkorderDocumentationModel::getAll(['workorderId' => $this->request->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);
// Increment counter for the specific document type
$documentTypeKey = $doc->documentType;
if (!isset($typeCounts[$documentTypeKey])) {
$typeCounts[$documentTypeKey] = 1;
} else {
$typeCounts[$documentTypeKey]++;
}
// Construct the new filename using the original key
$originalFilename = $file->orig_filename ?? $file->filename;
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$translatedType = $translationMap[$documentTypeKey] ?? $documentTypeKey;
$newFilename = "{$translatedType} {$typeCounts[$documentTypeKey]}." . strtolower($extension);
// Get the translated text, with a fallback to the original key
// Build the response object with 'id' mapped from 'fileId' and the translated type
$responseDocs[] = [
'id' => $doc->fileId,
'fileName' => $newFilename,
'documentType' => $documentTypeKey,
'mimetype' => $file->mimetype,
];
}
self::returnJson($responseDocs);
}
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.']);
}
}