diff --git a/app/Http/Controllers/Admin/Finance/DebtController.php b/app/Http/Controllers/Admin/Finance/DebtController.php index 9bb5def..8f8579e 100644 --- a/app/Http/Controllers/Admin/Finance/DebtController.php +++ b/app/Http/Controllers/Admin/Finance/DebtController.php @@ -38,10 +38,14 @@ public function store(DebtRequest $request): RedirectResponse $barbershopId = Auth::user()->barbershop_id; $barbershop = Barbershop::findOrFail($barbershopId); $user = User::findOrFail($validatedData['employee']); - $transaction = Transaction::where('user_id', $user->id)->where('is_paid', '0')->first(); - $transactionAmount = $transaction ? $transaction->total : 0; - $maxDebt = $user->base_salary + (0.5 * $transactionAmount); + if ($user->role_id === 4) { + $transaction = Transaction::where('barbershop_id', $barbershopId)->where('is_paid', '0')->sum('total'); + } else { + $transaction = Transaction::where('user_id', $user->id)->where('is_paid', '0')->sum('total'); + } + + $maxDebt = $user->base_salary + (0.5 * $transaction); if ($barbershop->cash < $validatedData['amount']) { notify()->error('Kas tidak cukup untuk melakukan transaksi ini', 'Gagal'); @@ -50,7 +54,7 @@ public function store(DebtRequest $request): RedirectResponse } if ($validatedData['amount'] > $maxDebt) { - notify()->error('Jumlah utang tidak boleh lebih dari gaji pokok + 50% dari transaksi karyawan. Maksimal kasbon: '.formatToRupiah($maxDebt), 'Gagal'); + notify()->error('Jumlah utang tidak boleh lebih dari gaji pokok + 50% dari transaksi. Maksimal utang: Rp '.number_format($maxDebt, 2), 'Gagal'); return back()->withInput(); } @@ -74,7 +78,12 @@ public function update(DebtRequest $request, Debt $debt): RedirectResponse $barbershopId = Auth::user()->barbershop_id; $barbershop = Barbershop::findOrFail($barbershopId); $user = User::findOrFail($debt->user_id); - $transaction = Transaction::where('user_id', $user->id)->where('is_paid', '0')->first(); + + if ($user->role_id == 4) { + $transaction = Transaction::where('barbershop_id', $barbershopId)->where('is_paid', '0')->first(); + } else { + $transaction = Transaction::where('user_id', $user->id)->where('is_paid', '0')->first(); + } $transactionAmount = $transaction ? $transaction->total : 0; $maxDebt = $user->base_salary + (0.5 * $transactionAmount); @@ -88,14 +97,13 @@ public function update(DebtRequest $request, Debt $debt): RedirectResponse } if ($validatedData['amount'] > $maxDebt) { - notify()->error('Jumlah utang tidak boleh lebih dari gaji pokok + 50% dari transaksi karyawan. Maksimal utang: Rp '.number_format($maxDebt, 2), 'Gagal'); + notify()->error('Jumlah utang tidak boleh lebih dari gaji pokok + 50% dari transaksi. Maksimal utang: Rp '.number_format($maxDebt, 2), 'Gagal'); return back()->withInput(); } DB::transaction(function () use ($validatedData, $debt, $barbershop, $amountDifference) { $debt->update([ - 'user_id' => $validatedData['employee'], 'amount' => $validatedData['amount'], ]); diff --git a/app/Http/Controllers/Admin/Finance/PayrollController.php b/app/Http/Controllers/Admin/Finance/PayrollController.php index ac7d3af..1d5fa6d 100644 --- a/app/Http/Controllers/Admin/Finance/PayrollController.php +++ b/app/Http/Controllers/Admin/Finance/PayrollController.php @@ -4,6 +4,8 @@ use App\Http\Controllers\Controller; use App\Http\Requests\Admin\Finance\PayrollRequest; +use App\Models\Barbershop; +use App\Models\Debt; use App\Models\Payroll; use App\Models\Transaction; use App\Models\User; @@ -41,15 +43,28 @@ public function create(): View public function store(PayrollRequest $request): RedirectResponse { $validatedData = $request->validated(); + $barbershopId = Auth::user()->barbershop_id; - DB::transaction(function () use ($validatedData) { + DB::transaction(function () use ($validatedData, $barbershopId) { Payroll::create(array_merge($validatedData, [ 'user_id' => Auth::id(), 'employee_id' => $validatedData['employee'], - 'barbershop_id' => Auth::user()->barbershop_id, + 'barbershop_id' => $barbershopId, ])); - Transaction::where('user_id', $validatedData['employee']) + $totalDebt = Debt::where('user_id', $validatedData['employee']) + ->where('is_paid', '0') + ->sum('amount'); + + $barbershop = Barbershop::findOrFail($barbershopId); + + if ($validatedData['cut'] > $totalDebt) { + $barbershop->increment('cash', $totalDebt); + } elseif ($validatedData['cut'] <= $totalDebt) { + $barbershop->increment('cash', $validatedData['cut']); + } + + Debt::where('user_id', $validatedData['employee']) ->where('is_paid', '0') ->update(['is_paid' => '1']); }); @@ -68,24 +83,63 @@ public function edit(Payroll $payroll): View ]); } - public function update(Payroll $payroll, PayrollRequest $request): RedirectResponse + public function update(PayrollRequest $request, Payroll $payroll): RedirectResponse { $validatedData = $request->validated(); + $barbershopId = Auth::user()->barbershop_id; - $payroll->update($validatedData); + DB::transaction(function () use ($validatedData, $payroll, $barbershopId) { + $oldCut = $payroll->cut; + $newCut = $validatedData['cut']; + $cutDifference = $newCut - $oldCut; - notify()->success('Data berhasil diubah', 'Berhasil'); + $totalDebt = Debt::where('user_id', $payroll->employee) + ->where('is_paid', '0') + ->sum('amount'); + + $payroll->update($validatedData); + + if ($cutDifference > 0) { + $barbershop = Barbershop::findOrFail($barbershopId); + + if ($newCut > $totalDebt) { + $barbershop->increment('cash', $totalDebt); + } elseif ($newCut <= $totalDebt) { + $barbershop->increment('cash', $newCut); + } + } + }); + + notify()->success('Data berhasil diperbarui', 'Berhasil'); return redirect('dashboard/finance/payrolls'); } public function delete(Payroll $payroll): RedirectResponse { - $payroll->delete(); + $barbershopId = Auth::user()->barbershop_id; + + DB::transaction(function () use ($payroll, $barbershopId) { + $cut = $payroll->cut; + + $totalDebt = Debt::where('user_id', $payroll->employee_id) + ->where('is_paid', '0') + ->sum('amount'); + + $payroll->delete(); + + $barbershop = Barbershop::findOrFail($barbershopId); + + if ($cut > $totalDebt) { + $barbershop->increment('cash', $totalDebt); + } elseif ($cut <= $totalDebt) { + $barbershop->increment('cash', $cut); + } + }); notify()->success('Data berhasil dihapus', 'Berhasil'); - return back(); + return redirect('dashboard/finance/payrolls'); } public function getSalaryEmployee(string $employeeId): JsonResponse @@ -108,6 +162,7 @@ public function getSalaryEmployee(string $employeeId): JsonResponse } }) ->sum('total'); + $debt = Debt::where('user_id', $employeeId)->where('is_paid', '0')->sum('amount'); return response()->json([ 'status' => true, @@ -115,6 +170,7 @@ public function getSalaryEmployee(string $employeeId): JsonResponse 'data' => [ 'baseSalary' => formatToRupiah($employee->base_salary), 'shavingResults' => in_array($employee->role_id, [4, 5]) ? formatToRupiah($shavingResults) : 'Rp0', + 'debt' => formatToRupiah($debt), ], ]); } diff --git a/public/assets/admin/js/custom/payroll.js b/public/assets/admin/js/custom/payroll.js index cabc766..104b79d 100644 --- a/public/assets/admin/js/custom/payroll.js +++ b/public/assets/admin/js/custom/payroll.js @@ -19,6 +19,7 @@ $(document).ready(function () { success: function (response) { if (response.status) { $('#amount').val(response.data.baseSalary || '0'); + $('#cut').val(response.data.debt || '0'); $('#shaving_result').val(response.data.shavingResults || '0'); shavingResults = parseFloat(unformatRupiah(response.data.shavingResults)) || 0;