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