﻿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

IF NOT EXISTS
(
    SELECT *
    FROM [INFORMATION_SCHEMA].[COLUMNS]
    WHERE [TABLE_NAME] = 'StationSettings'
          AND [COLUMN_NAME] = 'EPDRetries'
)
    ALTER TABLE [dbo].[StationSettings] ADD [EPDRetries] INT;

IF NOT EXISTS
(
    SELECT *
    FROM [INFORMATION_SCHEMA].[COLUMNS]
    WHERE [TABLE_NAME] = 'StationSettings'
          AND [COLUMN_NAME] = 'RestartCom'
)
    ALTER TABLE [dbo].[StationSettings] ADD [RestartCom] BIT;
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],
       [EPDRetries],
       [RestartCom]
FROM [dbo].[StationSettings]
WHERE [PCNr] = HOST_NAME ();
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
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]
    )
    VALUES
    (HOST_NAME (), @ComPort, @Inst_CODE1, @Inst_CODE2, @Inst_Name1, @Inst_Name2, @TestCSN, @Reader, @useTestButtons,
     @useBeep, @checkUser, @EPDRetries, @RestartCom);
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
    WHERE [PCNr] = HOST_NAME ();
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)
FROM [dbo].[StationSettings]
WHERE [PCNr] = HOST_NAME ();

go

ALTER PROCEDURE [dbo].[GetPersonInfo]    @CSN CHAR(14) =NULL,    @PersID INT = NULL AS
--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 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 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
	 go

---- Luc 2022-03-25	 
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,
 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

	 
	 
CREATE OR ALTER PROCEDURE [syncLoc].[UpdateRemoteReturns] @ID_LocalStation INT=null AS
	 -- Luc 2022-03-25
	 --DECLARE @ID_LocalStation INT = 9;
	 DECLARE @pcnr VARCHAR(20)
	 
	 select @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'

	 IF @ID_LocalStation IS NULL SELECT @ID_LocalStation =id_station From stationsettings WHERE pcnr= @pcnr 
	 select @ID_LocalStation,pcnr FROM 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])
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]
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 [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] = ISNULL([c].[isManualExit], [v].[isManualExit]),
    [ID_Station_Return] = ISNULL([c].[ID_Station_Return], [v].[ID_Station_Return]),
    [EXIT_EPD_CLOCK] = [c].[EXIT_EPD_CLOCK]
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;

GO

GRANT EXECUTE ON [syncLoc].[UpdateRemoteReturns] TO [eDosStation]
go