Compile function NDW_PACKAGING_NEW_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_Packaging_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)
        10/11/19    djs     Created initial commuter module.
        10/07/21	DPC		rearranged layout, added delay after Cassette1 scan
                                            
***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_PACKAGING_NEW

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES
$insert LSL_USERS_EQUATES
$insert MESSAGE_BOX_EQUATES
$insert SRP_POPUP_EQUATES
$INSERT LOGICAL

Equ MSG_WIDTH$ to 600

Declare subroutine  SRP_Show_Window, ErrMsg, Packaging_Services, Delay
Declare function    Database_Services, Security_Services, MemberOf, Form_Services, Scan_Services, Packaging_Services
Declare function    Message_Box, SRP_Get_Window_Rect

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
end

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)
    EventFlow = 1
End Event


Event EDL_USER_ID_SCAN.LOSTFOCUS(Flag, FocusID)
    If Flag EQ 1 then
        ScanData = Get_Property(CtrlEntID, 'TEXT')
        If ScanData NE '' then
            ScanData = Packaging_Services('ProcessScanDataNew', ScanData, 'EMPLOYEE')
            If Error_Services('NoError') then
                Set_Property(CtrlEntID, 'TEXT', ScanData)
                Set_Property(@Window:'.EDL_CASS_SCAN_1', 'ENABLED', True$)
                Set_Property(@Window:'.EDL_CASS_SCAN_1', 'FOCUS', True$)
                Set_Property(CtrlEntID, 'ENABLED', False$)
            end else
                GoSub ClearForm
                ErrorMessage = Error_Services('GetMessage')
                MsgStruct = "" 
                MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
            end
        end
    end
    
end event


Event EDL_CASS_SCAN_1.LOSTFOCUS(Flag, FocusID)
    If Flag EQ 1 then
        ScanData = Get_Property(CtrlEntID, 'TEXT')
        If ScanData NE '' then
            ScanData = Packaging_Services('ProcessScanDataNew', ScanData, 'CASSETTE_1')
            If Error_Services('NoError') then
                Set_Property(CtrlEntID, 'TEXT', ScanData)
                Set_Property(@Window:'.EDL_POLY_SCAN', 'ENABLED', True$)
                Set_Property(@Window:'.EDL_POLY_SCAN', 'FOCUS', True$)
                Set_Property(CtrlEntID, 'ENABLED', False$)
            end else
                GoSub ClearForm
                ErrorMessage = Error_Services('GetMessage')
                MsgStruct = "" 
                MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)                
            end
        end
    end
    
end event


Event EDL_CASS_SCAN_2.LOSTFOCUS(Flag, FocusID)
    
    If Flag EQ 1 then
        ScanData = Get_Property(CtrlEntID, 'TEXT')
        If ScanData NE '' then
            FirstCassID = Get_Property(@Window:'.EDL_CASS_SCAN_1', 'TEXT')
            OperatorID  = Get_Property(@Window:'.EDL_USER_ID_SCAN', 'TEXT')

            ScanData    = Packaging_Services('ProcessScanDataNew', ScanData, 'CASSETTE_2', FirstCassID, OperatorID)
            If Error_Services('NoError') then
                Set_Property(CtrlEntID, 'TEXT', ScanData)
                Set_Property(@Window:'.EDL_PASSWORD_SCAN', 'ENABLED', True$)
                Set_Property(@Window:'.EDL_PASSWORD_SCAN', 'FOCUS', True$)
                Set_Property(CtrlEntID, 'ENABLED', False$)
            end else
                GoSub ClearForm
                ErrorMessage = Error_Services('GetMessage')
                MsgStruct = "" 
                MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)                
            end
        end
    end
    
end event


Event EDL_POLY_SCAN.LOSTFOCUS(Flag, FocusID)
    
    If Flag EQ 1 then
        ScanData = Get_Property(CtrlEntID, 'TEXT')
        If ScanData NE '' then
            ScanData = Packaging_Services('ProcessScanDataNew', ScanData, 'POLY')
            If Error_Services('NoError') then
                Set_Property(CtrlEntID, 'TEXT', ScanData)
         		Utility("CURSOR", "H")
				*Gosub ShowPopup
				Delay(30)
				Utility("CURSOR", "A")

                Set_Property(@Window:'.EDL_TRILAM_SCAN', 'ENABLED', True$)
                Set_Property(@Window:'.EDL_TRILAM_SCAN', 'FOCUS', True$)
                Set_Property(CtrlEntID, 'ENABLED', False$)
            end else
                ScanData = Get_Property(CtrlEntID, 'TEXT')
                If ScanData[1, 3] EQ 'PWD' then Set_Property(CtrlEntID, 'TEXT', '')
                ErrorMessage = Error_Services('GetMessage')
                MsgStruct = "" 
                MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
                Set_Property(CtrlEntID, 'FOCUS', True$)
                SelectAll = 1:@FM:65534
                Set_Property(CtrlEntID, 'SELECTION', SelectAll)
            end
        end
    end
    
end event


Event EDL_TRILAM_SCAN.LOSTFOCUS(Flag, FocusID)
    
    If Flag EQ 1 then
        ScanData = Get_Property(CtrlEntID, 'TEXT')
        If ScanData NE '' then
            ScanData = Packaging_Services('ProcessScanDataNew', ScanData, 'TRILAM')
            If Error_Services('NoError') then
                Set_Property(CtrlEntID, 'TEXT', ScanData)
                Set_Property(@Window:'.EDL_CASS_SCAN_2', 'ENABLED', True$)
                Set_Property(@Window:'.EDL_CASS_SCAN_2', 'FOCUS', True$)
                Set_Property(CtrlEntID, 'ENABLED', False$)
            end else
                ScanData = Get_Property(CtrlEntID, 'TEXT')
                If ScanData[1, 3] EQ 'PWD' then Set_Property(CtrlEntID, 'TEXT', '')
                ErrorMessage = Error_Services('GetMessage')
                MsgStruct = "" 
                MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
                Set_Property(CtrlEntID, 'FOCUS', True$)
                SelectAll = 1:@FM:65534
                Set_Property(CtrlEntID, 'SELECTION', SelectAll)
            end
        end
    end
    
end event

Event EDL_PASSWORD_SCAN.LOSTFOCUS(Flag, FocusID)
    If Flag EQ 1 then
        ScanData = Get_Property(CtrlEntID, 'TEXT')
        If ScanData NE '' then
            Username = Get_Property(@Window:'.EDL_USER_ID_SCAN', 'TEXT')
            ScanData = Packaging_Services('ProcessScanDataNew', ScanData, 'PASSWORD', Username)
            If Error_Services('NoError') then
                Set_Property(CtrlEntID, 'TEXT', ScanData)
                
                // Complete packaging
                CassetteID = Get_Property(@Window:'.EDL_CASS_SCAN_1', 'TEXT')
                //replace period with asterisk for GaN cassettes
                Convert '.' to '*' in CassetteID
                OperatorID   = Get_Property(@Window:'.EDL_USER_ID_SCAN', 'TEXT')
                *Packaging_Services('CompletePackaging', CassetteID, OperatorID)

                If Error_Services('NoError') then
                    SuccessMessage = 'Bagging process complete!'
                    MsgStruct = "" 
                    MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                    Msg(@Window, MsgStruct, 'PROCESS_COMPLETE', '', SuccessMessage)       
                end else        
                    ErrorMessage = Error_Services('GetMessage')        
                    MsgStruct = "" 
                    MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                    Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
                end
               
                GoSub ClearForm
                
            end else
                ErrorMessage = Error_Services('GetMessage')
                MsgStruct = "" 
                MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
                Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
                Set_Property(CtrlEntID, 'FOCUS', True$)
                SelectAll = 1:@FM:65534
                Set_Property(CtrlEntID, 'SELECTION', SelectAll)
            end
        end
    end
    
end event


Event PUB_CLEAR_FORM.CLICK()
    GoSub ClearForm
end event


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

ClearForm:

    Set_Property(@Window:'.EDL_USER_ID_SCAN'     , 'TEXT'   , '')
    Set_Property(@Window:'.EDL_CASS_SCAN_1'      , 'TEXT'   , '')
    Set_Property(@Window:'.EDL_CASS_SCAN_2'      , 'TEXT'   , '')
    Set_Property(@Window:'.EDL_POLY_SCAN'        , 'TEXT'   , '')
    Set_Property(@Window:'.EDL_TRILAM_SCAN'      , 'TEXT'   , '')
    Set_Property(@Window:'.EDL_PASSWORD_SCAN'    , 'TEXT'   , '')
    Set_Property(@Window:'.EDL_USER_ID_SCAN'     , 'ENABLED', True$)
    Set_Property(@Window:'.EDL_CASS_SCAN_1'      , 'ENABLED', -1)
    Set_Property(@Window:'.EDL_CASS_SCAN_2'      , 'ENABLED', -1)
    Set_Property(@Window:'.EDL_POLY_SCAN'        , 'ENABLED', -1)
    Set_Property(@Window:'.EDL_TRILAM_SCAN'      , 'ENABLED', -1)
    Set_Property(@Window:'.EDL_PASSWORD_SCAN'    , 'ENABLED', -1)
    Set_Property(@Window:'.PUB_COMPLETE_BAGGING' , 'ENABLED', -1)
    Set_Property(@Window:'.EDL_USER_ID_SCAN', 'FOCUS', True$)

return

* ShowPopup:
* 
*    GoSub HidePopup
* 	PopupCtrl   = @Window : '.OLE_POPUP'
* 	
*    BackColor   = Get_Property(@Window, 'BACKCOLOR')
*    ForeColor   = Get_Property(@Window, 'FORECOLOR')
*    Position	= Get_Property(@Window, 'POSITION')
*    CursorXPos  = Field(Position, ',', 1, 1)
*    CursorYPos  = Field(Position, ',', 2, 1)
*    FormSize    = SRP_Get_Window_Rect(@Window)
*    FormXPos    = FormSize<1>
*    FormYPos    = FormSize<2>
*    CtrlSize    = Get_Property(CtrlEntID, 'SIZE')
*    CtrlXPos    = CtrlSize<1>
*    CtrlYPos    = CtrlSize<2>
*    PopupWidth  = 0
*    PopupHeight = 0
*    XPadding    = 10
*    YPadding    = 10
*    XOffset     = 10
*    YOffset     = 55
*    PopupXPos   = FormXPos + CtrlXPos + CursorXPos + XOffset
*    PopupYPos   = FormYPos + CtrlYPos + CursorYPos + YOffset
* 
* 	Delay = 30000
* 	BackColor   = 153 + (255*256) + (153*65536)
* 
* 	popupMsg = 'Do not just stand there dummy!'
*    ItemData   = popupMsg
*    NumLines    = DCount(ItemData, @FM)
* 	ItemList    = ''
* 	If ItemData NE '' then
*		For Each Item in ItemData using @FM setting ItemCount
*			ItemLength = Len(Item)
*			PopupWidth += (25 * ItemLength)
*			rows = INT(PopupWidth /300)
*			if (PopupWidth) GE 300 then
*				PopupWidth = 400
*				PopupHeight = (30 * rows)
*			end else
*				PopupHeight = 50
*			end
*			
*			ItemList<ItemCount,SRP_POPUP.RECTANGLE,1> = XPadding                        ; // X Position of item     
*			ItemList<ItemCount,SRP_POPUP.RECTANGLE,2> = (20 * (ItemCount - 1) )         ; // Y Position of item      
*			ItemList<ItemCount,SRP_POPUP.RECTANGLE,3> = PopupWidth - (2 * XPadding)		; // Width           
*			ItemList<ItemCount,SRP_POPUP.RECTANGLE,4> = PopupHeight - (2 * YPadding)    ; // Height
*			ItemList<ItemCount,SRP_POPUP.CAPTION>     = Item
*			ItemList<ItemCount,SRP_POPUP.COLORS>      = Forecolor
*			ItemList<ItemCount,SRP_POPUP.FONT>        = 'Tahoma' : @SVM : '16' : @SVM : '700' : @SVM : '0' : @SVM : '0'
*			ItemList<ItemCount,SRP_POPUP.ALIGNMENT>   = 'Center' : @SVM : 'Center'       
*		Next Item
* 	end	
* 	
* 	Set_Property(PopupCtrl, 'OLE.Size', Size)	
* 	Set_Property(PopupCtrl, 'OLE.Opacity', 255)
* 	Set_Property(PopupCtrl, 'OLE.ShowDelay', Delay)
* 	Set_Property(PopupCtrl, 'OLE.Theme', 'Custom')
*    Set_Property(PopupCtrl, 'OLE.Background', 'Vertical(Gradient(' : Backcolor : ', ' : Backcolor : '), Border(' : Backcolor : ' L=20))' : @FM : 'None' : @FM : 'None')
*    Set_Property(PopupCtrl, 'OLE.ItemList', ItemList)
*    Set_Property(PopupCtrl, 'OLE.Size', 0 : @FM : 0 : @FM : PopupWidth : @FM : PopupHeight)   
*    Send_Message(PopupCtrl, 'OLE.ShowAt', PopupXPos, PopupYPos+25)
* 	
* return
* 
* HidePopup:
* 
*    PopupCtrl       = @Window : '.OLE_POPUP'
*    PopupVisible    = Get_Property(PopupCtrl, 'OLE.Visible')
*    If PopupVisible EQ True$ then
*        Send_Message(PopupCtrl, 'OLE.Close')
*    end
* 
* return


