401 lines
19 KiB
PHP
401 lines
19 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' => 'preordercampaign_id', 'text' => 'Kampagne', 'modal' => false, 'table' => ['filter' => 'select', 'sortable' => true]],
|
|
['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'));
|
|
$preCamColIdx = array_search('preordercampaign_id', 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;
|
|
}
|
|
}
|
|
|
|
$campaigns = Helper::getPreorderCampaignFromUser($this->user, true);
|
|
|
|
if ($preCamColIdx !== false) {
|
|
$this->columns[$preCamColIdx]['table']['filterOptions'] = array_map(fn($c) => ['value' => $c->id, 'text' => $c->name], $campaigns);
|
|
if (!$this->user->isAdmin() && count($campaigns) === 1) {
|
|
$this->columns[$preCamColIdx]['table']['defaultFilter'] = $campaigns[0]->id;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//region ACTIONS
|
|
public function indexAction()
|
|
{
|
|
$this->createWorkordersFromPreorders();
|
|
$this->autoCompleteDocumentedWorkorders();
|
|
$this->archiveWorkorders();
|
|
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 = $this->postData['deadlineDate'] ?? strtotime('+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);
|
|
$preorderCampaign = new Preordercampaign($preorder->preordercampaign_id);
|
|
$network = new Network($preorderCampaign->network_id);
|
|
if ($preorder->id && $network->owner_id == 4807) {
|
|
$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 chargeWorkorderAction()
|
|
{
|
|
if (!$this->user->can('RMLAdmin')) {
|
|
self::sendError("Keine Berechtigung.");
|
|
}
|
|
|
|
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 !== 'completed') {
|
|
self::sendError("Nur abgeschlossene Arbeitsaufträge können verrechnet werden.");
|
|
}
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'charged';
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id,
|
|
'text' => 'Arbeitsauftrag wurde als verrechnet markiert.',
|
|
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('charged'),
|
|
'create' => time(),
|
|
'createBy' => $this->user->id,
|
|
]);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Arbeitsauftrag als verrechnet markiert.']);
|
|
}
|
|
|
|
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,
|
|
]);
|
|
|
|
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);
|
|
$preorderCampaign = new Preordercampaign($preorder->preordercampaign_id);
|
|
$network = new Network($preorderCampaign->network_id);
|
|
if ($preorder->id && $network->owner_id == 4807) {
|
|
$preorder->status_id = 10; // In Ausführung
|
|
$preorder->edit_by = $this->user->id;
|
|
$preorder->save();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected function revertDocumentedStatusAction()
|
|
{
|
|
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("Nur Aufträge mit Status 'Dokumentiert' können zurückgesetzt werden.");
|
|
}
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'assigned'; // Revert to 'assigned' status
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id,
|
|
'text' => 'Status von Admin von "' . $this->getStatusText($oldStatus) . '" auf "' . $this->getStatusText('assigned') . '" zurückgesetzt.',
|
|
'statusChange' => $this->getStatusText($oldStatus) . " -> " . $this->getStatusText('assigned'),
|
|
'create' => time(),
|
|
'createBy' => $this->user->id,
|
|
]);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Status erfolgreich auf "Zugewiesen" zurückgesetzt.']);
|
|
}
|
|
|
|
protected function scheduleAppointmentAction()
|
|
{
|
|
if (empty($this->postData['workorderId']) || empty($this->postData['appointmentDate'])) self::sendError("Erforderliche Felder fehlen.");
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
if ((int)date('H', $this->postData['appointmentDate']) >= 23 || (int)date('H', $this->postData['appointmentDate']) < 1) self::sendError("Bitte geben Sie eine Uhrzeit an!");
|
|
|
|
$workorder->appointmentDate = $this->postData['appointmentDate'];
|
|
$workorder->status = 'scheduled';
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => 'Termin festgelegt auf: ' . date('d.m.Y H:i', $this->postData['appointmentDate']),
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
self::returnJson(['success' => true, 'message' => 'Termin erfolgreich gespeichert.']);
|
|
}
|
|
|
|
protected function rescheduleAppointmentAction()
|
|
{
|
|
if (empty($this->postData['workorderId']) || empty($this->postData['appointmentDate']) || empty($this->postData['reason'])) self::sendError("Erforderliche Felder fehlen.");
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
if ((int)date('H', $this->postData['appointmentDate']) >= 23 || (int)date('H', $this->postData['appointmentDate']) < 1) self::sendError("Bitte geben Sie eine Uhrzeit an!");
|
|
|
|
$oldDateFormatted = $workorder->appointmentDate ? date('d.m.Y H:i', $workorder->appointmentDate) : 'N/A';
|
|
$newDateFormatted = date('d.m.Y H:i', $this->postData['appointmentDate']);
|
|
$workorder->appointmentDate = $this->postData['appointmentDate'];
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => "Termin verschoben von {$oldDateFormatted} auf {$newDateFormatted}. Grund: " . $this->postData['reason'],
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
self::returnJson(['success' => true, 'message' => 'Termin erfolgreich verschoben.']);
|
|
}
|
|
|
|
protected function clearAppointmentAction()
|
|
{
|
|
if (empty($this->postData['workorderId'])) self::sendError("Arbeitsauftrags-ID fehlt.");
|
|
$workorder = WorkorderModel::get($this->postData['workorderId']);
|
|
if (!$workorder) self::sendError("Arbeitsauftrag nicht gefunden.");
|
|
|
|
$oldDateFormatted = $workorder->appointmentDate ? date('d.m.Y H:i', $workorder->appointmentDate) : 'N/A';
|
|
$workorder->appointmentDate = null;
|
|
$workorder->status = 'assigned';
|
|
WorkorderModel::update((array)$workorder);
|
|
|
|
WorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id, 'text' => "Termin gelöscht (war: {$oldDateFormatted}).",
|
|
'create' => time(), 'createBy' => $this->user->id,
|
|
]);
|
|
self::returnJson(['success' => true, 'message' => 'Termin erfolgreich gelöscht.']);
|
|
}
|
|
//endregion
|
|
} |