Project

General

Profile

Bug #1130 » RptSPTahun_20220606_New_FAMA.sql

Tri Rizqiaty, 06/06/2022 10:57 AM

 
1
USE [MinovaES_BankFama_Prod]
2
GO
3
/****** Object:  StoredProcedure [dbo].[RptSPTahun]    Script Date: 6/6/2022 10:17:12 AM ******/
4
SET ANSI_NULLS ON
5
GO
6
SET QUOTED_IDENTIFIER ON
7
GO
8
ALTER PROCEDURE [dbo].[RptSPTahun]
9
    @CompanyID VARCHAR(4) = NULL ,
10
    @Year VARCHAR(4) ,
11
    @TaxOffice VARCHAR(10) ,
12
    @EmployeeStatus VARCHAR(2) = NULL ,
13
    @PayrollGroup VARCHAR(2) = NULL ,
14
    @EmployeeIDFrom VARCHAR(8) = NULL ,
15
    @EmployeeIDTo VARCHAR(8) = NULL
16
AS 
17
    SET NOCOUNT ON;
18

    
19
--DECLARE @CompanyID varchar(4) = ''
20
--DECLARE @Year varchar(4) = '2022'
21
--DECLARE @TaxOffice varchar(8) = 'BDG1'
22
--DECLARE @EmployeeStatus varchar(2) = null
23
--DECLARE @PayrollGroup varchar(2) = ''
24
--DECLARE @EmployeeIDFrom varchar(8) = '19930301'--'00000300'
25
--DECLARE @EmployeeIDTo varchar(8) = NULL
26

    
27

    
28
    DECLARE @begda INT
29
    DECLARE @endda INT
30
    DECLARE @employee_status_beg VARCHAR(5)
31
    DECLARE @employee_status_end VARCHAR(5)
32
    DECLARE @payroll_group_beg VARCHAR(5)
33
    DECLARE @payroll_group_END VARCHAR(5)
34

    
35
--//Set begin & end of period
36
    SET @begda = @Year + '0101'
37
    SET @endda = @Year + '1231'
38

    
39
--//Set employee status
40
    IF @EmployeeStatus = ''
41
        OR @EmployeeStatus IS NULL 
42
        BEGIN
43
            SET @employee_status_beg = '00000'
44
            SET @employee_status_end = '99999'
45
        END
46
    ELSE 
47
        BEGIN
48
            SET @employee_status_beg = @EmployeeStatus
49
            SET @employee_status_end = @EmployeeStatus
50
        END
51

    
52
--//Set employee group
53
    IF @PayrollGroup = ''
54
        OR @PayrollGroup IS NULL 
55
        BEGIN
56
            SET @payroll_group_beg = '00000'
57
            SET @payroll_group_END = '99999'
58
        END
59
    ELSE 
60
        BEGIN
61
            SET @payroll_group_beg = @PayrollGroup
62
            SET @payroll_group_END = @PayrollGroup
63
        END
64

    
65

    
66
--//Set employee id start
67
    IF @EmployeeIDFrom = ''
68
        OR @EmployeeIDFrom IS NULL 
69
        BEGIN
70
            SET @EmployeeIDFrom = '00000000'
71
            SET @EmployeeIDTo = '99999999'
72
        END
73

    
74
    IF @EmployeeIDTo = ''
75
        OR @EmployeeIDTo IS NULL 
76
        SET @EmployeeIDTo = @EmployeeIDFrom 
77

    
78
--//Get system parameter
79
    DECLARE @is_encrypted VARCHAR
80
    DECLARE @npwp_address_subtype VARCHAR(4)
81

    
82
    SELECT  @is_encrypted = Value1
83
    FROM    dbo.PCMEPGENPARAM
84
    WHERE   Parameter = 'HR_PY_ENCRYPTED'
85
            AND StartDate <= @endda
86
            AND EndDate >= @endda
87

    
88
    SELECT  @npwp_address_subtype = Value1
89
    FROM    dbo.PCMEPGENPARAM
90
    WHERE   Parameter = 'HR_PY_NPWP_ADDRESS_SUBTYPE'
91
            AND StartDate <= @endda
92
            AND EndDate >= @endda
93

    
94
-----// Get employee tax office list
95
    DECLARE @emp_office TABLE
96
        (
97
          [StartDate] [varchar](8) NOT NULL ,
98
          [EndDate] [varchar](8) NOT NULL ,
99
          [EmployeeOffice] [varchar](4) NOT NULL ,
100
          [EmployeeOfficeDesc] [varchar](50) NULL ,
101
          [EmployeeArea] [varchar](4) NOT NULL ,
102
          [TaxOffice] [varchar](4) NULL ,
103
          [NPWP] [varchar](50) NULL ,
104
          [Address] [varchar](500) NULL ,
105
          [EmployeeOfficeGroup] [varchar](5) NULL ,
106
          [MinimumWageRegional] [varchar](4) NULL ,
107
          [Notes] [varchar](500) NULL ,
108
          [CreateBy] [varchar](18) NULL ,
109
          [CreateDate] [varchar](14) NULL ,
110
          [ChangeBy] [varchar](18) NULL ,
111
          [ChangeDate] [varchar](14) NULL
112
        )
113

    
114
    INSERT  INTO @emp_office
115
            SELECT  [StartDate] ,
116
                    [EndDate] ,
117
                    [EmployeeOffice] ,
118
                    [EmployeeOfficeDesc] ,
119
                    [EmployeeArea] ,
120
                    [TaxOffice] ,
121
                    [NPWP] ,
122
                    [Address] ,
123
                    [EmployeeOfficeGroup] ,
124
                    [MinimumWageRegional] ,
125
                    [Notes] ,
126
                    [CreateBy] ,
127
                    [CreateDate] ,
128
                    [ChangeBy] ,
129
                    [ChangeDate]
130
            FROM    dbo.PCMEPEMPOFF
131
            WHERE   TaxOffice = @TaxOffice
132
	
133
--Get Employee Tobe Processed
134
    DECLARE @employee_tobe_process2 TABLE
135
        (
136
          emp_id VARCHAR(8) ,
137
          start_date INT ,
138
          end_date INT ,
139
          employee_status VARCHAR(2)
140
        )
141

    
142
    DECLARE @employee_tobe_process TABLE
143
        (
144
          emp_id VARCHAR(8) ,
145
          start_date INT ,
146
          end_date INT
147
        )
148

    
149
    DECLARE @payroll_header TABLE
150
        (
151
          employee_id VARCHAR(8) ,
152
          start_date VARCHAR(8) ,
153
          end_date VARCHAR(8) ,
154
          employee_office VARCHAR(4) ,
155
          run_period_month VARCHAR(2)
156
        )
157
    DECLARE @payroll_header_tmp TABLE
158
        (
159
          employee_id VARCHAR(8) ,
160
          start_date VARCHAR(8) ,
161
          end_date VARCHAR(8) ,
162
          employee_office VARCHAR(4) ,
163
          run_period_month VARCHAR(2)
164
        )
165
    DECLARE @payroll_header0 TABLE
166
        (
167
          employee_id VARCHAR(8) ,
168
          start_date VARCHAR(8) ,
169
          end_date VARCHAR(8) ,
170
          employee_office VARCHAR(4) ,
171
          run_period_month VARCHAR(2)
172
        )
173

    
174
    INSERT  INTO @payroll_header_tmp
175
            SELECT DISTINCT
176
                    md2.EmployeeID ,
177
                    ph.StartDate ,
178
                    ph.EndDate ,
179
                    md2.EmployeeOffice ,
180
                    RunPeriodMonth
181
            FROM    dbo.PHRPYTR0300 ph
182
                    INNER JOIN dbo.PHRPA0002 md2 ON md2.EmployeeID = ph.EmployeeID
183
            WHERE   ph.StartDate >= @begda
184
                    AND ph.EndDate <= @endda
185
                    AND md2.StartDate <= ph.EndDate
186
                    AND md2.EndDate >= ph.EndDate
187
                    AND md2.EmployeeID BETWEEN @EmployeeIDFrom
188
                                       AND     @EmployeeIDTo
189
				
190
--//get the latest run data
191
    INSERT  INTO @payroll_header0
192
            SELECT  py_head.*
193
            FROM    @payroll_header_tmp AS py_head
194
                    INNER JOIN ( SELECT employee_id ,
195
                                        start_date ,
196
                                        end_date ,
197
                                        MAX(run_period_month) AS run_period_month
198
                                 FROM   @payroll_header_tmp
199
                                 GROUP BY employee_id ,
200
                                        start_date ,
201
                                        end_date
202
                               ) a ON py_head.employee_id = a.employee_id
203
                                      AND py_head.start_date = a.start_date
204
                                      AND py_head.end_date = a.end_date
205
                                      AND py_head.run_period_month = a.run_period_month
206
			
207
    INSERT  INTO @payroll_header
208
            SELECT DISTINCT
209
                    employee_id ,
210
                    start_date ,
211
                    end_date ,
212
                    employee_office ,
213
                    run_period_month
214
            FROM    @payroll_header0 ph0
215
                    INNER JOIN @emp_office eo ON ph0.employee_office = eo.EmployeeOffice
216
					
217
----//get list spliiter running payroll for others TAX Office.
218
    SELECT DISTINCT
219
            employee_id ,
220
            start_date ,
221
            end_date ,
222
            employee_office ,
223
            run_period_month
224
    INTO    #tmp0
225
    FROM    @payroll_header0 ph0
226
    WHERE   NOT EXISTS ( SELECT *
227
                         FROM   @payroll_header ph
228
                         WHERE  ph0.employee_id = ph.employee_id
229
                                AND ph0.start_date = ph.start_date
230
                                AND ph0.end_date = ph.end_date
231
                                AND ph0.employee_office = ph.employee_office
232
                                AND ph0.run_period_month = ph.run_period_month )
233
		
234
-- Get list date range splitter.
235
    SELECT  a.employee_id ,
236
            a.start_date mindt0 ,
237
            b.start_date maxdt0
238
    INTO    #tmp
239
    FROM    @payroll_header a ,
240
            @payroll_header b
241
    WHERE   a.employee_id = b.employee_id
242
            AND b.start_date > a.start_date
243
            AND ( CONVERT(INT, SUBSTRING(b.start_date, 5, 2))
244
                  - CONVERT(INT, SUBSTRING(a.start_date, 5, 2)) ) > 1
245
            AND ( SELECT    COUNT(*)
246
                  FROM      @payroll_header c
247
                  WHERE     ( CONVERT(INT, SUBSTRING(a.start_date, 5, 2)) < CONVERT(INT, SUBSTRING(c.start_date,
248
                                                              5, 2)) )
249
                            AND ( CONVERT(INT, SUBSTRING(b.start_date, 5, 2)) > CONVERT(INT, SUBSTRING(c.end_date,
250
                                                              5, 2)) )
251
                            AND c.employee_id = a.employee_id
252
                ) = 0
253
            AND ( SELECT    COUNT(*)
254
                  FROM      #tmp0 d
255
                  WHERE     d.employee_id = a.employee_id
256
                            AND a.start_date < d.start_date
257
                            AND b.start_date > d.end_date
258
                ) <> 0
259
	
260
    DROP TABLE #tmp0
261

    
262

    
263
    DECLARE @n_empty_range INT
264
	  
265
    SELECT  @n_empty_range = COUNT(*)
266
    FROM    #tmp	  
267

    
268
    IF ( @n_empty_range <> 0 ) 
269
        BEGIN
270
            INSERT  INTO @employee_tobe_process
271
                    SELECT  ddd.employee_id ,
272
                            ddd.mindt ,
273
                            ddd.maxdt
274
                    FROM    ( SELECT    dd.employee_id ,
275
                                        MIN(dd.start_date) mindt ,
276
                                        MAX(dd.end_date) maxdt
277
                              FROM      ( SELECT    x.employee_id ,
278
                                                    x.start_date ,
279
                                                    x.end_date ,
280
                                                    SUM(ISNULL(d.flag, 0)) flag
281
                                          FROM      @payroll_header x
282
                                                    LEFT OUTER JOIN ( SELECT
283
                                                              a.* ,
284
                                                              1 'flag'
285
                                                              FROM
286
                                                              @payroll_header a
287
                                                              LEFT OUTER JOIN #tmp b ON a.employee_id = b.employee_id
288
                                                              WHERE
289
                                                              a.start_date <= b.mindt0
290
                                                              AND a.end_date < b.maxdt0
291
                                                              ) d ON x.employee_id = d.employee_id
292
                                                              AND x.start_date = d.start_date
293
                                                              AND x.end_date = d.end_date
294
                                          GROUP BY  x.employee_id ,
295
                                                    x.start_date ,
296
                                                    x.end_date
297
                                        ) dd
298
                              GROUP BY  dd.employee_id ,
299
                                        dd.flag
300
                            ) ddd
301
                    ORDER BY ddd.employee_id ,
302
                            ddd.mindt	
303
        END
304
    ELSE 
305
        BEGIN
306
            INSERT  INTO @employee_tobe_process
307
                    SELECT  main.employee_id ,
308
                            mindate.mindt ,
309
                            maxdate.maxdt
310
                    FROM    ( SELECT DISTINCT
311
                                        employee_id
312
                              FROM      @payroll_header
313
                            ) main
314
                            INNER JOIN ( SELECT ph.employee_id ,
315
                                                MIN(ph.start_date) mindt
316
                                         FROM   @payroll_header ph
317
                                         GROUP BY employee_id
318
                                       ) mindate ON main.employee_id = mindate.employee_id
319
                            INNER JOIN ( SELECT employee_id ,
320
                                                MAX(end_date) maxdt
321
                                         FROM   @payroll_header ph
322
                                         GROUP BY employee_id
323
                                       ) maxdate ON main.employee_id = maxdate.employee_id
324
        END
325
				
326
    DROP TABLE #tmp           
327
                
328
    DECLARE @kode_form VARCHAR(7)
329
    DECLARE @tahun_pajak VARCHAR(4)
330
    DECLARE @pembetulan INT
331
    DECLARE @nomor_urut VARCHAR(7)
332
    DECLARE @npwp_pegawai VARCHAR(15)
333
    DECLARE @nama_pegawai VARCHAR(50)
334
    DECLARE @alamat_pegawai VARCHAR(255)
335
    DECLARE @jabatan_pegawai VARCHAR(30)
336
    DECLARE @jenis_kelamin VARCHAR(1)
337
    DECLARE @status_pegawai VARCHAR(1)
338
    DECLARE @status_kawin VARCHAR(1)
339
    DECLARE @flag_asing VARCHAR(1)
340
    DECLARE @status_ptkp VARCHAR(2)
341
    DECLARE @jumlah_tanggungan VARCHAR(1)
342
    DECLARE @masa_perolehan_1 VARCHAR(2)
343
    DECLARE @masa_perolehan_2 VARCHAR(2)
344
    DECLARE @flag_status VARCHAR(1)
345
    DECLARE @a1 DECIMAL
346
    DECLARE @flg_a2 VARCHAR(1)
347
    DECLARE @a2 DECIMAL
348
    DECLARE @a3 DECIMAL
349
    DECLARE @a4 DECIMAL
350
    DECLARE @a5 DECIMAL
351
    DECLARE @a6 DECIMAL
352
    DECLARE @a7 DECIMAL
353
    DECLARE @a8 DECIMAL
354
    DECLARE @a9 DECIMAL
355
    DECLARE @a10 DECIMAL
356
    DECLARE @a11 DECIMAL
357
    DECLARE @a12 DECIMAL
358
    DECLARE @a13 DECIMAL
359
    DECLARE @a14 DECIMAL
360
    DECLARE @a15 DECIMAL
361
    DECLARE @a16 DECIMAL
362
    DECLARE @a16_flag DECIMAL
363
    DECLARE @a17 DECIMAL
364
    DECLARE @a18 DECIMAL
365
    DECLARE @a19 DECIMAL
366
    DECLARE @a19b DECIMAL
367
    DECLARE @a20 DECIMAL
368
    DECLARE @a21 DECIMAL
369
    DECLARE @a22 DECIMAL
370
    DECLARE @a22a DECIMAL
371
    DECLARE @a22b DECIMAL
372
    DECLARE @a23 DECIMAL
373
    DECLARE @a24 DECIMAL
374
    DECLARE @flg_a24 VARCHAR(1)
375
    DECLARE @bln_a24 VARCHAR(6)
376
    DECLARE @masa_pajak VARCHAR(10)
377
    DECLARE @kode_pajak VARCHAR(30)
378
    DECLARE @npwp_pemotong VARCHAR(15)
379
    DECLARE @nama_pemotong VARCHAR(200)
380
    DECLARE @company_name VARCHAR(200)
381
    DECLARE @a19murni DECIMAL
382
    DECLARE @a17_att DECIMAL
383
    DECLARE @ktp VARCHAR(50)
384

    
385
    DECLARE @jml_bulan_perolehan INT
386
    DECLARE @tot_biaya_jabatan DECIMAL
387

    
388
    DECLARE @result TABLE
389
        (
390
          kode_form VARCHAR(7) ,
391
          tahun_pajak VARCHAR(4) ,
392
          pembetulan INT ,
393
          nomor_urut CHAR(7) ,
394
          npwp_pegawai VARCHAR(30) ,
395
          nama_pegawai VARCHAR(50) ,
396
          alamat_pegawai VARCHAR(255) ,
397
          jabatan_pegawai VARCHAR(30) ,
398
          jenis_kelamin VARCHAR(5) ,
399
          status_pegawai VARCHAR(5) ,
400
          status_kawin VARCHAR(5) ,
401
          flag_asing VARCHAR(1) ,
402
          status_ptkp VARCHAR(5) ,
403
          jumlah_tanggungan VARCHAR(1) ,
404
          masa_perolehan_1 VARCHAR(2) ,
405
          masa_perolehan_2 VARCHAR(2) ,
406
          flag_status VARCHAR(1) ,
407
          a1 DECIMAL ,
408
          flg_a2 VARCHAR(1) ,
409
          a2 DECIMAL ,
410
          a3 DECIMAL ,
411
          a4 DECIMAL ,
412
          a5 DECIMAL ,
413
          a6 DECIMAL ,
414
          a7 DECIMAL ,
415
          a8 DECIMAL ,
416
          a9 DECIMAL ,
417
          a10 DECIMAL ,
418
          a11 DECIMAL ,
419
          a12 DECIMAL ,
420
          a13 DECIMAL ,
421
          a14 DECIMAL ,
422
          a15 DECIMAL ,
423
          a16 DECIMAL ,
424
          a17 DECIMAL ,
425
          a18 DECIMAL ,
426
          a19 DECIMAL ,
427
          a20 DECIMAL ,
428
          a21 DECIMAL ,
429
          a22 DECIMAL ,
430
          a22a DECIMAL ,
431
          a22b DECIMAL ,
432
          a23 DECIMAL ,
433
          a24 DECIMAL ,
434
          flg_a24 VARCHAR(1) ,
435
          bln_a24 VARCHAR(6)
436
	--,emp_id VARCHAR(20)
437
          ,
438
          KTP VARCHAR(50) ,
439
          nama_pemotong VARCHAR(200) ,
440
          npwp_pemotong VARCHAR(30) ,
441
          kode_pajak VARCHAR(30)
442
	--,company_name VARCHAR(200)
443
        )                   
444
 
445
    DECLARE @cu0300 TABLE
446
        (
447
          [code] [nVARCHAR](4) NOT NULL ,
448
          [start_date] [nVARCHAR](8) NOT NULL ,
449
          [end_date] [nVARCHAR](8) NOT NULL ,
450
          [spt_no] [INT] NULL
451
        )
452

    
453
    DECLARE @cu0300_tmp TABLE
454
        (
455
          [code] [nVARCHAR](4) NOT NULL ,
456
          [start_date] [nVARCHAR](8) NOT NULL ,
457
          [end_date] [nVARCHAR](8) NOT NULL ,
458
          [spt_no] [INT] NULL
459
        )
460
	
461
    DECLARE @tbl_tr_prev_ori TABLE
462
        (
463
          employee_id VARCHAR(8) ,
464
          wage_type VARCHAR(4) ,
465
          amount DECIMAL ,
466
          spt_no INT ,
467
          run_period_month VARCHAR(2)
468
        )
469

    
470
    DECLARE @tbl_tr_prev_ori_tmp TABLE
471
        (
472
          employee_id VARCHAR(8) ,
473
          wage_type VARCHAR(4) ,
474
          amount DECIMAL ,
475
          spt_no INT ,
476
          run_period_month VARCHAR(2)
477
        )
478
 
479
    DECLARE @tbl_tr_prev_cum TABLE
480
        (
481
          employee_id VARCHAR(8) ,
482
          wage_type VARCHAR(4) ,
483
          amount DECIMAL ,
484
          spt_no INT ,
485
          run_period_month VARCHAR(2)
486
        )
487

    
488
    DECLARE @tbl_tr_prev_cum_tmp TABLE
489
        (
490
          employee_id VARCHAR(8) ,
491
          wage_type VARCHAR(4) ,
492
          amount DECIMAL ,
493
          spt_no INT ,
494
          run_period_month VARCHAR(2)
495
        )
496

    
497
    DECLARE @tbl_tr_dec_ori TABLE
498
        (
499
          employee_id VARCHAR(8) ,
500
          wage_type VARCHAR(4) ,
501
          amount DECIMAL ,
502
          spt_no INT ,
503
          run_period_month VARCHAR(2)
504
        )
505

    
506
    DECLARE @tbl_tr_dec_ori_tmp TABLE
507
        (
508
          employee_id VARCHAR(8) ,
509
          wage_type VARCHAR(4) ,
510
          amount DECIMAL ,
511
          spt_no INT ,
512
          run_period_month VARCHAR(2)
513
        )
514
 
515
    DECLARE @tbl_tr_dec TABLE
516
        (
517
          employee_id VARCHAR(8) ,
518
          wage_type VARCHAR(4) ,
519
          amount DECIMAL ,
520
          spt_no INT ,
521
          run_period_month VARCHAR(2)
522
        )
523

    
524
    DECLARE @tbl_tr_dec_tmp TABLE
525
        (
526
          employee_id VARCHAR(8) ,
527
          wage_type VARCHAR(4) ,
528
          amount DECIMAL ,
529
          spt_no INT ,
530
          run_period_month VARCHAR(2)
531
        )
532

    
533
    DECLARE @tbl_tr_dec_total TABLE
534
        (
535
          employee_id VARCHAR(8) ,
536
          amount DECIMAL ,
537
          spt_no INT
538
        )
539
	 
540
    DECLARE @tbl_tr_enc TABLE
541
        (
542
          employee_id VARCHAR(8) ,
543
          wage_type VARCHAR(4) ,
544
          amount VARCHAR(250) ,
545
          spt_no INT
546
        )
547
                   
548
    SET @kode_form = 'D113248'
549
    SET @tahun_pajak = @Year
550
    SET @pembetulan = 0
551
 
552
    DECLARE @c_landscape VARCHAR(3)
553
    DECLARE @c_emp_id VARCHAR(8)
554
    DECLARE @c_start_date INT
555
    DECLARE @c_end_date INT
556

    
557
    DECLARE @last_period_payroll INT
558
    DECLARE @previous_period_payroll INT
559
    DECLARE @previous_period_payroll0 INT
560
    DECLARE @previous_period_payroll_varchar VARCHAR(2)
561

    
562
----//Run Cursor
563
    DECLARE cur_employee CURSOR
564
    FOR
565
        SELECT  *
566
        FROM    @employee_tobe_process
567
    OPEN cur_employee
568
    FETCH cur_employee INTO @c_emp_id, @c_start_date, @c_end_date
569
    WHILE @@fetch_status = 0 
570
        BEGIN  
571
            SET @last_period_payroll = @c_end_date
572
            SET @nomor_urut = CONVERT(INTEGER, RIGHT(@c_emp_id, 7))
573

    
574
            SELECT  @npwp_pegawai = REPLACE(REPLACE(ISNULL(NPWP,
575
                                                           '000000000000000'),
576
                                                    '.', ''), '-', '') ,
577
                    @status_kawin = CASE LEFT(TaxStatus, 1)
578
                                      WHEN 'T' THEN '1'
579
                                      ELSE '2'
580
                                    END ,
581
                    @status_ptkp = a.TaxStatus ,
582
                    @jumlah_tanggungan = CASE RIGHT(TaxStatus, 1)
583
                                           WHEN 'K' THEN '0'
584
                                           ELSE RIGHT(TaxStatus, 1)
585
                                         END
586
            FROM    dbo.PHRPA0003 AS a
587
            WHERE   EmployeeID = @c_emp_id
588
                    AND StartDate <= @c_end_date
589
                    AND EndDate >= @c_end_date
590
	
591
            SELECT  DISTINCT
592
                    @a17 = PTKP
593
            FROM    dbo.PHRPYCU0302
594
            WHERE   StartDate <= @c_end_date
595
                    AND EndDate >= @c_end_date
596
                    AND TaxStatus = ( SELECT DISTINCT
597
                                                TaxStatus
598
                                      FROM      dbo.PHRPA0003
599
                                      WHERE     EmployeeID = @c_emp_id
600
                                                AND StartDate <= @c_end_date
601
                                                AND EndDate >= @c_end_date
602
                                    )
603

    
604
            SELECT  @nama_pegawai = FullName ,
605
                    @jenis_kelamin = b.GenderDescription ,
606
                    @ktp = c.IDDescription
607
            FROM    dbo.PHRPA0001 AS a
608
                    LEFT JOIN dbo.PHRPAGENDER AS b ON a.Gender = b.Gender
609
                                                      AND b.StartDate <= @c_end_date
610
                                                      AND b.EndDate >= @c_end_date
611
                    LEFT JOIN dbo.PHRPA0013 AS c ON a.EmployeeID = c.EmployeeID
612
                                                    AND c.StartDate <= @c_end_date
613
                                                    AND c.EndDate >= @c_end_date
614
            WHERE   a.EmployeeID = @c_emp_id
615
                    AND a.StartDate <= @c_end_date
616
                    AND a.EndDate >= @c_end_date
617
                    AND c.IDType = '01'
618
	 
619
            SELECT  @nama_pemotong = t.TaxOfficeDescription ,
620
                    @npwp_pemotong = o.NPWP
621
            FROM    dbo.PHRPA0002 AS a
622
                    LEFT JOIN dbo.PCMEPEMPOFF AS o ON a.EmployeeOffice = o.EmployeeOffice
623
                                                      AND o.StartDate <= @c_end_date
624
                                                      AND o.EndDate >= @c_end_date
625
                    LEFT JOIN dbo.PHRPYTAXOFF AS t ON t.TaxOffice = o.TaxOffice
626
                                                      AND t.StartDate <= @c_end_date
627
                                                      AND t.EndDate >= @c_end_date
628
            WHERE   a.StartDate <= @c_end_date
629
                    AND a.EndDate >= @c_end_date
630
                    AND a.EmployeeID = @c_emp_id
631
			
632
            SELECT  @kode_pajak = e.TaxCode
633
            FROM    dbo.PHRPA0002 AS a
634
                    LEFT JOIN dbo.PCMEPEMPTYP AS e ON a.EmployeeType = e.EmployeeType
635
            WHERE   a.StartDate <= @c_end_date
636
                    AND a.EndDate >= @c_end_date
637
                    AND a.EmployeeID = @c_emp_id
638
			
639
	--//alamat & flag status belakangan
640
            SELECT  @alamat_pegawai = REPLACE(md06.Address, ',', ' ')
641
            FROM    dbo.PHRPA0006 AS md06
642
            WHERE   EmployeeID = @c_emp_id
643
                    AND StartDate <= @c_end_date
644
                    AND EndDate >= @c_end_date
645
                    AND AddressType = @npwp_address_subtype
646

    
647
            SELECT  @jabatan_pegawai = mdobj.ObjectDescription
648
            FROM    dbo.PHRPA0002 AS md02
649
                    INNER JOIN dbo.PHROM0001 AS mdobj ON md02.Position = mdobj.ObjectID
650
                                                         AND mdobj.ObjectClass = 'P'
651
                                                         AND mdobj.StartDate <= @c_end_date
652
                                                         AND mdobj.EndDate >= @c_end_date
653
            WHERE   md02.EmployeeID = @c_emp_id
654
                    AND md02.StartDate <= @c_end_date
655
                    AND md02.EndDate >= @c_end_date
656
	
657
            SELECT  @status_pegawai = CASE EmployeeStatus
658
                                        WHEN '01' THEN '1'
659
                                        WHEN '03' THEN '2'
660
                                        ELSE '1'
661
                                      END ,
662
                    @flag_asing = CASE EmployeeType
663
                                    WHEN '05' THEN '1'
664
                                    ELSE '0'
665
                                  END
666
            FROM    dbo.PHRPA0002 AS md02
667
            WHERE   EmployeeID = @c_emp_id
668
                    AND StartDate <= @c_end_date
669
                    AND EndDate >= @c_end_date
670

    
671
            SET @masa_perolehan_1 = CONVERT(INTEGER, SUBSTRING(CONVERT(VARCHAR, @c_start_date),
672
                                                              5, 2))
673
            SET @masa_perolehan_2 = CONVERT(INTEGER, SUBSTRING(CONVERT(VARCHAR, @c_end_date),
674
                                                              5, 2))
675
            SET @jml_bulan_perolehan = ( CONVERT(INT, @masa_perolehan_2)
676
                                         - CONVERT(INT, @masa_perolehan_1) + 1 )
677

    
678
	--//#region getwtconfiguration
679
            DELETE  @cu0300_tmp
680

    
681
            INSERT  INTO @cu0300_tmp
682
                    SELECT  WageTypeDefinition ,
683
                            StartDate ,
684
                            EndDate ,
685
                            SPTNo
686
                    FROM    dbo.PHRPYCU0300
687
                    WHERE   StartDate <= @c_end_date
688
                            AND EndDate >= @c_start_date
689
                            AND SPTNo > 0
690

    
691
            UPDATE  @cu0300_tmp
692
            SET     start_date = @begda
693
            WHERE   start_date < @c_start_date
694
	  
695
            UPDATE  @cu0300_tmp
696
            SET     end_date = @endda
697
            WHERE   end_date > @c_end_date
698

    
699
--/*
700
            DELETE  @cu0300
701

    
702
            INSERT  INTO @cu0300
703
                    SELECT  cu0300.*
704
                    FROM    @cu0300_tmp AS cu0300
705
                            INNER JOIN ( SELECT code ,
706
                                                MIN(start_date) AS start_date ,
707
                                                MAX(end_date) AS end_date
708
                                         FROM   @cu0300_tmp
709
                                         GROUP BY code
710
                                       ) a ON cu0300.code = a.code
711
                                              AND cu0300.end_date = a.end_date
712

    
713
            DELETE  @tbl_tr_dec
714
            DELETE  @tbl_tr_dec_ori
715

    
716
            DELETE  @tbl_tr_dec_tmp
717
            DELETE  @tbl_tr_dec_ori_tmp
718

    
719
            DECLARE @cc_landscape VARCHAR(3)
720
            DECLARE @cc_code VARCHAR(4)
721
            DECLARE @cc_start_date INT
722
            DECLARE @cc_end_date INT
723
            DECLARE @cc_spt_no INT
724

    
725
            INSERT  INTO @tbl_tr_dec_tmp
726
                    SELECT  tr301.EmployeeID ,
727
                            tr301.WageType ,
728
                            CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'),
729
                                         ',', '.') AS DECIMAL(22, 0)) ,
730
                            cu300.spt_no ,
731
                            tr301.RunPeriodMonth
732
                    FROM    dbo.PHRPYTR0301CUM AS tr301
733
                            INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
734
                    WHERE   tr301.EmployeeID = @c_emp_id
735
                            AND tr301.PayPeriodMonth = SUBSTRING(CONVERT(VARCHAR, @last_period_payroll),
736
                                                              5, 2)
737
                            AND tr301.PayPeriodYear = tr301.RunPeriodYear
738
                            AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
739
                                                           4)
740
		  
741
	--//get the latest run data
742
            INSERT  INTO @tbl_tr_dec
743
                    SELECT  tr_dec.*
744
                    FROM    @tbl_tr_dec_tmp AS tr_dec
745
                            INNER JOIN ( SELECT employee_id ,
746
                                                wage_type ,
747
                                                MAX(run_period_month) AS run_period_month
748
                                         FROM   @tbl_tr_dec_tmp
749
                                         GROUP BY employee_id ,
750
                                                wage_type
751
                                       ) a ON tr_dec.employee_id = a.employee_id
752
                                              AND tr_dec.wage_type = a.wage_type
753
                                              AND tr_dec.run_period_month = a.run_period_month
754

    
755

    
756
            INSERT  INTO @tbl_tr_dec_ori_tmp
757
                    SELECT  tr301.EmployeeID ,
758
                            tr301.WageType ,
759
                            CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'),
760
                                         ',', '.') AS DECIMAL(22, 0)) ,
761
                            cu300.spt_no ,
762
                            tr301.RunPeriodMonth
763
                    FROM    dbo.PHRPYTR0301 AS tr301
764
                            INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
765
                    WHERE   tr301.EmployeeID = @c_emp_id
766
                            AND tr301.PayPeriodMonth = SUBSTRING(CONVERT(VARCHAR, @last_period_payroll),
767
                                                              5, 2)
768
                            AND tr301.PayPeriodYear = tr301.RunPeriodYear
769
                            AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
770
                                                           4)
771
		  
772
	--//get the latest run data
773
            INSERT  INTO @tbl_tr_dec_ori
774
                    SELECT  tr_dec_ori.*
775
                    FROM    @tbl_tr_dec_ori_tmp AS tr_dec_ori
776
                            INNER JOIN ( SELECT employee_id ,
777
                                                wage_type ,
778
                                                MAX(run_period_month) AS run_period_month
779
                                         FROM   @tbl_tr_dec_ori_tmp
780
                                         GROUP BY employee_id ,
781
                                                wage_type
782
                                       ) a ON tr_dec_ori.employee_id = a.employee_id
783
                                              AND tr_dec_ori.wage_type = a.wage_type
784
                                              AND tr_dec_ori.run_period_month = a.run_period_month
785
		
786
            IF ( @masa_perolehan_1 > '1'
787
                 OR @masa_perolehan_1 > '01'
788
               ) 
789
                BEGIN
790
                    SET @previous_period_payroll = CONVERT(INT, @masa_perolehan_1)
791
                        - 1
792
                    IF ( @previous_period_payroll < 10 ) 
793
                        SET @previous_period_payroll_varchar = '0'
794
                            + CONVERT(VARCHAR(1), @previous_period_payroll)
795
                    ELSE 
796
                        SET @previous_period_payroll_varchar = CONVERT(VARCHAR(2), @previous_period_payroll)
797
			
798
		--//get the latest pay period if previous period no pay period.
799
                    SET @previous_period_payroll0 = @previous_period_payroll
800
                    SELECT TOP ( 1 )
801
                            @previous_period_payroll0 = CONVERT(INT, PayPeriodMonth)
802
                    FROM    dbo.PHRPYTR0300
803
                    WHERE   EmployeeID = @c_emp_id
804
                            AND PayPeriodMonth <= @previous_period_payroll_varchar
805
                            AND PayPeriodYear = LEFT(@last_period_payroll, 4)
806
                            AND RunPeriodYear = LEFT(@last_period_payroll, 4)
807
                    ORDER BY PayPeriodMonth DESC
808
			
809
                    IF ( @previous_period_payroll0 < 10 ) 
810
                        SET @previous_period_payroll_varchar = '0'
811
                            + CONVERT(VARCHAR(1), @previous_period_payroll0)
812
                    ELSE 
813
                        SET @previous_period_payroll_varchar = CONVERT(VARCHAR(2), @previous_period_payroll0)
814
					
815
                    DELETE  FROM @tbl_tr_prev_ori
816
                    DELETE  FROM @tbl_tr_prev_cum
817
		
818
                    DELETE  FROM @tbl_tr_prev_ori_tmp
819
                    DELETE  FROM @tbl_tr_prev_cum_tmp
820

    
821
                    INSERT  INTO @tbl_tr_prev_ori_tmp
822
                            SELECT  tr301.EmployeeID ,
823
                                    tr301.WageType ,
824
                                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount),
825
                                                        '0'), ',', '.') AS DECIMAL(22,
826
                                                              0)) ,
827
                                    cu300.spt_no ,
828
                                    tr301.RunPeriodMonth
829
                            FROM    dbo.PHRPYTR0301 AS tr301
830
                                    INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
831
                            WHERE   tr301.EmployeeID = @c_emp_id
832
                                    AND tr301.PayPeriodMonth = @previous_period_payroll_varchar
833
                                    AND tr301.PayPeriodYear = tr301.RunPeriodYear
834
                                    AND tr301.RunPeriodMonth = LEFT(@last_period_payroll,
835
                                                              4)
836

    
837
		--//get the latest run data
838
                    INSERT  INTO @tbl_tr_prev_ori
839
                            SELECT  tr_prev_ori.*
840
                            FROM    @tbl_tr_prev_ori_tmp AS tr_prev_ori
841
                                    INNER JOIN ( SELECT employee_id ,
842
                                                        wage_type ,
843
                                                        MAX(run_period_month) AS run_period_month
844
                                                 FROM   @tbl_tr_prev_ori_tmp
845
                                                 GROUP BY employee_id ,
846
                                                        wage_type
847
                                               ) a ON tr_prev_ori.employee_id = a.employee_id
848
                                                      AND tr_prev_ori.wage_type = a.wage_type
849
                                                      AND tr_prev_ori.run_period_month = a.run_period_month
850

    
851
                    INSERT  INTO @tbl_tr_prev_cum_tmp
852
                            SELECT  tr301.EmployeeID ,
853
                                    tr301.WageType ,
854
                                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount),
855
                                                        '0'), ',', '.') AS DECIMAL(22,
856
                                                              0)) ,
857
                                    cu300.spt_no ,
858
                                    tr301.RunPeriodMonth
859
                            FROM    dbo.PHRPYTR0301CUM AS tr301
860
                                    INNER JOIN @cu0300 AS cu300 ON cu300.code = tr301.WageType
861
                            WHERE   tr301.EmployeeID = @c_emp_id
862
                                    AND tr301.PayPeriodMonth = @previous_period_payroll_varchar
863
                                    AND tr301.PayPeriodYear = tr301.RunPeriodYear
864
                                    AND tr301.RunPeriodYear = LEFT(@last_period_payroll,
865
                                                              4)
866

    
867
		--//get the latest run data
868
                    INSERT  INTO @tbl_tr_prev_cum
869
                            SELECT  tr_prev_cum.*
870
                            FROM    @tbl_tr_prev_cum_tmp AS tr_prev_cum
871
                                    INNER JOIN ( SELECT employee_id ,
872
                                                        wage_type ,
873
                                                        MAX(run_period_month) AS run_period_month
874
                                                 FROM   @tbl_tr_prev_cum_tmp
875
                                                 GROUP BY employee_id ,
876
                                                        wage_type
877
                                               ) a ON tr_prev_cum.employee_id = a.employee_id
878
                                                      AND tr_prev_cum.wage_type = a.wage_type
879
                                                      AND tr_prev_cum.run_period_month = a.run_period_month	
880
                END 
881

    
882
            DECLARE @emp_status VARCHAR(2) ,
883
                @emp_type VARCHAR(2) ,
884
                @movement_type VARCHAR(2) ,
885
                @movement_reason VARCHAR(2) ,
886
                @emp_type_payroll VARCHAR(20) ,
887
                @movement_reason_payroll VARCHAR(20) ,
888
                @is_pegawai_baru VARCHAR(1)
889
		
890
            DECLARE @death_mov_reason VARCHAR(5)
891
            SELECT TOP ( 1 )
892
                    @death_mov_reason = Value1
893
            FROM    dbo.PCMEPGENPARAM
894
            WHERE   Parameter = 'HR_ESPT_MOV_REASON_DEATH'
895
                    AND StartDate <= @endda
896
                    AND EndDate >= @endda
897
		
898
            SELECT  @emp_status = md02.EmployeeStatus ,
899
                    @emp_type = md02.EmployeeType ,
900
                    @movement_type = md02.MovementType ,
901
                    @movement_reason = md02.MovementReason ,
902
                    @emp_type_payroll = ret.PayrollProcess ,
903
                    @movement_reason_payroll = rmr.PayrollProcess
904
            FROM    dbo.PHRPA0002 md02
905
                    INNER JOIN dbo.PCMEPEMPTYP ret ON md02.EmployeeType = ret.EmployeeType
906
                    INNER JOIN dbo.PCMEPMOVR rmr ON md02.MovementReason = rmr.MovementReason
907
            WHERE   EmployeeID = @c_emp_id
908
                    AND md02.StartDate <= @endda
909
                    AND md02.EndDate >= @endda
910
		
911
	
912
            SET @is_pegawai_baru = '0'
913
            IF @c_start_date > @begda 
914
                BEGIN
915
                    SELECT  @is_pegawai_baru = CASE WHEN HiringDate >= @c_start_date
916
                                                    THEN '1'
917
                                                    ELSE '0'
918
                                               END
919
                    FROM    dbo.PHRPA0015
920
                    WHERE   EmployeeID = @c_emp_id
921
                END
922

    
923
	--//Setting flag status
924
            IF @emp_status = '01'
925
                AND @masa_perolehan_1 = '1'
926
                AND @masa_perolehan_2 = '12'
927
                AND ISNULL(@emp_type_payroll, '') <> 'EXP' 
928
                SET @flag_status = '0'
929
            ELSE 
930
                IF @emp_status = '01'
931
                    AND @masa_perolehan_1 = '1'
932
                    AND @masa_perolehan_2 = '12'
933
                    AND ISNULL(@emp_type_payroll, '') = 'EXP' 
934
                    SET @flag_status = '3'
935
                ELSE 
936
                    IF @emp_status = '01'
937
                        AND @masa_perolehan_2 <> '12' 
938
                        SET @flag_status = '1'
939
                    ELSE 
940
                        IF @is_pegawai_baru = '1' 
941
                            SET @flag_status = '4'
942
                        ELSE 
943
                            IF @emp_status = '01'
944
                                AND @masa_perolehan_2 <> '1'
945
                                AND @masa_perolehan_2 = '12' 
946
                                SET @flag_status = '5'
947
                            ELSE 
948
                                IF ( @movement_type = '17'
949
                                     OR @emp_status <> '01'
950
                                   ) 
951
                                    BEGIN
952
                                        IF @movement_reason = '12' 
953
                                            SET @flag_status = '2'
954
                                        ELSE 
955
                                            IF @movement_reason = @death_mov_reason
956
                                                OR @emp_type_payroll = 'EXP' 
957
                                                SET @flag_status = '3'
958
                                            ELSE 
959
                                                IF @masa_perolehan_1 = '1'
960
                                                    AND @masa_perolehan_2 = '12' 
961
                                                    SET @flag_status = '0'
962
                                                ELSE 
963
                                                    SET @flag_status = '2'
964
                                    END
965

    
966
            SELECT  @a1 = ISNULL(SUM(amount), 0)
967
            FROM    @tbl_tr_dec
968
            WHERE   spt_no = 1
969
            GROUP BY spt_no
970

    
971
            IF ( @masa_perolehan_1 <> '1' ) 
972
                SELECT  @a1 = @a1 - ISNULL(SUM(amount), 0)
973
                FROM    @tbl_tr_prev_cum
974
                WHERE   spt_no = 1
975
                GROUP BY spt_no
976

    
977
            SET @a1 = ISNULL(@a1, 0)
978
            SELECT  @a2 = ISNULL(SUM(amount), 0)
979
            FROM    @tbl_tr_dec
980
            WHERE   spt_no = 2
981
            GROUP BY spt_no
982

    
983
            IF ( @masa_perolehan_1 <> '1' ) 
984
                SELECT  @a2 = @a2 - ISNULL(SUM(amount), 0)
985
                FROM    @tbl_tr_prev_cum
986
                WHERE   spt_no = 2
987
                GROUP BY spt_no
988
				  
989
				
990
            SET @a2 = ISNULL(@a2, 0)
991
	
992
            SELECT  @a3 = ISNULL(SUM(amount), 0)
993
            FROM    @tbl_tr_dec
994
            WHERE   spt_no = 3
995
            GROUP BY spt_no
996
	  
997
            IF ( @masa_perolehan_1 <> '1' ) 
998
                SELECT  @a3 = @a3 - ISNULL(SUM(amount), 0)
999
                FROM    @tbl_tr_prev_cum
1000
                WHERE   spt_no = 3
1001
                GROUP BY spt_no
1002
				  
1003
            SET @a3 = ISNULL(@a3, 0)
1004
	
1005
            SELECT  @a4 = ISNULL(SUM(amount), 0)
1006
            FROM    @tbl_tr_dec
1007
            WHERE   spt_no = 4
1008
            GROUP BY spt_no
1009
	
1010
            SET @a4 = ISNULL(@a4, 0)
1011
	
1012
            SELECT  @a5 = ISNULL(SUM(amount), 0)
1013
            FROM    @tbl_tr_dec
1014
            WHERE   spt_no = 5
1015
            GROUP BY spt_no
1016
					  
1017
            IF ( @masa_perolehan_1 <> '1' ) 
1018
                SELECT  @a5 = @a5 - ISNULL(SUM(amount), 0)
1019
                FROM    @tbl_tr_prev_cum
1020
                WHERE   spt_no = 5
1021
                GROUP BY spt_no
1022
					  
1023
            SET @a5 = ISNULL(@a5, 0)
1024
            SELECT  @a6 = ISNULL(SUM(amount), 0)
1025
            FROM    @tbl_tr_dec
1026
            WHERE   spt_no = 6
1027
            GROUP BY spt_no
1028
	
1029
            SET @a6 = ISNULL(@a6, 0)
1030

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

    
1033
            SELECT  @a8 = ISNULL(SUM(amount), 0)
1034
            FROM    @tbl_tr_dec
1035
            WHERE   spt_no = 8
1036
            GROUP BY spt_no
1037
					  
1038
            IF ( @masa_perolehan_1 <> '1' ) 
1039
                SELECT  @a8 = @a8 - ISNULL(SUM(amount), 0)
1040
                FROM    @tbl_tr_prev_cum
1041
                WHERE   spt_no = 8
1042
                GROUP BY spt_no
1043

    
1044
            SET @a8 = ISNULL(@a8, 0)
1045

    
1046
            SET @a9 = ISNULL(@a7 + @a8, 0)
1047

    
1048
            SELECT  @a10 = ISNULL(SUM(amount), 0)
1049
            FROM    @tbl_tr_dec
1050
            WHERE   spt_no = 10
1051
            GROUP BY spt_no
1052

    
1053
            IF ( @masa_perolehan_1 <> '1' ) 
1054
                SELECT  @a10 = @a10 - ISNULL(SUM(amount), 0)
1055
                FROM    @tbl_tr_prev_cum
1056
                WHERE   spt_no = 10
1057
                GROUP BY spt_no
1058

    
1059
            SET @a10 = ISNULL(@a10, 0)
1060

    
1061
            SET @tot_biaya_jabatan = 500000 * @jml_bulan_perolehan
1062
            IF ( @a10 > @tot_biaya_jabatan ) 
1063
                BEGIN
1064
                    SET @a10 = @tot_biaya_jabatan
1065
                    SET @a11 = 0
1066
                END
1067

    
1068
            SELECT  @a11 = ISNULL(SUM(amount), 0)
1069
            FROM    @tbl_tr_dec
1070
            WHERE   spt_no = 11
1071
            GROUP BY spt_no
1072
					  
1073
            IF ( @masa_perolehan_1 <> '1' ) 
1074
                SELECT  @a11 = @a11 - ISNULL(SUM(amount), 0)
1075
                FROM    @tbl_tr_prev_cum
1076
                WHERE   spt_no = 11
1077
                GROUP BY spt_no
1078
					  
1079
            SET @a11 = ISNULL(@a11, 0)
1080

    
1081
            IF ( ( @a10 + @a11 ) > @tot_biaya_jabatan ) 
1082
                BEGIN 
1083
                    SET @a11 = @tot_biaya_jabatan - @a10
1084
                END
1085

    
1086
            SELECT  @a12 = ISNULL(SUM(amount), 0)
1087
            FROM    @tbl_tr_dec
1088
            WHERE   spt_no = 12
1089
            GROUP BY spt_no
1090

    
1091
            IF ( @masa_perolehan_1 <> '1' ) 
1092
                SELECT  @a12 = @a12 - ISNULL(SUM(amount), 0)
1093
                FROM    @tbl_tr_prev_cum
1094
                WHERE   spt_no = 12
1095
                GROUP BY spt_no
1096

    
1097
            SET @a12 = ISNULL(@a12, 0)
1098
	
1099
            SET @a13 = ISNULL(@a10 + @a11 + @a12, 0)
1100
	
1101
            SET @a14 = ISNULL(@a9 - @a13, 0)
1102

    
1103
            SELECT  @a15 = ISNULL(SUM(amount), 0)
1104
            FROM    @tbl_tr_dec_ori
1105
            WHERE   spt_no = 15
1106
            GROUP BY spt_no
1107
            SET @a15 = ISNULL(@a15, 0)
1108

    
1109
            SET @a16 = ISNULL(@a14 + @a15, 0)
1110

    
1111
	--//spt_no 16 case khusus flag_status khusus
1112
            SELECT  @a16_flag = ISNULL(SUM(amount), 0)
1113
            FROM    @tbl_tr_dec_ori
1114
            WHERE   spt_no = 16
1115
            GROUP BY spt_no
1116
            SET @a16_flag = ISNULL(@a16_flag, 0)
1117

    
1118
            IF @movement_reason = @death_mov_reason
1119
                OR ISNULL(@emp_type_payroll, '') = 'EXP' 
1120
                BEGIN
1121
                    SELECT  @a16 = CASE WHEN ISNULL(@emp_type_payroll, '') = 'EXP'
1122
                                        THEN @a16
1123
                                        ELSE @a16 * 12
1124
                                             / ( CONVERT(INT, @masa_perolehan_2)
1125
                                                 - CONVERT(INT, @masa_perolehan_1)
1126
                                                 + 1 )
1127
                                   END
1128
                END
1129
	
1130
            IF @flag_status = '1'
1131
                OR @flag_status = '3' 
1132
                BEGIN
1133
                    SET @a16 = @a16_flag
1134
                END
1135

    
1136
            SET @a18 = @a16 - @a17
1137
	
1138
            IF ( RIGHT(RTRIM(CAST(@a18 AS VARCHAR(19))), 3) <> '000' ) 
1139
                BEGIN
1140
                    DECLARE @Pembulatan1000 AS VARCHAR(19)
1141
                    SET @Pembulatan1000 = CAST(@a18 AS VARCHAR(19)); 		
1142
                    SET @a18 = SUBSTRING(@Pembulatan1000, 1,
1143
                                         LEN(RTRIM(@Pembulatan1000)) - 3)
1144
                        + REPLICATE('0', 3); 		
1145
                END
1146

    
1147
	--//ambil mt murni tanpa kondisi
1148
            SELECT  @a19murni = ISNULL(SUM(amount), 0)
1149
            FROM    @tbl_tr_dec
1150
            WHERE   spt_no = 19
1151
            GROUP BY spt_no
1152

    
1153
	--//ambil att murni tanpa kondisi
1154
            SELECT  @a17_att = ISNULL(SUM(amount), 0)
1155
            FROM    @tbl_tr_dec_ori
1156
            WHERE   spt_no = 99
1157
            GROUP BY spt_no
1158

    
1159
	--//cek status employee apakah sudah resign
1160
            SELECT  @a19 = ISNULL(SUM(amount), 0)
1161
            FROM    @tbl_tr_dec
1162
            WHERE   spt_no = 19
1163
            GROUP BY spt_no
1164

    
1165
            IF ISNULL(@emp_type_payroll, '') = 'EXP' 
1166
                BEGIN
1167
                    SELECT  @a19b = ISNULL(SUM(amount), 0)
1168
                    FROM    @tbl_tr_prev_cum
1169
                    WHERE   spt_no = 19
1170
                    GROUP BY spt_no
1171
                    SET @a19 = @a19 * 12 / ( CONVERT(INT, @masa_perolehan_2)
1172
                                             - CONVERT(INT, @masa_perolehan_1)
1173
                                             + 1 )			
1174
                END
1175
            ELSE 
1176
                IF ( @movement_type = 17
1177
                     OR @emp_status <> '01'
1178
                   ) 
1179
                    BEGIN
1180
                        IF @movement_reason = '12' 
1181
                            BEGIN
1182
                                SET @a19 = ISNULL(@a19murni, 0)
1183
                            END
1184
                    END
1185
                ELSE 
1186
                    BEGIN
1187
                        IF ( ( @masa_perolehan_1 = '1' )
1188
                             AND ( @masa_perolehan_2 <> '12' )
1189
                           ) 
1190
                            BEGIN
1191
                                SET @a19 = @a19 * 12
1192
                                    / CONVERT(INT, @masa_perolehan_2)
1193
                            END
1194
                    END
1195
			
1196
            SET @a19 = ISNULL(@a19, 0)
1197

    
1198
			SET @a17_att = CASE WHEN @emp_status <> '1' THEN @a19 ELSE @a17_att END ---- // add by Tri nwh 20220606
1199

    
1200
            SELECT  @a20 = CASE WHEN CONVERT(INT, @masa_perolehan_1) > 1
1201
                                THEN ISNULL(SUM(amount), 0)
1202
                                ELSE 0
1203
                           END
1204
            FROM    @tbl_tr_dec_ori
1205
            WHERE   spt_no = 20
1206
            GROUP BY spt_no
1207

    
1208
            SET @a20 = ISNULL(@a20, 0)
1209

    
1210
	------------------ // cek apa sdh pernah running di period sebelum, untuk kebutuhan init paycum, mengganggap a20 lama atau a18 baru menjadi 0 // -----------------
1211
            DECLARE @flag_running VARCHAR(200)
1212
            SELECT  @flag_running = CASE WHEN COUNT(tr.EmployeeID) <= 0
1213
                                         THEN 'no_data'
1214
                                         ELSE 'yes_data'
1215
                                    END
1216
            FROM    dbo.PHRPYTR0300 AS tr
1217
            WHERE   tr.RunPeriodMonth = tr.PayPeriodMonth
1218
                    AND tr.RunPeriodYear = tr.PayPeriodYear
1219
                    AND tr.RunPeriodMonth = @previous_period_payroll_varchar
1220
                    AND tr.EmployeeID = @c_emp_id
1221
	------------------ // pengecekan running selesai // ------------------ 
1222

    
1223
            IF ( @emp_status = '01'
1224
                 AND NOT ( @masa_perolehan_1 = '1'
1225
                           AND @masa_perolehan_2 = '12'
1226
                         )
1227
               )
1228
                OR ISNULL(@emp_type_payroll, '') = 'EXP'
1229
                OR @movement_reason = @death_mov_reason 
1230
                BEGIN	
1231
                    IF @flag_running = 'no_data'  -----// by Tri 20151229 untuk perubahan case upload init paycum
1232
                        BEGIN
1233
                            SET @a21 = ISNULL(@a19murni - 0, 0)
1234
                        END 
1235
                    ELSE 
1236
                        BEGIN
1237
                            SET @a21 = ISNULL(@a19murni - @a20, 0)
1238
                        END --@a2--@a19 								
1239
                END
1240

    
1241
--		ELSE
1242
            SET @a21 = ISNULL(@a19 - @a20, 0)
1243

    
1244
		----// sementara di hard-code dulu, untuk handling case mks.
1245

    
1246
            SET @a22 = @a21 + @a20
1247
            SET @a22a = 0
1248
            SET @a22b = @a21
1249
            SET @a23 = ISNULL(@a21 - @a22, 0)
1250
            SET @a24 = ISNULL(@a23, 0)
1251

    
1252
            SET @bln_a24 = SUBSTRING(CONVERT(VARCHAR, @c_end_date), 5, 2)
1253
                + LEFT(CONVERT(VARCHAR, @c_end_date), 4) 
1254

    
1255
            IF ( @a21 - @a22 = 0 ) 
1256
                BEGIN
1257
                    SET @flg_a24 = '0'
1258
                    SET @bln_a24 = ''
1259
                END
1260

    
1261
            IF ( @a21 - @a22 > 0 ) 
1262
                BEGIN
1263
                    SET @flg_a24 = '1'
1264
                END
1265

    
1266
            IF ( @a21 - @a22 < 0 ) 
1267
                BEGIN
1268
                    SET @flg_a24 = '2'
1269
                END
1270

    
1271
            SET @flg_a2 = '0'
1272
		
1273
		--//Pegawai yang dipindahkan
1274
            SET @flg_a2 = '1'
1275

    
1276
            DELETE  @tbl_tr_dec
1277

    
1278
            INSERT  INTO @result
1279
                    SELECT  @kode_form ,
1280
                            @tahun_pajak ,
1281
                            @pembetulan ,
1282
                            CONVERT(CHAR, RIGHT('0000000'
1283
                                                + CONVERT(VARCHAR(7), @nomor_urut),
1284
                                                7)) AS nomor_urut ,
1285
                            @npwp_pegawai ,
1286
                            @nama_pegawai ,
1287
                            @alamat_pegawai ,
1288
                            @jabatan_pegawai ,
1289
                            @jenis_kelamin ,
1290
                            @status_pegawai ,
1291
                            @status_kawin ,
1292
                            @flag_asing ,
1293
                            @status_ptkp ,
1294
                            @jumlah_tanggungan ,
1295
                            @masa_perolehan_1 ,
1296
                            @masa_perolehan_2 ,
1297
                            @flag_status ,
1298
                            @a1 ,
1299
                            @flg_a2 ,
1300
                            @a2 ,
1301
                            @a3 ,
1302
                            @a4 ,
1303
                            @a5 ,
1304
                            @a6 ,
1305
                            @a7 ,
1306
                            @a8 ,
1307
                            @a9 ,
1308
                            @a10 ,
1309
                            @a11 ,
1310
                            @a12 ,
1311
                            @a13 ,
1312
                            @a14 ,
1313
                            @a15 ,
1314
                            @a16 ,
1315
                            @a17 ,
1316
                            @a18 ,
1317
							@a17_att ,
1318
                            @a20 ,
1319
                            @a21 ,
1320
                            @a22 ,
1321
                            @a22a ,
1322
                            @a22b ,
1323
                            @a23 ,
1324
                            @a24 ,
1325
                            @flg_a24 ,
1326
                            @bln_a24 ,
1327
                            @ktp
1328
		--, @c_emp_id
1329
                            ,
1330
                            @nama_pemotong ,
1331
                            @npwp_pemotong ,
1332
                            @kode_pajak
1333
		--, @company_name
1334
					
1335

    
1336
            SET @nomor_urut = NULL
1337
            SET @npwp_pegawai = NULL
1338
            SET @nama_pegawai = NULL
1339
            SET @alamat_pegawai = NULL
1340
            SET @jabatan_pegawai = NULL
1341
            SET @jenis_kelamin = NULL
1342
            SET @status_pegawai = NULL
1343
            SET @status_kawin = NULL
1344
            SET @flag_asing = NULL
1345
            SET @status_ptkp = NULL
1346
            SET @jumlah_tanggungan = NULL
1347
            SET @masa_perolehan_1 = NULL
1348
            SET @masa_perolehan_2 = NULL
1349
            SET @flag_status = NULL
1350
            SET @a1 = NULL
1351
            SET @flg_a2 = NULL
1352
            SET @a2 = NULL
1353
            SET @a3 = NULL
1354
            SET @a4 = NULL
1355
            SET @a5 = NULL
1356
            SET @a6 = NULL
1357
            SET @a7 = NULL
1358
            SET @a8 = NULL
1359
            SET @a9 = NULL
1360
            SET @a10 = NULL
1361
            SET @a11 = NULL
1362
            SET @a12 = NULL
1363
            SET @a13 = NULL
1364
            SET @a14 = NULL
1365
            SET @a15 = NULL
1366
            SET @a16 = NULL
1367
            SET @a16_flag = NULL
1368
            SET @a17 = NULL
1369
            SET @a18 = NULL
1370
            SET @a19 = NULL
1371
            SET @a20 = NULL
1372
            SET @a21 = NULL
1373
            SET @a22 = NULL
1374
            SET @a22a = NULL
1375
            SET @a22b = NULL
1376
            SET @a23 = NULL
1377
            SET @a24 = NULL
1378
            SET @flg_a24 = NULL
1379
            SET @bln_a24 = NULL
1380

    
1381
            FETCH cur_employee INTO @c_emp_id, @c_start_date, @c_end_date   
1382
        END
1383
    CLOSE cur_employee
1384
    DEALLOCATE cur_employee
1385

    
1386
    UPDATE  @result
1387
    SET     npwp_pegawai = '000000000000000'
1388
    WHERE   LEN(npwp_pegawai) < 15
1389

    
1390
    UPDATE  @result
1391
    SET     alamat_pegawai = '-'
1392
    WHERE   alamat_pegawai = ''
1393
            OR alamat_pegawai IS NULL
1394
		
1395
    SELECT  masa_perolehan_2 AS TaxPeriod ,
1396
            tahun_pajak AS TaxableYear,
1397
            pembetulan AS TaxCorrection ,
1398
            '1.1-' + CONVERT(CHAR(2), RIGHT('00'
1399
                                            + CONVERT(VARCHAR(2), masa_perolehan_2),
1400
                                            2)) + '.' + RIGHT(@Year, 2) + '-'
1401
            + nomor_urut AS WithholdingTax ,
1402
            --CONVERT(CHAR(2), RIGHT('00' + CONVERT(VARCHAR(2), masa_perolehan_2),
1403
            --                       2)) AS bulan_bukti_potong ,
1404
            --RIGHT(@Year, 2) AS tahun_bukti_potong ,
1405
            --nomor_urut ,
1406
            masa_perolehan_1 AS AcquisitionPeriod ,
1407
            masa_perolehan_2 AS AcquisitionPeriodFinal ,
1408
            npwp_pegawai AS NPWP ,
1409
            --RIGHT(npwp_pegawai, 3) AS tiga_akhir_npwp_pegawai ,
1410
            --SUBSTRING(npwp_pegawai, 10, 3) AS enam_akhir_npwp_pegawai ,
1411
            --LEFT(npwp_pegawai, 9) AS awal_npwp_pegawai ,
1412
            --SUBSTRING(npwp_pegawai, 1, 2) + '.' + SUBSTRING(npwp_pegawai, 3, 3)
1413
            --+ '.' + SUBSTRING(npwp_pegawai, 6, 3) + '.'
1414
            --+ SUBSTRING(npwp_pegawai, 9, 1) AS awal_titik_npwp_pegawai ,
1415
            ktp AS EmployeeID ,
1416
            nama_pegawai AS Name,
1417
            alamat_pegawai AS Address,
1418
            CASE WHEN jenis_kelamin = 'W'
1419
                      OR jenis_kelamin = 'Wanita'
1420
                      OR jenis_kelamin = 'Female'
1421
                      OR jenis_kelamin = 'F' THEN 'F'
1422
                 WHEN jenis_kelamin = 'P'
1423
                      OR jenis_kelamin = 'Pria'
1424
                      OR jenis_kelamin = 'Male'
1425
                      OR jenis_kelamin = 'M' THEN 'M'
1426
            END AS Gender ,
1427
            CASE WHEN status_ptkp = 'TK' THEN 'TK'
1428
                 WHEN status_ptkp = 'K1'
1429
                      OR status_ptkp = 'K2'
1430
                      OR status_ptkp = 'K3' THEN 'K'
1431
                 WHEN status_ptkp = 'T1'
1432
                      OR status_ptkp = 'T2'
1433
                      OR status_ptkp = 'T3' THEN 'HB'
1434
            END AS PTKPStatus ,
1435
            CASE WHEN status_ptkp = 'TK' THEN '0'
1436
                 ELSE RIGHT(status_ptkp, 1)
1437
            END AS NumberOfChildren --,jumlah_tanggungan
1438
            ,
1439
            jabatan_pegawai AS JobDescription ,
1440
            CASE WHEN flag_asing = 0 THEN 'N'
1441
                 ELSE 'Y'
1442
            END AS NonResidentTaxpayer ,
1443
            '' AS CountryCode ,
1444
            kode_pajak AS TaxCode ,
1445
            a1 AS Amount1 ,
1446
            a2 AS Amount2 ,
1447
            a3 AS Amount3 ,
1448
            a4 AS Amount4 ,
1449
            a5 AS Amount5 ,
1450
            a6 AS Amount6 ,
1451
            a8 AS Amount7 ,
1452
            a9 AS Amount8 ,
1453
            a10 + a11 AS Amount9 ,
1454
            a12 AS Amount10 ,
1455
            a13 AS Amount11 ,
1456
            a14 AS Amount12 ,
1457
            a15 AS Amount13 ,
1458
            CASE WHEN a16 < 0 THEN 0
1459
                 ELSE a16
1460
            END AS Amount14 ,
1461
            a17 AS Amount15 ,
1462
            CASE WHEN a18 < 0 THEN 0
1463
                 ELSE a18
1464
            END AS Amount16 ,
1465
            a19 AS Amount17 --@a17_att AS a17 
1466
            ,
1467
            CASE WHEN a20 < 0 THEN 0
1468
                 ELSE a20
1469
            END AS Amount18 ,
1470
            a21 AS Amount19 ,
1471
            a22 AS Amount20 ,
1472
            '' AS MovementStatus ,
1473
            npwp_pemotong AS NPWPPemotong ,
1474
            --RIGHT(npwp_pemotong, 3) AS tiga_akhir_npwp_pemotong ,
1475
            --SUBSTRING(npwp_pemotong, 10, 3) AS enam_akhir_npwp_pemotong ,
1476
            --LEFT(npwp_pemotong, 9) AS awal_npwp_pemotong ,
1477
            --SUBSTRING(npwp_pemotong, 1, 2) + '.' + SUBSTRING(npwp_pemotong, 3,
1478
            --                                                 3) + '.'
1479
            --+ SUBSTRING(npwp_pemotong, 6, 3) + '.' + SUBSTRING(npwp_pemotong,
1480
            --                                                  9, 1) AS awal_titik_npwp_pemotong ,
1481
            nama_pemotong AS NamaPemotong ,
1482
            dbo.fn_formatdatetime(GETDATE(), 'dd/mm/yyyy') AS TanggalBuktiPotong 
1483
            --dbo.fn_formatdatetime(GETDATE(), 'dd') AS day_tgl_bukti_potong ,
1484
            --dbo.fn_formatdatetime(GETDATE(), 'mm') AS month_tgl_bukti_potong ,
1485
            --dbo.fn_formatdatetime(GETDATE(), 'yyyy') AS year_tgl_bukti_potong
1486
    FROM    @result
(4-4/4)