if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsDiferidos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsDiferidos]
GO

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].[paInsFinCauMora]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsFinCauMora]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinClieSaldosLin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsFinClieSaldosLin]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinCuotas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsFinCuotas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinFacturas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsFinFacturas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinPagos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsFinPagos]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFinPrestamo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsFinPrestamo]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsProdImpFron]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsProdImpFron]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsProdImptos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsProdImptos]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTercClieLineas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsTercClieLineas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTercCliePrestamo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsTercCliePrestamo]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_PreCuotas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_PreCuotas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_PreCuotas_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_PreCuotas_Sel]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_PreIntMora]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_PreIntMora]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_PreIntMora_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_PreIntMora_Sel]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_PresPagos_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_PresPagos_Sel]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAfAmortizaLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAfAmortizaLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAfAmortizaRes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAfAmortizaRes]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAfDifIngresoCr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAfDifIngresoCr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAfDifIngresoLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAfDifIngresoLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAfDifTrasladoCr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAfDifTrasladoCr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryAfDifTrasladoLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryAfDifTrasladoLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryClaseTarImp]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryClaseTarImp]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDiferidos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDiferidos]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDiferidosAmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDiferidosAmt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDiferidosHis]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDiferidosHis]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDiferidosLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDiferidosLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinAbonosFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinAbonosFmt]
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].[paQryFinCauMoraFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinCauMoraFmt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinCausacionFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinCausacionFmt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinClieSaldosLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinClieSaldosLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinCuotas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinCuotas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinCuotasFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinCuotasFmt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinCuotasLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinCuotasLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinDevPreCr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinDevPreCr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinDevPreFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinDevPreFmt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinFacturas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinFacturas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinPrestamo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinPrestamo]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinPrestamoCr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinPrestamoCr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinPrestamoFmt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinPrestamoFmt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinPrestamoLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinPrestamoLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFinPrestamoRes]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFinPrestamoRes]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdImpFron]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryProdImpFron]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdImpFronLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryProdImpFronLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdImpFronUlt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryProdImpFronUlt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdImptos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryProdImptos]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdImptosFec]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryProdImptosFec]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdImptosLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryProdImptosLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryProdImptosUlt]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryProdImptosUlt]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTercClieLineas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTercClieLineas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTercClieLineasLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTercClieLineasLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTercCliePrestamo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTercCliePrestamo]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTercCliePrestamoLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTercCliePrestamoLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_PreCuotasLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQrytm_PreCuotasLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_PreIntMoraLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQrytm_PreIntMoraLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpDiferidos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpDiferidos]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpFinClieSaldosLin]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpFinClieSaldosLin]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpFinPrestamo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpFinPrestamo]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpProdImpFron]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpProdImpFron]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpProdImptos]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpProdImptos]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTercClieLineas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpTercClieLineas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTercCliePrestamo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpTercCliePrestamo]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDiferidos @pmIdDiferido VARCHAR(30),@pmNomDiferido VARCHAR(250),@pmIdTipDif VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdDep VARCHAR(4)
,@pmIdRespons VARCHAR(16),@pmIdCia CHAR(2),@pmFecInicio SMALLDATETIME,@pmValorTotal MONEY,@pmVrAdiciones MONEY,@pmVrDisminucion MONEY,@pmNumPeriodos INT,@pmFecFinAmtza SMALLDATETIME
,@pmAjustable BIT,@pmContable BIT,@pmVrAjuInfAcum MONEY,@pmVrAmtzaAcuHis MONEY,@pmVrAmtzaAcum MONEY,@pmVrAjuInfAmtza MONEY,@pmCuotasAmtza INT,@pmVrCorrMonDifAcu MONEY
,@pmCorrMonDiferida MONEY,@pmNumMesesDif INT,@pmFecUltAmtza SMALLDATETIME,@pmCueEnGrupo BIT,@pmIdCuenta VARCHAR(16),@pmIdCueAmortiza VARCHAR(16),@pmIdCueAjuDif VARCHAR(16),@pmIdCueCorrDif VARCHAR(16)
,@pmIdCueAjuAmtza VARCHAR(16),@pmIdCueCorrAA VARCHAR(16),@pmNumDoc VARCHAR(20),@pmReferencia VARCHAR(50),@pmDesUbicacion VARCHAR(150),@pmIdEstado VARCHAR(4),@pmFecRetiro SMALLDATETIME
,@pmIdCausal VARCHAR(4),@pmComentRetiro VARCHAR(250),@pmComentarios VARCHAR(250),@pmTipoAmtza CHAR(1),@pmNitCliente VARCHAR(16),@pmAgenCliente VARCHAR(16),@pmCantGalones DECIMAL (14,4),@pmIdProv VARCHAR(16)
,@pmNumPlaca  VARCHAR(20),@pmCodInstala VARCHAR(4),@pmFecCreacion SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Diferidos (IdDiferido,NomDiferido,IdTipDif,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,IdCia,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum
,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,DesUbicacion,IdEstado,FecRetiro,IdCausal
,ComentRetiro,Comentarios,FecCreacion,IdUsuario,TipoAmtza,NitCliente,AgenCliente,CantGalones,IdProv,NumPlaca,CodInstala)
 VALUES (@pmIdDiferido,@pmNomDiferido,@pmIdTipDif,@pmIdGrupo,@pmIdCCosto,@pmIdSubCos,@pmIdDep,@pmIdRespons,@pmIdCia,@pmFecInicio,@pmValorTotal,@pmVrAdiciones,@pmVrDisminucion,@pmNumPeriodos
,@pmFecFinAmtza,@pmAjustable,@pmContable,@pmVrAjuInfAcum,@pmVrAmtzaAcuHis,@pmVrAmtzaAcum,@pmVrAjuInfAmtza,@pmCuotasAmtza,@pmVrCorrMonDifAcu,@pmCorrMonDiferida,@pmNumMesesDif
,@pmFecUltAmtza,@pmCueEnGrupo,@pmIdCuenta,@pmIdCueAmortiza,@pmIdCueAjuDif,@pmIdCueCorrDif,@pmIdCueAjuAmtza,@pmIdCueCorrAA,@pmNumDoc,@pmReferencia,@pmDesUbicacion,@pmIdEstado,@pmFecRetiro,@pmIdCausal
,@pmComentRetiro,@pmComentarios,@pmFecCreacion,@pmIdUsuario,@pmTipoAmtza,@pmNitCliente,@pmAgenCliente,@pmCantGalones,@pmIdProv,@pmNumPlaca,@pmCodInstala)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE 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)
 AS 
 INSERT INTO Trn_FinAfavor (TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend,CodLinCred)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmVrAFavor,@pmVrOtrosDb,@pmVrAbono,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmCodLinCred)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinCauMora @pmTipoProc VARCHAR(3),@pmNumProc INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaPre CHAR(2),@pmItemPre INT,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmFecVence SMALLDATETIME,@pmVrTotal MONEY,@pmVrAbonado MONEY,@pmFecUltLiq SMALLDATETIME,@pmFecIniLiq SMALLDATETIME,@pmFecFinLiq SMALLDATETIME
,@pmDiasLiquida INT,@pmDiasGracia INT,@pmBaseLiquida MONEY,@pmTasaEA DECIMAL(14,4),@pmTasaEM DECIMAL(14,4),@pmTasaDia DECIMAL(14,4),@pmVrLiquida MONEY,@pmTarifaIva DECIMAL(14,4),@pmVrIvaInt MONEY,@pmNumNotaDb INT,@pmNumComp INT
,@pmCdLinCred VARCHAR(4)
 AS 
 INSERT INTO Trn_FinCauMora (TipoProc,NumProc,IdCia,Item,TipDoc,IdPrestamo,IdCiaPre,ItemPre,IdCliente,IdAgencia,IdVend,FecVence,VrTotal,VrAbonado,FecUltLiq,FecIniLiq,FecFinLiq,DiasLiquida,DiasGracia,BaseLiquida,TasaEA,TasaEM,TasaDia,VrLiquida,TarifaIva,VrIvaInt,NumNotaDb,NumComp,CdLinCred)
 VALUES (@pmTipoProc,@pmNumProc,@pmIdCia,@pmItem,@pmTipDoc,@pmIdPrestamo,@pmIdCiaPre,@pmItemPre,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmFecVence,@pmVrTotal,@pmVrAbonado,@pmFecUltLiq,@pmFecIniLiq,@pmFecFinLiq
,@pmDiasLiquida,@pmDiasGracia,@pmBaseLiquida,@pmTasaEA,@pmTasaEM,@pmTasaDia,@pmVrLiquida,@pmTarifaIva,@pmVrIvaInt,@pmNumNotaDb,@pmNumComp,@pmCdLinCred)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinClieSaldosLin @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4)
,@pmIdCia CHAR(2),@pmVrSaldoCap MONEY,@pmVrSaldoInt MONEY,@pmVrSaldoOtro MONEY
 AS 
 INSERT INTO FinClieSaldosLin (IdClie,CdAgencia,IdLinea,IdCia,VrSaldoCap,VrSaldoInt,VrSaldoOtro)
 VALUES (@pmIdClie,@pmCdAgencia,@pmIdLinea,@pmIdCia,@pmVrSaldoCap,@pmVrSaldoInt,@pmVrSaldoOtro)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinCuotas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumCuota INT,@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrTotal MONEY,@pmVrAbonado MONEY
,@pmTipoTasa VARCHAR(10),@pmDTF_EA DECIMAL(14,4),@pmTasaNom DECIMAL(14,4),@pmSpread_TA DECIMAL(14,4),@pmTasaEfe DECIMAL(14,4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmTipoCuota INT
,@pmNumCausacion INT,@pmIdCiaCau CHAR(2),@pmDetalle VARCHAR(250),@pmFecUltPago SMALLDATETIME,@pmConcepto VARCHAR(10),@pmFecLiqMora SMALLDATETIME,@pmFechaCrea SMALLDATETIME,@pmCodLinCred VARCHAR(4)
,@pmNumFactura VARCHAR(20),@pmVrInteresFact MONEY,@pmDiasLiquida INT
 AS 
 INSERT INTO Trn_FinCuotas (TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente,IdAgencia,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,FechaCrea,CodLinCred,NumFactura,VrInteresFact,DiasLiquida)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumCuota,@pmFecha,@pmFechaVence,@pmVrTotal,@pmVrAbonado,@pmTipoTasa,@pmDTF_EA,@pmTasaNom,@pmSpread_TA,@pmTasaEfe,@pmIdCliente
,@pmIdAgencia,@pmConcepto,@pmTipoCuota,@pmNumCausacion,@pmIdCiaCau,@pmDetalle,@pmFecUltPago,@pmFecLiqMora,@pmFechaCrea,@pmCodLinCred,@pmNumFactura,@pmVrInteresFact,@pmDiasLiquida)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinFacturas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumFactura VARCHAR(20),@pmFecha SMALLDATETIME
,@pmFechaVence SMALLDATETIME,@pmVrFactura MONEY,@pmVrInteres MONEY,@pmDiasLiq DECIMAL(14,4),@pmTasaEfeMes DECIMAL(14,4),@pmTasaEfeDia DECIMAL(14,4),@pmFechaPago SMALLDATETIME,@pmItemPago INT
 AS 
 INSERT INTO Trn_FinFacturas (TipDoc,IdPrestamo,IdCia,Item,NumFactura,Fecha,FechaVence,VrFactura,VrInteres,DiasLiq,TasaEfeMes,TasaEfeDia,FechaPago,ItemPago)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumFactura,@pmFecha,@pmFechaVence,@pmVrFactura,@pmVrInteres,@pmDiasLiq,@pmTasaEfeMes,@pmTasaEfeDia,@pmFechaPago,@pmItemPago)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinPagos @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumero VARCHAR(20),@pmFecha SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmVrCuota MONEY
,@pmEdoForma INT,@pmCdBanco VARCHAR(4),@pmCtaCheque VARCHAR(30),@pmDetalle VARCHAR(150),@pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCiaRec CHAR(2)
 AS 
 INSERT INTO Trn_FinPagos (TipDoc,IdPrestamo,IdCia,Item,Numero,Fecha,FecPago,VrCuota,EdoForma,CdBanco,CtaCheque,Detalle,TipRec,Recibo,IdCiaRec)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumero,@pmFecha,@pmFecPago,@pmVrCuota,@pmEdoForma,@pmCdBanco,@pmCtaCheque,@pmDetalle,@pmTipRec,@pmRecibo,@pmIdCiaRec)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPrestamo SMALLDATETIME,@pmFecCorte SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdTasa VARCHAR(4),@pmNPlazos INT,@pmCausacion VARCHAR(10),@pmIdMora VARCHAR(4),@pmNContrato INT,@pmCdCiaCon CHAR(2)
,@pmNActaJunta INT,@pmNumCredito VARCHAR(20),@pmIdVend VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoGarantia VARCHAR(10),@pmVrGarantia MONEY,@pmDescGarantia VARCHAR(250),@pmCxPagar BIT,@pmNumCuotaIni INT,@pmAcumCapital MONEY,@pmAcumIntereses MONEY,@pmAcumIntMora MONEY,@pmAcumCuotas INT,@pmPagosCapital MONEY,@pmPagosIntereses MONEY,@pmPagosIntMora MONEY,@pmCuotasPagadas INT
,@pmCuotasMora INT,@pmTipoAprob VARCHAR(20),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmDiaCausac INT,@pmDiaCausac2 INT,@pmDiasVence VARCHAR(50),@pmIdLinea VARCHAR(4),@pmTipoPlazo CHAR(1),@pmTipoCausac INT,@pmCdTasa2 VARCHAR(4),@pmVrSeguro MONEY,@pmVrIntereses MONEY,@pmVrNeto MONEY,@pmVrCuota MONEY
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinPrestamo (TipDoc,IdPrestamo,IdCia,Fecha,FecPrestamo,FecCorte,DiaCausac,DiaCausac2,DiasVence,IdConcepto,IdCliente,IdAgencia,VrPrestamo,IdLinea,IdTasa,NPlazos,TipoPlazo,Causacion,IdMora,NContrato,CdCiaCon,NActaJunta,NumCredito,IdVend,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora
,TipoAprob,TipoCausac,CdTasa2,VrSeguro,VrIntereses,VrNeto,VrCuota,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmFecha,@pmFecPrestamo,@pmFecCorte,@pmDiaCausac,@pmDiaCausac2,@pmDiasVence,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmVrPrestamo,@pmIdLinea,@pmIdTasa,@pmNPlazos,@pmTipoPlazo,@pmCausacion,@pmIdMora,@pmNContrato,@pmCdCiaCon,@pmNActaJunta,@pmNumCredito,@pmIdVend,@pmpVehiculo,@pmCdConductor,@pmTipoGarantia,@pmVrGarantia,@pmDescGarantia
,@pmCxPagar,@pmNumCuotaIni,@pmAcumCapital,@pmAcumIntereses,@pmAcumIntMora,@pmAcumCuotas,@pmPagosCapital,@pmPagosIntereses,@pmPagosIntMora,@pmCuotasPagadas,@pmCuotasMora,@pmTipoAprob,@pmTipoCausac
,@pmCdTasa2,@pmVrSeguro,@pmVrIntereses,@pmVrNeto,@pmVrCuota,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsProdImpFron @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmIdLocal VARCHAR(8),@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 INSERT INTO ProdImpFron (IdProducto,IdClase,Item,IdLocal,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe)
 VALUES (@pmIdProducto,@pmIdClase,@pmItem,@pmIdLocal,@pmTarifa,@pmSimbTfa,@pmCdTarifa,@pmRngFecha,@pmFecInicial
,@pmFecFinal,@pmLtaPre,@pmInactivo,@pmFecUpdate,@pmTarifZfe)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdImptos @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 INSERT INTO ProdImptos (IdProducto,IdClase,Item,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe)
 VALUES (@pmIdProducto,@pmIdClase,@pmItem,@pmTarifa,@pmSimbTfa,@pmCdTarifa,@pmRngFecha,@pmFecInicial,@pmFecFinal,@pmLtaPre,@pmInactivo,@pmFecUpdate,@pmTarifZfe)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTercClieLineas @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmIdTasa VARCHAR(4),@pmnPlazo INT,@pmTipoPlazo CHAR(1),@pmCupoCredito MONEY,@pmSaldoCapital MONEY,@pmSaldoInteres MONEY,@pmMaxCupoSmlv DECIMAL(14,4),@pmTipoCausac VARCHAR(10),@pmDiasVence VARCHAR(20)
,@pmFecPreAprob SMALLDATETIME,@pmPlazoAprob INT,@pmEtdoAprob INT,@pmTipoAprob VARCHAR(20),@pmDescAprob VARCHAR(250),@pmGarantias BIT,@pmTipoGarantia VARCHAR(10),@pmDescGarantia VARCHAR(250),@pmVrGarantia MONEY,@pmReqCheques BIT,@pmFecActCupo SMALLDATETIME,@pmEstadoAct INT
,@pmLiqSeguro BIT, @pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercClieLineas (IdClie,IdAgencia,IdLinea,IdTasa,nPlazo,TipoPlazo,CupoCredito,SaldoCapital,SaldoInteres,MaxCupoSmlv,TipoCausac,DiasVence,LiqSeguro,FecPreAprob,PlazoAprob,EtdoAprob,TipoAprob,DescAprob,Garantias,TipoGarantia,DescGarantia,VrGarantia,ReqCheques,FecActCupo,EstadoAct,FechaCrea,IdUsuario)
 VALUES (@pmIdClie,@pmIdAgencia,@pmIdLinea,@pmIdTasa,@pmnPlazo,@pmTipoPlazo,@pmCupoCredito,@pmSaldoCapital,@pmSaldoInteres,@pmMaxCupoSmlv,@pmTipoCausac,@pmDiasVence,@pmLiqSeguro,@pmFecPreAprob,@pmPlazoAprob,@pmEtdoAprob,@pmTipoAprob,@pmDescAprob,@pmGarantias,@pmTipoGarantia,@pmDescGarantia,@pmVrGarantia,@pmReqCheques,@pmFecActCupo,@pmEstadoAct,@pmFechaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTercCliePrestamo @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100)
,@pmCargContac VARCHAR(50),@pmIdSzona VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdTipoTerc VARCHAR(4),@pmIdVend VARCHAR(16),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmEsExento BIT,@pmCalcIntMora BIT,@pmDiasGracia INT
,@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmVrSalBasico MONEY,@pmMaxDcto DECIMAL(14,4),@pmMatMerc VARCHAR(20),@pmFecMat SMALLDATETIME
,@pmNomAgencia VARCHAR(150),@pmDirAgencia VARCHAR(250),@pmIdLocalAge VARCHAR(8),@pmComentarios VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTipoSalario CHAR(1),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercCliePrestamo (IdClie,IdAgencia,IdSzona,IdGrupo,IdTipoTerc,IdVend,IdClase,NumCuenta,IdBanco,NomAgencia,DirAgencia,IdLocalAge,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,EsExento,CalcIntMora,DiasGracia,FecIngreso,FecVigencia,FecRetiro,VrSalBasico
,MaxDcto,TipoSalario,MatMerc,FecMat,Comentarios,IdEstado,Inactivo,FechaCrea,IdUsuario)
 VALUES (@pmIdClie,@pmIdAgencia,@pmIdSzona,@pmIdGrupo,@pmIdTipoTerc,@pmIdVend,@pmIdClase,@pmNumCuenta,@pmIdBanco,@pmNomAgencia,@pmDirAgencia,@pmIdLocalAge,@pmNitRepLeg,@pmNomRepLeg,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargContac,@pmEsExento
,@pmCalcIntMora,@pmDiasGracia,@pmFecIngreso,@pmFecVigencia,@pmFecRetiro,@pmVrSalBasico,@pmMaxDcto,@pmTipoSalario,@pmMatMerc,@pmFecMat,@pmComentarios,@pmIdEstado,@pmInactivo,@pmFechaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInstm_PreCuotas @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipDoc VARCHAR(3),@pmtmPrestamo INT,@pmtmIdCiaPre CHAR(2),@pmtmItemPre INT,@pmtmCuota INT,@pmtmConcepto VARCHAR(10)
,@pmtmFecCausa SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmVrTotal MONEY,@pmtmTipoTasa VARCHAR(10),@pmtmDTF_EA DECIMAL(14,4),@pmtmTasaNom DECIMAL(14,4),@pmtmSpread DECIMAL(14,4)
,@pmtmTasaEfe DECIMAL(14,4),@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmEstado INT,@pmtmDetalle VARCHAR(250),@pmtmTotAbono MONEY,@pmtmLinCred VARCHAR(4)
,@pmtmFactura VARCHAR(20),@pmtmDiasLiq INT,@pmtmVrIntCorr MONEY
 AS 
 INSERT INTO tm_PreCuotas (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCiaPre,tmItemPre,tmCuota,tmConcepto,tmFecCausa,tmFecVence,tmVrTotal,tmTipoTasa,tmDTF_EA,tmTasaNom,tmSpread,tmTasaEfe,tmIdCliente
,tmIdAgencia,tmIdVend,tmEstado,tmDetalle,tmTotAbono,tmLinCred,tmFactura,tmDiasLiq,tmVrIntCorr)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmPrestamo,@pmtmIdCiaPre,@pmtmItemPre,@pmtmCuota,@pmtmConcepto,@pmtmFecCausa,@pmtmFecVence,@pmtmVrTotal,@pmtmTipoTasa,@pmtmDTF_EA,@pmtmTasaNom
,@pmtmSpread,@pmtmTasaEfe,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmEstado,@pmtmDetalle,@pmtmTotAbono,@pmtmLinCred,@pmtmFactura,@pmtmDiasLiq,@pmtmVrIntCorr)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreCuotas_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 INSERT INTO tm_PreCuotas (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCiaPre,tmItemPre,tmCuota,tmConcepto,tmFecCausa,tmFecVence,tmVrTotal,tmTipoTasa
,tmDTF_EA,tmTasaNom,tmSpread,tmTasaEfe,tmIdCliente,tmIdAgencia,tmIdVend,tmEstado,tmDetalle,tmTotAbono,tmLinCred,tmFactura,tmDiasLiq,tmVrIntCorr)
SELECT @pmtmNumero,Item,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Concepto,Fecha,FechaVence,VrTotal,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente
,IdAgencia,'0',1,Detalle,VrAbonado,CodLinCred,NumFactura,DiasLiquida,VrInteresFact
 FROM Trn_FinCuotas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreIntMora @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipDoc VARCHAR(3),@pmtmPrestamo INT,@pmtmIdCia CHAR(2),@pmtmItemPre INT,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16)
,@pmtmFecVence SMALLDATETIME,@pmtmVrTotal MONEY,@pmtmVrAbonado MONEY,@pmtmFecUltLiq SMALLDATETIME,@pmtmFecIniLiq SMALLDATETIME,@pmtmFecFinLiq SMALLDATETIME,@pmtmDiasLiq INT,@pmtmDiasGracia INT,@pmtmBaseLiq MONEY
,@pmtmTasaEA DECIMAL(14,4),@pmtmTasaEM DECIMAL(14,4),@pmtmTasaDia DECIMAL(14,4),@pmtmVrLiquida MONEY,@pmtmTarifaIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmEstado INT,@pmtmDetalle VARCHAR(250),@pmtmLinCred VARCHAR(4)
 AS 
 INSERT INTO tm_PreIntMora (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCia,tmItemPre,tmIdCliente,tmIdAgencia,tmIdVend,tmFecVence,tmVrTotal,tmVrAbonado,tmFecUltLiq,tmFecIniLiq,tmFecFinLiq,tmDiasLiq,tmDiasGracia,tmBaseLiq,tmTasaEA,tmTasaEM,tmTasaDia
,tmVrLiquida,tmTarifaIva,tmVrIva,tmEstado,tmDetalle,tmLinCred)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmPrestamo,@pmtmIdCia,@pmtmItemPre,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmFecVence,@pmtmVrTotal,@pmtmVrAbonado,@pmtmFecUltLiq,@pmtmFecIniLiq,@pmtmFecFinLiq
,@pmtmDiasLiq,@pmtmDiasGracia,@pmtmBaseLiq,@pmtmTasaEA,@pmtmTasaEM,@pmtmTasaDia,@pmtmVrLiquida,@pmtmTarifaIva,@pmtmVrIva,@pmtmEstado,@pmtmDetalle,@pmtmLinCred)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreIntMora_Sel @pmtmNumero VARCHAR(5),@pmTipoProc VARCHAR(3),@pmNumProc INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_PreIntMora (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCia,tmItemPre,tmIdCliente,tmIdAgencia,tmIdVend,tmFecVence,tmVrTotal,tmVrAbonado
,tmFecUltLiq,tmFecIniLiq,tmFecFinLiq,tmDiasLiq,tmDiasGracia,tmBaseLiq,tmTasaEA,tmTasaEM,tmTasaDia,tmVrLiquida,tmTarifaIva,tmVrIva,tmEstado,tmDetalle,tmLinCred)
 SELECT @pmtmNumero,Item,TipDoc,IdPrestamo,IdCiaPre,ItemPre,IdCliente,IdAgencia,IdVend,FecVence,VrTotal,VrAbonado,FecUltLiq,FecIniLiq,FecFinLiq,DiasLiquida
,DiasGracia,BaseLiquida,TasaEA,TasaEM,TasaDia,VrLiquida,TarifaIva,VrIvaInt,1,'',CdLinCred
 FROM Trn_FinCauMora 
 WHERE TipoProc=@pmTipoProc AND  NumProc=@pmNumProc AND  IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PresPagos_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 INSERT INTO tm_PresPagos (tmNumero,tmItem,tmNumForma,tmFecha,tmFecPago,tmVrCuota,tmCdBanco,tmCtaCheque,tmDetalle)
 SELECT @pmtmNumero,Item, Numero, Fecha, FecPago, VrCuota,  CdBanco, CtaCheque, Detalle
 FROM Trn_FinPagos
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfAmortizaLta  @pmNumeroIni INT,@pmNumeroFin INT
 AS 
 SELECT AMZ.Numero AS NumRegistro,Fecha,AMZ.Estado AS EstadoDoc,AMZ.IdUsuario AS CdUsuario,Usuario,FechaCrea,AMZ.IdCia AS CdCia,CI.Compania AS NomCia
,Item,D.IdDiferido AS CdDiferido,NomDiferido, D.ValorTotal AS ValorInicial, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminucion, VrAcuAjustes, VrAmortizaHis, VrAmortizaMes, VrAjustesInf
, VrAjustesAmtza,VrAmtzaHisAcum, VrAmortizaAcum, VrAjuAmtzaAcum, VrCorrMonAcum, VrBaseCorrMon, VrCorrMonDif, TarifaAju
,D.IdCia AS DetIdCia,CID.Compania AS DetNomCia,CdCCosto,CCosto,CdSubCos,SubCosto, CdDep, Dependencia,NitRespons,RazonSocial,FecInicioAmtza, NPeriodos
--datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS DifCdCentro,A.IdSubCos AS DifCdSubcentro
,A.IdDep AS DifCdDep,DesUbicacion,IdRespons,A.IdCia AS DifIdCia,FecInicio,A.ValorTotal AS DifValTotal,A.VrAdiciones AS DifValAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,ED.Estado AS EstadoDif
,Comentarios,A.FecCreacion AS DifFecCrea,VentaGal,CuotaGal,VrInversion,NumPlaca,CodInstala,Instlacion
 FROM Trn_AfAmortiza AS AMZ INNER JOIN Trn_AfAmtDetalle AS D ON AMZ.Numero=D.Numero
 INNER JOIN Companias AS CI ON AMZ.IdCia=CI.IdCia  INNER JOIN adm_Usuarios AS U ON AMZ.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido  INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif  INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN Companias AS CID ON D.IdCia=CID.IdCia 
 INNER JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto 
 INNER JOIN Dependencias AS DP ON D.CdDep=DP.IdDep INNER JOIN Terceros AS T ON D.NitRespons=T.IdTercero
 LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos  LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE  AMZ.Numero BETWEEN @pmNumeroIni  AND @pmNumeroFin
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfAmortizaRes @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdLinea VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdCia CHAR(2)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null
,@pmIdDiferido VARCHAR(30)
 AS 
 SELECT D.IdDiferido AS CdDiferido,NomDiferido,AMZ.Numero AS NumRegistro,Fecha,AMZ.Estado AS EstadoDoc,AMZ.IdUsuario AS CdUsuario,Usuario,FechaCrea,AMZ.IdCia AS CdCia,CI.Compania AS NomCia
,Item,D.ValorTotal AS ValorInicial, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminucion, VrAcuAjustes, VrAmortizaHis, VrAmortizaMes, VrAjustesInf
, VrAjustesAmtza,VrAmtzaHisAcum, VrAmortizaAcum, VrAjuAmtzaAcum, VrCorrMonAcum, VrBaseCorrMon, VrCorrMonDif, TarifaAju
,D.IdCia AS DetIdCia,CID.Compania AS DetNomCia,CdCCosto,CCD.CCosto AS NomCentroCosto,CdSubCos,SCD.SubCosto AS NomSubcentro, CdDep, DPD.Dependencia AS NomDepend
,NitRespons,TRD.RazonSocial AS DetResponsable,FecInicioAmtza, NPeriodos,VentaGal,CuotaGal,VrInversion
--datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro
,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro,A.IdDep AS CdDep,DP.Dependencia AS DifDependencia,DesUbicacion
,IdRespons,T.RazonSocial AS NomResponsable,A.IdCia AS CdCia,CIA.Compania AS DifNomCia,FecInicio,A.ValorTotal AS DifValTotal,A.VrAdiciones AS DifAdiciones,A.VrDisminucion AS DifDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,ED.Estado AS DifEstado
,FecRetiro,A.IdCausal AS CdCausal,Causal,ComentRetiro,Comentarios,NumPlaca,CodInstala,Instlacion,FecCreacion,A.IdUsuario AS IdUsuari
--DATOS DEL DIFERIDO
 FROM Trn_AfAmtDetalle AS D INNER JOIN Trn_AfAmortiza AS  AMZ  ON D.Numero=AMZ.Numero  INNER JOIN Companias AS CI ON AMZ.IdCia=CI.IdCia
 INNER JOIN adm_Usuarios AS U ON AMZ.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos  AS A ON D.IdDiferido=A.IdDiferido  INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN Companias AS CIA ON A.IdCia=CIA.IdCia 
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado   INNER JOIN Companias AS CID ON D.IdCia=CID.IdCia 
 INNER JOIN CentroCosto AS CCD ON D.CdCCosto=CCD.IdCCosto 
 INNER JOIN Dependencias AS DPD ON D.CdDep=DPD.IdDep INNER JOIN Terceros AS TRD ON D.NitRespons=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN SubCentros AS SCD ON D.CdSubCos=SCD.IdSubCos   LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE Fecha BETWEEN  @pmFechaIni  AND @pmFechaFin AND AMZ.Estado<>0 AND D.IdDiferido LIKE ISNULL(@pmIdDiferido ,'%')
 AND AMZ.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND  G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')  
 AND D.CdDep LIKE ISNULL(@pmIdDep,'%')  AND D.NitRespons LIKE ISNULL(@pmIdRespons,'%')  AND D.CdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY AMZ.Numero,NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifIngresoCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2),@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDiferido VARCHAR(30)=Null
AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
, Item,D.IdDiferido AS CdDiferido,NomDiferido,FecActivacion, D.NumPeriodos AS NumMeses, VrBase, VrMejoras, D.VrDisminucion AS ValDisminuc, D.VrAjuInfAcum AS ValAjuInfAcum, D.VrAmortizaHis AS ValAmzaHis
, VrAmortizaAju,D.VrAjuInfAmtza AS ValAjuInflacAmza, NumMesesAmtza, VrCorrMonDif, NumMesesCorr, D.FecUltAmtza AS FecUltAmortza, Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,RazonSocial,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,NumPlaca,CodInstala,Instlacion,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifIngreso AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos   LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND D.IdDiferido LIKE ISNULL(@pmIdDiferido,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY M.Numero,NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifIngresoLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT
,@pmIdCia CHAR(2)
AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
, Item,D.IdDiferido AS CdDiferido,NomDiferido,FecActivacion, D.NumPeriodos AS NumMeses, VrBase, VrMejoras, D.VrDisminucion AS ValDisminuc, D.VrAjuInfAcum AS ValAjuInfAcum, D.VrAmortizaHis AS ValAmzaHis
, VrAmortizaAju,D.VrAjuInfAmtza AS ValAjuInflacAmza, NumMesesAmtza, VrCorrMonDif, NumMesesCorr, D.FecUltAmtza AS FecUltAmortza, Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,RazonSocial,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,NumPlaca,CodInstala,Instlacion,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifIngreso AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos  LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni  AND @pmNumeroFin  AND M.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifTrasladoCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2),@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDiferido VARCHAR(30)=Null
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumTraslado,M.IdCia AS CdCia,CI.Compania AS NomCia,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,D.IdGrupo AS CdGrupoOri,GOR.Grupo AS GrupoOrigen,D.IdCCosto AS CdCentroOrig,CCO.CCosto AS CentroOrigen,D.IdSubCos AS CdSubCosOrig,SCO.SubCosto AS SubCentroOrig
,D.IdDep AS CdDepOrig,DPO.Dependencia AS DepOrigen,D.IdRespons AS IdResponsOrig,TRO.RazonSocial AS ResponsOrigen,Ubicacion,CdCiaDest,CND.Compania  AS CiaDestino,CdGrupoDest,GDT.Grupo AS NomGrupoDestino,CdCCostoDest,CCD.CCosto AS CentroDestino
,CdSubCosDest,SCD.SubCosto AS SubCenDestino,CdDepDest,DPD.Dependencia AS DepDestino,CdResponsDest,TRD.RazonSocial AS NomRespDestino,UbicacionDest,Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,L.Linea AS LineaDif,A.IdGrupo AS CodGrupo,G.Grupo AS DifGrupo,A.IdCCosto AS CdCentro,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro
,A.IdDep AS CdDep,DP.Dependencia AS DependDif,DesUbicacion,A.IdRespons AS DifNitRespons,T.RazonSocial AS NombreRespons,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado,NumPlaca,CodInstala,Instlacion
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifTraslado AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN CentroCosto AS CCO ON D.IdCCosto=CCO.IdCCosto  INNER JOIN Dependencias AS DPO ON D.IdDep=DPO.IdDep INNER JOIN Terceros AS TRO ON D.IdRespons=TRO.IdTercero
 INNER JOIN AfGrupos AS  GOR ON D.IdGrupo=GOR.IdGrupo INNER JOIN AfLineas AS LOR ON GOR.IdLinea=LOR.IdLinea
 LEFT JOIN   Companias AS CND ON D.CdCiaDest=CND.IdCia LEFT JOIN AfGrupos AS GDT ON D.CdGrupoDest=GDT.IdGrupo
 LEFT JOIN CentroCosto AS CCD ON D.CdCCostoDest=CCD.IdCCosto 
 LEFT JOIN Dependencias AS DPD ON D.CdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON D.CdResponsDest=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN SubCentros AS SCO ON D.IdSubCos=SCO.IdSubCos
 LEFT JOIN SubCentros AS SCD ON D.CdSubCosDest=SCD.IdSubCos LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND D.IdDiferido LIKE ISNULL(@pmIdDiferido,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY M.Numero,NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifTrasladoLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumTraslado,M.IdCia AS CdCia,CI.Compania AS NomCia,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,D.IdGrupo AS CdGrupoOri,GOR.Grupo AS GrupoOrigen,D.IdCCosto AS CdCentroOrig,CCO.CCosto AS CentroOrigen,D.IdSubCos AS CdSubCosOrig,SCO.SubCosto AS SubCentroOrig
,D.IdDep AS CdDepOrig,DPO.Dependencia AS DepOrigen,D.IdRespons AS IdResponsOrig,TRO.RazonSocial AS ResponsOrigen,Ubicacion,CdCiaDest,CND.Compania  AS CiaDestino,CdGrupoDest,GDT.Grupo AS NomGrupoDestino,CdCCostoDest,CCD.CCosto AS CentroDestino
,CdSubCosDest,SCD.SubCosto AS SubCenDestino,CdDepDest,DPD.Dependencia AS DepDestino,CdResponsDest,TRD.RazonSocial AS NomRespDestino,UbicacionDest,Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,L.Linea AS LineaDif,A.IdGrupo AS CodGrupo,G.Grupo AS DifGrupo,A.IdCCosto AS CdCentro,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro
,A.IdDep AS CdDep,DP.Dependencia AS DependDif,DesUbicacion,A.IdRespons AS DifNitRespons,T.RazonSocial AS NombreRespons,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,NumPlaca,CodInstala,Instlacion,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifTraslado AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN CentroCosto AS CCO ON D.IdCCosto=CCO.IdCCosto  INNER JOIN Dependencias AS DPO ON D.IdDep=DPO.IdDep INNER JOIN Terceros AS TRO ON D.IdRespons=TRO.IdTercero
 INNER JOIN AfGrupos AS  GOR ON D.IdGrupo=GOR.IdGrupo INNER JOIN AfLineas AS LOR ON GOR.IdLinea=LOR.IdLinea
 LEFT JOIN   Companias AS CND ON D.CdCiaDest=CND.IdCia LEFT JOIN AfGrupos AS GDT ON D.CdGrupoDest=GDT.IdGrupo
 LEFT JOIN CentroCosto AS CCD ON D.CdCCostoDest=CCD.IdCCosto 
 LEFT JOIN Dependencias AS DPD ON D.CdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON D.CdResponsDest=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN SubCentros AS SCO ON D.IdSubCos=SCO.IdSubCos
 LEFT JOIN SubCentros AS SCD ON D.CdSubCosDest=SCD.IdSubCos LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin  AND M.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseTarImp
 AS 
  SELECT IdClase,NomClase
  FROM ClaseTar
  WHERE Inactivo=0 AND IdClase IN ('DEP','GLO','ICA','IVA','MUN','NAC','RET','RIV','SOB','SOL','MGM','PRE','TSN','TSD','TSM','SUB','MAR','COS','IVI','IGP')
  ORDER BY NomClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidos @pmIdDiferido VARCHAR(30)
 AS 
 SELECT IdDiferido,NomDiferido,IdTipDif,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,IdCia,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,DesUbicacion,IdEstado
,FecRetiro,IdCausal,ComentRetiro,Comentarios,FecCreacion,IdUsuario,TipoAmtza,NitCliente,AgenCliente,CantGalones,IdProv
,NumPlaca,CodInstala
 FROM Diferidos
 WHERE IdDiferido=@pmIdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidosAmt @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT IdDiferido,NomDiferido,IdTipDif,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,IdCia,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,CorrMonDiferida,NumMesesDif,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA
,IdEstado,FecRetiro,TipoAmtza,NitCliente,AgenCliente,CantGalones,IdProv,NumPlaca,CodInstala
 FROM Diferidos
 WHERE FecInicio<=@pmFechaFin
  AND (IdEstado<>'9999'  OR (IdEstado='9999' AND FecRetiro>=@pmFechaIni))
 ORDER BY IdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidosHis @pmIdDiferido VARCHAR(30)
 AS 
 SELECT A.IdDiferido AS CdDiferido,NomDiferido,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro
,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro,A.IdDep AS CdDep,DP.Dependencia AS DifDependencia,DesUbicacion
,IdRespons,T.RazonSocial AS NomResponsable,A.IdCia AS CdCia,CIA.Compania AS DifNomCia,FecInicio,A.ValorTotal AS DifValTotal,A.VrAdiciones AS DifAdiciones,A.VrDisminucion AS DifDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,ED.Estado AS DifEstado
,FecRetiro,A.IdCausal AS CdCausal,Causal,ComentRetiro,Comentarios,FecCreacion,A.IdUsuario AS IdUsuari
--DATOS DEL DIFERIDO
,AMZ.Numero AS NumRegistro,Fecha,AMZ.Estado AS EstadoDoc,AMZ.IdUsuario AS CdUsuario,Usuario,FechaCrea,AMZ.IdCia AS CdCia,CI.Compania AS NomCia
,Item,D.ValorTotal AS ValorInicial, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminucion, VrAcuAjustes, VrAmortizaHis, VrAmortizaMes, VrAjustesInf
, VrAjustesAmtza,VrAmtzaHisAcum, VrAmortizaAcum, VrAjuAmtzaAcum, VrCorrMonAcum, VrBaseCorrMon, VrCorrMonDif, TarifaAju
,D.IdCia AS DetIdCia,CID.Compania AS DetNomCia,CdCCosto,CCD.CCosto AS NomCentroCosto,CdSubCos,SCD.SubCosto AS NomSubcentro, CdDep, DPD.Dependencia AS NomDepend
,NitRespons,TRD.RazonSocial AS DetResponsable,FecInicioAmtza, NPeriodos,TipoAmtza,NitCliente,AgenCliente,CantGalones
,IdProv,PV.RazonSocial AS NomProveedor,NumPlaca,CodInstala,Instlacion
 FROM Diferidos  AS A INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN Companias AS CIA ON A.IdCia=CIA.IdCia  INNER JOIN Terceros AS PV ON A.IdProv=PV.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN Trn_AfAmtDetalle AS D ON A.IdDiferido=D.IdDiferido LEFT  JOIN Trn_AfAmortiza AS AMZ  ON D.Numero=AMZ.Numero
 LEFT JOIN Companias AS CI ON AMZ.IdCia=CI.IdCia  LEFT JOIN adm_Usuarios AS U ON AMZ.IdUsuario=U.IdUsuario
 LEFT JOIN Companias AS CID ON D.IdCia=CID.IdCia 
 LEFT JOIN CentroCosto AS CCD ON D.CdCCosto=CCD.IdCCosto 
 LEFT JOIN Dependencias AS DPD ON D.CdDep=DPD.IdDep LEFT JOIN Terceros AS TRD ON D.NitRespons=TRD.IdTercero
 LEFT JOIN SubCentros AS SCD ON D.CdSubCos=SCD.IdSubCos  LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE A.IdDiferido LIKE ISNULL(@pmIdDiferido ,'%')
 ORDER BY NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidosLta @pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdTipDif VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdDiferido,NomDiferido,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,T.RazonSocial  AS NomResponsable,A.IdCia AS CdCia,Compania,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumPlaca,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado
,FecRetiro,A.IdCausal AS CdCausal,Causal,ComentRetiro,Comentarios,FecCreacion,A.IdUsuario AS IdUsuari,Usuario,TipoAmtza,NitCliente,AgenCliente,CantGalones
,IdProv,PV.RazonSocial AS NomProveedor,CodInstala,Instlacion
--datos del responsable
,T.Codigo AS TercCodigo,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigla,T.Direccion AS TercDirecc,T.IdLocal AS CdLocalidad,Localidad
,LC.IdDep AS Cd_Depto,Departamento,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM Diferidos  AS A INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS PV ON A.IdProv=PV.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal INNER JOIN Departamentos AS D ON LC.IdDep=D.IdDep
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND A.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND  A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 AND A.IdDep LIKE ISNULL(@pmIdDep,'%') AND IdRespons LIKE ISNULL(@pmIdRespons,'%')
 AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (FecInicio>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecInicio<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinAbonosFmt @pmTipRec VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2) 
 AS 
 SELECT TipRec,Recibo,A.IdCia AS CdCia,Compania,A.Item AS ItemRec,A.Fecha AS FechaRec,A.TipDoc AS Tip_Doc,A.IdPrestamo AS NumPtmo,IdCiaPre,ItemPre
,TotalAbono,VrBaseCms,A.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,A.IdAgencia AS CdAgencia,NomAgencia AS Agencia,A.IdVend AS CdVend,V.RazonSocial AS NomVendedor,Comision
,CantGal, A.Detalle AS DetallePago,TipoAplica,FecPago,TimeSys,TipoDoc
--Datos de la cuota
,NumCuota,Concepto,C.Fecha AS FechaCausa,FechaVence,VrTotal,VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,TipoCuota
,NumCausacion,IdCiaCau,C.Detalle AS DetalleCuota,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_Crea
 FROM  Trn_FinAbonos AS A INNER JOIN Trn_FinCuotas AS C ON A.TipDoc=C.TipDoc AND A.IdPrestamo=C.IdPrestamo AND A.IdCiaPre=C.IdCia AND A.ItemPre=C.Item
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS T ON A.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON A.IdVend=V.IdTercero 
 LEFT JOIN TercCliePrestamo AS CLI ON  A.IdCliente=CLI.IdClie AND A.IdAgencia=CLI.IdAgencia
 LEFT JOIN Sys_TiposDoc AS TD ON A.TipRec=TD.IdDoc
 WHERE A.TipRec=@pmTipRec AND A.Recibo BETWEEN @pmReciboIni AND  @pmReciboFin AND A.IdCia=@pmIdCia
 ORDER BY A.Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend,CodLinCred
 FROM Trn_FinAfavor
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryFinCauMoraFmt @pmTipoProc VARCHAR(3),@pmNumProcIni INT,@pmNumProcFin INT,@pmIdCia CHAR(2)
AS
 SELECT  CM.NumProc AS NumCausac,CM.IdCia AS CdCia,Compania,Fecha,Item,TipDoc, IdPrestamo, IdCiaPre, ItemPre, IdCliente,T.RazonSocial AS NomCliente,CM.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,CM.IdVend AS CdVend,V.RazonSocial AS NomVendedor,FecVence,VrTotal, VrAbonado, FecUltLiq,FecIniLiq, FecFinLiq, DiasLiquida, CM.DiasGracia AS Dias_Gracia
, BaseLiquida, TasaEA, TasaEM, TasaDia, VrLiquida, TarifaIva, VrIvaInt, NumNotaDb, NumComp,C.Observacion AS Observ,CdLinCred,C.Anulado AS Anuldo,C.IdUsuario AS CdUsuario,Usuario
 FROM  Trn_FinCauMora  AS CM INNER JOIN Trn_FinCausacion AS C ON CM.TipoProc=C.TipoProc AND CM.NumProc=C.NumProc  AND CM.IdCia=C.IdCia
 INNER JOIN Companias AS CI ON CM.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON  C.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS T ON CM.IdCliente=T.IdTercero   INNER JOIN Terceros AS V ON CM.IdVend=V.IdTercero
 LEFT JOIN TercCliePrestamo AS CLI ON CM.IdCliente=CLI.IdClie AND CM.IdAgencia=CLI.IdAgencia
 WHERE CM.TipoProc=@pmTipoProc  AND  CM.NumProc BETWEEN @pmNumProcIni AND @pmNumProcFin AND CM.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinCausacionFmt @pmTipoProc VARCHAR(3),@pmNumProcIni INT,@pmNumProcFin INT,@pmIdCia CHAR(2) 
 AS 
 SELECT  NumProc,FC.IdCia AS CdCia,Compania,FC.Fecha AS FecCausac,FC.TipCom AS TipComp,FC.Comprobante AS NumComp,FC.Anulado AS Anuldo
 ,FC.Observacion AS CauObserv,C.TipDoc AS Tip_Doc,C.IdPrestamo AS NumPstmo,Item,NumCuota,FechaVence
,VrTotal,VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,C.IdCliente AS NitCliente,RazonSocial,C.IdAgencia AS  CdAgencia,NomAgencia AS Agencia
,TipoCuota,Concepto,Detalle,FecUltPago,FecLiqMora,C.FechaCrea AS CuoFecCrea,FC.TimeSys AS CauFechaCrea,FC.IdUsuario AS CdUsuario,Usuario
--datos del prestamo
,FecPrestamo,FecCorte,NPlazos,Causacion,VrPrestamo,NumCredito,P.Observacion AS Observ
 FROM  Trn_FinCausacion AS FC INNER JOIN Trn_FinCuotas AS C ON FC.NumProc=C.NumCausacion AND FC.IdCia=C.IdCiaCau
 INNER JOIN Companias AS CI ON FC.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON FC.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero   
 INNER JOIN Trn_FinPrestamo AS P ON C.TipDoc=P.TipDoc AND C.IdPrestamo=P.IdPrestamo AND C.IdCia=P.IdCia
 LEFT JOIN TercCliePrestamo AS CLI ON  C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 WHERE TipoProc=@pmTipoProc AND  NumProc BETWEEN @pmNumProcIni AND  @pmNumProcFin AND FC.IdCia=@pmIdCia
 AND C.TipDoc='PR1'

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinClieSaldosLta @pmIdClie VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS
 SELECT  F.IdClie AS NitClie,RazonSocial,CdAgencia,NomAgencia AS  Agencia,F.IdCia AS CdCia,Compania,F.VrSaldo AS SaldoActual
 FROM  FinClieSaldos AS F INNER JOIN  Terceros AS T ON F.IdClie=T.IdTercero
 INNER JOIN TercCliePrestamo AS CLI ON F.IdClie=CLI.IdClie AND F.CdAgencia=CLI.IdAgencia
 INNER JOIN Companias AS CI ON F.IdCia=CI.IdCia
  WHERE F.IdClie LIKE ISNULL(@pmIdClie,'%') AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY RazonSocial,F.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinCuotas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 SELECT TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado
,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente,IdAgencia,TipoCuota,Concepto
,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,FechaCrea,CodLinCred,NumFactura,VrInteresFact,DiasLiquida
 FROM Trn_FinCuotas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinCuotasFmt @pmTipDoc VARCHAR(3),@pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2) 
 AS 
 SELECT  IdPrestamo,C.IdCia AS CdCia,Compania,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado
,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente,RazonSocial,C.IdAgencia AS  CdAgencia,NomAgencia AS Agencia,TipoCuota,Concepto
,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_crea,CodLinCred,NumFactura,VrInteresFact,DiasLiquida,TipDoc
 FROM Trn_FinCuotas AS C INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero  
 LEFT JOIN TercCliePrestamo AS CLI ON  C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 WHERE TipDoc=@pmTipDoc AND IdPrestamo BETWEEN @pmIdPrestamoIni AND  @pmIdPrestamoFin AND C.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinCuotasLta @pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT C.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,NomAgencia AS Agencia,C.TipDoc AS CdTipDoc,TipoDoc,C.IdPrestamo AS NumDcmto,C.IdCia AS CdCia,Compania
,Item,VrTotal,VrAbonado,VrTotal-VrAbonado AS ValorSaldo,C.Fecha AS FechaEmision,FechaVence,DATEDIFF(day,FechaVence,@pmFecActual) AS DiasMora
,Concepto,NumCuota,Detalle,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,TipoCuota,NumCausacion,IdCiaCau,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_Crea,CodLinCred,NumFactura,VrInteresFact,DiasLiquida
--Datos del prestamo
,FecPrestamo,VrPrestamo,NPlazos,Causacion,P.NContrato AS NumContrato,CdCiaCon,NActaJunta,NumCredito,ISNULL(P.IdVend,CLI.IdVend) AS CdVend,ISNULL(VN.RazonSocial,VNC.RazonSocial) AS Vendedor,P.IdLinea AS CdLinea,LinCredito
--Datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.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,Comentarios
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinCuotas AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 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 TercCliePrestamo AS CLI ON  C.IdCliente=CLI.IdClie AND C.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 Terceros AS VNC ON CLI.IdVend=VNC.IdTercero
 INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc  
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Trn_FinPrestamo AS P ON C.TipDoc=P.TipDoc AND C.IdPrestamo=P.IdPrestamo AND C.IdCia=P.IdCia
 LEFT JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE  (VrTotal-VrAbonado)>@pmSaldo AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevPreCr @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipDocPre,D.IdPrestamo AS NumPrestamo,IdCiaDoc,FecDoc,FecPrestamo,FecCorte
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,NomAgencia AS Agencia,D.VrPrestamo AS ValorDev,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,NumCuotaIni,AcumCapital,AcumCuotas
,NPlazos,TipoPlazo,Causacion,NActaJunta,NumCredito,P.NContrato AS NumContrato,CdCiaCon,D.CxPagar AS Cx_pagar,ModdDev,D.TipCom AS Tip_Comp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp
,DiaCausac ,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.OrigenAdd AS Origen_Crea,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaAct,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--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,DP.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,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge  AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinDevPre AS D INNER JOIN Trn_FinPrestamo AS P ON D.TipDoc=P.TipDoc AND  D.IdPrestamo=P.IdPrestamo  AND D.IdCiaDoc=P.IdCia
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
 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  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.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 TC ON D.TipCom=TC.IdCom 
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE TipDev=@pmTipDev   AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY  D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevPreFmt @pmTipDev VARCHAR(3), @pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipDocPre,D.IdPrestamo AS NumPrestamo,IdCiaDoc,FecDoc,FecPrestamo,FecCorte
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,NomAgencia AS Agencia,D.VrPrestamo AS ValorDev,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int
,TasaEfectiva,P.IdMora AS CdMora,DescMora,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,NPlazos,TipoPlazo,Causacion,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta
,DiaCausac,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito
,NumCredito,D.CxPagar AS Cx_pagar,ModdDev,D.TipCom AS Tip_Comp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.OrigenAdd AS Origen_Crea,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaAct,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--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,DP.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,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM  Trn_FinDevPre AS D INNER JOIN Trn_FinPrestamo AS P ON D.TipDoc=P.TipDoc AND  D.IdPrestamo=P.IdPrestamo  AND D.IdCiaDoc=P.IdCia
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero
 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 Terceros AS VN ON D.IdVend=VN.IdTercero INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.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 TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE D.TipDev=@pmTipDev  AND  Devolucion  BETWEEN @pmDevolucionIni AND @pmDevolucionFin  AND D.IdCia=@pmIdCia
 ORDER BY Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinFacturas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,IdPrestamo,IdCia,Item,NumFactura,Fecha,FechaVence,VrFactura,VrInteres,DiasLiq,TasaEfeMes,TasaEfeDia,FechaPago,ItemPago
 FROM Trn_FinFacturas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,IdPrestamo,IdCia,Fecha,FecPrestamo,FecCorte,IdConcepto,IdCliente,IdAgencia,VrPrestamo,IdTasa,NPlazos,Causacion,IdMora,NContrato,CdCiaCon,NActaJunta,NumCredito
,IdVend,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora
,CuotasPagadas,CuotasMora,TipoAprob,TipoCausac,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,DiaCausac ,DiaCausac2,DiasVence,IdLinea,TipoPlazo
,CdTasa2,VrSeguro,VrIntereses,VrNeto,VrCuota,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinPrestamo
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoCr @pmTipDoc VARCHAR(3), @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdTasa VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdPrestamo,P.IdCia AS CdCia,Compania,Fecha,FecPrestamo,FecCorte,P.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,P.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,VrPrestamo,VrSeguro,VrIntereses,VrNeto,VrCuota,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int,TasaEfectiva,TipoPeriodo,CausaInteres,DTF_EA,Spread_TA,OpcCompra,NPlazos,Causacion
,P.IdMora AS CdMora,DescMora,TIM.TipoInteres AS TipoIntMora,TasaEA,TasaEM,TIM.DiasGracia AS Dias_Gracia,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta,NumCredito,P.IdVend AS CdVend,VN.RazonSocial AS NomVendedor
,pVehiculo,CdConductor,CD.RazonSocial AS NomConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,P.Observacion AS Observ,DiaCausac ,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito,TipoPlazo,CdTasa2
,P.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario,TipDoc
--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,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado  INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 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 P.IdCliente=CLI.IdClie AND P.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 TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON P.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Terceros AS CD ON P.CdConductor=CD.IdTercero  
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE TipDoc=@pmTipDoc  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdVend LIKE ISNULL(@pmIdVend,'%') AND P.IdTasa LIKE ISNULL(@pmIdTasa,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY P.IdCia,IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoFmt @pmTipDoc VARCHAR(3), @pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT P.IdPrestamo AS NumPtmo,P.IdCia AS CdCia,Compania,P.Fecha AS FechaPtmo,FecPrestamo,FecCorte,P.IdConcepto AS CdConcepto,C.Concepto AS DescConcept,P.IdCliente AS NitCliente
,T.RazonSocial AS NomCliente,P.IdAgencia AS CdAgencia,NomAgencia AS Agencia,VrPrestamo,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int,TasaEfectiva,NPlazos,TipoPlazo,Causacion,P.IdMora AS CdMora,DescMora
,TIM.TipoInteres AS Tipo_IntMora,TasaEM,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta,NumCredito
,P.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,CdTasa2,VrSeguro,VrIntereses,VrNeto,VrCuota,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,P.Observacion AS Observ,DiaCausac ,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito
,P.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario
--Datos del detalle
,Item,NumCuota,PC.Fecha AS FechaCausa,FechaVence,VrTotal,VrAbonado,TipoTasa,PC.DTF_EA AS TasaDTF,TasaNom,PC.Spread_TA AS Puntos,TasaEfe,TipoCuota,PC.Concepto AS ConceptoCuota
,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,PC.FechaCrea AS Fec_Crea
--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,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero
 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 Terceros AS VN ON P.IdVend=VN.IdTercero INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN  TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.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 LineasCred AS LC ON P.IdLinea=LC.IdLinea
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON P.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Trn_FinCuotas AS PC ON P.TipDoc=PC.TipDoc AND P.IdPrestamo=PC.IdPrestamo AND P.IdCia=PC.IdCia
 WHERE P.TipDoc=@pmTipDoc  AND P.IdPrestamo BETWEEN @pmIdPrestamoIni AND @pmIdPrestamoFin  AND P.IdCia=@pmIdCia
 ORDER BY  P.IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoLta @pmTipDoc VARCHAR(3), @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdTasa VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdPrestamo,P.IdCia AS CdCia,Compania,Fecha,FecPrestamo,FecCorte,DiaCausac ,DiaCausac2,DiasVence,P.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrPrestamo,P.IdTasa AS CdTasa,DescTasa
,NPlazos,TipoPlazo,Causacion,IdLinea,IdMora,VrSeguro,VrIntereses,VrNeto,VrCuota,NContrato,CdCiaCon,TipoAprob,TipoCausac,NActaJunta,NumCredito,IdVend,VN.RazonSocial AS NomVendedor,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia
,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora
,CuotasPagadas,CuotasMora,CdTasa2,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 WHERE TipDoc=@pmTipDoc  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdVend LIKE ISNULL(@pmIdVend,'%') AND P.IdTasa LIKE ISNULL(@pmIdTasa,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY P.IdCia,IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinPrestamoRes @pmSaldo DECIMAL(12,2),@pmTipDoc VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdTasa VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT T.RazonSocial AS NomCliente,IdCliente,NomAgencia AS Agencia,P.IdAgencia AS CdAgencia,P.IdPrestamo AS NumPrest,P.IdCia AS CdCia,Compania
,NumCredito,P.Fecha AS FecRadica,FecPrestamo,DiaCausac,DiaCausac2,DiasVence,TipoAprob,NActaJunta,VrPrestamo,VrSeguro,VrIntereses,VrNeto,VrCuota,NPlazos,TipoPlazo,Causacion,P.IdLinea AS CdLinea,LinCredito,P.IdTasa AS CdTasa,DescTasa
,tmVrCuota AS VrCuotaDTF,tmDescCuota AS DescripCuota,tmVrMora AS VrEnMora,AcumCapital,AcumIntereses,VrPrestamo-AcumCapital AS SaldoCapital,tmDiasMora AS PromDiasMora
,CausaMora,GestionClie,AcuerdosClie,TipoGarantia,DescGarantia,VrGarantia,NumCuotaIni,AcumCuotas,CuotasMora
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CdTasa2,P.IdVend AS CdVend,VN.RazonSocial AS NomAsesor,P.NContrato AS NumContrato,CdCiaCon,P.Observacion AS Observ
,P.IdConcepto AS CdConcepto,Concepto,P.IdEstado AS CdEstado,Estado
,TI.TipoInteres AS Tipo_Int,TasaEfectiva,TipoPeriodo,CausaInteres,DTF_EA,Spread_TA,OpcCompra
--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,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad
,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto   INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado 
 INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 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 P.IdCliente=CLI.IdClie AND P.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 Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Trn_FinObserv AS PD ON P.TipDoc=PD.TipDoc AND P.IdPrestamo=PD.IdPrestamo AND P.IdCia=PD.IdCia
 LEFT JOIN tm_PreMora AS PM ON P.TipDoc=PM.tmTipDoc AND P.IdPrestamo=PM.tmIdPrestamo AND P.IdCia=PM.tmIdCia
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE P.TipDoc=@pmTipDoc  AND P.Anulado=0   AND (VrPrestamo-AcumCapital)>=@pmSaldo
 AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND P.IdVend LIKE ISNULL(@pmIdVend,'%') 
 AND P.IdTasa LIKE ISNULL(@pmIdTasa,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY P.IdCia,P.IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryProdImpFron @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT
 AS 
 SELECT IdProducto,IdClase,Item,IdLocal,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe
 FROM ProdImpFron
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImpFronLta @pmIdProducto VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null
 AS 
 SELECT PIM.IdProducto AS CdProducto,DescripProd,PIM.IdClase AS CdClase,NomClase,Item,PIM.IdLocal AS CdLocal,Localidad,Tarifa,TarifZfe,SimbTfa,CdTarifa
,RngFecha,FecInicial,FecFinal,LtaPre,PIM.Inactivo AS Inactvo,FecUpdate
 FROM ProdImpFron AS PIM  INNER JOIN ProdMcias AS P ON PIM.IdProducto=P.IdProducto
 INNER JOIN ClaseTar AS CT ON PIM.IdClase=CT.IdClase
 INNER JOIN Localidades AS L ON PIM.IdLocal=L.IdLocal
 WHERE PIM.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PIM.IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY  DescripProd,PIM.IdClase,PIM.IdLocal,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImpFronUlt @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
,@pmIdLocal VARCHAR(8)
 AS 
 SELECT IdProducto,IdClase,Item,IdLocal,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,TarifZfe
 FROM ProdImpFron
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase  AND IdLocal=@pmIdLocal
 AND Inactivo=0
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptos @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT
 AS 
 SELECT IdProducto,IdClase,Item,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe
 FROM ProdImptos
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptosFec @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
,@pmFecha SMALLDATETIME
 AS 
 SELECT IdProducto,IdClase,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,TarifZfe
 FROM ProdImptos
 WHERE  Inactivo=0 AND IdProducto=@pmIdProducto AND IdClase=@pmIdClase
 AND (RngFecha=0 OR (RngFecha<>0 AND @pmFecha BETWEEN FecInicial AND FecFinal))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptosLta @pmIdProducto VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null
,@pmInactivo BIT=Null
 AS 
 SELECT PIM.IdProducto AS CodProd,DescripProd,PIM.IdClase AS CdClase,NomClase,Item,Tarifa,TarifZfe,SimbTfa,CdTarifa,RngFecha
,FecInicial,FecFinal,LtaPre,PIM.Inactivo AS Inactiv,FecUpdate
 FROM ProdImptos AS PIM  INNER JOIN ProdMcias AS P ON PIM.IdProducto=P.IdProducto
 INNER JOIN ClaseTar AS CT ON PIM.IdClase=CT.IdClase
 WHERE PIM.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PIM.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND (PIM.Inactivo=ISNULL(@pmInactivo,0) or PIM.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY  DescripProd,PIM.IdClase,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptosUlt @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdProducto,IdClase,Item,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,TarifZfe
 FROM ProdImptos
 WHERE  Inactivo=0 AND IdProducto=@pmIdProducto AND IdClase LIKE ISNULL(@pmIdClase ,'%')
 ORDER BY IdClase,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTercClieLineas @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4)
 AS 
 SELECT IdClie,IdAgencia,IdLinea,IdTasa,nPlazo,TipoPlazo,CupoCredito,SaldoCapital,SaldoInteres,MaxCupoSmlv,TipoCausac,DiasVence,LiqSeguro,FecPreAprob
,PlazoAprob,EtdoAprob,TipoAprob,DescAprob,Garantias,TipoGarantia,DescGarantia,VrGarantia,ReqCheques,FecActCupo,EstadoAct,FechaCrea,FechaAct,IdUsuario
 FROM TercClieLineas
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieLineasLta @pmIdClie VARCHAR(16)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipoTerc  VARCHAR(4)=Null
 AS 
 SELECT LC.IdClie AS NitCliente,RazonSocial,LC.IdAgencia AS CdAgencia,NomAgencia,LC.IdLinea AS CdLinea,LinCredito,LC.IdTasa AS CdTasa,DescTasa,TasaEfectiva,nPlazo,TipoPlazo,CupoCredito
,SaldoCapital,SaldoInteres,MaxCupoSmlv,TipoCausac,DiasVence,FecPreAprob,PlazoAprob,EtdoAprob,TipoAprob,DescAprob,Garantias,TipoGarantia,DescGarantia,VrGarantia
,ReqCheques,FecActCupo,EstadoAct,LiqSeguro,LC.FechaCrea AS FecCrea,LC.FechaAct AS FecActualiza,LC.IdUsuario AS CdUsuario,Usuario
--datos del cliente
,TipoId,Dv,C.IdTipoTerc AS CdTipoTerc,TipoTercero,C.IdEstado AS CdEstado,Estado,FecIngreso,VrSalBasico,MaxDcto,Comentarios
 FROM TercClieLineas AS LC  INNER JOIN LineasCred AS L ON LC.IdLinea=L.IdLinea
 INNER JOIN  TercCliePrestamo AS C ON LC.IdClie=C.IdClie AND LC.IdAgencia=C.IdAgencia  INNER JOIN Terceros AS T ON LC.IdClie=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON LC.IdUsuario=U.IdUsuario
 INNER JOIN TiposTerc AS TC ON C.IdTipoTerc=TC.IdTipoTerc  INNER JOIN EstadoTer AS EC ON C.IdEstado=EC.IdEstado
 LEFT JOIN TasasInteres AS TI ON LC.IdTasa=TI.IdTasa
 WHERE C.Inactivo=0 AND LC.IdClie LIKE ISNULL(@pmIdClie,'%') AND LC.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND  C.IdTipoTerc  LIKE ISNULL(@pmIdTipoTerc ,'%')
 ORDER BY RazonSocial,LC.IdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTercCliePrestamo @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16)
 AS 
 SELECT IdClie,IdAgencia,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,NomAgencia,DirAgencia,IdLocalAge,IdSzona,IdGrupo,IdTipoTerc,IdVend,IdClase,NumCuenta,IdBanco
,EsExento,CalcIntMora,DiasGracia,FecIngreso,FecVigencia,FecRetiro,VrSalBasico,MaxDcto,TipoSalario,MatMerc,FecMat,Comentarios,IdEstado,Inactivo,FechaCrea,FechaAct,IdUsuario
 FROM TercCliePrestamo
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCliePrestamoLta  @pmIdGrupo VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdSzona VARCHAR(4)=Null
,@pmIdTipoTerc  VARCHAR(4)=Null,@pmFecIngIni SMALLDATETIME=Null,@pmFecIngFin SMALLDATETIME=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdClie,T.RazonSocial AS Razon_Social,T.TipoId AS TipId,T.Dv AS DvNit,T.NomCial AS NombreCmcial,T.SiglaRaz AS SiglaRazSocial
,IdAgencia,NomAgencia,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,T.Direccion AS TercDireccion,T.IdLocal AS CdCiudad,L.Localidad AS Ciudad,L.IdDep AS CdDpto
,D.Departamento AS Dpto,T.Telefono AS Tlefono,T.Fax AS NumFax,T.TelMovil AS TelCeluar,T.SitioWeb AS WebSite,T.e_mail AS Email
,DirAgencia,IdLocalAge,LE.Localidad AS CiuAgencia,DE.Departamento AS DptoAgencia,C.IdTipoTerc AS CdTipoTerc,TipoTercero
,SZ.IdZona AS CdZona,Zona,C.IdSzona AS CdSubzona,Subzona,C.IdGrupo AS CdGrupo,GrupoClie,T.IdSector AS CdSector,SectorEco,T.IdProf AS CdProf,Profesion,T.IdRegimen AS CdRgmen,Regimen
,T.TipEnte AS TipoEnte,IdVend,TV.RazonSocial AS NomVendedor,C.IdClase AS CdClase,ClaseCuenta,NumCuenta,C.IdBanco AS CdBanco,Banco
,EsExento,CalcIntMora,DiasGracia,FecIngreso,FecVigencia,FecRetiro,VrSalBasico,TipoSalario,MaxDcto,MatMerc,FecMat,Comentarios,C.IdEstado AS CdEstado,EC.Estado AS EstadoClie,C.Inactivo AS ClieInactivo,FechaCrea,FechaAct,C.IdUsuario AS CdUsuario,Usuario
,T.IniStgNom AS PosNomb,T.IdLugarCed AS IdLugCed,LC.Localidad AS LugarCed,T.FecExpCed AS FecExpCedula,T.Inactivo AS TercInactivo,T.Codigo AS TercCodigo
 FROM TercCliePrestamo AS C  INNER JOIN Terceros AS T ON C.IdClie=T.IdTercero  INNER JOIN Terceros AS TV ON C.IdVend=TV.IdTercero
 INNER JOIN GruposCli AS G ON C.IdGrupo=G.IdGrupo INNER JOIN TiposTerc AS TC ON C.IdTipoTerc=TC.IdTipoTerc
 INNER JOIN Subzonas AS SZ ON C.IdSzona=SZ.IdSzona  INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 INNER JOIN EstadoTer AS EC ON C.IdEstado=EC.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LE ON C.IdLocalAge=LE.IdLocal INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN  SectoresEco AS S ON T.IdSector=S.IdSector  INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen
 LEFT JOIN ClaseCta AS CTA ON  C.IdClase=CTA.IdClase LEFT JOIN Bancos AS B ON C.IdBanco=B.IdBanco 
 LEFT JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal  LEFT JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 WHERE C.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND  C.IdSzona LIKE ISNULL(@pmIdSzona,'%')  AND  C.IdTipoTerc  LIKE ISNULL(@pmIdTipoTerc ,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (FecIngreso>=ISNULL(@pmFecIngIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQrytm_PreCuotasLta @pmtmNumero VARCHAR(5)
AS
SELECT  tmItem, tmTipDoc, tmPrestamo, tmIdCiaPre, tmItemPre, tmCuota, tmConcepto, tmFecCausa, tmFecVence, tmVrTotal
,tmTipoTasa, tmDTF_EA,tmTasaNom, tmSpread, tmTasaEfe, tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,Agencia
,tmIdVend,V.RazonSocial AS NomVendedor,tmEstado, tmDetalle,tmLinCred,tmFactura,tmDiasLiq,tmVrIntCorr, tmTotAbono
--datos del prestamo
,FecPrestamo,VrPrestamo,NPlazos,Causacion,P.NContrato AS NumContrato,CdCiaCon,NumCuotaIni,P.IdEstado AS CdEstado
 FROM   tm_PreCuotas AS PC  INNER JOIN Terceros AS T ON PC.tmIdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON PC.tmIdVend=V.IdTercero
 INNER JOIN Trn_FinPrestamo AS P ON PC.tmTipDoc=P.TipDoc AND PC.tmPrestamo=P.IdPrestamo AND PC.tmIdCiaPre=P.IdCia
 LEFT JOIN Agencias  AS A ON PC.tmIdAgencia=A.IdAgencia
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQrytm_PreIntMoraLta @pmtmNumero VARCHAR(5)
AS
 SELECT      tmItem, tmTipDoc, tmPrestamo, tmIdCia, tmItemPre, tmIdCliente,T.RazonSocial AS NomCliente, tmIdAgencia,Agencia,tmIdVend,V.RazonSocial AS NomVendedor
, tmFecVence, tmVrTotal, tmVrAbonado, tmFecUltLiq,tmFecIniLiq, tmFecFinLiq, tmDiasLiq, tmDiasGracia, tmBaseLiq
,tmTasaEA, tmTasaEM, tmTasaDia, tmVrLiquida, tmTarifaIva, tmVrIva, tmEstado,tmDetalle,tmLinCred
 FROM  tm_PreIntMora AS IM INNER JOIN Terceros AS T ON IM.tmIdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON IM.tmIdVend=V.IdTercero
 LEFT JOIN Agencias  AS A ON IM.tmIdAgencia=A.IdAgencia
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDiferidos @pmIdDiferido VARCHAR(30),@pmNomDiferido VARCHAR(250),@pmIdTipDif VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdDep VARCHAR(4),@pmIdRespons VARCHAR(16),@pmIdCia CHAR(2),@pmFecInicio SMALLDATETIME,@pmValorTotal MONEY,@pmVrAdiciones MONEY,@pmVrDisminucion MONEY,@pmNumPeriodos INT
,@pmFecFinAmtza SMALLDATETIME,@pmAjustable BIT,@pmContable BIT,@pmVrAjuInfAcum MONEY,@pmVrAmtzaAcuHis MONEY,@pmVrAmtzaAcum MONEY,@pmVrAjuInfAmtza MONEY,@pmCuotasAmtza INT
,@pmVrCorrMonDifAcu MONEY,@pmCorrMonDiferida MONEY,@pmNumMesesDif INT,@pmFecUltAmtza SMALLDATETIME,@pmCueEnGrupo BIT,@pmIdCuenta VARCHAR(16),@pmIdCueAmortiza VARCHAR(16)
,@pmIdCueAjuDif VARCHAR(16),@pmIdCueCorrDif VARCHAR(16),@pmIdCueAjuAmtza VARCHAR(16),@pmIdCueCorrAA VARCHAR(16),@pmNumDoc VARCHAR(20),@pmReferencia VARCHAR(50)
,@pmDesUbicacion VARCHAR(150),@pmIdEstado VARCHAR(4),@pmFecRetiro SMALLDATETIME,@pmIdCausal VARCHAR(4),@pmComentRetiro VARCHAR(250),@pmComentarios VARCHAR(250)
,@pmTipoAmtza CHAR(1),@pmNitCliente VARCHAR(16),@pmAgenCliente VARCHAR(16),@pmCantGalones DECIMAL (14,4),@pmIdProv VARCHAR(16),@pmNumPlaca  VARCHAR(20),@pmCodInstala VARCHAR(4)
 AS 
 UPDATE Diferidos SET NomDiferido=@pmNomDiferido,IdTipDif=@pmIdTipDif,IdGrupo=@pmIdGrupo,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdDep=@pmIdDep
,IdRespons=@pmIdRespons,IdCia=@pmIdCia,FecInicio=@pmFecInicio,ValorTotal=@pmValorTotal,VrAdiciones=@pmVrAdiciones,VrDisminucion=@pmVrDisminucion
,NumPeriodos=@pmNumPeriodos,FecFinAmtza=@pmFecFinAmtza,Ajustable=@pmAjustable,Contable=@pmContable,VrAjuInfAcum=@pmVrAjuInfAcum,VrAmtzaAcuHis=@pmVrAmtzaAcuHis
,VrAmtzaAcum=@pmVrAmtzaAcum,VrAjuInfAmtza=@pmVrAjuInfAmtza,CuotasAmtza=@pmCuotasAmtza,VrCorrMonDifAcu=@pmVrCorrMonDifAcu,CorrMonDiferida=@pmCorrMonDiferida,NumMesesDif=@pmNumMesesDif
,FecUltAmtza=@pmFecUltAmtza,CueEnGrupo=@pmCueEnGrupo,IdCuenta=@pmIdCuenta,IdCueAmortiza=@pmIdCueAmortiza,IdCueAjuDif=@pmIdCueAjuDif,IdCueCorrDif=@pmIdCueCorrDif,IdCueAjuAmtza=@pmIdCueAjuAmtza
,IdCueCorrAA=@pmIdCueCorrAA,NumDoc=@pmNumDoc,Referencia=@pmReferencia,DesUbicacion=@pmDesUbicacion,IdEstado=@pmIdEstado,FecRetiro=@pmFecRetiro,IdCausal=@pmIdCausal
,ComentRetiro=@pmComentRetiro,Comentarios=@pmComentarios,TipoAmtza=@pmTipoAmtza,NitCliente=@pmNitCliente,AgenCliente=@pmAgenCliente,CantGalones=@pmCantGalones,IdProv=@pmIdProv
,NumPlaca=@pmNumPlaca,CodInstala=@pmCodInstala
 WHERE IdDiferido=@pmIdDiferido

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinClieSaldosLin @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)
,@pmIdLinea VARCHAR(4),@pmIdCia CHAR(2),@pmVrSaldoCap MONEY,@pmVrSaldoInt MONEY,@pmVrSaldoOtro MONEY
 AS 
 UPDATE FinClieSaldosLin SET VrSaldoCap=@pmVrSaldoCap,VrSaldoInt=@pmVrSaldoInt,VrSaldoOtro=@pmVrSaldoOtro
 WHERE IdClie=@pmIdClie AND CdAgencia=@pmCdAgencia AND IdLinea=@pmIdLinea AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPrestamo SMALLDATETIME,@pmFecCorte SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdTasa VARCHAR(4),@pmNPlazos INT,@pmCausacion VARCHAR(10),@pmIdMora VARCHAR(4)
,@pmNContrato INT,@pmCdCiaCon CHAR(2),@pmNActaJunta INT,@pmNumCredito VARCHAR(20),@pmIdVend VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoGarantia VARCHAR(10),@pmVrGarantia MONEY,@pmDescGarantia VARCHAR(250),@pmCxPagar BIT,@pmNumCuotaIni INT,@pmAcumCapital MONEY,@pmAcumIntereses MONEY,@pmAcumIntMora MONEY,@pmAcumCuotas INT,@pmPagosCapital MONEY
,@pmPagosIntereses MONEY,@pmPagosIntMora MONEY,@pmCuotasPagadas INT,@pmCuotasMora INT,@pmTipoAprob VARCHAR(20),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmDiaCausac INT,@pmDiaCausac2 INT,@pmDiasVence VARCHAR(50),@pmIdLinea VARCHAR(4),@pmTipoPlazo CHAR(1),@pmTipoCausac INT,@pmCdTasa2 VARCHAR(4),@pmVrSeguro MONEY,@pmVrIntereses MONEY,@pmVrNeto MONEY,@pmVrCuota MONEY,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinPrestamo SET Fecha=@pmFecha,FecPrestamo=@pmFecPrestamo,FecCorte=@pmFecCorte,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrPrestamo=@pmVrPrestamo,IdTasa=@pmIdTasa,NPlazos=@pmNPlazos,Causacion=@pmCausacion,IdMora=@pmIdMora,NContrato=@pmNContrato,CdCiaCon=@pmCdCiaCon,NActaJunta=@pmNActaJunta,NumCredito=@pmNumCredito,IdVend=@pmIdVend
,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipoGarantia=@pmTipoGarantia,VrGarantia=@pmVrGarantia,DescGarantia=@pmDescGarantia,CxPagar=@pmCxPagar,NumCuotaIni=@pmNumCuotaIni,AcumCapital=@pmAcumCapital,AcumIntereses=@pmAcumIntereses,AcumIntMora=@pmAcumIntMora,AcumCuotas=@pmAcumCuotas,PagosCapital=@pmPagosCapital,PagosIntereses=@pmPagosIntereses,PagosIntMora=@pmPagosIntMora,CuotasPagadas=@pmCuotasPagadas
,CuotasMora=@pmCuotasMora,TipoAprob=@pmTipoAprob,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,CdTasa2=@pmCdTasa2,VrSeguro=@pmVrSeguro,VrIntereses=@pmVrIntereses,VrNeto=@pmVrNeto,VrCuota=@pmVrCuota,FecUpdate=@pmFecUpdate
,DiaCausac=@pmDiaCausac,DiaCausac2=@pmDiaCausac2,DiasVence=@pmDiasVence,IdLinea=@pmIdLinea,TipoPlazo=@pmTipoPlazo,TipoCausac=@pmTipoCausac
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpProdImpFron @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmIdLocal VARCHAR(8),@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 UPDATE ProdImpFron SET IdLocal=@pmIdLocal,Tarifa=@pmTarifa,SimbTfa=@pmSimbTfa,CdTarifa=@pmCdTarifa,RngFecha=@pmRngFecha,FecInicial=@pmFecInicial,FecFinal=@pmFecFinal
,LtaPre=@pmLtaPre,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate,TarifZfe=@pmTarifZfe
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdImptos @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 UPDATE ProdImptos SET Tarifa=@pmTarifa,SimbTfa=@pmSimbTfa,CdTarifa=@pmCdTarifa,RngFecha=@pmRngFecha,FecInicial=@pmFecInicial,FecFinal=@pmFecFinal,LtaPre=@pmLtaPre
,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate,TarifZfe=@pmTarifZfe
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTercClieLineas @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmIdTasa VARCHAR(4),@pmnPlazo INT,@pmTipoPlazo CHAR(1),@pmCupoCredito MONEY,@pmSaldoCapital MONEY,@pmSaldoInteres MONEY,@pmMaxCupoSmlv DECIMAL(14,4),@pmTipoCausac VARCHAR(10),@pmDiasVence VARCHAR(20)
,@pmFecPreAprob SMALLDATETIME,@pmPlazoAprob INT,@pmEtdoAprob INT,@pmTipoAprob VARCHAR(20),@pmDescAprob VARCHAR(250),@pmGarantias BIT,@pmTipoGarantia VARCHAR(10),@pmDescGarantia VARCHAR(250),@pmVrGarantia MONEY,@pmReqCheques BIT,@pmFecActCupo SMALLDATETIME,@pmEstadoAct INT,@pmLiqSeguro BIT,@pmFechaAct SMALLDATETIME
 AS 
 UPDATE TercClieLineas SET IdTasa=@pmIdTasa,nPlazo=@pmnPlazo,TipoPlazo=@pmTipoPlazo,CupoCredito=@pmCupoCredito,SaldoCapital=@pmSaldoCapital,SaldoInteres=@pmSaldoInteres,MaxCupoSmlv=@pmMaxCupoSmlv,TipoCausac=@pmTipoCausac,DiasVence=@pmDiasVence,FecPreAprob=@pmFecPreAprob,PlazoAprob=@pmPlazoAprob,EtdoAprob=@pmEtdoAprob
,TipoAprob=@pmTipoAprob,DescAprob=@pmDescAprob,Garantias=@pmGarantias,TipoGarantia=@pmTipoGarantia,DescGarantia=@pmDescGarantia,VrGarantia=@pmVrGarantia,ReqCheques=@pmReqCheques,FecActCupo=@pmFecActCupo,EstadoAct=@pmEstadoAct,LiqSeguro=@pmLiqSeguro,FechaAct=@pmFechaAct
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTercCliePrestamo @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargContac VARCHAR(50)
,@pmIdSzona VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdTipoTerc VARCHAR(4),@pmIdVend VARCHAR(16),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmEsExento BIT,@pmCalcIntMora BIT,@pmDiasGracia INT,@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME
,@pmFecRetiro SMALLDATETIME,@pmVrSalBasico MONEY,@pmMaxDcto DECIMAL(14,4),@pmMatMerc VARCHAR(20),@pmFecMat SMALLDATETIME,@pmNomAgencia VARCHAR(150),@pmDirAgencia VARCHAR(250),@pmIdLocalAge VARCHAR(8),@pmComentarios VARCHAR(2000),@pmIdEstado VARCHAR(4)
,@pmInactivo BIT,@pmTipoSalario CHAR(1),@pmFechaAct SMALLDATETIME
 AS 
 UPDATE TercCliePrestamo SET NitRepLeg=@pmNitRepLeg,NomRepLeg=@pmNomRepLeg,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargContac=@pmCargContac,IdSzona=@pmIdSzona,IdGrupo=@pmIdGrupo,IdTipoTerc=@pmIdTipoTerc,IdVend=@pmIdVend
,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,EsExento=@pmEsExento,CalcIntMora=@pmCalcIntMora,DiasGracia=@pmDiasGracia,FecIngreso=@pmFecIngreso,FecVigencia=@pmFecVigencia,FecRetiro=@pmFecRetiro,VrSalBasico=@pmVrSalBasico,MaxDcto=@pmMaxDcto,MatMerc=@pmMatMerc,FecMat=@pmFecMat
,Comentarios=@pmComentarios,IdEstado=@pmIdEstado,NomAgencia=@pmNomAgencia,DirAgencia=@pmDirAgencia,IdLocalAge=@pmIdLocalAge,Inactivo=@pmInactivo,FechaAct=@pmFechaAct,TipoSalario=@pmTipoSalario
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

