From 32c83d75e34c3c120330448e74694eced355a0d6 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Mon, 6 Nov 2023 11:24:50 +0700 Subject: [PATCH] Add Module Jenis Fasilitas --- DataTables/FacilityTypeDataTable.php | 90 ++++++++++++++++++ DataTables/GuaranteeTypeDataTable.php | 2 +- ..._11_042437_create_facility_types_table.php | 5 +- Entities/FacilityType.php | 16 ++++ Http/Controllers/FacilityTypeController.php | 37 ++++++++ .../Currency/UpdateCurrencyRequest.php | 1 - .../FacilityType/StoreFacilityTypeRequest.php | 75 +++++++++++++++ .../UpdateFacilityTypeRequest.php | 73 ++++++++++++++ Livewire/FacilityType/FacilityTypeModal.php | 94 +++++++++++++++++++ .../facility-type-modal.blade.php | 72 ++++++++++++++ .../facility_types/_actions.blade.php | 23 +++++ .../parameter/facility_types/_draw-scripts.js | 37 ++++++++ .../parameter/facility_types/index.blade.php | 72 ++++++++++++++ Resources/views/partials/menu/_app.blade.php | 10 +- Routes/breadcrumbs.php | 5 + Routes/web.php | 1 + 16 files changed, 609 insertions(+), 4 deletions(-) create mode 100644 DataTables/FacilityTypeDataTable.php create mode 100644 Entities/FacilityType.php create mode 100644 Http/Controllers/FacilityTypeController.php create mode 100644 Http/Requests/FacilityType/StoreFacilityTypeRequest.php create mode 100644 Http/Requests/FacilityType/UpdateFacilityTypeRequest.php create mode 100644 Livewire/FacilityType/FacilityTypeModal.php create mode 100644 Resources/views/livewire/facility-type/facility-type-modal.blade.php create mode 100644 Resources/views/parameter/facility_types/_actions.blade.php create mode 100644 Resources/views/parameter/facility_types/_draw-scripts.js create mode 100644 Resources/views/parameter/facility_types/index.blade.php diff --git a/DataTables/FacilityTypeDataTable.php b/DataTables/FacilityTypeDataTable.php new file mode 100644 index 0000000..5b96979 --- /dev/null +++ b/DataTables/FacilityTypeDataTable.php @@ -0,0 +1,90 @@ +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->locale('id')->translatedFormat('d F Y H:i:s'); + })->rawColumns(['action'])->addColumn('action', function ($facility_type) { + return view('writeoff::parameter.facility_types._actions', compact('facility_type')); + })->setRowId('id'); + } + + /** + * Get the query source of dataTable. + */ + public function query(FacilityType $model) + : QueryBuilder + { + return $model->newQuery(); + } + + /** + * Optional method if you want to use the html builder. + */ + public function html() + : HtmlBuilder + { + return $this->builder() + ->setTableId('facility-type-table') + ->columns($this->getColumns()) + ->minifiedAjax() + ->stateSave(false) + ->responsive() + ->autoWidth(true) + ->orderBy(1) + ->parameters([ + 'scrollX' => false, + 'drawCallback' => 'function() { KTMenu.createInstances(); }', + ]) + ->addTableClass('align-middle table-row-dashed fs-6 gy-5') + ->drawCallback("function() {" . file_get_contents(Module::getModulePath('writeoff').'Resources/views/parameter/facility_types/_draw-scripts.js') . "}"); + } + + /** + * Get the dataTable columns definition. + */ + public function getColumns() + : array + { + return [ + Column::make('DT_RowIndex')->title('No')->orderable(false)->searchable(false), + Column::make('kode')->title('Kode'), + Column::make('name')->title('Jenis Fasilitas'), + Column::make('updated_at')->title('Last Update'), + Column::computed('action')->exportable(false)->printable(false)->width(60)->addClass('text-center'), + ]; + } + + /** + * Get the filename for export. + */ + protected function filename() + : string + { + return 'FacilityType_' . date('YmdHis'); + } + } diff --git a/DataTables/GuaranteeTypeDataTable.php b/DataTables/GuaranteeTypeDataTable.php index 84e6dcf..92c0105 100644 --- a/DataTables/GuaranteeTypeDataTable.php +++ b/DataTables/GuaranteeTypeDataTable.php @@ -72,7 +72,7 @@ { return [ Column::make('DT_RowIndex')->title('No')->orderable(false)->searchable(false), - Column::make('kode')->title('Jenis Jaminan'), + Column::make('kode')->title('Kode'), Column::make('name')->title('Jenis Jaminan'), Column::make('updated_at')->title('Last Update'), Column::computed('action')->exportable(false)->printable(false)->width(60)->addClass('text-center'), diff --git a/Database/Migrations/2023_10_11_042437_create_facility_types_table.php b/Database/Migrations/2023_10_11_042437_create_facility_types_table.php index de7662e..ed36e21 100644 --- a/Database/Migrations/2023_10_11_042437_create_facility_types_table.php +++ b/Database/Migrations/2023_10_11_042437_create_facility_types_table.php @@ -17,13 +17,16 @@ return new class extends Migration $table->id(); $table->string('kode',3)->unique(); $table->string('name'); - $table->string('status', 1)->default('A'); + $table->char('status', 1)->default('A'); $table->timestamps(); + $table->timestamp('authorized_at')->nullable(); + $table->char('authorized_status', 1)->nullable(); $table->softDeletes(); $table->unsignedBigInteger('created_by')->nullable(); $table->unsignedBigInteger('updated_by')->nullable(); $table->unsignedBigInteger('deleted_by')->nullable(); + $table->unsignedBigInteger('authorized_by')->nullable(); }); } diff --git a/Entities/FacilityType.php b/Entities/FacilityType.php new file mode 100644 index 0000000..b5858e4 --- /dev/null +++ b/Entities/FacilityType.php @@ -0,0 +1,16 @@ +middleware(function ($request, $next) { + $this->user = Auth::guard('web')->user(); + return $next($request); + }); + } + + /** + * Display a listing of the FacilityTypes. + * + * @param \Modules\Writeoff\DataTables\FacilityTypeDataTable $dataTable + * + * @return mixed + */ + public function index(FacilityTypeDataTable $dataTable, Request $request) + { + if (is_null($this->user) || !$this->user->can('master.read')) { + abort(403, 'Sorry !! You are Unauthorized to view any master data !'); + } + + return $dataTable->render('writeoff::parameter.facility_types.index'); + } + } diff --git a/Http/Requests/Currency/UpdateCurrencyRequest.php b/Http/Requests/Currency/UpdateCurrencyRequest.php index bdcdd48..e4b302c 100644 --- a/Http/Requests/Currency/UpdateCurrencyRequest.php +++ b/Http/Requests/Currency/UpdateCurrencyRequest.php @@ -66,5 +66,4 @@ 'messages' => 'Currency updated failed.' ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); } - } diff --git a/Http/Requests/FacilityType/StoreFacilityTypeRequest.php b/Http/Requests/FacilityType/StoreFacilityTypeRequest.php new file mode 100644 index 0000000..e8ff66a --- /dev/null +++ b/Http/Requests/FacilityType/StoreFacilityTypeRequest.php @@ -0,0 +1,75 @@ + + */ + public function rules() + : array + { + return [ + 'kode' => 'required|string|max:3|min:3|unique:facility_types,kode', + 'name' => 'required|string|max:100' + ]; + } + + public function ignored() + : string + { + return $this->id; + } + + /** + * Configure the validator instance. + */ + public function withValidator(Validator $validator) + : void + { + $validator->after(function (Validator $validator) { + if ($validator->errors()->any()) { + $errors = json_decode($validator->errors()->toJson(), true); + + + foreach ($errors as $key => $value) { + flash($value[0]); + } + return redirect() + ->route('parameter.facility_types.index') + ->with('error', 'Facility Type created failed.'); + } + + }); + } + + protected function failedValidation(Validator|\Illuminate\Contracts\Validation\Validator $validator) + : JsonResponse + { + $errors = (new ValidationException($validator))->errors(); + + throw new HttpResponseException(response()->json([ + 'success' => false, + 'errors' => $errors, + 'messages' => 'Facility Type created failed.' + ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); + } + } diff --git a/Http/Requests/FacilityType/UpdateFacilityTypeRequest.php b/Http/Requests/FacilityType/UpdateFacilityTypeRequest.php new file mode 100644 index 0000000..79ebf12 --- /dev/null +++ b/Http/Requests/FacilityType/UpdateFacilityTypeRequest.php @@ -0,0 +1,73 @@ + + */ + public function rules() + : array + { + $this->_id = json_decode(json_decode(file_get_contents('php://input'))->components[0]->snapshot)->data->id; + + return [ + 'kode' => 'required|string|max:3|min:3|unique:facility_types,kode,' . $this->_id, + 'name' => 'required|string|max:100' + ]; + } + + /** + * Configure the validator instance. + */ + public function withValidator(Validator $validator) + : void + { + $validator->after(function (Validator $validator) { + if ($validator->errors()->any()) { + $error = json_decode($validator->errors()->toJson(), true); + foreach ($error as $key => $value) { + flash($value[0]); + } + + return redirect() + ->route('parameter.facility_types.index') + ->with('error', 'Facility Type updated failed.'); + } + }); + } + + protected function failedValidation(Validator|\Illuminate\Contracts\Validation\Validator $validator) + : JsonResponse + { + $errors = (new ValidationException($validator))->errors(); + + throw new HttpResponseException(response()->json([ + 'success' => false, + 'errors' => $errors, + 'messages' => 'Facility Type updated failed.' + ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); + } + + } diff --git a/Livewire/FacilityType/FacilityTypeModal.php b/Livewire/FacilityType/FacilityTypeModal.php new file mode 100644 index 0000000..cbb17c4 --- /dev/null +++ b/Livewire/FacilityType/FacilityTypeModal.php @@ -0,0 +1,94 @@ + 'delete', + 'update' => 'update', + ]; + + public function render() + { + return view('writeoff::livewire.facility-type.facility-type-modal'); + } + + public function submit() + { + $this->validate(); + + // Validate the form input data + DB::transaction(function () { + // Prepare the data for creating a new user + $data = [ + 'kode' => $this->kode, + 'name' => $this->name + ]; + + if ($this->edit_mode) { + // Emit a success event with a message + $facility_type = FacilityType::find($this->id); + $facility_type->update($data); + + $this->dispatch('success', __('Facility Type updated')); + } else { + // Emit a success event with a message + FacilityType::create($data); + $this->dispatch('success', __('New Facility Type created')); + } + }); + + // Reset the form fields after successful submission + $this->reset(); + } + + public function update($id) + { + $this->edit_mode = true; + + $facility_type = FacilityType::find($id); + + $this->id = $facility_type->id; + $this->kode = $facility_type->kode; + $this->name = $facility_type->name; + } + + public function delete($id) + { + FacilityType::destroy($id); + + // Emit a success event with a message + $this->dispatch('success', 'Facility Type successfully deleted'); + } + + public function hydrate() + { + $this->resetErrorBag(); + $this->resetValidation(); + } + + protected function rules() + { + if ($this->edit_mode) { + $request = new UpdateFacilityTypeRequest(); + } else { + $request = new StoreFacilityTypeRequest(); + } + + return $request->rules(); + } + } + diff --git a/Resources/views/livewire/facility-type/facility-type-modal.blade.php b/Resources/views/livewire/facility-type/facility-type-modal.blade.php new file mode 100644 index 0000000..6995143 --- /dev/null +++ b/Resources/views/livewire/facility-type/facility-type-modal.blade.php @@ -0,0 +1,72 @@ + diff --git a/Resources/views/parameter/facility_types/_actions.blade.php b/Resources/views/parameter/facility_types/_actions.blade.php new file mode 100644 index 0000000..827b51c --- /dev/null +++ b/Resources/views/parameter/facility_types/_actions.blade.php @@ -0,0 +1,23 @@ + + Actions + + + + + diff --git a/Resources/views/parameter/facility_types/_draw-scripts.js b/Resources/views/parameter/facility_types/_draw-scripts.js new file mode 100644 index 0000000..ac87681 --- /dev/null +++ b/Resources/views/parameter/facility_types/_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', { id : this.getAttribute('data-kt-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', { id : this.getAttribute('data-kt-id') }); + }); +}); + +// Listen for 'success' event emitted by Livewire +Livewire.on('success', (message) => { + // Reload the users-table datatable + LaravelDataTables['facility-type-table'].ajax.reload(); +}); diff --git a/Resources/views/parameter/facility_types/index.blade.php b/Resources/views/parameter/facility_types/index.blade.php new file mode 100644 index 0000000..a84869c --- /dev/null +++ b/Resources/views/parameter/facility_types/index.blade.php @@ -0,0 +1,72 @@ + + + @section('title') + Jenis Fasilitas + @endsection + + @section('breadcrumbs') + {{ Breadcrumbs::render('parameter.facility-types') }} + @endsection + +
+ +
+ +
+ +
+ {!! getIcon('magnifier', 'fs-3 position-absolute ms-5') !!} + +
+ +
+ + + +
+ +
+ + + +
+ + + + + +
+ +
+ + + +
+ +
+ {{ $dataTable->table() }} +
+ +
+ +
+ + @push('scripts') + {{ $dataTable->scripts() }} + + @endpush + +
diff --git a/Resources/views/partials/menu/_app.blade.php b/Resources/views/partials/menu/_app.blade.php index 7f2e0a9..f1118bb 100644 --- a/Resources/views/partials/menu/_app.blade.php +++ b/Resources/views/partials/menu/_app.blade.php @@ -29,13 +29,21 @@ - + Jenis Jaminan + + + + + + Jenis Fasilitas + + diff --git a/Routes/breadcrumbs.php b/Routes/breadcrumbs.php index b2e7257..91577d1 100644 --- a/Routes/breadcrumbs.php +++ b/Routes/breadcrumbs.php @@ -22,3 +22,8 @@ $trail->parent('parameter'); $trail->push('Jenis Jaminan', route('parameter.guarantee_types.index')); }); + + Breadcrumbs::for('parameter.facility-types', function (BreadcrumbTrail $trail) { + $trail->parent('parameter'); + $trail->push('Jenis Fasilitas', route('parameter.facility_types.index')); + }); diff --git a/Routes/web.php b/Routes/web.php index f63b6a2..2714268 100644 --- a/Routes/web.php +++ b/Routes/web.php @@ -15,4 +15,5 @@ Route::name('parameter.')->prefix('parameter')->group(function() { Route::get('branches', 'BranchController@index')->name('branches.index'); Route::get('currencies', 'CurrencyController@index')->name('currencies.index'); Route::get('guarantee-types', 'GuaranteeTypeController@index')->name('guarantee_types.index'); + Route::get('facility-types', 'FacilityTypeController@index')->name('facility_types.index'); });