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

50 lines
2 KiB
Transact-SQL
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 ALTER PROCEDURE [dbo].[Hr_S_WorkCalendarAfterSave]
@CalendarId uniqueidentifier,
@ActorId uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
-- Çalışma Takvimi satırı eklenince, değişince ya da silinince tatile denk gelen izin taleplerinin
-- gün sayısı ve puantaj yeniden hesaplanır; aksi hâlde sonradan girilen bayram izinden düşmeye,
-- puantajda çalışma günü görünmeye devam ederdi. Yıllık tekrarlı satırın bu yılki ve gelecek
-- yılki karşılığı işlenir; kapalı puantaj ayı yazılmaz.
DECLARE @TenantId uniqueidentifier, @StartDate date, @EndDate date, @Recurrence nvarchar(256);
SELECT @TenantId = TenantId, @StartDate = CAST(StartDate AS date), @EndDate = CAST(EndDate AS date),
@Recurrence = RecurrenceRule
FROM Hr_T_WorkCalendar
WHERE Id = @CalendarId;
IF @StartDate IS NULL
RETURN;
IF @Recurrence = N'FREQ=YEARLY'
BEGIN
DECLARE @Shift int = YEAR(GETDATE()) - YEAR(@StartDate);
IF @Shift > 0
BEGIN
SET @StartDate = DATEADD(year, @Shift, @StartDate);
SET @EndDate = DATEADD(year, @Shift + 1, @EndDate);
END
ELSE
SET @EndDate = DATEADD(year, 1, @EndDate);
END
UPDATE r
SET Days = CASE WHEN r.IsHalfDay = 1 AND n.Days > 0.5 THEN 0.5 ELSE n.Days END
FROM Hr_T_LeaveRequest r
INNER JOIN Hr_T_LeaveType t ON t.Id = r.LeaveTypeId
CROSS APPLY (
SELECT CASE WHEN t.Code = N'Sick'
THEN dbo.Hr_F_LeaveWorkDays(r.TenantId, CAST(r.StartDate AS date), CAST(r.EndDate AS date))
ELSE dbo.Hr_F_LeaveNetDays(r.TenantId, r.EmployeeId, CAST(r.StartDate AS date), CAST(r.EndDate AS date))
END AS Days
) n
WHERE r.IsDeleted = 0
AND (r.TenantId = @TenantId OR (r.TenantId IS NULL AND @TenantId IS NULL))
AND CAST(r.StartDate AS date) <= @EndDate AND CAST(r.EndDate AS date) >= @StartDate;
EXEC dbo.Hr_S_TimesheetCalculate @TenantId, NULL, @StartDate, @EndDate, @ActorId;
END
GO