Project

General

Profile

Support #1348 » PRPTPYSPTBULANAN_20220711.sql

Tri Rizqiaty, 07/12/2022 02:07 PM

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

3
(
4

5
	@CompanyID VARCHAR(4) ,
6

7
    @PayGroup VARCHAR(5) ,
8

9
    @EmployeeArea VARCHAR(8),
10

11
	@EmployeeOffice VARCHAR(8),
12

13
	@TaxOffice VARCHAR(8),
14

15
    @PayPeriod VARCHAR(6) ,
16

17
    @MenuID VARCHAR(10) ,
18

19
    @UserID VARCHAR(10)
20

21
)
22

23
AS
24

25

26

27
--DECLARE @CompanyID AS VARCHAR(5) 
28

29
--DECLARE @PayGroup AS VARCHAR(5) 
30

31
--DECLARE @PayPeriod AS VARCHAR(6) 
32

33
--DECLARE @pembetulan AS VARCHAR(4) 
34

35
--DECLARE @EmployeeArea AS VARCHAR(8)
36

37
--DECLARE @EmployeeOffice AS VARCHAR(8)
38

39
--DECLARE @TaxOffice AS VARCHAR(8)
40

41
--DECLARE @MenuID AS VARCHAR(10) = 'PYAR01'
42

43
--DECLARE @UserID AS VARCHAR(10) = 'Ade'
44

45
--SET @CompanyID = '1000' 
46

47
--SET @PayGroup = '02'
48

49
--SET @PayPeriod = '202206'
50

51
--SET @pembetulan = ''
52

53
--SET @EmployeeArea = '0001'
54

55
--SET @EmployeeOffice = '0001'
56

57
--SET @TaxOffice = 'HO'
58

59

60

61

62

63
------------------ // Get Otorisasi //---------------
64

65
    DECLARE @TableResult TABLE
66

67
	 ( 
68

69
		EmployeeID VARCHAR(20) )
70

71
		
72

73
INSERT INTO @TableResult
74

75
EXEC dbo.GETEMPIDAUTHBYMENU 
76

77
	@MenuID = @MenuID, -- varchar(max)
78

79
    @UserID = @UserID -- varchar(max)
80

81

82

83
------------------ // End Get Otorisasi //---------------
84

85

86

87

88

89
DECLARE @t_emp TABLE
90

91
    (
92

93
      pyg VARCHAR(8) ,
94

95
      pyg_desc VARCHAR(125) ,
96

97
      emp_id VARCHAR(10) ,
98

99
      emp_name VARCHAR(225) ,
100

101
      npwp_no VARCHAR(60) ,
102

103
      kode_pajak VARCHAR(60) ,
104

105
      tax_office_m2 VARCHAR(60) ,
106

107
      tax_office_off VARCHAR(60) ,
108

109
      tax_office VARCHAR(60)
110

111
    )
112

113

114

115
DECLARE @tbl_tr_enc TABLE
116

117
    (
118

119
      employee_id VARCHAR(8) ,
120

121
      payPeriodMonth VARCHAR(2) ,
122

123
      payPeriodYear VARCHAR(4) ,
124

125
      runPeriodMonth VARCHAR(2) ,
126

127
      runPeriodYear VARCHAR(4) ,
128

129
      wage_type VARCHAR(4) ,
130

131
      amount VARCHAR(250) ,
132

133
      rate VARCHAR(250) ,
134

135
      flg_retro VARCHAR(3)
136

137
    )
138

139

140

141
DECLARE @tbl_tr_dec TABLE
142

143
    (
144

145
      employee_id VARCHAR(8) ,
146

147
      payPeriodMonth VARCHAR(2) ,
148

149
      payPeriodYear VARCHAR(4) ,
150

151
      runPeriodMonth VARCHAR(2) ,
152

153
      runPeriodYear VARCHAR(4) ,
154

155
      wage_type VARCHAR(4) ,
156

157
      amount DECIMAL(22,0) ,
158

159
      rate DECIMAL(22,0) ,
160

161
      flg_retro VARCHAR(3)
162

163
    )
164

165

166

167
DECLARE @tbl_tr_dec_sum TABLE
168

169
    (
170

171
      employee_id VARCHAR(8) ,
172

173
      payPeriodMonth VARCHAR(2) ,
174

175
      payPeriodYear VARCHAR(4) ,
176

177
      runPeriodMonth VARCHAR(2) ,
178

179
      runPeriodYear VARCHAR(4) ,
180

181
      wage_type VARCHAR(4) ,
182

183
      amount DECIMAL(22,0) ,
184

185
      rate DECIMAL(22,0) ,
186

187
      flg_retro VARCHAR(1)
188

189
    )
190

191

192

193
DECLARE @paymonth AS NVARCHAR(2)
194

195
DECLARE @payyear AS NVARCHAR(4)
196

197
DECLARE @begda AS NVARCHAR(8)
198

199
DECLARE @endda AS NVARCHAR(8)
200

201
DECLARE @enddate AS VARCHAR(8)
202

203
SET @paymonth = RIGHT(@payperiod, 2)
204

205
SET @payyear = LEFT(@payperiod, 4)
206

207
SET @begda = @payperiod + '01'
208

209
SET @endda = CONVERT(NVARCHAR(8), DATEADD(d, -1, DATEADD(m, 1, @begda)), 112)
210

211

212

213
INSERT  INTO @t_emp
214

215
        SELECT DISTINCT
216

217
				PA02.PayrollGroup,     
218

219
                PYG.PayrollGroupDescription ,
220

221
                PA02.EmployeeID ,
222

223
                PA01.FullName ,
224

225
                CASE WHEN PA03.NPWP = '' THEN '000000000'
226

227
                     WHEN PA03.NPWP = '-' THEN '000000000'
228

229
                     ELSE PA03.NPWP
230

231
                END AS NPWP ,
232

233
                EMPTYPE.TaxCode ,
234

235
                PA02.TaxOffice TaxOffice_PA2 ,
236

237
                EMPOFF.TaxOffice TaxOffice_OFF ,
238

239
                CASE WHEN PA02.TaxOffice <> '' THEN PA02.TaxOffice
240

241
                     ELSE EMPOFF.TaxOffice
242

243
                END TaxOffice
244

245
        FROM    dbo.PHRPYTR0300 AS TR300
246

247
                INNER JOIN dbo.PHRPA0002 AS PA02 ON TR300.EmployeeID = PA02.EmployeeID
248

249
                INNER JOIN @TableResult AS TableRef ON TableRef.EmployeeID = PA02.EmployeeID
250

251
                LEFT JOIN dbo.PHRPA0003 AS PA03 ON TR300.EmployeeID = PA03.EmployeeID
252

253
                                                   AND PA03.StartDate <= @endda
254

255
                                                   AND PA03.EndDate >= @endda
256

257
                LEFT JOIN dbo.PHRPA0001 AS PA01 ON TR300.EmployeeID = PA01.EmployeeID
258

259
                                                   AND PA01.StartDate <= @endda
260

261
                                                   AND PA01.EndDate >= @endda
262

263
                LEFT JOIN dbo.PHRPYPGRUP AS PYG ON PYG.PayrollGroup = PA02.PayrollGroup
264

265
                LEFT JOIN dbo.PCMEPEMPTYP AS EMPTYPE ON EMPTYPE.EmployeeType = PA02.EmployeeType
266

267
                LEFT JOIN dbo.PCMEPEMPOFF AS EMPOFF ON PA02.EmployeeOffice = EMPOFF.EmployeeOffice
268

269
        WHERE   TR300.PayPeriodMonth = TR300.RunPeriodMonth
270

271
                AND TR300.PayPeriodYear = TR300.RunPeriodYear
272

273
                AND TR300.RunPeriodMonth = @paymonth
274

275
                AND TR300.RunPeriodYear = @payyear
276

277
                AND PA02.StartDate <= @endda
278

279
                AND PA02.EndDate >= @endda
280

281
                AND ( PA02.PayrollGroup = @PayGroup
282

283
                      OR @PayGroup = ''
284

285
                    )
286

287
                AND ( PA02.EmployeeArea = @EmployeeArea
288

289
                      OR @EmployeeArea = ''
290

291
                    )
292

293
                AND ( PA02.EmployeeOffice = @EmployeeOffice
294

295
                      OR @EmployeeOffice = ''
296

297
                    )
298

299

300

301
INSERT  INTO @tbl_tr_enc
302

303
        SELECT  PR.EmployeeID ,
304

305
                PR.PayPeriodMonth ,
306

307
                PR.PayPeriodYear ,
308

309
                PR.RunPeriodMonth ,
310

311
                PR.RunPeriodYear ,
312

313
                PR.WageType ,
314

315
                PR.Amount ,
316

317
                PR.Rate ,
318

319
                'N'
320

321
        FROM    PHRPYTR0301 PR WITH ( NOLOCK )
322

323
                INNER JOIN @t_emp AS t_emp ON PR.EmployeeID = t_emp.emp_id
324

325
        WHERE   PR.PayPeriodMonth = @paymonth
326

327
                AND PR.PayPeriodYear = @payyear
328

329
                AND PR.RunPeriodMonth = @paymonth
330

331
                AND PR.RunPeriodYear = @payyear 
332

333
		--AND pr.wage_type in ('RN','RG','MT')
334

335
                AND PR.WageType IN ( 'TGI', 'MT' )
336

337

338

339
INSERT  INTO @tbl_tr_enc
340

341
        SELECT  PR.EmployeeID ,
342

343
                PR.PayPeriodMonth ,
344

345
                PR.PayPeriodYear ,
346

347
                PR.RunPeriodMonth ,
348

349
                PR.RunPeriodYear ,
350

351
                PR.WageType ,
352

353
                PR.Amount ,
354

355
                PR.Rate ,
356

357
                'R'
358

359
        FROM    PHRPYTR0301RET PR WITH ( NOLOCK )
360

361
                INNER JOIN @t_emp AS t_emp ON PR.EmployeeID = t_emp.emp_id
362

363
        WHERE   PR.PayPeriodMonth = @paymonth
364

365
                AND PR.PayPeriodYear = @payyear
366

367
                AND PR.RunPeriodMonth = @paymonth
368

369
                AND PR.RunPeriodYear = @payyear 
370

371
		--AND pr.wage_type in ('RN','RG','MT')
372

373
                AND PR.WageType IN ( 'TGI', 'MT' )
374

375

376

377
INSERT  INTO @tbl_tr_dec
378

379
        SELECT  PR.employee_id ,
380

381
                PR.payPeriodMonth ,
382

383
                PR.payPeriodYear ,
384

385
                PR.runPeriodMonth ,
386

387
                PR.runPeriodYear ,
388

389
                PR.wage_type ,
390

391
                CONVERT(DECIMAL(22, 0), CONVERT(NVARCHAR(22), dbo.GetPEN(PR.Rate))) ,
392

393
                CONVERT(DECIMAL(22, 0), dbo.GetPEN(PR.Rate)) ,
394

395
                PR.flg_retro
396

397
        FROM    @tbl_tr_enc AS PR		
398

399

400

401
INSERT  INTO @tbl_tr_dec_sum
402

403
        SELECT  employee_id ,
404

405
                payPeriodMonth ,
406

407
                payPeriodYear ,
408

409
                runPeriodMonth ,
410

411
                runPeriodYear ,
412

413
                wage_type ,
414

415
                SUM(amount) AS amount ,
416

417
                SUM(rate) AS rate ,
418

419
                MIN(flg_retro) AS flg_retro
420

421
        FROM    @tbl_tr_dec AS tbl_tr_dec
422

423
        GROUP BY employee_id ,
424

425
                payPeriodMonth ,
426

427
                payPeriodYear ,
428

429
                runPeriodMonth ,
430

431
                runPeriodYear ,
432

433
                wage_type
434

435

436

437
SELECT DISTINCT
438

439
        @paymonth AS bulan ,
440

441
        @payyear AS tahun ,
442

443
        pyg ,
444

445
        pyg_desc ,
446

447
        emp_id ,
448

449
        emp_name ,
450

451
        npwp_no ,
452

453
        TGI ,
454

455
        MT ,
456

457
        1 AS emp ,
458

459
        kode_pajak ,
460

461
        tax_office_m2 ,
462

463
        tax_office_off ,
464

465
        tax_office
466

467
FROM    ( SELECT DISTINCT
468

469
                    t_emp.* ,
470

471
                    t_dec.wage_type ,
472

473
                    t_dec.amount
474

475
          FROM      @t_emp AS t_emp
476

477
                    INNER JOIN @tbl_tr_dec_sum AS t_dec ON t_emp.emp_id = t_dec.employee_id
478

479
        ) AS SourceData PIVOT
480

481
( SUM(amount) FOR wage_type IN ( [TGI], [MT] ) ) AS pvt
482

483
WHERE   ( tax_office = @TaxOffice
484

485
          OR @TaxOffice = ''
486

487
        )
488

489

(5-5/6)