-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2025, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_GridColumns_get')
  DROP PROCEDURE [dbo].[itcproc_GridColumns_get]
GO
CREATE PROCEDURE [dbo].[itcproc_GridColumns_get]
  @Parm_FormName   varchar(8)
 ,@Parm_GridName   varchar(40)
 ,@Parm_Scope      varchar(1) = ''
 ,@Parm_CompName   varchar(50) = ''
 ,@Parm_UserName   varchar(50) = ''
AS
  SET NOCOUNT ON
  DECLARE @m_Scope varchar(1) = @Parm_Scope

  IF @m_Scope = '4' BEGIN
	IF 0 < (SELECT COUNT(*) FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	        AND Scope = @m_Scope AND ScopeName = @Parm_UserName AND Activated <> 0) 
    BEGIN
	  SELECT * FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	       AND Scope = @m_Scope AND ScopeName = @Parm_UserName AND Activated <> 0
	       ORDER BY Position
	  RETURN
	END
	SET @m_Scope = '3'
  END

  IF @m_Scope = '3' BEGIN
	IF 0 < (SELECT COUNT(*) FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	        AND Scope = @m_Scope AND ScopeName = @Parm_CompName AND Activated <> 0) 
    BEGIN
	  SELECT * FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	       AND Scope = @m_Scope AND ScopeName = @Parm_CompName AND Activated <> 0
	       ORDER BY Position
	  RETURN
	END
	SET @m_Scope = '2'
  END

  IF @m_Scope = '2' BEGIN
	IF 0 < (SELECT COUNT(*) FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	        AND Scope = @m_Scope AND Activated <> 0) 
    BEGIN
	  SELECT * FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	       AND Scope = @m_Scope AND Activated <> 0
	       ORDER BY Position
	  RETURN
	END
	SET @m_Scope = '1'
  END

  IF @m_Scope = '1' BEGIN
	IF 0 < (SELECT COUNT(*) FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	        AND Scope = @m_Scope AND Activated <> 0) 
    BEGIN
	  SELECT * FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	       AND Scope = @m_Scope AND Activated <> 0
	       ORDER BY Position
	  RETURN
	END
	SET @m_Scope = '0'
  END

  SELECT * FROM GridColumn WHERE FormName = @Parm_FormName AND GridName = @Parm_GridName 
	       AND Scope = '0' AND Activated <> 0 ORDER BY Position
GO

-- select * from GridColumn
-- EXECUTE [dbo].[itcproc_GridColumns_get] 'FM000053', 'ZUSATZTEXTE::BROWSE', '4', 'SA-ELITE01', 'DEFAULT'
