@if(isset($pickings) && $pickings->count())
| SO No
|
Items
|
Status |
Action |
@foreach($pickings->sortKeysDesc() as $so_no => $group)
| {{ $so_no }} |
@php
$allItems = collect();
foreach($group as $packing) {
$itemsData = $packing->items;
if(is_array($itemsData)) {
// Handle array of JSON strings
foreach($itemsData as $itemString) {
if(is_string($itemString)) {
$decoded = json_decode($itemString, true);
if(is_array($decoded) && isset($decoded['item']) && isset($decoded['qty'])) {
$allItems->push($decoded);
}
} elseif(is_array($itemString) && isset($itemString['item']) && isset($itemString['qty'])) {
$allItems->push($itemString);
}
}
} elseif(is_string($itemsData)) {
// Try to decode JSON string
$decoded = json_decode($itemsData, true);
if(is_array($decoded)) {
// If decoded result is an array of items
if(isset($decoded[0]) && is_array($decoded[0])) {
foreach($decoded as $item) {
if(isset($item['item']) && isset($item['qty'])) {
$allItems->push($item);
}
}
}
// If decoded result is a single item object
elseif(isset($decoded['item']) && isset($decoded['qty'])) {
$allItems->push($decoded);
}
}
}
}
// Remove duplicates and group by item
$uniqueItems = $allItems->groupBy('item')->map(function($group) {
return [
'item' => $group->first()['item'],
'qty' => $group->sum('qty')
];
})->values();
// Get comparison data for this SO
$shortItems = $comparisonData[$so_no] ?? [];
@endphp
@php
// Combine picked items and shortages for comprehensive display
// Use a keyed collection by item name to ensure mutations persist
$allDisplayItems = collect();
// Seed with picked items
foreach ($uniqueItems as $item) {
$key = (string)($item['item'] ?? '');
if ($key === '') continue;
$allDisplayItems[$key] = [
'item' => $key,
'picked_qty' => (float)($item['qty'] ?? 0),
'short_qty' => 0.0,
'type' => 'picked',
];
}
// Merge in shortage items
foreach ($shortItems as $shortItem) {
$key = (string)($shortItem['item'] ?? '');
if ($key === '') continue;
$short = (float)($shortItem['short_qty'] ?? 0);
if (isset($allDisplayItems[$key])) {
$row = $allDisplayItems[$key];
$row['short_qty'] = $short;
$allDisplayItems[$key] = $row; // write back to persist
} else {
$allDisplayItems[$key] = [
'item' => $key,
'picked_qty' => 0.0,
'short_qty' => $short,
'type' => 'shortage',
];
}
}
@endphp
@php
// Show only items that actually have a shortage
$shortOnlyItems = $allDisplayItems->filter(function($row){
return isset($row['short_qty']) && (float)$row['short_qty'] > 0;
})->values();
@endphp
@foreach($shortOnlyItems as $displayItem)
|
{{ $displayItem['item'] }}
|
@if($displayItem['picked_qty'] > 0)
{{ $displayItem['picked_qty'] }}
@else
0
@endif
|
{{ $displayItem['short_qty'] }}
|
@endforeach
|
@if($group->contains('status', 'hold'))
hold
@elseif($group->contains('status', 'force_completed'))
closed with shortage
@else
completed
@endif
|
@if($group->contains('status', 'hold'))
@elseif($group->contains('status', 'force_completed'))
Closed with shortage
@else
Completed
@endif
|
@endforeach
@else
No packings on hold found.
@endif