orderBy("test_package_no", "DESC") ->where("iso_number", $data->iso_number) ->where("test_package_no", $data->test_package_no) ->get(); $repairLogs = db("repair_logs") ->where("iso_number", $data->iso_number) ->where("test_package_no", $data->test_package_no) ->get(); $supports = db("supports") ->where("line_number", $data->line_number) ->get(); // Update supports weld_or_assembled_date when welding_date is available if (!empty($data->welding_date) && !empty($data->line_number)) { db("supports") ->where("line_number", $data->line_number) ->where(function($query) use ($data) { $query->where("support_code", $data->element_code_1) ->orWhere("support_code", $data->element_code_2); }) ->update([ "weld_or_assembled_date" => $data->welding_date ]); } $punchLists = PunchList::where("line_isometric_no", $data->line_number) ->where("test_package", $data->test_package_no) ->get(); $logTypes = array_keys(log_test_types()); $testTypeToMedium = [ 'PNEUMATIC' => 'Air', 'HYDRAULIC' => 'Water', 'VISUAL' => '-', 'ACUISTIC' => '-', ]; // Delete empty test packages TestPackage::orWhere("test_package_number", "")->orWhereNull("test_package_number")->delete(); TestPackBaseStatus::orWhere("test_package_no", "")->orWhereNull("test_package_no")->delete(); $weldLogTestPackCount = 0; // Calculate support statistics $supportStats = $this->calculateSupportStats($supports); // Calculate punch list statistics $punchListStats = $this->calculatePunchListStats($punchLists); // Calculate weld log statistics $weldLogStats = $this->calculateWeldLogStats($weldLogs, $logTypes); // Get NDT calculations // Temporarily set $_GET['type'] for the view's getEsit() helper $originalGetType = $_GET['type'] ?? null; $_GET['type'] = 'test_packs'; try { $ndtCalculation = j(view('admin-ajax.ndt-calculation-no-cache', [ 'test_package_no' => $data->test_package_no, 'iso_number2' => $data->iso_number, 'type' => 'test_packs', ])->render()); } finally { // Restore original $_GET['type'] if ($originalGetType !== null) { $_GET['type'] = $originalGetType; } else { unset($_GET['type']); } } $tpStats = $ndtCalculation['tpStats'] ?? []; $tpIsoStats = $ndtCalculation['tpISOStats'] ?? []; // Process Test Package data in chunks $this->processTestPackageData( $weldLogs, $punchListStats, $weldLogStats, $tpStats, $tpIsoStats, $supportStats, $testTypeToMedium, $weldLogTestPackCount ); // Sync weld logs to test packages for this specific test package try { if(isset($data->test_package_no) && $data->test_package_no != "") { $syncParams = [ 'test_package_no' => $data->test_package_no ]; $syncResult = view('cron.weld_logs-sync-from-weldlog-to-test-pack', $syncParams)->render(); Log::debug("Weld logs sync completed for test package: " . $data->test_package_no); } } catch (\Throwable $th) { Log::error("Weld logs sync failed for test package: " . $data->test_package_no . " - " . $th->getMessage()); } return [ 'success' => true, 'processed_records' => $weldLogTestPackCount ]; } /** * Calculate support statistics */ protected function calculateSupportStats($supports): array { $supportStats = []; $supportStatsTP = []; $isoToTP = []; foreach($supports AS $support) { $tpNo = @$isoToTP[$support->line_number]; if(!isset($supportStatsTP[$tpNo]['support_remaining'])) { $supportStatsTP[$tpNo]['support_remaining'] = 0; $supportStatsTP[$tpNo]['welded_support_quantity'] = 0; $supportStatsTP[$tpNo]['support_progress'] = 0; } if(!isset($supportStats[$support->line_number]['support_remaining'])) $supportStats[$support->line_number]['support_remaining'] = 0; if(!isset($supportStats[$support->line_number]['welded_support_quantity'])) $supportStats[$support->line_number]['welded_support_quantity'] = 0; if(!isset($supportStats[$support->line_number]['support_progress'])) $supportStats[$support->line_number]['support_progress'] = 0; if(!rejected_date($support->weld_or_assembled_date) && $support->erection_type == "Weld") { $supportStats[$support->line_number]['welded_support_quantity']+= $support->quantity; $supportStatsTP[$tpNo]['welded_support_quantity']+= $support->quantity; } else { $supportStats[$support->line_number]['support_remaining']+= $support->quantity; $supportStatsTP[$tpNo]['support_remaining']+= $support->quantity; } if($supportStats[$support->line_number]['welded_support_quantity'] > 0) { $supportStats[$support->line_number]['support_progress'] = 100 - ($supportStats[$support->line_number]['support_remaining'] * 100) / ($supportStats[$support->line_number]['welded_support_quantity'] + $supportStats[$support->line_number]['support_remaining']); } else { $supportStats[$support->line_number]['support_progress'] = 0; } if($supportStatsTP[$tpNo]['welded_support_quantity'] > 0) { $supportStatsTP[$tpNo]['support_progress'] = 100 - ($supportStatsTP[$tpNo]['support_remaining'] * 100) / ($supportStatsTP[$tpNo]['welded_support_quantity'] + $supportStatsTP[$tpNo]['support_remaining']); } else { $supportStatsTP[$tpNo]['support_progress'] = 0; } } return ['supportStats' => $supportStats, 'supportStatsTP' => $supportStatsTP]; } /** * Calculate punch list statistics */ protected function calculatePunchListStats($punchLists): array { $stats = []; $punchListNumbers = []; foreach($punchLists AS $punchList) { $drawingNo = $punchList->line_isometric_no; if(!isset($stats[$drawingNo][$punchList->test_package]['punch_a_quantity'])) $stats[$drawingNo][$punchList->test_package]['punch_a_quantity'] = 0; if(!isset($stats[$drawingNo][$punchList->test_package]['punch_b_quantity'])) $stats[$drawingNo][$punchList->test_package]['punch_b_quantity'] = 0; if(!isset($stats[$drawingNo][$punchList->test_package]['punch_c_quantity'])) $stats[$drawingNo][$punchList->test_package]['punch_c_quantity'] = 0; if(!isset($punchListNumbers[$punchList->test_package])) $punchListNumbers[$punchList->test_package] = []; if(!in_array($punchList->punch_list_no, $punchListNumbers[$punchList->test_package])) $punchListNumbers[$punchList->test_package][] = $punchList->punch_list_no; if(!isset($stats[$punchList->test_package]['punch_a_open_quantity'])) $stats[$punchList->test_package]['punch_a_open_quantity'] = 0; if(!isset($stats[$punchList->test_package]['punch_b_open_quantity'])) $stats[$punchList->test_package]['punch_b_open_quantity'] = 0; if(!isset($stats[$punchList->test_package]['punch_c_open_quantity'])) $stats[$punchList->test_package]['punch_c_open_quantity'] = 0; if($punchList->category == "A") { if($punchList->status == "Open") { $stats[$punchList->test_package]['punch_a_open_quantity']++; } $stats[$drawingNo][$punchList->test_package]['punch_a_quantity']++; } if($punchList->category == "B") { if($punchList->status == "Open") { $stats[$punchList->test_package]['punch_b_open_quantity']++; } $stats[$drawingNo][$punchList->test_package]['punch_b_quantity']++; } if($punchList->category == "C") { if($punchList->status == "Open") { $stats[$punchList->test_package]['punch_c_open_quantity']++; } $stats[$drawingNo][$punchList->test_package]['punch_c_quantity']++; } $stats[$punchList->test_package]['found_date'] = $punchList->found_date; $stats[$punchList->test_package]['punch_list'] = $punchList->punch_list; } return ['stats' => $stats, 'punchListNumbers' => $punchListNumbers]; } /** * Calculate weld log statistics including golden joints, WDI, and backlogs */ protected function calculateWeldLogStats($weldLogs, $logTypes): array { $stats = []; $quantity_of_iso = []; $addedSheet = []; foreach($weldLogs AS $weldLog) { $jointNo = $weldLog->no_of_the_joint_as_per_as_built_survey; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['goldenJoints'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['goldenJoints'] = 0; if(!isset($quantity_of_iso[$weldLog->test_package_no]['total_qty'])) $quantity_of_iso[$weldLog->test_package_no]['total_qty'] = 0; if(!isset($addedSheet[$weldLog->test_package_no])) $addedSheet[$weldLog->test_package_no] = []; if(!in_array($weldLog->quantity_of_iso, $addedSheet[$weldLog->test_package_no])) { $quantity_of_iso[$weldLog->test_package_no]['total_qty']++; $addedSheet[$weldLog->test_package_no][] = $weldLog->quantity_of_iso; } if(!isset($quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_qty'])) $quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_qty'] = 0; if(!isset($addedSheet[$weldLog->iso_number][$weldLog->test_package_no])) $addedSheet[$weldLog->iso_number][$weldLog->test_package_no] = []; if(!in_array($weldLog->quantity_of_iso, $addedSheet[$weldLog->iso_number][$weldLog->test_package_no])) { $quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_qty']++; $addedSheet[$weldLog->iso_number][$weldLog->test_package_no][] = $weldLog->quantity_of_iso; } if(!isset($stats[$weldLog->test_package_no]['goldenJoints'])) $stats[$weldLog->test_package_no]['goldenJoints'] = 0; if(strpos(strtoupper($jointNo), "GJ") !== false) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['goldenJoints']++; $stats[$weldLog->test_package_no]['goldenJoints']++; } if(!isset($quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_iso_quantity'])) $quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_iso_quantity'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['rt_ut_status'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['rt_ut_status'] = 0; $backlogs = [ 'rt_backlog', 'ut_backlog', 'mt_backlog', 'pmi_backlog', 'vt_backlog', 'pt_backlog', 'ferrit_backlog', 'pwht_backlog', 'total_backlog', ]; foreach($backlogs AS $backlog) { if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no][$backlog])) $stats[$weldLog->iso_number][$weldLog->test_package_no][$backlog] = 0; if(!isset($stats[$weldLog->test_package_no][$backlog])) $stats[$weldLog->test_package_no][$backlog] = 0; } if(!isset($stats[$weldLog->test_package_no]['total_backlog'])) $stats[$weldLog->test_package_no]['total_backlog'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['rt_ut_done'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['rt_ut_done'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['mt_pt_backlog'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['mt_pt_backlog'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['mt_pt_done'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['mt_pt_done'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['remaining_wdi'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['remaining_wdi'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['total_wdi'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_wdi'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['total_complated_wdi'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_complated_wdi'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['total_joint_qty'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_joint_qty'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['total_welded_joint_qty'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_welded_joint_qty'] = 0; if(!isset($stats[$weldLog->iso_number][$weldLog->test_package_no]['welded_support_quantity'])) $stats[$weldLog->iso_number][$weldLog->test_package_no]['welded_support_quantity'] = 0; foreach($logTypes AS $logType) { $weldLogArray = (Array) $weldLog; $stats[$weldLog->test_package_no]['total_backlog'] += (float) $weldLogArray[$logType . "_scope"]; } if(!isset($addedSheet[$weldLog->iso_number][$weldLog->test_package_no]['total_iso'])) $addedSheet[$weldLog->iso_number][$weldLog->test_package_no]['total_iso'] = []; if(!in_array($weldLog->quantity_of_iso, $addedSheet[$weldLog->iso_number][$weldLog->test_package_no]['total_iso'])) { $quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_iso_quantity']++; $addedSheet[$weldLog->iso_number][$weldLog->test_package_no]['total_iso'][] = $weldLog->quantity_of_iso; } if(rejected_date($weldLog->welding_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['remaining_wdi'] += (float) $weldLog->nps_1; } // Backlog calculations for each test type if(rejected_date($weldLog->rt_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['rt_backlog']++; $stats[$weldLog->test_package_no]['rt_backlog']++; } if(rejected_date($weldLog->ut_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['ut_backlog']++; $stats[$weldLog->test_package_no]['ut_backlog']++; } if(rejected_date($weldLog->pwht_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['pwht_backlog']++; $stats[$weldLog->test_package_no]['pwht_backlog']++; } if(rejected_date($weldLog->pmi_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['pmi_backlog']++; $stats[$weldLog->test_package_no]['pmi_backlog']++; } if(rejected_date($weldLog->ferrite_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['ferrit_backlog']++; $stats[$weldLog->test_package_no]['ferrit_backlog']++; } if(rejected_date($weldLog->pt_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['pt_backlog']++; $stats[$weldLog->test_package_no]['pt_backlog']++; } if(rejected_date($weldLog->mt_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['mt_backlog']++; $stats[$weldLog->test_package_no]['mt_backlog']++; } if(rejected_date($weldLog->vt_test_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['vt_backlog']++; $stats[$weldLog->test_package_no]['vt_backlog']++; } if(!rejected_date($weldLog->welding_date)) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_complated_wdi'] += (float) $weldLog->nps_1; $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_welded_joint_qty']++; } $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_wdi'] += (float) $weldLog->nps_1; // Welding progress calculation - ISO based stats $totalWdi = $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_wdi']; $totalComplatedWdi = $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_complated_wdi']; if ($totalWdi > 0) { $stats[$weldLog->iso_number][$weldLog->test_package_no]['welding_progress'] = round(($totalComplatedWdi * 100) / $totalWdi, 2); } else { $stats[$weldLog->iso_number][$weldLog->test_package_no]['welding_progress'] = 0; } $stats[$weldLog->iso_number][$weldLog->test_package_no]['total_joint_qty']++; } return ['stats' => $stats, 'quantity_of_iso' => $quantity_of_iso]; } /** * Process and update test package data */ protected function processTestPackageData( $weldLogs, $punchListStats, $weldLogStats, $tpStats, $tpIsoStats, $supportStats, $testTypeToMedium, &$weldLogTestPackCount ) { $punchListNumbers = $punchListStats['punchListNumbers']; $stats = array_merge_recursive($punchListStats['stats'], $weldLogStats['stats']); $quantity_of_iso = $weldLogStats['quantity_of_iso']; $supportStatsData = $supportStats['supportStats']; $supportStatsTP = $supportStats['supportStatsTP']; // Process Test Package in chunks with TransactionHelper TransactionHelper::chunkTransaction( $weldLogs, function ($weldLogChunk) use ($punchListNumbers, $stats, $tpStats, $tpIsoStats, $supportStatsTP, $quantity_of_iso, $testTypeToMedium, $supportStatsData, &$weldLogTestPackCount) { foreach($weldLogChunk as $weldLog) { try { $punchListData = implode(",", $punchListNumbers[$weldLog->test_package_no] ?? []); } catch (\Throwable $th) { $punchListData = ""; } $thisStats = @$stats[$weldLog->test_package_no]; $thisTpStats = @$tpStats[$weldLog->test_package_no]; $thisTpISOStats = @$tpIsoStats[$weldLog->test_package_no][$weldLog->iso_number]; $thisSupportStats = @$supportStatsTP[$weldLog->test_package_no]; $thisTotalBacklog = (@$thisTpStats['vt'] ?? 0) + (@$thisTpStats['ut'] ?? 0) + (@$thisTpStats['mt'] ?? 0) + (@$thisTpStats['pmi'] ?? 0) + (@$thisTpStats['pt'] ?? 0) + (@$thisTpStats['rt'] ?? 0) + (@$thisTpStats['pwht'] ?? 0) + (@$thisTpStats['ferrite'] ?? 0); $testPackageData = [ 'unit' => $weldLog->design_area, 'test_package_number' => $weldLog->test_package_no, 'discipline' => $weldLog->piping_type, 'circuit_number' => $weldLog->circuit_number, 'p_id' => $weldLog->p_id, 'iso_quantity' => $quantity_of_iso[$weldLog->test_package_no]['total_qty'], 'test_type' => $weldLog->type_of_test, 'test_medium' => @$testTypeToMedium[strtoupper($weldLog->type_of_test)], 'test_pressure' => $weldLog->test_pressure, 'golden_joints' => @$stats[$weldLog->iso_number][$weldLog->test_package_no]['goldenJoints'], 'walkdown_date' => @$stats[$weldLog->iso_number][$weldLog->test_package_no]['found_date'], 'punch_list' => $punchListData, 'welded_support_quantity' => @$thisSupportStats['welded_support_quantity'], 'support_remaining' => @$thisSupportStats['support_remaining'], 'support_progress' => @$thisSupportStats['support_progress'], 'a_punch_point_open' => @$stats[$weldLog->iso_number][$weldLog->test_package_no]['punch_a_open_quantity'], 'b_punch_point_open' => @$stats[$weldLog->iso_number][$weldLog->test_package_no]['punch_b_open_quantity'], 'c_punch_point_open' => @$stats[$weldLog->iso_number][$weldLog->test_package_no]['punch_c_open_quantity'], 'vt_backlog' => @$thisTpStats['vt'], 'ut_backlog' => @$thisTpStats['ut'], 'mt_backlog' => @$thisTpStats['mt'], 'pmi_backlog' => @$thisTpStats['pmi'], 'pt_backlog' => @$thisTpStats['pt'], 'rt_backlog' => @$thisTpStats['rt'], 'pwht_backlog' => @$thisTpStats['pwht'], 'ferrit_backlog' => @$thisTpStats['ferrite'], 'ndt_status' => @$thisTotalBacklog == 0 ? "Completed" : "Not Completed", 'welding_progress' => @$stats[$weldLog->iso_number][$weldLog->test_package_no]['welding_progress'], ]; $thisStats = @$stats[$weldLog->iso_number][$weldLog->test_package_no]; $thisSupportStats = @$supportStatsData[$weldLog->iso_number]; try { $pipeProgress = round($thisStats['total_complated_wdi'] * 100 / $thisStats['total_wdi'], 2); } catch (\Throwable $th) { $pipeProgress = 0; } $testPackBaseStatusData = [ 'area' => $weldLog->project, 'drawing_no' => $weldLog->iso_number, 'test_package_no' => $weldLog->test_package_no, 'discipline' => $weldLog->piping_type, 'iso_qty' => $quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_qty'], 'test_type' => $weldLog->type_of_test, 'test_medium' => @$testTypeToMedium[strtoupper($weldLog->type_of_test)], 'test_pressure_mpa' => $weldLog->test_pressure, 'tp_status' => '', // Will be filled from test pack base stat 'total_iso_quantity' => $quantity_of_iso[$weldLog->iso_number][$weldLog->test_package_no]['total_iso_quantity'], 'remaining_wdi' => $thisStats['remaining_wdi'], 'total_wdi' => $thisStats['total_wdi'], 'total_complated_wdi' => $thisStats['total_complated_wdi'], 'pipe_progress' => $pipeProgress, 'total_joint_qty' => $thisStats['total_joint_qty'], 'total_welded_joint_qty' => $thisStats['total_welded_joint_qty'], 'welded_support_quantity' => @$thisSupportStats['welded_support_quantity'], 'support_remaining' => @$thisSupportStats['support_remaining'], 'support_progress' => @$thisSupportStats['support_progress'], 'punch_a_quantity' => @$thisStats['punch_a_quantity'], 'punch_b_quantity' => @$thisStats['punch_b_quantity'], 'punch_c_quantity' => @$thisStats['punch_c_quantity'], 'rt_ut_status' => $thisStats['rt_ut_status'], 'rt_backlog' => @$thisTpISOStats['rt'], 'ut_backlog' => @$thisTpISOStats['ut'], 'mt_backlog' => @$thisTpISOStats['mt'], 'pmi_backlog' => @$thisTpISOStats['pmi'], 'vt_backlog' => @$thisTpISOStats['vt'], 'pt_backlog' => @$thisTpISOStats['pt'], 'pwht_backlog' => @$thisTpISOStats['pwht'], 'ferrit_backlog' => @$thisTpISOStats['ferrite'], ]; $testPackWhereData = [ 'test_package_number' => $weldLog->test_package_no ]; $testPackBaseStatusWhereData = [ 'test_package_no' => $weldLog->test_package_no, 'drawing_no' => $weldLog->iso_number, ]; if($weldLog->test_package_no != "" && !is_null($weldLog->test_package_no)) { $testPackResult = TestPackage::updateOrCreate($testPackWhereData, $testPackageData); $testPackBaseResult = TestPackBaseStatus::updateOrCreate($testPackBaseStatusWhereData, $testPackBaseStatusData); $weldLogTestPackCount++; } } return $weldLogChunk->count(); }, 50, // Chunk size 1000 // 1ms delay ); } }