'PREPAPTE - creates APTEDATE file from all Estimators' records in APPNTMNT.DB
' 15-01-14 - new version for MEASURES/NEWMENUS - selects [Ftr_Est]="E" only

external   vloadif() dpath scr sch scw vunloadif() messboxwait() ipath Background()
external   progress() fgp bgp messbox() cpath fgi bgi progtag() remove()
external   exception() userid makeidx() PrintReport()

public     #start #end #days

global     x startdate enddate y SelectRecords() w WriteApptDate() #recs i
global     $date $dow $dfa $name $a1 $a2 $a3 $a4 $a5 $a6 $a7 DrawLine()
global     $b1 $b2 $b3 $b4 $b5 $b6 $b7 $tit $date1 $shwdate
global     $mess cat ReturnToMenu() $newrec


MAIN
single-step off
'--------------------------------------setup files etc
  	#days = 20                    ' ALL dates in APPNTMNT.DB USED
	Background()
  	progress(fgp,bgp," Finding records ... ",0)
  	file unload all

'--------------------------------------clear out old records from file
  	vloadif(dpath|"aptedate.vws")
  	error off
  	data query execute "delete.dfq"
  	vunloadif("aptedate.vws")
  	data utilities purge dpath|"aptedate.vws"

'--------------------------------------select new records to create
  	x = SelectRecords()
  	if x = 1
    		ReturnToMenu()
  	end if

'--------------------------------------create new records
  	progress(fgp,bgp,"  ÿÿÿÿÿCreating new file ... ÿÿÿÿÿ ",1)
  	#recs = records/7                    '
' message "#recs is:"&str(#recs)
  	if mod(records,7)<>0                    '
    		messboxwait(" Incorrect number of records in file - see DC PE L41 ",0,0,1)
    		ReturnToMenu()
  	end if
  	$tit = 0
  	for i = 1 to #recs
    		$newrec = 0
    		if (deleted)
      		$newrec = 1
    		end if
    		$date  = [Date]
    		$date1 = [Date]
    		progtag(fgi,bgi," Creating appointments on"&date2($date1))
    		$dfa   = left([DFA],13)
    		$dow   = left(dayname([Date]),3)
    		$shwdate = left(date1([Date]),6)
    		$name = [Nickname]
    		$a1 = [Post_Job]
    		$b1 = [Status]
' message "PE L63 $name) is:"&str($name)
' message "$a1) is:"&str($a1)
' message "$b1) is:"&str($b1)
    		data goto record next
    		$a2 = [Post_Job]
    		$b2 = [Status]
    		data goto record next
    		$a3 = [Post_Job]
    		$b3 = [Status]
    		data goto record next
    		$a4 = [Post_Job]
    		$b4 = [Status]
    		data goto record next
    		$a5 = [Post_Job]
    		$b5 = [Status]
    		data goto record next
    		$a6 = [Post_Job]
    		$b6 = [Status]
    		data goto record next
    		$a7 = [Post_Job]
    		$b7 = [Status]
    		data goto record next
'     WriteApptDate()
'     if $tit = 0
'       $tit = 1
'     end if
'     if $date1 <> [Date]                 'message "Next day"
'       DrawLine()
'       $tit = 0
'     end if
'   end for
    		if $newrec = 0
      		WriteApptDate()
    		end if
    		if $tit = 0
      		$tit = 1
    		end if
    		if $date1 <> [Date]                 'message "Next day"
      		if $newrec = 0
        			DrawLine()
      		end if
      		$tit = 0
    		end if
  	end for
' repaint on
' repaint
' single-step on

'--------------------------------------finish
  	ReturnToMenu()

END MAIN


FUNCTION SelectRecords()
  	vloadif(dpath|"shwappt4.vw")
  	data query execute "prepday2.dfq" index "prep.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Date])>(days(today)-60)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		messbox(" No relevant appointments found ",0,0,1)
    		return (1)
  	end if
  data query execute "prepapte.dfq" index "prep0.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Ftr_Est]="E"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No relevant appointments found ",0,0,1)
    return (1)
  end if
  order sort now dictionary "prep1.idx" fields "[DFA]" ascending
END FUNCTION 'SelectRecords()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
END FUNCTION ' ReturnToMenu()


FUNCTION WriteApptDate()
  vloadif(dpath|"aptedate.vws")
' repaint on
' repaint
' single-step on
  data enter lock
  if $tit = 0
    [Shw_Date] = $shwdate
    [DOW]      = $dow
  else
    [Shw_Date] = blank
    [DOW]      = blank
  end if
    [Date]     = $date
    [DayFitter]= $dfa
    [Nickname] = $name
    [A1]       = $a1
    [A2]       = $a2
    [A3]       = $a3
    [A4]       = $a4
    [A5]       = $a5
    [A6]       = $a6
    [A7]       = $a7
    [B1]       = $b1
    [B2]       = $b2
    [B3]       = $b3
    [B4]       = $b4
    [B5]       = $b5
    [B6]       = $b6
    [B7]       = $b7
  write-record
  vloadif(dpath|"shwappt4.vw")
END FUNCTION 'WriteApptDate()


FUNCTION DrawLine()
  vloadif(dpath|"aptedate.vws")
  $a1   = "ÛÛÛÛÛÛÛÛÛÛ"
  $b1   = "S"
  data enter lock
    [Date]     = blank
    [DOW]      = blank
    [DayFitter]= blank
    [Nickname] = blank
    [A1]       = $a1
    [A2]       = $a1
    [A3]       = $a1
    [A4]       = $a1
    [A5]       = $a1
    [A6]       = $a1
    [A7]       = $a1
    [B1]       = $b1
    [B2]       = $b1
    [B3]       = $b1
    [B4]       = $b1
    [B5]       = $b1
    [B6]       = $b1
    [B7]       = $b1
  write-record
  vloadif(dpath|"shwappt4.vw")
END FUNCTION ' DrawLine()


