﻿-- Stored procedure    
-- =============================================    
-- Author:  <Author,,Name>    
-- Create date: <Create Date,,>    
-- Description: <Description,,>    
-- =============================================    
CREATE procedure [C6MartPeriodico].[DM_ANAG_CC]    
AS    
BEGIN    
 -- SET NOCOUNT ON added to prevent extra result sets from    
 -- interfering with SELECT statements.    
 SET NOCOUNT ON;    
  DECLARE @ID_ELAB INT    
 DECLARE @TIPO_ELAB VARCHAR(1)    
 SET @ID_ELAB = C6MartPeriodico.GETIDELAB()    
 SET @TIPO_ELAB = C6MartPeriodico.GETTIPOELAB()    
 -- Log esecuzione    
 INSERT INTO LOG_ESECUZIONE_PERIODICO    
  (Nome, Inizio, Nota)    
 VALUES (    
  'DM_ANAG_CC',    
  GETDATE(),    
  'Elaborazione trimestrale'    
 )    
 TRUNCATE TABLE [C6MartPeriodico].[ANAG_CC]    
 INSERT INTO [C6MartPeriodico].[ANAG_CC]    
           ([RETE]    
     ,[COD_FISCALE]    
     ,[INTERMEDIARIO]    
     ,[COINTEST]    
     ,[COD_CC]    
     ,[FASCIA_CPP]    
     ,[SALDOCONTABILE]    
     ,[SALDODISPONIBILE]    
     ,[DATA_APERTURA]    
     ,[DATA_CHIUSURA]    
     ,[ID_ELAB]    
     ,[TIPO_ELAB])    
 SELECT     
  CONTR_SINTESI.RETE AS RETE,    
  CONTR_SINTESI.CODFIS AS COD_FISCALE,    
  'FIDEURAM' AS INTERMEDIARIO,    
  NULL AS COINTEST,    
  CONTR_SINTESI.CONTO AS COD_CC,    
  '' AS FASCIA_CPP,    
  CONTR_SINTESI.CTV AS SALDOCONTABILE,    
  CONTR_SINTESI.SALRISC AS SALDODISPONIBILE,    
  --CONTR_SINTESI.DATADISOTTOSCRIZIONE AS DATA_APERTURA,    
  CAST(CAST(CONTR_SINTESI.DTSOTTOS AS VARCHAR(8)) AS DATETIME) AS DATA_APERTURA,    
  NULL AS DATA_CHIUSURA,    
  @ID_ELAB AS ID_ELAB,    
  @TIPO_ELAB AS TIPO_ELAB    
 FROM     
  C6StagingPeriodico.SPB_CONTR_SINTESI AS CONTR_SINTESI    
 WHERE     
  CONTR_SINTESI.TIPPROD = 'Cc'  
  -- FerAcu 20240116 risolvere duplicazioni  
    and not (CODFIS = 'NFRDNL84L30D612S' and CONTO = '00166487247' and RETE = 'F' and CODINT = 'VP')  
    and not (CODFIS = 'CRLNTN57R17G273H' and CONTO = '00167278823' and RETE = 'F' and CODINT = '91')    
    and not (CODFIS = 'CNOGRG38D23A271O' and CONTO = '00166129502' and RETE = 'F' and CODINT = '91')  
    and not (CODFIS = 'NDRGNN42L59D704K' and CONTO = '00166139788' and RETE = 'F' and CODINT = '91')  
    and not (CODFIS = 'FRRGRL57M24B885X' and CONTO = '00166050608' and RETE = 'F' and CODINT = '91')  
    and not (CODFIS = 'LNRFRC79R18F401E' and CONTO = '00167389552' and RETE = 'F' and CODINT = '91')  
    and not (CODFIS = 'VIEPRZ64D63D612I' and CONTO = '00166403317' and RETE = 'F' and CODINT = '91')  
 -- Log esecuzione    
 UPDATE    
  LOG_ESECUZIONE_PERIODICO    
 SET    
  Fine = GETDATE(),    
  Tipo = 'INSERT',    
  Righe = @@ROWCOUNT    
 WHERE    
  Nome = 'DM_ANAG_CC' AND    
  Inizio = (    
   SELECT    
    MAX(Inizio)    
   FROM    
    LOG_ESECUZIONE_PERIODICO    
   WHERE    
    Nome = 'DM_ANAG_CC'    
  )    
END