-- Migration: 023_CreateWorkOrderRoutingTable
-- Source: OLD/Database/Tables/WorkOrderRouting_Curr.sql
-- Note: Legacy file creates [dbo].[WorkOrderRouting] (no _Curr suffix - single table)
-- Changes: DATETIME -> DATETIME2(7)

IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'WorkOrderRouting' AND schema_id = SCHEMA_ID('dbo'))
BEGIN
    CREATE TABLE [dbo].[WorkOrderRouting]
    (
        [UserID] VARCHAR(40) NOT NULL,
        [BatchNumber] VARCHAR(60) NOT NULL,
        [TransactionNumber] VARCHAR(88) NOT NULL,
        [LineNumber] INT NOT NULL,
        [StepNumber] DECIMAL(7, 2) NOT NULL,
        [WorkCenterCode] VARCHAR(12) NOT NULL,
        [WorkOrderNumber] BIGINT NOT NULL,
        [RoutingType] VARCHAR(12) NULL,
        [BranchCode] VARCHAR(12) NULL,
        [StepDescription] VARCHAR(120) NULL,
        [FunctionCode] VARCHAR(60) NULL,
        [TransactionDate] DATETIME2(7) NOT NULL,
        [LastUpdateDT] DATETIME2(7) NOT NULL,
        CONSTRAINT [PK_WorkOrderRouting] PRIMARY KEY CLUSTERED([UserID], [BatchNumber], [TransactionNumber], [LineNumber], [StepNumber], [WorkCenterCode])
    );
END
GO

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_WorkOrderRouting_Lookup' AND object_id = OBJECT_ID('dbo.WorkOrderRouting'))
BEGIN
    CREATE INDEX [IX_WorkOrderRouting_Lookup] ON [dbo].[WorkOrderRouting] ([WorkOrderNumber], [WorkCenterCode], [FunctionCode]) INCLUDE([StepNumber]);
END
GO
