CREATE TABLE FinClieSaldosLin ( 
IdClie VARCHAR(16) NOT NULL,
CdAgencia VARCHAR(16) DEFAULT ('0') NOT NULL,
IdLinea VARCHAR(4) NOT NULL,
IdCia CHAR(2) NOT NULL,
VrSaldoCap MONEY DEFAULT (0) NOT NULL,
VrSaldoInt MONEY DEFAULT (0) NOT NULL,
VrSaldoOtro MONEY DEFAULT (0) NOT NULL
 CONSTRAINT PK_FinClieSaldosLin PRIMARY KEY CLUSTERED (IdClie,CdAgencia,IdLinea,IdCia),
 CONSTRAINT CK_FinClieSaldosLinCdAgencia CHECK ((len([CdAgencia]) > 0)),
 CONSTRAINT CK_FinClieSaldosLinIdCia CHECK ((len([IdCia]) > 0)),
 CONSTRAINT CK_FinClieSaldosLinIdClie CHECK ((len([IdClie]) > 0)),
 CONSTRAINT CK_FinClieSaldosLinIdLinea CHECK ((len([IdLinea]) > 0)))
CREATE TABLE Trn_FinFacturas ( 
TipDoc VARCHAR(3) DEFAULT ('PR1') NOT NULL,
IdPrestamo INT DEFAULT (0) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
Item INT DEFAULT (1) NOT NULL,
NumFactura VARCHAR(20) NOT NULL,
Fecha SMALLDATETIME NOT NULL,
FechaVence SMALLDATETIME NOT NULL,
VrFactura MONEY DEFAULT (0) NOT NULL,
VrInteres MONEY DEFAULT (0) NOT NULL,
DiasLiq DECIMAL(14,4) DEFAULT (0) NOT NULL,
TasaEfeMes DECIMAL(14,4) DEFAULT (0) NOT NULL,
TasaEfeDia DECIMAL(14,4) DEFAULT (0) NOT NULL,
FechaPago SMALLDATETIME NOT NULL,
ItemPago INT DEFAULT (0) NOT NULL
 CONSTRAINT PK_Trn_FinFacturas PRIMARY KEY CLUSTERED (TipDoc,IdPrestamo,IdCia,Item),
 CONSTRAINT CK_Trn_FinFacturasIdCia CHECK ((len([IdCia]) > 0)),
 CONSTRAINT CK_Trn_FinFacturasNumFactura CHECK ((len([NumFactura]) > 0)),
 CONSTRAINT CK_Trn_FinFacturasTipDoc CHECK ((len([TipDoc]) > 0)))
CREATE TABLE Trn_FinPagos ( 
TipDoc VARCHAR(3) DEFAULT ('PR1') NOT NULL,
IdPrestamo INT DEFAULT (0) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
Item INT DEFAULT (1) NOT NULL,
Numero VARCHAR(20) NOT NULL,
Fecha SMALLDATETIME NOT NULL,
FecPago SMALLDATETIME NOT NULL,
VrCuota MONEY DEFAULT (0) NOT NULL,
EdoForma INT DEFAULT (0) NOT NULL,
CdBanco VARCHAR(4),
CtaCheque VARCHAR(30),
Detalle VARCHAR(150),
TipRec VARCHAR(3),
Recibo INT DEFAULT (0) NOT NULL,
IdCiaRec CHAR(2)
 CONSTRAINT PK_Trn_FinPagos PRIMARY KEY CLUSTERED (TipDoc,IdPrestamo,IdCia,Item),
 CONSTRAINT CK_Trn_FinPagosIdCia CHECK ((len([IdCia]) > 0)),
 CONSTRAINT CK_Trn_FinPagosNumero CHECK ((len([Numero]) > 0)),
 CONSTRAINT CK_Trn_FinPagosTipDoc CHECK ((len([TipDoc]) > 0)))
CREATE TABLE tm_PresPagos ( 
tmNumero VARCHAR(5) NOT NULL,
tmItem INT DEFAULT (0) NOT NULL,
tmNumForma VARCHAR(20) NOT NULL,
tmFecha SMALLDATETIME NOT NULL,
tmFecPago SMALLDATETIME NOT NULL,
tmVrCuota MONEY DEFAULT (0) NOT NULL,
tmCdBanco VARCHAR(4),
tmCtaCheque VARCHAR(30),
tmDetalle VARCHAR(150)
 CONSTRAINT PK_tm_PresPagos PRIMARY KEY CLUSTERED (tmNumero,tmItem),
 CONSTRAINT CK_tm_PresPagostmNumero CHECK ((len([tmNumero]) > 0)),
 CONSTRAINT CK_tm_PresPagostmNumForma CHECK ((len([tmNumForma]) > 0)))
CREATE TABLE FinCuposEmp ( 
nAnno INT DEFAULT (0) NOT NULL,
Item INT DEFAULT (0) NOT NULL,
nSmlvIni DECIMAL(14,4) DEFAULT (0) NOT NULL,
nSmlvFin DECIMAL(14,4) DEFAULT (0) NOT NULL,
Antig_Ini DECIMAL(14,4) DEFAULT (0) NOT NULL,
Antig_Fin DECIMAL(14,4) DEFAULT (0) NOT NULL,
MontoConGar DECIMAL(14,4) DEFAULT (0) NOT NULL,
MontoSinGar DECIMAL(14,4) DEFAULT (0) NOT NULL
 CONSTRAINT PK_FinCuposEmp PRIMARY KEY CLUSTERED (nAnno,Item))
CREATE TABLE TercClieSalarios ( 
IdClie VARCHAR(16) NOT NULL,
IdAgencia VARCHAR(16) DEFAULT ('0') NOT NULL,
nMes INT DEFAULT (0) NOT NULL,
nAnno INT DEFAULT (0) NOT NULL,
VrSalario MONEY DEFAULT (0) NOT NULL,
TarifDcto DECIMAL(14,4) DEFAULT (0) NOT NULL
 CONSTRAINT PK_TercClieSalarios PRIMARY KEY CLUSTERED (IdClie,IdAgencia,nMes,nAnno),
 CONSTRAINT CK_TercClieSalariosIdAgencia CHECK ((len([IdAgencia]) > 0)),
 CONSTRAINT CK_TercClieSalariosIdClie CHECK ((len([IdClie]) > 0)))
CREATE TABLE TercClieCupos ( 
nAnno INT DEFAULT (0) NOT NULL,
nMes INT DEFAULT (0) NOT NULL,
IdClie VARCHAR(16) NOT NULL,
IdAgencia VARCHAR(16) NOT NULL,
CupoGals DECIMAL(14,4) DEFAULT (0) NOT NULL,
CupoZFE DECIMAL(14,4) DEFAULT (0) NOT NULL,
CupoAdic DECIMAL(14,4) DEFAULT (0) NOT NULL
 CONSTRAINT PK_TercClieCupos PRIMARY KEY CLUSTERED (nAnno,nMes,IdClie,IdAgencia),
 CONSTRAINT CK_TercClieCuposIdAgencia CHECK ((len([IdAgencia]) > 0)),
 CONSTRAINT CK_TercClieCuposIdClie CHECK ((len([IdClie]) > 0)))
GO
ALTER TABLE FinClieSaldosLin ADD 
 CONSTRAINT FK_FinClieSaldosLin_Companias FOREIGN KEY (IdCia) REFERENCES [dbo].[Companias] (IdCia),
 CONSTRAINT FK_FinClieSaldosLin_LineasCred FOREIGN KEY (IdLinea) REFERENCES [dbo].[LineasCred] (IdLinea),
 CONSTRAINT FK_FinClieSaldosLin_Terceros FOREIGN KEY (IdClie) REFERENCES [dbo].[Terceros] (IdTercero)
ALTER TABLE Trn_FinFacturas ADD 
 CONSTRAINT FK_Trn_FinFacturas_Trn_FinPrestamo FOREIGN KEY (TipDoc,IdPrestamo,IdCia) REFERENCES [dbo].[Trn_FinPrestamo] (TipDoc,IdPrestamo,IdCia)
ALTER TABLE Trn_FinPagos ADD 
 CONSTRAINT FK_Trn_FinPagos_Trn_FinPrestamo FOREIGN KEY (TipDoc,IdPrestamo,IdCia) REFERENCES [dbo].[Trn_FinPrestamo] (TipDoc,IdPrestamo,IdCia)
ALTER TABLE TercClieSalarios ADD 
 CONSTRAINT FK_TercClieSalarios_Terceros FOREIGN KEY (IdClie) REFERENCES [dbo].[Terceros] (IdTercero)
ALTER TABLE TercClieCupos ADD 
 CONSTRAINT FK_TercClieCupos_Terceros FOREIGN KEY (IdClie) REFERENCES [dbo].[Terceros] (IdTercero)
 CREATE NONCLUSTERED INDEX IX_Trn_FinFacturasNumFactura ON Trn_FinFacturas(NumFactura)
 CREATE NONCLUSTERED INDEX IX_Trn_FinFacturasFecha ON Trn_FinFacturas(Fecha)
 CREATE NONCLUSTERED INDEX IX_Trn_FinPagosNumero ON Trn_FinPagos(Numero)
 CREATE NONCLUSTERED INDEX IX_Trn_FinPagosFecha ON Trn_FinPagos(Fecha)
