248 lines
7.2 KiB
PHP
248 lines
7.2 KiB
PHP
<?php
|
|
use Carbon\Carbon;
|
|
|
|
// Increase memory and time limits for heavy queries
|
|
ini_set('memory_limit', '1024M');
|
|
set_time_limit(300);
|
|
|
|
$bugun = Carbon::now();
|
|
$totalWelderWdi = [];
|
|
$totalWelderJoint = [];
|
|
$todayTotalWelderWdi = [];
|
|
$todayTotalWelderJoint = [];
|
|
$dailyTotalWdi = [];
|
|
$dailyAverage = [];
|
|
|
|
$dailyShopTotalWdi = [];
|
|
$dailyShopAverage = [];
|
|
$dailyFieldTotalWdi = [];
|
|
$dailyFieldAverage = [];
|
|
$weldlogWelders = [];
|
|
$weldLogs = apply_welded_filter(
|
|
db("weld_logs")
|
|
->whereNotNull('welding_date')
|
|
->whereNotNull('real_welder_1')
|
|
)->get();
|
|
foreach($weldLogs AS $weldLog) {
|
|
$welderId = get_welder_id($weldLog->real_welder_1);
|
|
if(!$welderId) continue;
|
|
|
|
$date = $weldLog->welding_date;
|
|
$nps = (float) $weldLog->nps_1;
|
|
|
|
if(!in_array($welderId, $weldlogWelders)) {
|
|
$weldlogWelders[] = $welderId;
|
|
}
|
|
|
|
|
|
|
|
if(!isset($totalWelderWdi[$welderId])) $totalWelderWdi[$welderId] = 0;
|
|
if(!isset($totalWelderJoint[$welderId])) $totalWelderJoint[$welderId] = 0;
|
|
if(!isset($todayTotalWelderWdi[$welderId])) $todayTotalWelderWdi[$welderId] = 0;
|
|
if(!isset($todayTotalWelderJoint[$welderId])) $todayTotalWelderJoint[$welderId] = 0;
|
|
if(!isset($dailyTotalWdi[$welderId][$date])) $dailyTotalWdi[$welderId][$date] = 0;
|
|
if(!isset($dailyShopTotalWdi[$welderId][$date])) $dailyShopTotalWdi[$welderId][$date] = 0;
|
|
if(!isset($dailyFieldTotalWdi[$welderId][$date])) $dailyFieldTotalWdi[$welderId][$date] = 0;
|
|
|
|
$totalWelderWdi[$welderId] += $nps;
|
|
$totalWelderJoint[$welderId]++;
|
|
|
|
if($weldLog->welding_date) {
|
|
try {
|
|
$weldingDate = Carbon::parse($weldLog->welding_date);
|
|
if ($weldingDate->isToday()) {
|
|
$todayTotalWelderWdi[$welderId] += $nps;
|
|
$todayTotalWelderJoint[$welderId]++;
|
|
}
|
|
} catch (\Throwable $th) {
|
|
// Skip invalid dates
|
|
}
|
|
}
|
|
|
|
|
|
if($weldLog->type_of_joint == "S") {
|
|
$dailyShopTotalWdi[$welderId][$date] += $nps;
|
|
} else {
|
|
$dailyFieldTotalWdi[$welderId][$date] += $nps;
|
|
}
|
|
|
|
}
|
|
|
|
foreach($dailyTotalWdi AS $welderId => $data) {
|
|
try {
|
|
$dailyAverage[$welderId] = round(array_sum($data) / count(array_filter($data)), 2);
|
|
} catch (\Throwable $th) {
|
|
$dailyAverage[$welderId] = 0;
|
|
}
|
|
|
|
}
|
|
|
|
foreach($dailyShopTotalWdi AS $welderId => $data) {
|
|
try {
|
|
$dailyShopAverage[$welderId] = round(array_sum($data) / count(array_filter($data)), 2);
|
|
} catch (\Throwable $th) {
|
|
$dailyShopAverage[$welderId] = 0;
|
|
}
|
|
}
|
|
|
|
foreach($dailyFieldTotalWdi AS $welderId => $data) {
|
|
try {
|
|
$dailyFieldAverage[$welderId] = round(array_sum($data) / count(array_filter($data)), 2);
|
|
} catch (\Throwable $th) {
|
|
$dailyFieldAverage[$welderId] = 0;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$totalExaminedJoint = [];
|
|
$totalAcceptedJoint = [];
|
|
$totalNotAcceptedJoint = [];
|
|
$todayNotAcceptedJoint = [];
|
|
$repairRate = [];
|
|
|
|
$rtLogs = db("radiographic_tests")->whereNotNull('real_welder_1')->get();
|
|
|
|
foreach($rtLogs AS $rtLog) {
|
|
|
|
$welderId = get_welder_id($rtLog->real_welder_1);
|
|
if(!$welderId) continue;
|
|
|
|
if(!isset($totalExaminedJoint[$welderId])) $totalExaminedJoint[$welderId] = 0;
|
|
if(!isset($repairRate[$welderId])) $repairRate[$welderId] = 0;
|
|
if(!isset($totalAcceptedJoint[$welderId])) $totalAcceptedJoint[$welderId] = 0;
|
|
if(!isset($totalNotAcceptedJoint[$welderId])) $totalNotAcceptedJoint[$welderId] = 0;
|
|
if(!isset($todayNotAcceptedJoint[$welderId])) $todayNotAcceptedJoint[$welderId] = 0;
|
|
|
|
|
|
if($rtLog->rt_result != "") {
|
|
$totalExaminedJoint[$welderId]++;
|
|
}
|
|
|
|
if($rtLog->rt_result == "Accept / Годен") {
|
|
$totalAcceptedJoint[$welderId]++;
|
|
} elseif($rtLog->rt_result != "") {
|
|
$totalNotAcceptedJoint[$welderId]++;
|
|
}
|
|
|
|
if($rtLog->welding_date) {
|
|
try {
|
|
$weldingDate = Carbon::parse($rtLog->welding_date);
|
|
if ($weldingDate->isToday()) {
|
|
$todayNotAcceptedJoint[$welderId]++;
|
|
}
|
|
} catch (\Throwable $th) {
|
|
// Skip invalid dates
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If no welders found, return empty array
|
|
if(empty($weldlogWelders)) {
|
|
echo json_encode([]);
|
|
return;
|
|
}
|
|
|
|
$query = db("naks_welders")->orderBy("id","DESC")
|
|
->select(
|
|
"welder_name_ru",
|
|
"company",
|
|
"status",
|
|
"welder_id",
|
|
/*
|
|
DB::raw("(SELECT SUM(nps_1) FROM weld_logs WHERE weld_logs.real_welder_1 = naks_welders.welder_id) AS total_welder_wdi"),
|
|
DB::raw("(SELECT COUNT(nps_1) FROM weld_logs WHERE weld_logs.real_welder_1 = naks_welders.welder_id) AS total_welder_joint"),
|
|
DB::raw("(SELECT SUM(nps_1) FROM weld_logs WHERE weld_logs.real_welder_1 = naks_welders.welder_id AND DATE(welding_date) = CURRENT_DATE()) AS today_total_welder_wdi"),
|
|
DB::raw("(SELECT COUNT(nps_1) FROM weld_logs WHERE weld_logs.real_welder_1 = naks_welders.welder_id AND DATE(welding_date) = CURRENT_DATE()) AS today_total_welder_joint"),
|
|
*/
|
|
)
|
|
->whereIn("welder_id", $weldlogWelders)
|
|
->groupBy("welder_id")
|
|
->get();
|
|
|
|
foreach($query AS $q) {
|
|
|
|
if(isset($totalWelderWdi[$q->welder_id])) {
|
|
$q->total_welder_wdi = $totalWelderWdi[$q->welder_id];
|
|
} else {
|
|
$q->total_welder_wdi = 0;
|
|
}
|
|
|
|
if(isset($totalWelderJoint[$q->welder_id])) {
|
|
$q->total_welder_joint = $totalWelderJoint[$q->welder_id];
|
|
} else {
|
|
$q->total_welder_joint = 0;
|
|
}
|
|
|
|
if(isset($todayTotalWelderWdi[$q->welder_id])) {
|
|
$q->today_total_welder_wdi = $todayTotalWelderWdi[$q->welder_id];
|
|
} else {
|
|
$q->today_total_welder_wdi = 0;
|
|
}
|
|
|
|
if(isset($todayTotalWelderJoint[$q->welder_id])) {
|
|
$q->today_total_welder_joint = $todayTotalWelderJoint[$q->welder_id];
|
|
} else {
|
|
$q->today_total_welder_joint = 0;
|
|
}
|
|
|
|
if(isset($totalExaminedJoint[$q->welder_id])) {
|
|
$q->total_examined_joint = $totalExaminedJoint[$q->welder_id];
|
|
} else {
|
|
$q->total_examined_joint = 0;
|
|
}
|
|
|
|
if(isset($totalAcceptedJoint[$q->welder_id])) {
|
|
$q->total_accepted_joint = $totalAcceptedJoint[$q->welder_id];
|
|
} else {
|
|
$q->total_accepted_joint = 0;
|
|
}
|
|
if(isset($totalNotAcceptedJoint[$q->welder_id])) {
|
|
$q->total_not_accepted_joint = $totalNotAcceptedJoint[$q->welder_id];
|
|
} else {
|
|
$q->total_not_accepted_joint = 0;
|
|
}
|
|
|
|
try {
|
|
$q->repair_rate = round($q->total_not_accepted_joint * 100 / $q->total_examined_joint,2);
|
|
} catch (\Throwable $th) {
|
|
$q->repair_rate = 0;
|
|
}
|
|
|
|
if(isset($dailyAverage[$q->welder_id])) {
|
|
$q->daily_average = $dailyAverage[$q->welder_id];
|
|
} else {
|
|
$q->daily_average = 0;
|
|
}
|
|
|
|
if(isset($dailyShopAverage[$q->welder_id])) {
|
|
$q->daily_shop_average = $dailyShopAverage[$q->welder_id];
|
|
} else {
|
|
$q->daily_shop_average = 0;
|
|
}
|
|
|
|
if(isset($dailyFieldAverage[$q->welder_id])) {
|
|
$q->daily_field_average = $dailyFieldAverage[$q->welder_id];
|
|
} else {
|
|
$q->daily_field_average = 0;
|
|
}
|
|
|
|
if(isset($todayNotAcceptedJoint[$q->welder_id])) {
|
|
$q->today_not_acc_joint = $todayNotAcceptedJoint[$q->welder_id];
|
|
} else {
|
|
$q->today_not_acc_joint = 0;
|
|
}
|
|
}
|
|
$json = json_encode_tr($query);
|
|
$json = str_replace("_", " ", $json);
|
|
echo $json;
|
|
|
|
?>
|