Project

General

Profile

Bug #2206 » PRPTPYSPTBULANAN_20230606.sql

Tri Rizqiaty, 06/06/2023 03:22 PM

 
1
??USE [MinovaES_Pertalife_Prod]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[PRPTPYSPTBULANAN]    Script Date: 06/06/2023 11.05.28 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[PRPTPYSPTBULANAN]
16

17
(
18

19
	@CompanyID VARCHAR(4) ,
20

21
    @PayGroup VARCHAR(5) ,
22

23
    @EmployeeArea VARCHAR(8),
24

25
	@EmployeeOffice VARCHAR(8),
26

27
	@TaxOffice VARCHAR(8),
28

29
    @PayPeriod VARCHAR(6) ,
30

31
    @MenuID VARCHAR(10) ,
32

33
    @UserID VARCHAR(10)
34

35
)
36

37
AS
38

39

40

41
--DECLARE @CompanyID AS VARCHAR(5) 
42

43
--DECLARE @PayGroup AS VARCHAR(5) 
44

45
--DECLARE @PayPeriod AS VARCHAR(6) 
46

47
--DECLARE @pembetulan AS VARCHAR(4) 
48

49
--DECLARE @EmployeeArea AS VARCHAR(8)
50

51
--DECLARE @EmployeeOffice AS VARCHAR(8)
52

53
--DECLARE @TaxOffice AS VARCHAR(8)
54

55
--DECLARE @MenuID AS VARCHAR(10) = 'PA01'
56

57
--DECLARE @UserID AS VARCHAR(10) = 'minovais'
58

59
--SET @CompanyID = '1000' 
60

61
--SET @PayGroup = '0006'
62

63
--SET @PayPeriod = '202211'
64

65
--SET @pembetulan = ''
66

67
--SET @EmployeeArea = ''
68

69
--SET @EmployeeOffice = ''
70

71
--SET @TaxOffice = ''
72

73

74

75

76

77
------------------ // Get Otorisasi //---------------
78

79
    DECLARE @TableResult TABLE
80

81
	 ( 
82

83
		EmployeeID VARCHAR(20) )
84

85
		
86

87
INSERT INTO @TableResult
88

89
EXEC dbo.GETEMPIDAUTHBYMENU 
90

91
	@MenuID = @MenuID, -- varchar(max)
92

93
    @UserID = @UserID -- varchar(max)
94

95

96

97
------------------ // End Get Otorisasi //---------------
98

99

100

101

102

103
DECLARE @t_emp TABLE
104

105
    (
106

107
      pyg VARCHAR(8) ,
108

109
      pyg_desc VARCHAR(125) ,
110

111
      emp_id VARCHAR(10) ,
112

113
      emp_name VARCHAR(225) ,
114

115
      npwp_no VARCHAR(60) ,
116

117
      kode_pajak VARCHAR(60) ,
118

119
      tax_office_m2 VARCHAR(60) ,
120

121
      tax_office_off VARCHAR(60) ,
122

123
      tax_office VARCHAR(60)
124

125
    )
126

127

128

129
DECLARE @tbl_tr_enc TABLE
130

131
    (
132

133
      employee_id VARCHAR(8) ,
134

135
      payPeriodMonth VARCHAR(2) ,
136

137
      payPeriodYear VARCHAR(4) ,
138

139
      runPeriodMonth VARCHAR(2) ,
140

141
      runPeriodYear VARCHAR(4) ,
142

143
      wage_type VARCHAR(4) ,
144

145
      amount VARCHAR(250) ,
146

147
      rate VARCHAR(250) ,
148

149
      flg_retro VARCHAR(3)
150

151
    )
152

153

154

155
DECLARE @tbl_tr_dec TABLE
156

157
    (
158

159
      employee_id VARCHAR(8) ,
160

161
      payPeriodMonth VARCHAR(2) ,
162

163
      payPeriodYear VARCHAR(4) ,
164

165
      runPeriodMonth VARCHAR(2) ,
166

167
      runPeriodYear VARCHAR(4) ,
168

169
      wage_type VARCHAR(4) ,
170

171
      amount DECIMAL(22,0) ,
172

173
      rate DECIMAL(22,0) ,
174

175
      flg_retro VARCHAR(3)
176

177
    )
178

179

180

181
DECLARE @tbl_tr_dec_sum TABLE
182

183
    (
184

185
      employee_id VARCHAR(8) ,
186

187
      payPeriodMonth VARCHAR(2) ,
188

189
      payPeriodYear VARCHAR(4) ,
190

191
      runPeriodMonth VARCHAR(2) ,
192

193
      runPeriodYear VARCHAR(4) ,
194

195
      wage_type VARCHAR(4) ,
196

197
      amount DECIMAL(22,0) ,
198

199
      rate DECIMAL(22,0) ,
200

201
      flg_retro VARCHAR(1)
202

203
    )
204

205

206

207
DECLARE @paymonth AS NVARCHAR(2)
208

209
DECLARE @payyear AS NVARCHAR(4)
210

211
DECLARE @begda AS NVARCHAR(8)
212

213
DECLARE @endda AS NVARCHAR(8)
214

215
DECLARE @enddate AS VARCHAR(8)
216

217
SET @paymonth = RIGHT(@payperiod, 2)
218

219
SET @payyear = LEFT(@payperiod, 4)
220

221
SET @begda = @payperiod + '01'
222

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

225

226

227
INSERT  INTO @t_emp
228

229
        SELECT DISTINCT
230

231
				PA02.PayrollGroup,     
232

233
                PYG.PayrollGroupDescription ,
234

235
                PA02.EmployeeID ,
236

237
                PA01.FullName ,
238

239
                CASE WHEN PA03.NPWP = '' THEN '000000000'
240

241
                     WHEN PA03.NPWP = '-' THEN '000000000'
242

243
                     ELSE PA03.NPWP
244

245
                END AS NPWP ,
246

247
                EMPTYPE.TaxCode ,
248

249
                PA02.TaxOffice TaxOffice_PA2 ,
250

251
                EMPOFF.TaxOffice TaxOffice_OFF ,
252

253
                CASE WHEN PA02.TaxOffice <> '' THEN PA02.TaxOffice
254

255
                     ELSE EMPOFF.TaxOffice
256

257
                END TaxOffice
258

259
        FROM    dbo.PHRPYTR0300 AS TR300
260

261
                INNER JOIN dbo.PHRPA0002 AS PA02 ON TR300.EmployeeID = PA02.EmployeeID
262

263
                INNER JOIN @TableResult AS TableRef ON TableRef.EmployeeID = PA02.EmployeeID
264

265
                LEFT JOIN dbo.PHRPA0003 AS PA03 ON TR300.EmployeeID = PA03.EmployeeID
266

267
                                                   AND PA03.StartDate <= @endda
268

269
                                                   AND PA03.EndDate >= @endda
270

271
                LEFT JOIN dbo.PHRPA0001 AS PA01 ON TR300.EmployeeID = PA01.EmployeeID
272

273
                                                   AND PA01.StartDate <= @endda
274

275
                                                   AND PA01.EndDate >= @endda
276

277
                LEFT JOIN dbo.PHRPYPGRUP AS PYG ON PYG.PayrollGroup = PA02.PayrollGroup
278

279
                LEFT JOIN dbo.PCMEPEMPTYP AS EMPTYPE ON EMPTYPE.EmployeeType = PA02.EmployeeType
280

281
                LEFT JOIN dbo.PCMEPEMPOFF AS EMPOFF ON PA02.EmployeeOffice = EMPOFF.EmployeeOffice
282

283
        WHERE   TR300.PayPeriodMonth = TR300.RunPeriodMonth
284

285
                AND TR300.PayPeriodYear = TR300.RunPeriodYear
286

287
                AND TR300.RunPeriodMonth = @paymonth
288

289
                AND TR300.RunPeriodYear = @payyear
290

291
                AND PA02.StartDate <= @endda
292

293
                AND PA02.EndDate >= @endda
294

295
                AND ( PA02.PayrollGroup = @PayGroup
296

297
                      OR @PayGroup = ''
298

299
                    )
300

301
                AND ( PA02.EmployeeArea = @EmployeeArea
302

303
                      OR @EmployeeArea = ''
304

305
                    )
306

307
                AND ( PA02.EmployeeOffice = @EmployeeOffice
308

309
                      OR @EmployeeOffice = ''
310

311
                    )
312

313

314

315
INSERT  INTO @tbl_tr_enc
316

317
        SELECT  PR.EmployeeID ,
318

319
                PR.PayPeriodMonth ,
320

321
                PR.PayPeriodYear ,
322

323
                PR.RunPeriodMonth ,
324

325
                PR.RunPeriodYear ,
326

327
                PR.WageType ,
328

329
                PR.Amount ,
330

331
                PR.Rate ,
332

333
                'N'
334

335
        FROM    PHRPYTR0301 PR WITH ( NOLOCK )
336

337
                INNER JOIN @t_emp AS t_emp ON PR.EmployeeID = t_emp.emp_id
338

339
        WHERE   PR.PayPeriodMonth = @paymonth
340

341
                AND PR.PayPeriodYear = @payyear
342

343
                AND PR.RunPeriodMonth = @paymonth
344

345
                AND PR.RunPeriodYear = @payyear 
346

347
		--AND pr.wage_type in ('RN','RG','MT')
348

349
                AND PR.WageType IN ( 'TGI', 'MT' )
350

351

352

353
INSERT  INTO @tbl_tr_enc
354

355
        SELECT  PR.EmployeeID ,
356

357
                PR.PayPeriodMonth ,
358

359
                PR.PayPeriodYear ,
360

361
                PR.RunPeriodMonth ,
362

363
                PR.RunPeriodYear ,
364

365
                PR.WageType ,
366

367
                PR.Amount ,
368

369
                PR.Rate ,
370

371
                'R'
372

373
        FROM    PHRPYTR0301RET PR WITH ( NOLOCK )
374

375
                INNER JOIN @t_emp AS t_emp ON PR.EmployeeID = t_emp.emp_id
376

377
        WHERE   PR.PayPeriodMonth = @paymonth
378

379
                AND PR.PayPeriodYear = @payyear
380

381
                AND PR.RunPeriodMonth = @paymonth
382

383
                AND PR.RunPeriodYear = @payyear 
384

385
		--AND pr.wage_type in ('RN','RG','MT')
386

387
                AND PR.WageType IN ( 'TGI', 'MT' )
388

389

390

391
INSERT  INTO @tbl_tr_dec
392

393
        SELECT  PR.employee_id ,
394

395
                PR.payPeriodMonth ,
396

397
                PR.payPeriodYear ,
398

399
                PR.runPeriodMonth ,
400

401
                PR.runPeriodYear ,
402

403
                PR.wage_type ,
404

405
                CONVERT(DECIMAL(22, 0), CONVERT(NVARCHAR(22), dbo.GetPEN(PR.Rate))) ,
406

407
                CONVERT(DECIMAL(22, 0), dbo.GetPEN(PR.Rate)) ,
408

409
                PR.flg_retro
410

411
        FROM    @tbl_tr_enc AS PR		
412

413

414

415
INSERT  INTO @tbl_tr_dec_sum
416

417
        SELECT  employee_id ,
418

419
                payPeriodMonth ,
420

421
                payPeriodYear ,
422

423
                runPeriodMonth ,
424

425
                runPeriodYear ,
426

427
                wage_type ,
428

429
                SUM(amount) AS amount ,
430

431
                SUM(rate) AS rate ,
432

433
                MIN(flg_retro) AS flg_retro
434

435
        FROM    @tbl_tr_dec AS tbl_tr_dec
436

437
        GROUP BY employee_id ,
438

439
                payPeriodMonth ,
440

441
                payPeriodYear ,
442

443
                runPeriodMonth ,
444

445
                runPeriodYear ,
446

447
                wage_type
448

449

450

451
SELECT DISTINCT
452

453
        @paymonth AS bulan ,
454

455
        @payyear AS tahun ,
456

457
        pyg ,
458

459
        pyg_desc ,
460

461
        emp_id ,
462

463
        emp_name ,
464

465
        npwp_no ,
466

467
        TGI ,
468

469
        MT ,
470

471
        1 AS emp ,
472

473
        kode_pajak ,
474

475
        tax_office_m2 ,
476

477
        tax_office_off ,
478

479
        tax_office
480

481
FROM    ( SELECT DISTINCT
482

483
                    t_emp.* ,
484

485
                    t_dec.wage_type ,
486

487
                    t_dec.amount
488

489
          FROM      @t_emp AS t_emp
490

491
                    INNER JOIN @tbl_tr_dec_sum AS t_dec ON t_emp.emp_id = t_dec.employee_id
492

493
        ) AS SourceData PIVOT
494

495
( SUM(amount) FOR wage_type IN ( [TGI], [MT] ) ) AS pvt
496

497
WHERE   ( tax_office = @TaxOffice
498

499
          OR @TaxOffice = ''
500

501
        )
502

503

(2-2/3)