'PREPAPPT - creates APPTDATE file from all records in APPNTMNT.DB

external   vloadif() dpath scr sch scw vunloadif() messboxwait() ipath
external   progress() fgp bgp messbox() cpath fgi bgi progtag() remove()
external   exception() userid makeidx() PrintReport() vloadexcif()

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"
  vunloadif("apptdate.vws")
  data utilities purge dpath|"apptdate.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)
  $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],11)
    $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 ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
'   transfer cpath|"pm_menu.psl" in-memory
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 SelectRecords()
  vloadif(dpath|"shwappt4.vw")
  order change key "[DFA]"
  data query execute "prepday4.dfq" index "prep1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Job_Nr] <> "SUNDAY"
'   or
'   [Job_Nr] <> "BNKHOL"
'   replace delete
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No relevant appointments found ",0,0,1)
    return (1)
  end if

'   data query execute "prepday3.dfq" index "prep1.idx"
' ' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' '   [Job_Nr] <> "SUNDAY"
' '   and
' '   [Job_Nr] <> "BNKHOL"
' ' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
'   if cerror
'     messbox(" No relevant appointments found ",0,0,1)
'     return (1)
'   end if
'   vloadif(dpath|"prep_min.vw")
'   order change index "prep1.idx"
'   order sort execute dictionary "prepdays.dfs" index "prepdays.idx"
'   vloadif(dpath|"shwappt4.vw")
'   order change index "prepdays.idx"
END FUNCTION 'SelectRecords()


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()
