Project

General

Profile

Bug #4099 » SPTBulanan_Bupot_Resign_Coretax_20251219.sql

Tri Rizqiaty, 12/19/2025 01:49 PM

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

3
	@landscape varchar(3),
4

5
	@company_code varchar(4),
6

7
	@year varchar(4),
8

9
	@tax_office varchar(8),
10

11
	@employee_status varchar(2) = null,
12

13
	@payroll_group varchar(2) = null,
14

15
	@emp_id_beg varchar(8) = null,
16

17
	@emp_id_end varchar(8) = NULL
18

19
AS
20

21

22

23
--DECLARE @landscape varchar(3) = '100'
24

25
--DECLARE @company_code varchar(4) = '1000'
26

27
--DECLARE @year varchar(4) = '2025'
28

29
--DECLARE @tax_office varchar(8) = ''
30

31
--DECLARE @employee_status varchar(2) = ''
32

33
--DECLARE @payroll_group varchar(2) = '13'
34

35
--DECLARE @emp_id_beg varchar(8) = '20241073'
36

37
--DECLARE @emp_id_end varchar(8) = '20241073'
38

39

40

41
-- Extract year and month from @nowperiod
42

43
DECLARE @nowmonth varchar (2) = dbo.fn_formatdatetime(GETDATE(),'mm')
44

45
DECLARE @nowperiod varchar (6) = dbo.fn_formatdatetime(GETDATE(),'yyyymm')
46

47

48

49
-- Calculate the next month
50

51
DECLARE @years INT = CAST(SUBSTRING(@nowperiod, 1, 4) AS INT);
52

53
DECLARE @months INT = CAST(SUBSTRING(@nowperiod, 5, 2) AS INT);
54

55
SET @months = @months + 1;
56

57
DECLARE @next_month VARCHAR(6) = CAST(@year AS VARCHAR(4)) + RIGHT('0' + CAST(@months AS VARCHAR(2)), 2);
58

59

60

61
-- Temporary tables for final SELECT
62

63
select distinct emp_subarea into #emp_subarea from base_cust_ref_emp_office with (nolock) where tax_office=@tax_office
64

65
select distinct emp_id,start_date,end_date,npwp,npwp_date,tax_status into #md3 from hr_md_emp_md0003 with (nolock) where end_date='99991231'
66

67
select distinct emp_id,start_date,end_date,full_name,ktp,gender into #md1 from hr_md_emp_md0001 with (nolock) where end_date='99991231'
68

69
select distinct emp_id,start_date,end_date,address_type,street into #md6 from hr_md_emp_md0006 with (nolock) where end_date='99991231' and address_type='01'
70

71
select distinct emp_id,hiring_date into #md15 from hr_md_emp_md0015 with (nolock)
72

73

74

75

76

77
-- Result
78

79
select distinct
80

81
	emp_id
82

83
	,start_date
84

85
	,end_date
86

87
	,position
88

89
	,employee_type
90

91
	,employee_status
92

93
	,employee_office
94

95
	,movement_type
96

97
into #result
98

99
from hr_md_emp_md0002 with (nolock) 
100

101
where 
102

103
	 @year+'1231' between start_date and end_date
104

105
	and (payroll_group=@payroll_group or @payroll_group='')
106

107
	 and (emp_id>=@emp_id_beg or @emp_id_beg='')
108

109
	and (emp_id<=@emp_id_end or @emp_id_end='')
110

111
	--and (employee_office in (select emp_subarea from #emp_subarea) or @tax_office='')
112

113
	and ((employee_status in ('03','04') and left(start_date,4)=@year) or (employee_status in ('01','02')))
114

115

116

117
	----select * from  #result
118

119

120

121
-- Result_
122

123
select distinct 
124

125
r.emp_id
126

127
,r.employee_status
128

129
,case 
130

131
	when left(min(md2.start_date),4)=@year then right(left(min(md2.start_date),6),2) -- Employee Aktif
132

133
	when  left(min(md2.start_date),4)=@year AND r.employee_status IN ('03', '04') THEN
134

135
	CASE
136

137
			WHEN RIGHT(min(md2.start_date), 1) = '1' THEN 
138

139
				-- Subtract 1 from the month, ensuring it doesn't go below 01
140

141
				RIGHT(CONCAT('0', 
142

143
					CASE 
144

145
						WHEN CAST(RIGHT(LEFT(min(md2.start_date), 6), 2) AS INT) - 1 < 1 
146

147
						THEN 1 
148

149
						ELSE CAST(RIGHT(LEFT(min(md2.start_date), 6), 2) AS INT) - 1 
150

151
					END
152

153
				), 2) -- Employee Resign di awal bulan
154

155
			ELSE RIGHT(LEFT(min(md2.start_date), 6), 2) -- Employee Resign di tengah bulan
156

157
		END
158

159
	else '01'
160

161
end as start_date
162

163
,CASE 
164

165
	WHEN LEFT(MAX(md2.end_date), 4) = @year THEN RIGHT(LEFT(MAX(md2.end_date), 6), 2) -- Employee Aktif
166

167
	WHEN LEFT(MAX(md2.start_date), 4) = @year AND r.employee_status IN ('03', '04') THEN 
168

169
		CASE
170

171
			WHEN RIGHT(MAX(md2.start_date), 1) = '1' THEN 
172

173
				-- Subtract 1 from the month, ensuring it doesn't go below 01
174

175
				RIGHT(CONCAT('0', 
176

177
					CASE 
178

179
						WHEN CAST(RIGHT(LEFT(MAX(md2.start_date), 6), 2) AS INT) - 1 < 1 
180

181
						THEN 1 
182

183
						ELSE CAST(RIGHT(LEFT(MAX(md2.start_date), 6), 2) AS INT) - 1 
184

185
					END
186

187
				), 2) -- Employee Resign di awal bulan
188

189
			ELSE RIGHT(LEFT(MAX(md2.start_date), 6), 2) -- Employee Resign di tengah bulan
190

191
		END
192

193
	ELSE '12'
194

195
END as end_date
196

197
,office.tax_office AS employee_office
198

199
,r.movement_type
200

201
into #result_
202

203
from #result r
204

205
left join hr_md_emp_md0002 md2 on r.emp_id=md2.emp_id
206

207
LEFT JOIN base_cust_ref_emp_office as office
208

209
	ON md2.employee_office = office.emp_subarea
210

211
where 
212

213
@year between left(md2.start_date,4) and left(md2.end_date,4)
214

215
group by r.emp_id,office.tax_office,r.employee_status,r.movement_type
216

217

218

219
--	--select * from #result_ where emp_id='20231441'
220

221

222

223
--Result_2
224

225
SELECT 
226

227
    emp_id,
228

229
    start_date,
230

231
    end_date,
232

233
	employee_status,
234

235
    employee_office,
236

237
	CASE 
238

239
		WHEN movement_type='54' THEN 'Annualized'
240

241
		WHEN employee_status in('04','03','02') THEN 'Partial Year'
242

243
	ELSE 'Full Year'
244

245
	END AS status_bukti_potong,
246

247
    -- Calculate the number of months between start_date and end_date
248

249
	CASE 
250

251
		WHEN employee_status in('04','03','02') THEN 0
252

253
		ELSE
254

255
			CASE 
256

257
			WHEN CAST(start_date AS INT) <= CAST(end_date AS INT) THEN 
258

259
				CAST(end_date AS INT) - CAST(start_date AS INT) + 1
260

261
			ELSE 
262

263
				(12 - CAST(start_date AS INT) + 1) + CAST(end_date AS INT)
264

265
		END
266

267
	END AS number_of_months
268

269
INTO #result_2
270

271
FROM 
272

273
    #result_;
274

275

276

277
	--select * from #result_2 where emp_id='20231441'
278

279

280

281

282

283
-- Tr301 Empoff
284

285
;WITH RankedData AS (
286

287
    SELECT 
288

289
        r_.emp_id,
290

291
        r_.start_date,
292

293
        r_.end_date,
294

295
        r_.employee_office,
296

297
        cum.wage_type,
298

299
        CONVERT(DECIMAL(18, 0), dbo.SDE(cum.amount, 'M!N0V@2010')) AS amount,
300

301
        cum.pay_period_month,
302

303
        DENSE_RANK() OVER (
304

305
            PARTITION BY r_.emp_id, r_.end_date 
306

307
            ORDER BY 
308

309
                CASE 
310

311
                    WHEN cum.pay_period_month = r_.end_date THEN 1 
312

313
                    ELSE 2 
314

315
                END,
316

317
                cum.pay_period_month DESC
318

319
        ) AS rn
320

321
    FROM 
322

323
        #result_ r_
324

325
    LEFT JOIN 
326

327
        hr_tr0301_cum cum 
328

329
        ON cum.employee_id = r_.emp_id 
330

331
           AND cum.pay_period_month = cum.run_period_month
332

333
           AND cum.pay_period_year = cum.run_period_year 
334

335
           AND cum.pay_period_year = @year
336

337
	WHERE cum.amount is not null
338

339
)
340

341
, CalculatedData AS (
342

343
    SELECT 
344

345
        emp_id,
346

347
        start_date,
348

349
        end_date,
350

351
        employee_office,
352

353
        wage_type,
354

355
        amount,
356

357
        pay_period_month,
358

359
        amount - LAG(amount, 1, 0) OVER (
360

361
            PARTITION BY emp_id, wage_type 
362

363
            ORDER BY pay_period_month
364

365
        ) AS amount_difference
366

367
    FROM 
368

369
        RankedData
370

371
    WHERE 
372

373
        rn = 1
374

375
)
376

377
SELECT DISTINCT
378

379
    emp_id,
380

381
    start_date,
382

383
    end_date,
384

385
    employee_office,
386

387
    wage_type,
388

389
    amount,
390

391
    pay_period_month,
392

393
    amount_difference
394

395
INTO #tr301_empoff
396

397
FROM 
398

399
    CalculatedData
400

401
ORDER BY 
402

403
    emp_id, 
404

405
    wage_type, 
406

407
    pay_period_month;
408

409

410

411
	--select * from #tr301_empoff
412

413

414

415
select distinct code into #a1 from hr_cu0300 where spt_no='1'
416

417
		select distinct code into #a2 from hr_cu0300 where spt_no='2'
418

419
		select distinct code into #a3 from hr_cu0300 where spt_no='3'
420

421
		select distinct code into #a4 from hr_cu0300 where spt_no='4'
422

423
		select distinct code into #a5 from hr_cu0300 where spt_no='5'
424

425
		select distinct code into #a6 from hr_cu0300 where spt_no='6'
426

427
		select distinct code into #a8 from hr_cu0300 where spt_no='8'
428

429
		select distinct code into #a10 from hr_cu0300 where spt_no='10'
430

431
		select distinct code into #a11 from hr_cu0300 where spt_no='11'
432

433
		select distinct code into #a12 from hr_cu0300 where spt_no='12'
434

435
		select distinct code into #a15 from hr_cu0300 where spt_no='15'
436

437
		select distinct code into #a20 from hr_cu0300 where spt_no='20'
438

439

440

441
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a1_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a1) group by r.emp_id,tr.employee_office
442

443
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a2_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a2) group by r.emp_id,tr.employee_office
444

445
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a3_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a3) group by r.emp_id,tr.employee_office
446

447
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a4_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a4) group by r.emp_id,tr.employee_office
448

449
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a5_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a5) group by r.emp_id,tr.employee_office
450

451
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a6_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a6) group by r.emp_id,tr.employee_office
452

453
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a8_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a8) group by r.emp_id,tr.employee_office
454

455
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a10_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a10) group by r.emp_id,tr.employee_office
456

457
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a11_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a11) group by r.emp_id,tr.employee_office
458

459
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a12_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a12) group by r.emp_id,tr.employee_office
460

461
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a15_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a15) group by r.emp_id,tr.employee_office
462

463
		select distinct r.emp_id , tr.employee_office,sum(tr.amount_difference) as amount into #a20_result from #result r inner join #tr301_empoff tr on r.emp_id=tr.emp_id and tr.wage_type in (select * from #a20) group by r.emp_id,tr.employee_office
464

465

466

467

468

469
-- Final SELECT statement
470

471
select distinct
472

473
    dbo.fn_formatdatetime(GETDATE(),'dd/mm/yyyy') AS tgl_pemotongan,
474

475
'No' as pemberi_kerja,
476

477
  @year as tahun,
478

479
    r.emp_id,
480

481
    md1.ktp as npwp_pegawai,
482

483
    CASE 
484

485
        WHEN md3.tax_status = 'TK' THEN 'TK'
486

487
        WHEN md3.tax_status = 'K0' OR md3.tax_status = 'K1' OR md3.tax_status = 'K2' OR md3.tax_status = 'K3' THEN 'K' 
488

489
        WHEN md3.tax_status = 'T1' OR md3.tax_status = 'T2' OR md3.tax_status = 'T3' THEN 'HB'	
490

491
    END AS status_ptkp,
492

493
    obj.description as nama_jabatan,
494

495
    'Tidak' as karyawan_asing,
496

497
    'IDN' as kode_negara,
498

499
	r2.status_bukti_potong,
500

501
	r2.number_of_months,
502

503
    emp_type.kode_pajak as kode_objek_pajak,
504

505
    r_.employee_status,
506

507
    CAST(r_.start_date AS INT) AS masa_penghasilan_awal,
508

509
    CAST(r_.end_date AS INT) AS masa_penghasilan_akhir,
510

511
	r_.end_date,
512

513
    case when md3.npwp is null then 'NIK' else 'NPWP' end as penandatanganan_penggunaan,
514

515
	'Resident' as Nationality,
516

517
	'' as no_passport,
518

519
    '0' as Zakat,
520

521
	'NO' as OpsiGrossUp,
522

523
    r_.employee_office,
524

525
	empoff.remarks,
526

527
    case when a1.amount is null then 0 else a1.amount end as a1,
528

529
    case when a2.amount is null then 0 else a2.amount end as a2,
530

531
    case when a3.amount is null then 0 else a3.amount end as a3,
532

533
    case when a4.amount is null then 0 else a4.amount end as a4,
534

535
    case when a5.amount is null then 0 else a5.amount end as a5,
536

537
    case when a6.amount is null then 0 else a6.amount end as a6,
538

539
    case when a8.amount is null then 0 else a8.amount end as a7,
540

541
    case when a12.amount is null then 0 else a12.amount end as a10,
542

543
    case when REPLACE(LTRIM(REPLACE(case when left(md15.hiring_date,4)=@year then right(left(md15.hiring_date,6),2) else '01' end, '0', ' ')), ' ', '0')>1 then case when a20.amount is null then 0 else a20.amount end else 0 end as a18,
544

545
    case 
546

547
        when tax_status ='K0' then 'K/0' 
548

549
        when tax_status ='K1' then 'K/1'
550

551
        when tax_status ='K2' then 'K/2'
552

553
        when tax_status ='K3' then 'K/3'
554

555
        when tax_status ='T1' then 'TK/1'
556

557
        when tax_status ='T2' then 'TK/2'
558

559
        when tax_status ='T3' then 'TK/3'
560

561
        when tax_status ='TK' then 'TK/0'
562

563
    end as kodeptkp,
564

565
    'N/A' as Fasilitas,
566

567
    '' as noFasilitas,
568

569
	0 AS NoBuktiPotongSebelum
570

571
from #result_ r_
572

573
left join #result r on r.emp_id=r_.emp_id 
574

575
left join #result_2 r2 on r.emp_id=r2.emp_id and r2.employee_office=r_.employee_office and r2.employee_status=r_.employee_status
576

577
left join #md3 md3 on r.emp_id=md3.emp_id
578

579
left join #md1 md1 on r.emp_id=md1.emp_id
580

581
left join #md6 md6 on r.emp_id=md6.emp_id
582

583
left join #md15 md15 on r.emp_id=md15.emp_id
584

585
left join hr_md_orm_object obj on r.position=obj.object and obj.end_date='99991231' and obj.class='P'
586

587
left join base_cust_ref_emp_type emp_type on r.employee_type= emp_type.emp_type
588

589
left join base_cust_ref_emp_office office on r.employee_office=office.emp_subarea
590

591
left join #a1_result a1 on a1.emp_id=r.emp_id and r_.employee_office=a1.employee_office
592

593
left join #a2_result a2 on a2.emp_id=r.emp_id and r_.employee_office=a2.employee_office
594

595
left join #a3_result a3 on a3.emp_id=r.emp_id and r_.employee_office=a3.employee_office
596

597
left join #a4_result a4 on a4.emp_id=r.emp_id and r_.employee_office=a4.employee_office
598

599
left join #a5_result a5 on a5.emp_id=r.emp_id and r_.employee_office=a5.employee_office
600

601
left join #a6_result a6 on a6.emp_id=r.emp_id and r_.employee_office=a6.employee_office
602

603
left join #a8_result a8 on a8.emp_id=r.emp_id and r_.employee_office=a8.employee_office
604

605
left join #a12_result a12 on a12.emp_id=r.emp_id and r_.employee_office=a12.employee_office
606

607
left join #a20_result a20 on a20.emp_id=r.emp_id and r_.employee_office=a20.employee_office
608

609
left join base_cust_ref_emp_office empoff on r_.employee_office= empoff.emp_subarea
610

611
where (r_.end_date<=@nowmonth or  @year< dbo.fn_formatdatetime(GETDATE(),'yyyy'))
612

613
order by r.emp_id
614

615

616

617
-- Drop temporary tables
618

619

620

621
drop table #result
622

623
drop table #result_
624

625
drop table #result_2
626

627
drop table #md1
628

629
drop table #md3
630

631
drop table #md6
632

633
drop table #md15
634

635
drop table #emp_subarea
636

637
drop table #a1
638

639
drop table #a2
640

641
drop table #a3
642

643
drop table #a4
644

645
drop table #a5
646

647
drop table #a6
648

649
drop table #a8
650

651
drop table #a10
652

653
drop table #a1_result
654

655
drop table #a2_result
656

657
drop table #a3_result
658

659
drop table #a4_result
660

661
drop table #a5_result
662

663
drop table #a6_result
664

665
drop table #a8_result
666

667
drop table #a10_result
668

669
drop table #a11
670

671
drop table #a11_result
672

673
drop table #a12
674

675
drop table #a12_result
676

677
drop table #a15
678

679
drop table #a15_result
680

681
drop table #a20
682

683
drop table #a20_result
684

685
drop table #tr301_empoff
(3-3/4)