input('perPage', 5); $query = Transfer::join(DB::raw('(SELECT peti_id, MAX(updated_at) as latest_update FROM transfers GROUP BY peti_id) as latest_peti'), function ($join) { $join->on('transfers.peti_id', '=', 'latest_peti.peti_id') ->on('transfers.updated_at', '=', 'latest_peti.latest_update'); }) ->orderBy('created_at', 'desc'); // Add search logic similar to the Peti index controller $search = $request->input('search') ?? ''; if ($search) { $query->where(function ($q) use ($search) { $q->where('name_customer', 'like', "%$search%") ->orWhereHas('peti', function ($petiQuery) use ($search) { $petiQuery->where('fix_lot', 'like', "%$search%"); }) ->orWhereHas('customer', function ($customerQuery) use ($search) { $customerQuery->where('name', 'like', "%$search%"); }) ->orWhereHas('sourceWarehouse', function ($sourceWarehouseQuery) use ($search) { $sourceWarehouseQuery->where('name', 'like', "%$search%"); }) ->orWhereHas('destinationWarehouse', function ($destinationWarehouseQuery) use ($search) { $destinationWarehouseQuery->where('name', 'like', "%$search%"); }) ->orWhere(function ($dateQuery) use ($search) { try { $formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d'); $dateQuery->whereDate('date', $formattedDate); } catch (\Exception $e) { Log::error('Error parsing date: ' . $e->getMessage()); } }); }); } if ($perPage == 'Semua') { $chunkSize = 100; $transfers = new Collection(); $currentPage = 1; $query->chunk($chunkSize, function ($transferChunk) use ($transfers, &$currentPage) { foreach ($transferChunk as $transfer) { $transfer->setAttribute('i', ($currentPage - 1) * $transferChunk->perPage() + 1); $transfers->push($transfer); $currentPage++; } }); } else { $transfers = $query->paginate($perPage); } $data = [ 'transfer' => $transfers, 'i' => ($transfers->currentPage() - 1) * $transfers->perPage() + 1, 'search' => $search, 'active' => 'menu-transfer', ]; return view('dashboard.Transfer.index', $data); } /** * Show the form for creating a new resource. */ public function create() { $petiWithStatusNotZeroOrEmptyAndActive = Peti::where(function ($query) { $query->whereHas('assetStatuses', function ($subquery) { $subquery->where('status', '!=', 0) ->where('created_at', '=', function ($maxQuery) { $maxQuery->selectRaw('MAX(created_at)') ->from('asset_statuses') ->whereColumn('peti_id', 'petis.id'); }); })->orWhereDoesntHave('assetStatuses'); })->where('status', 'aktif')->get(); $data = [ 'customer' => Customer::get(), 'peti' => $petiWithStatusNotZeroOrEmptyAndActive, 'warehouse' => m_warehouse::get(), 'active' => 'menu-transfer', ]; return view('dashboard.Transfer.create', $data); } /** * Store a newly created resource in storage. */ public function store(ValidasiCreateTransfer $request) { // dd($request); try { // Mendapatkan informasi pengguna yang sedang login $currentUser = Auth::user(); // Validasi data dari request $validatedData = $request->validated(); // Menambahkan informasi pengguna ke dalam data yang akan disimpan $validatedData['mobile_id'] = Uuid::v4(); // Menambahkan ID Mobile $validatedData['created_by'] = $currentUser->fullname; $validatedData['updated_by'] = $currentUser->fullname; // Membuat entitas Transfer $transfer = Transfer::create($validatedData); // dd($transfer); // Mengupdate warehouse_id pada model Peti Peti::where('id', $transfer->peti_id) ->update([ 't_warehouse_id' => $transfer->destination_warehouse, 'updated_by' => $currentUser->fullname, // 'customer_id' => $transfer->name_customer, // tambahan update customer id ]); return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil ditambahkan'); } catch (\Throwable $th) { return redirect()->back()->with('error', 'Data Transfer Peti gagal ditransfer'); } } /** * Show the form for editing the specified resource. */ public function edit($id) { $data = [ 'transfer' => Transfer::findOrFail($id), 'peti' => Peti::get(), 'customer' => Customer::get(), 'warehouse' => m_warehouse::get(), 'active' => 'menu-transfer', ]; return view('dashboard.Transfer.edit', $data); } /** * Update the specified resource in storage. */ public function update(ValidasiUpdateTransfer $request, $id) { // dd($request->all()); try { // Mendapatkan informasi pengguna yang sedang login $currentUser = Auth::user(); // Validasi data dari request $validatedData = $request->validated(); // Menambahkan informasi pengguna ke dalam data yang akan disimpan $validatedData['updated_by'] = $currentUser->fullname; // Mencari transfer berdasarkan ID $transfer = Transfer::find($id); if (!$transfer) { return redirect()->route('dashboard.transfer.index')->with('error', 'Data Transfer Peti tidak ditemukan'); } // Memperbarui data transfer $transfer->update($validatedData); // Mengupdate warehouse_id pada model Peti Peti::where('id', $transfer->peti_id) ->update([ 't_warehouse_id' => $transfer->destination_warehouse, // 'customer_id' => $transfer->name_customer, 'updated_by' => $currentUser->fullname, ]); return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil diperbaharui'); } catch (\Throwable $th) { return redirect()->back()->with('error', 'Data Transfer Peti gagal diperbaharui'); } } /** * Remove the specified resource from storage. */ public function destroy($id) { // dd("oke"); try { $typetransfer = Transfer::findOrFail($id); $typetransfer->delete(); return redirect()->back()->with('success', 'Data transfer peti berhasil dihapus'); } catch (\Throwable $th) { return redirect()->back()->with('error', 'Data transfer peti gagal dihapus'); } } // Proses select2 autocomplete public function autoCompleteSearch(Request $request): JsonResponse { $petiData = []; $customerData = []; if ($request->filled('q')) { $petiData = Peti::select("fix_lot", "id") ->where('fix_lot', 'LIKE', '%' . $request->get('q') . '%') ->where(function ($query) { $query->whereHas('assetStatuses', function ($subquery) { $subquery->where('status', '!=', 0) ->where('created_at', '=', function ($maxQuery) { $maxQuery->selectRaw('MAX(created_at)') ->from('asset_statuses') ->whereColumn('peti_id', 'petis.id'); }); })->orWhereDoesntHave('assetStatuses'); }) ->where('status', 'AKTIF') ->get(); } return response()->json(['peti' => $petiData, 'customer' => $customerData]); } // proses search Customer public function searchCustomers(Request $request): JsonResponse { $query = $request->get('q'); $customers = Customer::select('id', 'name') ->where('name', 'like', "%$query%") ->get() ->toArray(); return response()->json(['customers' => $customers]); } public function searchWarehouses(Request $request): JsonResponse { $query = $request->get('q'); $warehouses = m_warehouse::select('id', 'name') ->where('name', 'like', "%$query%") ->get() ->toArray(); return response()->json(['warehouses' => $warehouses]); } // proses auto select customer dan warehouse berdasarkan peti id public function getCustomerAndWarehouseByPeti(Request $request): JsonResponse { $petiId = $request->get('peti_id'); // Fetch customer data and warehouse based on the selected peti $peti = Peti::with(['customer', 't_warehouse']) ->where('id', $petiId) ->first(); $customerData = []; $selectedCustomer = null; $warehouseData = []; $selectedWarehouse = null; if ($peti && $peti->customer) { // Extract customer data from the related relationship $customerData[] = [ 'id' => $peti->customer->id, 'name' => $peti->customer->name, // Add any other fields you want to include ]; // Set the selectedCustomer to the customer id $selectedCustomer = $peti->customer->id; } if ($peti && $peti->t_warehouse) { // Extract warehouse data from the related relationship $warehouseData[] = [ 'id' => $peti->t_warehouse->id, 'name' => $peti->t_warehouse->name, // Add any other fields you want to include ]; // Set the selectedWarehouse to the warehouse id $selectedWarehouse = $peti->t_warehouse->id; } return response()->json([ 'customers' => $customerData, 'selectedCustomer' => $selectedCustomer, 'warehouses' => $warehouseData, 'selectedWarehouse' => $selectedWarehouse, ]); } }