diff --git a/DataTables/BranchDataTable.php b/DataTables/BranchDataTable.php index f39ecb9..85b3394 100644 --- a/DataTables/BranchDataTable.php +++ b/DataTables/BranchDataTable.php @@ -8,6 +8,7 @@ use Yajra\DataTables\Html\Builder as HtmlBuilder; use Yajra\DataTables\Html\Column; use Yajra\DataTables\Services\DataTable; + use Nwidart\Modules\Facades\Module; class BranchDataTable extends DataTable { @@ -20,14 +21,16 @@ : EloquentDataTable { return (new EloquentDataTable($query))->filter(function ($query) { - if (request()->has('search')) { - $search = request()->get('search'); - $query->where('kode', 'like', "%" . $search['value'] . "%") - ->orWhere('name', 'like', "%" . $search['value'] . "%"); - } - })->addIndexColumn()->editColumn('updated_at', function ($row) { - return $row->updated_at->format('d-m-Y H:i:s'); - })->addColumn('action', 'writeoff::parameter.branch._action')->setRowId('id'); + if (request()->has('search')) { + $search = request()->get('search'); + $query->where('kode', 'like', "%" . $search['value'] . "%") + ->orWhere('name', 'like', "%" . $search['value'] . "%"); + } + })->addIndexColumn()->editColumn('updated_at', function ($row) { + return $row->updated_at->format('d-m-Y H:i:s'); + })->rawColumns(['action'])->addColumn('action', function ($branch) { + return view('writeoff::parameter.branches._actions', compact('branch')); + })->setRowId('id'); } /** @@ -57,7 +60,8 @@ 'scrollX' => false, 'drawCallback' => 'function() { KTMenu.createInstances(); }', ]) - ->addTableClass('align-middle table-row-dashed fs-6 gy-5'); + ->addTableClass('align-middle table-row-dashed fs-6 gy-5') + ->drawCallback("function() {" . file_get_contents(Module::getModulePath('writeoff').'Resources/views/parameter/branches/_draw-scripts.js') . "}"); } /** @@ -68,9 +72,9 @@ { return [ Column::make('DT_RowIndex')->title('No')->orderable(false)->searchable(false), - Column::make('kode')->name('Kode Branch'), - Column::make('name')->name('Nama Branch'), - Column::make('updated_at')->name('Last Update')->visible(false), + Column::make('kode')->title('Kode Branch'), + Column::make('name')->title('Nama Branch'), + Column::make('updated_at')->title('Last Update')->visible(false), Column::computed('action')->exportable(false)->printable(false)->width(60)->addClass('text-center'), ]; } diff --git a/Http/Livewire/Branch/BranchModal.php b/Http/Livewire/Branch/BranchModal.php deleted file mode 100644 index c627908..0000000 --- a/Http/Livewire/Branch/BranchModal.php +++ /dev/null @@ -1,87 +0,0 @@ - 'required|string|unique:branches,kode', - 'name' => 'required|string', - ]; - - // This is the list of listeners that this component listens to. - protected $listeners = [ - 'modal.show.brahcn' => 'mountBranch', - 'delete_branch' => 'delete' - ]; - - public function render() - { - return view('branch::livewire.branch.branch-modal'); - } - - public function mountBranch($branch_name = '', $branch_kode='') - { - if (empty($branch_kode)) { - // Create new - $this->branch = new Branch; - $this->kode = ''; - if (empty($branch_name)) { - $this->name = ''; - } - return; - } - - // Get the role by name. - $permission = Branch::where('kode', $branch_kode)->first(); - if (is_null($permission)) { - $this->emit('error', 'The selected kode [' . $branch_kode . '] is not found'); - return; - } - - $this->branch = $permission; - - // Set the name and checked permissions properties to the role's values. - $this->kode = $this->branch->kode; - $this->name = $this->branch->name; - } - - public function submit() - { - $this->validate(); - - $this->branch->kode = strtoupper($this->kode); - $this->branch->name = strtolower($this->name); - if ($this->branch->isDirty()) { - $this->branch->save(); - } - - // Emit a success event with a message indicating that the permissions have been updated. - $this->emit('success', 'Branch updated'); - } - - public function delete($kode) - { - $branch = Branch::where('kode', $kode)->first(); - - if (!is_null($branch)) { - $branch->delete(); - } - - $this->emit('success', 'Branch deleted'); - } - - public function hydrate() - { - $this->resetErrorBag(); - $this->resetValidation(); - } -} diff --git a/Livewire/Branch/BranchModal.php b/Livewire/Branch/BranchModal.php new file mode 100644 index 0000000..b53d69a --- /dev/null +++ b/Livewire/Branch/BranchModal.php @@ -0,0 +1,91 @@ + 'required|string|max:255|unique:branches,kode', + 'name' => 'required|string', + ]; + + protected $listeners = [ + 'delete_branch' => 'deleteBranch', + 'update_branch' => 'updateBranch', + ]; + + public function render() + { + return view('writeoff::livewire.branch.branch-modal'); + } + + public function submit() + { + // Validate the form input data + $this->validate(); + + DB::transaction(function () { + // Prepare the data for creating a new user + $data = [ + 'name' => $this->name, + ]; + + // Create a new user record in the database + $user = Branch::updateOrCreate([ + 'kode' => $this->kode, + ], $data); + + if ($this->edit_mode) { + // Emit a success event with a message + $this->dispatch('success', __('Branch updated')); + } else { + // Emit a success event with a message + $this->dispatch('success', __('New Branch created')); + } + }); + + // Reset the form fields after successful submission + $this->reset(); + } + + public function deleteBranch($id) + { + // Prevent deletion of current user + if ($id == Auth::id()) { + $this->dispatch('error', 'Branch cannot be deleted'); + return; + } + + // Delete the user record with the specified ID + Branch::destroy($id); + + // Emit a success event with a message + $this->dispatch('success', 'Branch successfully deleted'); + } + + public function updateBranch($id) + { + $this->edit_mode = true; + + $branch = Branch::find($id); + + $this->kode = $branch->kode; + $this->name = $branch->name; + } + + public function hydrate() + { + $this->resetErrorBag(); + $this->resetValidation(); + } + } + diff --git a/Resources/views/livewire/branch/branch-modal.blade.php b/Resources/views/livewire/branch/branch-modal.blade.php new file mode 100644 index 0000000..a20ae14 --- /dev/null +++ b/Resources/views/livewire/branch/branch-modal.blade.php @@ -0,0 +1,72 @@ + diff --git a/Resources/views/parameter/branches/_actions.blade.php b/Resources/views/parameter/branches/_actions.blade.php new file mode 100644 index 0000000..e573554 --- /dev/null +++ b/Resources/views/parameter/branches/_actions.blade.php @@ -0,0 +1,23 @@ + + Actions + + + + + diff --git a/Resources/views/parameter/branches/_draw-scripts.js b/Resources/views/parameter/branches/_draw-scripts.js new file mode 100644 index 0000000..30c605e --- /dev/null +++ b/Resources/views/parameter/branches/_draw-scripts.js @@ -0,0 +1,37 @@ +// Initialize KTMenu +KTMenu.init(); + +// Add click event listener to delete buttons +document.querySelectorAll('[data-kt-action="delete_row"]').forEach(function (element) { + element.addEventListener('click', function () { + Swal.fire({ + text: 'Are you sure you want to remove?', + icon: 'warning', + buttonsStyling: false, + showCancelButton: true, + confirmButtonText: 'Yes', + cancelButtonText: 'No', + customClass: { + confirmButton: 'btn btn-danger', + cancelButton: 'btn btn-secondary', + } + }).then((result) => { + if (result.isConfirmed) { + Livewire.dispatch('delete_branch', this.getAttribute('data-kt-branch-id')); + } + }); + }); +}); + +// Add click event listener to update buttons +document.querySelectorAll('[data-kt-action="update_row"]').forEach(function (element) { + element.addEventListener('click', function () { + Livewire.dispatch('update_branch', this.getAttribute('data-kt-branch-id')); + }); +}); + +// Listen for 'success' event emitted by Livewire +Livewire.on('success', (message) => { + // Reload the users-table datatable + LaravelDataTables['branch-table'].ajax.reload(); +}); diff --git a/Resources/views/parameter/branches/index.blade.php b/Resources/views/parameter/branches/index.blade.php index e69de29..a85666a 100644 --- a/Resources/views/parameter/branches/index.blade.php +++ b/Resources/views/parameter/branches/index.blade.php @@ -0,0 +1,72 @@ + + + @section('title') + Cabang + @endsection + + @section('breadcrumbs') + {{ Breadcrumbs::render('parameter.branches') }} + @endsection + +
+ +
+ +
+ +
+ {!! getIcon('magnifier', 'fs-3 position-absolute ms-5') !!} + +
+ +
+ + + +
+ +
+ + + +
+ + + + + +
+ +
+ + + +
+ +
+ {{ $dataTable->table() }} +
+ +
+ +
+ + @push('scripts') + {{ $dataTable->scripts() }} + + @endpush + +
diff --git a/module.json b/module.json index 52e934a..8a9dc66 100644 --- a/module.json +++ b/module.json @@ -1,6 +1,7 @@ { "name": "Writeoff", "alias": "writeoff", + "database": "", "description": "", "keywords": [], "priority": 0,