<x-app-layout><x-slot name="header"><div class="d-flex flex-wrap align-items-center justify-content-between gap-2"><div><h2 class="h4 mb-0">Inventory</h2><p class="text-muted mb-0">Stock, equipment issue/return and movement history</p></div><div class="d-flex gap-2"><a href="{{ route('admin.inventory.export') }}" class="btn btn-outline-success">Export CSV</a>@can('inventory.manage')<button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#transactionModal">Stock transaction</button><button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#itemModal">Add item</button>@endcan</div></div></x-slot>
<form class="card border-0 shadow-sm mb-3"><div class="card-body row g-2 align-items-end"><div class="col-md-6"><label class="form-label">Search item</label><input name="search" value="{{ request('search') }}" class="form-control" placeholder="SKU, name or category"></div><div class="col-md-2"><label class="form-label">Stock</label><select name="stock" class="form-select"><option value="">All</option><option value="low" @selected(request('stock')==='low')>Low stock</option></select></div><div class="col-md-2"><label class="form-label">Status</label><select name="status" class="form-select"><option value="">All</option><option value="1" @selected(request('status')==='1')>Active</option><option value="0" @selected(request('status')==='0')>Inactive</option></select></div><div class="col-md-2"><button class="btn btn-dark w-100">Filter</button></div></div></form>
<div class="row g-3 mb-4"><div class="col-6 col-xl-3"><div class="card border-0 shadow-sm h-100"><div class="card-body"><small class="text-muted">Items</small><div class="h3 mb-0">{{ number_format($summary['items']) }}</div></div></div></div><div class="col-6 col-xl-3"><div class="card border-0 shadow-sm h-100"><div class="card-body"><small class="text-muted">Stock units</small><div class="h3 mb-0">{{ number_format($summary['stock'],2) }}</div></div></div></div><div class="col-6 col-xl-3"><div class="card border-0 shadow-sm h-100"><div class="card-body"><small class="text-muted">Low stock</small><div class="h3 mb-0 text-danger">{{ $summary['low'] }}</div></div></div></div><div class="col-6 col-xl-3"><div class="card border-0 shadow-sm h-100"><div class="card-body"><small class="text-muted">Stock value</small><div class="h3 mb-0">@money($summary['value'])</div></div></div></div></div>
<div class="card border-0 shadow-sm mb-4"><div class="card-header bg-white"><h3 class="h5 mb-0">Item list</h3></div><div class="table-responsive"><table class="table table-hover align-middle mb-0"><thead><tr><th>SKU</th><th>Item</th><th>Category</th><th class="text-end">Purchase</th><th class="text-end">Sale</th><th class="text-end">Stock</th><th>Status / Action</th></tr></thead><tbody>@forelse($items as $item)<tr class="{{ (float)$item->stock_quantity <= (float)$item->minimum_stock ? 'table-warning':'' }}"><td><code>{{ $item->sku }}</code></td><td><strong>{{ $item->name }}</strong><div class="small text-muted">{{ $item->description }}</div></td><td>{{ $item->category ?: '-' }}</td><td class="text-end">{{ number_format($item->purchase_price,2) }}</td><td class="text-end">{{ number_format($item->sale_price,2) }}</td><td class="text-end fw-bold">{{ number_format($item->stock_quantity,2) }} {{ $item->unit }}</td><td><span class="badge {{ $item->status?'bg-success':'bg-secondary' }}">{{ $item->status?'Active':'Inactive' }}</span> @can('inventory.manage')<button class="btn btn-sm btn-outline-primary ms-1" data-bs-toggle="modal" data-bs-target="#editItem{{ $item->id }}">Edit</button><div class="modal fade text-start" id="editItem{{ $item->id }}" tabindex="-1"><div class="modal-dialog modal-lg"><form method="post" action="{{ route('admin.inventory.update',$item) }}" class="modal-content">@csrf @method('PUT')<div class="modal-header"><h5 class="modal-title">Edit {{ $item->name }}</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body row g-3"><div class="col-md-4"><label class="form-label">SKU</label><input name="sku" value="{{ $item->sku }}" class="form-control" required></div><div class="col-md-8"><label class="form-label">Name</label><input name="name" value="{{ $item->name }}" class="form-control" required></div><div class="col-md-4"><label class="form-label">Category</label><input name="category" value="{{ $item->category }}" class="form-control"></div><div class="col-md-2"><label class="form-label">Unit</label><input name="unit" value="{{ $item->unit }}" class="form-control" required></div><div class="col-md-3"><label class="form-label">Purchase</label><input name="purchase_price" value="{{ $item->purchase_price }}" type="number" step=".01" min="0" class="form-control" required></div><div class="col-md-3"><label class="form-label">Sale</label><input name="sale_price" value="{{ $item->sale_price }}" type="number" step=".01" min="0" class="form-control" required></div><div class="col-md-4"><label class="form-label">Minimum stock</label><input name="minimum_stock" value="{{ $item->minimum_stock }}" type="number" step=".01" min="0" class="form-control" required></div><div class="col-md-4 d-flex align-items-end"><div class="form-check"><input type="hidden" name="status" value="0"><input class="form-check-input" type="checkbox" name="status" value="1" @checked($item->status)><label class="form-check-label">Active</label></div></div><div class="col-12"><label class="form-label">Description</label><textarea name="description" class="form-control">{{ $item->description }}</textarea></div></div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button><button class="btn btn-primary">Update</button></div></form></div></div>@endcan</td></tr>@empty<tr><td colspan="7" class="text-center py-4">No inventory items.</td></tr>@endforelse</tbody></table></div><div class="card-footer bg-white">{{ $items->links() }}</div></div>
<div class="card border-0 shadow-sm"><div class="card-header bg-white"><h3 class="h5 mb-0">Recent stock statement</h3></div><div class="table-responsive"><table class="table table-striped align-middle mb-0"><thead><tr><th>Date</th><th>Item</th><th>Type</th><th>Customer</th><th>Reference</th><th class="text-end">Qty</th><th class="text-end">Value</th><th>By</th></tr></thead><tbody>@forelse($transactions as $txn)<tr><td>{{ $txn->transaction_date?->format('d M Y') }}</td><td>{{ $txn->item?->name }}</td><td><span class="badge {{ in_array($txn->type,['purchase','return','adjustment_in'])?'bg-success':'bg-warning text-dark' }}">{{ str_replace('_',' ',ucfirst($txn->type)) }}</span></td><td>{{ $txn->customer?->name ?? '-' }}</td><td>{{ $txn->reference ?: '-' }}</td><td class="text-end">{{ number_format($txn->quantity,2) }}</td><td class="text-end">{{ number_format($txn->quantity*$txn->unit_price,2) }}</td><td>{{ $txn->creator?->name ?? '-' }}</td></tr>@empty<tr><td colspan="8" class="text-center py-4">No stock transactions.</td></tr>@endforelse</tbody></table></div></div>
@can('inventory.manage')
<div class="modal fade" id="itemModal" tabindex="-1"><div class="modal-dialog modal-lg"><form method="post" action="{{ route('admin.inventory.store') }}" class="modal-content">@csrf<div class="modal-header"><h5 class="modal-title">Add inventory item</h5><button class="btn-close" data-bs-dismiss="modal" type="button"></button></div><div class="modal-body"><div class="row g-3"><div class="col-md-4"><label class="form-label">SKU *</label><input name="sku" class="form-control" required></div><div class="col-md-8"><label class="form-label">Item name *</label><input name="name" class="form-control" required></div><div class="col-md-4"><label class="form-label">Category</label><input name="category" class="form-control"></div><div class="col-md-4"><label class="form-label">Unit *</label><input name="unit" value="pcs" class="form-control" required></div><div class="col-md-4"><label class="form-label">Opening stock</label><input name="opening_stock" value="0" type="number" step="0.01" min="0" class="form-control"></div><div class="col-md-4"><label class="form-label">Purchase price</label><input name="purchase_price" value="0" type="number" step="0.01" min="0" class="form-control" required></div><div class="col-md-4"><label class="form-label">Sale price</label><input name="sale_price" value="0" type="number" step="0.01" min="0" class="form-control" required></div><div class="col-md-4"><label class="form-label">Low stock alert</label><input name="minimum_stock" value="0" type="number" step="0.01" min="0" class="form-control" required></div><div class="col-12"><label class="form-label">Description</label><textarea name="description" class="form-control"></textarea></div></div></div><div class="modal-footer"><button class="btn btn-secondary" data-bs-dismiss="modal" type="button">Cancel</button><button class="btn btn-primary">Save item</button></div></form></div></div>
<div class="modal fade" id="transactionModal" tabindex="-1"><div class="modal-dialog modal-lg"><form method="post" action="{{ route('admin.inventory.transaction') }}" class="modal-content">@csrf<div class="modal-header"><h5 class="modal-title">Stock transaction</h5><button class="btn-close" data-bs-dismiss="modal" type="button"></button></div><div class="modal-body"><div class="row g-3"><div class="col-md-6"><label class="form-label">Item *</label><select name="inventory_item_id" class="form-select" required><option value="">Select</option>@foreach($activeItems as $item)<option value="{{ $item->id }}">{{ $item->sku }} — {{ $item->name }} ({{ $item->stock_quantity }} {{ $item->unit }})</option>@endforeach</select></div><div class="col-md-6"><label class="form-label">Type *</label><select name="type" id="stockType" class="form-select" required><option value="purchase">Purchase / Stock in</option><option value="issue">Issue to customer</option><option value="return">Return from customer</option><option value="adjustment_in">Adjustment in</option><option value="adjustment_out">Adjustment out</option></select></div><div class="col-md-6"><label class="form-label">Customer</label><select name="customer_id" class="form-select"><option value="">Not applicable</option>@foreach($customers as $customer)<option value="{{ $customer->id }}">{{ $customer->customer_code }} — {{ $customer->name }}</option>@endforeach</select></div><div class="col-md-3"><label class="form-label">Quantity *</label><input name="quantity" type="number" step="0.01" min="0.01" class="form-control" required></div><div class="col-md-3"><label class="form-label">Unit price</label><input name="unit_price" type="number" step="0.01" min="0" class="form-control"></div><div class="col-md-4"><label class="form-label">Date *</label><input name="transaction_date" type="date" value="{{ now()->toDateString() }}" class="form-control" required></div><div class="col-md-8"><label class="form-label">Reference</label><input name="reference" class="form-control" placeholder="Purchase invoice / device serial"></div><div class="col-12"><label class="form-label">Note</label><textarea name="note" class="form-control"></textarea></div></div></div><div class="modal-footer"><button class="btn btn-secondary" data-bs-dismiss="modal" type="button">Cancel</button><button class="btn btn-primary">Save transaction</button></div></form></div></div>
@endcan
@if($errors->any()) @push('scripts')<script>document.addEventListener('DOMContentLoaded',()=>Swal.fire({icon:'error',title:'Please check the form',html:@json(implode('<br>', $errors->all()))}));</script>@endpush @endif
</x-app-layout>
