diff --git a/app/Http/Controllers/HistoryController.php b/app/Http/Controllers/HistoryController.php index 7becc99..f2dab39 100644 --- a/app/Http/Controllers/HistoryController.php +++ b/app/Http/Controllers/HistoryController.php @@ -6,15 +6,108 @@ use App\Models\Transfer; use App\Models\m_warehouse; use App\Models\asset_status; use Illuminate\Http\Request; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; class HistoryController extends Controller { - public function historyPeminjaman() + // public function historyPeminjaman() + // { + + + // $data = [ + // 'peminjaman' => asset_status::withTrashed()->orderBy('created_at', 'desc')->get(), + // 'warehouse' => m_warehouse::get(), + // 'active' => 'history-peminjaman', + // ]; + + // return view('dashboard.History.Peminjaman.index', $data); + // } + public function historyPeminjaman(Request $request) { + $perPage = $request->input('perPage', 5); + + $query = asset_status::with(['customer', 'warehouseId', 'warehouse', 'warehouseEnter', 'kondisi_peti']) + ->orderBy('created_at', 'desc'); + + //logika pencarian + $search = $request->input('search') ?? ''; + if ($search) { + $query->where(function ($q) use ($search) { + $q->where('mobile_id', 'like', "%$search%") + ->orWhereHas('peti', function ($warehouseQuery) use ($search) { + $warehouseQuery->where('fix_lot', 'like', "%$search%"); + }) + ->orWhereHas('customer', function ($warehouseQuery) use ($search) { + $warehouseQuery->where('name', 'like', "%$search%"); + }) + // ->orWhereHas('peti.customer', function ($customerQuery) use ($search) { + // $customerQuery->where('code_customer', 'like', "%$search%"); + // }) + // ->orWhereHas('peti.tipe_peti', function ($tipePetiQuery) use ($search) { + // $tipePetiQuery->where('type', 'like', "%$search%"); + // }) + // ->orWhere(function ($combinedQuery) use ($search) { + // // Pisahkan code customer dan tipe peti dari pencarian + // list($codeCustomer, $typePeti) = explode(' - ', $search); + // // Cek kesamaan code customer dan tipe peti + // $combinedQuery->whereHas('peti.customer', function ($customerQuery) use ($codeCustomer) { + // $customerQuery->where('code_customer', 'like', "%$codeCustomer%"); + // })->whereHas('peti.tipe_peti', function ($tipePetiQuery) use ($typePeti) { + // $tipePetiQuery->where('type', 'like', "%$typePeti%"); + // }); + // }) + ->orWhere(function ($dateQuery) use ($search) { + try { + // Format tanggal yang diharapkan dari input pengguna + $formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d'); + + // Cek kesamaan tanggal + $dateQuery->whereDate('exit_at', $formattedDate); + } catch (\Exception $e) { + Log::error('Error parsing date: ' . $e->getMessage()); + } + }) + ->orWhere(function ($dateQuery) use ($search) { + try { + // Format tanggal yang diharapkan dari input pengguna + $formattedDate = \Carbon\Carbon::createFromFormat('d-m-Y', $search)->format('Y-m-d'); + + // Cek kesamaan tanggal + $dateQuery->whereDate('est_pengembalian', $formattedDate); + } catch (\Exception $e) { + Log::error('Error parsing date: ' . $e->getMessage()); + } + }) + ->orWhere('exit_pic', 'like', "%$search%") + ->orWhereHas('warehouse', function ($warehouseQuery) use ($search) { + $warehouseQuery->where('name', 'like', "%$search%") + ->orWhere('address', 'like', "%$search%"); + }); + }); + } + + if ($perPage == 'Semua') { + $chunkSize = 100; + $stores = new Collection(); + $currentPage = 1; + + $query->chunk($chunkSize, function ($storesChunk) use ($stores, &$currentPage) { + foreach ($storesChunk as $store) { + $store->setAttribute('i', ($currentPage - 1) * $storesChunk->perPage() + 1); + $stores->push($store); + $currentPage++; + } + }); + } else { + $stores = $query->paginate($perPage); + } $data = [ - 'peminjaman' => asset_status::withTrashed()->orderBy('created_at', 'desc')->get(), + 'peminjaman' => $stores, 'warehouse' => m_warehouse::get(), + 'i' => ($stores->currentPage() - 1) * $stores->perPage() + 1, + 'search' => $search, 'active' => 'history-peminjaman', ]; diff --git a/app/Http/Controllers/PeminjamanController.php b/app/Http/Controllers/PeminjamanController.php index 5b87d08..d9551ff 100644 --- a/app/Http/Controllers/PeminjamanController.php +++ b/app/Http/Controllers/PeminjamanController.php @@ -40,29 +40,6 @@ class PeminjamanController extends Controller ->orWhereHas('customer', function ($warehouseQuery) use ($search) { $warehouseQuery->where('name', 'like', "%$search%"); }) - ->orWhereHas('peti.customer', function ($customerQuery) use ($search) { - $customerQuery->where('code_customer', 'like', "%$search%"); - }) - ->orWhereHas('peti.tipe_peti', function ($tipePetiQuery) use ($search) { - $tipePetiQuery->where('type', 'like', "%$search%"); - }) - ->orWhere(function ($combinedQuery) use ($search) { - // Pisahkan code customer dan tipe peti dari pencarian - list($codeCustomer, $typePeti) = explode(' - ', $search); - - // Cek kesamaan code customer dan tipe peti - $combinedQuery->whereHas('peti.customer', function ($customerQuery) use ($codeCustomer) { - $customerQuery->where('code_customer', 'like', "%$codeCustomer%"); - })->whereHas('peti.tipe_peti', function ($tipePetiQuery) use ($typePeti) { - $tipePetiQuery->where('type', 'like', "%$typePeti%"); - }); - }) - ->orWhere('exit_pic', 'like', "%$search%") - ->orWhereHas('warehouse', function ($warehouseQuery) use ($search) { - $warehouseQuery->where('name', 'like', "%$search%") - ->orWhere('address', 'like', "%$search%"); - }) - ->orWhere(function ($dateQuery) use ($search) { try { // Format tanggal yang diharapkan dari input pengguna diff --git a/resources/views/dashboard/History/Peminjaman/index.blade.php b/resources/views/dashboard/History/Peminjaman/index.blade.php index fbd96fe..1dd2050 100644 --- a/resources/views/dashboard/History/Peminjaman/index.blade.php +++ b/resources/views/dashboard/History/Peminjaman/index.blade.php @@ -18,8 +18,44 @@
No | @@ -32,12 +68,9 @@||||||
---|---|---|---|---|---|---|
{{ $no_peminjaman++ }} | +{{ $i++ }} | {{ $data_peminjaman->peti->fix_lot }} | {{ $data_peminjaman->peti->customer->name }} | {{ $data_peminjaman->peti->customer->code_customer }} - @@ -47,10 +80,46 @@ | {{ $data_peminjaman->warehouse->name }} | |
Data Kosong | +
Showing {{ $peminjaman->firstItem() }} to + {{ $peminjaman->lastItem() }} of + {{ $peminjaman->total() }} entries
+ @endif +