IF NOT EXISTS (
    SELECT 1
    FROM sys.indexes
    WHERE name = 'IX_PhS_SalesDocumentDetails_Origin'
      AND object_id = OBJECT_ID('dbo.PhS_SalesDocumentDetails')
)
BEGIN
    CREATE INDEX IX_PhS_SalesDocumentDetails_Origin
        ON dbo.PhS_SalesDocumentDetails (origin_type, origin_id)
        INCLUDE (salesdocument_id, quantity);
END;
