/****** Object:  StoredProcedure [dbo].[sp_cupomfiscalrecebimento_delete]    Script Date: 11/28/2013 09:35:25 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_cupomfiscalrecebimento_delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_cupomfiscalrecebimento_delete]
GO
GO
--exec sp_cupomfiscalrecebimento_delete 1, 31, 800451
create procedure [dbo].[sp_cupomfiscalrecebimento_delete] (@cd_emp int, @cd_filial	int, @cd_cd int)
WITH ENCRYPTION AS 
SET NOCOUNT ON
begin

CREATE TABLE #pdvConvenio (CD_EMP INT, CD_FILIAL INT,CD_VD INT, CD_CTR INT )

delete 
	it 
from 
	pdv_vd_it_vd_pbm it inner join pdv_vd vd on 
	vd.cd_emp = it.cd_emp and 
	vd.cd_filial = it.cd_filial and vd.cd_vd = it.cd_vd  
 where 
	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd 

delete 
	it 
from 
	rc_adm_cartao_mov_lote_pdv_vd it inner join pdv_vd vd on 
	vd.cd_emp = it.cd_emp and 
	vd.cd_filial = it.cd_filial and 
	vd.cd_vd = it.cd_vd  
 where 
	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd 	
  
delete 
	it 
from 
	pdv_vd_tef it inner join pdv_vd vd on 
	vd.cd_emp = it.cd_emp and 
	vd.cd_filial = it.cd_filial and 
	vd.cd_vd = it.cd_vd  
 where 
	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd 	
	  
delete 
	it 
from 
	pdv_vd_gerar_dup_pendencia it inner join pdv_vd vd on 
	vd.cd_emp = it.cd_emp and 
	vd.cd_filial = it.cd_filial and 
	vd.cd_vd = it.cd_vd  
 where 
 	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd 	

delete 
	it 
from 
	pdv_vd_chqs_chq_lanc it inner join pdv_vd vd on 
	vd.cd_emp = it.cd_emp and 
	vd.cd_filial = it.cd_filial and 
	vd.cd_vd = it.cd_vd  
 where 
 	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd 	
	 
delete 
	it 
from 
	pdv_vd_chqs it inner join pdv_vd vd on 
	vd.cd_emp = it.cd_emp and 
	vd.cd_filial = it.cd_filial and 
	vd.cd_vd = it.cd_vd  
 where 
	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd 	
	  
delete 
	it 
from 
	pdv_vd_chqs it inner join pdv_vd vd on 
	vd.cd_emp = it.cd_emp and 
	vd.cd_filial = it.cd_filial and 
	vd.cd_vd = it.cd_vd  
 where 
 	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd 	

insert into #pdvConvenio
select
	cd_emp,
	cd_filial,
	cd_vd,
	cd_ctr
from 
	pdv_vd_rc_vd_conv 
where 
 	pdv_vd_rc_vd_conv.cd_emp = @cd_emp 
	and pdv_vd_rc_vd_conv.cd_filial = @cd_filial  
	and pdv_vd_rc_vd_conv.cd_vd = @cd_cd 	

delete 
	it 
from 
	pdv_vd_rc_vd_conv it  
 where 
 	it.cd_emp = @cd_emp 
	and it.cd_filial = @cd_filial  
	and it.cd_vd = @cd_cd 	

delete	
	it 
from 
	#pdvConvenio 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

delete	
	convdeb
from		
	#pdvConvenio vd inner join RC_VD_CONV_RC_DEB convdeb on
	vd.cd_emp = convdeb.cd_emp and
	vd.cd_ctr  = convdeb.cd_ctr 

delete	
	vd
from		
	rc_vd_conv vd inner join #pdvConvenio vdconv on
	vd.cd_emp = vdconv.cd_emp and
	vd.cd_ctr  = vdconv.cd_ctr 

 delete 
	conv 
from 
	pdv_vd_cancel_conv conv 
 where 
 	conv.cd_emp = @cd_emp 
	and conv.cd_filial = @cd_filial  
	and conv.cd_vd = @cd_cd 

update
	vd 
set 
	VLR_DINH=0,
	VLR_CHQS=0,
	VLR_CARTAO=0,
	VLR_CONV=0,
	VLR_DUP=0,
	VLR_NCC=0,
	VLR_TICKETS=0,
	VLR_CONV_PBM=0
FROM
	PDV_VD vd        
where 
	vd.cd_emp = @cd_emp 
	and vd.cd_filial = @cd_filial  
	and vd.cd_vd = @cd_cd
	 
DROP TABLE #pdvConvenio;
end


