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

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

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

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

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

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

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

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

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

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMarcas @pmIdMarca VARCHAR(4),@pmMarca VARCHAR(50),@pmTipoMarca VARCHAR(20)
,@pmCodigoMT VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Marcas (IdMarca,Marca,TipoMarca,CodigoMT,Inactivo)
 VALUES (@pmIdMarca,@pmMarca,@pmTipoMarca,@pmCodigoMT,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehRemolq @pmIdRemque VARCHAR(10),@pmIdMarca VARCHAR(4),@pmIdColor VARCHAR(4),@pmIdCrceria VARCHAR(4),@pmModelo VARCHAR(4),@pmNEjes INT,@pmnLlantas INT,@pmAltura DECIMAL(14,4)
,@pmAncho DECIMAL(14,4),@pmLargo DECIMAL(14,4),@pmVlumen DECIMAL(14,4),@pmPesoKg DECIMAL(14,4),@pmCapneta DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmNumComp INT,@pmCapcComp VARCHAR(50),@pmNumSerial VARCHAR(30)
,@pmIdPropietario VARCHAR(16),@pmNitProv VARCHAR(16),@pmFecCompra SMALLDATETIME,@pmVidaUtil INT,@pmVrComercial MONEY,@pmVrAvaludo MONEY,@pmVrAsegurado MONEY,@pmKilmetraje INT,@pmEquipoAdic VARCHAR(50)
,@pmDescripcion VARCHAR(100),@pmCdCenSer VARCHAR(4),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTarjetaProp VARCHAR(30),@pmFecTarjProp  SMALLDATETIME
,@pmVigTarjProp SMALLDATETIME,@pmConfEjes VARCHAR(5),@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO VehRemolq (IdRemque,IdMarca,IdColor,IdCrceria,Modelo,NEjes,nLlantas,ConfEjes,Altura,Ancho,Largo,Vlumen,PesoKg,Capneta,UndCapc,NumComp,CapcComp,NumSerial,IdPropietario,NitProv,FecCompra,VidaUtil,VrComercial,VrAvaludo,VrAsegurado,Kilmetraje
,EquipoAdic,Descripcion,CdCenSer,CdLocal,Ubicacion,Observacion,IdEstado,Inactivo,FecAdd,IdUsuario,TarjetaProp,FecTarjProp,VigTarjProp)
 VALUES (@pmIdRemque,@pmIdMarca,@pmIdColor,@pmIdCrceria,@pmModelo,@pmNEjes,@pmnLlantas,@pmConfEjes,@pmAltura,@pmAncho,@pmLargo,@pmVlumen,@pmPesoKg,@pmCapneta,@pmUndCapc,@pmNumComp,@pmCapcComp,@pmNumSerial,@pmIdPropietario
,@pmNitProv,@pmFecCompra,@pmVidaUtil,@pmVrComercial,@pmVrAvaludo,@pmVrAsegurado,@pmKilmetraje,@pmEquipoAdic,@pmDescripcion,@pmCdCenSer,@pmCdLocal,@pmUbicacion,@pmObservacion,@pmIdEstado,@pmInactivo,@pmFecAdd,@pmIdUsuario,@pmTarjetaProp,@pmFecTarjProp,@pmVigTarjProp)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMarcas @pmIdMarca VARCHAR(4)
 AS 
 SELECT IdMarca,Marca,TipoMarca,CodigoMT,Inactivo
 FROM Marcas
 WHERE IdMarca=@pmIdMarca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMarcasLta  @pmTipoMarca VARCHAR(20)=Null
 AS 
 SELECT IdMarca,Marca,TipoMarca,CodigoMT
 FROM Marcas
 WHERE Inactivo=0  AND ( TipoMarca='GENERAL'  OR  TipoMarca LIKE ISNULL(@pmTipoMarca,'%') )
 ORDER BY Marca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehRemolq @pmIdRemque VARCHAR(10)
 AS 
 SELECT IdRemque,IdMarca,IdColor,IdCrceria,Modelo,NEjes,nLlantas,Altura,Ancho,Largo,Vlumen,PesoKg,Capneta,UndCapc,NumComp,CapcComp
,NumSerial,IdPropietario,NitProv,FecCompra,VidaUtil,VrComercial,VrAvaludo,VrAsegurado,Kilmetraje,EquipoAdic,Descripcion,CdCenSer,CdLocal,Ubicacion
,Observacion,IdEstado,Inactivo,FecAdd,FecUpdate,IdUsuario,TarjetaProp,FecTarjProp,VigTarjProp,ConfEjes
 FROM VehRemolq
 WHERE IdRemque=@pmIdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehRemolqLta @pmIdMarca VARCHAR(4)=Null,@pmIdCrceria VARCHAR(4)=Null,@pmModelo VARCHAR(4)=Null
,@pmIdPropietario VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmFecComIni SMALLDATETIME=Null,@pmFecComFin SMALLDATETIME=Null
 AS 
 SELECT IdRemque,R.IdMarca AS CdMarca,Marca,R.IdColor AS CodColor,NomColor,R.IdCrceria AS CodCarr,TipoCar,Modelo,ConfEjes,NEjes,nLlantas,Altura,Ancho,Largo,Vlumen
,PesoKg,Capneta,UndCapc,NumComp,CapcComp,NumSerial,IdPropietario,NP.RazonSocial AS Propietario,NitProv,NPV.RazonSocial AS Proveedor,FecCompra,VidaUtil
,VrComercial,VrAvaludo,VrAsegurado,Kilmetraje,EquipoAdic,Descripcion,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion
,LU.IdDep AS CodDpto,Departamento,Ubicacion,R.Observacion AS Observ,R.IdEstado AS CodEstado,Estado,R.Inactivo AS Inactiv,R.IdUsuario AS IdUsuari,Usuario
,FecAdd,FecUpdate,EV.NColor AS NumColor,OutDemand,TarjetaProp,FecTarjProp,VigTarjProp
 FROM VehRemolq AS R INNER JOIN Marcas AS M ON R.IdMarca=M.IdMarca  INNER JOIN TiposCol AS CL ON R.IdColor=CL.IdColor
 INNER JOIN TiposCar AS TC ON R.IdCrceria=TC.IdCrceria  INNER JOIN Terceros AS NP ON  R.IdPropietario=NP.IdTercero
 INNER JOIN EstadoVeh AS EV ON R.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS NPV ON R.NitProv=NPV.IdTercero
 LEFT JOIN CentrosServ AS CS ON R.CdCenSer =CS.IdCenSer 
 LEFT JOIN Localidades AS LU ON R.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
 WHERE R.IdMarca LIKE ISNULL(@pmIdMarca,'%')  AND R.IdCrceria LIKE ISNULL(@pmIdCrceria,'%')
 AND Modelo LIKE ISNULL(@pmModelo,'%')  AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (R.Inactivo=ISNULL(@pmInactivo,0) or R.Inactivo=ISNULL(@pmInactivo,1))
 AND (FecCompra>=ISNULL(@pmFecComIni,CAST('19100101' AS SMALLDATETIME)) AND FecCompra<=ISNULL(@pmFecComFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY  IdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehRemolq @pmIdRemque VARCHAR(10),@pmIdMarca VARCHAR(4),@pmIdColor VARCHAR(4),@pmIdCrceria VARCHAR(4),@pmModelo VARCHAR(4),@pmNEjes INT,@pmnLlantas INT,@pmAltura DECIMAL(14,4),@pmAncho DECIMAL(14,4),@pmLargo DECIMAL(14,4)
,@pmVlumen DECIMAL(14,4),@pmPesoKg DECIMAL(14,4),@pmCapneta DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmNumComp INT,@pmCapcComp VARCHAR(50),@pmNumSerial VARCHAR(30),@pmIdPropietario VARCHAR(16),@pmNitProv VARCHAR(16),@pmFecCompra SMALLDATETIME
,@pmVidaUtil INT,@pmVrComercial MONEY,@pmVrAvaludo MONEY,@pmVrAsegurado MONEY,@pmKilmetraje INT,@pmEquipoAdic VARCHAR(50),@pmDescripcion VARCHAR(100),@pmCdCenSer VARCHAR(4),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100),@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTarjetaProp VARCHAR(30),@pmFecTarjProp  SMALLDATETIME,@pmVigTarjProp SMALLDATETIME,@pmConfEjes VARCHAR(5),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE VehRemolq SET IdMarca=@pmIdMarca,IdColor=@pmIdColor,IdCrceria=@pmIdCrceria,Modelo=@pmModelo,NEjes=@pmNEjes,nLlantas=@pmnLlantas,ConfEjes=@pmConfEjes,Altura=@pmAltura,Ancho=@pmAncho,Largo=@pmLargo,Vlumen=@pmVlumen,PesoKg=@pmPesoKg,Capneta=@pmCapneta,UndCapc=@pmUndCapc
,NumComp=@pmNumComp,CapcComp=@pmCapcComp,NumSerial=@pmNumSerial,IdPropietario=@pmIdPropietario,NitProv=@pmNitProv,FecCompra=@pmFecCompra,VidaUtil=@pmVidaUtil,VrComercial=@pmVrComercial,VrAvaludo=@pmVrAvaludo,VrAsegurado=@pmVrAsegurado,Kilmetraje=@pmKilmetraje
,EquipoAdic=@pmEquipoAdic,Descripcion=@pmDescripcion,CdCenSer=@pmCdCenSer,CdLocal=@pmCdLocal,Ubicacion=@pmUbicacion,Observacion=@pmObservacion,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate,TarjetaProp=@pmTarjetaProp,FecTarjProp=@pmFecTarjProp,VigTarjProp=@pmVigTarjProp
 WHERE IdRemque=@pmIdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMarcas @pmIdMarca VARCHAR(4),@pmMarca VARCHAR(50),@pmTipoMarca VARCHAR(20)
,@pmCodigoMT VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Marcas SET Marca=@pmMarca,TipoMarca=@pmTipoMarca,CodigoMT=@pmCodigoMT,Inactivo=@pmInactivo
 WHERE IdMarca=@pmIdMarca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercPropietario_Sel @pmIdPropietario VARCHAR(16),@pmNewPropietario VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdPropietario FROM  TercPropietario WHERE  IdPropietario=@pmIdPropietario)
  INSERT INTO TercPropietario (IdPropietario,IdGrupo,CupoCredito,SaldoActual,Avala,IdAdmon,IdTipCon,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,Observacion,IdEstado,FechaAdd,IdUsuario)
  SELECT @pmNewPropietario,IdGrupo,CupoCredito,SaldoActual,Avala,IdAdmon,IdTipCon,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,Observacion,IdEstado,FechaAdd,IdUsuario
  FROM TercPropietario WHERE  IdPropietario=@pmIdPropietario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

