' ********************************************************************************
' Project file: TCALLS
' Find call backs with today's date
' Executed In-memory from menu contacts.
' ********************************************************************************



external $$fct $$usrid ##lev $$workdir
external run_query_str() up_db_stat() get_choice() display_message()
public run_query()
public #rqs #choice $choices

repaint off

if fileinfo($$workdir|"tcalls.idx",f_type) = 1
#choice = get_choice("Choose from the following","options:","","","Use_Existing_Index Requery Oops",3)
case #choice
 when 1
   order change index $$workdir|"tcalls.idx"


 when 2
   run_query()

 when 3
   exit case
end case

elseif fileinfo($$workdir|"tcalls.idx",f_type) = 0
    run_query()

end if
graphics window cursor 0 ' pointer cursor
repaint on
repaint



function run_query()
'*************************************************************************
local $$ord_type
repaint
graphics window cursor 4 ' hourglass cursor
display_message("Querying TODAY's calls for "|$$usrid,"",13)
order change key [Callback]
clearerror
#rqs = run_query_str("tcalls","[Callback] >= date(today) and [Rep] = $$usrid",0)
repaint
if #rqs = -3143
   beep
   display_message("NO callbacks TODAY for "|$$usrid,"Press ENTER or click OK",3)
else
   beep
   display_message(str(#rqs)|" callbacks TODAY for "|$$usrid,"Press ENTER or click OK",3)

end if
$$ord_type = $$workdir|"tcalls"

if #rqs < 50 'Fakes out query optimiser which runs sequential search on small queries
display_message("Sorting callbacks for "|$$usrid,"",13)
graphics window cursor 4 ' hourglass cursor
order sort now smart $$workdir|"stcalls" fields "[Callback]" ascending
$$ord_type = $$workdir|"stcalls"
end if

up_db_stat(##lev,record,"index",$$ord_type,"")
graphics window cursor 0 ' pointer cursor
end function

