CREATE TABLE TiposTerc ( 
IdTipoTerc VARCHAR(4) NOT NULL,
TipoTercero VARCHAR(100) NOT NULL,
Inactivo BIT DEFAULT (0) NOT NULL
 CONSTRAINT PK_TiposTerc PRIMARY KEY CLUSTERED (IdTipoTerc),
 CONSTRAINT CK_TiposTercIdTipoTerc CHECK ((len([IdTipoTerc]) > 0)),
 CONSTRAINT CK_TiposTercTipoTercero CHECK ((len([TipoTercero]) > 0)))
GO
CREATE TABLE LineasCred ( 
IdLinea VARCHAR(4) NOT NULL,
LinCredito VARCHAR(100) NOT NULL,
Inactivo BIT DEFAULT (0) NOT NULL
 CONSTRAINT PK_LineasCred PRIMARY KEY CLUSTERED (IdLinea),
 CONSTRAINT CK_LineasCredIdLinea CHECK ((len([IdLinea]) > 0)),
 CONSTRAINT CK_LineasCredLinCredito CHECK ((len([LinCredito]) > 0)))
GO
CREATE TABLE TercCliePrestamo ( 
IdClie VARCHAR(16) NOT NULL,
IdAgencia VARCHAR(16) DEFAULT ('0') NOT NULL,
IdSzona VARCHAR(4) NOT NULL,
IdGrupo VARCHAR(4) NOT NULL,
IdTipoTerc VARCHAR(4) NOT NULL,
IdVend VARCHAR(16) NOT NULL,
IdClase VARCHAR(4) NOT NULL,
NumCuenta VARCHAR(30),
IdBanco VARCHAR(4) NOT NULL,
NomAgencia VARCHAR(150),
DirAgencia VARCHAR(250),
IdLocalAge VARCHAR(8) NOT NULL,
NitRepLeg VARCHAR(16),
NomRepLeg VARCHAR(150),
NitContac VARCHAR(16),
NomContac VARCHAR(150),
TelContac VARCHAR(20),
emlContac VARCHAR(100),
CargContac VARCHAR(50),
EsExento BIT DEFAULT (0) NOT NULL,
CalcIntMora BIT DEFAULT (0) NOT NULL,
DiasGracia INT DEFAULT (0) NOT NULL,
FecIngreso SMALLDATETIME NOT NULL,
FecVigencia SMALLDATETIME,
FecRetiro SMALLDATETIME,
VrSalBasico MONEY DEFAULT (0) NOT NULL,
MaxDcto DECIMAL(14,4) DEFAULT (0) NOT NULL,
MatMerc VARCHAR(20),
FecMat SMALLDATETIME,
Comentarios VARCHAR(2000),
IdEstado VARCHAR(4) NOT NULL,
Inactivo BIT DEFAULT (0) NOT NULL,
FechaCrea SMALLDATETIME NOT NULL,
FechaAct SMALLDATETIME,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_TercCliePrestamo PRIMARY KEY CLUSTERED (IdClie,IdAgencia),
 CONSTRAINT CK_TercCliePrestamoIdAgencia CHECK ((len([IdAgencia]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdBanco CHECK ((len([IdBanco]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdClase CHECK ((len([IdClase]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdClie CHECK ((len([IdClie]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdEstado CHECK ((len([IdEstado]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdGrupo CHECK ((len([IdGrupo]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdLocalAge CHECK ((len([IdLocalAge]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdSzona CHECK ((len([IdSzona]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdTipoTerc CHECK ((len([IdTipoTerc]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdUsuario CHECK ((len([IdUsuario]) > 0)),
 CONSTRAINT CK_TercCliePrestamoIdVend CHECK ((len([IdVend]) > 0)))
GO
CREATE TABLE TercClieLineas ( 
IdClie VARCHAR(16) NOT NULL,
IdAgencia VARCHAR(16) DEFAULT ('0') NOT NULL,
IdLinea VARCHAR(4) NOT NULL,
IdTasa VARCHAR(4) NOT NULL,
nPlazo INT DEFAULT (0) NOT NULL,
TipoPlazo CHAR(1) DEFAULT ('D') NOT NULL,
CupoCredito MONEY DEFAULT (0) NOT NULL,
SaldoCapital MONEY DEFAULT (0) NOT NULL,
SaldoInteres MONEY DEFAULT (0) NOT NULL,
MaxCupoSmlv DECIMAL(14,4) DEFAULT (0) NOT NULL,
TipoCausac VARCHAR(10) DEFAULT ('MENSUAL') NOT NULL,
DiasVence VARCHAR(20),
FecPreAprob SMALLDATETIME,
PlazoAprob INT DEFAULT (0) NOT NULL,
EtdoAprob INT DEFAULT (0) NOT NULL,
TipoAprob VARCHAR(20),
DescAprob VARCHAR(250),
Garantias BIT DEFAULT (0) NOT NULL,
TipoGarantia VARCHAR(10),
DescGarantia VARCHAR(250),
VrGarantia MONEY DEFAULT (0) NOT NULL,
ReqCheques BIT DEFAULT (0) NOT NULL,
FecActCupo SMALLDATETIME,
EstadoAct INT DEFAULT (0) NOT NULL,
FechaCrea SMALLDATETIME NOT NULL,
FechaAct SMALLDATETIME,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_TercClieLineas PRIMARY KEY CLUSTERED (IdClie,IdAgencia,IdLinea),
 CONSTRAINT CK_TercClieLineasIdAgencia CHECK ((len([IdAgencia]) > 0)),
 CONSTRAINT CK_TercClieLineasIdClie CHECK ((len([IdClie]) > 0)),
 CONSTRAINT CK_TercClieLineasIdLinea CHECK ((len([IdLinea]) > 0)),
 CONSTRAINT CK_TercClieLineasIdTasa CHECK ((len([IdTasa]) > 0)),
 CONSTRAINT CK_TercClieLineasIdUsuario CHECK ((len([IdUsuario]) > 0)),
 CONSTRAINT CK_TercClieLineasTipoCausac CHECK ((len([TipoCausac]) > 0)),
 CONSTRAINT CK_TercClieLineasTipoPlazo CHECK ((len([TipoPlazo]) > 0)))
GO
CREATE TABLE TercCliePreRef ( 
IdClie VARCHAR(16) NOT NULL,
IdAgencia VARCHAR(16) NOT NULL,
Item INT DEFAULT (0) NOT NULL,
TipoRef VARCHAR(10) NOT NULL,
Referencia VARCHAR(150) NOT NULL,
NitRefncia VARCHAR(16),
DirRefncia VARCHAR(150),
TelRefncia VARCHAR(30) NOT NULL,
emlRefncia VARCHAR(100),
CargRefncia VARCHAR(50),
NumCuenta VARCHAR(30),
Observcion VARCHAR(250)
 CONSTRAINT PK_TercCliePreRef PRIMARY KEY CLUSTERED (IdClie,IdAgencia,Item),
 CONSTRAINT CK_TercCliePreRefIdAgencia CHECK ((len([IdAgencia]) > 0)),
 CONSTRAINT CK_TercCliePreRefIdClie CHECK ((len([IdClie]) > 0)),
 CONSTRAINT CK_TercCliePreRefReferencia CHECK ((len([Referencia]) > 0)),
 CONSTRAINT CK_TercCliePreRefTelRefncia CHECK ((len([TelRefncia]) > 0)),
 CONSTRAINT CK_TercCliePreRefTipoRef CHECK ((len([TipoRef]) > 0)))
GO
ALTER TABLE TercCliePreRef ADD 
 CONSTRAINT FK_TercCliePreRef_TercCliePrestamo FOREIGN KEY (IdClie,IdAgencia) REFERENCES [dbo].[TercCliePrestamo] (IdClie,IdAgencia)
ALTER TABLE TercCliePrestamo ADD 
 CONSTRAINT FK_TercCliePrestamo_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_TercCliePrestamo_EstadoTer FOREIGN KEY (IdEstado) REFERENCES [dbo].[EstadoTer] (IdEstado),
 CONSTRAINT FK_TercCliePrestamo_GruposCli FOREIGN KEY (IdGrupo) REFERENCES [dbo].[GruposCli] (IdGrupo),
 CONSTRAINT FK_TercCliePrestamo_Localidades FOREIGN KEY (IdLocalAge) REFERENCES [dbo].[Localidades] (IdLocal),
 CONSTRAINT FK_TercCliePrestamo_Subzonas FOREIGN KEY (IdSzona) REFERENCES [dbo].[Subzonas] (IdSzona),
 CONSTRAINT FK_TercCliePrestamo_Terceros FOREIGN KEY (IdClie) REFERENCES [dbo].[Terceros] (IdTercero),
 CONSTRAINT FK_TercCliePrestamo_Terceros1 FOREIGN KEY (IdVend) REFERENCES [dbo].[Terceros] (IdTercero),
 CONSTRAINT FK_TercCliePrestamo_TiposTerc FOREIGN KEY (IdTipoTerc) REFERENCES [dbo].[TiposTerc] (IdTipoTerc)
ALTER TABLE TercClieLineas ADD 
 CONSTRAINT FK_TercClieLineas_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_TercClieLineas_LineasCred FOREIGN KEY (IdLinea) REFERENCES [dbo].[LineasCred] (IdLinea),
 CONSTRAINT FK_TercClieLineas_TasasInteres FOREIGN KEY (IdTasa) REFERENCES [dbo].[TasasInteres] (IdTasa),
 CONSTRAINT FK_TercClieLineas_Terceros FOREIGN KEY (IdClie) REFERENCES [dbo].[Terceros] (IdTercero)
