﻿USE [eDosLocal];
GO

/****** Object:  StoredProcedure [dbo].[GetPersonInfo]    Script Date: 8/03/2022 15:53:18 ******/
SET ANSI_NULLS ON;
GO

SET QUOTED_IDENTIFIER ON;
GO

USE [eDosLocal];
GO

/****** Object:  StoredProcedure [dbo].[GetPersonInfo]    Script Date: 19/04/2022 13:08:50 ******/
SET ANSI_NULLS ON;
GO

SET QUOTED_IDENTIFIER ON;
GO

ALTER PROCEDURE [dbo].[GetPersonInfo] @CSN CHAR(14) = NULL,
    @PersID INT = NULL,
    @AllowMultipleEPD BIT = 0
AS BEGIN
    --DECLARE    @PersID INT = null,    @CSN CHAR(14) = 'TP10';
    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;

    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());

    SELECT
        [a].[PersID],
        [a].[Type],
        [a].[CSN],
        [a].[FirstName],
        [a].[LastName],
        [a].[NickName],
        [AccessAllow] = 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' ELSE [a].[ReasonNoAccess] END,
        [a].[Dose_52W_microSv],
        [a].[hasEPD],
        [activeStation] = @lid_station,
        [activeVisit] = @lid_visit
    FROM(SELECT
             [p].[PersID],
             [p].[Type],
             [p].[CSN],
             [p].[FirstName],
             [p].[LastName],
             [p].[NickName],
             [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],
             [hasEPD] = CONVERT(BIT, CASE WHEN @lid_visit IS NOT NULL THEN 1 ELSE 0 END)
         FROM [dbo].[PersonInfoSnapshot] [p]
         WHERE [p].[PersID] = @PersID) [a];
END;

go

---- Luc 2022-03-25	 
USE [eDosLocal]
go
IF SCHEMA_ID('syncloc') IS NULL
BEGIN
	exec('CREATE SCHEMA [syncLoc]')
END 

GO
IF OBJECT_ID ('[syncLoc].[compareVisit]') IS NULL
BEGIN
   CREATE TABLE [syncLoc].[compareVisit](
	[ID_Station] [INT] NOT NULL,
	[ID_Visit] [INT] NOT NULL,
	[Inst_CODE] [INT] NOT NULL,
	[PersID] [INT] NULL,
	[ID_Task] [INT] NULL,
	[EntryTime] [DATETIME] NULL,
	[ReturnTime] [DATETIME] NULL,
	[Hp10Dose] [FLOAT] NULL,
	[Hp07Dose] [FLOAT] NULL,
	[Hp10Total] [FLOAT] NULL,
	[Hp07Total] [FLOAT] NULL,
	[Hp10Peak] [FLOAT] NULL,
	[Hp07Peak] [FLOAT] NULL,
	[Hp10Time] [DATETIME] NULL,
	[Hp07Time] [DATETIME] NULL,
	[ID_EPD] [INT] NULL,
	[QualityData] [INT] NULL,
	[K1] [INT] NULL,
	[K2] [INT] NULL,
	[K3] [INT] NULL,
	[K4] [INT] NULL,
	[K5] [INT] NULL,
	[K6] [INT] NULL,
	[SG] [INT] NULL,
	[BC] [INT] NULL,
	[FB] [INT] NULL,
	[HG] [INT] NULL,
	[alarmStatus] [INT] NULL,
	[otherAlarms] [INT] NULL,
	[errorStatus] [INT] NULL,
	[operatingStatus] [INT] NULL,
	[isBG] [BIT] NULL,
	[isComplete] [BIT] NOT NULL,
	[isDeleted] [BIT] NOT NULL,
	[isAlarm] [BIT] NOT NULL,
	[isAlarmCleared] [BIT] NOT NULL,
	[isManualExit] [BIT] NOT NULL,
	[ID_Station_Return] [INT] NULL,
	[ENTRY_EPD_CLOCK] [NUMERIC](10, 0) NULL,
	[EXIT_EPD_CLOCK] [NUMERIC](10, 0) NULL,
	[TIME_LOST] [INT] NULL,
	[epdID] [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,
 CONSTRAINT [PK_compareVisit] PRIMARY KEY CLUSTERED 
(
	[ID_Station] ASC,
	[ID_Visit] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];

    GRANT ALTER,
          INSERT,
          UPDATE,
          DELETE,
          SELECT
    ON [syncLoc].[compareVisit]
    TO  [edosStation];
END;
GO

IF NOT EXISTS (SELECT
                   *
               FROM [sys].[columns]
               WHERE [object_id] = OBJECT_ID('[syncLoc].[compareVisit]')
                     AND [name] = 'eDosBatteryWarning')BEGIN
    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosBatteryWarning] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosDose07Warning] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosDose07Alarm] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosRate07Warning] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosRate07Alarm] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosDose10Warning] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosDose10Alarm] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosRate10Warning] BIT NULL;

    ALTER TABLE [syncLoc].[compareVisit] ADD [eDosRate10Alarm] BIT NULL;
END;
GO

ALTER PROCEDURE [syncLoc].[UpdateRemoteReturns] @ID_LocalStation INT = NULL,
    @pcnr VARCHAR(20) = NULL
AS BEGIN
    -- Luc 2022-03-25
    --DECLARE @ID_LocalStation INT = 9;
    --DECLARE @pcnr VARCHAR(20);
    IF @pcnr = 'EDOS-SCH' SET @pcnr = 'EDOS-WSCH';
    ELSE IF @pcnr = 'DESKTOP-CCDL4A3' SET @pcnr = 'EDOS-LNK';
    ELSE IF @pcnr = 'EDOS-ZZZ' SET @pcnr = 'EDOS-LSCH';

    IF @ID_LocalStation IS NULL BEGIN
        IF @pcnr IS NULL SET @pcnr = @@serverName;

        IF @pcnr = 'EDOS-SCH' SET @pcnr = 'EDOS-WSCH';
        ELSE IF @pcnr = 'DESKTOP-CCDL4A3' SET @pcnr = 'EDOS-LNK';
        ELSE IF @pcnr = 'EDOS-ZZZ' SET @pcnr = 'EDOS-LSCH';

        SELECT
            @ID_LocalStation = [ID_Station]
        FROM [dbo].[StationSettings]
        WHERE [PCNr] = @pcnr;
    END;

    SELECT
        @ID_LocalStation,
        [PCNr]
    FROM [dbo].[StationSettings]
    WHERE [ID_Station] = @ID_LocalStation;

    TRUNCATE TABLE [syncLoc].[compareVisit];

    INSERT [syncLoc].[compareVisit]([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], [epdID], [eDosBatteryWarning], [eDosDose07Warning], [eDosDose07Alarm], [eDosRate07Warning], [eDosRate07Alarm], [eDosDose10Warning], [eDosDose10Alarm], [eDosRate10Warning], [eDosRate10Alarm])
    SELECT
        [v].[ID_Station],
        [v].[ID_Visit],
        [v].[Inst_CODE],
        [v].[PersID],
        [v].[ID_Task],
        [v].[EntryTime],
        [v].[ReturnTime],
        [v].[Hp10Dose],
        [v].[Hp07Dose],
        [v].[Hp10Total],
        [v].[Hp07Total],
        [v].[Hp10Peak],
        [v].[Hp07Peak],
        [v].[Hp10Time],
        [v].[Hp07Time],
        [v].[ID_EPD],
        [v].[QualityData],
        [v].[K1],
        [v].[K2],
        [v].[K3],
        [v].[K4],
        [v].[K5],
        [v].[K6],
        [v].[SG],
        [v].[BC],
        [v].[FB],
        [v].[HG],
        [v].[alarmStatus],
        [v].[otherAlarms],
        [v].[errorStatus],
        [v].[operatingStatus],
        [v].[isBG],
        [v].[isComplete],
        [v].[isDeleted],
        [v].[isAlarm],
        [v].[isAlarmCleared],
        [v].[isManualExit],
        [v].[ID_Station_Return],
        [v].[ENTRY_EPD_CLOCK],
        [v].[EXIT_EPD_CLOCK],
        [v].[TIME_LOST],
        [v].[epdID],
        [v].[eDosBatteryWarning],
        [v].[eDosDose07Warning],
        [v].[eDosDose07Alarm],
        [v].[eDosRate07Warning],
        [v].[eDosRate07Alarm],
        [v].[eDosDose10Warning],
        [v].[eDosDose10Alarm],
        [v].[eDosRate10Warning],
        [v].[eDosRate10Alarm]
    FROM [EDOS].[edos].[dbo].[Visit] [v]
         INNER JOIN(SELECT
                        [ID_Visit]
                    FROM [dbo].[Visit]
                    WHERE [ID_Station] = @ID_LocalStation
                          AND [ReturnTime] IS NULL) [i] ON [i].[ID_Visit] = [v].[ID_Visit]
    WHERE [v].[ID_Station] = @ID_LocalStation
          AND [v].[ReturnTime] IS NOT NULL;

    UPDATE
        [dbo].[Visit]
    SET
        [ReturnTime] = [c].[ReturnTime],
        [Hp10Dose] = [c].[Hp10Dose],
        [Hp07Dose] = [c].[Hp07Dose],
        [Hp10Total] = [c].[Hp10Total],
        [Hp07Total] = [c].[Hp07Total],
        [Hp10Peak] = [c].[Hp10Peak],
        [Hp07Peak] = [c].[Hp07Peak],
        [Hp10Time] = [c].[Hp10Time],
        [Hp07Time] = [c].[Hp07Time],
        [QualityData] = [c].[QualityData],
        [K1] = [c].[K1],
        [K2] = [c].[K2],
        [K3] = [c].[K3],
        [K4] = [c].[K4],
        [K5] = [c].[K5],
        [K6] = [c].[K6],
        [SG] = [c].[SG],
        [BC] = [c].[BC],
        [FB] = [c].[FB],
        [HG] = [c].[HG],
        [alarmStatus] = [c].[alarmStatus],
        [otherAlarms] = [c].[otherAlarms],
        [errorStatus] = [c].[errorStatus],
        [operatingStatus] = [c].[operatingStatus],
        [isComplete] = [c].[isComplete],
        [isDeleted] = [c].[isDeleted],
        [isAlarm] = [c].[isAlarm],
        [isAlarmCleared] = [c].[isAlarmCleared],
        [isManualExit] = CASE WHEN [v].[isManualExit] IS NULL
                                   OR [c].[isManualExit] = 1 THEN [c].[isManualExit] ELSE [v].[isManualExit] END,
        [ID_Station_Return] = ISNULL([c].[ID_Station_Return], [v].[ID_Station_Return]),
        [EXIT_EPD_CLOCK] = [c].[EXIT_EPD_CLOCK],
        [eDosBatteryWarning] = CASE WHEN [v].[eDosBatteryWarning] IS NULL
                                         OR [c].[eDosBatteryWarning] = 1 THEN [c].[eDosBatteryWarning] ELSE [v].[eDosBatteryWarning] END,
        [eDosDose07Warning] = CASE WHEN [v].[eDosDose07Warning] IS NULL
                                        OR [c].[eDosDose07Warning] = 1 THEN [c].[eDosDose07Warning] ELSE [v].[eDosDose07Warning] END,
        [eDosDose07Alarm] = CASE WHEN [v].[eDosDose07Alarm] IS NULL
                                      OR [c].[eDosDose07Alarm] = 1 THEN [c].[eDosDose07Alarm] ELSE [v].[eDosDose07Alarm] END,
        [eDosRate07Warning] = CASE WHEN [v].[eDosRate07Warning] IS NULL
                                        OR [c].[eDosRate07Warning] = 1 THEN [c].[eDosRate07Warning] ELSE [v].[eDosRate07Warning] END,
        [eDosRate07Alarm] = CASE WHEN [v].[eDosRate07Alarm] IS NULL
                                      OR [c].[eDosRate07Alarm] = 1 THEN [c].[eDosRate07Alarm] ELSE [v].[eDosRate07Alarm] END,
        [eDosDose10Warning] = CASE WHEN [v].[eDosDose10Warning] IS NULL
                                        OR [c].[eDosDose10Warning] = 1 THEN [c].[eDosDose10Warning] ELSE [v].[eDosDose10Warning] END,
        [eDosDose10Alarm] = CASE WHEN [v].[eDosDose10Alarm] IS NULL
                                      OR [c].[eDosDose10Alarm] = 1 THEN [c].[eDosDose10Alarm] ELSE [v].[eDosDose10Alarm] END,
        [eDosRate10Warning] = CASE WHEN [v].[eDosRate10Warning] IS NULL
                                        OR [c].[eDosRate10Warning] = 1 THEN [c].[eDosRate10Warning] ELSE [v].[eDosRate10Warning] END,
        [eDosRate10Alarm] = CASE WHEN [v].[eDosRate10Alarm] IS NULL
                                      OR [c].[eDosRate10Alarm] = 1 THEN [c].[eDosRate10Alarm] ELSE [v].[eDosRate10Alarm] END
    FROM [dbo].[Visit] [v]
         INNER JOIN [syncLoc].[compareVisit] [c] ON [c].[ID_Station] = [v].[ID_Station]
                                                    AND [c].[ID_Visit] = [v].[ID_Visit]
    WHERE [v].[ReturnTime] IS NULL
          AND [v].[isComplete] = 0;
END;
GO
GRANT EXECUTE ON [syncLoc].[UpdateRemoteReturns] TO [eDosStation];
GO
