test_package_no; $lineNumber = $data->line_number; $testPackages = TestPackage::where("test_package_number", $testPackageNo)->get(); $testPackagesIso = TestPackBaseStatus::where("test_package_no", $testPackageNo)->get(); $weldLogs = db("weld_logs") ->where("test_package_no", $testPackageNo) ->get(); $repairLogs = db("repair_logs") ->where("test_package_no", $testPackageNo) ->get(); $testPackagesSummary = []; $testPackagesIsoSummary = []; $totalFields = [ 'total_wdi', 'total_complated_wdi', 'welding_progress', 'total_shop_wdi', 'total_complated_shop_wdi', 'total_field_wdi', 'total_complated_field_wdi', ]; // Calculate repair log summaries $repairSummaries = $this->calculateRepairSummaries($repairLogs); // Calculate weld log summaries foreach($weldLogs AS $weldLog) { foreach($totalFields AS $field) { if(!isset($testPackagesSummary[$weldLog->test_package_no][$field])) $testPackagesSummary[$weldLog->test_package_no][$field] = 0; if(!isset($testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no][$field])) $testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no][$field] = 0; } $wdi = (float) $weldLog->nps_1; $testPackagesSummary[$weldLog->test_package_no]['total_wdi'] += $wdi ; $testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no]['total_wdi'] += $wdi; if($weldLog->type_of_joint == "F") { $testPackagesSummary[$weldLog->test_package_no]['total_field_wdi'] += $wdi; $testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no]['total_field_wdi'] += $wdi ; if(!rejected_date($weldLog->welding_date)) { $testPackagesSummary[$weldLog->test_package_no]['total_complated_field_wdi'] += $wdi; $testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no]['total_complated_field_wdi'] += $wdi; } } if($weldLog->type_of_joint == "S") { $testPackagesSummary[$weldLog->test_package_no]['total_shop_wdi'] += $wdi; $testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no]['total_shop_wdi'] += $wdi; if(!rejected_date($weldLog->welding_date)) { $testPackagesSummary[$weldLog->test_package_no]['total_complated_shop_wdi'] += $wdi; $testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no]['total_complated_shop_wdi'] += $wdi; } } if(!rejected_date($weldLog->welding_date)) { $testPackagesSummary[$weldLog->test_package_no]['total_complated_wdi'] += $wdi; $testPackagesIsoSummary[$weldLog->iso_number][$weldLog->test_package_no]['total_complated_wdi'] += $wdi; } // Welding progress calculation $totalWdi = $testPackagesSummary[$weldLog->test_package_no]['total_wdi']; $totalComplatedWdi = $testPackagesSummary[$weldLog->test_package_no]['total_complated_wdi']; if ($totalWdi > 0) { $testPackagesSummary[$weldLog->test_package_no]['welding_progress'] = round(($totalComplatedWdi * 100) / $totalWdi, 2); } else { $testPackagesSummary[$weldLog->test_package_no]['welding_progress'] = 0; } } // Update test pack base statuses $this->updateTestPackBaseStatuses($testPackagesIsoSummary, $repairSummaries); // Update test packages summary $this->updateTestPackagesSummary($testPackagesSummary, $repairSummaries['repairLogsSummary2']); // Update test packages with WDI fields and status $processedCount = $this->updateTestPackagesWDI($testPackages, $testPackagesSummary, $totalFields); return [ 'success' => true, 'processed_records' => $processedCount ]; } /** * Calculate repair log summaries */ protected function calculateRepairSummaries($repairLogs): array { $repairLogsSummary = []; $repairLogsSummary2 = []; $repairLogsSummaryCompleted = []; $repairLogsSummaryCompleted2 = []; $repairLogsSummaryRemaining = []; $repairLogsSummaryRemaining2 = []; foreach($repairLogs AS $repairLog) { if(!isset($repairLogsSummary[$repairLog->iso_number][$repairLog->test_package_no])) $repairLogsSummary[$repairLog->iso_number][$repairLog->test_package_no] = 0; if(!isset($repairLogsSummary2[$repairLog->test_package_no])) $repairLogsSummary2[$repairLog->test_package_no] = 0; if(!isset($repairLogsSummaryCompleted[$repairLog->iso_number][$repairLog->test_package_no])) $repairLogsSummaryCompleted[$repairLog->iso_number][$repairLog->test_package_no] = 0; if(!isset($repairLogsSummaryRemaining[$repairLog->iso_number][$repairLog->test_package_no])) $repairLogsSummaryRemaining[$repairLog->iso_number][$repairLog->test_package_no] = 0; if(!isset($repairLogsSummaryCompleted2[$repairLog->test_package_no])) $repairLogsSummaryCompleted2[$repairLog->test_package_no] = 0; if(!isset($repairLogsSummaryRemaining2[$repairLog->test_package_no])) $repairLogsSummaryRemaining2[$repairLog->test_package_no] = 0; if($repairLog->repair_status == "Not Done") { $repairLogsSummary[$repairLog->iso_number][$repairLog->test_package_no]++; $repairLogsSummary2[$repairLog->test_package_no]++; } if(!rejected_date($repairLog->repair_date)) { $repairLogsSummaryCompleted[$repairLog->iso_number][$repairLog->test_package_no]++; $repairLogsSummaryCompleted2[$repairLog->test_package_no]++; } else { $repairLogsSummaryRemaining[$repairLog->iso_number][$repairLog->test_package_no]++; $repairLogsSummaryRemaining2[$repairLog->test_package_no]++; } } return [ 'repairLogsSummary' => $repairLogsSummary, 'repairLogsSummary2' => $repairLogsSummary2, 'repairLogsSummaryCompleted' => $repairLogsSummaryCompleted, 'repairLogsSummaryCompleted2' => $repairLogsSummaryCompleted2, 'repairLogsSummaryRemaining' => $repairLogsSummaryRemaining, 'repairLogsSummaryRemaining2' => $repairLogsSummaryRemaining2, ]; } /** * Update test pack base statuses */ protected function updateTestPackBaseStatuses($testPackagesIsoSummary, $repairSummaries) { TransactionHelper::retryTransaction(function () use ($testPackagesIsoSummary, $repairSummaries) { foreach($testPackagesIsoSummary AS $isoNumber => $data) { foreach($data AS $tpNo => $data2) { if($data2['total_complated_wdi'] == $data2['total_wdi']) { $status = "Completed"; } if($data2['total_complated_wdi'] < $data2['total_wdi']) { $status = "On Going"; } if($data2['total_complated_wdi'] == 0) { $status = "Waiting"; } $updateData = [ 'welding_status' => $status, 'repair_qty' => @$repairSummaries['repairLogsSummary'][$isoNumber][$tpNo], 'repair_completed' => @$repairSummaries['repairLogsSummaryCompleted'][$isoNumber][$tpNo], 'repair_remaining' => @$repairSummaries['repairLogsSummaryRemaining'][$isoNumber][$tpNo], ]; db("test_pack_base_statuses") ->where("drawing_no", $isoNumber) ->where("test_package_no", $tpNo) ->update($updateData); $updateData = [ 'welding_status' => $status, 'repair_qty' => @$repairSummaries['repairLogsSummary2'][$tpNo], 'repair_completed' => @$repairSummaries['repairLogsSummaryCompleted2'][$tpNo], 'repair_remaining' => @$repairSummaries['repairLogsSummaryRemaining2'][$tpNo], ]; db("test_packages") ->where("test_package_number", $tpNo) ->update($updateData); } } }, 5); } /** * Update test packages summary */ protected function updateTestPackagesSummary($testPackagesSummary, $repairLogsSummary2) { TransactionHelper::retryTransaction(function () use ($testPackagesSummary, $repairLogsSummary2) { foreach($testPackagesSummary AS $tpNo => $data2) { if($data2['total_complated_wdi'] == $data2['total_wdi']) { $status = "Completed"; } if($data2['total_complated_wdi'] < $data2['total_wdi']) { $status = "On Going"; } if($data2['total_complated_wdi'] == 0) { $status = "Waiting"; } db("test_packages") ->where("test_package_number", $tpNo) ->update([ 'welding_status' => $status, 'repair_status_total' => @$repairLogsSummary2[$tpNo] ]); } }, 5); } /** * Update test packages with WDI fields and status */ protected function updateTestPackagesWDI($testPackages, $testPackagesSummary, $totalFields): int { $k = 0; TransactionHelper::retryTransaction(function () use ($testPackages, $testPackagesSummary, $totalFields, &$k) { foreach($testPackages AS $testPackage) { $updateData = []; if(isset($testPackagesSummary[$testPackage->test_package_number])) { foreach($totalFields AS $field) { $testPackage->$field = $testPackagesSummary[$testPackage->test_package_number][$field]; $updateData[$field] = $testPackagesSummary[$testPackage->test_package_number][$field]; } } $updateData = array_merge($updateData, updateTestPackageStatus($testPackage)); db("test_packages")->where("id", $testPackage->id) ->update($updateData); db("test_pack_base_statuses")->where("test_package_no", $testPackage->test_package_number) ->update([ 'tp_status' => $updateData['status'] ?? 'Waiting', 'priority' => $testPackage->priority, 'priority_info' => $testPackage->priority_info, 'responsible_person' => $testPackage->responsible_test, 'target_test_date' => $testPackage->planned_test_date, ]); $k++; } return $k; }, 5); return $k; } }