1
|
|
2
|
|
3
|
ALTER PROCEDURE [dbo].[GenerateMedPlafMass]
|
4
|
AS
|
5
|
|
6
|
|
7
|
DECLARE @year VARCHAR(12) = dbo.fn_formatdatetime(GETDATE(), 'yyyy')
|
8
|
|
9
|
DECLARE @issimulate VARCHAR (2)='0'
|
10
|
DECLARE @treattype varchar(50)=''
|
11
|
DECLARE @CreateBy VARCHAR(200)=''
|
12
|
DECLARE @ChangeBy VARCHAR(200)=''
|
13
|
DECLARE @startdate VARCHAR(20)=@year+'0101'
|
14
|
DECLARE @enddate VARCHAR(20)=@year+'1231'
|
15
|
DECLARE @paygradestart VARCHAR(20)=''
|
16
|
DECLARE @paygradeend VARCHAR(20)=''
|
17
|
DECLARE @empstart VARCHAR(200)='00000000'
|
18
|
DECLARE @empend VARCHAR(200)='99999999'
|
19
|
DECLARE @emptype VARCHAR(200)=''
|
20
|
DECLARE @empsubtype VARCHAR(200)=''
|
21
|
|
22
|
|
23
|
DECLARE @now VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmdd')
|
24
|
DECLARE @datecreate VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmddhhmmdd')
|
25
|
DECLARE @CreateDate VARCHAR(200)=''
|
26
|
DECLARE @ChangeDate VARCHAR(200)=''
|
27
|
set @CreateDate =@datecreate
|
28
|
set @ChangeDate =@datecreate
|
29
|
|
30
|
|
31
|
DECLARE @datatable as table (
|
32
|
HeaderSequence varchar(500),
|
33
|
Sequence varchar(500),
|
34
|
StartDate varchar(500),
|
35
|
EndDate varchar(500),
|
36
|
EmployeeID varchar(500),
|
37
|
TreatmentType varchar(500),
|
38
|
Plafond varchar(500),
|
39
|
PlafondRemain varchar(500),
|
40
|
PlafondUsed varchar(500),
|
41
|
MedicalFamily varchar(500),
|
42
|
PayrollGrade varchar(500),
|
43
|
CreateBy varchar(500),
|
44
|
CreateDate varchar(500),
|
45
|
ChangeBy varchar(500),
|
46
|
ChangeDate varchar(500)
|
47
|
)
|
48
|
DECLARE @datatablefinish as table (
|
49
|
HeaderSequence varchar(500),
|
50
|
Sequence varchar(500),
|
51
|
StartDate varchar(500),
|
52
|
EndDate varchar(500),
|
53
|
EmployeeID varchar(500),
|
54
|
TreatmentType varchar(500),
|
55
|
Plafond varchar(500),
|
56
|
PlafondRemain varchar(500),
|
57
|
PlafondUsed varchar(500),
|
58
|
MedicalFamily varchar(500),
|
59
|
PayrollGrade varchar(500),
|
60
|
CreateBy varchar(500),
|
61
|
CreateDate varchar(500),
|
62
|
ChangeBy varchar(500),
|
63
|
ChangeDate varchar(500)
|
64
|
)
|
65
|
|
66
|
|
67
|
if @emptype !='' or @emptype is not null
|
68
|
begin
|
69
|
select convert(varchar(50),'1')as HeaderSequence
|
70
|
,ROW_NUMBER() OVER (partition by pa2.EmployeeID order by pa2.EmployeeID) as [Sequence],@startdate as StartDate,@enddate as EndDate,pa2.EmployeeID
|
71
|
,PLAF.TreatmentType,PLAF.FixedValue as Plafond,PLAF.FixedValue as PlafondRemain,convert(varchar(50),'0.00')as PlafondUsed ,PLAF.MedicalFamily
|
72
|
,pa3.PayrollGrade
|
73
|
into #temp
|
74
|
from PHRPA0002 pa2 left outer join
|
75
|
PHRPA0003 pa3 on pa2.EmployeeID = pa3.EmployeeID and pa3.EndDate='99991231' left outer join
|
76
|
PHRBNMEDTREATPLAF PLAF on PLAF.CompanyID = pa2.CompanyID and PLAF.PayClass = pa3.PayrollClass and PLAF.EndDate='99991231'
|
77
|
and (PLAF.PayClass = pa3.PayrollClass or PLAF.PayClass ='*')and (PLAF.PayType = pa3.PayrollType or PLAF.PayType ='*')
|
78
|
and (PLAF.PayGrade = pa3.PayrollGrade or PLAF.PayGrade ='*')and (PLAF.PayArea = pa3.PayrollArea or PLAF.PayArea ='*')
|
79
|
--and PLAF.TreatmentType = @treattype
|
80
|
where pa2.EmployeeStatus='01' and pa2.StartDate <= @now and pa2.EndDate >= @now and pa2.EmployeeType = @emptype
|
81
|
|
82
|
update #temp set
|
83
|
HeaderSequence = a.HeaderSequence from (select max(HeaderSequence) + 1as HeaderSequence,EmployeeID from phrpa0040 group by EmployeeID)a where a.EmployeeID = #temp.EmployeeID
|
84
|
|
85
|
|
86
|
insert into @datatable
|
87
|
select *,
|
88
|
@CreateBy,
|
89
|
@CreateDate,
|
90
|
@ChangeBy,
|
91
|
@ChangeDate
|
92
|
from #temp
|
93
|
|
94
|
drop table #temp
|
95
|
end
|
96
|
|
97
|
if @emptype ='' or @emptype is null
|
98
|
begin
|
99
|
select convert(varchar(50),'1')as HeaderSequence
|
100
|
,ROW_NUMBER() OVER (partition by pa2.EmployeeID order by pa2.EmployeeID) as [Sequence],@startdate as StartDate,@enddate as EndDate,pa2.EmployeeID
|
101
|
,PLAF.TreatmentType,PLAF.FixedValue as Plafond,PLAF.FixedValue as PlafondRemain,convert(varchar(50),'0.00')as PlafondUsed ,PLAF.MedicalFamily
|
102
|
,pa3.PayrollGrade
|
103
|
into #temp2
|
104
|
from PHRPA0002 pa2 left outer join
|
105
|
PHRPA0003 pa3 on pa2.EmployeeID = pa3.EmployeeID and pa3.EndDate='99991231' left outer join
|
106
|
PHRBNMEDTREATPLAF PLAF on PLAF.CompanyID = pa2.CompanyID and PLAF.EndDate='99991231'
|
107
|
and (PLAF.PayClass = pa3.PayrollClass or PLAF.PayClass ='*')and (PLAF.PayType = pa3.PayrollType or PLAF.PayType ='*')
|
108
|
and (PLAF.PayGrade = pa3.PayrollGrade or PLAF.PayGrade ='*')and (PLAF.PayArea = pa3.PayrollArea or PLAF.PayArea ='*')
|
109
|
--and PLAF.TreatmentType = @treattype
|
110
|
where pa2.EmployeeStatus='01' and pa2.StartDate <= @now and pa2.EndDate >= @now
|
111
|
update #temp2 set
|
112
|
HeaderSequence = a.HeaderSequence from (select max(HeaderSequence) + 1as HeaderSequence,EmployeeID from phrpa0040 group by EmployeeID)a where a.EmployeeID = #temp2.EmployeeID
|
113
|
|
114
|
|
115
|
insert into @datatable
|
116
|
select *,
|
117
|
@CreateBy,
|
118
|
@CreateDate,
|
119
|
@ChangeBy,
|
120
|
@ChangeDate
|
121
|
from #temp2
|
122
|
|
123
|
drop table #temp2
|
124
|
end
|
125
|
|
126
|
if @paygradestart !='' and @empstart=''
|
127
|
begin
|
128
|
insert into @datatablefinish
|
129
|
select * from @datatable where PayrollGrade between @paygradestart and @paygradeend
|
130
|
end
|
131
|
if @empstart !='' and @paygradestart =''
|
132
|
begin
|
133
|
insert into @datatablefinish
|
134
|
select * from @datatable where EmployeeID between @empstart and @empend
|
135
|
end
|
136
|
if @paygradestart !='' and @empstart !=''
|
137
|
begin
|
138
|
insert into @datatablefinish
|
139
|
select * from @datatable where PayrollGrade between @paygradestart and @paygradeend and EmployeeID between @empstart and @empend
|
140
|
end
|
141
|
|
142
|
|
143
|
|
144
|
if @issimulate !='0'
|
145
|
begin
|
146
|
insert into PHRPA0040
|
147
|
select distinct
|
148
|
StartDate ,
|
149
|
EndDate ,
|
150
|
EmployeeID ,
|
151
|
HeaderSequence ,
|
152
|
'',
|
153
|
'System Mass',
|
154
|
CreateBy ,
|
155
|
CreateDate ,
|
156
|
ChangeBy ,
|
157
|
ChangeDate ,
|
158
|
'','','','','','','','','','','','','','',0,'','','',null
|
159
|
from @datatablefinish
|
160
|
|
161
|
insert into PHRPA0041
|
162
|
select
|
163
|
EmployeeID ,
|
164
|
HeaderSequence ,
|
165
|
Sequence,
|
166
|
TreatmentType,
|
167
|
Plafond,
|
168
|
PlafondRemain,
|
169
|
PlafondUsed,
|
170
|
MedicalFamily,
|
171
|
''
|
172
|
from @datatablefinish
|
173
|
end
|
174
|
--select distinct EmployeeID,'Sukses Generate'as Status from @datatablefinish
|
175
|
|
176
|
select
|
177
|
EmployeeID ,
|
178
|
HeaderSequence ,
|
179
|
Sequence,
|
180
|
TreatmentType,
|
181
|
Plafond,
|
182
|
PlafondRemain,
|
183
|
PlafondUsed,
|
184
|
MedicalFamily,
|
185
|
CASE WHEN @issimulate='0' then 'Simulate' WHEN @issimulate='1' then 'Save Success' end as Status
|
186
|
from @datatablefinish
|
187
|
|
188
|
|