USE [Clinica]
GO

-- Crear esquemas faltantes
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'Imed')
    EXEC('CREATE SCHEMA [Imed]')
GO

IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'Reportes')
    EXEC('CREATE SCHEMA [Reportes]')
GO

-- Crear Caja.TipoEstadoMovimientoCaja (primero por FK)
CREATE TABLE [Caja].[TipoEstadoMovimientoCaja](
	[IdTipoEstadoMovimientoCaja] [int] IDENTITY(1,1) NOT NULL,
	[Descripcion] [varchar](50) NULL,
	PRIMARY KEY CLUSTERED ([IdTipoEstadoMovimientoCaja] ASC)
)
GO

-- Crear Clinica.EstadoImed
CREATE TABLE [Clinica].[EstadoImed](
	[IdEstadoImed] [int] NOT NULL,
	[Descripcion] [varchar](50) NULL,
	PRIMARY KEY CLUSTERED ([IdEstadoImed] ASC)
)
GO

-- Crear Caja.AbonoAtencion
CREATE TABLE [Caja].[AbonoAtencion](
	[IdAbono] [int] IDENTITY(1,1) NOT NULL,
	[IdAtencion] [int] NOT NULL,
	[FechaHora] [datetime] NULL,
	[Monto] [decimal](16, 2) NULL,
	[IdUsuario] [int] NOT NULL,
	[IndEliminado] [tinyint] NOT NULL DEFAULT ((0)),
	PRIMARY KEY CLUSTERED ([IdAbono] ASC),
	FOREIGN KEY([IdAtencion]) REFERENCES [Clinica].[Atencion] ([IdAtencion]),
	FOREIGN KEY([IdUsuario]) REFERENCES [Core].[Usuario] ([IdUsuario])
)
GO

-- Crear Caja.PresupuestoAtencion
CREATE TABLE [Caja].[PresupuestoAtencion](
	[IdPresupuestoAtencion] [int] IDENTITY(1,1) NOT NULL,
	[IdAtencion] [int] NOT NULL,
	[Monto] [decimal](16, 2) NULL,
	[IndPagado] [tinyint] NOT NULL DEFAULT ((0)),
	[IndEliminado] [tinyint] NOT NULL DEFAULT ((0)),
	PRIMARY KEY CLUSTERED ([IdPresupuestoAtencion] ASC),
	FOREIGN KEY([IdAtencion]) REFERENCES [Clinica].[Atencion] ([IdAtencion])
)
GO

-- Crear Clinica.CitaFichaEvolucionExamen
CREATE TABLE [Clinica].[CitaFichaEvolucionExamen](
	[IdCitaFichaEvolucionExamen] [int] IDENTITY(1,1) NOT NULL,
	[IdCita] [int] NOT NULL,
	[IdFichaEvolucion] [int] NOT NULL,
	[IdFichaEvolucionExamen] [int] NOT NULL,
	PRIMARY KEY CLUSTERED ([IdCitaFichaEvolucionExamen] ASC)
)
GO

-- Crear Clinica.Evidencia
CREATE TABLE [Clinica].[Evidencia](
	[IdEvidencia] [int] IDENTITY(1,1) NOT NULL,
	[IdTipoEvidencia] [int] NULL,
	[IdCita] [int] NOT NULL,
	[RutaArchivo] [varchar](300) NOT NULL,
	[NombreArchivo] [varchar](250) NOT NULL,
	[NombreArchivoReal] [varchar](250) NULL,
	[IndVigente] [int] NOT NULL,
	CONSTRAINT [PK_Evidencia] PRIMARY KEY CLUSTERED ([IdEvidencia] ASC)
)
GO

-- Crear Imed.BonoInterfaz
CREATE TABLE [Imed].[BonoInterfaz](
	[IdBonoInterfaz] [int] IDENTITY(1,1) NOT NULL,
	[IdCita] [int] NOT NULL,
	[CodFinanciador] [int] NOT NULL,
	[RutConvenio] [varchar](15) NOT NULL,
	[CorrConvenio] [int] NOT NULL,
	[RutTratante] [varchar](15) NOT NULL,
	[RutSolic] [varchar](15) NULL,
	[NomSolic] [nvarchar](100) NULL,
	[CodEspecia] [varchar](10) NULL,
	[RutBenef] [varchar](15) NOT NULL,
	[IndUrgencia] [char](1) NOT NULL DEFAULT ('N'),
	[CodError] [int] NOT NULL DEFAULT ((0)),
	[GloError] [nvarchar](255) NOT NULL DEFAULT ('OK'),
	[FechaCreacion] [datetime2](0) NOT NULL DEFAULT (sysdatetime()),
	[UsuarioCreacion] [sysname] NOT NULL DEFAULT (suser_sname()),
	PRIMARY KEY CLUSTERED ([IdBonoInterfaz] ASC)
)
GO

-- Crear Imed.ListaBono
CREATE TABLE [Imed].[ListaBono](
	[IdListaBono] [int] IDENTITY(1,1) NOT NULL,
	[IdBonoInterfaz] [int] NOT NULL,
	[FecEmi] [datetime] NOT NULL,
	[FolioBono] [bigint] NOT NULL,
	[MontoAfecto] [int] NOT NULL,
	[MontoExento] [int] NOT NULL,
	[MontoTotal] [int] NOT NULL,
	[NumBoleta] [int] NOT NULL,
	[NumPrestacion] [int] NOT NULL,
	PRIMARY KEY CLUSTERED ([IdListaBono] ASC)
)
GO

-- Crear Imed.ListaPrestVta
CREATE TABLE [Imed].[ListaPrestVta](
	[IdListaPrestVta] [int] IDENTITY(1,1) NOT NULL,
	[IdListaBono] [int] NOT NULL,
	[Cantidad] [int] NOT NULL,
	[CodCanasta] [varchar](500) NULL,
	[CodIntSanitaria] [varchar](500) NULL,
	[CodItem] [varchar](2) NOT NULL,
	[CodPatologia] [varchar](500) NULL,
	[CodPrestacion] [varchar](50) NOT NULL,
	[EsGes] [char](1) NOT NULL,
	[MontoBon] [int] NOT NULL,
	[MontoCopago] [int] NOT NULL,
	[MontoPrest] [int] NOT NULL,
	[NumPieza] [varchar](50) NULL,
	[RecargoHora] [char](1) NOT NULL,
	PRIMARY KEY CLUSTERED ([IdListaPrestVta] ASC)
)
GO

-- Crear Imed.ListaOtrasBon
CREATE TABLE [Imed].[ListaOtrasBon](
	[IdListaOtrasBon] [int] IDENTITY(1,1) NOT NULL,
	[IdListaPrestVta] [int] NOT NULL,
	[CodBonAdic] [int] NOT NULL,
	[GloBonAdic] [varchar](100) NULL,
	[MtoBonAdic] [int] NOT NULL,
	PRIMARY KEY CLUSTERED ([IdListaOtrasBon] ASC)
)
GO

-- Actualizar Caja.MovimientoCaja (agregar columnas faltantes)
ALTER TABLE [Caja].[MovimientoCaja] ADD 
    [IdBanco] [int] NULL,
    [NroCuentaCheque] [int] NULL,
    [NroSerieCheque] [int] NULL,
    [FechaEmisionCheque] [date] NULL,
    [NumeroGarantia] [int] NULL,
    [IdCita] [int] NULL,
    [IdIsapre] [int] NULL,
    [NroBoleta] [varchar](50) NULL,
    [IdTipoEstadoMovimientoCaja] [int] NOT NULL DEFAULT(1)
GO

ALTER TABLE [Caja].[MovimientoCaja] WITH CHECK ADD CONSTRAINT [FK_MovimientoCaja_TipoEstadoMovimientoCaja] 
FOREIGN KEY([IdTipoEstadoMovimientoCaja]) REFERENCES [Caja].[TipoEstadoMovimientoCaja] ([IdTipoEstadoMovimientoCaja])
GO

-- Actualizar Clinica.Cita (agregar columnas faltantes)
ALTER TABLE [Clinica].[Cita] ADD 
    [IdUsuario] [int] NULL,
    [CodTransac] [varchar](20) NULL,
    [CodAuditoria] [varchar](50) NULL,
    [Token] [varchar](max) NULL,
    [IdEstadoImed] [int] NULL,
    [IdFichaEvolucion] [int] NULL,
    [IndCitaControl] [tinyint] NULL
GO

-- Eliminar tablas obsoletas
IF OBJECT_ID('dbo.tmp_ordenes', 'U') IS NOT NULL DROP TABLE dbo.tmp_ordenes
IF OBJECT_ID('dbo.pdfrut', 'U') IS NOT NULL DROP TABLE dbo.pdfrut  
IF OBJECT_ID('dbo.pdf', 'U') IS NOT NULL DROP TABLE dbo.pdf
GO