Compile function OVERRIDE_REPORT_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler

equ crlf$$ to \0D0A\

#Window OVERRIDE_REPORT

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES
$insert OVERRIDE_LOG_EQUATES



Declare subroutine SRP_Show_Window, Msg

Declare function   Utility, Datetime, SRP_DateTime, SRP_SORT_ARRAY, Override_Log_Services, Database_Services

// 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$

Event WINDOW.CREATE(CreateParam)
	
	GoSub Setup_OLE_Controls
	FormSize = '' 
	SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
	
end event

Event OLE_DATEPICKER_FROM.OnSelChange(FirstDate)
	
	
	SelectedDate    = Get_Property(CtrlEntId, 'OLE.Selection')
	Set_Property(@Window : '.OLE_SCHEDULE', 'OLE.Date', SelectedDate)
	Set_Property(@Window : '.DATE_FROM','DEFPROP', OCONV(SelectedDate,'DT4/HS'))
	
end event

Event OLE_DATEPICKER_TO.OnSelChange(FirstDate)
	
	
	SelectedDate    = Get_Property(CtrlEntId, 'OLE.Selection')
	Set_Property(@Window : '.OLE_SCHEDULE', 'OLE.Date', SelectedDate)
	Set_Property(@Window : '.DATE_TO','DEFPROP', OCONV(SelectedDate,'DT4/HS'))
	
end event

Event PUB_DATE_FROM.CLICK:
	
	target = '.OLE_DATEPICKER_FROM'
	GoSub ToggleVisible
	
return

Event PUB_DATE_TO.CLICK:
	
	target = '.OLE_DATEPICKER_TO'
	GoSub ToggleVisible
	
return

Event PUB_DIR_BROWSE.CLICK
	value = "Select a directory." : @fm : "c:\temp"
	dir   = Utility( "CHOOSEDIR", @window, value)
	
	if dir NE '' then
		Set_Property(@Window : '.FILE_PATH', 'DEFPROP', dir: '\overrideexport.csv')	
	end
	
end event

Event PUB_EXPORT.CLICK
	tableLines = Get_Property(@WINDOW:'.OVERRIDE_TABLE','LIST')
	HeaderOut = Get_Property(@WINDOW:'.OVERRIDE_TABLE','LABEL') : crlf$$
	swap @FM with ',' in HeaderOut
	filepath = Get_Property(@Window : '.FILE_PATH', 'DEFPROP')
	DataOut = ''
	
	IF filepath NE '' then
		For Each line in tableLines using @FM
			For a = 1 to COUNT(line,@VM)
				line<1,a> = QUOTE(line<1,a>)
			Next a
			
			swap @VM with ',' in line
			DataOut := line: crlf$$
		Next line
	end else
		msg(@Window, 'No save path specified.')
		return
	end
	
	OSOPEN filepath TO hFilePath THEN
		OSWrite DataOut ON filepath			;* Clear file it was already there
	END ELSE
		OSWrite DataOut ON filepath			;* Create the file if it wasn't
		OSOPEN filepath TO hFilePath ELSE
			Msg(@Window, 'Unable to write the file!')
			RETURN ''
		END
	END  
	
	SWAP @FM WITH ',' IN HeaderOut
	
	FilePointer = 0
	
	OSBWrite HeaderOut ON filepath AT FilePointer
	FilePointer += LEN(HeaderOut)
	
	OSBWrite DataOut ON filepath AT FilePointer
	OSClose hFilePath
	Msg(@Window, 'Successfuly wrote file at ' : filepath : '.')
	
	
end event

Event SEARCH.CLICK
	Set_Property(@WINDOW:'.OVERRIDE_TABLE','LIST','')
	Set_Property(@WINDOW:'.OVERRIDE_TABLE','LABEL','')
	DynamicFields = 'RDS':@FM:'REACTOR'
	SearchType = Get_Property(@WINDOW:'.REPORT_BY','DEFPROP')
	EntityId = Get_Property(@WINDOW:'.ITEM_FILTER','DEFPROP')
	OverrideType = Get_Property(@Window : '.OVERRIDE_TYPE', 'DEFPROP')
	OverrideUser = Get_Property(@WINDOW:'.EDL_USER', 'TEXT')
	DateFrom = Get_Property(@WINDOW:'.DATE_FROM','DEFPROP')
	DateFrom = ICONV(DateFrom,'DT4/HS')
	DateTo = Get_Property(@WINDOW:'.DATE_TO','DEFPROP')
	DateTo = ICONV(DateTo,'DT')
	TableData = ''
	IF DateTo EQ '' then
		DateTo = DateTime()
	end
	IF DateFrom EQ '' then
		DateFrom = SRP_DateTime('AddDays', DateTo, -7)
	end
	Begin Case
		Case SearchType EQ 'ALL'
			EntityType = ''
		Case Otherwise$
			EntityType = SearchType
	End Case
	If OverrideType = 'ALL' then
		OverrideType = ''
	end
	OverrideLogRecordKeys = Override_Log_Services('GetOverrideLogKeys', EntityId, EntityType, OverrideType, OverrideUser)
	for each OverrideLogRecordKey in OverrideLogRecordKeys using @VM
	
		OverrideLogRec = Database_Services('ReadDataRow', 'OVERRIDE_LOG', OverrideLogRecordKey, True$, 0, False$)
		//second loop to handle legacy multivalues in Table and Key columns. New overrides shouldn't have multivalues.
		for each Table in OverrideLogRec<OVERRIDE_LOG_TABLE$> using @VM setting TablePos
			OverrideDtm = OverrideLogRec<OVERRIDE_LOG_DTM$>
			EntityId = OverrideLogRec<OVERRIDE_LOG_KEY$, TablePos>
			EntityType = OverrideLogRec<OVERRIDE_LOG_TABLE$, TablePos>
			OverrideUser = OverrideLogRec<OVERRIDE_LOG_USER$>
			OverrideType = OverrideLogRec<OVERRIDE_LOG_CATEGORY$>
			OverrideCause = OverrideLogRec<OVERRIDE_LOG_CAUSE$>
			OverrideComment = OverrideLogRec<OVERRIDE_LOG_COMMENT$>
			TableData<-1> = OverrideDtm : @VM : EntityId : @VM : EntityType : @VM : OverrideUser : @VM : OverrideUser : @VM : OverrideType : @VM : OverrideCause : @VM : OverrideComment
		Next Table
			
	Next OverrideLogRecordKey
	TableData =  SRP_SORT_Array(TableData, "DR1", 1)
	For each line in TableData using @FM
		
	Next line
	Set_Property(@WINDOW:'.OVERRIDE_TABLE','LIST',TableData)
	
end event

Setup_OLE_Controls:

Ctrl = @Window : '.OLE_DATEPICKER_FROM'
Set_Property(Ctrl, 'OLE.Border', 'XP Flat')
Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : '9')
Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', 1)

Ctrl = @Window : '.OLE_DATEPICKER_TO'
Set_Property(Ctrl, 'OLE.Border', 'XP Flat')
Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : '9')
Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', 1)

return

* * * * * * * * 
ToggleVisible:
* * * * * * * *

visible = Get_Property(@Window : target , 'VISIBLE')

if visible then
Set_Property(@Window : target, 'VISIBLE', 0)
end else
Set_Property(@Window : target, 'VISIBLE', 1)
end
target = ''
return




