@extends('admin.layouts')
@section('title', 'Stock Details')
@section('head')
@endsection
@section('content')
{{--
Stock Details for {{ $product->title ?? 'Product' }}
| S.No |
Date & Time |
Transaction Type |
Quantity |
Invoice / Order |
Closing Stock |
@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)
| {{ $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'] }} |
@endforeach
@endsection
@section('footer')
@endsection