Project

General

Profile

Bug #3680 » CRPTATTANDANCE_202506251429.sql

Tri Rizqiaty, 06/25/2025 02:53 PM

 
1
??ALTER procedure [dbo].[CRPTATTANDANCE]
2

3

4

5
(
6

7
 @PeriodFrom VARCHAR (10),
8

9
 @PeriodTo VARCHAR (10),
10

11
 @AttendanceType VARCHAR (20),
12

13
 @CostCenter VARCHAR (50),
14

15
 @AbsenceType VARCHAR (50),
16

17
 @EmployeeID VARCHAR (20)
18

19
)
20

21
AS
22

23

24

25
--DECLARE @PeriodFrom VARCHAR (10)='20250101'
26

27
--DECLARE @PeriodTo VARCHAR (10)='20250131'
28

29
--DECLARE @AttendanceType VARCHAR (20)=''
30

31
--DECLARE @CostCenter VARCHAR (50)=''
32

33
--DECLARE @AbsenceType VARCHAR (50)=''
34

35
--DECLARE @EmployeeID VARCHAR (50)='05740202'
36

37

38

39
----select * from PHRTMDAYTYP
40

41
----select * from PHRPA0002
42

43

44

45
DECLARE @AttendanceTypeDesc VARChar (100) = (select top(1)AttendanceTypeDesc from PHRTMATTTYP where AttendanceType=@AttendanceType and EndDate='99991231')
46

47
DECLARE @CostCenterDesc VARCHAR (100) = (select top(1)ObjectDescription from PHROM0001 where ObjectID=@CostCenter and EndDate='99991231' and ObjectClass='CC')
48

49

50

51

52

53
DECLARE @Result TABLE 
54

55
(
56

57
    StartDate VARCHAR(20),
58

59
    EndDate VARCHAR(20),
60

61
    EmployeeID VARCHAR(20),
62

63
    FullName VARCHAR(MAX),
64

65
    CostCenter VARCHAR(MAX),
66

67
    CostCenterDescription VARCHAR(MAX),
68

69
    Position VARCHAR(MAX),
70

71
    PositionDescription VARCHAR(MAX),
72

73
    [Date] VARCHAR(MAX),
74

75
    AttendanceType VARCHAR(MAX),
76

77
    AttendanceTypeDesc VARCHAR(MAX),
78

79
    AbsenceType VARCHAR(MAX),
80

81
    AbsenceTypeDesc VARCHAR(MAX),
82

83
    StartTime VARCHAR(MAX),
84

85
    EndTime VARCHAR(MAX),
86

87
    AbsencePoinIn VARCHAR(MAX),
88

89
    AbsencePoinOut VARCHAR(MAX),
90

91
    WorkScheduleType VARCHAR(MAX),
92

93
    DayType VARCHAR(MAX),
94

95
    StartTimeDayType VARCHAR(MAX),
96

97
    DeviationIn VARCHAR(MAX),
98

99
    DeviationTimeIn VARCHAR(MAX),
100

101
    EndTimeDayType VARCHAR(MAX),
102

103
    DeviationOut VARCHAR(MAX),
104

105
    DeviationTimeOut VARCHAR(MAX),
106

107
    PRIMARY KEY (StartDate, EndDate, EmployeeID)
108

109
);
110

111

112

113
-- First Insert
114

115
INSERT INTO @Result
116

117
SELECT
118

119
    pa19.StartDate
120

121
    ,pa19.EndDate
122

123
    ,pa19.EmployeeID 
124

125
    ,pa1.FullName
126

127
    ,pa2.CostCenter
128

129
    ,ccdesc.ObjectDescription as CostCenterDescription
130

131
    ,pa2.Position
132

133
    ,posdesc.ObjectDescription as PositionDescription
134

135
    ,dbo.fn_formatdatetime(pa19.StartDate, 'DD MMMM YYYY') as [Date]
136

137
    ,pa19.AttendanceType
138

139
    ,atttyp.AttendanceTypeDesc
140

141
    ,NULL as AbsenceType
142

143
    ,NULL as AbsenceTypeDesc
144

145
    ,LEFT(pa19.StartTime, 2) + ':' + RIGHT(pa19.StartTime, 2) as StartTime
146

147
    ,LEFT(pa19.EndTime, 2) + ':' + RIGHT(pa19.EndTime, 2) as EndTime
148

149
    ,case when pa19.StartTime is null then '' when pa19.StartTime ='' then '' else '0.5' end as AbsencePoinIn 
150

151
    ,case when pa19.EndTime is null then '' when pa19.EndTime ='' then '' else '0.5' end as AbsencePoinOut 
152

153
    ,pa25.WorkScheduleType
154

155
    ,wscal.DayType
156

157
    ,daytype.StartTime as StartTimeDayType
158

159
    ,case when pa19.StartTime<=daytype.StartTime then 'Tidak Telat' when pa19.StartTime>daytype.StartTime then 'Telat' end as DeviationIn
160

161
    ,CASE 
162

163
        WHEN ISDATE('1900-01-01 ' + SUBSTRING(pa19.StartTime, 1, 2) + ':' + SUBSTRING(pa19.StartTime, 3, 2)) = 1 AND ISDATE('1900-01-01 ' + SUBSTRING(daytype.StartTime, 1, 2) + ':' + SUBSTRING(daytype.StartTime, 3, 2)) = 1 
164

165
            THEN 
166

167
                CASE 
168

169
                    WHEN DATEDIFF(MINUTE, 
170

171
                                  CAST('1900-01-01 ' + SUBSTRING(daytype.StartTime, 1, 2) + ':' + SUBSTRING(daytype.StartTime, 3, 2) AS DATETIME),
172

173
                                  CAST('1900-01-01 ' + SUBSTRING(pa19.StartTime, 1, 2) + ':' + SUBSTRING(pa19.StartTime, 3, 2) AS DATETIME)
174

175
                                 ) <= 0 THEN 0
176

177
                    ELSE DATEDIFF(MINUTE, 
178

179
                                  CAST('1900-01-01 ' + SUBSTRING(daytype.StartTime, 1, 2) + ':' + SUBSTRING(daytype.StartTime, 3, 2) AS DATETIME),
180

181
                                  CAST('1900-01-01 ' + SUBSTRING(pa19.StartTime, 1, 2) + ':' + SUBSTRING(pa19.StartTime, 3, 2) AS DATETIME)
182

183
                                 )
184

185
                END
186

187
        ELSE NULL 
188

189
    END AS DeviationTimeIn
190

191
    ,daytype.EndTime as EndTimeDayType
192

193
    ,case when pa19.EndTime<daytype.EndTime then 'Cepat' when pa19.EndTime>=daytype.EndTime then 'Tidak Cepat' end as DeviationOut
194

195
    ,CASE 
196

197
        WHEN ISDATE('1900-01-01 ' + SUBSTRING(pa19.EndTime, 1, 2) + ':' + SUBSTRING(pa19.EndTime, 3, 2)) = 1 AND ISDATE('1900-01-01 ' + SUBSTRING(daytype.EndTime, 1, 2) + ':' + SUBSTRING(daytype.EndTime, 3, 2)) = 1 
198

199
            THEN 
200

201
                CASE 
202

203
                    WHEN DATEDIFF(MINUTE, 
204

205
                                  CAST('1900-01-01 ' + SUBSTRING(pa19.EndTime, 1, 2) + ':' + SUBSTRING(pa19.EndTime, 3, 2) AS DATETIME), 
206

207
                                  CAST('1900-01-01 ' + SUBSTRING(daytype.EndTime, 1, 2) + ':' + SUBSTRING(daytype.EndTime, 3, 2) AS DATETIME)
208

209
                                 ) <= 0 THEN 0
210

211
                    ELSE DATEDIFF(MINUTE, 
212

213
                                  CAST('1900-01-01 ' + SUBSTRING(pa19.EndTime, 1, 2) + ':' + SUBSTRING(pa19.EndTime, 3, 2) AS DATETIME), 
214

215
                                  CAST('1900-01-01 ' + SUBSTRING(daytype.EndTime, 1, 2) + ':' + SUBSTRING(daytype.EndTime, 3, 2) AS DATETIME)
216

217
                                 )
218

219
                END
220

221
        ELSE NULL 
222

223
    END AS DeviationTimeOut
224

225
from PHRPA0019 pa19
226

227
left join PHRPA0001 pa1 on pa19.EmployeeID=pa1.EmployeeID and pa1.EndDate='99991231'
228

229
left join PHRPA0002 pa2 on pa19.EmployeeID = pa2.EmployeeID and pa2.EndDate='99991231'
230

231
left join PHROM0001 ccdesc on pa2.CostCenter=ccdesc.ObjectID and ccdesc.EndDate='99991231' and ccdesc.ObjectClass='CC'
232

233
left join PHROM0001 posdesc on pa2.Position=posdesc.ObjectID and posdesc.EndDate='99991231' and posdesc.ObjectClass='P'
234

235
left join PHRTMATTTYP atttyp on pa19.AttendanceType = atttyp.AttendanceType and atttyp.EndDate='99991231'
236

237
left join PHRPA0025 pa25 on pa19.EmployeeID=pa25.EmployeeID and (pa19.StartDate between pa25.StartDate and pa25.EndDate)
238

239
left join PHRTMWSCAL wscal on wscal.Country='0001' and pa19.StartDate = wscal.DateSpecified and pa25.WorkScheduleType=wscal.WorkScheduleType
240

241
left join PHRTMDAYTYP daytype on wscal.DayType=daytype.DayType  and daytype.EndDate='99991231'
242

243
where 
244

245
    pa19.StartDate BETWEEN @PeriodFrom and @PeriodTO
246

247
    and (pa2.EmployeeID = @EmployeeID or @EmployeeID='')
248

249
    and (pa2.CostCenter=@CostCenter or @CostCenter='')
250

251

252

253
-- Convert the period parameters to DATE
254

255
DECLARE @PeriodFromDate DATE = CONVERT(DATE, @PeriodFrom, 112);
256

257
DECLARE @PeriodToDate DATE = CONVERT(DATE, @PeriodTo, 112);
258

259

260

261
-- CTE to generate the sequence of dates
262

263
;WITH DateSequence AS (
264

265
    SELECT @PeriodFromDate AS DateValue
266

267
    UNION ALL
268

269
    SELECT DATEADD(DAY, 1, DateValue)
270

271
    FROM DateSequence
272

273
    WHERE DateValue < @PeriodToDate
274

275
)
276

277

278

279
-- Use MERGE to conditionally insert rows and update AbsenceTypeDesc
280

281
MERGE INTO @Result AS target
282

283
USING (
284

285
    SELECT 
286

287
        CONVERT(VARCHAR(10), ds.DateValue, 112) AS StartDate
288

289
        ,CONVERT(VARCHAR(10), ds.DateValue, 112) AS EndDate
290

291
        ,pa18.EmployeeID
292

293
        ,pa1.FullName
294

295
        ,pa2.CostCenter
296

297
        ,ccdesc.ObjectDescription as CostCenterDescription
298

299
        ,pa2.Position
300

301
        ,posdesc.ObjectDescription as PositionDescription
302

303
        ,dbo.fn_formatdatetime(CONVERT(VARCHAR(10), ds.DateValue, 112), 'DD MMMM YYYY') as [Date]
304

305
        ,NULL as AttendanceType
306

307
        ,NULL as AttendanceTypeDesc
308

309
        ,pa18.AbsenceType 
310

311
        ,CASE WHEN daytype.Flag = 'WORK' THEN abs_type.AbsenceTypeDesc ELSE 'Libur' END AbsenceTypeDesc
312

313
        ,NULL as StartTime
314

315
        ,NULL as EndTime
316

317
        ,NULL as AbsencePoinIn
318

319
        ,NULL as AbsencePoinOut
320

321
        ,NULL as WorkScheduleType
322

323
        ,NULL as DayType
324

325
        ,NULL as StartTimeDayType
326

327
        ,NULL as DeviationIn
328

329
        ,NULL as DeviationTimeIn
330

331
        ,NULL as EndTimeDayType
332

333
        ,NULL as DeviationOut
334

335
        ,NULL as DeviationTimeOut
336

337
    FROM 
338

339
        PHRPA0018 pa18
340

341
        INNER JOIN DateSequence ds 
342

343
			ON ds.DateValue BETWEEN CONVERT(DATE, pa18.StartDate, 112) AND CONVERT(DATE, pa18.EndDate, 112)
344

345
        left join PHRPA0001 pa1 on pa18.EmployeeID=pa1.EmployeeID and pa1.EndDate='99991231'
346

347
        left join PHRPA0002 pa2 on pa18.EmployeeID = pa2.EmployeeID and pa2.EndDate='99991231'
348

349
        left join PHROM0001 ccdesc on pa2.CostCenter=ccdesc.ObjectID and ccdesc.EndDate='99991231' and ccdesc.ObjectClass='CC'
350

351
        left join PHROM0001 posdesc on pa2.Position=posdesc.ObjectID and posdesc.EndDate='99991231' and posdesc.ObjectClass='P'
352

353
        left join PHRTMABSTYP abs_type on pa18.AbsenceType = abs_type.AbsenceType and abs_type.EndDate='99991231'
354

355
		LEFT JOIN dbo.PHRPA0025 pa25 ON pa1.EmployeeID = pa25.EmployeeID AND pa25.StartDate <= @PeriodTo AND pa25.EndDate >= @PeriodTo
356

357
		LEFT JOIN dbo.PHRTMWSCAL wscal ON pa25.WorkScheduleType = wscal.WorkScheduleType
358

359
		LEFT JOIN dbo.PHRTMDAYTYP daytype ON wscal.DayType = daytype.DayType 
360

361
    WHERE 
362

363
        CONVERT(DATE, pa18.StartDate, 112) >= @PeriodFromDate
364

365
        AND CONVERT(DATE, pa18.EndDate, 112) <= @PeriodToDate
366

367
        and (pa2.EmployeeID = @EmployeeID or @EmployeeID='')
368

369
        and (pa2.CostCenter=@CostCenter or @CostCenter='')
370

371
		AND wscal.DateSpecified >= pa18.StartDate AND wscal.DateSpecified <= pa18.EndDate
372

373
		AND CONVERT(DATE, wscal.DateSpecified, 112) = ds.DateValue
374

375
		AND daytype.DayType <> 'OFF'
376

377
) AS source
378

379
ON (target.StartDate = source.StartDate AND target.EndDate = source.EndDate AND target.EmployeeID = source.EmployeeID)
380

381
WHEN MATCHED THEN
382

383
     UPDATE SET 
384
        target.AbsenceTypeDesc = source.AbsenceTypeDesc,
385
        target.AbsencePoinIn = source.AbsencePoinIn,
386
        target.AbsencePoinOut = source.AbsencePoinOut
387

388
WHEN NOT MATCHED BY TARGET THEN
389

390
    INSERT (
391

392
        StartDate, EndDate, EmployeeID, FullName, CostCenter, CostCenterDescription, Position, PositionDescription, [Date], AttendanceType, AttendanceTypeDesc, AbsenceType, AbsenceTypeDesc, StartTime, EndTime, AbsencePoinIn, AbsencePoinOut, WorkScheduleType, DayType, StartTimeDayType, DeviationIn, DeviationTimeIn, EndTimeDayType, DeviationOut, DeviationTimeOut
393

394
    )
395

396
    VALUES (
397

398
        source.StartDate, source.EndDate, source.EmployeeID, source.FullName, source.CostCenter, source.CostCenterDescription, source.Position, source.PositionDescription, source.[Date], source.AttendanceType, source.AttendanceTypeDesc, source.AbsenceType, source.AbsenceTypeDesc, source.StartTime, source.EndTime, source.AbsencePoinIn, source.AbsencePoinOut, source.WorkScheduleType, source.DayType, source.StartTimeDayType, source.DeviationIn, source.DeviationTimeIn, source.EndTimeDayType, source.DeviationOut, source.DeviationTimeOut
399

400
    )
401

402
OPTION (MAXRECURSION 0);  -- Allow the recursion to run for a larger range
403

404

405

406

407

408
	
409

410
	--select * from @Result
411

412

413

414
	select 
415

416
	dbo.fn_formatdatetime(@PeriodFrom, 'DD MMMM YYYY') as PeriodFrom
417

418
	,dbo.fn_formatdatetime(@PeriodTo, 'DD MMMM YYYY') as PeriodTo
419

420
	,@AttendanceTypeDesc as AttDescHeader
421

422
	,@CostCenterDesc as CCHeader
423

424
	,StartDate
425

426
	,EndDate
427

428
	,EmployeeID
429

430
	,FullName
431

432
	,CostCenter
433

434
	,CostCenterDescription
435

436
	,Position
437

438
	,PositionDescription
439

440
	,Date
441

442
	,AttendanceType
443

444
	--,AttendanceTypeDesc
445

446
	--,AbsenceType
447

448
	--,AbsenceTypeDesc
449

450
	,case when AbsenceTypeDesc is null then AttendanceTypeDesc else AbsenceTypeDesc end as AttendanceTypeDesc
451

452
	,StartTime
453

454
	,EndTime
455

456
	,AbsencePoinIn
457

458
	,AbsencePoinOut
459

460
	,WorkScheduleType
461

462
	,DayType
463

464
	,StartTimeDayType
465

466
	,case when AttendanceType ='1000' then DeviationIn else NULL end as DeviationIn
467

468
	--,DeviationIn
469

470
	,case when AttendanceType ='1000' then DeviationTimeIn else NULL end as DeviationTimeIn
471

472
	--,DeviationTimeIn
473

474
	,EndTimeDayType
475

476
	-- Dinas, training, WFH, izin datang telat, izin pulang cepat
477

478
	,case when AttendanceType ='1000' then DeviationOut else NULL end as DeviationOut
479

480
	--,DeviationOut
481

482
	,case when AttendanceType ='1000' then DeviationTimeOut else NULL end as DeviationTimeOut
483

484
	--,DeviationTimeOut
485

486
	,case when AttendanceType ='1000' then (case when DeviationTimeIn is null then 0 else DeviationTimeIn end)+(case when DeviationTimeOut is null then 0 else DeviationTimeOut end) else NULL end as TotalDeviation
487

488
	--, as TotalDeviation
489

490
	from
491

492
	@Result
493

494
	where
495

496
	(AttendanceType=@AttendanceType or @AttendanceType='')
497

498
	order by StartDate,CostCenter,EmployeeID
499

500

501

502
	--drop table #Result
503

504

505

506
	--select * from PHRTMATTTYP
(6-6/7)