263 lines
12 KiB
PHP
263 lines
12 KiB
PHP
<?php
|
|
// RMLWorkorderAdminController.php
|
|
|
|
class RMLWorkorderAdminController extends TTCrud
|
|
{
|
|
protected string $headerTitle = 'RML Arbeitsaufträge (Admin)';
|
|
protected bool $createText = false;
|
|
protected array $permissionCheck = ['RMLAdmin'];
|
|
|
|
protected array $columns = [
|
|
// ['key' => 'id', 'text' => 'Auftrag-Nr.', 'table' => ['sortable' => true]],
|
|
['key' => 'preordercampaign_id', 'text' => 'Cluster', 'modal' => false, 'table' => ['filter' => 'select']],
|
|
['key' => 'preorderInfo', 'text' => 'Kunde / Projekt', 'modal' => false, 'table' => ['sortable' => false]],
|
|
['key' => 'rimo_fcp_name', 'text' => 'FCP', 'modal' => false, 'table' => ['sortable' => false]],
|
|
['key' => 'companyName', 'text' => 'Zuständige Firma', 'modal' => false],
|
|
['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' => 'intervention_required', 'text' => 'Eingriff benötigt', 'icon' => 'fas fa-times-circle 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']],
|
|
];
|
|
|
|
protected function indexAction()
|
|
{
|
|
$campaigns = Helper::getPreorderCampaignFromUser($this->user, true);
|
|
$this->columns[array_search('preordercampaign_id', array_column($this->columns, 'key'))]['table']['filterOptions'] = array_map(
|
|
fn($c) => ['value' => $c->id, 'text' => $c->name],
|
|
$campaigns
|
|
);
|
|
|
|
$this->createWorkordersFromPreorders();
|
|
Helper::renderVue($this, 'RMLWorkorderAdmin', $this->headerTitle, [
|
|
"CRUD_CONFIG" => $this->getCrudConfig(),
|
|
"TABLE_URL" => $this::getUrl("RMLWorkorderAdmin/get"),
|
|
]);
|
|
}
|
|
|
|
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'] ?? [];
|
|
|
|
$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;
|
|
}
|
|
|
|
$limit = $pagination['per_page'];
|
|
$offset = ($pagination['page'] - 1) * $limit;
|
|
|
|
$workorders = RMLWorkorderModel::getAdminWorkorders($filters, $limit, $offset, $order, $allowedCampaignIds);
|
|
$totalCount = RMLWorkorderModel::countAdminWorkorders($filters, $allowedCampaignIds);
|
|
|
|
$rows = array_map(function($workorder) {
|
|
$row = (array)$workorder;
|
|
|
|
$row['companyName'] ??= 'Nicht zugewiesen';
|
|
$row['deadlineDateFormatted'] = $row['deadlineDate'] ? date('d.m.Y', $row['deadlineDate']) : 'Keine Deadline';
|
|
$row['daysUntilDeadline'] = $row['deadlineDate'] ? ceil(($row['deadlineDate'] - time()) / (60 * 60 * 24)) : null;
|
|
|
|
return $row;
|
|
}, $workorders);
|
|
|
|
self::returnJson([
|
|
'rows' => $rows,
|
|
'pagination' => [
|
|
'page' => $pagination['page'],
|
|
'per_page' => $pagination['per_page'],
|
|
'total_rows' => $totalCount,
|
|
'total_pages' => ceil($totalCount / $limit),
|
|
'filtered_available' => $totalCount
|
|
]
|
|
]);
|
|
}
|
|
|
|
private function createWorkordersFromPreorders() {
|
|
$newPreorders = PreorderModel::searchActive(['status_code' => 220, 'preorder_status_flags_all' => [3,5]]);
|
|
if (empty($newPreorders)) return;
|
|
|
|
foreach ($newPreorders as $preorder) {
|
|
if (!RMLWorkorderModel::getFirst(['preorderId' => $preorder->id])) {
|
|
RMLWorkorderModel::create([
|
|
'preorderId' => $preorder->id,
|
|
'clusterId' => $preorder->preordercampaign_id,
|
|
'status' => 'new',
|
|
'create' => time(),
|
|
'createBy' => $this->user->id
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function assignWorkorderAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['workorderId']) || empty($post['companyId'])) self::sendError("Required fields are missing.");
|
|
|
|
$workorder = RMLWorkorderModel::get($post['workorderId']);
|
|
if (!$workorder) self::sendError("Workorder not found.");
|
|
|
|
$workorder->companyId = $post['companyId'];
|
|
$workorder->status = 'assigned';
|
|
$workorder->assignmentDate = time();
|
|
$workorder->deadlineDate = $post['deadlineDate'] ?? strtotime('+6 weeks');
|
|
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Auftrag erfolgreich zugewiesen.']);
|
|
}
|
|
|
|
protected function getDocumentationAction() {
|
|
if(empty($this->request->workorderId)) self::sendError("Workorder ID missing.");
|
|
|
|
$docs = RMLWorkorderDocumentationModel::getAll(['workorderId' => $this->request->workorderId], null, 0, ['key' => 'create', 'order' => 'DESC']);
|
|
$journals = RMLWorkorderJournalModel::getAll(['workorderId' => $this->request->workorderId], null, 0, ['key' => 'create', 'order' => 'DESC']);
|
|
$users = UserModel::search();
|
|
|
|
foreach($docs as $doc) {
|
|
$file = new File($doc->fileId);
|
|
$doc->fileName = $file->orig_filename ?? $file->filename;
|
|
$doc->userName = UserModel::getOne($doc->createBy)->name ?? 'Unbekannt';
|
|
$doc->mimetype = $file->mimetype ?? 'application/octet-stream';
|
|
}
|
|
foreach($journals as $journal) {
|
|
$journal->createByName = UserModel::getOne($journal->createBy)->name ?? 'Unbekannt';
|
|
}
|
|
self::returnJson(['docs' => $docs, 'journals' => $journals]);
|
|
}
|
|
|
|
protected function massAssignWorkordersAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['workorderIds']) || empty($post['companyId'])) self::sendError("Required fields are missing.");
|
|
|
|
$deadline = strtotime($post['deadlineDate'] ?? '+6 weeks');
|
|
$count = 0;
|
|
foreach ($post['workorderIds'] as $workorderId) {
|
|
$workorder = RMLWorkorderModel::get($workorderId);
|
|
if ($workorder && $workorder->status === 'new') {
|
|
$workorder->companyId = $post['companyId'];
|
|
$workorder->status = 'assigned';
|
|
$workorder->assignmentDate = time();
|
|
$workorder->deadlineDate = $deadline;
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
$count++;
|
|
}
|
|
}
|
|
self::returnJson(['success' => true, 'message' => "$count Aufträge erfolgreich zugewiesen."]);
|
|
}
|
|
|
|
protected function requestCorrectionAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['workorderId']) || empty($post['text'])) self::sendError("Required fields are missing.");
|
|
|
|
$workorder = RMLWorkorderModel::get($post['workorderId']);
|
|
if (!$workorder) self::sendError("Workorder not found.");
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'correction_requested';
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
|
|
RMLWorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id,
|
|
'text' => $post['text'],
|
|
'fileIds' => !empty($post['fileIds']) ? json_encode($post['fileIds']) : null,
|
|
'statusChange' => "$oldStatus -> correction_requested",
|
|
'create' => time(),
|
|
'createBy' => $this->user->id,
|
|
]);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Korrektur wurde angefordert.']);
|
|
}
|
|
|
|
protected function getCompaniesAction() {
|
|
$companies = RMLWorkorderCompanyModel::getAll([], null, 0, ['key' => 'name', 'order' => 'ASC']);
|
|
$items = array_map(fn($c) => ['value' => $c->id, 'text' => $c->name], $companies);
|
|
self::returnJson($items);
|
|
}
|
|
|
|
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)->name ?? 'Unbekannt';
|
|
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
|
|
]);
|
|
}
|
|
|
|
protected function updateDeadlineAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['workorderId']) || empty($post['deadlineDate'])) self::sendError("Required fields are missing.");
|
|
|
|
$workorder = RMLWorkorderModel::get($post['workorderId']);
|
|
if (!$workorder) self::sendError("Workorder not found.");
|
|
|
|
$workorder->deadlineDate = $post['deadlineDate'];
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
|
|
RMLWorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id,
|
|
'text' => 'Deadline wurde auf ' . date('d.m.Y', $post['deadlineDate']) . ' geändert.',
|
|
'create' => time(),
|
|
'createBy' => $this->user->id,
|
|
]);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Deadline erfolgreich aktualisiert.']);
|
|
}
|
|
|
|
protected function acceptDocumentationAction() {
|
|
$post = json_decode(file_get_contents('php://input'), true);
|
|
if (empty($post['workorderId'])) self::sendError("Workorder ID is missing.");
|
|
|
|
$workorder = RMLWorkorderModel::get($post['workorderId']);
|
|
if (!$workorder) self::sendError("Workorder not found.");
|
|
|
|
if ($workorder->status !== 'documented') {
|
|
self::sendError("Die Dokumentation muss zuerst von der Firma als fertig markiert werden.");
|
|
}
|
|
|
|
$oldStatus = $workorder->status;
|
|
$workorder->status = 'completed';
|
|
RMLWorkorderModel::update((array)$workorder);
|
|
|
|
RMLWorkorderJournalModel::create([
|
|
'workorderId' => $workorder->id,
|
|
'text' => 'Dokumentation wurde akzeptiert und der Auftrag abgeschlossen.',
|
|
'statusChange' => "$oldStatus -> completed",
|
|
'create' => time(),
|
|
'createBy' => $this->user->id,
|
|
]);
|
|
|
|
self::returnJson(['success' => true, 'message' => 'Dokumentation akzeptiert und Auftrag abgeschlossen.']);
|
|
}
|
|
} |