From e422a1a2adb756062e9e473a55eb1382e50d354a Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Mon, 12 Aug 2024 10:58:17 +0700 Subject: [PATCH] Feature #5 : Currency --- app/Exports/CurrencyExport.php | 54 ++++++ app/Http/Controllers/CurrencyController.php | 149 +++++++++++++++++ app/Http/Requests/CurrencyRequest.php | 41 +++++ app/Models/Currency.php | 14 ++ ...4_08_12_024448_create_currencies_table.php | 39 +++++ module.json | 21 +++ resources/views/currency/create.blade.php | 69 ++++++++ resources/views/currency/index.blade.php | 155 ++++++++++++++++++ routes/breadcrumbs.php | 16 ++ routes/web.php | 20 +++ 10 files changed, 578 insertions(+) create mode 100644 app/Exports/CurrencyExport.php create mode 100644 app/Http/Controllers/CurrencyController.php create mode 100644 app/Http/Requests/CurrencyRequest.php create mode 100644 app/Models/Currency.php create mode 100644 database/migrations/2024_08_12_024448_create_currencies_table.php create mode 100644 resources/views/currency/create.blade.php create mode 100644 resources/views/currency/index.blade.php diff --git a/app/Exports/CurrencyExport.php b/app/Exports/CurrencyExport.php new file mode 100644 index 0000000..8b9f6d3 --- /dev/null +++ b/app/Exports/CurrencyExport.php @@ -0,0 +1,54 @@ +id, + $row->code, + $row->name, + $row->decimal_places, + $row->updated_at, + $row->deleted_at, + $row->created_at + ]; + } + + public function headings() + : array + { + return [ + 'ID', + 'Code', + 'Name', + 'Decimal Places', + 'Created At' + ]; + } + + public function columnFormats() + : array + { + return [ + 'A' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER, + 'B' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER, + 'E' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME + ]; + } + } diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php new file mode 100644 index 0000000..1fe138a --- /dev/null +++ b/app/Http/Controllers/CurrencyController.php @@ -0,0 +1,149 @@ +validated(); + + if ($validate) { + try { + // Save to database + Currency::create($validate); + return redirect() + ->route('basicdata.currency.index') + ->with('success', 'Currency created successfully'); + } catch (\Exception $e) { + return redirect() + ->route('basicdata.currency.create') + ->with('error', 'Failed to create province'); + } + } + } + + public function create() + { + return view('lpj::currency.create'); + } + + public function edit($id) + { + $currency = Currency::find($id); + return view('lpj::currency.create', compact('currency')); + } + + public function update(CurrencyRequest $request, $id) + { + $validate = $request->validated(); + + if ($validate) { + try { + // Update in database + $currency = Currency::find($id); + $currency->update($validate); + return redirect() + ->route('basicdata.currency.index') + ->with('success', 'Currency updated successfully'); + } catch (\Exception $e) { + return redirect() + ->route('basicdata.currency.edit', $id) + ->with('error', 'Failed to update province'); + } + } + } + + public function destroy($id) + { + try { + // Delete from database + $currency = Currency::find($id); + $currency->delete(); + + echo json_encode(['success' => true, 'message' => 'Currency deleted successfully']); + } catch (\Exception $e) { + echo json_encode(['success' => false, 'message' => 'Failed to delete province']); + } + } + + public function dataForDatatables(Request $request) + { + if (is_null($this->user) || !$this->user->can('currency.view')) { + //abort(403, 'Sorry! You are not allowed to view users.'); + } + + // Retrieve data from the database + $query = Currency::query(); + + // 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%"); + }); + } + + // Apply sorting if provided + if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) { + $order = $request->get('sortOrder'); + $column = $request->get('sortField'); + $query->orderBy($column, $order); + } + + // Get the total count of records + $totalRecords = $query->count(); + + // Apply pagination if provided + if ($request->has('page') && $request->has('size')) { + $page = $request->get('page'); + $size = $request->get('size'); + $offset = ($page - 1) * $size; // Calculate the offset + + $query->skip($offset)->take($size); + } + + // Get the filtered count of records + $filteredRecords = $query->count(); + + // Get the data for the current page + $data = $query->get(); + + // Calculate the page count + $pageCount = ceil($totalRecords / $request->get('size')); + + // Calculate the current page number + $currentPage = 0 + 1; + + // Return the response data as a JSON object + return response()->json([ + 'draw' => $request->get('draw'), + 'recordsTotal' => $totalRecords, + 'recordsFiltered' => $filteredRecords, + 'pageCount' => $pageCount, + 'page' => $currentPage, + 'totalCount' => $totalRecords, + 'data' => $data, + ]); + } + + public function export() + { + return Excel::download(new CurrencyExport, 'currency.xlsx'); + } + } diff --git a/app/Http/Requests/CurrencyRequest.php b/app/Http/Requests/CurrencyRequest.php new file mode 100644 index 0000000..fded635 --- /dev/null +++ b/app/Http/Requests/CurrencyRequest.php @@ -0,0 +1,41 @@ + 'required|string|max:255', + 'decimal_places' => 'nullable|integer|between:0,3', + 'status' => 'nullable|boolean', + 'authorized_at' => 'nullable|datetime', + 'authorized_status' => 'nullable|string|max:1', + 'authorized_by' => 'nullable|exists:users,id', + ]; + + if ($this->method() == 'PUT') { + $rules['code'] = 'required|string|max:3|unique:currencies,code,' . $this->id; + } else { + $rules['code'] = 'required|string|max:3|unique:currencies,code'; + } + + return $rules; + } + + /** + * Determine if the user is authorized to make this request. + */ + public function authorize() + : bool + { + return true; + } + } diff --git a/app/Models/Currency.php b/app/Models/Currency.php new file mode 100644 index 0000000..6fafc3f --- /dev/null +++ b/app/Models/Currency.php @@ -0,0 +1,14 @@ +id(); + $table->string('code',3)->unique(); + $table->string('name'); + $table->integer('decimal_places')->default(2); + $table->boolean('status')->default(true)->nullable(); + $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(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('currencies'); + } +}; diff --git a/module.json b/module.json index 2488a9c..e48362b 100644 --- a/module.json +++ b/module.json @@ -103,6 +103,27 @@ ], "sub": [ { + "title": "Cabang", + "path": "", + "classes": "", + "attributes": [], + "permission": "", + "roles": [] + }, { + "title": "Mata Uang", + "path": "basicdata.currency", + "classes": "", + "attributes": [], + "permission": "", + "roles": [] + }, { + "title": "Debitur", + "path": "", + "classes": "", + "attributes": [], + "permission": "", + "roles": [] + },{ "title": "Jenis Fasilitas Kredit", "path": "basicdata.jenis-fasilitas-kredit", "classes": "", diff --git a/resources/views/currency/create.blade.php b/resources/views/currency/create.blade.php new file mode 100644 index 0000000..6c02d64 --- /dev/null +++ b/resources/views/currency/create.blade.php @@ -0,0 +1,69 @@ +@extends('layouts.main') + +@section('breadcrumbs') + {{ Breadcrumbs::render(request()->route()->getName()) }} +@endsection + +@section('content') +
+ @if(isset($currency->id)) +
+ + @method('PUT') + @else + + @endif + @csrf +
+
+

+ {{ isset($currency->id) ? 'Edit' : 'Tambah' }} Currency +

+
+ Back +
+
+
+
+ +
+ + @error('code') + {{ $message }} + @enderror +
+
+
+ +
+ + @error('name') + {{ $message }} + @enderror +
+
+
+ +
+ + @error('decimal_places') + {{ $message }} + @enderror +
+
+
+ +
+
+
+
+
+@endsection diff --git a/resources/views/currency/index.blade.php b/resources/views/currency/index.blade.php new file mode 100644 index 0000000..976c828 --- /dev/null +++ b/resources/views/currency/index.blade.php @@ -0,0 +1,155 @@ +@extends('layouts.main') + +@section('breadcrumbs') + {{ Breadcrumbs::render('basicdata.currency') }} +@endsection + +@section('content') +
+
+
+

+ Daftar Mata Uang +

+ +
+
+
+ + + + + + + + + + +
+ + + Code + + + Mata Uang + + + Decimal Places + + Action
+
+ +
+
+
+@endsection + +@push('scripts') + + + +@endpush + diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index ace16b9..f9c1e10 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -67,3 +67,19 @@ $trail->push('Edit Jenis Aset'); }); + + Breadcrumbs::for('basicdata.currency', function (BreadcrumbTrail $trail) { + $trail->parent('basicdata'); + $trail->push('Mata Uang', route('basicdata.currency.index')); + }); + + Breadcrumbs::for('basicdata.currency.create', function (BreadcrumbTrail $trail) { + $trail->parent('basicdata.currency'); + $trail->push('Tambah Mata Uang', route('basicdata.currency.create')); + }); + + Breadcrumbs::for('basicdata.currency.edit', function (BreadcrumbTrail $trail) { + $trail->parent('basicdata.currency'); + $trail->push('Edit Mata Uang'); + }); + diff --git a/routes/web.php b/routes/web.php index 9b9676f..44f2266 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ name('export'); }); Route::resource('jenis-aset', JenisAsetController::class); + + Route::name('currency.')->prefix('mata-uang')->group(function () { + Route::get('restore/{id}', [CurrencyController::class, 'restore'])->name('restore'); + Route::get('datatables', [CurrencyController::class, 'dataForDatatables']) + ->name('datatables'); + Route::get('export', [CurrencyController::class, 'export'])->name('export'); + }); + + Route::resource('mata-uang', CurrencyController::class, [ + 'names' => [ + 'index' => 'currency.index', + 'show' => 'currency.show', + 'create' => 'currency.create', + 'store' => 'currency.store', + 'edit' => 'currency.edit', + 'update' => 'currency.update', + 'destroy' => 'currency.destroy', + ] + ]); }); });