Project

General

Profile

Bug #3660 » CRPTLPPOMESSPAYSLIP_20250616.sql

Tri Rizqiaty, 06/16/2025 02:33 PM

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

3
    (
4

5
      @PayPeriod VARCHAR(6) ,
6

7
      @EmpID VARCHAR(20) ,
8

9
      @Running VARCHAR(2) ,
10

11
      @PayType VARCHAR(2) 
12

13
    )
14

15
AS
16

17

18

19
    --DECLARE @PayPeriod VARCHAR(6) = '202501'
20

21
    --DECLARE @EmpID VARCHAR(8) = '00000002'
22

23
    --DECLARE @Running VARCHAR(2) = '1'
24

25
    --DECLARE @PayType VARCHAR(2) = ''
26

27
    
28

29

30

31
    DECLARE @CompanyCode AS VARCHAR(50) 
32

33
    DECLARE @CompanyName AS VARCHAR(250) 
34

35
    DECLARE @CompanyLogo AS VARBINARY(MAX)
36

37
    DECLARE @PayMonth AS NVARCHAR(2)
38

39
    DECLARE @PayYear AS NVARCHAR(4)
40

41
    DECLARE @Begda AS NVARCHAR(8)
42

43
    DECLARE @Endda AS NVARCHAR(8)
44

45

46

47
	DECLARE @PayGroup AS VARCHAR(50) 
48

49
	DECLARE @BegdaWS AS NVARCHAR(8)
50

51
    DECLARE @EnddaWS AS NVARCHAR(8)
52

53

54

55
    SET @PayMonth = RIGHT(@PayPeriod, 2)
56

57
    SET @PayYear = LEFT(@PayPeriod, 4)
58

59
    SET @Begda = @PayPeriod + '01'
60

61
    SET @Endda = CONVERT(NVARCHAR(8), DATEADD(d, -1, DATEADD(m, 1, @Begda)), 112)
62

63
    
64

65
   
66

67

68

69
    DECLARE @PaySlipSigned AS NVARCHAR(50)
70

71

72

73
    SELECT TOP ( 1 )
74

75
            @PaySlipSigned = Value
76

77
    FROM    dbo.PHRPYCU0307
78

79
    WHERE   ( PayrollParameter = 'PAYSLIPSIGNED' )
80

81
            AND ( StartDate <= @Endda
82

83
                  AND EndDate >= @Endda
84

85
                )
86

87

88

89
    IF ( @Running = '1' )
90

91
        BEGIN
92

93
            SELECT TOP ( 1 )
94

95
                    @CompanyCode = CompanyID
96

97
					,@PayGroup = PayrollGroup
98

99
            FROM    dbo.PHRPYTR0300 
100

101
             WHERE   ( EmployeeID = @EmpID )
102

103
                    AND ( PayPeriodMonth = @PayMonth )
104

105
                    AND ( PayPeriodYear = @PayYear )
106

107
                    AND ( RunPeriodMonth = @PayMonth )
108

109
                    AND ( RunPeriodYear = @PayYear )
110

111
        END
112

113
    ELSE
114

115
        BEGIN
116

117
            SELECT TOP ( 1 )
118

119
                    @CompanyCode = CompanyID
120

121
					,@PayGroup = PayrollGroup
122

123
            FROM    dbo.PHRPYTR0300SIM
124

125
            WHERE   ( EmployeeID = @EmpID )
126

127
                    AND ( PayPeriodMonth = @PayMonth )
128

129
                    AND ( PayPeriodMonth = @PayYear )
130

131
                    AND ( RunPeriodMonth = @PayMonth )
132

133
                    AND ( RunPeriodYear = @PayYear )
134

135
        END
136

137

138

139
       
140

141

142

143
    SELECT  @CompanyName = COM.CompanyDescription ,
144

145
            @CompanyLogo = CAT.FileData
146

147
    FROM    dbo.PCMEPCOMPID AS COM
148

149
	LEFT JOIN dbo.SFILECATALOG CAT ON COM.Logo = CAT.FileName
150

151
    WHERE   ( CompanyID = @CompanyCode )
152

153
            AND StartDate <= @Endda
154

155
            AND EndDate >= @Endda
156

157

158

159
	DECLARE @Disclaimer VARCHAR(250)
160

161
	SELECT @Disclaimer = Value1 FROM dbo.PCMEPGENPARAM WHERE Parameter = 'DISCLAIMEREPAYSLIP'
162

163

164

165

166

167
	IF @CompanyCode = '3000'
168

169
	BEGIN 
170

171
	SELECT @Disclaimer = Value1 FROM dbo.PCMEPGENPARAM WHERE Parameter = 'DISCLAIMEREPAYSLIPIHATEC'
172

173
	END
174

175

176

177
	----//---- get hari kerja start
178

179
	DECLARE @PeriodPayGroup VARCHAR(20)
180

181
	SELECT @PeriodPayGroup = PayrollPeriod FROM dbo.PHRPYPGRUP WHERE PayrollGroup = @PayGroup
182

183

184

185
	SELECT @BegdaWS = StartDate, @EnddaWS = EndDate FROM dbo.PHRPYTRPAYPERIOD WHERE PayrollPeriod = @PeriodPayGroup AND StartDate <= @Begda AND EndDate >= @Begda
186

187
	
188

189
	DECLARE @TotHK DECIMAL(18,0)
190

191
	SELECT @TotHK = COUNT(pa125.StartDate)
192

193
	FROM PHRPA0025 AS pa25
194

195
	LEFT JOIN PHRPA0125 AS pa125
196

197
		ON pa25.EmployeeID = pa125.EmployeeID
198

199
	WHERE pa25.EmployeeID = @EmpID
200

201
		AND pa25.StartDate <= @EnddaWS AND pa25.EndDate >= @EnddaWS
202

203
		AND pa125.StartDate >= @BegdaWS AND pa125.StartDate <= @EnddaWS
204

205
		AND pa125.DayType IN (SELECT DayType FROM dbo.PHRTMDAYTYP WHERE Flag = 'WORK')
206

207
	----//---- get hari kerja end
208

209

210

211
	----//---- get attendance start
212

213
	DECLARE @TotAtt DECIMAL(18,2)
214

215
	SELECT @TotAtt = COUNT(StartDate)
216

217
	FROM dbo.PHRPA0019
218

219
	WHERE EmployeeID = @EmpID
220

221
		AND StartDate >= @BegdaWS AND StartDate <= @EnddaWS
222

223
		AND AttendanceType = '1000'
224

225
	----//---- get hari kerja end
226

227

228

229
    IF ( @Running = '1' )
230

231
        BEGIN   
232

233
		
234

235
            SELECT  @PaySlipSigned PaySlipSigned ,
236

237
                    @CompanyName CompanyName ,
238

239
                    @CompanyLogo logo ,
240

241
                    c308.ReportID ,
242

243
                    c308.GroupBy ,
244

245
                    c308P.PayslipDescription ParentDescription ,
246

247
                    c308.Sequence ,
248

249
                    c308.PayslipDescription ,
250

251
                    c308.PayrollType ,
252

253
                    c308.WageType ,
254

255
                    c308.IsDisplayWageType ,
256

257
                    c308.IsDisplayNumber ,
258

259
                    c308.IsDisplayRate ,
260

261
                    c308.IsDisplayAmount ,
262

263
                    c308.StartDate ,
264

265
                    c308.EndDate ,
266

267
                    c308.IsActive ,
268

269
                    c308.ChangeBy ,
270

271
                    c308.ChangeDate ,
272

273
                    c308.CreateBy ,
274

275
                    c308.CreateDate ,
276

277
                    tr301.EmployeeID ,
278

279
                    tr301.PayPeriodMonth ,
280

281
                    tr301.PayPeriodYear ,
282

283
                    tr301.RunPeriodMonth ,
284

285
                    tr301.RunPeriodYear ,
286

287
                    tr301.SplitIndicator ,
288

289
					CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Rate), '0'), ',', '.') AS DECIMAL) 'rate' ,
290

291
                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'), ',','.') AS DECIMAL) 'amount' ,
292

293
                    CASE WHEN c308.WageType = '1001' THEN CONVERT(DECIMAL(22,2),@TotAtt) ELSE CONVERT(DECIMAL(22,2),tr301.Number) END AS Number,
294

295
                    tr301.Currency ,
296

297
                    tr301.Flag ,
298

299
                    md001.FullName ,
300

301
					md001.ExternalID ,
302

303
                    md003.TaxStatus ,
304

305
                    taxstat.TaxStatusDescription TaxStatusDesc ,
306

307
                    md001.MaritalStatus ,
308

309
                    ms.MaritalStatusDescription MaritalDesc ,
310

311
                    md003.PayrollType AS PayIndustry ,
312

313
                    md003.PayrollArea ,
314

315
                    md003.PayrollClass ,
316

317
                    md003.PayrollGrade ,
318

319
                    md003.NPWP ,
320

321
                    grd.PayrollGradeDescription AS PayGradeDesc ,
322

323
                    md002.Job ,
324

325
                    md002.EmployeeArea ,
326

327
                    area.EmployeeAreaDescription AreaDesc ,
328

329
                    md002.Organization ,
330

331
                    md002.OrgEasy AS OrgDesc, --org.ObjectDescription OrgDesc , ----// comment for ekno
332

333
                    md002.Position ,
334

335
                    md002.PositionEasy AS PosDesc --pos.ObjectDescription PosDesc ----// comment for ekno
336

337
					,@Disclaimer AS Disclaimer
338

339
					,pa22.BenefitNo AS NoKPJ
340

341
					,@TotHK AS TotHK
342

343
            FROM    dbo.PHRPYCU0308 AS c308
344

345
                    LEFT JOIN dbo.PHRPYCU0308 AS c308P ON c308.GroupBy = c308P.ReportID
346

347
					INNER JOIN ( SELECT * ,
348

349
                                        'N' RetroType
350

351
                                 FROM   PHRPYTR0301
352

353
                                 UNION ALL
354

355
                                 SELECT * ,
356

357
                                        'R' RetroType
358

359
                                 FROM   PHRPYTR0301RET
360

361
                               ) tr301 ON c308.WageType = tr301.WageType
362

363
                                          AND c308.RetroType = tr301.RetroType
364

365
                                          AND ( c308.PayrollType = @PayType OR @PayType = '' )
366

367
                    INNER JOIN ( SELECT m1.*
368

369
                                 FROM   dbo.PHRPA0001 m1
370

371
                                 WHERE  ( m1.StartDate <= @Endda AND m1.EndDate >= @Endda)
372

373
                               ) md001 ON md001.EmployeeID = tr301.EmployeeID
374

375
                    LEFT JOIN ( SELECT  m1.*
376

377
                                FROM    dbo.PHRPA0003 m1
378

379
                                WHERE   ( m1.StartDate <= @Endda AND m1.EndDate >= @Endda)
380

381
                              ) md003 ON md001.EmployeeID = md003.EmployeeID
382

383
                    LEFT JOIN ( SELECT  m1.*
384

385
                                FROM    dbo.PHRPA0002 m1
386

387
                                WHERE   ( m1.StartDate <= @Endda AND m1.EndDate >= @Endda)
388

389
                              ) md002 ON tr301.EmployeeID = md002.EmployeeID
390

391
                    LEFT JOIN dbo.PCMEPEMPAREA area ON md002.EmployeeArea = area.EmployeeArea
392

393
                                                       AND area.StartDate <= @Endda AND area.EndDate >= @Endda
394

395
                    LEFT JOIN ( SELECT  m1.*
396

397
                                FROM    dbo.PHROM0001 m1
398

399
                                WHERE   ObjectClass = 'O'
400

401
                                AND (StartDate <= @Endda AND EndDate >= @Endda)
402

403
                              ) org ON md002.Organization = org.ObjectID
404

405
                    LEFT JOIN ( SELECT  m1.*
406

407
                                FROM    PHROM0001 m1
408

409
                                WHERE   ObjectClass = 'P'
410

411
                                AND (StartDate <= @Endda AND EndDate >= @Endda)
412

413
                              ) pos ON md002.Position = pos.ObjectID
414

415
                    LEFT JOIN dbo.PHRPYPGRAD grd ON md003.PayrollGrade = grd.PayrollGrade
416

417
													AND md003.PayrollClass = grd.PayrollClass
418

419
                                                    AND grd.StartDate <= @Endda
420

421
                                                    AND grd.EndDate >= @Endda
422

423
                    LEFT JOIN dbo.PHRPAMSTAT ms ON md001.MaritalStatus = ms.MaritalStatus
424

425
                                                   AND ms.StartDate <= @Endda
426

427
                                                   AND ms.EndDate >= @Endda
428

429
                    LEFT JOIN dbo.PHRPYTAXSTAT taxstat ON md003.TaxStatus = taxstat.TaxStatus
430

431
                                                          AND taxstat.StartDate <= @Endda
432

433
                                                          AND taxstat.EndDate >= @Endda
434

435
					LEFT JOIN dbo.PHRPA0022 pa22 ON md002.EmployeeID = pa22.EmployeeID
436

437
													AND pa22.StartDate <= @Endda AND pa22.EndDate >= @Endda
438

439
            WHERE   tr301.EmployeeID = @EmpID
440

441
                    AND tr301.PayPeriodMonth = @PayMonth
442

443
                    AND tr301.PayPeriodYear = @PayYear
444

445
                    AND tr301.RunPeriodMonth = @PayMonth
446

447
                    AND tr301.RunPeriodYear = @PayYear
448

449
                    AND CAST(REPLACE(ISNULL(dbo.GetPEN(tr301.Amount), '0'),
450

451
                                     ',', '.') AS DECIMAL) <> 0
452

453
            ORDER BY c308.GroupBy ,
454

455
                    c308.WageType
456

457
        END
458

459
    ELSE
460

461
        BEGIN   
462

463
		
464

465
            SELECT  @PaySlipSigned PaySlipSigned ,
466

467
                    @CompanyName CompanyName ,
468

469
                    @CompanyLogo logo ,
470

471
                    c308.ReportID ,
472

473
                    c308.GroupBy ,
474

475
                    c308P.PayslipDescription ParentDescription ,
476

477
                    c308.Sequence ,
478

479
                    c308.PayslipDescription ,
480

481
                    c308.PayrollType ,
482

483
                    c308.WageType ,
484

485
                    c308.IsDisplayWageType ,
486

487
                    c308.IsDisplayNumber ,
488

489
                    c308.IsDisplayRate ,
490

491
                    c308.IsDisplayAmount ,
492

493
                    c308.StartDate ,
494

495
                    c308.EndDate ,
496

497
                    c308.IsActive ,
498

499
                    c308.ChangeBy ,
500

501
                    c308.ChangeDate ,
502

503
                    c308.CreateBy ,
504

505
                    c308.CreateDate ,
506

507
                    tr301Sim.EmployeeID ,
508

509
                    tr301Sim.PayPeriodMonth ,
510

511
                    tr301Sim.PayPeriodYear ,
512

513
                    tr301Sim.RunPeriodMonth ,
514

515
                    tr301Sim.RunPeriodYear ,
516

517
                    tr301Sim.SplitIndicator ,
518

519
                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301Sim.Rate), '0'), ',',
520

521
                                 '.') AS DECIMAL) 'rate' ,
522

523
                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301Sim.Amount), '0'), ',',
524

525
                                 '.') AS DECIMAL) 'amount' ,
526

527
                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301RSim.Rate), '0'), ',',
528

529
                                 '.') AS DECIMAL) 'rate_r' ,
530

531
                    CAST(REPLACE(ISNULL(dbo.GetPEN(tr301RSim.Amount), '0'),
532

533
                                 ',', '.') AS DECIMAL) 'amount_r' ,
534

535
                    ( CAST(REPLACE(ISNULL(dbo.GetPEN(tr301Sim.Rate), '0'), ',',
536

537
                                   '.') AS DECIMAL)
538

539
                      + CAST(REPLACE(ISNULL(dbo.GetPEN(tr301RSim.Rate), '0'),
540

541
                                     ',', '.') AS DECIMAL) ) 'total_rate' ,
542

543
                    ( CAST(REPLACE(ISNULL(dbo.GetPEN(tr301Sim.Amount), '0'),
544

545
                                   ',', '.') AS DECIMAL)
546

547
                      + CAST(REPLACE(ISNULL(dbo.GetPEN(tr301RSim.Amount), '0'),
548

549
                                     ',', '.') AS DECIMAL) ) 'total_amount' ,
550

551
                    tr301Sim.Number ,
552

553
                    tr301Sim.Currency ,
554

555
                    tr301Sim.Flag ,
556

557
                    md001.FullName ,
558

559
					md001.ExternalID ,
560

561
                    md003.TaxStatus ,
562

563
                    taxstat.TaxStatusDescription TaxStatusDesc ,
564

565
                    md001.MaritalStatus ,
566

567
                    ms.MaritalStatusDescription MaritalDesc ,
568

569
                    md003.PayrollType AS PayIndustry ,
570

571
                    md003.PayrollArea ,
572

573
                    md003.PayrollClass ,
574

575
                    md003.PayrollGrade ,
576

577
                    md003.NPWP ,
578

579
                    grd.PayrollGradeDescription AS PayGradeDesc ,
580

581
                    md002.Job ,
582

583
                    md002.EmployeeArea ,
584

585
                    area.EmployeeAreaDescription AreaDesc ,
586

587
                    md002.Organization ,
588

589
                    org.ObjectDescription OrgDesc ,
590

591
                    md002.Position ,
592

593
                    pos.ObjectDescription PosDesc
594

595
					,@Disclaimer AS Disclaimer
596

597
            FROM    dbo.PHRPYCU0308 AS c308
598

599
                    LEFT JOIN dbo.PHRPYCU0308 AS c308P ON c308.GroupBy = c308P.ReportID
600

601
                    INNER JOIN ( SELECT * ,
602

603
                                        'N' RetroType
604

605
                                 FROM   dbo.PHRPYTR0301SIM
606

607
                               ) tr301Sim ON c308.WageType = tr301Sim.WageType
608

609
                                             AND c308.RetroType = tr301Sim.RetroType
610

611
                                             AND ( c308.PayrollType = @PayType
612

613
                                                   OR @PayType = ''
614

615
                                                 )
616

617
                    LEFT OUTER JOIN ( SELECT    * ,
618

619
                                                'R' retrotype
620

621
                                      FROM      dbo.PHRPYTR0301RETSIM
622

623
                                    ) tr301RSim ON c308.WageType = tr301RSim.WageType
624

625
                                                   AND ( c308.PayrollType = @PayType
626

627
                                                         OR @PayType = ''
628

629
                                                       )
630

631
                                                   AND tr301RSim.EmployeeID = @EmpID
632

633
                                                   --AND tr301RSim.PayPeriodMonth = @PayMonth
634

635
                                                   --AND tr301RSim.PayPeriodYear = @PayYear
636

637
                                                   AND tr301RSim.RunPeriodMonth = @PayMonth
638

639
                                                   AND tr301RSim.RunPeriodYear = @PayYear
640

641
                    INNER JOIN ( SELECT m1.*
642

643
                                 FROM   dbo.PHRPA0001 m1
644

645
                                 WHERE  ( m1.StartDate <= @Endda
646

647
                                          AND m1.EndDate >= @Endda
648

649
                                        )
650

651
                               ) md001 ON md001.EmployeeID = tr301Sim.EmployeeID
652

653
                    LEFT JOIN ( SELECT  m1.*
654

655
                                FROM    dbo.PHRPA0003 m1
656

657
                                WHERE   ( m1.StartDate <= @Endda
658

659
                                          AND m1.EndDate >= @Endda
660

661
                                        )
662

663
                              ) md003 ON md001.EmployeeID = md003.EmployeeID
664

665
                    LEFT JOIN ( SELECT  m1.*
666

667
                                FROM    dbo.PHRPA0002 m1
668

669
                                WHERE   ( m1.StartDate <= @Endda
670

671
                                          AND m1.EndDate >= @Endda
672

673
                                        )
674

675
                              ) md002 ON tr301Sim.EmployeeID = md002.EmployeeID
676

677
                    LEFT JOIN dbo.PCMEPEMPAREA area ON md002.EmployeeArea = area.EmployeeArea
678

679
                                                       AND area.StartDate <= @Endda
680

681
                                                       AND area.EndDate >= @Endda
682

683
                    LEFT JOIN ( SELECT  m1.*
684

685
                                FROM    dbo.PHROM0001 m1
686

687
                                WHERE   ObjectClass = 'O'
688

689
                                AND (StartDate <= @Endda AND EndDate >= @Endda)
690

691
				--AND (StartDate <= @Endda AND StartDate >= @Endda)
692

693
                              ) org ON md002.Organization = org.ObjectID
694

695
                    LEFT JOIN ( SELECT  m1.*
696

697
                                FROM    PHROM0001 m1
698

699
                                WHERE   ObjectClass = 'P'
700

701
                                AND (StartDate <= @Endda AND EndDate >= @Endda)
702

703
				--AND (StartDate <= @Endda AND StartDate >= @Endda)
704

705
                              ) pos ON md002.Position = pos.ObjectID
706

707
                    LEFT JOIN dbo.PHRPYPGRAD grd ON md003.PayrollGrade = grd.PayrollGrade
708

709
													AND md003.PayrollClass = grd.PayrollClass
710

711
                                                    AND grd.StartDate <= @Endda
712

713
                                                    AND grd.EndDate >= @Endda
714

715
                    LEFT JOIN dbo.PHRPAMSTAT ms ON md001.MaritalStatus = ms.MaritalStatus
716

717
                                                   AND ms.StartDate <= @Endda
718

719
                                                   AND ms.EndDate >= @Endda
720

721
                    LEFT JOIN dbo.PHRPYTAXSTAT taxstat ON md003.TaxStatus = taxstat.TaxStatus
722

723
                                                          AND taxstat.StartDate <= @Endda
724

725
                                                          AND taxstat.EndDate >= @Endda
726

727
            WHERE   tr301Sim.EmployeeID = @EmpID
728

729
                    AND tr301Sim.PayPeriodMonth = @PayMonth
730

731
                    AND tr301Sim.PayPeriodYear = @PayYear
732

733
                    AND tr301Sim.RunPeriodMonth = @PayMonth
734

735
                    AND tr301Sim.RunPeriodYear = @PayYear
736

737
                    AND CAST(REPLACE(ISNULL(dbo.GetPEN(tr301Sim.Amount), '0'),
738

739
                                     ',', '.') AS DECIMAL) <> 0
740

741
            ORDER BY c308.GroupBy ,
742

743
                    c308.WageType
744

745
        END
(5-5/5)