diff --git a/app/Http/Controllers/Admin/Finance/ExpenseController.php b/app/Http/Controllers/Admin/Finance/ExpenseController.php index 90ee3b1..2873d7f 100644 --- a/app/Http/Controllers/Admin/Finance/ExpenseController.php +++ b/app/Http/Controllers/Admin/Finance/ExpenseController.php @@ -19,9 +19,10 @@ class ExpenseController extends Controller public function index(): View { return view('pages.admin.finance.expenses', [ - 'pageTitle' => 'Pengeluaran', + 'pageTitle' => 'Pengeluaran Tetap', 'expenses' => Expense::with(['user', 'user.biography']) ->barbershop() + ->fixed() ->when(auth()->user()->role_id === 4, function ($query) { return $query->where('user_id', auth()->id()); }) diff --git a/app/Http/Controllers/Admin/Finance/GoodsExpenseController.php b/app/Http/Controllers/Admin/Finance/GoodsExpenseController.php new file mode 100644 index 0000000..f925e7f --- /dev/null +++ b/app/Http/Controllers/Admin/Finance/GoodsExpenseController.php @@ -0,0 +1,115 @@ + 'Pengeluaran Barang', + 'expenses' => Expense::with(['user', 'user.biography']) + ->barbershop() + ->goods() + ->latest() + ->get(), + ]); + } + + public function store(ExpenseRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + $barbershopId = Auth::user()->barbershop_id; + $barbershop = Barbershop::findOrFail($barbershopId); + + if ($validatedData['use_cash'] === '1' && $barbershop->cash < $validatedData['amount']) { + notify()->error('Saldo kas tidak mencukupi', 'Gagal'); + + return back()->withInput(); + } + + DB::transaction(function () use ($validatedData, $barbershop, $barbershopId) { + $newExpense = Expense::create(array_merge($validatedData, [ + 'user_id' => Auth::id(), + 'barbershop_id' => $barbershopId, + 'type' => '2', + ])); + + if ($validatedData['use_cash'] === '1') { + $barbershop->decrement('cash', $validatedData['amount']); + } + + $this->uploadAttachment($validatedData['proof'], 'expenses', Expense::class, $newExpense->id, 'expenses'); + }); + + notify()->success('Data berhasil ditambahkan', 'Berhasil'); + + return back(); + } + + public function update(ExpenseRequest $request, Expense $expense): RedirectResponse + { + $validatedData = $request->validated(); + $barbershop = Barbershop::findOrFail($expense->barbershop_id); + + $amountDifference = $validatedData['amount'] - $expense->amount; + + if ($validatedData['use_cash'] === '1') { + $requiredCash = $barbershop->cash - ($amountDifference < 0 ? 0 : $amountDifference); + if ($requiredCash < 0) { + notify()->error('Saldo kas tidak mencukupi', 'Gagal'); + + return back()->withInput(); + } + } + + DB::transaction(function () use ($validatedData, $expense, $barbershop) { + if ($expense->use_cash === '1') { + $barbershop->increment('cash', $expense->amount); + } + + $expense->update($validatedData); + + if ($validatedData['use_cash'] === '1') { + $barbershop->decrement('cash', $validatedData['amount']); + } + + if (isset($validatedData['proof'])) { + $this->uploadAttachment($validatedData['proof'], 'expenses', Expense::class, $expense->id, 'expenses'); + } + }); + + notify()->success('Data berhasil diperbarui', 'Berhasil'); + + return back(); + } + + public function delete(Expense $expense): RedirectResponse + { + $barbershop = Barbershop::findOrFail($expense->barbershop_id); + + DB::transaction(function () use ($expense, $barbershop) { + if ($expense->use_cash === '1') { + $barbershop->increment('cash', $expense->amount); + } + + $expense->delete(); + }); + + notify()->success('Data berhasil dihapus', 'Berhasil'); + + return back(); + } +} diff --git a/app/Models/Expense.php b/app/Models/Expense.php index b79fe0c..e1246db 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -24,6 +24,7 @@ class Expense extends Model 'name', 'amount', 'use_cash', + 'type', ]; public function scopeBarbershop(Builder $query): Builder @@ -41,6 +42,16 @@ public function scopeYesterday(Builder $query): Builder return $query->whereDate('created_at', Carbon::yesterday()); } + public function scopeGoods(Builder $query): Builder + { + return $query->where('type', '2'); + } + + public function scopeFixed(Builder $query): Builder + { + return $query->where('type', '1'); + } + public function user(): BelongsTo { return $this->belongsTo(User::class); diff --git a/database/migrations/2024_12_30_152153_add_type_to_expenses.php b/database/migrations/2024_12_30_152153_add_type_to_expenses.php new file mode 100644 index 0000000..45cc16c --- /dev/null +++ b/database/migrations/2024_12_30_152153_add_type_to_expenses.php @@ -0,0 +1,28 @@ +enum('type', ['1', '2'])->default('1')->after('use_cash')->comment('1:Bukan Barang 2:Barang'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('expenses', function (Blueprint $table) { + $table->dropColumn('type'); + }); + } +}; diff --git a/resources/views/pages/admin/finance/goods-expense.blade.php b/resources/views/pages/admin/finance/goods-expense.blade.php new file mode 100644 index 0000000..6360b21 --- /dev/null +++ b/resources/views/pages/admin/finance/goods-expense.blade.php @@ -0,0 +1,192 @@ +@extends('layouts.admin') +@section('styles') + +@endsection +@section('app') +
| No | +Penginput | +Nama | +Jumlah | +Bukti | +Tanggal | + @if (in_array(auth()->user()->role_id, [1, 3, 4])) +Aksi | + @endif +
|---|---|---|---|---|---|---|
| {{ $loop->iteration }} | +{{ $expense->user->biography->full_name ?? '-deleted-' }} | +{{ $expense->name }} | +{{ formatToRupiah($expense->amount) ?? '-' }} | +
+ @if ($expense->attachment && $expense->attachment->formatted_attachment)
+ attachment->formatted_attachment}") }}">
+
+
+ @endif
+ |
+ {{ formatDateIndo($expense->created_at) }} | + @if (in_array(auth()->user()->role_id, [1, 3, 4])) +
+
|
+ @endif
+