Compile function NDW_LOG_TEST_WAFER_USAGE_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_LOG_TEST_WAFER_USAGE

Declare function    Get_Property, Error_Services, Test_Run_Services, Reactor_Services, Tool_Services, Database_Services, Lot_Services
declare function    UCASE
Declare subroutine  Set_Property, Error_Services, Msg, End_Dialog, Lot_Services, PlaceDialog, NDW_LOG_TEST_WAFER_USAGE_EVENTS

$Insert APP_INSERTS
$Insert TEST_RUN_EQUATES
$Insert RDS_EQUATES
$Insert LOT_EQUATES
$Insert TEST_WAFER_PROD_EQUATES
$Insert LOT_OPERATION_EQUATES

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

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

Event WINDOW.CREATE(CreateParam)
    
    PlaceDialog(-2, -2)
    Set_Property(@Window, 'VISIBLE', 1)
    NewTWSystemActive   = XLATE('APP_INFO', 'NEW_TW_SYSTEM_ACTIVE_SWITCH', 1, 'X')
    If NewTWSystemActive EQ 0 then
        msg(@Window, 'New TW System not activated, closing out')
        Result      = ''
        Result<1>   = False$
        End_Dialog(@Window, Result)
    end
    GoSub FillFormMasterData
    RDSNo               = ''
    PresetTestType      = ''
    PresetEquipmentType = ''
    PresetEquipmentId   = ''
    If CreateParam NE '' then
        RDSNo               = CreateParam<1, 1>
        PresetTestType      = CreateParam<1, 2>
        PresetEquipmentType = CreateParam<1, 3>
        PresetEquipmentId   = CreateParam<1, 4>  
    end else
        Set_Property(@Window, '@KeepWindowOpen', True$)
    end
    If RDSNo NE '' AND RowExists('RDS', RDSNo) then
        PSN = XLATE('RDS', RDSNo, RDS_PROD_SPEC_ID$, 'X')
        Set_Property(@Window : '.EDL_RDS', 'TEXT', RDSNo)
        Set_Property(@Window : '.EDL_PSN', 'TEXT', PSN)
        Set_Property(@Window : '.EDL_PSN', 'ENABLED', False$)
    end
    If PresetTestType NE '' then
        Begin Case
            Case PresetTestType EQ 'STANDARD'
                Set_Property(@Window : '.CMB_TEST_TYPE', 'TEXT', 'Standard Sampling')
        End Case
    end
    If PresetEquipmentType NE '' then
        Begin Case
            Case PresetEquipmentType EQ 'Reactor'
                Set_Property(@Window : '.CMB_EQUIP_TYPE', 'TEXT', 'Reactor')
            Case PresetEquipmentType EQ 'Non-Reactor'
                Set_Property(@Window : '.CMB_EQUIP_TYPE', 'TEXT', 'Tool')
        End Case
    end
    If PresetEquipmentId NE '' then
        SelectedEquipType = Get_Property(@Window : '.CMB_EQUIP_TYPE', 'TEXT')
        Begin Case
            Case SelectedEquipType EQ 'Reactor'
                If RowExists('REACTOR', PresetEquipmentId) then
                    Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', PresetEquipmentId)
                end
            Case SelectedEquipType EQ 'Non-Reactor'
                If RowExists('REACTOR', PresetEquipmentId) then
                    Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', PresetEquipmentId)
                end
        End Case
    end
    EventFlow = 1
    
End Event

Event Window.CLOSE(CancelFlag, CloseFlags)
    
    Result      = ''
    Result<1>   = False$
    End_Dialog(@Window, Result)
    
end event

Event EDL_LOT_ID.LOSTFOCUS(Flag, FocusID)
    
    LotIdString = UCASE(Get_Property(@Window : '.EDL_LOT_ID', 'TEXT'))
    If LotIdString NE '' then
        If LotIdString[1, 4] EQ '1TTW' then
        LotIdString = LotIdString[3, 999]
        Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', LotIdString)
        end
        NDW_LOG_TEST_WAFER_USAGE_EVENTS(@Window : '.PUB_ADD_LOT', 'CLICK')
        Set_Property(@Window : '.EDL_LOT_ID', 'FOCUS', True$)
    end
    
end event

Event CMB_EQUIP_TYPE.CHANGED(NewData)
    
    ReactorOpts = Get_Property(@Window, '@REACTOR_OPTS')
    NonReactorOpts = Get_Property(@Window, '@NON_REACTOR_OPTS')
    Begin Case
        Case NewData EQ 'Non-Reactor'
            Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'LIST', NonReactorOpts)
        Case NewData EQ 'Reactor'
            Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'LIST', ReactorOpts)
    End Case
    
end event

Event EDL_RDS.LOSTFOCUS(Flag, FocusID)
	
	RDSNo = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	If Len(RDSNo) GE 6 then
		If RowExists('RDS', RDSNo) then
			RDSRec    = Database_Services('ReadDataRow', 'RDS', RDSNo)
			PSN       = RDSRec<RDS_PROD_SPEC_ID$>
			ReactorNo = RDSRec<RDS_REACTOR$>
			If PSN NE '' then
				Set_Property(@Window : '.EDL_PSN', 'TEXT', PSN)
				Set_Property(@Window : '.EDL_PSN', 'ENABLED', -1)
			end
			If ReactorNo NE '' then
				Set_Property(@Window : '.CMB_EQUIP_TYPE', 'VALUE', 'Reactor')
				Set_Property(@Window : '.CMB_EQUIP_TYPE', 'ENABLED', -1)
				Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', ReactorNo)
				Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'ENABLED', -1)
			end
		end
	end else
		Set_Property(@Window : '.EDL_PSN', 'TEXT', '')
		Set_Property(@Window : '.EDL_PSN', 'ENABLED', True$)
		Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', '')
		Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'ENABLED', True$)
		Set_Property(@Window : '.CMB_EQUIP_TYPE', 'ENABLED', True$)
	end
	
end event

Event PUB_ADD_LOT.CLICK()
    
    TWLotUseArray  = Get_Property(@Window: '.EDT_TW_USAGE', 'LIST')
    LotId          = Get_Property(@Window : '.EDL_LOT_ID', 'TEXT')
    If LotId NE ''  then
        Found = False$
        for i = 1 to DCount(TWLotUseArray, @FM)
            if LotId EQ TWLotUseArray<i, 1> then
                Found = True$
            end
        until Found    
        Next i
        If Not(Found) then
            If RowExists('LOT', LotId) then
            //Check if lot is a test wafer type
                LotRec = Database_Services('ReadDataRow', 'LOT', LotId, True$, 0, False$)
                if Error_Services('NoError') AND LotRec NE '' then
                    LotType   = LotRec<LOT_TYPE$>
                    LotOpen   = LotRec<LOT_OPEN$>
                    LotOnHold = LotRec<LOT_HOLD$>
                    If LotType EQ 'TW' then
                        If LotOpen then
                            If Not(LotOnHold) then
                                LotCurrOperationId = Lot_Services('GetLotCurrOperationId', LotId)
                                LotCurrOperation   = XLATE('LOT_OPERATION', LotCurrOperationId, LOT_OPERATION_OPERATION_ID$, 'X')
                                If LotCurrOperation EQ 'TW_CREATE' Or LotCurrOperation EQ 'TW_CLOSE' then
                                    Msg(@Window, 'Error: Lot ':LotId:' is currently at  ' : LotCurrOperation : '. It cannot be used at this time.')
                                    Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
                                end else
                                    TestWaferProdName = Database_Services('ReadDataColumn', 'TEST_WAFER_PROD', LotRec<LOT_PROD_ID$>, TEST_WAFER_PROD_PART_NAME$, True$, 0, False$)
                                    TWLotUseArray<-1> = LotId : @VM : TestWaferProdName : @VM : ''
                                    Set_Property(@Window : '.EDT_TW_USAGE', 'LIST', TWLotUseArray)
                                    Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '') 
                                end
                            end else
                                Msg(@Window, 'Error: Lot ':LotId:' is on hold and cannot be used.')
                                Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
                            end
                        end else
                            Msg(@Window, 'Error: Lot ':LotId:' is not in an open status.')
                            Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
                        end
                    end else
                        Msg(@Window, 'Error: Lot ':LotId:' is not a test wafer lot.')
                        Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
                    end
                end else
                    Msg(@Window, 'Error: Error reading Lot ':LotId:' from LOT table.')
                    Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
                end
            end else
                Msg(@Window, 'Error: Lot not found in LOT table.')
                Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
            end    
        end else
           Msg(@Window, 'Error: Lot ':LotId:' already exists in the list.')
           Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
        end
    end
    
end event

Event EDT_TW_USAGE.ROWSELCHANGED(SelRow, SelState)
    
    If SelRow NE '' then
        Set_Property(@Window: '.PUB_REMOVE_SELECTED', 'ENABLED', True$)
    end else
        Set_Property(@Window: '.PUB_REMOVE_SELECTED', 'ENABLED', False$)
    end
    
end event

Event PUB_REMOVE_SELECTED.CLICK()
    
    SelectedRow   = Get_Property(@Window : '.EDT_TW_USAGE', 'SELPOS')<2>
    TWLotUseArray = Get_Property(@Window: '.EDT_TW_USAGE', 'LIST')
    If SelectedRow LE DCount(TWLotUseArray, @FM) then
        TWLotUseArray = Delete(TWLotUseArray, SelectedRow , 0, 0)
    end
    Set_Property(@Window : '.EDT_TW_USAGE', 'LIST', TWLotUseArray)
    
end event

Event PUB_CLEAR.CLICK()
    
    GoSub ClearFormData
    
end event

Event PUB_SUBMIT.CLICK()
	
	NotReadyReason = ''
	//Gather all field information
	ReasonForTest  = Get_Property(@Window : '.CMB_TEST_TYPE', 'TEXT')
	RelatedRDS     = Get_Property(@Window : '.EDL_RDS', 'TEXT')
	RelatedPSN     = Get_Property(@Window : '.EDL_PSN', 'TEXT')
	EquipmentType  = Get_Property(@Window : '.CMB_EQUIP_TYPE', 'TEXT')
	EquipmentID    = Get_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT')
	//Translate selection to IDs. Get Run Type ID
	SelRunTypeID = '';*Test Run Obj variable 
	RunTypeOpts  = Get_Property(@Window, '@RUN_TYPE_OPTS')
	Locate ReasonForTest in RunTypeOpts<2> using @VM setting rPos then
		SelRunTypeID = RunTypeOpts<1, rPos>
	end else
		NotReadyReason := 'Must select a valid run type reason.' : CRLF$ 
	end
	//Translate Equipment Type. R for Reactor, T for non-reactor
	SelEquipType = '';*Test Run Obj variable 
	Begin Case
		Case EquipmentType EQ 'Reactor'
			SelEquipType = 'R'
		Case EquipmentType EQ 'Non-Reactor'
			SelEquipType = 'T'
	End Case
	//Get Test wafer usage data from table
    TWUsageData = Get_Property(@Window: '.EDT_TW_USAGE', 'LIST')
    TWLots      = ''
    TWLotQtys   = ''
	for each TWRow in TWUsageData using @FM
		Quantity = TWRow<1,3>
		ProdName = TWRow<1,2>
		TWLot    = TWRow<1,1>
		If TWLot NE ''  then
            If Quantity NE '' then
                if Num(Quantity) then
                    Begin Case
                        Case Quantity GT 0 AND Quantity LE 25
                            TWLots<1, -1>    = TWRow<1,1>
                            TWLotQtys<1, -1> = TWRow<1,3>
                        Case Quantity GT 25
                            NotReadyReason := 'Quantity value for test wafer lot ' : TWLot : ' must be less than 25.' : CRLF$
                        Case Quantity LE 0
                            NotReadyReason := 'Quantity value for test wafer lot ' : TWLot : ' must be greater than 0.' : CRLF$
                        Case Otherwise$
                            NotReadyReason := 'Quantity value for test wafer lot ' : TWLot : ' is invalid.' : CRLF$
                    End Case
                end else
                   NotReadyReason := 'Quantity value for test wafer lot ' : TWLot : ' must be a number' : CRLF$
                end
            end else
                NotReadyReason := 'A quantity of test wafers used is required for test wafer lot ' : TWLot : CRLF$
            end
		end
	Next TWRow
	If NotReadyReason EQ '' then
		    TRWaferKey = Test_Run_Services('CreateTestRunRecord', SelRunTypeID, SelEquipType, EquipmentID, RelatedPSN, RelatedRDS, @User4, TWLots, TWLotQtys) 
			If Error_Services('NoError') then
				Msg(@Window, 'Test Run Created Successfully!')
				Result    = ''
				Result<1> = True$
				KeepWindowOpen = Get_Property(@Window, '@KeepWindowOpen')
				If KeepWindowOpen then
				    GoSub ClearFormData
				end else
				    End_Dialog(@Window, Result)
				end
                
			end else
			    ErrorMessage = Error_Services('GetMessage')
			    Msg(@Window, 'Error! - ' : ErrorMessage)
			end
	end else
		Msg(@Window, NotReadyReason)
	end
	
end event

//-----------------------------------------------------------------------------
// Internal GoSubs
//-----------------------------------------------------------------------------

FillFormMasterData:
	
	//Get Test run type options. Populate to CMB_TEST_TYPE
	TestRunTypeList = Test_Run_Services('GetAllTestRunTypes')
	Set_Property(@Window, '@RUN_TYPE_OPTS', TestRunTypeList)
	TestRunTypeOpts = ''
	for each TestRunID in TestRunTypeList<1> using @VM setting tPos
		TestRunName         = TestRunTypeList<2, tPos>
		TestRunTypeOpts<-1> = TestRunName
	Next TestRunID
	Set_Property(@Window : '.CMB_TEST_TYPE', 'LIST', TestRunTypeOpts)
	
	//Get Tool and Reactor Identifiers and buffer them in user defined window properties @REACTOR_OPTS and @NON_REACTOR_OPTS
	//Reactors
	ReactorOpts    = Reactor_Services('GetReactorNumbers')
	Set_Property(@Window, '@REACTOR_OPTS', ReactorOpts)
	
	//Non Reactors
	NonReactorOpts = Tool_Services('GetAllTools')
	swap @VM with @FM in NonReactorOpts
	Set_Property(@Window : '.EDT_NON_REACTORS', 'TEXT', NonReactorOpts)
	Set_Property(@Window, '@NON_REACTOR_OPTS', NonReactorOpts)
	//Then set the inital combo list to be filled with reactors because that is the default.
	Set_Property(@Window : '.CMB_EQUIP_TYPE', 'DEFPROP', 'Reactor')
	Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'LIST', ReactorOpts)
	//Get TW Products and store them in user defined window property @TW_PROD_OPTS
	Prods    = Test_Run_Services('GetAllTWProdKeys', 1)
	Set_Property(@Window, '@TW_PROD_OPTS', Prods)
	
Return

ClearFormData:
    
    Set_Property(@Window : '.EDL_LOT_ID', 'TEXT', '')
    Set_Property(@Window : '.EDL_RDS', 'TEXT', '')
    Set_Property(@Window : '.EDL_PSN', 'TEXT', '')
    Set_Property(@Window : '.EDL_PSN', 'ENABLED', True$)
    Set_Property(@Window : '.CMB_EQUIP_TYPE', 'TEXT', '')
    Set_Property(@Window : '.CMB_EQUIP_TYPE', 'ENABLED', True$)
    Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'TEXT', '')
    Set_Property(@Window : '.CMB_EQUIPMENT_ID', 'ENABLED', True$)
    Set_Property(@Window : 'CMB_TEST_TYPE', 'TEXT', '')
    Set_Property(@Window : '.EDT_TW_USAGE', 'LIST', '')
    
return

