Function Scheduler_Actions_Stop_Events(CtrlEntId, Event, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10, Param11, Param12, Param13, Param14, Param15)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   Scheduler_Actions_Stop_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert

    History     :   (Date, Initials, Notes)
        04/13/21    djs     Created initial commuter module.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler
#Window SCHEDULER_ACTIONS_STOP

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES
$insert SCHED_DET_NG_EQUATES
$insert WO_LOG_EQUATES
$insert WO_SCHEDULE_NG_EQUATES
$insert REACTOR_EQUATES

Equ OI_DISABLED_GREY$ TO 239 + (239*256) + (239*65536)

WorkOrderCtrls      = 'OLE_DIV_WORK_ORDER_DETAILS,WO_NO_LABEL,WO_NO,EPI_PART_LABEL,EPI_PART,DESCRIPTION_LABEL,DESCRIPTION,CUSTOMER_NAME_LABEL,CUSTOMER_NAME,'
WorkOrderCtrls     := 'TOTAL_WAFERS_LABEL,TOTAL_WAFERS,TOTAL_REMAINING_LABEL,TOTAL_REMAINING,PERCENT_COMPLETE_LABEL,PERCENT_COMPLETE'
DateRangeCtrls      = 'SCHEDULE_START_DTM_LABEL,SCHEDULE_START_DTM,SCHEDULE_END_DTM_LABEL,SCHEDULE_END_DTM,DAYS_LABEL,DAYS'

Declare function    SRP_Array, Schedule_Services_Dev, Database_Services, Work_Order_Services, SRP_Math
Declare function    Epi_Part_Services, Datetime
Declare subroutine  obj_Appwindow, Center_Window, Schedule_Services_Dev, Form_Services, SRP_Show_Window
Declare subroutine  Form_Services

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
    Transfer Param4 to Param3
    Transfer Param5 to Param4
    Transfer Param6 to Param5
    Transfer Param7 to Param6
    Transfer Param8 to Param7
end

Type = Get_Property(CtrlEntId, 'TYPE')
If Type EQ 'RADIOBUTTON' then
    Convert ' ' to '_' in CtrlEntID
end

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Event WINDOW.CREATE(CreateParam)

    SchedDetNGKey = CreateParam<3>
    Set_Property(@Window, '@SCHEDULE_KEY_ID', SchedDetNGKey)
    GoSub PopulateControls
	Set_Property(@Window : '.DESCRIPTION', 'NEXT', @Window : '.NEW_QTY')
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$)

end event


Event WINDOW.CLOSE(CancelFlag)

end event


Event SAVE_BUTTON.CLICK()

    GoSub UpdateWorkOrderDetails

    ResponseData = '' 
    ReactorNo    = Get_Property(@Window : '.REACTOR_NO', 'TEXT')
    WorkOrder    = Get_Property(@Window : '.WO_NO', 'TEXT')
    StartDTM     = Get_Property(@Window : '.SCHEDULE_START_DTM', 'INVALUE')
    EndDTM       = Get_Property(@Window : '.SCHEDULE_END_DTM', 'INVALUE')
    Description  = Get_Property(@Window : '.DESCRIPTION', 'INVALUE')
    NewQty       = Get_Property(@Window, '@NEW_QTY', 'TEXT')
    ResponseData = ReactorNo : @FM : WorkOrder : @FM : StartDTM : @FM : EndDTM : @FM : Description : @FM : NewQty
    End_Dialog(@Window, ResponseData)

end event


Event CLOSE_BUTTON.CLICK()

    End_Dialog(@Window, '')
   
end event


Event OLE_SUBCLASS.OnOptionClick(CtrlId)

    Send_Event(CtrlId, 'OPTIONS')

end event


Event OLE_SUBCLASS.OnComboClick(CtrlId)

    Send_Event(CtrlId, 'LOSTFOCUS')

end event


Event NEW_QTY.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)
    
    Form_Services('CloseControlMessage', CtrlEntId)
    GoSub EnableSaveButton
        
end event


Event NEW_QTY.LOSTFOCUS(Flag, FocusID)
    
    ValidQty = False$
    NewQty   = Get_Property(CtrlEntID, 'TEXT')
    MinQty   = Get_Property(@Window, '@MIN_QTY')
    MaxQty   = Get_Property(@Window, '@MAX_QTY')
    Begin Case
        Case NewQty EQ ''
            Null
        Case NewQty LE 0
            Set_Property(CtrlEntID, 'FOCUS', True$)
            Set_Property(CtrlEntID, 'SELECTION', 1 : @FM : 999)
            Message = 'New quantity must greater than zero.'
            Form_Services('DisplayControlMessage', Message, 'Invalid Quantity', CtrlEndID, 'VALIDATION', 'RGB(229,20,0)')
        Case NewQty LT MinQty
            Set_Property(CtrlEntID, 'FOCUS', True$)
            Set_Property(CtrlEntID, 'SELECTION', 1 : @FM : 999)
            Message = 'New quantity is below already processed quantity.'
            Form_Services('DisplayControlMessage', Message, 'Invalid Quantity', CtrlEndID, 'VALIDATION', 'RGB(229,20,0)')
        Case NewQty GT MaxQty
            Set_Property(CtrlEntID, 'FOCUS', True$)
            Set_Property(CtrlEntID, 'SELECTION', 1 : @FM : 999)
            Message = 'New quantity exceeds the available quantity.'
            Form_Services('DisplayControlMessage', Message, 'Invalid Quantity', CtrlEndID, 'VALIDATION', 'RGB(229,20,0)')
        Case Otherwise$
            ValidQty = True$
    End Case
    If ValidQty then GoSub UpdateWorkOrderDetails
    
end event


Event DESCRIPTION.CHAR(VirtCode, ScanCode, CtrlKey, ShiftKey, AltKey)
    
    GoSub EnableSaveButton
    
end event


Event CMB_STOP_AFTER.CHANGED(NewData)
    
    GoSub UpdateWorkOrderDetails
    GoSub EnableSaveButton
    
end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SelectAll:

    Set_Property(CtrlEntId, 'SELECTION', 1 : @FM : 999)

return


UpdateScheduleDetails:

    ScheduleDetail = Schedule_Services_Dev('GetScheduleDetail', ScheduleKeyID)
    Set_Property(@Window, '@ORIG_SCHEDULEDETAIL', ScheduleDetail)
    Desc           = ScheduleDetail<SCHED_DET_NG.DESC$>
    Set_Property(@Window : '.DESCRIPTION', 'TEXT', Desc)
    Set_Property(@Window, '@ORIG_DESC', Desc)

return


UpdateCustomerName:

    WoNo            = Get_Property(@Window : '.WO_NO', 'TEXT')
    If WoNo NE '' then
        WorkOrderDetail = Work_Order_Services('GetWorkOrderLogDetail', WONo)
        CustNo          = WorkOrderDetail<WO_LOG_CUST_NO$>
        CompanyRow      = Database_Services('ReadDataRow', 'COMPANY', CustNo)
        CustName        = CompanyRow<42>
    end else
        CustName        = ''
    end
    Set_Property(@Window : '.CUSTOMER_NAME', 'TEXT', CustName)

return


UpdateReactorDetails:

    ReactorNo       = Get_Property(@Window : '.REACTOR_NO', 'TEXT')
    If Index(ReactorNo, '/', 1) then
        // EpiPro reactors formatted as such, "40/42". We must truncate the reactor number to the first reactor.
        ReactorNo = Field(ReactorNo, '/', 1, 1)
        Set_Property(@Window : '.REACTOR_NO', 'TEXT', ReactorNo)
    end
    ReactorDetails  = Schedule_Services_Dev('GetReactorDetails', ReactorNo)
    ReactorType     = ReactorDetails<REACTOR_REACT_TYPE$>
    Begin Case
        Case ReactorType _EQC 'EPP' ; ReactorType = 'EpiPro'
        Case ReactorType _EQC 'GAN' ; ReactorType = 'GaN'
    End Case
    Set_Property(@Window : '.REACTOR_TYPE', 'TEXT', ReactorType)
    Set_Property(@Window : '.REACTOR_SIZE', 'TEXT', ReactorDetails<REACTOR_SUSC_POCKET_SIZE$>)
    FutureEvents    = Schedule_Services_Dev('GetScheduleEvents', Date() + 1, '', ReactorNo, '', False$)
    Set_Property(@Window, '@FUTURE_EVENTS', FutureEvents)

return


InitializeProperties:
    
    SchedDetNGKey           = Get_Property(@Window, '@SCHEDULE_KEY_ID')
    WorkOrder               = Get_Property(@Window : '.WO_NO', 'TEXT')
    ReactNo                 = Get_Property(@Window : '.REACTOR_NO', 'TEXT')
    ReactorType             = Get_Property(@Window : '.REACTOR_TYPE', 'TEXT')
    WorkOrderDetail         = Work_Order_Services('GetWorkOrderLogDetail', WorkOrder)
    RemainingToProcess      = Work_Order_Services('GetUnprocessedWafers', WorkOrder)
    RemainingToSchedule     = Schedule_Services_Dev('GetUnscheduledWfrQty', WorkOrder)
    EpiPart                 = WorkOrderDetail<WO_LOG_EPI_PART_NO$>
    MinutesPerWaferAdjusted = Epi_Part_Services('GetAdjustedWafersPerDayScheduler', EpiPart, ReactorType)
    ScheduledWfrQty         = Schedule_Services_Dev('GetScheduledWfrQty', WorkOrder)
    EventCurrCass           = Schedule_Services_Dev('GetEventCurrRun', SchedDetNGKey)
    Set_Property(@Window, '@WORK_ORDER_DETAIL', WorkOrderDetail)
    Set_Property(@Window, '@REMAINING_TO_PROCESS', RemainingToProcess)
    Set_Property(@Window, '@REMAINING_TO_SCHEDULE', RemainingToSchedule)
    Set_Property(@Window, '@MINUTES_PER_WAFER_ADJUSTED', MinutesPerWaferAdjusted)
    Set_Property(@Window, '@SCHEDULED_WFR_QTY', ScheduledWfrQty)
    Set_Property(@Window, '@EVENT_CURR_CASS', EventCurrCass)
    
return


UpdateWorkOrderDetails:

    SchedDetNGKey = Get_Property(@Window, '@SCHEDULE_KEY_ID')
    If SchedDetNGKey NE '' then
        CurrDTM             = Datetime()
        SchedDetNGRec       = Database_Services('ReadDataRow', 'SCHED_DET_NG', SchedDetNGKey)
        WorkOrder           = Get_Property(@Window : '.WO_NO', 'TEXT')
        ReactNo             = Get_Property(@Window : '.REACTOR_NO', 'TEXT')
        ReactType           = Xlate('REACTOR', ReactNo, 'REACT_TYPE', 'X')
        WorkOrderDetail     = Get_Property(@Window, '@WORK_ORDER_DETAIL')
        EpiPart             = WorkOrderDetail<WO_LOG_EPI_PART_NO$>
        TotalWafers         = SchedDetNGRec<SCHED_DET_NG.EVENT_TOTAL_WFRS$>
        RemainingToProcess  = Get_Property(@Window, '@REMAINING_TO_PROCESS')
        RemainingToSchedule = Get_Property(@Window, '@REMAINING_TO_SCHEDULE')
        If TotalWafers GT 0 then
            Complete = ( (TotalWafers - RemainingToProcess) / TotalWafers) * 100
        end else
            Complete = 0
        end
        ReactorType             = Get_Property(@Window : '.REACTOR_TYPE', 'TEXT')
        MinutesPerWaferAdjusted = Get_Property(@Window, '@MINUTES_PER_WAFER_ADJUSTED')
        ScheduledWfrQty         = Get_Property(@Window, '@SCHEDULED_WFR_QTY')
        AvailableWfrQty         = RemainingToSchedule
        EventCurrCass           = Get_Property(@Window, '@EVENT_CURR_CASS')
        Set_Property(@Window:'.EDL_CURR_CASS', 'TEXT', EventCurrCass)
        EventProcCass           = SchedDetNGRec<SCHED_DET_NG.PROCESSED_CASS$>
        EventUnprocCass         = SchedDetNGRec<SCHED_DET_NG.UNPROCESSED_CASS$>
        CassList                = Get_Property(@Window:'.CMB_STOP_AFTER', 'LIST')
        
        If ReactType EQ 'EPP' then
            WOStepKey     = WorkOrder:'*1'
            RDSRunNos     = Xlate('WO_STEP', WOStepKey, 'ALL_RDS_RUN_NO', 'X')
            RDSNos        = Xlate('WO_STEP', WOStepKey, 'RDS_KEY', 'X')
            StopAfterList = RDSRunNos
            Locate EventCurrCass in StopAfterList using @VM setting vPos then
                If vPos GT 1 then
                    For Pos = 1 to (vPos - 1)
                        StopAfterList = Delete(StopAfterList, 0, 1, 0)
                    Next Pos
                end
            end
            Convert @VM to @FM in StopAfterList
            Set_Property(@Window:'.CMB_STOP_AFTER', 'LIST', StopAfterList)
            CassList     = StopAfterList
            NumStopRuns  = DCount(CassList, @FM)
            StopRun      = Get_Property(@Window:'.CMB_STOP_AFTER', 'TEXT')
            ModifyRunQty = 0
            For Run = 1 to StopRun
                RDSNo         = RDSNos<0, Run>
                ModifyRunQty += Xlate('REACT_RUN', RDSNo, 'WFRS_EPI_LOAD', 'X')
            Next Run
            NewEventQty  = ModifyRunQty
            Set_Property(@Window, '@NEW_QTY', NewEventQty)
            StartDTM     = SchedDetNGRec<SCHED_DET_NG.START_DTM$>
            If StopRun NE '' then
                DurationEstimate       = Schedule_Services_Dev('GetEventDuration', WorkOrder, NewEventQty)
                EndDTM                 = CurrDTM + DurationEstimate
            end else
                EndDTM = SchedDetNGRec<SCHED_DET_NG.STOP_DTM$>
            end
            TotalRunTime = EndDTM - StartDTM
        end else
            If CassList EQ '' then
                StopAfterList = SRP_Array('Join', EventCurrCass, EventUnprocCass, 'OR', @VM)
                StopAfterList = SRP_Array('SortSimpleList', StopAfterList, 'AscendingNumbers', @VM)
                Locate EventCurrCass in StopAfterList using @VM setting vPos then
                    If vPos GT 1 then
                        For Pos = 1 to (vPos - 1)
                            StopAfterList = Delete(StopAfterList, 0, 1, 0)
                        Next Pos
                    end
                end
                Convert @VM to @FM in StopAfterList
                Set_Property(@Window:'.CMB_STOP_AFTER', 'LIST', StopAfterList)
                CassList      = StopAfterList
            end
            NumStopCass       = DCount(CassList, @FM)
            StopCass          = Get_Property(@Window:'.CMB_STOP_AFTER', 'TEXT')
            ModifyCassQty     = 0
            Locate StopCass in CassList using @FM setting fPos then
                For CassIndex = (fPos + 1) to NumStopCass
                    ModifyCassQty += 1
                Next CassIndex
            end
            NewEventQty = TotalWafers - (ModifyCassQty * 25)
            Set_Property(@Window, '@NEW_QTY', NewEventQty)
            StartDTM    = SchedDetNGRec<SCHED_DET_NG.START_DTM$>
            If StopCass NE '' then
                NewRemainingToProcess = (DCount(EventUnprocCass, @VM) - ModifyCassQty) * 25
                DurationEstimate = Schedule_Services_Dev('GetEventDuration', WorkOrder, NewRemainingToProcess)
                EndDTM           = CurrDTM + DurationEstimate
            end else
                EndDTM = SchedDetNGRec<SCHED_DET_NG.STOP_DTM$>
            end
            TotalRunTime = EndDTM - StartDTM
        end
        
        If MinutesPerWaferAdjusted EQ '' then
            Msg(@Window, 'Warning: ' : EpiPart : ' appears to be missing minutes per wafer values for ' : ReactorType)
        end
    end

    If (SchedDetNGKey NE '') then
        Set_Property(@Window : '.EPI_PART', 'TEXT', EpiPart)
        Set_Property(@Window : '.TOTAL_WAFERS', 'TEXT', TotalWafers)
        Set_Property(@Window : '.TOTAL_REMAINING', 'TEXT', RemainingToProcess)
        Set_Property(@Window : '.PERCENT_COMPLETE', 'TEXT', Oconv(Complete, 'MD0[ %]S'))
        Set_Property(@Window : '.SCHEDULE_END_DTM', 'INVALUE', EndDTM)
        RunTimeRounded = SRP_Math('ROUND', TotalRunTime, 1)
        Set_Property(@Window : '.DAYS', 'DEFPROP', RunTimeRounded)
        Set_Property(@Window : '.AVAILABLE_QTY', 'TEXT', AvailableWfrQty)
        Set_Property(@Window : '.OPTIONS_NEW_QTY', 'TEXT', NewQtyLabel)
    end else
        Error_Services('DisplayError')
        Set_Property(@Window : '.EPI_PART', 'TEXT', '')
        Set_Property(@Window : '.TOTAL_WAFERS', 'TEXT', '')
        Set_Property(@Window : '.TOTAL_REMAINING', 'TEXT', '')
        Set_Property(@Window : '.SCHEDULE_WAFERS', 'TEXT', '')
        Set_Property(@Window : '.PERCENT_COMPLETE', 'TEXT', '')
        Set_Property(@Window : '.SCHEDULE_END_DTM', 'INVALUE', '')
        Set_Property(@Window : '.DAYS', 'DEFPROP', '')
    end
    GoSub EnableSaveButton

return


PopulateControls:
    
    Set_Property(@Window, 'TEXT', 'Scheduler Actions - Stop Work Order Event')
    Set_Property(@Window : '.REACTOR_NO', 'NEXT', @Window : '.SCHEDULE_START_DTM')
    Set_Property(@Window : '.DESCRIPTION', 'NEXT', @Window : '.REACTOR_NO')
    SchedDetNGKey = Get_Property(@Window, '@SCHEDULE_KEY_ID')
    SchedDetRec   = Schedule_Services_Dev('GetScheduleDetail', SchedDetNGKey)
    WONo          = SchedDetRec<SCHED_DET_NG.WO_NO$>
    ReactorNo     = SchedDetRec<SCHED_DET_NG.REACT_NO$>
    StartDTM      = SchedDetRec<SCHED_DET_NG.START_DTM$>
    StopDTM       = SchedDetRec<SCHED_DET_NG.STOP_DTM$>
    EventWfrs     = SchedDetRec<SCHED_DET_NG.EVENT_TOTAL_WFRS$>
    ReactType     = Xlate('WO_LOG', WONo, 'REACT_TYPE', 'X')
    If ReactType EQ 'EPP' then
        Set_Property(@Window:'.LBL_OPTIONS_CURRENT_CASS', 'TEXT', 'Current Run: ')
        Set_Property(@Window:'.LBL_OPTIONS_STOP_AFTER', 'TEXT', 'Stop After Run: ')
    end else
        Set_Property(@Window:'.LBL_OPTIONS_CURRENT_CASS', 'TEXT', 'Current Cassette: ')
        Set_Property(@Window:'.LBL_OPTIONS_STOP_AFTER', 'TEXT', 'Stop After Cassette: ')
    end
    Set_Property(@Window : '.WO_NO', 'TEXT', WONo)
    Set_Property(@Window : '.REACTOR_NO', 'TEXT', ReactorNo)
    Set_Property(@Window : '.SCHEDULE_START_DTM', 'INVALUE', StartDTM)
    Set_Property(@Window : '.SCHEDULE_END_DTM', 'INVALUE', StopDTM)
    Set_Property(@Window : '.SCHEDULE_WAFERS', 'INVALUE', EventWfrs)
    GoSub UpdateScheduleDetails
    GoSub UpdateReactorDetails
    GoSub UpdateCustomerName
    GoSub InitializeProperties
    GoSub UpdateWorkOrderDetails
    Set_Property(@Window : '.REACTOR_NO', 'FOCUS', True$)
    
return


EnableSaveButton:

    StopAfterCass = Get_Property(@Window : '.CMB_STOP_AFTER', 'TEXT')
    ButtonEnabled = (StopAfterCass NE '')
    Set_Property(@Window : '.SAVE_BUTTON', 'ENABLED', ButtonEnabled)
    
return


