first(); if($request->ajax()){ $data = Opname::with('user','dealer'); return DataTables::of($data) ->addIndexColumn() ->addColumn('user_name', function ($row){ return $row->user ? $row->user->name : '-'; }) ->addColumn('dealer_name', function ($row){ return $row->dealer ? $row->dealer->name : '-'; }) ->addColumn('action', function ($row) use ($menu) { $btn = '
'; $btn .= ''; $btn .= '
'; return $btn; }) ->rawColumns(['action']) ->make(true); } return view('warehouse_management.opnames.index'); } public function create(){ try{ $dealers = Dealer::all(); $products = Product::all(); return view('warehouse_management.opnames.create', compact('dealers','products')); }catch(\Exception $ex){ Log::error($ex->getMessage()); } } public function store(Request $request){ try{ $request->validate([ 'dealer' => 'required|exists:dealers,id', 'product' => 'required|array', 'product.*' => 'nullable|exists:products,id', 'system_quantity' => 'required|array', 'physical_quantity' => 'required|array', ]); // 1. Create Opname master record $opname = Opname::create([ 'dealer_id' => $request->dealer, 'opname_date' => now(), // or $request->opname_date if you provide it 'user_id' => auth()->id(), // assuming the user is logged in 'note' => null, // or $request->note if needed ]); // 2. Loop over products to create OpnameDetails foreach ($request->product as $index => $productId) { if (!$productId) continue; // Skip empty rows $system = $request->system_quantity[$index] ?? 0; $physical = $request->physical_quantity[$index] ?? 0; OpnameDetail::create([ 'opname_id' => $opname->id, 'product_id' => $productId, 'system_stock' => $system, 'physical_stock' => $physical, 'difference' => $physical - $system, 'note' => null, // or include from input ]); } return redirect()->route('opnames.index') ->with('success', 'Opname berhasil disimpan.'); }catch(\Exception $ex){ Log::error($ex->getMessage()); } } }