if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelDocSoporte]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paDelDocSoporte]
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].[paInsEdsCortes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsEdsCortes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsEdsFormas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsEdsFormas]
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].[paInsPagos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsPagos]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTiposDocCons]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsTiposDocCons]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_EdsFormas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_EdsFormas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_EdsFormas_Cre]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_EdsFormas_Cre]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_EdsFormas_Fac]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_EdsFormas_Fac]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_EdsFormas_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_EdsFormas_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].[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].[paQryEdsCortes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEdsCortes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEdsCortes_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEdsCortes_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEdsCortesLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEdsCortesLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEdsCortesRel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEdsCortesRel]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEdsFormas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEdsFormas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEdsFormas_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEdsFormas_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryEdsFormasLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEdsFormasLta]
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].[paQryPagos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPagos]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryPagos_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPagos_Cr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryPagos_Lta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPagos_Lta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryPagosFor]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryPagosFor]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposDocCons]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposDocCons]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposDocConsLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTiposDocConsLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_EdsFormas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_EdsFormas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_EdsFormasDsc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_EdsFormasDsc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_EdsFormasDso]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_EdsFormasDso]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_EdsFormasEfe]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_EdsFormasEfe]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_EdsFormasTip]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_EdsFormasTip]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_EdsFormasTot]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQrytm_EdsFormasTot]
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].[paQryTraLiqViajesFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryTraLiqViajesFmt]
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].[paUpEdsCortes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpEdsCortes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpEdsFormas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpEdsFormas]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpEdsFormasPla]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpEdsFormasPla]
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].[paUpTiposDocCons]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpTiposDocCons]
GO



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelDocSoporte] @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DocSoporte
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTraLiqViajesFmt] @pmTipDoc VARCHAR(3),@pmLiquidacionIni INT,@pmLiquidacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT LQ.TipDoc AS TipoLiq,TipoDoc,LQ.Liquidacion AS NumLiquida,LQ.IdCia AS CdCia,Compania,Fecha,LQ.IdConcepto AS CdConcepto,C.Concepto AS DescConcepto,Modalidad,TipMuc,Manifiesto,IdCiaMuc,FechaMuc
,LQ.IdVehiculo AS PlacaVeh,LQ.IdConductor AS CedConductor,CDT.RazonSocial AS NomConductor,LQ.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,LQ.IdPoseedor AS NitPoseedor,T.RazonSocial AS Poseedor
,LQ.NitEmpresa AS Nit_Empresa,ETR.RazonSocial AS NomEmpresa,VrAnticipos,VrGastos,VrCombustible,VrOtrosCred,VrDiferencia,galsComb,PesoTotal,Cantidad,Volumen
,LQ.IdRuta AS CdRuta,Ruta,IdLocOrigen,CO.Localidad AS CiuOrigen,CO.IdDep AS CodDepOrigen,DPO.Departamento AS DptoOrigen,IdLocDestino,CD.Localidad AS CiuDestino,CD.IdDep AS CodDepDestino,DPD.Departamento AS DptoDestino
,Referencia,DescMcias,LiqMixto,Num_Ppto,kmtInicial,kmtFinal,EstFactura,TipDocRef,NumDocRef,IdCiaRef,FecDocRef,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,LQ.Observacion AS Observ,LQ.IdEstado AS CdEstado,Estado,TimeSys,LQ.FecUpdate AS FechaAct,IdCiaCrea,LQ.IdUsuario AS CdUsuario,Usuario,Leyenda
,Item,D.IdConcepto AS DetCodConc,CG.Concepto AS DetNomConc,DescConcepto,D.IdCuenta AS CdCuenta,NomCuenta,VrDebito,VrCredito,D.IdTercero AS NitTercero,N.RazonSocial AS NomTercero
,CdCCosto,CCosto,CdSubCos,SubCosto,D.RubroConc,VrBase,TarifBase,CantComb,TipFac,Factura,IdCiaFac,ItemFac,FecVence,RefConcep,D.TipSop,D.NumSoporte,D.IdCiaSop
--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
,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,V.NitEmpresa AS VehNitEmpresa,NE.RazonSocial AS VehNomEmpresa,V.IdPpd AS CdTipProp,TipoProp,VehPropio,TipoAfil,NumSoat,FecSoat,VigSoat
,CertGases,FecCertGas,VigCertGas,V.Descripcion AS VehDescripcion
 FROM Trn_TraLiqViajes AS LQ  INNER JOIN Companias AS CN ON LQ.IdCia=CN.IdCia INNER JOIN Sys_TiposDoc AS TD ON LQ.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON LQ.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON LQ.IdUsuario=U.IdUsuario
 INNER JOIN Conceptos AS C ON LQ.IdConcepto=C.IdConcepto
 INNER JOIN Localidades AS CO ON  LQ.IdLocOrigen=CO.IdLocal INNER JOIN Departamentos AS DPO ON CO.IdDep=DPO.IdDep
 INNER JOIN Localidades AS CD ON LQ.IdLocDestino=CD.IdLocal INNER JOIN Departamentos AS DPD ON CD.IdDep=DPD.IdDep
 INNER JOIN Rutas AS R ON LQ.IdRuta=R.IdRuta  INNER JOIN Terceros AS  CDT ON LQ.IdConductor=CDT.IdTercero 
 INNER JOIN Terceros AS NP ON LQ.IdPropietario=NP.IdTercero   INNER JOIN Terceros AS T ON LQ.IdPoseedor=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 INNER JOIN Vehiculos AS V ON LQ.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 ETR ON LQ.NitEmpresa=ETR.IdTercero LEFT JOIN TiposCom AS TCM ON LQ.TipCom=TCM.IdCom 
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 LEFT JOIN Trn_TraLiqDetalle AS D ON LQ.TipDoc=D.TipDoc AND LQ.Liquidacion=D.Liquidacion AND LQ.IdCia=D.IdCia
 LEFT JOIN ConcDiversos AS CG ON D.IdConcepto=CG.IdConcepto LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS N ON  D.IdTercero=N.IdTercero
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos 
 WHERE LQ.TipDoc=@pmTipDoc AND LQ.Liquidacion BETWEEN @pmLiquidacionIni AND @pmLiquidacionFin AND LQ.IdCia=@pmIdCia
 ORDER BY LQ.Liquidacion
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEdsCortesLta]  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmPlanillaIni INT=Null,@pmPlanillaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdJornada VARCHAR(4)=Null
,@pmNitTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Planilla,IdCia,Fecha,C.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,C.IdJornada AS  CdJornada,Jornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros
,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad
,FecCorte,NumCorte,NumTurno,TipoInt,VrDctoClie,VrExcedClie,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDoc
 FROM Trn_EdsCortes AS C  INNER JOIN Terceros AS T ON C.NitTercero=T.IdTercero
 INNER JOIN Terceros AS V ON C.IdVend=V.IdTercero
 INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario 
 INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Planilla BETWEEN ISNULL(@pmPlanillaIni,0) AND ISNULL(@pmPlanillaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND C.IdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')  AND NitTercero LIKE ISNULL(@pmNitTercero,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')  AND IdVend LIKE ISNULL(@pmIdVend,'%')  
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Planilla
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsEdsCortes] @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitTercero VARCHAR(16),@pmIdJornada VARCHAR(4),@pmVrSubTotal MONEY,@pmVrSobretasa MONEY
,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrSobrantes MONEY,@pmVrFaltantes MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrCreditos MONEY,@pmVrGastComb MONEY,@pmVrGastOtros MONEY,@pmVrPagosOtr MONEY
,@pmCantidad DECIMAL(14,4),@pmVrConTotal MONEY,@pmVrConImp MONEY,@pmVrConDcto MONEY,@pmVrConCos MONEY,@pmConCant DECIMAL(14,4),@pmVrCreTotal MONEY,@pmVrCreImp MONEY,@pmVrCreDcto MONEY,@pmVrCreCos MONEY,@pmCreCant DECIMAL(14,4)
,@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitRef VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmModalidad VARCHAR(10),@pmFecCorte SMALLDATETIME
,@pmNumCorte INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTipoInt VARCHAR(10),@pmVrDctoClie MONEY,@pmNumTurno INT,@pmVrExcedClie MONEY
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_EdsCortes (TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,IdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos
,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,IdVend,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad,FecCorte,NumCorte,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,TipoInt,VrDctoClie,NumTurno,VrExcedClie)
 VALUES (@pmTipDoc,@pmPlanilla,@pmIdCia,@pmFecha,@pmIdConcepto,@pmNitTercero,@pmIdJornada,@pmVrSubTotal,@pmVrSobretasa,@pmVrImpGlobal,@pmVrOtros,@pmVrSobrantes,@pmVrFaltantes,@pmVrCostos,@pmVrNeto,@pmVrEfectivo,@pmVrCreditos,@pmVrGastComb
,@pmVrGastOtros,@pmVrPagosOtr,@pmCantidad,@pmVrConTotal,@pmVrConImp,@pmVrConDcto,@pmVrConCos,@pmConCant,@pmVrCreTotal,@pmVrCreImp,@pmVrCreDcto,@pmVrCreCos,@pmCreCant,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmNitRef,@pmIdCCosto,@pmIdSubCos
,@pmReferencia,@pmModalidad,@pmFecCorte,@pmNumCorte,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoInt,@pmVrDctoClie,@pmNumTurno,@pmVrExcedClie)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpEdsCortes] @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitTercero VARCHAR(16),@pmIdJornada VARCHAR(4),@pmVrSubTotal MONEY
,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrSobrantes MONEY,@pmVrFaltantes MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrCreditos MONEY,@pmVrGastComb MONEY
,@pmVrGastOtros MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4),@pmVrConTotal MONEY,@pmVrConImp MONEY,@pmVrConDcto MONEY,@pmVrConCos MONEY,@pmConCant DECIMAL(14,4),@pmVrCreTotal MONEY,@pmVrCreImp MONEY
,@pmVrCreDcto MONEY,@pmVrCreCos MONEY,@pmCreCant DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitRef VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmReferencia VARCHAR(50),@pmModalidad VARCHAR(10),@pmFecCorte SMALLDATETIME,@pmNumCorte INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTipoInt VARCHAR(10),@pmVrDctoClie MONEY,@pmNumTurno INT,@pmVrExcedClie MONEY,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_EdsCortes SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,NitTercero=@pmNitTercero,IdJornada=@pmIdJornada,VrSubTotal=@pmVrSubTotal,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrOtros=@pmVrOtros,VrSobrantes=@pmVrSobrantes
,VrFaltantes=@pmVrFaltantes,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,VrEfectivo=@pmVrEfectivo,VrCreditos=@pmVrCreditos,VrGastComb=@pmVrGastComb,VrGastOtros=@pmVrGastOtros,VrPagosOtr=@pmVrPagosOtr,Cantidad=@pmCantidad,VrConTotal=@pmVrConTotal
,VrConImp=@pmVrConImp,VrConDcto=@pmVrConDcto,VrConCos=@pmVrConCos,ConCant=@pmConCant,VrCreTotal=@pmVrCreTotal,VrCreImp=@pmVrCreImp,VrCreDcto=@pmVrCreDcto,VrCreCos=@pmVrCreCos,CreCant=@pmCreCant,IdCajero=@pmIdCajero,IdVend=@pmIdVend
,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Referencia=@pmReferencia,Modalidad=@pmModalidad,FecCorte=@pmFecCorte,NumCorte=@pmNumCorte,NitRef=@pmNitRef
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,TipoInt=@pmTipoInt,VrDctoClie=@pmVrDctoClie,NumTurno=@pmNumTurno,VrExcedClie=@pmVrExcedClie
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryEdsCortes] @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,IdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros
,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,IdVend,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad
,FecCorte,NumCorte,NumTurno,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TipoInt,VrDctoClie,VrExcedClie,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsCortes
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsCortes_Cr] @pmTipDoc VARCHAR(3),@pmPlanillaIni INT,@pmPlanillaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS Tip_Doc,TipoDoc,Planilla,C.IdCia AS CdCia,Compania,C.Fecha AS FechaDoc,C.IdConcepto AS CodConcepto,Concepto
,NitTercero,T.RazonSocial AS NomTercero,C.IdJornada AS CdJornada,Jornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros
,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,VrDctoClie,VrExcedClie
,IdCajero,UC.Usuario AS NomCajero,C.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitRef,TR.RazonSocial AS NomTerceroRef,C.Referencia AS DocReferencia,Modalidad
,FecCorte,NumCorte,NumTurno,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TipoInt,C.TimeSys AS Fec_Add,C.FecUpdate AS Fec_Update,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--Detalles
,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,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd
,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,K.IdVend AS KarNitVend,KV.RazonSocial AS KarVendedor,Comision,CdOperario,KO.RazonSocial AS NomOperario,ComisnOper,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,CdMngra,NumInicial,NumFinal
--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,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_EdsCortes  AS C  INNER JOIN Trn_Kardex AS K ON C.TipDoc=K.TipDoc AND C.Planilla=K.Documento AND C.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN  Conceptos AS CNC ON C.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON C.IdVend=VN.IdTercero
 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 Terceros AS T ON C.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON C.NitRef=TR.IdTercero
 LEFT JOIN TiposCom AS TCM ON C.TipCom=TCM.IdCom
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN Terceros AS KV ON K.IdVend=KV.IdTercero  LEFT JOIN Terceros AS KO ON K.CdOperario=KO.IdTercero
 WHERE C.TipDoc=@pmTipDoc AND Planilla BETWEEN @pmPlanillaIni AND @pmPlanillaFin  AND C.IdCia=@pmIdCia
 ORDER BY Planilla,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsCortesRel] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdJornada VARCHAR(4)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Planilla,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CodConcepto,Concepto,NitTercero,T.RazonSocial AS NomTercero,C.IdJornada AS CdJornada,Jornada
,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp
,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,VrDctoClie,VrExcedClie
,IdCajero,UC.Usuario AS NomCajero,C.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitRef,TR.RazonSocial AS NomTerceroRef,C.Referencia AS DocReferencia,Modalidad
,FecCorte,NumCorte,TipoInt,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,C.FecUpdate AS Fec_Update,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_EdsCortes  AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia
 INNER JOIN  Conceptos AS CNC ON C.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON C.IdVend=VN.IdTercero
 LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON C.NitRef=TR.IdTercero
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdJornada LIKE ISNULL(@pmIdJornada,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY C.IdCia,Planilla
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryPagos_Lta] @pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdForma VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmEsCaja BIT=Null,@pmIdBanco VARCHAR(4)=Null
,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null,@pmNitCliente VARCHAR(16)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT TipDoc,TipoDoc,Documento,P.IdCia AS CodCia,Compania,Fecha,Item,P.IdForma AS CdForma,FormaPago,Detalle
,VrPagado,VrCambio,EsCaja,P.IdBanco AS CodBanco,B.Banco AS NomBanco,NumForma,FecForma,CtaForma,Beneficiario
,NitCliente,RazonSocial,CdAgencia,Agencia,CodAgencia,A.Referencia AS RefeAgencia,Referncia1,Referncia2,NumAutoriza,CdLocal,LF.Localidad AS CiudadPlaza,LF.IdDep AS CdDptoPlaza
,DF.Departamento AS DptoPlaza,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BC.Banco AS CtaBanco,VrDenom,Cantidad,TipDenom
,TipRef,DocRef,IdCiaRef,ItemDoc,P.CdProducto,FE_Ticket,Cant_Gals,PrecioBase,PrecioUnit,P.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS TerCodigo,NomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,L.IdDep AS TercCdDep,D.Departamento AS TercDpto,DocAnulado,NumDev,FecDev
 FROM  Trn_Pagos AS P INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS B ON P.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep 
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN CtasCorrientes AS CTA ON P.CdCta=CTA.IdCta LEFT JOIN  Bancos AS BC ON CTA.IdBanco=BC.IdBanco
 LEFT JOIN (SELECT RC.TipDoc AS DocTipo,RC.Recibo AS DocNumero,RC.IdCia AS DocCia,RC.Anulado AS DocAnulado,NumDev,FecDev FROM Trn_Recibos AS RC 
  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Factura,IdCia,Anulado,NumDev,FecDev FROM Trn_Factcon  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Consignacion,IdCia,Anulado,NumDev,FecDev FROM Trn_Consigna  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Recibo,IdCia,Anulado,NumDev,FecDev FROM Trn_CompCaja  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Planilla,IdCia,Anulado,NumDev,FecDev FROM Trn_EdsCortes  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  ) AS DOC ON P.TipDoc=DOC.DocTipo AND P.Documento=DOC.DocNumero  AND P.IdCia=DOC.DocCia
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdForma  LIKE ISNULL(@pmIdForma,'%') AND P.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND  (EsCaja=ISNULL(@pmEsCaja,0) or EsCaja=ISNULL(@pmEsCaja,1))
 AND Documento BETWEEN ISNULL(@pmDocumentoIni,0) AND ISNULL(@pmDocumentoFin,2147483647)
 AND  NitCliente LIKE ISNULL(@pmNitCliente,'%') AND P.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 ORDER BY TipDoc,P.IdCia,Documento,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposDocConsLta] @pmIdDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT N.IdDoc AS CdDoc,TipoDoc,N.IdCia AS CdCia,Compania,LDesde,LHasta,Resolucion,RangoNum,FechaRes,Prefijo,Numero
,NumManual,IntLotes,ConfigFecha,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias
,ConsUnico,Leyenda,FechaVigRes,ClaveTecDian,N.CodResol,N.FechaAdd AS Fec_Add,N.FechaUpdate AS Fec_Update
 FROM TiposDocCons AS N INNER JOIN Sys_TiposDoc AS T ON N.IdDoc=T.IdDoc
 INNER JOIN Companias AS C ON N.IdCia=C.IdCia
 WHERE T.Inactivo=0 AND N.IdDoc LIKE ISNULL(@pmIdDoc,'%')  AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY N.IdDoc,N.IdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryTiposDocCons] @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 SELECT IdDoc,IdCia,LDesde,LHasta,Resolucion,RangoNum,FechaRes,Prefijo,Numero
,NumManual,IntLotes,ConfigFecha,Formato,TipoPapel,Orientacion,VistaPrevia
,VerSetup,NumCopias,FechaVigRes,ClaveTecDian,CodResol,FechaAdd,FechaUpdate
 FROM TiposDocCons
 WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpTiposDocCons] @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2),@pmLDesde INT,@pmLHasta INT,@pmResolucion VARCHAR(30),@pmRangoNum VARCHAR(50),@pmFechaRes SMALLDATETIME
,@pmPrefijo VARCHAR(5),@pmNumero INT,@pmNumManual BIT,@pmIntLotes BIT,@pmConfigFecha VARCHAR(10),@pmFormato VARCHAR(20),@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT
,@pmVerSetup BIT,@pmNumCopias INT,@pmFechaVigRes SMALLDATETIME,@pmClaveTecDian VARCHAR(250),@pmCodResol VARCHAR(4),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TiposDocCons SET LDesde=@pmLDesde,LHasta=@pmLHasta,Resolucion=@pmResolucion,RangoNum=@pmRangoNum,FechaRes=@pmFechaRes,Prefijo=@pmPrefijo
,Numero=@pmNumero,NumManual=@pmNumManual,IntLotes=@pmIntLotes,ConfigFecha=@pmConfigFecha,Formato=@pmFormato,TipoPapel=@pmTipoPapel
,Orientacion=@pmOrientacion,VistaPrevia=@pmVistaPrevia,VerSetup=@pmVerSetup,NumCopias=@pmNumCopias,FechaUpdate=@pmFechaUpdate
,FechaVigRes=@pmFechaVigRes,ClaveTecDian=@pmClaveTecDian,CodResol=@pmCodResol
 WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsTiposDocCons] @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2),@pmLDesde INT,@pmLHasta INT,@pmResolucion VARCHAR(30)
,@pmRangoNum VARCHAR(50),@pmFechaRes SMALLDATETIME,@pmPrefijo VARCHAR(5),@pmNumero INT,@pmNumManual BIT,@pmIntLotes BIT
,@pmConfigFecha VARCHAR(10),@pmFormato VARCHAR(20),@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT,@pmVerSetup BIT
,@pmNumCopias INT,@pmFechaVigRes SMALLDATETIME,@pmClaveTecDian VARCHAR(250),@pmCodResol VARCHAR(4),@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO TiposDocCons (IdDoc,IdCia,LDesde,LHasta,Resolucion,RangoNum,FechaRes,Prefijo,Numero,NumManual,IntLotes,ConfigFecha
,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,FechaAdd,FechaVigRes,ClaveTecDian,CodResol)
 VALUES (@pmIdDoc,@pmIdCia,@pmLDesde,@pmLHasta,@pmResolucion,@pmRangoNum,@pmFechaRes,@pmPrefijo,@pmNumero
,@pmNumManual,@pmIntLotes,@pmConfigFecha,@pmFormato,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmFechaAdd,@pmFechaVigRes,@pmClaveTecDian,@pmCodResol)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormas_Cr]  @pmTipoRango VARCHAR(3),@pmIdRango INT
,@pmNumSerie VARCHAR(5),@pmNumFormaIni INT,@pmNumFormaFin INT
 AS 
 SELECT F.TipoRango AS TipRango,F.IdRango AS NumRango,F.NumSerie AS NroSerie,F.NumForma AS NumVale
,Fecha,VrTotal,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,F.IdCuenta AS  CdCuenta,NomCuenta
,pVehiculo,nVehiculo,IdCajero,Usuario,F.IdVend AS CdVend,V.RazonSocial AS Vendedor
,F.Referencia AS Referncia,NumRef2,F.Observacion AS Observ,CantPuntos,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm
,Anulado,NumDev,FecDev,Planilla,F.IdCia AS CdCia,Compania,F.VrPrecio AS VrPrecioUnd,F.VrBase,F.VrPrecioEsp
--detalles
,D.Item AS nItem,D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,Bodega,D.Cantidad AS Cant,D.VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto
,Servicios,Descripcion,ListaPrec,PM.IdUnd AS CdUnid,Unidad,CdCCosto,CdSubCos,D.CantDev,D.CantFact,D.CdOperario,OP.RazonSocial AS Operario,D.Comision
 FROM Trn_EdsFormas AS F INNER JOIN Trn_EdsForItems AS D ON F.TipoRango=D.TipoRango
 AND F.IdRango=D.IdRango AND F.NumSerie=D.NumSerie AND F.NumForma=D.NumForma
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia
 INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 INNER JOIN adm_Usuarios AS U ON F.IdCajero=U.IdUsuario
 INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
 INNER JOIN ProdMcias AS PM ON D.IdProducto=PM.IdProducto 
 INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 LEFT JOIN Companias AS C ON F.IdCia=C.IdCia
 LEFT JOIN Terceros AS OP ON D.CdOperario=OP.IdTercero
 WHERE F.TipoRango=@pmTipoRango AND F.IdRango=@pmIdRango
 AND F.NumSerie=@pmNumSerie  AND F.NumForma BETWEEN @pmNumFormaIni AND @pmNumFormaFin
 ORDER  BY D.Item
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,@pmInactivo BIT
 AS 
 INSERT INTO Formaspago (IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,VleConsumo,CodIntegrity,CodValera,Inactivo,CodIntCom,RefProducto,FactTicket)
 VALUES (@pmIdForma,@pmFormaPago,@pmConsecutivo,@pmCuadreCaja,@pmRefDcmto,@pmRefNit,@pmRefBco,@pmRefFech,@pmRefAtza
,@pmRefOtro,@pmVldRangos,@pmVleConsumo,@pmCodIntegrity,@pmCodValera,@pmInactivo,@pmCodIntCom,@pmRefProducto,@pmFactTicket)
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,@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,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
  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,Inactivo
  FROM Formaspago
  WHERE IdForma=@pmIdForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPagos_Cr] @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,TipoDoc,Documento,P.IdCia AS CodCia,Compania,Fecha,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja
,P.IdBanco AS CodBanco,B.Banco AS NomBanco,NitCliente,RazonSocial,CdAgencia,Agencia,CodAgencia,A.Referencia AS RefeAgencia,NumForma
,FecForma,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,CdProducto,FE_Ticket
,Cant_Gals,PrecioBase,PrecioUnit,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BC.Banco AS CtaBanco,P.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS TerCodigo,NomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento,Telefono,e_mail
 FROM  Trn_Pagos AS P INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS B ON P.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN CtasCorrientes AS CTA ON P.CdCta=CTA.IdCta LEFT JOIN  Bancos AS BC ON CTA.IdBanco=BC.IdBanco
 WHERE P.TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND P.IdCia=@pmIdCia
 ORDER BY Documento,Item
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormasDso] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmCredito,tmIdForma,FormaPago,tmTipoRango,tmIdRango,tmNumSerie,tmNumForma,tmNForma,tmVrTotal
,tmIdBanco,Banco,tmObservacion,tmIdCuenta,tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,tmpVehiculo,tmnVehiculo
,tmIdVend,V.RazonSocial AS Vendedor,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmFecForma,tmCtaForma,tmBeneficiario
,tmIdUsuario,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm,tmTipDenom,tmCdCta,tmEsCaja,tmCantPuntos
,tmTipAplica,tmNumero,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmVrPrecio*tmCantidad AS VrPublico,tmVrTotal-(tmVrPrecio*tmCantidad) AS Excedente
,tmVrBase,tmVrPresp
 FROM tm_EdsFormas AS P INNER JOIN Formaspago AS F ON P.tmIdForma=F.IdForma
 INNER JOIN Bancos AS B ON P.tmIdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON P.tmIdCliente=T.IdTercero
 LEFT JOIN Terceros AS V ON P.tmIdVend=V.IdTercero
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormasLta] @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT F.TipoRango,F.IdRango,F.NumSerie,F.NumForma,F.Planilla,F.IdCia AS CdCia,Compania,F.Item,F.Fecha,F.VrTotal,F.IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia
,F.IdCuenta AS CdCuenta,NomCuenta,F.pVehiculo,F.nVehiculo,F.IdCajero,Usuario,F.IdVend AS CdVend,V.RazonSocial AS Vendedor,F.CdProducto,DescripProd,F.Cantidad,F.IdForma AS CdForma,FormaPago
,F.Referencia AS Referncia,F.NumRef2,F.CantPuntos,F.Observacion AS Observ,F.TipDcm,F.Documento,F.IdCiaDcm,F.FechaDcm,AgencDcm,F.Anulado,F.NumDev,F.FecDev,F.CdCCosto,F.CdSubCos,F.VrPrecio,F.VrBase,F.VrPrecioEsp
,ISNULL(D.Cant_Neto,0) AS Cant_Neto,ISNULL(D.Cant_Dev,0) AS Cant_Dev,(ISNULL(D.STOTDEV,0)-ISNULL(D.SDCTDEV,0)+ISNULL(D.SIVADEV,0)) AS TotalDev
FROM Trn_EdsFormas AS F INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia
 INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta INNER JOIN adm_Usuarios AS U ON F.IdCajero=U.IdUsuario
 INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma
 INNER JOIN Companias AS C ON F.IdCia=C.IdCia
 LEFT JOIN ProdMcias AS R ON F.CdProducto=R.IdProducto  
 LEFT JOIN (SELECT TipoRango,IdRango,NumSerie,NumForma,SUM(Cantidad-CantDev) AS Cant_Neto,SUM(CantDev) AS Cant_Dev,SUM((Cantidad-CantDev)*VrPrecio) AS SUBTOTAL
,SUM(CASE WHEN (CantDev>0 AND TarifaDct>0 AND VrDcto>0) THEN (((Cantidad-CantDev)*VrPrecio)*TarifaDct)/100 ELSE VrDcto END) AS STOTDCT
,SUM(CASE WHEN (CantDev>0 AND TarifaIva>0 AND VrIva>0) THEN ((((Cantidad-CantDev)*VrPrecio)-((((Cantidad-CantDev)*VrPrecio)*TarifaDct)/100))*TarifaIva)/100 ELSE VrIva END) AS STOTIVA
,SUM(CantDev*VrPrecio) AS STOTDEV,SUM(((CantDev*VrPrecio)*TarifaDct)/100) AS SDCTDEV
,SUM((((CantDev*VrPrecio)-(((CantDev*VrPrecio)*TarifaDct)/100))*TarifaIva)/100) AS SIVADEV
FROM Trn_EdsForItems GROUP BY TipoRango,IdRango,NumSerie,NumForma) AS D ON F.TipoRango=D.TipoRango AND F.IdRango=D.IdRango AND F.NumSerie=D.NumSerie AND F.NumForma=D.NumForma
WHERE (F.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND F.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
AND (F.IdCia=@pmIdCia OR @pmIdCia IS NULL) AND (F.IdCliente=@pmIdCliente OR @pmIdCliente IS NULL)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormasDsc] @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmTipoRango,tmIdRango,tmNumSerie,tmNumForma,tmVrTotal,tmIdCuenta,tmIdCliente,T.RazonSocial AS NomCliente
,tmIdAgencia,tmpVehiculo,tmnVehiculo,tmCdProducto,DescripProd,tmCantidad,tmReferencia,tmReferncia2,tmObservacion,tmIdForma,FormaPago
,tmIdUsuario,tmIdVend,V.RazonSocial AS Vendedor,tmCantPuntos,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmTipAplica,tmVrBase,tmVrPresp
 FROM tm_EdsFormas  AS P INNER JOIN Formaspago AS F ON P.tmIdForma=F.IdForma
 INNER JOIN Terceros AS T ON P.tmIdCliente=T.IdTercero
 LEFT JOIN Terceros AS V ON P.tmIdVend=V.IdTercero
 LEFT JOIN ProdMcias AS PD ON P.tmCdProducto=PD.IdProducto
 WHERE tmNumero=@pmtmNumero  AND tmCredito<>0
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormasTip] @pmtmNumero VARCHAR(5),@pmtmCredito BIT
 AS 
 SELECT tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma
,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo
,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza
,tmVrDenom,tmCantDenm,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmVrBase,tmVrPresp
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito=@pmtmCredito
 ORDER BY tmItem
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_EdsFormas_Fac] @pmtmNumero VARCHAR(5),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2)
AS 
 INSERT INTO tm_EdsFormas (tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma
,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm
,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmVrBase,tmVrPresp)
SELECT @pmtmNumero,ROW_NUMBER() OVER(ORDER BY Planilla),1,TipoRango,IdRango,IdForma,Observacion,0,'0','0',Fecha,'0','',NumSerie,NumForma,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo
,IdVend,ISNULL(CdProducto,'0'),Cantidad,Referencia,NumRef2,CantPuntos,'0','',0,0,'B','0','',IdCajero,CdCCosto,CdSubCos,VrPrecio,VrBase,VrPrecioEsp
FROM Trn_EdsFormas
WHERE TipDcm=@pmTipDcm AND Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm
AND Planilla>0 AND IdCia<>'' AND IdCia<>'00'
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_EdsFormas_Cre]  @pmtmNumero VARCHAR(5),@pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_EdsFormas (tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma
,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm
,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmVrBase,tmVrPresp)
 SELECT @pmtmNumero,Item,1,TipoRango,IdRango,IdForma,Observacion,0,'0','0',Fecha,'0','',NumSerie,NumForma,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo
,IdVend,ISNULL(CdProducto,'0'),Cantidad,Referencia,NumRef2,CantPuntos,'0','',0,0,'B','0','',IdCajero,CdCCosto,CdSubCos,VrPrecio,VrBase,VrPrecioEsp
 FROM Trn_EdsFormas
 WHERE Planilla=@pmPlanilla AND IdCia=@pmIdCia
 GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormasTot] @pmtmNumero VARCHAR(5),@pmtmCredito BIT=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrTotal) AS STOTAL,SUM(tmCantPuntos) AS STOTPUN
,SUM(tmCantidad) AS STOTCAN,SUM(tmVrDenom*tmCantDenm) AS STOTDEN,SUM(tmCantDenm) AS SCANDEN
,SUM(CASE WHEN tmVrPrecio>0 AND tmCantidad>0 AND tmVrTotal>(tmCantidad*tmVrPrecio) THEN tmVrTotal-(tmCantidad*tmVrPrecio) ELSE 0 END) AS STOTEXCD
,SUM(CASE WHEN tmVrPrecio>0 AND tmCantidad>0 AND (tmCantidad*tmVrPrecio)>tmVrTotal THEN (tmCantidad*tmVrPrecio)-tmVrTotal ELSE 0 END) AS STOTDCT
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero 
 AND (tmCredito=ISNULL(@pmtmCredito,0) or tmCredito=ISNULL(@pmtmCredito,1))
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormasEfe] @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrTotal) AS STOTAL,SUM(tmCantPuntos) AS STOTPUN
,SUM(tmCantidad) AS STOTCAN,SUM(tmVrDenom*tmCantDenm) AS STOTDEN,SUM(tmCantDenm) AS SCANDEN
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito=0 AND tmTipoRango<>'TKT' AND tmIdForma='EFE'
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_EdsFormas_Sel] @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_EdsFormas (tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma
,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm
,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmVrBase,tmVrPresp)
 SELECT @pmtmNumero,Item,0,CASE WHEN FE_Ticket=1 THEN 'TKT' ELSE '0' END,0,IdForma,Detalle,EsCaja,IdBanco,NumForma,FecForma,CtaForma,Beneficiario,'0',0
,VrPagado,NitCliente,CdAgencia,'0','0','0','0',ISNULL(CdProducto,'0'),Cant_Gals,Referncia1,Referncia2,0,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,CdCta,'',IdUsuario,'0','0',0,PrecioBase,PrecioUnit
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_EdsFormas] @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmCredito BIT,@pmtmTipoRango VARCHAR(3),@pmtmIdRango INT,@pmtmIdForma VARCHAR(4)
,@pmtmObservacion VARCHAR(150),@pmtmEsCaja BIT,@pmtmIdBanco VARCHAR(4),@pmtmNForma VARCHAR(20),@pmtmFecForma SMALLDATETIME,@pmtmCtaForma VARCHAR(30)
,@pmtmBeneficiario VARCHAR(150),@pmtmNumSerie VARCHAR(5),@pmtmNumForma INT,@pmtmVrTotal MONEY,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16)
,@pmtmIdCuenta VARCHAR(16),@pmtmpVehiculo VARCHAR(10),@pmtmnVehiculo VARCHAR(10),@pmtmIdVend VARCHAR(16),@pmtmCdProducto VARCHAR(16)
,@pmtmCantidad DECIMAL(14,4),@pmtmReferencia VARCHAR(50),@pmtmReferncia2 VARCHAR(50),@pmtmCantPuntos DECIMAL(14,4),@pmtmCdLocal VARCHAR(8)
,@pmtmNumAutoriza VARCHAR(30),@pmtmVrDenom MONEY,@pmtmCantDenm INT,@pmtmTipDenom CHAR(1),@pmtmCdCta VARCHAR(4),@pmtmTipAplica VARCHAR(10),@pmtmIdUsuario VARCHAR(11)
,@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmVrPrecio MONEY,@pmtmVrBase MONEY,@pmtmVrPresp MONEY
 AS 
 INSERT INTO tm_EdsFormas (tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma
,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm
,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmVrBase,tmVrPresp)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmCredito,@pmtmTipoRango,@pmtmIdRango,@pmtmIdForma,@pmtmObservacion,@pmtmEsCaja,@pmtmIdBanco,@pmtmNForma,@pmtmFecForma,@pmtmCtaForma
,@pmtmBeneficiario,@pmtmNumSerie,@pmtmNumForma,@pmtmVrTotal,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdCuenta,@pmtmpVehiculo,@pmtmnVehiculo,@pmtmIdVend,@pmtmCdProducto
,@pmtmCantidad,@pmtmReferencia,@pmtmReferncia2,@pmtmCantPuntos,@pmtmCdLocal,@pmtmNumAutoriza,@pmtmVrDenom,@pmtmCantDenm,@pmtmTipDenom,@pmtmCdCta,@pmtmTipAplica,@pmtmIdUsuario,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmVrPrecio,@pmtmVrBase,@pmtmVrPresp)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQrytm_EdsFormas] @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma
,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo
,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza
,tmVrDenom,tmCantDenm,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos,tmVrPrecio,tmVrBase,tmVrPresp
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero 
  AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
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,Compania,D.Fecha,D.IdTercero AS NitTercero,T.RazonSocial,D.IdAdquiriente,N.RazonSocial AS Adquiriente
,D.ValorTotal,D.Retencion,D.ReteIca,D.TarifaRet,D.TarifaIca,D.DescServicio,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.CodTarRet,D.CodTarIca
,D.CodRes,R.Resolucion,R.Prefijo,R.NumInicial,R.NumFinal,R.FechaExpRes,R.FechaVigencia,R.Establecimiento,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 CN ON D.IdCia=CN.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
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].[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
 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].[paQryDocSoporteFmt] @pmTipDoc VARCHAR(3),@pmNumDocIni INT,@pmNumDocFin INT,@pmIdCia CHAR(2)
AS 
 SELECT D.TipDoc,TipoDoc,D.NumDoc,D.IdCia AS CdCia,Compania,D.Fecha,D.IdTercero AS NitTercero,T.RazonSocial,D.IdAdquiriente,N.RazonSocial AS Adquiriente
,D.ValorTotal,D.Retencion,D.ReteIca,D.TarifaRet,D.TarifaIca,D.DescServicio,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.CodTarRet,D.CodTarIca
,DT.Item,DT.TipCom,TipoCom,DT.Comprobante,DT.IdCiaCom,DT.ItemCom,DT.CdCuenta,DT.Detalle,DT.NitTercero AS DetNitTercero,NT.RazonSocial AS NomTercero,DT.ValorOper
,D.CodRes,R.Resolucion,R.Prefijo,R.NumInicial,R.NumFinal,R.FechaExpRes,R.FechaVigencia,R.Establecimiento,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 CN ON D.IdCia=CN.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 TiposComRes AS R ON D.CodRes=R.IdRes
LEFT JOIN Terceros AS N ON D.IdAdquiriente=N.IdTercero
LEFT JOIN Terceros AS NT ON DT.NitTercero=NT.IdTercero
LEFT JOIN TiposCom AS TC ON DT.TipCom=TC.IdCom
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].[paInsPagos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdForma VARCHAR(4),@pmDetalle VARCHAR(150),@pmVrPagado MONEY,@pmVrCambio MONEY
,@pmEsCaja BIT,@pmIdBanco VARCHAR(4),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmNumForma VARCHAR(20),@pmFecForma SMALLDATETIME,@pmCtaForma VARCHAR(30),@pmBeneficiario VARCHAR(150),@pmReferncia1 VARCHAR(50)
,@pmReferncia2 VARCHAR(50),@pmCdLocal VARCHAR(8),@pmNumAutoriza VARCHAR(30),@pmVrDenom MONEY,@pmCantidad INT,@pmTipDenom CHAR(1),@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmItemDoc INT
,@pmCdCta VARCHAR(4),@pmCdProducto VARCHAR(16),@pmFE_Ticket BIT,@pmCant_Gals DECIMAL(14,4),@pmPrecioBase MONEY,@pmPrecioUnit MONEY,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Pagos (TipDoc,Documento,IdCia,Item,Fecha,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta,IdUsuario
,CdProducto,FE_Ticket,Cant_Gals,PrecioBase,PrecioUnit)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecha,@pmIdForma,@pmDetalle,@pmVrPagado,@pmVrCambio,@pmEsCaja,@pmIdBanco,@pmNitCliente,@pmCdAgencia,@pmNumForma,@pmFecForma,@pmCtaForma,@pmBeneficiario,@pmReferncia1,@pmReferncia2
,@pmCdLocal,@pmNumAutoriza,@pmVrDenom,@pmCantidad,@pmTipDenom,@pmTipRef,@pmDocRef,@pmIdCiaRef,@pmItemDoc,@pmCdCta,@pmIdUsuario,@pmCdProducto,@pmFE_Ticket,@pmCant_Gals,@pmPrecioBase,@pmPrecioUnit)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryPagos] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Fecha,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma
,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta
,CdProducto,FE_Ticket,Cant_Gals,PrecioBase,PrecioUnit,IdUsuario
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento 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 OFF
GO
CREATE PROCEDURE [dbo].[paQryPagosFor] @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmIdBanco VARCHAR(4)
 AS 
 SELECT Item,Fecha,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma
,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta,CdProducto,FE_Ticket,IdUsuario
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND IdForma=@pmIdForma  AND NumForma=@pmNumForma AND IdBanco=@pmIdBanco
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),@pmValorTotal DECIMAL(14,4),@pmRetencion DECIMAL(14,4),@pmReteIca DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmCodTarRet VARCHAR(4)
,@pmCodTarIca VARCHAR(4),@pmDescServicio VARCHAR(500),@pmCodRes VARCHAR(4),@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,ValorTotal,Retencion,ReteIca,TarifaRet,TarifaIca,CodTarRet,CodTarIca,DescServicio,CodRes,TipDocRef,NumDocRef,IdCiaRef,Anulado,TipDocDev,NumDev,CdCiaDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumDoc,@pmIdCia,@pmFecha,@pmIdTercero,@pmIdAdquiriente,@pmValorTotal,@pmRetencion,@pmReteIca,@pmTarifaRet,@pmTarifaIca,@pmCodTarRet,@pmCodTarIca,@pmDescServicio,@pmCodRes,@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),@pmValorTotal DECIMAL(14,4),@pmRetencion DECIMAL(14,4),@pmReteIca DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmCodTarRet VARCHAR(4)
,@pmCodTarIca VARCHAR(4),@pmDescServicio VARCHAR(500),@pmCodRes VARCHAR(4),@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,ValorTotal=@pmValorTotal,Retencion=@pmRetencion,ReteIca=@pmReteIca,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,DescServicio=@pmDescServicio,CodRes=@pmCodRes,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 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,ValorTotal,Retencion,ReteIca,TarifaRet,TarifaIca,CodTarRet,CodTarIca,DescServicio,CodRes,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 OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEdsFormas] @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmVrTotal MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdCuenta VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmCdProducto VARCHAR(16)
,@pmCantidad DECIMAL(14,4),@pmIdForma VARCHAR(4),@pmItem INT,@pmReferencia VARCHAR(50),@pmCantPuntos DECIMAL(14,4),@pmObservacion VARCHAR(150),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFechaDcm SMALLDATETIME,@pmAgencDcm VARCHAR(16),@pmAnulado BIT
,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmVrPrecio MONEY,@pmNumRef2 VARCHAR(50),@pmVrBase MONEY,@pmVrPrecioEsp MONEY
 AS 
 UPDATE Trn_EdsFormas SET Planilla=@pmPlanilla,IdCia=@pmIdCia,Fecha=@pmFecha,VrTotal=@pmVrTotal,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdCuenta=@pmIdCuenta,pVehiculo=@pmpVehiculo,nVehiculo=@pmnVehiculo,IdCajero=@pmIdCajero,IdVend=@pmIdVend,CdProducto=@pmCdProducto,Cantidad=@pmCantidad,IdForma=@pmIdForma,Item=@pmItem,Referencia=@pmReferencia,CantPuntos=@pmCantPuntos
,Observacion=@pmObservacion,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,FechaDcm=@pmFechaDcm,AgencDcm=@pmAgencDcm,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,VrPrecio=@pmVrPrecio,NumRef2=@pmNumRef2,VrBase=@pmVrBase,VrPrecioEsp=@pmVrPrecioEsp
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsEdsFormas] @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME
,@pmVrTotal MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdCuenta VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCajero VARCHAR(11)
,@pmIdVend VARCHAR(16),@pmCdProducto VARCHAR(16),@pmCantidad DECIMAL(14,4),@pmIdForma VARCHAR(4),@pmItem INT,@pmReferencia VARCHAR(50),@pmCantPuntos DECIMAL(14,4)
,@pmObservacion VARCHAR(150),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFechaDcm SMALLDATETIME,@pmAgencDcm VARCHAR(16),@pmAnulado BIT
,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmVrPrecio MONEY,@pmNumRef2 VARCHAR(50),@pmVrBase MONEY,@pmVrPrecioEsp MONEY
 AS 
 INSERT INTO Trn_EdsFormas (TipoRango,IdRango,NumSerie,NumForma,Planilla,IdCia,Fecha,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo,IdCajero,IdVend,CdProducto,Cantidad
,IdForma,Item,Referencia,CantPuntos,Observacion,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm,Anulado,NumDev,FecDev,CdCCosto,CdSubCos,VrPrecio,NumRef2,VrBase,VrPrecioEsp)
 VALUES (@pmTipoRango,@pmIdRango,@pmNumSerie,@pmNumForma,@pmPlanilla,@pmIdCia,@pmFecha,@pmVrTotal,@pmIdCliente,@pmIdAgencia,@pmIdCuenta,@pmpVehiculo
,@pmnVehiculo,@pmIdCajero,@pmIdVend,@pmCdProducto,@pmCantidad,@pmIdForma,@pmItem,@pmReferencia,@pmCantPuntos,@pmObservacion
,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmFechaDcm,@pmAgencDcm,@pmAnulado,@pmNumDev,@pmFecDev,@pmCdCCosto,@pmCdSubCos,@pmVrPrecio,@pmNumRef2,@pmVrBase,@pmVrPrecioEsp)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paQryEdsFormas] @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,Planilla,IdCia,Fecha,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo,IdCajero
,IdVend,CdProducto,Cantidad,IdForma,Item,Referencia,CantPuntos,Observacion,TipDcm,Documento,IdCiaDcm,FechaDcm
,AgencDcm,Anulado,NumDev,FecDev,CdCCosto,CdSubCos,VrPrecio,NumRef2,VrBase,VrPrecioEsp
 FROM Trn_EdsFormas
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paUpEdsFormasPla] @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmPlanilla INT,@pmIdCia CHAR(2),@pmVrTotal MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmIdCuenta VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmCdProducto VARCHAR(16),@pmCantidad DECIMAL(14,4)
,@pmIdForma VARCHAR(4),@pmReferencia VARCHAR(50),@pmCantPuntos DECIMAL(14,4),@pmObservacion VARCHAR(150),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmNumRef2 VARCHAR(50),@pmVrBase MONEY,@pmVrPrecioEsp MONEY,@pmVrPrecio MONEY
 AS 
 UPDATE Trn_EdsFormas SET VrTotal=@pmVrTotal,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdCuenta=@pmIdCuenta,pVehiculo=@pmpVehiculo,nVehiculo=@pmnVehiculo,IdCajero=@pmIdCajero
,IdVend=@pmIdVend,CdProducto=@pmCdProducto,Cantidad=@pmCantidad,IdForma=@pmIdForma,Referencia=@pmReferencia,CantPuntos=@pmCantPuntos,Observacion=@pmObservacion,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos
,NumRef2=@pmNumRef2,VrBase=@pmVrBase,VrPrecioEsp=@pmVrPrecioEsp,VrPrecio=@pmVrPrecio
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
 AND  Planilla=@pmPlanilla 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)
 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)
 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)
GO
--Sept 10 

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].[paUpEntradas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpEntradas]
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].[paQryEntradas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryEntradas]
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].[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].[paInstm_TraDetConc_Leg]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_TraDetConc_Leg]
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,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
,E.TarifaIva AS TarifIva,E.TarifaRet AS TarifRet,E.TarifaIca AS TarifIca,TarifaRiv,VrIvaMvc,VrReteCREE,TarifaRtc,CodTarRtc,CodTarifBom,VrBomberil,TarifaBom,BaseIvaObsq,VrIvaObsequio,VrImpuCons,VrSobtasaCons
,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].[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,CdCCosto,CCosto,CdSubCos,SubCosto,TipEgr,Egreso,IdCiaEgr,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 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)
 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
 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].[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
,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 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)
,@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)
 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)
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),@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
 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
 FROM Trn_Entradas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelCompanias]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paDelCompanias]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paDelCompanias] @pmIdCia CHAR(2)
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'TrnCompanias'
  BEGIN TRANSACTION @TrnNombre
  DELETE FROM CompListas WHERE IdCia=@pmIdCia 
  DELETE FROM Companias WHERE IdCia=@pmIdCia
  COMMIT TRANSACTION  @TrnNombre
 END
GO