Project

General

Profile

Bug #743 » PRPTTMCUTIREKAP_20220112.sql

Tri Rizqiaty, 01/12/2022 03:40 PM

 
1
ALTER PROCEDURE [dbo].[PRPTTMCUTIREKAP]
2
    (
3
      @year VARCHAR(4) ,
4
      @EmployeeID VARCHAR(12)
5
    ) 
6
AS 
7

    
8
--DECLARE @year VARCHAR(4) = '2021'
9
--DECLARE @EmployeeID VARCHAR(12) = '10000156'
10

    
11
DECLARE @startdate VARCHAR(12) = @year + '0101'
12
DECLARE @enddate VARCHAR(12) = @year + '1231'
13

    
14
DECLARE @now VARCHAR(10)
15
SET @now = dbo.fn_formatdatetime(GETDATE(), 'yyyymmdd')
16

    
17
DECLARE @emp_detail TABLE
18
(
19
    emp_id VARCHAR(12) ,
20
    emp_name VARCHAR(250) ,
21
    external_id VARCHAR(30) ,
22
    pos VARCHAR(20) ,
23
    pos_desc VARCHAR(250) ,
24
    pos_abbr VARCHAR(250) ,
25
    quota_type VARCHAR(20) ,
26
    quota_desc VARCHAR(250) ,
27
    quota_stdate VARCHAR(20) ,
28
    quota_endate VARCHAR(20) ,
29
	quota_stdeduction VARCHAR(20) ,
30
	quota_endeduction VARCHAR(20) ,
31
	ExpiredQuota VARCHAR(20),
32
    ori_quota DECIMAL(18, 1) ,
33
    remain_quota DECIMAL(18, 1) ,
34
	previous_qouta DECIMAL(18,1)
35
)
36

    
37
INSERT INTO @emp_detail
38
SELECT DISTINCT TOP(1)
39
                md2.EmployeeID ,
40
                md1.FullName ,
41
                md1.ExternalID ,
42
                md2.position ,
43
                pos.ObjectDescription ,
44
                pos.Abbreviation ,
45
				md17.AbsenceQuotaType ,
46
                absen.AbsenceTypeDesc ,
47
                md17.StartDate ,
48
                md17.EndDate ,
49
				md17.StartDeduction,
50
				md17.EndDeduction,
51
				CASE WHEN md17.ExpiredQuota <> '' THEN md17.ExpiredQuota ELSE '0' END AS ExpiredQuota,
52
                CASE WHEN md17.OriginalQuota <> '' THEN  md17.OriginalQuota ELSE '0' END AS OriginalQuota, 	
53
                CASE WHEN md17.RemainQuota <> '' THEN md17.RemainQuota  ELSE '0' END AS RemainQuota,
54
				CASE WHEN md17.PreviousQuota <> '' THEN md17.PreviousQuota  ELSE '0' END AS PreviousQuota
55
        FROM    dbo.PHRPA0002 AS md2
56
                INNER JOIN dbo.PHRPA0001 AS md1 ON md1.EmployeeID = md2.EmployeeID
57
                                                   AND md1.StartDate <= @enddate
58
                                                   AND md1.EndDate >= @enddate
59
                INNER JOIN dbo.PHRPA0017 AS md17 ON md17.EmployeeID = md1.EmployeeID
60
                INNER JOIN dbo.PHRTMABSTYP AS absen ON absen.AbsenceQuotaType = md17.AbsenceQuotaType
61
                LEFT JOIN dbo.PHROM0001 AS pos ON pos.ObjectID = md2.position
62
                                                  AND pos.ObjectClass = 'P'
63
                                                  AND pos.StartDate <= @enddate
64
                                                  AND pos.EndDate >= @enddate
65
        WHERE   ( md2.EmployeeID = @EmployeeID
66
                  OR @EmployeeID = ''
67
                )
68
                AND md2.StartDate <= @enddate
69
                AND md2.EndDate >= @enddate
70
                AND md17.StartDate <= @enddate
71
                AND md17.EndDate >= @enddate
72
                AND absen.quotadeduction = 'Y'
73

    
74
DECLARE @absence_detail TABLE
75
(
76
    emp_id VARCHAR(12) ,
77
    ws_type VARCHAR(20) ,
78
    start_date VARCHAR(20) ,
79
    end_date VARCHAR(20) ,
80
    id_on_behalf VARCHAR(12) ,
81
    abs_type VARCHAR(20) ,
82
    abs_type_desc VARCHAR(200) ,
83
    abs_descrition VARCHAR(200) ,
84
    abs_durasi DECIMAL(18, 1)
85
)
86

    
87
INSERT  INTO @absence_detail
88
EXECUTE PRPTTMCUTIDETAIL @year, @EmployeeID
89

    
90
UPDATE @absence_detail
91
SET start_date = (RIGHT(start_date,4) + SUBSTRING(start_date, 4,2) + LEFT(start_date,2))
92
	,end_date = (RIGHT(end_date,4) + SUBSTRING(end_date, 4,2) + LEFT(end_date,2))
93

    
94
DECLARE @EndDeductionDate VARCHAR(20) = (SELECT DISTINCT ExpiredQuota FROM @emp_detail)  --------- end date deduction quota cuti tahun kemarin di md17
95
DECLARE @SumPrevQuotaNow DECIMAL(22,1) = (SELECT DISTINCT previous_qouta FROM @emp_detail) --------- jumlah quota cuti dari tahun kemarin di md17
96
DECLARE @RemaingQuotaNow DECIMAL(22,1) = (SELECT DISTINCT remain_quota FROM @emp_detail) --------- jumlah quota cuti dari tahun ini di md17
97

    
98
DECLARE @DurTaken DECIMAL(22,1)  --------- jumlah cuti yang terjadi di tahun sekarang
99
SELECT DISTINCT @DurTaken = SUM(abs_durasi)
100
FROM @absence_detail
101

    
102
DECLARE @DurQuotaSetengahLast DECIMAL(22,1) --------- jumlah cuti setengah hari yang terjadi di tahun kemarin 
103
SELECT DISTINCT @DurQuotaSetengahLast = ISNULL(SUM(abs_durasi),0)
104
FROM @absence_detail AS AbsDetail
105
WHERE end_date <= @EndDeductionDate AND abs_durasi < 1
106

    
107
DECLARE @DurQuotaSetengahNow DECIMAL(22,1) --------- jumlah cuti setengah hari yang terjadi di tahun sekarang 
108
SELECT DISTINCT @DurQuotaSetengahNow = ISNULL(SUM(abs_durasi),0)
109
FROM @absence_detail AS AbsDetail
110
WHERE end_date > @EndDeductionDate AND abs_durasi < 1
111

    
112
DECLARE @DurQuotaLast DECIMAL(22,1) --------- jumlah cuti yang terjadi sampai expired date yang ada di md17
113
SELECT DISTINCT @DurQuotaLast = ISNULL(SUM(abs_durasi),0)
114
FROM @absence_detail AS AbsDetail
115
WHERE end_date <= @EndDeductionDate
116

    
117
DECLARE @DurQuotaNow DECIMAL(22,1) --------- jumlah cuti yang terjadi setelah expired date yang ada di md17
118
SELECT DISTINCT @DurQuotaNow = ISNULL(SUM(ISNULL(abs_durasi,0)),0)
119
FROM @absence_detail AS AbsDetailx
120
WHERE end_date > @EndDeductionDate
121

    
122
DECLARE @Use_Prev_Quota DECIMAL(22,1) --------- jumlah cuti tahun yang menggunakan quota tahun kemarin
123
SET @Use_Prev_Quota = ( CASE WHEN @DurQuotaLast > @SumPrevQuotaNow THEN @SumPrevQuotaNow ELSE @DurQuotaLast END )
124

    
125
DECLARE @SisaCutiInExpiredDate DECIMAL(22,1)  --------- sisa kelebihan cuti yang termasuk dalam expired date
126
SET @SisaCutiInExpiredDate = CASE 
127
								 WHEN @SumPrevQuotaNow > 0 THEN
128
									 CASE 
129
										WHEN @DurQuotaLast > @SumPrevQuotaNow THEN @DurQuotaLast - @SumPrevQuotaNow ELSE 0 
130
									 END
131
								 ELSE 0
132
							 END
133

    
134
DECLARE @used_current_quota DECIMAL(22,1) --------- jumlah cuti tahun yang menggunakan quota tahun sekarang
135
SET @used_current_quota = @DurTaken - CASE WHEN @DurQuotaLast > @SumPrevQuotaNow THEN @SumPrevQuotaNow ELSE @DurQuotaLast END
136

    
137
DECLARE @Remaining_Quotas DECIMAL(22,1) -------- jumlah remaning quota di tahun sekarang setelah dikurangi cuti setengah hari & sisa cuti dari tahun kemarin
138
SET  @Remaining_Quotas = CASE 
139
							WHEN @now > @EndDeductionDate THEN @RemaingQuotaNow - (CASE WHEN @SumPrevQuotaNow > 0 THEN @DurQuotaSetengahNow ELSE @DurQuotaSetengahLast + @DurQuotaSetengahNow END) - @SisaCutiInExpiredDate 
140
							WHEN @now <= @EndDeductionDate THEN @RemaingQuotaNow - ( @DurQuotaSetengahNow + @DurQuotaSetengahLast )
141
						 END
142

    
143
SELECT distinct *
144
,@DurTaken AS totaaal
145
, @Use_Prev_Quota AS Use_Prev_Quota
146
, @used_current_quota AS used_current_quota
147
, emp_detail.previous_qouta AS previous_remaining_quota
148
, @Remaining_Quotas AS Remaining_Quotas
149
FROM @emp_detail AS emp_detail
150

    
151

    
152

    
(2-2/2)