Project

General

Profile

Bug #777 ยป RptSPTahun_20220125.sql

Tri Rizqiaty, 01/25/2022 03:25 PM

 
1
??USE [MinovaES_BBG_Prod]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[RptSPTahun]    Script Date: 25/01/2022 15.24.15 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
--USE [MinovaES_BBG_Prod]
16

17
--GO
18

19
--/****** Object:  StoredProcedure [dbo].[RptSPTahun]    Script Date: 08/02/2021 11:13:16 ******/
20

21
--SET ANSI_NULLS ON
22

23
--GO
24

25
--SET QUOTED_IDENTIFIER ON
26

27
--GO
28

29
ALTER PROCEDURE [dbo].[RptSPTahun]
30

31
    @CompanyID VARCHAR(4) = NULL ,
32

33
    @Year VARCHAR(4) ,
34

35
    @TaxOffice VARCHAR(10) ,
36

37
    @EmployeeStatus VARCHAR(2) = NULL ,
38

39
    @PayrollGroup VARCHAR(2) = NULL ,
40

41
    @EmployeeIDFrom VARCHAR(8) = NULL ,
42

43
    @EmployeeIDTo VARCHAR(8) = NULL
44

45
AS 
46

47
    SET NOCOUNT ON;
48

49

50

51
--DECLARE @CompanyID varchar(4) = '1000'
52

53
--DECLARE @Year varchar(4) = '2021'
54

55
--DECLARE @TaxOffice varchar(8) = 'BMI'
56

57
--DECLARE @EmployeeStatus varchar(2) = null
58

59
--DECLARE @PayrollGroup varchar(2) = '04'
60

61
--DECLARE @EmployeeIDFrom varchar(8) = '00000160'--'00000300'
62

63
--DECLARE @EmployeeIDTo varchar(8) = '00000160'
64

65

66

67

68

69
    DECLARE @begda INT
70

71
    DECLARE @endda INT
72

73
    DECLARE @employee_status_beg VARCHAR(5)
74

75
    DECLARE @employee_status_end VARCHAR(5)
76

77
    DECLARE @payroll_group_beg VARCHAR(5)
78

79
    DECLARE @payroll_group_END VARCHAR(5)
80

81

82

83
--//Set begin & end of period
84

85
    SET @begda = @Year + '0101'
86

87
    SET @endda = @Year + '1231'
88

89

90

91
--//Set employee status
92

93
    IF @EmployeeStatus = ''
94

95
        OR @EmployeeStatus IS NULL 
96

97
        BEGIN
98

99
            SET @employee_status_beg = '00000'
100

101
            SET @employee_status_end = '99999'
102

103
        END
104

105
    ELSE 
106

107
        BEGIN
108

109
            SET @employee_status_beg = @EmployeeStatus
110

111
            SET @employee_status_end = @EmployeeStatus
112

113
        END
114

115

116

117
--//Set employee group
118

119
    IF @PayrollGroup = ''
120

121
        OR @PayrollGroup IS NULL 
122

123
        BEGIN
124

125
            SET @payroll_group_beg = '00000'
126

127
            SET @payroll_group_END = '99999'
128

129
        END
130

131
    ELSE 
132

133
        BEGIN
134

135
            SET @payroll_group_beg = @PayrollGroup
136

137
            SET @payroll_group_END = @PayrollGroup
138

139
        END
140

141

142

143

144

145
--//Set employee id start
146

147
    IF @EmployeeIDFrom = ''
148

149
        OR @EmployeeIDFrom IS NULL 
150

151
        BEGIN
152

153
            SET @EmployeeIDFrom = '00000000'
154

155
            SET @EmployeeIDTo = '99999999'
156

157
        END
158

159

160

161
    IF @EmployeeIDTo = ''
162

163
        OR @EmployeeIDTo IS NULL 
164

165
        SET @EmployeeIDTo = @EmployeeIDFrom 
166

167

168

169
--//Get system parameter
170

171
    DECLARE @is_encrypted VARCHAR
172

173
    DECLARE @npwp_address_subtype VARCHAR(4)
174

175

176

177
    SELECT  @is_encrypted = Value1
178

179
    FROM    dbo.PCMEPGENPARAM
180

181
    WHERE   Parameter = 'HR_PY_ENCRYPTED'
182

183
            AND StartDate <= @endda
184

185
            AND EndDate >= @endda
186

187

188

189
    SELECT  @npwp_address_subtype = Value1
190

191
    FROM    dbo.PCMEPGENPARAM
192

193
    WHERE   Parameter = 'HR_PY_NPWP_ADDRESS_SUBTYPE'
194

195
            AND StartDate <= @endda
196

197
            AND EndDate >= @endda
198

199

200

201
-----// Get employee tax office list
202

203
    DECLARE @emp_office TABLE
204

205
        (
206

207
          [StartDate] [varchar](8) NOT NULL ,
208

209
          [EndDate] [varchar](8) NOT NULL ,
210

211
          [EmployeeOffice] [varchar](4) NOT NULL ,
212

213
          [EmployeeOfficeDesc] [varchar](50) NULL ,
214

215
          [EmployeeArea] [varchar](4) NOT NULL ,
216

217
          [TaxOffice] [varchar](4) NULL ,
218

219
          [NPWP] [varchar](50) NULL ,
220

221
          [Address] [varchar](500) NULL ,
222

223
          [EmployeeOfficeGroup] [varchar](5) NULL ,
224

225
          [MinimumWageRegional] [varchar](4) NULL ,
226

227
          [Notes] [varchar](500) NULL ,
228

229
          [CreateBy] [varchar](18) NULL ,
230

231
          [CreateDate] [varchar](14) NULL ,
232

233
          [ChangeBy] [varchar](18) NULL ,
234

235
          [ChangeDate] [varchar](14) NULL
236

237
        )
238

239

240

241
    INSERT  INTO @emp_office
242

243
            SELECT  [StartDate] ,
244

245
                    [EndDate] ,
246

247
                    [EmployeeOffice] ,
248

249
                    [EmployeeOfficeDesc] ,
250

251
                    [EmployeeArea] ,
252

253
                    [TaxOffice] ,
254

255
                    [NPWP] ,
256

257
                    [Address] ,
258

259
                    [EmployeeOfficeGroup] ,
260

261
                    [MinimumWageRegional] ,
262

263
                    [Notes] ,
264

265
                    [CreateBy] ,
266

267
                    [CreateDate] ,
268

269
                    [ChangeBy] ,
270

271
                    [ChangeDate]
272

273
            FROM    dbo.PCMEPEMPOFF
274

275
            WHERE   TaxOffice = @TaxOffice
276

277
	
278

279
--Get Employee Tobe Processed
280

281
    DECLARE @employee_tobe_process2 TABLE
282

283
        (
284

285
          emp_id VARCHAR(8) ,
286

287
          start_date INT ,
288

289
          end_date INT ,
290

291
          employee_status VARCHAR(2)
292

293
        )
294

295

296

297
    DECLARE @employee_tobe_process TABLE
298

299
        (
300

301
          emp_id VARCHAR(8) ,
302

303
          start_date INT ,
304

305
          end_date INT
306

307
        )
308

309

310

311
    DECLARE @payroll_header TABLE
312

313
        (
314

315
          employee_id VARCHAR(8) ,
316

317
          start_date VARCHAR(8) ,
318

319
          end_date VARCHAR(8) ,
320

321
          employee_office VARCHAR(4) ,
322

323
          run_period_month VARCHAR(2)
324

325
        )
326

327
    DECLARE @payroll_header_tmp TABLE
328

329
        (
330

331
          employee_id VARCHAR(8) ,
332

333
          start_date VARCHAR(8) ,
334

335
          end_date VARCHAR(8) ,
336

337
          employee_office VARCHAR(4) ,
338

339
          run_period_month VARCHAR(2)
340

341
        )
342

343
    DECLARE @payroll_header0 TABLE
344

345
        (
346

347
          employee_id VARCHAR(8) ,
348

349
          start_date VARCHAR(8) ,
350

351
          end_date VARCHAR(8) ,
352

353
          employee_office VARCHAR(4) ,
354

355
          run_period_month VARCHAR(2)
356

357
        )
358

359

360

361
    INSERT  INTO @payroll_header_tmp
362

363
            SELECT DISTINCT
364

365
                    md2.EmployeeID ,
366

367
                    ph.StartDate ,
368

369
                    ph.EndDate ,
370

371
					CASE WHEN md2.TaxOffice <> '' THEN md2.TaxOffice ELSE md2.EmployeeOffice END ,   --------- //// Edit By Tri nwh 20220125  -----md2.EmployeeOffice
372

373
                    RunPeriodMonth 
374

375
            FROM    dbo.PHRPYTR0300 ph
376

377
                    INNER JOIN dbo.PHRPA0002 md2 ON md2.EmployeeID = ph.EmployeeID
378

379
            WHERE   ph.StartDate >= @begda
380

381
                    AND ph.EndDate <= @endda
382

383
                    AND md2.StartDate <= ph.EndDate
384

385
                    AND md2.EndDate >= ph.EndDate
386

387
                    AND md2.EmployeeID BETWEEN @EmployeeIDFrom
388

389
                                       AND     @EmployeeIDTo
390

391
				
392

393
--//get the latest run data
394

395
    INSERT  INTO @payroll_header0
396

397
            SELECT  py_head.*
398

399
            FROM    @payroll_header_tmp AS py_head
400

401
                    INNER JOIN ( SELECT employee_id ,
402

403
                                        start_date ,
404

405
                                        end_date ,
406

407
                                        MAX(run_period_month) AS run_period_month
408

409
                                 FROM   @payroll_header_tmp
410

411
                                 GROUP BY employee_id ,
412

413
                                        start_date ,
414

415
                                        end_date
416

417
                               ) a ON py_head.employee_id = a.employee_id
418

419
                                      AND py_head.start_date = a.start_date
420

421
                                      AND py_head.end_date = a.end_date
422

423
                                      AND py_head.run_period_month = a.run_period_month
424

425
			
426

427
    INSERT  INTO @payroll_header
428

429
            SELECT DISTINCT
430

431
                    employee_id ,
432

433
                    start_date ,
434

435
                    end_date ,
436

437
                    employee_office ,
438

439
                    run_period_month
440

441
            FROM    @payroll_header0 ph0
442

443
                    INNER JOIN @emp_office eo ON ph0.employee_office = eo.EmployeeOffice
444

445
      
446

447
    INSERT  INTO @payroll_header  -------- //// add By Tri nwh 20220125
448

449
            SELECT DISTINCT
450

451
                    employee_id ,
452

453
                    start_date ,
454

455
                    end_date ,
456

457
                    employee_office ,
458

459
                    run_period_month
460

461
            FROM    @payroll_header0 ph0
462

463
            WHERE ph0.employee_office = @TaxOffice
464

465
										
466

467
----//get list spliiter running payroll for others TAX Office.
468

469
    SELECT DISTINCT
470

471
            employee_id ,
472

473
            start_date ,
474

475
            end_date ,
476

477
            employee_office ,
478

479
            run_period_month
480

481
    INTO    #tmp0
482

483
    FROM    @payroll_header0 ph0
484

485
    WHERE   NOT EXISTS ( SELECT *
486

487
                         FROM   @payroll_header ph
488

489
                         WHERE  ph0.employee_id = ph.employee_id
490

491
                                AND ph0.start_date = ph.start_date
492

493
                                AND ph0.end_date = ph.end_date
494

495
                                AND ph0.employee_office = ph.employee_office
496

497
                                AND ph0.run_period_month = ph.run_period_month )
498

499
		
500

501
-- Get list date range splitter.
502

503
    SELECT  a.employee_id ,
504

505
            a.start_date mindt0 ,
506

507
            b.start_date maxdt0
508

509
    INTO    #tmp
510

511
    FROM    @payroll_header a ,
512

513
            @payroll_header b
514

515
    WHERE   a.employee_id = b.employee_id
516

517
            AND b.start_date > a.start_date
518

519
            AND ( CONVERT(INT, SUBSTRING(b.start_date, 5, 2))
520

521
                  - CONVERT(INT, SUBSTRING(a.start_date, 5, 2)) ) > 1
522

523
            AND ( SELECT    COUNT(*)
524

525
                  FROM      @payroll_header c
526

527
                  WHERE     ( CONVERT(INT, SUBSTRING(a.start_date, 5, 2)) < CONVERT(INT, SUBSTRING(c.start_date,
528

529
                                                              5, 2)) )
530

531
                            AND ( CONVERT(INT, SUBSTRING(b.start_date, 5, 2)) > CONVERT(INT, SUBSTRING(c.end_date,
532

533
                                                              5, 2)) )
534

535
                            AND c.employee_id = a.employee_id
536

537
                ) = 0
538

539
            AND ( SELECT    COUNT(*)
540

541
                  FROM      #tmp0 d
542

543
                  WHERE     d.employee_id = a.employee_id
544

545
                            AND a.start_date < d.start_date
546

547
                            AND b.start_date > d.end_date
548

549
                ) <> 0
550

551
	
552

553
    DROP TABLE #tmp0
554

555

556

557

558

559
    DECLARE @n_empty_range INT
560

561
	  
562

563
    SELECT  @n_empty_range = COUNT(*)
564

565
    FROM    #tmp	  
566

567

568

569
    IF ( @n_empty_range <> 0 ) 
570

571
        BEGIN
572

573
            INSERT  INTO @employee_tobe_process
574

575
                    SELECT  ddd.employee_id ,
576

577
                            ddd.mindt ,
578

579
                            ddd.maxdt
580

581
                    FROM    ( SELECT    dd.employee_id ,
582

583
                                        MIN(dd.start_date) mindt ,
584

585
                                        MAX(dd.end_date) maxdt
586

587
                              FROM      ( SELECT    x.employee_id ,
588

589
                                                    x.start_date ,
590

591
                                                    x.end_date ,
592

593
                                                    SUM(ISNULL(d.flag, 0)) flag
594

595
                                          FROM      @payroll_header x
596

597
                                                    LEFT OUTER JOIN ( SELECT
598

599
                                                              a.* ,
600

601
                                                              1 'flag'
602

603
                                                              FROM
604

605
                                                              @payroll_header a
606

607
                                                              LEFT OUTER JOIN #tmp b ON a.employee_id = b.employee_id
608

609
                                                              WHERE
610

611
                                                              a.start_date <= b.mindt0
612

613
                                                              AND a.end_date < b.maxdt0
614

615
                                                              ) d ON x.employee_id = d.employee_id
616

617
                                                              AND x.start_date = d.start_date
618

619
                                                              AND x.end_date = d.end_date
620

621
                                          GROUP BY  x.employee_id ,
622

623
                                                    x.start_date ,
624

625
                                                    x.end_date
626

627
                                        ) dd
628

629
                              GROUP BY  dd.employee_id ,
630

631
                                        dd.flag
632

633
                            ) ddd
634

635
                    ORDER BY ddd.employee_id ,
636

637
                            ddd.mindt	
638

639
        END
640

641
    ELSE 
642

643
        BEGIN
644

645
            INSERT  INTO @employee_tobe_process
646

647
                    SELECT  main.employee_id ,
648

649
                            mindate.mindt ,
650

651
                            maxdate.maxdt
652

653
                    FROM    ( SELECT DISTINCT
654

655
                                        employee_id
656

657
                              FROM      @payroll_header
658

659
                            ) main
660

661
                            INNER JOIN ( SELECT ph.employee_id ,
662

663
                                                MIN(ph.start_date) mindt
664

665
                                         FROM   @payroll_header ph
666

667
                                         GROUP BY employee_id
668

669
                                       ) mindate ON main.employee_id = mindate.employee_id
670

671
                            INNER JOIN ( SELECT employee_id ,
672

673
                                                MAX(end_date) maxdt
674

675
                                         FROM   @payroll_header ph
676

677
                                         GROUP BY employee_id
678

679
                                       ) maxdate ON main.employee_id = maxdate.employee_id
680

681
        END
682

683
				
684

685
    DROP TABLE #tmp           
686

687
                
688

689
    DECLARE @kode_form VARCHAR(7)
690

691
    DECLARE @tahun_pajak VARCHAR(4)
692

693
    DECLARE @pembetulan INT
694

695
    DECLARE @nomor_urut VARCHAR(7)
696

697
    DECLARE @npwp_pegawai VARCHAR(15)
698

699
    DECLARE @nama_pegawai VARCHAR(50)
700

701
    DECLARE @alamat_pegawai VARCHAR(255)
702

703
    DECLARE @jabatan_pegawai VARCHAR(30)
704

705
    DECLARE @jenis_kelamin VARCHAR(1)
706

707
    DECLARE @status_pegawai VARCHAR(1)
708

709
    DECLARE @status_kawin VARCHAR(1)
710

711
    DECLARE @flag_asing VARCHAR(1)
712

713
    DECLARE @status_ptkp VARCHAR(2)
714

715
    DECLARE @jumlah_tanggungan VARCHAR(1)
716

717
    DECLARE @masa_perolehan_1 VARCHAR(2)
718

719
    DECLARE @masa_perolehan_2 VARCHAR(2)
720

721
    DECLARE @flag_status VARCHAR(1)
722

723
    DECLARE @a1 DECIMAL
724

725
    DECLARE @flg_a2 VARCHAR(1)
726

727
    DECLARE @a2 DECIMAL
728

729
    DECLARE @a3 DECIMAL
730

731
    DECLARE @a4 DECIMAL
732

733
    DECLARE @a5 DECIMAL
734

735
    DECLARE @a6 DECIMAL
736

737
    DECLARE @a7 DECIMAL
738

739
    DECLARE @a8 DECIMAL
740

741
    DECLARE @a9 DECIMAL
742

743
    DECLARE @a10 DECIMAL
744

745
    DECLARE @a11 DECIMAL
746

747
    DECLARE @a12 DECIMAL
748

749
    DECLARE @a13 DECIMAL
750

751
    DECLARE @a14 DECIMAL
752

753
    DECLARE @a15 DECIMAL
754

755
    DECLARE @a16 DECIMAL
756

757
    DECLARE @a16_flag DECIMAL
758

759
    DECLARE @a17 DECIMAL
760

761
    DECLARE @a18 DECIMAL
762

763
    DECLARE @a19 DECIMAL
764

765
    DECLARE @a19b DECIMAL
766

767
    DECLARE @a20 DECIMAL
768

769
    DECLARE @a21 DECIMAL
770

771
    DECLARE @a22 DECIMAL
772

773
    DECLARE @a22a DECIMAL
774

775
    DECLARE @a22b DECIMAL
776

777
    DECLARE @a23 DECIMAL
778

779
    DECLARE @a24 DECIMAL
780

781
    DECLARE @flg_a24 VARCHAR(1)
782

783
    DECLARE @bln_a24 VARCHAR(6)
784

785
    DECLARE @masa_pajak VARCHAR(10)
786

787
    DECLARE @kode_pajak VARCHAR(30)
788

789
    DECLARE @npwp_pemotong VARCHAR(15)
790

791
    DECLARE @nama_pemotong VARCHAR(200)
792

793
    DECLARE @company_name VARCHAR(200)
794

795
    DECLARE @a19murni DECIMAL
796

797
    DECLARE @a17_att DECIMAL
798

799
    DECLARE @ktp VARCHAR(50)
800

801

802

803
    DECLARE @jml_bulan_perolehan INT
804

805
    DECLARE @tot_biaya_jabatan DECIMAL
806

807

808

809
    DECLARE @result TABLE
810

811
        (
812

813
          kode_form VARCHAR(7) ,
814

815
          tahun_pajak VARCHAR(4) ,
816

817
          pembetulan INT ,
818

819
          nomor_urut CHAR(7) ,
820

821
          npwp_pegawai VARCHAR(30) ,
822

823
          nama_pegawai VARCHAR(50) ,
824

825
          alamat_pegawai VARCHAR(255) ,
826

827
          jabatan_pegawai VARCHAR(30) ,
828

829
          jenis_kelamin VARCHAR(5) ,
830

831
          status_pegawai VARCHAR(5) ,
832

833
          status_kawin VARCHAR(5) ,
834

835
          flag_asing VARCHAR(1) ,
836

837
          status_ptkp VARCHAR(5) ,
838

839
          jumlah_tanggungan VARCHAR(1) ,
840

841
          masa_perolehan_1 VARCHAR(2) ,
842

843
          masa_perolehan_2 VARCHAR(2) ,
844

845
          flag_status VARCHAR(1) ,
846

847
          a1 DECIMAL ,
848

849
          flg_a2 VARCHAR(1) ,
850

851
          a2 DECIMAL ,
852

853
          a3 DECIMAL ,
854

855
          a4 DECIMAL ,
856

857
          a5 DECIMAL ,
858

859
          a6 DECIMAL ,
860

861
          a7 DECIMAL ,
862

863
          a8 DECIMAL ,
864

865
          a9 DECIMAL ,
866

867
          a10 DECIMAL ,
868

869
          a11 DECIMAL ,
870

871
          a12 DECIMAL ,
872

873
          a13 DECIMAL ,
874

875
          a14 DECIMAL ,
876

877
          a15 DECIMAL ,
878

879
          a16 DECIMAL ,
880

881
          a17 DECIMAL ,
882

883
          a18 DECIMAL ,
884

885
          a19 DECIMAL ,
886

887
          a20 DECIMAL ,
888

889
          a21 DECIMAL ,
890

891
          a22 DECIMAL ,
892

893
          a22a DECIMAL ,
894

895
          a22b DECIMAL ,
896

897
          a23 DECIMAL ,
898

899
          a24 DECIMAL ,
900

901
          flg_a24 VARCHAR(1) ,
902

903
          bln_a24 VARCHAR(6)
904

905
	--,emp_id VARCHAR(20)
906

907
          ,
908

909
          KTP VARCHAR(50) ,
910

911
          nama_pemotong VARCHAR(200) ,
912

913
          npwp_pemotong VARCHAR(30) ,
914

915
          kode_pajak VARCHAR(30)
916

917
	--,company_name VARCHAR(200)
918

919
        )                   
920

921
 
922

923
    DECLARE @cu0300 TABLE
924

925
        (
926

927
          [code] [nVARCHAR](4) NOT NULL ,
928

929
          [start_date] [nVARCHAR](8) NOT NULL ,
930

931
          [end_date] [nVARCHAR](8) NOT NULL ,
932

933
          [spt_no] [INT] NULL
934

935
        )
936

937

938

939
    DECLARE @cu0300_tmp TABLE
940

941
        (
942

943
          [code] [nVARCHAR](4) NOT NULL ,
944

945
          [start_date] [nVARCHAR](8) NOT NULL ,
946

947
          [end_date] [nVARCHAR](8) NOT NULL ,
948

949
          [spt_no] [INT] NULL
950

951
        )
952

953
	
954

955
    DECLARE @tbl_tr_prev_ori TABLE
956

957
        (
958

959
          employee_id VARCHAR(8) ,
960

961
          wage_type VARCHAR(4) ,
962

963
          amount DECIMAL ,
964

965
          spt_no INT ,
966

967
          run_period_month VARCHAR(2)
968

969
        )
970

971

972

973
    DECLARE @tbl_tr_prev_ori_tmp TABLE
974

975
        (
976

977
          employee_id VARCHAR(8) ,
978

979
          wage_type VARCHAR(4) ,
980

981
          amount DECIMAL ,
982

983
          spt_no INT ,
984

985
          run_period_month VARCHAR(2)
986

987
        )
988

989
 
990

991
    DECLARE @tbl_tr_prev_cum TABLE
992

993
        (
994

995
          employee_id VARCHAR(8) ,
996

997
          wage_type VARCHAR(4) ,
998

999
          amount DECIMAL ,
1000

1001
          spt_no INT ,
1002

1003
          run_period_month VARCHAR(2)
1004

1005
        )
1006

1007

1008

1009
    DECLARE @tbl_tr_prev_cum_tmp TABLE
1010

1011
        (
1012

1013
          employee_id VARCHAR(8) ,
1014

1015
          wage_type VARCHAR(4) ,
1016

1017
          amount DECIMAL ,
1018

1019
          spt_no INT ,
1020

1021
          run_period_month VARCHAR(2)
1022

1023
        )
1024

1025

1026

1027
    DECLARE @tbl_tr_dec_ori TABLE
1028

1029
        (
1030

1031
          employee_id VARCHAR(8) ,
1032

1033
          wage_type VARCHAR(4) ,
1034

1035
          amount DECIMAL ,
1036

1037
          spt_no INT ,
1038

1039
          run_period_month VARCHAR(2)
1040

1041
        )
1042

1043

1044

1045
    DECLARE @tbl_tr_dec_ori_tmp TABLE
1046

1047
        (
1048

1049
          employee_id VARCHAR(8) ,
1050

1051
          wage_type VARCHAR(4) ,
1052

1053
          amount DECIMAL ,
1054

1055
          spt_no INT ,
1056

1057
          run_period_month VARCHAR(2)
1058

1059
        )
1060

1061
 
1062

1063
    DECLARE @tbl_tr_dec TABLE
1064

1065
        (
1066

1067
          employee_id VARCHAR(8) ,
1068

1069
          wage_type VARCHAR(4) ,
1070

1071
          amount DECIMAL ,
1072

1073
          spt_no INT ,
1074

1075
          run_period_month VARCHAR(2)
1076

1077
        )
1078

1079

1080

1081
    DECLARE @tbl_tr_dec_tmp TABLE
1082

1083
        (
1084

1085
          employee_id VARCHAR(8) ,
1086

1087
          wage_type VARCHAR(4) ,
1088

1089
          amount DECIMAL ,
1090

1091
          spt_no INT ,
1092

1093
          run_period_month VARCHAR(2)
1094

1095
        )
1096

1097

1098

1099
    DECLARE @tbl_tr_dec_total TABLE
1100

1101
        (
1102

1103
          employee_id VARCHAR(8) ,
1104

1105
          amount DECIMAL ,
1106

1107
          spt_no INT
1108

1109
        )
1110

1111
	 
1112

1113
    DECLARE @tbl_tr_enc TABLE
1114

1115
        (
1116

1117
          employee_id VARCHAR(8) ,
1118

1119
          wage_type VARCHAR(4) ,
1120

1121
          amount VARCHAR(250) ,
1122

1123
          spt_no INT
1124

1125
        )
1126

1127
                   
1128

1129
    SET @kode_form = 'D113248'
1130

1131
    SET @tahun_pajak = @Year
1132

1133
    SET @pembetulan = 0
1134

1135
 
1136

1137
    DECLARE @c_landscape VARCHAR(3)
1138

1139
    DECLARE @c_emp_id VARCHAR(8)
1140

1141
    DECLARE @c_start_date INT
1142

1143
    DECLARE @c_end_date INT
1144

1145

1146

1147
    DECLARE @last_period_payroll INT
1148

1149
    DECLARE @previous_period_payroll INT
1150

1151
    DECLARE @previous_period_payroll0 INT
1152

1153
    DECLARE @previous_period_payroll_varchar VARCHAR(2)
1154

1155

1156

1157
----//Run Cursor
1158

1159
    DECLARE cur_employee CURSOR
1160

1161
    FOR
1162

1163
        SELECT  *
1164

1165
        FROM    @employee_tobe_process
1166

1167
    OPEN cur_employee
1168

1169
    FETCH cur_employee INTO @c_emp_id, @c_start_date, @c_end_date
1170

1171
    WHILE @@fetch_status = 0 
1172

1173
        BEGIN  
1174

1175
            SET @last_period_payroll = @c_end_date
1176

1177
            SET @nomor_urut = CONVERT(INTEGER, RIGHT(@c_emp_id, 7))
1178

1179

1180

1181
            SELECT  @npwp_pegawai = REPLACE(REPLACE(ISNULL(NPWP,
1182

1183
                                                           '000000000000000'),
1184

1185
                                                    '.', ''), '-', '') ,
1186

1187
                    @status_kawin = CASE LEFT(TaxStatus, 1)
1188

1189
                                      WHEN 'T' THEN '1'
1190

1191
                                      ELSE '2'
1192

1193
                                    END ,
1194

1195
                    @status_ptkp = a.TaxStatus ,
1196

1197
                    @jumlah_tanggungan = CASE RIGHT(TaxStatus, 1)
1198

1199
                                           WHEN 'K' THEN '0'
1200

1201
                                           ELSE RIGHT(TaxStatus, 1)
1202

1203
                                         END
1204

1205
            FROM    dbo.PHRPA0003 AS a
1206

1207
            WHERE   EmployeeID = @c_emp_id
1208

1209
                    AND StartDate <= @c_end_date
1210

1211
                    AND EndDate >= @c_end_date
1212

1213
	
1214

1215
            SELECT  DISTINCT
1216

1217
                    @a17 = PTKP
1218

1219
            FROM    dbo.PHRPYCU0302
1220

1221
            WHERE   StartDate <= @c_end_date
1222

1223
                    AND EndDate >= @c_end_date
1224

1225
                    AND TaxStatus = ( SELECT DISTINCT
1226

1227
                                                TaxStatus
1228

1229
                                      FROM      dbo.PHRPA0003
1230

1231
                                      WHERE     EmployeeID = @c_emp_id
1232

1233
                                                AND StartDate <= @c_end_date
1234

1235
                                                AND EndDate >= @c_end_date
1236

1237
                                    )
1238

1239

1240

1241
            SELECT  @nama_pegawai = FullName ,
1242

1243
                    @jenis_kelamin = b.GenderDescription ,
1244

1245
                    ----@ktp = c.IDDescription -----  dikomen by Tri
1246

1247
					@ktp = a.IDCard
1248

1249
            FROM    dbo.PHRPA0001 AS a
1250

1251
                    LEFT JOIN dbo.PHRPAGENDER AS b ON a.Gender = b.Gender
1252

1253
                                                      AND b.StartDate <= @c_end_date
1254

1255
                                                      AND b.EndDate >= @c_end_date
1256

1257
                    LEFT JOIN dbo.PHRPA0013 AS c ON a.EmployeeID = c.EmployeeID
1258

1259
                                                    AND c.StartDate <= @c_end_date
1260

1261
                                                    AND c.EndDate >= @c_end_date
1262

1263
													AND c.IDType = '01' -----  add by Tri
1264

1265
            WHERE   a.EmployeeID = @c_emp_id
1266

1267
                    AND a.StartDate <= @c_end_date
1268

1269
                    AND a.EndDate >= @c_end_date
1270

1271
                    -----AND c.IDType = '01' -----  dikomen by Tri
1272

1273
	 
1274

1275
            SELECT  @nama_pemotong = t.TaxOfficeDescription ,
1276

1277
                    @npwp_pemotong = o.NPWP
1278

1279
            FROM    dbo.PHRPA0002 AS a
1280

1281
                    LEFT JOIN dbo.PCMEPEMPOFF AS o ON a.EmployeeOffice = o.EmployeeOffice
1282

1283
                                                      AND o.StartDate <= @c_end_date
1284

1285
                                                      AND o.EndDate >= @c_end_date
1286

1287
                    LEFT JOIN dbo.PHRPYTAXOFF AS t ON t.TaxOffice = o.TaxOffice
1288

1289
                                                      AND t.StartDate <= @c_end_date
1290

1291
                                                      AND t.EndDate >= @c_end_date
1292

1293
            WHERE   a.StartDate <= @c_end_date
1294

1295
                    AND a.EndDate >= @c_end_date
1296

1297
                    AND a.EmployeeID = @c_emp_id
1298

1299
			
1300

1301
            SELECT  @kode_pajak = e.TaxCode
1302

1303
            FROM    dbo.PHRPA0002 AS a
1304

1305
                    LEFT JOIN dbo.PCMEPEMPTYP AS e ON a.EmployeeType = e.EmployeeType
1306

1307
            WHERE   a.StartDate <= @c_end_date
1308

1309
                    AND a.EndDate >= @c_end_date
1310

1311
                    AND a.EmployeeID = @c_emp_id
1312

1313
			
1314

1315
	--//alamat & flag status belakangan
1316

1317
            SELECT  @alamat_pegawai = REPLACE(md06.Address, ',', ' ')
1318

1319
            FROM    dbo.PHRPA0006 AS md06
1320

1321
            WHERE   EmployeeID = @c_emp_id
1322

1323
                    AND StartDate <= @c_end_date
1324

1325
                    AND EndDate >= @c_end_date
1326

1327
                    AND AddressType = @npwp_address_subtype
1328

1329

1330

1331
            SELECT  @jabatan_pegawai = mdobj.ObjectDescription
1332

1333
            FROM    dbo.PHRPA0002 AS md02
1334

1335
                    INNER JOIN dbo.PHROM0001 AS mdobj ON md02.Position = mdobj.ObjectID
1336

1337
                                                         AND mdobj.ObjectClass = 'P'
1338

1339
                                                         AND mdobj.StartDate <= @c_end_date
1340

1341
                                                         AND mdobj.EndDate >= @c_end_date
1342

1343
            WHERE   md02.EmployeeID = @c_emp_id
1344

1345
                    AND md02.StartDate <= @c_end_date
1346

1347
                    AND md02.EndDate >= @c_end_date
1348

1349
	
1350

1351
            SELECT  @status_pegawai = CASE EmployeeStatus
1352

1353
                                        WHEN '01' THEN '1'
1354

1355
                                        WHEN '03' THEN '2'
1356

1357
                                        ELSE '1'
1358

1359
                                      END ,
1360

1361
                    @flag_asing = CASE EmployeeType
1362

1363
                                    WHEN '05' THEN '1'
1364

1365
                                    ELSE '0'
1366

1367
                                  END
1368

1369
            FROM    dbo.PHRPA0002 AS md02
1370

1371
            WHERE   EmployeeID = @c_emp_id
1372

1373
                    AND StartDate <= @c_end_date
1374

1375
                    AND EndDate >= @c_end_date
1376

1377

1378

1379
            SET @masa_perolehan_1 = CONVERT(INTEGER, SUBSTRING(CONVERT(VARCHAR, @c_start_date),
1380

1381
                                                              5, 2))
1382

1383
            SET @masa_perolehan_2 = CONVERT(INTEGER, SUBSTRING(CONVERT(VARCHAR, @c_end_date),
1384

1385
                                                              5, 2))
1386

1387
            SET @jml_bulan_perolehan = ( CONVERT(INT, @masa_perolehan_2)
1388

1389
                                         - CONVERT(INT, @masa_perolehan_1) + 1 )
1390

1391

1392

1393
	--//#region getwtconfiguration
1394

1395
            DELETE  @cu0300_tmp
1396

1397

1398

1399
            INSERT  INTO @cu0300_tmp
1400

1401
                    SELECT  WageTypeDefinition ,
1402

1403
                            StartDate ,
1404

1405
                            EndDate ,
1406

1407
                            SPTNo
1408

1409
                    FROM    dbo.PHRPYCU0300
1410

1411
                    WHERE   StartDate <= @c_end_date
1412

1413
                            AND EndDate >= @c_start_date
1414

1415
                            AND SPTNo > 0
1416

1417

1418

1419
            UPDATE  @cu0300_tmp
1420

1421
            SET     start_date = @begda
1422

1423
            WHERE   start_date < @c_start_date
1424

1425
	  
1426

1427
            UPDATE  @cu0300_tmp
1428

1429
            SET     end_date = @endda
1430

1431
            WHERE   end_date > @c_end_date
1432

1433

1434

1435
--/*
1436

1437
            DELETE  @cu0300
1438

1439

1440

1441
            INSERT  INTO @cu0300
1442

1443
                    SELECT  cu0300.*
1444

1445
                    FROM    @cu0300_tmp AS cu0300
1446

1447
                            INNER JOIN ( SELECT code ,
1448

1449
                                                MIN(start_date) AS start_date ,
1450

1451
                                                MAX(end_date) AS end_date
1452

1453
                                         FROM   @cu0300_tmp
1454

1455
                                         GROUP BY code
1456

1457
                                       ) a ON cu0300.code = a.code
1458

1459
                                              AND cu0300.end_date = a.end_date
1460

1461

1462

1463
            DELETE  @tbl_tr_dec
1464

1465
            DELETE  @tbl_tr_dec_ori
1466

1467

1468

1469
            DELETE  @tbl_tr_dec_tmp
1470

1471
            DELETE  @tbl_tr_dec_ori_tmp
1472

1473

1474

1475
            DECLARE @cc_landscape VARCHAR(3)
1476

1477
            DECLARE @cc_code VARCHAR(4)
1478

1479
            DECLARE @cc_start_date INT
1480

1481
            DECLARE @cc_end_date INT
1482

1483
            DECLARE @cc_spt_no INT
1484

1485

1486

1487
            INSERT  INTO @tbl_tr_dec_tmp
1488

1489
                    SELECT  tr301.EmployeeID ,
1490

1491
                            tr301.WageType ,
1492

1493
                            CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'),
1494

1495
                                         ',', '.') AS DECIMAL(22, 0)) ,
1496

1497
                            cu300.spt_no ,
1498

1499
                            tr301.RunPeriodMonth
1500

1501
                    FROM    dbo.PHRPYTR0301CUM AS tr301
1502

1503
                            INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1504

1505
                    WHERE   tr301.EmployeeID = @c_emp_id
1506

1507
                            AND tr301.PayPeriodMonth = SUBSTRING(CONVERT(VARCHAR, @last_period_payroll),
1508

1509
                                                              5, 2)
1510

1511
                            AND tr301.PayPeriodYear = tr301.RunPeriodYear
1512

1513
                            AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
1514

1515
                                                           4)
1516

1517
		  
1518

1519
	--//get the latest run data
1520

1521
            INSERT  INTO @tbl_tr_dec
1522

1523
                    SELECT  tr_dec.*
1524

1525
                    FROM    @tbl_tr_dec_tmp AS tr_dec
1526

1527
                            INNER JOIN ( SELECT employee_id ,
1528

1529
                                                wage_type ,
1530

1531
                                                MAX(run_period_month) AS run_period_month
1532

1533
                                         FROM   @tbl_tr_dec_tmp
1534

1535
                                         GROUP BY employee_id ,
1536

1537
                                                wage_type
1538

1539
                                       ) a ON tr_dec.employee_id = a.employee_id
1540

1541
                                              AND tr_dec.wage_type = a.wage_type
1542

1543
                                              AND tr_dec.run_period_month = a.run_period_month
1544

1545

1546

1547

1548

1549
            INSERT  INTO @tbl_tr_dec_ori_tmp
1550

1551
                    SELECT  tr301.EmployeeID ,
1552

1553
                            tr301.WageType ,
1554

1555
                            CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'),
1556

1557
                                         ',', '.') AS DECIMAL(22, 0)) ,
1558

1559
                            cu300.spt_no ,
1560

1561
                            tr301.RunPeriodMonth
1562

1563
                    FROM    dbo.PHRPYTR0301 AS tr301
1564

1565
                            INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1566

1567
                    WHERE   tr301.EmployeeID = @c_emp_id
1568

1569
                            AND tr301.PayPeriodMonth = SUBSTRING(CONVERT(VARCHAR, @last_period_payroll),
1570

1571
                                                              5, 2)
1572

1573
                            AND tr301.PayPeriodYear = tr301.RunPeriodYear
1574

1575
                            AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
1576

1577
                                                           4)
1578

1579
		  
1580

1581
	--//get the latest run data
1582

1583
            INSERT  INTO @tbl_tr_dec_ori
1584

1585
                    SELECT  tr_dec_ori.*
1586

1587
                    FROM    @tbl_tr_dec_ori_tmp AS tr_dec_ori
1588

1589
                            INNER JOIN ( SELECT employee_id ,
1590

1591
                                                wage_type ,
1592

1593
                                                MAX(run_period_month) AS run_period_month
1594

1595
                                         FROM   @tbl_tr_dec_ori_tmp
1596

1597
                                         GROUP BY employee_id ,
1598

1599
                                                wage_type
1600

1601
                                       ) a ON tr_dec_ori.employee_id = a.employee_id
1602

1603
                                              AND tr_dec_ori.wage_type = a.wage_type
1604

1605
                                              AND tr_dec_ori.run_period_month = a.run_period_month
1606

1607
		
1608

1609
            IF ( @masa_perolehan_1 > '1'
1610

1611
                 OR @masa_perolehan_1 > '01'
1612

1613
               ) 
1614

1615
                BEGIN
1616

1617
                    SET @previous_period_payroll = CONVERT(INT, @masa_perolehan_1)
1618

1619
                        - 1
1620

1621
                    IF ( @previous_period_payroll < 10 ) 
1622

1623
                        SET @previous_period_payroll_varchar = '0'
1624

1625
                            + CONVERT(VARCHAR(1), @previous_period_payroll)
1626

1627
                    ELSE 
1628

1629
                        SET @previous_period_payroll_varchar = CONVERT(VARCHAR(2), @previous_period_payroll)
1630

1631
			
1632

1633
		--//get the latest pay period if previous period no pay period.
1634

1635
                    SET @previous_period_payroll0 = @previous_period_payroll
1636

1637
                    SELECT TOP ( 1 )
1638

1639
                            @previous_period_payroll0 = CONVERT(INT, PayPeriodMonth)
1640

1641
                    FROM    dbo.PHRPYTR0300
1642

1643
                    WHERE   EmployeeID = @c_emp_id
1644

1645
                            AND PayPeriodMonth <= @previous_period_payroll_varchar
1646

1647
                            AND PayPeriodYear = LEFT(@last_period_payroll, 4)
1648

1649
                            AND RunPeriodYear = LEFT(@last_period_payroll, 4)
1650

1651
                    ORDER BY PayPeriodMonth DESC
1652

1653
			
1654

1655
                    IF ( @previous_period_payroll0 < 10 ) 
1656

1657
                        SET @previous_period_payroll_varchar = '0'
1658

1659
                            + CONVERT(VARCHAR(1), @previous_period_payroll0)
1660

1661
                    ELSE 
1662

1663
                        SET @previous_period_payroll_varchar = CONVERT(VARCHAR(2), @previous_period_payroll0)
1664

1665
					
1666

1667
                    DELETE  FROM @tbl_tr_prev_ori
1668

1669
                    DELETE  FROM @tbl_tr_prev_cum
1670

1671
		
1672

1673
                    DELETE  FROM @tbl_tr_prev_ori_tmp
1674

1675
                    DELETE  FROM @tbl_tr_prev_cum_tmp
1676

1677

1678

1679
                    INSERT  INTO @tbl_tr_prev_ori_tmp
1680

1681
                            SELECT  tr301.EmployeeID ,
1682

1683
                                    tr301.WageType ,
1684

1685
                                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount),
1686

1687
                                                        '0'), ',', '.') AS DECIMAL(22,
1688

1689
                                                              0)) ,
1690

1691
                                    cu300.spt_no ,
1692

1693
                                    tr301.RunPeriodMonth
1694

1695
                            FROM    dbo.PHRPYTR0301 AS tr301
1696

1697
                                    INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1698

1699
                            WHERE   tr301.EmployeeID = @c_emp_id
1700

1701
                                    AND tr301.PayPeriodMonth = @previous_period_payroll_varchar
1702

1703
                                    AND tr301.PayPeriodYear = tr301.RunPeriodYear
1704

1705
                                    AND tr301.RunPeriodMonth = LEFT(@last_period_payroll,4)
1706

1707

1708

1709
		--//get the latest run data
1710

1711
                    INSERT  INTO @tbl_tr_prev_ori
1712

1713
                            SELECT  tr_prev_ori.*
1714

1715
                            FROM    @tbl_tr_prev_ori_tmp AS tr_prev_ori
1716

1717
                                    INNER JOIN ( SELECT employee_id ,
1718

1719
                                                        wage_type ,
1720

1721
                                                        MAX(run_period_month) AS run_period_month
1722

1723
                                                 FROM   @tbl_tr_prev_ori_tmp
1724

1725
                                                 GROUP BY employee_id ,
1726

1727
                                                        wage_type
1728

1729
                                               ) a ON tr_prev_ori.employee_id = a.employee_id
1730

1731
                                                      AND tr_prev_ori.wage_type = a.wage_type
1732

1733
                                                      AND tr_prev_ori.run_period_month = a.run_period_month
1734

1735
					
1736

1737
                    INSERT  INTO @tbl_tr_prev_cum_tmp
1738

1739
                            SELECT  tr301.EmployeeID ,
1740

1741
                                    tr301.WageType ,
1742

1743
                                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount),
1744

1745
                                                        '0'), ',', '.') AS DECIMAL(22,
1746

1747
                                                              0)) ,
1748

1749
                                    cu300.spt_no ,
1750

1751
                                    tr301.RunPeriodMonth
1752

1753
                            FROM    dbo.PHRPYTR0301CUM AS tr301
1754

1755
                                    INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
1756

1757
                            WHERE   tr301.EmployeeID = @c_emp_id
1758

1759
                                    AND tr301.PayPeriodMonth = @previous_period_payroll_varchar
1760

1761
                                    AND tr301.PayPeriodYear = tr301.RunPeriodYear
1762

1763
                                    AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
1764

1765
                                                              4)
1766

1767

1768

1769
		--//get the latest run data
1770

1771
                    INSERT  INTO @tbl_tr_prev_cum
1772

1773
                            SELECT  tr_prev_cum.*
1774

1775
                            FROM    @tbl_tr_prev_cum_tmp AS tr_prev_cum
1776

1777
                                    INNER JOIN ( SELECT employee_id ,
1778

1779
                                                        wage_type ,
1780

1781
                                                        MAX(run_period_month) AS run_period_month
1782

1783
                                                 FROM   @tbl_tr_prev_cum_tmp
1784

1785
                                                 GROUP BY employee_id ,
1786

1787
                                                        wage_type
1788

1789
                                               ) a ON tr_prev_cum.employee_id = a.employee_id
1790

1791
                                                      AND tr_prev_cum.wage_type = a.wage_type
1792

1793
                                                      AND tr_prev_cum.run_period_month = a.run_period_month	
1794

1795
                END 
1796

1797

1798

1799
            DECLARE @emp_status VARCHAR(2) ,
1800

1801
                @emp_type VARCHAR(2) ,
1802

1803
                @movement_type VARCHAR(2) ,
1804

1805
                @movement_reason VARCHAR(2) ,
1806

1807
                @emp_type_payroll VARCHAR(20) ,
1808

1809
                @movement_reason_payroll VARCHAR(20) ,
1810

1811
                @is_pegawai_baru VARCHAR(1)
1812

1813
		
1814

1815
            DECLARE @death_mov_reason VARCHAR(5)
1816

1817
            SELECT TOP ( 1 )
1818

1819
                    @death_mov_reason = Value1
1820

1821
            FROM    dbo.PCMEPGENPARAM
1822

1823
            WHERE   Parameter = 'HR_ESPT_MOV_REASON_DEATH'
1824

1825
                    AND StartDate <= @endda
1826

1827
                    AND EndDate >= @endda
1828

1829
		
1830

1831
            SELECT  @emp_status = md02.EmployeeStatus ,
1832

1833
                    @emp_type = md02.EmployeeType ,
1834

1835
                    @movement_type = md02.MovementType ,
1836

1837
                    @movement_reason = md02.MovementReason ,
1838

1839
                    @emp_type_payroll = ret.PayrollProcess ,
1840

1841
                    @movement_reason_payroll = rmr.PayrollProcess
1842

1843
            FROM    dbo.PHRPA0002 md02
1844

1845
                    INNER JOIN dbo.PCMEPEMPTYP ret ON md02.EmployeeType = ret.EmployeeType
1846

1847
                    INNER JOIN dbo.PCMEPMOVR rmr ON md02.MovementReason = rmr.MovementReason
1848

1849
            WHERE   EmployeeID = @c_emp_id
1850

1851
                    AND md02.StartDate <= @endda
1852

1853
                    AND md02.EndDate >= @endda
1854

1855
		
1856

1857
	
1858

1859
            SET @is_pegawai_baru = '0'
1860

1861
            IF @c_start_date > @begda 
1862

1863
                BEGIN
1864

1865
                    SELECT  @is_pegawai_baru = CASE WHEN HiringDate >= @c_start_date
1866

1867
                                                    THEN '1'
1868

1869
                                                    ELSE '0'
1870

1871
                                               END
1872

1873
                    FROM    dbo.PHRPA0015
1874

1875
                    WHERE   EmployeeID = @c_emp_id
1876

1877
                END
1878

1879

1880

1881
	--//Setting flag status
1882

1883
            IF @emp_status = '01'
1884

1885
                AND @masa_perolehan_1 = '1'
1886

1887
                AND @masa_perolehan_2 = '12'
1888

1889
                AND ISNULL(@emp_type_payroll, '') <> 'EXP' 
1890

1891
                SET @flag_status = '0'
1892

1893
            ELSE 
1894

1895
                IF @emp_status = '01'
1896

1897
                    AND @masa_perolehan_1 = '1'
1898

1899
                    AND @masa_perolehan_2 = '12'
1900

1901
                    AND ISNULL(@emp_type_payroll, '') = 'EXP' 
1902

1903
                    SET @flag_status = '3'
1904

1905
                ELSE 
1906

1907
                    IF @emp_status = '01'
1908

1909
                        AND @masa_perolehan_2 <> '12' 
1910

1911
                        SET @flag_status = '1'
1912

1913
                    ELSE 
1914

1915
                        IF @is_pegawai_baru = '1' 
1916

1917
                            SET @flag_status = '4'
1918

1919
                        ELSE 
1920

1921
                            IF @emp_status = '01'
1922

1923
                                AND @masa_perolehan_2 <> '1'
1924

1925
                                AND @masa_perolehan_2 = '12' 
1926

1927
                                SET @flag_status = '5'
1928

1929
                            ELSE 
1930

1931
                                IF ( @movement_type = '17'
1932

1933
                                     OR @emp_status <> '01'
1934

1935
                                   ) 
1936

1937
                                    BEGIN
1938

1939
                                        IF @movement_reason = '12' 
1940

1941
                                            SET @flag_status = '2'
1942

1943
                                        ELSE 
1944

1945
                                            IF @movement_reason = @death_mov_reason
1946

1947
                                                OR @emp_type_payroll = 'EXP' 
1948

1949
                                                SET @flag_status = '3'
1950

1951
                                            ELSE 
1952

1953
                                                IF @masa_perolehan_1 = '1'
1954

1955
                                                    AND @masa_perolehan_2 = '12' 
1956

1957
                                                    SET @flag_status = '0'
1958

1959
                                                ELSE 
1960

1961
                                                    SET @flag_status = '2'
1962

1963
                                    END
1964

1965

1966

1967
            SELECT  @a1 = ISNULL(SUM(amount), 0)
1968

1969
            FROM    @tbl_tr_dec
1970

1971
            WHERE   spt_no = 1
1972

1973
            GROUP BY spt_no
1974

1975

1976

1977
            IF ( @masa_perolehan_1 <> '1' ) 
1978

1979
                SELECT  @a1 = @a1 - ISNULL(SUM(amount), 0)
1980

1981
                FROM    @tbl_tr_prev_cum
1982

1983
                WHERE   spt_no = 1
1984

1985
                GROUP BY spt_no
1986

1987

1988

1989
            SET @a1 = ISNULL(@a1, 0)
1990

1991
            SELECT  @a2 = ISNULL(SUM(amount), 0)
1992

1993
            FROM    @tbl_tr_dec
1994

1995
            WHERE   spt_no = 2
1996

1997
            GROUP BY spt_no
1998

1999

2000

2001
            IF ( @masa_perolehan_1 <> '1' ) 
2002

2003
                SELECT  @a2 = @a2 - ISNULL(SUM(amount), 0)
2004

2005
                FROM    @tbl_tr_prev_cum
2006

2007
                WHERE   spt_no = 2
2008

2009
                GROUP BY spt_no
2010

2011
				  
2012

2013
				
2014

2015
            SET @a2 = ISNULL(@a2, 0)
2016

2017
	
2018

2019
            SELECT  @a3 = ISNULL(SUM(amount), 0)
2020

2021
            FROM    @tbl_tr_dec
2022

2023
            WHERE   spt_no = 3
2024

2025
            GROUP BY spt_no
2026

2027
	  
2028

2029
            IF ( @masa_perolehan_1 <> '1' ) 
2030

2031
                SELECT  @a3 = @a3 - ISNULL(SUM(amount), 0)
2032

2033
                FROM    @tbl_tr_prev_cum
2034

2035
                WHERE   spt_no = 3
2036

2037
                GROUP BY spt_no
2038

2039
				  
2040

2041
            SET @a3 = ISNULL(@a3, 0)
2042

2043
	
2044

2045
            SELECT  @a4 = ISNULL(SUM(amount), 0)
2046

2047
            FROM    @tbl_tr_dec
2048

2049
            WHERE   spt_no = 4
2050

2051
            GROUP BY spt_no
2052

2053
	
2054

2055
            SET @a4 = ISNULL(@a4, 0)
2056

2057
	
2058

2059
            SELECT  @a5 = ISNULL(SUM(amount), 0)
2060

2061
            FROM    @tbl_tr_dec
2062

2063
            WHERE   spt_no = 5
2064

2065
            GROUP BY spt_no
2066

2067
					  
2068

2069
            IF ( @masa_perolehan_1 <> '1' ) 
2070

2071
                SELECT  @a5 = @a5 - ISNULL(SUM(amount), 0)
2072

2073
                FROM    @tbl_tr_prev_cum
2074

2075
                WHERE   spt_no = 5
2076

2077
                GROUP BY spt_no
2078

2079
					  
2080

2081
            SET @a5 = ISNULL(@a5, 0)
2082

2083
            SELECT  @a6 = ISNULL(SUM(amount), 0)
2084

2085
            FROM    @tbl_tr_dec
2086

2087
            WHERE   spt_no = 6
2088

2089
            GROUP BY spt_no
2090

2091
	
2092

2093
            SET @a6 = ISNULL(@a6, 0)
2094

2095

2096

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

2099

2100

2101
            SELECT  @a8 = ISNULL(SUM(amount), 0)
2102

2103
            FROM    @tbl_tr_dec
2104

2105
            WHERE   spt_no = 8
2106

2107
            GROUP BY spt_no
2108

2109
					  
2110

2111
            IF ( @masa_perolehan_1 <> '1' ) 
2112

2113
                SELECT  @a8 = @a8 - ISNULL(SUM(amount), 0)
2114

2115
                FROM    @tbl_tr_prev_cum
2116

2117
                WHERE   spt_no = 8
2118

2119
                GROUP BY spt_no
2120

2121

2122

2123
            SET @a8 = ISNULL(@a8, 0)
2124

2125

2126

2127
            SET @a9 = ISNULL(@a7 + @a8, 0)
2128

2129

2130

2131
            SELECT  @a10 = ISNULL(SUM(amount), 0)
2132

2133
            FROM    @tbl_tr_dec
2134

2135
            WHERE   spt_no = 10
2136

2137
            GROUP BY spt_no
2138

2139

2140

2141
            IF ( @masa_perolehan_1 <> '1' ) 
2142

2143
                SELECT  @a10 = @a10 - ISNULL(SUM(amount), 0)
2144

2145
                FROM    @tbl_tr_prev_cum
2146

2147
                WHERE   spt_no = 10
2148

2149
                GROUP BY spt_no
2150

2151

2152

2153
            SET @a10 = ISNULL(@a10, 0)
2154

2155

2156

2157
            SET @tot_biaya_jabatan = 500000 * @jml_bulan_perolehan
2158

2159
            IF ( @a10 > @tot_biaya_jabatan ) 
2160

2161
                BEGIN
2162

2163
                    SET @a10 = @tot_biaya_jabatan
2164

2165
                    SET @a11 = 0
2166

2167
                END
2168

2169

2170

2171
            SELECT  @a11 = ISNULL(SUM(amount), 0)
2172

2173
            FROM    @tbl_tr_dec
2174

2175
            WHERE   spt_no = 11
2176

2177
            GROUP BY spt_no
2178

2179
					  
2180

2181
            IF ( @masa_perolehan_1 <> '1' ) 
2182

2183
                SELECT  @a11 = @a11 - ISNULL(SUM(amount), 0)
2184

2185
                FROM    @tbl_tr_prev_cum
2186

2187
                WHERE   spt_no = 11
2188

2189
                GROUP BY spt_no
2190

2191
					  
2192

2193
            SET @a11 = ISNULL(@a11, 0)
2194

2195

2196

2197
            IF ( ( @a10 + @a11 ) > @tot_biaya_jabatan ) 
2198

2199
                BEGIN 
2200

2201
                    SET @a11 = @tot_biaya_jabatan - @a10
2202

2203
                END
2204

2205

2206

2207
            SELECT  @a12 = ISNULL(SUM(amount), 0)
2208

2209
            FROM    @tbl_tr_dec
2210

2211
            WHERE   spt_no = 12
2212

2213
            GROUP BY spt_no
2214

2215

2216

2217
            IF ( @masa_perolehan_1 <> '1' ) 
2218

2219
                SELECT  @a12 = @a12 - ISNULL(SUM(amount), 0)
2220

2221
                FROM    @tbl_tr_prev_cum
2222

2223
                WHERE   spt_no = 12
2224

2225
                GROUP BY spt_no
2226

2227

2228

2229
            SET @a12 = ISNULL(@a12, 0)
2230

2231
	
2232

2233
            SET @a13 = ISNULL(@a10 + @a11 + @a12, 0)
2234

2235
	
2236

2237
            SET @a14 = ISNULL(@a9 - @a13, 0)
2238

2239

2240

2241
            SELECT  @a15 = ISNULL(SUM(amount), 0)
2242

2243
            FROM    @tbl_tr_dec_ori
2244

2245
            WHERE   spt_no = 15
2246

2247
            GROUP BY spt_no
2248

2249
            SET @a15 = ISNULL(@a15, 0)
2250

2251

2252

2253
            SET @a16 = ISNULL(@a14 + @a15, 0)
2254

2255

2256

2257
	--//spt_no 16 case khusus flag_status khusus
2258

2259
            SELECT  @a16_flag = ISNULL(SUM(amount), 0)
2260

2261
            FROM    @tbl_tr_dec_ori
2262

2263
            WHERE   spt_no = 16
2264

2265
            GROUP BY spt_no
2266

2267
            SET @a16_flag = ISNULL(@a16_flag, 0)
2268

2269

2270

2271
            IF @movement_reason = @death_mov_reason
2272

2273
                OR ISNULL(@emp_type_payroll, '') = 'EXP' 
2274

2275
                BEGIN
2276

2277
                    SELECT  @a16 = CASE WHEN ISNULL(@emp_type_payroll, '') = 'EXP'
2278

2279
                                        THEN @a16
2280

2281
                                        ELSE @a16 * 12
2282

2283
                                             / ( CONVERT(INT, @masa_perolehan_2)
2284

2285
                                                 - CONVERT(INT, @masa_perolehan_1)
2286

2287
                                                 + 1 )
2288

2289
                                   END
2290

2291
                END
2292

2293
	
2294

2295
            IF @flag_status = '1'
2296

2297
                OR @flag_status = '3' 
2298

2299
                BEGIN
2300

2301
                    SET @a16 = @a16_flag
2302

2303
                END
2304

2305

2306

2307
            SET @a18 = @a16 - @a17
2308

2309
	
2310

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

2313
                BEGIN
2314

2315
                    DECLARE @Pembulatan1000 AS VARCHAR(19)
2316

2317
                    SET @Pembulatan1000 = CAST(@a18 AS VARCHAR(19)); 		
2318

2319
                    SET @a18 = SUBSTRING(@Pembulatan1000, 1,
2320

2321
                                         LEN(RTRIM(@Pembulatan1000)) - 3)
2322

2323
                        + REPLICATE('0', 3); 		
2324

2325
                END
2326

2327

2328

2329
	--//ambil mt murni tanpa kondisi
2330

2331
            SELECT  @a19murni = ISNULL(SUM(amount), 0)
2332

2333
            FROM    @tbl_tr_dec
2334

2335
            WHERE   spt_no = 19
2336

2337
            GROUP BY spt_no
2338

2339

2340

2341
	--//ambil att murni tanpa kondisi
2342

2343
            SELECT  @a17_att = ISNULL(SUM(amount), 0)
2344

2345
            FROM    @tbl_tr_dec_ori
2346

2347
            WHERE   spt_no = 99
2348

2349
            GROUP BY spt_no
2350

2351

2352

2353
	--//cek status employee apakah sudah resign
2354

2355
            SELECT  @a19 = ISNULL(SUM(amount), 0)
2356

2357
            FROM    @tbl_tr_dec
2358

2359
            WHERE   spt_no = 19
2360

2361
            GROUP BY spt_no
2362

2363

2364

2365
            IF ISNULL(@emp_type_payroll, '') = 'EXP' 
2366

2367
                BEGIN
2368

2369
                    SELECT  @a19b = ISNULL(SUM(amount), 0)
2370

2371
                    FROM    @tbl_tr_prev_cum
2372

2373
                    WHERE   spt_no = 19
2374

2375
                    GROUP BY spt_no
2376

2377
                    SET @a19 = @a19 * 12 / ( CONVERT(INT, @masa_perolehan_2)
2378

2379
                                             - CONVERT(INT, @masa_perolehan_1)
2380

2381
                                             + 1 )			
2382

2383
                END
2384

2385
            ELSE 
2386

2387
                IF ( @movement_type = 17
2388

2389
                     OR @emp_status <> '01'
2390

2391
                   ) 
2392

2393
                    BEGIN
2394

2395
                        IF @movement_reason = '12' 
2396

2397
                            BEGIN
2398

2399
                                SET @a19 = ISNULL(@a19murni, 0)
2400

2401
                            END
2402

2403
                    END
2404

2405
                ELSE 
2406

2407
                    BEGIN
2408

2409
                        IF ( ( @masa_perolehan_1 = '1' )
2410

2411
                             AND ( @masa_perolehan_2 <> '12' )
2412

2413
                           ) 
2414

2415
                            BEGIN
2416

2417
                                SET @a19 = @a19 * 12
2418

2419
                                    / CONVERT(INT, @masa_perolehan_2)
2420

2421
                            END
2422

2423
                    END
2424

2425

2426

2427
            SET @a19 = ISNULL(@a19, 0)
2428

2429

2430

2431
			DECLARE @MTPrevious Decimal(22,0)
2432

2433
			SELECT @MTPrevious = (amount * 12) / @previous_period_payroll
2434

2435
			FROM @tbl_tr_prev_cum WHERE wage_type = 'MT'
2436

2437

2438

2439
            /* SELECT  @a20 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2440

2441
                                THEN ISNULL(SUM(amount), 0)
2442

2443
                                ELSE 0
2444

2445
                           END
2446

2447
            FROM    @tbl_tr_dec_ori
2448

2449
            WHERE   spt_no = 20
2450

2451
            GROUP BY spt_no 
2452

2453

2454

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

2457

2458

2459
			SET @a20 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2460

2461
                                THEN ISNULL(SUM(@MTPrevious), 0)
2462

2463
                                ELSE 0
2464

2465
                           END
2466

2467

2468

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

2471
            DECLARE @flag_running VARCHAR(200)
2472

2473
            SELECT  @flag_running = CASE WHEN COUNT(tr.EmployeeID) <= 0
2474

2475
                                         THEN 'no_data'
2476

2477
                                         ELSE 'yes_data'
2478

2479
                                    END
2480

2481
            FROM    dbo.PHRPYTR0300 AS tr
2482

2483
            WHERE   tr.RunPeriodMonth = tr.PayPeriodMonth
2484

2485
                    AND tr.RunPeriodYear = tr.PayPeriodYear
2486

2487
                    AND tr.RunPeriodMonth = @previous_period_payroll_varchar
2488

2489
                    AND tr.EmployeeID = @c_emp_id
2490

2491
	------------------ // pengecekan running selesai // ----------------- 
2492

2493
			
2494

2495
            IF ( @emp_status = '01'
2496

2497
                 AND NOT ( @masa_perolehan_1 = '1'
2498

2499
                           AND @masa_perolehan_2 = '12'
2500

2501
                         )
2502

2503
               )
2504

2505
                OR ISNULL(@emp_type_payroll, '') = 'EXP'
2506

2507
                OR @movement_reason = @death_mov_reason 
2508

2509
                BEGIN	
2510

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

2513
                        BEGIN
2514

2515
                            SET @a21 = ISNULL(@a19murni - 0, 0)
2516

2517
                        END 
2518

2519
                    ELSE 
2520

2521
                        BEGIN
2522

2523
                            SET @a21 = ISNULL(@a19murni - @a20, 0)
2524

2525
                        END --@a2--@a19 								
2526

2527
                END
2528

2529
				
2530

2531
--		ELSE
2532

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

2535
			SET @a21 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2536

2537
                                THEN ISNULL(@a19,0)
2538

2539
                                ELSE ISNULL(@a19 - @a20, 0)
2540

2541
                           END
2542

2543

2544

2545
		----// sementara di hard-code dulu, untuk handling case mks.
2546

2547

2548

2549
            /*SET @a22 = @a21 + @a20*/
2550

2551
			SET @a22 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
2552

2553
                                THEN ISNULL(@a21,0)
2554

2555
                                ELSE ISNULL(@a21 + @a20, 0)
2556

2557
                           END
2558

2559
            SET @a22a = 0
2560

2561
            SET @a22b = @a21
2562

2563
            SET @a23 = ISNULL(@a21 - @a22, 0)
2564

2565
            SET @a24 = ISNULL(@a23, 0)
2566

2567

2568

2569
            SET @bln_a24 = SUBSTRING(CONVERT(VARCHAR, @c_end_date), 5, 2)
2570

2571
                + LEFT(CONVERT(VARCHAR, @c_end_date), 4) 
2572

2573

2574

2575
            IF ( @a21 - @a22 = 0 ) 
2576

2577
                BEGIN
2578

2579
                    SET @flg_a24 = '0'
2580

2581
                    SET @bln_a24 = ''
2582

2583
                END
2584

2585

2586

2587
            IF ( @a21 - @a22 > 0 ) 
2588

2589
                BEGIN
2590

2591
                    SET @flg_a24 = '1'
2592

2593
                END
2594

2595

2596

2597
            IF ( @a21 - @a22 < 0 ) 
2598

2599
                BEGIN
2600

2601
                    SET @flg_a24 = '2'
2602

2603
                END
2604

2605

2606

2607
            SET @flg_a2 = '0'
2608

2609
		
2610

2611
		--//Pegawai yang dipindahkan
2612

2613
            SET @flg_a2 = '1'
2614

2615

2616

2617
            DELETE  @tbl_tr_dec
2618

2619
		
2620

2621
            INSERT  INTO @result
2622

2623
                    SELECT  @kode_form ,
2624

2625
                            @tahun_pajak ,
2626

2627
                            @pembetulan ,
2628

2629
                            CONVERT(CHAR, RIGHT('0000000'
2630

2631
                                                + CONVERT(VARCHAR(7), @nomor_urut),
2632

2633
                                                7)) AS nomor_urut ,
2634

2635
                            @npwp_pegawai ,
2636

2637
                            @nama_pegawai ,
2638

2639
                            @alamat_pegawai ,
2640

2641
                            @jabatan_pegawai ,
2642

2643
                            @jenis_kelamin ,
2644

2645
                            @status_pegawai ,
2646

2647
                            @status_kawin ,
2648

2649
                            @flag_asing ,
2650

2651
                            @status_ptkp ,
2652

2653
                            @jumlah_tanggungan ,
2654

2655
                            @masa_perolehan_1 ,
2656

2657
                            @masa_perolehan_2 ,
2658

2659
                            @flag_status ,
2660

2661
                            @a1 ,
2662

2663
                            @flg_a2 ,
2664

2665
                            @a2 ,
2666

2667
                            @a3 ,
2668

2669
                            @a4 ,
2670

2671
                            @a5 ,
2672

2673
                            @a6 ,
2674

2675
                            @a7 ,
2676

2677
                            @a8 ,
2678

2679
                            @a9 ,
2680

2681
                            @a10 ,
2682

2683
                            @a11 ,
2684

2685
                            @a12 ,
2686

2687
                            @a13 ,
2688

2689
                            @a14 ,
2690

2691
                            @a15 ,
2692

2693
                            @a16 ,
2694

2695
                            @a17 ,
2696

2697
                            @a18 ,
2698

2699
                            @a17_att ,
2700

2701
                            @a20 ,
2702

2703
                            @a21 ,
2704

2705
                            @a22 ,
2706

2707
                            @a22a ,
2708

2709
                            @a22b ,
2710

2711
                            @a23 ,
2712

2713
                            @a24 ,
2714

2715
                            @flg_a24 ,
2716

2717
                            @bln_a24 ,
2718

2719
                            @ktp
2720

2721
		--, @c_emp_id
2722

2723
                            ,
2724

2725
                            @nama_pemotong ,
2726

2727
                            @npwp_pemotong ,
2728

2729
                            @kode_pajak
2730

2731
		--, @company_name
2732

2733
					
2734

2735

2736

2737
            SET @nomor_urut = NULL
2738

2739
            SET @npwp_pegawai = NULL
2740

2741
            SET @nama_pegawai = NULL
2742

2743
            SET @alamat_pegawai = NULL
2744

2745
            SET @jabatan_pegawai = NULL
2746

2747
            SET @jenis_kelamin = NULL
2748

2749
            SET @status_pegawai = NULL
2750

2751
            SET @status_kawin = NULL
2752

2753
            SET @flag_asing = NULL
2754

2755
            SET @status_ptkp = NULL
2756

2757
            SET @jumlah_tanggungan = NULL
2758

2759
            SET @masa_perolehan_1 = NULL
2760

2761
            SET @masa_perolehan_2 = NULL
2762

2763
            SET @flag_status = NULL
2764

2765
            SET @a1 = NULL
2766

2767
            SET @flg_a2 = NULL
2768

2769
            SET @a2 = NULL
2770

2771
            SET @a3 = NULL
2772

2773
            SET @a4 = NULL
2774

2775
            SET @a5 = NULL
2776

2777
            SET @a6 = NULL
2778

2779
            SET @a7 = NULL
2780

2781
            SET @a8 = NULL
2782

2783
            SET @a9 = NULL
2784

2785
            SET @a10 = NULL
2786

2787
            SET @a11 = NULL
2788

2789
            SET @a12 = NULL
2790

2791
            SET @a13 = NULL
2792

2793
            SET @a14 = NULL
2794

2795
            SET @a15 = NULL
2796

2797
            SET @a16 = NULL
2798

2799
            SET @a16_flag = NULL
2800

2801
            SET @a17 = NULL
2802

2803
            SET @a18 = NULL
2804

2805
            SET @a19 = NULL
2806

2807
            SET @a20 = NULL
2808

2809
            SET @a21 = NULL
2810

2811
            SET @a22 = NULL
2812

2813
            SET @a22a = NULL
2814

2815
            SET @a22b = NULL
2816

2817
            SET @a23 = NULL
2818

2819
            SET @a24 = NULL
2820

2821
            SET @flg_a24 = NULL
2822

2823
            SET @bln_a24 = NULL
2824

2825

2826

2827
            FETCH cur_employee INTO @c_emp_id, @c_start_date, @c_end_date   
2828

2829
        END
2830

2831
    CLOSE cur_employee
2832

2833
    DEALLOCATE cur_employee
2834

2835

2836

2837
    UPDATE  @result
2838

2839
    SET     npwp_pegawai = '000000000000000'
2840

2841
    WHERE   LEN(npwp_pegawai) < 15
2842

2843

2844

2845
    UPDATE  @result
2846

2847
    SET     alamat_pegawai = '-'
2848

2849
    WHERE   alamat_pegawai = ''
2850

2851
            OR alamat_pegawai IS NULL
2852

2853
		
2854

2855
    SELECT  masa_perolehan_2 AS TaxPeriod ,
2856

2857
            tahun_pajak AS TaxableYear,
2858

2859
            pembetulan AS TaxCorrection ,
2860

2861
            '1.1-' + CONVERT(CHAR(2), RIGHT('00'
2862

2863
                                            + CONVERT(VARCHAR(2), masa_perolehan_2),
2864

2865
                                            2)) + '.' + RIGHT(@Year, 2) + '-'
2866

2867
            + nomor_urut AS WithholdingTax ,
2868

2869
            --CONVERT(CHAR(2), RIGHT('00' + CONVERT(VARCHAR(2), masa_perolehan_2),
2870

2871
            --                       2)) AS bulan_bukti_potong ,
2872

2873
            --RIGHT(@Year, 2) AS tahun_bukti_potong ,
2874

2875
            --nomor_urut ,
2876

2877
            masa_perolehan_1 AS AcquisitionPeriod ,
2878

2879
            masa_perolehan_2 AS AcquisitionPeriodFinal ,
2880

2881
            npwp_pegawai AS NPWP ,
2882

2883
            --RIGHT(npwp_pegawai, 3) AS tiga_akhir_npwp_pegawai ,
2884

2885
            --SUBSTRING(npwp_pegawai, 10, 3) AS enam_akhir_npwp_pegawai ,
2886

2887
            --LEFT(npwp_pegawai, 9) AS awal_npwp_pegawai ,
2888

2889
            --SUBSTRING(npwp_pegawai, 1, 2) + '.' + SUBSTRING(npwp_pegawai, 3, 3)
2890

2891
            --+ '.' + SUBSTRING(npwp_pegawai, 6, 3) + '.'
2892

2893
            --+ SUBSTRING(npwp_pegawai, 9, 1) AS awal_titik_npwp_pegawai ,
2894

2895
            ktp AS EmployeeID ,
2896

2897
            nama_pegawai AS Name,
2898

2899
            alamat_pegawai AS Address,
2900

2901
            CASE WHEN jenis_kelamin = 'W'
2902

2903
                      OR jenis_kelamin = 'Wanita'
2904

2905
                      OR jenis_kelamin = 'Female'
2906

2907
                      OR jenis_kelamin = 'F' THEN 'F'
2908

2909
                 WHEN jenis_kelamin = 'P'
2910

2911
                      OR jenis_kelamin = 'Pria'
2912

2913
                      OR jenis_kelamin = 'Male'
2914

2915
                      OR jenis_kelamin = 'M' THEN 'M'
2916

2917
            END AS Gender ,
2918

2919
            CASE WHEN status_ptkp = 'TK' THEN 'TK'
2920

2921
                 WHEN status_ptkp = 'K1'
2922

2923
                      OR status_ptkp = 'K2'
2924

2925
                      OR status_ptkp = 'K3' THEN 'K'
2926

2927
                 WHEN status_ptkp = 'T1'
2928

2929
                      OR status_ptkp = 'T2'
2930

2931
                      OR status_ptkp = 'T3' THEN 'HB'
2932

2933
            END AS PTKPStatus ,
2934

2935
            CASE WHEN status_ptkp = 'TK' THEN '0'
2936

2937
                 ELSE RIGHT(status_ptkp, 1)
2938

2939
            END AS NumberOfChildren --,jumlah_tanggungan
2940

2941
            ,
2942

2943
            jabatan_pegawai AS JobDescription ,
2944

2945
            CASE WHEN flag_asing = 0 THEN 'N'
2946

2947
                 ELSE 'Y'
2948

2949
            END AS NonResidentTaxpayer ,
2950

2951
            '' AS CountryCode ,
2952

2953
            kode_pajak AS TaxCode ,
2954

2955
            a1 AS Amount1 ,
2956

2957
            a2 AS Amount2 ,
2958

2959
            a3 AS Amount3 ,
2960

2961
            a4 AS Amount4 ,
2962

2963
            a5 AS Amount5 ,
2964

2965
            a6 AS Amount6 ,
2966

2967
            a8 AS Amount7 ,
2968

2969
            a9 AS Amount8 ,
2970

2971
            a10 + a11 AS Amount9 ,
2972

2973
            a12 AS Amount10 ,
2974

2975
            a13 AS Amount11 ,
2976

2977
            a14 AS Amount12 ,
2978

2979
            a15 AS Amount13 ,
2980

2981
            CASE WHEN a16 < 0 THEN 0
2982

2983
                 ELSE a16
2984

2985
            END AS Amount14 ,
2986

2987
            a17 AS Amount15 ,
2988

2989
            CASE WHEN a18 < 0 THEN 0
2990

2991
                 ELSE a18
2992

2993
            END AS Amount16 ,
2994

2995
            a19 AS Amount17 --@a17_att AS a17 
2996

2997
            ,
2998

2999
            CASE WHEN a20 < 0 THEN 0
3000

3001
                 ELSE a20
3002

3003
            END AS Amount18 ,
3004

3005
            a21 AS Amount19 ,
3006

3007
            a22 AS Amount20 ,
3008

3009
            '' AS MovementStatus ,
3010

3011
            npwp_pemotong AS NPWPPemotong ,
3012

3013
            --RIGHT(npwp_pemotong, 3) AS tiga_akhir_npwp_pemotong ,
3014

3015
            --SUBSTRING(npwp_pemotong, 10, 3) AS enam_akhir_npwp_pemotong ,
3016

3017
            --LEFT(npwp_pemotong, 9) AS awal_npwp_pemotong ,
3018

3019
            --SUBSTRING(npwp_pemotong, 1, 2) + '.' + SUBSTRING(npwp_pemotong, 3,
3020

3021
            --                                                 3) + '.'
3022

3023
            --+ SUBSTRING(npwp_pemotong, 6, 3) + '.' + SUBSTRING(npwp_pemotong,
3024

3025
            --                                                  9, 1) AS awal_titik_npwp_pemotong ,
3026

3027
            nama_pemotong AS NamaPemotong ,
3028

3029
            dbo.fn_formatdatetime(GETDATE(), 'dd/mm/yyyy') AS TanggalBuktiPotong 
3030

3031
            --dbo.fn_formatdatetime(GETDATE(), 'dd') AS day_tgl_bukti_potong ,
3032

3033
            --dbo.fn_formatdatetime(GETDATE(), 'mm') AS month_tgl_bukti_potong ,
3034

3035
            --dbo.fn_formatdatetime(GETDATE(), 'yyyy') AS year_tgl_bukti_potong
3036

3037
    FROM    @result
3038

3039

    (1-1/1)