@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
$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)
| {{ $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'] }} |
@endforeach
@endsection
@section('footer')
@endsection
{{--