﻿CREATE procedure [C6MartPeriodico].[POPOLA_S132ProfiloRischioAdeguatezza]  
AS  
BEGIN  
--------------------------------------------------------  
--*****************************************************  
-- Replat della Sp  PL_MP_S132ProfiloRischioAdeguatezza  
-- Attenzione : la insert sulla tabella TB_S132ProfiloRischioAdeguatezza inserisci su alcuni campi valori   
--    che successivamente sono aggiornati con un UPD ( vedi fine SP )  
--*****************************************************  
 SET NOCOUNT ON;  
truncate table  C6MartPeriodico.TB_S132ProfiloRischioAdeguatezza  
insert into C6MartPeriodico.TB_S132ProfiloRischioAdeguatezza  
    SELECT distinct  
    PATRBF.RETE,  
    PATRBF.COD_FISCALE,  
    PATRBF.INSTITUTENAME,  
    CTV,  
    M.PROFILO_ASS AS CODICEPROFILO,  
--CONVERT(DATETIME,M.DATA_INIZIO_VAL) AS  DATAINIZIOVALIDITA,  
 DBO.TOSHORTDATESTRING(M.DATA_INIZIO_VAL) AS  DATAINIZIOVALIDITA,  
    CR.MAX_VAR AS  VARMAX,  
 ProfiliDiRischio.NOMEPROFILO as NOMEPROFILO,  
 RISK_AGG.VAR_PERC_PTF,-- VAR,  
 null  VARSTRING,  
    RISK_AGG.CREDITRISK  RISKCLASS,  
         CASE   
  WHEN ISNULL(RISK_AGG.COPERTURA,0.00) = 0.00 THEN 'n.a.'   
        WHEN RISK_AGG.creditrisk is null THEN 'n.a.'   
        WHEN RISK_AGG.creditrisk = 0.00 THEN 'n.a.'   
  ELSE NULL   
 END AS  RISKCLASSSTRING,  
 ISNULL(RISK_AGG.COPERTURA,0.00)  COVERAGE,  
 NULL AS COVERAGESTRING,  
 --V changed from string value (descrizione) to the numeric (profilo)  
    M.RISKCLASS AS RISKCLASSMAX,  
    (SUM(CTV) OVER (PARTITION BY PATRBF.COD_FISCALE,PATRBF.RETE, PATRBF.INSTITUTENAME) /   
    -- 16.07.2011 COLAIANNI-CONSOLI GESTIONE DIVISIONE PER ZERO  
    CASE WHEN ( SUM(CTV) OVER (PARTITION BY PATRBF.RETE, PATRBF.COD_FISCALE) ) = 0  
         THEN 1  
         ELSE SUM(CTV) OVER (PARTITION BY PATRBF.RETE, PATRBF.COD_FISCALE)  
    END )  
    *100 AS PERCENTAGE,  
    case when RISK_AGG.VAR_PERC_PTF > CR.MAX_VAR then 0 else 1 end as VarIsOk,  
    case when isnull(RISK_AGG.CREDITRISK,0) > Ck.PROFILO then 0 else 1 end as RiskClassIsOk,  
    CASE WHEN CMP.COMPLESSITA > M.EXPERIENCE THEN 0 ELSE 1 END  as ComplexityIsOk,  
    RISK_AGG.Ully_perc  
    FROM  
    (  
    select   
    PATRBF.RETE,  
    PATRBF.COD_FISCALE,  
    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio Fideuram' WHEN PATRBF.RETE='S' THEN  'Patrimonio Sanpaolo Invest' ELSE 'Patrimonio IW Private Inv.' END AS INSTITUTENAME,  
    SUM(PATRBF.CTV) AS CTV  
    from  
    c6martperiodico.patrimonio_bf  PATRBF  
    where   
    1=1   
   -- and cod_fiscale = @codiceFiscale  
  --  and rete= @rete  
    GROUP BY   
    PATRBF.RETE,  
    PATRBF.COD_FISCALE,  
    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio Fideuram' WHEN PATRBF.RETE='S' THEN  'Patrimonio Sanpaolo Invest' ELSE 'Patrimonio IW Private Inv.' END  
    ) PATRBF  
    LEFT OUTER JOIN   
    c6martperiodico.MIFID AS M  
    ON   
    PATRBF.COD_FISCALE=M.COD_FISCALE  
    AND PATRBF.RETE=M.RETE  
 LEFT OUTER JOIN c6martperiodico.CODIFICA_RISCHIO AS CR  
 ON M.PROFILO_ASS = CR.PROFILO  
    LEFT OUTER JOIN   
    c6martperiodico.CODIFICA_CREDITRISK  CK  
    ON   
    M.riskclass=CK.PROFILO  
    LEFT OUTER JOIN   
    c6martperiodico.RISCHIO_AGGREGATO  RISK_AGG  
    ON   
    PATRBF.COD_FISCALE=RISK_AGG.COD_FISCALE  
    AND PATRBF.RETE=RISK_AGG.RETE  
    left outer join C6MartPeriodico.COMPLESSITA cmp  
    on   
    CMP.COD_FISCALE=PATRBF.COD_FISCALE  
    AND  
    CMP.RETE=PATRBF.RETE  
INNER JOIN DBO.ProfiliDiRischio AS ProfiliDiRischio  
  ON M.PROFILO_ASS = ProfiliDiRischio.CODICEPROFILO  
   where  
    (  
      RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF'   
    )   
update s132  
 set var  = ( CASE  
   WHEN (nocc - CC = 0 ) THEN 0.00  
   else var  
  END ),  
  COVERAGE = ( CASE  
   WHEN (nocc - CC = 0 ) THEN 100  
   else COVERAGE  
  END )  
  --,  
  --VARSTRING=( CASE  
  -- WHEN VARSTRING = 0.00 AND (nocc - CC <> 0 ) THEN 'n.c.'   
  --END )    
 from c6martperiodico.TB_S132ProfiloRischioAdeguatezza s132  
 inner join (  
 select rete,cod_fiscale  
 ,sum ( case when tipo_prodotto ='CC' then tot else 0 end ) as cc   
 ,sum ( tot ) as nocc   
 from   
 ( select rete,cod_fiscale,tipo_prodotto,count(*) tot  
   from c6martperiodico.patrimonio_Bf  
   --where cod_fiscale = '00082850397' and rete = 'F'  
   group by rete,cod_fiscale,tipo_prodotto  
 ) as gr  
 group by rete,cod_fiscale  
 ) as tabCount  
     on tabcount.rete= s132.rete and tabcount.cod_fiscale=s132.cod_fiscale  
END  
--USE [C6StampeCentralizzate]
--GO
--/****** Object:  StoredProcedure [C6MartPeriodico].[POPOLA_S132ProfiloRischioAdeguatezza]    Script Date: 10/02/2022 16:41:23 ******/
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--ALTER procedure [C6MartPeriodico].[POPOLA_S132ProfiloRischioAdeguatezza]  
--AS  
--BEGIN  
----------------------------------------------------------  
----*****************************************************  
---- Replat della Sp  PL_MP_S132ProfiloRischioAdeguatezza  
---- Attenzione : la insert sulla tabella TB_S132ProfiloRischioAdeguatezza inserisci su alcuni campi valori   
----    che successivamente sono aggiornati con un UPD ( vedi fine SP )  
----*****************************************************  
-- SET NOCOUNT ON;  
--truncate table  C6MartPeriodico.TB_S132ProfiloRischioAdeguatezza  
--insert into C6MartPeriodico.TB_S132ProfiloRischioAdeguatezza  
--    SELECT   
--    PATRBF.RETE,  
--    PATRBF.COD_FISCALE,  
--    PATRBF.INSTITUTENAME,  
--    CTV,  
--    M.PROFILO_ASS AS CODICEPROFILO,  
----CONVERT(DATETIME,M.DATA_INIZIO_VAL) AS  DATAINIZIOVALIDITA,  
-- DBO.TOSHORTDATESTRING(M.DATA_INIZIO_VAL) AS  DATAINIZIOVALIDITA,  
--    CR.MAX_VAR AS  VARMAX,  
-- ProfiliDiRischio.NOMEPROFILO as NOMEPROFILO,  
-- RISK_AGG.VAR_PERC_PTF,-- VAR,  
-- null  VARSTRING,  
--    RISK_AGG.CREDITRISK  RISKCLASS,  
--         CASE   
--  WHEN ISNULL(RISK_AGG.COPERTURA,0.00) = 0.00 THEN 'n.a.'   
--        WHEN RISK_AGG.creditrisk is null THEN 'n.a.'   
--        WHEN RISK_AGG.creditrisk = 0.00 THEN 'n.a.'   
--  ELSE NULL   
-- END AS  RISKCLASSSTRING,  
-- ISNULL(RISK_AGG.COPERTURA,0.00)  COVERAGE,  
-- NULL AS COVERAGESTRING,  
-- --V changed from string value (descrizione) to the numeric (profilo)  
--    M.RISKCLASS AS RISKCLASSMAX,  
--    (SUM(CTV) OVER (PARTITION BY PATRBF.COD_FISCALE,PATRBF.RETE, PATRBF.INSTITUTENAME) /   
--    -- 16.07.2011 COLAIANNI-CONSOLI GESTIONE DIVISIONE PER ZERO  
--    CASE WHEN ( SUM(CTV) OVER (PARTITION BY PATRBF.RETE, PATRBF.COD_FISCALE) ) = 0  
--         THEN 1  
--         ELSE SUM(CTV) OVER (PARTITION BY PATRBF.RETE, PATRBF.COD_FISCALE)  
--    END )  
--    *100 AS PERCENTAGE,  
--    case when RISK_AGG.VAR_PERC_PTF > CR.MAX_VAR then 0 else 1 end as VarIsOk,  
--    case when isnull(RISK_AGG.CREDITRISK,0) > Ck.PROFILO then 0 else 1 end as RiskClassIsOk,  
--    CASE WHEN CMP.COMPLESSITA > M.EXPERIENCE THEN 0 ELSE 1 END  as ComplexityIsOk,  
--    RISK_AGG.Ully_perc  
--    FROM  
--    (  
--    select   
--    PATRBF.RETE,  
--    PATRBF.COD_FISCALE,  
--    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio Fideuram' ELSE 'Patrimonio Sanpaolo Invest' END AS INSTITUTENAME,  
--    SUM(PATRBF.CTV) AS CTV  
--    from  
--    c6martperiodico.patrimonio_bf  PATRBF  
--    where   
--    1=1   
--   -- and cod_fiscale = @codiceFiscale  
--  --  and rete= @rete  
--    GROUP BY   
--    PATRBF.RETE,  
--    PATRBF.COD_FISCALE,  
--    CASE WHEN PATRBF.RETE='F' THEN 'Patrimonio Fideuram' ELSE 'Patrimonio Sanpaolo Invest' END  
--    ) PATRBF  
--    LEFT OUTER JOIN   
--    c6martperiodico.MIFID AS M  
--    ON   
--    PATRBF.COD_FISCALE=M.COD_FISCALE  
--    AND PATRBF.RETE=M.RETE  
-- LEFT OUTER JOIN c6martperiodico.CODIFICA_RISCHIO AS CR  
-- ON M.PROFILO_ASS = CR.PROFILO  
--    LEFT OUTER JOIN   
--    c6martperiodico.CODIFICA_CREDITRISK  CK  
--    ON   
--    M.riskclass=CK.PROFILO  
--    LEFT OUTER JOIN   
--    c6martperiodico.RISCHIO_AGGREGATO  RISK_AGG  
--    ON   
--    PATRBF.COD_FISCALE=RISK_AGG.COD_FISCALE  
--    AND PATRBF.RETE=RISK_AGG.RETE  
--    left outer join C6MartPeriodico.COMPLESSITA cmp  
--    on   
--    CMP.COD_FISCALE=PATRBF.COD_FISCALE  
--    AND  
--    CMP.RETE=PATRBF.RETE  
--INNER JOIN DBO.ProfiliDiRischio AS ProfiliDiRischio  
--  ON M.PROFILO_ASS = ProfiliDiRischio.CODICEPROFILO  
--   where  
--    (  
--      RISK_AGG.COD_AGGREG= 'COMPLESSIVO|BF'   
--    )   
--update s132  
-- set var  = ( CASE  
--   WHEN (nocc - CC = 0 ) THEN 0.00  
--   else var  
--  END ),  
--  COVERAGE = ( CASE  
--   WHEN (nocc - CC = 0 ) THEN 100  
--   else COVERAGE  
--  END )  
--  --,  
--  --VARSTRING=( CASE  
--  -- WHEN VARSTRING = 0.00 AND (nocc - CC <> 0 ) THEN 'n.c.'   
--  --END )    
-- from c6martperiodico.TB_S132ProfiloRischioAdeguatezza s132  
-- inner join (  
-- select rete,cod_fiscale  
-- ,sum ( case when tipo_prodotto ='CC' then tot else 0 end ) as cc   
-- ,sum ( tot ) as nocc   
-- from   
-- ( select rete,cod_fiscale,tipo_prodotto,count(*) tot  
--   from c6martperiodico.patrimonio_Bf  
--   --where cod_fiscale = '00082850397' and rete = 'F'  
--   group by rete,cod_fiscale,tipo_prodotto  
-- ) as gr  
-- group by rete,cod_fiscale  
-- ) as tabCount  
--     on tabcount.rete= s132.rete and tabcount.cod_fiscale=s132.cod_fiscale  
--END