set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
/****** Object: StoredProcedure [dbo].[cst_ImportDEMOCClaimReserve] Script Date: 07/16/2008 16:06:53 ******/
ALTER PROCEDURE [dbo].[cst_ImportDEMOCClaimReserve] (
@BwcClaimNumber varchar(20),
@ReserveAmt int,
@CompReserveAmt int,
@MedReserveAmt int,
@ReserveCode char(2),
@ReducibleAmt int,
@NonReducibleAmt int,
@CompAmt int,
@MedAmt int,
@HandicapPercent smallint,
@ExtractDate smalldatetime,
@PolicyNumber int,
@BusSeqNo int,
@InjuryDate smalldatetime,
@IWName varchar(20),
@DeathDate smalldatetime,
@ClaimRatingIndicator char(1),
@ClaimStatus char(2),
@ReducibleMedAmt int,
@NonReducibleMedAmt int,
@SubrogationAmt int,
@ICCAI Char(1))
AS
set nocount on
Declare @pkClaim int,
@MinYear smalldatetime,
@TpaClaimStatus varchar(20),
@ClaimProfile int,
@HandicapSum int,
@pkEmployer int,
@pkInjuredWorker int,
@pkIwLocation int,
@pkEmployerAddress int,
@Today datetime,
@ActiveEmployer char
Set @Today = getdate()
Set @pkClaim = 0
--First grab claim if Policynumber in file match what is in Team
Select @pkClaim = pkClaim from Claim c
join AccountsAddress a on c.fkEmployerAddress = a.pkAddress
join AccountsEmployer e on a.fkEmployer = e.pkEmployer
where BwcClaimNum = @BwcClaimNumber
and e.PolicyNumber = Convert(Varchar(20),@PolicyNumber) + '-' + Convert(Varchar(20),@BusSeqNo)
--and not exists (Select pkClaimReserveHist from ClaimReserveHist h where c.pkClaim = h.fkClaim and h.ExtractDate = @ExtractDate)
--or exists in (Select ObjPolicyNo from v_BwcMaxSuccessor v where v.PolicyNo = e
--If claim was not found, grab claim if successor policy does not exist in Rate (this means that if a successor policy does exist, we did not request it in the file)
If @pkClaim = 0
Select @pkClaim = pkClaim from Claim c
join AccountsAddress a on c.fkEmployerAddress = a.pkAddress
join AccountsEmployer e on a.fkEmployer = e.pkEmployer
Where not exists (Select SuccPolicyNo from rating.dbo.v_MaxBWCCombination v
where Convert(Varchar(20),v.PolicyNo) + '-' + Convert(Varchar(20),v.BusSeqNo) = e.PolicyNumber)
--and not exists (Select pkClaimReserveHist from ClaimReserveHist h where c.pkClaim = h.fkClaim and h.ExtractDate = @ExtractDate)
and BwcClaimNum = @BwcClaimNumber
--If claim was not found, grab claim if successor policy is SI
If @pkClaim = 0
Select @pkClaim = pkClaim from Claim c
join AccountsAddress a on c.fkEmployerAddress = a.pkAddress
join AccountsEmployer e on a.fkEmployer = e.pkEmployer
join Rating.dbo.v_MaxBWCSuccessor v
on v.PolicyNo = @PolicyNumber and v.BusSeqNo = @BusSeqNo
and v.ObjPolicyNo between 20000000 and 30000000
and Convert(Varchar(20),v.ObjPolicyNo) + '-' + Convert(Varchar(20),v.ObjBusSeqNo) = e.PolicyNumber
--and not exists (Select pkClaimReserveHist from ClaimReserveHist h where c.pkClaim = h.fkClaim and h.ExtractDate = @ExtractDate)
and BwcClaimNum = @BwcClaimNumber
If @pkClaim > 0
Begin
Insert into ClaimReserveHist(
fkClaim,
ReserveAmt,
ReducibleAmt,
NonReducibleAmt,
CompAmt,
MedicalAmt,
HandicapPercent,
UserLUP,
DateLUP,
ExtractDate,
PolicyNumber,
CompReserveAmt,
MedReserveAmt,
ReserveCode,
ClaimRatingIndicator,
ClaimStatus,
ReducibleMedAmt, --jld 7.19.07
NonReducibleMedAmt, --jld 7.19.07
SubrogationAmt,
appealindicator --RAV 7/1/08
)
Values(
@pkClaim,
@ReserveAmt,
@ReducibleAmt,
@NonReducibleAmt,
@CompAmt,
@MedAmt,
@HandicapPercent,
User_Name(),
@Today,
@ExtractDate,
@PolicyNumber,
@CompReserveAmt,
@MedReserveAmt,
@ReserveCode,
@ClaimRatingIndicator,
@ClaimStatus,
@ReducibleMedAmt, --jld 7.19.07
@NonReducibleMedAmt, --jld 7.19.07
@SubrogationAmt,
@ICCAI --RAV 7/1/08
)
Update
Claim
Set
InjuryDate = @InjuryDate
Where
pkClaim = @pkClaim
and InjuryDate is null
and @InjuryDate is not null
-- Tyson Price 05/13/2008 - Added Update to Claim SubrogationAmount
Update
Claim
Set
SubrogationAmount = isnull(@SubrogationAmt,0)
Where
pkClaim = @pkClaim
and isnull(SubrogationAmount,0) = 0
End
Else --Claim does not yet exist
Begin
Set @MinYear = '01/01/' + convert(varchar(4), datepart(year, @Today) - 6)
If Exists( Select pkEmployer
From AccountsEmployer
Where PolicyNumber = Convert(Varchar(20),@PolicyNumber) + '-' + Convert(Varchar(20),@BusSeqNo))
And Not Exists( Select bwcClaimNum
From Claim
Where @BwcClaimNumber = BwcClaimNum)
And (@InjuryDate >= @MinYear or @ClaimRatingIndicator = 'R') --jdorazio 4.19.06
Begin
Begin tran
--Find the primary key for the employer
Select @pkEmployer = ( Select max(pkEmployer)
From AccountsEmployer
Where PolicyNumber = Convert(Varchar(20),@PolicyNumber) + '-' + Convert(Varchar(20),@BusSeqNo))
--Find the active status of the employer
select @ActiveEmployer = (
select case when ae.TPATerminationDate >= GetDate() and ae.TPAOrgID in (54, 5917) then 'Y'
when ae.TPATerminationDate is NULL and ae.TPAOrgID in (54, 5917) then 'Y'
when ae.TPATerminationDate = '' and ae.TPAOrgID in (54, 5917) then 'Y'
else 'N' end
From AccountsEmployer ae (nolock)
where pkEmployer = @pkEmployer)
--Find the employers address for the claim
Select @pkEmployerAddress = ( Select max(pkAddress)
From AccountsEmployer ae,
AccountsAddress aa
Where ae.PolicyNumber = Convert(varchar(20), @PolicyNumber) + '-' + Convert(varchar(20),@BusSeqNo) and
ae.pkEmployer = aa.fkEmployer)
--Find the primary key of the injured worker
select @pkInjuredWorker = isnull( (Select max(pkInjuredWorker)
From InjuredWorker
With (updlock)),0) + 1
Set @ClaimProfile = IsNull( ( Select max(fkClaimProfile)
From AccountsEmployerClaimProfile
Where FkEmployer = @pkEmployer),121)
--Set new claims to OPEN per Tara Heath - jdorazio 4.19.06
SET @TpaClaimStatus = NULL
IF @ClaimProfile = 121
BEGIN
IF ISNULL(@ClaimStatus, '') NOT IN ('DA', 'DP', 'DS', 'SI', 'SM', 'ST')
SET @TpaClaimStatus = 'Open'
END
Insert InjuredWorker
(pkInjuredWorker
,Claimant
,LastName
,FirstName
,MidName)
Values
(@pkInjuredWorker
,@IwName
--Lastname
,Case Charindex(',',Isnull(@IwName,''))
When 0 then
Case Patindex('% %', Isnull(@IwName, ''))
When 0 then @IwName
Else right(@IwName, patindex('% %',reverse(@IwName)) - 1)
End
Else IsNull(Left(ltrim(rtrim(@IwName)), Charindex(',', ltrim(rtrim(@IwName)))-1), '')
end,
--FirstName
Case CharIndex(',', Isnull(@IwName,''))
When 0 then
Case Patindex('% %', Isnull(@IwName, ''))
When 0 then ''
Else left(@IwName, len(@IwName) - patindex('% %',reverse(@IwName)))
end
Else Case Patindex('% %', IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), ''))
When 0 then IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), '')
Else Left(IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), ''), Patindex('% %',IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), '')))
end
end,
--MidName
LEFT(case Patindex('% %',
isnull((Case CharIndex(',', Isnull(@IwName,''))
When 0 then
Case Patindex('% %', Isnull(@IwName, ''))
When 0 then ''
Else
ltrim(rtrim(left(@IwName, len(@IwName) - patindex('% %',reverse(@IwName)))))
end
Else IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), '')
end),''))
When 0 then ''
Else Right(
( Case CharIndex(',', Isnull(@IwName,''))
When 0 then
Case Patindex('% %', Isnull(@IwName, ''))
When 0 then ''
Else
ltrim(rtrim(left(@IwName, len(@IwName) - patindex('% %',reverse(@IwName)))))
end
Else IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), '')
end),
(
Len( ( Case CharIndex(',', Isnull(@IwName,''))
When 0 then
Case Patindex('% %', Isnull(@IwName, ''))
When 0 then ''
Else left(@IwName, len(@IwName) - patindex('% %',reverse(@IwName)))
end
Else IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), '')
end)))
- Patindex('% %', ( Case CharIndex(',', Isnull(@IwName,''))
When 0 then
Case Patindex('% %', Isnull(@IwName, ''))
When 0 then ''
Else left(@IwName, len(@IwName) - patindex('% %',reverse(@IwName)))
end
Else IsNull(ltrim(rtrim(Right(rtrim(ltrim(@IwName)), (len(@IwName) - charindex(',',@IwName))))), '')
end))
)
end,1))
Select @pkIwLocation = isnull( (select max(pkIwLocation)
From IwLocation
with (updlock)),0) + 1
Insert IwLocation
(pkIwLocation
,fkInjuredWorker)
values
(@pkIwLocation
,@pkInjuredWorker)
select @pkClaim = isnull( (Select max(pkClaim)
From Claim
With (updlock)),0) + 1
-- Tyson Price 05/13/2008
-- Added SubrogationAmount
Insert Claim
(pkClaim
,BwcClaimNum
,fkEmployerAddress
,fkIwLocation
,InjuryDate
,DateEntered
,EnteredBy
,ClaimProfile
,TPAClaimStatus
,SubrogationAmount)
Values
(@pkClaim
,@BwcClaimNumber
,@pkEmployerAddress
,@pkIwLocation
,@InjuryDate
,getdate()
,User_Name()
,@ClaimProfile
,@TPAClaimStatus
,isnull(@SubrogationAmt,0))
Commit Tran
if not exists( Select fkClaim
From ClaimReserveHist
Where fkClaim = @pkClaim
and ExtractDate = @ExtractDate
and ReserveAmt = @ReserveAmt
and ReducibleAmt = @ReducibleAmt
and NonReducibleAmt = @NonReducibleAmt
and CompAmt = @CompAmt
and MedicalAmt = @MedAmt
and HandicapPercent = @HandicapPercent
and CompReserveAmt = @CompReserveAmt --jfrease 9.21.05
and MedReserveAmt = @MedReserveAmt --jfrease 9.21.05
and ReserveCode = @ReserveCode --jdorazio 4.19.06
and ClaimRatingIndicator = @ClaimRatingIndicator --jdorazio 4.19.06
and ClaimStatus = @ClaimStatus --jdorazio 4.19.06
and ReducibleMedAmt = @ReducibleMedAmt --jld 7.19.07
and NonReducibleMedAmt = @NonReducibleMedAmt --jld 7.19.07
and SubrogationAmt = @SubrogationAmt) --jld 12.12.07
begin
Insert into ClaimReserveHist
(fkClaim,
ReserveAmt,
ReducibleAmt,
NonReducibleAmt,
CompAmt,
MedicalAmt,
HandicapPercent,
UserLUP,
DateLUP,
ExtractDate,
PolicyNumber,
CompReserveAmt, --jfrease 9.21.05
MedReserveAmt, --jfrease 9.21.05
ReserveCode, --jdorazio 4.19.06
ClaimRatingIndicator, --jdorazio 4.19.06
ClaimStatus, --jdorazio 4.19.06
ReducibleMedAmt, --jld 7.19.07
NonReducibleMedAmt, --jld 7.19.07
SubrogationAmt --jld 12.12.07
)
Values
(@pkClaim,
@ReserveAmt,
@ReducibleAmt,
@NonReducibleAmt,
@CompAmt,
@MedAmt,
@HandicapPercent,
User_Name(),
@Today,
@ExtractDate,
@PolicyNumber,
@CompReserveAmt, --jfrease 9.21.05
@MedReserveAmt, --jfrease 9.21.05
@ReserveCode, --dorazioj 4.19.06
@ClaimRatingIndicator, --jdorazio 4.19.06
@ClaimStatus, --jdorazio 4.19.06
@ReducibleMedAmt, --jld 7.19.07
@NonReducibleMedAmt, --jld 7.19.07
@SubrogationAmt) --jld 12.12.07
end
if not exists( Select fkClaim
From ClaimHandicap
Where fkClaim = @pkClaim and
HandicapType = 26)
begin
Select @HandicapSum = ( Select Sum(AwardPercent)
From ClaimHandicap
Where fkClaim = @pkClaim)
if isnull(@HandicapSum,0) < @HandicapPercent
begin
If @HandicapPercent <> 0
begin
Exec cst_SaveClaimHandicap 0, --Insert Row
@pkClaim, --Current claim
26, --Unknown Type
@ExtractDate,
@ExtractDate,
@ExtractDate,
@HandicapPercent,
'From BWC' , --User
null, --Y1
null, --Y2
null, --Y3
null --Y4
end
end
end
if ((@CompAmt > 5000 or
@MedAmt > 10000 or
(@ReserveAmt <> 0 and @CompAmt = 0) or
@ReserveAmt > 25000) and @ActiveEmployer = 'Y')
begin
Exec sp_CleanupClaimReserveHistWaitingAddNote @pkClaim,
@pkEmployer
end
end
end