@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 // Ensure we have a collection to work with $sortedTransactions = $transactions ?? collect(); $calculated_stock_from_history = 0; $stockHistory = []; // --- STEP 1: Calculate Total Stock from History --- // Iterate through the ASCENDING sorted list to find the running total foreach ($sortedTransactions as $t) { $quantity = (int) $t['quantity']; if ($t['type'] === 'Purchase' || $t['type'] === 'Admin Add') { $calculated_stock_from_history += $quantity; } elseif ($t['type'] === 'Sale' || $t['type'] === 'Admin Reduce') { $calculated_stock_from_history -= $quantity; } } // --- STEP 2: Determine the Initial Stock Offset --- // The difference between the actual stock (68) and the historical total (e.g., 60 or 61). $initial_stock_offset = $product->stock - $calculated_stock_from_history; // Initialize the running closing stock with the offset. $closing_stock = $initial_stock_offset; // --- STEP 3: Calculate Running Closing Stock with Offset --- // If there's a positive offset, we first add a virtual 'Initial Stock' transaction if ($initial_stock_offset > 0) { // Find the date of the very first recorded transaction $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' => 'System Offset', 'closing_stock' => $initial_stock_offset, ]; } // Now process the actual database transactions foreach ($sortedTransactions as $t) { $quantity = (int) $t['quantity']; // Update running closing stock if ($t['type'] === 'Purchase' || $t['type'] === 'Admin Add') { $closing_stock += $quantity; } elseif ($t['type'] === 'Sale' || $t['type'] === 'Admin Reduce') { $closing_stock -= $quantity; } // Ensure stock doesn't drop below zero $closing_stock = max(0, $closing_stock); $stockHistory[] = array_merge($t, [ 'closing_stock' => $closing_stock, ]); } // --- STEP 4: Sort descending for display (latest first) --- $stockHistory = collect($stockHistory)->sortByDesc(function($t) { return \Carbon\Carbon::parse($t['date'])->timestamp; })->values(); @endphp {{-- Loop through the final, descending history array --}} @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 @elseif ($transaction['type'] == 'Initial Stock') Initial Stock @endif {{-- Display quantity change with sign --}} @if ($transaction['type'] == 'Admin Add' || $transaction['type'] == 'Initial Stock' || $transaction['type'] == 'Purchase') +{{ $transaction['quantity'] }} @elseif ($transaction['type'] == 'Admin Reduce' || $transaction['type'] == 'Sale') -{{ $transaction['quantity'] }} @else {{ $transaction['quantity'] }} @endif {{ $transaction['ref'] ?? '-' }} {{ $transaction['closing_stock'] }}
@endsection @section('footer') @endsection