-- Schema: C6MartPeriodico
-- Stored Procedure: GESTIONE_INSERT_ZIP


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
-- Inserimento nuovo [C6Mart].[GESTIONE_INSERT_ZIP] -1,'NomeFileOk','NomeFileOk'
CREATE procedure [C6MartPeriodico].[GESTIONE_INSERT_ZIP]
@NomeFile as varchar(255),
@NomeCartella as varchar(255),
@FKIDElaborazioneZip as int
AS
BEGIN 
BEGIN TRANSACTION
DECLARE @ID_ELAB INT
SET @ID_ELAB = C6Martperiodico.GETIDELAB()
declare @IdZip as int
set @IdZip = -1
--recupero idzip se Esiste insieme al NomeCartella
SELECT @IdZip=id,@NomeCartella=NomeCartella 
FROM
	C6MartPeriodico.GESTIONE_ZIP_FTP
WHERE
	FlagInvio = 'N'
	AND DateCopyToSend IS NULL

IF(@IDZIP = -1)--NON ESISTE
	BEGIN
	 INSERT INTO [C6MartPeriodico].[GESTIONE_ZIP_FTP]
           ( [NOMEFILE]
			,[DATAINVIO]
			,[FLAGINVIO]
			,[NOMECARTELLA]
			,[ID_ELAB]
			,[FKIDElaborazioneZip])
     VALUES
           ( @NOMEFILE
			,NULL
			,'N'
			,@NOMECARTELLA
			,@ID_ELAB
			,@FKIDElaborazioneZip)
	SET @IDZIP = SCOPE_IDENTITY()
	END
ELSE --ESISTE
	BEGIN
		 UPDATE [C6MartPeriodico].[GESTIONE_ZIP_FTP]
		 SET [NOMEFILE] = @NOMEFILE
		 WHERE ID = @IDZIP
	END
IF @@error <> 0
	begin
		rollback
	end
else
	begin
		commit
		select @IdZip IdZip,@NomeFile NomeFile,@NomeCartella NomeCartella
	end


end


