Project

General

Profile

Bug #2664 » Rpt_IMFI_HR_PY_WageTypeRecapV2_20240322.sql

Tri Rizqiaty, 03/22/2024 02:01 PM

 
1
??ALTER PROCEDURE [dbo].[Rpt_IMFI_HR_PY_WageTypeRecapV2]
2

3
	 @landscape varchar(3),
4

5
	 @payrollGroupStart varchar(2),
6

7
	 @payrollGroupEnd varchar(2),
8

9
	 @CostCenterStart varchar(8),
10

11
	 @CostCenterEnd varchar(8),
12

13
	 @employee_idStart varchar(8),
14

15
	 @employee_idEnd varchar(8),
16

17
	 @payPeriod varchar(6)
18

19

20

21
AS
22

23

24

25

26

27
 --declare @landscape varchar(3) = N'100'
28

29
 --declare @payPeriod varchar(6) = '202403'
30

31
 --declare @employee_idStart varchar(8) = '20000003'
32

33
 --declare @employee_idEnd varchar(8)='20000003'
34

35
 --declare @payrollGroupStart varchar(2) =''
36

37
 --declare @payrollGroupEnd varchar(2)=''
38

39
 --declare @CostCenterStart varchar(8)
40

41
 --declare @CostCenterEnd varchar(8)
42

43
 --DECLARE @emp_type VARCHAR(12) = ''
44

45

46

47

48

49
--//-- Variable Declaration
50

51
DECLARE @payPeriodMonth varchar(2)
52

53
DECLARE @payPeriodYear varchar(4)
54

55
DECLARE @payPeriodStartDate varchar(8)
56

57
DECLARE @payPeriodEndDate varchar(8)
58

59
DECLARE @payPeriodEndDateDt datetime
60

61

62

63
DECLARE @employee_idMin varchar(8)
64

65
DECLARE @employee_idMax varchar(8)
66

67
DECLARE @PayrollGroupMin varchar(2)
68

69
DECLARE @PayrollGroupMax varchar(2)
70

71
DECLARE @CostCenterMin varchar(8)
72

73
DECLARE @CostCenterMax varchar(8)
74

75

76

77
--//-- Table Declaration
78

79
DECLARE @tbl_result TABLE
80

81
	(employee_id varchar(8),
82

83
	 employee_name varchar(50),
84

85
	 CostCenterGroup varchar(50),
86

87
	 CostCenterGroup_Abr varchar(50),
88

89
	 CostCenterGroup_Desc varchar(50),
90

91
	 CostCenter varchar(50),
92

93
	 CostCenter_Abr varchar(50),
94

95
	 CostCenter_Desc varchar(50),
96

97
	 WT0101 int,WT0102 int,WT0103 int,WT0104 int,WT0105 int,WT0106 int,WT0107 int,WT0108 int,WT0109 int,WT0110 int,WT0111 int,WT0112 int,WT0113 int,WT0114 int,WT0115 int,WT0116 int,WT0117 int,WT0118 int,WT0119 int,WT0120 int,WT0121 int,WT0122 int,WT0123 int,WT0124 int,WT0125 int,WT0126 int,WT0127 int,WT0128 int,WT0129 int,WT0130 int,WT0131 int,WT0132 int,WT0133 int,WT0134 int,WT0135 int,WT0136 int,WT0137 int,WT0138 int,WT0139 int,WT0140 int,WT0141 int,WT0142 int,WT0143 int,WT0144 int,WT0145 int,WT0146 int,WT0147 int,WT0148 int,WT0149 int,WT0150 int,
98

99
	 WT0201 int,WT0202 int,WT0203 int,WT0204 int,WT0205 int,WT0206 int,WT0207 int,WT0208 int,WT0209 int,WT0210 int,WT0211 int,WT0212 int,WT0213 int,WT0214 int,WT0215 int,WT0216 int,WT0217 int,WT0218 int,WT0219 int,WT0220 int,WT0221 int,WT0222 int,WT0223 int,WT0224 int,WT0225 int,WT0226 int,WT0227 int,WT0228 int,WT0229 int,WT0230 int,WT0231 int,WT0232 int,WT0233 int,WT0234 int,WT0235 int,WT0236 int,WT0237 int,WT0238 int,WT0239 int,WT0240 int,WT0241 int,WT0242 int,WT0243 int,WT0244 int,WT0245 int,WT0246 int,WT0247 int,WT0248 int,WT0249 int,WT0250 int,
100

101
	 WT0301 int,WT0302 int,WT0303 int,WT0304 int,WT0305 int,WT0306 int,WT0307 int,WT0308 int,WT0309 int,WT0310 int,WT0311 int,WT0312 int,WT0313 int,WT0314 int,WT0315 int,WT0316 int,WT0317 int,WT0318 int,WT0319 int,WT0320 int,WT0321 int,WT0322 int,WT0323 int,WT0324 int,WT0325 int,WT0326 int,WT0327 int,WT0328 int,WT0329 int,WT0330 int,WT0331 int,WT0332 int,WT0333 int,WT0334 int,WT0335 int,WT0336 int,WT0337 int,WT0338 int,WT0339 int,WT0340 int,WT0341 int,WT0342 int,WT0343 int,WT0344 int,WT0345 int,WT0346 int,WT0347 int,WT0348 int,WT0349 int,WT0350 int,
102

103
	 WT0401 int,WT0402 int,WT0403 int,WT0404 int,WT0405 int,WT0406 int,WT0407 int,WT0408 int,WT0409 int,WT0410 int,WT0411 int,WT0412 int,WT0413 int,WT0414 int,WT0415 int,WT0416 int,WT0417 int,WT0418 int,WT0419 int,WT0420 int,WT0421 int,WT0422 int,WT0423 int,WT0424 int,WT0425 int,WT0426 int,WT0427 int,WT0428 int,WT0429 int,WT0430 int,WT0431 int,WT0432 int,WT0433 int,WT0434 int,WT0435 int,WT0436 int,WT0437 int,WT0438 int,WT0439 int,WT0440 int,WT0441 int,WT0442 int,WT0443 int,WT0444 int,WT0445 int,WT0446 int,WT0447 int,WT0448 int,WT0449 int,WT0450 int)
104

105

106

107
DECLARE @tbl_employee TABLE
108

109
	(employee_id varchar(8),
110

111
	 employee_name varchar(250),
112

113
	 CCG varchar(50),
114

115
	 CCG_Abr varchar(50),
116

117
	 CCG_Desc varchar(250),
118

119
	 CC varchar(50),
120

121
	 CC_Abr varchar(50),
122

123
	 CC_Desc varchar(250))
124

125

126

127

128

129
DECLARE @tbl_employee2 TABLE
130

131
	(
132

133
		employee_id	varchar(250),
134

135
		employee_name	varchar(250),
136

137
		employee_area_id	varchar(250),
138

139
		employee_area_descr	varchar(250),
140

141
		employee_office_id	varchar(250),
142

143
		employee_office_descr	varchar(250),
144

145
		employee_status_id	varchar(250),
146

147
		employee_status_descr	varchar(250),
148

149
		employee_type_id	varchar(250),
150

151
		employee_type_descr	varchar(250),
152

153
		employee_sub_type_id	varchar(250),
154

155
		employee_sub_type_descr	varchar(250),
156

157
		payroll_group_id	varchar(250),
158

159
		payroll_group_descr	varchar(250),
160

161
		organization_id	varchar(250),
162

163
		organization_descr	varchar(250),
164

165
		cost_center_id	varchar(250),
166

167
		cost_center_abbr	varchar(250),
168

169
		cost_center_descr	varchar(250),
170

171
		cost_center_group_id	varchar(250),
172

173
		cost_center_group_abbr	varchar(250),
174

175
		cost_center_group_descr	varchar(250),
176

177
		position_id	varchar(250),
178

179
		position_descr	varchar(250),
180

181
		job_id	varchar(250),
182

183
		job_descr	varchar(250),
184

185
		pay_type_id	varchar(250),
186

187
		pay_type_descr	varchar(250),
188

189
		pay_class_id	varchar(250),
190

191
		pay_class_descr	varchar(250),
192

193
		pay_grade_id	varchar(250),
194

195
		pay_grade_descr	varchar(250),
196

197
		pay_area_id	varchar(250),
198

199
		pay_area_descr	varchar(250),
200

201
		resign_date VARCHAR(8),
202

203
		hiring_date VARCHAR(8)
204

205
	 )
206

207

208

209
DECLARE @tbl_tr_enc TABLE
210

211
	(employee_id varchar(8),
212

213
	 payPeriodMonth varchar(2),
214

215
	 payPeriodYear varchar(4),
216

217
	 runPeriodMonth varchar(2),
218

219
	 runPeriodYear varchar(4),
220

221
	 wageType varchar(4),
222

223
	 amount varchar(250))
224

225

226

227
--DECLARE @tbl_tr_dec TABLE
228

229
--	(employee_id varchar(8),
230

231
--	 payPeriodMonth varchar(2),
232

233
--	 payPeriodYear varchar(4),
234

235
--	 runPeriodMonth varchar(2),
236

237
--	 runPeriodYear varchar(4),
238

239
--	 wageType varchar(4),
240

241
--	 amount int,
242

243
--	 column_name varchar(6))
244

245

246

247
DECLARE @hrtr0300 TABLE(
248

249
	[landscape] [VARCHAR](3) NOT NULL,
250

251
	[employee_id] [VARCHAR](8) NOT NULL,
252

253
	[pay_period_month] [VARCHAR](2) NOT NULL,
254

255
	[pay_period_year] [VARCHAR](4) NOT NULL,
256

257
	[run_period_month] [VARCHAR](2) NOT NULL,
258

259
	[run_period_year] [VARCHAR](4) NOT NULL,
260

261
	[split_indicator] [VARCHAR](1) NOT NULL,
262

263
	[start_date] [VARCHAR](8) NOT NULL,
264

265
	[end_date] [VARCHAR](8) NOT NULL,
266

267
	[data_status] [VARCHAR](1) NULL,
268

269
	[company_id] [VARCHAR](4) NULL,
270

271
	[cost_center] [VARCHAR](8) NULL,
272

273
	[employee_area] [VARCHAR](4) NOT NULL,
274

275
	[employee_office] [VARCHAR](4) NOT NULL,
276

277
	[employee_status] [VARCHAR](2) NOT NULL,
278

279
	[employee_type] [VARCHAR](2) NOT NULL,
280

281
	[employee_subtype] [VARCHAR](2) NOT NULL,
282

283
	[payroll_group] [VARCHAR](2) NOT NULL,
284

285
	[organization] [VARCHAR](8) NOT NULL,
286

287
	[position] [VARCHAR](8) NOT NULL,
288

289
	[job] [VARCHAR](8) NOT NULL,
290

291
	[pay_type] [VARCHAR](2) NULL,
292

293
	[pay_class] [VARCHAR](2) NULL,
294

295
	[pay_grade] [VARCHAR](2) NULL,
296

297
	[pay_area] [VARCHAR](2) NULL,
298

299
	[pay_curr] [VARCHAR](5) NULL,
300

301
	[pay_curr_rate] [DECIMAL](18, 7) NULL,
302

303
	[change_by] [VARCHAR](12) NOT NULL,
304

305
	[change_date] [VARCHAR](14) NOT NULL,
306

307
	[created_by] [VARCHAR](12) NOT NULL,
308

309
	[created_date] [VARCHAR](14) NOT NULL )
310

311

312

313
--//-- Setting variable
314

315
SET @payPeriodMonth = SUBSTRING(@payPeriod, 5, 2)
316

317
SET @payPeriodYear = SUBSTRING(@payPeriod, 1, 4)
318

319
SET @payPeriodStartDate = @payPeriod + '01'
320

321
SET @payPeriodEndDateDt = DATEADD(dd, -1, DATEADD(mm, 1, CONVERT(datetime, @payPeriodStartDate)))
322

323
SET @payPeriodEndDate = @payPeriod + CONVERT(varchar(2), DATEPART(dd, @payPeriodEndDateDt))
324

325

326

327
INSERT INTO @hrtr0300
328

329
SELECT DISTINCT * FROM hr_tr0300 WITH (NOLOCK) WHERE landscape = @landscape AND pay_period_year = run_period_year AND pay_period_month = run_period_month AND run_period_year = @payPeriodYear AND run_period_month = @payPeriodMonth
330

331

332

333
--//-- Set Filter
334

335
SELECT @employee_idMin = MIN(employee_id) FROM @hrtr0300 WHERE landscape = @landscape AND pay_period_year = run_period_year AND pay_period_month = run_period_month AND run_period_year = @payPeriodYear AND run_period_month = @payPeriodMonth
336

337
SELECT @employee_idMax = MAX(employee_id) FROM @hrtr0300  WHERE landscape = @landscape AND pay_period_year = run_period_year AND pay_period_month = run_period_month AND run_period_year = @payPeriodYear AND run_period_month = @payPeriodMonth
338

339
SELECT @CostCenterMin = MIN(cost_center) FROM @hrtr0300  WHERE landscape = @landscape AND pay_period_year = run_period_year AND pay_period_month = run_period_month AND run_period_year = @payPeriodYear AND run_period_month = @payPeriodMonth
340

341
SELECT @CostCenterMax = MAX(cost_center) FROM @hrtr0300 WHERE landscape = @landscape AND pay_period_year = run_period_year AND pay_period_month = run_period_month AND run_period_year = @payPeriodYear AND run_period_month = @payPeriodMonth
342

343
SELECT @PayrollGroupMin = MIN(payroll_group) FROM @hrtr0300  WHERE landscape = @landscape AND pay_period_year = run_period_year AND pay_period_month = run_period_month AND run_period_year = @payPeriodYear AND run_period_month = @payPeriodMonth
344

345
SELECT @PayrollGroupMax = MAX(payroll_group) FROM @hrtr0300 WHERE landscape = @landscape AND pay_period_year = run_period_year AND pay_period_month = run_period_month AND run_period_year = @payPeriodYear AND run_period_month = @payPeriodMonth
346

347

348

349
IF (LTRIM(RTRIM(@employee_idStart)) <> '')
350

351
BEGIN
352

353
	SET @employee_idMin = @employee_idStart
354

355
	IF (LTRIM(RTRIM(@employee_idEnd)) <> '')
356

357
		SET @employee_idMax = @employee_idEnd
358

359
	ELSE
360

361
		SET @employee_idMax = @employee_idStart
362

363
END
364

365

366

367
IF (LTRIM(RTRIM(@payrollGroupStart)) <> '')
368

369
BEGIN
370

371
	SET @PayrollGroupMin = @payrollGroupStart
372

373
	IF (LTRIM(RTRIM(@payrollGroupEnd)) <> '')
374

375
		SET @PayrollGroupMax = @payrollGroupEnd
376

377
	ELSE
378

379
		SET @PayrollGroupMax = @payrollGroupStart
380

381
END
382

383

384

385
IF (LTRIM(RTRIM(@CostCenterStart)) <> '')
386

387
BEGIN
388

389
	SET @CostCenterMin = @CostCenterStart
390

391
	IF (LTRIM(RTRIM(@CostCenterEnd)) <> '')
392

393
		SET @CostCenterMax = @CostCenterEnd
394

395
	ELSE
396

397
		SET @CostCenterMax = @CostCenterStart
398

399
END
400

401

402

403
----//-- Select Employee
404

405
INSERT INTO @tbl_employee
406

407
SELECT DISTINCT md01.emp_id, 
408

409
md01.full_name,
410

411
'' CG, --cg.object, 
412

413
'' CG_Abbr, --cg.abbreviation, 
414

415
'' CG_Desc, --cg.description, 
416

417
cc.abbreviation, 
418

419
cc.abbreviation, 
420

421
cc.description
422

423
--,md2.start_date, md2.end_date, md2.movement_type
424

425
FROM hr_md_emp_md0001 md01 WITH (NOLOCK) 
426

427
	LEFT JOIN @hrtr0300 AS hr_tr0300
428

429
		ON md01.landscape = hr_tr0300.landscape
430

431
	LEFT JOIN hr_md_orm_object cc  WITH (NOLOCK)
432

433
		ON cc.landscape = @landscape
434

435
		AND cc.class = 'CC'
436

437
		AND cc.start_date <= @payPeriodEndDate
438

439
		AND cc.end_date >= @payPeriodEndDate
440

441
	--LEFT JOIN hr_md_orm_relationship ccg  WITH (NOLOCK)
442

443
	--	ON cc.object = ccg.object
444

445
	--	AND ccg.landscape = @landscape
446

447
	--	AND ccg.class = 'CC'
448

449
	--	AND ccg.start_date <= @payPeriodEndDate
450

451
	--	AND ccg.end_date >= @payPeriodEndDate			
452

453
	--	AND ccg.rel_class = 'CG'
454

455
	--	AND ccg.rel_dir = 'B'
456

457
	--	AND ccg.rel_type = '001'
458

459
	--LEFT JOIN hr_md_orm_object cg  WITH (NOLOCK)
460

461
	--	ON ccg.rel_object = cg.object
462

463
	--	AND cg.landscape = @landscape
464

465
	--	AND cg.class = 'CG'
466

467
	--	AND cg.start_date <= @payPeriodEndDate
468

469
	--	AND cg.end_date >= @payPeriodEndDate
470

471
WHERE (hr_tr0300.cost_center >= @CostCenterMin AND hr_tr0300.cost_center <= @CostCenterMax) 
472

473
	AND ( hr_tr0300.payroll_group >= @PayrollGroupMin AND hr_tr0300.payroll_group <= @PayrollGroupMax)
474

475
	AND md01.start_date <= @payPeriodEndDate
476

477
	AND md01.end_date >= @payPeriodEndDate 
478

479
	AND md01.emp_id >= @employee_idMin
480

481
	AND md01.emp_id <= @employee_idMax
482

483
	AND md01.emp_id = hr_tr0300.employee_id
484

485
	AND hr_tr0300.cost_center = cc.object
486

487
	--AND ( dbo.hr_tr0300.employee_type = @emp_type OR @emp_type = '')
488

489

490

491
DECLARE @emphiringresign TABLE
492

493
(
494

495
	landscape VARCHAR(5)
496

497
	,emp_id VARCHAR(10)
498

499
	,sdate VARCHAR(20)
500

501
	,edate VARCHAR(20)
502

503
	,movtype VARCHAR(20)
504

505
)
506

507
INSERT INTO @emphiringresign  
508

509
SELECT DISTINCT md2a.landscape, md2a.emp_id, md2a.start_date, md2a.end_date, md2a.movement_type
510

511
FROM dbo.hr_md_emp_md0002 AS md2a
512

513
WHERE md2a.landscape = @landscape AND md2a.start_date <= @payPeriodEndDate AND md2a.end_date >= @payPeriodEndDate
514

515
AND md2a.emp_id >= @employee_idMin AND md2a.emp_id <= @employee_idMax AND md2a.movement_type = '80'
516

517

518

519
DECLARE @employee_id varchar(8)
520

521
DECLARE @employee_name varchar(50)
522

523
DECLARE @CCG varchar(50)
524

525
DECLARE @CCG_Abr varchar(50)
526

527
DECLARE @CCG_Desc varchar(50)
528

529
DECLARE @CC varchar(50)
530

531
DECLARE @CC_Abr varchar(50)
532

533
DECLARE @CC_Desc varchar(50)
534

535
	 
536

537
  
538

539
DECLARE @tr0300 TABLE(
540

541
[landscape] [varchar](3) NOT NULL,
542

543
[employee_id] [varchar](8) NOT NULL,
544

545
[pay_period_month] [varchar](2) NOT NULL,
546

547
[pay_period_year] [varchar](4) NOT NULL,
548

549
[run_period_month] [varchar](2) NOT NULL,
550

551
[run_period_year] [varchar](4) NOT NULL,
552

553
[split_indicator] [varchar](1) NOT NULL,
554

555
[start_date] [varchar](8) NOT NULL,
556

557
[end_date] [varchar](8) NOT NULL,
558

559
[data_status] [varchar](1) NULL,
560

561
[company_id] [varchar](4) NULL,
562

563
[cost_center] [varchar](8) NULL,
564

565
[employee_area] [varchar](4) NOT NULL,
566

567
[employee_office] [varchar](4) NOT NULL,
568

569
[employee_status] [varchar](2) NOT NULL,
570

571
[employee_type] [varchar](2) NOT NULL,
572

573
[employee_subtype] [varchar](2) NOT NULL,
574

575
[payroll_group] [varchar](2) NOT NULL,
576

577
[organization] [varchar](8) NOT NULL,
578

579
[position] [varchar](8) NOT NULL,
580

581
[job] [varchar](8) NOT NULL,
582

583
[pay_type] [varchar](2) NULL,
584

585
[pay_class] [varchar](2) NULL,
586

587
[pay_grade] [varchar](2) NULL,
588

589
[pay_area] [varchar](2) NULL,
590

591
pay_curr  VARCHAR(5),
592

593
pay_curr_rate DECIMAL(18,7),
594

595
[change_by] [varchar](12) NOT NULL,
596

597
[change_date] [varchar](14) NOT NULL,
598

599
[created_by] [varchar](12) NOT NULL,
600

601
[created_date] [varchar](14) NOT NULL
602

603
)
604

605

606

607
  
608

609
  
610

611
declare @emp_id varchar (8) 
612

613
declare @pay_month varchar (2) 
614

615
set @pay_month = substring(@payPeriod,5,2)
616

617

618

619
declare @pay_year varchar (4) 
620

621
set @pay_year = substring(@payPeriod,1,4)
622

623

624

625

626

627
declare @start_date varchar (10)
628

629
declare @end_date varchar (10)
630

631
  
632

633
  
634

635
  
636

637
declare @pay_period_month varchar (4) 
638

639
declare @pay_period_year varchar(4)
640

641
declare @employe_id varchar (8)
642

643
declare @full_name varchar (250)
644

645
DECLARE @company_id varchar(4) 
646

647
DECLARE @cost_center   varchar (8) 
648

649
DECLARE @employee_area   varchar (250)  
650

651
DECLARE @employee_office   varchar (250) 
652

653
DECLARE @employee_status   varchar (250) 
654

655
DECLARE @employee_type   varchar (250)  
656

657
DECLARE @employee_subtype   varchar (250) 
658

659
DECLARE @payroll_group   varchar (250) 
660

661
DECLARE @organization   varchar (250) 
662

663
DECLARE @position   varchar (250) 
664

665
DECLARE @job   varchar (250) 
666

667
DECLARE @pay_type   varchar (250) 
668

669
DECLARE @pay_class   varchar (250) 
670

671
DECLARE @pay_grade   varchar (250)
672

673
DECLARE @pay_area   varchar (250) 
674

675
  
676

677
  --Get System Parameter
678

679
declare @is_encrypted varchar
680

681

682

683
SELECT    @is_encrypted = val1
684

685
FROM         dbo.base_cust_parameter
686

687
where	landscape = @landscape and
688

689
		param = 'HR_PY_ENCRYPTED' and
690

691
		start_date <= @payPeriodEndDate and 
692

693
		end_date >= @payPeriodEndDate
694

695
  
696

697
  
698

699
  
700

701
  
702

703
  
704

705
  
706

707
DECLARE cur_employee CURSOR FOR SELECT DISTINCT * from @tbl_employee
708

709
OPEN cur_employee
710

711
FETCH cur_employee INTO  @employee_id, 
712

713
				   @employee_name, 
714

715
				   @CCG, 
716

717
				   @CCG_Abr, 
718

719
				   @CCG_Desc, 
720

721
				   @CC, 
722

723
				   @CC_Abr, 
724

725
				   @CC_Desc 
726

727

728

729
 -- start the main processing loop.
730

731

732

733
WHILE @@Fetch_Status = 0
734

735

736

737
   BEGIN
738

739
  
740

741
  delete from @tr0300
742

743

744

745
set @emp_id = @employee_id
746

747

748

749

750

751

752

753
set @start_date = ''
754

755
set @end_date = ''
756

757

758

759
set @pay_period_month = ''
760

761
set @pay_period_year = ''
762

763
set @employe_id = ''
764

765
set @full_name = ''
766

767
set @company_id = ''
768

769
set @cost_center   = ''
770

771
set @employee_area   = ''
772

773
set @employee_office   = ''
774

775
set @employee_status   = ''
776

777
set @employee_type   = ''
778

779
set @employee_subtype   = ''
780

781
set @payroll_group   = ''
782

783
set @organization   = ''
784

785
set @position   = ''
786

787
set @job   = ''
788

789
set @pay_type   = ''
790

791
set @pay_class   = ''
792

793
set @pay_grade   = ''
794

795
set @pay_area   = ''
796

797

798

799

800

801
insert  into @tr0300 
802

803
select top 1 *
804

805
from @hrtr0300
806

807
where	employee_id = @emp_id
808

809
	AND pay_period_month = @pay_month
810

811
	AND pay_period_year = @pay_year order by run_period_month desc
812

813

814

815
select @start_date = tr0300.start_date , @end_date = tr0300.end_date
816

817
from @tr0300 as tr0300
818

819

820

821
select @pay_period_month = pay_period_month, @pay_period_year = pay_period_year
822

823
from @tr0300 
824

825

826

827
select @employe_id = md1.emp_id, @full_name = md1.full_name
828

829
from hr_md_emp_md0001 as md1
830

831
inner join @tr0300 as tr0300
832

833
	on md1.emp_id = tr0300.employee_id
834

835
	and md1.start_date <= @end_date
836

837
	and md1.end_date >= @end_date
838

839

840

841
select @cost_center = object.description
842

843
from @tr0300 as tr0300
844

845
left join hr_md_orm_object as object
846

847
	on object.object = tr0300.cost_center
848

849
	and object.start_date <= @start_date
850

851
	and object.end_date >= @end_date
852

853
	and object.class = 'CC'
854

855

856

857
select @employee_area = object.description
858

859
from @tr0300 as tr0300
860

861
left join base_cust_ref_emp_area  as object
862

863
	on object.emp_area = tr0300.employee_area
864

865
	
866

867
select @employee_office = object.emp_subarea_description
868

869
from @tr0300 as tr0300
870

871
left join base_cust_ref_emp_office  as object
872

873
	on object.emp_subarea = tr0300.employee_office
874

875

876

877
select @employee_status = object.description
878

879
from @tr0300 as tr0300
880

881
left join base_cust_ref_emp_status  as object
882

883
	on object.emp_status = tr0300.employee_status
884

885

886

887
select @employee_type = object.description
888

889
from @tr0300 as tr0300
890

891
left join base_cust_ref_emp_type  as object
892

893
	on object.emp_type = tr0300.employee_type
894

895

896

897
select @employee_subtype = object.description
898

899
from @tr0300 as tr0300
900

901
left join base_cust_ref_emp_subtype  as object
902

903
	on object.emp_subtype = tr0300.employee_subtype
904

905

906

907
select @payroll_group = object.description
908

909
from @tr0300 as tr0300
910

911
left join base_cust_ref_payroll_group  as object
912

913
	on object.payroll_group = tr0300.payroll_group
914

915

916

917
select @organization = object.description
918

919
from @tr0300 as tr0300
920

921
left join hr_md_orm_object as object
922

923
	on object.object = tr0300.organization
924

925
	and object.start_date <= @end_date
926

927
	and object.end_date >= @end_date
928

929
	and object.class = 'O'
930

931

932

933
select @position = object.description
934

935
from @tr0300 as tr0300
936

937
left join hr_md_orm_object as object
938

939
	on object.object = tr0300.position
940

941
	and object.start_date <= @end_date
942

943
	and object.end_date >= @end_date
944

945
	and object.class = 'P'
946

947

948

949
select @job = object.description
950

951
from @tr0300 as tr0300
952

953
left join hr_md_orm_object as object
954

955
	on object.object = tr0300.job
956

957
	and object.start_date <= @end_date
958

959
	and object.end_date >= @end_date
960

961
	and object.class = 'J'
962

963

964

965
select @pay_type = object.description
966

967
from @tr0300 as tr0300
968

969
left join base_cust_ref_pay_type  as object
970

971
	on object.pay_type = tr0300.pay_type
972

973

974

975
select @pay_class = object.description
976

977
from @tr0300 as tr0300
978

979
left join base_cust_ref_pay_class  as object
980

981
	on object.pay_class = tr0300.pay_class
982

983

984

985
select @pay_grade = object.description
986

987
from @tr0300 as tr0300
988

989
left join base_cust_ref_pay_grade  as object
990

991
	on object.pay_grade = tr0300.pay_grade
992

993
	
994

995
select @pay_area = object.description
996

997
from @tr0300 as tr0300
998

999
left join base_cust_ref_pay_area  as object
1000

1001
	on object.pay_area = tr0300.pay_area
1002

1003

1004

1005

1006

1007

1008

1009

1010

1011
INSERT INTO @tbl_employee2
1012

1013
SELECT DISTINCT
1014

1015
tr0300.employee_id	,
1016

1017
employee_name	,
1018

1019
tr0300.employee_area as employee_area_id	,
1020

1021
a.employee_area_descr	,
1022

1023
tr0300.employee_office as employee_office_id	,
1024

1025
a.employee_office_descr	,
1026

1027
tr0300.employee_status as employee_status_id	,
1028

1029
a.employee_status_descr	,
1030

1031
tr0300.employee_type as employee_type_id	,
1032

1033
a.employee_type_descr	,
1034

1035
tr0300.employee_subtype as employee_subtype_id	,
1036

1037
a.employee_subtype_descr	,
1038

1039
tr0300.payroll_group as payroll_group_id	,
1040

1041
a.payroll_group_descr	,
1042

1043
tr0300.organization as organization_id	,
1044

1045
a.organization_descr	,   
1046

1047
@CC, 
1048

1049
@CC_Abr, 
1050

1051
@CC_Desc ,
1052

1053
@CCG, 
1054

1055
@CCG_Abr, 
1056

1057
@CCG_Desc, 
1058

1059
tr0300.position as position_id	,
1060

1061
a.position_descr	,
1062

1063
tr0300.job as job_id	,
1064

1065
a.job_descr	,
1066

1067
tr0300.pay_type	as pay_type_id,
1068

1069
a.pay_type_descr	,
1070

1071
tr0300.pay_class as pay_class_id	,
1072

1073
a.pay_class_descr	,
1074

1075
tr0300.pay_grade as pay_grade_id	,
1076

1077
a.pay_grade_descr	,
1078

1079
tr0300.pay_area as pay_area_id	,
1080

1081
a.pay_area_descr
1082

1083
, c.sdate AS resign_date, d.sdate AS hiring_date	
1084

1085
 from(
1086

1087
select
1088

1089
	  @employe_id as employee_id
1090

1091
	 ,@pay_period_month as pay_period_month
1092

1093
	 ,@pay_period_year as pay_period_year
1094

1095
	 ,@full_name as employee_name
1096

1097
	 ,@cost_center as cost_center_descr
1098

1099
	 ,@employee_area as employee_area_descr
1100

1101
	 ,@employee_office as employee_office_descr
1102

1103
	 ,@employee_status as employee_status_descr
1104

1105
	 ,@employee_type as  employee_type_descr
1106

1107
	 ,@employee_subtype as employee_subtype_descr
1108

1109
	 ,@payroll_group as payroll_group_descr
1110

1111
	 ,@organization as organization_descr
1112

1113
	 ,@position as position_descr
1114

1115
	 ,@job as job_descr
1116

1117
	 ,@pay_type as pay_type_descr
1118

1119
	 ,@pay_class as pay_class_descr
1120

1121
	 ,@pay_grade as pay_grade_descr 
1122

1123
	 ,@pay_area as pay_area_descr
1124

1125
	 ) as a
1126

1127
	 INNER JOIN @tr0300 as tr0300 on a.employee_id = tr0300.employee_id
1128

1129
	 LEFT JOIN @emphiringresign AS c
1130

1131
	 ON a.employee_id = c.emp_id AND c.sdate <= @payPeriodEndDate AND c.edate >= @payPeriodEndDate AND c.movtype = '80'
1132

1133
	 LEFT JOIN @emphiringresign AS d
1134

1135
	 ON a.employee_id = d.emp_id AND c.movtype = '10'
1136

1137
	 
1138

1139
		FETCH cur_employee INTO 
1140

1141
						   @employee_id, 
1142

1143
						   @employee_name, 
1144

1145
						   @CCG, 
1146

1147
						   @CCG_Abr, 
1148

1149
						   @CCG_Desc, 
1150

1151
						   @CC, 
1152

1153
						   @CC_Abr, 
1154

1155
						   @CC_Desc  
1156

1157

1158

1159
   END
1160

1161

1162

1163
CLOSE cur_employee
1164

1165

1166

1167
DEALLOCATE cur_employee
1168

1169
  
1170

1171
  
1172

1173
  
1174

1175
  
1176

1177
  
1178

1179
  
1180

1181
  
1182

1183
  declare @hr_cust_py_report TABLE (
1184

1185
	[landscape] [varchar](4) NOT NULL,
1186

1187
	[rep_id] [varchar](50) NOT NULL,
1188

1189
	[row] [int] NOT NULL,
1190

1191
	[col_no] [int] NOT NULL,
1192

1193
	[seq_no] [int] NOT NULL,
1194

1195
	[wage_type] [varchar](5) NULL,
1196

1197
	[descr] [varchar](50) NULL
1198

1199
)
1200

1201
  
1202

1203
insert into @hr_cust_py_report
1204

1205
SELECT DISTINCT [landscape]
1206

1207
      ,[rep_id]
1208

1209
      ,[row]
1210

1211
      ,[col_no]
1212

1213
      ,[seq_no]
1214

1215
      ,[wage_type]
1216

1217
      ,[descr]
1218

1219
  FROM [dbo].[hr_cust_py_report] WITH (NOLOCK) WHERE start_date <= @payPeriodEndDate
1220

1221
  AND end_date >= @payPeriodEndDate and rep_id = 'WT01R'
1222

1223
  
1224

1225
     declare @hr_cust_py_report_col_name TABLE (
1226

1227
	[landscape] [varchar](4)  NULL,
1228

1229
	[rep_id] [varchar](50) NULL,
1230

1231
	[row] [int]  NULL,
1232

1233
	[col_no] [int]  NULL,
1234

1235
	column_name varchar(6))
1236

1237

1238

1239
insert into @hr_cust_py_report_col_name	
1240

1241
select *,
1242

1243
'WT' + right('00' + convert(varchar,(ROW_NUMBER () OVER (ORDER BY landscape, rep_id, row, col_no) + 49) / 50) ,2) 
1244

1245
+ right('00' + convert(varchar,((ROW_NUMBER () OVER (ORDER BY landscape, rep_id, row, col_no) -1) % 50)+1) ,2) as column_name
1246

1247
 from(
1248

1249
  select distinct landscape, rep_id, row, col_no from @hr_cust_py_report )a ORDER BY landscape, rep_id, row, col_no
1250

1251
  
1252

1253
  
1254

1255
	
1256

1257
  
1258

1259
  declare @row int
1260

1261
  declare @col int
1262

1263
  declare @column_name varchar(6)
1264

1265
  set @row = 0
1266

1267
  set @col = 0
1268

1269
  while @row < 4
1270

1271
  begin
1272

1273
  set @row = @row + 1
1274

1275
  set @col = 0
1276

1277
    while @col < 50
1278

1279
		begin
1280

1281
		set @col = @col + 1
1282

1283
		set @column_name = null
1284

1285
		
1286

1287
		select @column_name = column_name from @hr_cust_py_report_col_name 
1288

1289
		where column_name = 'WT' + right('00' + convert(varchar, @row),2) + right('00' + convert(varchar, @col),2)
1290

1291
		
1292

1293
if @column_name is null or @column_name = ''
1294

1295
begin
1296

1297
insert into @hr_cust_py_report_col_name
1298

1299
select @landscape,'',0,0,'WT' + right('00' + convert(varchar, @row),2) + right('00' + convert(varchar, @col),2)
1300

1301
end
1302

1303

1304

1305

1306

1307

1308

1309
		end
1310

1311
		
1312

1313
	end
1314

1315
	
1316

1317
	    declare @hr_cust_py_report_desc TABLE (
1318

1319
	[landscape] [varchar](4) NOT NULL,
1320

1321
	[rep_id] [varchar](50) NOT NULL,
1322

1323
	[row] [int] NOT NULL,
1324

1325
	[col_no] [int] NOT NULL,
1326

1327
	column_name varchar(6),
1328

1329
	[description] [varchar](50) NULL)
1330

1331
	
1332

1333
	insert into @hr_cust_py_report_desc
1334

1335
	SELECT DISTINCT a.*, b.descr from @hr_cust_py_report_col_name as a
1336

1337
	left join(
1338

1339
	select MAX(landscape) as landscape, MAX(rep_id) as rep_id, MAX(row) as row, MAX(col_no) as col_no,MAX(descr) as descr
1340

1341
	 from @hr_cust_py_report group by landscape, rep_id, row,col_no)as b
1342

1343
	 on a.landscape =b.landscape and 
1344

1345
	 a.rep_id=b.rep_id and
1346

1347
	 a.row=b.row and
1348

1349
	 a.col_no=b.col_no
1350

1351

1352

1353

1354

1355
INSERT INTO @tbl_tr_enc
1356

1357
SELECT DISTINCT pr.employee_id, pr.pay_period_month, pr.pay_period_year, 
1358

1359
	pr.run_period_month, pr.run_period_year, pr.wage_type, pr.amount 
1360

1361
	--CASE WHEN (rep.[column] < 10)
1362

1363
	--THEN 'WT0' + CONVERT(varchar(1), rep.row) + '0' + CONVERT(varchar(1), rep.[column])
1364

1365
	--ELSE 'WT0' + CONVERT(varchar(1), rep.row) + CONVERT(varchar(2), rep.[column]) END
1366

1367
FROM hr_tr0301 pr  WITH (NOLOCK)
1368

1369
	INNER JOIN @tbl_employee emp
1370

1371
		ON pr.employee_id = emp.employee_id
1372

1373
	INNER JOIN hr_cust_py_report rep WITH (NOLOCK)
1374

1375
		ON pr.landscape = rep.landscape
1376

1377
WHERE pr.run_period_year = @payPeriodYear
1378

1379
	AND pr.run_period_month = @payPeriodMonth
1380

1381
	AND pr.pay_period_year = @payPeriodYear
1382

1383
	AND pr.pay_period_month = @payPeriodMonth
1384

1385
	AND pr.wage_type = rep.wage_type
1386

1387
	--AND pr.employee_id in (select employee_id from @tbl_employee)
1388

1389
	--and pr.wage_type in (select wage_type from @hr_cust_py_report)
1390

1391

1392

1393
INSERT INTO @tbl_tr_enc
1394

1395
SELECT DISTINCT pr.employee_id, pr.pay_period_month, pr.pay_period_year, 
1396

1397
	pr.run_period_month, pr.run_period_year, pr.wage_type, pr.amount 
1398

1399
	--CASE WHEN (rep.[column] < 10)
1400

1401
	--THEN 'WT0' + CONVERT(varchar(1), rep.row) + '0' + CONVERT(varchar(1), rep.[column])
1402

1403
	--ELSE 'WT0' + CONVERT(varchar(1), rep.row) + CONVERT(varchar(2), rep.[column]) END
1404

1405
FROM hr_tr0301_retro pr  WITH (NOLOCK)
1406

1407
	INNER JOIN @tbl_employee emp
1408

1409
		ON pr.employee_id = emp.employee_id
1410

1411
	INNER JOIN hr_cust_py_report rep WITH (NOLOCK) 
1412

1413
		ON pr.landscape = rep.landscape
1414

1415
WHERE pr.run_period_year = @payPeriodYear
1416

1417
	AND pr.run_period_month = @payPeriodMonth
1418

1419
	AND pr.pay_period_year = @payPeriodYear
1420

1421
	AND pr.pay_period_month = @payPeriodMonth
1422

1423
	AND pr.wage_type = rep.wage_type
1424

1425
	AND (rep.wage_type NOT IN ('MT','MIT','MRT','5000','5001'))
1426

1427
	--AND pr.employee_id in (select employee_id from @tbl_employee)
1428

1429
	--and pr.wage_type in (select wage_type from @hr_cust_py_report)
1430

1431
	
1432

1433
DECLARE @tbl_tr_dec TABLE
1434

1435
	(col_no int,
1436

1437
	 employee_id varchar(8),
1438

1439
	 amount decimal(20,2))
1440

1441
	 
1442

1443
insert into @tbl_tr_dec
1444

1445
select col_no, employee_id, SUM(amount) as amount from(
1446

1447
select b.col_no,  c.employee_id, 
1448

1449
CASE @is_encrypted when '0' then convert(decimal(18, 2),amount) else convert(decimal(18, 2),dbo.SDE(amount, N'M!N0V@2010')) end as amount
1450

1451

1452

1453
  from @hr_cust_py_report_desc as b
1454

1455
inner  join @hr_cust_py_report as a on 
1456

1457
a.landscape = b.landscape and 
1458

1459
a.col_no = b.col_no and
1460

1461
a.rep_id = b.rep_id and
1462

1463
a.row = b.row 
1464

1465
inner join @tbl_tr_enc c on 
1466

1467
a.wage_type = c.wageType ) as d
1468

1469
group by col_no, employee_id
1470

1471

1472

1473

1474

1475
SELECT  employee_id	,
1476

1477
		employee_name	,
1478

1479
		employee_area_id	,
1480

1481
		employee_area_descr	,
1482

1483
		employee_office_id	,
1484

1485
		employee_office_descr	,
1486

1487
		employee_status_id	,
1488

1489
		employee_status_descr	,
1490

1491
		employee_type_id	,
1492

1493
		employee_type_descr	,
1494

1495
		employee_sub_type_id	,
1496

1497
		employee_sub_type_descr	,
1498

1499
		payroll_group_id	,
1500

1501
		payroll_group_descr	,
1502

1503
		organization_id	,
1504

1505
		organization_descr	,
1506

1507
		cost_center_id	,
1508

1509
		cost_center_abbr	,
1510

1511
		cost_center_descr	,
1512

1513
		cost_center_group_id	,
1514

1515
		cost_center_group_abbr	,
1516

1517
		cost_center_group_descr	,
1518

1519
		position_id	,
1520

1521
		position_descr	,
1522

1523
		job_id	,
1524

1525
		job_descr	,
1526

1527
		pay_type_id	,
1528

1529
		pay_type_descr	,
1530

1531
		pay_class_id	,
1532

1533
		pay_class_descr	,
1534

1535
		pay_grade_id	,
1536

1537
		pay_grade_descr	,
1538

1539
		pay_area_id	,
1540

1541
		pay_area_descr,
1542

1543
		dbo.fn_formatdatetime(hiring_date, 'dd/mm/yyyy') AS hiring_date,
1544

1545
		dbo.fn_formatdatetime(resign_date, 'dd/mm/yyyy') AS resign_date,
1546

1547
		1 AS jum_emp,
1548

1549
		--dbo.fn_formatdatetime((DATEADD(dd, -1, DATEADD(mm, 0, CONVERT(datetime, resign_date)))), 'yyyymmdd') resign_date,
1550

1551
	Max([WT0101]) as [WT0101],
1552

1553
Max([WT0102]) as [WT0102],
1554

1555
Max([WT0103]) as [WT0103],
1556

1557
Max([WT0104]) as [WT0104],
1558

1559
Max([WT0105]) as [WT0105],
1560

1561
Max([WT0106]) as [WT0106],
1562

1563
Max([WT0107]) as [WT0107],
1564

1565
Max([WT0108]) as [WT0108],
1566

1567
Max([WT0109]) as [WT0109],
1568

1569
Max([WT0110]) as [WT0110],
1570

1571
Max([WT0111]) as [WT0111],
1572

1573
Max([WT0112]) as [WT0112],
1574

1575
Max([WT0113]) as [WT0113],
1576

1577
Max([WT0114]) as [WT0114],
1578

1579
Max([WT0115]) as [WT0115],
1580

1581
Max([WT0116]) as [WT0116],
1582

1583
Max([WT0117]) as [WT0117],
1584

1585
Max([WT0118]) as [WT0118],
1586

1587
Max([WT0119]) as [WT0119],
1588

1589
Max([WT0120]) as [WT0120],
1590

1591
Max([WT0121]) as [WT0121],
1592

1593
Max([WT0122]) as [WT0122],
1594

1595
Max([WT0123]) as [WT0123],
1596

1597
Max([WT0124]) as [WT0124],
1598

1599
Max([WT0125]) as [WT0125],
1600

1601
Max([WT0126]) as [WT0126],
1602

1603
Max([WT0127]) as [WT0127],
1604

1605
Max([WT0128]) as [WT0128],
1606

1607
Max([WT0129]) as [WT0129],
1608

1609
Max([WT0130]) as [WT0130],
1610

1611
Max([WT0131]) as [WT0131],
1612

1613
Max([WT0132]) as [WT0132],
1614

1615
Max([WT0133]) as [WT0133],
1616

1617
Max([WT0134]) as [WT0134],
1618

1619
Max([WT0135]) as [WT0135],
1620

1621
Max([WT0136]) as [WT0136],
1622

1623
Max([WT0137]) as [WT0137],
1624

1625
Max([WT0138]) as [WT0138],
1626

1627
Max([WT0139]) as [WT0139],
1628

1629
Max([WT0140]) as [WT0140],
1630

1631
Max([WT0141]) as [WT0141],
1632

1633
Max([WT0142]) as [WT0142],
1634

1635
Max([WT0143]) as [WT0143],
1636

1637
Max([WT0144]) as [WT0144],
1638

1639
Max([WT0145]) as [WT0145],
1640

1641
Max([WT0146]) as [WT0146],
1642

1643
Max([WT0147]) as [WT0147],
1644

1645
Max([WT0148]) as [WT0148],
1646

1647
Max([WT0149]) as [WT0149],
1648

1649
Max([WT0150]) as [WT0150],
1650

1651
Max([WT0201]) as [WT0201],
1652

1653
Max([WT0202]) as [WT0202],
1654

1655
Max([WT0203]) as [WT0203],
1656

1657
Max([WT0204]) as [WT0204],
1658

1659
Max([WT0205]) as [WT0205],
1660

1661
Max([WT0206]) as [WT0206],
1662

1663
Max([WT0207]) as [WT0207],
1664

1665
Max([WT0208]) as [WT0208],
1666

1667
Max([WT0209]) as [WT0209],
1668

1669
Max([WT0210]) as [WT0210],
1670

1671
Max([WT0211]) as [WT0211],
1672

1673
Max([WT0212]) as [WT0212],
1674

1675
Max([WT0213]) as [WT0213],
1676

1677
Max([WT0214]) as [WT0214],
1678

1679
Max([WT0215]) as [WT0215],
1680

1681
Max([WT0216]) as [WT0216],
1682

1683
Max([WT0217]) as [WT0217],
1684

1685
Max([WT0218]) as [WT0218],
1686

1687
Max([WT0219]) as [WT0219],
1688

1689
Max([WT0220]) as [WT0220],
1690

1691
Max([WT0221]) as [WT0221],
1692

1693
Max([WT0222]) as [WT0222],
1694

1695
Max([WT0223]) as [WT0223],
1696

1697
Max([WT0224]) as [WT0224],
1698

1699
Max([WT0225]) as [WT0225],
1700

1701
Max([WT0226]) as [WT0226],
1702

1703
Max([WT0227]) as [WT0227],
1704

1705
Max([WT0228]) as [WT0228],
1706

1707
Max([WT0229]) as [WT0229],
1708

1709
Max([WT0230]) as [WT0230],
1710

1711
Max([WT0231]) as [WT0231],
1712

1713
Max([WT0232]) as [WT0232],
1714

1715
Max([WT0233]) as [WT0233],
1716

1717
Max([WT0234]) as [WT0234],
1718

1719
Max([WT0235]) as [WT0235],
1720

1721
Max([WT0236]) as [WT0236],
1722

1723
Max([WT0237]) as [WT0237],
1724

1725
Max([WT0238]) as [WT0238],
1726

1727
Max([WT0239]) as [WT0239],
1728

1729
Max([WT0240]) as [WT0240],
1730

1731
Max([WT0241]) as [WT0241],
1732

1733
Max([WT0242]) as [WT0242],
1734

1735
Max([WT0243]) as [WT0243],
1736

1737
Max([WT0244]) as [WT0244],
1738

1739
Max([WT0245]) as [WT0245],
1740

1741
Max([WT0246]) as [WT0246],
1742

1743
Max([WT0247]) as [WT0247],
1744

1745
Max([WT0248]) as [WT0248],
1746

1747
Max([WT0249]) as [WT0249],
1748

1749
Max([WT0250]) as [WT0250],
1750

1751
Max([WT0301]) as [WT0301],
1752

1753
Max([WT0302]) as [WT0302],
1754

1755
Max([WT0303]) as [WT0303],
1756

1757
Max([WT0304]) as [WT0304],
1758

1759
Max([WT0305]) as [WT0305],
1760

1761
Max([WT0306]) as [WT0306],
1762

1763
Max([WT0307]) as [WT0307],
1764

1765
Max([WT0308]) as [WT0308],
1766

1767
Max([WT0309]) as [WT0309],
1768

1769
Max([WT0310]) as [WT0310],
1770

1771
Max([WT0311]) as [WT0311],
1772

1773
Max([WT0312]) as [WT0312],
1774

1775
Max([WT0313]) as [WT0313],
1776

1777
Max([WT0314]) as [WT0314],
1778

1779
Max([WT0315]) as [WT0315],
1780

1781
Max([WT0316]) as [WT0316],
1782

1783
Max([WT0317]) as [WT0317],
1784

1785
Max([WT0318]) as [WT0318],
1786

1787
Max([WT0319]) as [WT0319],
1788

1789
Max([WT0320]) as [WT0320],
1790

1791
Max([WT0321]) as [WT0321],
1792

1793
Max([WT0322]) as [WT0322],
1794

1795
Max([WT0323]) as [WT0323],
1796

1797
Max([WT0324]) as [WT0324],
1798

1799
Max([WT0325]) as [WT0325],
1800

1801
Max([WT0326]) as [WT0326],
1802

1803
Max([WT0327]) as [WT0327],
1804

1805
Max([WT0328]) as [WT0328],
1806

1807
Max([WT0329]) as [WT0329],
1808

1809
Max([WT0330]) as [WT0330],
1810

1811
Max([WT0331]) as [WT0331],
1812

1813
Max([WT0332]) as [WT0332],
1814

1815
Max([WT0333]) as [WT0333],
1816

1817
Max([WT0334]) as [WT0334],
1818

1819
Max([WT0335]) as [WT0335],
1820

1821
Max([WT0336]) as [WT0336],
1822

1823
Max([WT0337]) as [WT0337],
1824

1825
Max([WT0338]) as [WT0338],
1826

1827
Max([WT0339]) as [WT0339],
1828

1829
Max([WT0340]) as [WT0340],
1830

1831
Max([WT0341]) as [WT0341],
1832

1833
Max([WT0342]) as [WT0342],
1834

1835
Max([WT0343]) as [WT0343],
1836

1837
Max([WT0344]) as [WT0344],
1838

1839
Max([WT0345]) as [WT0345],
1840

1841
Max([WT0346]) as [WT0346],
1842

1843
Max([WT0347]) as [WT0347],
1844

1845
Max([WT0348]) as [WT0348],
1846

1847
Max([WT0349]) as [WT0349],
1848

1849
Max([WT0350]) as [WT0350],
1850

1851
Max([WT0401]) as [WT0401],
1852

1853
Max([WT0402]) as [WT0402],
1854

1855
Max([WT0403]) as [WT0403],
1856

1857
Max([WT0404]) as [WT0404],
1858

1859
Max([WT0405]) as [WT0405],
1860

1861
Max([WT0406]) as [WT0406],
1862

1863
Max([WT0407]) as [WT0407],
1864

1865
Max([WT0408]) as [WT0408],
1866

1867
Max([WT0409]) as [WT0409],
1868

1869
Max([WT0410]) as [WT0410],
1870

1871
Max([WT0411]) as [WT0411],
1872

1873
Max([WT0412]) as [WT0412],
1874

1875
Max([WT0413]) as [WT0413],
1876

1877
Max([WT0414]) as [WT0414],
1878

1879
Max([WT0415]) as [WT0415],
1880

1881
Max([WT0416]) as [WT0416],
1882

1883
Max([WT0417]) as [WT0417],
1884

1885
Max([WT0418]) as [WT0418],
1886

1887
Max([WT0419]) as [WT0419],
1888

1889
Max([WT0420]) as [WT0420],
1890

1891
Max([WT0421]) as [WT0421],
1892

1893
Max([WT0422]) as [WT0422],
1894

1895
Max([WT0423]) as [WT0423],
1896

1897
Max([WT0424]) as [WT0424],
1898

1899
Max([WT0425]) as [WT0425],
1900

1901
Max([WT0426]) as [WT0426],
1902

1903
Max([WT0427]) as [WT0427],
1904

1905
Max([WT0428]) as [WT0428],
1906

1907
Max([WT0429]) as [WT0429],
1908

1909
Max([WT0430]) as [WT0430],
1910

1911
Max([WT0431]) as [WT0431],
1912

1913
Max([WT0432]) as [WT0432],
1914

1915
Max([WT0433]) as [WT0433],
1916

1917
Max([WT0434]) as [WT0434],
1918

1919
Max([WT0435]) as [WT0435],
1920

1921
Max([WT0436]) as [WT0436],
1922

1923
Max([WT0437]) as [WT0437],
1924

1925
Max([WT0438]) as [WT0438],
1926

1927
Max([WT0439]) as [WT0439],
1928

1929
Max([WT0440]) as [WT0440],
1930

1931
Max([WT0441]) as [WT0441],
1932

1933
Max([WT0442]) as [WT0442],
1934

1935
Max([WT0443]) as [WT0443],
1936

1937
Max([WT0444]) as [WT0444],
1938

1939
Max([WT0445]) as [WT0445],
1940

1941
Max([WT0446]) as [WT0446],
1942

1943
Max([WT0447]) as [WT0447],
1944

1945
Max([WT0448]) as [WT0448],
1946

1947
Max([WT0449]) as [WT0449],
1948

1949
Max([WT0450]) as [WT0450]
1950

1951
 from(
1952

1953
SELECT DISTINCT d.*, c.column_name, c.description, c.landscape, c.rep_id, c.row from @hr_cust_py_report_desc as c left join
1954

1955
(
1956

1957

1958

1959
select DISTINCT b.*, a.col_no,a.amount--, c.start_date AS resign_date--, d.start_date AS hiring_date
1960

1961
from @tbl_tr_dec as a 
1962

1963
inner join @tbl_employee2 as b on a.employee_id = b.employee_id
1964

1965
--left join hr_md_emp_md0002 as c 
1966

1967
--	on  c.emp_id = b.employee_id 
1968

1969
--	and c.start_date <= @payPeriodEndDate and c.end_date >= @payPeriodEndDate 
1970

1971
--	and c.movement_type = '80'
1972

1973
--left join hr_md_emp_md0002 as d 
1974

1975
--	on  d.emp_id = b.employee_id 
1976

1977
	--and d.start_date <= @payPeriodEndDate and d.end_date >= @payPeriodEndDate 
1978

1979
	--and d.movement_type = '10'
1980

1981
 ) as d
1982

1983
 on c.col_no = d.col_no 
1984

1985
 )as SourceTable
1986

1987
 PIVOT
1988

1989
(
1990

1991
  max(amount)
1992

1993
  for column_name in  ([WT0101],[WT0102],[WT0103],[WT0104],[WT0105],[WT0106],[WT0107],[WT0108],[WT0109],[WT0110],[WT0111],[WT0112],[WT0113],[WT0114],[WT0115],[WT0116],[WT0117],[WT0118],[WT0119],[WT0120],[WT0121],[WT0122],[WT0123],[WT0124],[WT0125],[WT0126],[WT0127],[WT0128],[WT0129],[WT0130],[WT0131],[WT0132],[WT0133],[WT0134],[WT0135],[WT0136],[WT0137],[WT0138],[WT0139],[WT0140],[WT0141],[WT0142],[WT0143],[WT0144],[WT0145],[WT0146],[WT0147],[WT0148],[WT0149],[WT0150],
1994

1995
			[WT0201],[WT0202],[WT0203],[WT0204],[WT0205],[WT0206],[WT0207],[WT0208],[WT0209],[WT0210],[WT0211],[WT0212],[WT0213],[WT0214],[WT0215],[WT0216],[WT0217],[WT0218],[WT0219],[WT0220],[WT0221],[WT0222],[WT0223],[WT0224],[WT0225],[WT0226],[WT0227],[WT0228],[WT0229],[WT0230],[WT0231],[WT0232],[WT0233],[WT0234],[WT0235],[WT0236],[WT0237],[WT0238],[WT0239],[WT0240],[WT0241],[WT0242],[WT0243],[WT0244],[WT0245],[WT0246],[WT0247],[WT0248],[WT0249],[WT0250],
1996

1997
			[WT0301],[WT0302],[WT0303],[WT0304],[WT0305],[WT0306],[WT0307],[WT0308],[WT0309],[WT0310],[WT0311],[WT0312],[WT0313],[WT0314],[WT0315],[WT0316],[WT0317],[WT0318],[WT0319],[WT0320],[WT0321],[WT0322],[WT0323],[WT0324],[WT0325],[WT0326],[WT0327],[WT0328],[WT0329],[WT0330],[WT0331],[WT0332],[WT0333],[WT0334],[WT0335],[WT0336],[WT0337],[WT0338],[WT0339],[WT0340],[WT0341],[WT0342],[WT0343],[WT0344],[WT0345],[WT0346],[WT0347],[WT0348],[WT0349],[WT0350],
1998

1999
			[WT0401],[WT0402],[WT0403],[WT0404],[WT0405],[WT0406],[WT0407],[WT0408],[WT0409],[WT0410],[WT0411],[WT0412],[WT0413],[WT0414],[WT0415],[WT0416],[WT0417],[WT0418],[WT0419],[WT0420],[WT0421],[WT0422],[WT0423],[WT0424],[WT0425],[WT0426],[WT0427],[WT0428],[WT0429],[WT0430],[WT0431],[WT0432],[WT0433],[WT0434],[WT0435],[WT0436],[WT0437],[WT0438],[WT0439],[WT0440],[WT0441],[WT0442],[WT0443],[WT0444],[WT0445],[WT0446],[WT0447],[WT0448],[WT0449],[WT0450]))AS PivotTable
2000

2001
	group by employee_id	,
2002

2003
		employee_name	,
2004

2005
		employee_area_id	,
2006

2007
		employee_area_descr	,
2008

2009
		employee_office_id	,
2010

2011
		employee_office_descr	,
2012

2013
		employee_status_id	,
2014

2015
		employee_status_descr	,
2016

2017
		employee_type_id	,
2018

2019
		employee_type_descr	,
2020

2021
		employee_sub_type_id	,
2022

2023
		employee_sub_type_descr	,
2024

2025
		payroll_group_id	,
2026

2027
		payroll_group_descr	,
2028

2029
		organization_id	,
2030

2031
		organization_descr	,
2032

2033
		cost_center_id	,
2034

2035
		cost_center_abbr	,
2036

2037
		cost_center_descr	,
2038

2039
		cost_center_group_id	,
2040

2041
		cost_center_group_abbr	,
2042

2043
		cost_center_group_descr	,
2044

2045
		position_id	,
2046

2047
		position_descr	,
2048

2049
		job_id	,
2050

2051
		job_descr	,
2052

2053
		pay_type_id	,
2054

2055
		pay_type_descr	,
2056

2057
		pay_class_id	,
2058

2059
		pay_class_descr	,
2060

2061
		pay_grade_id	,
2062

2063
		pay_grade_descr	,
2064

2065
		pay_area_id	,
2066

2067
		pay_area_descr,
2068

2069
		hiring_date,
2070

2071
		resign_date	
2072

2073
	having employee_id is not null
2074

2075

2076

2077

2078

2079
--inner join
2080

2081
-- @hr_cust_py_report_desc as b on a.landscape = b.landscape and
2082

2083
-- a.rep_id =b.rep_id 
2084

2085
  
2086

2087
--  select @companyname as companyname, @logo as logo, @payPeriodStr as payPeriodStr, 'Employee ID' as employee_id , 'Employee Name' as employee_name, 'Cost Center Group' as CostCenterGroup, 'Cost Center Group_Abr' as CostCenterGroup_Abr, 	 'Cost Center Group_Desc' as CostCenterGroup_Desc, 	 'Cost Center' as CostCenter, 	 'Cost Center_Abr' as CostCenter_Abr, 	 'Cost Center_Desc' as CostCenter_Desc,  * from (select column_name, description from @hr_cust_py_report_desc)as SourceTable
2088

2089
--PIVOT
2090

2091
--(
2092

2093
--  max(description)
2094

2095
--  for column_name in  ([WT0101],[WT0102],[WT0103],[WT0104],[WT0105],[WT0106],[WT0107],[WT0108],[WT0109],[WT0110],[WT0111],[WT0112],[WT0113],[WT0114],[WT0115],[WT0116],[WT0117],[WT0118],[WT0119],[WT0120],[WT0121],[WT0122],[WT0123],[WT0124],[WT0125],[WT0126],[WT0127],[WT0128],[WT0129],[WT0130],[WT0131],[WT0132],[WT0133],[WT0134],[WT0135],[WT0136],[WT0137],[WT0138],[WT0139],[WT0140],[WT0141],[WT0142],[WT0143],[WT0144],[WT0145],[WT0146],[WT0147],[WT0148],[WT0149],[WT0150],
2096

2097
--			[WT0201],[WT0202],[WT0203],[WT0204],[WT0205],[WT0206],[WT0207],[WT0208],[WT0209],[WT0210],[WT0211],[WT0212],[WT0213],[WT0214],[WT0215],[WT0216],[WT0217],[WT0218],[WT0219],[WT0220],[WT0221],[WT0222],[WT0223],[WT0224],[WT0225],[WT0226],[WT0227],[WT0228],[WT0229],[WT0230],[WT0231],[WT0232],[WT0233],[WT0234],[WT0235],[WT0236],[WT0237],[WT0238],[WT0239],[WT0240],[WT0241],[WT0242],[WT0243],[WT0244],[WT0245],[WT0246],[WT0247],[WT0248],[WT0249],[WT0250],
2098

2099
--			[WT0301],[WT0302],[WT0303],[WT0304],[WT0305],[WT0306],[WT0307],[WT0308],[WT0309],[WT0310],[WT0311],[WT0312],[WT0313],[WT0314],[WT0315],[WT0316],[WT0317],[WT0318],[WT0319],[WT0320],[WT0321],[WT0322],[WT0323],[WT0324],[WT0325],[WT0326],[WT0327],[WT0328],[WT0329],[WT0330],[WT0331],[WT0332],[WT0333],[WT0334],[WT0335],[WT0336],[WT0337],[WT0338],[WT0339],[WT0340],[WT0341],[WT0342],[WT0343],[WT0344],[WT0345],[WT0346],[WT0347],[WT0348],[WT0349],[WT0350],
2100

2101
--			[WT0401],[WT0402],[WT0403],[WT0404],[WT0405],[WT0406],[WT0407],[WT0408],[WT0409],[WT0410],[WT0411],[WT0412],[WT0413],[WT0414],[WT0415],[WT0416],[WT0417],[WT0418],[WT0419],[WT0420],[WT0421],[WT0422],[WT0423],[WT0424],[WT0425],[WT0426],[WT0427],[WT0428],[WT0429],[WT0430],[WT0431],[WT0432],[WT0433],[WT0434],[WT0435],[WT0436],[WT0437],[WT0438],[WT0439],[WT0440],[WT0441],[WT0442],[WT0443],[WT0444],[WT0445],[WT0446],[WT0447],[WT0448],[WT0449],[WT0450]))AS PivotTable;
2102

2103
  
2104

2105
  
2106

2107
  
2108

2109

2110

2111

2112

2113

2114

2115

(7-7/7)