fix double calculation when mechanic created and auto claim work
This commit is contained in:
@@ -943,7 +943,9 @@ class TransactionController extends Controller
|
|||||||
"date" => $request->date,
|
"date" => $request->date,
|
||||||
"status" => 0, // pending (0) - Mark as pending initially
|
"status" => 0, // pending (0) - Mark as pending initially
|
||||||
"created_at" => date('Y-m-d H:i:s'),
|
"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;
|
$data[] = $transactionData;
|
||||||
@@ -1226,22 +1228,14 @@ class TransactionController extends Controller
|
|||||||
], 400);
|
], 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
|
// Update transaction with claim information
|
||||||
$transaction->update([
|
$transaction->update([
|
||||||
'claimed_at' => now(),
|
'claimed_at' => now(),
|
||||||
'claimed_by' => Auth::user()->id
|
'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 = app(\App\Services\KpiService::class);
|
||||||
$kpiService->calculateKpiAchievementWithClaims(Auth::user());
|
$kpiService->calculateKpiAchievementWithClaims(Auth::user());
|
||||||
|
|
||||||
|
|||||||
@@ -414,9 +414,10 @@ class KpiService
|
|||||||
->whereMonth('date', $month)
|
->whereMonth('date', $month)
|
||||||
->sum('qty');
|
->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)
|
$claimedTransactions = Transaction::where('claimed_by', $user->id)
|
||||||
->whereNotNull('claimed_at')
|
->whereNotNull('claimed_at')
|
||||||
|
->where('user_id', '!=', $user->id) // Exclude transactions created by the same user
|
||||||
->whereYear('claimed_at', $year)
|
->whereYear('claimed_at', $year)
|
||||||
->whereMonth('claimed_at', $month)
|
->whereMonth('claimed_at', $month)
|
||||||
->sum('qty');
|
->sum('qty');
|
||||||
|
|||||||
Reference in New Issue
Block a user