SET QUOTED_IDENTIFIER OFF
GO
IF EXISTS (SELECT 1 FROM dbo.sysobjects WHERE ID = OBJECT_ID(N'[dbo].[sp_fco_del_reportes]') 
AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
   DROP PROCEDURE dbo.sp_fco_del_reportes
GO
CREATE PROCEDURE dbo.sp_fco_del_reportes
(
  @pch_compania char(4),
  @pti_cod_rep tinyint,
  @pch_operacion char(8)
)
AS
/********************************************************************/
/*** Reportes e Interfases de Data Tecnologia                     ***/
/*** Descripcion: borra registros de las tablas fco_reportes      ***/
/*** y fco_reportes_cias, fco_reportes_par, fdt_rutas             ***/
/*** Autor:	Diana Cecilia Rios Peinado                            ***/
/*** Fecha:	Mayo 14 de 2005                                       ***/
/********************************************************************/
/*** Pasado a SQL, Diana C. Rios, Septiembre 25/2005							***/
DECLARE
  @vvc_parametro   varchar(10)
BEGIN--1
      
    IF EXISTS (SELECT 1 FROM fco_reportes_cias
      				 WHERE cod_rep = @pti_cod_rep AND compania <> @pch_compania)
		BEGIN
				RAISERROR('Este reporte no se puede eliminar porque tiene relación con otras compañías', 16, 1)
        RETURN;        
    END
        
    SET @vvc_parametro = 'report_' + LTRIM(RTRIM(@pti_cod_rep))
    DELETE FROM fco_reportes_cias WHERE compania = @pch_compania AND cod_rep = @pti_cod_rep
    DELETE FROM fco_reportes_par WHERE cod_rep = @pti_cod_rep
    DELETE FROM fdt_rutas WHERE codigo = @pti_cod_rep AND operacion = @pch_operacion
    DELETE FROM fco_reportes WHERE cod_rep = @pti_cod_rep
    DELETE FROM fdt_parametros WHERE pais = 'COL' AND compania = @pch_compania
    AND parametro = @vvc_parametro

		IF NOT EXISTS (SELECT 1 FROM fco_reportes_cias WHERE compania <> @pch_compania AND cod_rep = @pti_cod_rep)
		BEGIN
	    DELETE FROM fdt_parametros WHERE pais = 'COL' AND compania = '0'
	    AND parametro = @vvc_parametro
		END
        
END--1
GO
GRANT EXECUTE ON sp_fco_del_reportes TO Public