Project

General

Profile

Bug #2916 » Rpt_IMFI_HR_PY_TaxComponent_detail_202407240958.sql

Tri Rizqiaty, 07/24/2024 10:42 AM

 
1
??USE [MinovaHR_Indomobil_Prod]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[Rpt_IMFI_HR_PY_TaxComponent_detail]    Script Date: 24/07/2024 09.58.01 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[Rpt_IMFI_HR_PY_TaxComponent_detail] 
16

17
	@landscape AS VARCHAR(3),
18

19
	@companycode AS VARCHAR(5),
20

21
	@paygroup AS VARCHAR(5),
22

23
	@payperiod AS VARCHAR(6),
24

25
	@tax_office VARCHAR(12),
26

27
	@emp_id varchar(12)
28

29
AS
30

31

32

33
--DECLARE @landscape AS VARCHAR(3) 
34

35
--DECLARE @companycode AS VARCHAR(5) 
36

37
--DECLARE @paygroup AS VARCHAR(5) 
38

39
--DECLARE @payperiod AS VARCHAR(6) 
40

41
--DECLARE @tax_office VARCHAR(12)
42

43
--DECLARE @emp_id VARCHAR(8)
44

45
--SET @landscape = '100'
46

47
--SET @companycode = '1000' 
48

49
--SET @paygroup = '11'
50

51
--SET @payperiod = '202403'
52

53
--SET @tax_office = ''
54

55
--SET @emp_id = '20000003'
56

57

58

59

60

61
DECLARE @companyname AS NVARCHAR(50)
62

63
DECLARE @companylogo AS VARBINARY(MAX)
64

65
DECLARE @payrollgroup AS NVARCHAR(50)
66

67
DECLARE @paymonth AS NVARCHAR(2)
68

69
DECLARE @payyear AS NVARCHAR(4)
70

71
DECLARE @begda AS NVARCHAR(8)
72

73
DECLARE @endda AS NVARCHAR(8)
74

75
DECLARE @enddate AS VARCHAR(8)
76

77
SET @paymonth = RIGHT(@payperiod,2)
78

79
SET @payyear = LEFT(@payperiod,4)
80

81
SET @begda = @payperiod + '01'
82

83
SET @endda = convert(NVARCHAR(8),dateadd(d,-1, dateadd(m, 1, @begda)), 112)
84

85

86

87
DECLARE @last_date AS varchar(12) 
88

89
DECLARE @last_year AS varchar(8)
90

91
DECLARE @last_month AS varchar(8)
92

93
SET @last_date = convert(VARCHAR(6), dateadd(d,0, dateadd(m,-1, @begda)), 112)
94

95
SET @last_year = LEFT(@last_date,4)
96

97
SET @last_month = RIGHT(@last_date,2)
98

99

100

101
-----//----- tabel-tabel internal
102

103
DECLARE @tbl_emp TABLE (
104

105
	emp_id varchar(8)
106

107
	,emp_name varchar(250)
108

109
	,[bank_id] [varchar](2) 
110

111
	,[bank_account] [varchar](50) 
112

113
	,[pay_grade] [varchar](8) 
114

115
	,[tax_status] [varchar](8) 
116

117
	,[npwp] [varchar](50) 
118

119
	,[jamsostek_type] [varchar](4) 
120

121
	,[jamsostek] [varchar](50) 
122

123
	,[jamsostek_level] [varchar](5)
124

125
	,[cost_center] [varchar](8) 
126

127
	,[employee_area] [varchar](4) 
128

129
	,[employee_office] [varchar](4) 
130

131
	,[employee_status] [varchar](8) 
132

133
	,[employee_type] [varchar](8) 
134

135
	,[employee_subtype] [varchar](8) 
136

137
	,[payroll_group] [varchar](8) 
138

139
	,[organization] [varchar](8) 
140

141
	,[position] [varchar](8) 
142

143
	,[job] [varchar](8) 
144

145
	,office_name varchar(250) 
146

147
	,pygroup varchar(10)
148

149
	,pygroup_desc varchar(250)
150

151
	,tax_desc varchar(250)
152

153
	,npwp_taxoffice varchar(50)
154

155
	)
156

157
	
158

159
DECLARE @tbl_py TABLE (
160

161
	emp_id varchar(8)
162

163
	,this_amount decimal(18,0)
164

165
	,wage_type VARCHAR(10)
166

167
	,wage_type_desc VARCHAR(100)
168

169
	,jum_emp DECIMAL(18,0)
170

171
	)
172

173

174

175
DECLARE @tbl_py_last TABLE (
176

177
	emp_id varchar(8)
178

179
	,last_amount decimal(18,0)
180

181
	,wage_type VARCHAR(10)
182

183
	,wage_type_desc VARCHAR(100)
184

185
	,jum_emp DECIMAL(18,0)
186

187
	)
188

189

190

191
DECLARE @tbl_tr_enc TABLE
192

193
	(landscape varchar (3),
194

195
	 employee_id varchar(8),
196

197
	 payPeriodMonth varchar(2),
198

199
	 payPeriodYear varchar(4),
200

201
	 runPeriodMonth varchar(2),
202

203
	 runPeriodYear varchar(4),
204

205
	 wage_type varchar(4),
206

207
	 amount varchar(250),
208

209
	 rate varchar(250),
210

211
	 flg_retro varchar(3),
212

213
	 flg_period VARCHAR(20),
214

215
	 wt_group VARCHAR(100))
216

217

218

219
DECLARE @tbl_tr_dec TABLE
220

221
	(landscape varchar (3),
222

223
	 employee_id varchar(8),
224

225
	 payPeriodMonth varchar(2),
226

227
	 payPeriodYear varchar(4),
228

229
	 runPeriodMonth varchar(2),
230

231
	 runPeriodYear varchar(4),
232

233
	 wage_type varchar(4),
234

235
	 amount decimal(18,0),
236

237
	 rate decimal(18,0),
238

239
	 flg_retro varchar(3),
240

241
	 flg_period VARCHAR(20),
242

243
	 wt_group VARCHAR(100))
244

245

246

247
DECLARE @tbl_tr_dec_sum TABLE
248

249
	(landscape varchar (3),
250

251
	 employee_id varchar(8),
252

253
	 payPeriodMonth varchar(2),
254

255
	 payPeriodYear varchar(4),
256

257
	 runPeriodMonth varchar(2),
258

259
	 runPeriodYear varchar(4),
260

261
	 wage_type varchar(4),
262

263
	 amount decimal(18,0),
264

265
	 rate decimal(18,0),
266

267
	 flg_retro varchar(1),
268

269
	 flg_period VARCHAR(20),
270

271
	 wt_group VARCHAR(100))
272

273
	 
274

275
DECLARE @t_paygroup TABLE
276

277
(
278

279
	paygroup VARCHAR(5)
280

281
)
282

283

284

285
DECLARE @t_wt TABLE
286

287
(
288

289
	wt VARCHAR(8),
290

291
	wt_group VARCHAR(100)
292

293
)
294

295

296

297
SELECT @companyname = description
298

299
	 , @companylogo = logo
300

301
FROM
302

303
	base_cust_ref_companycode
304

305
WHERE
306

307
	(landscape = @landscape)
308

309
	AND (companycode = @companycode)
310

311

312

313
SELECT @payrollgroup = description
314

315
FROM
316

317
	base_cust_ref_payroll_group
318

319
WHERE
320

321
	(landscape = @landscape)
322

323
	AND (payroll_group = @paygroup)
324

325

326

327
-----//----- get wt
328

329
INSERT INTO @t_wt
330

331
SELECT val1, val2 FROM base_cust_parameter WHERE param = 'RPT_IMFI_TAX_COMPONENT'
332

333

334

335
-----//----- pengambilan data employee
336

337
INSERT INTO @tbl_emp 	
338

339
SELECT  DISTINCT tr300.employee_id
340

341
		,md3.no_tht--,md1.full_name
342

343
		--,CASE WHEN md3.bank_account_name <> '' THEN md3.bank_account_name ELSE md1.full_name END
344

345
		,md3.[bank_id]
346

347
		,md3.[bank_account]
348

349
		,md3.[pay_grade]
350

351
		,md3.[tax_status]
352

353
		,md3.[npwp]
354

355
		,md3.[jamsostek_type]
356

357
		,md3.[jamsostek]
358

359
		,md3.[jamsostek_level]
360

361
		,tr300.[cost_center]
362

363
		,tr300.[employee_area]
364

365
		,tr300.[employee_office]
366

367
		,tr300.[employee_status]
368

369
		,tr300.[employee_type]
370

371
		,tr300.[employee_subtype]
372

373
		,tr300.[payroll_group]
374

375
		,tr300.[organization]
376

377
		,tr300.[position]
378

379
		,tr300.[job]
380

381
		,office.emp_subarea_description
382

383
		,md2.payroll_group
384

385
		,pyg.description
386

387
		,tax.description
388

389
		,office.npwp
390

391
FROM hr_tr0300 AS tr300
392

393
LEFT JOIN hr_md_emp_md0001 AS md1
394

395
	ON tr300.landscape = md1.landscape
396

397
	AND tr300.employee_id = md1.emp_id 
398

399
	AND md1.start_date <= @endda AND md1.end_date >= @endda
400

401
LEFT JOIN hr_md_emp_md0002 AS md2
402

403
	ON md2.landscape = tr300.landscape
404

405
	AND md2.emp_id = tr300.employee_id
406

407
	AND md2.start_date < = @endda AND md2.end_date >= @endda	 
408

409
LEFT JOIN hr_md_emp_md0003 AS md3
410

411
	ON tr300.landscape = md3.landscape
412

413
	AND tr300.employee_id = md3.emp_id 
414

415
	AND md3.start_date <= @endda AND md3.end_date >= @endda 
416

417
LEFT JOIN base_cust_ref_emp_office AS office
418

419
	ON office.emp_subarea = tr300.employee_office
420

421
	AND office.landscape = tr300.landscape 
422

423
LEFT JOIN base_cust_ref_tax_office AS tax
424

425
	ON tax.landscape = md2.landscape
426

427
	AND tax.tax_office = office.tax_office
428

429
LEFT JOIN dbo.base_cust_ref_payroll_group AS pyg
430

431
	ON pyg.landscape = md2.landscape
432

433
	AND pyg.payroll_group = md2.payroll_group
434

435
WHERE tr300.landscape = @landscape
436

437
	AND tr300.payroll_group = @paygroup 
438

439
	AND tr300.pay_period_month = @paymonth AND tr300.pay_period_year = @payyear
440

441
	AND tr300.run_period_month = @paymonth AND tr300.run_period_year = @payyear 
442

443
	AND (tr300.company_id = @companycode OR @companycode = '')
444

445
	AND (office.tax_office = @tax_office OR @tax_office = '')
446

447
	AND (tr300.employee_id = @emp_id OR @emp_id = '')
448

449

450

451
--------------- get data payroll --------------
452

453
INSERT INTO @tbl_tr_enc
454

455
SELECT  pr.landscape , pr.employee_id, 
456

457
		pr.pay_period_month, pr.pay_period_year, 
458

459
	    pr.run_period_month, pr.run_period_year, 
460

461
	    pr.wage_type, pr.amount, pr.rate, 'N', 'now'
462

463
	    ,wt.wt_group
464

465
FROM hr_tr0301 pr  WITH (NOLOCK)
466

467
	INNER JOIN @tbl_emp emp
468

469
		ON pr.employee_id = emp.emp_id
470

471
	INNER JOIN @t_wt wt
472

473
		ON pr.wage_type = wt.wt
474

475
WHERE pr.pay_period_month = @paymonth AND pr.pay_period_year = @payyear 
476

477
	AND pr.run_period_month = @paymonth AND pr.run_period_year = @payyear 
478

479
	--AND pr.wage_type in (SELECT * FROM base_cust_parameter WHERE PARAM = 'RPT_IMFI_TAX_COMPONENT')
480

481
	
482

483
INSERT INTO @tbl_tr_enc
484

485
SELECT	pr.landscape, pr.employee_id, 
486

487
		pr.pay_period_month, pr.pay_period_year, 
488

489
		pr.run_period_month, pr.run_period_year, 
490

491
		pr.wage_type, pr.amount, pr.rate, 'R', 'now'
492

493
		,wt.wt_group
494

495
FROM hr_tr0301_retro pr  WITH (NOLOCK)
496

497
	INNER JOIN @tbl_emp emp
498

499
		ON pr.employee_id = emp.emp_id
500

501
	INNER JOIN @t_wt wt
502

503
		ON pr.wage_type = wt.wt
504

505
WHERE pr.pay_period_month = @paymonth AND pr.pay_period_year = @payyear 
506

507
	AND pr.run_period_month = @paymonth AND pr.run_period_year = @payyear 
508

509
	--AND (pr.wage_type NOT IN ('MT','5000','5001'))
510

511
	--AND pr.wage_type in ('1000','1300','1202','1221','1222','1223','1224','1225','1226','1227','1228','1229','1304','1200','1201','1301','1203','1210','1212','1213','1302','1500','1205','1231','1232','1233','1234','1235','1303','1501','1504','2000','2001','2002','2003','1502','6000','6200','6202','6203','6204','6205','6206','6207','6500','6501','6502','6503','6504','1503','3000','3100','3200','5000','5001','JER3','JER2','TD','TI','MT','TP') ----------- tambahin wage_type apa aja ya---------
512

513

514

515
INSERT INTO @tbl_tr_enc
516

517
SELECT  pr.landscape , pr.employee_id, 
518

519
		pr.pay_period_month, pr.pay_period_year, 
520

521
	    pr.run_period_month, pr.run_period_year, 
522

523
	    pr.wage_type, pr.amount, pr.rate, 'N', 'last'
524

525
	    ,wt.wt_group
526

527
FROM hr_tr0301 pr  WITH (NOLOCK)
528

529
	INNER JOIN @tbl_emp emp
530

531
		ON pr.employee_id = emp.emp_id
532

533
	INNER JOIN @t_wt wt
534

535
		ON pr.wage_type = wt.wt
536

537
WHERE pr.pay_period_month = @last_month AND pr.pay_period_year = @last_year 
538

539
	AND pr.run_period_month = @last_month AND pr.run_period_year = @last_year
540

541
	--AND pr.wage_type in ('1000','1300','1202','1221','1222','1223','1224','1225','1226','1227','1228','1229','1304','1200','1201','1301','1203','1210','1212','1213','1302','1500','1205','1231','1232','1233','1234','1235','1303','1501','1504','2000','2001','2002','2003','1502','6000','6200','6202','6203','6204','6205','6206','6207','6500','6501','6502','6503','6504','1503','3000','3100','3200','5000','5001','JER3','JER2','TD','TI','MT','TP') ----------- tambahin wage_type apa aja ya---------
542

543
	
544

545
INSERT INTO @tbl_tr_enc
546

547
SELECT	pr.landscape, pr.employee_id, 
548

549
		pr.pay_period_month, pr.pay_period_year, 
550

551
		pr.run_period_month, pr.run_period_year, 
552

553
		pr.wage_type, pr.amount, pr.rate, 'R', 'last'
554

555
		,wt.wt_group
556

557
FROM hr_tr0301_retro pr  WITH (NOLOCK)
558

559
	INNER JOIN @tbl_emp emp
560

561
		ON pr.employee_id = emp.emp_id
562

563
	INNER JOIN @t_wt wt
564

565
		ON pr.wage_type = wt.wt
566

567
WHERE pr.pay_period_month = @last_month AND pr.pay_period_year = @last_year 
568

569
	AND pr.run_period_month = @last_month AND pr.run_period_year = @last_year
570

571
	--AND (pr.wage_type NOT IN ('MT','5000','5001'))
572

573
	--AND pr.wage_type in ('1000','1300','1202','1221','1222','1223','1224','1225','1226','1227','1228','1229','1304','1200','1201','1301','1203','1210','1212','1213','1302','1500','1205','1231','1232','1233','1234','1235','1303','1501','1504','2000','2001','2002','2003','1502','6000','6200','6202','6203','6204','6205','6206','6207','6500','6501','6502','6503','6504','1503','3000','3100','3200','5000','5001','JER3','JER2','TD','TI','MT','TP') ----------- tambahin wage_type apa aja ya---------
574

575
	 
576

577
INSERT INTO @tbl_tr_dec
578

579
SELECT pr.landscape, pr.employee_id, 
580

581
	   pr.payPeriodMonth, pr.payPeriodYear, 
582

583
	   pr.runPeriodMonth, pr.runPeriodYear, pr.wage_type,
584

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

587
	   CONVERT(decimal(18,0), dbo.SDE(pr.rate, 'M!N0V@2010')),  pr.flg_retro, pr.flg_period
588

589
	   ,wt_group
590

591
FROM @tbl_tr_enc pr
592

593

594

595
INSERT INTO @tbl_tr_dec_sum
596

597
SELECT tbl_tr_dec.landscape, employee_id,
598

599
	   payPeriodMonth, payPeriodYear,
600

601
	   runPeriodMonth, runPeriodYear, wage_type,
602

603
	   SUM(amount)AS amount,
604

605
	   SUM(rate) AS rate,
606

607
	   min(flg_retro) AS flg_retro, flg_period
608

609
	   ,wt_group
610

611
FROM @tbl_tr_dec AS tbl_tr_dec
612

613
GROUP BY tbl_tr_dec.landscape, employee_id, 
614

615
		 payPeriodMonth, payPeriodYear,
616

617
		 runPeriodMonth,runPeriodYear, wage_type, flg_period
618

619
		 ,wt_group
620

621

622

623

624

625
-----//----- data payroll bulan ini
626

627
INSERT INTO @tbl_py 
628

629
SELECT  tbl_tr_dec_sum.employee_id
630

631
		,tbl_tr_dec_sum.amount 'amount'
632

633
		,tbl_tr_dec_sum.wage_type
634

635
		,wt_group
636

637
		,1
638

639
FROM @tbl_tr_dec_sum AS tbl_tr_dec_sum 
640

641
WHERE flg_period = 'now'
642

643

644

645
-----//------ pengambilan data bulan lalu
646

647
INSERT INTO @tbl_py_last  
648

649
SELECT  tbl_tr_dec_sum.employee_id
650

651
		,tbl_tr_dec_sum.amount 'amount'
652

653
		,tbl_tr_dec_sum.wage_type
654

655
		,wt_group
656

657
		,1
658

659
FROM @tbl_tr_dec_sum AS tbl_tr_dec_sum 
660

661
WHERE flg_period = 'last'
662

663

664

665
--SELECT DISTINCT tbl_emp.employee_office 
666

667
--			,tbl_emp.office_name 
668

669
--			,tbl_emp.tax_desc
670

671
--			,isnull(SUM(tbl_py.this_amount), 0) AS this_amount
672

673
--			,tbl_py.wage_type_desc
674

675
--			,tbl_py.jum_emp
676

677
--			--,SUM(tbl_py.jum_emp) AS jum_emp
678

679
--FROM @tbl_emp AS tbl_emp
680

681
--INNER JOIN @tbl_py AS tbl_py
682

683
--	ON tbl_py.emp_id = tbl_emp.emp_id  
684

685
--WHERE tbl_py.this_amount > 0
686

687
--GROUP BY tbl_emp.employee_office
688

689
--		,tbl_emp.office_name 
690

691
--		,tbl_emp.tax_desc
692

693
--		,tbl_py.wage_type_desc
694

695
--		,tbl_py.jum_emp
696

697

698

699
--SELECT DISTINCT tbl_emp.emp_id
700

701
--			,tbl_emp.employee_office 
702

703
--			,tbl_emp.office_name 
704

705
--			,tbl_emp.tax_desc
706

707
--			,isnull(tbl_py.this_amount, 0) AS this_amount
708

709
--			,tbl_py.wage_type_desc
710

711
--			,jum.jum_emp
712

713
--	FROM @tbl_emp AS tbl_emp
714

715
--	INNER JOIN @tbl_py AS tbl_py
716

717
--		ON tbl_py.emp_id = tbl_emp.emp_id 
718

719
--	LEFT JOIN ( 
720

721
--				SELECT tbl_emp.employee_office 
722

723
--						,tbl_emp.office_name 
724

725
--						,tbl_emp.tax_desc
726

727
--						,COUNT(tbl_emp.emp_id) AS jum_emp 
728

729
--				FROM @tbl_emp AS tbl_emp
730

731
--				GROUP BY tbl_emp.employee_office 
732

733
--						,tbl_emp.office_name 
734

735
--						,tbl_emp.tax_desc
736

737
--			  ) AS jum
738

739
--		ON tbl_emp.employee_office  = jum.employee_office 
740

741
--		AND tbl_emp.tax_desc = jum.tax_desc
742

743
--	WHERE tbl_py.this_amount > 0
744

745

746

747
---//----- data result 
748

749
SELECT * FROM 
750

751
(
752

753
SELECT DISTINCT  tbl_emp.emp_id , tbl_emp.emp_name,
754

755
tbl_emp.employee_office 
756

757
			,tbl_emp.office_name 
758

759
			,tbl_emp.tax_desc
760

761
			,tbl_emp.npwp_taxoffice
762

763
			,isnull(SUM(tbl_py.this_amount), 0) AS this_amount
764

765
			,tbl_py.wage_type_desc
766

767
			,jum.jum_emp
768

769
	FROM @tbl_emp AS tbl_emp
770

771
	INNER JOIN @tbl_py AS tbl_py
772

773
		ON tbl_py.emp_id = tbl_emp.emp_id 
774

775
	LEFT JOIN ( 
776

777
				SELECT tbl_emp.employee_office 
778

779
						,tbl_emp.office_name 
780

781
						,tbl_emp.tax_desc
782

783
						,COUNT(tbl_emp.emp_id) AS jum_emp 
784

785
				FROM @tbl_emp AS tbl_emp
786

787
				GROUP BY tbl_emp.employee_office 
788

789
						,tbl_emp.office_name 
790

791
						,tbl_emp.tax_desc
792

793
			  ) AS jum
794

795
		ON tbl_emp.employee_office  = jum.employee_office 
796

797
		AND tbl_emp.tax_desc = jum.tax_desc
798

799
	--WHERE tbl_py.this_amount > 0
800

801
	GROUP BY tbl_emp.emp_id , tbl_emp.emp_name,
802

803
tbl_emp.employee_office 
804

805
			,tbl_emp.office_name, tbl_emp.tax_desc
806

807
		,tbl_py.wage_type_desc
808

809
		,jum.jum_emp
810

811
		,tbl_emp.npwp_taxoffice
812

813
) AS SourceTable
814

815
PIVOT
816

817
(
818

819
	SUM (this_amount)
820

821
FOR wage_type_desc IN
822

823
(	[Basic Salary], 
824

825
	[HP Allowance],
826

827
	[Meal Allowance],
828

829
	[Non Taxable Income],
830

831
	[Rapel/Other Allowance],
832

833
	[Tax],
834

835
	[Tax Allowance],
836

837
	[Total Allowance],
838

839
	[Total Income],
840

841
	[Total Tax Component],
842

843
	[Transport Allowance],
844

845
	[Position Allowance],
846

847
	[Insentif Allowance],
848

849
	[Medical],
850

851
	[THR/Bonus],
852

853
	[Total Deduction],
854

855
	[Overtime],
856

857
	[JKK/JKM],
858

859
	[Tax Paid]
860

861
	
862

863
)
864

865
) AS pvt
866

867

(5-5/5)