From 0bb12812a578e5189b9f73af0870e7b792839226 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Mon, 19 May 2025 08:56:44 +0700 Subject: [PATCH] feat(branch): tambahkan fitur filter dan search pada eksport dan tabel cabang - Menambahkan parameter `search` dan `parent_id` pada `BranchExport` untuk mendukung fitur filter. - Memodifikasi method `collection` di `BranchExport` agar mendukung filter pencarian dan parent cabang. - Memperbaiki issue pada method `collection` terkait penggunaan query `LOWER` untuk pencarian. - Mengubah method `export` di `BranchController` agar menerima parameter filter dari request. - Menambahkan logika filtering untuk `search` dan `parent_id` pada method index API `BranchController`. - Menambahkan dropdown filter parent di tampilan `branch/index.blade.php`. - Implementasi JavaScript di `branch/index.blade.php` untuk mendukung filter pencarian dan parent cabang. - Menambahkan logika sinkronisasi URL eksport dengan parameter filter. - Menambahkan event listener untuk filter pencarian dan dropdown parent. - Menambahkan validasi agar filter diterapkan ke datatable dan URL eksport secara dinamis. - Memperbaiki penghitungan halaman pagination di datatable. - Penyesuaian minor pada model Branch dan cara logging aktivitas di model Base. Fitur ini memungkinkan pengguna melakukan filter data cabang berdasarkan pencarian dan parent cabang saat menampilkan tabel ataupun mengekspor data ke Excel. Signed-off-by: Daeng Deni Mardaeni --- app/Exports/BranchExport.php | 33 ++++++++++- app/Http/Controllers/BranchController.php | 33 ++++++++--- app/Models/Base.php | 2 - app/Models/Branch.php | 1 - resources/views/branch/index.blade.php | 68 +++++++++++++++++++++-- 5 files changed, 119 insertions(+), 18 deletions(-) diff --git a/app/Exports/BranchExport.php b/app/Exports/BranchExport.php index 702c322..0a62ebc 100644 --- a/app/Exports/BranchExport.php +++ b/app/Exports/BranchExport.php @@ -9,11 +9,38 @@ use Modules\Basicdata\Models\Branch; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; - class BranchExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping + class BranchExport implements WithColumnFormatting, WithHeadings, FromCollection, WithMapping { + protected $search; + + public function __construct($search = null, $parent_id = null) + { + $this->search = $search; + $this->parent_id = $parent_id; + } + public function collection() { - return Branch::all(); + $query = Branch::query(); + + if (!empty($this->search)) { + $search = strtolower($this->search); + $query->where(function ($q) use ($search) { + $q->whereRaw('LOWER(code) LIKE ?', ['%' . $search . '%']) + ->orWhereRaw('LOWER(name) LIKE ?', ['%' . $search . '%']) + ->orWhereHas('parent', function ($q) use ($search) { + $q->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%']); + }); + }); + } + + // Apply parent filter if provided + if (isset($this->parent_id) && !empty($this->parent_id)) { + $parentId = $this->parent_id; + $query->where('parent_id', $parentId); + } + + return $query->get(); } public function map($row) @@ -23,6 +50,7 @@ $row->id, $row->code, $row->name, + $row->parent ? $row->parent->name : '', $row->created_at ]; } @@ -34,6 +62,7 @@ 'ID', 'Code', 'Name', + 'Parent Branch', 'Created At' ]; } diff --git a/app/Http/Controllers/BranchController.php b/app/Http/Controllers/BranchController.php index 65860b7..0178c5b 100644 --- a/app/Http/Controllers/BranchController.php +++ b/app/Http/Controllers/BranchController.php @@ -182,11 +182,24 @@ // Apply search filter if provided if ($request->has('search') && !empty($request->get('search'))) { - $search = $request->get('search'); - $query->where(function ($q) use ($search) { - $q->where('code', 'LIKE', "%$search%"); - $q->orWhere('name', 'LIKE', "%$search%"); - }); + $search = json_decode($request->get('search')); + + if(isset($search->search)) { + $search_ = strtolower($search->search); + $query->where(function ($q) use ($search_) { + $q->whereRaw('LOWER(code) LIKE ?', ['%' . strtolower($search_) . '%']); + $q->orWhereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search_) . '%']); + $q->orWhereHas('parent', function ($q) use ($search_) { + $q->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search_) . '%']); + }); + }); + } + + // Apply parent filter if provided + if (isset($search->parent_id) && !empty($search->parent_id)) { + $parentId = $search->parent_id; + $query->where('parent_id', $parentId); + } } // Apply sorting if provided @@ -227,7 +240,7 @@ $pageCount = ceil($totalRecords / $request->get('size')); // Calculate the current page number - $currentPage = 0 + 1; + $currentPage = $request->get('page') ?: 1; // Return the response data as a JSON object @@ -242,13 +255,17 @@ ]); } - public function export() + public function export(Request $request) { // Check if the authenticated user has the required permission to export branches if (is_null($this->user) || !$this->user->can('basic-data.export')) { abort(403, 'Sorry! You are not allowed to export branches.'); } - return Excel::download(new BranchExport, 'branch.xlsx'); + // Get search parameter from request + $search = $request->get('search'); + $parentId = $request->get('parent_id'); + + return Excel::download(new BranchExport($search,$parentId), 'branch.xlsx'); } } diff --git a/app/Models/Base.php b/app/Models/Base.php index 09da381..252e4b7 100644 --- a/app/Models/Base.php +++ b/app/Models/Base.php @@ -47,8 +47,6 @@ public function getActivitylogOptions() : LogOptions { - //CauserResolver::setCauser(auth()->user()); - return LogOptions::defaults()->logAll()->useLogName('Basic Data'); } } diff --git a/app/Models/Branch.php b/app/Models/Branch.php index 367a2ce..4c9b790 100644 --- a/app/Models/Branch.php +++ b/app/Models/Branch.php @@ -2,7 +2,6 @@ namespace Modules\Basicdata\Models; - class Branch extends Base { protected $table = 'branches'; diff --git a/resources/views/branch/index.blade.php b/resources/views/branch/index.blade.php index f8ceee8..c429c4b 100644 --- a/resources/views/branch/index.blade.php +++ b/resources/views/branch/index.blade.php @@ -12,15 +12,21 @@ Daftar Cabang
-
+
+
-
+
@can('basic-data.export') - Export to Excel + Export to Excel @endcan @can('basic-data.create') Tambah Cabang @@ -149,7 +155,9 @@