Project

General

Profile

Feature #2116 » PRPTPYSPT1721BULANAN.sql

Tri Rizqiaty, 04/14/2023 04:06 PM

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

3
    @CompanyID VARCHAR(4) = NULL ,
4

5
    @Year VARCHAR(6) ,
6

7
    @TaxOffice VARCHAR(10) ,
8

9
    @EmployeeStatus VARCHAR(2) = NULL ,
10

11
    @PayrollGroup VARCHAR(2) = NULL ,
12

13
    @EmployeeIDFrom VARCHAR(8) = NULL ,
14

15
    @EmployeeIDTo VARCHAR(8) = NULL
16

17
AS 
18

19
    SET NOCOUNT ON;
20

21

22

23
--DECLARE @CompanyID varchar(4) = ''
24

25
--DECLARE @Year varchar(6) = '202201'
26

27
--DECLARE @TaxOffice varchar(8) = 'CKG'
28

29
--DECLARE @EmployeeStatus varchar(2) = null
30

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

33
--DECLARE @EmployeeIDFrom varchar(8) = '00000351'--'00000300'
34

35
--DECLARE @EmployeeIDTo varchar(8) = '00000351'
36

37

38

39

40

41
    DECLARE @begda INT
42

43
    DECLARE @endda INT
44

45
    DECLARE @employee_status_beg VARCHAR(5)
46

47
    DECLARE @employee_status_end VARCHAR(5)
48

49
    DECLARE @payroll_group_beg VARCHAR(5)
50

51
    DECLARE @payroll_group_END VARCHAR(5)
52

53

54

55
--//Set begin & end of period
56

57
	SET @begda = @Year + '01'
58

59
	SET @endda = CONVERT(NVARCHAR(8), DATEADD(d, -1,DATEADD(m, 1,CONVERT(VARCHAR(8),@begda))), 112)
60

61
    --SET @begda = @Year + '0101'
62

63
    --SET @endda = @Year + '1231'
64

65

66

67
--//Set employee status
68

69
    IF @EmployeeStatus = ''
70

71
        OR @EmployeeStatus IS NULL 
72

73
        BEGIN
74

75
            SET @employee_status_beg = '00000'
76

77
            SET @employee_status_end = '99999'
78

79
        END
80

81
    ELSE 
82

83
        BEGIN
84

85
            SET @employee_status_beg = @EmployeeStatus
86

87
            SET @employee_status_end = @EmployeeStatus
88

89
        END
90

91

92

93
--//Set employee group
94

95
    IF @PayrollGroup = ''
96

97
        OR @PayrollGroup IS NULL 
98

99
        BEGIN
100

101
            SET @payroll_group_beg = '00000'
102

103
            SET @payroll_group_END = '99999'
104

105
        END
106

107
    ELSE 
108

109
        BEGIN
110

111
            SET @payroll_group_beg = @PayrollGroup
112

113
            SET @payroll_group_END = @PayrollGroup
114

115
        END
116

117

118

119

120

121
--//Set employee id start
122

123
    IF @EmployeeIDFrom = ''
124

125
        OR @EmployeeIDFrom IS NULL 
126

127
        BEGIN
128

129
            SET @EmployeeIDFrom = '00000000'
130

131
            SET @EmployeeIDTo = '99999999'
132

133
        END
134

135

136

137
    IF @EmployeeIDTo = ''
138

139
        OR @EmployeeIDTo IS NULL 
140

141
        SET @EmployeeIDTo = @EmployeeIDFrom 
142

143

144

145
--//Get system parameter
146

147
    DECLARE @is_encrypted VARCHAR
148

149
    DECLARE @npwp_address_subtype VARCHAR(4)
150

151

152

153
    SELECT  @is_encrypted = Value1
154

155
    FROM    dbo.PCMEPGENPARAM
156

157
    WHERE   Parameter = 'HR_PY_ENCRYPTED'
158

159
            AND StartDate <= @endda
160

161
            AND EndDate >= @endda
162

163

164

165
    SELECT  @npwp_address_subtype = Value1
166

167
    FROM    dbo.PCMEPGENPARAM
168

169
    WHERE   Parameter = 'HR_PY_NPWP_ADDRESS_SUBTYPE'
170

171
            AND StartDate <= @endda
172

173
            AND EndDate >= @endda
174

175

176

177
-----// Get employee tax office list
178

179
    DECLARE @emp_office TABLE
180

181
        (
182

183
          [StartDate] [varchar](8) NOT NULL ,
184

185
          [EndDate] [varchar](8) NOT NULL ,
186

187
          [EmployeeOffice] [varchar](4) NOT NULL ,
188

189
          [EmployeeOfficeDesc] [varchar](50) NULL ,
190

191
          [EmployeeArea] [varchar](4) NOT NULL ,
192

193
          [TaxOffice] [varchar](4) NULL ,
194

195
          [NPWP] [varchar](50) NULL ,
196

197
          [Address] [varchar](500) NULL ,
198

199
          [EmployeeOfficeGroup] [varchar](5) NULL ,
200

201
          [MinimumWageRegional] [varchar](4) NULL ,
202

203
          [Notes] [varchar](500) NULL ,
204

205
          [CreateBy] [varchar](18) NULL ,
206

207
          [CreateDate] [varchar](14) NULL ,
208

209
          [ChangeBy] [varchar](18) NULL ,
210

211
          [ChangeDate] [varchar](14) NULL
212

213
        )
214

215

216

217
    INSERT  INTO @emp_office
218

219
            SELECT  [StartDate] ,
220

221
                    [EndDate] ,
222

223
                    [EmployeeOffice] ,
224

225
                    [EmployeeOfficeDesc] ,
226

227
                    [EmployeeArea] ,
228

229
                    [TaxOffice] ,
230

231
                    [NPWP] ,
232

233
                    [Address] ,
234

235
                    [EmployeeOfficeGroup] ,
236

237
                    [MinimumWageRegional] ,
238

239
                    [Notes] ,
240

241
                    [CreateBy] ,
242

243
                    [CreateDate] ,
244

245
                    [ChangeBy] ,
246

247
                    [ChangeDate]
248

249
            FROM    dbo.PCMEPEMPOFF
250

251
            WHERE   TaxOffice = @TaxOffice
252

253
	
254

255
--Get Employee Tobe Processed
256

257
    DECLARE @employee_tobe_process2 TABLE
258

259
        (
260

261
          emp_id VARCHAR(8) ,
262

263
          start_date INT ,
264

265
          end_date INT ,
266

267
          employee_status VARCHAR(2)
268

269
        )
270

271

272

273
    DECLARE @employee_tobe_process TABLE
274

275
        (
276

277
          emp_id VARCHAR(8) ,
278

279
          start_date INT ,
280

281
          end_date INT
282

283
        )
284

285

286

287
    DECLARE @payroll_header TABLE
288

289
        (
290

291
          employee_id VARCHAR(8) ,
292

293
          start_date VARCHAR(8) ,
294

295
          end_date VARCHAR(8) ,
296

297
          employee_office VARCHAR(4) ,
298

299
          run_period_month VARCHAR(2)
300

301
        )
302

303
    DECLARE @payroll_header_tmp TABLE
304

305
        (
306

307
          employee_id VARCHAR(8) ,
308

309
          start_date VARCHAR(8) ,
310

311
          end_date VARCHAR(8) ,
312

313
          employee_office VARCHAR(4) ,
314

315
          run_period_month VARCHAR(2)
316

317
        )
318

319
    DECLARE @payroll_header0 TABLE
320

321
        (
322

323
          employee_id VARCHAR(8) ,
324

325
          start_date VARCHAR(8) ,
326

327
          end_date VARCHAR(8) ,
328

329
          employee_office VARCHAR(4) ,
330

331
          run_period_month VARCHAR(2)
332

333
        )
334

335

336

337
    INSERT  INTO @payroll_header_tmp
338

339
            SELECT DISTINCT
340

341
                    md2.EmployeeID ,
342

343
                    ph.StartDate ,
344

345
                    ph.EndDate ,
346

347
                    md2.EmployeeOffice ,
348

349
                    RunPeriodMonth
350

351
            FROM    dbo.PHRPYTR0300 ph
352

353
                    INNER JOIN dbo.PHRPA0002 md2 ON md2.EmployeeID = ph.EmployeeID
354

355
            WHERE   ph.StartDate >= @begda
356

357
                    AND ph.EndDate <= @endda
358

359
                    AND md2.StartDate <= ph.EndDate
360

361
                    AND md2.EndDate >= ph.EndDate
362

363
                    AND md2.EmployeeID BETWEEN @EmployeeIDFrom
364

365
                                       AND     @EmployeeIDTo
366

367
				
368

369
--//get the latest run data
370

371
    INSERT  INTO @payroll_header0
372

373
            SELECT  py_head.*
374

375
            FROM    @payroll_header_tmp AS py_head
376

377
                    INNER JOIN ( SELECT employee_id ,
378

379
                                        start_date ,
380

381
                                        end_date ,
382

383
                                        MAX(run_period_month) AS run_period_month
384

385
                                 FROM   @payroll_header_tmp
386

387
                                 GROUP BY employee_id ,
388

389
                                        start_date ,
390

391
                                        end_date
392

393
                               ) a ON py_head.employee_id = a.employee_id
394

395
                                      AND py_head.start_date = a.start_date
396

397
                                      AND py_head.end_date = a.end_date
398

399
                                      AND py_head.run_period_month = a.run_period_month
400

401
			
402

403
    INSERT  INTO @payroll_header
404

405
            SELECT DISTINCT
406

407
                    employee_id ,
408

409
                    start_date ,
410

411
                    end_date ,
412

413
                    employee_office ,
414

415
                    run_period_month
416

417
            FROM    @payroll_header0 ph0
418

419
                    INNER JOIN @emp_office eo ON ph0.employee_office = eo.EmployeeOffice
420

421
					
422

423
----//get list spliiter running payroll for others TAX Office.
424

425
    SELECT DISTINCT
426

427
            employee_id ,
428

429
            start_date ,
430

431
            end_date ,
432

433
            employee_office ,
434

435
            run_period_month
436

437
    INTO    #tmp0
438

439
    FROM    @payroll_header0 ph0
440

441
    WHERE   NOT EXISTS ( SELECT *
442

443
                         FROM   @payroll_header ph
444

445
                         WHERE  ph0.employee_id = ph.employee_id
446

447
                                AND ph0.start_date = ph.start_date
448

449
                                AND ph0.end_date = ph.end_date
450

451
                                AND ph0.employee_office = ph.employee_office
452

453
                                AND ph0.run_period_month = ph.run_period_month )
454

455
		
456

457
-- Get list date range splitter.
458

459
    SELECT  a.employee_id ,
460

461
            a.start_date mindt0 ,
462

463
            b.start_date maxdt0
464

465
    INTO    #tmp
466

467
    FROM    @payroll_header a ,
468

469
            @payroll_header b
470

471
    WHERE   a.employee_id = b.employee_id
472

473
            AND b.start_date > a.start_date
474

475
            AND ( CONVERT(INT, SUBSTRING(b.start_date, 5, 2))
476

477
                  - CONVERT(INT, SUBSTRING(a.start_date, 5, 2)) ) > 1
478

479
            AND ( SELECT    COUNT(*)
480

481
                  FROM      @payroll_header c
482

483
                  WHERE     ( CONVERT(INT, SUBSTRING(a.start_date, 5, 2)) < CONVERT(INT, SUBSTRING(c.start_date,
484

485
                                                              5, 2)) )
486

487
                            AND ( CONVERT(INT, SUBSTRING(b.start_date, 5, 2)) > CONVERT(INT, SUBSTRING(c.end_date,
488

489
                                                              5, 2)) )
490

491
                            AND c.employee_id = a.employee_id
492

493
                ) = 0
494

495
            AND ( SELECT    COUNT(*)
496

497
                  FROM      #tmp0 d
498

499
                  WHERE     d.employee_id = a.employee_id
500

501
                            AND a.start_date < d.start_date
502

503
                            AND b.start_date > d.end_date
504

505
                ) <> 0
506

507
	
508

509
    DROP TABLE #tmp0
510

511

512

513

514

515
    DECLARE @n_empty_range INT
516

517
	  
518

519
    SELECT  @n_empty_range = COUNT(*)
520

521
    FROM    #tmp	  
522

523

524

525
    IF ( @n_empty_range <> 0 ) 
526

527
        BEGIN
528

529
            INSERT  INTO @employee_tobe_process
530

531
                    SELECT  ddd.employee_id ,
532

533
                            ddd.mindt ,
534

535
                            ddd.maxdt
536

537
                    FROM    ( SELECT    dd.employee_id ,
538

539
                                        MIN(dd.start_date) mindt ,
540

541
                                        MAX(dd.end_date) maxdt
542

543
                              FROM      ( SELECT    x.employee_id ,
544

545
                                                    x.start_date ,
546

547
                                                    x.end_date ,
548

549
                                                    SUM(ISNULL(d.flag, 0)) flag
550

551
                                          FROM      @payroll_header x
552

553
                                                    LEFT OUTER JOIN ( SELECT
554

555
                                                              a.* ,
556

557
                                                              1 'flag'
558

559
                                                              FROM
560

561
                                                              @payroll_header a
562

563
                                                              LEFT OUTER JOIN #tmp b ON a.employee_id = b.employee_id
564

565
                                                              WHERE
566

567
                                                              a.start_date <= b.mindt0
568

569
                                                              AND a.end_date < b.maxdt0
570

571
                                                              ) d ON x.employee_id = d.employee_id
572

573
                                                              AND x.start_date = d.start_date
574

575
                                                              AND x.end_date = d.end_date
576

577
                                          GROUP BY  x.employee_id ,
578

579
                                                    x.start_date ,
580

581
                                                    x.end_date
582

583
                                        ) dd
584

585
                              GROUP BY  dd.employee_id ,
586

587
                                        dd.flag
588

589
                            ) ddd
590

591
                    ORDER BY ddd.employee_id ,
592

593
                            ddd.mindt	
594

595
        END
596

597
    ELSE 
598

599
        BEGIN
600

601
            INSERT  INTO @employee_tobe_process
602

603
                    SELECT  main.employee_id ,
604

605
                            mindate.mindt ,
606

607
                            maxdate.maxdt
608

609
                    FROM    ( SELECT DISTINCT
610

611
                                        employee_id
612

613
                              FROM      @payroll_header
614

615
                            ) main
616

617
                            INNER JOIN ( SELECT ph.employee_id ,
618

619
                                                MIN(ph.start_date) mindt
620

621
                                         FROM   @payroll_header ph
622

623
                                         GROUP BY employee_id
624

625
                                       ) mindate ON main.employee_id = mindate.employee_id
626

627
                            INNER JOIN ( SELECT employee_id ,
628

629
                                                MAX(end_date) maxdt
630

631
                                         FROM   @payroll_header ph
632

633
                                         GROUP BY employee_id
634

635
                                       ) maxdate ON main.employee_id = maxdate.employee_id
636

637
        END
638

639
				
640

641
    DROP TABLE #tmp           
642

643
                
644

645
    DECLARE @kode_form VARCHAR(7)
646

647
    DECLARE @tahun_pajak VARCHAR(4)
648

649
    DECLARE @pembetulan INT
650

651
    DECLARE @nomor_urut VARCHAR(7)
652

653
    DECLARE @npwp_pegawai VARCHAR(15)
654

655
    DECLARE @nama_pegawai VARCHAR(50)
656

657
    DECLARE @alamat_pegawai VARCHAR(255)
658

659
    DECLARE @jabatan_pegawai VARCHAR(30)
660

661
    DECLARE @jenis_kelamin VARCHAR(1)
662

663
    DECLARE @status_pegawai VARCHAR(1)
664

665
    DECLARE @status_kawin VARCHAR(1)
666

667
    DECLARE @flag_asing VARCHAR(1)
668

669
    DECLARE @status_ptkp VARCHAR(2)
670

671
    DECLARE @jumlah_tanggungan VARCHAR(1)
672

673
    DECLARE @masa_perolehan_1 VARCHAR(2)
674

675
    DECLARE @masa_perolehan_2 VARCHAR(2)
676

677
    DECLARE @flag_status VARCHAR(1)
678

679
    DECLARE @a1 DECIMAL
680

681
    DECLARE @flg_a2 VARCHAR(1)
682

683
    DECLARE @a2 DECIMAL
684

685
    DECLARE @a3 DECIMAL
686

687
    DECLARE @a4 DECIMAL
688

689
    DECLARE @a5 DECIMAL
690

691
    DECLARE @a6 DECIMAL
692

693
    DECLARE @a7 DECIMAL
694

695
    DECLARE @a8 DECIMAL
696

697
    DECLARE @a9 DECIMAL
698

699
    DECLARE @a10 DECIMAL
700

701
    DECLARE @a11 DECIMAL
702

703
    DECLARE @a12 DECIMAL
704

705
    DECLARE @a13 DECIMAL
706

707
    DECLARE @a14 DECIMAL
708

709
    DECLARE @a15 DECIMAL
710

711
    DECLARE @a16 DECIMAL
712

713
    DECLARE @a16_flag DECIMAL
714

715
    DECLARE @a17 DECIMAL
716

717
    DECLARE @a18 DECIMAL
718

719
    DECLARE @a19 DECIMAL
720

721
    DECLARE @a19b DECIMAL
722

723
    DECLARE @a20 DECIMAL
724

725
    DECLARE @a21 DECIMAL
726

727
    DECLARE @a22 DECIMAL
728

729
    DECLARE @a22a DECIMAL
730

731
    DECLARE @a22b DECIMAL
732

733
    DECLARE @a23 DECIMAL
734

735
    DECLARE @a24 DECIMAL
736

737
    DECLARE @flg_a24 VARCHAR(1)
738

739
    DECLARE @bln_a24 VARCHAR(6)
740

741
    DECLARE @masa_pajak VARCHAR(10)
742

743
    DECLARE @kode_pajak VARCHAR(30)
744

745
    DECLARE @npwp_pemotong VARCHAR(15)
746

747
    DECLARE @nama_pemotong VARCHAR(200)
748

749
    DECLARE @company_name VARCHAR(200)
750

751
    DECLARE @a19murni DECIMAL
752

753
    DECLARE @a17_att DECIMAL
754

755
    DECLARE @ktp VARCHAR(50)
756

757

758

759
    DECLARE @jml_bulan_perolehan INT
760

761
    DECLARE @tot_biaya_jabatan DECIMAL
762

763

764

765
    DECLARE @result TABLE
766

767
        (
768

769
          kode_form VARCHAR(7) ,
770

771
          tahun_pajak VARCHAR(4) ,
772

773
          pembetulan INT ,
774

775
          nomor_urut CHAR(7) ,
776

777
          npwp_pegawai VARCHAR(30) ,
778

779
          nama_pegawai VARCHAR(50) ,
780

781
          alamat_pegawai VARCHAR(255) ,
782

783
          jabatan_pegawai VARCHAR(30) ,
784

785
          jenis_kelamin VARCHAR(5) ,
786

787
          status_pegawai VARCHAR(5) ,
788

789
          status_kawin VARCHAR(5) ,
790

791
          flag_asing VARCHAR(1) ,
792

793
          status_ptkp VARCHAR(5) ,
794

795
          jumlah_tanggungan VARCHAR(1) ,
796

797
          masa_perolehan_1 VARCHAR(2) ,
798

799
          masa_perolehan_2 VARCHAR(2) ,
800

801
          flag_status VARCHAR(1) ,
802

803
          a1 DECIMAL ,
804

805
          flg_a2 VARCHAR(1) ,
806

807
          a2 DECIMAL ,
808

809
          a3 DECIMAL ,
810

811
          a4 DECIMAL ,
812

813
          a5 DECIMAL ,
814

815
          a6 DECIMAL ,
816

817
          a7 DECIMAL ,
818

819
          a8 DECIMAL ,
820

821
          a9 DECIMAL ,
822

823
          a10 DECIMAL ,
824

825
          a11 DECIMAL ,
826

827
          a12 DECIMAL ,
828

829
          a13 DECIMAL ,
830

831
          a14 DECIMAL ,
832

833
          a15 DECIMAL ,
834

835
          a16 DECIMAL ,
836

837
          a17 DECIMAL ,
838

839
          a18 DECIMAL ,
840

841
          a19 DECIMAL ,
842

843
          a20 DECIMAL ,
844

845
          a21 DECIMAL ,
846

847
          a22 DECIMAL ,
848

849
          a22a DECIMAL ,
850

851
          a22b DECIMAL ,
852

853
          a23 DECIMAL ,
854

855
          a24 DECIMAL ,
856

857
          flg_a24 VARCHAR(1) ,
858

859
          bln_a24 VARCHAR(6)
860

861
	--,emp_id VARCHAR(20)
862

863
          ,
864

865
          KTP VARCHAR(50) ,
866

867
          nama_pemotong VARCHAR(200) ,
868

869
          npwp_pemotong VARCHAR(30) ,
870

871
          kode_pajak VARCHAR(30)
872

873
	--,company_name VARCHAR(200)
874

875
        )                   
876

877
 
878

879
    DECLARE @cu0300 TABLE
880

881
        (
882

883
          [code] [nVARCHAR](4) NOT NULL ,
884

885
          [start_date] [nVARCHAR](8) NOT NULL ,
886

887
          [end_date] [nVARCHAR](8) NOT NULL ,
888

889
          [spt_no] [INT] NULL
890

891
        )
892

893

894

895
    DECLARE @cu0300_tmp TABLE
896

897
        (
898

899
          [code] [nVARCHAR](4) NOT NULL ,
900

901
          [start_date] [nVARCHAR](8) NOT NULL ,
902

903
          [end_date] [nVARCHAR](8) NOT NULL ,
904

905
          [spt_no] [INT] NULL
906

907
        )
908

909
	
910

911
    DECLARE @tbl_tr_prev_ori TABLE
912

913
        (
914

915
          employee_id VARCHAR(8) ,
916

917
          wage_type VARCHAR(4) ,
918

919
          amount DECIMAL ,
920

921
          spt_no INT ,
922

923
          run_period_month VARCHAR(2)
924

925
        )
926

927

928

929
    DECLARE @tbl_tr_prev_ori_tmp TABLE
930

931
        (
932

933
          employee_id VARCHAR(8) ,
934

935
          wage_type VARCHAR(4) ,
936

937
          amount DECIMAL ,
938

939
          spt_no INT ,
940

941
          run_period_month VARCHAR(2)
942

943
        )
944

945
 
946

947
    DECLARE @tbl_tr_prev_cum TABLE
948

949
        (
950

951
          employee_id VARCHAR(8) ,
952

953
          wage_type VARCHAR(4) ,
954

955
          amount DECIMAL ,
956

957
          spt_no INT ,
958

959
          run_period_month VARCHAR(2)
960

961
        )
962

963

964

965
    DECLARE @tbl_tr_prev_cum_tmp TABLE
966

967
        (
968

969
          employee_id VARCHAR(8) ,
970

971
          wage_type VARCHAR(4) ,
972

973
          amount DECIMAL ,
974

975
          spt_no INT ,
976

977
          run_period_month VARCHAR(2)
978

979
        )
980

981

982

983
    DECLARE @tbl_tr_dec_ori TABLE
984

985
        (
986

987
          employee_id VARCHAR(8) ,
988

989
          wage_type VARCHAR(4) ,
990

991
          amount DECIMAL ,
992

993
          spt_no INT ,
994

995
          run_period_month VARCHAR(2)
996

997
        )
998

999

1000

1001
    DECLARE @tbl_tr_dec_ori_tmp TABLE
1002

1003
        (
1004

1005
          employee_id VARCHAR(8) ,
1006

1007
          wage_type VARCHAR(4) ,
1008

1009
          amount DECIMAL ,
1010

1011
          spt_no INT ,
1012

1013
          run_period_month VARCHAR(2)
1014

1015
        )
1016

1017
 
1018

1019
    DECLARE @tbl_tr_dec TABLE
1020

1021
        (
1022

1023
          employee_id VARCHAR(8) ,
1024

1025
          wage_type VARCHAR(4) ,
1026

1027
          amount DECIMAL ,
1028

1029
          spt_no INT ,
1030

1031
          run_period_month VARCHAR(2)
1032

1033
        )
1034

1035

1036

1037
    DECLARE @tbl_tr_dec_tmp TABLE
1038

1039
        (
1040

1041
          employee_id VARCHAR(8) ,
1042

1043
          wage_type VARCHAR(4) ,
1044

1045
          amount DECIMAL ,
1046

1047
          spt_no INT ,
1048

1049
          run_period_month VARCHAR(2)
1050

1051
        )
1052

1053

1054

1055
    DECLARE @tbl_tr_dec_total TABLE
1056

1057
        (
1058

1059
          employee_id VARCHAR(8) ,
1060

1061
          amount DECIMAL ,
1062

1063
          spt_no INT
1064

1065
        )
1066

1067
	 
1068

1069
    DECLARE @tbl_tr_enc TABLE
1070

1071
        (
1072

1073
          employee_id VARCHAR(8) ,
1074

1075
          wage_type VARCHAR(4) ,
1076

1077
          amount VARCHAR(250) ,
1078

1079
          spt_no INT
1080

1081
        )
1082

1083
                   
1084

1085
    SET @kode_form = 'D113248'
1086

1087
    SET @tahun_pajak = @Year
1088

1089
    SET @pembetulan = 0
1090

1091
 
1092

1093
    DECLARE @c_landscape VARCHAR(3)
1094

1095
    DECLARE @c_emp_id VARCHAR(8)
1096

1097
    DECLARE @c_start_date INT
1098

1099
    DECLARE @c_end_date INT
1100

1101

1102

1103
    DECLARE @last_period_payroll INT
1104

1105
    DECLARE @previous_period_payroll INT
1106

1107
    DECLARE @previous_period_payroll0 INT
1108

1109
    DECLARE @previous_period_payroll_varchar VARCHAR(2)
1110

1111

1112

1113
----//Run Cursor
1114

1115
    DECLARE cur_employee CURSOR
1116

1117
    FOR
1118

1119
        SELECT  *
1120

1121
        FROM    @employee_tobe_process
1122

1123
    OPEN cur_employee
1124

1125
    FETCH cur_employee INTO @c_emp_id, @c_start_date, @c_end_date
1126

1127
    WHILE @@fetch_status = 0 
1128

1129
        BEGIN  
1130

1131
            SET @last_period_payroll = @c_end_date
1132

1133
            SET @nomor_urut = CONVERT(INTEGER, RIGHT(@c_emp_id, 7))
1134

1135

1136

1137
            SELECT  @npwp_pegawai = REPLACE(REPLACE(ISNULL(NPWP,
1138

1139
                                                           '000000000000000'),
1140

1141
                                                    '.', ''), '-', '') ,
1142

1143
                    @status_kawin = CASE LEFT(TaxStatus, 1)
1144

1145
                                      WHEN 'T' THEN '1'
1146

1147
                                      ELSE '2'
1148

1149
                                    END ,
1150

1151
                    @status_ptkp = a.TaxStatus ,
1152

1153
                    @jumlah_tanggungan = CASE RIGHT(TaxStatus, 1)
1154

1155
                                           WHEN 'K' THEN '0'
1156

1157
                                           ELSE RIGHT(TaxStatus, 1)
1158

1159
                                         END
1160

1161
            FROM    dbo.PHRPA0003 AS a
1162

1163
            WHERE   EmployeeID = @c_emp_id
1164

1165
                    AND StartDate <= @c_end_date
1166

1167
                    AND EndDate >= @c_end_date
1168

1169
	
1170

1171
            SELECT  DISTINCT
1172

1173
                    @a17 = PTKP
1174

1175
            FROM    dbo.PHRPYCU0302
1176

1177
            WHERE   StartDate <= @c_end_date
1178

1179
                    AND EndDate >= @c_end_date
1180

1181
                    AND TaxStatus = ( SELECT DISTINCT
1182

1183
                                                TaxStatus
1184

1185
                                      FROM      dbo.PHRPA0003
1186

1187
                                      WHERE     EmployeeID = @c_emp_id
1188

1189
                                                AND StartDate <= @c_end_date
1190

1191
                                                AND EndDate >= @c_end_date
1192

1193
                                    )
1194

1195

1196

1197
            SELECT  @nama_pegawai = FullName ,
1198

1199
                    @jenis_kelamin = b.GenderDescription ,
1200

1201
                    ----@ktp = c.IDDescription -----  dikomen by Tri
1202

1203
					@ktp = a.IDCard
1204

1205
            FROM    dbo.PHRPA0001 AS a
1206

1207
                    LEFT JOIN dbo.PHRPAGENDER AS b ON a.Gender = b.Gender
1208

1209
                                                      AND b.StartDate <= @c_end_date
1210

1211
                                                      AND b.EndDate >= @c_end_date
1212

1213
                    LEFT JOIN dbo.PHRPA0013 AS c ON a.EmployeeID = c.EmployeeID
1214

1215
                                                    AND c.StartDate <= @c_end_date
1216

1217
                                                    AND c.EndDate >= @c_end_date
1218

1219
													AND c.IDType = '01' -----  add by Tri
1220

1221
            WHERE   a.EmployeeID = @c_emp_id
1222

1223
                    AND a.StartDate <= @c_end_date
1224

1225
                    AND a.EndDate >= @c_end_date
1226

1227
                    -----AND c.IDType = '01' -----  dikomen by Tri
1228

1229
	 
1230

1231
            SELECT  @nama_pemotong = t.TaxOfficeDescription ,
1232

1233
                    @npwp_pemotong = o.NPWP
1234

1235
            FROM    dbo.PHRPA0002 AS a
1236

1237
                    LEFT JOIN dbo.PCMEPEMPOFF AS o ON a.EmployeeOffice = o.EmployeeOffice
1238

1239
                                                      AND o.StartDate <= @c_end_date
1240

1241
                                                      AND o.EndDate >= @c_end_date
1242

1243
                    LEFT JOIN dbo.PHRPYTAXOFF AS t ON t.TaxOffice = o.TaxOffice
1244

1245
                                                      AND t.StartDate <= @c_end_date
1246

1247
                                                      AND t.EndDate >= @c_end_date
1248

1249
            WHERE   a.StartDate <= @c_end_date
1250

1251
                    AND a.EndDate >= @c_end_date
1252

1253
                    AND a.EmployeeID = @c_emp_id
1254

1255
			
1256

1257
            SELECT  @kode_pajak = e.TaxCode
1258

1259
            FROM    dbo.PHRPA0002 AS a
1260

1261
                    LEFT JOIN dbo.PCMEPEMPTYP AS e ON a.EmployeeType = e.EmployeeType
1262

1263
            WHERE   a.StartDate <= @c_end_date
1264

1265
                    AND a.EndDate >= @c_end_date
1266

1267
                    AND a.EmployeeID = @c_emp_id
1268

1269
			
1270

1271
	--//alamat & flag status belakangan
1272

1273
            SELECT  @alamat_pegawai = REPLACE(md06.Address, ',', ' ')
1274

1275
            FROM    dbo.PHRPA0006 AS md06
1276

1277
            WHERE   EmployeeID = @c_emp_id
1278

1279
                    AND StartDate <= @c_end_date
1280

1281
                    AND EndDate >= @c_end_date
1282

1283
                    AND AddressType = @npwp_address_subtype
1284

1285

1286

1287
            SELECT  @jabatan_pegawai = mdobj.ObjectDescription
1288

1289
            FROM    dbo.PHRPA0002 AS md02
1290

1291
                    INNER JOIN dbo.PHROM0001 AS mdobj ON md02.Position = mdobj.ObjectID
1292

1293
                                                         AND mdobj.ObjectClass = 'P'
1294

1295
                                                         AND mdobj.StartDate <= @c_end_date
1296

1297
                                                         AND mdobj.EndDate >= @c_end_date
1298

1299
            WHERE   md02.EmployeeID = @c_emp_id
1300

1301
                    AND md02.StartDate <= @c_end_date
1302

1303
                    AND md02.EndDate >= @c_end_date
1304

1305
	
1306

1307
            SELECT  @status_pegawai = CASE EmployeeStatus
1308

1309
                                        WHEN '01' THEN '1'
1310

1311
                                        WHEN '03' THEN '2'
1312

1313
                                        ELSE '1'
1314

1315
                                      END ,
1316

1317
                    @flag_asing = CASE EmployeeType
1318

1319
                                    WHEN '05' THEN '1'
1320

1321
                                    ELSE '0'
1322

1323
                                  END
1324

1325
            FROM    dbo.PHRPA0002 AS md02
1326

1327
            WHERE   EmployeeID = @c_emp_id
1328

1329
                    AND StartDate <= @c_end_date
1330

1331
                    AND EndDate >= @c_end_date
1332

1333

1334

1335
            SET @masa_perolehan_1 = CONVERT(INTEGER, SUBSTRING(CONVERT(VARCHAR, @c_start_date),
1336

1337
                                                              5, 2))
1338

1339
            SET @masa_perolehan_2 = CONVERT(INTEGER, SUBSTRING(CONVERT(VARCHAR, @c_end_date),
1340

1341
                                                              5, 2))
1342

1343
            SET @jml_bulan_perolehan = ( CONVERT(INT, @masa_perolehan_2)
1344

1345
                                         - CONVERT(INT, @masa_perolehan_1) + 1 )
1346

1347

1348

1349
	--//#region getwtconfiguration
1350

1351
            DELETE  @cu0300_tmp
1352

1353

1354

1355
            INSERT  INTO @cu0300_tmp
1356

1357
                    SELECT  WageTypeDefinition ,
1358

1359
                            StartDate ,
1360

1361
                            EndDate ,
1362

1363
                            SPTNo
1364

1365
                    FROM    dbo.PHRPYCU0300
1366

1367
                    WHERE   StartDate <= @c_end_date
1368

1369
                            AND EndDate >= @c_start_date
1370

1371
                            AND SPTNo > 0
1372

1373

1374

1375
            UPDATE  @cu0300_tmp
1376

1377
            SET     start_date = @begda
1378

1379
            WHERE   start_date < @c_start_date
1380

1381
	  
1382

1383
            UPDATE  @cu0300_tmp
1384

1385
            SET     end_date = @endda
1386

1387
            WHERE   end_date > @c_end_date
1388

1389

1390

1391
--/*
1392

1393
            DELETE  @cu0300
1394

1395

1396

1397
            INSERT  INTO @cu0300
1398

1399
                    SELECT  cu0300.*
1400

1401
                    FROM    @cu0300_tmp AS cu0300
1402

1403
                            INNER JOIN ( SELECT code ,
1404

1405
                                                MIN(start_date) AS start_date ,
1406

1407
                                                MAX(end_date) AS end_date
1408

1409
                                         FROM   @cu0300_tmp
1410

1411
                                         GROUP BY code
1412

1413
                                       ) a ON cu0300.code = a.code
1414

1415
                                              AND cu0300.end_date = a.end_date
1416

1417

1418

1419
            DELETE  @tbl_tr_dec
1420

1421
            DELETE  @tbl_tr_dec_ori
1422

1423

1424

1425
            DELETE  @tbl_tr_dec_tmp
1426

1427
            DELETE  @tbl_tr_dec_ori_tmp
1428

1429

1430

1431
            DECLARE @cc_landscape VARCHAR(3)
1432

1433
            DECLARE @cc_code VARCHAR(4)
1434

1435
            DECLARE @cc_start_date INT
1436

1437
            DECLARE @cc_end_date INT
1438

1439
            DECLARE @cc_spt_no INT
1440

1441

1442

1443
            INSERT  INTO @tbl_tr_dec_tmp
1444

1445
                    SELECT  tr301.EmployeeID ,
1446

1447
                            tr301.WageType ,
1448

1449
                            CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'),
1450

1451
                                         ',', '.') AS DECIMAL(22, 0)) ,
1452

1453
                            cu300.spt_no ,
1454

1455
                            tr301.RunPeriodMonth
1456

1457
                    FROM    dbo.PHRPYTR0301CUM AS tr301 
1458

1459
                            INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1460

1461
                    WHERE   tr301.EmployeeID = @c_emp_id
1462

1463
                            AND tr301.PayPeriodMonth = SUBSTRING(CONVERT(VARCHAR, @last_period_payroll),
1464

1465
                                                              5, 2)
1466

1467
                            AND tr301.PayPeriodYear = tr301.RunPeriodYear
1468

1469
                            AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
1470

1471
                                                           4)
1472

1473
		  
1474

1475
	--//get the latest run data
1476

1477
            INSERT  INTO @tbl_tr_dec
1478

1479
                    SELECT  tr_dec.*
1480

1481
                    FROM    @tbl_tr_dec_tmp AS tr_dec
1482

1483
                            INNER JOIN ( SELECT employee_id ,
1484

1485
                                                wage_type ,
1486

1487
                                                MAX(run_period_month) AS run_period_month
1488

1489
                                         FROM   @tbl_tr_dec_tmp
1490

1491
                                         GROUP BY employee_id ,
1492

1493
                                                wage_type
1494

1495
                                       ) a ON tr_dec.employee_id = a.employee_id
1496

1497
                                              AND tr_dec.wage_type = a.wage_type
1498

1499
                                              AND tr_dec.run_period_month = a.run_period_month
1500

1501

1502

1503

1504

1505
            INSERT  INTO @tbl_tr_dec_ori_tmp
1506

1507
                    SELECT  tr301.EmployeeID ,
1508

1509
                            tr301.WageType ,
1510

1511
                            CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'),
1512

1513
                                         ',', '.') AS DECIMAL(22, 0)) ,
1514

1515
                            cu300.spt_no ,
1516

1517
                            tr301.RunPeriodMonth
1518

1519
                    FROM    dbo.PHRPYTR0301 AS tr301
1520

1521
                            INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1522

1523
                    WHERE   tr301.EmployeeID = @c_emp_id
1524

1525
                            AND tr301.PayPeriodMonth = SUBSTRING(CONVERT(VARCHAR, @last_period_payroll),
1526

1527
                                                              5, 2)
1528

1529
                            AND tr301.PayPeriodYear = tr301.RunPeriodYear
1530

1531
                            AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
1532

1533
                                                           4)
1534

1535
		  
1536

1537
	--//get the latest run data
1538

1539
            INSERT  INTO @tbl_tr_dec_ori
1540

1541
                    SELECT  tr_dec_ori.*
1542

1543
                    FROM    @tbl_tr_dec_ori_tmp AS tr_dec_ori
1544

1545
                            INNER JOIN ( SELECT employee_id ,
1546

1547
                                                wage_type ,
1548

1549
                                                MAX(run_period_month) AS run_period_month
1550

1551
                                         FROM   @tbl_tr_dec_ori_tmp
1552

1553
                                         GROUP BY employee_id ,
1554

1555
                                                wage_type
1556

1557
                                       ) a ON tr_dec_ori.employee_id = a.employee_id
1558

1559
                                              AND tr_dec_ori.wage_type = a.wage_type
1560

1561
                                              AND tr_dec_ori.run_period_month = a.run_period_month
1562

1563
		
1564

1565
            IF ( @masa_perolehan_1 > '1'
1566

1567
                 OR @masa_perolehan_1 > '01'
1568

1569
               ) 
1570

1571
                BEGIN
1572

1573
                    SET @previous_period_payroll = CONVERT(INT, @masa_perolehan_1)
1574

1575
                        - 1
1576

1577
                    IF ( @previous_period_payroll < 10 ) 
1578

1579
                        SET @previous_period_payroll_varchar = '0'
1580

1581
                            + CONVERT(VARCHAR(1), @previous_period_payroll)
1582

1583
                    ELSE 
1584

1585
                        SET @previous_period_payroll_varchar = CONVERT(VARCHAR(2), @previous_period_payroll)
1586

1587
			
1588

1589
		--//get the latest pay period if previous period no pay period.
1590

1591
                    SET @previous_period_payroll0 = @previous_period_payroll
1592

1593
                    SELECT TOP ( 1 )
1594

1595
                            @previous_period_payroll0 = CONVERT(INT, PayPeriodMonth)
1596

1597
                    FROM    dbo.PHRPYTR0300
1598

1599
                    WHERE   EmployeeID = @c_emp_id
1600

1601
                            AND PayPeriodMonth <= @previous_period_payroll_varchar
1602

1603
                            AND PayPeriodYear = LEFT(@last_period_payroll, 4)
1604

1605
                            AND RunPeriodYear = LEFT(@last_period_payroll, 4)
1606

1607
                    ORDER BY PayPeriodMonth DESC
1608

1609
			
1610

1611
                    IF ( @previous_period_payroll0 < 10 ) 
1612

1613
                        SET @previous_period_payroll_varchar = '0'
1614

1615
                            + CONVERT(VARCHAR(1), @previous_period_payroll0)
1616

1617
                    ELSE 
1618

1619
                        SET @previous_period_payroll_varchar = CONVERT(VARCHAR(2), @previous_period_payroll0)
1620

1621
					
1622

1623
                    DELETE  FROM @tbl_tr_prev_ori
1624

1625
                    DELETE  FROM @tbl_tr_prev_cum
1626

1627
		
1628

1629
                    DELETE  FROM @tbl_tr_prev_ori_tmp
1630

1631
                    DELETE  FROM @tbl_tr_prev_cum_tmp
1632

1633

1634

1635
                    INSERT  INTO @tbl_tr_prev_ori_tmp
1636

1637
                            SELECT  tr301.EmployeeID ,
1638

1639
                                    tr301.WageType ,
1640

1641
                                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount),
1642

1643
                                                        '0'), ',', '.') AS DECIMAL(22,
1644

1645
                                                              0)) ,
1646

1647
                                    cu300.spt_no ,
1648

1649
                                    tr301.RunPeriodMonth
1650

1651
                            FROM    dbo.PHRPYTR0301 AS tr301
1652

1653
                                    INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1654

1655
                            WHERE   tr301.EmployeeID = @c_emp_id
1656

1657
                                    AND tr301.PayPeriodMonth = @previous_period_payroll_varchar
1658

1659
                                    AND tr301.PayPeriodYear = tr301.RunPeriodYear
1660

1661
                                    AND tr301.RunPeriodMonth = LEFT(@last_period_payroll,4)
1662

1663

1664

1665
		--//get the latest run data
1666

1667
                    INSERT  INTO @tbl_tr_prev_ori
1668

1669
                            SELECT  tr_prev_ori.*
1670

1671
                            FROM    @tbl_tr_prev_ori_tmp AS tr_prev_ori
1672

1673
                                    INNER JOIN ( SELECT employee_id ,
1674

1675
                                                        wage_type ,
1676

1677
                                                        MAX(run_period_month) AS run_period_month
1678

1679
                                                 FROM   @tbl_tr_prev_ori_tmp
1680

1681
                                                 GROUP BY employee_id ,
1682

1683
                                                        wage_type
1684

1685
                                               ) a ON tr_prev_ori.employee_id = a.employee_id
1686

1687
                                                      AND tr_prev_ori.wage_type = a.wage_type
1688

1689
                                                      AND tr_prev_ori.run_period_month = a.run_period_month
1690

1691
					
1692

1693
                    INSERT  INTO @tbl_tr_prev_cum_tmp
1694

1695
                            SELECT  tr301.EmployeeID ,
1696

1697
                                    tr301.WageType ,
1698

1699
                                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount),
1700

1701
                                                        '0'), ',', '.') AS DECIMAL(22,
1702

1703
                                                              0)) ,
1704

1705
                                    cu300.spt_no ,
1706

1707
                                    tr301.RunPeriodMonth
1708

1709
                            FROM    dbo.PHRPYTR0301CUM AS tr301
1710

1711
                                    INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1712

1713
                            WHERE   tr301.EmployeeID = @c_emp_id
1714

1715
                                    AND tr301.PayPeriodMonth = @previous_period_payroll_varchar
1716

1717
                                    AND tr301.PayPeriodYear = tr301.RunPeriodYear
1718

1719
                                    AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
1720

1721
                                                              4)
1722

1723

1724

1725
		--//get the latest run data
1726

1727
                    INSERT  INTO @tbl_tr_prev_cum
1728

1729
                            SELECT  tr_prev_cum.*
1730

1731
                            FROM    @tbl_tr_prev_cum_tmp AS tr_prev_cum
1732

1733
                                    INNER JOIN ( SELECT employee_id ,
1734

1735
                                                        wage_type ,
1736

1737
                                                        MAX(run_period_month) AS run_period_month
1738

1739
                                                 FROM   @tbl_tr_prev_cum_tmp
1740

1741
                                                 GROUP BY employee_id ,
1742

1743
                                                        wage_type
1744

1745
                                               ) a ON tr_prev_cum.employee_id = a.employee_id
1746

1747
                                                      AND tr_prev_cum.wage_type = a.wage_type
1748

1749
                                                      AND tr_prev_cum.run_period_month = a.run_period_month	
1750

1751
                END 
1752

1753

1754

1755
            DECLARE @emp_status VARCHAR(2) ,
1756

1757
                @emp_type VARCHAR(2) ,
1758

1759
                @movement_type VARCHAR(2) ,
1760

1761
                @movement_reason VARCHAR(2) ,
1762

1763
                @emp_type_payroll VARCHAR(20) ,
1764

1765
                @movement_reason_payroll VARCHAR(20) ,
1766

1767
                @is_pegawai_baru VARCHAR(1)
1768

1769
		
1770

1771
            DECLARE @death_mov_reason VARCHAR(5)
1772

1773
            SELECT TOP ( 1 )
1774

1775
                    @death_mov_reason = Value1
1776

1777
            FROM    dbo.PCMEPGENPARAM
1778

1779
            WHERE   Parameter = 'HR_ESPT_MOV_REASON_DEATH'
1780

1781
                    AND StartDate <= @endda
1782

1783
                    AND EndDate >= @endda
1784

1785
		
1786

1787
            SELECT  @emp_status = md02.EmployeeStatus ,
1788

1789
                    @emp_type = md02.EmployeeType ,
1790

1791
                    @movement_type = md02.MovementType ,
1792

1793
                    @movement_reason = md02.MovementReason ,
1794

1795
                    @emp_type_payroll = ret.PayrollProcess ,
1796

1797
                    @movement_reason_payroll = rmr.PayrollProcess
1798

1799
            FROM    dbo.PHRPA0002 md02
1800

1801
                    INNER JOIN dbo.PCMEPEMPTYP ret ON md02.EmployeeType = ret.EmployeeType
1802

1803
                    INNER JOIN dbo.PCMEPMOVR rmr ON md02.MovementReason = rmr.MovementReason
1804

1805
            WHERE   EmployeeID = @c_emp_id
1806

1807
                    AND md02.StartDate <= @endda
1808

1809
                    AND md02.EndDate >= @endda
1810

1811
		
1812

1813
	
1814

1815
            SET @is_pegawai_baru = '0'
1816

1817
            IF @c_start_date > @begda 
1818

1819
                BEGIN
1820

1821
                    SELECT  @is_pegawai_baru = CASE WHEN HiringDate >= @c_start_date
1822

1823
                                                    THEN '1'
1824

1825
                                                    ELSE '0'
1826

1827
                                               END
1828

1829
                    FROM    dbo.PHRPA0015
1830

1831
                    WHERE   EmployeeID = @c_emp_id
1832

1833
                END
1834

1835

1836

1837
	--//Setting flag status
1838

1839
            IF @emp_status = '01'
1840

1841
                AND @masa_perolehan_1 = '1'
1842

1843
                AND @masa_perolehan_2 = '12'
1844

1845
                AND ISNULL(@emp_type_payroll, '') <> 'EXP' 
1846

1847
                SET @flag_status = '0'
1848

1849
            ELSE 
1850

1851
                IF @emp_status = '01'
1852

1853
                    AND @masa_perolehan_1 = '1'
1854

1855
                    AND @masa_perolehan_2 = '12'
1856

1857
                    AND ISNULL(@emp_type_payroll, '') = 'EXP' 
1858

1859
                    SET @flag_status = '3'
1860

1861
                ELSE 
1862

1863
                    IF @emp_status = '01'
1864

1865
                        --AND @masa_perolehan_2 <> '12' ---- comment untuk spt tahunan bulanan Tri nwh 20230414
1866

1867
                        SET @flag_status = '1'
1868

1869
                    ELSE 
1870

1871
                        IF @is_pegawai_baru = '1' 
1872

1873
                            SET @flag_status = '4'
1874

1875
                        ELSE 
1876

1877
                            IF @emp_status = '01'
1878

1879
                                AND @masa_perolehan_2 <> '1'
1880

1881
                                AND @masa_perolehan_2 = '12' 
1882

1883
                                SET @flag_status = '5'
1884

1885
                            ELSE 
1886

1887
                                IF ( @movement_type = '17'
1888

1889
                                     OR @emp_status <> '01'
1890

1891
                                   ) 
1892

1893
                                    BEGIN
1894

1895
                                        IF @movement_reason = '12' 
1896

1897
                                            SET @flag_status = '2'
1898

1899
                                        ELSE 
1900

1901
                                            IF @movement_reason = @death_mov_reason
1902

1903
                                                OR @emp_type_payroll = 'EXP' 
1904

1905
                                                SET @flag_status = '3'
1906

1907
                                            ELSE 
1908

1909
                                                IF @masa_perolehan_1 = '1'
1910

1911
                                                    AND @masa_perolehan_2 = '12' 
1912

1913
                                                    SET @flag_status = '0'
1914

1915
                                                ELSE 
1916

1917
                                                    SET @flag_status = '2'
1918

1919
                                    END
1920

1921

1922

1923
            SELECT  @a1 = ISNULL(SUM(amount), 0)
1924

1925
            FROM    @tbl_tr_dec
1926

1927
            WHERE   spt_no = 1
1928

1929
            GROUP BY spt_no
1930

1931

1932

1933
            IF ( @masa_perolehan_1 <> '1' ) 
1934

1935
                SELECT  @a1 = @a1 - ISNULL(SUM(amount), 0)
1936

1937
                FROM    @tbl_tr_prev_cum
1938

1939
                WHERE   spt_no = 1
1940

1941
                GROUP BY spt_no
1942

1943

1944

1945
            SET @a1 = ISNULL(@a1, 0)
1946

1947
            SELECT  @a2 = ISNULL(SUM(amount), 0)
1948

1949
            FROM    @tbl_tr_dec
1950

1951
            WHERE   spt_no = 2
1952

1953
            GROUP BY spt_no
1954

1955

1956

1957
            IF ( @masa_perolehan_1 <> '1' ) 
1958

1959
                SELECT  @a2 = @a2 - ISNULL(SUM(amount), 0)
1960

1961
                FROM    @tbl_tr_prev_cum
1962

1963
                WHERE   spt_no = 2
1964

1965
                GROUP BY spt_no
1966

1967
				  
1968

1969
				
1970

1971
            SET @a2 = ISNULL(@a2, 0)
1972

1973
	
1974

1975
            SELECT  @a3 = ISNULL(SUM(amount), 0)
1976

1977
            FROM    @tbl_tr_dec
1978

1979
            WHERE   spt_no = 3
1980

1981
            GROUP BY spt_no
1982

1983
	  
1984

1985
            IF ( @masa_perolehan_1 <> '1' ) 
1986

1987
                SELECT  @a3 = @a3 - ISNULL(SUM(amount), 0)
1988

1989
                FROM    @tbl_tr_prev_cum
1990

1991
                WHERE   spt_no = 3
1992

1993
                GROUP BY spt_no
1994

1995
				  
1996

1997
            SET @a3 = ISNULL(@a3, 0)
1998

1999
	
2000

2001
            SELECT  @a4 = ISNULL(SUM(amount), 0)
2002

2003
            FROM    @tbl_tr_dec
2004

2005
            WHERE   spt_no = 4
2006

2007
            GROUP BY spt_no
2008

2009
	
2010

2011
            SET @a4 = ISNULL(@a4, 0)
2012

2013
	
2014

2015
            SELECT  @a5 = ISNULL(SUM(amount), 0)
2016

2017
            FROM    @tbl_tr_dec
2018

2019
            WHERE   spt_no = 5
2020

2021
            GROUP BY spt_no
2022

2023
					  
2024

2025
            IF ( @masa_perolehan_1 <> '1' ) 
2026

2027
                SELECT  @a5 = @a5 - ISNULL(SUM(amount), 0)
2028

2029
                FROM    @tbl_tr_prev_cum
2030

2031
                WHERE   spt_no = 5
2032

2033
                GROUP BY spt_no
2034

2035
					  
2036

2037
            SET @a5 = ISNULL(@a5, 0)
2038

2039
            SELECT  @a6 = ISNULL(SUM(amount), 0)
2040

2041
            FROM    @tbl_tr_dec
2042

2043
            WHERE   spt_no = 6
2044

2045
            GROUP BY spt_no
2046

2047
	
2048

2049
            SET @a6 = ISNULL(@a6, 0)
2050

2051

2052

2053
            SET @a7 = ISNULL(@a1 + @a2 + @a3 + @a4 + @a5 + @a6, 0)
2054

2055

2056

2057
            SELECT  @a8 = ISNULL(SUM(amount), 0)
2058

2059
            FROM    @tbl_tr_dec
2060

2061
            WHERE   spt_no = 8
2062

2063
            GROUP BY spt_no
2064

2065
					  
2066

2067
            IF ( @masa_perolehan_1 <> '1' ) 
2068

2069
                SELECT  @a8 = @a8 - ISNULL(SUM(amount), 0)
2070

2071
                FROM    @tbl_tr_prev_cum
2072

2073
                WHERE   spt_no = 8
2074

2075
                GROUP BY spt_no
2076

2077

2078

2079
            SET @a8 = ISNULL(@a8, 0)
2080

2081

2082

2083
            SET @a9 = ISNULL(@a7 + @a8, 0)
2084

2085

2086

2087
            SELECT  @a10 = ISNULL(SUM(amount), 0)
2088

2089
            FROM    @tbl_tr_dec
2090

2091
            WHERE   spt_no = 10
2092

2093
            GROUP BY spt_no
2094

2095

2096

2097
            IF ( @masa_perolehan_1 <> '1' ) 
2098

2099
                SELECT  @a10 = @a10 - ISNULL(SUM(amount), 0)
2100

2101
                FROM    @tbl_tr_prev_cum
2102

2103
                WHERE   spt_no = 10
2104

2105
                GROUP BY spt_no
2106

2107

2108

2109
            SET @a10 = ISNULL(@a10, 0)
2110

2111

2112

2113
            SET @tot_biaya_jabatan = 500000 * @jml_bulan_perolehan
2114

2115
            IF ( @a10 > @tot_biaya_jabatan ) 
2116

2117
                BEGIN
2118

2119
                    SET @a10 = @tot_biaya_jabatan
2120

2121
                    SET @a11 = 0
2122

2123
                END
2124

2125

2126

2127
            SELECT  @a11 = ISNULL(SUM(amount), 0)
2128

2129
            FROM    @tbl_tr_dec
2130

2131
            WHERE   spt_no = 11
2132

2133
            GROUP BY spt_no
2134

2135
					  
2136

2137
            IF ( @masa_perolehan_1 <> '1' ) 
2138

2139
                SELECT  @a11 = @a11 - ISNULL(SUM(amount), 0)
2140

2141
                FROM    @tbl_tr_prev_cum
2142

2143
                WHERE   spt_no = 11
2144

2145
                GROUP BY spt_no
2146

2147
					  
2148

2149
            SET @a11 = ISNULL(@a11, 0)
2150

2151

2152

2153
            IF ( ( @a10 + @a11 ) > @tot_biaya_jabatan ) 
2154

2155
                BEGIN 
2156

2157
                    SET @a11 = @tot_biaya_jabatan - @a10
2158

2159
                END
2160

2161

2162

2163
            SELECT  @a12 = ISNULL(SUM(amount), 0)
2164

2165
            FROM    @tbl_tr_dec
2166

2167
            WHERE   spt_no = 12
2168

2169
            GROUP BY spt_no
2170

2171

2172

2173
            IF ( @masa_perolehan_1 <> '1' ) 
2174

2175
                SELECT  @a12 = @a12 - ISNULL(SUM(amount), 0)
2176

2177
                FROM    @tbl_tr_prev_cum
2178

2179
                WHERE   spt_no = 12
2180

2181
                GROUP BY spt_no
2182

2183

2184

2185
            SET @a12 = ISNULL(@a12, 0)
2186

2187
	
2188

2189
            SET @a13 = ISNULL(@a10 + @a11 + @a12, 0)
2190

2191
	
2192

2193
            SET @a14 = ISNULL(@a9 - @a13, 0)
2194

2195

2196

2197
            SELECT  @a15 = ISNULL(SUM(amount), 0)
2198

2199
            FROM    @tbl_tr_dec_ori
2200

2201
            WHERE   spt_no = 15
2202

2203
            GROUP BY spt_no
2204

2205
            SET @a15 = ISNULL(@a15, 0)
2206

2207
            SET @a16 = ISNULL(@a14 + @a15, 0)
2208

2209

2210

2211
	--//spt_no 16 case khusus flag_status khusus
2212

2213
            SELECT  @a16_flag = ISNULL(SUM(amount), 0)
2214

2215
            FROM    @tbl_tr_dec_ori
2216

2217
            WHERE   spt_no = 16
2218

2219
            GROUP BY spt_no
2220

2221
            SET @a16_flag = ISNULL(@a16_flag, 0)
2222

2223

2224

2225
            IF @movement_reason = @death_mov_reason
2226

2227
                OR ISNULL(@emp_type_payroll, '') = 'EXP' 
2228

2229
                BEGIN
2230

2231
                    SELECT  @a16 = CASE WHEN ISNULL(@emp_type_payroll, '') = 'EXP'
2232

2233
                                        THEN @a16
2234

2235
                                        ELSE @a16 * 12
2236

2237
                                             / ( CONVERT(INT, @masa_perolehan_2)
2238

2239
                                                 - CONVERT(INT, @masa_perolehan_1)
2240

2241
                                                 + 1 )
2242

2243
                                   END
2244

2245
                END
2246

2247

2248

2249
            IF @flag_status = '1'
2250

2251
                OR @flag_status = '3' 
2252

2253
                BEGIN
2254

2255
                    SET @a16 = @a16_flag
2256

2257
                END
2258

2259
			
2260

2261
            SET @a18 = @a16 - @a17
2262

2263
	
2264

2265
            IF ( RIGHT(RTRIM(CAST(@a18 AS VARCHAR(19))), 3) <> '000' ) 
2266

2267
                BEGIN
2268

2269
                    DECLARE @Pembulatan1000 AS VARCHAR(19)
2270

2271
                    SET @Pembulatan1000 = CAST(@a18 AS VARCHAR(19)); 		
2272

2273
                    SET @a18 = SUBSTRING(@Pembulatan1000, 1,
2274

2275
                                         LEN(RTRIM(@Pembulatan1000)) - 3)
2276

2277
                        + REPLICATE('0', 3); 		
2278

2279
                END
2280

2281

2282

2283
	--//ambil mt murni tanpa kondisi
2284

2285
            SELECT  @a19murni = ISNULL(SUM(amount), 0)
2286

2287
            FROM    @tbl_tr_dec
2288

2289
            WHERE   spt_no = 19
2290

2291
            GROUP BY spt_no
2292

2293

2294

2295
	--//ambil att murni tanpa kondisi
2296

2297
            SELECT  @a17_att = ISNULL(SUM(amount), 0)
2298

2299
            FROM    @tbl_tr_dec_ori
2300

2301
            WHERE   spt_no = 99
2302

2303
            GROUP BY spt_no
2304

2305

2306

2307
	--//cek status employee apakah sudah resign
2308

2309
            SELECT  @a19 = ISNULL(SUM(amount), 0)
2310

2311
            FROM    @tbl_tr_dec
2312

2313
            WHERE   spt_no = 19
2314

2315
            GROUP BY spt_no
2316

2317

2318

2319
            IF ISNULL(@emp_type_payroll, '') = 'EXP' 
2320

2321
                BEGIN
2322

2323
                    SELECT  @a19b = ISNULL(SUM(amount), 0)
2324

2325
                    FROM    @tbl_tr_prev_cum
2326

2327
                    WHERE   spt_no = 19
2328

2329
                    GROUP BY spt_no
2330

2331
                    SET @a19 = @a19 * 12 / ( CONVERT(INT, @masa_perolehan_2)
2332

2333
                                             - CONVERT(INT, @masa_perolehan_1)
2334

2335
                                             + 1 )			
2336

2337
                END
2338

2339
            ELSE 
2340

2341
                IF ( @movement_type = 17
2342

2343
                     OR @emp_status <> '01'
2344

2345
                   ) 
2346

2347
                    BEGIN
2348

2349
                        IF @movement_reason = '12' 
2350

2351
                            BEGIN
2352

2353
                                SET @a19 = ISNULL(@a19murni, 0)
2354

2355
                            END
2356

2357
                    END
2358

2359
                ELSE 
2360

2361
                    BEGIN
2362

2363
                        IF ( ( @masa_perolehan_1 = '1' )
2364

2365
                             AND ( @masa_perolehan_2 <> '12' )
2366

2367
                           ) 
2368

2369
                            BEGIN
2370

2371
                                SET @a19 = @a19 * 12
2372

2373
                                    / CONVERT(INT, @masa_perolehan_2)
2374

2375
                            END
2376

2377
                    END
2378

2379

2380

2381
            SET @a19 = ISNULL(@a19, 0)
2382

2383

2384

2385
			DECLARE @MTPrevious Decimal(22,0)
2386

2387
			SELECT @MTPrevious = (amount * 12) / @previous_period_payroll
2388

2389
			FROM @tbl_tr_prev_cum WHERE wage_type = 'MT'
2390

2391

2392

2393
            SELECT  @a20 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2394

2395
                                THEN ISNULL(SUM(amount), 0)
2396

2397
                                ELSE 0
2398

2399
                           END
2400

2401
            FROM    @tbl_tr_dec_ori
2402

2403
            WHERE   spt_no = 20
2404

2405
            GROUP BY spt_no 
2406

2407

2408

2409
            SET @a20 = ISNULL(@a20, 0) ----- dikomen karena apit * aprt di ES belum tersimpan di table result by Tri, untuk spt 1721 bulanan tidak dipakai
2410

2411

2412

2413
			/*SET @a20 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2414

2415
                                THEN ISNULL(SUM(@MTPrevious), 0)
2416

2417
                                ELSE 0
2418

2419
                           END*/ ----- comment by Tri nwh untuk spt 1721 bulanan 20230414
2420

2421

2422

2423
	------------------ // cek apa sdh pernah running di period sebelum, untuk kebutuhan init paycum, mengganggap a20 lama atau a18 baru menjadi 0 // -----------------
2424

2425
            DECLARE @flag_running VARCHAR(200)
2426

2427
            SELECT  @flag_running = CASE WHEN COUNT(tr.EmployeeID) <= 0
2428

2429
                                         THEN 'no_data'
2430

2431
                                         ELSE 'yes_data'
2432

2433
                                    END
2434

2435
            FROM    dbo.PHRPYTR0300 AS tr
2436

2437
            WHERE   tr.RunPeriodMonth = tr.PayPeriodMonth
2438

2439
                    AND tr.RunPeriodYear = tr.PayPeriodYear
2440

2441
                    AND tr.RunPeriodMonth = @previous_period_payroll_varchar
2442

2443
                    AND tr.EmployeeID = @c_emp_id
2444

2445
	------------------ // pengecekan running selesai // ----------------- 
2446

2447
			
2448

2449
            IF ( @emp_status = '01'
2450

2451
                 AND NOT ( @masa_perolehan_1 = '1'
2452

2453
                           AND @masa_perolehan_2 = '12'
2454

2455
                         )
2456

2457
               )
2458

2459
                OR ISNULL(@emp_type_payroll, '') = 'EXP'
2460

2461
                OR @movement_reason = @death_mov_reason 
2462

2463
                BEGIN	
2464

2465
                    IF @flag_running = 'no_data'  -----// by Tri 20151229 untuk perubahan case upload init paycum
2466

2467
                        BEGIN
2468

2469
                            SET @a21 = ISNULL(@a19murni - 0, 0)
2470

2471
                        END 
2472

2473
                    ELSE 
2474

2475
                        BEGIN
2476

2477
                            SET @a21 = ISNULL(@a19murni - @a20, 0)
2478

2479
                        END --@a2--@a19 								
2480

2481
                END
2482

2483
				
2484

2485
--		ELSE
2486

2487
            /*SET @a21 = ISNULL(@a19 - @a20, 0)*/   --------------- dikomen karena apit * aprt di ES belum tersimpan di table result by Tri
2488

2489
			SET @a21 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2490

2491
                                THEN ISNULL(@a19,0)
2492

2493
                                ELSE ISNULL(@a19 - @a20, 0)
2494

2495
                           END
2496

2497

2498

2499
		----// sementara di hard-code dulu, untuk handling case mks.
2500

2501

2502

2503
            /*SET @a22 = @a21 + @a20*/
2504

2505
			SET @a22 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2506

2507
                                THEN ISNULL(@a21,0)
2508

2509
                                ELSE ISNULL(@a21 + @a20, 0)
2510

2511
                           END
2512

2513
            SET @a22a = 0
2514

2515
            SET @a22b = @a21
2516

2517
            SET @a23 = ISNULL(@a21 - @a22, 0)
2518

2519
            SET @a24 = ISNULL(@a23, 0)
2520

2521

2522

2523
            SET @bln_a24 = SUBSTRING(CONVERT(VARCHAR, @c_end_date), 5, 2)
2524

2525
                + LEFT(CONVERT(VARCHAR, @c_end_date), 4) 
2526

2527

2528

2529
            IF ( @a21 - @a22 = 0 ) 
2530

2531
                BEGIN
2532

2533
                    SET @flg_a24 = '0'
2534

2535
                    SET @bln_a24 = ''
2536

2537
                END
2538

2539

2540

2541
            IF ( @a21 - @a22 > 0 ) 
2542

2543
                BEGIN
2544

2545
                    SET @flg_a24 = '1'
2546

2547
                END
2548

2549

2550

2551
            IF ( @a21 - @a22 < 0 ) 
2552

2553
                BEGIN
2554

2555
                    SET @flg_a24 = '2'
2556

2557
                END
2558

2559

2560

2561
            SET @flg_a2 = '0'
2562

2563
		
2564

2565
		--//Pegawai yang dipindahkan
2566

2567
            SET @flg_a2 = '1'
2568

2569

2570

2571
            DELETE  @tbl_tr_dec
2572

2573
		
2574

2575
            INSERT  INTO @result
2576

2577
                    SELECT  @kode_form ,
2578

2579
                            @tahun_pajak ,
2580

2581
                            @pembetulan ,
2582

2583
                            CONVERT(CHAR, RIGHT('0000000'
2584

2585
                                                + CONVERT(VARCHAR(7), @nomor_urut),
2586

2587
                                                7)) AS nomor_urut ,
2588

2589
                            @npwp_pegawai ,
2590

2591
                            @nama_pegawai ,
2592

2593
                            @alamat_pegawai ,
2594

2595
                            @jabatan_pegawai ,
2596

2597
                            @jenis_kelamin ,
2598

2599
                            @status_pegawai ,
2600

2601
                            @status_kawin ,
2602

2603
                            @flag_asing ,
2604

2605
                            @status_ptkp ,
2606

2607
                            @jumlah_tanggungan ,
2608

2609
                            @masa_perolehan_1 ,
2610

2611
                            @masa_perolehan_2 ,
2612

2613
                            @flag_status ,
2614

2615
                            @a1 ,
2616

2617
                            @flg_a2 ,
2618

2619
                            @a2 ,
2620

2621
                            @a3 ,
2622

2623
                            @a4 ,
2624

2625
                            @a5 ,
2626

2627
                            @a6 ,
2628

2629
                            @a7 ,
2630

2631
                            @a8 ,
2632

2633
                            @a9 ,
2634

2635
                            @a10 ,
2636

2637
                            @a11 ,
2638

2639
                            @a12 ,
2640

2641
                            @a13 ,
2642

2643
                            @a14 ,
2644

2645
                            @a15 ,
2646

2647
                            @a16 ,
2648

2649
                            @a17 ,
2650

2651
                            @a18 ,
2652

2653
                            @a17_att ,
2654

2655
                            @a20 ,
2656

2657
                            @a21 ,
2658

2659
                            @a22 ,
2660

2661
                            @a22a ,
2662

2663
                            @a22b ,
2664

2665
                            @a23 ,
2666

2667
                            @a24 ,
2668

2669
                            @flg_a24 ,
2670

2671
                            @bln_a24 ,
2672

2673
                            @ktp
2674

2675
		--, @c_emp_id
2676

2677
                            ,
2678

2679
                            @nama_pemotong ,
2680

2681
                            @npwp_pemotong ,
2682

2683
                            @kode_pajak
2684

2685
		--, @company_name
2686

2687
					
2688

2689

2690

2691
            SET @nomor_urut = NULL
2692

2693
            SET @npwp_pegawai = NULL
2694

2695
            SET @nama_pegawai = NULL
2696

2697
            SET @alamat_pegawai = NULL
2698

2699
            SET @jabatan_pegawai = NULL
2700

2701
            SET @jenis_kelamin = NULL
2702

2703
            SET @status_pegawai = NULL
2704

2705
            SET @status_kawin = NULL
2706

2707
            SET @flag_asing = NULL
2708

2709
            SET @status_ptkp = NULL
2710

2711
            SET @jumlah_tanggungan = NULL
2712

2713
            SET @masa_perolehan_1 = NULL
2714

2715
            SET @masa_perolehan_2 = NULL
2716

2717
            SET @flag_status = NULL
2718

2719
            SET @a1 = NULL
2720

2721
            SET @flg_a2 = NULL
2722

2723
            SET @a2 = NULL
2724

2725
            SET @a3 = NULL
2726

2727
            SET @a4 = NULL
2728

2729
            SET @a5 = NULL
2730

2731
            SET @a6 = NULL
2732

2733
            SET @a7 = NULL
2734

2735
            SET @a8 = NULL
2736

2737
            SET @a9 = NULL
2738

2739
            SET @a10 = NULL
2740

2741
            SET @a11 = NULL
2742

2743
            SET @a12 = NULL
2744

2745
            SET @a13 = NULL
2746

2747
            SET @a14 = NULL
2748

2749
            SET @a15 = NULL
2750

2751
            SET @a16 = NULL
2752

2753
            SET @a16_flag = NULL
2754

2755
            SET @a17 = NULL
2756

2757
            SET @a18 = NULL
2758

2759
            SET @a19 = NULL
2760

2761
            SET @a20 = NULL
2762

2763
            SET @a21 = NULL
2764

2765
            SET @a22 = NULL
2766

2767
            SET @a22a = NULL
2768

2769
            SET @a22b = NULL
2770

2771
            SET @a23 = NULL
2772

2773
            SET @a24 = NULL
2774

2775
            SET @flg_a24 = NULL
2776

2777
            SET @bln_a24 = NULL
2778

2779

2780

2781
            FETCH cur_employee INTO @c_emp_id, @c_start_date, @c_end_date   
2782

2783
        END
2784

2785
    CLOSE cur_employee
2786

2787
    DEALLOCATE cur_employee
2788

2789

2790

2791
    UPDATE  @result
2792

2793
    SET     npwp_pegawai = '000000000000000'
2794

2795
    WHERE   LEN(npwp_pegawai) < 15
2796

2797

2798

2799
    UPDATE  @result
2800

2801
    SET     alamat_pegawai = '-'
2802

2803
    WHERE   alamat_pegawai = ''
2804

2805
            OR alamat_pegawai IS NULL
2806

2807
		
2808

2809
    SELECT  masa_perolehan_2 AS TaxPeriod ,
2810

2811
            tahun_pajak AS TaxableYear,
2812

2813
            pembetulan AS TaxCorrection ,
2814

2815
            '1.1-' + CONVERT(CHAR(2), RIGHT('00'
2816

2817
                                            + CONVERT(VARCHAR(2), masa_perolehan_2),
2818

2819
                                            2)) + '.' + RIGHT(@Year, 2) + '-'
2820

2821
            + nomor_urut AS WithholdingTax ,
2822

2823
            --CONVERT(CHAR(2), RIGHT('00' + CONVERT(VARCHAR(2), masa_perolehan_2),
2824

2825
            --                       2)) AS bulan_bukti_potong ,
2826

2827
            --RIGHT(@Year, 2) AS tahun_bukti_potong ,
2828

2829
            --nomor_urut ,
2830

2831
            masa_perolehan_1 AS AcquisitionPeriod ,
2832

2833
            masa_perolehan_2 AS AcquisitionPeriodFinal ,
2834

2835
            npwp_pegawai AS NPWP ,
2836

2837
            --RIGHT(npwp_pegawai, 3) AS tiga_akhir_npwp_pegawai ,
2838

2839
            --SUBSTRING(npwp_pegawai, 10, 3) AS enam_akhir_npwp_pegawai ,
2840

2841
            --LEFT(npwp_pegawai, 9) AS awal_npwp_pegawai ,
2842

2843
            --SUBSTRING(npwp_pegawai, 1, 2) + '.' + SUBSTRING(npwp_pegawai, 3, 3)
2844

2845
            --+ '.' + SUBSTRING(npwp_pegawai, 6, 3) + '.'
2846

2847
            --+ SUBSTRING(npwp_pegawai, 9, 1) AS awal_titik_npwp_pegawai ,
2848

2849
            ktp AS EmployeeID ,
2850

2851
            nama_pegawai AS Name,
2852

2853
            alamat_pegawai AS Address,
2854

2855
            CASE WHEN jenis_kelamin = 'W'
2856

2857
                      OR jenis_kelamin = 'Wanita'
2858

2859
                      OR jenis_kelamin = 'Female'
2860

2861
                      OR jenis_kelamin = 'F' THEN 'F'
2862

2863
                 WHEN jenis_kelamin = 'P'
2864

2865
                      OR jenis_kelamin = 'Pria'
2866

2867
                      OR jenis_kelamin = 'Male'
2868

2869
                      OR jenis_kelamin = 'M' THEN 'M'
2870

2871
            END AS Gender ,
2872

2873
            CASE WHEN status_ptkp = 'TK' THEN 'TK'
2874

2875
                 WHEN status_ptkp = 'K1'
2876

2877
                      OR status_ptkp = 'K2'
2878

2879
                      OR status_ptkp = 'K3' THEN 'K'
2880

2881
                 WHEN status_ptkp = 'T1'
2882

2883
                      OR status_ptkp = 'T2'
2884

2885
                      OR status_ptkp = 'T3' THEN 'HB'
2886

2887
            END AS PTKPStatus ,
2888

2889
            CASE WHEN status_ptkp = 'TK' THEN '0'
2890

2891
                 ELSE RIGHT(status_ptkp, 1)
2892

2893
            END AS NumberOfChildren --,jumlah_tanggungan
2894

2895
            ,
2896

2897
            jabatan_pegawai AS JobDescription ,
2898

2899
            CASE WHEN flag_asing = 0 THEN 'N'
2900

2901
                 ELSE 'Y'
2902

2903
            END AS NonResidentTaxpayer ,
2904

2905
            '' AS CountryCode ,
2906

2907
            kode_pajak AS TaxCode ,
2908

2909
            a1 AS Amount1 ,
2910

2911
            a2 AS Amount2 ,
2912

2913
            a3 AS Amount3 ,
2914

2915
            a4 AS Amount4 ,
2916

2917
            a5 AS Amount5 ,
2918

2919
            a6 AS Amount6 ,
2920

2921
            a8 AS Amount7 ,
2922

2923
            a9 AS Amount8 ,
2924

2925
            a10 + a11 AS Amount9 ,
2926

2927
            a12 AS Amount10 ,
2928

2929
            a13 AS Amount11 ,
2930

2931
            a14 AS Amount12 ,
2932

2933
            a15 AS Amount13 ,
2934

2935
            CASE WHEN a16 < 0 THEN 0
2936

2937
                 ELSE a16
2938

2939
            END AS Amount14 ,
2940

2941
            a17 AS Amount15 ,
2942

2943
            CASE WHEN a18 < 0 THEN 0
2944

2945
                 ELSE a18
2946

2947
            END AS Amount16 ,
2948

2949
            a19 AS Amount17 --@a17_att AS a17 
2950

2951
            ,
2952

2953
            CASE WHEN a20 < 0 THEN 0
2954

2955
                 ELSE a20
2956

2957
            END AS Amount18 ,
2958

2959
            a21 AS Amount19 ,
2960

2961
            a22 AS Amount20 ,
2962

2963
            '' AS MovementStatus ,
2964

2965
            npwp_pemotong AS NPWPPemotong ,
2966

2967
            --RIGHT(npwp_pemotong, 3) AS tiga_akhir_npwp_pemotong ,
2968

2969
            --SUBSTRING(npwp_pemotong, 10, 3) AS enam_akhir_npwp_pemotong ,
2970

2971
            --LEFT(npwp_pemotong, 9) AS awal_npwp_pemotong ,
2972

2973
            --SUBSTRING(npwp_pemotong, 1, 2) + '.' + SUBSTRING(npwp_pemotong, 3,
2974

2975
            --                                                 3) + '.'
2976

2977
            --+ SUBSTRING(npwp_pemotong, 6, 3) + '.' + SUBSTRING(npwp_pemotong,
2978

2979
            --                                                  9, 1) AS awal_titik_npwp_pemotong ,
2980

2981
            nama_pemotong AS NamaPemotong ,
2982

2983
            dbo.fn_formatdatetime(GETDATE(), 'dd/mm/yyyy') AS TanggalBuktiPotong 
2984

2985
            --dbo.fn_formatdatetime(GETDATE(), 'dd') AS day_tgl_bukti_potong ,
2986

2987
            --dbo.fn_formatdatetime(GETDATE(), 'mm') AS month_tgl_bukti_potong ,
2988

2989
            --dbo.fn_formatdatetime(GETDATE(), 'yyyy') AS year_tgl_bukti_potong
2990

2991
    FROM    @result
2992

2993

(8-8/9)