/****** Object:  StoredProcedure [dbo].[P_RC_CONC_PDV_VD_TEF_REENVIO]    Script Date: 04/08/2014 09:57:57 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_RC_CONC_PDV_VD_TEF_REENVIO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_RC_CONC_PDV_VD_TEF_REENVIO]
GO
--EXEC P_RC_CONC_PDV_VD_TEF_REENVIO 1,33,1
CREATE PROCEDURE P_RC_CONC_PDV_VD_TEF_REENVIO (@CD_EMP INT, @CD_CTR_LOTE INT,@TP_CUPOM INT=0)
AS
SET NOCOUNT ON
	DELETE
	FROM
		RC_CONC_PDV_VD_TEF_CONCILIADO
	WHERE
		CD_EMP =@CD_EMP
		AND CD_CTR_LOTE=@CD_CTR_LOTE


IF @TP_CUPOM= 0
BEGIN
     print 'delete: RC_CONC_PDV_VD_TEF_ENVIO_PENDENCIA '
	   	DELETE 
	FROM
		RC_CONC_PDV_VD_TEF_ENVIO_PENDENCIA
	WHERE
		CD_EMP =@CD_EMP
		AND CD_CTR_LOTE=@CD_CTR_LOTE
 
      
    print 'insert: RC_CONC_PDV_VD_TEF_ENVIO_PENDENCIA '
	INSERT INTO RC_CONC_PDV_VD_TEF_ENVIO_PENDENCIA
	(CD_EMP,
	CD_CTR_LOTE,
	CD_FILIAL,
	CD_VD,
	CD_CTR,
	DT_CAD,
	FLAG_RESPOSTA)
	SELECT
		CD_EMP,
	CD_CTR_LOTE,
	CD_FILIAL,
	CD_VD,
	CD_CTR,
	GETDATE(),
	0
	FROM
		RC_CONC_PDV_VD_TEF
	WHERE
		CD_EMP =@CD_EMP
		AND CD_CTR_LOTE=@CD_CTR_LOTE	
    END
ELSE
    BEGIN
    print 'delete: RC_CONC_PDV_CUPOM_NAO_FISCAL_TEF_ENVIO_PENDENCIA '
    	DELETE 
	FROM
		RC_CONC_PDV_CUPOM_NAO_FISCAL_TEF_ENVIO_PENDENCIA
	WHERE
		CD_EMP =@CD_EMP
		AND CD_CTR_LOTE=@CD_CTR_LOTE
    

      print 'insert: RC_CONC_PDV_CUPOM_NAO_FISCAL_TEF_ENVIO_PENDENCIA '
    	    INSERT INTO RC_CONC_PDV_CUPOM_NAO_FISCAL_TEF_ENVIO_PENDENCIA
	    (CD_EMP,
	    CD_CTR_LOTE,
	    CD_FILIAL,
	    CD_VD,
	    CD_CTR,
	    FLAG_RESPOSTA)
	    SELECT
		    CD_EMP,
	    CD_CTR_LOTE,
	    CD_FILIAL,
	    CD_VD,
	    CD_CTR, 
	    0
	    FROM
		    RC_CONC_PDV_CUPOM_NAO_FISCAL_TEF
	    WHERE
		    CD_EMP =@CD_EMP
		    AND CD_CTR_LOTE=@CD_CTR_LOTE	;
    END 
