sozsoft-platform/configs/seeds/sql/object/Hr_S_PerformanceReviewRecalc.sql
2026-10-05 15:58:59 +03:00

83 lines
No EOL
3.4 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

CREATE OR ALTER PROCEDURE [dbo].[Hr_S_PerformanceReviewRecalc]
@ReviewId uniqueidentifier
AS
BEGIN
SET NOCOUNT ON;
IF @ReviewId IS NULL
RETURN;
-- Gerçekleşme %: Artan hedefte Gerçekleşen/Hedef, Azalan hedefte Hedef/Gerçekleşen; 0–150'ye kırpılır.
-- Azalan hedefte sıfır gerçekleşme hedefin tamamen tutturulduğu anlamına gelir, tavan verilir.
UPDATE g
SET AchievementRate = CASE
WHEN x.Rate IS NULL THEN NULL
WHEN x.Rate > 150 THEN 150
WHEN x.Rate < 0 THEN 0
ELSE x.Rate
END
FROM Hr_T_PerformanceGoal g
CROSS APPLY (SELECT CAST(CASE
WHEN g.ActualValue IS NULL THEN NULL
WHEN g.Direction = N'Decreasing' AND g.ActualValue <= 0 THEN 150
WHEN g.Direction = N'Decreasing' THEN g.TargetValue / g.ActualValue * 100
ELSE g.ActualValue / g.TargetValue * 100
END AS decimal(18, 2)) AS Rate) x
WHERE g.ReviewId = @ReviewId AND g.IsDeleted = 0;
-- Yetkinlik puanı yöneticinin seçtiği seviyenin Progress değeridir.
UPDATE c
SET Score = l.Progress
FROM Hr_T_PerformanceCompetency c
LEFT JOIN Hr_T_SkillLevel l ON l.Id = c.ManagerLevelId AND l.IsDeleted = 0
WHERE c.ReviewId = @ReviewId AND c.IsDeleted = 0;
UPDATE f
SET Score = x.Score
FROM Hr_T_PerformanceFeedback f
OUTER APPLY (
SELECT CAST(AVG(CAST(l.Progress AS decimal(9, 2))) AS decimal(6, 2)) AS Score
FROM Hr_T_PerformanceFeedbackScore s
INNER JOIN Hr_T_SkillLevel l ON l.Id = s.LevelId AND l.IsDeleted = 0
WHERE s.FeedbackId = f.Id AND s.IsDeleted = 0) x
WHERE f.ReviewId = @ReviewId AND f.IsDeleted = 0;
UPDATE rv
SET GoalScore = gs.Score,
CompetencyScore = cs.Score,
Feedback360Score = fs.Score,
OverallScore = CASE
WHEN gs.Score IS NULL AND cs.Score IS NULL THEN NULL
ELSE CAST((COALESCE(gs.Score, 0) * p.GoalWeight + COALESCE(cs.Score, 0) * p.CompetencyWeight) / 100 AS decimal(6, 2))
END
FROM Hr_T_PerformanceReview rv
INNER JOIN Hr_T_PerformancePeriod p ON p.Id = rv.PeriodId
OUTER APPLY (
SELECT CASE WHEN COUNT(*) = 0 THEN NULL
ELSE CAST(SUM(COALESCE(g.AchievementRate, 0) * g.Weight) / 100 AS decimal(6, 2)) END AS Score
FROM Hr_T_PerformanceGoal g
WHERE g.ReviewId = rv.Id AND g.IsDeleted = 0) gs
OUTER APPLY (
SELECT CAST(AVG(c.Score) AS decimal(6, 2)) AS Score
FROM Hr_T_PerformanceCompetency c
WHERE c.ReviewId = rv.Id AND c.IsDeleted = 0 AND c.Score IS NOT NULL) cs
OUTER APPLY (
SELECT CAST(AVG(f.Score) AS decimal(6, 2)) AS Score
FROM Hr_T_PerformanceFeedback f
WHERE f.ReviewId = rv.Id AND f.IsDeleted = 0 AND f.Status = N'Submitted' AND f.Score IS NOT NULL) fs
WHERE rv.Id = @ReviewId;
-- Derece kalibre puandan, yoksa genel puandan; aralık alt sınırı dahil, üst sınırı hariçtir.
UPDATE rv
SET RatingId = (
SELECT TOP 1 rt.Id
FROM Hr_T_PerformanceRating rt
WHERE rt.IsDeleted = 0 AND rt.IsActive = 1
AND (rt.TenantId = rv.TenantId OR (rt.TenantId IS NULL AND rv.TenantId IS NULL))
AND COALESCE(rv.CalibratedScore, rv.OverallScore) >= rt.MinScore
AND COALESCE(rv.CalibratedScore, rv.OverallScore) < rt.MaxScore
ORDER BY rt.[Level] DESC)
FROM Hr_T_PerformanceReview rv
WHERE rv.Id = @ReviewId;
END
GO