1
|
|
2
|
ALTER PROCEDURE [dbo].[GenerateAbsenQuotaCutiTahunan]
|
3
|
AS
|
4
|
|
5
|
|
6
|
DECLARE @AbsenceQuotaType VARCHAR (10)='1000'
|
7
|
DECLARE @Year VARCHAR (4)=dbo.fn_formatdatetime(GETDATE(), 'yyyy')
|
8
|
DECLARE @CreateDate VARCHAR (50)=CONVERT(VARCHAR, GETDATE(), 112) + REPLACE(CONVERT(VARCHAR, GETDATE(), 108), ':', '')
|
9
|
DECLARE @OriginalQuota INT = 12
|
10
|
DECLARE @RemainQuota INT = 12
|
11
|
DECLARE @CreateBy VARCHAR (100) = 'minovais'
|
12
|
DECLARE @GenerateType VARCHAR = '1'
|
13
|
|
14
|
--GENERATE TYPE 1 = EXECUTE
|
15
|
--GENERATE TYPE 0 = SIMULATE
|
16
|
|
17
|
--GET PREVIOUS QUOTA
|
18
|
|
19
|
DECLARE @YearBefore VARCHAR (4) = (Select @Year-1)
|
20
|
|
21
|
select distinct
|
22
|
EmployeeID
|
23
|
,MAX(Sequence) AS MaxSeq
|
24
|
into #MaxSeq
|
25
|
from PHRPA0017
|
26
|
where left(StartDate,4)=@YearBefore and AbsenceQuotaType=@AbsenceQuotaType
|
27
|
group by EmployeeID
|
28
|
|
29
|
select distinct
|
30
|
pa17.EmployeeID
|
31
|
,pa17.RemainQuota
|
32
|
into #RemainQuota
|
33
|
from PHRPA0017 pa17
|
34
|
inner join #MaxSeq ms on ms.EmployeeID=pa17.EmployeeID and ms.MaxSeq=pa17.Sequence
|
35
|
where left(pa17.StartDate,4)=@YearBefore and pa17.AbsenceQuotaType=@AbsenceQuotaType
|
36
|
|
37
|
--select @YearBefore
|
38
|
|
39
|
select distinct
|
40
|
@Year+'0101' AS StartDate
|
41
|
,@Year+'1231' AS EndDate
|
42
|
,pa2.EmployeeID
|
43
|
,@AbsenceQuotaType AS AbsenceQuotaType
|
44
|
,1 AS Sequence
|
45
|
,@Year+'0101' AS StartDeduction
|
46
|
,@Year+'0331' AS EndDeduction
|
47
|
,@OriginalQuota AS OriginalQuota
|
48
|
,@RemainQuota AS RemainQuota
|
49
|
,rq.RemainQuota as PreviousQuota
|
50
|
,'' as ExpiredQuota
|
51
|
,'Generate Auto' AS Notes
|
52
|
,@CreateBy AS CreateBy
|
53
|
,@CreateDate AS CreateDate
|
54
|
,@CreateBy AS ChangeBy
|
55
|
,@CreateDate AS ChangeDate
|
56
|
into #Result
|
57
|
from PHRPA0002 pa2
|
58
|
left join #RemainQuota rq on pa2.EmployeeID=rq.EmployeeID
|
59
|
where EmployeeStatus='01' and EndDate='99991231' and EmployeeType='01'
|
60
|
|
61
|
IF @GenerateType = '0'
|
62
|
BEGIN
|
63
|
select 'Simulate' AS Flag,* from #Result
|
64
|
END
|
65
|
|
66
|
IF @GenerateType ='1'
|
67
|
BEGIN
|
68
|
delete from PHRPA0017 where left(StartDate,4) = @Year
|
69
|
insert into PHRPA0017 select * from #Result;
|
70
|
select 'Execute Success' AS Flag,* from #Result
|
71
|
END
|
72
|
|
73
|
drop table #RemainQuota
|
74
|
drop table #Result
|
75
|
drop table #MaxSeq
|