INSERT INTO adm_Opciones  (IdOpc,Opcion,TipoDato,Valor,NivUp,nModulo)
 VALUES ('CC1','CIERRE DE CUENTA CORRIENTE SOBRE SALDOS AL DIA','BOOLEAN','0',5,'BUSES')
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAcuPropietariosSalCue]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAcuPropietariosSalCue]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAcuPropietariosSalPro]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAcuPropietariosSalPro]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryAcuPropietariosSalCue @pmnAnno INT,@pmnMes INT
,@pmSaldoActual MONEY,@pmIdAdmon VARCHAR(4)
AS
 SELECT V.IdPropietario AS NitPropietario,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS StotSal
 FROM  AcuPropietarios AS A INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND V.IdAdmon<>@pmIdAdmon AND V.TipoAfil<>'PROPIO'
 GROUP BY V.IdPropietario
 HAVING  SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos)<=@pmSaldoActual
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryAcuPropietariosSalPro @pmnAnno INT,@pmnMes INT, @pmIdPropietario VARCHAR(16)
,@pmIdAdmon VARCHAR(4)
AS
 SELECT V.IdVehiculo AS PlacaVeh,V.IdPropietario AS NitPropietario,NumVeh,IdAdmon,TipoAfil
,IdConductor,RazonSocial,IdGrupo,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS StotSal
 FROM  AcuPropietarios AS A INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS T ON V.IdConductor=T.IdTercero
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND V.IdPropietario=@pmIdPropietario
 AND V.IdAdmon<>@pmIdAdmon AND V.TipoAfil<>'PROPIO'
 GROUP BY  V.IdVehiculo,V.IdPropietario,NumVeh,IdAdmon,TipoAfil,IdConductor,RazonSocial,IdGrupo
 ORDER BY  V.IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

