Skip to content

Commit 7b5c8e4

Browse files
authored
Finish feature/OTC-40-2
Feature/otc 40
2 parents 3905426 + f370841 commit 7b5c8e4

2 files changed

Lines changed: 286 additions & 1 deletion

File tree

-32.2 KB
Binary file not shown.

Migration script/openIMIS migration latest.sql

Lines changed: 286 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4725,4 +4725,289 @@ BEGIN
47254725
END
47264726

47274727

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

0 commit comments

Comments
 (0)