CREATE TABLE [dbo].[CANTONES] (
	[CODIGO_CANTON] [int] NOT NULL ,
	[NOMBRE_CANTON] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[POBLACION_CANTON] [int] NOT NULL ,
	[CODIGO_MUNICIPIO] [int] NOT NULL ,
	[XPOS] [int] NULL ,
	[YPOS] [int] NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[DEPARTAMENTOS] (
	[CODIGO_DEPARTAMENTO] [int] NOT NULL ,
	[NOMBRE_DEPARTAMENTO] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[ESTADOS] (
	[CODIGO_ESTADO] [int] NOT NULL ,
	[NOMBRE_ESTADO] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[ETAPAS] (
	[CODIGO_ETAPA] [int] NOT NULL ,
	[NOMBRE_ETAPA] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[FUENTES_FINANCIAMIENTO] (
	[CODIGO_FUENTE] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[NOMBRE_FUENTE] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CODIGO_PAIS] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[INSTITUCIONES] (
	[CODIGO_INSTITUCION] [int] NOT NULL ,
	[NOMBRE_INSTITUCION] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[SIGLAS_INSTITUCION] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[MUNICIPIOS] (
	[CODIGO_MUNICIPIO] [int] NOT NULL ,
	[NOMBRE_MUNICIPIO] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CODIGO_DEPARTAMENTO] [int] NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[NECESIDADES] (
	[CODIGO_OBRA] [int] NOT NULL ,
	[CODIGO_NECESIDAD] [int] NOT NULL ,
	[NOMBRE_NECESIDAD] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[DESCRIPCION_NECESIDAD] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[MONTO_NECESIDAD] [decimal](18, 2) NOT NULL ,
	[FECHA_INGRESO] [datetime] NOT NULL ,
	[CODIGO_RESPONSABLE] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[OBRAS_FISICAS] (
	[CODIGO_OBRA] [int] IDENTITY (1, 1) NOT NULL ,
	[CODIGO_UNIDAD] [int] NOT NULL ,
	[COORX_OBRA] [decimal](18, 2) NOT NULL ,
	[COORY_OBRA] [decimal](18, 2) NOT NULL ,
	[NOMBRE_OBRA] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[CODIGO_CANTON] [int] NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[PAISES] (
	[CODIGO_PAIS] [varchar] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[NOMBRE_PAIS] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[PROYECTOS] (
	[CODIGO_OBRA] [int] NOT NULL ,
	[CODIGO_PROYECTO] [int] NOT NULL ,
	[NOMBRE_PROYECTO] [varchar] (150) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[DESCRIPCION_PROYECTO] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[FECHA_INICIO_PROGRAMADA] [datetime] NOT NULL ,
	[FECHA_FINAL_PROGRAMADA] [datetime] NOT NULL ,
	[FECHA_INICIO_REAL] [datetime] NOT NULL ,
	[FECHA_FINAL_REAL] [datetime] NOT NULL ,
	[CODIGO_ETAPA] [int] NOT NULL ,
	[CODIGO_ESTADO] [int] NOT NULL ,
	[EMPLEOS_PROYECTO] [int] NOT NULL ,
	[BENEFICIARIOS_PROYECTO] [int] NOT NULL ,
	[CODIGO_TIPO] [int] NOT NULL ,
	[CODIGO_NECESIDAD] [int] NOT NULL ,
	[CODIGO_RESPONSABLE] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[PROYECTOS_FUENTES] (
	[CODIGO_OBRA] [int] NOT NULL ,
	[CODIGO_PROYECTO] [int] NOT NULL ,
	[CODIGO_FUENTE] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CODIGO_TIPOFUENTE] [int] NOT NULL ,
	[MONTO] [decimal](18, 2) NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[REPORTES] (
	[COD_CUA] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[NOM_CUA] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[SEL_CUA] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[TIP_CUA] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[ENC_CUA] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[ESQ_CUA] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[DAT_CUA] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CO1_CUA] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CO2_CUA] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[RESPONSABLES] (
	[CODIGO_RESPONSABLE] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CLAVE_RESPONSABLE] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[NOMBRE_RESPONSABLE] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[APELLIDO_RESPONSABLE] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CARGO_RESPONSABLE] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[EMAIL_RESPONSABLE] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[CODIGO_INSTITUCION] [int] NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[SEGUIMIENTO] (
	[CODIGO_OBRA] [int] NOT NULL ,
	[CODIGO_PROYECTO] [int] NOT NULL ,
	[AO] [int] NOT NULL ,
	[MES] [int] NOT NULL ,
	[PROGRAMADO_FINANCIERO] [decimal](18, 2) NOT NULL ,
	[PROGRAMADO_FISICO] [numeric](5, 2) NOT NULL ,
	[EJECUCION_FINANCIERO] [decimal](18, 2) NOT NULL ,
	[EJECUCION_FISICO] [numeric](5, 2) NOT NULL ,
	[OBSERVACIONES] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[SISMEN] (
	[COD_MEN] [varchar] (20) COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[NOM_MEN] [varchar] (100) COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[URL_MEN] [text] COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[AYU_MEN] [varchar] (100) COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[SUP_MEN] [varchar] (20) COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[ORD_MEN] [varchar] (2) COLLATE Modern_Spanish_CI_AS NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

CREATE TABLE [dbo].[SISPER] (
	[COD_PER] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[NOM_PER] [varchar] (40) COLLATE Modern_Spanish_CI_AS NOT NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

CREATE TABLE [dbo].[SISPERMEN] (
	[COD_MEN] [varchar] (20) COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[COD_PER] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[SISUSUPER] (
	[COD_PER] [char] (10) COLLATE Modern_Spanish_CI_AS NOT NULL ,
	[CODIGO_RESPONSABLE] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[TABLAS] (
	[NOMBRE_TABLA] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[ROTULO_TABLA] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[SUPERIOR_TABLA] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[TABLAS_CAMPOS] (
	[NOMBRE_CAMPO] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[NOMBRE_TABLA] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[ROTULO_CAMPO] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[TIPO_CAMPO] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[LONG_CAMPO] [numeric](18, 0) NOT NULL ,
	[LLAVE_CAMPO] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[ORDEN_CAMPO] [numeric](18, 0) NOT NULL ,
	[ESLLAVE_CAMPO] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[TIPOS_FUENTES] (
	[CODIGO_TIPOFUENTE] [int] NOT NULL ,
	[NOMBRE_TIPOFUENTE] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[TIPOS_PROYECTO] (
	[CODIGO_TIPO] [int] NOT NULL ,
	[NOMBRE_TIPO] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[UNIDADES_MEDIDA] (
	[CODIGO_UNIDAD] [int] IDENTITY (1, 1) NOT NULL ,
	[NOMBRE_UNIDAD] [varchar] (17) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
	[IMAGEN_UNIDAD] [varchar] (150) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE   procedure AvanceFinancieroObras @institucion int ,@obra int ,@anio int ,@usuario varchar(10)
as

select codigo_proyecto,
nombre_proyecto,
convert(varchar(20),'-') as prog,
convert(varchar(20),'-') as ejec,convert(varchar(20),'-') as porc
into #RESULTADOS
from proyectos
where codigo_obra=@obra


if @institucion<>999
BEGIN
select p.codigo_proyecto,
convert(varchar(20),convert(money,sum(s.programado_financiero)),1) as prog,
convert(varchar(20),convert(money,sum(s.ejecucion_financiero)),1) as ejec,
porc=
	Case sum(s.programado_financiero)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)),1)+'%'	
	end
into #RESULTADOS2
from seguimiento s,proyectos p,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=@obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
group by p.codigo_proyecto

update #RESULTADOS set prog=#RESULTADOS2.prog,
ejec=#RESULTADOS2.ejec,
porc=#RESULTADOS2.porc
from #RESULTADOS,#RESULTADOS2
where #RESULTADOS.codigo_proyecto=#RESULTADOS2.codigo_proyecto

END
else
BEGIN
select p.codigo_proyecto,
convert(varchar(20),convert(money,sum(s.programado_financiero)),1) as prog,
convert(varchar(20),convert(money,sum(s.ejecucion_financiero)),1) as ejec,
porc=
	Case sum(s.programado_financiero)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)),1)+'%'	
	end
into #RESULTADO3
from seguimiento s,proyectos p
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=@obra
and s.ao=@anio
group by p.codigo_proyecto

update #RESULTADOS set prog=#RESULTADO3.prog,
ejec=#RESULTADO3.ejec,
porc=#RESULTADO3.porc
from #RESULTADOS,#RESULTADO3
where #RESULTADOs.codigo_proyecto=#RESULTADO3.codigo_proyecto


END


select nombre_proyecto as Proyecto,prog as [Monto Programado],ejec as [Monto Ejecutado], porc as [Porcentaje de Avance]
from #RESULTADOS
order by codigo_proyecto






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE   procedure AvanceFinancieroObrasCanton @institucion int ,@canton int ,@anio int ,@usuario varchar(10)
as


select codigo_obra,
'<a href=/servlet/Reportes?Codigo=05&USUARIO='+@usuario+'&parametro='+convert(varchar(4),codigo_obra)+'&institucion='+convert(varchar(4),@institucion)+'&anio='+convert(varchar(4),@anio)+'>'+nombre_obra+'</a>' as nombre_obra,
convert(varchar(20),'-') as prog,
convert(varchar(20),'-') as ejec,convert(varchar(20),'-') as porc
into #RESULTADOS
from obras_fisicas
where codigo_canton=@canton


if @institucion<>999
BEGIN
select o.codigo_obra,
convert(varchar(20),convert(money,sum(s.programado_financiero)),1) as prog,
convert(varchar(20),convert(money,sum(s.ejecucion_financiero)),1) as ejec,
porc=
	Case sum(s.programado_financiero)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)),1)+'%'	
	end
into #RESULTADOS2
from seguimiento s,proyectos p,obras_fisicas o,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
and o.codigo_canton=@canton
group by o.codigo_obra

update #RESULTADOS set prog=#RESULTADOS2.prog,
ejec=#RESULTADOS2.ejec,
porc=#RESULTADOS2.porc
from #RESULTADOS,#RESULTADOS2
where #RESULTADOS.codigo_obra=#RESULTADOS2.codigo_obra

END
else
BEGIN
select o.codigo_obra,
convert(varchar(20),convert(money,sum(s.programado_financiero)),1) as prog,
convert(varchar(20),convert(money,sum(s.ejecucion_financiero)),1) as ejec,
porc=
	Case sum(s.programado_financiero)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)),1)+'%'	
	end
into #RESULTADO3
from seguimiento s,proyectos p,obras_fisicas o
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and s.ao=@anio
and o.codigo_canton=@canton
group by o.codigo_obra

update #RESULTADOS set prog=#RESULTADO3.prog,
ejec=#RESULTADO3.ejec,
porc=#RESULTADO3.porc
from #RESULTADOS,#RESULTADO3
where #RESULTADOs.codigo_obra=#RESULTADO3.codigo_obra


END


select nombre_obra as Obra,prog as [Monto Programado],ejec as [Monto Ejecutado], porc as [Porcentaje de Avance]
from #RESULTADOS
where prog<>'-'
order by codigo_obra




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE   procedure AvanceFisicoObras @institucion int ,@obra int ,@anio int ,@usuario varchar(10)
as

select codigo_proyecto,
nombre_proyecto,
convert(varchar(20),'-') as porc
into #RESULTADOS
from proyectos
where codigo_obra=@obra


if @institucion<>999
BEGIN
select p.codigo_proyecto,
porc=
	Case sum(s.programado_fisico)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)),1)+'%'	
	end
into #RESULTADOS2
from seguimiento s,proyectos p,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=@obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
group by p.codigo_proyecto

update #RESULTADOS set 
porc=#RESULTADOS2.porc
from #RESULTADOS,#RESULTADOS2
where #RESULTADOS.codigo_proyecto=#RESULTADOS2.codigo_proyecto

END
else
BEGIN
select p.codigo_proyecto,
porc=
	Case sum(s.programado_fisico)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)),1)+'%'	
	end
into #RESULTADO3
from seguimiento s,proyectos p
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=@obra
and s.ao=@anio
group by p.codigo_proyecto

update #RESULTADOS set 
porc=#RESULTADO3.porc
from #RESULTADOS,#RESULTADO3
where #RESULTADOs.codigo_proyecto=#RESULTADO3.codigo_proyecto


END


select nombre_proyecto as Proyecto,porc as [Porcentaje de Avance]
from #RESULTADOS
order by codigo_proyecto

--exec AvanceFisicoObras 1,32,2003,'DATOSMINED'



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO




CREATE    procedure AvanceFisicoObrasCanton @institucion int ,@canton int ,@anio int ,@usuario varchar(10)
as

select codigo_obra,
'<a href=/servlet/Reportes?Codigo=06&USUARIO='+@usuario+'&parametro='+convert(varchar(4),codigo_obra)+'&institucion='+convert(varchar(4),@institucion)+'&anio='+convert(varchar(4),@anio)+'>'+nombre_obra+'</a>' as nombre_obra,
convert(varchar(20),'-') as porc
into #RESULTADOS
from obras_fisicas
where codigo_canton=@canton


if @institucion<>999
BEGIN
select o.codigo_obra,
porc=
	Case sum(s.programado_fisico)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)),1)+'%'	
	end
into #RESULTADOS2
from seguimiento s,proyectos p,obras_fisicas o,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
and o.codigo_canton=@canton
group by o.codigo_obra

update #RESULTADOS set porc=#RESULTADOS2.porc
from #RESULTADOS,#RESULTADOS2
where #RESULTADOS.codigo_obra=#RESULTADOS2.codigo_obra

END
else
BEGIN
select o.codigo_obra,
porc=
	Case sum(s.programado_fisico)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)),1)+'%'	
	end
into #RESULTADO3
from seguimiento s,proyectos p,obras_fisicas o
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and s.ao=@anio
and o.codigo_canton=@canton
group by o.codigo_obra

update #RESULTADOS set porc=#RESULTADO3.porc
from #RESULTADOS,#RESULTADO3
where #RESULTADOS.codigo_obra=#RESULTADO3.codigo_obra


END


select nombre_obra as Obra,
porc as [Porcentaje de Avance]
from #RESULTADOS
where porc<>'-'
order by codigo_obra


--exec AvanceFisicoObrasCanton 999,880,2003,'CSUAREZ'




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE  procedure DetalleAvanceFinanciero @institucion int,@parametro char(1),@anio int,@usuario varchar(10)
as

select codigo_canton,
'<a href=/servlet/Reportes?Codigo=03&USUARIO='+@usuario+'&parametro='+convert(varchar(4),codigo_canton)+'&institucion='+convert(varchar(4),@institucion)+'&anio='+convert(varchar(4),@anio)+'>'+nombre_canton+'</a>' as nombre_canton,
convert(varchar(20),'-') as prog,
convert(varchar(20),'-') as ejec,convert(varchar(20),'-') as porc
into #RESULTADOS
from cantones
where codigo_municipio=601


if @institucion<>999
BEGIN
select c.codigo_canton,
convert(varchar(20),convert(money,sum(s.programado_financiero)),1) as prog,
convert(varchar(20),convert(money,sum(s.ejecucion_financiero)),1) as ejec,
porc=
	Case sum(s.programado_financiero)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)),1)+'%'	
	end
into #RESULTADOS2
from cantones c,seguimiento s,proyectos p,obras_fisicas o,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
and o.codigo_canton=c.codigo_canton
group by c.codigo_canton

update #RESULTADOS set prog=#RESULTADOS2.prog,
ejec=#RESULTADOS2.ejec,
porc=#RESULTADOS2.porc
from #RESULTADOS,#RESULTADOS2
where #RESULTADOS.codigo_canton=#RESULTADOS2.codigo_canton

END
else
BEGIN
select c.codigo_canton,
convert(varchar(20),convert(money,sum(s.programado_financiero)),1) as prog,
convert(varchar(20),convert(money,sum(s.ejecucion_financiero)),1) as ejec,
porc=
	Case sum(s.programado_financiero)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)),1)+'%'	
	end
into #RESULTADO3
from cantones c,seguimiento s,proyectos p,obras_fisicas o
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and s.ao=@anio
and o.codigo_canton=c.codigo_canton
group by c.codigo_canton

update #RESULTADOS set prog=#RESULTADO3.prog,
ejec=#RESULTADO3.ejec,
porc=#RESULTADO3.porc
from #RESULTADOS,#RESULTADO3
where #RESULTADOs.codigo_canton=#RESULTADO3.codigo_canton


END


select nombre_canton as Canton,prog as [Monto Programado],ejec as [Monto Ejecutado], porc as [Porcentaje de Avance]
from #RESULTADOS
order by codigo_canton

--exec DetalleAvanceFinanciero 999,null,2003,'DATOSMINED'


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE   procedure DetalleAvanceFisico @institucion int,@parametro char(1),@anio int,@usuario varchar(10)
as

select codigo_canton,
'<a href=/servlet/Reportes?Codigo=03&USUARIO='+@usuario+'&parametro='+convert(varchar(4),codigo_canton)+'&institucion='+convert(varchar(4),@institucion)+'&anio='+convert(varchar(4),@anio)+'>'+nombre_canton+'</a>' as nombre_canton,
convert(varchar(20),'-') as porc
into #RESULTADOS
from cantones
where codigo_municipio=601


if @institucion<>999
BEGIN
select c.codigo_canton,
porc=
	Case sum(s.programado_fisico)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)),1)+'%'	
	end
into #RESULTADOS2
from cantones c,seguimiento s,proyectos p,obras_fisicas o,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
and o.codigo_canton=c.codigo_canton
group by c.codigo_canton

update #RESULTADOS set porc=#RESULTADOS2.porc
from #RESULTADOS,#RESULTADOS2
where #RESULTADOS.codigo_canton=#RESULTADOS2.codigo_canton

END
else
BEGIN
select c.codigo_canton,
porc=
	Case sum(s.programado_fisico)
		When 0 then convert(varchar(20),'0.00%') else convert(varchar(20),convert(money,(sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)),1)+'%'	
	end
into #RESULTADO3
from cantones c,seguimiento s,proyectos p,obras_fisicas o
where s.codigo_proyecto=p.codigo_proyecto
and s.codigo_obra=p.codigo_obra
and p.codigo_obra=o.codigo_obra
and s.ao=@anio
and o.codigo_canton=c.codigo_canton
group by c.codigo_canton

update #RESULTADOS set porc=#RESULTADO3.porc
from #RESULTADOS,#RESULTADO3
where #RESULTADOs.codigo_canton=#RESULTADO3.codigo_canton


END


select nombre_canton as Canton,porc as [Porcentaje de Avance]
from #RESULTADOS
order by codigo_canton





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE procedure InversionTipoObra @parametro char(1) as
select distinct codigo_unidad,nombre_unidad, convert(numeric(20,2),0) as monto
into #RESULTADO
from unidades_medida


select a.codigo_unidad,sum(c.programado_financiero) as monto
into #RESULTADO2
from obras_fisicas a,proyectos p,seguimiento c,unidades_medida u
where a.codigo_obra=p.codigo_obra
and p.codigo_proyecto=c.codigo_proyecto
and u.codigo_unidad=a.codigo_unidad
group by a.codigo_unidad

update #RESULTADO set monto=#RESULTADO2.monto
from #RESULTADO,#RESULTADO2
where #RESULTADO.codigo_unidad=#RESULTADO2.codigo_unidad

insert into #RESULTADO
select 'TOTAL',sum(monto) from #RESULTADO

select nombre_unidad as [Tipo Obra],
monto as Inversin
from #RESULTADO
order by codigo_unidad

--exec InversionTipoObra '1'

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

create procedure  InversionTipoObraMunicipioCanton @mun int
as 

select c.codigo_canton, c.nombre_canton,count(p.codigo_proyecto) as cantidad,
t.codigo_tipo,t.nombre_tipo,convert(numeric(20,2),0) as monto
into #RESULTADO
from proyectos p,cantones c,obras_fisicas o,tipos_proyecto t
where o.codigo_obra=p.codigo_obra
and o.codigo_canton=c.codigo_canton
and p.codigo_tipo=t.codigo_tipo
group by c.codigo_canton,c.nombre_canton,t.codigo_tipo,t.nombre_tipo


insert into #RESULTADO
select 99,'TOTAL',count(p.codigo_proyecto),
t.codigo_tipo,t.nombre_tipo,convert(numeric(20,2),0) as monto
from proyectos p,cantones c,obras_fisicas o,tipos_proyecto t
where o.codigo_obra=p.codigo_obra
and o.codigo_canton=c.codigo_canton
and p.codigo_tipo=t.codigo_tipo
group by t.codigo_tipo,t.nombre_tipo




select c.codigo_canton,t.codigo_tipo,sum(s.programado_financiero) as monto
into #RESULTADO2
from proyectos p,cantones c,obras_fisicas o,tipos_proyecto t,seguimiento s
where o.codigo_obra=p.codigo_obra
and o.codigo_canton=c.codigo_canton
and p.codigo_tipo=t.codigo_tipo
and s.codigo_proyecto=p.codigo_proyecto
group by c.codigo_canton,t.codigo_tipo


insert into #RESULTADO2
select 99,t.codigo_tipo,sum(s.programado_financiero) as monto
from proyectos p,cantones c,obras_fisicas o,tipos_proyecto t,seguimiento s
where o.codigo_obra=p.codigo_obra
and o.codigo_canton=c.codigo_canton
and p.codigo_tipo=t.codigo_tipo
and s.codigo_proyecto=p.codigo_proyecto
group by t.codigo_tipo


update #RESULTADO set monto=#RESULTADO2.monto
from #RESULTADO,#RESULTADO2
where #RESULTADO.codigo_canton=#RESULTADO2.codigo_canton
and #RESULTADO.codigo_tipo=#RESULTADO2.codigo_tipo



select nombre_canton as Canton,
convert(varchar(2),cantidad)+' '+nombre_tipo as [Tipo Proyectos],
monto as Inversin
from #RESULTADO
order by codigo_canton

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

create procedure ListadoProyectos @obra int,@usuario varchar(10)
as 

declare @perfil varchar(10)

select @perfil=cod_per from sisusuper where codigo_responsable=@usuario

if @perfil='CONSULSETE' or @perfil='ADMIN'
BEGIN
select p.nombre_proyecto as Proyecto,
p.empleos_proyecto as Empleos,
p.beneficiarios_proyecto as Beneficiarios,
e.nombre_etapa as Etapa,
t.nombre_tipo as [Tipo de Proyecto]
from proyectos p,etapas e,tipos_proyecto t
where e.codigo_etapa=p.codigo_etapa
and t.codigo_tipo=p.codigo_tipo
and p.codigo_obra= @obra
END
else
BEGIN
declare @institucion int
select @institucion=codigo_institucion from responsables where codigo_responsable=@usuario

select p.nombre_proyecto as Proyecto,
p.empleos_proyecto as Empleos,
p.beneficiarios_proyecto as Beneficiarios,
e.nombre_etapa as Etapa,
t.nombre_tipo as [Tipo de Proyecto]
from proyectos p,etapas e,tipos_proyecto t,responsables r
where e.codigo_etapa=p.codigo_etapa
and t.codigo_tipo=p.codigo_tipo
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion = @institucion
and p.codigo_obra= @obra
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

create procedure PRUEBA @parametro char(1)
as
select * from proyectos

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO






CREATE      procedure PorcentajeAvanceFinancieroCanton @institucion int,@codigo_anio int
as


select codigo_canton,convert(numeric(8,2),-1) as valor
into #RESULTADO
from cantones where codigo_municipio=601

if @institucion=999
BEGIN
select o.codigo_canton, 
porc=
	Case sum(s.programado_financiero)
		When 0 then 0.00 else (sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)
	end
into #RESULTADO1
from seguimiento s,proyectos p,obras_fisicas o
where s.codigo_obra=p.codigo_obra
and s.codigo_proyecto=p.codigo_proyecto
and o.codigo_obra=p.codigo_obra
and s.ao=@codigo_anio
group by o.codigo_canton

update #RESULTADO set valor=#RESULTADO1.porc
from #RESULTADO,#RESULTADO1
where #RESULTADO.codigo_canton=#RESULTADO1.codigo_canton

select codigo_canton,valor from #RESULTADO


END
else
BEGIN
select o.codigo_canton, 
porc=
	Case sum(s.programado_financiero)
		When 0 then 0.00 else (sum(s.ejecucion_financiero)/sum(s.programado_financiero)*100)
	end
into #RESULTADO2
from seguimiento s,proyectos p,obras_fisicas o,responsables r
where s.codigo_obra=p.codigo_obra
and s.codigo_proyecto=p.codigo_proyecto
and o.codigo_obra=p.codigo_obra
and s.ao=@codigo_anio
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
group by o.codigo_canton

update #RESULTADO set valor=#RESULTADO2.porc
from #RESULTADO,#RESULTADO2
where #RESULTADO.codigo_canton=#RESULTADO2.codigo_canton

select codigo_canton,valor from #RESULTADO

END









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO






CREATE      procedure PorcentajeAvanceFisicoCanton @institucion int,@codigo_anio int
as


select codigo_canton,convert(numeric(8,2),-1) as valor
into #RESULTADO
from cantones where codigo_municipio=601


if @institucion=999
BEGIN
select o.codigo_canton, 
porc=
	Case sum(s.programado_fisico)
		When 0 then 0.00 else (sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)
	end
into #RESULTADO1
from seguimiento s,proyectos p,obras_fisicas o
where s.codigo_obra=p.codigo_obra
and s.codigo_proyecto=p.codigo_proyecto
and o.codigo_obra=p.codigo_obra
and s.ao=@codigo_anio
group by o.codigo_canton

update #RESULTADO set valor=#RESULTADO1.porc
from #RESULTADO,#RESULTADO1
where #RESULTADO.codigo_canton=#RESULTADO1.codigo_canton

select codigo_canton,valor from #RESULTADO


END
else
BEGIN
select o.codigo_canton,
porc=
	Case sum(s.programado_fisico)
		When 0 then 0.00 else (sum(s.ejecucion_fisico)/sum(s.programado_fisico)*100)
	end
into #RESULTADO2
from seguimiento s,proyectos p,obras_fisicas o,responsables r
where s.codigo_obra=p.codigo_obra
and s.codigo_proyecto=p.codigo_proyecto
and o.codigo_obra=p.codigo_obra
and s.ao=@codigo_anio
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
group by o.codigo_canton


update #RESULTADO set valor=#RESULTADO2.porc
from #RESULTADO,#RESULTADO2
where #RESULTADO.codigo_canton=#RESULTADO2.codigo_canton

select codigo_canton,valor from #RESULTADO

END









GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE  procedure ProgramacionEjecucionAnual @institucion int,@anio int
as

if (@institucion=999)
BEGIN
select mes as cod_mes,convert(varchar(15),'?') as mes,sum(programado_financiero) as Programado,sum(ejecucion_financiero) as Ejecucin
into #RESULTADO
from seguimiento 
where ao=@anio
group by mes


update #RESULTADO set mes ='Enero' where cod_mes=1
update #RESULTADO set mes ='Febrero' where cod_mes=2
update #RESULTADO set mes ='Marzo' where cod_mes=3
update #RESULTADO set mes ='Abril' where cod_mes=4
update #RESULTADO set mes ='Mayo' where cod_mes=5
update #RESULTADO set mes ='Junio' where cod_mes=6
update #RESULTADO set mes ='Julio' where cod_mes=7
update #RESULTADO set mes ='Agosto' where cod_mes=8
update #RESULTADO set mes ='Septiembre' where cod_mes=9
update #RESULTADO set mes ='Octubre' where cod_mes=10
update #RESULTADO set mes ='Noviembre' where cod_mes=11
update #RESULTADO set mes ='Diciembre' where cod_mes=12

select mes as Mes,Programado,Ejecucin from #RESULTADO order by cod_mes

END
ELSE
BEGIN
select s.mes as cod_mes,convert(varchar(15),'?') as mes,sum(s.programado_financiero) as Programado,
sum(s.ejecucion_financiero) as Ejecucin
into #RESULTADO2
from seguimiento  s,proyectos p,responsables r
where s.ao=@anio
and s.codigo_proyecto=p.codigo_proyecto
and r.codigo_responsable=p.codigo_responsable
and r.codigo_institucion=@institucion
group by mes


update #RESULTADO2 set mes ='Enero' where cod_mes=1
update #RESULTADO2 set mes ='Febrero' where cod_mes=2
update #RESULTADO2 set mes ='Marzo' where cod_mes=3
update #RESULTADO2 set mes ='Abril' where cod_mes=4
update #RESULTADO2 set mes ='Mayo' where cod_mes=5
update #RESULTADO2 set mes ='Junio' where cod_mes=6
update #RESULTADO2 set mes ='Julio' where cod_mes=7
update #RESULTADO2 set mes ='Agosto' where cod_mes=8
update #RESULTADO2 set mes ='Septiembre' where cod_mes=9
update #RESULTADO2 set mes ='Octubre' where cod_mes=10
update #RESULTADO2 set mes ='Noviembre' where cod_mes=11
update #RESULTADO2 set mes ='Diciembre' where cod_mes=12

select mes as Mes,Programado,Ejecucin from #RESULTADO2 order by cod_mes
END







GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE   procedure ProgramacionEjecucionEtapa @usuario varchar(10),@institucion int,@anio int
as

if(@institucion=999)
BEGIN
select '<a href=/servlet/ReportesListado?CODIGO=01&USUARIO='+@usuario+'&PARAMETRO='+convert(varchar(5),e.codigo_etapa)+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'>'+e.nombre_etapa+'</a>' as [Etapa],
sum(s.programado_financiero) as Programado,
sum(s.ejecucion_financiero) as Ejecutado
from proyectos p,seguimiento s,etapas e
where p.codigo_proyecto=s.codigo_proyecto
and p.codigo_etapa=e.codigo_etapa
and s.ao=@anio
group by e.codigo_etapa,e.nombre_etapa


END
ELSE
BEGIN

select '<a href=/servlet/ReportesListado?CODIGO=01&USUARIO='+@usuario+'&PARAMETRO='+convert(varchar(5),e.codigo_etapa)+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'>'+e.nombre_etapa+'</a>' as [Etapa],
sum(s.programado_financiero) as Programado,
sum(s.ejecucion_financiero) as Ejecutado
from proyectos p,seguimiento s,etapas e,responsables r
where p.codigo_proyecto=s.codigo_proyecto
and p.codigo_etapa=e.codigo_etapa
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
group by e.codigo_etapa,e.nombre_etapa


END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO





CREATE     procedure ProgramacionEjecucionFuente @usuario varchar(10),@institucion int,@anio int
as

IF (@institucion=999)
BEGIN
select '<a href=/servlet/ReportesListado?CODIGO=02&USUARIO='+@usuario+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'&PARAMETRO='+f.codigo_fuente+'>'+fp.nombre_fuente+'</a>' as [Fuente de Financiamiento],
sum(s.programado_financiero) as Programacin,
sum(s.ejecucion_financiero) as Ejecucin
from proyectos_fuentes f,proyectos p,seguimiento s,fuentes_financiamiento fp
where p.codigo_proyecto=s.codigo_proyecto
and p.codigo_proyecto=f.codigo_proyecto
and s.ao=@anio
and f.codigo_fuente=fp.codigo_fuente
group by f.codigo_fuente,fp.nombre_fuente
END
ELSE
BEGIN
select '<a href=/servlet/ReportesListado?CODIGO=02&USUARIO='+@usuario+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'&PARAMETRO='+f.codigo_fuente+'>'+fp.nombre_fuente+'</a>' as [Fuente de Financiamiento],
sum(s.programado_financiero) as Programacin,
sum(s.ejecucion_financiero) as Ejecucin
from proyectos_fuentes f,proyectos p,seguimiento s,responsables r,fuentes_financiamiento fp
where p.codigo_proyecto=s.codigo_proyecto
and p.codigo_proyecto=f.codigo_proyecto
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
and f.codigo_fuente=fp.codigo_fuente
group by f.codigo_fuente,fp.nombre_fuente

END





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO





CREATE     procedure ProgramacionEjecucionInstitucion @usuario varchar(10),@institucion int,@anio int
as

if (@institucion=999)
BEGIN
select '<a href=/servlet/ReportesConsolidados?ANIO='+convert(varchar(4),@anio)+'&USUARIO='+@usuario+'&INSTITUCION='+convert(varchar(3),i.codigo_institucion)+'>'+i.nombre_institucion+'</a>' as Institucin,
sum(s.programado_financiero) as Programado,
sum(s.ejecucion_financiero) as Ejecutado
from seguimiento s,proyectos p,instituciones i,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=i.codigo_institucion
and s.ao=@anio
group by i.codigo_institucion,i.nombre_institucion
END
ELSE
BEGIN
select i.nombre_institucion as Institucin,
sum(s.programado_financiero) as Programado,
sum(s.ejecucion_financiero) as Ejecutado
from seguimiento s,proyectos p,instituciones i,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=i.codigo_institucion
and i.codigo_institucion=@institucion
and s.ao=@anio
group by i.nombre_institucion

END






GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO





CREATE     procedure ProgramacionEjecucionUnidadesMedida @usuario varchar(10),@institucion int,@anio int
as




if(@institucion=999)
BEGIN
select '<a href=/servlet/ReportesListado?CODIGO=03&USUARIO='+@usuario+'&PARAMETRO='+convert(varchar(5),u.codigo_unidad)+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'>'+u.nombre_unidad+'</a>' as [Tipos de Obra],
sum (s.programado_financiero) as Programado,
sum(s.ejecucion_financiero) as Ejecutado
from unidades_medida u,proyectos p,obras_fisicas o,seguimiento s
where s.codigo_proyecto=p.codigo_proyecto
and p.codigo_obra=o.codigo_obra
and o.codigo_unidad=u.codigo_unidad
and s.ao=@anio
group by u.codigo_unidad,u.nombre_unidad




END
ELSE
BEGIN
select'<a href=/servlet/ReportesListado?CODIGO=03&USUARIO='+@usuario+'&PARAMETRO='+convert(varchar(5),u.codigo_unidad)+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'>'+u.nombre_unidad+'</a>' as [Tipos de Obra],
sum (s.programado_financiero) as Programado,
sum(s.ejecucion_financiero) as Ejecutado
from unidades_medida u,proyectos p,obras_fisicas o,seguimiento s,responsables r
where s.codigo_proyecto=p.codigo_proyecto
and p.codigo_obra=o.codigo_obra
and o.codigo_unidad=u.codigo_unidad
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and s.ao=@anio
group by u.codigo_unidad,u.nombre_unidad
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE procedure ProyectosEtapa @etapa int, @institucion int,@anio int
as

if(@institucion=999)
BEGIN
select o.nombre_obra as Obra,p.nombre_proyecto as Proyecto,t.nombre_tipo as [Tipo de Proyecto],
p.empleos_proyecto as [Empleos Generados],
p.beneficiarios_proyecto as [Beneficiarios],
convert(varchar(20),convert(money,sum(s.programado_financiero))) as [Monto Programado],
convert(money,convert(varchar(20),sum(s.ejecucion_financiero))) as [Monto Ejecutado]
from proyectos p,tipos_proyecto t,seguimiento s,obras_fisicas o
where p.codigo_tipo=t.codigo_tipo
and p.codigo_etapa= @etapa
and p.codigo_proyecto=s.codigo_proyecto
and s.ao=@anio
and o.codigo_obra=p.codigo_obra
group by o.nombre_obra,p.nombre_proyecto,t.nombre_tipo,p.empleos_proyecto,p.beneficiarios_proyecto
END
else
BEGIN
select o.nombre_obra as Obra,p.nombre_proyecto as Proyecto,t.nombre_tipo as [Tipo de Proyecto],
p.empleos_proyecto as [Empleos Generados],
p.beneficiarios_proyecto as [Beneficiarios],
convert(money,convert(varchar(20),sum(s.programado_financiero))) as [Monto Programado],
convert(money,convert(varchar(20),sum(s.ejecucion_financiero))) as [Monto Ejecutado]
from proyectos p,tipos_proyecto t,seguimiento s,responsables r,obras_fisicas o
where p.codigo_tipo=t.codigo_tipo
and p.codigo_etapa= @etapa
and p.codigo_proyecto=s.codigo_proyecto
and s.ao=@anio
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and o.codigo_obra=p.codigo_obra
group by o.nombre_obra,p.nombre_proyecto,t.nombre_tipo,p.empleos_proyecto,p.beneficiarios_proyecto

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE procedure ProyectosFuente @fuente char(10),@institucion int,@anio int
as
if (@institucion=999)
BEGIN

select o.codigo_obra,o.nombre_obra,p.codigo_proyecto,p.nombre_proyecto,
t.nombre_tipo, e.nombre_etapa,
sum(s.programado_financiero ) as monto_total,
convert(numeric(20,2),0) as monto_fuente
into #RESULTADO1
from obras_fisicas o,proyectos p,seguimiento s,etapas e,tipos_proyecto t
where o.codigo_obra=p.codigo_obra
and p.codigo_obra=s.codigo_obra
and p.codigo_proyecto=s.codigo_proyecto
and p.codigo_tipo=t.codigo_tipo
and p.codigo_etapa=e.codigo_etapa
and s.ao=@anio
group by o.codigo_obra,o.nombre_obra,p.codigo_proyecto,p.nombre_proyecto,t.nombre_tipo,e.nombre_etapa


select f.codigo_obra,f.codigo_proyecto,f.monto
into #RESULTADO2
from proyectos_fuentes f
where f.codigo_fuente=@fuente

update #RESULTADO1 set monto_fuente=#RESULTADO2.monto
from #RESULTADO1,#RESULTADO2
where #RESULTADO1.codigo_obra=#RESULTADO2.codigo_obra
and #RESULTADO1.codigo_proyecto=#RESULTADO2.codigo_proyecto

delete #RESULTADO1 where monto_fuente=0

select nombre_obra as Obra, nombre_proyecto as Proyecto, 
nombre_tipo as [Tipo de Proyecto],nombre_etapa as [Etapa],
monto_Total as [Monto Total de Inversin],
monto_fuente as [Monto Financiado]
from #RESULTADO1

END
else
BEGIN

select o.codigo_obra,o.nombre_obra,p.codigo_proyecto,p.nombre_proyecto,
t.nombre_tipo, e.nombre_etapa,
sum(s.programado_financiero ) as monto_total,
convert(numeric(20,2),0) as monto_fuente
into #RESULTADO3
from obras_fisicas o,proyectos p,seguimiento s,etapas e,tipos_proyecto t,responsables r
where o.codigo_obra=p.codigo_obra
and p.codigo_obra=s.codigo_obra
and p.codigo_proyecto=s.codigo_proyecto
and p.codigo_tipo=t.codigo_tipo
and p.codigo_etapa=e.codigo_etapa
and s.ao=@anio
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
group by o.codigo_obra,o.nombre_obra,p.codigo_proyecto,p.nombre_proyecto,t.nombre_tipo,e.nombre_etapa


select f.codigo_obra,f.codigo_proyecto,f.monto
into #RESULTADO4
from proyectos_fuentes f
where f.codigo_fuente=@fuente

update #RESULTADO3 set monto_fuente=#RESULTADO4.monto
from #RESULTADO3,#RESULTADO4
where #RESULTADO3.codigo_obra=#RESULTADO4.codigo_obra
and #RESULTADO3.codigo_proyecto=#RESULTADO4.codigo_proyecto

delete #RESULTADO3 where monto_fuente=0

select nombre_obra as Obra, nombre_proyecto as Proyecto, 
nombre_tipo as [Tipo de Proyecto],nombre_etapa as [Etapa],
monto_Total as [Monto Total de Inversin],
monto_fuente as [Monto Financiado]
from #RESULTADO3


END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE  procedure ProyectosObra @obra int,@institucion int,@anio int
as 

if @institucion=999
BEGIN
select p.nombre_proyecto as Proyecto,
t.nombre_tipo as [Tipo de Proyecto],
e.nombre_etapa as Etapa,
p.empleos_proyecto as [Empleos Generados],
p.beneficiarios_proyecto as [Beneficiarios],
sum(s.programado_financiero) as [Monto Programado],
sum(s.ejecucion_financiero) as [Monto Ejecutado]
from proyectos p,obras_fisicas o,tipos_proyecto t,etapas e,seguimiento s
where o.codigo_obra=p.codigo_obra
and p.codigo_proyecto=s.codigo_proyecto
and p.codigo_obra=s.codigo_obra
and p.codigo_tipo=t.codigo_Tipo
and p.codigo_etapa=e.codigo_etapa
and s.ao=@anio
and o.codigo_obra=@obra
group by p.nombre_proyecto,t.nombre_tipo,e.nombre_etapa,
p.empleos_proyecto,p.beneficiarios_proyecto
END
ELSE
BEGIN
select p.nombre_proyecto as Proyecto,
t.nombre_tipo as [Tipo de Proyecto],
e.nombre_etapa as Etapa,
p.empleos_proyecto as [Empleos Generados],
p.beneficiarios_proyecto as [Beneficiarios],
sum(s.programado_financiero) as [Monto Programado],
sum(s.ejecucion_financiero) as [Monto Ejecutado]
from proyectos p,obras_fisicas o,tipos_proyecto t,etapas e,seguimiento s,responsables r
where o.codigo_obra=p.codigo_obra
and p.codigo_proyecto=s.codigo_proyecto
and p.codigo_obra=s.codigo_obra
and p.codigo_tipo=t.codigo_Tipo
and p.codigo_etapa=e.codigo_etapa
and s.ao=@anio
and o.codigo_obra=@obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
group by p.nombre_proyecto,t.nombre_tipo,e.nombre_etapa,
p.empleos_proyecto,p.beneficiarios_proyecto
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE procedure RetornarAnios
@buscarpor char(1),
@codigo varchar(3),
@consulta char(1)
as

if @buscarpor='N'
begin
	if @consulta='0'
		Select Count(*) from Seguimiento
	else
		Select distinct (ao) From Seguimiento
end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE procedure RetornarDepartamentos
@buscarpor char(1),
@codigo varchar(3),
@consulta char(1)
as

if @buscarpor='N'
begin
	if @consulta='0'
		Select Count(*) from Departamentos
	else
		Select * From Departamentos
end
else
begin
	if @buscarpor='i'
	begin
		if @consulta='0'		
		begin
			Select Count(distinct d.codigo_departamento)
			From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
			Responsables r, Instituciones i
			Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
			and c.codigo_municipio=m.codigo_municipio
			and m.codigo_departamento=d.codigo_departamento
			and p.codigo_responsable=r.codigo_responsable
			and r.codigo_institucion=i.codigo_institucion
			and i.codigo_institucion=@codigo
		end
		else
		begin
			Select distinct d.*
			From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
				Responsables r, Instituciones i
			Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
			and c.codigo_municipio=m.codigo_municipio
			and m.codigo_departamento=d.codigo_departamento
			and p.codigo_responsable=r.codigo_responsable
			and r.codigo_institucion=i.codigo_institucion
			and i.codigo_institucion=@codigo
		end
	end
	else
	begin
		if @buscarpor='t'
		begin
			if @consulta='0'		
			begin
				Select Count(*)
				From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
				Unidades_Medida u
				Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
				and c.codigo_municipio=m.codigo_municipio
				and m.codigo_departamento=d.codigo_departamento
				and o.codigo_unidad=u.codigo_unidad
				and u.codigo_unidad=@codigo
			end
			else
			begin
				Select d.*
				From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
				Unidades_Medida u
				Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
				and c.codigo_municipio=m.codigo_municipio
				and m.codigo_departamento=d.codigo_departamento
				and o.codigo_unidad=u.codigo_unidad
				and u.codigo_unidad=@codigo
			end
		end
	end
end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE  procedure RetornarInstituciones
@buscarpor char(1),
@codigo varchar(3),
@consulta char(1)
as

if @buscarpor='N'
begin
	if @consulta='0'
		Select Count(*) from Instituciones
	else
	begin
		if @consulta='1'
			Select * From Instituciones
		else
			Select * From Instituciones where Codigo_Institucion=@codigo
	end
end
else
begin
	if @buscarpor='d'
	begin
		if @consulta='0'		
		begin
			Select Count(*) From Instituciones
			Where codigo_institucion in
			(
				Select codigo_institucion from responsables
				Where codigo_responsable in 
				(
					Select p.codigo_responsable
					From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d
					Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
					and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and d.codigo_departamento=convert(int,@codigo)
				)
			)
		end
		else
		begin
			Select * From Instituciones
			Where codigo_institucion in
			(
				Select codigo_institucion from responsables
				Where codigo_responsable in 
				(
					Select p.codigo_responsable
					From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d
					Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
					and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and d.codigo_departamento=convert(int,@codigo)
				)
			)				
		end
	end
	else
	begin
		if @buscarpor='t'
		begin
			if @consulta='0'		
			begin
				Select Count(*) From Instituciones
				Where codigo_institucion in
				(
					Select codigo_institucion from responsables
					Where codigo_responsable in 
					(
						Select p.codigo_responsable
						From Proyectos p, Obras_Fisicas o, Unidades_Medida as u
						Where p.codigo_obra=o.codigo_obra
						and o.codigo_unidad=u.codigo_unidad
						and u.codigo_unidad=@codigo
					)
				)
			end
			else
			begin
				Select * From Instituciones
				Where codigo_institucion in
				(
					Select codigo_institucion from responsables
					Where codigo_responsable in 
					(
						Select p.codigo_responsable
						From Proyectos p, Obras_Fisicas o, Unidades_Medida as u
						Where p.codigo_obra=o.codigo_obra
						and o.codigo_unidad=u.codigo_unidad
						and u.codigo_unidad=@codigo
					)
				)				
			end
		end
	end
end



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE procedure RetornarMunicipios
@buscarpor char(1),
@codigo varchar(3),
@consulta char(1)
as

if @buscarpor='N'
begin
	if @consulta='0'
		Select Count(*) from Municipios
	else
		Select * From Municipios
end
else
begin
	if @buscarpor='i'
	begin
		if @consulta='0'		
		begin
			Select Count(distinct d.codigo_departamento)
			From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
			Responsables r, Instituciones i
			Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
			and c.codigo_municipio=m.codigo_municipio
			and m.codigo_departamento=d.codigo_departamento
			and p.codigo_responsable=r.codigo_responsable
			and r.codigo_institucion=i.codigo_institucion
			and i.codigo_institucion=@codigo
		end
		else
		begin
			Select distinct d.*
			From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
				Responsables r, Instituciones i
			Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
			and c.codigo_municipio=m.codigo_municipio
			and m.codigo_departamento=d.codigo_departamento
			and p.codigo_responsable=r.codigo_responsable
			and r.codigo_institucion=i.codigo_institucion
			and i.codigo_institucion=@codigo
		end
	end
	else
	begin
		if @buscarpor='t'
		begin
			if @consulta='0'		
			begin
				Select Count(*)
				From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
				Unidades_Medida u
				Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
				and c.codigo_municipio=m.codigo_municipio
				and m.codigo_departamento=d.codigo_departamento
				and o.codigo_unidad=u.codigo_unidad
				and u.codigo_unidad=@codigo
			end
			else
			begin
				Select distinct m.*
				From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
				Unidades_Medida u
				Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
				and c.codigo_municipio=m.codigo_municipio
				and m.codigo_departamento=d.codigo_departamento
				and o.codigo_unidad=u.codigo_unidad
				and u.codigo_unidad=@codigo
			end
		end
		else
			if @buscarpor='d'
			begin
				if @consulta='0'		
				begin
					Select Count(*)
					From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
					Unidades_Medida u
					Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
					and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and o.codigo_unidad=u.codigo_unidad
					and d.codigo_departamento=@codigo
				end
				else
				begin
					Select distinct m.*
					From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d,
					Unidades_Medida u
					Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
					and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and o.codigo_unidad=u.codigo_unidad
					and d.codigo_departamento=@codigo
				end
			end
	end
end



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE   procedure RetornarObras
@institucion int,
@tipoObra varchar(50),
@departamento int,
@etapa int,
@municipio int,
@anio int,
@todos int =0

as

if @institucion<>999
begin
	if @tipoObra<>'-1'
	begin
		if @departamento<>-1
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and m.codigo_municipio=@municipio
				end
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and i.codigo_institucion=@institucion
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and m.codigo_municipio=@municipio
				end
				else
					begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and i.codigo_institucion=@institucion
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
		end
		else
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i, municipios m, cantones c,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i, municipios m, cantones c,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and i.codigo_institucion=@institucion
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and m.codigo_municipio=@municipio
				end
				else
					begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and i.codigo_institucion=@institucion
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
		end
	end
	else
	begin

		if @departamento<>-1
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and i.codigo_institucion=@institucion
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
					and m.codigo_municipio=@municipio
				end
				else
					begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and i.codigo_institucion=@institucion
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
				end
			end
		end
		else
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i, municipios m, cantones c,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and			
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and p.codigo_etapa=e.codigo_etapa
					and i.codigo_institucion=@institucion and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i, municipios m, cantones c,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and i.codigo_institucion=@institucion
					and o.codigo_unidad=u.codigo_unidad
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r, instituciones i,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra and
					p.codigo_responsable=r.codigo_responsable and r.codigo_institucion=i.codigo_institucion
					and i.codigo_institucion=@institucion
					and o.codigo_unidad=u.codigo_unidad
				end
			end
		end
	end
end
else
begin

	if @tipoObra<>'-1'
	begin
		if @departamento<>-1
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
		end
		else
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, municipios m, cantones c,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and p.codigo_etapa=e.codigo_etapa
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and p.codigo_etapa=e.codigo_etapa
					and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, municipios m, cantones c,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_unidad=u.codigo_unidad and u.codigo_unidad=@tipoObra
				end
			end
		end
	end
	else
	begin

		if @departamento<>-1
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra			
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, responsables r,
					departamentos d, municipios m, cantones c, etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra			
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and p.codigo_etapa=e.codigo_etapa
					and e.codigo_etapa=@etapa
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					departamentos d, municipios m, cantones c, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_departamento=d.codigo_departamento
					and d.codigo_departamento=@departamento
					and o.codigo_unidad=u.codigo_unidad
				end
			end
		end
		else
		begin
			if @etapa<>-1
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, municipios m, cantones c,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra		
					and p.codigo_etapa=e.codigo_etapa
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					etapas e, unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra		
					and p.codigo_etapa=e.codigo_etapa
					and e.codigo_etapa=@etapa
					and o.codigo_unidad=u.codigo_unidad
				end
			end
			else
			begin
				if @municipio<>-1
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p, municipios m, cantones c,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_unidad=u.codigo_unidad
					and o.codigo_canton=c.codigo_canton and c.codigo_municipio=m.codigo_municipio
					and m.codigo_municipio=@municipio
				end
				else
				begin
					SELECT distinct u.* FROM obras_fisicas o,
					proyectos p,
					unidades_medida u
					WHERE o.codigo_obra=p.codigo_obra
					and o.codigo_unidad=u.codigo_unidad
				end
			end
		end
	end
end




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE procedure RetornarTipoObra
@buscarpor char(1),
@codigo varchar(3),
@consulta char(1)
as

if @buscarpor='N'
begin
	if @consulta='0'
		Select Count(*) from Unidades_Medida
	else
		Select * From Unidades_Medida
end
else
begin
	if @buscarpor='d'
	begin
		if @consulta='0'		
		begin
			Select Count(*) From Unidades_Medida
			Where codigo_unidad in
			(
				Select o.codigo_unidad
				From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d
				Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
				and c.codigo_municipio=m.codigo_municipio
				and m.codigo_departamento=d.codigo_departamento
				and d.codigo_departamento=convert(int,@codigo)
			)
		end
		else
		begin
			Select * From Unidades_Medida
			Where codigo_unidad in
			(
				Select o.codigo_unidad
				From Proyectos p, Obras_Fisicas o, cantones c, municipios m, departamentos d
				Where p.codigo_obra=o.codigo_obra and o.codigo_canton=c.codigo_canton
				and c.codigo_municipio=m.codigo_municipio
				and m.codigo_departamento=d.codigo_departamento
				and d.codigo_departamento=convert(int,@codigo)
			)
		end
	end
	else
	begin
		if @buscarpor='i'
		begin
			if @consulta='0'		
			begin
				Select Count(*) From Unidades_Medida
				Where codigo_unidad in
				(
					Select o.codigo_unidad
					From Proyectos p, Obras_Fisicas o, Responsables as r, Instituciones i
					Where p.codigo_obra=o.codigo_obra
					and p.codigo_responsable=r.codigo_responsable
					and r.codigo_institucion=i.codigo_institucion
					and i.codigo_institucion=@codigo
				)
			end
			else
			begin
				Select * From Unidades_Medida
				Where codigo_unidad in
				(
					Select o.codigo_unidad
					From Proyectos p, Obras_Fisicas o, Responsables as r, Instituciones i
					Where p.codigo_obra=o.codigo_obra
					and p.codigo_responsable=r.codigo_responsable
					and r.codigo_institucion=i.codigo_institucion
					and i.codigo_institucion=@codigo
				)
			end
		end
	end
end

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO




CREATE   procedure TiposObra @unidad int,@institucion int,@anio int,@usuario varchar(10)
as
if @institucion=999
BEGIN
select o.codigo_obra,o.nombre_obra, 
convert(numeric(20,2),0) as [Empleos],
sum(s.programado_financiero) as [Monto Programado],
sum(s.ejecucion_financiero) as [Monto Ejecutado]
into #RESULTADO1
from obras_fisicas o,seguimiento s,unidades_medida u
where o.codigo_obra=s.codigo_obra
and o.codigo_unidad=u.codigo_unidad
and u.codigo_unidad =@unidad
and s.ao=@anio
group by o.codigo_obra,o.nombre_obra

select o.codigo_obra,sum(p.empleos_proyecto)as empleos
into #RESULTADO2
from obras_fisicas o,proyectos p,unidades_medida u
where o.codigo_obra=p.codigo_obra
and o.codigo_unidad=u.codigo_unidad
and u.codigo_unidad=@unidad
group by o.codigo_obra

update  #RESULTADO1 set empleos =#RESULTADO2.empleos
from #RESULTADO1,#RESULTADO2
where #RESULTADO1.codigo_obra=#RESULTADO2.codigo_obra

select '<a href=/servlet/ReportesListado?CODIGO=04&USUARIO='+@usuario+'&PARAMETRO='+convert(varchar(5),codigo_obra)+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'>'+nombre_obra+'</a>' as [Obra],
empleos as [Empleos Generados],
[Monto Programado],
[Monto Ejecutado]
from #RESULTADO1


END
ELSE
BEGIN
select o.codigo_obra,o.nombre_obra, 
convert(numeric(20,2),0) as [Empleos],
sum(s.programado_financiero) as [Monto Programado],
sum(s.ejecucion_financiero) as [Monto Ejecutado]
into #RESULTADO3
from obras_fisicas o,seguimiento s,proyectos p,responsables r,unidades_medida u
where o.codigo_obra=s.codigo_obra
and o.codigo_obra=p.codigo_obra
and p.codigo_proyecto=s.codigo_proyecto
and p.codigo_obra=s.codigo_obra
and p.codigo_responsable=r.codigo_responsable
and r.codigo_institucion=@institucion
and o.codigo_unidad=u.codigo_unidad
and u.codigo_unidad =@unidad
and s.ao=@anio
group by o.codigo_obra,o.nombre_obra

select o.codigo_obra,sum(p.empleos_proyecto)as empleos
into #RESULTADO4
from obras_fisicas o,proyectos p,unidades_medida u
where o.codigo_obra=p.codigo_obra
and o.codigo_unidad=u.codigo_unidad
and u.codigo_unidad=@unidad
group by o.codigo_obra

update  #RESULTADO3 set empleos =#RESULTADO4.empleos
from #RESULTADO3,#RESULTADO4
where #RESULTADO3.codigo_obra=#RESULTADO4.codigo_obra


select '<a href=/servlet/ReportesListado?CODIGO=04&USUARIO='+@usuario+'&PARAMETRO='+convert(varchar(5),codigo_obra)+'&INSTITUCION='+convert(varchar(3),@institucion)+'&ANIO='+convert(varchar(4),@anio)+'>'+nombre_obra+'</a>' as [Obra],
empleos as [Empleos Generados],
[Monto Programado],
[Monto Ejecutado]
from #RESULTADO3
END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE procedure ToolTipObra @obra int
as

select o.codigo_obra, o.nombre_obra,
convert(numeric(25,2),0) as inversion,
convert(numeric(25,2),0) as proyectos,
convert(numeric(25,2),0) as necesidades,
convert(numeric(25,2),0) as empleos,
convert(numeric(25,2),0) as porcentaje
into #RESULTADO
from obras_fisicas o
where o.codigo_obra=@obra


select p.codigo_obra,sum(s.programado_financiero) as inversion,
porcentaje=
	Case sum(s.ejecucion_financiero)
		When 0 then 0.00 else sum(s.programado_financiero)/sum(s.ejecucion_financiero)*100	
	end
into #RESULTADO1
from proyectos p,seguimiento s
where s.codigo_obra=p.codigo_obra
and s.codigo_proyecto=p.codigo_proyecto
and p.codigo_obra=@obra
group by p.codigo_obra

update #RESULTADO set inversion=#RESULTADO1.inversion,
porcentaje=#RESULTADO1.porcentaje
from #RESULTADO,#RESULTADO1
where #RESULTADO.codigo_obra=#RESULTADO1.codigo_obra


select codigo_obra,count(codigo_proyecto) as proyectos into #RESULTADO2 from proyectos where codigo_obra=@obra  group by codigo_obra
select codigo_obra,count(codigo_necesidad) as necesidades into #RESULTADO3 from necesidades where codigo_obra=@obra  group by codigo_obra
select codigo_obra,sum(empleos_proyecto) as empleos into #RESULTADO4 from proyectos where codigo_obra=@obra group by codigo_obra


update #RESULTADO set proyectos =#RESULTADO2.proyectos
from #RESULTADO,#RESULTADO2
where #RESULTADO.codigo_obra=#RESULTADO2.codigo_obra



update #RESULTADO set necesidades =#RESULTADO3.necesidades
from #RESULTADO,#RESULTADO3
where #RESULTADO.codigo_obra=#RESULTADO3.codigo_obra



update #RESULTADO set empleos =#RESULTADO4.empleos
from #RESULTADO,#RESULTADO4
where #RESULTADO.codigo_obra=#RESULTADO4.codigo_obra



select nombre_obra,convert(varchar(20),convert(money,inversion),1),
proyectos,necesidades,empleos,
convert(varchar(7),porcentaje)+'%' 
from #RESULTADO


--exec ToolTipObra 32

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

