From 9eb8e757dd761abb5a30e3c9f0a6a00833cf5f64 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 26 Nov 2024 23:34:45 +0700 Subject: [PATCH] feat(tranasction): crud,route model binidng,customer js dan lainnya yang berkaitan dengan fitur transaksi --- .../Admin/Manage/TransactionController.php | 101 ++++++++++++ .../Admin/Manage/TransactionRequest.php | 50 ++++++ app/Providers/RouteServiceProvider.php | 2 + public/assets/admin/css/style.css | 2 +- public/assets/admin/js/custom/transaction.js | 44 ++++++ .../admin/manage/transaction/create.blade.php | 146 ++++++++++++++++++ .../admin/manage/transaction/edit.blade.php | 59 +++++++ .../admin/manage/transaction/index.blade.php | 95 ++++++++++++ .../views/partials/admin/_sidebar.blade.php | 7 + routes/web.php | 11 ++ 10 files changed, 516 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Admin/Manage/TransactionController.php create mode 100644 app/Http/Requests/Admin/Manage/TransactionRequest.php create mode 100644 public/assets/admin/js/custom/transaction.js create mode 100644 resources/views/pages/admin/manage/transaction/create.blade.php create mode 100644 resources/views/pages/admin/manage/transaction/edit.blade.php create mode 100644 resources/views/pages/admin/manage/transaction/index.blade.php diff --git a/app/Http/Controllers/Admin/Manage/TransactionController.php b/app/Http/Controllers/Admin/Manage/TransactionController.php new file mode 100644 index 0000000..c3d9fd7 --- /dev/null +++ b/app/Http/Controllers/Admin/Manage/TransactionController.php @@ -0,0 +1,101 @@ + 'Transaksi', + 'transactions' => Transaction::latest()->get(), + ]); + } + + public function create(): View + { + return view('pages.admin.manage.transaction.create', [ + 'pageTitle' => 'Tambah Transaksi', + 'services' => Service::latest()->get(), + 'customers' => Customer::latest()->get(), + ]); + } + + public function store(TransactionRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + DB::transaction(function () use ($validatedData) { + $newTransaction = Transaction::create(array_merge($validatedData, [ + 'user_id' => Auth::id(), + 'barbershop_id' => Auth::user()->barbershop_id, + 'service_id' => $validatedData['service'], + 'customer_id' => $validatedData['customer'], + ])); + $this->uploadAttachment($validatedData['image'], 'transactions', Transaction::class, $newTransaction->id); + }); + + notify()->success('Data berhasil ditambahkan', 'Berhasil'); + + return redirect('dashboard/manage/transactions'); + } + + public function edit(Transaction $transaction): View + { + return view('pages.admin.manage.transaction.edit', [ + 'pageTitle' => 'Edit Transaksi', + 'transaction' => $transaction, + ]); + } + + public function update(Transaction $transaction, TransactionRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + + $transaction->update($validatedData); + + notify()->success('Data berhasil diubah', 'Berhasil'); + + return redirect('dashboard/manage/transactions'); + } + + public function delete(Transaction $transaction): RedirectResponse + { + $transaction->delete(); + + notify()->success('Data berhasil dihapus', 'Berhasil'); + + return back(); + } + + public function getPrice(string $serviceId): JsonResponse + { + $service = Service::find($serviceId); + + if (! $service) { + return response()->json([ + 'status' => false, + 'message' => 'Service not found', + ], 404); + } + + return response()->json([ + 'status' => true, + 'message' => 'Data retrieved successfully', + 'data' => decimalToInteger($service->price), + ], 200); + } +} diff --git a/app/Http/Requests/Admin/Manage/TransactionRequest.php b/app/Http/Requests/Admin/Manage/TransactionRequest.php new file mode 100644 index 0000000..27a0f76 --- /dev/null +++ b/app/Http/Requests/Admin/Manage/TransactionRequest.php @@ -0,0 +1,50 @@ +merge([ + 'price' => removeRupiahFormatting($this->price), + 'discount' => removeRupiahFormatting($this->discount), + 'total' => removeRupiahFormatting($this->total), + ]); + } + + public function rules(): array + { + $isEdit = $this->isMethod('put'); + + return [ + 'service' => ['required', Rule::exists('services', 'id')], + 'customer' => ['required', Rule::exists('customers', 'id')], + 'price' => ['required', 'numeric', 'min:0', 'max:999999.99'], + 'discount' => ['required', 'numeric', 'min:0', 'max:999999.99'], + 'total' => ['required', 'numeric', 'min:0', 'max:999999.99'], + 'image' => $isEdit + ? ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'] + : ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'], + 'payment_method' => ['required', Rule::in(['1', '2', '3'])], + ]; + } + + protected function failedValidation(Validator $validator): void + { + $this->handleValidationFailure($validator); + } +} diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 8a569ea..8e35b4f 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -9,6 +9,7 @@ use App\Models\Inventory; use App\Models\Payroll; use App\Models\Service; +use App\Models\Transaction; use App\Models\User; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; @@ -36,5 +37,6 @@ public function boot(): void Route::bind('payroll', fn (string $payroll) => Payroll::findOrFail(decryptId($payroll))); Route::bind('cash', fn (string $cash) => Cash::findOrFail(decryptId($cash))); Route::bind('inventory', fn (string $inventory) => Inventory::findOrFail(decryptId($inventory))); + Route::bind('transaction', fn (string $transaction) => Transaction::findOrFail(decryptId($transaction))); } } diff --git a/public/assets/admin/css/style.css b/public/assets/admin/css/style.css index c1cbac1..2fc3fe6 100644 --- a/public/assets/admin/css/style.css +++ b/public/assets/admin/css/style.css @@ -10096,7 +10096,7 @@ .radio-wrapper, .checkbox-wrapper { display: flex; gap: calc(8px + 8 * (100vw - 320px) / 1600); - flex-wrap: wrap; + /* flex-wrap: wrap; */ /* justify-content: center; */ } diff --git a/public/assets/admin/js/custom/transaction.js b/public/assets/admin/js/custom/transaction.js new file mode 100644 index 0000000..f266718 --- /dev/null +++ b/public/assets/admin/js/custom/transaction.js @@ -0,0 +1,44 @@ +$(document).ready(function () { + // Ketika service berubah + $('#service').on('change', function () { + let serviceId = $(this).val(); + + // Lakukan AJAX untuk mendapatkan harga + $.ajax({ + url: `/dashboard/manage/transaction/get-price/${serviceId}`, + method: 'GET', + success: function (response) { + if (response.status) { + // Masukkan harga ke field price (read-only) + $('#price').val(response.data || '0'); + + // Hitung total + calculateTotal(); + } + }, + error: function () { + Swal.fire({ + icon: 'error', + title: 'Oops...', + text: 'Terjadi kesalahan saat mengambil data layanan! Silakan coba lagi. Jika masalah berlanjut, hubungi administrator.', + }); + } + }); + }); + + // Fungsi untuk menghitung total + function calculateTotal() { + let price = parseFloat($('#price').val().replace(/[^0-9]/g, '') || 0); + let discount = parseFloat($('#discount').val().replace(/[^0-9]/g, '') || 0); + + let total = price - discount; + + // Tampilkan hasil dalam format rupiah + $('#total').val('Rp' + total.toLocaleString('id-ID')); + } + + // Jalankan perhitungan setiap kali discount berubah + $('#discount').on('input', function () { + calculateTotal(); + }); +}); diff --git a/resources/views/pages/admin/manage/transaction/create.blade.php b/resources/views/pages/admin/manage/transaction/create.blade.php new file mode 100644 index 0000000..8864485 --- /dev/null +++ b/resources/views/pages/admin/manage/transaction/create.blade.php @@ -0,0 +1,146 @@ +@extends('layouts.admin') +@section('styles') + +@endsection +@section('app') +
+
+
+ Kembali +
+
+
+
+
+ @csrf +
+ + + @error('customer') +
+ {{ $message }} +
+ @enderror +
+
+ + + @error('service') +
+ {{ $message }} +
+ @enderror +
+
+ + + @error('price') +
+ {{ $message }} +
+ @enderror +
+
+ + + @error('discount') +
+ {{ $message }} +
+ @enderror +
+
+ + + @error('total') +
+ {{ $message }} +
+ @enderror +
+
+ + + @error('image') +
+ {{ $message }} +
+ @enderror +
+
+ +
+
    +
  • + + +
  • +
  • + + +
  • +
  • + + +
  • +
+
+ @error('payment_method') +
+ {{ $message }} +
+ @enderror +
+
+ +
+
+
+
+
+@endsection +@section('afterScripts') + + + + + +@endsection diff --git a/resources/views/pages/admin/manage/transaction/edit.blade.php b/resources/views/pages/admin/manage/transaction/edit.blade.php new file mode 100644 index 0000000..3888d7a --- /dev/null +++ b/resources/views/pages/admin/manage/transaction/edit.blade.php @@ -0,0 +1,59 @@ +@extends('layouts.admin') +@section('styles') +@endsection +@section('app') +
+
+
+ Kembali +
+
+
+
+
+ @csrf + @method('put') +
+ + + @error('name') +
+ {{ $message }} +
+ @enderror +
+
+ + + @error('price') +
+ {{ $message }} +
+ @enderror +
+
+ + + @error('description') +
+ {{ $message }} +
+ @enderror +
+
+ +
+
+
+
+
+@endsection +@section('afterScripts') + + +@endsection diff --git a/resources/views/pages/admin/manage/transaction/index.blade.php b/resources/views/pages/admin/manage/transaction/index.blade.php new file mode 100644 index 0000000..0356e88 --- /dev/null +++ b/resources/views/pages/admin/manage/transaction/index.blade.php @@ -0,0 +1,95 @@ +@extends('layouts.admin') +@section('styles') + +@endsection +@section('app') +
+
+
+
+
+ Tambah +
+
+
+
+
+ + + + + + + + + + + + + + + + @foreach ($transactions as $transaction) + + + + + + + + + + + + @endforeach + +
NoPelangganLayananHargaDiskonTotalGambarTanggalAksi
{{ $loop->iteration }}{{ $transaction->customer->name }}{{ $transaction->service->name }}{{ formatToRupiah($transaction->price) }}{{ formatToRupiah($transaction->discount) }}{{ formatToRupiah($transaction->total) }} + @if ($transaction->attachment && $transaction->attachment->formatted_attachment) + attachment->formatted_attachment}") }}"> + attachment->formatted_attachment}") }}" + alt="{{ $transaction->name }}" width="77" height="77"> + + @else + + Default Image + + @endif + {{ formatDateIndo($transaction->created_at) }} +
    +
  • + + + +
  • +
  • +
    + @csrf + @method('delete') + + + +
    +
  • +
+
+
+
+
+
+
+
+@endsection +@section('afterScripts') + + + + +@endsection diff --git a/resources/views/partials/admin/_sidebar.blade.php b/resources/views/partials/admin/_sidebar.blade.php index 1a4388d..2a5d914 100644 --- a/resources/views/partials/admin/_sidebar.blade.php +++ b/resources/views/partials/admin/_sidebar.blade.php @@ -120,6 +120,13 @@ Kehadiran +