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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFinAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo 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 paDelFinAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE 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 ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFinCuotas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 DELETE 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 ON 
GO

CREATE PROCEDURE paDelFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinDevPre
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion 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 paDelFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinDevRec
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion 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 paDelFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinNotas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota 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 paDelFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 DELETE 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 ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinRecibos
 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 paInsFinAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaPre CHAR(2)
,@pmItemPre INT,@pmTotalAbono MONEY,@pmVrBaseCms MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16)
,@pmComision DECIMAL(14,4),@pmCantGal DECIMAL(14,4),@pmDetalle VARCHAR(150),@pmTipoAplica INT,@pmFecPago SMALLDATETIME,@pmTimeSys SMALLDATETIME
 AS 
 INSERT INTO Trn_FinAbonos (TipRec,Recibo,IdCia,Item,Fecha,TipDoc,IdPrestamo,IdCiaPre,ItemPre,TotalAbono,VrBaseCms,IdCliente,IdAgencia,IdVend,Comision,CantGal,Detalle,TipoAplica,FecPago,TimeSys)
 VALUES (@pmTipRec,@pmRecibo,@pmIdCia,@pmItem,@pmFecha,@pmTipDoc,@pmIdPrestamo,@pmIdCiaPre,@pmItemPre,@pmTotalAbono,@pmVrBaseCms,@pmIdCliente,@pmIdAgencia
,@pmIdVend,@pmComision,@pmCantGal,@pmDetalle,@pmTipoAplica,@pmFecPago,@pmTimeSys)
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)
 AS 
 INSERT INTO Trn_FinAfavor (TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmVrAFavor,@pmVrOtrosDb,@pmVrAbono,@pmIdCliente,@pmIdAgencia,@pmIdVend)
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
 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)
 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)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinCausacion @pmTipoProc VARCHAR(3),@pmNumProc INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTotalCapital MONEY
,@pmTotalInteres MONEY,@pmTotalIntMora MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmAnulado BIT,@pmObservacion VARCHAR(250)
,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinCausacion (TipoProc,NumProc,IdCia,Fecha,TotalCapital,TotalInteres,TotalIntMora,TipCom,Comprobante,Anulado,Observacion,TimeSys,IdUsuario)
 VALUES (@pmTipoProc,@pmNumProc,@pmIdCia,@pmFecha,@pmTotalCapital,@pmTotalInteres,@pmTotalIntMora,@pmTipCom,@pmComprobante,@pmAnulado
,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 INSERT INTO FinClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
 VALUES (@pmIdClie,@pmCdAgencia,@pmIdCia,@pmVrSaldo)

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
 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)
 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)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdVend VARCHAR(16),@pmCxPagar BIT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinDevPre (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,IdPrestamo,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrPrestamo,IdVend,CxPagar,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmIdPrestamo,@pmIdCiaDoc,@pmFecDoc,@pmIdCliente,@pmIdAgencia,@pmVrPrestamo,@pmIdVend,@pmCxPagar,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante
,@pmIdCiaCom,@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 paInsFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY
,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmReferencia VARCHAR(50),@pmNumAutoriza INT
,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinDevRec (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,IdAgencia,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,Referencia,NumAutoriza,ModdDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmRecibo,@pmIdCiaDoc,@pmFecDoc,@pmFecPago,@pmIdCliente,@pmIdAgencia,@pmVrRecibido,@pmVrSubTotal,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrOtros,@pmVrOtrDcto,@pmVrPagosMas,@pmVrNeto,@pmVrAplicado,@pmVrAnticipo,@pmVrEfectivo,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmIdVend,@pmTarifaCom
,@pmCodTarCom,@pmEnEfectivo,@pmReferencia,@pmNumAutoriza,@pmModdDev,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@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 paInsFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrSancion MONEY,@pmVrOtros MONEY,@pmVrNeto MONEY
,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16)
,@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFecDcm SMALLDATETIME,@pmCdForma VARCHAR(4),@pmNumCheque VARCHAR(20),@pmCdBanco VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmModalidad VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinNotas (TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdForma,NumCheque,CdBanco
,MulPlazos,IdPlazo,Modalidad,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFechaVence,@pmVrSubTotal,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrSancion,@pmVrOtros,@pmVrNeto,@pmVrAplicado,@pmVrAnticipo,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv
,@pmIdCCosto,@pmIdSubCos,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmReferencia,@pmpVehiculo,@pmCdConductor,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmFecDcm,@pmCdForma,@pmNumCheque,@pmCdBanco,@pmMulPlazos,@pmIdPlazo,@pmModalidad,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmAnulado,@pmIdEstado,@pmOrigenAdd,@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 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),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinPrestamo (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,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmFecha,@pmFecPrestamo,@pmFecCorte,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmVrPrestamo,@pmIdTasa,@pmNPlazos,@pmCausacion,@pmIdMora,@pmNContrato,@pmCdCiaCon,@pmNActaJunta,@pmNumCredito,@pmIdVend,@pmpVehiculo,@pmCdConductor,@pmTipoGarantia,@pmVrGarantia,@pmDescGarantia,@pmCxPagar,@pmNumCuotaIni,@pmAcumCapital,@pmAcumIntereses,@pmAcumIntMora,@pmAcumCuotas,@pmPagosCapital,@pmPagosIntereses
,@pmPagosIntMora,@pmCuotasPagadas,@pmCuotasMora,@pmTipoAprob,@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 paInsFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY
,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrEfectivo MONEY,@pmVrAnticipo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4)
,@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinRecibos (TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco,CdCta,Referencia,pVehiculo,CdConductor
,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFecPago,@pmVrRecibido,@pmVrSubTotal,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrOtros,@pmVrOtrDcto,@pmVrPagosMas,@pmVrNeto,@pmVrAplicado,@pmVrEfectivo,@pmVrAnticipo,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarRet,@pmCodTarIca
,@pmCodTarRiv,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmEnEfectivo,@pmCdForma,@pmNumForma,@pmDetallePago,@pmCdBanco,@pmCdCta,@pmReferencia,@pmpVehiculo,@pmCdConductor,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@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 paInsTasasDTF @pmnAnno INT,@pmnMes INT,@pmnSem INT,@pmDTF_EA DECIMAL(14,4),@pmTIBC_EA DECIMAL(14,4),@pmFactor_Mon DECIMAL(14,4)
 AS 
 INSERT INTO TasasDTF (nAnno,nMes,nSem,DTF_EA,TIBC_EA,Factor_Mon)
 VALUES (@pmnAnno,@pmnMes,@pmnSem,@pmDTF_EA,@pmTIBC_EA,@pmFactor_Mon)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTasasInteres @pmIdTasa VARCHAR(4),@pmDescTasa VARCHAR(50),@pmTipoInteres VARCHAR(10),@pmTipoPeriodo VARCHAR(10),@pmCausaInteres INT,@pmTipoDTF VARCHAR(10)
,@pmDTF_EA DECIMAL(14,4),@pmSpread_TA DECIMAL(14,4),@pmTasaEfectiva DECIMAL(14,4),@pmOpcCompra DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO TasasInteres (IdTasa,DescTasa,TipoInteres,TipoPeriodo,CausaInteres,TipoDTF,DTF_EA,Spread_TA,TasaEfectiva,OpcCompra,Inactivo)
 VALUES (@pmIdTasa,@pmDescTasa,@pmTipoInteres,@pmTipoPeriodo,@pmCausaInteres,@pmTipoDTF,@pmDTF_EA,@pmSpread_TA,@pmTasaEfectiva,@pmOpcCompra,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTasasIntmora @pmIdMora VARCHAR(4),@pmDescMora VARCHAR(50),@pmTipoInteres VARCHAR(10)
,@pmTasaEA DECIMAL(14,4),@pmTasaEM DECIMAL(14,4),@pmDiasGracia INT,@pmInactivo BIT
 AS 
 INSERT INTO TasasIntmora (IdMora,DescMora,TipoInteres,TasaEA,TasaEM,DiasGracia,Inactivo)
 VALUES (@pmIdMora,@pmDescMora,@pmTipoInteres,@pmTasaEA,@pmTasaEM,@pmDiasGracia,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreAbonos @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipDoc VARCHAR(3),@pmtmIdPrestamo INT,@pmtmIdCiaPre CHAR(2),@pmtmItemPre INT,@pmtmVrAbono MONEY,@pmtmVrBaseCms MONEY
,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmComision DECIMAL(14,4),@pmtmDetalle VARCHAR(150),@pmtmGalones DECIMAL(14,4),@pmtmTipoAplica INT,@pmtmCodTarCom VARCHAR(4),@pmtmTipRec VARCHAR(3),@pmtmRecibo INT
,@pmtmCiaRec CHAR(2),@pmtmItemRec INT,@pmtmFecPago SMALLDATETIME
 AS 
 INSERT INTO tm_PreAbonos (tmNumero,tmItem,tmTipDoc,tmIdPrestamo,tmIdCiaPre,tmItemPre,tmVrAbono,tmVrBaseCms,tmIdCliente,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmGalones,tmTipoAplica,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmIdPrestamo,@pmtmIdCiaPre,@pmtmItemPre,@pmtmVrAbono,@pmtmVrBaseCms,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmComision,@pmtmDetalle,@pmtmGalones,@pmtmTipoAplica
,@pmtmCodTarCom,@pmtmTipRec,@pmtmRecibo,@pmtmCiaRec,@pmtmItemRec,@pmtmFecPago)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreAbonos_Sel  @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_PreAbonos (tmNumero,tmItem,tmTipDoc,tmIdPrestamo,tmIdCiaPre,tmItemPre,tmVrAbono,tmVrBaseCms,tmIdCliente,tmIdAgencia
,tmIdVend,tmComision,tmDetalle,tmGalones,tmTipoAplica,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago)
SELECT @pmtmNumero,Item,TipDoc,IdPrestamo,IdCiaPre,ItemPre,TotalAbono,VrBaseCms,IdCliente,IdAgencia
,IdVend,Comision,Detalle,CantGal,TipoAplica,'0',TipRec,Recibo,IdCia,Item,FecPago
 FROM Trn_FinAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo 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 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
 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)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmPrestamo,@pmtmIdCiaPre,@pmtmItemPre,@pmtmCuota,@pmtmConcepto,@pmtmFecCausa,@pmtmFecVence,@pmtmVrTotal,@pmtmTipoTasa,@pmtmDTF_EA,@pmtmTasaNom
,@pmtmSpread,@pmtmTasaEfe,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmEstado,@pmtmDetalle,@pmtmTotAbono)


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)
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
 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)
 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)
 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)
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)
 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,''
 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 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,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,FechaCrea
 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 Agencias AS AG ON A.IdAgencia=AG.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
 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,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,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 Agencias  AS A ON CM.IdAgencia=A.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,Agencia
,TipoCuota,Concepto,Detalle,FecUltPago,FecLiqMora,FechaCrea,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 Agencias AS A ON C.IdAgencia=A.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 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
 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,Agencia,TipoCuota,Concepto
,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,FechaCrea,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 Agencias AS A ON C.IdAgencia=A.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 ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,IdPrestamo,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrPrestamo,IdVend,CxPagar,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinDevPre
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion 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 paQryFinDevPreDoc @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,IdPrestamo,IdCiaDoc,FecDoc,IdCliente,ModdDev
 FROM Trn_FinDevPre
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCiaDoc=@pmIdCiaDoc

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,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,Causacion,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta
,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
--agencias
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 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 TercCliente AS CLI ON D.IdCliente=CLI.IdClie 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
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 LEFT JOIN Agencias AS A ON D.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 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 OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevPreLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente 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,D.VrPrestamo AS ValorDev,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor
,NPlazos,Causacion,NActaJunta,NumCredito,D.CxPagar AS Cx_pagar,ModdDev,D.TipCom AS Tip_Comp,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
 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
 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,'%')
 ORDER BY  D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,IdAgencia,VrRecibido,VrSubTotal,VrRetencion
,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo
,Referencia,NumAutoriza,ModdDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinDevRec
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion 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 paQryFinDevRecDoc @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,IdCliente,ModdDev
 FROM Trn_FinDevRec
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCiaDoc=@pmIdCiaDoc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevRecFmt @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 TipRec,Recibo,IdCiaDoc,FecDoc,FecPago
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,VrRecibido,VrSubTotal,VrRetencion
,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,D.IdVend AS CdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,EnEfectivo
,D.Referencia AS DevRefrencia,D.NumAutoriza AS NumAutza,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--datos de forma de pago
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco
,P.NumForma AS PagNumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2
,CdLocal,LF.Localidad AS CiudadPlaza,P.NumAutoriza AS PagAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,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
--agencias
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM  Trn_FinDevRec AS D  INNER JOIN Trn_Pagos AS P ON D.TipDev=P.TipDoc AND D.Devolucion=P.Documento AND D.IdCia=P.IdCia
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario   INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS 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 TercCliente AS CLI ON D.IdCliente=CLI.IdClie 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
 LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom  LEFT JOIN Agencias  AS A ON D.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE 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 OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevRecLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,T.RazonSocial AS NomCliente,IdAgencia
,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv
,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,EnEfectivo,Referencia,NumAutoriza,ModdDev,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
 FROM Trn_FinDevRec AS D  INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') 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 ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv
,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdForma,NumCheque,CdBanco,MulPlazos,IdPlazo,Modalidad,TipCom,Comprobante,IdCiaCom
,Observacion,Anulado,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinNotas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota 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 paQryFinNotasFmt @pmTipDoc VARCHAR(3),@pmNumNotaIni INT, @pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  NumNota,N.IdCia AS CdCia,Compania,N.Fecha AS FechaNota,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,N.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia
,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv
,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,N.IdCCosto AS CdcCosto,CC.CCosto AS NotCenCosto,N.IdSubCos AS CdSubCost,SC.SubCosto AS NotSubCentro,N.IdVend AS CdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,N.Referencia AS NotReferencia,pVehiculo,CdConductor,TipDcm,N.Documento AS NumDcmto,IdCiaDcm,FecDcm,CdForma,N.NumCheque AS NroCheque,CdBanco,BN.Banco AS NomBanco,MulPlazos
,N.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,Modalidad,N.TipCom AS TipComp,TipoCom,N.Comprobante AS NumComp,IdCiaCom,N.Observacion AS Observ,Anulado,N.IdEstado AS CdEstado,Estado
,OrigenAdd,N.TimeSys AS FechaCrea,N.FecUpdate AS FechaAct,N.IdCiaCrea  AS CdCiaCrea,N.IdUsuario AS CdUsuario,Usuario
--detalle del comprobante
,Item,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,IdVehiculo,D.IdCCosto AS DetCodCentCost,DC.CCosto AS DetCentCost
,D.IdSubCos AS DetCodSubCentro,DS.SubCosto  AS DetSubCentro,VrBase,TarifaBase,D.TipDoc AS DetTipoDoc,D.Documento AS DetNumDoc,IdCiaDoc,TipFac,Factura,IdCiaFac,ItemFac
,FecVence,D.NumCheque AS DetNumCheque,TipoAplica,NitOtros,Referncia
--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
--agencias
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_FinNotas AS N INNER JOIN Terceros AS T ON N.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  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto INNER JOIN  Companias AS CI ON N.IdCia=CI.IdCia
 INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero  INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado  INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN  Plazos AS PZ ON N.IdPlazo=PZ.IdPlazo
 INNER JOIN TercCliente AS CLI ON N.IdCliente=CLI.IdClie 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
 LEFT JOIN Agencias  AS A ON N.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TC ON N.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Bancos AS BN ON N.CdBanco=BN.IdBanco LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 LEFT JOIN  Trn_ComDetalle AS D ON N.TipCom=D.TipCom AND N.Comprobante=D.Comprobante AND N.IdCiaCom=D.IdCia
 LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta LEFT JOIN Terceros AS DT ON D.IdTercero=DT.IdTercero
 LEFT JOIN CentroCosto AS DC ON D.IdCCosto=DC.IdCCosto  LEFT JOIN SubCentros AS DS ON D.IdSubCos=DS.IdSubCos
 WHERE N.TipDoc=@pmTipDoc  AND NumNota BETWEEN  @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
 ORDER BY NumNota

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinNotasLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT  NumNota,N.IdCia AS CdCia,Compania,Fecha,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto
,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,N.IdCCosto AS CdcCosto,N.IdSubCos AS CdSubCost,IdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdForma,NumCheque,CdBanco,MulPlazos,IdPlazo,Modalidad,TipCom,Comprobante,IdCiaCom
,N.Observacion AS Observ,Anulado,N.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,N.IdUsuario AS CdUsuario,Usuario
 FROM Trn_FinNotas AS N INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON N.IdCia=CI.IdCia INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
  AND N.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
  ORDER BY N.IdCia,NumNota

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,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,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 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,Agencia,VrPrestamo,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int,TasaEfectiva,NPlazos,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,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,P.Observacion AS Observ
,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,FechaCrea
--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
--agencias
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 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 TercCliente AS CLI ON P.IdCliente=CLI.IdClie 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
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON P.TipCom=TC.IdCom
 LEFT JOIN Agencias AS A ON P.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=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,P.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrPrestamo,P.IdTasa AS CdTasa,DescTasa
,NPlazos,Causacion,IdMora,NContrato,CdCiaCon,TipoAprob,NActaJunta,NumCredito,IdVend,VN.RazonSocial AS NomVendedor,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia
,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora
,CuotasPagadas,CuotasMora,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 paQryFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo
,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinRecibos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinRecibosDoc  @pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipDcm VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo
,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinRecibos
 WHERE Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm AND TipDcm LIKE ISNULL(@pmTipDcm,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinRecibosFmt @pmTipDoc VARCHAR(3),@pmReciboIni INT, @pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,R.Fecha AS FecRecibo,R.IdConcepto AS CdConcepto,Concepto,R.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia
,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo
,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,R.IdVend AS CdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,EnEfectivo,CdForma,R.NumForma AS RecNumForma,DetallePago,CdBanco,BR.Banco AS RecBanco,R.CdCta AS RecCodCta,R.Referencia AS RecReferncia,pVehiculo,CdConductor
,TipDcm,R.Documento AS NumDcmto,IdCiaDcm,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
--datos de forma de pago
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco
,P.NumForma AS PagNumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2
,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
--agencias
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_FinRecibos AS R  INNER JOIN Trn_Pagos AS P ON R.TipDoc=P.TipDoc AND R.Recibo=P.Documento AND R.IdCia=P.IdCia
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario   INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN TercCliente AS CLI ON R.IdCliente=CLI.IdClie 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
 LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom  LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco
 LEFT JOIN Agencias  AS A ON R.IdAgencia=A.IdAgencia  LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE R.TipDoc=@pmTipDoc  AND Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND R.IdCia=@pmIdCia
 ORDER BY  R.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinRecibosLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,Fecha,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA
,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
 FROM Trn_FinRecibos AS R  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') 
 ORDER BY R.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTasasInteres @pmIdTasa VARCHAR(4)
 AS 
 SELECT IdTasa,DescTasa,TipoInteres,TipoPeriodo,CausaInteres,TipoDTF,DTF_EA,Spread_TA,TasaEfectiva,OpcCompra,Inactivo
 FROM TasasInteres
 WHERE IdTasa=@pmIdTasa

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTasasInteresDso 
 AS 
 SELECT IdTasa,DescTasa,IdTasa+' '+DescTasa+' ( '+TipoInteres+'  '+TipoPeriodo
 + ' '+CASE CausaInteres WHEN 1 THEN 'ANTICIPADA' ELSE 'VENCIDA' END +'  DTF '+TipoDTF+')' AS DsTasa 
 FROM TasasInteres
 WHERE Inactivo=0 
 ORDER BY IdTasa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTasasIntmoraDso 
 AS 
 SELECT IdMora,DescMora,IdMora+' '+DescMora+' ( '+TipoInteres+'  '+STR(TasaEM,7,4)+ ')' AS DsTasa 
 FROM TasasIntmora
 WHERE Inactivo=0 
 ORDER BY IdMora

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CuotasCr @pmtmEst CHAR(2)
AS
 SELECT tmPrestamoID,tmIdCia,tmNumCuota,tmFecCausa,tmFecVence,tmValorCapital,tmValorInteres,tmValorCuota,tmSaldo
,tmValorCausado AS VrPrestamo,tmFecPago,tmRecibo AS NPlazos
 FROM    tm_Cuotas
 WHERE tmEst=@pmtmEst
 ORDER BY tmPrestamoID,tmNumCuota


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PreAbonos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmTipDoc,tmIdPrestamo,tmIdCiaPre,tmItemPre,tmVrAbono,tmVrBaseCms,tmIdCliente,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmGalones
,tmTipoAplica,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago
 FROM tm_PreAbonos
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
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, 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
 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 OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinAfavorAbo @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmVrAbono MONEY
 AS 
 UPDATE Trn_FinAfavor SET VrAbono=@pmVrAbono
 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 paUpFinAfavorApl @pmIdCliente VARCHAR(16)=Null,@pmIdVend  VARCHAR(16)=Null
 AS 
 UPDATE Trn_FinAfavor SET  Trn_FinAfavor.VrAbono=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE  Trn_FinAfavor.TipDoc=Trn_FinAbonos.TipRec AND Trn_FinAfavor.Recibo=Trn_FinAbonos.Recibo
   AND Trn_FinAfavor.IdCia=Trn_FinAbonos.IdCia AND TipoAplica=2)
  WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinAfavorDoc @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_FinAfavor SET  Trn_FinAfavor.VrAbono=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE  Trn_FinAfavor.TipDoc=Trn_FinAbonos.TipRec AND Trn_FinAfavor.Recibo=Trn_FinAbonos.Recibo
   AND Trn_FinAfavor.IdCia=Trn_FinAbonos.IdCia AND TipoAplica=2)
  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 paUpFinAfavorOdb @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmVrAbono MONEY,@pmVrOtrosDb MONEY
 AS 
 UPDATE Trn_FinAfavor  SET VrAbono=@pmVrAbono,VrOtrosDb=@pmVrOtrosDb
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo 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 paUpFinClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 UPDATE FinClieSaldos SET VrSaldo=@pmVrSaldo
 WHERE IdClie=@pmIdClie AND CdAgencia=@pmCdAgencia 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 paUpFinClieSaldosAcu @pmIdClie VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE NAME LIKE '#FinClieSaldos%') 
 DROP TABLE #FinClieSaldos
 CREATE TABLE #FinClieSaldos (tmId INT PRIMARY KEY IDENTITY (1,1)
 ,IdClie VARCHAR(16),CdAgencia VARCHAR(16),IdCia CHAR(2),VrCuotas MONEY,VrAbonos MONEY,VrAFavor MONEY,VrAbonoSAF MONEY)
--cuotas
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,SUM(VrTotal),0,0,0
 FROM Trn_FinCuotas
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
--abonos de cuotas
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,SUM(TotalAbono),0,0
 FROM Trn_FinAbonos
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
--recibos a favor
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,0,SUM(VrAFavor-VrOtrosDb),0
 FROM Trn_FinAfavor
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
 --abonos de saldos a favor
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,0,0,SUM(TotalAbono)
 FROM Trn_FinAbonos
 WHERE TipoAplica=2 AND IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia 
 --actualiza saldos
 DECLARE @TrnNombre VARCHAR(20)
 SET @TrnNombre = 'TrnSaldos'
 BEGIN TRANSACTION @TrnNombre
 DELETE FROM FinClieSaldos WHERE IdClie LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 INSERT INTO FinClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
 SELECT IdClie,CdAgencia,IdCia,SUM((VrCuotas-VrAbonos)-(VrAFavor-VrAbonoSAF)) 
 FROM #FinClieSaldos
 GROUP BY IdClie,CdAgencia,IdCia
 COMMIT TRANSACTION  @TrnNombre
--DROP TABLE #FinClieSaldos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinCuotasAbo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT
,@pmVrAbonado MONEY,@pmFecUltPago SMALLDATETIME
 AS 
 UPDATE Trn_FinCuotas SET  VrAbonado=@pmVrAbonado,FecUltPago=@pmFecUltPago
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia 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 paUpFinCuotasApl  @pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   UPDATE Trn_FinCuotas SET  Trn_FinCuotas.VrAbonado=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE Trn_FinCuotas.TipDoc=Trn_FinAbonos.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinAbonos.IdPrestamo
   AND Trn_FinCuotas.IdCia=Trn_FinAbonos.IdCiaPre  AND Trn_FinCuotas.Item=Trn_FinAbonos.ItemPre)
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
   AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ELSE
   UPDATE Trn_FinCuotas SET  Trn_FinCuotas.VrAbonado=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE Trn_FinCuotas.TipDoc=Trn_FinAbonos.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinAbonos.IdPrestamo
   AND Trn_FinCuotas.IdCia=Trn_FinAbonos.IdCiaPre  AND Trn_FinCuotas.Item=Trn_FinAbonos.ItemPre)
   WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinCuotasCero  @pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   UPDATE Trn_FinCuotas SET VrAbonado=0
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
   AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ELSE
   UPDATE Trn_FinCuotas SET VrAbonado=0 WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdVend VARCHAR(16),@pmCxPagar BIT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinDevPre SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,IdPrestamo=@pmIdPrestamo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrPrestamo=@pmVrPrestamo,IdVend=@pmIdVend,CxPagar=@pmCxPagar
,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion 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 paUpFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY
,@pmVrOtrDcto MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmReferencia VARCHAR(50),@pmNumAutoriza INT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinDevRec SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Recibo=@pmRecibo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,FecPago=@pmFecPago,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrRecibido=@pmVrRecibido,VrSubTotal=@pmVrSubTotal,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrOtros=@pmVrOtros,VrOtrDcto=@pmVrOtrDcto,VrPagosMas=@pmVrPagosMas,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado
,VrAnticipo=@pmVrAnticipo,VrEfectivo=@pmVrEfectivo,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,EnEfectivo=@pmEnEfectivo,Referencia=@pmReferencia,NumAutoriza=@pmNumAutoriza,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion 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 paUpFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrSancion MONEY,@pmVrOtros MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4)
,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFecDcm SMALLDATETIME,@pmCdForma VARCHAR(4),@pmNumCheque VARCHAR(20)
,@pmCdBanco VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinNotas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrSancion=@pmVrSancion,VrOtros=@pmVrOtros,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,VrAnticipo=@pmVrAnticipo,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv
,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,Referencia=@pmReferencia,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,FecDcm=@pmFecDcm,CdForma=@pmCdForma,NumCheque=@pmNumCheque,CdBanco=@pmCdBanco,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,Modalidad=@pmModalidad
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,Anulado=@pmAnulado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota 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),@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,FecUpdate=@pmFecUpdate
 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 paUpFinPrestamoAcum  @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_FinPrestamo SET Trn_FinPrestamo.AcumCapital=(SELECT ISNULL(SUM(Trn_FinCuotas.VrTotal),0)
 FROM Trn_FinCuotas  WHERE Trn_FinCuotas.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinCuotas.IdCia=Trn_FinPrestamo.IdCia AND Concepto='CAPITAL')
 ,Trn_FinPrestamo.AcumCuotas=(SELECT ISNULL(COUNT(Trn_FinCuotas.NumCuota),0)
 FROM Trn_FinCuotas  WHERE Trn_FinCuotas.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinCuotas.IdCia=Trn_FinPrestamo.IdCia AND Concepto='CAPITAL')
,Trn_FinPrestamo.AcumIntereses=(SELECT ISNULL(SUM(Trn_FinCuotas.VrTotal),0)
 FROM Trn_FinCuotas  WHERE Trn_FinCuotas.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinCuotas.IdCia=Trn_FinPrestamo.IdCia AND Concepto='INTERESES')
  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 paUpFinPrestamoDev @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_FinPrestamo SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 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 paUpFinPrestamoPag  @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_FinPrestamo SET Trn_FinPrestamo.PagosCapital=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
 FROM Trn_FinAbonos INNER JOIN Trn_FinCuotas ON Trn_FinAbonos.TipDoc=Trn_FinCuotas.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinCuotas.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinCuotas.IdCia AND  Trn_FinAbonos.ItemPre=Trn_FinCuotas.Item
  WHERE Trn_FinAbonos.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinPrestamo.IdCia  AND Concepto='CAPITAL')
--INTERESS
,Trn_FinPrestamo.PagosIntereses=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
 FROM Trn_FinAbonos INNER JOIN Trn_FinCuotas ON Trn_FinAbonos.TipDoc=Trn_FinCuotas.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinCuotas.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinCuotas.IdCia AND  Trn_FinAbonos.ItemPre=Trn_FinCuotas.Item
  WHERE Trn_FinAbonos.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinPrestamo.IdCia  AND Concepto='INTERESES')
 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 paUpFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY
,@pmVrAplicado MONEY,@pmVrEfectivo MONEY,@pmVrAnticipo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4)
,@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinRecibos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FecPago=@pmFecPago,VrRecibido=@pmVrRecibido,VrSubTotal=@pmVrSubTotal,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrOtros=@pmVrOtros,VrOtrDcto=@pmVrOtrDcto,VrPagosMas=@pmVrPagosMas,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,VrEfectivo=@pmVrEfectivo,VrAnticipo=@pmVrAnticipo
,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,EnEfectivo=@pmEnEfectivo,CdForma=@pmCdForma,NumForma=@pmNumForma,DetallePago=@pmDetallePago,CdBanco=@pmCdBanco,CdCta=@pmCdCta
,Referencia=@pmReferencia,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 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 paUpFinRecibosDev @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_FinRecibos SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo 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 paUpTasasDTF @pmnAnno INT,@pmnMes INT,@pmnSem INT,@pmDTF_EA DECIMAL(14,4),@pmTIBC_EA DECIMAL(14,4),@pmFactor_Mon DECIMAL(14,4)
 AS 
 UPDATE TasasDTF SET DTF_EA=@pmDTF_EA,TIBC_EA=@pmTIBC_EA,Factor_Mon=@pmFactor_Mon
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND nSem=@pmnSem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTasasInteres @pmIdTasa VARCHAR(4),@pmDescTasa VARCHAR(50),@pmTipoInteres VARCHAR(10),@pmTipoPeriodo VARCHAR(10),@pmCausaInteres INT,@pmTipoDTF VARCHAR(10)
,@pmDTF_EA DECIMAL(14,4),@pmSpread_TA DECIMAL(14,4),@pmTasaEfectiva DECIMAL(14,4),@pmOpcCompra DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE TasasInteres SET DescTasa=@pmDescTasa,TipoInteres=@pmTipoInteres,TipoPeriodo=@pmTipoPeriodo,CausaInteres=@pmCausaInteres,TipoDTF=@pmTipoDTF,DTF_EA=@pmDTF_EA,Spread_TA=@pmSpread_TA
,TasaEfectiva=@pmTasaEfectiva,OpcCompra=@pmOpcCompra,Inactivo=@pmInactivo
 WHERE IdTasa=@pmIdTasa

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTasasIntmora @pmIdMora VARCHAR(4),@pmDescMora VARCHAR(50),@pmTipoInteres VARCHAR(10)
,@pmTasaEA DECIMAL(14,4),@pmTasaEM DECIMAL(14,4),@pmDiasGracia INT,@pmInactivo BIT
 AS 
 UPDATE TasasIntmora SET DescMora=@pmDescMora,TipoInteres=@pmTipoInteres,TasaEA=@pmTasaEA,TasaEM=@pmTasaEM,DiasGracia=@pmDiasGracia,Inactivo=@pmInactivo
 WHERE IdMora=@pmIdMora

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

