Order::with(['items.product'])->latest()->get(), ]); } public function create(): Response { return Inertia::render('admin/manage/order/create', [ 'products' => Product::with(['prices', 'categories'])->active()->get(), 'cartItems' => OrderItem::with(['product.prices', 'product.categories']) ->where('user_id', auth()->id()) ->whereNull('order_id') ->latest() ->get(), 'orderStatus' => collect(OrderStatus::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), 'orderChannels' => collect(OrderChannel::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), 'paymentMethods' => collect(PaymentMethod::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), 'priceTypes' => collect(PriceType::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), ]); } public function store(OrderRequest $request): RedirectResponse { $validated = $request->validated(); DB::transaction(function () use ($validated) { $totalItemsPrice = collect($validated['items'])->sum('total'); $hpp = collect($validated['items'])->sum(function ($item) { $product = Product::find($item['product_id']); $purchasePrice = $product->prices()->where('price_type', PriceType::PURCHASE)->first()?->price ?? 0; return $purchasePrice * $item['qty']; }); $order = Order::create([ 'user_id' => auth()->id(), 'invoice_number' => $validated['invoice_number'] ?? 'INV-'.now()->format('YmdHis').'-'.strtoupper(fake()->bothify('??##')), 'customer_name' => $validated['customer_name'], 'hpp' => $hpp, 'discount' => $validated['discount'], 'payment' => $validated['payment'], 'total' => $totalItemsPrice - $validated['discount'], 'payment_method' => $validated['payment_method'], 'order_status' => $validated['order_status'], 'order_channel' => $validated['order_channel'], ]); foreach ($validated['items'] as $item) { OrderItem::create([ 'user_id' => auth()->id(), 'order_id' => $order->id, 'product_id' => $item['product_id'], 'price' => $item['price'], 'qty' => $item['qty'], 'total' => $item['total'], 'price_type' => $item['price_type'], ]); Product::find($item['product_id'])->decrement('stock', $item['qty']); } OrderItem::where('user_id', auth()->id()) ->whereNull('order_id') ->delete(); }); return redirect()->route('order.index')->with('success', 'Pesanan berhasil disimpan'); } public function edit(Order $order): Response { $order->load(['items.product']); return Inertia::render('admin/manage/order/edit', [ 'order' => $order, 'products' => Product::with(['prices', 'categories'])->active()->get(), 'orderStatus' => collect(OrderStatus::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), 'orderChannels' => collect(OrderChannel::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), 'paymentMethods' => collect(PaymentMethod::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), 'priceTypes' => collect(PriceType::cases())->map(fn ($case) => ['value' => $case->value, 'label' => $case->label()]), ]); } public function update(OrderRequest $request, Order $order): RedirectResponse { $validated = $request->validated(); DB::transaction(function () use ($validated, $order) { $totalItemsPrice = collect($validated['items'])->sum(fn ($item) => $item['qty'] * $item['price']); $hpp = collect($validated['items'])->sum(function ($item) { $product = Product::find($item['product_id']); $purchasePrice = $product->prices()->where('price_type', PriceType::PURCHASE)->first()?->price ?? 0; return $purchasePrice * $item['qty']; }); // Restore stock for old items foreach ($order->items as $item) { $item->product->increment('stock', $item->qty); } $order->items()->delete(); $order->update([ 'customer_name' => $validated['customer_name'], 'hpp' => $hpp, 'discount' => $validated['discount'], 'payment' => $validated['payment'], 'total' => $totalItemsPrice - $validated['discount'], 'payment_method' => $validated['payment_method'], 'order_status' => $validated['order_status'], 'order_channel' => $validated['order_channel'], ]); foreach ($validated['items'] as $item) { $order->items()->create([ 'user_id' => auth()->id(), 'product_id' => $item['product_id'], 'price' => $item['price'], 'qty' => $item['qty'], 'total' => $item['total'], 'price_type' => $item['price_type'], ]); Product::find($item['product_id'])->decrement('stock', $item['qty']); } }); return redirect()->route('order.index')->with('success', 'Pesanan berhasil diperbarui'); } public function destroy(Order $order): RedirectResponse { DB::transaction(function () use ($order) { foreach ($order->items as $item) { $item->product->increment('stock', $item->qty); } $order->delete(); }); return redirect()->back()->with('success', 'Pesanan berhasil dihapus'); } public function bulkDestroy(Request $request): RedirectResponse { $ids = $request->input('ids'); DB::transaction(function () use ($ids) { $orders = Order::with('items')->whereIn('id', $ids)->get(); foreach ($orders as $order) { foreach ($order->items as $item) { $item->product->increment('stock', $item->qty); } $order->delete(); } }); return redirect()->back()->with('success', 'Pesanan terpilih berhasil dihapus'); } }