Compile function Lot_Event_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler

Declare function Error_Services, Logging_Services, Environment_Services, Database_Services, RTI_CreateGUID, Lot_Services
Declare function Lot_Event_Services

Declare subroutine Error_Services, Logging_Services, Database_Services, Lot_Services, Service_Services

$insert LOGICAL
$Insert LOT_EVENT_EQUATES
$Insert LOT_EQUATES
$Insert LOT_OPERATION_EQUATES

Options EVENT_TYPES = 'MOVE_IN', 'MOVE_OUT', 'HOLD_ON', 'HOLD_OFF', 'REDUCE_WAFER_QTY', 'BONUS_WAFER_QTY', 'COMMENT', 'LOCATION', 'LOAD', 'UNSIGN_LOAD', 'TW_USE', 'CLOSE'
Options LOT_TYPES = 'TW', 'RDS', 'WM_OUT', 'WM_IN', 'WO_MAT', 'LOT'
Options LEGACY_LOT_TYPES = 'RDS', 'WM_OUT', 'WM_IN'
Options BOOLEAN = 'True', 'False'

GoToService

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service CreateLotEvent(LotId, EventDateTime, EventType=EVENT_TYPES, EventNote, EventEquipmentId, OperatorId, IsLegacyLotId=BOOLEAN, LegacyLotType=LEGACY_LOT_TYPES)

    GoSub InitEventLog
    ErrorMessage   = ''
    //Handle Legacy Lots
    If IsLegacyLotId then
        If LegacyLotType NE '' then
            LegacyLotId = LotId
            LotId = Lot_Services('GetLotIdByLegacyLotIdAndType', LotId, LegacyLotType)
            If LotID EQ '' then
                LotId = Lot_Services('CreateNewLot', LegacyLotType, '', '', '', '', '', OperatorId, '', LegacyLotId)
            end
        end else
            LotId = ''
        end
    end
    If RowExists('LOT', LotId) then
        NewEventId = RTI_CreateGUID()
        If NewEventId NE '' then
            NewEventSequence = Lot_Event_Services('GetLotEventNextSequence', LotId)
            If Error_Services('NoError') then
                NewEventRec                                    = ''
                NewEventRec<LOT_EVENT_LOT_ID$>                 = LotId
                NewEventRec<LOT_EVENT_LOT_EVENT_TYPE$>         = EventType
                NewEventRec<LOT_EVENT_EVENT_DATETIME$>         = EventDatetime
                NewEventRec<LOT_EVENT_EVENT_NOTE$>             = EventNote
                NewEventRec<LOT_EVENT_EQUIPMENT_ID$>           = EventEquipmentId
                NewEventRec<LOT_EVENT_EVENT_OPERATOR_ID$>      = OperatorId
                NewEventRec<LOT_EVENT_SEQUENCE$>               = NewEventSequence
                Database_Services('WriteDataRow', 'LOT_EVENT', NewEventId, NewEventRec)
                If Error_Services('NoError') then
                    Service_Services('PostProcedure', 'LOT_EVENT_SERVICES', 'SetLatestLotEvent':@VM:LotId:@VM:NewEventId, True$)
                end else
                    ErrorMessage = 'Error creating new event : ' : Error_Services('GetMessage')
                end
            end else
                ErrorMessage = Error_Services('GetMessage')
            end
        end else
            ErrorMessage = 'Error creating an event Id.'
        end
    end else
        ErrorMessage = 'Error in Create Lot Event routine, Lot id was not found.'
    end
    If ErrorMessage EQ '' then
        LogData    = ''
        LogData<1> = LoggingDTM
        LogData<2> = LotId
        LogData<3> = EventType
        LogData<4> = OperatorId
        LogData<9> = 'Successfully logged event.'
        Logging_Services('AppendLog', objLotEventLog, LogData, @RM, @FM, False$)
    end else
        LogData    = ''
        LogData<1> = LoggingDTM
        LogData<2> = LotId
        LogData<3> = EventType
        LogData<4> = OperatorId
        LogData<9> = ErrorMessage
        Logging_Services('AppendLog', objLotEventLog, LogData, @RM, @FM, False$)
        Error_Services('Add', ErrorMessage)
    end
    
end service

Service GetLotEventNextSequence(LotId)
    
    ErrorMessage = ''
    NextSequence = 1
    If LotID NE '' then
        If RowExists('LOT', LotId) then
            LotEvents = XLATE('LOT', LotId, LOT_LOT_EVENTS$, 'X')
            for each LotEvent in LotEvents using @VM
                ThisEventSequence = XLATE('LOT_EVENT', LotEvent, LOT_EVENT_SEQUENCE$, 'X')
                If ThisEventSequence GE NextSequence then NextSequence = ThisEventSequence + 1
            Next LotEvent
        end else
            ErrorMessage = 'Unable to get lot event sequence: Lot ID not found.'
        end
    end else
        ErrorMessage = 'Unable to get lot event sequence: Lot ID was null.'
    end
    If ErrorMessage EQ '' then
        Response = NextSequence
    end else
        Error_Services('Add', ErrorMessage)
    end
end service

//Returns a @FM delimited list of events in sequence
Service GetLotEventsInSequence(LotId)
    LotEventsUnsorted = ''
    LotEventsSorted   = ''
    LotEventsToReturn = ''
    If LotID NE '' then
        //Get Operations
        LotEvents = Xlate('LOT', LotId, LOT_LOT_EVENTS$, 'X')
        for each LotEvent in LotEvents using @VM
            ThisEventSequence                    = XLATE('LOT_EVENT', LotEvent, LOT_EVENT_SEQUENCE$, 'X')
            LotEventsToReturn<ThisEventSequence> = LotEvent
        Next LotOperation 
    end else
        //error: lot id was null
    end
    Response = LotEventsToReturn 
end service

Service SetLatestLotEvent(LotId, LotEventId)
    
    ErrorMessage = ''
    If RowExists('LOT', LotId) then
        If RowExists('LOT_EVENT', LotEventId) then
            LotRec = Database_Services('ReadDataRow', 'LOT', LotId, True$, 0, False$)
            LotRec<LOT_MOST_RECENT_LOT_EVENT_ID$> = LotEventId
            Database_Services('WriteDataRow', 'LOT', LotId, LotRec)
            If Error_Services('HasError') then
                ErrorMessage = Error_Services('GetMessage')
            end
        end else
            ErrorMessage = 'Lot event ' : LotEventId : ' for lot ' : LotId : ' not found in LOT_EVENT table'
        end
    end else
        ErrorMessage = 'Lot Id ' : LotId : ' not found in LOT table.'
    end
    If ErrorMessage NE '' then
        Error_Services('Add', ErrorMessage)
    end
end service

/* * * * * * * * * *
* INTERNAL GOSUBS
* * * * * * * * *  */

InitEventLog:
    
    LogPath      = Environment_Services('GetApplicationRootPath') : '\LogFiles\Lot'
    LogDate      = Oconv(Date(), 'D4/')
    LogTime      = Oconv(Time(), 'MTS')
    LoggingDTM   = LogDate : ' ' : LogTime   ; // Logging DTM
    LogFileName    = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' LotEvent.csv'
    Headers        = 'Logging DTM' : @FM : 'Lot Id' : @FM : 'Event Type' : @FM : 'Operator' : @FM : 'Begin Wafer Qty' : @FM : 'End Wafer Qty' : @FM : 'Bonus Wafer Qty' : @FM : 'Reduce Wafer Qty' : @FM : 'Message'
    objLotEventLog = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, ',', Headers, '', False$, False$)

return


