dstpabuaran.com/app/Services/Admin/Finance/Payroll/PayrollAdjustmentService.php
Yoga Pangestu 0023309a8f Refactor services to improve role checks and streamline data retrieval
- Updated CustomerService to simplify getAll method.
- Refactored ProductService to utilize HasRoleChecks trait and improved role verification logic.
- Enhanced ProductVariantService with new methods for fetching data for restocking and transactions.
- Cleaned up RawMaterialService by removing unused methods and improving data retrieval.
- Adjusted SupplierService to streamline getAll method.
- Refactored RoleService to use Spatie's Role model and improved role filtering logic.
- Updated NotificationService to handle role labels more effectively.
- Improved StockMutationService by removing redundant paginated method.
- Cleaned up various frontend components to directly accept necessary props instead of nested data objects.
- Updated tests to reflect changes in service method names and ensure proper notification handling.
2026-08-09 11:33:25 +07:00

78 lines
2.3 KiB
PHP

<?php
namespace App\Services\Admin\Finance\Payroll;
use App\Enums\PayrollAdjustmentType;
use App\Enums\PayrollStatus;
use App\Models\Payroll;
use App\Models\PayrollAdjustment;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class PayrollAdjustmentService
{
public function store(Payroll $payroll, array $data): PayrollAdjustment
{
if ($payroll->status !== PayrollStatus::UNPAID) {
throw ValidationException::withMessages([
'payroll' => 'Hanya gaji berstatus belum dibayar yang bisa diadjust.',
]);
}
return DB::transaction(function () use ($payroll, $data) {
$adjustment = PayrollAdjustment::create([
'payroll_id' => $payroll->id,
'created_by_id' => auth()->id(),
'attendance_id' => $data['attendance_id'] ?? null,
'type' => $data['type'],
'amount' => $data['amount'],
'description' => $data['description'],
]);
$this->recalculatePayroll($payroll);
return $adjustment;
});
}
public function destroy(PayrollAdjustment $adjustment): bool
{
$payroll = $adjustment->payroll;
if ($payroll->status !== PayrollStatus::UNPAID) {
throw ValidationException::withMessages([
'payroll' => 'Hanya gaji berstatus belum dibayar yang bisa diadjust.',
]);
}
return DB::transaction(function () use ($adjustment, $payroll) {
$result = $adjustment->delete();
$this->recalculatePayroll($payroll);
return $result;
});
}
private function recalculatePayroll(Payroll $payroll): void
{
$payroll->refresh();
$bonuses = $payroll->payrollAdjustments()
->where('type', PayrollAdjustmentType::BONUS)
->sum('amount');
$deductions = $payroll->payrollAdjustments()
->where('type', PayrollAdjustmentType::DEDUCTION)
->sum('amount');
$total = $payroll->base_salary + $bonuses - $deductions;
$payroll->update([
'bonus_amount' => $bonuses,
'deduction_amount' => $deductions,
'total_amount' => $total,
]);
}
}