Project

General

Profile

Feature #984 » CRPTTUGUPYPOSTTOGL_20220223.sql

Tri Rizqiaty, 03/23/2022 10:40 AM

 
1
??USE [MinovaES_TPI_Prod]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[CRPTTUGUPYPOSTTOGL]    Script Date: 23/03/2022 10.34.59 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[CRPTTUGUPYPOSTTOGL] 
16

17
	-- exec Rpt_HR_PY_PostToGL_BNP  '100', '1000', '01', '', '', '', '201102', 'scriberion', 'triadblank'
18

19
	-- Add the parameters for the stored procedure here
20

21
	@CompanyID VARCHAR(4),
22

23
	@PayGroup VARCHAR(5),
24

25
	@EmployeeArea VARCHAR(8), 
26

27
	@CostCenter VARCHAR(8),
28

29
	@EmployeeID VARCHAR(8),
30

31
	@RunPeriod varchar(6),
32

33
	@key1 AS NVARCHAR(max),
34

35
	@key2 AS NVARCHAR(max),
36

37
	@EmployeeType VARCHAR(8),
38

39
	@MenuID VARCHAR(250),
40

41
	@UserID VARCHAR(250)
42

43
AS
44

45

46

47
--DECLARE @CompanyID VARCHAR(4) = '1000'
48

49
--DECLARE	@PayGroup VARCHAR(5) = '01'
50

51
--DECLARE	@EmployeeArea VARCHAR(8) = ''
52

53
--DECLARE	@CostCenter VARCHAR(8) = ''
54

55
--DECLARE	@EmployeeID VARCHAR(8) = ''--'00000582'
56

57
--DECLARE	@RunPeriod varchar(6) = '202001'
58

59
--DECLARE	@key1 AS NVARCHAR(max) = 'scriberion'
60

61
--DECLARE	@key2 AS NVARCHAR(max) = 'triadblank'
62

63
--DECLARE @EmployeeType VARCHAR(8) = ''
64

65
--DECLARE @MenuID VARCHAR(250) = 'woty'
66

67
--DECLARE @UserID VARCHAR(250) = 'PYAR40'
68

69

70

71
--//-- Variable Declaration
72

73
DECLARE @payPeriodMonth varchar(2)
74

75
DECLARE @payPeriodYear varchar(4)
76

77
DECLARE @payPeriodStartDate varchar(8)
78

79
DECLARE @payPeriodEndDate varchar(8)
80

81
DECLARE @payPeriodEndDateDt datetime
82

83

84

85
--//-- Table Declaration
86

87
DECLARE @t_final TABLE
88

89
	(EmployeeID varchar(8),
90

91
	 PayPeriodMonth varchar(2),
92

93
	 PayPeriodYear varchar(4),
94

95
	 RunPeriodMonth varchar(2),
96

97
	 RunPeriodYear varchar(4),
98

99
	 WageType varchar(20), 
100

101
	 WageTypeDesc varchar(250),
102

103
	 CostElementDesc VARCHAR (250),
104

105
	 CostElement VARCHAR (50),
106

107
	 GLAccount VARCHAR (150),
108

109
     DEBET DECIMAL(18,2),
110

111
     CostCenterAbbr varchar(250),
112

113
	 EmployeeArea varchar (12),
114

115
	 EmployeeAreaDesc varchar (250),
116

117
	 CostCenter varchar(12),
118

119
	 CostCenterDesc varchar(250),
120

121
	 CostElementGroup VARCHAR(10),
122

123
     CostElementGroupDesc VARCHAR(250),
124

125
	 EmployeeType VARCHAR(10),
126

127
	 EmployeeTypeDesc VARCHAR(250),
128

129
	 PeriodMonth VARCHAR(50),
130

131
	 CostElementTotal DECIMAL(18,2),
132

133
	 CostElementAbbr VARCHAR(250)
134

135
	 )
136

137

138

139
DECLARE @tbl_result TABLE
140

141
	(EmployeeID varchar(8),
142

143
	 PayPeriodMonth varchar(2),
144

145
	 PayPeriodYear varchar(4),
146

147
	 RunPeriodMonth varchar(2),
148

149
	 RunPeriodYear varchar(4),
150

151
	 WageType varchar(20), 
152

153
	 WageTypeDesc varchar(250),
154

155
	 CostElementDesc VARCHAR (250),
156

157
	 CostElement VARCHAR (50),
158

159
	 GLAccount VARCHAR (150),
160

161
     DEBET DECIMAL(18,2),
162

163
     CostCenterAbbr varchar(250),
164

165
	 EmployeeArea varchar (12),
166

167
	 EmployeeAreaDesc varchar (250),
168

169
	 CostCenter varchar(12),
170

171
	 CostCenterDesc varchar(250),
172

173
	 CostElementGroup VARCHAR(10),
174

175
     CostElementGroupDesc VARCHAR(250),
176

177
	 EmployeeType VARCHAR(10),
178

179
	 EmployeeTypeDesc VARCHAR(250),
180

181
	 PeriodMonth VARCHAR(50),
182

183
	 CostElementTotal DECIMAL(18,2),
184

185
	 CostElementAbbr VARCHAR(250)
186

187
	 )
188

189
	 
190

191
DECLARE @tbl_employee TABLE
192

193
	(
194

195
	 EmployeeID varchar(8),
196

197
	 CostCenter varchar(12),
198

199
	 EmployeeArea varchar (12),
200

201
	 EmployeeAreaDesc varchar (250),
202

203
	 CostCenterDesc varchar(250),
204

205
	 CostCenterAbbr varchar(250),
206

207
	 EmployeeType VARCHAR(10),
208

209
	 EmployeeTypeDesc VARCHAR(250))
210

211

212

213

214

215

216

217
DECLARE @tbl_tr_enc TABLE
218

219
	(EmployeeID varchar(8),
220

221
	 PayPeriodMonth varchar(2),
222

223
	 PayPeriodYear varchar(4),
224

225
	 RunPeriodMonth varchar(2),
226

227
	 RunPeriodYear varchar(4),
228

229
	 WageTyoe varchar(4),
230

231
	 Amount varchar(250),
232

233
	 Rate varchar(250),
234

235
	 FlagRetro varchar(3))
236

237

238

239
DECLARE @tbl_tr_dec TABLE
240

241
	(EmployeeID varchar(8),
242

243
	 PayPeriodMonth varchar(2),
244

245
	 PayPeriodYear varchar(4),
246

247
	 RunPeriodMonth varchar(2),
248

249
	 RunPeriodYear varchar(4),
250

251
	 WageTyoe varchar(4),
252

253
	 Amount DECIMAL(22,2),
254

255
	 Rate DECIMAL(22,2),
256

257
	 FlagRetro varchar(3))
258

259
	 
260

261
DECLARE @tbl_cu0300 TABLE
262

263
	(
264

265
	WageType VARCHAR(20) 
266

267
    ,StartDate VARCHAR(20)
268

269
    ,EndDate VARCHAR(20) 
270

271
    ,WageTypeDesc VARCHAR(250) 
272

273
    ,CostElement VARCHAR(20) 
274

275
    ,CostElementDesc VARCHAR(250) 
276

277
    ,GLAccount VARCHAR(250) 
278

279
    ,DebitAcc VARCHAR(50) 
280

281
	,CreditAcc VARCHAR(50)
282

283
    ,CostElementGroup VARCHAR(20)
284

285
    ,CostElementGroupDesc VARCHAR(250) 
286

287
    ,CostElementAbbr VARCHAR(250)
288

289
	)
290

291
	
292

293
DECLARE @tbl_tr_dec_final TABLE
294

295
	(EmployeeID varchar(8),
296

297
	 PayPeriodMonth varchar(2),
298

299
	 PayPeriodYear varchar(4),
300

301
	 RunPeriodMonth varchar(2),
302

303
	 RunPeriodYear varchar(4),
304

305
	 WageTyoe varchar(4),
306

307
	 Amount DECIMAL(22,2),
308

309
	 Aate DECIMAL(22,2),
310

311
	 FlagRetro varchar(3))
312

313
	 
314

315
DECLARE @t_ce_area TABLE
316

317
(
318

319
	CostElement VARCHAR(10)
320

321
	,EmployeeArea VARCHAR(10)
322

323
)
324

325

326

327

328

329
--//-- Setting variable
330

331
SET @payPeriodMonth = SUBSTRING(@RunPeriod, 5, 2)
332

333
SET @payPeriodYear = SUBSTRING(@RunPeriod, 1, 4)
334

335
SET @payPeriodStartDate = @RunPeriod + '01'
336

337
SET @payPeriodEndDateDt = DATEADD(dd, -1, DATEADD(mm, 1, CONVERT(datetime, @payPeriodStartDate)))
338

339
SET @payPeriodEndDate = @RunPeriod + CONVERT(varchar(2), DATEPART(dd, @payPeriodEndDateDt))
340

341

342

343
--//-- Select Employee
344

345
INSERT INTO @tbl_employee
346

347
SELECT  tr0300.EmployeeID, 
348

349
		tr0300.CostCenter, 
350

351
		tr0300.EmployeeArea, 
352

353
		area.EmployeeAreaDescription,
354

355
	    cost_center.ObjectDescription AS CCDesc, 
356

357
		cost_center.ObjectDescription AS CCAbbr ----cost_center.abbreviation
358

359
	    ,tr0300.EmployeeType, 
360

361
		emptype.EmployeeTypeDescription
362

363
FROM dbo.PHRPYTR0300 AS tr0300
364

365
LEFT JOIN dbo.PHROM0001 AS cost_center
366

367
		ON tr0300.CostCenter= cost_center.ObjectID
368

369
		AND cost_center.ObjectClass = 'CC'
370

371
		AND cost_center.StartDate <= @payPeriodEndDate
372

373
		AND cost_center.EndDate >= @payPeriodEndDate
374

375
LEFT JOIN dbo.PCMEPEMPAREA AS area
376

377
		ON tr0300.EmployeeArea = area.EmployeeArea 
378

379
LEFT JOIN dbo.PCMEPEMPTYP AS emptype
380

381
		ON emptype.EmployeeType = tr0300.EmployeeType
382

383
WHERE (tr0300.EmployeeID = @EmployeeID OR @EmployeeID = '')
384

385
	  AND tr0300.PayPeriodMonth = @payPeriodMonth
386

387
	  AND tr0300.PayPeriodYear = @payPeriodYear 
388

389
	  AND tr0300.RunPeriodMonth = @payPeriodMonth
390

391
	  AND tr0300.RunPeriodYear = @payPeriodYear 
392

393
	  AND (tr0300.CostCenter = @CostCenter OR @CostCenter = '')
394

395
	  AND (tr0300.EmployeeType = @EmployeeType OR @EmployeeType = '')
396

397
	  AND (tr0300.PayrollGroup = @PayGroup OR @PayGroup = '')
398

399

400

401
--//-- Table cu_0300
402

403
INSERT INTO @tbl_cu0300
404

405
SELECT cu_0300.WageTypeDefinition 
406

407
      ,cu_0300.StartDate
408

409
      ,cu_0300.EndDate
410

411
      ,cu_0300.WTDefinitionDesc  
412

413
      ,CE.ObjectID AS CE
414

415
      ,CE_Desc.ObjectDescription AS CEDesc
416

417
      ,CE.GLAccount
418

419
      ,cu_0300.DebitAcc	  
420

421
	  ,cu_0300.CreditAcc
422

423
      ,eg.ObjectID AS EG
424

425
      ,eg.ObjectDescription AS EGDesc
426

427
      ,CE_Desc.Abbreviation AS CEAbbr
428

429
FROM dbo.PHRPYCU0300 AS cu_0300
430

431
LEFT JOIN PHROM0017 AS CE
432

433
	  ON CE.StartDate <= @payPeriodEndDate
434

435
	  AND CE.EndDate >= @payPeriodEndDate
436

437
	  AND (CE.ObjectID = cu_0300.DebitAcc Or CE.ObjectID = cu_0300.CreditAcc)	  
438

439
LEFT JOIN dbo.PHROM0001 AS CE_Desc
440

441
	  ON CE_Desc.StartDate <= @payPeriodEndDate
442

443
	  AND CE_Desc.EndDate >= @payPeriodEndDate
444

445
	  AND CE_Desc.ObjectID = CE.ObjectID
446

447
	  AND CE_Desc.ObjectClass = 'CE'
448

449
LEFT JOIN PHROM0002 AS ce_eg
450

451
	  ON CE_Desc.ObjectID = ce_eg.ObjectID
452

453
	  AND ce_eg.ObjectClass = 'CE' AND ce_eg.RelationshipClass = 'EG'
454

455
	  AND ce_eg.StartDate <= @payPeriodEndDate 
456

457
	  AND ce_eg.EndDate >= @payPeriodEndDate
458

459
LEFT JOIN PHROM0001 AS eg
460

461
	  ON eg.ObjectID = ce_eg.RelationshipObject
462

463
	  AND eg.ObjectClass = 'EG'
464

465
	  AND eg.StartDate <= @payPeriodEndDate 
466

467
	  AND eg.EndDate >= @payPeriodEndDate
468

469
WHERE cu_0300.StartDate <= @payPeriodEndDate
470

471
      AND cu_0300.EndDate >= @payPeriodEndDate
472

473
      AND cu_0300.IsActive = '1'
474

475
      AND (cu_0300.DebitAcc <> '' or cu_0300.CreditAcc <> '')
476

477
----SELECT * FROM @tbl_cu0300
478

479

480

481
------------------- Get data payroll --------------------
482

483
INSERT INTO @tbl_tr_enc
484

485
SELECT  pr.EmployeeID, 
486

487
		pr.PayPeriodMonth, pr.PayPeriodYear, 
488

489
	    pr.RunPeriodMonth, pr.RunPeriodYear, 
490

491
	    pr.WageType, pr.amount, pr.rate, 'N'
492

493
FROM dbo.PHRPYTR0301 pr  WITH (NOLOCK)
494

495
INNER JOIN @tbl_employee emp
496

497
		ON pr.EmployeeID = emp.EmployeeID
498

499
WHERE pr.RunPeriodYear = @payPeriodYear
500

501
	  AND pr.RunPeriodMonth = @payPeriodMonth
502

503
	  AND pr.PayPeriodYear = @payPeriodYear
504

505
      AND pr.PayPeriodMonth = @payPeriodMonth
506

507
      AND (pr.EmployeeID = @EmployeeID OR @EmployeeID = '')
508

509
      AND pr.WageType IN ( SELECT DISTINCT WageType FROM @tbl_cu0300)
510

511

512

513
INSERT INTO @tbl_tr_enc
514

515
SELECT  pr.EmployeeID, 
516

517
		pr.PayPeriodMonth, pr.PayPeriodYear, 
518

519
	    pr.RunPeriodMonth, pr.RunPeriodYear, 
520

521
	    pr.WageType, pr.amount, pr.rate, 'R'
522

523
FROM dbo.PHRPYTR0301RET pr  WITH (NOLOCK)
524

525
INNER JOIN @tbl_employee emp
526

527
		ON pr.EmployeeID = emp.EmployeeID
528

529
WHERE pr.RunPeriodYear = @payPeriodYear
530

531
	  AND pr.RunPeriodMonth = @payPeriodMonth
532

533
	  AND pr.PayPeriodYear = @payPeriodYear
534

535
      AND pr.PayPeriodMonth = @payPeriodMonth
536

537
      AND (pr.EmployeeID = @EmployeeID OR @EmployeeID = '')
538

539
      AND pr.WageType IN ( SELECT DISTINCT WageType FROM @tbl_cu0300)
540

541

542

543
INSERT INTO @tbl_tr_dec
544

545
SELECT pr.EmployeeID, 
546

547
	   pr.PayPeriodMonth, pr.PayPeriodYear, 
548

549
	   pr.RunPeriodMonth, pr.RunPeriodYear, pr.WageTyoe,
550

551
	   CONVERT(decimal(18,0), dbo.SDE(pr.amount, 'M!N0V@2010')),
552

553
	   CONVERT(decimal(18,0), dbo.SDE(pr.rate, 'M!N0V@2010')),  pr.FlagRetro
554

555
FROM @tbl_tr_enc pr
556

557
--select * from @tbl_tr_dec
558

559

560

561
INSERT INTO @tbl_tr_dec_final
562

563
SELECT tbl_tr_dec.EmployeeID,
564

565
	   PayPeriodMonth, PayPeriodYear,
566

567
	   RunPeriodMonth, RunPeriodYear, tbl_tr_dec.WageTyoe,
568

569
	   SUM(tbl_tr_dec.Amount) AS amount, SUM(tbl_tr_dec.Rate) AS rate, 
570

571
	   min(tbl_tr_dec.FlagRetro) AS flg_retro
572

573
FROM @tbl_tr_dec AS tbl_tr_dec
574

575
GROUP BY tbl_tr_dec.EmployeeID, 
576

577
		PayPeriodMonth, PayPeriodYear,
578

579
		RunPeriodMonth, RunPeriodYear, WageTyoe
580

581
----select * from @tbl_tr_dec_final	
582

583

584

585
INSERT INTO @t_ce_area
586

587
SELECT Value1, Value2 FROM dbo.PCMEPGENPARAM WHERE Parameter = 'TUGU_GL_COSTELEMENT' --AND val1 = '00000044'
588

589

590

591

592

593
INSERT INTO @tbl_result
594

595
SELECT DISTINCT  
596

597
	   tbl_employee.EmployeeID ,
598

599
	   tbl_tr_dec_final.PayPeriodMonth ,
600

601
	   tbl_tr_dec_final.PayPeriodYear ,
602

603
	   tbl_tr_dec_final.RunPeriodMonth ,
604

605
	   tbl_tr_dec_final.RunPeriodYear , 
606

607
	   tbl_tr_dec_final.WageTyoe , 
608

609
	   tbl_cu0300.WageTypeDesc ,
610

611
	   --tbl_cu0300.cost_element_name, 
612

613
	   --tbl_cu0300.cost_element,
614

615
	   CASE 
616

617
			WHEN tbl_cu0300.GLAccount <> '' THEN tbl_cu0300.CostElementDesc
618

619
			ELSE tbl_employee.EmployeeTypeDesc
620

621
	   END, 
622

623
	   CASE 
624

625
			WHEN tbl_cu0300.GLAccount <> '' THEN tbl_cu0300.CostElement
626

627
			ELSE tbl_employee.EmployeeTypeDesc
628

629
	   END,
630

631
	   CASE 
632

633
			WHEN tbl_cu0300.GLAccount <> '' THEN tbl_cu0300.GLAccount
634

635
			ELSE ''
636

637
	   END,  
638

639
	   tbl_tr_dec_final.amount AS DEBET ,
640

641
	   tbl_employee.CostCenterAbbr , 
642

643
	   tbl_employee.EmployeeArea ,
644

645
	   tbl_employee.EmployeeAreaDesc ,
646

647
	   tbl_employee.CostCenter , 
648

649
	   tbl_employee.CostCenterDesc ,
650

651
	   tbl_cu0300.CostElementGroup ,
652

653
	   tbl_cu0300.CostElementGroupDesc ,
654

655
	   tbl_employee.EmployeeType ,
656

657
	   tbl_employee.EmployeeTypeDesc
658

659
	   ,dbo.fn_formatdatetime(@RunPeriod + '01', 'mmmm') AS period_month
660

661
	   ,CASE WHEN tbl_cu0300.CostElementGroup = '00000003' THEN tbl_tr_dec_final.Amount ELSE tbl_tr_dec_final.Amount * -1 END AS CETotal
662

663
	   ,tbl_cu0300.CostElementAbbr
664

665
FROM @tbl_tr_dec_final AS tbl_tr_dec_final
666

667
LEFT JOIN @tbl_cu0300 AS tbl_cu0300
668

669
	   ON tbl_cu0300.WageType  = tbl_tr_dec_final.WageTyoe
670

671
LEFT JOIN @tbl_employee AS tbl_employee
672

673
	ON tbl_tr_dec_final.EmployeeID = tbl_employee.EmployeeID
674

675
WHERE tbl_tr_dec_final.Amount <> 0
676

677

678

679
------SELECT * FROM @t_ce_area
680

681
------SELECT * FROM @tbl_result 
682

683

684

685
INSERT INTO @t_final
686

687
SELECT tr.* 
688

689
FROM @tbl_result AS tr
690

691
WHERE tr.CostElement NOT IN ( SELECT CostElement FROM @t_ce_area)
692

693

694

695
DECLARE @c_ce VARCHAR(10)
696

697
DECLARE @c_area VARCHAR(10)
698

699
DECLARE cur_ce CURSOR FOR SELECT DISTINCT EmployeeArea, CostElement  FROM @t_ce_area 
700

701
OPEN cur_ce
702

703
FETCH cur_ce INTO @c_area, @c_ce
704

705
WHILE @@Fetch_Status = 0
706

707
	BEGIN
708

709
	
710

711
		INSERT INTO @t_final
712

713
		SELECT DISTINCT 
714

715
				 EmployeeID,
716

717
				 PayPeriodMonth,
718

719
				 PayPeriodYear,
720

721
				 RunPeriodMonth,
722

723
				 RunPeriodYear,
724

725
				 WageType, 
726

727
				 WageTypeDesc,
728

729
				 CostElementDesc + ' - ' + CostCenterDesc,
730

731
				 CostElement,
732

733
				 GLAccount,
734

735
				 DEBET,
736

737
				 CostCenterAbbr,
738

739
				 EmployeeArea,
740

741
				 EmployeeAreaDesc,
742

743
				 CostCenter,
744

745
				 CostCenterDesc,
746

747
				 CostElementGroup,
748

749
				 CostElementGroupDesc,
750

751
				 EmployeeType,
752

753
				 EmployeeTypeDesc,
754

755
				 PeriodMonth,
756

757
				 CostElementTotal,
758

759
				 CostElementAbbr
760

761
		FROM @tbl_result WHERE CostCenter = @c_area AND CostElement = @c_ce
762

763
	
764

765
		INSERT INTO @t_final
766

767
		SELECT DISTINCT a.* FROM @tbl_result AS a
768

769
		WHERE a.CostCenter <> @c_area 
770

771
			AND a.CostElement = @c_ce
772

773
			AND a.CostCenter NOT IN ( SELECT DISTINCT EmployeeArea FROM @t_ce_area)
774

775

776

777
			
778

779
		--	--AND a.employee_id NOT IN (SELECT DISTINCT employee_id FROM @tbl_result)
780

781
		
782

783
	FETCH cur_ce INTO @c_area, @c_ce   
784

785
	END
786

787
CLOSE cur_ce
788

789
DEALLOCATE cur_ce
790

791

792

793
SELECT DISTINCT * FROM @t_final
794

795
WHERE CostElementGroupDesc <> 'C. Take Home Pay'		------//------ Add by Tri nwh 20220322 untuk perubahan template
796

797

798

799

800

801
----SELECT tr.* 
802

803
----	,dbo.fn_formatdatetime(@run_period + '01', 'mmmm') AS period_month
804

805
----	,CASE WHEN cost_element_group_id = '00000003' THEN tr.DEBET ELSE tr.DEBET * -1 END AS ce_total
806

807
----FROM @tbl_result AS tr
808

809

810

811

812

813

814

815
	
816

817

(2-2/3)