Compile function Override_Log_Services(@Service, @Params)
#pragma precomp SRP_PreCompiler

$Insert SERVICE_SETUP
$insert APP_INSERTS
$Insert OVERRIDE_LOG_EQUATES

Declare function   Datetime, NextKey, Error_Services, SRP_Datetime
Declare subroutine Database_Services, Btree.Extract, Error_Services

GoToService

Return Response or ""

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options OVERRIDE_TYPES     = 'ROTR_BLOCK,UNSIGN,CLEAN_INSP,REACTOR_PROVE_IN,REACTOR_LOG_PM'

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service Create(Table, Key, OverrideUser, OverrideComment, OverrideType=OVERRIDE_TYPES, OverrideCause='')
	
	ErrorMsg = ''
	RecKey   = ''
    for each TableName in Table using @VM setting TablePos
    	RecKey                        = Nextkey('OVERRIDE_LOG')
		orRec                         = ''
		orRec<OVERRIDE_LOG_TABLE$>    = Table<1, TablePos>
		orRec<OVERRIDE_LOG_KEY$>      = Key<1, TablePos>
		orRec<OVERRIDE_LOG_USER$>     = OverrideUser
		orRec<OVERRIDE_LOG_COMMENT$>  = OverrideComment
		orRec<OVERRIDE_LOG_DTM$>      = Datetime()
		orRec<OVERRIDE_LOG_CATEGORY$> = OverrideType
		orRec<OVERRIDE_LOG_CAUSE$>	  = OverrideCause
		
		Database_Services('WriteDataRow', 'OVERRIDE_LOG', RecKey, orRec)
    Next TablePos
    
    If Error_Services('NoError') then
        Response = RecKey
    end else
        Response = ''
    end
    
End Service


Service GetOverrideLogKeys(EntityId, Table, Type, User, DateTimeFrom, DateTimeTo)
	
	If DateTimeFrom EQ '' OR Not(Num(DateTimeFrom)) Or DateTimeFrom GT DateTimeTo then
		DateTimeFrom = SRP_Datetime('AddDays', Datetime(), -30)
	end
	If DateTimeTo EQ '' OR Not(Num(DateTimeFrom)) then
		DateTimeTo = Datetime()
	end
	Open 'DICT.OVERRIDE_LOG' To @DICT then
		SearchString = ''
		SearchString := 'DTM':@VM:DateTimeFrom:'~':DateTimeTo:@FM
		if EntityId NE '' then
			SearchString := 'KEY':@VM:EntityId:@FM
		end
		If Table NE '' then
			SearchString := 'TABLE':@VM:Table:@FM
		end
		If Type NE '' then
			SearchString := 'CATEGORY':@VM:Type:@FM
		end
		If User NE '' then
			SearchString := 'USER':@VM:User:@FM
		end
		
		keylist = ''
		option = ''
		flag = ''
		Btree.Extract(SearchString, 'OVERRIDE_LOG', @DICT, keylist, option, flag)
	end else
		Error_Services('Add', 'Error in ' : service : ' unable to open OVERRIDE_LOG dictionary.')
	end
	Response = keylist
	
end service

