140 lines
5.9 KiB
PHP
140 lines
5.9 KiB
PHP
<?php
|
||
|
||
use App\Models\PunchList;
|
||
use App\Services\Dashboards\TestPackageDashboardService;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
$projectMap = TestPackageDashboardService::projectMap();
|
||
|
||
// Line/ISO number üzerinden project ve design_area mapping
|
||
$lineProjectMap = DB::table('weld_logs')
|
||
->select('line_number', DB::raw('MAX(project) as project'), DB::raw('MAX(design_area) as design_area'))
|
||
->whereNotNull('line_number')
|
||
->where('line_number', '!=', '')
|
||
->whereNotNull('project')
|
||
->groupBy('line_number')
|
||
->get()
|
||
->mapWithKeys(function($item) {
|
||
return [$item->line_number => ['project' => $item->project, 'design_area' => $item->design_area]];
|
||
})
|
||
->toArray();
|
||
|
||
$isoProjectMap = DB::table('weld_logs')
|
||
->select('iso_number', DB::raw('MAX(project) as project'), DB::raw('MAX(design_area) as design_area'))
|
||
->whereNotNull('iso_number')
|
||
->where('iso_number', '!=', '')
|
||
->whereNotNull('project')
|
||
->groupBy('iso_number')
|
||
->get()
|
||
->mapWithKeys(function($item) {
|
||
return [$item->iso_number => ['project' => $item->project, 'design_area' => $item->design_area]];
|
||
})
|
||
->toArray();
|
||
|
||
// test_package_no üzerinden project ve design_area mapping (tek sorguda tümünü yükle)
|
||
$testPackageProjectMap = [];
|
||
$punchListsTemp = PunchList::get();
|
||
$testPackageNos = $punchListsTemp->pluck('test_package')->filter()->unique()->toArray();
|
||
if (!empty($testPackageNos)) {
|
||
$testPackageProjectMap = DB::table('weld_logs')
|
||
->select('test_package_no', DB::raw('MAX(project) as project'), DB::raw('MAX(design_area) as design_area'))
|
||
->whereIn('test_package_no', $testPackageNos)
|
||
->whereNotNull('project')
|
||
->groupBy('test_package_no')
|
||
->get()
|
||
->mapWithKeys(function($item) {
|
||
return [$item->test_package_no => ['project' => $item->project, 'design_area' => $item->design_area]];
|
||
})
|
||
->toArray();
|
||
}
|
||
|
||
$punchLists = $punchListsTemp->map(function ($item) use ($projectMap, $lineProjectMap, $isoProjectMap, $testPackageProjectMap) {
|
||
$project = 'Unknown';
|
||
$designArea = null;
|
||
|
||
// 1. Önce test_package üzerinden project ve design_area bul
|
||
if (!empty($item->test_package)) {
|
||
if (isset($testPackageProjectMap[$item->test_package])) {
|
||
$project = trim($testPackageProjectMap[$item->test_package]['project']);
|
||
$designArea = trim($testPackageProjectMap[$item->test_package]['design_area'] ?? '');
|
||
} elseif (isset($projectMap[$item->test_package])) {
|
||
$project = trim($projectMap[$item->test_package]);
|
||
}
|
||
}
|
||
|
||
// 2. Eğer hala project bulunamadıysa line_isometric_no üzerinden bul
|
||
if ($project === 'Unknown' && !empty($item->line_isometric_no)) {
|
||
if (isset($lineProjectMap[$item->line_isometric_no])) {
|
||
$project = trim($lineProjectMap[$item->line_isometric_no]['project']);
|
||
$designArea = trim($lineProjectMap[$item->line_isometric_no]['design_area'] ?? '');
|
||
} elseif (isset($isoProjectMap[$item->line_isometric_no])) {
|
||
$project = trim($isoProjectMap[$item->line_isometric_no]['project']);
|
||
$designArea = trim($isoProjectMap[$item->line_isometric_no]['design_area'] ?? '');
|
||
}
|
||
}
|
||
|
||
$projectRaw = $project ?: 'Unknown';
|
||
$designAreaRaw = $designArea ?: 'Unknown';
|
||
|
||
// Create normalized display values (ucwords for title case - each word capitalized)
|
||
$project = ($projectRaw === 'Unknown' || $projectRaw === '') ? 'Unknown' : ucwords(strtolower($projectRaw));
|
||
$designArea = ($designAreaRaw === 'Unknown' || $designAreaRaw === '') ? 'Unknown' : ucwords(strtolower($designAreaRaw));
|
||
|
||
$item->project = $project;
|
||
$item->design_area = $designArea;
|
||
|
||
// Category alanını normalize et - sadece A, B, C değerlerini kabul et
|
||
$category = null;
|
||
if (!empty($item->category)) {
|
||
$category = strtoupper(trim($item->category));
|
||
// Eğer category "A-B" gibi birleşik bir değerse, sadece ilk harfi al
|
||
if (strlen($category) > 1 && strpos($category, '-') !== false) {
|
||
$categoryParts = explode('-', $category);
|
||
$category = strtoupper(trim($categoryParts[0]));
|
||
}
|
||
// Sadece A, B, C değerlerini kabul et (D ve E'yi de kabul ediyoruz ama sadece A, B, C gösterilecek)
|
||
if (in_array($category, ['A', 'B', 'C'])) {
|
||
$item->category = $category;
|
||
} else {
|
||
// Geçersiz category değerlerini null yap
|
||
$item->category = null;
|
||
}
|
||
} else {
|
||
$item->category = null;
|
||
}
|
||
|
||
// Status alanını normalize et - sadece Open, Closed, Canceled değerlerini kabul et
|
||
if (!empty($item->status)) {
|
||
$status = ucfirst(strtolower(trim($item->status)));
|
||
// Sadece Open, Closed, Canceled değerlerini kabul et
|
||
if (in_array($status, ['Open', 'Closed', 'Canceled'])) {
|
||
$item->status = $status;
|
||
} else {
|
||
// Geçersiz status değerlerini null yap
|
||
$item->status = null;
|
||
}
|
||
} else {
|
||
$item->status = null;
|
||
}
|
||
|
||
// Discipline alanını normalize et
|
||
if (!empty($item->discipline)) {
|
||
$disciplineRaw = trim($item->discipline);
|
||
$item->discipline = ($disciplineRaw === 'Unknown' || $disciplineRaw === '') ? 'Unknown' : ucwords(strtolower($disciplineRaw));
|
||
} else {
|
||
$item->discipline = 'Unknown';
|
||
}
|
||
|
||
// Area alanını kaldır, sadece design_area kullanılacak
|
||
unset($item->area);
|
||
|
||
return $item;
|
||
})->filter(function ($item) {
|
||
// Null category veya status'lu kayıtları filtrele
|
||
// Sadece geçerli category (A, B, C) ve geçerli status (Open, Closed, Canceled) olan kayıtları döndür
|
||
$hasValidCategory = !empty($item->category) && in_array($item->category, ['A', 'B', 'C']);
|
||
$hasValidStatus = !empty($item->status) && in_array($item->status, ['Open', 'Closed', 'Canceled']);
|
||
return $hasValidCategory && $hasValidStatus;
|
||
})->values();
|
||
|
||
echo json_encode_tr($punchLists); |