fix double calculation when mechanic created and auto claim work

This commit is contained in:
2025-07-14 11:06:29 +07:00
parent 720e314bbd
commit 68e7eb3087
2 changed files with 7 additions and 12 deletions

View File

@@ -943,7 +943,9 @@ class TransactionController extends Controller
"date" => $request->date,
"status" => 0, // pending (0) - Mark as pending initially
"created_at" => date('Y-m-d H:i:s'),
"updated_at" => date('Y-m-d H:i:s')
"updated_at" => date('Y-m-d H:i:s'),
"claimed_at" => Auth::user()->role_id == 3 ? now() : null,
"claimed_by" => Auth::user()->role_id == 3 ? Auth::user()->id : null,
];
$data[] = $transactionData;
@@ -1226,22 +1228,14 @@ class TransactionController extends Controller
], 400);
}
// Check if transaction was created by SA (role_id = 4)
$creator = User::find($transaction->user_id);
if (!$creator || $creator->role_id != 4) {
return response()->json([
'status' => 400,
'message' => 'Hanya transaksi yang dibuat oleh Service Advisor yang dapat diklaim'
], 400);
}
// Update transaction with claim information
$transaction->update([
'claimed_at' => now(),
'claimed_by' => Auth::user()->id
]);
// Recalculate KPI achievement after claiming
// Only recalculate KPI if this is a manual claim (not auto-claimed during creation)
// Auto-claimed transactions during creation already have KPI calculated in store method
$kpiService = app(\App\Services\KpiService::class);
$kpiService->calculateKpiAchievementWithClaims(Auth::user());

View File

@@ -414,9 +414,10 @@ class KpiService
->whereMonth('date', $month)
->sum('qty');
// Get transactions claimed by the user
// Get transactions claimed by the user (excluding those created by the same user to avoid double counting)
$claimedTransactions = Transaction::where('claimed_by', $user->id)
->whereNotNull('claimed_at')
->where('user_id', '!=', $user->id) // Exclude transactions created by the same user
->whereYear('claimed_at', $year)
->whereMonth('claimed_at', $month)
->sum('qty');