@extends('layouts.app') @section('content') @php $priceTypeOptions = $priceTypes ?? ['umum' => 'Umum', 'studio' => 'Instansi']; $normalizeInitialPriceValue = function ($value) { if ($value === null || $value === '') { return 0; } if (is_numeric($value)) { return (float) $value; } $value = trim((string) $value); $value = str_replace([' ', '.'], '', $value); $value = str_replace(',', '.', $value); return is_numeric($value) ? (float) $value : 0; }; $initialComponents = collect(old('components', [])) ->filter(function ($item) { return !empty($item['component_material_id']) || (($item['qty'] ?? '') !== ''); }) ->map(function ($item) { return [ 'component_material_id' => (string) ($item['component_material_id'] ?? ''), 'qty' => (float) ($item['qty'] ?? 1), ]; }) ->values() ->all(); $initialPriceGroups = collect(old('prices', [])) ->groupBy(function ($item) { return ($item['sisi'] ?? 1) . '-' . ($item['min_qty'] ?? 1) . '-' . (($item['max_qty'] ?? null) ?: 'null'); }) ->map(function ($group) use ($priceTypeOptions, $normalizeInitialPriceValue) { $first = $group->first(); $prices = []; foreach ($priceTypeOptions as $typeCode => $typeName) { $row = collect($group)->firstWhere('member_type', $typeCode); $prices[$typeCode] = $normalizeInitialPriceValue($row['price'] ?? 0); } return [ 'sisi' => (int) ($first['sisi'] ?? 1), 'min_qty' => (int) ($first['min_qty'] ?? 1), 'max_qty' => $first['max_qty'] ?? null, 'prices' => $prices, ]; }) ->values() ->all(); @endphp