@@ -4737,4 +4737,289 @@ BEGIN
47374737 END
47384738
47394739
4740- GO
4740+ GO
4741+
4742+ IF OBJECT_ID (' [uspInsertIndexMonthly]' , ' P' ) IS NOT NULL
4743+ DROP PROCEDURE [uspInsertIndexMonthly]
4744+ GO
4745+ CREATE PROCEDURE [dbo].[uspInsertIndexMonthly]
4746+ (
4747+ @Type varchar (1 ),
4748+ @RelType INT , -- M 12, Q 4, Y 1
4749+ @MStart INT , -- M 1--12 Q 1--4 Y --1
4750+ @MEnd INT ,
4751+ @Year INT ,
4752+ @Period INT ,
4753+ @LocationId INT = 0 ,
4754+ @ProductID INT = 0 ,
4755+ @PrdValue decimal (18 ,2 ) = 0 ,
4756+ @AuditUser int = - 1
4757+ )
4758+
4759+ AS
4760+ BEGIN
4761+ DECLARE @DistrPerc as decimal (18 ,2 )
4762+ DECLARE @ClaimValueItems as decimal (18 ,2 )
4763+ DECLARE @ClaimValueservices as decimal (18 ,2 )
4764+ DECLARE @RelIndex as decimal (18 ,4 )
4765+
4766+
4767+
4768+ SELECT @DistrPerc = ISNULL (DistrPerc,1 ) FROM dbo .tblRelDistr WHERE ProdID = @ProductID AND Period = @Period AND DistrType = @RelType AND DistrCareType = @Type AND ValidityTo IS NULL
4769+
4770+ SELECT @ClaimValueItems = ISNULL (SUM (tblClaimItems .PriceValuated ),0 )
4771+ FROM tblClaim INNER JOIN
4772+ tblClaimItems ON tblClaim .ClaimID = tblClaimItems .ClaimID INNER JOIN
4773+ tblHF ON tblClaim .HFID = tblHF .HfID
4774+ INNER JOIN tblProductItems pi on tblClaimItems .ProdID = pi .ProdID and pi .PriceOrigin = ' R' AND pi .ValidityTo is null and tblClaimItems .ItemID = pi .ItemID
4775+ WHERE (tblClaimItems .ClaimItemStatus = 1 ) AND (tblClaim .ValidityTo IS NULL ) AND (tblClaimItems .ValidityTo IS NULL ) AND (tblClaim .ClaimStatus = 16 OR
4776+ tblClaim .ClaimStatus = 8 ) AND (ISNULL (MONTH (tblClaim .ProcessStamp ) ,- 1 ) BETWEEN @MStart AND @MEnd ) AND
4777+ (ISNULL (YEAR (tblClaim .ProcessStamp ) ,- 1 ) = @Year) AND
4778+ (tblClaimItems .ProdID = @ProductID)
4779+ AND ((@TYPE = ' O' AND (tblHF .HFLevel = ' H' )) OR (@TYPE = ' I' AND (tblHF .HFLevel <> ' H' )) OR @TYPE = ' B' )
4780+
4781+
4782+ SELECT @ClaimValueservices = ISNULL (SUM (tblClaimServices .PriceValuated ) ,0 )
4783+ FROM tblClaim INNER JOIN
4784+ tblClaimServices ON tblClaim .ClaimID = tblClaimServices .ClaimID INNER JOIN
4785+ tblHF ON tblClaim .HFID = tblHF .HfID
4786+ INNER JOIN tblProductServices ps on tblClaimServices .ProdID = ps .ProdID and ps .PriceOrigin = ' R' AND ps .ValidityTo is null and tblClaimServices .ServiceID = ps .ServiceID
4787+ WHERE (tblClaimServices .ClaimServiceStatus = 1 ) AND (tblClaim .ValidityTo IS NULL ) AND (tblClaimServices .ValidityTo IS NULL ) AND (tblClaim .ClaimStatus = 16 OR
4788+ tblClaim .ClaimStatus = 8 ) AND (ISNULL (MONTH (tblClaim .ProcessStamp ) ,- 1 ) BETWEEN @MStart AND @MEnd ) AND
4789+ (ISNULL (YEAR (tblClaim .ProcessStamp ) ,- 1 ) = @Year) AND
4790+ (tblClaimServices .ProdID = @ProductID)
4791+ AND ((@TYPE = ' O' AND (tblHF .HFLevel = ' H' )) OR (@TYPE = ' I' AND (tblHF .HFLevel <> ' H' )) OR @TYPE = ' B' )
4792+
4793+
4794+ IF @ClaimValueItems + @ClaimValueservices = 0
4795+ BEGIN
4796+ -- basically all 100% is available
4797+ SET @RelIndex = 1
4798+ INSERT INTO [tblRelIndex] ([ProdID],[RelType],[RelCareType],[RelYear],[RelPeriod],[CalcDate],[RelIndex],[AuditUserID],[LocationId] )
4799+ VALUES (@ProductID,@RelType,@Type,@Year,@Period,GETDATE (),@RelIndex,@AuditUser,@LocationId )
4800+ END
4801+ ELSE
4802+ BEGIN
4803+ SET @RelIndex = CAST ((@PrdValue * @DistrPerc) as Decimal (18 ,4 )) / (@ClaimValueItems + @ClaimValueservices)
4804+ INSERT INTO [tblRelIndex] ([ProdID],[RelType],[RelCareType],[RelYear],[RelPeriod],[CalcDate],[RelIndex],[AuditUserID],[LocationId])
4805+ VALUES (@ProductID,@RelType,@Type,@Year,@Period,GETDATE (),@RelIndex,@AuditUser,@LocationId )
4806+ END
4807+ END
4808+ GO
4809+
4810+
4811+ IF OBJECT_ID (' [uspRelativeIndexCalculationMonthly]' , ' P' ) IS NOT NULL
4812+ DROP PROCEDURE [uspRelativeIndexCalculationMonthly]
4813+ GO
4814+ CREATE PROCEDURE [dbo].[uspRelativeIndexCalculationMonthly]
4815+ (
4816+ @RelType INT , -- Month = 12 Quarter = 4 Year = 1
4817+ @Period INT , -- M 1--12 Q 1--4 Y --1
4818+ @Year INT ,
4819+ @LocationId INT = 0 ,
4820+ @ProductID INT = 0 ,
4821+ @AuditUser int = - 1 ,
4822+ @RtnStatus as int = 0 OUTPUT
4823+ )
4824+
4825+ AS
4826+ BEGIN
4827+ DECLARE @oReturnValue as int
4828+ SET @oReturnValue = 0
4829+ BEGIN TRY
4830+
4831+ DECLARE @MStart as int
4832+ DECLARE @MEnd as int
4833+ DECLARE @Month as int
4834+ DECLARE @PrdID as int
4835+ DECLARE @CurLocationId as int
4836+ DECLARE @PrdValue as decimal (18 ,2 )
4837+
4838+ -- !!!! Check first if not existing in the meantime !!!!!!!
4839+
4840+ CREATE TABLE #Numerator (
4841+ LocationId int ,
4842+ ProdID int ,
4843+ Value decimal (18 ,2 ),
4844+ WorkValue bit
4845+ )
4846+
4847+
4848+ -- first include the right period for processing
4849+ IF @RelType = 12
4850+ BEGIN
4851+ SET @MStart = @Period
4852+ SET @MEnd = @Period
4853+
4854+ END
4855+
4856+ IF @RelType = 4
4857+ BEGIN
4858+ IF @Period = 1
4859+ BEGIN
4860+ SET @MStart = 1
4861+ SET @MEnd = 3
4862+ END
4863+ IF @Period = 2
4864+ BEGIN
4865+ SET @MStart = 4
4866+ SET @MEnd = 6
4867+ END
4868+ IF @Period = 3
4869+ BEGIN
4870+ SET @MStart = 7
4871+ SET @MEnd = 9
4872+ END
4873+ IF @Period = 4
4874+ BEGIN
4875+ SET @MStart = 10
4876+ SET @MEnd = 12
4877+ END
4878+ END
4879+
4880+ IF @RelType = 1
4881+ BEGIN
4882+ SET @MStart = 1
4883+ SET @MEnd = 12
4884+
4885+ END
4886+
4887+ DECLARE @Date date
4888+ DECLARE @DaysInMonth int
4889+ DECLARE @EndDate date
4890+
4891+ SET @Month = @MStart
4892+ WHILE @Month <= @MEnd
4893+ BEGIN
4894+
4895+ SELECT @Date = CAST (CAST (@Year AS VARCHAR (4 )) + ' -' + CAST (@Month AS VARCHAR (2 )) + ' -' + ' 01' AS DATE )
4896+ SELECT @DaysInMonth = DATEDIFF (DAY ,@Date,DATEADD (MONTH ,1 ,@Date))
4897+ SELECT @EndDate = CAST (CONVERT (VARCHAR (4 ),@Year) + ' -' + CONVERT (VARCHAR (2 ),@Month ) + ' -' + CONVERT (VARCHAR (2 ),@DaysInMonth) AS DATE )
4898+
4899+ INSERT INTO #Numerator (LocationId,ProdID,Value ,WorkValue )
4900+
4901+
4902+ -- Get all the payment falls under the current month and assign it to Allocated
4903+
4904+ SELECT NumValue .LocationId , NumValue .ProdID , ISNULL (SUM (NumValue .Allocated ),0 ) Allocated , 1
4905+ FROM
4906+ (
4907+ SELECT L .LocationId ,Prod .ProdID ,
4908+ CASE
4909+ WHEN MONTH (DATEADD (D,- 1 ,PL .ExpiryDate )) = @Month AND YEAR (DATEADD (D,- 1 ,PL .ExpiryDate )) = @Year AND (DAY (PL .ExpiryDate )) > 1
4910+ THEN CASE WHEN DATEDIFF (D,CASE WHEN PR .PayDate < @Date THEN @Date ELSE PR .PayDate END ,PL .ExpiryDate ) = 0 THEN 1 ELSE DATEDIFF (D,CASE WHEN PR .PayDate < @Date THEN @Date ELSE PR .PayDate END ,PL .ExpiryDate ) END * ((SUM (PR .Amount ))/ (CASE WHEN (DATEDIFF (DAY ,CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ,PL .ExpiryDate )) <= 0 THEN 1 ELSE DATEDIFF (DAY ,CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ,PL .ExpiryDate ) END ))
4911+ WHEN MONTH (CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ) = @Month AND YEAR (CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ) = @Year
4912+ THEN ((@DaysInMonth + 1 - DAY (CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END )) * ((SUM (PR .Amount ))/ CASE WHEN DATEDIFF (DAY ,CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ,PL .ExpiryDate ) <= 0 THEN 1 ELSE DATEDIFF (DAY ,CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ,PL .ExpiryDate ) END ))
4913+ WHEN PL .EffectiveDate < @Date AND PL .ExpiryDate > @EndDate AND PR .PayDate < @Date
4914+ THEN @DaysInMonth * (SUM (PR .Amount )/ CASE WHEN (DATEDIFF (DAY ,CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ,DATEADD (D,- 1 ,PL .ExpiryDate ))) <= 0 THEN 1 ELSE DATEDIFF (DAY ,CASE WHEN PR .PayDate < PL .EffectiveDate THEN PL .EffectiveDate ELSE PR .PayDate END ,PL .ExpiryDate ) END )
4915+ END Allocated
4916+ FROM tblPremium PR INNER JOIN tblPolicy PL ON PR .PolicyID = PL .PolicyID
4917+ INNER JOIN tblProduct Prod ON PL .ProdID = Prod .ProdID
4918+ LEFT JOIN tblLocations L ON ISNULL (Prod .LocationId ,- 1 ) = ISNULL (L .LocationId ,- 1 )
4919+ WHERE PR .ValidityTo IS NULL
4920+ AND PL .ValidityTo IS NULL
4921+ AND Prod .ValidityTo IS NULL
4922+ AND ISNULL (Prod .LocationId ,- 1 ) = ISNULL (@LocationId,- 1 )
4923+ AND (Prod .ProdID = @ProductID OR @ProductId = 0 )
4924+ AND PL .PolicyStatus <> 1
4925+ AND PR .PayDate <= PL .ExpiryDate
4926+
4927+ -- AND ((MONTH(PR.PayDate) = @Counter OR MONTH(PL.EffectiveDate) = @Counter)
4928+ -- OR (YEAR(PR.PayDate) = @Year OR YEAR(PL.EffectiveDate) = @Year))
4929+ GROUP BY L .LocationId ,Prod .ProdID ,PR .Amount ,PR .PayDate ,PL .ExpiryDate ,PL .EffectiveDate
4930+ ) NumValue
4931+
4932+ GROUP BY LocationId,ProdID
4933+
4934+ SET @Month = @Month + 1
4935+ END
4936+
4937+ -- Now sum up the collected values
4938+ INSERT INTO #Numerator (LocationId,ProdID,Value ,WorkValue)
4939+ SELECT LocationId, ProdID, ISNULL (SUM (Value ),0 ) Allocated , 0
4940+ FROM #Numerator GROUP BY LocationId,ProdID
4941+
4942+ DELETE FROM #Numerator WHERE WorkValue = 1
4943+
4944+ -- DECLARE @Test as decimal(18,2)
4945+ -- SELECT @Test = SUM(Value) FROM #Numerator WHERE ProdID = 108
4946+
4947+
4948+ -- Now fetch the product percentage for relative prices --If not found then assume 1 = 100%
4949+ DECLARE @DistrType as char (1 ) ,@DistrTypeIP as char (1 ),@DistrTypeOP as char (1 )
4950+
4951+ -- DECLARE @ClaimValueItems as decimal(18,2)
4952+ -- DECLARE @ClaimValueservices as decimal(18,2)
4953+ -- DECLARE @RelIndex as decimal(18,4)
4954+ DECLARE @DistrPeriod as int , @DistrPeriodIP as int , @DistrPeriodOP as int
4955+ -- DECLARE @ClaimValueG as decimal(18,2)
4956+ -- DECLARE @ClaimValueIP as decimal(18,2)
4957+ -- DECLARE @ClaimValueOP as decimal(18,2)
4958+
4959+ DECLARE PRDLOOP CURSOR LOCAL FORWARD_ONLY FOR SELECT ProdID,Value , LocationId FROM #Numerator
4960+ OPEN PRDLOOP
4961+ FETCH NEXT FROM PRDLOOP INTO @PrdID , @PrdValue , @CurLocationId
4962+ WHILE @@FETCH_STATUS = 0
4963+ BEGIN
4964+
4965+ -- IF @PrdID = 108
4966+ -- BEGIN
4967+ -- SET @Test = @PrdValue
4968+ -- END
4969+ -- SELECT @DistrType = ISNULL(PeriodRelPrices,''), @DistrTypeIP = ISNULL(PeriodRelPricesIP,''), @DistrTypeOP = ISNULL(PeriodRelPricesOP,'') FROM dbo.tblProduct Where ProdID = @PrdID
4970+ SET @DistrType = (SELECT ISNULL (PeriodRelPrices,' ' ) FROM dbo .tblProduct Where ProdID = @PrdID)
4971+ SET @DistrTypeIP = (SELECT ISNULL (PeriodRelPricesIP,' ' ) FROM dbo .tblProduct Where ProdID = @PrdID)
4972+ SET @DistrTypeOP = (SELECT ISNULL (PeriodRelPricesOP,' ' ) FROM dbo .tblProduct Where ProdID = @PrdID)
4973+
4974+ -- don't run the index if not required
4975+ SET @DistrPeriod = CASE WHEN @RelType = 12 AND @DistrType = ' M' THEN 12
4976+ WHEN (@RelType = 4 ) AND @DistrType = ' Q' THEN 4
4977+ WHEN (@RelType = 1 ) AND @DistrType = ' Y' THEN 1
4978+ ELSE 0
4979+ END
4980+ SET @DistrPeriodIP = CASE
4981+ WHEN @RelType = 12 AND @DistrTypeIP = ' M' THEN 12
4982+ WHEN (@RelType = 4 ) AND @DistrTypeIP = ' Q' THEN 4
4983+ WHEN (@RelType = 1 ) AND @DistrTypeIP = ' Y' THEN 1
4984+ ELSE 0
4985+ END
4986+ SET @DistrPeriodOP = CASE WHEN @RelType = 12 AND @DistrTypeOP = ' M' THEN 12
4987+ WHEN (@RelType = 4 ) AND @DistrTypeOP = ' Q' THEN 4
4988+ WHEN (@RelType = 1 ) AND @DistrTypeOP = ' Y' THEN 1
4989+ ELSE 0
4990+ END
4991+
4992+ IF @DistrPeriod > 0 BEGIN EXEC [dbo].[uspInsertIndexMonthly] ' B' , @RelType,@MStart, @MEnd, @Year,@Period,@CurLocationId ,@PrdID ,@PrdValue ,@AuditUser END
4993+ ELSE -- cannot have IP/OP with General
4994+ BEGIN
4995+ IF @DistrPeriodIP > 0 BEGIN EXEC [dbo].[uspInsertIndexMonthly] ' I' , @RelType, @MStart, @MEnd, @Year,@Period ,@CurLocationId ,@PrdID ,@PrdValue ,@AuditUser END
4996+ IF @DistrPeriodOP > 0 BEGIN EXEC [dbo].[uspInsertIndexMonthly] ' O' , @RelType, @MStart, @MEnd, @Year ,@Period,@CurLocationId ,@PrdID ,@PrdValue , @AuditUser END
4997+ END
4998+
4999+
5000+ -- GET the total Claim Value
5001+
5002+ -- Now insert into the relative index table
5003+
5004+ FETCH NEXT FROM PRDLOOP INTO @PrdID , @PrdValue,@CurLocationId
5005+ END
5006+ CLOSE PRDLOOP
5007+ DEALLOCATE PRDLOOP
5008+
5009+ SET @RtnStatus = 0
5010+ FINISH:
5011+
5012+ RETURN @oReturnValue
5013+ END TRY
5014+
5015+ BEGIN CATCH
5016+ SELECT ' Unexpected error encountered'
5017+ SET @oReturnValue = 1
5018+ SET @RtnStatus = 1
5019+ RETURN @oReturnValue
5020+
5021+ END CATCH
5022+
5023+ END
5024+
5025+ SET ANSI_NULLS ON
0 commit comments