| 1 | USE [MinovaES_HINO_Dev]
 | 
  
    | 2 | GO
 | 
  
    | 3 | /****** Object:  StoredProcedure [dbo].[GenerateMedPlafMass]    Script Date: 27/12/2023 11.28.38 ******/
 | 
  
    | 4 | SET ANSI_NULLS ON
 | 
  
    | 5 | GO
 | 
  
    | 6 | SET QUOTED_IDENTIFIER ON
 | 
  
    | 7 | GO
 | 
  
    | 8 | 
 | 
  
    | 9 | ALTER PROCEDURE [dbo].[GenerateMedPlafMass]
 | 
  
    | 10 | (
 | 
  
    | 11 | @issimulate VARCHAR(20),
 | 
  
    | 12 | @CreateBy VARCHAR(200),
 | 
  
    | 13 | @ChangeBy VARCHAR(200),
 | 
  
    | 14 | @startdate VARCHAR(20),
 | 
  
    | 15 | @enddate VARCHAR(20),
 | 
  
    | 16 | @paygradestart VARCHAR(20),
 | 
  
    | 17 | @paygradeend VARCHAR(20),
 | 
  
    | 18 | @empstart VARCHAR(200),
 | 
  
    | 19 | @empend VARCHAR(200),
 | 
  
    | 20 | @emptype VARCHAR(200),
 | 
  
    | 21 | @empsubtype VARCHAR(200)
 | 
  
    | 22 | )
 | 
  
    | 23 | AS
 | 
  
    | 24 | 
 | 
  
    | 25 | 
 | 
  
    | 26 | 
 | 
  
    | 27 | --DECLARE @issimulate VARCHAR (2)='0'
 | 
  
    | 28 | --DECLARE @treattype varchar(50)=''
 | 
  
    | 29 | --DECLARE @CreateBy VARCHAR(200)='00000111'
 | 
  
    | 30 | --DECLARE @ChangeBy VARCHAR(200)='00000111'
 | 
  
    | 31 | --DECLARE @startdate VARCHAR(20)='20240101'
 | 
  
    | 32 | --DECLARE @enddate VARCHAR(20)='20241231'
 | 
  
    | 33 | --DECLARE @paygradestart VARCHAR(20)=''
 | 
  
    | 34 | --DECLARE @paygradeend VARCHAR(20)=''
 | 
  
    | 35 | --DECLARE @empstart VARCHAR(200)='00000445'
 | 
  
    | 36 | --DECLARE @empend VARCHAR(200)='00000446'
 | 
  
    | 37 | --DECLARE @emptype VARCHAR(200)=''
 | 
  
    | 38 | --DECLARE @empsubtype VARCHAR(200)=''
 | 
  
    | 39 | 
 | 
  
    | 40 | 
 | 
  
    | 41 | DECLARE @now VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmdd')
 | 
  
    | 42 | DECLARE @datecreate VARCHAR(20) = dbo.fn_formatdatetime(GETDATE(), 'yyyymmddhhmmdd')
 | 
  
    | 43 | DECLARE @CreateDate VARCHAR(200)=''
 | 
  
    | 44 | DECLARE @ChangeDate VARCHAR(200)=''
 | 
  
    | 45 | 
 | 
  
    | 46 | set @CreateDate =@datecreate
 | 
  
    | 47 | set @ChangeDate =@datecreate
 | 
  
    | 48 | 
 | 
  
    | 49 | --set @CreateBy =''
 | 
  
    | 50 | --set @ChangeBy =''
 | 
  
    | 51 | --set @startdate =@now
 | 
  
    | 52 | --set @enddate =@now
 | 
  
    | 53 | --set @paygradestart ='00'
 | 
  
    | 54 | --set @paygradeend ='03'
 | 
  
    | 55 | --set @empstart ='10000148'
 | 
  
    | 56 | --set @empend ='10000151'
 | 
  
    | 57 | --set @emptype ='04'
 | 
  
    | 58 | --set @empsubtype ='10'
 | 
  
    | 59 | 
 | 
  
    | 60 | SELECT EmployeeID,CAST(REPLACE(ISNULL(dbo.GetPEN(Amount), '0'), ',', '.') AS DECIMAL(22,0)) as Amount 
 | 
  
    | 61 | into #pa4
 | 
  
    | 62 | from PHRPA0004 
 | 
  
    | 63 | where 
 | 
  
    | 64 | 	EmployeeID between @empstart and @empend 
 | 
  
    | 65 | 	and WageType=(select top(1)WageType from PHRPYCU0305 where WageTypeCum='B003' and EndDate='99991231')
 | 
  
    | 66 | 	and @startdate<=EndDate
 | 
  
    | 67 | 	and @enddate>=StartDate
 | 
  
    | 68 | 
 | 
  
    | 69 | DECLARE @datatable as table (
 | 
  
    | 70 | HeaderSequence varchar(500),
 | 
  
    | 71 | Sequence varchar(500),
 | 
  
    | 72 | StartDate varchar(500),
 | 
  
    | 73 | EndDate varchar(500),
 | 
  
    | 74 | EmployeeID varchar(500),
 | 
  
    | 75 | TreatmentType varchar(500),
 | 
  
    | 76 | Plafond varchar(500),
 | 
  
    | 77 | PlafondRemain varchar(500),
 | 
  
    | 78 | PlafondUsed varchar(500),
 | 
  
    | 79 | MedicalFamily varchar(500),
 | 
  
    | 80 | PayrollGrade varchar(500),
 | 
  
    | 81 | CreateBy varchar(500),
 | 
  
    | 82 | CreateDate varchar(500),
 | 
  
    | 83 | ChangeBy varchar(500),
 | 
  
    | 84 | ChangeDate varchar(500)
 | 
  
    | 85 | )
 | 
  
    | 86 | DECLARE @datatablefinish as table (
 | 
  
    | 87 | HeaderSequence varchar(500),
 | 
  
    | 88 | Sequence varchar(500),
 | 
  
    | 89 | StartDate varchar(500),
 | 
  
    | 90 | EndDate varchar(500),
 | 
  
    | 91 | EmployeeID varchar(500),
 | 
  
    | 92 | TreatmentType varchar(500),
 | 
  
    | 93 | Plafond varchar(500),
 | 
  
    | 94 | PlafondRemain varchar(500),
 | 
  
    | 95 | PlafondUsed varchar(500),
 | 
  
    | 96 | MedicalFamily varchar(500),
 | 
  
    | 97 | PayrollGrade varchar(500),
 | 
  
    | 98 | CreateBy varchar(500),
 | 
  
    | 99 | CreateDate varchar(500),
 | 
  
    | 100 | ChangeBy varchar(500),
 | 
  
    | 101 | ChangeDate varchar(500)
 | 
  
    | 102 | )
 | 
  
    | 103 | 
 | 
  
    | 104 | 
 | 
  
    | 105 | if @emptype !='' or @emptype is not null
 | 
  
    | 106 | begin
 | 
  
    | 107 | select convert(varchar(50),'1')as HeaderSequence
 | 
  
    | 108 | ,ROW_NUMBER()  OVER (partition by pa2.EmployeeID order by pa2.EmployeeID)+5 as [Sequence]
 | 
  
    | 109 | ,@startdate as StartDate
 | 
  
    | 110 | ,@enddate as EndDate
 | 
  
    | 111 | ,pa2.EmployeeID
 | 
  
    | 112 | ,PLAF.TreatmentType
 | 
  
    | 113 | ,case 
 | 
  
    | 114 | when PLAF.TreatmentType='300' then pa4.Amount
 | 
  
    | 115 | when PLAF.TreatmentType='400' then pa4.Amount
 | 
  
    | 116 | else PLAF.FixedValue end as Plafond
 | 
  
    | 117 | ,case 
 | 
  
    | 118 | when PLAF.TreatmentType='300' then pa4.Amount
 | 
  
    | 119 | when PLAF.TreatmentType='400' then pa4.Amount
 | 
  
    | 120 | else PLAF.FixedValue end as PlafondRemain
 | 
  
    | 121 | ,convert(varchar(50),'0.00')as PlafondUsed 
 | 
  
    | 122 | ,PLAF.MedicalFamily
 | 
  
    | 123 | ,pa3.PayrollGrade
 | 
  
    | 124 | into #temp
 | 
  
    | 125 | from PHRPA0002 pa2 
 | 
  
    | 126 | left join #pa4 pa4 on pa2.EmployeeID=pa4.EmployeeID
 | 
  
    | 127 | left outer join PHRPA0003 pa3 on pa2.EmployeeID = pa3.EmployeeID and pa3.EndDate='99991231' 
 | 
  
    | 128 | left outer join  PHRBNMEDTREATPLAF PLAF on 
 | 
  
    | 129 | 	PLAF.CompanyID = pa2.CompanyID 
 | 
  
    | 130 | 	and PLAF.EndDate='99991231'
 | 
  
    | 131 | 	and (PLAF.PayClass = pa3.PayrollClass  or PLAF.PayClass ='*')
 | 
  
    | 132 | 	and (PLAF.PayType = pa3.PayrollType  or PLAF.PayType ='*')
 | 
  
    | 133 | 	and (PLAF.PayGrade = pa3.PayrollGrade  or PLAF.PayGrade ='*')
 | 
  
    | 134 | 	and (PLAF.PayArea = pa3.PayrollArea  or PLAF.PayArea ='*')
 | 
  
    | 135 | 	--and PLAF.TreatmentType = @treattype
 | 
  
    | 136 | where pa2.EmployeeStatus='01' and pa2.StartDate <= @now and pa2.EndDate >= @now and pa2.EmployeeType = @emptype  and pa2.EmployeeID between @empstart and @empend 
 | 
  
    | 137 | 
 | 
  
    | 138 | 
 | 
  
    | 139 | update #temp set
 | 
  
    | 140 | HeaderSequence = a.HeaderSequence from (select  max(HeaderSequence) + 1as HeaderSequence,EmployeeID from phrpa0040 group by EmployeeID)a where a.EmployeeID = #temp.EmployeeID
 | 
  
    | 141 | 
 | 
  
    | 142 | insert into @datatable
 | 
  
    | 143 | select *,
 | 
  
    | 144 |  @CreateBy,
 | 
  
    | 145 |  @CreateDate,
 | 
  
    | 146 |  @ChangeBy,
 | 
  
    | 147 |  @ChangeDate
 | 
  
    | 148 |  from #temp
 | 
  
    | 149 | 
 | 
  
    | 150 |  drop table #temp
 | 
  
    | 151 | end
 | 
  
    | 152 | 
 | 
  
    | 153 | 
 | 
  
    | 154 | if @emptype ='' or @emptype is null
 | 
  
    | 155 | begin
 | 
  
    | 156 | select convert(varchar(50),'1')as HeaderSequence
 | 
  
    | 157 | ,ROW_NUMBER()  OVER (partition by pa2.EmployeeID order by pa2.EmployeeID)+5 as [Sequence],@startdate as StartDate
 | 
  
    | 158 | ,@enddate as EndDate,pa2.EmployeeID
 | 
  
    | 159 | ,PLAF.TreatmentType
 | 
  
    | 160 | ,case 
 | 
  
    | 161 | when PLAF.TreatmentType='300' then pa4.Amount
 | 
  
    | 162 | when PLAF.TreatmentType='400' then pa4.Amount
 | 
  
    | 163 | else PLAF.FixedValue end as Plafond
 | 
  
    | 164 | ,case 
 | 
  
    | 165 | when PLAF.TreatmentType='300' then pa4.Amount
 | 
  
    | 166 | when PLAF.TreatmentType='400' then pa4.Amount
 | 
  
    | 167 | else PLAF.FixedValue end as PlafondRemain
 | 
  
    | 168 | ,convert(varchar(50),'0.00')as PlafondUsed 
 | 
  
    | 169 | ,PLAF.MedicalFamily
 | 
  
    | 170 | ,pa3.PayrollGrade
 | 
  
    | 171 | into #temp2
 | 
  
    | 172 | from PHRPA0002 pa2 
 | 
  
    | 173 | left join #pa4 pa4 on pa2.EmployeeID=pa4.EmployeeID
 | 
  
    | 174 | left outer join PHRPA0003 pa3 on pa2.EmployeeID = pa3.EmployeeID and pa3.EndDate='99991231'
 | 
  
    | 175 | left outer join PHRBNMEDTREATPLAF PLAF on PLAF.CompanyID = pa2.CompanyID  
 | 
  
    | 176 | 	and PLAF.EndDate='99991231'
 | 
  
    | 177 | 	and (PLAF.PayClass = pa3.PayrollClass  or PLAF.PayClass ='*')
 | 
  
    | 178 | 	and (PLAF.PayType = pa3.PayrollType  or PLAF.PayType ='*')
 | 
  
    | 179 | 	and (PLAF.PayGrade = pa3.PayrollGrade  or PLAF.PayGrade ='*')
 | 
  
    | 180 | 	and (PLAF.PayArea = pa3.PayrollArea  or PLAF.PayArea ='*')
 | 
  
    | 181 | 	--and PLAF.TreatmentType = @treattype
 | 
  
    | 182 | where pa2.EmployeeStatus='01' and pa2.StartDate <= @now and pa2.EndDate >= @now and pa2.EmployeeID between @empstart and @empend 
 | 
  
    | 183 | update #temp2 set
 | 
  
    | 184 | HeaderSequence = a.HeaderSequence from (select  max(HeaderSequence) + 1as HeaderSequence,EmployeeID from phrpa0040 group by EmployeeID)a where a.EmployeeID = #temp2.EmployeeID
 | 
  
    | 185 | 
 | 
  
    | 186 | insert into @datatable
 | 
  
    | 187 | select *,
 | 
  
    | 188 |  @CreateBy,
 | 
  
    | 189 |  @CreateDate,
 | 
  
    | 190 |  @ChangeBy,
 | 
  
    | 191 |  @ChangeDate
 | 
  
    | 192 |  from #temp2
 | 
  
    | 193 | 
 | 
  
    | 194 | drop table #temp2
 | 
  
    | 195 | end
 | 
  
    | 196 | 
 | 
  
    | 197 | if @paygradestart !='' and @empstart=''
 | 
  
    | 198 | begin
 | 
  
    | 199 | insert into @datatablefinish
 | 
  
    | 200 | select * from @datatable where PayrollGrade between @paygradestart and @paygradeend
 | 
  
    | 201 | end
 | 
  
    | 202 | if @empstart !='' and @paygradestart =''
 | 
  
    | 203 | begin
 | 
  
    | 204 | insert into @datatablefinish
 | 
  
    | 205 | select * from @datatable where  EmployeeID between @empstart and @empend 
 | 
  
    | 206 | end
 | 
  
    | 207 | if @paygradestart !='' and @empstart !=''
 | 
  
    | 208 | begin
 | 
  
    | 209 | insert into @datatablefinish
 | 
  
    | 210 | select * from @datatable where PayrollGrade between @paygradestart and @paygradeend 
 | 
  
    | 211 | end
 | 
  
    | 212 | 
 | 
  
    | 213 | if @issimulate !='0'
 | 
  
    | 214 | begin
 | 
  
    | 215 | insert into PHRPA0040
 | 
  
    | 216 | select distinct
 | 
  
    | 217 | StartDate ,
 | 
  
    | 218 | EndDate ,
 | 
  
    | 219 | EmployeeID ,
 | 
  
    | 220 | HeaderSequence ,
 | 
  
    | 221 | '',
 | 
  
    | 222 | 'System Mass',
 | 
  
    | 223 | CreateBy ,
 | 
  
    | 224 | CreateDate ,
 | 
  
    | 225 | ChangeBy ,
 | 
  
    | 226 | ChangeDate ,
 | 
  
    | 227 | '','','','','','','','','','','','','','',0,'','','',null
 | 
  
    | 228 | from @datatablefinish
 | 
  
    | 229 | 
 | 
  
    | 230 | insert into PHRPA0041
 | 
  
    | 231 | select 
 | 
  
    | 232 | EmployeeID ,
 | 
  
    | 233 | HeaderSequence ,
 | 
  
    | 234 | Sequence,
 | 
  
    | 235 | TreatmentType,
 | 
  
    | 236 | Plafond,
 | 
  
    | 237 | PlafondRemain,
 | 
  
    | 238 | PlafondUsed,
 | 
  
    | 239 | MedicalFamily,
 | 
  
    | 240 | ''
 | 
  
    | 241 | from @datatablefinish
 | 
  
    | 242 | end
 | 
  
    | 243 | --select distinct EmployeeID,'Sukses Generate'as Status from @datatablefinish
 | 
  
    | 244 | 
 | 
  
    | 245 | select 
 | 
  
    | 246 | EmployeeID ,
 | 
  
    | 247 | HeaderSequence ,
 | 
  
    | 248 | Sequence,
 | 
  
    | 249 | TreatmentType,
 | 
  
    | 250 | Plafond,
 | 
  
    | 251 | PlafondRemain,
 | 
  
    | 252 | PlafondUsed,
 | 
  
    | 253 | MedicalFamily,
 | 
  
    | 254 | CASE WHEN @issimulate='0' then 'Simulate' WHEN @issimulate='1' then 'Save Success' end as Status
 | 
  
    | 255 | from @datatablefinish
 | 
  
    | 256 | 
 | 
  
    | 257 | drop table #pa4
 | 
  
    | 258 | 
 | 
  
    | 259 | 
 |