sozsoft-platform/configs/seeds/host/sql/object/Hr_F_LeaveNetDays.sql
2026-10-05 00:18:13 +03:00

32 lines
1.2 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 FUNCTION [dbo].[Hr_F_LeaveNetDays]
(
@TenantId uniqueidentifier,
@EmployeeId uniqueidentifier,
@StartDate date,
@EndDate date
)
RETURNS decimal(9, 1)
AS
BEGIN
-- İzin talebinin düştüğü gün sayısı: çalışma günleri (Hr_F_LeaveWorkDays) eksi çalışanın
-- rapor (istirahat) kapsamındaki günleri. İzin sırasında alınan raporun günleri izinden sayılmaz;
-- puantajda da o günler Raporlu'dur, bakiye ile puantaj aynı günü iki kez kullanmaz.
IF @StartDate IS NULL OR @EndDate IS NULL OR @EndDate < @StartDate
RETURN 0;
DECLARE @Total decimal(9, 1) = dbo.Hr_F_LeaveWorkDays(@TenantId, @StartDate, @EndDate);
DECLARE @Day date = @StartDate;
WHILE @Day <= @EndDate AND @Total > 0
BEGIN
IF EXISTS (SELECT 1 FROM Adm_T_UserSickLeave s
WHERE s.UserId = @EmployeeId AND s.IsDeleted = 0
AND @Day BETWEEN CAST(s.StartDate AS date) AND CAST(s.EndDate AS date))
SET @Total -= dbo.Hr_F_LeaveWorkDays(@TenantId, @Day, @Day);
SET @Day = DATEADD(day, 1, @Day);
END
RETURN CASE WHEN @Total < 0 THEN 0 ELSE @Total END;
END
GO