@php $spkItems = collect($order->items ?? [])->values(); $resolveCategoryName = function ($item) { $materialCategory = null; try { if ($item->relationLoaded('material') && $item->material) { $materialCategory = optional($item->material->category)->name; } elseif (!empty($item->material_id) && method_exists($item, 'material')) { $materialCategory = optional(optional($item->material()->with('category')->first())->category)->name; } } catch (\Throwable $e) { $materialCategory = null; } $candidates = [ $materialCategory, ($item->needs_outdoor_installation ?? false) ? 'Outdoor / Pemasangan' : null, $item->material_name ?? null, $item->product_name ?? null, ]; foreach ($candidates as $candidate) { $candidate = trim((string) $candidate); if ($candidate !== '') { return $candidate; } } return 'Lainnya'; }; $categoryGroups = $spkItems ->groupBy(fn ($item) => \Illuminate\Support\Str::upper($resolveCategoryName($item))) ->sortKeys(); if ($categoryGroups->isEmpty()) { $categoryGroups = collect(['LAINNYA' => collect()]); } $totalGroups = $categoryGroups->count(); $printedAt = now(); @endphp @foreach($categoryGroups as $categoryName => $items)
@if(env('APP_DEMO', false))
DEMO MODE
/TRANSAKSI PALSU
@endif
@if(!empty($shopLogo)) {{ $shopName ?? 'Logo' }} @endif

SPK PRODUKSI

SURAT PERINTAH KERJA
{{ $order->invoice_number }}
KATEGORI: {{ $categoryName }}
Grup {{ $loop->iteration }}/{{ $totalGroups }} {{ $items->count() }} Item
Pelanggan
{{ $order->customer_name }}
@if(!empty($order->notes))
Catatan Order
{{ $order->notes }}
@endif @forelse($items as $index => $item) @php $unitRaw = strtolower(trim((string) ($item->size_unit ?? 'lembar'))); if ($unitRaw === 'm²') { $unitRaw = 'm2'; } if (in_array($unitRaw, ['pcs', 'pc', 'piece', 'sheet'])) { $unitRaw = 'lembar'; } $showSize = in_array($unitRaw, ['m2', 'meter', 'm'], true) && !empty($item->size_description); $showSisi = !in_array($unitRaw, ['m2', 'meter', 'm'], true); $qtyUnit = 'LEMBAR'; if ($unitRaw === 'meter' || $unitRaw === 'm') { $qtyUnit = 'METER'; } elseif ($unitRaw === 'm2') { $qtyUnit = 'M2'; } $meterValue = null; $m2Value = null; $sizeText = strtolower(trim((string) ($item->size_description ?? ''))); $sizeText = str_replace(['×', '*', ' '], ['x', 'x', ''], $sizeText); if (preg_match('/^(\d+(?:[.,]\d+)?)x(\d+(?:[.,]\d+)?)(cm|m)?$/', $sizeText, $matches)) { $a = (float) str_replace(',', '.', $matches[1]); $b = (float) str_replace(',', '.', $matches[2]); $suffix = $matches[3] ?? ''; if ($suffix === 'cm') { $aCm = $a; $bCm = $b; } elseif ($suffix === 'm') { $aCm = $a * 100; $bCm = $b * 100; } else { if ($a >= 10 && $b >= 10) { $aCm = $a; $bCm = $b; } else { $aCm = $a * 100; $bCm = $b * 100; } } if ($unitRaw === 'meter' || $unitRaw === 'm') { $meterResult = min($aCm, $bCm) / 100; $meterValue = rtrim(rtrim(number_format($meterResult, 4, ',', '.'), '0'), ',') . ' meter'; } if ($unitRaw === 'm2') { $m2Result = ($aCm * $bCm) / 10000; $m2Value = rtrim(rtrim(number_format($m2Result, 4, ',', '.'), '0'), ',') . ' m²'; } } if (($unitRaw === 'meter' || $unitRaw === 'm') && !$meterValue && !empty($item->size_value)) { $meterValue = rtrim(rtrim(number_format((float) $item->size_value, 4, ',', '.'), '0'), ',') . ' meter'; } if ($unitRaw === 'm2' && !$m2Value && !empty($item->size_value)) { $m2Value = rtrim(rtrim(number_format((float) $item->size_value, 4, ',', '.'), '0'), ',') . ' m²'; } @endphp
ITEM {{ $loop->iteration }}
{{ $item->file_name }}
Produk {{ $item->product_name }}
@if($item->material_name)
Bahan {{ $item->material_name }}
@endif @if($showSize)
Ukuran {{ $item->size_description }}
@endif @if(!empty($meterValue))
Pakai {{ $meterValue }}
@endif @if(!empty($m2Value))
Luas {{ $m2Value }}
@endif @if($showSisi)
Sisi @if(($item->sisi ?? 1) == 2) {{ $item->sisi ?? 1 }} SISI @else {{ $item->sisi ?? 1 }} SISI @endif
@endif
Qty {{ $item->qty }} {{ $qtyUnit }}
@if($item->finishing_note)
Finishing
{{ $item->finishing_note }}
@endif
@empty
Tidak ada item pada kategori ini.
@endforelse
@endforeach