﻿USE [eDosLocal];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO

IF NOT EXISTS (SELECT * FROM sys.columns WHERE OBJECT_ID=OBJECT_ID('dbo.visit') AND name='eDosBatteryWarning')
begin
ALTER TABLE [dbo].[Visit] ADD eDosBatteryWarning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose07Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose07Alarm BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate07Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate07Alarm BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose10Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosDose10Alarm BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate10Warning BIT null;
ALTER TABLE [dbo].[Visit] ADD eDosRate10Alarm BIT null;
END 


IF NOT EXISTS (SELECT * FROM [sys].[columns] WHERE [object_id] = OBJECT_ID('dbo.visit') AND [name] = 'isFault')
BEGIN
    ALTER TABLE [dbo].[Visit] ADD [isFault] BIT NULL;
END;

GO
IF OBJECT_ID('dbo.PersonAlarms') IS NULL BEGIN
    CREATE TABLE [dbo].[PersonAlarms] ([PersID] INT NOT NULL,
    [VisitTime] DATETIME NOT NULL,
    [ID_Station] INT NOT NULL,
    [ID_Visit] INT NOT NULL,
    PRIMARY KEY NONCLUSTERED([PersID], [VisitTime]));
END;
GO
ALTER PROCEDURE [dbo].[VisitExit] @EPDVisitID INT,
    @HP07 FLOAT,
    @HP10 FLOAT,
    @HP07Peak FLOAT,
    @HP10Peak FLOAT,
    @HP07PeakTime DATETIME = NULL,
    @HP10PeakTime DATETIME = NULL,
    @HP07Total FLOAT,
    @HP10Total FLOAT,
    @K1 INT,
    @K2 INT,
    @K3 INT,
    @K4 INT,
    @K5 INT = NULL,
    @K6 INT = NULL,
    @SG INT,
    @BC INT,
    @FB INT,
    @HG INT,
    @QualityData INT,
    @alarmStatus INT,
    @otherAlarms INT,
    @errorStatus INT,
    @operatingStatus INT,
    @ENTRY_EPD_CLOCK DECIMAL(10, 0) NULL,
    @EXIT_EPD_CLOCK DECIMAL(10, 0) NULL,
    @PersID INT = NULL,
    @eDosBatteryWarning BIT = NULL,
    @eDosDose07Warning BIT = NULL,
    @eDosDose07Alarm BIT = NULL,
    @eDosRate07Warning BIT = NULL,
    @eDosRate07Alarm BIT = NULL,
    @eDosDose10Warning BIT = NULL,
    @eDosDose10Alarm BIT = NULL,
    @eDosRate10Warning BIT = NULL,
    @eDosRate10Alarm BIT = NULL,
    @isAlarm BIT = NULL,
    @isFault BIT = NULL
AS BEGIN
    DECLARE
        @ID_Visit INT,
        @ID_Station INT,
        @ID_Station_Return INT;

    SET @ID_Station = @EPDVisitID % 100;
    SET @ID_Visit = @EPDVisitID / 100;

    IF @isAlarm IS NULL
        SET @isAlarm = CASE WHEN ISNULL(@alarmStatus, 0) <> 0
                                 OR (ISNULL(@otherAlarms, 0) & 0x7f) <> 0 --bit 7=battery low
                                 OR ISNULL(@eDosDose07Warning, 0) <> 0
                                 OR ISNULL(@eDosRate07Warning, 0) <> 0
                                 OR ISNULL(@eDosDose10Warning, 0) <> 0
                                 OR ISNULL(@eDosRate10Warning, 0) <> 0
                                 OR ISNULL(@eDosDose07Alarm, 0) <> 0
                                 OR ISNULL(@eDosRate07Alarm, 0) <> 0
                                 OR ISNULL(@eDosDose10Alarm, 0) <> 0
                                 OR ISNULL(@eDosRate10Alarm, 0) <> 0 THEN 1 ELSE 0 END;

    DECLARE @Now DATETIME = GETDATE();

    IF @ID_Station_Return IS NULL
        SELECT
            @ID_Station_Return = [ID_Station]
        FROM [dbo].[StationSettings]
        WHERE [PCNr] = HOST_NAME();

    DECLARE @isComplete BIT = 1;

    IF NOT EXISTS (SELECT
                       *
                   FROM [dbo].[Visit]
                   WHERE [ID_Visit] = @ID_Visit
                         AND [ID_Station] = @ID_Station)BEGIN
        SET @isComplete = 0;

        INSERT [dbo].[Visit]([ID_Station], [ID_Visit], [Inst_CODE], [isManualExit], [isDeleted], [ID_Station_Return], [isComplete], [ENTRY_EPD_CLOCK], [PersID])
        VALUES(@ID_Station, @ID_Visit, 0, 0, 0, @ID_Station_Return, 0, @ENTRY_EPD_CLOCK, @PersID);
    END;

    UPDATE
        [dbo].[Visit]
    SET
        [Hp07Dose] = @HP07,
        [Hp10Dose] = @HP10,
        [isComplete] = @isComplete,
        [ReturnTime] = @Now,
        [Hp07Peak] = @HP07Peak,
        [Hp10Peak] = @HP10Peak,
        [Hp07Time] = @HP07PeakTime,
        [Hp10Time] = @HP10PeakTime,
        [Hp07Total] = @HP07Total,
        [Hp10Total] = @HP10Total,
        [K1] = @K1,
        [K2] = @K2,
        [K3] = @K3,
        [K4] = @K4,
        [K5] = @K5,
        [K6] = @K6,
        [SG] = @SG,
        [BC] = @BC,
        [FB] = @FB,
        [HG] = @HG,
        [QualityData] = @QualityData,
        [alarmStatus] = @alarmStatus,
        [otherAlarms] = @otherAlarms,
        [errorStatus] = @errorStatus,
        [operatingStatus] = @operatingStatus,
        --[D1] = @D1,    [D2] = @D2,    [D3] = @D3,    [D4] = @D4,
        [isAlarm] = @isAlarm,
        [isManualExit] = 0,
        [ID_Station_Return] = @ID_Station_Return,
        [ENTRY_EPD_CLOCK] = @ENTRY_EPD_CLOCK,
        [EXIT_EPD_CLOCK] = @EXIT_EPD_CLOCK,
        [PersID] = CASE WHEN [PersID] IS NULL THEN @PersID ELSE [PersID] END,
        [eDosBatteryWarning] = @eDosBatteryWarning,
        [eDosDose07Warning] = @eDosDose07Warning,
        [eDosDose07Alarm] = @eDosDose07Alarm,
        [eDosRate07Warning] = @eDosRate07Warning,
        [eDosRate07Alarm] = @eDosRate07Alarm,
        [eDosDose10Warning] = @eDosDose10Warning,
        [eDosDose10Alarm] = @eDosDose10Alarm,
        [eDosRate10Warning] = @eDosRate10Warning,
        [eDosRate10Alarm] = @eDosRate10Alarm,
        [isFault] = @isFault
    WHERE [ID_Visit] = @ID_Visit
          AND [ID_Station] = @ID_Station;

    -- local- >PersonInfoSnapshot
    IF @isAlarm = 1 BEGIN
        IF @PersID IS NULL
            SELECT
                @PersID = [PersID]
            FROM [dbo].[Visit]
            WHERE [ID_Visit] = @ID_Visit
                  AND [ID_Station] = @ID_Station;

        UPDATE
            [dbo].[PersonInfoSnapshot]
        SET
            [VisitIDLocked] = @ID_Visit * 100 + @ID_Station
        WHERE [PersID] = @PersID;

        IF NOT EXISTS (SELECT
                           *
                       FROM [dbo].[PersonAlarms]
                       WHERE [PersID] = @PersID
                             AND [VisitTime] = @Now)
            INSERT [dbo].[PersonAlarms]([PersID], [VisitTime], [ID_Station], [ID_Visit])
            VALUES(@PersID, @Now, @ID_Station, @ID_Visit);
    END;
END;

GO
ALTER PROCEDURE [export].[VisitsSelect]
AS
SELECT
    [ID_Station],
    [ID_Visit],
    [Inst_CODE],
    [PersID],
    [ID_Task],
    [EntryTime],
    [ReturnTime],
    [Hp10Dose],
    [Hp07Dose],
    [Hp10Total],
    [Hp07Total],
    [Hp10Peak],
    [Hp07Peak],
    [Hp10Time],
    [Hp07Time],
    [ID_EPD],
    [QualityData],
    [K1],
    [K2],
    [K3],
    [K4],
    [K5],
    [K6],
    [SG],
    [BC],
    [FB],
    [HG],
    [alarmStatus],
    [otherAlarms],
    [errorStatus],
    [operatingStatus],
    [isBG],
    [isComplete],
    [isDeleted],
    [isAlarm],
    [isAlarmCleared],
    [isManualExit],
    [ID_Station_Return],
    [ENTRY_EPD_CLOCK],
    [EXIT_EPD_CLOCK],
    [TIME_LOST],
    [VisitStatus] = CONVERT(TINYINT, CASE WHEN [EntryTime] IS NOT NULL
                                               AND [ReturnTime] IS NOT NULL THEN 3
                                     WHEN [EntryTime] IS NOT NULL THEN 1
                                     WHEN [ReturnTime] IS NOT NULL THEN 2 ELSE 0 END),
    [syncedEntry],
    [syncedExit],
    [epdType],
    [epdID],
    [eDosBatteryWarning],
    [eDosDose07Warning],
    [eDosDose07Alarm],
    [eDosRate07Warning],
    [eDosRate07Alarm],
    [eDosDose10Warning],
    [eDosDose10Alarm],
    [eDosRate10Warning],
    [eDosRate10Alarm]
FROM [dbo].[Visit]
WHERE([syncedEntry] = 0 OR [syncedExit] = 0);
GO
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[StationSettings]') AND name='RestartComFirst')
begin
ALTER TABLE [dbo].[StationSettings] ADD [RestartComFirst] BIT;
ALTER TABLE [dbo].[StationSettings] ADD [OnlyMK2] BIT;
END
GO
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[StationSettings]') AND name='AllowMultipleEPD')
ALTER TABLE [dbo].[StationSettings] ADD AllowMultipleEPD BIT NULL;
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[StationSettings]') AND name='EPDRetries')
ALTER TABLE [dbo].[StationSettings] ADD EPDRetries INT NULL;
IF EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[StationSettings]') AND name='EPDRetries' AND [system_type_id]<>56)
ALTER TABLE [dbo].[StationSettings] ALTER COLUMN [EPDREtries] INT

IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[StationSettings]') AND name = 'WriteConfig')
    ALTER TABLE [dbo].[StationSettings] ADD [WriteConfig] BIT NULL;

IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[StationSettings]') AND name = 'IssueWhenDoseAlarms')
    ALTER TABLE [dbo].[StationSettings] ADD [IssueWhenDoseAlarms] BIT NULL;

IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[StationSettings]') AND name = 'ElatecPort')
    ALTER TABLE [dbo].[StationSettings] ADD [ElatecPort] INT NULL;
GO
ALTER PROCEDURE [dbo].[StationSettingsSelect] AS
SELECT ID_Station,
       PCNr,
       ComPort,
       Inst_CODE1,
       Inst_CODE2,
       Inst_Name1,
       Inst_Name2,
       TestCSN,
       Reader,
       useTestButtons = ISNULL(useTestButtons, 0),
       useBeep = ISNULL(useBeep, 22),
       checkUser = ISNULL(checkUser, 0),
       EPDTimeout = ISNULL(EPDTimeout, 10000),
       EPDRetries = ISNULL(EPDREtries, 1),
       RestartCom = ISNULL(RestartCom, 0),
       RestartComFirst = ISNULL(RestartComFirst, 0),
       OnlyMK2 = ISNULL(OnlyMK2, 0),
       AllowMultipleEPD = ISNULL(AllowMultipleEPD, 0),
       WriteConfig = ISNULL(WriteConfig, 0),
       IssueWhenDoseAlarms = ISNULL(IssueWhenDoseAlarms, 0),
       ElatecPort
FROM dbo.StationSettings
WHERE PCNr = HOST_NAME();
GO
USE eDosLocal;
GO
/****** Object:  StoredProcedure [dbo].[StationSettingsUpdate]    Script Date: 2026-08-28 12:18:04 ******/
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
ALTER PROCEDURE dbo.StationSettingsUpdate
    @ComPort INT = NULL,
    @Inst_CODE1 INT = NULL,
    @Inst_CODE2 INT = NULL,
    @Inst_Name1 VARCHAR(50) = NULL,
    @Inst_Name2 VARCHAR(50) = NULL,
    @TestCSN VARCHAR(50) = NULL,
    @Reader VARCHAR(70) = NULL,
    @useTestButtons BIT = 0,
    @useBeep TINYINT = 22,
    @checkUser BIT = 0,
    @EPDTimeout INT = 10000,
    @EPDRetries INT = 1,
    @RestartCom BIT = 0,
    @RestartComFirst BIT = 0,
    @OnlyMK2 BIT = 0,
    @AllowMultipleEPD BIT = 0,
    @WriteConfig BIT = 1,
    @IssueWhenDoseAlarms BIT = 0,
    @ElatecPort INT = NULL
AS
IF NOT EXISTS (SELECT * FROM dbo.StationSettings WHERE PCNr = HOST_NAME())
    INSERT dbo.StationSettings
    (
        PCNr,
        ComPort,
        Inst_CODE1,
        Inst_CODE2,
        Inst_Name1,
        Inst_Name2,
        TestCSN,
        Reader,
        useTestButtons,
        useBeep,
        checkUser,
        EPDREtries,
        RestartCom,
        RestartComFirst,
        OnlyMK2,
        AllowMultipleEPD,
        WriteConfig,
        IssueWhenDoseAlarms,
        ElatecPort
    )
    VALUES
    (HOST_NAME(), @ComPort, @Inst_CODE1, @Inst_CODE2, @Inst_Name1, @Inst_Name2, @TestCSN, @Reader, @useTestButtons,
     @useBeep, @checkUser, @EPDRetries, @RestartCom, @RestartComFirst, @OnlyMK2, @AllowMultipleEPD, @WriteConfig,
     @IssueWhenDoseAlarms, @ElatecPort);
ELSE
    UPDATE dbo.StationSettings
    SET ComPort = @ComPort,
        Inst_CODE1 = @Inst_CODE1,
        Inst_CODE2 = @Inst_CODE2,
        Inst_Name1 = @Inst_Name1,
        Inst_Name2 = @Inst_Name2,
        TestCSN = @TestCSN,
        Reader = @Reader,
        useTestButtons = @useTestButtons,
        useBeep = @useBeep,
        checkUser = @checkUser,
        EPDTimeout = @EPDTimeout,
        EPDREtries = @EPDRetries,
        RestartCom = @RestartCom,
        RestartComFirst = @RestartComFirst,
        OnlyMK2 = @OnlyMK2,
        AllowMultipleEPD = @AllowMultipleEPD,
        WriteConfig = @WriteConfig,
        IssueWhenDoseAlarms = @IssueWhenDoseAlarms,
        ElatecPort = @ElatecPort
    WHERE PCNr = HOST_NAME();

go


CREATE OR ALTER PROCEDURE [dbo].[VisitInfo] @EPDVisitID INT = 51905,
    @hasExited BIT = 0 OUT,
    @isManualExit BIT = 0 OUT,
    @name VARCHAR(100) = NULL OUT
AS
--DECLARE @EPDVisitID INT= 51905
BEGIN
    SET NOCOUNT ON;

    DECLARE
        @ID_Visit INT,
        @ID_Station INT,
        @PersID INT;

    SET @ID_Station = @EPDVisitID % 100;
    SET @ID_Visit = @EPDVisitID / 100;
    SET @hasExited = 0;
    SET @isManualExit = 0;

    SELECT
        @hasExited = CONVERT(BIT, CASE WHEN [ReturnTime] IS NOT NULL OR [isManualExit] = 1 THEN 1 ELSE 0 END),
        @isManualExit = ISNULL([isManualExit], 0),
        @PersID = @PersID
    FROM [dbo].[Visit]
    WHERE [ID_Visit] = @ID_Visit
          AND [ID_Station] = @ID_Station;

    SET NOCOUNT OFF;

    SELECT [hasExited] = @hasExited;

    SELECT
        @name = ISNULL([name] + ' ' + ISNULL([callingname], [firstname]), '?')
    FROM [dbo].[PersonAll]
    WHERE [persid] = @PersID;

    RETURN CASE WHEN ISNULL(@hasExited, 0) = 1 THEN 1 ELSE 0 END + CASE WHEN ISNULL(@isManualExit, 0) = 1 THEN 2 ELSE 0 END;
END;

GO

GRANT EXECUTE ON [dbo].[VisitInfo] TO [eDosStation];
GO

--2024-09-06

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[dbo].[Visit]') AND name='InitError')
ALTER TABLE [dbo].[Visit] ADD InitError INT

go
CREATE OR ALTER  PROCEDURE [dbo].[VisitInitConfirm] 
    @ID_Station INT NULL,
    @ID_Visit INT,
    @InitError INT
as    
UPDATE [dbo].[Visit] SET InitError=@InitError WHERE ID_Station=@ID_Station AND ID_Visit=@ID_Visit;
go
GRANT EXECUTE ON [dbo].[VisitInitConfirm] TO [eDosStation];
GO
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[PersonInfoSnapshot]') AND name = 'Info')
 ALTER TABLE DBO.PersonInfoSnapshot ADD Info XML NULL;
GO
IF OBJECT_ID('[dbo].[LocalEPD]') IS NULL BEGIN
CREATE TABLE [dbo].[LocalEPD] (  [ID_STATION] INT NOT NULL , [EPD_InternalID] VARCHAR(8) NOT NULL, [ID_EPD] INT NOT NULL, [LastUsed] DATETIME null,  PRIMARY KEY([ID_STATION],[EPD_InternalID]) ) 
end
GO
CREATE OR ALTER PROCEDURE [dbo].[VisitInit] @PersID INT,
    @ID_Task INT,
    @Inst_CODE INT,
    @EPD_InternalID INT,
    @isBG BIT,
    @EPDVisitID INT = NULL OUTPUT,
    @ID_Station INT NULL,
    @ID_Visit INT,
    @ENTRY_EPD_CLOCK DECIMAL(10, 0) NULL,
    @epdType TINYINT = NULL,
    @InitError INT = 0
AS
--Local !!
DECLARE @ID_EPD INT ;
DECLARE @now DATETIME = GETDATE() ;

IF @ID_Station IS NULL
    SELECT
        @ID_Station = [ID_Station]
    FROM [dbo].[StationSettings]
    WHERE [PCNr] = HOST_NAME() ;

SELECT
    @ID_EPD = [ID_EPD]
FROM [dbo].[EPD]
WHERE [EPD_InternalID] = @EPD_InternalID ;

IF @ID_EPD IS NULL BEGIN
    INSERT [dbo].[EPD]([EPD_InternalID], [EPD_sn], [EPDType])
    VALUES(@EPD_InternalID, CONVERT(VARCHAR(8), @EPD_InternalID), CASE WHEN @epdType IS NOT NULL
                                                                       THEN @epdType
                                                                  WHEN @isBG = 0
                                                                  THEN 2
                                                                  ELSE 0
                                                                  END) ;

    SET @ID_EPD = SCOPE_IDENTITY() ;
END ;

IF NOT EXISTS
    (
        SELECT
            *
        FROM [dbo].[LocalEPD]
        WHERE [ID_Station] = @ID_Station
              AND [EPD_InternalID] = @EPD_InternalID
    )
    INSERT [dbo].[LocalEPD]([ID_EPD], [ID_Station], [EPD_InternalID], LastUsed)
    VALUES(@ID_EPD, @ID_Station, CONVERT(VARCHAR(8), @EPD_InternalID), @now) ;
ELSE
    UPDATE
        [dbo].[LocalEPD]
    SET
        [LastUsed] = @now
    WHERE [ID_Station] = @ID_Station
          AND [EPD_InternalID] = @EPD_InternalID ;


          

INSERT [dbo].[Visit]([ID_Station], [PersID], [Inst_CODE], [ID_Visit], [ID_Task], [EntryTime], [ID_EPD], [isBG], [isComplete], [isDeleted], [isAlarmCleared], [isAlarm], [isManualExit], [ENTRY_EPD_CLOCK], [EXIT_EPD_CLOCK], [epdType], [epdID], [InitError] )
VALUES(@ID_Station, @PersID, @Inst_CODE, @ID_Visit, @ID_Task, @now, @ID_EPD, @isBG, 0, 0, 0, 0, 0, @ENTRY_EPD_CLOCK, @ENTRY_EPD_CLOCK, @epdType, @EPD_InternalID, @InitError ) ;

SET @EPDVisitID = @ID_Visit * 100+@ID_Station ;

GO
USE [eDosLocal]
GO
/****** Object:  StoredProcedure [dbo].[GetPersonInfo]    Script Date: 9/11/2024 10:10:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM sys.columns WHERE OBJECT_ID=OBJECT_ID('dbo.[PersonInfoSnapshot]') AND name='eDosAgent')
BEGIN
 ALTER TABLE [dbo].[PersonInfoSnapshot] ADD [eDosAgent] BIT NULL;
END;
GO
CREATE OR ALTER PROCEDURE [dbo].[GetPersonInfo] @CSN CHAR(14) =NULL, @PersID INT=NULL, @AllowMultipleEPD BIT=0, @CheckUser BIT=1
AS BEGIN
    --DECLARE @PersID INT=12103, @CSN CHAR(14) =null, @AllowMultipleEPD BIT=0, @CheckUser bit=1;
    SET NOCOUNT ON;
    DECLARE @lid_station INT, @lid_visit INT;
    IF @PersID IS NULL
        SELECT @PersID=[PersID]
        FROM [dbo].[PersonInfoSnapshot]
        WHERE [CSN]=@CSN COLLATE SQL_Latin1_General_CP850_BIN;
    SET NOCOUNT OFF;
    SELECT TOP(1)@lid_station=[ID_Station], @lid_visit=[ID_Visit]
    FROM [dbo].[Visit]
    WHERE [PersID]=@PersID AND [ReturnTime] IS NULL AND [EntryTime]>DATEADD(YEAR, -1, GETDATE())AND([InitError] IS NULL OR [InitError]=0);
    SELECT [a].[PersID], [a].[Type], [a].[CSN], [a].[FirstName], [a].[LastName], [a].[NickName], [AccessAllow]=CONVERT(BIT, CASE WHEN @AllowMultipleEPD=1 AND [a].[hasEPD]=1 THEN 0 ELSE [a].[AccessAllow] END), [a].[Language], [a].[MarginHP10_M], [a].[MarginHP10_Y], [a].[Margin], [a].[VisitIDLocked], [a].[ID_CommentToPerson], [a].[Comment], [a].[Deleted], [ReasonNoAccess]=CASE WHEN @AllowMultipleEPD=1 AND [a].[hasEPD]=1 THEN 'EPD not returned'
                                                                                                                                                                                                                                                                                                                                                                                     WHEN ISNULL([VisitIDLocked], 0)>0 THEN 'Locked because of alarm' ELSE [a].[ReasonNoAccess] END, [a].[Dose_52W_microSv], [a].[hasEPD], [activeStation]=@lid_station, [activeVisit]=@lid_visit, [a].[isAgent], [a].[eDosAgent]
    FROM(SELECT [p].[PersID], [p].[Type], [p].[CSN], [p].[FirstName], [p].[LastName], [p].[NickName], [AccessAllow]=CASE WHEN @CheckUser=0 THEN 1 ELSE [p].[AccessAllow] END, [p].[Language], [p].[MarginHP10_M], [p].[MarginHP10_Y], [p].[Margin], [p].[VisitIDLocked], [p].[ID_CommentToPerson], [p].[Comment], [p].[Deleted], [p].[ReasonNoAccess], [p].[Dose_52W_microSv], [hasEPD]=CONVERT(BIT, CASE WHEN @lid_visit IS NOT NULL THEN 1 ELSE 0 END), [isAgent]=CONVERT(BIT, CASE WHEN [p].[isAgent] IS NULL THEN 0 ELSE [p].[isAgent] END), [eDosAgent]=CONVERT(BIT, ISNULL(p.edosAgent, 0))
         FROM [dbo].[PersonInfoSnapshot] [p]
         WHERE [p].[PersID]=@PersID) [a];
END;
GO
CREATE OR ALTER PROCEDURE [dbo].[GetPersonOverview] @PersID INT = NULL
AS
SELECT [p].[PersID],
       [p].[Type],
       [p].[FirstName],
       [p].[LastName],
       [p].[NickName],
       [AccessAllow] = [p].[AccessAllow],
       [p].[Language],
       [p].[MarginHP10_M],
       [p].[MarginHP10_Y],
       [p].[Margin],
       [p].[VisitIDLocked],
       [p].[ID_CommentToPerson],
       [p].[Comment],
       [p].[Deleted],
       [p].[ReasonNoAccess],
       [p].[Dose_52W_microSv],
       [isAgent] = CONVERT (   BIT,
                               CASE
                                   WHEN [p].[isAgent] IS NULL THEN
                                       0
                                   ELSE
                                       [p].[isAgent]
                               END
                           ),
       [info],
      [eDosAgent] = CONVERT (BIT, ISNULL ([p].[eDosAgent], 0))
FROM    [dbo].[PersonInfoSnapshot] [p]
WHERE [p].[PersID] = @PersID;
GO

GRANT EXECUTE ON [GetPersonOverview] TO [edosstation];
GO
