if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsCompCajaDet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsCompCajaDet]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsDevEnt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsDevEnt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsDocSoporte]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsDocSoporte]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsDocSoporteDet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsDocSoporteDet]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsEntradas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsEntradas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFormaspago]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsFormaspago]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTercProvee]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsTercProvee]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTercProvee_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsTercProvee_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_Comp_Caj]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_Comp_Caj]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_TraDetConc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_TraDetConc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_TraDetConc_Dev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_TraDetConc_Dev]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_TraDetConc_Dlg]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_TraDetConc_Dlg]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_TraDetConc_Leg]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_TraDetConc_Leg]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_TraDetConc_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_TraDetConc_Sel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTraLiqDetalle]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsTraLiqDetalle]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryCompCaja_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryCompCaja_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryCompCajaDet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryCompCajaDet]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryCompCajaRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryCompCajaRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevEnt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDevEnt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevEnt_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDevEnt_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevEntLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDevEntLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevEntRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDevEntRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDocSoporte]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDocSoporte]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDocSoporteFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDocSoporteFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDocSoporteRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryDocSoporteRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEntradas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEntradas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEntradas_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEntradas_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEntradasLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEntradasLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEntradasNet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEntradasNet]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEntradasNetDet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEntradasNetDet]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEntradasRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEntradasRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFormaspago]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFormaspago]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTercProvee]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTercProvee]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTercProveeLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTercProveeLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_CompDsc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_CompDsc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_TraDetConc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_TraDetConc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTraDevOdpRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTraDevOdpRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTraLiqDetalleFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTraLiqDetalleFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTraOrdenPagoRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTraOrdenPagoRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTraOrdenPagoRelDet]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTraOrdenPagoRelDet]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpDevEnt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpDevEnt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpDocSoporte]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpDocSoporte]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpEntradas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpEntradas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpFormaspago]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpFormaspago]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTercProvee]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpTercProvee]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsOilMediciones]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsOilMediciones]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpOilMediciones]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpOilMediciones]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryOilMediciones]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryOilMediciones]
GO


SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_Comp_Caj] @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_Comp (tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodAgncia,tmVehPropio,tmReferncia,tmComprobante,tmNombre,tmCcosto,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmConcTrib,tmTarifTrib,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut,tmCuentaNiif)
SELECT  @pmtmNumero,Item,D.IdCuenta,Detalle,VrDebito,VrCredito,NitTercero,pVehiculo,CdCCosto,CdSubCos,VrBase,TarifaBase,TipDoc,Recibo,IdCia,IdConcepto,TipFac,Factura,IdCiaFac,CASE WHEN LEN(ISNULL(D.TipoImp,''))>0 AND ISNULL(D.TipoImp,'')<>'0' THEN D.ItemBase ELSE D.ItemFac END,FecVence
,CodCta,NumCheque,1,TipoAplica,ISNULL(D.DocSop,0),IdConcepto,NitOtros,CodAgencia,0,Referncia,0,SUBSTRING(NomCuenta,1,100),SUBSTRING(CCosto,1,100),SUBSTRING(RazonSocial,1,150),CASE WHEN ISNULL(D.DocSop,0)=1 THEN D.TipSop ELSE D.TipDocRef END
,CASE WHEN ISNULL(D.DocSop,0)=1 THEN D.NumSoporte ELSE D.DocRef END,CASE WHEN ISNULL(D.DocSop,0)=1 THEN D.IdCiaSop ELSE D.IdCiaRef END,D.TipoImp,D.CodTarImp,'','','',Null,P.IdCueNiif
 FROM Trn_CompCajaDet AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero
 LEFT JOIN CentroCosto AS O ON D.CdCCosto=O.IdCCosto
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_CompDsc] @pmtmNumero VARCHAR(5) 
 AS  
 SELECT tmItem,tmCodCargo,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmTercero,tmCodAgncia,tmVrBase,tmTarifaBase
,tmIdCCosto,tmCcosto,tmIdSubCos,tmSubcc,tmIdVehiculo,tmTipoAplica,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,tmCodCta,tmNumCheque,tmReferncia,tmConsolida,tmTipDocRef,tmDocRef,tmIdCiaRef,tmConcTrib,tmTarifTrib
,tmNombre,tmNitOtros,tmEstablec,tmPtoEmision,tmAutoriza,tmFechAut
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPagoRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
,@pmOrdPagoIni INT=Null,@pmOrdPagoFin INT=Null
 AS 
 SELECT OP.TipDoc AS TipoOdp,OP.OrdPago AS NumOdp,OP.IdCia AS CdCia,Compania,OP.Fecha AS FechaOdp,OP.IdConcepto AS CdConcepto,Concepto,Modalidad,LiqRemesas,CxPagar,MulPlazos,OP.IdPlazo AS CdPlazo,Plazo,DiasPago,FechaVence
,OP.TipCom AS TipComp,TipoCom,OP.Comprobante AS NumComp,OP.IdCiaCom AS CdCiaComp,OP.Anulado AS Anuldo,NumDev,OP.FecDev AS FechaDev,OP.Observacion AS Observ,OP.IdEstado AS CdEstado,OP.TimeSys AS FechaCrea,OP.FecUpdate AS FechaAct,OP.IdCiaCrea AS CdCiaCrea,OP.IdUsuario AS CdUsuario,Usuario
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,O.IdVehiculo AS PlacaVeh,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,O.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp
,TipoLiq,TipEgr,Egreso,IdCiaEgr,FechaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrSeguroRc,VrRecCaja,VrReteCREE,TarifaRtc,CodTarRtc,VrImpAvTa,CodTarifAvta,TarifaAvta,CdForma,EdoLiqCausac
,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque,TipoAfiVehic
,IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupoPro,GrupoProp
,ISNULL(VC.VrTotalAbo,0) AS CxpAbonado,ISNULL(VC.VrSaldo,0) AS CxpSaldo
 FROM Trn_TraOrdenPago AS OP INNER JOIN Trn_TraOrdenManif AS O ON OP.TipDoc=O.TipDoc AND OP.OrdPago=O.OrdPago AND OP.IdCia=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Companias AS CN ON OP.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON OP.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON OP.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON OP.IdPlazo=PZ.IdPlazo
 INNER JOIN Terceros AS  CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON O.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NP ON O.IdPropietario=NP.IdTercero  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN TiposCom AS TCM ON OP.TipCom=TCM.IdCom  LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 --subconsulta abonos cuentas por pagar
 LEFT JOIN (SELECT TipFac,Factura,IdCia,IdProveedor,Documento,SUM(VrFactura) AS VrTotalFac,SUM(VrAbonado) AS VrTotalAbo,SUM(VrFactura-VrAbonado) AS VrSaldo FROM Trn_ComFactura
 WHERE TipFac=@pmTipDoc AND TipDoc=@pmTipDoc AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProveedor LIKE ISNULL(@pmIdPoseedor,'%')
 GROUP BY TipFac,Factura,IdCia,IdProveedor,Documento) AS VC ON O.TipDoc=VC.TipFac AND O.OrdPago=VC.Documento AND O.IdCia=VC.IdCia AND O.IdPoseedor=VC.IdProveedor
 WHERE OP.TipDoc=@pmTipDoc AND OP.OrdPago BETWEEN ISNULL(@pmOrdPagoIni,0) AND ISNULL(@pmOrdPagoFin,2147483647)
 AND OP.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 --Mayo 10/2018 Parametros Obsoletos
 --AND OP.IdCia LIKE ISNULL(@pmIdCia,'%%')
 --AND OP.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 --AND O.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 --2147483647
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraOrdenPagoRelDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
,@pmOrdPagoIni INT=Null,@pmOrdPagoFin INT=Null
 AS 
 SELECT OP.TipDoc AS TipoOdp,OP.OrdPago AS NumOdp,OP.IdCia AS CdCia,Compania,OP.Fecha AS FechaOdp,OP.IdConcepto AS CdConcepto,Concepto,OP.Modalidad,LiqRemesas,CxPagar,MulPlazos,OP.IdPlazo AS CdPlazo,Plazo,DiasPago,FechaVence
,OP.TipCom AS TipComp,TipoCom,OP.Comprobante AS NumComp,OP.IdCiaCom AS CdCiaComp,OP.Anulado AS Anuldo,NumDev,OP.FecDev AS FechaDev,OP.Observacion AS Observ,OP.IdEstado AS CdEstado,OP.TimeSys AS FechaCrea,OP.FecUpdate AS FechaAct,OP.IdCiaCrea AS CdCiaCrea,OP.IdUsuario AS CdUsuario,Usuario
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,O.IdVehiculo AS PlacaVeh,O.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,O.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,O.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp
,TipoLiq,TipEgr,Egreso,IdCiaEgr,FechaEgr,O.NumCheque,O.Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrSeguroRc,VrRecCaja,VrReteCREE,TarifaRtc,CodTarRtc,VrImpAvTa,CodTarifAvta,TarifaAvta,EdoLiqCausac
,M.Fecha AS FecManif,M.FecDespacho,M.FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,M.nRemolque,M.TipoAfiVehic
,M.IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
--detalles
,TipRem,ORM.Remesa AS NumRemesa,ORM.IdCiaRem AS CdCiaRem,ItemRem,ORM.Cantidad AS Cant,PesoNeto,ORM.UndMed AS CdUMed,UMP.Unidad AS UmPeso,ORM.Volumen AS Volmen,UndVol,Cases,Cajas,Palets,TarifPago,TarifTabla,UndTarifa,Detalle
,RM.IdCliente AS CdCliente,NC.RazonSocial AS NomCliente,RM.IdAgencia AS CdAgencia,A.Agencia
--Datos del vehiculo
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion,V.IdGrupo AS CdGrupoPro,GrupoProp
,ISNULL(VC.VrTotalAbo,0) AS CxpAbonado,ISNULL(VC.VrSaldo,0) AS CxpSaldo
 FROM Trn_TraOrdenPago AS OP INNER JOIN Trn_TraOrdenManif AS O ON OP.TipDoc=O.TipDoc AND OP.OrdPago=O.OrdPago AND OP.IdCia=O.IdCia
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Companias AS CN ON OP.IdCia=CN.IdCia  INNER JOIN adm_Usuarios AS U ON OP.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON OP.IdConcepto=C.IdConcepto INNER JOIN Plazos AS PZ ON OP.IdPlazo=PZ.IdPlazo
 INNER JOIN Terceros AS  CDT ON O.IdConductor=CDT.IdTercero INNER JOIN Terceros AS T ON O.IdPoseedor=T.IdTercero
 INNER JOIN Terceros AS NP ON O.IdPropietario=NP.IdTercero  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NS ON V.NitEmpSoat=NS.IdTercero
 LEFT JOIN TiposCom AS TCM ON OP.TipCom=TCM.IdCom 
 LEFT JOIN Trn_TraOrdenRemesas AS ORM ON O.TipDoc=ORM.TipDoc AND O.OrdPago=ORM.OrdPago AND O.IdCia=ORM.IdCia 
 AND O.TipMuc=ORM.TipMuc AND O.Manifiesto=ORM.Manifiesto AND O.IdCiaMuc=ORM.IdCiaMuc
 LEFT JOIN Sys_Um AS UMP ON ORM.UndMed=UMP.UndMed LEFT JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
 LEFT JOIN Trn_TraRemesa AS RM ON ORM.TipRem=RM.TipDoc AND ORM.Remesa=RM.NumOrden AND ORM.IdCiaRem=RM.IdCia
 LEFT JOIN Terceros AS NC ON RM.IdCliente=NC.IdTercero LEFT JOIN Agencias AS A ON RM.IdAgencia=A.IdAgencia
  --subconsulta abonos cuentas por pagar
 LEFT JOIN (SELECT TipFac,Factura,IdCia,IdProveedor,Documento,SUM(VrFactura) AS VrTotalFac,SUM(VrAbonado) AS VrTotalAbo,SUM(VrFactura-VrAbonado) AS VrSaldo FROM Trn_ComFactura
 WHERE TipFac=@pmTipDoc AND TipDoc=@pmTipDoc AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProveedor LIKE ISNULL(@pmIdPoseedor,'%')
 GROUP BY TipFac,Factura,IdCia,IdProveedor,Documento) AS VC ON O.TipDoc=VC.TipFac AND O.OrdPago=VC.Documento AND O.IdCia=VC.IdCia AND O.IdPoseedor=VC.IdProveedor
 WHERE OP.TipDoc=@pmTipDoc AND OP.OrdPago BETWEEN ISNULL(@pmOrdPagoIni,0) AND ISNULL(@pmOrdPagoFin,2147483647)
 AND OP.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 --Mayo 10/2018 Parametros Obsoletos
 --AND OP.IdCia LIKE ISNULL(@pmIdCia,'%%')
 --AND OP.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 --AND O.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEntradas_Cr]  @pmTipDoc VARCHAR(3),@pmEntradaIni INT,@pmEntradaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT E.TipDoc AS Tip_Doc,TipoDoc,Entrada,E.IdCia AS CdCia,Compania,E.Fecha AS FechaDoc,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,RazonSocial,E.Factura AS NumFactura,E.FechaFac,E.FechaVence
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrImpCarbono,VrIvaIngProd,VrNeto
,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CxPagar,DocEquiv,E.BaseImp,E.BaseRet,E.BaseIca,BaseRiv,BaseIvaIgp
,E.TarifaIva AS TarifIva,E.TarifaRet AS TarifRet,E.TarifaIca AS TarifIca,TarifaRiv,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,E.CodTarifBom,E.VrBomberil,E.TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta
,E.CdCCosto AS CodCenCosto,CC.CCosto AS CentCosto,E.CdSubCos AS CodSubCent,SC.SubCosto AS SubcCosto,E.TipEgr,E.Egreso,E.IdCiaEgr,E.Modalidad,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop
,E.TipCom,TipoCom,E.Comprobante,E.IdCiaCom,E.Anulado,E.NumDev,E.FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,E.OrigenAdd,E.TimeSys AS Fec_add,E.FecUpdate AS Fec_Update,E.IdCiaCrea,E.IdUsuario AS CdUsuario,Usuario,Leyenda
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
--Detalle
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,K.pVehiculo,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,K.Factura AS KarFactura,K.TipDocDev,K.NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,K.CdMoneda,K.VrTasaCamb,K.VrDivisa1,K.VrDivisa2,K.VrDivisa3,Referencia2,FecOrden,CodTarIco,TarifaIco,VrImpCon,TarifaStc,SobtasaCons,VrImvCosto,K.BaseIvp,K.TarifaIvp,K.IvaIngProd
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
--datos memo
,Comentarios,CantImp
--datos de resolucion doc. equivalente
,DS.CodRes,DS.DescServicio,RS.Resolucion,RS.Prefijo,RS.NumInicial,RS.NumFinal,RS.FechaExpRes,RS.FechaVigencia,RS.Establecimiento
 FROM Trn_Entradas AS E INNER JOIN Trn_Kardex AS K ON E.TipDoc=K.TipDoc AND E.Entrada=K.Documento AND E.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON E.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN TiposCom AS TC ON E.TipCom=TC.IdCom
 LEFT JOIN CentroCosto AS CC ON E.CdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON E.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON E.IdProv=TP.IdProv 
 LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 LEFT JOIN Trn_DocMemo AS MM ON E.TipDoc=MM.TipDoc AND E.Entrada=MM.Documento AND E.IdCia=MM.IdCia
 --datos de resolucion
 LEFT JOIN Trn_DocSoporte DS ON E.TipSop=DS.TipDoc AND E.NumDocSop=DS.NumDoc AND E.CdCiaSop=DS.IdCia
 LEFT JOIN TiposComRes AS RS ON DS.CodRes=RS.IdRes
 WHERE E.TipDoc=@pmTipDoc AND Entrada BETWEEN @pmEntradaIni AND @pmEntradaFin  AND E.IdCia=@pmIdCia
 ORDER BY Entrada,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDocSoporteRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmnIdTercero VARCHAR(16)=Null
AS 
 SELECT D.TipDoc,TipoDoc,D.NumDoc,D.IdCia AS CdCia,CI.Compania,D.Fecha,D.IdTercero AS NitTercero,T.RazonSocial,D.IdAdquiriente,N.RazonSocial AS Adquiriente
 ,D.Modalidad,D.IdConcepto,Concepto,D.IdMedPago,MedioPago,D.MetPago,D.NumFactura,D.FechaFac,D.FechaVence,D.SubTotal,D.Descuento,D.Retencion,D.ReteIca,D.Bomberil,D.ImpAviTab,D.OtrosCargos,D.OtrosDctos,D.ValorTotal
 ,D.BaseRet,D.BaseBom,D.TarifaRet,D.TarifaIca,D.TarifaBom,D.TarifaAvta,D.CodTarRet,D.CodTarIca,D.CodTarifBom,D.CodTarifAvta,D.DescServicio
 ,D.CodRes,R.Resolucion,R.Prefijo,R.NumInicial,R.NumFinal,R.FechaExpRes,R.FechaVigencia,R.Establecimiento,D.CdCCosto,CCosto,D.CdSubCos,SubCosto,D.TipCom,TipoCom,D.Comprobante,D.IdCiaCom,D.TipDocRef,D.NumDocRef,D.IdCiaRef
,D.Anulado,D.TipDocDev,D.NumDev,D.CdCiaDev,D.FecDev,D.Observacion,D.IdEstado AS CdEstado,ED.Estado,D.TimeSys AS FechaCrea,D.OrigenAdd,D.FecUpdate,D.IdCiaCrea,D.IdUsuario,Usuario
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad
,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco
,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
FROM Trn_DocSoporte AS D INNER JOIN Sys_TiposDoc AS TD ON D.TipDoc=TD.IdDoc
INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado 
INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
LEFT JOIN TiposComRes AS R ON D.CodRes=R.IdRes LEFT JOIN Terceros AS N ON D.IdAdquiriente=N.IdTercero
LEFT JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto LEFT JOIN NomMediosPago AS MP ON D.IdMedPago=MP.IdMedPago
LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto
LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
WHERE D.TipDoc=@pmTipDoc AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
AND (D.IdCia=@pmIdCia OR @pmIdCia IS NULL)
AND D.IdTercero LIKE ISNULL(@pmnIdTercero,'%')
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumDoc,IdCia,Fecha,IdTercero,IdAdquiriente,IdConcepto,IdMedPago,MetPago,Modalidad,NumFactura,FechaFac,FechaVence,SubTotal,Descuento,Retencion,ReteIca,Bomberil
,ImpAviTab,OtrosCargos,OtrosDctos,ValorTotal,BaseRet,BaseBom,TarifaRet,TarifaIca,TarifaBom,TarifaAvta,CodTarRet,CodTarIca,CodTarifBom,CodTarifAvta,DescServicio,CodRes,CdCCosto,CdSubCos,TipCom,Comprobante,IdCiaCom
,TipDocRef,NumDocRef,IdCiaRef,Anulado,TipDocDev,NumDev,CdCiaDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DocSoporte
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmIdAdquiriente VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmIdMedPago VARCHAR(4),@pmMetPago INT,@pmModalidad VARCHAR(20),@pmNumFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmBomberil DECIMAL(16,6),@pmImpAviTab DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmValorTotal DECIMAL(16,6)
,@pmBaseRet DECIMAL(16,6),@pmBaseBom DECIMAL(16,6),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaBom DECIMAL(14,4),@pmTarifaAvta DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmCodTarifAvta VARCHAR(4),@pmDescServicio VARCHAR(500),@pmCodRes VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmIdCiaRef CHAR(2),@pmAnulado BIT,@pmTipDocDev VARCHAR(3),@pmNumDev INT,@pmCdCiaDev CHAR(2),@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(500),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DocSoporte (TipDoc,NumDoc,IdCia,Fecha,IdTercero,IdAdquiriente,IdConcepto,IdMedPago,MetPago,Modalidad,NumFactura,FechaFac,FechaVence,SubTotal,Descuento,Retencion,ReteIca,Bomberil,ImpAviTab,OtrosCargos,OtrosDctos,ValorTotal,BaseRet,BaseBom,TarifaRet,TarifaIca,TarifaBom,TarifaAvta,CodTarRet,CodTarIca,CodTarifBom,CodTarifAvta,DescServicio,CodRes,CdCCosto,CdSubCos,TipCom,Comprobante,IdCiaCom,TipDocRef,NumDocRef,IdCiaRef,Anulado,TipDocDev,NumDev,CdCiaDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumDoc,@pmIdCia,@pmFecha,@pmIdTercero,@pmIdAdquiriente,@pmIdConcepto,@pmIdMedPago,@pmMetPago,@pmModalidad,@pmNumFactura,@pmFechaFac,@pmFechaVence,@pmSubTotal,@pmDescuento,@pmRetencion,@pmReteIca,@pmBomberil,@pmImpAviTab,@pmOtrosCargos,@pmOtrosDctos,@pmValorTotal,@pmBaseRet,@pmBaseBom,@pmTarifaRet,@pmTarifaIca,@pmTarifaBom,@pmTarifaAvta,@pmCodTarRet,@pmCodTarIca,@pmCodTarifBom,@pmCodTarifAvta,@pmDescServicio,@pmCodRes,@pmCdCCosto,@pmCdSubCos,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmTipDocRef,@pmNumDocRef,@pmIdCiaRef,@pmAnulado,@pmTipDocDev,@pmNumDev,@pmCdCiaDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmIdAdquiriente VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmIdMedPago VARCHAR(4),@pmMetPago INT,@pmModalidad VARCHAR(20),@pmNumFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmSubTotal DECIMAL(16,6),@pmDescuento DECIMAL(16,6),@pmRetencion DECIMAL(16,6),@pmReteIca DECIMAL(16,6),@pmBomberil DECIMAL(16,6),@pmImpAviTab DECIMAL(16,6),@pmOtrosCargos DECIMAL(16,6),@pmOtrosDctos DECIMAL(16,6),@pmValorTotal DECIMAL(16,6),@pmBaseRet DECIMAL(16,6),@pmBaseBom DECIMAL(16,6),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaBom DECIMAL(14,4),@pmTarifaAvta DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmCodTarifAvta VARCHAR(4),@pmDescServicio VARCHAR(500),@pmCodRes VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmTipDocRef VARCHAR(3),@pmNumDocRef INT,@pmIdCiaRef CHAR(2),@pmAnulado BIT,@pmTipDocDev VARCHAR(3),@pmNumDev INT,@pmCdCiaDev CHAR(2),@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(500),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DocSoporte SET Fecha=@pmFecha,IdTercero=@pmIdTercero,IdAdquiriente=@pmIdAdquiriente,IdConcepto=@pmIdConcepto,IdMedPago=@pmIdMedPago,MetPago=@pmMetPago,Modalidad=@pmModalidad,NumFactura=@pmNumFactura,FechaFac=@pmFechaFac,FechaVence=@pmFechaVence,SubTotal=@pmSubTotal,Descuento=@pmDescuento,Retencion=@pmRetencion,ReteIca=@pmReteIca,Bomberil=@pmBomberil,ImpAviTab=@pmImpAviTab,OtrosCargos=@pmOtrosCargos,OtrosDctos=@pmOtrosDctos,ValorTotal=@pmValorTotal,BaseRet=@pmBaseRet,BaseBom=@pmBaseBom,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaBom=@pmTarifaBom,TarifaAvta=@pmTarifaAvta,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarifBom=@pmCodTarifBom,CodTarifAvta=@pmCodTarifAvta,DescServicio=@pmDescServicio,CodRes=@pmCodRes,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,TipDocRef=@pmTipDocRef,NumDocRef=@pmNumDocRef,IdCiaRef=@pmIdCiaRef,Anulado=@pmAnulado,TipDocDev=@pmTipDocDev,NumDev=@pmNumDev,CdCiaDev=@pmCdCiaDev
,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDocSoporteFmt] @pmTipDoc VARCHAR(3),@pmNumDocIni INT,@pmNumDocFin INT,@pmIdCia CHAR(2)
AS 
 SELECT D.TipDoc,TipoDoc,D.NumDoc,D.IdCia AS CdCia,CI.Compania,D.Fecha,D.IdTercero AS NitTercero,T.RazonSocial,D.IdAdquiriente,N.RazonSocial AS Adquiriente
 ,D.Modalidad,D.IdConcepto,Concepto,D.IdMedPago,MedioPago,D.MetPago,D.NumFactura,D.FechaFac,D.FechaVence,D.SubTotal,D.Descuento,D.Retencion,D.ReteIca,D.Bomberil,D.ImpAviTab,D.OtrosCargos,D.OtrosDctos,D.ValorTotal
 ,D.BaseRet,D.BaseBom,D.TarifaRet,D.TarifaIca,D.TarifaBom,D.TarifaAvta,D.CodTarRet,D.CodTarIca,D.CodTarifBom,D.CodTarifAvta,D.DescServicio
,D.CdCCosto,CCosto,D.CdSubCos,SubCosto,D.TipCom,TipoCom,D.Comprobante,D.IdCiaCom,D.TipDocRef,D.NumDocRef,D.IdCiaRef,D.Anulado,D.TipDocDev,D.NumDev,D.CdCiaDev,D.FecDev
,D.Observacion,D.IdEstado AS CdEstado,ED.Estado,D.TimeSys AS FechaCrea,D.OrigenAdd,D.FecUpdate,D.IdCiaCrea,D.IdUsuario,Usuario
--detalles
,DT.Item,DT.IdProducto,P.DescripProd,DT.Descripcion,DT.Cantidad,DT.VrUnitario,DT.ValorOper,DT.VrDcto,DT.VrRetencion,DT.VrRetIca,DT.VrBomberil,DT.VrAviTab,DT.TarifaDct AS PorcDcto
,DT.TarifaRet AS PorcRet,DT.TarifaIca AS PorcICA,DT.TarifaBom AS PorcBom,DT.TarifaAvta AS PorcAvta,DT.CodTarDct AS DetCodDcto,DT.CodTarRet AS DetCodRet
,DT.CodTarIca AS DetCodIca,DT.CodTarifBom AS DetCodBom,DT.CodTarifAvta AS DetCodAvta,DT.ItemCom,DT.CdCuenta,PC.NomCuenta,DT.CdCueCre
--Datos resolución
,D.CodRes,R.Resolucion,R.Prefijo,R.NumInicial,R.NumFinal,R.FechaExpRes,R.FechaVigencia,R.Establecimiento
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad
,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco
,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
FROM Trn_DocSoporte AS D INNER JOIN Sys_TiposDoc AS TD ON D.TipDoc=TD.IdDoc
INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado 
INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
INNER JOIN Trn_DocSoporteDet AS DT ON D.TipDoc=DT.TipDoc AND D.NumDoc=DT.NumDoc AND D.IdCia=DT.IdCia
LEFT JOIN ProdMcias AS P ON DT.IdProducto=P.IdProducto
LEFT JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto LEFT JOIN NomMediosPago AS MP ON D.IdMedPago=MP.IdMedPago
LEFT JOIN TiposComRes AS R ON D.CodRes=R.IdRes LEFT JOIN Terceros AS N ON D.IdAdquiriente=N.IdTercero
LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto
LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos LEFT JOIN Puc AS PC ON DT.CdCuenta=PC.IdCuenta
WHERE D.TipDoc=@pmTipDoc AND D.NumDoc BETWEEN @pmNumDocIni AND @pmNumDocFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraDevOdpRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
 AS 
 SELECT D.TipDev,D.Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipoOdp,D.OrdPago AS NumOdp,IdCiaDoc,FecDoc
,D.IdVehiculo AS PlacaVeh,D.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor,D.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,CxPagar,ModdDev
,D.TipCom AS CdTipComp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp,D.Observacion AS Observ,D.TimeSys AS FechaCrea,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
,O.TipMuc AS TipManif,O.Manifiesto AS NumManif,O.IdCiaMuc AS CdCiaManif,VrTotalFletes,O.VrDescuento AS Vr_Dcto,O.VrRetencion AS Vr_RetFte,O.VrReteIca AS Vr_RetIca,VrAnticipos
,VrFaltantes,VrSeguros,VrFondos,VrAportes,VrOtrosDctos,VrImpuestos,VrEstampilla,VrOtrosPagos,O.VrNeto AS Vr_Neto
,TarifaTabla,O.TarifaFlete AS Tarifa_Flete,UnidTarifa,O.PesoTotal AS Peso_Total,Unidades,O.Volumen AS VolmTotal,PesoOrigen,UnidOrigen,VolOrigen,PesoDestino,UnidDestino,VolDestino
,O.BaseRet,O.BaseIca,BaseSeg,BaseImp,O.TarifaRet,O.TarifaIca,TarifaSeg,TarifaImp,TipoLiq,TipEgr,Egreso,IdCiaEgr,NumCheque,Referencia,CantFaltante,UnidadFalt,TolFaltNeto,VrConcPagos
,VrConcDctos,VrConcFondo,VrConcSeguro,VrConcAporte,VrConcImpuesto,VrRemesas,VrRecCaja,CdForma,EdoLiqCausac
,M.Fecha AS FecManif,FecDespacho,FecEntrega,M.IdOrigen AS CdOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen
,M.IdDestino AS CdDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino,M.IdRuta AS CdRuta,R.Ruta AS DescRuta,nRemolque,TipoAfiVehic
,IdLocFletes,CF.Localidad AS LugarFletes,M.Observacion AS MucObserv
--Datos del poseedor
,T.TipoId AS TercTipId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
--Datos del vehiculo
,NumVeh,V.IdTipoVeh AS CdTipVeh,TipoVehiculo,V.IdMarca AS CdMarca,MV.Marca AS MarcaVeh,V.IdLinea AS CdLinVeh,LineaVeh,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,Config
,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,NitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat,NitEmpSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraDevOdp  AS D INNER JOIN Trn_TraOrdenManif AS O ON D.TipDoc=O.TipDoc AND D.OrdPago=O.OrdPago AND D.IdCiaDoc=O.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Terceros AS  CDT ON D.IdConductor=CDT.IdTercero
 INNER JOIN Terceros AS T ON D.IdPoseedor=T.IdTercero INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Trn_TraManifiesto AS M ON O.TipMuc=M.TipDoc AND O.Manifiesto=M.Manifiesto AND O.IdCiaMuc=M.IdCia
 INNER JOIN Localidades AS CO ON  M.IdOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON M.IdDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON M.IdRuta=R.IdRuta    INNER JOIN Localidades AS CF ON M.IdLocFletes=CF.IdLocal
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh 
 INNER JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca INNER JOIN MarcasLin AS LV ON V.IdLinea=LV.IdLinea 
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposPpt AS TPR ON V.IdPpd=TPR.IdPpd
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero  LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
-- AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
-- AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%')
-- AND D.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDevEntRel] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmCxPagar BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Entrada,IdCiaDoc,FecDoc
,D.IdProv AS NitProvee,RazonSocial,Factura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal
,VrCargos,VrOtrDcto,VrImpCarbono,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons
,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta,CxPagar,DocEquiv,BaseImp,BaseRet,CdCCosto,CCosto,CdSubCos,SubCosto,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
 FROM Trn_DevEnt AS D INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero 
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON D.IdProv=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE TipDev=@pmTipDev AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdProv LIKE ISNULL(@pmIdProv,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND  ModdDev LIKE ISNULL(@pmModdDev,'%')
 AND (CxPagar=ISNULL(@pmCxPagar,0) or CxPagar=ISNULL(@pmCxPagar,1)) 
 ORDER BY D.IdCia,Devolucion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEntradasRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmCxPagar BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Entrada,E.IdCia AS CdCia,Compania,Fecha,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,RazonSocial,Factura,FechaFac,FechaVence
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrImpCarbono,VrIvaIngProd,VrNeto
,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIca,BaseRiv,BaseIvaIgp
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifBom,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta,CdCCosto,CCosto,CdSubCos,SubCosto
,TipEgr,Egreso,IdCiaEgr,E.Modalidad,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,TimeSys,E.FecUpdate AS Fec_Update,IdCiaCrea,E.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
 FROM Trn_Entradas AS E INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CentroCosto AS CC ON E.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON E.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON E.IdProv=TP.IdProv LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
  AND E.IdCia LIKE ISNULL(@pmIdCia,'%%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND E.IdProv LIKE ISNULL(@pmIdProv,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (CxPagar=ISNULL(@pmCxPagar,0) or CxPagar=ISNULL(@pmCxPagar,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY E.IdCia,Entrada
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTercProveeLta]  @pmIdLocal VARCHAR(8)=Null,@pmIdSector VARCHAR(8)=Null,@pmIdRegimen VARCHAR(4)=Null,@pmTipoId CHAR(1)=Null
,@pmIdProf VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdLocOrd VARCHAR(8)=Null,@pmIdForma VARCHAR(4)=Null,@pmIdBanco VARCHAR(4)=Null
,@pmFecIngIni SMALLDATETIME=Null,@pmFecIngFin SMALLDATETIME=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdEstadoTer VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmInactivoTer BIT=Null
 AS 
 SELECT IdProv,Dv,TipoId,RazonSocial,T.Codigo AS Codtercero,NomCial,SiglaRaz,NitRepLeg,NomRepLeg,T.Direccion AS Dreccion,T.IdLocal AS CodCiud,L.Localidad AS Ciudad,D.Departamento AS Departmto,Telefono,Fax,TelMovil
,DirOrdComp,IdLocOrd,LP.Localidad AS CiudadOrd,DP.Departamento AS DeptoOrd,SitioWeb,e_mail,TipEnte,T.IdSector AS CodSector,SectorEco,T.IdProf AS CodProf,Profesion,T.IdRegimen AS IdRegmen,Regimen,PV.IdGrupo AS CdGrupo,GrupoClie
,NitContac,NomContac,TelContac,emlContac,CargContac,DiasEntga,PV.IdPlazo AS CdPlazo,Plazo,PV.IdForma AS CdForma,FormaPago,PV.IdClase AS CdClase,ClaseCuenta,NumCuenta,PV.IdBanco AS CdBanco,Banco
,B.Direccion AS DirBanco,Contacto,CdMney,CdDct,Tarifa,Simbolo,Autoret,AutoIca,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv,CodRetIca,GartProd,IdLugarCed,LC.Localidad AS LugarCedula,DC.Departamento AS DptoLugcedula,FecExpCed
,Observacion,Cmntarios,ObsFinanc,ObsJuridica,PathFoto,PathFirma,T.IdEstado AS IdEstTerc,ET.Estado AS EstadTerc,T.Inactivo AS Ter_Inactvo,PV.IdEstado AS IdEstProv,EP.Estado AS EstadProv,PV.Inactivo AS Prv_Inactvo,CodRetCom
,Contrato,NContrato,FecIngreso,FecVigencia,FecRetiro,EsCliente,EsProveedor,EsPropietario,EsAccnista,EsCiaAseg,PV.FechaAdd AS Fec_Add,PV.FechaUpdate AS Fec_Upd,PV.IdUsuario AS IdUsario,Usuario,IniStgNom,EP.NColor AS ClrEstado
,T.FechaNac,T.ImagenDoc1,T.ImagenDoc2,T.ImagenDoc3,CupoElectcom
 FROM TercProvee AS PV INNER JOIN Terceros AS T ON PV.IdProv=T.IdTercero
 INNER JOIN GruposCli AS G ON PV.IdGrupo=G.IdGrupo INNER JOIN Plazos AS PZ ON PV.IdPlazo=PZ.IdPlazo
 INNER JOIN Formaspago AS F ON PV.IdForma=F.IdForma INNER JOIN ClaseCta AS CTA ON  PV.IdClase=CTA.IdClase
 INNER JOIN Bancos AS B ON PV.IdBanco=B.IdBanco INNER JOIN EstadoTer AS EP ON PV.IdEstado=EP.IdEstado
 INNER JOIN adm_Usuarios AS U ON PV.IdUsuario=U.IdUsuario   INNER JOIN Localidades AS LP ON PV.IdLocOrd=LP.IdLocal
 INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN  SectoresEco AS S ON T.IdSector=S.IdSector  INNER JOIN RegimenDian AS R ON T.IdRegimen=R.IdRegimen
 INNER JOIN EstadoTer AS ET ON T.IdEstado=ET.IdEstado INNER JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal 
 INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep  INNER JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 LEFT JOIN Tablapor AS TD ON PV.CdDct=TD.IdTarifa
 WHERE T.IdLocal LIKE ISNULL(@pmIdLocal,'%') AND T.IdSector LIKE ISNULL(@pmIdSector,'%')  AND T.IdRegimen LIKE ISNULL(@pmIdRegimen,'%')
 AND TipoId LIKE ISNULL(@pmTipoId,'%') AND T.IdProf LIKE ISNULL(@pmIdProf,'%') AND PV.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND IdLocOrd LIKE ISNULL(@pmIdLocOrd,'%') AND PV.IdForma LIKE ISNULL(@pmIdForma,'%') AND PV.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND PV.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND T.IdEstado LIKE ISNULL(@pmIdEstadoTer,'%')
 AND (PV.Inactivo=ISNULL(@pmInactivo,0) or PV.Inactivo=ISNULL(@pmInactivo,1))
 AND (T.Inactivo=ISNULL(@pmInactivoTer,0) or T.Inactivo=ISNULL(@pmInactivoTer,1))
 AND (FecIngreso>=ISNULL(@pmFecIngIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY RazonSocial
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDevEnt_Cr] @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipEnt,Entrada,IdCiaDoc,FecDoc
,D.IdProv AS NitProvee,RazonSocial,D.Factura AS EntFactura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,D.ImpAviTab,D.TarifaAvta
,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,VrIvaMvc,D.VrImpCarbono,CxPagar,DocEquiv,BaseImp,BaseRet,D.CdCCosto AS CodCenCosto,CC.CCosto AS CentCosto
,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,D.VrImpuCons,D.VrSobtasaCons,D.BaseIvaIgp,D.VrIvaIngProd,D.CdSubCos AS CodSubCent,SC.SubCosto AS SubcCosto
,Modalidad,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,Leyenda
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
--Detalle
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,pVehiculo,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,CodTarIco,TarifaIco,VrImpCon,TarifaStc,SobtasaCons,VrImvCosto,K.BaseIvp,K.TarifaIvp,K.IvaIngProd
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_DevEnt AS D  INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON D.IdProv=TP.IdProv LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEntradasNetDet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS 
 SELECT E.TipDoc,E.Entrada,E.IdCia AS CdCia,Compania,E.Fecha,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,T.RazonSocial AS NomProveedor,E.Factura,E.FechaFac,E.FechaVence
,E.VrSubTotal,E.VrDescuento,E.VrImpuesto,E.VrRetencion,E.VrReteICA,E.VrReteIVA,E.VrReteCREE,E.VrFletes,E.VrOtros,E.VrSobretasa,E.VrImpGlobal,E.VrCargos,E.VrOtrDcto,E.VrBomberil,E.VrNeto
,E.NitFletes,TF.RazonSocial AS NomFletes,E.VrRetFlete,E.VrIcaFlete,E.FletesCosto,E.TipOdc,E.OCompra,E.IdCiaOdc,E.MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,E.CxPagar,E.DocEquiv,E.BaseImp,E.BaseRet
,E.TarifaRet,E.TarifaIca,E.TarifaRiv,E.TarifaRtc,E.TarifaBom,E.VrIvaMvc,E.VrImpuCons,E.VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta,E.Modalidad
--detalles
 ,K.Item,K.IdProducto AS CdProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,K.Entradas,K.Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,K.VrUnitario
,VrCostoEnt,VrCostoSal,K.TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva,VrIvaEnt,VrIvaSal,K.Sobretasa,K.Soldicom,K.ImpGlobal,K.VrCostProm,K.Unidades,K.Descripcion,K.Referencia,Referencia2
,K.TipOrd,K.NumOrden,K.IdCiaOrd,TipDocDev,K.NumDocDev,OtroImpto,K.VrBruto,K.VrBase,K.Servcios,K.VrImvCosto,K.TarifaIco,K.VrImpCon,K.CantObseq,K.VrIvaObseq,K.BaseIvp,K.TarifaIvp,K.IvaIngProd,K.CdTanque,K.CdCCosto,CCosto,K.CdSubCos,SubCosto
,E.TipCom,E.Comprobante,E.IdCiaCom,E.Anulado,E.NumDev,FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,E.TimeSys,E.FecUpdate AS Fec_Update,IdCiaCrea,E.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDV,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCodLocal,Localidad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS TercCodSector,SectorEco,T.IdRegimen AS TercCodRegimen,Regimen,T.TipEnte AS TercTipoEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga,Autoret
 FROM Trn_Entradas AS E INNER JOIN Trn_Kardex AS K ON E.TipDoc=K.TipDoc AND E.Entrada=K.Documento AND E.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero
 INNER JOIN Conceptos AS C ON E.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON E.IdProv=TP.IdProv LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Terceros AS TF ON E.NitFletes=TF.IdTercero
 WHERE E.TipDoc=@pmTipDoc AND E.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND E.IdCia LIKE ISNULL(@pmIdCia,'%%')
 UNION ALL
 SELECT D.TipDev,D.Devolucion,D.IdCia,Compania,D.Fecha,D.IdConcepto,Concepto,D.IdProv,T.RazonSocial,D.Factura,E.FechaFac,E.FechaVence
,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrRetencion,D.VrReteICA,D.VrReteIVA,D.VrReteCREE,D.VrFletes,D.VrOtros,D.VrSobretasa,D.VrImpGlobal,D.VrCargos,D.VrOtrDcto,D.VrBomberil,D.VrNeto
,D.NitFletes,TF.RazonSocial,D.VrRetFlete,D.VrIcaFlete,D.FletesCosto,E.TipOdc,E.OCompra,E.IdCiaOdc,MulPlazos,E.IdPlazo,Plazo,NVmto,DiasPago,D.CxPagar,D.DocEquiv,D.BaseImp,D.BaseRet
,E.TarifaRet,E.TarifaIca,E.TarifaRiv,D.TarifaRtc,D.TarifaBom,D.VrIvaMvc,D.VrImpuCons,D.VrSobtasaCons,D.ImpAviTab,D.TarifaAvta,E.CodTarifAvta,D.Modalidad
,K.Item,K.IdProducto,DescripProd,K.IdBodega,Bodega,K.Entradas,K.Salidas,K.IdUnd,UM.Unidad,K.VrUnitario
,VrCostoEnt,VrCostoSal,K.TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva,VrIvaEnt,VrIvaSal,K.Sobretasa,K.Soldicom,K.ImpGlobal,K.VrCostProm,K.Unidades,K.Descripcion,K.Referencia,Referencia2
,K.TipOrd,K.NumOrden,K.IdCiaOrd,TipDocDev,K.NumDocDev,OtroImpto,K.VrBruto,K.VrBase,K.Servcios,K.VrImvCosto,K.TarifaIco,K.VrImpCon,K.CantObseq,K.VrIvaObseq,K.BaseIvp,K.TarifaIvp,K.IvaIngProd,K.CdTanque,K.CdCCosto,CCosto,K.CdSubCos,SubCosto
,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Entrada,FecDoc,D.Observacion,D.IdEstado,Estado,D.TimeSys,D.FecUpdate,D.IdCiaCrea,D.IdUsuario,Usuario
,T.TipoId,T.Dv,T.Codigo,T.NomCial,T.Direccion,T.IdLocal,Localidad,L.IdDep,Departamento,T.Telefono,T.Fax,T.e_mail,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,TP.IdGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga,Autoret
 FROM Trn_DevEnt AS D INNER JOIN Trn_Entradas AS E ON D.TipDoc=E.TipDoc AND D.Entrada=E.Entrada AND D.IdCiaDoc=E.IdCia
 INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto 
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen
 INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector INNER JOIN Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN ProdMcias AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON D.IdProv=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Terceros AS TF ON D.NitFletes=TF.IdTercero
 WHERE D.TipDoc=@pmTipDoc AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEntradasNet] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmCxPagar BIT=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Entrada,E.IdCia AS CdCia,Compania,Fecha,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,T.RazonSocial AS NomProveedor,Factura,FechaFac,FechaVence
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrReteCREE,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrBomberil,VrIvaIngProd,VrNeto
,Cantidad,NitFletes,TF.RazonSocial AS NomFletes,VrRetFlete,VrIcaFlete,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIvaIgp
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaRtc,TarifaBom,VrIvaMvc,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons,E.ImpAviTab,E.TarifaAvta,E.CodTarifAvta,CdCCosto,CCosto,CdSubCos,SubCosto,Modalidad
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,TimeSys,E.FecUpdate AS Fec_Update,IdCiaCrea,E.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDV,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCodLocal,Localidad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS TercCodSector,SectorEco,T.IdRegimen AS TercCodRegimen,Regimen,T.TipEnte AS TercTipoEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga,Autoret
 FROM Trn_Entradas AS E INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CentroCosto AS CC ON E.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON E.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON E.IdProv=TP.IdProv LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Terceros AS TF ON E.NitFletes=TF.IdTercero
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
  AND E.IdCia LIKE ISNULL(@pmIdCia,'%%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND E.IdProv LIKE ISNULL(@pmIdProv,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (CxPagar=ISNULL(@pmCxPagar,0) or CxPagar=ISNULL(@pmCxPagar,1))
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.IdProv AS NitProvee,T.RazonSocial AS NomProveedor
,D.Factura AS NumFactura,FechaFac,FechaVence,D.VrSubTotal AS SubTotal,D.VrDescuento AS Descuento,D.VrImpuesto AS Impuesto,D.VrRetencion AS ReteFte,D.VrReteICA AS ReteICA
,D.VrReteIVA AS ReteIva,D.VrReteCREE,D.VrFletes AS Fletes,D.VrOtros AS ValOtros,D.VrSobretasa AS Sobretasas,D.VrImpGlobal AS ImpGlobal,D.VrCargos AS OtrosCargos,D.VrOtrDcto AS OtrosDctos
,D.VrBomberil,D.VrIvaIngProd,D.VrNeto AS TotalNeto,D.Cantidad AS CantDev,D.NitFletes AS Nit_Fletes,TF.RazonSocial AS NomFletes,D.VrRetFlete,D.VrIcaFlete,D.FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago
,D.CxPagar AS Cx_Pagar,D.DocEquiv AS DocEquival,D.BaseImp AS VrBaseIva,D.BaseRet AS VrBaseRet,D.BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,D.TarifaRtc,D.TarifaBom,D.VrIvaMvc,D.BaseIvaObsq,D.VrIvaObsequio,D.VrImpuCons,D.VrSobtasaCons
,D.ImpAviTab,D.TarifaAvta,E.CodTarifAvta,D.CdCCosto AS CodCcost,CCosto,D.CdSubCos AS CodSubCent,SubCosto,D.Modalidad AS ModalidadEnt,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CodCiaComp,0,D.Entrada AS NumEntrada,FecDoc
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS FecAdd,D.FecUpdate AS Fec_Update,D.IdCiaCrea AS CodCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDV,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCodLocal,Localidad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS TercCodSector,SectorEco,T.IdRegimen AS TercCodRegimen,Regimen,T.TipEnte AS TercTipoEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga,Autoret
 FROM Trn_DevEnt AS D INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero 
 INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Trn_Entradas AS E ON D.TipDoc=E.TipDoc AND D.Entrada=E.Entrada AND D.IdCiaDoc=E.IdCia
 INNER JOIN Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON D.IdProv=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Terceros AS TF ON D.NitFletes=TF.IdTercero
 WHERE D.TipDoc=@pmTipDoc AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdProv LIKE ISNULL(@pmIdProv,'%') AND D.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (D.CxPagar=ISNULL(@pmCxPagar,0) or D.CxPagar=ISNULL(@pmCxPagar,1)) 
 ORDER BY E.IdCia,Entrada
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFormaspago] @pmIdForma VARCHAR(4),@pmFormaPago VARCHAR(50),@pmConsecutivo INT,@pmCuadreCaja BIT
,@pmRefDcmto BIT,@pmRefNit BIT,@pmRefBco BIT,@pmRefFech BIT,@pmRefAtza BIT,@pmRefOtro BIT,@pmVldRangos BIT,@pmVleConsumo BIT
,@pmCodIntegrity VARCHAR(20),@pmCodValera VARCHAR(10),@pmCodIntCom VARCHAR(20),@pmRefProducto BIT,@pmFactTicket BIT,@pmCdMedPago VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE Formaspago SET FormaPago=@pmFormaPago,Consecutivo=@pmConsecutivo,CuadreCaja=@pmCuadreCaja,RefDcmto=@pmRefDcmto,RefNit=@pmRefNit
,RefBco=@pmRefBco,RefFech=@pmRefFech,RefAtza=@pmRefAtza,RefOtro=@pmRefOtro,VldRangos=@pmVldRangos,VleConsumo=@pmVleConsumo
,CodIntegrity=@pmCodIntegrity,CodValera=@pmCodValera,CodIntCom=@pmCodIntCom,RefProducto=@pmRefProducto,FactTicket=@pmFactTicket,CdMedPago=@pmCdMedPago,Inactivo=@pmInactivo
 WHERE IdForma=@pmIdForma
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFormaspago] @pmIdForma VARCHAR(4)
 AS 
 IF @pmIdForma IS NULL
  SELECT IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,VleConsumo
,CodIntegrity,CodValera,CodIntCom,RefProducto,FactTicket,CdMedPago
  FROM Formaspago
  WHERE Inactivo=0
  ORDER BY Consecutivo
 ELSE
  SELECT IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,VleConsumo
,CodIntegrity,CodValera,CodIntCom,RefProducto,FactTicket,CdMedPago,Inactivo
  FROM Formaspago
  WHERE IdForma=@pmIdForma
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFormaspago] @pmIdForma VARCHAR(4),@pmFormaPago VARCHAR(50),@pmConsecutivo INT,@pmCuadreCaja BIT,@pmRefDcmto BIT
,@pmRefNit BIT,@pmRefBco BIT,@pmRefFech BIT,@pmRefAtza BIT,@pmRefOtro BIT,@pmVldRangos BIT,@pmVleConsumo BIT
,@pmCodIntegrity VARCHAR(20),@pmCodValera VARCHAR(10),@pmCodIntCom VARCHAR(20),@pmRefProducto BIT,@pmFactTicket BIT,@pmCdMedPago VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO Formaspago (IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,VleConsumo,CodIntegrity,CodValera,Inactivo,CodIntCom,RefProducto,FactTicket,CdMedPago)
 VALUES (@pmIdForma,@pmFormaPago,@pmConsecutivo,@pmCuadreCaja,@pmRefDcmto,@pmRefNit,@pmRefBco,@pmRefFech,@pmRefAtza
,@pmRefOtro,@pmVldRangos,@pmVleConsumo,@pmCodIntegrity,@pmCodValera,@pmInactivo,@pmCodIntCom,@pmRefProducto,@pmFactTicket,@pmCdMedPago)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCompCajaRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmCdCta VARCHAR(4)=Null,@pmEnEfectivo BIT=Null,@pmIdTercero VARCHAR(16)=Null,@pmCdAgencia VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmAnulado BIT=Null
 AS 
 SELECT R.TipDoc AS Tip_Doc,R.Recibo AS NumRecibo,R.IdCia AS CdCia,Compania,R.Fecha AS FechaDoc,R.IdConcepto AS CdConcepto,CN.Concepto AS ConceptoDoc
,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco
,R.IdTercero AS Nit_Tercero,T.RazonSocial AS NomTercero,CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,R.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,R.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto
,IdCajero,UC.Usuario AS NomCajero,NumForma,CdBanco,BR.Banco AS DocBanco,R.Referencia AS DocReferencia,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,R.FecUpdate AS Fec_Update,IdCiaCrea,R.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario
--detalle de cuentas
,Item,D.IdConcepto AS DetCodConcepto,CD.Concepto AS DetConcepto,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,NitTercero,DT.RazonSocial AS DetRazonSocial
,pVehiculo,CdCCosto,DC.CCosto AS DetCentCosto,CdSubCos,DS.SubCosto AS DetSubCentCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,NumCheque,TipoAplica
,NitOtros,D.CodAgencia AS DetCodAgencia,DA.Agencia AS DetAgencia,Referncia,D.TipDocRef,D.DocRef,D.IdCiaRef,D.DocSop,D.TipSop,D.NumSoporte,D.IdCiaSop,D.TipoImp,D.CodTarImp,D.ItemBase
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--agencia
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DAG.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,NumVehiculo
 FROM Trn_CompCaja AS R INNER JOIN Trn_CompCajaDet AS D ON R.TipDoc=D.TipDoc AND R.Recibo=D.Recibo AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON R.IdCajero=UC.IdUsuario  INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto 
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON R.IdTercero =T.IdTercero
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto  INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  CtasCorrientes AS CTA ON R.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Agencias  AS A ON R.CdAgencia=A.IdAgencia LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco  
 LEFT JOIN Terceros AS DT ON D.NitTercero=DT.IdTercero  LEFT JOIN CentroCosto AS DC ON D.CdCCosto=DC.IdCCosto 
 LEFT JOIN SubCentros AS DS ON D.CdSubCos=DS.IdSubCos LEFT JOIN Agencias AS DA ON D.CodAgencia=DA.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DAG ON LA.IdDep=DAG.IdDep
 WHERE R.TipDoc=@pmTipDoc AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND CdCta LIKE ISNULL(@pmCdCta,'%')  AND R.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')  AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY R.IdCia,R.Recibo
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCompCaja_Cr] @pmTipDoc VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS Tip_Doc,TipoDoc,R.Recibo AS NumRecibo,R.IdCia AS CdCia,Compania,R.Fecha AS FechaDoc,R.IdConcepto AS CdConcepto,CN.Concepto AS ConceptoDoc
,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco
,R.IdTercero AS Nit_Tercero,T.RazonSocial AS NomTercero,CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,R.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,R.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto
,IdCajero,UC.Usuario AS NomCajero,NumForma,CdBanco,BR.Banco AS DocBanco,R.Referencia AS DocReferencia,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,R.FecUpdate AS Fec_Update,IdCiaCrea,R.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario,Leyenda,NumVehiculo
--detalle de cuentas
,Item,D.IdConcepto AS DetCodConcepto,CD.Concepto AS DetConcepto,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,NitTercero,DT.RazonSocial AS DetRazonSocial
,pVehiculo,CdCCosto,DC.CCosto AS DetCentCosto,CdSubCos,DS.SubCosto AS DetSubCentCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,NumCheque,TipoAplica
,NitOtros,D.CodAgencia AS DetCodAgencia,DA.Agencia AS DetAgencia,Referncia,D.TipDocRef,D.DocRef,D.IdCiaRef,D.DocSop,D.TipSop,D.NumSoporte,D.IdCiaSop,D.TipoImp,D.CodTarImp,D.ItemBase
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--agencia
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DAG.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_CompCaja AS R INNER JOIN Trn_CompCajaDet AS D ON R.TipDoc=D.TipDoc AND R.Recibo=D.Recibo AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON R.IdCajero=UC.IdUsuario INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto 
 INNER JOIN Conceptos AS CN ON R.IdConcepto=CN.IdConcepto INNER JOIN Terceros AS T ON R.IdTercero =T.IdTercero
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CtasCorrientes AS CTA ON R.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Agencias  AS A ON R.CdAgencia=A.IdAgencia LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco  LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom
 LEFT JOIN Terceros AS DT ON D.NitTercero=DT.IdTercero  LEFT JOIN CentroCosto AS DC ON D.CdCCosto=DC.IdCCosto 
 LEFT JOIN SubCentros AS DS ON D.CdSubCos=DS.IdSubCos LEFT JOIN Agencias AS DA ON D.CodAgencia=DA.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DAG ON LA.IdDep=DAG.IdDep
 WHERE R.TipDoc=@pmTipDoc AND R.Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND R.IdCia=@pmIdCia
 ORDER BY R.Recibo,Item
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryTraLiqDetalleFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
AS 
 SELECT TipDoc,Liquidacion,D.IdCia,Compania,D.Item,D.IdConcepto AS CdConcepto,DescConcepto,D.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito
,D.IdTercero AS NitTercero,RazonSocial,D.CodAgncia,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo
,D.RubroConc,D.VrBase,D.TarifBase,D.TipFac,D.Factura,D.IdCiaFac,ItemFac,D.FecVence,RefConcep,Concepto,D.TipSop,D.NumSoporte,D.IdCiaSop
,D.DocSop,D.TipoImp,D.CodTarImp,D.ItemBase
 FROM Trn_TraLiqDetalle AS D INNER JOIN ConcDiversos AS C ON D.IdConcepto=C.IdConcepto 
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta LEFT JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 WHERE D.TipDoc=@pmTipDoc AND D.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND D.IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Dlg]  @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,DescConcepto,RubroConc,VrCredito,VrDebito,IdCuenta,IdTercero,CdCCosto,CdSubCos,pVehiculo
,'TERCEROS',VrBase,TarifBase,RefConcep,TipDoc,Liquidacion,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodAgncia,CantComb,DocSop,TipoImp,CodTarImp,ItemBase,TipSop,NumSoporte,IdCiaSop
 FROM Trn_TraLiqDetalle
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Leg]  @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,DescConcepto,RubroConc,VrDebito,VrCredito,IdCuenta,IdTercero,CdCCosto,CdSubCos,pVehiculo
,'TERCEROS',VrBase,TarifBase,RefConcep,TipSop,NumSoporte,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodAgncia,CantComb,DocSop,TipoImp,CodTarImp,ItemBase,TipSop,NumSoporte,IdCiaSop
 FROM Trn_TraLiqDetalle
 WHERE TipDoc=@pmTipDoc AND Liquidacion=@pmLiquidacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Sel]  @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,Descripcion,TipoConc,VrDebito,VrCredito,CdCuenta,NitTercero,CdCCosto,CdSubCos,pVehiculo
,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,Factura,IdCiaFac,ItemFac,FecVence,'0',0,0,'','',0,'0',0,'00'
 FROM Trn_TraCauDetalle
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdConcepto VARCHAR(4),@pmtmDescripcion VARCHAR(250),@pmtmTipoConc VARCHAR(10),@pmtmVrDebito MONEY,@pmtmVrCredito MONEY,@pmtmCdCuenta VARCHAR(16)
,@pmtmNitTercero VARCHAR(16),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmpVehiculo VARCHAR(10),@pmtmTipoAfiVehic VARCHAR(10),@pmtmVrBase MONEY,@pmtmTarifBase DECIMAL(14,4),@pmtmReferencia VARCHAR(50)
,@pmtmTipDocRef VARCHAR(3),@pmtmDocRef INT,@pmtmTipFac VARCHAR(3),@pmtmFactura VARCHAR(15),@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT,@pmtmFecVence SMALLDATETIME,@pmtmCodAgncia VARCHAR(16),@pmtmCantComb DECIMAL(14,4)
,@pmtmDocSop BIT,@pmtmTipoImp VARCHAR(4),@pmtmCodTarImp VARCHAR(4),@pmtmItemBase INT,@pmtmTipoDs VARCHAR(3),@pmtmNumDs INT,@pmtmIdCiaDs CHAR(2)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef
,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdConcepto,@pmtmDescripcion,@pmtmTipoConc,@pmtmVrDebito,@pmtmVrCredito,@pmtmCdCuenta,@pmtmNitTercero,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmpVehiculo,@pmtmTipoAfiVehic
,@pmtmVrBase,@pmtmTarifBase,@pmtmReferencia,@pmtmTipDocRef,@pmtmDocRef,@pmtmTipFac,@pmtmFactura,@pmtmIdCiaFac,@pmtmItemFac,@pmtmFecVence,@pmtmCodAgncia,@pmtmCantComb,@pmtmDocSop,@pmtmTipoImp,@pmtmCodTarImp,@pmtmItemBase,@pmtmTipoDs,@pmtmNumDs,@pmtmIdCiaDs)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInstm_TraDetConc_Dev]  @pmTipDoc VARCHAR(3),@pmCausacion INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_TraDetConc (tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs)
 SELECT @pmtmNumero,Item,IdConcepto,Descripcion,TipoConc,VrCredito,VrDebito,CdCuenta,NitTercero,CdCCosto,CdSubCos,pVehiculo
,TipoAfiVehic,VrBase,TarifBase,Referencia,TipDocRef,DocRef,TipFac,Factura,IdCiaFac,ItemFac,FecVence,'0',0,0,'','',0,'0',0,'00'
 FROM Trn_TraCauDetalle
 WHERE TipDoc=@pmTipDoc AND Causacion=@pmCausacion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_TraDetConc] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdConcepto,tmDescripcion,tmTipoConc,tmVrDebito,tmVrCredito,tmCdCuenta,tmNitTercero,tmCdCCosto,tmCdSubCos
,tmpVehiculo,tmTipoAfiVehic,tmVrBase,tmTarifBase,tmReferencia,tmTipDocRef,tmDocRef,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodAgncia,tmCantComb
,tmDocSop,tmTipoImp,tmCodTarImp,tmItemBase,tmTipoDs,tmNumDs,tmIdCiaDs
 FROM tm_TraDetConc
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpDevEnt] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdProv VARCHAR(16),@pmFactura VARCHAR(15),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY
,@pmVrImpGlobal MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmCdCCosto VARCHAR(16)
,@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmFletesCosto BIT,@pmVrIvaMvc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY
,@pmVrImpCarbono MONEY,@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevEnt SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Entrada=@pmEntrada,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdProv=@pmIdProv,Factura=@pmFactura,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento
,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto
,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,NitFletes=@pmNitFletes,CxPagar=@pmCxPagar,DocEquiv=@pmDocEquiv,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,Modalidad=@pmModalidad,ModdDev=@pmModdDev
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,VrRetFlete=@pmVrRetFlete,VrIcaFlete=@pmVrIcaFlete,FletesCosto=@pmFletesCosto,VrIvaMvc=@pmVrIvaMvc
,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,VrBomberil=@pmVrBomberil,TarifaBom=@pmTarifaBom,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,VrImpCarbono=@pmVrImpCarbono
,VrImpuCons=@pmVrImpuCons,VrSobtasaCons=@pmVrSobtasaCons,BaseIvaIgp=@pmBaseIvaIgp,VrIvaIngProd=@pmVrIvaIngProd,ImpAviTab=@pmImpAviTab,TarifaAvta=@pmTarifaAvta,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryDevEnt] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Entrada,IdCiaDoc,FecDoc,IdProv,Factura,VrSubTotal,VrDescuento,VrImpuesto
,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,VrIvaMvc,CxPagar,DocEquiv
,BaseImp,BaseRet,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,CdCCosto,CdSubCos,Modalidad,ModdDev,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,VrImpuCons,VrSobtasaCons,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta
,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevEnt
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsDevEnt] @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdProv VARCHAR(16)
,@pmFactura VARCHAR(15),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrCargos MONEY
,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10)
,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmFletesCosto BIT,@pmVrIvaMvc MONEY
,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevEnt (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Entrada,IdCiaDoc,FecDoc,IdProv,Factura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,CxPagar,DocEquiv
,BaseImp,BaseRet,CdCCosto,CdSubCos,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,VrImpuCons,VrSobtasaCons,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmEntrada,@pmIdCiaDoc,@pmFecDoc,@pmIdProv,@pmFactura,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrOtros
,@pmVrSobretasa,@pmVrImpGlobal,@pmVrCargos,@pmVrOtrDcto,@pmVrNeto,@pmCantidad,@pmNitFletes,@pmVrRetFlete,@pmVrIcaFlete,@pmFletesCosto,@pmCxPagar,@pmDocEquiv,@pmBaseImp,@pmBaseRet,@pmCdCCosto,@pmCdSubCos,@pmModalidad,@pmModdDev,@pmOrigenAdd
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVrIvaMvc,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmVrBomberil,@pmTarifaBom,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmVrImpCarbono,@pmVrImpuCons,@pmVrSobtasaCons,@pmBaseIvaIgp,@pmVrIvaIngProd,@pmImpAviTab,@pmTarifaAvta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryDevEntLta] @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmEntradaIni INT=Null,@pmEntradaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Entrada,IdCiaDoc,FecDoc,IdProv,RazonSocial,Factura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrOtros,VrSobretasa,VrImpGlobal,VrImpCarbono,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons
,BaseIvaIgp,VrIvaIngProd,ImpAviTab,TarifaAvta,CxPagar,DocEquiv,BaseImp,BaseRet,CdCCosto,CdSubCos,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TipDev,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
 FROM Trn_DevEnt AS D INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero
 INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipDev=@pmTipDev
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND Entrada BETWEEN ISNULL(@pmEntradaIni,0) AND ISNULL(@pmEntradaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY IdCia,Devolucion
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsDocSoporteDet] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(1000),@pmCantidad DECIMAL(16,6),@pmVrUnitario DECIMAL(16,6),@pmValorOper DECIMAL(16,6),@pmVrDcto DECIMAL(16,6),@pmVrRetencion DECIMAL(16,6),@pmVrRetIca DECIMAL(16,6)
,@pmVrBomberil DECIMAL(16,6),@pmVrAviTab DECIMAL(16,6),@pmTarifaDct DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaBom DECIMAL(14,4),@pmTarifaAvta DECIMAL(14,4),@pmCodTarDct VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmCodTarifAvta VARCHAR(4),@pmItemCom INT
,@pmCdCuenta VARCHAR(16),@pmCdCueCre VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmDetalle VARCHAR(500),@pmNitTercero VARCHAR(16)
 AS 
 INSERT INTO Trn_DocSoporteDet (TipDoc,NumDoc,IdCia,Item,IdProducto,Descripcion,Cantidad,VrUnitario,ValorOper,VrDcto,VrRetencion,VrRetIca,VrBomberil,VrAviTab,TarifaDct,TarifaRet,TarifaIca,TarifaBom,TarifaAvta,CodTarDct,CodTarRet,CodTarIca,CodTarifBom,CodTarifAvta,ItemCom,CdCuenta,CdCueCre,TipCom,Comprobante,IdCiaCom,Detalle,NitTercero)
 VALUES (@pmTipDoc,@pmNumDoc,@pmIdCia,@pmItem,@pmIdProducto,@pmDescripcion,@pmCantidad,@pmVrUnitario,@pmValorOper,@pmVrDcto,@pmVrRetencion,@pmVrRetIca,@pmVrBomberil,@pmVrAviTab,@pmTarifaDct,@pmTarifaRet,@pmTarifaIca,@pmTarifaBom,@pmTarifaAvta,@pmCodTarDct,@pmCodTarRet,@pmCodTarIca,@pmCodTarifBom,@pmCodTarifAvta,@pmItemCom,@pmCdCuenta,@pmCdCueCre,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmDetalle,@pmNitTercero)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTercProvee_Sel] @pmIdProv VARCHAR(16),@pmNewProv VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdProv FROM  TercProvee WHERE  IdProv=@pmIdProv)
 INSERT INTO TercProvee (IdProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv
,GartProd,FecIngreso,PathFoto,PathFirma,Cmntarios,IdEstado,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Inactivo,FechaAdd,IdUsuario,CodRetCom,CupoElectcom,AutoIca,CodRetIca)
 SELECT @pmNewProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv
,GartProd,FecIngreso,PathFoto,PathFirma,Cmntarios,IdEstado,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Inactivo,FechaAdd,IdUsuario,CodRetCom,CupoElectcom,AutoIca,CodRetIca
 FROM TercProvee WHERE  IdProv=@pmIdProv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTercProvee] @pmIdProv VARCHAR(16)
 AS 
 SELECT IdProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo
,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv,GartProd,FecIngreso,PathFoto,PathFirma
,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Cmntarios,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario,CodRetCom,CupoElectcom,AutoIca,CodRetIca
 FROM TercProvee
 WHERE IdProv=@pmIdProv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTercProvee] @pmIdProv VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20)
,@pmemlContac VARCHAR(100),@pmCargContac VARCHAR(50),@pmDirOrdComp VARCHAR(250),@pmIdLocOrd VARCHAR(8),@pmDiasEntga INT,@pmIdPlazo VARCHAR(4),@pmIdForma VARCHAR(4),@pmCdMney VARCHAR(5),@pmCdDct VARCHAR(4),@pmIdClase VARCHAR(4)
,@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmAutoret BIT,@pmIncRet BIT,@pmIncRiv BIT,@pmIncIca BIT,@pmLiqFletes BIT,@pmCdRet VARCHAR(4),@pmCdRiv VARCHAR(4),@pmGartProd INT,@pmFecIngreso SMALLDATETIME,@pmPathFoto VARCHAR(30)
,@pmPathFirma VARCHAR(30),@pmCmntarios VARCHAR(250),@pmContrato BIT,@pmNContrato VARCHAR(20),@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmObsFinanc VARCHAR(250),@pmObsJuridica VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmCodRetCom VARCHAR(4),@pmCupoElectcom DECIMAL(14,4),@pmAutoIca BIT,@pmCodRetIca VARCHAR(4),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercProvee SET IdGrupo=@pmIdGrupo,NitRepLeg=@pmNitRepLeg,NomRepLeg=@pmNomRepLeg,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargContac=@pmCargContac,DirOrdComp=@pmDirOrdComp,IdLocOrd=@pmIdLocOrd
,DiasEntga=@pmDiasEntga,IdPlazo=@pmIdPlazo,IdForma=@pmIdForma,CdMney=@pmCdMney,CdDct=@pmCdDct,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,Autoret=@pmAutoret,IncRet=@pmIncRet,IncRiv=@pmIncRiv,IncIca=@pmIncIca,LiqFletes=@pmLiqFletes,CdRet=@pmCdRet
,CdRiv=@pmCdRiv,GartProd=@pmGartProd,FecIngreso=@pmFecIngreso,PathFoto=@pmPathFoto,PathFirma=@pmPathFirma,Cmntarios=@pmCmntarios,Contrato=@pmContrato,NContrato=@pmNContrato,FecVigencia=@pmFecVigencia,FecRetiro=@pmFecRetiro,ObsFinanc=@pmObsFinanc,ObsJuridica=@pmObsJuridica
,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,CodRetCom=@pmCodRetCom,CupoElectcom=@pmCupoElectcom,AutoIca=@pmAutoIca,CodRetIca=@pmCodRetIca,FechaUpdate=@pmFechaUpdate
 WHERE IdProv=@pmIdProv
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTercProvee] @pmIdProv VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150)
,@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargContac VARCHAR(50),@pmDirOrdComp VARCHAR(250),@pmIdLocOrd VARCHAR(8),@pmDiasEntga INT,@pmIdPlazo VARCHAR(4),@pmIdForma VARCHAR(4)
,@pmCdMney VARCHAR(5),@pmCdDct VARCHAR(4),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmAutoret BIT,@pmIncRet BIT,@pmIncRiv BIT,@pmIncIca BIT,@pmLiqFletes BIT,@pmCdRet VARCHAR(4)
,@pmCdRiv VARCHAR(4),@pmGartProd INT,@pmFecIngreso SMALLDATETIME,@pmPathFoto VARCHAR(30),@pmPathFirma VARCHAR(30),@pmCmntarios VARCHAR(250),@pmContrato BIT,@pmNContrato VARCHAR(20),@pmFecVigencia SMALLDATETIME
,@pmFecRetiro SMALLDATETIME,@pmObsFinanc VARCHAR(250),@pmObsJuridica VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmCodRetCom VARCHAR(4),@pmCupoElectcom DECIMAL(14,4),@pmAutoIca BIT,@pmCodRetIca VARCHAR(4),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercProvee (IdProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv
,GartProd,FecIngreso,PathFoto,PathFirma,Cmntarios,IdEstado,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Inactivo,FechaAdd,IdUsuario,CodRetCom,CupoElectcom,AutoIca,CodRetIca)
 VALUES (@pmIdProv,@pmIdGrupo,@pmNitRepLeg,@pmNomRepLeg,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargContac,@pmDirOrdComp,@pmIdLocOrd,@pmDiasEntga,@pmIdPlazo,@pmIdForma,@pmCdMney,@pmCdDct,@pmIdClase
,@pmNumCuenta,@pmIdBanco,@pmAutoret,@pmIncRet,@pmIncRiv,@pmIncIca,@pmLiqFletes,@pmCdRet,@pmCdRiv,@pmGartProd,@pmFecIngreso,@pmPathFoto,@pmPathFirma,@pmCmntarios,@pmIdEstado
,@pmContrato,@pmNContrato,@pmFecVigencia,@pmFecRetiro,@pmObsFinanc,@pmObsJuridica,@pmInactivo,@pmFechaAdd,@pmIdUsuario,@pmCodRetCom,@pmCupoElectcom,@pmAutoIca,@pmCodRetIca)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEntradasLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmEntradaIni INT=Null,@pmEntradaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Entrada,IdCia,Fecha,E.IdConcepto AS CdConcepto,Concepto,IdProv,RazonSocial,Factura,FechaFac,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros
,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrBomberil,VrIvaIngProd,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,IdPlazo,CxPagar,DocEquiv
,BaseImp,BaseRet,BaseIca,BaseRiv,BaseIvaIgp,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,TarifaBom,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CodTarifBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,ImpAviTab,TarifaAvta,CodTarifAvta
,CdCCosto,CdSubCos,TipEgr,Egreso,IdCiaEgr,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,VrImpuCons,VrSobtasaCons,Modalidad,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,E.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_Entradas AS E INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero
 INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Entrada BETWEEN ISNULL(@pmEntradaIni,0) AND ISNULL(@pmEntradaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND E.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Entrada
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsCompCajaDet] @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmDetalle VARCHAR(250)
,@pmVrDebito MONEY,@pmVrCredito MONEY,@pmNitTercero VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmVrBase MONEY
,@pmTarifaBase DECIMAL(14,4),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME
,@pmCodCta VARCHAR(4),@pmNumCheque VARCHAR(20),@pmTipoAplica CHAR(1),@pmNitOtros VARCHAR(16),@pmCodAgencia VARCHAR(16),@pmReferncia VARCHAR(50),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2)
,@pmDocSop BIT,@pmTipSop VARCHAR(3),@pmNumSoporte INT,@pmIdCiaSop CHAR(2),@pmTipoImp VARCHAR(4),@pmCodTarImp VARCHAR(4),@pmItemBase INT
 AS 
 INSERT INTO Trn_CompCajaDet (TipDoc,Recibo,IdCia,Item,IdConcepto,IdCuenta,Detalle,VrDebito,VrCredito,NitTercero,pVehiculo,CdCCosto,CdSubCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence
,CodCta,NumCheque,TipoAplica,NitOtros,CodAgencia,Referncia,TipDocRef,DocRef,IdCiaRef,DocSop,TipSop,NumSoporte,IdCiaSop,TipoImp,CodTarImp,ItemBase)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmItem,@pmIdConcepto,@pmIdCuenta,@pmDetalle,@pmVrDebito,@pmVrCredito,@pmNitTercero,@pmpVehiculo,@pmCdCCosto,@pmCdSubCos,@pmVrBase
,@pmTarifaBase,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmCodCta,@pmNumCheque,@pmTipoAplica,@pmNitOtros,@pmCodAgencia,@pmReferncia,@pmTipDocRef,@pmDocRef,@pmIdCiaRef
,@pmDocSop,@pmTipSop,@pmNumSoporte,@pmIdCiaSop,@pmTipoImp,@pmCodTarImp,@pmItemBase)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryCompCajaDet] @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Recibo,IdCia,Item,IdConcepto,IdCuenta,Detalle,VrDebito,VrCredito,NitTercero,pVehiculo,CdCCosto,CdSubCos
,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,TipoAplica,NitOtros,CodAgencia,Referncia,TipDocRef,DocRef,IdCiaRef
,DocSop,TipSop,NumSoporte,IdCiaSop,TipoImp,CodTarImp,ItemBase
 FROM Trn_CompCajaDet
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEntradas] @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY
,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmTipOdc VARCHAR(3)
,@pmOCompra INT,@pmIdCiaOdc CHAR(2),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4)
,@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT
,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmCodTarRetFle VARCHAR(4),@pmCodTarIcaFle VARCHAR(4),@pmFletesCosto BIT,@pmVrIvaMvc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4),@pmCodTarifBom VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4)
,@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmTipoSA VARCHAR(3),@pmNumSalAlm INT,@pmCdCiaSA CHAR(2),@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY,@pmTipSop VARCHAR(3),@pmNumDocSop INT,@pmCdCiaSop CHAR(2),@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4),@pmCodTarifAvta VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Entradas (TipDoc,Entrada,IdCia,Fecha,IdConcepto,IdProv,Factura,FechaFac,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,IdPlazo,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva
,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CdCCosto,CdSubCos,TipEgr,Egreso,IdCiaEgr,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifBom,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,TipoSA,NumSalAlm,CdCiaSA,VrImpuCons,VrSobtasaCons,BaseIvaIgp,VrIvaIngProd,TipSop,NumDocSop,CdCiaSop,ImpAviTab,TarifaAvta,CodTarifAvta)
 VALUES (@pmTipDoc,@pmEntrada,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdProv,@pmFactura,@pmFechaFac,@pmFechaVence,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrOtros,@pmVrSobretasa,@pmVrImpGlobal,@pmVrCargos,@pmVrOtrDcto,@pmVrNeto,@pmCantidad,@pmNitFletes
, @pmVrRetFlete,@pmVrIcaFlete,@pmCodTarRetFle,@pmCodTarIcaFle,@pmFletesCosto,@pmTipOdc,@pmOCompra,@pmIdCiaOdc,@pmMulPlazos,@pmIdPlazo,@pmCxPagar,@pmDocEquiv,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmCdCCosto,@pmCdSubCos,@pmTipEgr,@pmEgreso,@pmIdCiaEgr,@pmModalidad,@pmOrigenAdd
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmVrIvaMvc,@pmVrReteCREE,@pmTarifaRtc,@pmCodTarRtc,@pmCodTarifBom,@pmVrBomberil,@pmTarifaBom,@pmBaseIvaObsq,@pmVrIvaObsequio,@pmVrImpCarbono,@pmTipoSA,@pmNumSalAlm,@pmCdCiaSA,@pmVrImpuCons,@pmVrSobtasaCons,@pmBaseIvaIgp,@pmVrIvaIngProd,@pmTipSop,@pmNumDocSop,@pmCdCiaSop,@pmImpAviTab,@pmTarifaAvta,@pmCodTarifAvta)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpEntradas] @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY
,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmTipOdc VARCHAR(3),@pmOCompra INT,@pmIdCiaOdc CHAR(2)
,@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4)
,@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmCodTarRetFle VARCHAR(4),@pmCodTarIcaFle VARCHAR(4),@pmFletesCosto BIT,@pmVrIvaMvc MONEY,@pmVrReteCREE MONEY,@pmTarifaRtc DECIMAL(14,4),@pmCodTarRtc VARCHAR(4)
,@pmCodTarifBom VARCHAR(4),@pmVrBomberil MONEY,@pmTarifaBom DECIMAL(14,4),@pmBaseIvaObsq MONEY,@pmVrIvaObsequio MONEY,@pmVrImpCarbono MONEY,@pmTipoSA VARCHAR(3),@pmNumSalAlm INT,@pmCdCiaSA CHAR(2),@pmVrImpuCons MONEY,@pmVrSobtasaCons MONEY,@pmBaseIvaIgp MONEY,@pmVrIvaIngProd MONEY
,@pmTipSop VARCHAR(3),@pmNumDocSop INT,@pmCdCiaSop CHAR(2),@pmImpAviTab MONEY,@pmTarifaAvta DECIMAL(14,4),@pmCodTarifAvta VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Entradas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdProv=@pmIdProv,Factura=@pmFactura,FechaFac=@pmFechaFac,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes
,VrOtros=@pmVrOtros,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,NitFletes=@pmNitFletes,TipOdc=@pmTipOdc,OCompra=@pmOCompra,IdCiaOdc=@pmIdCiaOdc,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CxPagar=@pmCxPagar,DocEquiv=@pmDocEquiv
,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,TipEgr=@pmTipEgr
,Egreso=@pmEgreso,IdCiaEgr=@pmIdCiaEgr,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,VrRetFlete=@pmVrRetFlete,VrIcaFlete=@pmVrIcaFlete,CodTarRetFle=@pmCodTarRetFle,CodTarIcaFle=@pmCodTarIcaFle,FletesCosto=@pmFletesCosto,VrIvaMvc=@pmVrIvaMvc,FecUpdate=@pmFecUpdate,VrReteCREE=@pmVrReteCREE,TarifaRtc=@pmTarifaRtc,CodTarRtc=@pmCodTarRtc,CodTarifBom=@pmCodTarifBom,VrBomberil=@pmVrBomberil,TarifaBom=@pmTarifaBom
,BaseIvaObsq=@pmBaseIvaObsq,VrIvaObsequio=@pmVrIvaObsequio,VrImpCarbono=@pmVrImpCarbono,TipoSA=@pmTipoSA,NumSalAlm=@pmNumSalAlm,CdCiaSA=@pmCdCiaSA,VrImpuCons=@pmVrImpuCons,VrSobtasaCons=@pmVrSobtasaCons,BaseIvaIgp=@pmBaseIvaIgp,VrIvaIngProd=@pmVrIvaIngProd,TipSop=@pmTipSop,NumDocSop=@pmNumDocSop,CdCiaSop=@pmCdCiaSop
,ImpAviTab=@pmImpAviTab,TarifaAvta=@pmTarifaAvta,CodTarifAvta=@pmCodTarifAvta
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEntradas] @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Entrada,IdCia,Fecha,IdConcepto,IdProv,Factura,FechaFac,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion
,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,BaseIvaIgp,VrIvaIngProd
,TipOdc,OCompra,IdCiaOdc,MulPlazos,IdPlazo,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv
,CdCCosto,CdSubCos,TipEgr,Egreso,IdCiaEgr,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion
,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifBom,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpCarbono,VrImpuCons,VrSobtasaCons
,TipoSA,NumSalAlm,CdCiaSA,TipSop,NumDocSop,CdCiaSop,ImpAviTab,TarifaAvta,CodTarifAvta
 FROM Trn_Entradas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTraLiqDetalle] @pmTipDoc VARCHAR(3),@pmLiquidacion INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDescConcepto VARCHAR(250),@pmIdCuenta VARCHAR(16),@pmVrDebito MONEY,@pmVrCredito MONEY,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16)
,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmRubroConc VARCHAR(10),@pmVrBase MONEY,@pmTarifBase DECIMAL(14,4),@pmCantComb DECIMAL(14,4),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT
,@pmFecVence SMALLDATETIME,@pmRefConcep VARCHAR(50),@pmTipSop VARCHAR(3),@pmNumSoporte INT,@pmIdCiaSop CHAR(2),@pmDocSop BIT,@pmTipoImp VARCHAR(4),@pmCodTarImp VARCHAR(4),@pmItemBase INT
 AS 
 INSERT INTO Trn_TraLiqDetalle (TipDoc,Liquidacion,IdCia,Item,IdConcepto,DescConcepto,IdCuenta,VrDebito,VrCredito,IdTercero,CodAgncia,CdCCosto,CdSubCos,pVehiculo,RubroConc,VrBase,TarifBase,CantComb,TipFac,Factura,IdCiaFac,ItemFac,FecVence,RefConcep,TipSop,NumSoporte,IdCiaSop
,DocSop,TipoImp,CodTarImp,ItemBase)
 VALUES (@pmTipDoc,@pmLiquidacion,@pmIdCia,@pmItem,@pmIdConcepto,@pmDescConcepto,@pmIdCuenta,@pmVrDebito,@pmVrCredito,@pmIdTercero,@pmCodAgncia,@pmCdCCosto,@pmCdSubCos,@pmpVehiculo,@pmRubroConc,@pmVrBase,@pmTarifBase
,@pmCantComb,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmRefConcep,@pmTipSop,@pmNumSoporte,@pmIdCiaSop,@pmDocSop,@pmTipoImp,@pmCodTarImp,@pmItemBase)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsOilMediciones] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecProceso SMALLDATETIME,@pmIdTurno VARCHAR(4),@pmIdOperario VARCHAR(16),@pmNomOperario VARCHAR(150),@pmAjuInvSob INT,@pmAjuInvFal INT,@pmIdCiaAin CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipDocAju VARCHAR(3),@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OilMediciones (TipDoc,NumInvent,IdCia,Fecha,FecProceso,IdTurno,IdOperario,NomOperario,Modalidad,TipDocAju,AjuInvSob,AjuInvFal,IdCiaAin,Anulado,NumDev,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipDoc,@pmNumInvent,@pmIdCia,@pmFecha,@pmFecProceso,@pmIdTurno,@pmIdOperario,@pmNomOperario,@pmModalidad,@pmTipDocAju,@pmAjuInvSob,@pmAjuInvFal,@pmIdCiaAin,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpOilMediciones] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecProceso SMALLDATETIME,@pmIdTurno VARCHAR(4),@pmIdOperario VARCHAR(16),@pmNomOperario VARCHAR(150),@pmAjuInvSob INT,@pmAjuInvFal INT,@pmIdCiaAin CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipDocAju VARCHAR(3)
 AS 
 UPDATE Trn_OilMediciones SET Fecha=@pmFecha,FecProceso=@pmFecProceso,IdTurno=@pmIdTurno,IdOperario=@pmIdOperario,NomOperario=@pmNomOperario,AjuInvSob=@pmAjuInvSob,AjuInvFal=@pmAjuInvFal,IdCiaAin=@pmIdCiaAin,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,Modalidad=@pmModalidad,TipDocAju=@pmTipDocAju
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryOilMediciones] @pmTipDoc VARCHAR(3),@pmNumInvent INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumInvent,IdCia,Fecha,FecProceso,IdTurno,IdOperario,NomOperario,Modalidad,TipDocAju,AjuInvSob,AjuInvFal,IdCiaAin,Anulado,NumDev,FecDev,Observacion,IdEstado,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_OilMediciones
 WHERE TipDoc=@pmTipDoc AND NumInvent=@pmNumInvent AND IdCia=@pmIdCia
GO