Files
2026-04-28 21:15:09 +03:00

282 lines
8.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* @api-readonly
* Returns handover status dashboard data
*/
use Illuminate\Support\Facades\DB;
// Controller sayısını ayardan al
$controlCount = setting('handover_control_count', 3);
// Initialize response
$response = [];
// Tüm handovers kayıtlarını çek
$selectFields = array_merge(
[
'created_date',
'object',
'responsible',
'atg_date',
'final_status'
],
// Tüm controller'lar için date alanları
array_reduce(range(1, $controlCount), function($carry, $i) {
$carry[] = "control{$i}_date1";
$carry[] = "control{$i}_date2";
$carry[] = "control{$i}_date3";
return $carry;
}, []),
// Tüm controller'lar için status alanları
array_map(function ($i) {
return "control{$i}_id_status";
}, range(1, $controlCount))
);
$handovers = DB::table('handovers')->select($selectFields)->get();
// ----------------------------------------------------------------
// 1) EXISTING LOGIC: Hand-over Status (Object/Controller Table)
// ----------------------------------------------------------------
// 1) TABLO DATASI: object bazında grupla
$objectGroups = [];
foreach ($handovers as $row) {
$object = $row->object;
if (!$object)
continue; // Skip empty objects
if (!isset($objectGroups[$object])) {
$objectGroups[$object] = [
'object' => $object,
'total' => 0,
'waiting' => 0,
'archive' => 0
];
for ($i = 1; $i <= $controlCount; $i++) {
$objectGroups[$object]["controller{$i}_accept"] = 0;
$objectGroups[$object]["controller{$i}_pending"] = 0;
}
}
$objectGroups[$object]['total']++;
// Waiting durumu: created_date yoksa veya final_status = 'Waiting' ise
if (!$row->created_date || $row->final_status === 'Waiting') {
$objectGroups[$object]['waiting']++;
}
// Archive durumu: final_status = 'Archive' veya archive alanı dolu ise
if ($row->final_status === 'Archive' || ($row->atg_date && $row->final_status !== 'Waiting')) {
$objectGroups[$object]['archive']++;
}
for ($i = 1; $i <= $controlCount; $i++) {
$status = $row->{"control{$i}_id_status"};
if ($status === 'Agreed / подписано') {
$objectGroups[$object]["controller{$i}_accept"]++;
} else {
$objectGroups[$object]["controller{$i}_pending"]++;
}
}
}
$tableData = array_values($objectGroups);
// 2) CHART DATASI: haftalık object bazında sayım
$objectData = [];
foreach ($handovers as $row) {
if (!$row->created_date)
continue;
$object = $row->object ?: 'Unknown';
$createdDate = $row->created_date;
if (!isset($objectData[$object])) {
$objectData[$object] = [
'created_date' => $createdDate,
'controllers' => []
];
}
for ($i = 1; $i <= $controlCount; $i++) {
$status = $row->{"control{$i}_id_status"};
if ($status === 'Agreed / подписано') {
$objectData[$object]['controllers'][$i] = true;
}
}
}
$weekObjectCounts = [];
foreach ($objectData as $object => $data) {
$hasAccept = !empty($data['controllers']);
if ($hasAccept && $data['created_date']) {
try {
$dt = new DateTime($data['created_date']);
$week = $dt->format('o-W');
if (!isset($weekObjectCounts[$week])) {
$weekObjectCounts[$week] = ['week' => $week, 'objects' => []];
}
$weekObjectCounts[$week]['objects'][$object] = true;
} catch (\Exception $e) {
}
}
}
$chartData = [];
foreach ($weekObjectCounts as $week => $weekData) {
$chartData[] = [
'week' => $week,
'accepted_objects' => count($weekData['objects'])
];
}
usort($chartData, function ($a, $b) {
return strcmp($a['week'], $b['week']);
});
$response['tableData'] = $tableData;
$response['chartData'] = $chartData;
// ----------------------------------------------------------------
// 2) NEW DASHBOARD LOGIC (Merged)
// ----------------------------------------------------------------
// Initialize containers
$weeklyCreatedMap = [];
$weeklyPersonnelMap = [];
$weeklyArchivedMap = [];
$monthlyWaitMap = []; // Format: ['Y-m' => ['control1_sum' => days, 'control2_sum' => days, ...]]
foreach ($handovers as $row) {
// A. Weekly Created
if ($row->created_date) {
try {
$dt = new DateTime($row->created_date);
$yearWeek = $dt->format('o-W');
// Store date of week start for better labels if needed, or just year-week
// To match previous logic: format as Y-m-d start of week
$dto = new DateTime();
$dto->setISODate($dt->format('o'), $dt->format('W'));
$weekStart = $dto->format('Y-m-d');
if (!isset($weeklyCreatedMap[$weekStart]))
$weeklyCreatedMap[$weekStart] = 0;
$weeklyCreatedMap[$weekStart]++;
// B. Weekly Personnel
if ($row->responsible) {
$key = $weekStart . '_' . $row->responsible;
if (!isset($weeklyPersonnelMap[$key])) {
$weeklyPersonnelMap[$key] = [
'week' => $weekStart,
'responsible' => $row->responsible,
'count' => 0
];
}
$weeklyPersonnelMap[$key]['count']++;
}
} catch (\Exception $e) {
}
}
// C. Weekly Archived (using atg_date or final_status = 'Archive')
// Archive tarihi: atg_date varsa onu kullan, yoksa created_date kullan (final_status = 'Archive' ise)
$archiveDate = null;
if ($row->atg_date) {
$archiveDate = $row->atg_date;
} elseif ($row->final_status === 'Archive' && $row->created_date) {
$archiveDate = $row->created_date;
}
if ($archiveDate) {
try {
$dt = new DateTime($archiveDate);
$dto = new DateTime();
$dto->setISODate($dt->format('o'), $dt->format('W'));
$weekStart = $dto->format('Y-m-d');
if (!isset($weeklyArchivedMap[$weekStart]))
$weeklyArchivedMap[$weekStart] = 0;
$weeklyArchivedMap[$weekStart]++;
} catch (\Exception $e) {
}
}
// D. Wait Time Analysis - Tüm Controller'lar için
// Her control için: Date 2 - Date 1 (toplam bekleme süresi)
for ($i = 1; $i <= $controlCount; $i++) {
$date1Field = "control{$i}_date1";
$date2Field = "control{$i}_date2";
if (isset($row->$date1Field) && isset($row->$date2Field) && $row->$date1Field && $row->$date2Field) {
try {
$d1 = new DateTime($row->$date1Field);
$d2 = new DateTime($row->$date2Field);
if ($d2 > $d1) {
$month = $d1->format('Y-m');
$waitDays = $d1->diff($d2)->days;
if (!isset($monthlyWaitMap[$month])) {
$monthlyWaitMap[$month] = [];
for ($j = 1; $j <= $controlCount; $j++) {
$monthlyWaitMap[$month]["control{$j}_sum"] = 0;
}
}
$monthlyWaitMap[$month]["control{$i}_sum"] += $waitDays;
}
} catch (\Exception $e) {
// Skip invalid dates
}
}
}
}
// Format Dashboard Data
// 1. Weekly Created
$weeklyCreatedData = [];
foreach ($weeklyCreatedMap as $week => $count) {
$weeklyCreatedData[] = ['week' => $week, 'count' => $count];
}
usort($weeklyCreatedData, function ($a, $b) {
return strcmp($a['week'], $b['week']); });
$response['weeklyCreated'] = $weeklyCreatedData;
// 2. Weekly Personnel
$weeklyPersonnelData = array_values($weeklyPersonnelMap);
usort($weeklyPersonnelData, function ($a, $b) {
return strcmp($a['week'], $b['week']); });
$response['weeklyPersonnel'] = $weeklyPersonnelData;
// 3. Weekly Archived
$weeklyArchivedData = [];
foreach ($weeklyArchivedMap as $week => $count) {
$weeklyArchivedData[] = ['week' => $week, 'count' => $count];
}
usort($weeklyArchivedData, function ($a, $b) {
return strcmp($a['week'], $b['week']); });
$response['weeklyArchived'] = $weeklyArchivedData;
// 4. Wait Data - Tüm Controller'lar için toplam bekleme süresi
$waitData = [];
foreach ($monthlyWaitMap as $month => $data) {
$item = ['month' => $month];
// Her control için toplam bekleme süresi (gün cinsinden)
for ($i = 1; $i <= $controlCount; $i++) {
$key = "control{$i}_sum";
$item["control{$i}"] = isset($data[$key]) ? round($data[$key], 1) : 0;
}
$waitData[] = $item;
}
usort($waitData, function ($a, $b) {
return strcmp($a['month'], $b['month']); });
$response['waitData'] = $waitData;
echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
?>