@extends('layouts.location')
@section('title', 'Packed Sales Order')
@section('page-title', 'Packed Sales Order')
@section('content')
{{-- Original PIC ID selection UI is kept here for reference but disabled. --}}
{{--------------
Print Packlist
--------------}}
Packed Sales Order
@if(isset($packings) && $packings->count() > 0)
SO No
Customer Name
Box
Weight (Kgs)
Remark
Transporter for Zoho
Packing Complete Date
Action
@foreach($packings as $row)
@php
// --- Total box count ---
$boxRaw = $row->box;
$boxCount = 0;
// Normalize to a single string
if (is_array($boxRaw)) {
$boxString = implode(',', array_map('strval', $boxRaw));
} else {
$boxString = (string) $boxRaw;
}
$boxString = trim($boxString);
if ($boxString !== '') {
// Try JSON first
$decodedBox = json_decode($boxString, true);
if (json_last_error() === JSON_ERROR_NONE && is_array($decodedBox)) {
$filtered = array_filter($decodedBox, function ($v) {
return $v !== null && $v !== '';
});
$boxCount = count($filtered);
}
if ($boxCount === 0) {
// Fallback: treat bracketed / comma-separated string as list
$tmp = trim($boxString, "[]\"'");
if ($tmp !== '') {
$parts = array_filter(array_map('trim', explode(',', $tmp)), function ($v) {
return $v !== '';
});
$boxCount = count($parts) > 0 ? count($parts) : 1;
} else {
$boxCount = 1;
}
}
}
// --- Total weight ---
$weightRaw = $row->weight;
$totalWeight = 0;
// Normalize to a single string
if (is_array($weightRaw)) {
$weightString = implode(',', array_map('strval', $weightRaw));
} else {
$weightString = (string) $weightRaw;
}
$weightString = trim($weightString);
if ($weightString !== '') {
// Try JSON first
$decodedW = json_decode($weightString, true);
if (json_last_error() === JSON_ERROR_NONE && is_array($decodedW)) {
foreach ($decodedW as $w) {
if ($w !== null && $w !== '') {
$totalWeight += (float) $w;
}
}
}
if ($totalWeight == 0) {
// Fallback: extract all numeric values and sum them
$matches = [];
preg_match_all('/-?\d+(?:\.\d+)?/', $weightString, $matches);
if (!empty($matches[0])) {
foreach ($matches[0] as $num) {
$totalWeight += (float) $num;
}
} else {
$totalWeight = (float) $weightString;
}
}
}
// Determine row status based on completion and force-completion lists
$isShort = !empty($shortSoNumbers ?? []) && in_array($row->so_no, $shortSoNumbers, true);
$isCompleted = !empty($completedSoNumbers ?? []) && in_array($row->so_no, $completedSoNumbers, true);
if ($isShort) {
$rowStatus = 'short';
} elseif ($isCompleted) {
$rowStatus = 'completed';
} else {
$rowStatus = 'pending';
}
@endphp