관리-도구
편집 파일: new 1.new 1
Text CREATE PROCEDURE dbo.sp_LoadRebalanceDetails ( @pi_intMasterId INTEGER, @pi_intControlId INTEGER ) AS /* ================================================================================ ' Name : dbo.sp_LoadRebalanceDetails ' Author : ' Description : This stored procedure is used to populate [Rebalance_Temp] table for the Rebalance logic. ' Inputs : MasterID INTEGER ControlID INTEGER ' Outputs : [Rebalance_Temp] table will be populated ' Return Code : -------------------------------------------------------------------------------- Revisions: -------------------------------------------------------------------------------- Ini | Date | Description -------------------------------------------------------------------------------- NJha |01/15/2009| ControlID has been added in the Input Parameters along with MasterID. Code to fetch the Manager Identifier of the Client/Bank other than SEIIAG have been added. Manager Identifier Column from the [Trust_To_Manager] table has been picked up dynamically. -------------------------------------------------------------------------------- ================================================================================ */ SET NOCOUNT ON TRUNCATE TABLE Rebalance_Temp DECLARE @Nonfund_str VARCHAR(8000), @QueryUpdateRebalTemp VARCHAR(1000), @Query VARCHAR(8000), @Qry1 VARCHAR(8000), @Nonfunds VARCHAR(8000), @Rebalance_str VARCHAR(8000), @Rebal VARCHAR(8000), @Assetidentifier VARCHAR(8000), @Assetidentifier_str VARCHAR(8000), @Condition VARCHAR(8000), @Condition1 VARCHAR(8000), @InvBoth BIT, @InvOffierCd BIT, @InvObjectiveCd BIT, @InvString VARCHAR(2000), @OrgString VARCHAR(200), @OrgId VARCHAR(50), @Fund_str VARCHAR(8000), @Funds VARCHAR(8000), @NonTaxable_str VARCHAR(2000), @NonTaxable VARCHAR(2000), @rPersonalTrust_str VARCHAR(2000), @rPersonalTrust VARCHAR(2000), @NonCustody_str VARCHAR(2000), @NonCustody VARCHAR(2000), @Custody_str VARCHAR(2000), @Custody VARCHAR(2000), @Manager_Identifier_Code CHAR(3), -- Naveen: To get Manager Identifier @TTM_Manager_Identifier_Column VARCHAR(50), -- Naveen: To get Trust_To_Manager Column for Manager's Trust Field Value @ACM_Manager_Identifier_Column VARCHAR(50), -- Naveen: To get Account_Master Column for Manager's Trust Field Value @OptionalCategoryCode VARCHAR(2) -- Naveen: Fetching Manager Identifier Code... SELECT @OrgId = Org_ID, @Manager_Identifier_Code = Manager_Identifier_Code FROM [IMAP_MA_FEES].[MA_FEES].[DBO].Client_Master_Information WHERE Master_Id = @pi_intMasterId AND Control_Id = @pi_intControlId -- Naveen: Getting Column Names from Trust_To_Manager and AccountMaster tables IF UPPER(LTRIM(RTRIM(@Manager_Identifier_Code))) = 'IOC' BEGIN SET @TTM_Manager_Identifier_Column = 'InvestmentObjectiveCd' SET @ACM_Manager_Identifier_Column = 'InvestmentObjectiveCd' END IF UPPER(LTRIM(RTRIM(@Manager_Identifier_Code))) = 'IFC' BEGIN SET @TTM_Manager_Identifier_Column = 'InvestmentOfficerCd' SET @ACM_Manager_Identifier_Column = 'InvestmentOffiverCd' END IF UPPER(LTRIM(RTRIM(@Manager_Identifier_Code))) = 'ADM' BEGIN SET @TTM_Manager_Identifier_Column = 'TMAccountAdministratorCd' SET @ACM_Manager_Identifier_Column = 'AccountAdministratorCd' END IF UPPER(LTRIM(RTRIM(@Manager_Identifier_Code))) = 'SAM' BEGIN SET @TTM_Manager_Identifier_Column = 'TMSenior_Administrator' SET @ACM_Manager_Identifier_Column = 'SeniorAdministratorCd' END IF UPPER(LTRIM(RTRIM(@Manager_Identifier_Code))) = 'ACT' BEGIN SET @TTM_Manager_Identifier_Column = 'TMAccountant' SET @ACM_Manager_Identifier_Column = 'TaxOfficerCd' END IF UPPER(LTRIM(RTRIM(@Manager_Identifier_Code))) = 'BRN' BEGIN SET @TTM_Manager_Identifier_Column = 'TMBranchCd' SET @ACM_Manager_Identifier_Column = 'BranchCd' END IF LEFT((UPPER(LTRIM(RTRIM(@Manager_Identifier_Code)))),1) = 'O' BEGIN SET @OptionalCategoryCode = RIGHT((UPPER(LTRIM(RTRIM(@Manager_Identifier_Code)))),2) SET @TTM_Manager_Identifier_Column = 'TMOptional_Class_Value' SET @ACM_Manager_Identifier_Column = 'OptionalCategoryCode' + CONVERT(VARCHAR(2), @OptionalCategoryCode) END -- Naveen: End of Getting Columns from Trust_To_Manager and AccountMaster SELECT @Nonfund_str = (SELECT 'ACT.'+NONFundAccount_Field+' '+NONFundAccount_Field_op+' '+ NONFundAccount_field_value FROM Bank_Condition_NONFund WHERE Multiple_Cond_Op IS NULL AND MasterId = @pi_intMasterId ) SET @NONfunds = ' ' SELECT @NONfunds=@NONfunds+' '+Multiple_Cond_Op+' '+'ACT.'+NONFundAccount_Field+''+NONFundAccount_Field_op +' '+NONFundAccount_field_value FROM Bank_Condition_NONfund A WHERE A.Imap_key = (SELECT Imap_key FROM Bank_Condition_NONfund B WHERE A.Imap_key = B.Imap_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @NONfunds = @NONfund_str+' '+@NONfunds SELECT @Rebalance_str = (SELECT 'ACT.'+Rebalance_Field+' '+Rebalance_Field_op+' '+Rebalance_field_value FROM Bank_Condition_Rebal WHERE multiple_cond_op IS NULL) SET @Rebal='' SELECT @Rebal=@Rebal +' '+Multiple_Cond_Op+' '+'ACT.'+Rebalance_Field+' '+Rebalance_Field_op +' '+Rebalance_field_value FROM Bank_Condition_Rebal A WHERE A.IMAP_key = (SELECT IMAP_key FROM Bank_Condition_Rebal B WHERE A.IMAP_key = B.IMAP_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @Rebal = @Rebalance_str+' '+@Rebal -- Sonal for Asset Identifier SELECT @Assetidentifier_str = (SELECT 'AST.'+Asset_Field+' '+Asset_Field_op+' '+ Asset_field_value FROM Bank_Condition_Asset WHERE Multiple_Cond_Op IS NULL AND MasterId = @pi_intMasterId) SET @Assetidentifier = ' ' SELECT @Assetidentifier = @Assetidentifier+' '+Multiple_Cond_Op+' '+'AST.'+Asset_Field+''+Asset_Field_op +' '+Asset_field_value FROM Bank_Condition_Asset A WHERE A.Imap_key = (SELECT Imap_key FROM Bank_Condition_Asset B WHERE A.Imap_key = B.Imap_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @Assetidentifier = @Assetidentifier_str+' '+@Assetidentifier -- Sonal -- Generating the Condition SET @Condition = ' ' IF @Nonfunds <> ' ' BEGIN SET @Condition = @Nonfunds END IF(@Rebal <> '' AND @Condition <> ' ') BEGIN SET @Condition = @Condition + ' AND ' +@Rebal END ELSE IF(@Rebal <> '' AND @Condition = ' ') BEGIN SET @Condition = @Rebal END SET @Condition1 = ' ' IF(@Condition <> ' ') --begin SET @Condition1 = ' AND ACT.AccountID = ACT.MainAccount ' --print 'check' --print @condition1 --print 'check' --end ELSE --begin SET @Condition1 = ' ACT.AccountID = ACT.MainAccount ' --print 'check' --print @condition1 --print 'check' --end IF(@Assetidentifier <> ' ') SET @Condition1 = @Condition1 + ' AND ('+ @Assetidentifier + ') GROUP BY ACT.MasterID, ACT.AccountID, AccountShortNm, M.ManagerName, M.ManagerID, M.AssetClassName, M.StyleName, M.StyleCode, ACT.DateOpened, ACT.MainAccount' ELSE SET @Condition1 = @Condition1 + ' GROUP BY ACT.MasterID, ACT.AccountID, AccountShortNm, M.ManagerName, M.ManagerID, M.AssetClassName, M.StyleName, M.StyleCode, ACT.DateOpened, ACT.MainAccount' SET @Query = ' ' SET @Query = 'INSERT INTO REBALANCE_TEMP(rMasterID, rMasterAccountID, rAccountID, rActualValue,rAccountName, rAssetIdentifier, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT ACT.MasterID, ACT.MainAccount AS MasterAccountID, ACT.AccountId, ISNULL(SUM(AST.CurrentMarketValueP1),0) AS ActualValue, REPLACE(AccountShortNm,''^'','' ''), M.ManagerID, M.ManagerName+'', ''+ M.StyleCode AS ManagerName, M.AssetClassName, M.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM tbl_Accounts_Imported ACT LEFT JOIN tbl_Assets_Imported AST ON AST.AccountID = ACT.AccountId AND AST.MasterID = ACT.MasterID JOIN vwManagers_IMAPOnly M ' -- This will work for SEIIAG only /* -- Commented by Naveen Jha As On 01/30/2009 SELECT @InvBoth = InvOfficerObjectiveCD FROM Bank_Master WHERE MasterId = @pi_intMasterId SELECT @InvOffierCd = InvOfficerCD FROM Bank_Master WHERE MasterId = @pi_intMasterId SELECT @InvObjectiveCd = InvObjectiveCD FROM Bank_Master WHERE MasterId = @pi_intMasterId SET @OrgString = '' SET @InvString = '' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @InvString = '' IF(@InvBoth = 1) BEGIN SET @InvString = 'ON (M.InvestmentOfficerCd = ACT.InvestmentOfficerCd AND M.InvestmentObjectiveCd = ACT.InvestmentObjectiveCd) AND ' END ELSE IF(@InvOffierCd = 1) BEGIN SET @InvString = ' ON (M.InvestmentOfficerCd = ACT.InvestmentOfficerCd) AND ' END ELSE IF(@InvObjectiveCd = 1) BEGIN SET @InvString = ' ON (M.InvestmentObjectiveCd = ACT.InvestmentObjectiveCd) AND ' END END */ SET @OrgString = '' SET @InvString = '' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @InvString = '' SET @InvString = 'ON (M.InvestmentOfficerCd = ACT.InvestmentOfficerCd AND M.InvestmentObjectiveCd = ACT.InvestmentObjectiveCd) AND ' END IF(UPPER(LTRIM(RTRIM(@OrgId))) <> 'SEIIAG') BEGIN SET @InvString = '' SET @InvString = ' ON (M.' + CONVERT(VARCHAR(50), @TTM_Manager_Identifier_Column) + ' = ACT.' + CONVERT(VARCHAR(50), @ACM_Manager_Identifier_Column) + ') AND ' END IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @OrgString = ' M.MasterID = ACT.MasterID WHERE ' END ELSE BEGIN SET @OrgString = ' M.MasterID = ' + CONVERT(VARCHAR(4), @pi_intMasterId) + ' AND M.Control_ID = ' + CONVERT(VARCHAR(4), @pi_intControlId) + ' WHERE ' END SET @Query = @Query + @InvString + @OrgString --print ('Main Account --- '+@query+ @condition + @condition1 +' -- END MAin Account---') EXEC (@Query + @Condition + @Condition1) --Load Managers data -- NON MAIN ACCOUNTS SET @Condition1 = '' IF(@Condition <> '') SET @Condition1 =' AND ACT.AccountID <> ACT.MainAccount GROUP BY ACT.MasterID, ACT.AccountID, AccountShortNm, M.ManagerName, M.ManagerID, M.AssetClassName, M.StyleName, M.StyleCode, ACT.DateOpened, ACT.MainAccount' ELSE SET @Condition1 =' ACT.AccountID <> ACT.MainAccount GROUP BY ACT.MasterID, ACT.AccountID, AccountShortNm, M.ManagerName, M.ManagerID, M.AssetClassName, M.StyleName, M.StyleCode, ACT.DateOpened, ACT.MainAccount' SET @Query = ' ' SET @Query =' INSERT INTO REBALANCE_TEMP ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAccountName, rAssetIdentifier, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT ACT.MasterID, ACT.MainAccount AS MasterAccountID, ACT.AccountId, ISNULL(SUM(AST.CurrentMarketValueP1),0) AS ActualValue, REPLACE(AccountShortNm,''^'','' ''), M.ManagerID, M.ManagerName+'', ''+M.StyleCode AS ManagerName, M.AssetClassName, M.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM tbl_Accounts_Imported ACT LEFT JOIN tbl_Assets_Imported AST ON AST.AccountID = ACT.AccountId AND AST.MasterID = ACT.MasterID JOIN vwManagers_IMAPOnly M ' SET @Query = @Query + @InvString + @OrgString --print 'NONMAINACCOUNTS -- ' + (@query+@condition +@condition1) EXEC (@Query + @Condition + @Condition1) --Load Funds; Fetch Fund Condition SELECT @Fund_str = (SELECT 'ACT.' + FundAccount_Field + ' '+FundAccount_Field_op+' '+FundAccount_field_value FROM Bank_Condition_Fund WHERE Multiple_Cond_Op IS NULL AND MasterId = @pi_intMasterId) SET @Funds = '' SELECT @Funds = @Funds+ ' '+ Multiple_Cond_Op +' '+'ACT.'+FundAccount_Field+' '+FundAccount_Field_op +' '+FundAccount_field_value FROM Bank_Condition_Fund A WHERE A.Imap_key = (SELECT Imap_key FROM Bank_Condition_Fund B WHERE A.Imap_key = B.Imap_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @Funds = @Fund_str + ' ' + @Funds --SET @Funds = @Fund_str + ' ' + @Funds SET @Condition = '' IF @Funds<>'' BEGIN SET @Condition = @Condition + @Funds END --print 'debug'+@condition IF @Rebal <> '' AND @Condition <> '' BEGIN SET @Condition = @Condition +' AND '+@Rebal END ELSE IF @Rebal <> '' AND @Condition = '' BEGIN SET @Condition = @rebal END --print 'debug'+@condition SET @Condition1 = '' SET @Query = '' SET @Query = 'INSERT INTO REBALANCE_TEMP ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAccountName, rAssetIdentifier, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT ACT.MasterID, ACT.MainAccount as MasterAccountID, ACT.AccountId, (AST.CurrentMarketValueP1) AS ActualValue, REPLACE(AccountShortNm,''^'','' ''), F.CUSIPID, F.FundName, F.AssetClassName, F.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM tbl_Assets_Imported AST JOIN tbl_Accounts_Imported ACT ON AST.AccountID = ACT.AccountId AND AST.MasterID = ACT.MasterID JOIN vwFunds F ON F.CusipID = AST.AssetIdentifier WHERE ' --Load Funds in PortfolioModels but with no Balance in Trust SET @Condition1 = '' SET @Condition1 = ' NOT EXISTS (SELECT * FROM REBALANCE_TEMP r WHERE rMasterID = ACT.MasterID AND ACT.AccountID = rAccountID AND rAssetIdentifier = F.CusipID) ' IF @Condition <> '' BEGIN --print ' Load Funds ---' + (@query+ @condition ) EXEC (@Query + @Condition) END ELSE BEGIN --print ' Load Funds ---' + (@query ) EXEC (@Query) END SET @Query = '' SET @Query = 'INSERT INTO REBALANCE_TEMP ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAccountName, rAssetIdentifier, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT PM.MasterID, PM.MasterAccountID, PM.AccountId, CAST(0 AS NUMERIC(14,2)) AS ActualValue, REPLACE(AccountShortNm,''^'','' ''), F.CUSIPID, F.FundName, F.AssetClassName, F.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM ' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') SET @Query = @Query + 'vwPortfoliomodels PM ' ELSE SET @Query = @Query + 'vwPortfoliomodels_NonSEI PM ' SET @Query = @Query + ' JOIN tbl_accounts_imported ACT ON ACT.MasterID = PM.MasterID AND ACT.AccountID = PM.AccountID JOIN vwFunds F ON F.CUSIPID = PM.MANAGERID WHERE ' IF @Condition <> '' BEGIN --print ' Load Funds from View ---' + (@query+ @condition + ' and ' + @condition1) EXEC (@Query + @Condition + ' AND ' + @Condition1 ) END ELSE BEGIN --print ' Load Funds from View ---' + (@query+ @condition ) EXEC (@Query + @Condition1) END -- Get Regular Managers from portfolio model Target percentage SET @Query = 'SELECT MainAccount INTO #tempAccount FROM tbl_Accounts_Imported ACT WHERE ACT.AccountID = ACT.MainAccount AND (' + @Rebal + '); ' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @Query = @Query + ' INSERT INTO Rebalance_Temp( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAssetIdentifier, rAccountName, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT PM.MasterID, PM.MasterAccountId AS MasterAccountID, PM.AccountId, (0) AS ActualValue, M.ManagerID, REPLACE(IsNull(ACT.AccountShortNm,''''),''^'','' '') AS AccountName, M.ManagerName+'', ''+M.StyleCode AS ManagerName, M.AssetClassName, M.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM vwPortfolioModels PM JOIN vwManagers_IMAPOnly M On M.MasterID = ''370'' AND M.MANAGERID = PM.ManagerID JOIN #tempAccount ON #tempAccount.MainAccount = PM.MasterAccountId Collate SQL_Latin1_General_CP1_CI_AS LEFT JOIN tbl_Accounts_Imported ACT ON PM.MasterID = ACT.MasterID AND ACT.AccountId = PM.AccountId WHERE PM.FundInd=''0'' AND PM.IMAInd = ''0'' AND NOT EXISTS (SELECT * FROM Rebalance_Temp r WHERE rMasterID = PM.MasterID AND PM.MasterAccountID = rMasterAccountID AND r.rAssetIdentifier = CAST(PM.ManagerId AS VARCHAR)) ' SET @Query = @Query + '; DROP TABLE #tempAccount' EXEC (@Query) END ELSE BEGIN SET @Query = @Query + 'INSERT INTO Rebalance_Temp ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAssetIdentifier, rAccountName, rSubAccountName, rAssetClass, rStyle, rDateOpened) select PM.MasterID, PM.MasterAccountId AS MasterAccountID, PM.AccountId, (0) AS ActualValue, M.ManagerID, REPLACE(ISNULL(ACT.AccountShortNm,''''),''^'','' '') AS AccountName, M.ManagerName+'', ''+M.StyleCode AS ManagerName, M.AssetClassName, M.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM vwPortfolioModels_NonSei PM JOIN vwManagers_IMAPOnly M ON M.MasterID = ' + CONVERT(VARCHAR(4), @pi_intMasterId) + ' AND M.Control_ID = ' + CONVERT(VARCHAR(4), @pi_intControlId) + ' AND M.MANAGERID = PM.ManagerID JOIN #tempAccount ON #tempAccount.MainAccount = PM.MasterAccountId Collate SQL_Latin1_General_CP1_CI_AS LEFT JOIN tbl_Accounts_Imported ACT ON PM.MasterID = ACT.MasterID AND ACT.AccountId = PM.AccountId WHERE PM.FundInd=''0'' AND PM.IMAInd = ''0'' AND NOT EXISTS (SELECT * FROM Rebalance_Temp r WHERE rMasterID = PM.MasterID AND PM.MasterAccountID = rMasterAccountID AND r.rAssetIdentifier = CAST(PM.ManagerId AS VARCHAR)) ' SET @Query = @Query + '; DROP TABLE #tempAccount' EXEC (@Query) END -- Get all IMAP Manager Entry from Portfolio Model Target Percentage SET @Query = 'SELECT ACT.MainAccount INTO #tempAccount FROM tbl_Accounts_Imported ACT WHERE ACT.AccountID = ACT.MainAccount AND (' + @Rebal + ');' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @Query = @Query + ' INSERT INTO Rebalance_Temp ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAssetIdentifier, rAccountName, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT DISTINCT PM.MasterID, PM.MasterAccountId AS MasterAccountID, PM.AccountId, (0) AS ActualValue, M.ManagerID, REPLACE(ISNULL(ACT.AccountShortNm,''''),''^'','' '') AS AccountName, M.ManagerName+'', ''+M.StyleCode AS ManagerName, M.AssetClassName, M.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM vwPortfolioModels PM JOIN vwManagers_IMAPOnly M ON M.MasterID = ''370'' AND M.MANAGERID = 27 JOIN #tempAccount ON #tempAccount.MainAccount = PM.MasterAccountId Collate SQL_Latin1_General_CP1_CI_AS LEFT JOIN tbl_Accounts_Imported ACT ON PM.MasterID = ACT.MasterID AND ACT.AccountId = PM.AccountId WHERE PM.FundInd=''0'' AND PM.IMAInd = ''1'' AND NOT EXISTS (SELECT * FROM Rebalance_Temp r WHERE rMasterID = PM.MasterID AND PM.AccountID = rAccountID AND r.rAssetIdentifier = ''27'') ' SET @Query = @Query + '; DROP TABLE #tempAccount' EXEC (@Query) END ELSE BEGIN SET @Query = @Query + 'INSERT INTO Rebalance_Temp ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAssetIdentifier, rAccountName, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT Distinct PM.MasterID, PM.MasterAccountId AS MasterAccountID, PM.AccountId, (0) AS ActualValue, M.ManagerID, REPLACE(ISNULL(ACT.AccountShortNm,''''),''^'','' '') AS AccountName, M.ManagerName+'', ''+M.StyleCode as ManagerName, M.AssetClassName, M.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM vwPortfolioModels_NonSEI PM JOIN vwManagers_IMAPOnly M ON M.MasterID = ' + CONVERT(VARCHAR(4), @pi_intMasterId) + ' AND M.Control_ID = ' + CONVERT(VARCHAR(4), @pi_intControlId) + ' AND M.MANAGERID = 27 JOIN #tempAccount ON #tempAccount.MainAccount = PM.MasterAccountId Collate SQL_Latin1_General_CP1_CI_AS LEFT JOIN tbl_Accounts_Imported ACT ON PM.MasterID = ACT.MasterID AND ACT.AccountId = PM.AccountId WHERE PM.FundInd=''0'' AND PM.IMAInd = ''1'' AND NOT EXISTS (SELECT * FROM Rebalance_Temp r WHERE rMasterID = PM.MasterID AND PM.AccountID = rAccountID AND r.rAssetIdentifier = ''27'') ' SET @Query = @Query + '; DROP TABLE #tempAccount' EXEC (@Query) END -- Get all Funds from Portfolio Model Target Percentage SET @Query = 'SELECT ACT.MainAccount INTO #tempAccount FROM tbl_Accounts_Imported ACT WHERE ACT.AccountID = ACT.MainAccount AND (' + @Rebal + ');' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @Query = @Query + ' INSERT INTO Rebalance_Temp ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAssetIdentifier, rAccountName, rSubAccountName, rAssetClass, rStyle, rDateOpened) select PM.MasterID, PM.MasterAccountId AS MasterAccountID, PM.AccountId, (0) AS ActualValue, F.CusipID, REPLACE(ISNULL(ACT.AccountShortNm,'' ''),''^'','' '') AS AccountName, F.FundName AS ManagerName, F.AssetClassName, F.StyleName, CAST(ACT.DateOpened AS DATETIME) FROM vwPortfolioModels PM JOIN vwFunds F ON F.CusipID = PM.ManagerID JOIN #tempAccount ON #tempAccount.MainAccount = PM.MasterAccountId Collate SQL_Latin1_General_CP1_CI_AS LEFT JOIN tbl_Accounts_Imported ACT ON PM.MasterID = ACT.MasterID AND ACT.AccountId = PM.AccountId WHERE PM.FundInd=''1'' AND NOT EXISTS (SELECT * FROM Rebalance_Temp r WHERE rMasterID = PM.MasterID AND PM.MasterAccountID = rMasterAccountID AND r.rAssetIdentifier = CAST(PM.ManagerId AS VARCHAR)) ' SET @Query = @Query + '; DROP TABLE #tempAccount' EXEC (@Query) END ELSE BEGIN SET @Query = @Query + ' INSERT INTO Rebalance_Temp ( rMasterID, rMasterAccountID, rAccountID, rActualValue, rAssetIdentifier, rAccountName, rSubAccountName, rAssetClass, rStyle, rDateOpened) SELECT PM.MasterID, PM.MasterAccountId AS MasterAccountID, PM.AccountId, (0) AS ActualValue, F.CusipID, REPLACE(ISNULL(ACT.AccountShortNm,''''),''^'','' '') AS AccountName, F.FundName AS ManagerName, F.AssetClassName, F.StyleName, CAST(ACT.DateOpened as datetime) FROM vwPortfolioModels_NonSEI PM JOIN vwFunds F ON F.CusipID = PM.ManagerID JOIN #tempAccount ON #tempAccount.MainAccount = PM.MasterAccountId Collate SQL_Latin1_General_CP1_CI_AS LEFT JOIN tbl_Accounts_Imported ACT ON PM.MasterID = ACT.MasterID AND ACT.AccountId = PM.AccountId WHERE PM.FundInd=''1'' AND NOT EXISTS (SELECT * FROM Rebalance_Temp r WHERE rMasterID = PM.MasterID AND PM.MasterAccountID = rMasterAccountID AND r.rAssetIdentifier = CAST(PM.ManagerId AS VARCHAR)) ' SET @Query = @Query + '; DROP TABLE #tempAccount' EXEC (@Query) END UPDATE REBALANCE_TEMP SET rTargetPercentage = 0, rTargetValue = 0, rActualPercentage =0, rUnmanagedFunds = 0, rMainAcctCash=0, rTaxable = 1, rPersonalTrust=0 -- Find Account Types -- Fetch rTaxable Condition SET @NonTaxable_str = '' SELECT @NonTaxable_str = (SELECT 'ACT.'+nontaxable_Field+' '+nontaxable_Field_op+' '+nontaxable_field_value FROM Bank_Condition_NonTaxable WHERE multiple_cond_op IS NULL AND MasterId = @pi_intMasterId) -- print @rtaxable_str SET @NonTaxable = ' ' SELECT @NonTaxable = @NonTaxable+' '+Multiple_Cond_Op+' '+'ACT.'+nontaxable_Field+' '+nontaxable_Field_op +' '+nontaxable_field_value FROM Bank_Condition_NonTaxable A WHERE A.Imap_key = (SELECT Imap_key FROM Bank_Condition_Nontaxable B WHERE A.Imap_key = B.Imap_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @nontaxable=@nontaxable_str+' '+@nontaxable -- print @nontaxable SET @Query = ' ' SET @Query = 'UPDATE REBALANCE_TEMP SET rTaxable = 0 FROM tbl_accounts_imported ACT WHERE ACT.MasterID = rMasterID AND ACT.AccountID = rAccountID AND ' -- ACT.OptionalCategoryCode13 = 2 -- print ' NonTaxable' + (@query+@nontaxable) EXEC (@Query + @Nontaxable) -- Fetch rPersonalTrust Condition SET @rPersonalTrust_str = '' SELECT @rPersonalTrust_str = (SELECT 'ACT.'+rPersonalTrust_Field+' '+rPersonalTrust_Field_op+' '+ rPersonalTrust_field_value FROM Bank_Condition_rPersonalTrust WHERE Multiple_Cond_Op IS NULL AND MasterId = @pi_intMasterId) -- print @rtaxable_str SET @rPersonalTrust = '' SELECT @rPersonalTrust=@rPersonalTrust+' '+Multiple_Cond_Op+' '+'ACT.'+rPersonalTrust_Field+' '+rPersonalTrust_Field_op +' '+rPersonalTrust_field_value FROM Bank_Condition_rPersonalTrust A WHERE A.Imap_key = (SELECT Imap_key FROM Bank_Condition_rPersonalTrust B WHERE A.Imap_key = B.Imap_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @rPersonalTrust = @rPersonalTrust_str+' '+@rPersonalTrust SET @Query = ' ' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @Query = 'UPDATE REBALANCE_TEMP SET rPersonalTrust = 1 FROM AccountMaster_0370 ACT WHERE ACT.MasterID = rMasterID AND ACT.AccountID = rAccountID AND ' END ELSE BEGIN SET @Query = 'UPDATE REBALANCE_TEMP SET rPersonalTrust = 1 FROM AccountMaster ACT WHERE ACT.MasterID = rMasterID AND ACT.AccountID = rAccountID AND ' END -- ACT.CapacityCd in (3,4,5,6,21) -- print ' Personal Trust --' + (@query+ @rPersonalTrust) EXEC (@Query + @rPersonalTrust) -- Populate target % UPDATE REBALANCE_TEMP SET rTargetPercentage = Percentage FROM PortfolioModels PM JOIN PortfolioModelDetail PMD ON PM.ModelID = PMD.ModelID WHERE PM.MasterID = rMasterID AND PM.AccountID = rMasterAccountID AND PMD.ManagerID = rAssetIdentifier AND PM.ActiveInd = 1 AND PMD.ActiveInd =1 AND IMaInd = 0 -- Populate Parametric % SET @Query = 'SELECT MasterID,' IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN SET @Query = @Query + 'AccountID AS AccountID, SUM(Percentage) AS TargetPercentage INTO #ParametricPercentage FROM vwPortfoliomodels GROUP BY MasterID, AccountID, IMAInd HAVING IMaInd = 1 ;' END ELSE BEGIN SET @Query = @Query + 'AccountID AS AccountID, SUM(Percentage) AS TargetPercentage INTO #ParametricPercentage FROM vwPortfoliomodels_NonSEI GROUP BY MasterID, AccountID, IMAInd HAVING IMaInd = 1 ;' END SET @Query = @Query + ' UPDATE REBALANCE_TEMP SET rTargetPercentage = TargetPercentage FROM #ParametricPercentage PP WHERE PP.MasterID = rMasterID AND PP.AccountID = rAccountID Collate SQL_Latin1_General_CP1_CI_AS ; DROP TABLE #ParametricPercentage ' -- print 'Parametic Percentage ' + (@query) EXEC (@Query) -- Calculate Total Value SELECT rMasterID AS MasterID, rMasterAccountID AS MasterAccountID, SUM(ISNULL(rActualValue,0)) AS SumActualValue INTO #TempAccount FROM REBALANCE_TEMP GROUP BY rMasterID, rMasterAccountID -- Update Total Value UPDATE REBALANCE_TEMP SET rTotalValue = SumActualValue FROM #TempAccount WHERE MasterID = rMasterID AND MasterAccountID = rMasterAccountID DROP TABLE #TempAccount -- Calculate Main Account Cash SELECT rMasterID AS MasterID, rMasterAccountID AS MasterAccountID, SUM(ISNULL(rActualValue,0)) AS SumActualValue INTO #TempAccount1 FROM REBALANCE_TEMP WHERE -- rStyle in ('Cash','Main') rMasterAccountID = rAccountID COLLATE SQL_Latin1_General_CP1_CI_AS-- Just Main Accounts GROUP BY rMasterID, rMasterAccountID -- Update Main Acct Cash UPDATE REBALANCE_TEMP SET rMainAcctCash = SumActualValue FROM #TempAccount1 WHERE MasterID = rMasterID AND MasterAccountID = rMasterAccountID COLLATE SQL_Latin1_General_CP1_CI_AS DROP TABLE #TempAccount1 -- Sum Managed Funds -- Fetch non_custody condition SELECT @NonCustody_str = (SELECT NONCustodyAccount_Field+' '+NONCustodyAccount_Field_op+' '+ NONCustodyAccount_field_value FROM Bank_Condition_NONCustody WHERE Multiple_Cond_Op IS NULL AND MasterId = @pi_intMasterId) SET @NONCustody = '' SELECT @NONCustody = @NONCustody+' '+Multiple_Cond_Op+' '+NONCustodyAccount_Field+' '+NONCustodyAccount_Field_op +' '+NONCustodyAccount_field_value FROM Bank_Condition_NONCustody A WHERE A.Imap_key = (SELECT Imap_key FROM Bank_Condition_NONCustody B WHERE A.Imap_key = B.Imap_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @NONCustody = @NONCustody_str+' '+@NONCustody SET @Condition = '' IF @NonCustody<>'' BEGIN SET @Condition = ' AND ' + @NonCustody END SET @Condition1 = '' SET @Condition1 = ' GROUP BY rMasterID, rMasterAccountID' SET @Query = '' CREATE TABLE #TempAccount2( MasterID INTEGER, MasterAccountID VARCHAR(12), SumActualValue NUMERIC(14,2) ) SET @Query = 'SELECT rMasterID AS MasterID, rMasterAccountID AS MasterAccountID, SUM(ISNULL(rActualValue,0)) AS SumActualValue INTO #TempAccount2 FROM REBALANCE_TEMP JOIN tbl_Accounts_Imported ON rAccountID = AccountId COLLATE SQL_Latin1_General_CP1_CI_AS WHERE rMasterAccountID <> rAccountID COLLATE SQL_Latin1_General_CP1_CI_AS' -- Update Managed Funds SET @QueryUpdateRebalTemp = ';UPDATE REBALANCE_TEMP SET rManagerFunds = SumActualValue FROM #TempAccount2 WHERE MasterID = rMasterID AND MasterAccountID = rMasterAccountID COLLATE SQL_Latin1_General_CP1_CI_AS; DROP TABLE #TempAccount2 ' -- print 'Manage ' + (@query+@condition +@condition1+@queryUpdateRebalTemp) EXEC (@Query + @Condition + @Condition1 + @QueryUpdateRebalTemp) -- Sum UnManaged Funds -- Jags -- Fetch Condition for Custody Accounts SELECT @Custody_str = (SELECT Unmanaged_Field+' '+Unmanaged_Field_op+' '+ Unmanaged_field_value FROM Bank_Condition_UnManaged WHERE Multiple_Cond_Op IS NULL AND MasterId = @pi_intMasterId) SET @Custody = '' SELECT @Custody = @Custody+ ' '+Multiple_Cond_Op+' '+Unmanaged_Field+' '+Unmanaged_Field_op +' '+Unmanaged_field_value FROM Bank_Condition_UnManaged A WHERE A.Imap_key = (SELECT Imap_key FROM Bank_Condition_UnManaged B WHERE A.Imap_key = B.Imap_key AND Multiple_Cond_Op <>'' AND MasterId = @pi_intMasterId) SELECT @Custody = @Custody_str+' '+@Custody SET @Query = ' ' CREATE TABLE #TempAccount3( MasterID INTEGER, MasterAccountID VARCHAR(12), SumActualValue NUMERIC(14,2) ) SET @Query = 'SELECT rMasterID AS MasterID, rMasterAccountID AS MasterAccountID, SUM(ISNULL(rActualValue,0)) AS SumActualValue INTO #TempAccount3 FROM REBALANCE_TEMP JOIN tbl_Accounts_Imported ON rAccountID = AccountId COLLATE SQL_Latin1_General_CP1_CI_AS ' SET @Condition1 = '' SET @Condition1 = ' GROUP BY rMasterID, rMasterAccountID' SET @Condition = '' IF @Custody <> '' BEGIN SET @Condition = ' WHERE ' + @Custody END -- print 'Unmang' + (@query+ @condition+@condition1) EXEC (@Query + @Condition + @Condition1) -- Update UnManaged Funds SET @QueryUpdateRebalTemp = ';UPDATE REBALANCE_TEMP SET rUnmanagedFunds = SumActualValue FROM #TempAccount3 WHERE MasterID = rMasterID AND MasterAccountID = rMasterAccountID COLLATE SQL_Latin1_General_CP1_CI_AS; DROP TABLE #TempAccount3 ' EXEC (@Query + @Condition + @Condition1 + @QueryUpdateRebalTemp) -- Calculate Actual % UPDATE REBALANCE_TEMP SET rActualPercentage = rActualValue / (rTotalValue - rUnmanagedFunds ) * 100 WHERE (rTotalValue - rUnmanagedFunds ) > 0 AND (rTotalValue - rUnmanagedFunds ) >= rActualValue -- Calculate Target % UPDATE REBALANCE_TEMP set rTargetValue = (rTotalValue - rUnmanagedFunds ) * rTargetPercentage/100 UPDATE REBALANCE_TEMP SET rDifferencePercentage = (rTargetPercentage - rActualPercentage), rDifferenceValue = rActualValue - rTargetValue -- Take Closed Accounts out --1 IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN DELETE REBALANCE_TEMP WHERE rAccountID IN (SELECT AccountID FROM dbo.user_tbl_New_Closed_Accounts) END ELSE BEGIN DELETE REBALANCE_TEMP WHERE rAccountID IN (SELECT Account_ID FROM dbo.user_tbl_Closed_Accounts) END --2 DELETE REBALANCE_TEMP WHERE rAccountName LIKE '%CLOSED%' -- Take Custody accounts out DELETE REBALANCE_TEMP WHERE rSubAccountName = 'Custody Account, OC' -- No Zero total account UPDATE REBALANCE_TEMP SET rTotalValue = 0.01 WHERE rTotalValue = 0 -- It gets tricky here -- On a Quarter First day just insert records -- On a normal day delete all records created after the Quarter start date -- This is to have two type of records 1. Today 2. Quarter First day (and previous QuarterFirst day records) IF CAST(CONVERT(CHAR,GETDATE(),101) AS DATETIME) <> dbo.QuarterStartDate(GETDATE()) BEGIN DELETE REBALANCE WHERE REBALANCEDATE > dbo.QuarterStartDate(GETDATE()) END IF(UPPER(LTRIM(RTRIM(@OrgId))) = 'SEIIAG') BEGIN INSERT INTO [REBALANCE]( [RebalanceDate], [MASTERID], [MasterAccountId], [DateOpened], [AccountId], [AccountName],[AssetIdentifier], [SubAccountName],[AssetClass], [Style], [TotalValue],[MainAcctCash],[UnmanagedFunds], [ManagerFunds], [ActualPercentage], [ActualValue], [TargetPercentage], [TargetValue], [DifferencePercentage], [DifferenceValue],[Taxable], [PersonalTrust], [ActiveInd], [LastUpdatedBy],[LastUpdateDt] ) SELECT CAST(CONVERT(CHAR,GETDATE(),101) AS DATETIME), [rMASTERID],[rMasterAccountId],[rDateOpened],[rAccountId], [rAccountName], [rAssetIdentifier], [rSubAccountName], [rAssetClass], [rStyle],[rTotalValue], [rMainAcctCash],[rUnmanagedFunds], [rManagerFunds], [rActualPercentage], [rActualValue], [rTargetPercentage],[rTargetValue], [rDifferencePercentage], [rDifferenceValue], [rTaxable], [rPersonalTrust], 1, 'DataLoad', GETDATE() FROM REBALANCE_TEMP WHERE rMasterAccountId NOT IN (SELECT rMasterAccountId FROM REBALANCE_TEMP WHERE rMasterAccountId = rAccountId and rDateOpened > dbo.QuarterStartDate(dbo.QuarterStartDate(GETDATE())-1)) END ELSE BEGIN INSERT INTO [REBALANCE]( [RebalanceDate], [MASTERID], [MasterAccountId], [DateOpened], [AccountId], [AccountName],[AssetIdentifier], [SubAccountName],[AssetClass], [Style], [TotalValue],[MainAcctCash],[UnmanagedFunds], [ManagerFunds], [ActualPercentage], [ActualValue], [TargetPercentage], [TargetValue], [DifferencePercentage], [DifferenceValue],[Taxable], [PersonalTrust], [ActiveInd], [LastUpdatedBy],[LastUpdateDt] ) SELECT CAST(CONVERT(CHAR,GETDATE(),101) AS DATETIME), [rMASTERID],[rMasterAccountId],[rDateOpened],[rAccountId], [rAccountName], [rAssetIdentifier], [rSubAccountName], [rAssetClass], [rStyle],[rTotalValue], [rMainAcctCash],[rUnmanagedFunds], [rManagerFunds], [rActualPercentage], [rActualValue], [rTargetPercentage],[rTargetValue], [rDifferencePercentage], [rDifferenceValue], [rTaxable], [rPersonalTrust], 1, 'DataLoad', GETDATE() FROM REBALANCE_TEMP END