1
|
--ALTER PROCEDURE GENLEAVEQUOTA
|
2
|
--(
|
3
|
-- @Now VARCHAR(20)
|
4
|
-- ,@StartDate VARCHAR(20)
|
5
|
-- ,@EndDate VARCHAR(20)
|
6
|
-- ,@QuotaType VARCHAR(20)
|
7
|
-- ,@Sdeduc VARCHAR(20)
|
8
|
-- ,@Ededuc VARCHAR(20)
|
9
|
-- ,@Quota VARCHAR(20)
|
10
|
-- ,@UserName VARCHAR(20)
|
11
|
-- ,@CodeHiring VARCHAR(20)
|
12
|
-- ,@YosMin VARCHAR(20)
|
13
|
-- ,@YosMax VARCHAR(20)
|
14
|
-- ,@Now14 VARCHAR(20)
|
15
|
-- ,@MenuID VARCHAR(20)
|
16
|
-- ,@IsProrate VARCHAR(20)
|
17
|
-- ,@EmpType VARCHAR(20)
|
18
|
-- ,@EmpSubType VARCHAR(20)
|
19
|
-- ,@GradeStart VARCHAR(20)
|
20
|
-- ,@GradeEnd VARCHAR(20)
|
21
|
-- ,@EmpStart VARCHAR(20)
|
22
|
-- ,@EmpEnd VARCHAR(20)
|
23
|
--)
|
24
|
--AS
|
25
|
|
26
|
DECLARE @Now VARCHAR(20) = '20221129'
|
27
|
DECLARE @StartDate VARCHAR(20) = '20220101'
|
28
|
DECLARE @EndDate VARCHAR(20) = '20221231'
|
29
|
DECLARE @QuotaType VARCHAR(20) = '11'
|
30
|
DECLARE @Sdeduc VARCHAR(20) = '20220101'
|
31
|
DECLARE @Ededuc VARCHAR(20) = '20221231'
|
32
|
DECLARE @Quota VARCHAR(20) = '12'
|
33
|
DECLARE @UserName VARCHAR(20) = 'tri'
|
34
|
DECLARE @CodeHiring VARCHAR(20) = '01'
|
35
|
DECLARE @YosMin VARCHAR(20) = '1'
|
36
|
DECLARE @YosMax VARCHAR(20) = '99'
|
37
|
DECLARE @Now14 VARCHAR(20) = '20221129113059'
|
38
|
DECLARE @MenuID VARCHAR(20) = 'TM05'
|
39
|
DECLARE @IsProrate VARCHAR(20) = '1'
|
40
|
DECLARE @EmpType VARCHAR(20) = ''
|
41
|
DECLARE @EmpSubType VARCHAR(20) = ''
|
42
|
DECLARE @GradeStart VARCHAR(20) = ''
|
43
|
DECLARE @GradeEnd VARCHAR(20) = ''
|
44
|
DECLARE @EmpStart VARCHAR(20) = ''--'99010003'
|
45
|
DECLARE @EmpEnd VARCHAR(20) = ''--'21021444'
|
46
|
|
47
|
DECLARE @EmpAuth TABLE (EmployeeID VARCHAR(20))
|
48
|
INSERT INTO @EmpAuth EXECUTE dbo.GETEMPIDAUTHBYMENU @MenuID = @MenuID, @UserID = @UserName
|
49
|
SELECT * FROM
|
50
|
( SELECT DISTINCT @StartDate AS StartDate,
|
51
|
@EndDate AS EndDate,
|
52
|
md1.EmployeeID ,
|
53
|
CASE WHEN ( md39.Date IS NULL ) THEN md15.HiringDate
|
54
|
ELSE md39.Date
|
55
|
END AS HiringDate ,
|
56
|
DATEDIFF(MONTH,( CASE WHEN ( md39.Date IS NULL ) THEN md15.HiringDate ELSE md39.Date END ), @EndDate)/12 AS YearOfService ,
|
57
|
@QuotaType AS AbsenceQuotaType,
|
58
|
'1' AS Sequence,
|
59
|
@Sdeduc AS StartDeduction,
|
60
|
@Ededuc AS EndDeduction,
|
61
|
CASE WHEN @IsProrate = '1'
|
62
|
THEN
|
63
|
CASE WHEN DATEDIFF(MONTH,( CASE WHEN ( md39.Date IS NULL ) THEN md15.HiringDate ELSE md39.Date END ), @EndDate)/12 = 1
|
64
|
THEN CONVERT(DECIMAL(18,0),ROUND(CONVERT(DECIMAL(18,0),DATEDIFF(MONTH,CASE WHEN (md39.Date IS NULL) THEN md15.HiringDate ELSE md39.Date END,@StartDate))/12 * CONVERT(DECIMAL(18,0), @Quota),0))
|
65
|
WHEN DATEDIFF(MONTH,( CASE WHEN ( md39.Date IS NULL ) THEN md15.HiringDate ELSE md39.Date END ), @EndDate)/12 = 0
|
66
|
THEN '0'
|
67
|
ELSE @Quota END
|
68
|
ELSE @Quota END AS OriginalQuota,
|
69
|
CASE WHEN @IsProrate = '1'
|
70
|
THEN
|
71
|
CASE WHEN DATEDIFF(MONTH,( CASE WHEN ( md39.Date IS NULL ) THEN md15.HiringDate ELSE md39.Date END ), @EndDate)/12 = 1
|
72
|
THEN CONVERT(DECIMAL(18,0),ROUND(CONVERT(DECIMAL(18,0),DATEDIFF(MONTH,CASE WHEN (md39.Date IS NULL) THEN md15.HiringDate ELSE md39.Date END,@StartDate))/12 * CONVERT(DECIMAL(18,0), @Quota),0))
|
73
|
WHEN DATEDIFF(MONTH,( CASE WHEN ( md39.Date IS NULL ) THEN md15.HiringDate ELSE md39.Date END ), @EndDate)/12 = 0
|
74
|
THEN '0'
|
75
|
ELSE @Quota END
|
76
|
ELSE @Quota END AS RemainQuota,
|
77
|
md1.FullName ,
|
78
|
md2.EmployeeOffice ,
|
79
|
md2.EmployeeArea ,
|
80
|
md2.EmployeeType ,
|
81
|
md2.EmployeeSubType ,
|
82
|
md3.PayrollGrade,
|
83
|
@UserName AS CreateBy,
|
84
|
@Now14 AS CreateDate,
|
85
|
@UserName AS ChangeBy,
|
86
|
@Now14 AS ChangeDate
|
87
|
FROM PHRPA0015 md15 WITH(NOLOCK)
|
88
|
LEFT JOIN PHRPA0039 md39 WITH(NOLOCK)
|
89
|
ON md15.EmployeeID = md39.EmployeeID
|
90
|
AND md39.DateType = @CodeHiring
|
91
|
INNER JOIN PHRPA0001 md1 WITH(NOLOCK)
|
92
|
ON md15.EmployeeID = md1.EmployeeID
|
93
|
AND md1.StartDate <= @Now
|
94
|
AND md1.EndDate >= @Now
|
95
|
INNER JOIN PHRPA0002 md2 WITH(NOLOCK)
|
96
|
ON md1.EmployeeID = md2.EmployeeID
|
97
|
AND md2.StartDate <= @Now
|
98
|
AND md2.EndDate >= @Now
|
99
|
LEFT JOIN PHRPA0003 md3 WITH(NOLOCK)
|
100
|
ON md1.EmployeeID = md3.EmployeeID
|
101
|
AND md2.StartDate <= @Now
|
102
|
AND md2.EndDate >= @Now
|
103
|
INNER JOIN @EmpAuth EmpAuth ON md1.EmployeeID = EmpAuth.EmployeeID
|
104
|
WHERE (md2.EmployeeType = @EmpType OR @EmpType = '')
|
105
|
AND (md2.EmployeeSubType = @EmpSubType OR @EmpSubType = '')
|
106
|
AND (md2.EmployeeID BETWEEN @EmpStart AND @EmpEnd OR @EmpStart = '' OR @EmpEnd = '')
|
107
|
AND (md3.PayrollGrade BETWEEN @GradeStart AND @GradeEnd OR @GradeStart = '' OR @GradeEnd = '')
|
108
|
) AS tb
|
109
|
WHERE (YearOfService BETWEEN @YosMin AND @YosMax)
|