if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposMotdevDso]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposMotdevDso]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposMotdev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposMotdev]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTiposMotdev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpTiposMotdev]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTiposMotdev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsTiposMotdev]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTraOrdenAntApp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpTraOrdenAntApp]
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTraOrdenAntApp] @pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPoseedor VARCHAR(16)=Null
 AS
 UPDATE Trn_TraOrdenAnt SET Trn_TraOrdenAnt.VrAbonado=(SELECT ISNULL(SUM(Trn_TraOrdenAbo.VrAbono),0)
 FROM Trn_TraOrdenAbo WHERE Trn_TraOrdenAbo.TipAnt=Trn_TraOrdenAnt.TipDoc AND Trn_TraOrdenAbo.Anticipo=Trn_TraOrdenAnt.Anticipo AND Trn_TraOrdenAbo.IdCiaAnt=Trn_TraOrdenAnt.IdCia)
 FROM Trn_TraOrdenAnt INNER JOIN Trn_TraOrdenServ ON Trn_TraOrdenAnt.TipOds=Trn_TraOrdenServ.TipDoc AND Trn_TraOrdenAnt.NumOrden=Trn_TraOrdenServ.NumOrden AND Trn_TraOrdenAnt.IdCiaOds=Trn_TraOrdenServ.IdCia
 WHERE (Trn_TraOrdenAnt.IdCia=@pmIdCia OR @pmIdCia IS NULL)
 AND (Trn_TraOrdenServ.IdVehiculo=@pmIdVehiculo OR @pmIdVehiculo IS NULL)
 AND (Trn_TraOrdenServ.IdPoseedor=@pmIdPoseedor OR @pmIdPoseedor IS NULL)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposMotdev] @pmIdMotDev VARCHAR(4)
 AS 
 IF @pmIdMotDev IS NULL
  BEGIN
   SELECT M.IdMotDev,M.MotivoDev,M.TipoFact,M.EstadoPed,M.CodEstadoPed,ED.Estado,M.BodegaDev,M.CodBodegaDev,B.Bodega,M.CodCias,M.Inactivo
   FROM TiposMotdev AS M LEFT JOIN EstadoDoc AS ED ON M.CodEstadoPed=ED.IdEstado LEFT JOIN Bodegas AS B ON M.CodBodegaDev=B.IdBodega
  END
 ELSE
  BEGIN
   SELECT IdMotDev,MotivoDev,TipoFact,EstadoPed,CodEstadoPed,BodegaDev,CodBodegaDev,CodCias,Inactivo
   FROM TiposMotdev
   WHERE IdMotDev=@pmIdMotDev
  END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTiposMotdevDso] @pmTipoFact VARCHAR(4)=Null,@pmIdCia CHAR(2)=Null
 AS 
 --@pmTipoFact NO APLICA AUN
 SELECT IdMotDev,MotivoDev,TipoFact 
 FROM TiposMotdev
 WHERE Inactivo=0 AND (LEN(ISNULL(CodCias,''))=0 OR (ISNULL(CodCias,'') LIKE '%'+@pmIdCia+'%') OR @pmIdCia IS NULL)
 ORDER BY MotivoDev
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsTiposMotdev] @pmIdMotDev VARCHAR(4),@pmMotivoDev VARCHAR(150)
,@pmTipoFact VARCHAR(3),@pmInactivo BIT,@pmEstadoPed BIT,@pmCodEstadoPed VARCHAR(4),@pmBodegaDev BIT,@pmCodBodegaDev VARCHAR(4),@pmCodCias VARCHAR(500)
 AS 
 INSERT INTO TiposMotdev (IdMotDev,MotivoDev,TipoFact,Inactivo,EstadoPed,CodEstadoPed,BodegaDev,CodBodegaDev,CodCias)
 VALUES (@pmIdMotDev,@pmMotivoDev,@pmTipoFact,@pmInactivo,@pmEstadoPed,@pmCodEstadoPed,@pmBodegaDev,@pmCodBodegaDev,@pmCodCias)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpTiposMotdev] @pmIdMotDev VARCHAR(4),@pmMotivoDev VARCHAR(150),@pmTipoFact VARCHAR(3)
,@pmInactivo BIT,@pmEstadoPed BIT,@pmCodEstadoPed VARCHAR(4),@pmBodegaDev BIT,@pmCodBodegaDev VARCHAR(4),@pmCodCias VARCHAR(500)
 AS 
 UPDATE TiposMotdev SET MotivoDev=@pmMotivoDev,TipoFact=@pmTipoFact,Inactivo=@pmInactivo
,EstadoPed=@pmEstadoPed,CodEstadoPed=@pmCodEstadoPed,BodegaDev=@pmBodegaDev,CodBodegaDev=@pmCodBodegaDev,CodCias=@pmCodCias
 WHERE IdMotDev=@pmIdMotDev
GO
