|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Inventory\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Inventory\Models\CostingLayer; |
| 7 | +use App\Modules\Inventory\Models\Product; |
| 8 | +use App\Modules\Inventory\Models\ProductCostSnapshot; |
| 9 | +use Illuminate\Http\RedirectResponse; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Inertia\Inertia; |
| 12 | +use Inertia\Response; |
| 13 | + |
| 14 | +class CostingController extends Controller |
| 15 | +{ |
| 16 | + public function index(Request $request): Response |
| 17 | + { |
| 18 | + $this->authorize('viewAny', CostingLayer::class); |
| 19 | + |
| 20 | + $products = Product::withCount(['costingLayers' => fn ($q) => $q->where('quantity_remaining', '>', 0)]) |
| 21 | + ->orderBy('name') |
| 22 | + ->paginate(20); |
| 23 | + |
| 24 | + $filters = $request->only(['product_id']); |
| 25 | + |
| 26 | + return Inertia::render('Inventory/Costing/Index', compact('products', 'filters')); |
| 27 | + } |
| 28 | + |
| 29 | + public function layers(Request $request, Product $product): Response |
| 30 | + { |
| 31 | + $this->authorize('view', CostingLayer::class); |
| 32 | + |
| 33 | + $layers = CostingLayer::forProduct($product->id) |
| 34 | + ->withRemaining() |
| 35 | + ->fifo() |
| 36 | + ->paginate(20); |
| 37 | + |
| 38 | + return Inertia::render('Inventory/Costing/Layers', compact('product', 'layers')); |
| 39 | + } |
| 40 | + |
| 41 | + public function addLayer(Request $request): RedirectResponse |
| 42 | + { |
| 43 | + $this->authorize('create', CostingLayer::class); |
| 44 | + |
| 45 | + $data = $request->validate([ |
| 46 | + 'product_id' => 'required|exists:products,id', |
| 47 | + 'costing_method' => 'required|in:fifo,avco', |
| 48 | + 'quantity' => 'required|numeric|min:0.0001', |
| 49 | + 'unit_cost' => 'required|numeric|min:0', |
| 50 | + 'received_at' => 'nullable|date', |
| 51 | + 'reference_type' => 'nullable|string|max:50', |
| 52 | + ]); |
| 53 | + |
| 54 | + CostingLayer::create([ |
| 55 | + 'tenant_id' => auth()->user()->tenant_id, |
| 56 | + 'product_id' => $data['product_id'], |
| 57 | + 'costing_method' => $data['costing_method'], |
| 58 | + 'quantity_received' => $data['quantity'], |
| 59 | + 'quantity_remaining' => $data['quantity'], |
| 60 | + 'unit_cost' => $data['unit_cost'], |
| 61 | + 'received_at' => $data['received_at'] ?? now(), |
| 62 | + 'reference_type' => $data['reference_type'] ?? null, |
| 63 | + ]); |
| 64 | + |
| 65 | + return back()->with('success', 'Costing layer added.'); |
| 66 | + } |
| 67 | + |
| 68 | + public function snapshot(Request $request): RedirectResponse |
| 69 | + { |
| 70 | + $this->authorize('create', CostingLayer::class); |
| 71 | + |
| 72 | + $data = $request->validate([ |
| 73 | + 'product_id' => 'required|exists:products,id', |
| 74 | + ]); |
| 75 | + |
| 76 | + ProductCostSnapshot::takeSnapshot(auth()->user()->tenant_id, $data['product_id']); |
| 77 | + |
| 78 | + return back()->with('success', 'Snapshot taken.'); |
| 79 | + } |
| 80 | + |
| 81 | + public function report(Request $request): Response |
| 82 | + { |
| 83 | + $this->authorize('viewAny', CostingLayer::class); |
| 84 | + |
| 85 | + /** @var \App\Models\User $user */ |
| 86 | + $user = auth()->user(); |
| 87 | + |
| 88 | + $products = Product::withCount(['costingLayers' => fn ($q) => $q->where('quantity_remaining', '>', 0)]) |
| 89 | + ->orderBy('name') |
| 90 | + ->limit(50) |
| 91 | + ->get(); |
| 92 | + |
| 93 | + $rows = $products->map(fn ($product) => [ |
| 94 | + 'product' => $product, |
| 95 | + 'average_cost' => CostingLayer::getAverageCost($user->tenant_id, $product->id), |
| 96 | + 'layers_count' => $product->costing_layers_count, |
| 97 | + ])->all(); |
| 98 | + |
| 99 | + return Inertia::render('Inventory/Costing/Report', compact('rows')); |
| 100 | + } |
| 101 | +} |
0 commit comments