mari kita buat manajemen stok
// app\Models\StockMovement.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class StockMovement extends Model
{
protected $fillable = [
'product_id',
'type', // 'in' atau 'out'
'quantity',
'description',
'user_id'
];
public function product()
{
return $this->belongsTo(Product::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
// database\migrations\2024_01_01_000004_create_stock_movements_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('stock_movements', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained()->onDelete('cascade');
$table->enum('type', ['in', 'out']);
$table->integer('quantity');
$table->text('description')->nullable();
$table->foreignId('user_id')->constrained();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('stock_movements');
}
};
// app\Http\Controllers\Inventory\StockController.php
<?php
namespace App\Http\Controllers\Inventory;
use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\StockMovement;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class StockController extends Controller
{
public function index()
{
$products = Product::with(['category', 'stock'])->paginate(10);
return view('inventory.stock.index', compact('products'));
}
public function adjust(Product $product)
{
return view('inventory.stock.adjust', compact('product'));
}
public function update(Request $request, Product $product)
{
$request->validate([
'type' => 'required|in:in,out',
'quantity' => 'required|integer|min:1',
'description' => 'required|string'
]);
try {
DB::beginTransaction();
// Cek stok cukup jika pengurangan
if ($request->type === 'out') {
if ($product->stock->quantity < $request->quantity) {
return back()->with('error', 'Stok tidak mencukupi!');
}
}
// Buat record stock movement
StockMovement::create([
'product_id' => $product->id,
'type' => $request->type,
'quantity' => $request->quantity,
'description' => $request->description,
'user_id' => auth()->id()
]);
// Update stok produk
$newQuantity = $request->type === 'in'
? $product->stock->quantity + $request->quantity
: $product->stock->quantity - $request->quantity;
$product->stock->update(['quantity' => $newQuantity]);
DB::commit();
return redirect()->route('stock.index')
->with('success', 'Stok berhasil diperbarui');
} catch (\Exception $e) {
DB::rollback();
return back()->with('error', 'Terjadi kesalahan saat memperbarui stok');
}
}
public function history(Product $product)
{
$movements = StockMovement::with(['user'])
->where('product_id', $product->id)
->latest()
->paginate(10);
return view('inventory.stock.history', compact('product', 'movements'));
}
}
// routes\web.php
<?php
use App\Http\Controllers\Dashboard\DashboardController;
use App\Http\Controllers\Inventory\StockController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return redirect()->route('dashboard');
});
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
// Tambahkan route untuk kategori
Route::resource('categories', \App\Http\Controllers\Inventory\CategoryController::class);
Route::resource('products', \App\Http\Controllers\Inventory\ProductController::class);
// Stock Management Routes
Route::get('/stock', [StockController::class, 'index'])->name('stock.index');
Route::get('/stock/{product}/adjust', [StockController::class, 'adjust'])->name('stock.adjust');
Route::post('/stock/{product}/update', [StockController::class, 'update'])->name('stock.update');
Route::get('/stock/{product}/history', [StockController::class, 'history'])->name('stock.history');
});
// resources\views\inventory\stock\index.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Manajemen Stok') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-6">
@if(session('success'))
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4">
{{ session('success') }}
</div>
@endif
<table class="min-w-full divide-y divide-gray-200">
<thead>
<tr>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Kode</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Produk</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Kategori</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Stok</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Aksi</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@foreach($products as $product)
<tr>
<td class="px-6 py-4 whitespace-nowrap">{{ $product->code }}</td>
<td class="px-6 py-4 whitespace-nowrap">{{ $product->name }}</td>
<td class="px-6 py-4 whitespace-nowrap">{{ $product->category->name }}</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $product->stock->quantity <= $product->stock->minimum_stock ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-800' }}">
{{ $product->stock->quantity }}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="{{ route('stock.adjust', $product) }}" class="text-indigo-600 hover:text-indigo-900 mr-3">Adjust Stok</a>
<a href="{{ route('stock.history', $product) }}" class="text-gray-600 hover:text-gray-900">History</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="mt-4">
{{ $products->links() }}
</div>
</div>
</div>
</div>
</x-app-layout>
// resources\views\inventory\stock\adjust.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Adjust Stok') }} - {{ $product->name }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-6">
@if(session('error'))
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4">
{{ session('error') }}
</div>
@endif
<form action="{{ route('stock.update', $product) }}" method="POST">
@csrf
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2">
Stok Saat Ini: {{ $product->stock->quantity }}
</label>
</div>
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2">
Tipe Adjustment
</label>
<select name="type" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
<option value="in">Stok Masuk</option>
<option value="out">Stok Keluar</option>
</select>
</div>
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2">
Jumlah
</label>
<input type="number" name="quantity" min="1" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2">
Keterangan
</label>
<textarea name="description" rows="3" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"></textarea>
</div>
<div class="flex items-center justify-between">
<button type="submit" class="bg-blue-500 hover:bg-blue-700 text-black font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Simpan
</button>
<a href="{{ route('stock.index') }}" class="text-gray-600 hover:text-gray-800">Batal</a>
</div>
</form>
</div>
</div>
</div>
</x-app-layout>
// resources\views\inventory\stock\history.blade.php
<x-app-layout>
<x-slot name="header">
<div class="flex justify-between items-center">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('History Stok') }} - {{ $product->name }}
</h2>
<a href="{{ route('stock.index') }}" class="bg-gray-500 hover:bg-gray-700 text-black font-bold py-2 px-4 rounded">
Kembali
</a>
</div>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-6">
<table class="min-w-full divide-y divide-gray-200">
<thead>
<tr>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Tanggal</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Tipe</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Jumlah</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Keterangan</th>
<th class="px-6 py-3 bg-gray-50 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@foreach($movements as $movement)
<tr>
<td class="px-6 py-4 whitespace-nowrap">{{ $movement->created_at->format('d/m/Y H:i') }}</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $movement->type === 'in' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
{{ $movement->type === 'in' ? 'Masuk' : 'Keluar' }}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap">{{ $movement->quantity }}</td>
<td class="px-6 py-4">{{ $movement->description }}</td>
<td class="px-6 py-4 whitespace-nowrap">{{ $movement->user->name }}</td>
</tr>
@endforeach
</tbody>
</table>
<div class="mt-4">
{{ $movements->links() }}
</div>
</div>
</div>
</div>
</x-app-layout>
// resources\views\navigation-menu.blade.php
<nav x-data="{ open: false }" class="bg-white border-b border-gray-100">
<!-- Primary Navigation Menu -->
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex">
<!-- Logo -->
<div class="shrink-0 flex items-center">
<a href="{{ route('dashboard') }}">
<x-application-mark class="block h-9 w-auto" />
</a>
</div>
<!-- Navigation Links -->
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-nav-link href="{{ route('dashboard') }}" :active="request()->routeIs('dashboard')">
{{ __('Dashboard') }}
</x-nav-link>
<!-- Tambahkan Menu Kategori -->
<x-nav-link href="{{ route('categories.index') }}" :active="request()->routeIs('categories.*')">
{{ __('Kategori') }}
</x-nav-link>
<!-- Tambahkan Menu Produk -->
<x-nav-link href="{{ route('products.index') }}" :active="request()->routeIs('products.*')">
{{ __('Produk') }}
</x-nav-link>
<!-- Tambahkan Menu Stok -->
<x-nav-link href="{{ route('stock.index') }}" :active="request()->routeIs('stock.*')">
{{ __('Stok') }}
</x-nav-link>
</div>
</div>
<!-- Settings Dropdown -->
<div class="hidden sm:flex sm:items-center sm:ml-6">
<x-dropdown align="right" width="48">
<x-slot name="trigger">
<button class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-gray-300 transition">
<img class="h-8 w-8 rounded-full object-cover" src="{{ Auth::user()->profile_photo_url }}" alt="{{ Auth::user()->name }}" />
</button>
</x-slot>
<x-slot name="content">
<!-- Account Management -->
<div class="block px-4 py-2 text-xs text-gray-400">
{{ __('Manage Account') }}
</div>
<x-dropdown-link href="{{ route('profile.show') }}">
{{ __('Profile') }}
</x-dropdown-link>
<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}" x-data>
@csrf
<x-dropdown-link href="{{ route('logout') }}"
@click.prevent="$root.submit();">
{{ __('Log Out') }}
</x-dropdown-link>
</form>
</x-slot>
</x-dropdown>
</div>
<!-- Hamburger -->
<div class="-mr-2 flex items-center sm:hidden">
<button @click="open = ! open" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500 transition">
<svg class="h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path :class="{'hidden': open, 'inline-flex': ! open }" class="inline-flex" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
<path :class="{'hidden': ! open, 'inline-flex': open }" class="hidden" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
</div>
<!-- Responsive Navigation Menu -->
<div :class="{'block': open, 'hidden': ! open}" class="hidden sm:hidden">
<div class="pt-2 pb-3 space-y-1">
<x-responsive-nav-link href="{{ route('dashboard') }}" :active="request()->routeIs('dashboard')">
{{ __('Dashboard') }}
</x-responsive-nav-link>
<!-- Responsive Menu Kategori -->
<x-responsive-nav-link href="{{ route('categories.index') }}" :active="request()->routeIs('categories.*')">
{{ __('Kategori') }}
</x-responsive-nav-link>
<!-- Responsive Menu Produk -->
<x-responsive-nav-link href="{{ route('products.index') }}" :active="request()->routeIs('products.*')">
{{ __('Produk') }}
</x-responsive-nav-link>
<!-- Responsive Menu Stok -->
<x-responsive-nav-link href="{{ route('stock.index') }}" :active="request()->routeIs('stock.*')">
{{ __('Stok') }}
</x-responsive-nav-link>
</div>
<!-- Responsive Settings Options -->
<div class="pt-4 pb-1 border-t border-gray-200">
<div class="flex items-center px-4">
<div class="shrink-0">
<img class="h-10 w-10 rounded-full" src="{{ Auth::user()->profile_photo_url }}" alt="{{ Auth::user()->name }}" />
</div>
<div class="ml-3">
<div class="font-medium text-base text-gray-800">{{ Auth::user()->name }}</div>
<div class="font-medium text-sm text-gray-500">{{ Auth::user()->email }}</div>
</div>
</div>
<div class="mt-3 space-y-1">
<!-- Account Management -->
<x-responsive-nav-link href="{{ route('profile.show') }}" :active="request()->routeIs('profile.show')">
{{ __('Profile') }}
</x-responsive-nav-link>
<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}" x-data>
@csrf
<x-responsive-nav-link href="{{ route('logout') }}"
@click.prevent="$root.submit();">
{{ __('Log Out') }}
</x-responsive-nav-link>
</form>
</div>
</div>
</div>
</nav>