Function NDW_Makeup_Wafers_Events(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    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        :   NDW_Makeup_Wafers_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/18    dmb     Created initial commuter module.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#Window NDW_MAKEUP_WAFERS

$insert APP_INSERTS
$insert EVENT_SETUP
$insert WO_MAT_EQUATES
$insert MSG_EQUATES

Declare subroutine  SRP_EditTable_Manager, SRP_Show_Window, SRP_Stopwatch
Declare function    SRP_EditTable_Manager, Database_Services, Material_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

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$


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

Event WINDOW.CREATE(CreateParam)

    ThisWOMatKey    = CreateParam
    ThisWorkOrderNo = ThisWOMatKey[1, '*']
    ThisCassetteNo  = ThisWOMatKey[Col2() + 1, '*']
    
    ReactorType = Xlate('WO_LOG', ThisWorkOrderNo, 'REACT_TYPE', 'X')
    IsEpiPro    = (ReactorType EQ 'EPP')
    Set_Property(@Window, '@EPIPRO', IsEpiPro)
    
    MsgDef = ''
    MsgDef<MTEXT$> = 'Scanning available makeup boxes.' :@SVM: 'Please wait...'
    MsgDef<MTYPE$> = 'U'
    MsgDef<MJUST$> = 'C'
    MsgDef<MCOL$>  = '-2'
    MsgDef<MROW$>  = '-2'
    MsgUp = Msg(@Window, MsgDef)
    
    MakeupWafers    = Material_Services('GetAvailableMakeupWafers', ThisWorkOrderNo, False$)
   
	// Remove cassette being backfilled from available list
	AvailMuCass = ''
	NoFQAMuCass = ''
	NoBatchMuCass = ''
	NoLabelMuCass = ''
	
	If MakeupWafers<1> NE '' then
	    For each Row in  MakeupWafers<1> using @VM setting RowPos
	        MuWoMatKey = Row<0, 0, 1> : '*' : Row<0, 0, 2>
			If (MuWoMatKey NE ThisWOMatKey) then AvailMuCass<0, -1> = Row
	    Next Row
	end
	
	If MakeupWafers<2> NE '' then
	    For each Row in MakeupWafers<2> using @VM setting RowPos
	        MuWoMatKey = Row<0, 0, 1> : '*' : Row<0, 0, 2>
			If (MuWoMatKey NE ThisWOMatKey) then NoFQAMuCass<0, -1> = Row
	    Next Row
	end
	
	If MakeupWafers<3> NE '' then
	    For each Row in MakeupWafers<3> using @VM setting RowPos
	        MuWoMatKey = Row<0, 0, 1> : '*' : Row<0, 0, 2>
			If (MuWoMatKey NE ThisWOMatKey) then NoBatchMuCass<0, -1> = Row
	    Next Row
	end
	
	If MakeupWafers<4> NE '' then
	    For each Row in  MakeupWafers<4> using @VM setting RowPos
	        MuWoMatKey = Row<0, 0, 1> : '*' : Row<0, 0, 2>
			If (MuWoMatKey NE ThisWOMatKey) then NoLabelMuCass<0, -1> = Row
	    Next Row
	end
	
	MakeupWafers<1> = AvailMuCass
	MakeupWafers<2> = NoFQAMuCass
	MakeupWafers<3> = NoBatchMuCass
	MakeupWafers<4> = NoLabelMuCass

    Msg(@Window, MsgUp)
    
    GoSub Setup_OLE_Controls

    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)

end event


Event WINDOW.READ()

    SRP_EditTable_Manager('Read', @Window : '.OLE_EDT_PROCEDURES', Record)

end event


Event WINDOW.CLOSE(CancelFlag)

end event


Event WINDOW.CLEAR(bSaveKey, bSuppressWarning, bMaintainFocus, CtrlIDFocus)

    SRP_EditTable_Manager('Clear')

end event


Event PUB_OK.CLICK()

    SelPos  = Get_Property(@Window : '.OLE_RPT_SIGNED_WAFERS', 'OLE.SelPos')
    RowData = Get_Property(@Window : '.OLE_RPT_SIGNED_WAFERS', 'OLE.RowData[' : SelPos : ']')
    If RowData EQ '' then
        // User is asking to convert the current cassette to a makeup box.
        Result  = True$
    end else
        // User has selected the makeup cassette to use for backfilling.
        EpiPro = Get_Property(@Window, '@EPIPRO')
        If EpiPro then
            Result = RowData<1> : '*1*' : RowData<2>
        end else
            Result = RowData<1> : '*' : RowData<2>
        end
    end

    End_Dialog(@Window, Result)

end event


Event PUB_CANCEL.CLICK()

    End_Dialog(@Window, '')

end event


Event OLE_EDT_PROCEDURES.LostFocus()

    SRP_EditTable_Manager('LostFocus', CtrlEntId)

end event


Event OLE_SUBCLASS.OnComboClick(CtrlId, Sel, Value)

    Send_Event(CtrlId, 'LOSTFOCUS')

end event


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

Setup_OLE_Controls:

    Qualify     = ''
    Qualify<1>  = 1
    Qualify<4>  = 0

    RptSignedCtrl   = @Window : '.OLE_RPT_SIGNED_WAFERS'
    PicSignedCtrl   = @Window : '.OLE_PIC_AVAILABLE_DIVIDER'
    RptUnsignedCtrl = @Window : '.OLE_RPT_UNSIGNED_WAFERS'
    PicUnsignedCtrl = @Window : '.OLE_PIC_UNAVAILABLE_DIVIDER'
    RptNoBatchCtrl  = @Window : '.OLE_RPT_NO_BATCH_WAFERS'
    PicNoBatchCtrl  = @Window : '.OLE_PIC_NO_BATCH_DIVIDER'
    RptNoLabelCtrl  = @Window : '.OLE_RPT_NO_PACK_LABEL'
    PicNoLabelCtrl  = @Window : '.OLE_PIC_NO_PACK_LABEL'
    
    
    ColumnList      = ''
    ColumnList<1>   = 'WO No' : @VM : 'TEXT' : @VM : 100 : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<2>   = 'Cass No' : @VM : 'TEXT' : @VM : 100 : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<3>   = 'SAP Batch No' : @VM : 'TEXT' : @VM : 150 : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<4>   = 'MU Ps No' : @VM : 'TEXT' : @VM : 100 : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Center' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<5>   = 'Cust Part No' : @VM : 'TEXT' : @VM : 200 : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Left' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<6>   = 'Curr Wfr Qty' : @VM : 'TEXT' : @VM : 100 : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Right' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    ColumnList<7>   = 'RDS No' : @VM : 'TEXT' : @VM : 100 : @VM : @VM : @VM : @VM : @VM : @VM : @VM : 'Right' : @VM : @VM : @VM : @VM : 'Segoe UI,9'
    Set_Property(RptSignedCtrl, 'OLE.ColumnList', ColumnList)
    Set_Property(RptUnsignedCtrl, 'OLE.ColumnList', ColumnList)
    Set_Property(RptNoBatchCtrl, 'OLE.ColumnList', ColumnList)
    Set_Property(RptNoLabelCtrl, 'OLE.ColumnList', ColumnList)
    
    SignedWafers   = ''
    UnsignedWafers = ''
    NoBatchWafers  = ''
    NoLabelWafers  = ''
    If MakeupWafers NE '' then
        SignedWafers   = MakeupWafers<1>
        Convert @VM to @FM in SignedWafers
        Convert @SVM to @VM in SignedWafers
        UnsignedWafers = MakeupWafers<2>
        Convert @VM to @FM in UnsignedWafers
        Convert @SVM to @VM in UnsignedWafers
        NoBatchWafers  = MakeupWafers<3>
        Convert @VM to @FM in NoBatchWafers
        Convert @SVM to @VM in NoBatchWafers
        NoLabelWafers  = MakeupWafers<4>
        Convert @VM to @FM in NoLabelWafers
        Convert @SVM to @VM in NoLabelWafers
    end

    // Get the design time form size. This will be adjusted based on various conditions.
    FormSize            = Get_Property(@Window, 'SIZE')
    SignedRptCtrlSize   = Get_Property(RptSignedCtrl, 'SIZE')
    PicSignedCtrlSize   = Get_Property(PicSignedCtrl, 'SIZE')
    RptUnsignedCtrlSize = Get_Property(RptUnsignedCtrl, 'SIZE')
    PicUnsignedCtrlSize = Get_Property(PicUnsignedCtrl, 'SIZE')
    RptNoBatchCtrlSize  = Get_Property(RptNoBatchCtrl, 'SIZE')
    PicNoBatchCtrlSize  = Get_Property(PicNoBatchCtrl, 'SIZE')
    RptNoLabelCtrlSize  = Get_Property(RptNoLabelCtrl, 'SIZE')
    PicNoLabelCtrlSize  = Get_Property(PicNoLabelCtrl, 'SIZE')
    NumSignedWafers     = DCount(SignedWafers, @FM)
    NumUnsignedWafers   = DCount(UnsignedWafers, @FM)
    NumNoBatchWafers    = DCount(NoBatchWafers, @FM)
    NumNoLabelWafers    = DCount(NoLabelWafers, @FM)

    If NumSignedWafers EQ 0 then
        // If there are no signed wafers are available, display the prompt that asks the user to convert to a makeup box.
        Set_Property(PicSignedCtrl , 'VISIBLE', False$)
        Set_Property(RptSignedCtrl, 'VISIBLE', False$)
        Set_Property(@Window : '.OLE_PIC_NO_WAFERS', 'VISIBLE', True$)
        If NumUnsignedWafers GT 0 then
            // Increase the height of the form to display the unsigned wafers ReportTable control. Move the controls
            // down to display below the makeup box message.
            FormSize<4>             = FormSize<4> + 72
            RptUnsignedCtrlSize<2>  = RptUnsignedCtrlSize<2> + 146
            PicUnsignedCtrlSize<2>  = PicUnsignedCtrlSize<2> + 146
            Set_Property(RptUnsignedCtrl, 'SIZE', RptUnsignedCtrlSize)
            Set_Property(PicUnsignedCtrl, 'SIZE', PicUnsignedCtrlSize)
        end
        If NumNoBatchWafers GT 0 then
            // Increase the height of the form to display the no batch wafers ReportTable control. Move the controls
            // down to display below the makeup box message.
            FormSize<4>             = FormSize<4> + 72
            RptNoBatchCtrlSize<2>  = RptNoBatchCtrlSize<2> + 146
            PicNoBatchCtrlSize<2>  = PicNoBatchCtrlSize<2> + 146
            Set_Property(RptNoBatchCtrl, 'SIZE', RptNoBatchCtrlSize)
            Set_Property(PicNoBatchCtrl, 'SIZE', PicNoBatchCtrlSize)
        end
        If NumNoBatchWafers GT 0 then
            // Increase the height of the form to display the no label wafers ReportTable control. Move the controls
            // down to display below the makeup box message.
            FormSize<4>             = FormSize<4> + 72
            RptNoLabelCtrlSize<2>  = RptNoLabelCtrlSize<2> + 146
            PicNoLabelCtrlSize<2>  = PicNoLabelCtrlSize<2> + 146
            Set_Property(RptNoLabelCtrl, 'SIZE', RptNoLabelCtrlSize)
            Set_Property(PicNoLabelCtrl, 'SIZE', PicNoLabelCtrlSize)
        end
    end else
        Set_Property(RptSignedCtrl, 'OLE.List', SignedWafers)
        If NumSignedWafers GT 1 then
            SignedRptCtrlSize<4>    = SignedRptCtrlSize<4> + (20 * (NumSignedWafers - 1))
            Set_Property(RptSignedCtrl, 'SIZE', SignedRptCtrlSize)
        end
    end

    // Adjust unsigned wafers ReportTable
    If NumUnsignedWafers EQ 0 then
        Set_Property(PicUnsignedCtrl, 'VISIBLE', False$)
        Set_Property(RptUnsignedCtrl, 'VISIBLE', False$)
        If (NumSignedWafers GT 0) then
            FormSize<4> = FormSize<4> - 74
        end
    end else
        Set_Property(RptUnsignedCtrl, 'OLE.List', UnsignedWafers)
        If NumUnsignedWafers GT 1 then
            // Add more height to display extra rows.
            RptUnsignedCtrlSize<4>  = RptUnsignedCtrlSize<4> + (20 * (NumUnsignedWafers - 1))
        end
        If NumSignedWafers EQ 0 then
            // There are no signed wafers. Move the unsigned wafers ReportTable up.
            RptUnsignedCtrlSize<2> = RptUnsignedCtrlSize<2> - 74
            PicUnsignedCtrlSize<2> = PicUnsignedCtrlSize<2> - 74
        end else
            If NumSignedWafers GT 1 then
                // Signed wafers has been made taller. Move the unsigned wafers ReportTable down further.
                RptUnsignedCtrlSize<2> = RptUnsignedCtrlSize<2> + (20 * (NumSignedWafers - 1))
                PicUnsignedCtrlSize<2> = PicUnsignedCtrlSize<2> + (20 * (NumSignedWafers - 1))
            end
        end
        Set_Property(RptUnsignedCtrl, 'SIZE', RptUnsignedCtrlSize)
        Set_Property(PicUnsignedCtrl, 'SIZE', PicUnsignedCtrlSize)
    end
    
    // Adjust no batch ReportTable
    If NumNoBatchWafers EQ 0 then
        Set_Property(PicNoBatchCtrl, 'VISIBLE', False$)
        Set_Property(RptNoBatchCtrl, 'VISIBLE', False$)
        FormSize<4> = FormSize<4> - 74
    end else
        Set_Property(RptNoBatchCtrl, 'OLE.List', NoBatchWafers)
        If NumNoBatchWafers GT 1 then
            // Add more height to display extra rows.
            RptNoBatchCtrlSize<4>  = RptNoBatchCtrlSize<4> + (20 * (NumNoBatchWafers - 1))
        end
        If NumSignedWafers EQ 0 then
            // There are no signed wafers. Move the no batch ReportTable up.
            RptNoBatchCtrlSize<2>  = RptNoBatchCtrlSize<2> - 74
            PicNoBatchCtrlSize<2>  = PicNoBatchCtrlSize<2> - 74
        end else
            If NumSignedWafers GT 1 then
                // Signed wafers has been made taller. Move the no batch ReportTable down further.
                RptNoBatchCtrlSize<2>  = RptNoBatchCtrlSize<2> + (20 * (NumSignedWafers - 1))
                PicNoBatchCtrlSize<2>  = PicNoBatchCtrlSize<2> + (20 * (NumSignedWafers - 1))
            end
        end
        If NumUnsignedWafers EQ 0 then
            // There are no unsigned wafers. Move the no batch ReportTable up.
            RptNoBatchCtrlSize<2>  = RptNoBatchCtrlSize<2> - 74
            PicNoBatchCtrlSize<2>  = PicNoBatchCtrlSize<2> - 74
        end else
            If NumUnsignedWafers GT 1 then
                // Unsigned wafers has been made taller. Move the no batch ReportTable down further.
                RptNoBatchCtrlSize<2>  = RptNoBatchCtrlSize<2> + (20 * (NumUnsignedWafers - 1))
                PicNoBatchCtrlSize<2>  = PicNoBatchCtrlSize<2> + (20 * (NumUnsignedWafers - 1))
            end
        end
        Set_Property(RptNoBatchCtrl, 'SIZE', RptNoBatchCtrlSize)
        Set_Property(PicNoBatchCtrl, 'SIZE', PicNoBatchCtrlSize)
    end
    
    // Adjust no Label ReportTable
    If NumNoLabelWafers EQ 0 then
        Set_Property(PicNoLabelCtrl, 'VISIBLE', False$)
        Set_Property(RptNoLabelCtrl, 'VISIBLE', False$)
        FormSize<4> = FormSize<4> - 74
    end else
        Set_Property(RptNoLabelCtrl, 'OLE.List', NoLabelWafers)
        If NumNoLabelWafers GT 1 then
            // Add more height to display extra rows.
            RptNoLabelCtrlSize<4>  = RptNoLabelCtrlSize<4> + (20 * (NumNoLabelWafers - 1))
        end
        If NumSignedWafers EQ 0 then
            // There are no signed wafers. Move the no Label ReportTable up.
            RptNoLabelCtrlSize<2>  = RptNoLabelCtrlSize<2> - 74
            PicNoLabelCtrlSize<2>  = PicNoLabelCtrlSize<2> - 74
        end else
            If NumSignedWafers GT 1 then
                // Signed wafers has been made taller. Move the no Label ReportTable down further.
                RptNoLabelCtrlSize<2>  = RptNoLabelCtrlSize<2> + (20 * (NumSignedWafers - 1))
                PicNoLabelCtrlSize<2>  = PicNoLabelCtrlSize<2> + (20 * (NumSignedWafers - 1))
            end
        end
        If NumUnsignedWafers EQ 0 then
            // There are no unsigned wafers. Move the no Label ReportTable up.
            RptNoLabelCtrlSize<2>  = RptNoLabelCtrlSize<2> - 74
            PicNoLabelCtrlSize<2>  = PicNoLabelCtrlSize<2> - 74
        end else
            If NumUnsignedWafers GT 1 then
                // Unsigned wafers has been made taller. Move the no Label ReportTable down further.
                RptNoLabelCtrlSize<2>  = RptNoLabelCtrlSize<2> + (20 * (NumUnsignedWafers - 1))
                PicNoLabelCtrlSize<2>  = PicNoLabelCtrlSize<2> + (20 * (NumUnsignedWafers - 1))
            end
        end
        If NumNoBatchWafers EQ 0 then
            RptNoLabelCtrlSize<2>  = RptNoLabelCtrlSize<2> - 74
            PicNoLabelCtrlSize<2>  = PicNoLabelCtrlSize<2> - 74
        end else
            If NumNoBatchWafers GT 1 then
                // Unsigned wafers has been made taller. Move the no Label ReportTable down further.
                RptNoLabelCtrlSize<2>  = RptNoLabelCtrlSize<2> + (20 * (NumNoBatchWafers - 1))
                PicNoLabelCtrlSize<2>  = PicNoLabelCtrlSize<2> + (20 * (NumNoBatchWafers - 1))
            end
        end
        Set_Property(RptNoLabelCtrl, 'SIZE', RptNoLabelCtrlSize)
        Set_Property(PicNoLabelCtrl, 'SIZE', PicNoLabelCtrlSize)
    end
    
    AdditionalRowsAdj   = 0
    If NumSignedWafers GT 1 then AdditionalRowsAdj += (NumSignedWafers - 1) * 20
    If NumUnsignedWafers GT 1 then AdditionalRowsAdj += (NumUnsignedWafers - 1) * 20
    If NumNoBatchWafers GT 1 then AdditionalRowsAdj += (NumNoBatchWafers - 1) * 20
    FormSize<4> = FormSize<4> + AdditionalRowsAdj
    Set_Property(RptSignedCtrl, 'OLE.List', SignedWafers)
    Set_Property(RptUnsignedCtrl, 'OLE.List', UnsignedWafers)
    Set_Property(RptNoBatchCtrl, 'OLE.List', NoBatchWafers)

    Ctrl       = @Window : '.OLE_PIC_NO_WAFERS'
    SAPBatchNo = Xlate('WO_MAT', ThisWOMatKey, 'SAP_BATCH_NO', 'X')
    
    IsEpiPro    = Get_Property(@Window, '@EPIPRO')
	If IsEpiPro then
		WMOKey    = Xlate('WO_MAT', ThisWOMatKey, 'WMO_KEY', 'X')
		MakeupBox = Xlate('WM_OUT', WMOKey, 'MAKEUP_BOX', 'X')
	end else
		MakeupBox = Xlate('WO_MAT', ThisWOMatKey, 'MAKEUP_BOX', 'X')
	end
	
	Caption = 'There are no makeup wafers available for this cassette.'
	If MakeupBox then
        Set_Property(@Window:'.PUB_OK', 'ENABLED', False$)
    end else
        Caption := @TM : 'Click OK to convert this to a makeup box.'
    end
    Set_Property(Ctrl, 'OLE.Caption', Caption)

return

