if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryOilInventDevFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryOilInventDevFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryOilInventFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryOilInventFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryOilAjustesFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryOilAjustesFmt]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilInventDevFmt] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT DV.TipDev,TipoDoc,DV.Devolucion,DV.IdCia,Compania,DV.Fecha,DV.IdConcepto,Concepto,DV.TipEnt,DV.NumEntrada,DV.IdCiaDoc,DV.FecDoc,DV.IdProveedor,T.RazonSocial AS NomProveedor
,DV.Cantidad AS TotalCant,DV.ValorTotal,E.NumFactura,E.FechaFac,E.FechaVence,DV.CtaPagar,DV.TipCom,DV.Comprobante,DV.IdCiaCom,DV.ModdDev,DV.Observacion
,D.Item,D.IdTanque,D.IdProducto,D.Descripcion,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.TipoProd,D.IdEstacion AS CdCampo,EstServicio AS NomCampo,P.DescripProd
,DV.FechaCrea,DV.IdCiaCrea,DV.OrigenAdd,DV.IdUsuario,Usuario
FROM Trn_OilInventDev AS DV INNER JOIN Trn_OilInventDevp AS D ON DV.TipDev=D.TipDev AND DV.Devolucion=D.Devolucion AND DV.IdCia=D.IdCia
INNER JOIN Trn_OilInvent AS E ON DV.TipEnt=E.TipEnt AND DV.NumEntrada=E.NumEntrada AND DV.IdCiaDoc=E.IdCia
INNER JOIN Sys_TiposDoc AS TD ON DV.TipDev=TD.IdDoc  
INNER JOIN Companias AS CN ON DV.IdCia=CN.IdCia INNER JOIN Terceros AS T ON DV.IdProveedor=T.IdTercero
INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS CT ON DV.IdConcepto=CT.IdConcepto
INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
LEFT JOIN EdsServicio AS CM ON D.IdEstacion=CM.IdEstacion
WHERE DV.TipDev=@pmTipDev AND DV.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND DV.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilInventFmt] @pmTipEnt VARCHAR(3),@pmNumEntradaIni INT,@pmNumEntradaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT E.TipEnt,TipoDoc,E.NumEntrada,E.IdCia,Compania,E.Fecha,E.IdConcepto,Concepto,E.Modalidad,E.IdProveedor,T.RazonSocial AS NomProveedor,E.NumFactura,E.FechaFac,FechaVence
,E.Cantidad AS TotalCant,E.ValorTotal,E.CtaPagar,E.EstContable,E.TipCom,E.Comprobante,E.IdCiaCom,E.Anulado,E.FecDev,E.NumDev,E.Observacion,E.IdEstado,ED.Estado
,D.Item,D.IdTanque,D.IdProducto,D.Descripcion,D.Cantidad,D.UndMed,UM.Unidad,D.VrUnitario,D.TipoProd,D.IdEstacion AS CdCampo,EstServicio AS NomCampo,P.DescripProd
,E.FechaCrea,E.IdCiaCrea,E.OrigenAdd,E.IdUsuario,Usuario
 FROM Trn_OilInvent AS E INNER JOIN Sys_TiposDoc AS TD ON E.TipEnt=TD.IdDoc INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia INNER JOIN Terceros AS T ON E.IdProveedor=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario INNER JOIN Conceptos AS CT ON E.IdConcepto=CT.IdConcepto
 INNER JOIN Trn_OilInventProd AS D ON E.TipEnt=D.TipEnt AND E.NumEntrada=D.NumEntrada AND E.IdCia=D.IdCia
 INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
 LEFT JOIN EdsServicio AS CM ON D.IdEstacion=CM.IdEstacion
 WHERE E.TipEnt=@pmTipEnt AND E.NumEntrada BETWEEN @pmNumEntradaIni AND @pmNumEntradaFin AND E.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilAjustesFmt] @pmTipDoc VARCHAR(3),@pmNumProcesoIni INT,@pmNumProcesoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT A.TipDoc,TipoDoc,A.NumProceso,A.IdCia,Compania,A.Fecha,A.FecProceso,A.TipOrden,A.NumOrden,A.IdCiaOrden,A.TipoProceso,A.IdConcepto,Concepto,A.IdTercero,T.RazonSocial
,A.CantEntradas,A.CantSalidas,A.CostoEnt,A.CostoSal,A.TipCom,A.Comprobante,A.IdCiaCom,A.Anulado,A.NumDev,A.FecDev,A.Observacion
,A.IdEstado,ED.Estado,A.FechaCrea,A.IdCiaCrea,A.OrigenAdd,A.IdUsuario,Usuario
,D.Item,D.IdProducto,D.Descripcion,D.IdTanque,D.Entradas,D.Salidas,D.UndMed,UM.Unidad,D.VrUnitario,D.TipoProd,P.DescripProd
 FROM Trn_OilAjustes AS A INNER JOIN Trn_OilAjustesProd AS D ON A.TipDoc=D.TipDoc AND A.NumProceso=D.NumProceso AND A.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON A.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS T ON A.IdTercero=T.IdTercero INNER JOIN Sys_TiposDoc AS TD ON A.TipDoc=TD.IdDoc
-- INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto INNER JOIN Sys_Um AS UM ON D.UndMed=UM.UndMed
 WHERE A.TipDoc=@pmTipDoc AND A.NumProceso BETWEEN @pmNumProcesoIni AND @pmNumProcesoFin AND A.IdCia=@pmIdCia
GO
