Function NDW_PTI_MAT_SCAN_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 SRP Computer Solutions, Inc.

    Name        :   NDW_PTI_MAT_SCAN_EVENTS

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

    Notes       :   This procedure supports the same-named form.  Form is single use, for moving lots with 1K*PTI location
					angd warehouse code.  The form must 

    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)
        12/03/20    DPC     Original programmer.

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

#pragma precomp SRP_PreCompiler
#window NDW_PTI_MAT_SCAN

$insert EVENT_SETUP
$insert LOGICAL
$insert MSG_EQUATES
$insert POPUP_EQUATES
$insert	REACT_RUN_EQUATES
$insert RDS_EQUATES
$insert WO_MAT_EQUATES
$insert APPCOLORS
$insert MESSAGE_BOX_EQUATES

Equ CRLF$ to \0D0A\
Equ MSG_WIDTH$ to 600
Equ EditTable$ to @Window:'.OLE_EDT_SCANDATA'
Equ Cassette1$ to @Window:'.EDL_CASSETTE1_SCAN'
Equ Cassette2$ to @Window:'.EDL_CASSETTE2_SCAN'
Equ Supplier$ to @Window:'.EDL_SUPPLIER_SCAN'

Declare subroutine  Set_Property, Send_Event, Post_Event, Send_Message, SendMessage, Error_Services
Declare subroutine	Obj_Wo_Mat_Log, Errmsg, Utility, SRP_Show_Window, Material_Movement_Services, PlaceDialog
Declare function    Get_Property, Send_Message, SendMessage, Material_Movement_Services, Error_Services,
Declare function	Popup, obj_WO_Mat, Database_Services, Utility, Rds_Services, Message_Box

*Subclass = @Window : '.OLE_SUBCLASS'

// 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 else
	// Event not implemented
end

Return EventFlow or 1

*global
ScanField = ''

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

Event WINDOW.CREATE(CreateParam)
	
	CurrVer = Xlate('APP_INFO', 'LSL2_VERSION', '', 'X')
	UserVer = Xlate('LSL_USERS', @User4, 'LSL2_VERSION', 'X')
	If UserVer NE CurrVer then
		// Error message and close form
		ErrMsg = 'OpenInsight Version Error':@FM:'You are running an outdated version of OpenInsight and cannot release material until you restart your session.'
		Msg(@Window, '', 'OK', '', ErrMsg)
		Post_Event(@Window, 'CLOSE')
	end else
		GoSub SetupOLEControls
		GoSub ResetForm
		PlaceDialog(-2, -2)
		MsgStruct              = '' 
		MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
	end
	
end event


Event OLE_BTN_CLEAR.OnClick(Point, Button, Shift, Ctrl)
	
	*get count of unprocessed records	
	allCompleted = True$
	resp = ''
	Data = Get_Property(EditTable$, "OLE.ARRAY")
	cnt = count(Data<1>, @VM) + (Data<1># '')
	*debug
	for i = 1 to cnt while allCompleted EQ True$
		cntProc = 0
		status = Data<4,i>
		If status NE 'Completed' then
			allCompleted = False$
		end
	next 
	if allCompleted NE True$ then
		resp = Message_Box(@Window, 'There are unsaved records in grid - continue?', 'Confirm Reset', MSG_BTN_OKCAN$ + MSG_DEFAULT2$)
	end 
	if allCompleted EQ True$ OR resp EQ True$ then
		rv = Send_Message(EditTable$, "OLE.Clear", 2)
		if rv EQ '' then
			GoSub ResetForm
		end
	end
	
end event


Event OLE_BTN_SAVE.OnClick(Point, Button, Shift, Ctrl)
	
	*check for unfinished scan
	F1 = Get_Property(Cassette1$, 'TEXT')
	F2 = Get_Property(Cassette2$, 'TEXT')
	F3 = Get_Property(Supplier$, 'TEXT')
	resp = True$
	If F1 NE '' OR F2 NE '' OR F3 NE '' then
		resp = Message_Box(@Window, 'You have an unfinished scan (not in grid) - continue?', 'Confirm Save', MSG_BTN_OKCAN$ + MSG_DEFAULT2$)
	end
	if resp EQ True$ Then
		Warehouse = '1K'
		Location = 'PTI'
		Gosub SaveRecords
	end
	
end event


Event EDL_CASSETTE1_SCAN.LOSTFOCUS(Flag, FocusID)
	
	ScanField = 'Cassette1'
	If Flag EQ 1 then
		ScanData = Get_Property(CtrlEntID, 'TEXT')
		If ScanData NE '' then
			ScanData = Material_Movement_Services('ProcessPTIScanData', ScanData, 'CASSETTE1')
			If Error_Services('NoError') then
				Set_Property(CtrlEntID, 'TEXT', ScanData)
				Set_Property(Cassette2$, 'ENABLED', True$)
				Set_Property(Cassette2$, 'FOCUS', True$)
				Set_Property(CtrlEntID, 'ENABLED', False$)
				Gosub ValidateGrid
			end else
				GoSub ResetForm
				ErrorMessage = Error_Services('GetMessage')
				Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
				Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'FOCUS', True$)
			end
		end
	end
	
end event


Event EDL_CASSETTE2_SCAN.LOSTFOCUS(Flag, FocusID)
	
	ScanField = 'Cassette2'
	If Flag EQ 1 then
		ScanData = Get_Property(CtrlEntID, 'TEXT')
		Cass1 = Get_Property(Cassette1$, 'TEXT')
		If ScanData NE '' then
			ScanData = Material_Movement_Services('ProcessPTIScanData', ScanData, 'CASSETTE2', Cass1)
			If Error_Services('NoError') then
				Set_Property(CtrlEntID, 'TEXT', ScanData)
				Gosub ValidateGrid
				Set_Property(Supplier$, 'ENABLED', True$)
				Set_Property(Supplier$, 'FOCUS', True$)
				Set_Property(CtrlEntID, 'ENABLED', False$)
				Gosub ValidateGrid
			end else
				GoSub ResetForm
				ErrorMessage = Error_Services('GetMessage')
				Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
				Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'FOCUS', True$)
			end
		end
	end
	
end event


Event EDL_SUPPLIER_SCAN.LOSTFOCUS(Flag, FocusID)
	
	ScanField = 'Lot'
	If Flag EQ 1 then
		Cassette1 = Get_Property(Cassette1$, 'TEXT')
		ScanData = Get_Property(CtrlEntID, 'TEXT')
		If ScanData NE '' then
			ScanData = Material_Movement_Services('ProcessPTIScanData', ScanData, 'SUPPLIER', Cassette1)
			If Error_Services('NoError') then
				Set_Property(CtrlEntID, 'TEXT', ScanData)
				Set_Property(CtrlEntID, 'ENABLED', False$)
				Gosub ValidateGrid
			end else
				GoSub ResetForm
				ErrorMessage = Error_Services('GetMessage')
				Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage)
				Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'FOCUS', True$)
			end
		end
	end
	
end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal Gosubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SaveRecords:
	
	*iterate through all records in the grid, saving them and updating the status and location
	Data = Get_Property(EditTable$, "OLE.ARRAY")
	cnt = count(Data<1>, @VM) + (Data<1># '')
	MsgStruct = ''
	MsgStruct<MTEXTWIDTH$> = MSG_WIDTH$
	cntProc = 0
	err	= ''
	for i = 1 to cnt
		status = Data<4,i>
		If status NE 'Completed' then
			Material_Movement_Services('SaveRecord', Data<1,i>, Warehouse, Location, @USER4)
			If Error_Services('NoError') then
				Status = 'Completed'
				Loc = '1K*PTI'
				Set_Property(EditTable$, 'OLE.CellColors[ALL;':i:']', 'Auto':@FM:'Auto')
				cntProc += 1
			end else
				Status = 'Error'
				Set_Property(EditTable$, 'OLE.CellColors[ALL;':i:']', 'Auto':@FM:'Red')
			end
			
			Set_Property(EditTable$, 'OLE.CELLTEXT[4;':i:']', Status)
			Set_Property(EditTable$, 'OLE.CELLTEXT[5;':i:']', Loc)
		end
	Next i
	
	If Error_Services('NoError') then
		plural = ''
		if cntProc GT 1 then plural='s'
		if cntProc EQ 0 then
			Msg(@Window, MsgStruct, 'PROCESS_COMPLETE', '', 'No records saved - nothing was incomplete')
		end else if cntProc EQ cnt then
			Msg(@Window, MsgStruct, 'PROCESS_COMPLETE', '', 'All records successfully saved.')
		end else
			Msg(@Window, MsgStruct, 'PROCESS_COMPLETE', '', cntProc:' record':plural: ' out of ':cnt:' sucessfully saved')
		end
	end else        
		ErrorMessage = Error_Services('GetMessages')        
		Msg(@Window, MsgStruct, 'PROCESS_COMPLETE', '', cntProc:' record':plural: ' out of ':cnt:' sucessfully saved, but error condintion hit - ':ErrorMessage)
	end
	
	Gosub DisableFormControls
	
return


ValidateGrid:
	
	F1 = Get_Property(Cassette1$, 'TEXT')
	F2 = Get_Property(Cassette2$, 'TEXT')
	F3 = Get_Property(Supplier$, 'TEXT')
	*check grid to make sure RDS not already in there
	Data = Get_Property(EditTable$, "OLE.ARRAY")
	result = 0
	if ScanField NE 'Lot' then ; * do not look for duplicates for lot
		Locate ScanData In Data<1> Using @VM Setting POS then
			result = 1
		end
	end
	
	If INDEX(F1,'.',2) then
		RDSType = 'EPP'
	end else if INDEX(F1,'.',1) then
		RDSType = 'GAN'
	end else
		RDSType = 'SIC'
	end
	
	Convert '.' to '*' in F1
	Begin Case
		Case RDSType EQ 'SIC'
			WOMatKey = XLATE('RDS',F1, 'WO_MAT_KEY', '', '')
			Location = XLATE('WO_MAT', WOMatKey, 'CURR_LOCATION', '', '')
		Case RDSType EQ 'EPP'
			WOMatKey = FIELD(F1,'*',1):'*':FIELD(F1,'*',3)
			Location = XLATE('WO_MAT', WOMatKey, 'CURR_LOCATION', '', '')
		Case RDSType EQ 'GAN'
			WOMatKey = F1
			Location = XLATE('WO_MAT', WOMatKey, 'CURR_LOCATION', '', '')
			
	End Case
	
	if result EQ 1 then
		ErrorMessage = 'Value already exists in grid: ':ScanData
		Msg(@Window, MsgStruct, 'PROCESS_ERROR', '', ErrorMessage) 
		Gosub ResetForm
	end else if F1 NE '' AND F2 NE '' AND F3 NE '' then
		Data = ''
		Data<1> = F1:@VM:F2:@VM:F3:@VM:'Pending':@VM:Location:@VM
		Send_Message(EditTable$, 'OLE.INSERT', 1, Data)
		GoSub ResetForm
	end
	
	Data = Get_Property(EditTable$, "OLE.ARRAY")
	cnt = count(Data<1>, @VM) + (Data<1># '')
	Set_Property(@Window:'.OLE_BTN_SAVE', 'ENABLED', cnt GE 1)
	
return


DisableFormControls:
	
	Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'TEXT', '')
	Set_Property(@Window:'.EDL_CASSETTE2_SCAN', 'TEXT', '')
	Set_Property(@Window:'.EDL_SUPPLIER_SCAN', 'TEXT', '')
	Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'ENABLED', False$)
	Set_Property(@Window:'.EDL_CASSETTE2_SCAN', 'ENABLED', False$)
	Set_Property(@Window:'.EDL_SUPPLIER_SCAN', 'ENABLED', False$)
	
return


SetupOLEControls:
	
	// Qualify OLE events that we want to intercept
	Qualifier    = ''
	Qualifier<1> = 1
	Qualifier<3> = ''
	Qualifier<4> = 0 ; * process synchronously 
	NumCols      = 5
	NumRows      = 150
	
	Ctrl			= @Window:'.OLE_BTN_CLEAR'
	Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnClick', Qualifier)
	Ctrl			= @Window:'.OLE_BTN_SAVE'
	Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnClick', Qualifier)
	
	DimensionArray = NumCols:@FM:NumRows
	Set_Property(EditTable$, "OLE.Dimension", DimensionArray)
	Titles         = "Top Label":@VM:"Bottom Label":@VM:"Sub Lot":@VM:"Scan Status":@VM:"Curr Location"
	Set_Property(EditTable$, "OLE.TitleList", Titles)
	
	Set_Property(EditTable$, "OLE.DataColumn[1-2]", '85')
	Set_Property(EditTable$, "OLE.DataColumn[3]", '80')
	Set_Property(EditTable$, "OLE.DataColumn[4]", '90')
	Set_Property(EditTable$, "OLE.DataColumn[5]", '115')
	
	AlignArray	= 'C':@FM:'C':@FM:'L'
	Set_Property(EditTable$, "OLE.CellAlignment[All; All]", AlignArray)
	Set_Property(EditTable$, "OLE.HeaderAlignment[All; All]", AlignArray)
	
	HeaderFontArray   = 'Segoe UI':@SVM:8:@SVM:700
	Set_Property(EditTable$, "OLE.HeaderFont[All; All]", HeaderFontArray)
	Set_Property(EditTable$, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)
	
	AutoNumArray       = ''
	AutoNumArray<1, 1> = 'I' ; // Integers
	AutoNumArray<1, 2> = 1   ; // Starting integer
	Set_Property(EditTable$, "OLE.AutoNumbers", AutoNumArray)
	
	Set_Property(EditTable$, "OLE.CellProtection[All; All]", 'SEL')
	SelStyleArray = 'Black' : @VM : OI_HOT_BLUE$ : ' L=70' : @FM : 'Black' : @VM : OI_HOT_BLUE$ 
	Set_Property(EditTable$, 'OLE.SelectionStyle', SelStyleArray)     ; // Automatically highlight the current row with one color and highlight the current row with another color.
	
return


ResetForm:
	
	Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'ENABLED', True$)
	Set_Property(@Window:'.EDL_CASSETTE2_SCAN', 'ENABLED', True$)
	Set_Property(@Window:'.EDL_SUPPLIER_SCAN', 'ENABLED', True$)
	Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'TEXT', '')
	Set_Property(@Window:'.EDL_CASSETTE2_SCAN', 'TEXT', '')
	Set_Property(@Window:'.EDL_SUPPLIER_SCAN', 'TEXT', '')
	Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'ENABLED', True$)
	Set_Property(@Window:'.EDL_CASSETTE2_SCAN', 'ENABLED', False$)
	Set_Property(@Window:'.EDL_SUPPLIER_SCAN', 'ENABLED', False$)
	Set_Property(@Window:'.EDL_CASSETTE1_SCAN', 'FOCUS', True$)
	Data = Get_Property(EditTable$, "OLE.ARRAY")
	cnt = count(Data<1>, @VM) + (Data<1># '')
	Set_Property(@Window:'.OLE_BTN_SAVE', 'ENABLED', cnt GE 1)
	Set_Property(EditTable$, 'OLE.CellColors[ALL;All]', 'Auto':@FM:'Auto')
	
return

