'PREPAPTF - creates APPTDATE file for FITTERS from all records in APPNTMNT.DB;

' 15-01-14 - new version for MEASURES/NEWMENUS - selects [Ftr_Est]="F" only

external   vloadif() dpath scr sch scw vunloadif() messboxwait() ipath
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
  	screen clear box 1 1 sch scw 0 0 no-border
  	repaint off
  	progress(fgp,bgp," Finding records ... ",0)
  	file unload all

'--------------------------------------clear out old records from file
  	vloadif(dpath|"apptdate.vws")
  	error off
  	data query execute "delete.dfq"
' not(deleted) replace delete
	if cerror
	end if
  	vunloadif("apptdate.vws")
'   data utilities purge dpath|"apptdate.vws"
	data utilities purge smart4 dpath|"apptdate.vws"

'--------------------------------------select new records to create
  	x = SelectRecords()						'L115
  	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"|str(records)|" in file - see DC ",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 = [Job_Nr]
    		$b1 = [Status]
    		data goto record next
    		$a2 = [Job_Nr]
    		$b2 = [Status]
    		data goto record next
    		$a3 = [Job_Nr]
    $b3 = [Status]
    data goto record next
    $a4 = [Job_Nr]
    $b4 = [Status]
    data goto record next
    $a5 = [Job_Nr]
    $b5 = [Status]
    data goto record next
    $a6 = [Job_Nr]
    $b6 = [Status]
    data goto record next
    $a7 = [Job_Nr]
    $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

'--------------------------------------finish
  	ReturnToMenu()

END MAIN


FUNCTION SelectRecords()
  	vloadif(dpath|"shwappt4.vw")
'   order change key "[DFA]"
  	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 "prepappt.dfq" index "prep0.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Ftr_Est]="F"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  	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|"apptdate.vws")
  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|"apptdate.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()

' data utilities purge smart4 "X:\data\apptdate"

