From 68e7eb3087cc60ba9967568315d4388b4023063d Mon Sep 17 00:00:00 2001 From: arifal Date: Mon, 14 Jul 2025 11:06:29 +0700 Subject: [PATCH] fix double calculation when mechanic created and auto claim work --- app/Http/Controllers/TransactionController.php | 16 +++++----------- app/Services/KpiService.php | 3 ++- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index d047259..c4aa4a1 100755 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -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()); diff --git a/app/Services/KpiService.php b/app/Services/KpiService.php index c994210..53a4c13 100644 --- a/app/Services/KpiService.php +++ b/app/Services/KpiService.php @@ -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');