80 lines
2.5 KiB
PHP
Executable File
80 lines
2.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Finance;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Finance\DepositMoneyRequest;
|
|
use App\Models\Barbershop;
|
|
use App\Models\DepositMoney;
|
|
use App\Traits\UploadAttachment;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DepositMoneyController extends Controller
|
|
{
|
|
use UploadAttachment;
|
|
|
|
public function index(): View
|
|
{
|
|
return view('pages.admin.finance.deposit-money', [
|
|
'pageTitle' => 'Setor Uang',
|
|
'depositMoney' => DepositMoney::with(['user', 'user.biography'])
|
|
->barbershop()
|
|
->latest()
|
|
->get(),
|
|
]);
|
|
}
|
|
|
|
public function store(DepositMoneyRequest $request): RedirectResponse
|
|
{
|
|
$validatedData = $request->validated();
|
|
$barbershopId = Auth::user()->barbershop_id;
|
|
$barbershop = Barbershop::findOrFail($barbershopId);
|
|
|
|
DB::transaction(function () use ($validatedData, $barbershop) {
|
|
DepositMoney::create(array_merge($validatedData, [
|
|
'user_id' => Auth::id(),
|
|
'barbershop_id' => Auth::user()->barbershop_id,
|
|
]));
|
|
$barbershop->decrement('cash', $validatedData['amount']);
|
|
});
|
|
|
|
notify()->success('Data berhasil ditambahkan', 'Berhasil');
|
|
|
|
return back();
|
|
}
|
|
|
|
public function update(DepositMoneyRequest $request, DepositMoney $depositMoney): RedirectResponse
|
|
{
|
|
$validatedData = $request->validated();
|
|
$barbershop = Barbershop::findOrFail($depositMoney->barbershop_id);
|
|
|
|
DB::transaction(function () use ($validatedData, $depositMoney, $barbershop) {
|
|
$barbershop->increment('cash', $depositMoney->amount);
|
|
|
|
$depositMoney->update($validatedData);
|
|
|
|
$barbershop->decrement('cash', $validatedData['amount']);
|
|
});
|
|
|
|
notify()->success('Data berhasil diperbarui', 'Berhasil');
|
|
|
|
return back();
|
|
}
|
|
|
|
public function delete(DepositMoney $depositMoney): RedirectResponse
|
|
{
|
|
$barbershop = Barbershop::findOrFail($depositMoney->barbershop_id);
|
|
DB::transaction(function () use ($depositMoney, $barbershop) {
|
|
$barbershop->increment('cash', $depositMoney->amount);
|
|
$depositMoney->delete();
|
|
});
|
|
|
|
notify()->success('Data berhasil dihapus', 'Berhasil');
|
|
|
|
return back();
|
|
}
|
|
}
|