edit payroll
This commit is contained in:
@@ -253,6 +253,70 @@ class PayrollRunController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function updateEntry(Request $request, $payrollEntryId)
|
||||
{
|
||||
if (Auth::user()->can('edit-payroll-runs')) {
|
||||
$payrollEntry = PayrollEntry::where('id', $payrollEntryId)
|
||||
->whereHas('payrollRun', function ($q) {
|
||||
$q->whereIn('created_by', getCompanyAndUsersId());
|
||||
})
|
||||
->with('payrollRun')
|
||||
->first();
|
||||
|
||||
if (! $payrollEntry) {
|
||||
return redirect()->back()->with('error', __('Payroll entry not found.'));
|
||||
}
|
||||
|
||||
if ($payrollEntry->payrollRun->status !== 'draft') {
|
||||
return redirect()->back()->with('error', __('Cannot edit entry in a processed payroll run.'));
|
||||
}
|
||||
|
||||
try {
|
||||
$validated = $request->validate([
|
||||
'basic_salary' => 'nullable|numeric|min:0',
|
||||
'component_earnings' => 'nullable|numeric|min:0',
|
||||
'overtime_amount' => 'nullable|numeric|min:0',
|
||||
'unpaid_leave_deduction' => 'nullable|numeric|min:0',
|
||||
'total_deductions' => 'nullable|numeric|min:0',
|
||||
]);
|
||||
|
||||
// Merge with actual current properties if not actively overridden
|
||||
$basicSalary = isset($validated['basic_salary']) ? $validated['basic_salary'] : $payrollEntry->basic_salary;
|
||||
$componentEarnings = isset($validated['component_earnings']) ? $validated['component_earnings'] : $payrollEntry->component_earnings;
|
||||
$overtimeAmount = isset($validated['overtime_amount']) ? $validated['overtime_amount'] : $payrollEntry->overtime_amount;
|
||||
|
||||
$unpaidLeaveDeduction = isset($validated['unpaid_leave_deduction']) ? $validated['unpaid_leave_deduction'] : $payrollEntry->unpaid_leave_deduction;
|
||||
$totalDeductions = isset($validated['total_deductions']) ? $validated['total_deductions'] : $payrollEntry->total_deductions;
|
||||
|
||||
$totalEarnings = $basicSalary + $componentEarnings + $overtimeAmount - $unpaidLeaveDeduction;
|
||||
$grossPay = $totalEarnings;
|
||||
$netPay = max(0, $grossPay - $totalDeductions);
|
||||
|
||||
$payrollEntry->update([
|
||||
'basic_salary' => $basicSalary,
|
||||
'component_earnings' => $componentEarnings,
|
||||
'overtime_amount' => $overtimeAmount,
|
||||
'unpaid_leave_deduction' => $unpaidLeaveDeduction,
|
||||
'total_deductions' => $totalDeductions,
|
||||
'total_earnings' => $totalEarnings,
|
||||
'gross_pay' => $grossPay,
|
||||
'net_pay' => $netPay,
|
||||
]);
|
||||
|
||||
// Re-calculate Top Level Totals for parent Run Cards
|
||||
if ($payrollEntry->payrollRun) {
|
||||
$payrollEntry->payrollRun->calculateTotals();
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', __('Payroll entry updated successfully'));
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->back()->with('error', $e->getMessage() ?: __('Failed to update payroll entry'));
|
||||
}
|
||||
} else {
|
||||
return redirect()->back()->with('error', __('Permission Denied.'));
|
||||
}
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
if (Auth::user()->can('export-payroll-runs')) {
|
||||
|
||||
Reference in New Issue
Block a user