Function GAN_CYCLE_TIME_SERVICES(@Service, @Params)
/***********************************************************************************************************************

    Name        :   GAN_CYCLE_TIME_SERVICES

    Description :   Handler program for all module related services.

    Notes       :   Service module to support environmental state issues. Environmental refers to the state of the
                    operating system, which includes version, client vs. server, and path to critical systems.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        09/30/2020    jro     Original programmer.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler

$insert LOGICAL
$insert SERVICE_SETUP
$INSERT REACT_RUN_EQUATES
$INSERT GAN_CYCLE_TIME_EQUATES
$INSERT GAN_STAGE_CYCLE_TIME_EQUATES
$INSERT WO_LOG_EQUATES
$INSERT PROD_VER_EQUATES
$INSERT RUN_STAGE_WFR_EQUATES


Declare Function Get.RecCount, GetTickCount, Obj_Tables, Gan_Services
Declare Function DCount
Declare Function Database_Services
Declare Function Environment_Services, Logging_Services, Datetime
Declare subroutine Copy_Record_To_SQL, Delete_Record_From_SQL,GetTickCount, Obj_Tables, Logging_Services

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\GAN_CYCLE_TIME'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' GAN_CYCLE_TIME_Services.csv'
Headers     = 'Logging DTM' : @FM : 'User' : @FM : 'Notes'
objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, Comma$, Headers, '', False$, False$)
LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

GoToService else
	Error_Services('Add', Service : ' is not a valid service request within the ' : ServiceModule : ' services module.')
end
Return Response OR ''

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Services
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//----------------------------------------------------------------------------------------------------------------------
Service ExportRecords()
	hSysLists   = Database_Services('GetTableHandle', 'SYSLISTS')
	Lock hSysLists, ServiceKeyID then
		StartTime = GetTickCount()
		
		
		Open "GAN_STAGE_CYCLE_TIME" to hTable then
			
			ClearSelect
			Select hTable
			Done = 0
			Loop
				ReadNext Key else Done = 1
			Until Done
				//0.00139
				FmtKey = Key
				swap '*' with @VM in FmtKey
				//if FmtKey<1,1> EQ 386307 then 
				Record = Xlate('GAN_STAGE_CYCLE_TIME', Key, '', 'X')
				LastWriteTime = Record<GAN_STAGE_CYCLE_TIME_RUN_CREATION_DATE$>
				Now = DateTime()
				//Difference = Now - LastWriteTime
				If (Now - LastWriteTime) LT 0.00139 then
					*Record is too new, may not be finished writing. Do Nothing
					LogData       = ''
					LogData<1>    = LoggingDTM
					LogData<2>    = Key ' is <120 seconds... Doing nothing with it.'
					LogData<3>	  = 'Stage is ' : Record<GAN_STAGE_CYCLE_TIME_STAGE$> : '. Stop times are ' : Record<GAN_STAGE_CYCLE_TIME_STOP$> '.'
					LogData<4>    = '---------------------------------------------'
					Logging_Services('AppendLog', objLog, LogData, @FM, @VM)
					null;
				end else
					*Record is over 120 seconds old. Process it.
					//Fields from GAN_STAGE_CYCLE_TIME
					Stage = Record<GAN_STAGE_CYCLE_TIME_STAGE$>
					WfrID = Record<GAN_STAGE_CYCLE_TIME_WFR_ID$>
					OperatorStart = Record<GAN_STAGE_CYCLE_TIME_OPERATOR_START$>
					OperatorStop = Record<GAN_STAGE_CYCLE_TIME_OPERATOR_STOP$>
					Tool = Record<GAN_STAGE_CYCLE_TIME_TOOL$>
					ToolClass = Record<GAN_STAGE_CYCLE_TIME_TOOL_CLASS$>
					RDSNo = Record<GAN_STAGE_CYCLE_TIME_RDS_NO$>
					
					//Fields from Other tables
					ReactRunRec = Xlate('REACT_RUN',RDSNo, '', 'X')
					WO_NO = ReactRunRec<REACT_RUN_WO_NO$>
					RunID = ReactRunRec<REACT_RUN_GAN_RUN_ID$>
					ReactorNo = 'R':RunID[1,2]
					PartNo = XLATE('WO_LOG', WO_NO, WO_LOG_WAFER_TRACK_PART$,'X')
					ProdVerNo = XLATE('WO_LOG', WO_NO, WO_LOG_PROD_VER_NO$,'X')
					PSNo = Xlate('PROD_VER',ProdVerNo,PROD_VER_PROC_STEP_PSN$,'X')
					
					
					//Find Earliest and Latest START/STOP DTMs
					StartDTMs = Record<GAN_STAGE_CYCLE_TIME_START$>
					StopDTMs = Record<GAN_STAGE_CYCLE_TIME_STOP$>
					EarliestStart = StartDTMs<1,1>
					LatestStop = StopDTMs<1,1>
					//Find Earliest Start Time for stage
					For Each StartDTM in StartDTMs using @VM
						if StartDTM <= EarliestStart then
							EarliestStart = StartDTM
						end
					Next StartDTM    
					//Find Latest Stop Time for stage
					For Each StopDTM in StopDTMs using @VM
						if StopDTM >= LatestStop then
							LatestStop = StopDTM
						end
					Next StopDTM
					
					IF Stage EQ 'EPI_DEPOSITION' then
						Recipe = ReactRunRec<REACT_RUN_GAN_RECIPE$>
					end else
						Recipe = ''
					end
					IF Stage EQ 'SPLIT' then
						RecToWrite = ''
						RecToWrite<GAN_CYCLE_TIME_WO_NO$> =            WO_NO
						RecToWrite<GAN_CYCLE_TIME_RUN_ID$> =           RunID
						RecToWrite<GAN_CYCLE_TIME_RDS_No$> =           RDSNo
						RecToWrite<GAN_CYCLE_TIME_RECIPE$> =           Recipe
						RecToWrite<GAN_CYCLE_TIME_Part_NO$> =          PartNo
						RecToWrite<GAN_CYCLE_TIME_PSN_NO$> =           PSNo
						RecToWrite<GAN_CYCLE_TIME_STAGE_NAME$> =       Stage
						RecToWrite<GAN_CYCLE_TIME_TOOL_CLASS$> =       ToolClass
						RecToWrite<GAN_CYCLE_TIME_TOOL$> =             Tool
						RecToWrite<GAN_CYCLE_TIME_START_DTM$> =        EarliestStart
						RecToWrite<GAN_CYCLE_TIME_STOP_DTM$> =         LatestStop
						RecToWrite<GAN_CYCLE_TIME_OPERATOR_START$> =   OperatorStart
						RecToWrite<GAN_CYCLE_TIME_OPERATOR_STOP$> =    OperatorStop
						RecToWrite<GAN_CYCLE_TIME_WFR_SCRIBE$> = ''
						RecToWrite<GAN_CYCLE_TIME_REACTOR$> =		   ReactorNo
						NewRecKey = RDSNo : '-' : Stage
					end else
						RecToWrite = ''
						RecToWrite<GAN_CYCLE_TIME_WO_NO$> =            WO_NO
						RecToWrite<GAN_CYCLE_TIME_RUN_ID$> =           RunID
						RecToWrite<GAN_CYCLE_TIME_RDS_No$> =           RDSNo
						RecToWrite<GAN_CYCLE_TIME_RECIPE$> =           Recipe
						RecToWrite<GAN_CYCLE_TIME_Part_NO$> =          PartNo
						RecToWrite<GAN_CYCLE_TIME_PSN_NO$> =           PSNo
						RecToWrite<GAN_CYCLE_TIME_STAGE_NAME$> =       Stage
						RecToWrite<GAN_CYCLE_TIME_TOOL_CLASS$> =       ToolClass
						RecToWrite<GAN_CYCLE_TIME_TOOL$> =             Tool
						RecToWrite<GAN_CYCLE_TIME_START_DTM$> =        EarliestStart
						RecToWrite<GAN_CYCLE_TIME_STOP_DTM$> =         LatestStop
						RecToWrite<GAN_CYCLE_TIME_OPERATOR_START$> =   OperatorStart
						RecToWrite<GAN_CYCLE_TIME_OPERATOR_STOP$> =    OperatorStop
						RecToWrite<GAN_CYCLE_TIME_WFR_SCRIBE$> = ''
						RecToWrite<GAN_CYCLE_TIME_REACTOR$> =		   ReactorNo
						NewRecKey = RDSNo : '-' : Stage: '-' : LatestStop
					end
					Obj_Tables('WriteRec', 'GAN_CYCLE_TIME':@RM:NewRecKey:@RM:@RM:RecToWrite)
					LogData       = ''
					LogData<1>    = LoggingDTM
					LogData<2>    = Key ' is >120 seconds... Processing.'
					LogData<3>	  = 'Stage is ' : Record<GAN_STAGE_CYCLE_TIME_STAGE$> : '. Stop times are ' : Record<GAN_STAGE_CYCLE_TIME_STOP$> '.'
					LogData<4>    = '---------------------------------------------'
					Logging_Services('AppendLog', objLog, LogData, @FM, @VM)
					
					GoSub CheckFinishStatus
					
					Delete hTable, Key
				end
				//end
				
			Repeat
			
		end
	end
	Unlock hSysLists, ServiceKeyID else Null
Return

//////////////////////////////////////////////////////////////

end service

* * * * * * * * * *
CheckFinishStatus:
* * * * * * * * * *
    InWfrIds = ReactRunRec<REACT_RUN_IN_WFR_ID$>
    WaferFinished = ''

    for each InWfrId in InWfrIds using @VM setting wfrPos
        WaferFinished<1,wfrPos> = Gan_Services('GetMetrologyStatus', InWfrId)
    Next InWfrId
    Locate '0' in WaferFinished using @VM setting wPos then
        //Do Nothing, Lot isn't finished all stages
        return
    end else
        //Create a GAN_CYCLE_TIME Record
        ReactRunRec = Xlate('REACT_RUN',RDSNo, '', 'X')
        Stage = 'FINISH'
        WfrID = ''
        OperatorStart = ''
        OperatorStop = ''
        Tool = ''
        ToolClass = ''
        RDSNo = Record<GAN_STAGE_CYCLE_TIME_RDS_NO$>

        
        WO_NO = ReactRunRec<REACT_RUN_WO_NO$>
        RunID = ReactRunRec<REACT_RUN_GAN_RUN_ID$>
        ReactorNo = 'R':RunID[1,2]
        PartNo = XLATE('WO_LOG', WO_NO, WO_LOG_WAFER_TRACK_PART$,'X')
        ProdVerNo = XLATE('WO_LOG', WO_NO, WO_LOG_PROD_VER_NO$,'X')
        PSNo = Xlate('PROD_VER',ProdVerNo,PROD_VER_PROC_STEP_PSN$,'X')
        FinishTime = Datetime()
        

        RecToWrite = ''
        RecToWrite<GAN_CYCLE_TIME_WO_NO$> =            WO_NO
        RecToWrite<GAN_CYCLE_TIME_RUN_ID$> =           RunID
        RecToWrite<GAN_CYCLE_TIME_RDS_No$> =           RDSNo
        RecToWrite<GAN_CYCLE_TIME_RECIPE$> =           Recipe
        RecToWrite<GAN_CYCLE_TIME_Part_NO$> =          PartNo
        RecToWrite<GAN_CYCLE_TIME_PSN_NO$> =           PSNo
        RecToWrite<GAN_CYCLE_TIME_STAGE_NAME$> =       Stage
        RecToWrite<GAN_CYCLE_TIME_TOOL_CLASS$> =       ToolClass
        RecToWrite<GAN_CYCLE_TIME_TOOL$> =             Tool
        RecToWrite<GAN_CYCLE_TIME_START_DTM$> =        FinishTime
        RecToWrite<GAN_CYCLE_TIME_STOP_DTM$> =         FinishTime
        RecToWrite<GAN_CYCLE_TIME_OPERATOR_START$> =   OperatorStart
        RecToWrite<GAN_CYCLE_TIME_OPERATOR_STOP$> =    OperatorStop
        RecToWrite<GAN_CYCLE_TIME_WFR_SCRIBE$> = ''
        RecToWrite<GAN_CYCLE_TIME_REACTOR$> =		   ReactorNo
        NewRecKey = RDSNo : '-' : Stage
        
        Obj_Tables('WriteRec', 'GAN_CYCLE_TIME':@RM:NewRecKey:@RM:@RM:RecToWrite)
        LogData       = ''
        LogData<1>    = LoggingDTM
        LogData<2>    = RDSNo ' is finished processing.'
        LogData<3>	  = 'Stage is ' : Record<GAN_STAGE_CYCLE_TIME_STAGE$> : '. Stop times are ' : Record<GAN_STAGE_CYCLE_TIME_STOP$> '.'
        LogData<4>    = '---------------------------------------------'
        Logging_Services('AppendLog', objLog, LogData, @FM, @VM)
    end
    
return



