296 lines
14 KiB
PHP
296 lines
14 KiB
PHP
<?php
|
|
// WorkorderAdminController.php
|
|
|
|
class WorkorderAdminController extends WorkorderBaseController
|
|
{
|
|
protected string $headerTitle = 'Arbeitsaufträge Verwaltung';
|
|
protected bool $createText = false;
|
|
protected array $permissionCheck = ['RMLAdmin'];
|
|
|
|
protected array $columns = [
|
|
['key' => 'id', 'text' => 'Auftrags-Nr.', 'table' => ['sortable' => true, 'filter' => 'numberRange']],
|
|
['key' => 'netOwnerId', 'text' => 'Netzeigentümer', 'modal' => false, 'table' => ['filter' => 'select'], 'required' => false],
|
|
['key' => 'preorderInfo', 'text' => 'Kunde', 'modal' => false, 'table' => ['filter' => 'search', 'sortable' => false]],
|
|
['key' => 'companyName', 'text' => 'Firma', 'modal' => false, 'table' => ['filter' => 'search', 'sortable' => true]],
|
|
['key' => 'rimo_fcp_name', 'text' => 'FCP', 'modal' => false, 'table' => ['filter' => 'search', 'sortable' => true]],
|
|
// Status column is now inherited via prepareCrudConfig
|
|
['key' => 'additionalInfo', 'text' => 'Notiz', 'modal' => false, 'table' => ['sortable' => true]],
|
|
['key' => 'deadlineDate', 'text' => 'Deadline', 'modal' => false, 'table' => ['filter' => 'date', 'sortable' => true]],
|
|
['key' => 'appointmentDate', 'text' => 'Termin', 'modal' => false, 'table' => ['filter' => 'date', 'sortable' => true]],
|
|
];
|
|
|
|
/**
|
|
* Prepares the CRUD configuration.
|
|
*/
|
|
protected function prepareCrudConfig()
|
|
{
|
|
$preorderInfoColIdx = array_search('preorderInfo', array_column($this->columns, 'key'));
|
|
array_splice($this->columns, $preorderInfoColIdx + 1, 0, [$this->statusColumn]);
|
|
$netOwnerColIdx = array_search('netOwnerId', array_column($this->columns, 'key'));
|
|
|
|
if ($netOwnerColIdx !== false) {
|
|
if ($this->user->isAdmin()) {
|
|
$netOwners = Helper::getPreorderCampaignNetworkOwners();
|
|
$this->columns[$netOwnerColIdx]['table']['filterOptions'] = array_map(fn($o) => ['value' => $o->id, 'text' => $o->company], $netOwners);
|
|
} else {
|
|
$this->columns[$netOwnerColIdx]['table'] = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
//region ACTIONS
|
|
public function indexAction()
|
|
{
|
|
$this->createWorkordersFromPreorders();
|
|
parent::indexAction();
|
|
}
|
|
|
|
/**
|
|
* Fetches workorders for the admin view.
|
|
*/
|
|
protected function getAction()
|
|
{
|
|
$pagination = $this->postData['pagination'] ?? ['page' => 1, 'per_page' => 10];
|
|
$filters = $this->postData['filters'] ?? [];
|
|
$order = $this->postData['order'] ?? [];
|
|
|
|
$allowedCampaignIds = Helper::getPreorderCampaignFromUser($this->user);
|
|
if (empty($allowedCampaignIds)) {
|
|
self::returnJson(['rows' => [], 'pagination' => array_merge($pagination, ['total_rows' => 0, 'total_pages' => 0, 'filtered_available' => 0])]);
|
|
return;
|
|
}
|
|
|
|
$workorders = WorkorderModel::getAdminWorkorders($filters, $pagination['per_page'], ($pagination['page'] - 1) * $pagination['per_page'], $order, $allowedCampaignIds);
|
|
$totalCount = WorkorderModel::countAdminWorkorders($filters, $allowedCampaignIds);
|
|
|
|
$rows = array_map(function ($workorder) {
|
|
$row = (array)$workorder;
|
|
$row['companyName'] ??= 'Nicht zugewiesen';
|
|
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]
|
|
]);
|
|
}
|
|
|
|
protected function getCompaniesAction()
|
|
{
|
|
$tenantId = $this->request->tenantId;
|
|
$companies = WorkorderCompanyModel::getAll(['visibleForAddressId' => "%$tenantId%"]);
|
|
self::returnJson(array_map(fn($c) => ['value' => $c->id, 'text' => $c->name], $companies));
|
|
}
|
|
|
|
protected function assignWorkorderAction()
|
|
{
|
|
if (empty($this->postData['workorderId']) || empty($this->postData['companyId'])) self::sendError("Erforderliche Felder fehlen.");
|
|
$deadline = !empty($this->postData['deadlineDate']) ? $this->postData['deadlineDate'] : strtotime('+6 weeks');
|
|
|
|
if ($this->assignSingleWorkorder($this->postData['workorderId'], $this->postData['companyId'], $deadline, $this->user->id)) {
|
|
self::returnJson(['success' => true, 'message' => 'Arbeitsauftrag erfolgreich zugewiesen.']);
|
|
} else {
|
|
self::sendError("Arbeitsauftrag konnte nicht zugewiesen werden.");
|
|
}
|
|
}
|
|
|
|
protected function massAssignWorkordersAction()
|
|
{
|
|
if (empty($this->postData['workorderIds']) || empty($this->postData['companyId'])) self::sendError("Erforderliche Felder fehlen.");
|
|
|
|
$deadline = strtotime($this->postData['deadlineDate'] ?? '+6 weeks');
|
|
$count = 0;
|
|
foreach ($this->postData['workorderIds'] as $workorderId) {
|
|
if ($this->assignSingleWorkorder($workorderId, $this->postData['companyId'], $deadline, $this->user->id)) $count++;
|
|
}
|
|
self::returnJson(['success' => true, 'message' => "$count Arbeitsaufträge erfolgreich zugewiesen."]);
|
|
}
|
|
|
|
protected function requestCorrectionAction()
|
|
{
|
|
if (empty($this->postData['workorderId']) || empty($this->postData['text'])) self::sendError("Erforderliche Felder fehlen.");
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'correction_requested';
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => "Korrektur angefordert. Grund: " . $this->postData['text'],
|
|
'fileIds' => !empty($this->postData['fileIds']) ? json_encode($this->postData['fileIds']) : null,
|
|
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('correction_requested'),
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
self::returnJson(['success' => true, 'message' => 'Korrektur wurde angefordert.']);
|
|
}
|
|
|
|
protected function updateDeadlineAction()
|
|
{
|
|
if (empty($this->postData['workorderId']) || empty($this->postData['deadlineDate'])) self::sendError("Erforderliche Felder fehlen.");
|
|
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
|
|
$workorder->deadlineDate = $this->postData['deadlineDate'];
|
|
WorkorderModel::update((array)$workorder);
|
|
WorkorderJournalModel::create(['workorderId' => $workorder->id, 'text' => 'Deadline geändert auf ' . date('d.m.Y', $this->postData['deadlineDate']) . '.', 'create' => time(), 'createBy' => $this->user->id]);
|
|
self::returnJson(['success' => true, 'message' => 'Deadline erfolgreich aktualisiert.']);
|
|
}
|
|
|
|
protected function acceptDocumentationAction()
|
|
{
|
|
if (empty($this->postData['workorderId'])) self::sendError("Arbeitsauftrags-ID fehlt.");
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
if ($workorder->status !== 'documented') self::sendError("Die Dokumentation muss zuerst von der Firma als fertig markiert werden.");
|
|
|
|
$preorder = new Preorder($workorder->preorderId);
|
|
if ($preorder->id) {
|
|
$preorder->status_id = 15;
|
|
$preorder->edit_by = $this->user->id;
|
|
$preorder->save();
|
|
}
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'completed';
|
|
WorkorderModel::update((array)$workorder);
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => 'Dokumentation akzeptiert und Arbeitsauftrag abgeschlossen.',
|
|
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('completed'),
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
self::returnJson(['success' => true, 'message' => 'Dokumentation akzeptiert und Arbeitsauftrag abgeschlossen.']);
|
|
}
|
|
|
|
protected function setToProblemSolvedAction()
|
|
{
|
|
if (empty($this->postData['workorderId']) || empty($this->postData['text'])) self::sendError("Arbeitsauftrags-ID und Text sind erforderlich.");
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
if ($workorder->status !== 'intervention_required') self::sendError("Der Arbeitsauftrag muss den Status 'Eingriff erforderlich' haben.");
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'problem_solved';
|
|
WorkorderModel::update((array)$workorder);
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => "Problem gelöst: " . $this->postData['text'],
|
|
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('problem_solved'),
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
self::returnJson(['success' => true, 'message' => 'Arbeitsauftrag als "Problem gelöst" markiert.']);
|
|
}
|
|
|
|
protected function cancelWorkorderAction()
|
|
{
|
|
if (empty($this->postData['workorderId'])) self::sendError("Arbeitsauftrags-ID fehlt.");
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'cancelled';
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => 'Arbeitsauftrag wurde storniert.' . (!empty($this->postData['reason']) ? ' Grund: ' . $this->postData['reason'] : ''),
|
|
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('cancelled'),
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
|
|
$preorder = new Preorder($workorder->preorderId);
|
|
if ($preorder->id) {
|
|
$preorder->status_id = 99; // Storniert
|
|
$preorder->edit_by = $this->user->id;
|
|
$preorder->save();
|
|
}
|
|
self::returnJson(['success' => true, 'message' => 'Arbeitsauftrag wurde storniert.']);
|
|
}
|
|
|
|
protected function setCivilEngineeringRequiredAction()
|
|
{
|
|
if (empty($this->postData['workorderId'])) self::sendError("Arbeitsauftrags-ID fehlt.");
|
|
if (empty($this->postData['companyId'])) self::sendError("Bitte Tiefbaufirma auswählen.");
|
|
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
$company = WorkorderCompanyModel::get($this->postData['companyId']);
|
|
if (!$company) self::sendError("Tiefbaufirma nicht gefunden.");
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->civilEngineeringCompanyId = $company->id;
|
|
$workorder->originalCompanyId = $workorder->companyId;
|
|
$workorder->companyId = $company->id;
|
|
$workorder->status = 'civil_engineering_required';
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => "Tiefbau wurde angefordert. Firma '{$company->name}' wurde zugewiesen.",
|
|
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('civil_engineering_required'),
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
self::returnJson(['success' => true, 'message' => 'Tiefbau wurde angefordert und Firma zugewiesen.']);
|
|
}
|
|
|
|
//endregion
|
|
|
|
//region PRIVATE HELPERS
|
|
private function assignSingleWorkorder($workorderId, $companyId, $deadline, $userId): bool
|
|
{
|
|
$workorder = WorkorderModel::get($workorderId);
|
|
if (!$workorder) return false;
|
|
if ($workorder->status === 'civil_engineering_required') {
|
|
$workorder->companyId = $companyId;
|
|
$workorder->civilEngineeringCompanyId = $companyId;
|
|
WorkorderModel::update((array)$workorder);
|
|
return true;
|
|
}
|
|
$company = WorkorderCompanyModel::get($companyId);
|
|
if (!$company) return false;
|
|
|
|
$workorder->companyId = $companyId;
|
|
$workorder->status = 'assigned';
|
|
$workorder->assignmentDate = time();
|
|
$workorder->deadlineDate = $deadline;
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => "Firma '{$company->name}' wurde zugewiesen.", 'create' => time(), 'createBy' => $userId,
|
|
]);
|
|
|
|
$preorder = new Preorder($workorder->preorderId);
|
|
if ($preorder->id) {
|
|
$preorder->status_id = 10; // In Ausführung
|
|
$preorder->edit_by = $this->user->id;
|
|
$preorder->save();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private function createWorkordersFromPreorders()
|
|
{
|
|
$configs = WorkorderTenantConfigModel::getAll();
|
|
foreach ($configs as $config) {
|
|
$filters = json_decode($config->workorderCreationFilters, true);
|
|
if (empty($filters)) continue;
|
|
|
|
$networks = NetworkModel::getAll(['owner_id' => $config->addressId]);
|
|
if (empty($networks)) continue;
|
|
|
|
$tenantCampaigns = array_map(fn($n) => $n->id, PreordercampaignModel::getAll(['network_id' => array_map(fn($n) => $n->id, $networks)]));
|
|
if (empty($tenantCampaigns)) continue;
|
|
|
|
$filters['preordercampaign_id'] = $tenantCampaigns;
|
|
$newPreorders = PreorderModel::searchActive($filters);
|
|
|
|
foreach ($newPreorders as $preorder) {
|
|
if (!WorkorderModel::getFirst(['preorderId' => $preorder->id])) {
|
|
WorkorderModel::create([
|
|
'preorderId' => $preorder->id, 'clusterId' => $preorder->preordercampaign_id,
|
|
'status' => 'new', 'create' => time(), 'createBy' => 0 // System User
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//endregion
|
|
} |