CREATE TABLE Trn_FcSemanas ( 
IdVehiculo VARCHAR(10) NOT NULL,
nSemana INT DEFAULT (0) NOT NULL,
NumCargue INT DEFAULT (0) NOT NULL,
TipoSem INT DEFAULT (0) NOT NULL,
Fecha SMALLDATETIME NOT NULL,
FecVence SMALLDATETIME NOT NULL,
VrCargo MONEY DEFAULT (0) NOT NULL,
VrOtros MONEY DEFAULT (0) NOT NULL,
VrPagado MONEY DEFAULT (0) NOT NULL,
TipRec VARCHAR(3) NOT NULL,
Recibo INT DEFAULT (0) NOT NULL,
IdCiaRec CHAR(2) DEFAULT ('00') NOT NULL,
FechaPago SMALLDATETIME,
Observacion VARCHAR(250),
TimeSys SMALLDATETIME NOT NULL,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_Trn_FcSemanas PRIMARY KEY CLUSTERED (IdVehiculo,nSemana),
 CONSTRAINT CK_Trn_FcSemanasIdCiaRec CHECK ((len([IdCiaRec]) > 0)),
 CONSTRAINT CK_Trn_FcSemanasIdUsuario CHECK ((len([IdUsuario]) > 0)),
 CONSTRAINT CK_Trn_FcSemanasIdVehiculo CHECK ((len([IdVehiculo]) > 0)),
 CONSTRAINT CK_Trn_FcSemanasTipRec CHECK ((len([TipRec]) > 0)))
CREATE TABLE Trn_FcAbonos ( 
TipRec VARCHAR(3) NOT NULL,
Recibo INT DEFAULT (0) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
Item INT DEFAULT (0) NOT NULL,
Fecha SMALLDATETIME NOT NULL,
IdVehiculo VARCHAR(10) NOT NULL,
nSemana INT DEFAULT (0) NOT NULL,
VrAbono MONEY DEFAULT (0) NOT NULL,
Descripcion VARCHAR(150)
 CONSTRAINT PK_Trn_FcAbonos PRIMARY KEY CLUSTERED (TipRec,Recibo,IdCia,Item),
 CONSTRAINT CK_Trn_FcAbonosIdCia CHECK ((len([IdCia]) > 0)),
 CONSTRAINT CK_Trn_FcAbonosIdVehiculo CHECK ((len([IdVehiculo]) > 0)),
 CONSTRAINT CK_Trn_FcAbonosTipRec CHECK ((len([TipRec]) > 0)))
CREATE TABLE tm_Semanas ( 
tmNumero VARCHAR(5) NOT NULL,
tmItem INT DEFAULT (0) NOT NULL,
tmVehiculo VARCHAR(10) NOT NULL,
tmSemana INT DEFAULT (0) NOT NULL,
tmVrTotal MONEY DEFAULT (0) NOT NULL,
tmVrPagado MONEY DEFAULT (0) NOT NULL,
tmDescripcion VARCHAR(150)
 CONSTRAINT PK_tm_Semanas PRIMARY KEY CLUSTERED (tmNumero,tmItem),
 CONSTRAINT CK_tm_SemanastmNumero CHECK ((len([tmNumero]) > 0)),
 CONSTRAINT CK_tm_SemanastmVehiculo CHECK ((len([tmVehiculo]) > 0)))
GO
 CREATE NONCLUSTERED INDEX IX_Trn_FcAbonosFecha ON Trn_FcAbonos(Fecha)
 CREATE NONCLUSTERED INDEX IX_Trn_FcAbonosnSemana ON Trn_FcAbonos(IdVehiculo,nSemana)
 CREATE NONCLUSTERED INDEX IX_Trn_FcSemanasNumCargue ON Trn_FcSemanas(NumCargue)
 CREATE NONCLUSTERED INDEX IX_Trn_FcSemanasFecha ON Trn_FcSemanas(Fecha)
 CREATE NONCLUSTERED INDEX IX_Trn_FcSemanasRecibo ON Trn_FcSemanas(TipRec,Recibo,IdCiaRec)
 CREATE NONCLUSTERED INDEX IX_tm_SemanastmVehiculo ON tm_Semanas(tmVehiculo,tmSemana)
ALTER TABLE Trn_FcSemanas ADD 
 CONSTRAINT FK_Trn_FcSemanas_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_Trn_FcSemanas_Vehiculos FOREIGN KEY (IdVehiculo) REFERENCES [dbo].[Vehiculos] (IdVehiculo)
ALTER TABLE Trn_FcAbonos ADD 
 CONSTRAINT FK_Trn_FcAbonos_Companias FOREIGN KEY (IdCia) REFERENCES [dbo].[Companias] (IdCia),
 CONSTRAINT FK_Trn_FcAbonos_Trn_FcSemanas FOREIGN KEY (IdVehiculo,nSemana) REFERENCES [dbo].[Trn_FcSemanas] (IdVehiculo,nSemana)
