'PREP_EST - creates EST_DATE file from all records in EST_APPT.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()

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|"est_date.vws")
  error off
  data query execute "delete.dfq"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'  not(deleted) replace delete
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  vunloadif("est_date.vws")
  data utilities purge dpath|"est_date.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 ",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|"shw_est4.vw")
'   order change key "[DFA]"
  data query execute "prepday2.dfq" index "prep0.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Date])>(days(today)-60)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No relevant appointments found ",0,0,1)
    return (1)
  end if
  order sort now dictionary "prep1.idx" fields "[DFA]" ascending

'   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

'   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 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|"est_date.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|"shw_est4.vw")
END FUNCTION 'WriteApptDate()


FUNCTION DrawLine()
  vloadif(dpath|"est_date.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|"shw_est4.vw")
END FUNCTION ' DrawLine()



