diff --git a/app/Http/Controllers/Admin/Master/BarbershopController.php b/app/Http/Controllers/Admin/Master/BarbershopController.php new file mode 100644 index 0000000..4c685a2 --- /dev/null +++ b/app/Http/Controllers/Admin/Master/BarbershopController.php @@ -0,0 +1,116 @@ + 'Barbershop', + 'barbershop' => Barbershop::latest()->get(), + ]); + } + + public function create(): View + { + return view('pages.admin.master.barbershop.create', [ + 'pageTitle' => 'Tambah Barbershop', + ]); + } + + public function store(BarbershopRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + + DB::transaction(function () use ($validatedData) { + $newBarbershop = Barbershop::create($validatedData); + $this->uploadAttachment($validatedData['image'], 'barbershop', Barbershop::class, $newBarbershop->id); + }); + + notify()->success('Data berhasil ditambahkan', 'Berhasil'); + + return redirect('dashboard/master/barbershop'); + } + + public function edit(Barbershop $barbershop): View + { + return view('pages.admin.master.barbershop.edit', [ + 'pageTitle' => 'Edit Barbershop', + 'barbershop' => $barbershop, + ]); + } + + public function update(Barbershop $barbershop, BarbershopRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + + DB::transaction(function () use ($barbershop, $validatedData) { + $barbershop->update($validatedData); + $this->uploadAttachment($validatedData['image'], 'barbershop', Barbershop::class, $barbershop->id); + }); + + notify()->success('Data berhasil ditambahkan', 'Berhasil'); + + return redirect('dashboard/master/barbershop'); + } + + public function delete(Barbershop $barbershop): RedirectResponse + { + if (Auth::user()->barbershop_id === $barbershop->id) { + notify()->info('Anda sedang berada di barbershop ini, silakan pilih barbershop lain lalu Anda dapat menghapusnya', 'Informasi'); + + return back(); + } + $barbershop->delete(); + + notify()->success('Data berhasil dihapus', 'Berhasil'); + + return redirect('dashboard/master/barbershop'); + } + + public function generateActionButtons(Barbershop $barbershop): string + { + $editUrl = route('master.barbershop.edit', encryptId($barbershop->id)); + $deleteUrl = route('master.barbershop.delete', encryptId($barbershop->id)); + + return ' +
'; + } +} diff --git a/app/Http/Requests/Admin/Master/BarbershopRequest.php b/app/Http/Requests/Admin/Master/BarbershopRequest.php new file mode 100644 index 0000000..c4dbfe3 --- /dev/null +++ b/app/Http/Requests/Admin/Master/BarbershopRequest.php @@ -0,0 +1,44 @@ +merge([ + 'cash' => removeRupiahFormatting($this->cash), + ]); + } + + public function rules(): array + { + return [ + 'name' => ['required', 'string', 'max:50'], + 'contact_number' => ['required', 'string', 'regex:/^\+?\d{10,15}$/'], + 'landmark' => ['required', 'string', 'max:30'], + 'address' => ['required', 'string', 'max:500'], + 'opened' => ['required', 'date'], + 'closed' => ['nullable', 'date'], + 'cash' => ['required', 'numeric', 'min:0', 'max:99999999.99'], + 'image' => ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'], + ]; + } + + protected function failedValidation(Validator $validator): void + { + $this->handleValidationFailure($validator); + } +} diff --git a/app/Models/Barbershop.php b/app/Models/Barbershop.php index 62f3177..d9ae03c 100644 --- a/app/Models/Barbershop.php +++ b/app/Models/Barbershop.php @@ -2,15 +2,19 @@ namespace App\Models; +use App\Observers\BarbershopObserver; +use Dyrynda\Database\Support\CascadeSoftDeletes; +use Illuminate\Database\Eloquent\Attributes\ObservedBy; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\SoftDeletes; +#[ObservedBy([BarbershopObserver::class])] class Barbershop extends Model { - use HasFactory, SoftDeletes; + use CascadeSoftDeletes, HasFactory, SoftDeletes; protected $table = 'barbershop'; @@ -24,6 +28,23 @@ class Barbershop extends Model 'cash', ]; + protected $with = [ + 'attachment', + ]; + + protected $cascadeDeletes = [ + 'users', + 'customers', + 'services', + 'attendances', + 'expenses', + 'inventory', + 'transactions', + 'payrolls', + 'cash', + 'attachment', + ]; + public function users(): HasMany { return $this->hasMany(User::class); diff --git a/app/Observers/BarbershopObserver.php b/app/Observers/BarbershopObserver.php new file mode 100644 index 0000000..fd46a17 --- /dev/null +++ b/app/Observers/BarbershopObserver.php @@ -0,0 +1,51 @@ +attachment && $barbershop->attachment->formatted_attachment) { + Storage::delete("barbershop/{$barbershop->attachment->formatted_attachment}"); + } + } + + /** + * Handle the Barbershop "restored" event. + */ + public function restored(Barbershop $barbershop): void + { + // + } + + /** + * Handle the Barbershop "force deleted" event. + */ + public function forceDeleted(Barbershop $barbershop): void + { + // + } +} diff --git a/resources/views/pages/admin/master/barbershop/create.blade.php b/resources/views/pages/admin/master/barbershop/create.blade.php new file mode 100644 index 0000000..ceacdf3 --- /dev/null +++ b/resources/views/pages/admin/master/barbershop/create.blade.php @@ -0,0 +1,127 @@ +@extends('layouts.admin') +@section('styles') + +@endsection +@section('app') +| No | +Nama | +Kontak | +Patokan | +Alamat | +Dibuka | +Tutup | +Kas | +Gambar | +Tanggal | +Aksi | +
|---|---|---|---|---|---|---|---|---|---|---|
| {{ $loop->iteration }} | +{{ $item->name }} | +{{ $item->contact_number }} | +{{ $item->landmark }} | +{{ $item->address }} | +{{ formatDateIndo($item->opened) }} | +{{ $item->closed ? formatDateIndo($item->closed) : 'Masih Beroperasi' }} + | +{{ formatToRupiah($item->cash) }} | +
+ @if ($item->attachment && $item->attachment->formatted_attachment)
+ attachment->formatted_attachment}") }}">
+
+
+ @endif
+ |
+ {{ formatDateIndo($item->created_at) }} | +
+
|
+