/****** Object:  StoredProcedure [dbo].[sp_cupomfiscal_delete]    Script Date: 11/06/2013 22:27:02 ******/
IF EXISTS(SELECT * FROM sys.objects
  WHERE object_id=OBJECT_ID(N'[dbo].[sp_cupomfiscal_delete]')
        AND type IN(N'P',N'PC')
)
    DROP PROCEDURE [dbo].[sp_cupomfiscal_delete];
GO
GO

CREATE PROCEDURE [dbo].[sp_cupomfiscal_delete]
(
     @cd_emp    INT
    ,@cd_filial INT
    ,@cd_cx     INT
    ,@dt_vd     DATETIME
    ,@nr_ecf    INT
    ,@cd_vd_par INT     =0)
AS
BEGIN
DECLARE @cd_vd INT;

SET NOCOUNT ON;

IF @cd_vd_par>0
BEGIN
	SET @cd_vd=@cd_vd_par;
END;
ELSE
BEGIN
	SET @cd_vd=isnull((SELECT TOP 1
		   cd_vd
	  FROM
		   pdv_vd AS vd
	  WHERE vd.dt_vd=@dt_vd
			AND vd.cd_emp=@cd_emp
			AND vd.cd_filial=@cd_filial
			AND vd.cd_cx=@cd_cx
			AND vd.nr_ecf=@nr_ecf
	  ORDER BY
		   cd_vd DESC
	),0);
END;



IF @cd_vd=0
    RETURN;


print 'Removento a venda ' + convert(varchar, @cd_vd,112) + ' Filial=' + convert(varchar,@cd_filial) + ' Caixa ' + convert(varchar,@cd_cx) + ' Cupom ' + convert(varchar,@nr_ecf);

PRINT 'DELETE FROM pdv_vd_it_est_sngpc_pendente';
DELETE FROM pdv_vd_it_est_sngpc_pendente
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;

IF EXISTS(SELECT 1
  FROM
       pdv_vd_it_est_sngpc_saida
  WHERE cd_emp=@cd_emp
        AND cd_filial=@cd_filial
        AND cd_vd=@cd_vd
)
BEGIN
	PRINT ' pdv_vd_it_est_sngpc_saida cupom com lançamento no sngpc';
	DELETE FROM pdv_vd_it_est_sngpc_saida
	WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
  --return
END;
 
 
 	PRINT ' delelet d PDV_VD_IT_EST_SNGPC_PENDENTE_CANCELADO';
	DELETE FROM PDV_VD_IT_EST_SNGPC_PENDENTE_CANCELADO
	WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
  --return

 

PRINT 'DELETE FROM rc_cli_fideli_pontos_credito_pdv_vd_it_fideli_cartao';
DELETE FROM rc_cli_fideli_pontos_credito_pdv_vd_it_fideli_cartao
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;

PRINT 'DELETE FROM RC_CLI_FIDELI_PONTOS_DEBITO_PDV_VD_IT';
DELETE FROM RC_CLI_FIDELI_PONTOS_DEBITO_PDV_VD_IT
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
	  

PRINT 'DELETE FROM pdv_vd_est_nf_sai';
DELETE FROM pdv_vd_est_nf_sai
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM rc_crediario_parc_pdv_vd';
DELETE FROM rc_crediario_parc_pdv_vd
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;


PRINT 'DELETE FROM rc_crediario_parc_pdv_vd';
DELETE FROM pdv_vd_cancel
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
	  

	  

PRINT 'DELETE FROM pdv_vd_dev_est_prod_imp_est_nf_ent';
DELETE FROM pdv_vd_dev_est_prod_imp_est_nf_ent
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;

PRINT 'DELETE FROM pdv_vd_dev_est_prod_imp_pend_nf';
DELETE FROM pdv_vd_dev_est_prod_imp_pend_nf
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;

PRINT 'DELETE FROM pdv_vd_dev_ncc_pdv_vd_dev_est_prod_imp';
DELETE FROM pdv_vd_dev_ncc_pdv_vd_dev_est_prod_imp
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;


PRINT 'DELETE FROM pdv_vd_dev_est_prod_imp';
DELETE FROM pdv_vd_dev_est_prod_imp
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_it_vd_pbm';
DELETE FROM pdv_vd_it_vd_pbm
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_it_operador_caixa_comissao';
DELETE FROM pdv_vd_it_operador_caixa_comissao
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_it';
DELETE FROM pdv_vd_it
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM rc_adm_cartao_mov_lote_pdv_vd';
DELETE FROM rc_adm_cartao_mov_lote_pdv_vd
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM RC_CONC_PDV_VD_TEF_ENVIO_PENDENCIA';
DELETE FROM RC_CONC_PDV_VD_TEF_ENVIO_PENDENCIA
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM RC_CONC_PDV_VD_TEF';
DELETE FROM RC_CONC_PDV_VD_TEF
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_tef';
DELETE FROM pdv_vd_tef
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_gerar_dup_pendencia';
DELETE FROM pdv_vd_gerar_dup_pendencia
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_chqs_chq_lanc';
DELETE FROM pdv_vd_chqs_chq_lanc
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_chqs';
DELETE FROM pdv_vd_chqs
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_rc_vd_conv';
DELETE FROM pdv_vd_rc_vd_conv
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM rc_vd_conv_fecha_lote_rc_vd_conv';
DELETE it
FROM rc_vd_conv vd
INNER JOIN rc_vd_conv_fecha_lote_rc_vd_conv it ON vd.cd_emp=it.cd_emp
                                                  AND vd.cd_ctr=it.cd_ctr
WHERE
     vd.cd_emp=@cd_emp
     AND vd.cd_filial=@cd_filial
     AND dt_vd=@dt_vd
     AND vd.cd_cx=@cd_cx
     AND vd.nr_ecf=@nr_ecf;
PRINT 'DELETE FROM RC_VD_CONV_RC_DEB';
DELETE convdeb
FROM rc_vd_conv vd
INNER JOIN RC_VD_CONV_RC_DEB convdeb ON vd.cd_emp=convdeb.cd_emp
                                        AND vd.cd_ctr=convdeb.cd_ctr
WHERE
     vd.cd_emp=@cd_emp
     AND vd.cd_filial=@cd_filial
     AND dt_vd=@dt_vd
     AND vd.cd_cx=@cd_cx
     AND vd.nr_ecf=@nr_ecf;
PRINT 'DELETE FROM rc_vd_conv';
DELETE vd
FROM rc_vd_conv vd
WHERE
     vd.cd_emp=@cd_emp
     AND vd.cd_filial=@cd_filial
     AND dt_vd=@dt_vd
     AND vd.cd_cx=@cd_cx
     AND vd.nr_ecf=@nr_ecf;
PRINT 'DELETE FROM est_orc';
DELETE orc
FROM est_orc orc
INNER JOIN pdv_vd_est_orc it ON orc.cd_emp=it.cd_emp
                                AND orc.cd_filial=it.cd_filial
                                AND orc.cd_orc=it.cd_orc
WHERE
     it.cd_emp=@cd_emp
     AND it.cd_filial=@cd_filial
     AND it.cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_est_orc';
DELETE it
FROM pdv_vd_est_orc it
WHERE
     it.cd_emp=@cd_emp
     AND it.cd_filial=@cd_filial
     AND it.cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_dev_ncc_pdv_vd';
DELETE FROM pdv_vd_dev_ncc_pdv_vd
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd_cancel_conv';
DELETE FROM pdv_vd_cancel_conv
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM tele_orc_etapa_pdv_vd';
DELETE FROM tele_orc_etapa_pdv_vd
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM PDV_VD_NFCE_ARQ_DANFE';
DELETE ARQ
FROM PDV_VD_NFCE_ARQ_DANFE ARQ
INNER JOIN PDV_VD_PDV_VD_NFCE VD ON ARQ.CD_NFCE=VD.CD_NFCE
WHERE
     VD.cd_emp=@cd_emp
     AND VD.cd_filial=@cd_filial
     AND VD.cd_vd=@cd_vd;
PRINT 'DELETE FROM PDV_VD_NFCE_ARQ_DANFE_CANCEL';
DELETE ARQ
FROM PDV_VD_NFCE_ARQ_DANFE_CANCEL ARQ
INNER JOIN PDV_VD_PDV_VD_NFCE VD ON ARQ.CD_NFCE=VD.CD_NFCE
WHERE
     VD.cd_emp=@cd_emp
     AND VD.cd_filial=@cd_filial
     AND VD.cd_vd=@cd_vd;
PRINT 'DELETE FROM PDV_VD_NFCE_CANCEL';
DELETE ARQ
FROM PDV_VD_NFCE_CANCEL ARQ
INNER JOIN PDV_VD_PDV_VD_NFCE VD ON ARQ.CD_NFCE=VD.CD_NFCE
WHERE
     VD.cd_emp=@cd_emp
     AND VD.cd_filial=@cd_filial
     AND VD.cd_vd=@cd_vd;
PRINT 'DELETE FROM PDV_VD_PDV_VD_NFCE';
DELETE FROM PDV_VD_PDV_VD_NFCE
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM PDV_VD_PROCD_AUDIT';
DELETE FROM PDV_VD_PROCD_AUDIT
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
PRINT 'DELETE FROM pdv_vd';
DELETE FROM pdv_vd
WHERE
     cd_emp=@cd_emp
     AND cd_filial=@cd_filial
     AND cd_vd=@cd_vd;
END;