1
|
/*DECLARE @bizprocid VARCHAR(100) = 'ESS_Overtime_AJTM'
|
2
|
DECLARE @reg_id VARCHAR(10) = '55'*/
|
3
|
DECLARE @today VARCHAR(10)
|
4
|
SET @today = dbo.fn_formatdatetime(GETDATE(), 'yyyymm')
|
5
|
DECLARE @DateFrom VARCHAR(10)
|
6
|
DECLARE @DateFromOVT VARCHAR(10)
|
7
|
DECLARE @Sequence VARCHAR(10)
|
8
|
--- start variable ---
|
9
|
SET @Sequence = (SELECT MAX([Sequence]) FROM PCMWFRUNBIZDATA WHERE RegID = @reg_id)
|
10
|
SET @DateFrom = ( SELECT TOP (1)
|
11
|
SUBSTRING(Value, 1, 6)
|
12
|
FROM PCMWFRUNBIZDATA
|
13
|
WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and FieldID = 'DateFrom')
|
14
|
|
15
|
SET @DateFromOVT = ( SELECT TOP (1)
|
16
|
Value
|
17
|
FROM PCMWFRUNBIZDATA
|
18
|
WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and FieldID = 'DateFrom')
|
19
|
|
20
|
--- end variable ---
|
21
|
|
22
|
IF @DateFrom < @today
|
23
|
BEGIN
|
24
|
|
25
|
-- start (kondisi ketika period payroll yang sama maka di delete) redmine 1774 : 4
|
26
|
DECLARE @EmployeeOV AS TABLE
|
27
|
(
|
28
|
EmployeeID VARCHAR(MAX)
|
29
|
)
|
30
|
INSERT INTO @EmployeeOV
|
31
|
SELECT Value
|
32
|
FROM PCMWFRUNBIZDATA
|
33
|
WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and FieldID = 'EmployeeID_OT'
|
34
|
|
35
|
DECLARE @Temp AS TABLE
|
36
|
(
|
37
|
EmployeeID VARCHAR(MAX),
|
38
|
PayrollGrup VARCHAR(MAX),
|
39
|
PayrollPeriod VARCHAR(MAX)
|
40
|
)
|
41
|
INSERT INTO @Temp
|
42
|
SELECT pa02.EmployeeID,
|
43
|
pa02.PayrollGroup,
|
44
|
ctr.PayrollPeriod
|
45
|
FROM PHRPA0002 pa02
|
46
|
INNER JOIN @EmployeeOV emp ON pa02.EmployeeID = emp.EmployeeID
|
47
|
LEFT JOIN PHRPYPGCTRL ctr ON pa02.PayrollGroup = ctr.PayrollGroup
|
48
|
WHERE PayrollPeriod > (SELECT TOP (1)
|
49
|
SUBSTRING(Value, 1, 6)
|
50
|
FROM PCMWFRUNBIZDATA
|
51
|
WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and FieldID = 'DateFrom') and pa02.StartDate <= '20221207' and pa02.EndDate >= '20221207'
|
52
|
|
53
|
---- end (kondisi ketika period payroll yang sama maka di delete)
|
54
|
|
55
|
-- start kententuan ketentuan retro
|
56
|
DECLARE @UpdateBizData AS TABLE
|
57
|
(
|
58
|
DataSequencer VARCHAR(MAX),
|
59
|
FieldID VARCHAR(MAX),
|
60
|
DateFrom VARCHAR(MAX),
|
61
|
RegID VARCHAR(MAX),
|
62
|
[Sequence] VARCHAR(MAX),
|
63
|
TableReference VARCHAR(MAX),
|
64
|
BizProcessID VARCHAR(MAX)
|
65
|
|
66
|
)
|
67
|
INSERT INTO @UpdateBizData
|
68
|
select DataSequence, FieldID,Value,RegID,[Sequence],TableReference,BizProcessID from PCMWFRUNBIZDATA where FieldID ='DateFrom2' and BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and DataSequence in (SELECT distinct DataSequence FROM PCMWFRUNBIZDATA WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and DataSequence IN ( select DataSequence from PCMWFRUNBIZDATA WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and FieldID = 'EmployeeID_OT' and Value IN (
|
69
|
SELECT
|
70
|
EmployeeID
|
71
|
FROM
|
72
|
@Temp
|
73
|
) ))
|
74
|
|
75
|
DECLARE @EmployeeDataSeq AS TABLE
|
76
|
(
|
77
|
DataSequence VARCHAR(MAX),
|
78
|
FieldID VARCHAR(MAX),
|
79
|
EmployeeID VARCHAR(MAX)
|
80
|
)
|
81
|
INSERT INTO @EmployeeDataSeq
|
82
|
select DataSequence, FieldID,Value from PCMWFRUNBIZDATA where FieldID ='EmployeeID_OT' and BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and DataSequence in (SELECT distinct DataSequence FROM PCMWFRUNBIZDATA WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and DataSequence IN ( select DataSequence from PCMWFRUNBIZDATA WHERE BizProcessID = @bizprocid and RegID = @reg_id and [Sequence] = @Sequence and FieldID = 'EmployeeID_OT' and Value IN (
|
83
|
SELECT
|
84
|
EmployeeID
|
85
|
FROM
|
86
|
@Temp
|
87
|
) ))
|
88
|
|
89
|
|
90
|
UPDATE @UpdateBizData
|
91
|
SET DateFrom = Case when a.RetroDate > (select @DateFromOVT ) then (select @DateFromOVT )
|
92
|
when a.RetroDate is null then (select @DateFromOVT )
|
93
|
when a.RetroDate < (select @DateFromOVT) then a.RetroDate
|
94
|
else a.RetroDate END
|
95
|
FROM ( SELECT pa15.EmployeeID,pa15.RetroDate, emp.DataSequence
|
96
|
FROM PHRPA0015 pa15
|
97
|
LEFT JOIN @EmployeeDataSeq emp ON pa15.EmployeeID = emp.EmployeeID
|
98
|
where pa15.EmployeeID in (select EmployeeID from @Temp)
|
99
|
) a
|
100
|
where a.DataSequence = DataSequencer
|
101
|
|
102
|
UPDATE @UpdateBizData
|
103
|
SET DateFrom = @DateFromOVT
|
104
|
FROM ( SELECT pa15.EmployeeID,pa15.RetroDate, emp.DataSequence
|
105
|
FROM PHRPA0015 pa15
|
106
|
LEFT JOIN @EmployeeDataSeq emp ON pa15.EmployeeID = emp.EmployeeID
|
107
|
where pa15.EmployeeID in (select EmployeeID from @Temp)
|
108
|
) a
|
109
|
where SUBSTRING(a.RetroDate,1,4) not in (select SUBSTRING(PayrollPeriod,1,4) from PHRPYPGCTRL where EmployeeID in (select EmployeeID from @Temp)) and a.DataSequence = DataSequencer
|
110
|
|
111
|
DECLARE @T_final AS TABLE
|
112
|
(
|
113
|
RegID VARCHAR(MAX),
|
114
|
[Sequence] VARCHAR(MAX),
|
115
|
BizProcessID VARCHAR(MAX),
|
116
|
TableReference VARCHAR(MAX),
|
117
|
FieldID VARCHAR(MAX),
|
118
|
DataSequence VARCHAR(MAX),
|
119
|
Value VARCHAR(MAX)
|
120
|
)
|
121
|
INSERT INTO @T_final
|
122
|
SELECT RegID, [Sequence],BizProcessID,TableReference,FieldID,DataSequencer,DateFrom from @UpdateBizData
|
123
|
|
124
|
DELETE FROM PCMWFRUNBIZDATA WHERE RegID = @reg_id AND BizProcessID = @bizprocid AND Sequence = @Sequence AND FieldID = 'DateFrom2' AND DataSequence IN (SELECT DataSequencer FROM @UpdateBizData)
|
125
|
|
126
|
INSERT INTO PCMWFRUNBIZDATA
|
127
|
SELECT RegID,
|
128
|
[Sequence],
|
129
|
BizProcessID,
|
130
|
TableReference,
|
131
|
FieldID,
|
132
|
DataSequence,
|
133
|
Value
|
134
|
FROM @T_final
|
135
|
WHERE RegID = @reg_id AND BizProcessID = @bizprocid AND Sequence = @Sequence AND FieldID = 'DateFrom2' AND DataSequence IN (SELECT DataSequencer FROM @UpdateBizData )
|
136
|
|
137
|
-- end kententuan ketentuan retro
|
138
|
|
139
|
SELECT
|
140
|
'1' AS res
|
141
|
END
|
142
|
ELSE
|
143
|
BEGIN
|
144
|
SELECT
|
145
|
'0' AS res
|
146
|
END
|