Add spliceCompleted functionality and update WorkorderMph components

This commit is contained in:
2025-11-26 10:58:18 +01:00
parent ed8a7cb8d8
commit 8ea51b3900
8 changed files with 383 additions and 177 deletions

View File

@@ -18,29 +18,12 @@ class WorkorderMphBaseController extends TTCrud
protected array $additionalJS = ["js/pages/WorkorderMphBase/WorkorderMphBase.js"];
protected array $additionalHead = ["<link rel='stylesheet' href='/js/pages/WorkorderMphBase/WorkorderMphBase.css'>"];
// Wohneinheit status options
protected array $wohneinheitStatuses = [
['value' => 1, 'text' => '10 - new'],
['value' => 12, 'text' => '241 - BEP installed (MD)'],
['value' => 13, 'text' => '242 - Inhouse cabling finished'],
['value' => 18, 'text' => '243 - Cable in stairwell'],
['value' => 14, 'text' => '244 - BEP installed (SD)'],
['value' => 15, 'text' => '245 - Installation Approved'],
['value' => 16, 'text' => '300 - ONT installed'],
];
protected function getStatusText(string $statusKey): string
{
$statusMap = array_column($this->statusColumn['table']['filterOptions'] ?? [], 'text', 'value');
return $statusMap[$statusKey] ?? ucfirst(str_replace('_', ' ', $statusKey));
}
protected function getWohneinheitStatusText(int $statusValue): string
{
$statusMap = array_column($this->wohneinheitStatuses, 'text', 'value');
return $statusMap[$statusValue] ?? "Status $statusValue";
}
//region SHARED ACTIONS
/**
* Fetches documentation and journal entries for a given workorder.
@@ -143,38 +126,75 @@ class WorkorderMphBaseController extends TTCrud
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$hausnummerId = $db->escape($workorder->hausnummerId);
$sql = "SELECT w.id, w.bezeichner, w.contact, w.oaid, w.note
// Fetch statuses from addressdb
$statusSql = "SELECT id, code, name FROM Status WHERE type = 'wohneinheit' ORDER BY code ASC";
$statusResult = $db->query($statusSql);
$statuses = $statusResult ? $statusResult->fetch_all(MYSQLI_ASSOC) : [];
$statusOptions = array_map(function($s) {
return ['value' => intval($s['id']), 'text' => $s['code'] . ' - ' . $s['name'], 'code' => intval($s['code'])];
}, $statuses);
// Fetch Wohneinheiten directly
$sql = "SELECT w.id, w.zusatz, w.tuer, w.contact, w.oaid, w.note, w.status_id, w.splice_hak_completed
FROM Wohneinheit w
WHERE w.hausnummer_id = $hausnummerId
ORDER BY w.bezeichner";
ORDER BY w.zusatz";
$result = $db->query($sql);
$wohneinheiten = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
// Get existing WorkorderMphWohneinheit records
$existingRecords = WorkorderMphWohneinheitModel::getAll(['workorderMphId' => $workorderMphId]);
$recordsMap = [];
foreach ($existingRecords as $record) {
$recordsMap[$record->wohneinheitId] = $record;
// Get Preorders for this Hausnummer to fallback contact info
$preorders = [];
if (class_exists('PreorderModel')) {
$preorderList = PreorderModel::search(['adb_hausnummer_id' => $workorder->hausnummerId, 'deleted' => 0]);
foreach ($preorderList as $preorder) {
if ($preorder->adb_wohneinheit_id) {
$preorders[$preorder->adb_wohneinheit_id] = $preorder;
}
}
}
// Merge data
$response = [];
foreach ($wohneinheiten as $we) {
$record = $recordsMap[$we['id']] ?? null;
// Prefer WorkorderMphWohneinheit note, fallback to addressdb note
$note = $record ? $record->note : $we['note'];
// Contact info logic
$contact = $we['contact'];
$preorderContact = null;
$preorderUcode = null;
if (isset($preorders[$we['id']])) {
$p = $preorders[$we['id']];
$preorderUcode = $p->ucode;
$pContact = trim($p->firstname . ' ' . $p->lastname);
if ($p->phone) $pContact .= ' (' . $p->phone . ')';
$preorderContact = $pContact;
// If address contact is empty, use preorder contact
if (empty($contact)) {
$contact = $pContact;
}
}
$response[] = [
'wohneinheitId' => intval($we['id']),
'bezeichner' => $we['bezeichner'],
'contact' => $we['contact'],
'zusatz' => $we['zusatz'],
'tuer' => $we['tuer'],
'contact' => $contact,
'preorderContact' => $preorderContact,
'preorderUcode' => $preorderUcode,
'oaid' => $we['oaid'],
'status' => $record ? $record->status : 1,
'note' => $note,
'recordId' => $record ? $record->id : null,
'status' => intval($we['status_id']),
'spliceCompleted' => intval($we['splice_hak_completed'] ?? 0),
'note' => $we['note'],
];
}
self::returnJson(['wohneinheiten' => $response, 'statusOptions' => $this->wohneinheitStatuses]);
self::returnJson([
'wohneinheiten' => $response,
'statusOptions' => $statusOptions,
'hausnummerId' => $workorder->hausnummerId
]);
}
/**
@@ -189,63 +209,86 @@ class WorkorderMphBaseController extends TTCrud
$workorderMphId = intval($post['workorderMphId']);
$wohneinheitId = intval($post['wohneinheitId']);
$status = intval($post['status'] ?? 1);
$note = $post['note'] ?? null;
$newStatusId = intval($post['status'] ?? 1);
$spliceCompleted = isset($post['spliceCompleted']) ? intval($post['spliceCompleted']) : 0;
$tuer = $post['tuer'] ?? null;
$zusatz = $post['zusatz'] ?? null;
// Check if record exists
$existing = WorkorderMphWohneinheitModel::getFirst([
'workorderMphId' => $workorderMphId,
'wohneinheitId' => $wohneinheitId
]);
$oldStatus = $existing ? $existing->status : 1;
$oldNote = $existing ? $existing->note : null;
if ($existing) {
$existing->status = $status;
$existing->note = $note;
$existing->edit = time();
$existing->editBy = $this->user->id;
WorkorderMphWohneinheitModel::update((array)$existing);
} else {
WorkorderMphWohneinheitModel::create([
'workorderMphId' => $workorderMphId,
'wohneinheitId' => $wohneinheitId,
'status' => $status,
'note' => $note,
'create' => time(),
'createBy' => $this->user->id
]);
}
// Also save note to addressdb.Wohneinheit
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$escapedNote = $note !== null ? "'" . $db->escape($note) . "'" : "NULL";
$escapedWohneinheitId = $db->escape($wohneinheitId);
$updateSql = "UPDATE Wohneinheit SET note = $escapedNote WHERE id = $escapedWohneinheitId";
// Fetch current state
$currentSql = "SELECT status_id, tuer, zusatz, splice_hak_completed FROM Wohneinheit WHERE id = $escapedWohneinheitId";
$result = $db->query($currentSql);
$current = $result ? $result->fetch_assoc() : null;
if (!$current) self::sendError("Wohneinheit nicht gefunden.");
$oldStatusId = intval($current['status_id']);
$oldTuer = $current['tuer'];
$oldZusatz = $current['zusatz'];
$oldSplice = intval($current['splice_hak_completed'] ?? 0);
// Update Wohneinheit
$escapedTuer = $tuer !== null ? "'" . $db->escape($tuer) . "'" : "NULL";
$escapedZusatz = $zusatz !== null ? "'" . $db->escape($zusatz) . "'" : "NULL";
$escapedStatusId = $db->escape($newStatusId);
$escapedSplice = $db->escape($spliceCompleted);
$updateSql = "UPDATE Wohneinheit SET
status_id = $escapedStatusId,
tuer = $escapedTuer,
zusatz = $escapedZusatz,
splice_hak_completed = $escapedSplice
WHERE id = $escapedWohneinheitId";
$db->query($updateSql);
// Add journal entry if status or note changed
if ($oldStatus !== $status || $oldNote !== $note) {
$changes = [];
if ($oldStatus !== $status) {
$changes[] = "Status: " . $this->getWohneinheitStatusText($oldStatus) . "" . $this->getWohneinheitStatusText($status);
}
if ($oldNote !== $note) {
$changes[] = "Notiz aktualisiert";
}
// Journaling
$changes = [];
if ($oldStatusId !== $newStatusId) {
// Fetch status names for better logging
$statusNamesSql = "SELECT id, code, name FROM Status WHERE id IN ($oldStatusId, $newStatusId)";
$statusRes = $db->query($statusNamesSql);
$statusMap = [];
if ($statusRes) {
while($row = $statusRes->fetch_assoc()) {
$statusMap[$row['id']] = $row['code'] . ' - ' . $row['name'];
}
}
$oldText = $statusMap[$oldStatusId] ?? "ID $oldStatusId";
$newText = $statusMap[$newStatusId] ?? "ID $newStatusId";
$changes[] = "Status: $oldText$newText";
}
if ($oldSplice !== $spliceCompleted) {
$changes[] = "Spleiß: " . ($spliceCompleted ? 'Erledigt' : 'Nicht erledigt');
}
if ($oldTuer !== $tuer) {
$changes[] = "Tür aktualisiert: '$oldTuer' -> '$tuer'";
}
if ($oldZusatz !== $zusatz) {
$changes[] = "Zusatz aktualisiert: '$oldZusatz' -> '$zusatz'";
}
if (!empty($changes)) {
WorkorderMphJournalModel::create([
'workorderMphId' => $workorderMphId,
'text' => "Wohneinheit $wohneinheitId: " . implode(', ', $changes),
'create' => time(),
'createBy' => $this->user->id,
]);
// If status is 241 (BEP MD) or 300 (ONT installed), set statusflag 200 on Wohneinheit
if (in_array($status, [12, 16])) { // 12=241 BEP MD, 16=300 ONT
$this->setWohneinheitStatusflag($wohneinheitId, 200);
}
}
// Status flag logic for BEP MD (241) and ONT (300). Need to check codes for these IDs.
// Since we only have IDs, we need to check the code of the newStatusId.
$newStatusCodeSql = "SELECT code FROM Status WHERE id = $escapedStatusId";
$resCode = $db->query($newStatusCodeSql);
$newStatusCode = $resCode ? intval($resCode->fetch_assoc()['code']) : 0;
if (in_array($newStatusCode, [241, 300])) { // 241=BEP MD, 300=ONT
$this->setWohneinheitStatusflag($wohneinheitId, 200);
}
self::returnJson(['success' => true, 'message' => 'Wohneinheit aktualisiert.']);
@@ -286,6 +329,8 @@ class WorkorderMphBaseController extends TTCrud
$changes = [];
$checkboxFields = ['easement', 'btb', 'fttxLocationSupplied', 'conduitToHuepLaid', 'huepMounted', 'dropCableAvailable', 'spliceCompleted'];
$updateHausnummerStatus = false;
foreach ($checkboxFields as $field) {
if (array_key_exists($field, $post)) {
$oldValue = $workorder->$field;
@@ -293,6 +338,11 @@ class WorkorderMphBaseController extends TTCrud
if ($oldValue !== $newValue) {
$workorder->$field = $newValue;
$changes[] = "$field: " . ($newValue ? 'ja' : 'nein');
// Check for FTTx Location mit Leerrohr versorgt
if ($field === 'fttxLocationSupplied' && $newValue === 1) {
$updateHausnummerStatus = true;
}
}
}
}
@@ -300,6 +350,19 @@ class WorkorderMphBaseController extends TTCrud
if (!empty($changes)) {
WorkorderMphModel::update((array)$workorder);
if ($updateHausnummerStatus) {
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
// Find status ID for code 200
$statusSql = "SELECT id FROM Status WHERE code = 200 AND type = 'hausnummer' LIMIT 1";
$statusResult = $db->query($statusSql);
if ($statusResult && $row = $statusResult->fetch_assoc()) {
$statusId = $row['id'];
$hnId = $db->escape($workorder->hausnummerId);
$updateHnSql = "UPDATE Hausnummer SET status_id = $statusId WHERE id = $hnId";
$db->query($updateHnSql);
}
}
WorkorderMphJournalModel::create([
'workorderMphId' => $workorder->id,
'text' => "Dokumentation aktualisiert:\n" . implode("\n", $changes),