IF NOT EXISTS
(
    SELECT COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'Sahmiyeh'
          AND COLUMN_NAME = 'TTACInsuranceCode'
)
    ALTER TABLE dbo.Sahmiyeh ADD TTACInsuranceCode VARCHAR(5) NULL;

IF NOT EXISTS
(
    SELECT TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_NAME = 'TTACPrescription'
)
BEGIN

    CREATE TABLE [dbo].[TTACPrescription]
    (
        [Id] [UNIQUEIDENTIFIER] NOT NULL,
        [Kind] [TINYINT] NULL,
        [Status] [TINYINT] NULL,
        [TurnNo] [SMALLINT] NULL
            DEFAULT (0),
        [TrackingCode] [INT] IDENTITY(1, 1) NOT NULL,
        [InsuredCode] VARCHAR(20) NOT NULL,
        [NationalCode] [VARCHAR](12) NULL,
        [BasicInsuranceCode] [VARCHAR](5) NULL,
        [InsuranceCode] [VARCHAR](3) NULL,
        [MedicationNo] [VARCHAR](10) NULL,
        [InsurerTechnicalAmount] [MONEY] NULL,
        [InsuredTechnicalAmount] [MONEY] NULL,
        [OTCTechnicalAmount] [MONEY] NULL,
        [TotalInsuredAmount] [MONEY] NULL,
        [TotalInsurerAmount] [MONEY] NULL,
        [TotalDifferenceAmount] [MONEY] NULL,
        [FinalPaidAmount] [MONEY] NULL,
        [IsDeleted] [BIT] NULL,
        [DeletedBy] [SMALLINT] NULL,
        [DeletedOn] [DATETIME] NULL,
        [CreatedBy] [SMALLINT] NULL,
        [CreatedOn] [DATETIME] NULL
            DEFAULT (GETDATE()),
        CONSTRAINT [PK_TTACPrescription]
            PRIMARY KEY CLUSTERED ([Id] ASC)
    ) ON [PRIMARY];

    ALTER TABLE [dbo].[TTACPrescription]
    ADD CONSTRAINT [DF_TTACPrescription_IsDeleted]
        DEFAULT ((0)) FOR [IsDeleted];

    ALTER TABLE [dbo].[TTACPrescription] WITH CHECK
    ADD CONSTRAINT [FK_TTACPrescription_Sahmiyeh]
        FOREIGN KEY ([InsuranceCode])
        REFERENCES [dbo].[Sahmiyeh] ([Sazman_Code]);
    ALTER TABLE [dbo].[TTACPrescription] CHECK CONSTRAINT [FK_TTACPrescription_Sahmiyeh];
END;

IF NOT EXISTS
(
    SELECT TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_NAME = 'TTACPrescriptionDetail'
)
BEGIN
    CREATE TABLE [dbo].[TTACPrescriptionDetail]
    (
        [Id] [UNIQUEIDENTIFIER] NOT NULL,
        [PrescriptionId] [UNIQUEIDENTIFIER] NULL,
        [RowIndex] [TINYINT] NULL,
        [GoodsCode] [VARCHAR](15) NULL,
        [GenericCode] [NCHAR](6) NULL,
        [CoverCount] [FLOAT] NULL,
        [DeliverCount] [FLOAT] NULL,
        [StatusCode] [TINYINT] NULL
            DEFAULT (0),
        [IsAttached] [BIT] NULL
            DEFAULT (0),
        [HasTTAC] [BIT] NULL
            DEFAULT (0),
        [CheckInventory] [BIT] NULL,
        [Franshiz] [FLOAT] NULL,
        [SalesPriceAmount] [MONEY] NULL
            DEFAULT (0),
        [DifferenceAmount] [MONEY] NULL
            DEFAULT (0),
        [InsuredAmount] [MONEY] NULL
            DEFAULT (0),
        [InsurerAmount] [MONEY] NULL
            DEFAULT (0),
        [TotalAmount] [MONEY] NULL,
        [CreatedOn] [DATETIME] NULL
            DEFAULT (GETDATE()),
        CONSTRAINT [PK_TTACPrescriptionDetail]
            PRIMARY KEY CLUSTERED ([Id] ASC)
    ) ON [PRIMARY];

    ALTER TABLE [dbo].[TTACPrescriptionDetail] WITH CHECK
    ADD CONSTRAINT [FK_TTACPrescriptionDetail_KalaId]
        FOREIGN KEY ([GoodsCode])
        REFERENCES [dbo].[KalaId] ([K_Code]);


    ALTER TABLE [dbo].[TTACPrescriptionDetail] WITH CHECK
    ADD CONSTRAINT [FK_TTACPrescriptionDetail_TTACPrescription]
        FOREIGN KEY ([PrescriptionId])
        REFERENCES [dbo].[TTACPrescription] ([Id]) ON DELETE CASCADE;

    ALTER TABLE [dbo].[TTACPrescriptionDetail] CHECK CONSTRAINT [FK_TTACPrescriptionDetail_TTACPrescription];

END;


IF NOT EXISTS
(
    SELECT TABLE_NAME
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_NAME = 'TTACPrescriptionDetailUID'
)
BEGIN
    CREATE TABLE [dbo].[TTACPrescriptionDetailUID]
    (
        [Id] [UNIQUEIDENTIFIER] NOT NULL
            DEFAULT (NEWID()),
        [PrescriptionDetailId] [UNIQUEIDENTIFIER] NULL,
        [InqueryPrescriptionId] [INT] NULL,
        [GenericCode] [VARCHAR](6) NULL,
        [Amount] [FLOAT] NULL,
        [UID] [NVARCHAR](MAX) NULL,
        [TrackingCode] [INT] NULL,
        [IsTemporarySaved] [BIT] NULL
            DEFAULT (0),
        [GenericName] [NVARCHAR](MAX),
        [PersianName] [NVARCHAR](MAX),
        [IRC] [VARCHAR](16),
        [UnitPrice] [MONEY],
        [Expiration] [VARCHAR](50),
        [BatchCode] [VARCHAR](50),
        [CreatedBy] [SMALLINT],
        [CreatedOn] [DATETIME] NULL
            DEFAULT (GETDATE()),
        CONSTRAINT [PK_TTACPrescriptionDetailUID]
            PRIMARY KEY CLUSTERED ([Id] ASC)
    ) ON [PRIMARY];


    ALTER TABLE [dbo].[TTACPrescriptionDetailUID] WITH CHECK
    ADD CONSTRAINT [FK_TTACPrescriptionDetailUID_TTACPrescriptionDetail]
        FOREIGN KEY ([PrescriptionDetailId])
        REFERENCES [dbo].[TTACPrescriptionDetail] ([Id]) ON DELETE CASCADE;

    ALTER TABLE [dbo].[TTACPrescriptionDetailUID] CHECK CONSTRAINT [FK_TTACPrescriptionDetailUID_TTACPrescriptionDetail];
END;