@extends('admin.layouts') @section('title', 'Stock Details') @section('head') @endsection @section('content') {{--

Stock Details

@php $closing_stock = 0; @endphp @foreach ($transactions as $transaction) @if ($transaction['type'] == 'Purchase') @if ($closing_stock <= 0) @php $closing_stock = $transaction['quantity']; @endphp @else @php $closing_stock += $transaction['quantity']; @endphp @endif @else @php $closing_stock -= $transaction['quantity']; @endphp @endif @php $display_stock = $closing_stock < 0 ? 0 : $closing_stock; @endphp @endforeach
S.No Date Transaction Type Quantity Invoice / Order Closing Stock
{{ $loop->iteration }} {{ \Carbon\Carbon::parse($transaction['date'])->format('Y-m-d') }} @if ($transaction['type'] == 'Purchase') Purchase @else Sale @endif {{ $transaction['quantity'] }} {{ $transaction['ref'] }} {{ $display_stock }}
--}}

Stock Details for {{ $product->title ?? 'Product' }}

@php $sortedTransactions = collect($transactions ?? []) ->sortBy(fn($t) => \Carbon\Carbon::parse($t['date'])) ->values(); // ✅ STEP 1: Calculate history total $history_total = 0; foreach ($sortedTransactions as $t) { $qty = (int) ($t['quantity'] ?? 0); $type = $t['type'] ?? ''; if (in_array($type, ['Purchase','Admin Add'])) { $history_total += $qty; } elseif (in_array($type, ['Sale','Admin Reduce'])) { $history_total -= $qty; } } // ✅ STEP 2: Hidden Offset (NOT DISPLAYED) $offset = ($product->stock ?? 0) - $history_total; // ✅ STEP 3: Start closing from offset $closing_stock = $offset; $stockHistory = []; foreach ($sortedTransactions as $t) { $qty = (int) ($t['quantity'] ?? 0); $type = $t['type'] ?? ''; if (in_array($type, ['Purchase','Admin Add'])) { $closing_stock += $qty; } elseif (in_array($type, ['Sale','Admin Reduce'])) { $closing_stock -= $qty; } $stockHistory[] = [ 'date' => $t['date'], 'type' => $type, 'quantity' => $qty, 'ref' => $t['ref'] ?? '-', 'closing_stock' => $closing_stock, ]; } // Latest first $stockHistory = collect($stockHistory) ->sortByDesc(fn($t) => \Carbon\Carbon::parse($t['date'])->timestamp) ->values(); @endphp @foreach ($stockHistory as $transaction) @endforeach
S.No Date & Time Transaction Type Quantity Invoice / Order Closing Stock
{{ $loop->iteration }} {{ \Carbon\Carbon::parse($transaction['date'])->format('Y-m-d H:i:s') }} @if ($transaction['type'] == 'Purchase') Purchase @elseif ($transaction['type'] == 'Sale') Sale @elseif ($transaction['type'] == 'Admin Add') Admin Add @elseif ($transaction['type'] == 'Admin Reduce') Admin Reduce @endif @if (in_array($transaction['type'], ['Purchase','Admin Add'])) +{{ $transaction['quantity'] }} @else -{{ $transaction['quantity'] }} @endif {{ $transaction['ref'] }} {{ $transaction['closing_stock'] }}
@endsection @section('footer') @endsection