1
|
ALTER PROCEDURE [dbo].[GENEMPID]
|
2
|
(
|
3
|
@HiringDate VARCHAR(20)
|
4
|
,@PayrollGroup VARCHAR(20)
|
5
|
,@UserName VARCHAR(250)
|
6
|
)
|
7
|
AS
|
8
|
|
9
|
--DECLARE @HiringDate VARCHAR(20) = '20211123'
|
10
|
--DECLARE @PayrollGroup VARCHAR(20) = '0001'
|
11
|
--DECLARE @UserName VARCHAR(20) = 'tri'
|
12
|
|
13
|
DECLARE @EmployeeIDNew VARCHAR(20)
|
14
|
If(@PayrollGroup = '0002')
|
15
|
BEGIN
|
16
|
IF((SELECT DISTINCT MAX(LastID) FROM dbo.CKJLEMPID WHERE PayrollGroup = '0002' AND [Year] = LEFT(@HiringDate,4)) <> '')
|
17
|
BEGIN
|
18
|
SELECT DISTINCT @EmployeeIDNew = CASE WHEN MAX(RIGHT(LastID,4)) <> '' THEN @PayrollGroup + SUBSTRING(@HiringDate,3,4) + RIGHT('0000' + CONVERT(VARCHAR(20),MAX(RIGHT(LastID,4)) + 1),4) ELSE @PayrollGroup + SUBSTRING(@HiringDate,3,4) + '0001' END
|
19
|
FROM dbo.CKJLEMPID WHERE PayrollGroup = '0002' AND Year = LEFT(@HiringDate,4)
|
20
|
|
21
|
UPDATE dbo.CKJLEMPID
|
22
|
SET PayrollGroup = @PayrollGroup
|
23
|
,[Year] = LEFT(@HiringDate,4)
|
24
|
,LastID = @EmployeeIDNew
|
25
|
,ChangeBy = @UserName
|
26
|
,ChangeDate = dbo.fn_formatdatetime_indonesia(GETDATE(),'yyyymmddhhnnss')
|
27
|
WHERE PayrollGroup = '0002' AND [Year] = LEFT(@HiringDate,4)
|
28
|
END
|
29
|
ELSE
|
30
|
BEGIN
|
31
|
SELECT DISTINCT @EmployeeIDNew = @PayrollGroup + SUBSTRING(@HiringDate,3,4) + RIGHT('0000' + CONVERT(VARCHAR(20),COUNT(pa15.EmployeeID) + 1),4)
|
32
|
FROM PHRPA0015 AS pa15
|
33
|
LEFT JOIN dbo.PHRPA0002 AS pa2
|
34
|
ON pa2.EmployeeID = pa15.EmployeeID
|
35
|
WHERE LEFT(pa15.HiringDate,4) = LEFT(@HiringDate,4)
|
36
|
AND pa2.PayrollGroup = '0002'
|
37
|
|
38
|
INSERT INTO dbo.CKJLEMPID
|
39
|
SELECT PayrollGroup = @PayrollGroup
|
40
|
,Year = LEFT(@HiringDate,4)
|
41
|
,LastID = @EmployeeIDNew
|
42
|
,CreateBy = @UserName
|
43
|
,CreateDate = dbo.fn_formatdatetime_indonesia(GETDATE(),'yyyymmddhhnnss')
|
44
|
,ChangeBy = @UserName
|
45
|
,ChangeDate = dbo.fn_formatdatetime_indonesia(GETDATE(),'yyyymmddhhnnss')
|
46
|
END
|
47
|
END
|
48
|
ELSE
|
49
|
BEGIN
|
50
|
IF((SELECT DISTINCT MAX(LastID) FROM dbo.CKJLEMPID WHERE PayrollGroup <> '0002' AND [Year] = LEFT(@HiringDate,4)) <> '')
|
51
|
BEGIN
|
52
|
SELECT DISTINCT @EmployeeIDNew = CASE WHEN MAX(RIGHT(LastID,4)) <> '' THEN SUBSTRING(@HiringDate,3,4) + RIGHT('0000' + CONVERT(VARCHAR(20),MAX(RIGHT(LastID,4)) + 1),4) ELSE SUBSTRING(@HiringDate,3,4) + '0001' END
|
53
|
FROM dbo.CKJLEMPID WHERE PayrollGroup <> '0002' AND Year = LEFT(@HiringDate,4)
|
54
|
|
55
|
UPDATE dbo.CKJLEMPID
|
56
|
SET PayrollGroup = @PayrollGroup
|
57
|
,[Year] = LEFT(@HiringDate,4)
|
58
|
,LastID = @EmployeeIDNew
|
59
|
,ChangeBy = @UserName
|
60
|
,ChangeDate = dbo.fn_formatdatetime_indonesia(GETDATE(),'yyyymmddhhnnss')
|
61
|
WHERE PayrollGroup <> '0002' AND [Year] = LEFT(@HiringDate,4)
|
62
|
END
|
63
|
ELSE
|
64
|
BEGIN
|
65
|
SELECT DISTINCT @EmployeeIDNew = SUBSTRING(@HiringDate,3,4) + RIGHT('0000' + CONVERT(VARCHAR(20),COUNT(pa15.EmployeeID) + 1),4)
|
66
|
FROM PHRPA0015 AS pa15
|
67
|
LEFT JOIN dbo.PHRPA0002 AS pa2
|
68
|
ON pa2.EmployeeID = pa15.EmployeeID
|
69
|
WHERE LEFT(pa15.HiringDate,4) = LEFT(@HiringDate,4)
|
70
|
AND pa2.PayrollGroup <> '0002'
|
71
|
|
72
|
INSERT INTO dbo.CKJLEMPID
|
73
|
SELECT PayrollGroup = @PayrollGroup
|
74
|
,Year = LEFT(@HiringDate,4)
|
75
|
,LastID = @EmployeeIDNew
|
76
|
,CreateBy = @UserName
|
77
|
,CreateDate = dbo.fn_formatdatetime_indonesia(GETDATE(),'yyyymmddhhnnss')
|
78
|
,ChangeBy = @UserName
|
79
|
,ChangeDate = dbo.fn_formatdatetime_indonesia(GETDATE(),'yyyymmddhhnnss')
|
80
|
END
|
81
|
END
|
82
|
|
83
|
|
84
|
SELECT @EmployeeIDNew AS EmpID
|