-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [C6Mart].[GESTIONE_INSERT_PDF_FTP] 
	@Rete char(1), 
	@CodiceFiscale varchar(16),
	@IDReport int,
	@NomeFile varchar(50),
	@TipoReport varchar(1),
	@NumeroPagine int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	--SET NOCOUNT ON;
--begin tran
    -- Insert statements for procedure here
		DECLARE @ID_ELAB INT
		DECLARE @COD_AGENTE VARCHAR(6)
 		SET @ID_ELAB = C6Mart.GETIDELAB()
       SELECT   @COD_AGENTE = COD_AGENTE
       FROM [C6Mart].[VCONTRATTI] WHERE COD_FISCALE = @CodiceFiscale AND RETE=@RETE
		insert into C6Mart.GESTIONE_PDF_FTP
		(
			Rete,
			CodiceFiscale,
			IDReport,
			DataGenerazione,
			DataInvio,
			FlagInvio,
			NomeFile,
			TipoReport,
			numeroPagine,
			id_Fk_zip,
			ID_ELAB
		)
		values
		(
			@Rete,
			@CodiceFiscale,
			@IDReport,
			Getdate(),
			CASE WHEN @COD_AGENTE = '015312' THEN Getdate() ELSE null END,
			CASE WHEN @COD_AGENTE = '015312' THEN 'S' ELSE 'N' END,
			@NomeFile,
			@TipoReport,
			@NumeroPagine,
			CASE WHEN @COD_AGENTE = '015312' THEN 0 ELSE -1 END,
			@ID_ELAB
			)
		UPDATE [C6Mart].[CONTRATTOSEI_METADATI]
		SET InvioBenvenuto = 'S'
        FROM [C6Mart].[VCONTRATTI] VC
         INNER JOIN [C6Mart].[CONTRATTOSEI_metadati] CM
         ON VC.CHIAVECLIENTEPB = CM.CHIAVECLIENTEPB
		WHERE VC.Rete = @Rete
		AND VC.Cod_Fiscale = @CodiceFiscale
		return @@rowcount
END