if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinAfavor]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsFinAfavor]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinAportes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsFinAportes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinCanAportes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsFinCanAportes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinConvenios]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsFinConvenios]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinRecibos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInsFinRecibos]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_PreCartera_Apo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_PreCartera_Apo]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_PreCartera_Saf]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paInstm_PreCartera_Saf]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinAfavor]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinAfavor]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinAportes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinAportes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinAportesFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinAportesFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinCanAportes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinCanAportes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinCanAportesFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinCanAportesFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinConvenios]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinConvenios]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinConveniosFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinConveniosFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinRecibos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinRecibos]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinRecibosCr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinRecibosCr]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinRecibosDoc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinRecibosDoc]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinRecibosFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinRecibosFmt]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinRecibosLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paQryFinRecibosLta]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpFinAportes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpFinAportes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpFinCanAportes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpFinCanAportes]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpFinConvenios]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpFinConvenios]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpFinRecibos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
 DROP PROCEDURE [dbo].[paUpFinRecibos]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinRecibosCr] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null 
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,Fecha,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,NomAgencia AS Agencia,FecPago
,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo,VrAboOtros,BaseRet,BaseIca,BaseRiv
,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,R.IdVend AS CdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma
,DetallePago,CdBanco,Banco,CdCta,R.Referencia AS Refrencia,TipoCredito,pVehiculo,CdConductor,CD.RazonSocial AS Conductor,TipDcm,Documento,IdCiaDcm,VrAboCapital,LiqAboCapital
,VrPagoCuotas,VrPagoAportes,VrPagoConvenio,VrAboCuotas,VrAboAportes,VrAboConvenio,VrAntPrestamo,VrAntAportes,VrAntConvenio
,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario,TipDoc
--Datos del cliente
,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,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinRecibos AS R  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 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  TercCliePrestamo AS CLI ON R.IdCliente=CLI.IdClie AND R.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona   INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom  
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Bancos AS B ON R.CdBanco=B.IdBanco  LEFT JOIN Terceros AS CD ON R.CdConductor=CD.IdTercero
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND R.IdVend LIKE ISNULL(@pmIdVend,'%')  AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY R.IdCia,Recibo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinAportesFmt] @pmTipLiq VARCHAR(3),@pmNumLiquidaIni INT,@pmNumLiquidaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT L.TipLiq,L.NumLiquida,L.IdCia,CI.Compania,L.Fecha,L.FecLiquida,L.CodCia,L.CodGrupo,GrupoClie,L.CodTipoTerc,TipoTercero,L.ValorTotal,L.BaseApo
,L.TipCom,TipoCom,L.Comprobante,L.IdCiaCom,L.Observacion,L.IdEstado,ED.Estado,L.Anulado,L.NumDev,L.FecDev,L.FechaCrea,L.IdCiaCrea,L.OrigenAdd,L.IdUsuario,Usuario
,D.Item,D.FecVence,D.IdCliente,T.RazonSocial AS NomCliente,D.IdAgencia,NomAgencia,D.BaseAporte,D.TarifaApo,D.VrAporte,D.CdTarifApo,D.Descripcion
,D.FecUltPago,D.FecUltLiq,D.DctoNom,D.CodConcNom,D.PeriodoNom
 FROM Trn_FinAportes AS L INNER JOIN Trn_FinAportesDet AS D ON L.TipLiq=D.TipLiq AND L.NumLiquida=D.NumLiquida AND L.IdCia=D.IdCia
 INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON L.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CI ON L.IdCia=CI.IdCia INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero 
 INNER JOIN TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 LEFT JOIN TiposCom AS TC ON L.TipCom=TC.IdCom
 LEFT JOIN GruposCli AS GC ON L.CodGrupo=GC.IdGrupo LEFT JOIN TiposTerc AS TT ON L.CodTipoTerc=TT.IdTipoTerc
 WHERE L.TipLiq=@pmTipLiq AND L.NumLiquida BETWEEN @pmNumLiquidaIni AND @pmNumLiquidaFin AND L.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinRecibosFmt] @pmTipDoc VARCHAR(3),@pmReciboIni INT, @pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,R.Fecha AS FecRecibo,R.IdConcepto AS CdConcepto,Concepto,R.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,NomAgencia AS NomAgencia
,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo
,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,R.IdVend AS CdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,EnEfectivo,CdForma,R.NumForma AS RecNumForma,DetallePago,CdBanco,BR.Banco AS RecBanco,R.CdCta AS RecCodCta,R.Referencia AS RecReferncia,pVehiculo,CdConductor
,TipoCredito,TipDcm,R.Documento AS NumDcmto,IdCiaDcm,VrAboCapital,LiqAboCapital,VrAboOtros,VrPagoCuotas,VrPagoAportes,VrPagoConvenio,VrAboCuotas,VrAboAportes,VrAboConvenio,VrAntPrestamo,VrAntAportes,VrAntConvenio
,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
--datos de forma de pago
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco
,P.NumForma AS PagNumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2
,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--Datos del cliente
,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,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinRecibos AS R  INNER JOIN Trn_Pagos AS P ON R.TipDoc=P.TipDoc AND R.Recibo=P.Documento AND R.IdCia=P.IdCia
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario   INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 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  TercCliePrestamo AS CLI ON R.IdCliente=CLI.IdClie AND R.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom  LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE R.TipDoc=@pmTipDoc  AND Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND R.IdCia=@pmIdCia
 ORDER BY  R.Recibo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFinCanAportes] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmValorTotal MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(2000),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinCanAportes (TipDoc,NumNota,IdCia,Fecha,IdCliente,IdAgencia,ValorTotal,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,FecDev,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmIdCliente,@pmIdAgencia,@pmValorTotal,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmAnulado,@pmFecDev,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFinCanAportes] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmValorTotal MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(2000),@pmAnulado BIT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE Trn_FinCanAportes SET Fecha=@pmFecha,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,ValorTotal=@pmValorTotal,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,Anulado=@pmAnulado,FecDev=@pmFecDev
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinCanAportes] @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,IdCliente,IdAgencia,ValorTotal,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,FecDev,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_FinCanAportes
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinCanAportesFmt] @pmTipDoc VARCHAR(3),@pmNumNotaIni INT,@pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc,C.NumNota,C.IdCia,Compania,C.Fecha,C.IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia,NomAgencia,C.ValorTotal,C.TipCom,TipoCom,C.Comprobante,C.IdCiaCom
,C.Observacion,C.Anulado,C.FecDev,C.FechaCrea,C.IdCiaCrea,C.OrigenAdd,C.IdUsuario,Usuario
--ABONOS
,A.Item AS ItemRec,A.Fecha AS FechaRec,A.TipDoc AS TipDocApo,A.IdPrestamo AS NumAporte,A.IdCiaPre AS IdCiaApo,A.ItemPre
,A.TotalAbono,A.VrBaseCms,A.IdVend AS CdVend,V.RazonSocial AS NomVendedor,A.Comision,A.CantGal,A.Detalle AS DetallePago,A.TipoAplica,A.FecPago
,CU.NumCuota,CU.Concepto,CU.Fecha AS FechaLiq,CU.FechaVence,CU.VrTotal,CU.VrAbonado,CU.TipoTasa,CU.DTF_EA,CU.TasaNom,CU.Spread_TA,CU.TasaEfe,CU.TipoCuota
,CU.Detalle AS DetalleCuota,CU.FecUltPago,CU.FecLiqMora
 FROM Trn_FinCanAportes AS C INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero 
 INNER JOIN TercCliePrestamo AS CLI ON C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 INNER JOIN Trn_FinAbonos AS A ON C.TipDoc=A.TipRec AND C.NumNota=A.Recibo AND C.IdCia=A.IdCia
 INNER JOIN Trn_FinCuotas AS CU ON A.TipDoc=CU.TipDoc AND A.IdPrestamo=CU.IdPrestamo AND A.IdCiaPre=CU.IdCia AND A.ItemPre=CU.Item
 INNER JOIN Terceros AS V ON A.IdVend=V.IdTercero 
 LEFT JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 WHERE C.TipDoc=@pmTipDoc AND C.NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreCartera_Apo] @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLinea VARCHAR(4)=Null
 AS 
 INSERT INTO tm_PreCartera (tmEst,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,VrSaldo,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,IdCliente,IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 SELECT @pmtmEst,C.TipDoc,C.IdPrestamo,C.IdCia,C.Item,C.NumCuota,C.Fecha,C.FechaVence,C.VrTotal,C.VrAbonado,C.VrTotal-C.VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,C.IdCliente,C.IdAgencia,'0',C.Concepto,TipoCuota,NumCausacion,IdCiaCau,C.Detalle,'0',FecUltPago,FecLiqMora,FechaCrea,'0','','','','0'
 FROM Trn_FinCuotas AS C 
 WHERE C.TipDoc IN ('APO','CVN') AND VrTotal>VrAbonado AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.CodLinCred LIKE ISNULL(@pmIdLinea,'%')
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInstm_PreCartera_Saf] @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_PreCartera (tmEst,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,VrSaldo,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,IdCliente,IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 SELECT @pmtmEst,'SAF',MAX(Recibo),IdCia,0,0,getdate(),getdate(),SUM(VrAFavor-VrOtrosDb),SUM(VrAbono),SUM((VrAFavor-VrOtrosDb)-VrAbono),'0',0,0,0,0
,IdCliente,IdAgencia,IdVend,ISNULL(Concepto,'SAF'),0,0,'00','SALDO A FAVOR','0',NULL,NULL,getdate(),'0','','','','0'
 FROM Trn_FinAfavor
 WHERE  (VrAFavor-VrOtrosDb)>VrAbono AND  IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')   AND IdVend LIKE ISNULL(@pmIdVend,'%')
 GROUP BY IdCia,IdCliente,IdAgencia,IdVend,Concepto
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinConveniosFmt] @pmTipLiq VARCHAR(3),@pmNumLiquidaIni INT,@pmNumLiquidaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipLiq,C.NumLiquida,C.IdCia,Compania,C.Fecha,C.IdConcepto,CN.Concepto,C.IdProveedor,T.RazonSocial,C.NumFactura,C.FecFactura,C.FecVence,C.ValorTotal
,C.TipCom,TipoCom,C.Comprobante,C.IdCiaCom,C.Observacion,C.Anulado,C.NumDev,C.FecDev,C.FechaCrea,C.IdCiaCrea,C.OrigenAdd,C.IdUsuario,Usuario
,CU.Item,CU.Fecha AS FecEmision,CU.FechaVence,CU.VrTotal,CU.VrAbonado,CU.IdCliente,TC.RazonSocial AS NomCliente,CU.IdAgencia AS CodAgencia,CL.NomAgencia,CU.Detalle,CU.FecUltPago
 FROM Trn_FinConvenios AS C INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto INNER JOIN Terceros AS T ON C.IdProveedor=T.IdTercero  
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Trn_FinCuotas AS CU ON C.TipLiq=CU.TipDoc AND C.NumLiquida=CU.IdPrestamo AND C.IdCia=CU.IdCia
 INNER JOIN Terceros AS TC ON CU.IdCliente=TC.IdTercero
 LEFT JOIN TercCliePrestamo AS CL ON CU.IdCliente=CL.IdClie AND CU.IdAgencia=CL.IdAgencia
 LEFT JOIN TiposCom AS TCO ON C.TipCom=TCO.IdCom
 WHERE C.TipLiq=@pmTipLiq AND C.NumLiquida BETWEEN @pmNumLiquidaIni AND @pmNumLiquidaFin AND C.IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinRecibosLta] @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,Fecha,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA
,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAboOtros,VrEfectivo,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco,CdCta,Referencia,pVehiculo,CdConductor,TipoCredito,VrPagoCuotas,VrPagoAportes,VrPagoConvenio,VrAboCuotas,VrAboAportes,VrAboConvenio,VrAntPrestamo,VrAntAportes,VrAntConvenio
,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,VrAboCapital,LiqAboCapital,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
 FROM Trn_FinRecibos AS R  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') 
 ORDER BY R.IdCia,Recibo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinAfavor] @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend,CodLinCred,Concepto
 FROM Trn_FinAfavor
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[paInsFinAfavor] @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmVrAFavor MONEY
,@pmVrOtrosDb MONEY,@pmVrAbono MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmCodLinCred VARCHAR(4),@pmConcepto VARCHAR(10)
 AS 
 INSERT INTO Trn_FinAfavor (TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend,CodLinCred,Concepto)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmVrAFavor,@pmVrOtrosDb,@pmVrAbono,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmCodLinCred,@pmConcepto)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFinAportes] @pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecLiquida SMALLDATETIME,@pmCodCia CHAR(2),@pmCodGrupo VARCHAR(4),@pmCodTipoTerc VARCHAR(4),@pmValorTotal MONEY,@pmBaseApo MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE Trn_FinAportes SET Fecha=@pmFecha,FecLiquida=@pmFecLiquida,CodCia=@pmCodCia,CodGrupo=@pmCodGrupo,CodTipoTerc=@pmCodTipoTerc,ValorTotal=@pmValorTotal,BaseApo=@pmBaseApo,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
 WHERE TipLiq=@pmTipLiq AND NumLiquida=@pmNumLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinAportes] @pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipLiq,NumLiquida,IdCia,Fecha,FecLiquida,CodCia,CodGrupo,CodTipoTerc,ValorTotal,BaseApo,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,Anulado,NumDev,FecDev,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_FinAportes
 WHERE TipLiq=@pmTipLiq AND NumLiquida=@pmNumLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFinAportes] @pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecLiquida SMALLDATETIME,@pmCodCia CHAR(2),@pmCodGrupo VARCHAR(4),@pmCodTipoTerc VARCHAR(4),@pmValorTotal MONEY,@pmBaseApo MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinAportes (TipLiq,NumLiquida,IdCia,Fecha,FecLiquida,CodCia,CodGrupo,CodTipoTerc,ValorTotal,BaseApo,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,Anulado,NumDev,FecDev,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipLiq,@pmNumLiquida,@pmIdCia,@pmFecha,@pmFecLiquida,@pmCodCia,@pmCodGrupo,@pmCodTipoTerc,@pmValorTotal,@pmBaseApo,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmAnulado,@pmNumDev,@pmFecDev,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFinConvenios] @pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProveedor VARCHAR(16),@pmNumFactura VARCHAR(15),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmValorTotal MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(2000),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmFechaCrea SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmOrigenAdd VARCHAR(10),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinConvenios (TipLiq,NumLiquida,IdCia,Fecha,IdConcepto,IdProveedor,NumFactura,FecFactura,FecVence,ValorTotal,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,NumDev,FecDev,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario)
 VALUES (@pmTipLiq,@pmNumLiquida,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdProveedor,@pmNumFactura,@pmFecFactura,@pmFecVence,@pmValorTotal,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmAnulado,@pmNumDev,@pmFecDev,@pmFechaCrea,@pmIdCiaCrea,@pmOrigenAdd,@pmIdUsuario)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFinConvenios] @pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProveedor VARCHAR(16),@pmNumFactura VARCHAR(15),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmValorTotal MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(2000),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE Trn_FinConvenios SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdProveedor=@pmIdProveedor,NumFactura=@pmNumFactura,FecFactura=@pmFecFactura,FecVence=@pmFecVence,ValorTotal=@pmValorTotal,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
 WHERE TipLiq=@pmTipLiq AND NumLiquida=@pmNumLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinConvenios] @pmTipLiq VARCHAR(3),@pmNumLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipLiq,NumLiquida,IdCia,Fecha,IdConcepto,IdProveedor,NumFactura,FecFactura,FecVence,ValorTotal,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,NumDev,FecDev,FechaCrea,IdCiaCrea,OrigenAdd,IdUsuario
 FROM Trn_FinConvenios
 WHERE TipLiq=@pmTipLiq AND NumLiquida=@pmNumLiquida AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paInsFinRecibos] @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY
,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrEfectivo MONEY,@pmVrAnticipo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4)
,@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoCredito VARCHAR(10),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2)
,@pmVrAboCapital MONEY,@pmLiqAboCapital INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrAboOtros MONEY,@pmVrPagoCuotas MONEY,@pmVrPagoAportes MONEY,@pmVrPagoConvenio MONEY,@pmVrAboCuotas MONEY,@pmVrAboAportes MONEY,@pmVrAboConvenio MONEY,@pmVrAntPrestamo MONEY,@pmVrAntAportes MONEY,@pmVrAntConvenio MONEY
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinRecibos (TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco,CdCta,Referencia,pVehiculo,CdConductor
,TipoCredito,TipDcm,Documento,IdCiaDcm,VrAboCapital,LiqAboCapital,VrAboOtros,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario,VrPagoCuotas,VrPagoAportes,VrPagoConvenio,VrAboCuotas,VrAboAportes,VrAboConvenio,VrAntPrestamo,VrAntAportes,VrAntConvenio)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFecPago,@pmVrRecibido,@pmVrSubTotal,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrOtros,@pmVrOtrDcto,@pmVrPagosMas,@pmVrNeto,@pmVrAplicado,@pmVrEfectivo,@pmVrAnticipo,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarRet,@pmCodTarIca
,@pmCodTarRiv,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmEnEfectivo,@pmCdForma,@pmNumForma,@pmDetallePago,@pmCdBanco,@pmCdCta,@pmReferencia,@pmpVehiculo,@pmCdConductor,@pmTipoCredito,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmVrAboCapital,@pmLiqAboCapital,@pmVrAboOtros,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario
,@pmVrPagoCuotas,@pmVrPagoAportes,@pmVrPagoConvenio,@pmVrAboCuotas,@pmVrAboAportes,@pmVrAboConvenio,@pmVrAntPrestamo,@pmVrAntAportes,@pmVrAntConvenio)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinRecibos] @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo
,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipoCredito,TipDcm,Documento,IdCiaDcm,VrAboCapital,LiqAboCapital,VrAboOtros,VrPagoCuotas,VrPagoAportes,VrPagoConvenio,VrAboCuotas,VrAboAportes,VrAboConvenio,VrAntPrestamo,VrAntAportes,VrAntConvenio
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinRecibos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paQryFinRecibosDoc]  @pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipDcm VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo
,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipoCredito,TipDcm,Documento,IdCiaDcm,VrAboCapital,LiqAboCapital,VrAboOtros,VrPagoCuotas,VrPagoAportes,VrPagoConvenio,VrAboCuotas,VrAboAportes,VrAboConvenio,VrAntPrestamo,VrAntAportes,VrAntConvenio
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinRecibos
 WHERE Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm AND TipDcm LIKE ISNULL(@pmTipDcm,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,Recibo
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[paUpFinRecibos] @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY
,@pmVrAplicado MONEY,@pmVrEfectivo MONEY,@pmVrAnticipo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4)
,@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoCredito VARCHAR(10),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmVrAboCapital MONEY,@pmLiqAboCapital INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmVrAboOtros MONEY,@pmVrPagoCuotas MONEY,@pmVrPagoAportes MONEY,@pmVrPagoConvenio MONEY,@pmVrAboCuotas MONEY,@pmVrAboAportes MONEY,@pmVrAboConvenio MONEY,@pmVrAntPrestamo MONEY,@pmVrAntAportes MONEY,@pmVrAntConvenio MONEY,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinRecibos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FecPago=@pmFecPago,VrRecibido=@pmVrRecibido,VrSubTotal=@pmVrSubTotal,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrOtros=@pmVrOtros,VrOtrDcto=@pmVrOtrDcto,VrPagosMas=@pmVrPagosMas,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,VrEfectivo=@pmVrEfectivo,VrAnticipo=@pmVrAnticipo
,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,EnEfectivo=@pmEnEfectivo,CdForma=@pmCdForma,NumForma=@pmNumForma,DetallePago=@pmDetallePago,CdBanco=@pmCdBanco,CdCta=@pmCdCta
,Referencia=@pmReferencia,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipoCredito=@pmTipoCredito,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,VrAboCapital=@pmVrAboCapital,LiqAboCapital=@pmLiqAboCapital
,VrAboOtros=@pmVrAboOtros,VrPagoCuotas=@pmVrPagoCuotas,VrPagoAportes=@pmVrPagoAportes,VrPagoConvenio=@pmVrPagoConvenio,VrAboCuotas=@pmVrAboCuotas,VrAboAportes=@pmVrAboAportes,VrAboConvenio=@pmVrAboConvenio,VrAntPrestamo=@pmVrAntPrestamo,VrAntAportes=@pmVrAntAportes,VrAntConvenio=@pmVrAntConvenio,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
