Allow inline editing of monthly earnings breakdown for 13th month backtracking

This commit is contained in:
2026-07-22 10:49:03 +08:00
parent 7b7cdf3f03
commit 11a55cd6de
417 changed files with 14855 additions and 14763 deletions

View File

@@ -101,28 +101,44 @@ class ThirteenthMonthController extends Controller
}
/**
* Update an individual employee's adjustment or note.
* Update an individual employee's adjustment, monthly earnings breakdown, or notes.
*/
public function updateEntry(Request $request, $id)
{
$request->validate([
'adjustment_amount' => 'required|numeric',
'adjustment_amount' => 'nullable|numeric',
'monthly_earnings' => 'nullable|array',
'notes' => 'nullable|string|max:255',
]);
$entry = ThirteenthMonthEntry::findOrFail($id);
$adjustment = (float) $request->adjustment_amount;
$entry->adjustment_amount = $adjustment;
$entry->final_payout = round($entry->computed_amount + $adjustment, 2);
if ($request->has('adjustment_amount')) {
$entry->adjustment_amount = (float) $request->adjustment_amount;
}
if ($request->has('monthly_earnings')) {
$monthlyEarnings = $request->monthly_earnings;
$entry->monthly_earnings = $monthlyEarnings;
// Recalculate employee total base earned, computed amount, and final payout
$totalBase = array_sum(array_map('floatval', $monthlyEarnings));
$entry->total_base_earned = round($totalBase, 2);
$entry->computed_amount = round($totalBase / 12, 2);
}
$entry->final_payout = round($entry->computed_amount + $entry->adjustment_amount, 2);
if ($request->has('notes')) {
$entry->notes = $request->notes;
}
$entry->save();
// Recalculate parent run totals
$run = $entry->run;
if ($run) {
$run->total_base_earnings = round($run->entries()->sum('total_base_earned'), 2);
$run->total_payout = round($run->entries()->sum('final_payout'), 2);
$run->save();
}