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

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

@php $sortedTransactions = collect($transactions ?? []) ->sortBy(fn($t) => \Carbon\Carbon::parse($t['date'])) ->values(); $stockHistory = []; $closing_stock = 0; $started = false; // ✅ track start point foreach ($sortedTransactions as $t) { $qty = (int) ($t['quantity'] ?? 0); $type = $t['type'] ?? ''; // ✅ Skip invalid starting sales if (!$started) { if (in_array($type, ['Purchase', 'Admin Add'])) { $started = true; $closing_stock += $qty; } else { continue; // ❌ skip early sales } } else { if (in_array($type, ['Purchase', 'Admin Add'])) { $closing_stock += $qty; } elseif (in_array($type, ['Sale', 'Admin Reduce'])) { $closing_stock -= $qty; } } $closing_stock = max(0, $closing_stock); $stockHistory[] = [ 'date' => $t['date'], 'type' => $type, 'quantity' => $qty, 'ref' => $t['ref'] ?? '-', 'closing_stock' => $closing_stock, ]; } $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 {{-- @php $sortedTransactions = $transactions ?? collect(); $calculated_stock_from_history = 0; $stockHistory = []; // STEP 1: Calculate total stock from history foreach ($sortedTransactions as $t) { $qty = (int) $t['quantity']; if ($t['type'] === 'Purchase' || $t['type'] === 'Admin Add') { $calculated_stock_from_history += $qty; } elseif ($t['type'] === 'Sale' || $t['type'] === 'Admin Reduce') { $calculated_stock_from_history -= $qty; } } // STEP 2: Offset (Old Stock logic) $initial_stock_offset = $product->stock - $calculated_stock_from_history; $closing_stock = $initial_stock_offset; // STEP 3: Add Old Stock entry if ($initial_stock_offset > 0) { $firstTransactionDate = $sortedTransactions->first() ? \Carbon\Carbon::parse($sortedTransactions->first()['date'])->subSecond() : now(); $stockHistory[] = [ 'date' => $firstTransactionDate->format('Y-m-d H:i:s'), 'type' => 'Initial Stock', 'quantity' => $initial_stock_offset, 'ref' => 'Old Stock', // ✅ changed here 'closing_stock' => $initial_stock_offset, ]; } // STEP 4: Process all transactions foreach ($sortedTransactions as $t) { $qty = (int) $t['quantity']; if ($t['type'] === 'Purchase' || $t['type'] === 'Admin Add') { $closing_stock += $qty; } elseif ($t['type'] === 'Sale' || $t['type'] === 'Admin Reduce') { $closing_stock -= $qty; } $closing_stock = max(0, $closing_stock); $stockHistory[] = array_merge($t, [ 'closing_stock' => $closing_stock, ]); } // STEP 5: Sort latest first $stockHistory = collect($stockHistory) ->sortByDesc(fn($t) => \Carbon\Carbon::parse($t['date'])->timestamp) ->values(); @endphp @foreach ($stockHistory as $transaction) {{ $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 @elseif ($transaction['type'] == 'Initial Stock') Initial Stock @endif @if (in_array($transaction['type'], ['Purchase','Admin Add','Initial Stock'])) +{{ $transaction['quantity'] }} @else -{{ $transaction['quantity'] }} @endif {{ $transaction['ref'] ?? '-' }} {{ $transaction['closing_stock'] }} @endforeach --}}