@foreach ($specData as $gedung => $specGedung)
@foreach ($specGedung['MICs'] as $micName => $mic)
@php
$micName = trim($mic['MIC Name'] ?? '');
$unit = trim($mic['Unit'] ?? '');
$lookupKey = strtolower($micName) . '|' . strtolower($unit);
$existingDetail = $existingCoaDetails[$lookupKey] ?? null;
if (!function_exists('numericValueForCoaFormat')) {
function numericValueForCoaFormat($value)
{
if ($value === null || $value === '') {
return 0.0;
}
if (is_numeric($value)) {
return (float) $value;
}
$normalized = trim((string) $value);
if (str_contains($normalized, ',') && !str_contains($normalized, '.')) {
$normalized = str_replace(',', '.', $normalized);
} else {
$normalized = str_replace(',', '', $normalized);
}
return is_numeric($normalized) ? (float) $normalized : 0.0;
}
}
// ✅ Target Value, Upper Limit, Lower Limit: dari v_mst_standard_properties → SAP
$targetValue = number_format(numericValueForCoaFormat($mic['Target Value'] ?? 0), 2,
'.', '');
$upperLimit = number_format(numericValueForCoaFormat($mic['Upper Limit'] ?? 0), 2,
'.', '');
$lowerLimit = number_format(numericValueForCoaFormat($mic['Lower Limit'] ?? 0), 2,
'.', '');
// ✅ Agreement logic: data New Target/Upper/Lower diambil dari mst_Agreement (tertinggi) -> Agreement Avg Properties (fallback)
$normalizedKey = $mic['Normalized Key'] ?? null;
$customerAgreementItem = $customerAgreement[$normalizedKey] ?? null;
$isFromAgreement = false;
// New Target/Upper/Lower: existingDetail → Agreement → fallback ke base
// Target/Upper/Lower
$agreementTarget = $mic['Agreement Target'] ?? null;
$agreementUpper = $mic['Agreement Upper'] ?? null;
$agreementLower = $mic['Agreement Lower'] ?? null;
$agreementUnit = $mic['Agreement Unit'] ?? null;
if ($agreementTarget !== null || $agreementUpper !== null || $agreementLower !== null) {
$isFromAgreement = true;
}
$newTarget = $existingDetail->new_target_value
?? ($agreementTarget !== null && trim((string)$agreementTarget) !== '' ?
$agreementTarget : $targetValue);
$newUpper = $existingDetail->new_upper_limit
?? ($agreementUpper !== null && trim((string)$agreementUpper) !== '' ?
$agreementUpper : $upperLimit);
$newLower = $existingDetail->new_lower_limit
?? ($agreementLower !== null && trim((string)$agreementLower) !== '' ?
$agreementLower : $lowerLimit);
$avgMittelwert = number_format(numericValueForCoaFormat($mic['avg_mittelwert'] ??
0), 2, '.', '');
$newValue = $existingDetail->new_value ?? $avgMittelwert;
$indikator = $existingDetail->indikator ?? '';
$method = $existingDetail->method ?? '';
$urut = $existingDetail->urut ?? 9999;
$newUom = $existingDetail->new_uom ?? ($agreementUnit ?? $unit);
$formattedTarget = $newTarget;
$formattedUpper = $newUpper;
$formattedLower = $newLower;
$formattedValue = $newValue;
if (!function_exists('formatWithDbPrecision')) {
function formatWithDbPrecision(
$value,
$lookupKey,
$precisionMap,
$default = 1,
) {
$key = strtolower(trim($lookupKey));
$decimalType = $precisionMap[$key]['decimal'] ?? $default;
switch ($decimalType) {
case 0:
return number_format((float) $value, 0, '.', '');
case 1:
return number_format((float) $value, 1, '.', '');
case 2:
return number_format((float) $value, 2, '.', '');
case 3:
return number_format((float) $value, 3, '.', '');
default:
return number_format((float) $value, $default, '.', '');
}
}
}
// Format hanya jika numerik
$formattedTarget = is_numeric($formattedTarget)
? formatWithDbPrecision(
$formattedTarget,
$lookupKey,
$micPrecisionMap,
)
: $formattedTarget;
$formattedUpper = is_numeric($formattedUpper)
? formatWithDbPrecision(
$formattedUpper,
$lookupKey,
$micPrecisionMap,
)
: $formattedUpper;
$formattedLower = is_numeric($formattedLower)
? formatWithDbPrecision(
$formattedLower,
$lookupKey,
$micPrecisionMap,
)
: $formattedLower;
$formattedValue = is_numeric($formattedValue)
? formatWithDbPrecision(
$formattedValue,
$lookupKey,
$micPrecisionMap,
)
: $formattedValue;
@endphp
|
@if (isset($existingCoa) && $existingCoa)
@endif
|
{{ $micName }}
|
{{ $unit }}
|
{{ formatWithDbPrecision($targetValue, $lookupKey, $micPrecisionMap) }}
|
{{ formatWithDbPrecision($upperLimit, $lookupKey, $micPrecisionMap) }}
|
{{ formatWithDbPrecision($lowerLimit, $lookupKey, $micPrecisionMap) }}
|
{{ formatWithDbPrecision($mic['avg_mittelwert'] ?? 0, $lookupKey,
$micPrecisionMap) }}
|
Sebelumnya:
{{ formatWithDbPrecision($newTarget, $lookupKey, $micPrecisionMap) }}
|
Sebelumnya:
{{ formatWithDbPrecision($newUpper, $lookupKey, $micPrecisionMap) }}
|
Sebelumnya:
{{ formatWithDbPrecision($newLower, $lookupKey, $micPrecisionMap) }}
|
Sebelumnya:
{{ $formattedValue }}
|
|
|
|
@endforeach
@endforeach