sozsoft-platform/configs/seeds/postgres/object/Hr_F_LeaveNetDays.sql
2026-10-06 14:24:30 +03:00

37 lines
1.2 KiB
PL/PgSQL
Raw Permalink 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 REPLACE FUNCTION public."Hr_F_LeaveNetDays"
(
p_TenantId uuid,
p_EmployeeId uuid,
p_StartDate date,
p_EndDate date
)
RETURNS numeric(9, 1)
LANGUAGE plpgsql
STABLE
AS $$
DECLARE
v_Total numeric(9, 1);
v_Day date := p_StartDate;
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 p_StartDate IS NULL OR p_EndDate IS NULL OR p_EndDate < p_StartDate THEN
RETURN 0;
END IF;
v_Total := public."Hr_F_LeaveWorkDays"(p_TenantId, p_StartDate, p_EndDate);
WHILE v_Day <= p_EndDate AND v_Total > 0 LOOP
IF EXISTS (SELECT 1 FROM "Adm_T_UserSickLeave" s
WHERE s."UserId" = p_EmployeeId AND s."IsDeleted" = FALSE
AND v_Day BETWEEN s."StartDate"::date AND s."EndDate"::date) THEN
v_Total := v_Total - public."Hr_F_LeaveWorkDays"(p_TenantId, v_Day, v_Day);
END IF;
v_Day := v_Day + 1;
END LOOP;
RETURN CASE WHEN v_Total < 0 THEN 0 ELSE v_Total END;
END;
$$;