@if(!$box || $box === 'all') @php $grouped = $packitems->groupBy('box'); @endphp @php // Build a single aggregated list of all boxes across all groups $summaryRows = []; $totalBoxesAll = 0; $totalWeightAll = 0.0; $cleanToArray = function($val) { if (is_array($val)) return $val; if (is_null($val) || $val === '') return []; if (is_string($val)) { $decoded = json_decode($val, true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) return $decoded; $parts = preg_split('/\s*,\s*/', $val); return array_values(array_filter(array_map(function($v){ return trim($v, " \"'[]"); }, $parts), function($v){ return $v !== ''; })); } return [$val]; }; foreach($grouped as $gBoxNo => $gItems) { $gFirst = $gItems->first(); $gBoxArr = $cleanToArray($gBoxNo); $gDimArr = $cleanToArray($gFirst->dimension ?? null); $gWtArr = $cleanToArray($gFirst->weight ?? null); $gMax = max(count($gBoxArr), count($gDimArr), count($gWtArr)); for ($gi = 0; $gi < $gMax; $gi++) { $rowBox = $gBoxArr[$gi] ?? ($gBoxArr[0] ?? '-'); $rowDim = $gDimArr[$gi] ?? ($gDimArr[0] ?? '-'); $rowWt = $gWtArr[$gi] ?? ($gWtArr[0] ?? '-'); $summaryRows[] = [ 'box' => $rowBox, 'dim' => $rowDim, 'wt' => $rowWt, ]; $totalBoxesAll++; // Try to parse numeric weight for total $numWt = is_numeric($rowWt) ? (float)$rowWt : (float)preg_replace('/[^0-9.\-]/', '', (string)$rowWt); if (is_finite($numWt)) { $totalWeightAll += $numWt; } } } @endphp @if(count($summaryRows))

Sales Order Number: {{ $so_no }}

Date: {{ now()->format('d/m/Y') }}

Generated On: {{ now()->format('d/m/Y H:i:s') }}

@foreach($summaryRows as $r) @endforeach
Total Boxes {{ $totalBoxesAll }}
Total Weight {{ rtrim(rtrim(number_format((float)$totalWeightAll, 3, '.', ''), '0'), '.') }}
Box Number Dimension Weight
{{ $r['box'] }} {{ $r['dim'] }} {{ $r['wt'] }}

@endif @foreach($grouped as $boxNo => $items)

Sales Order Number: {{ $so_no }}

Date: {{ now()->format('d/m/Y') }}

Generated On: {{ now()->format('d/m/Y H:i:s') }}

@php $first = $items->first(); $toArray = function($val) { if (is_array($val)) return $val; if (is_null($val) || $val === '') return []; if (is_string($val)) { $decoded = json_decode($val, true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) return $decoded; $parts = preg_split('/\s*,\s*/', $val); return array_values(array_filter(array_map(function($v){ return trim($v, " \"'[]"); }, $parts), function($v){ return $v !== ''; })); } return [$val]; }; $boxArr = $toArray($boxNo); $dimArr = $toArray($first->dimension ?? null); $wtArr = $toArray($first->weight ?? null); $maxLen = max(count($boxArr), count($dimArr), count($wtArr)); @endphp @for($i = 0; $i < $maxLen; $i++) @endfor
Box Number Dimension Weight
{{ $boxArr[$i] ?? '-' }} {{ $dimArr[$i] ?? ($dimArr[0] ?? '-') }} {{ $wtArr[$i] ?? ($wtArr[0] ?? '-') }}
Total Boxes {{ $maxLen }}
@php $serial = 1; @endphp @foreach($items as $item) @php $itemsArray = json_decode($item->items, true); @endphp @if(is_array($itemsArray)) @foreach($itemsArray as $packItem) @php $itemData = json_decode($packItem, true); @endphp @endforeach @endif @endforeach
Serial No Item Name Quantity
{{ $serial++ }} {{ $itemData['item'] ?? 'N/A' }} {{ $itemData['qty'] ?? 'N/A' }}

@endforeach @else

Sales Order Number: {{ $so_no }}

Date: {{ now()->format('d/m/Y') }}

Generated On: {{ now()->format('d/m/Y H:i:s') }}

@php $first = $packitems->first(); $toArray = function($val) { if (is_array($val)) return $val; if (is_null($val) || $val === '') return []; if (is_string($val)) { $decoded = json_decode($val, true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) return $decoded; $parts = preg_split('/\s*,\s*/', $val); return array_values(array_filter(array_map(function($v){ return trim($v, " \"'[]"); }, $parts), function($v){ return $v !== ''; })); } return [$val]; }; $boxArr = $toArray($box); // if $box is single but DB has arrays, try from first record if (count($boxArr) <= 1) { $boxArr = $toArray($first->box ?? $box); } $dimArr = $toArray($first->dimension ?? null); $wtArr = $toArray($first->weight ?? null); $maxLen = max(count($boxArr), count($dimArr), count($wtArr)); @endphp @for($i = 0; $i < $maxLen; $i++) @endfor
Box Number Dimension Weight
{{ $boxArr[$i] ?? '-' }} {{ $dimArr[$i] ?? ($dimArr[0] ?? '-') }} {{ $wtArr[$i] ?? ($wtArr[0] ?? '-') }}
Total Boxes {{ $maxLen }}
@php $serial = 1; @endphp @foreach($packitems as $item) @php $itemsArray = json_decode($item->items, true); @endphp @if(is_array($itemsArray)) @foreach($itemsArray as $packItem) @php $itemData = json_decode($packItem, true); @endphp @endforeach @endif @endforeach
Serial No Item Name Quantity
{{ $serial++ }} {{ $itemData['item'] ?? 'N/A' }} {{ $itemData['qty'] ?? 'N/A' }}
@endif @if(isset($isForceCompleted) && $isForceCompleted && !empty($shortages))

Short Summary (force completed)

@foreach($shortages as $s) @endforeach
Item Ordered Qty Picked Qty Short Qty
{{ $s['item'] }} {{ rtrim(rtrim(number_format((float)$s['ordered_qty'], 4, '.', ''), '0'), '.') }} {{ rtrim(rtrim(number_format((float)$s['picked_qty'], 4, '.', ''), '0'), '.') }} {{ rtrim(rtrim(number_format((float)$s['short_qty'], 4, '.', ''), '0'), '.') }}
@endif