'ALT_APPT - enter, update and cancel Fitting Appointments.  This is the
'           only way to enter, update, cancel appts.  All screens used in
'           other modules should have the appropriate field ([Fitting_Date]
'           etc.) marked as "Read-Only".
' REQUSN.Reference_Nr is allocated at L.173

external   messbox() fentrybox() dpath vloadif() vunloadif() cpath
external   sch scw shopmask progress() fgp bgp scr bpopdb() entry chkdate()
external   fgs bgs getrec navrecs() scrollbox() dsa
external   userid relperf() fge bge getday getftr
external   makeidx() arytostr() popuplist() addidxrec()
external   remove() jobnr entryline() messline()

public     ftrcode apptstatus ftrname x ptary[1]
public     ptstr ptval datecode requsnnr viewdate

global     Choice()
global     LoadFiles()
global     ReturnToMenu()
global     ReserveAppt()
global     ChangeDeleteAppt()
global     apptcmmt
global     ChooseDayAndFitter()
global     FirstApptChoose()
global     FirstApptAny()
global     UnloadScreens() Titles()
global     CurrentLastDay()
global     deljobnr
global     existappt
global     slotnr
global     bpop_ret
global     #rec
global     PrepareAppt()
global     bline
global     z
global     #slotsrem $perf apptnr
global     reqd_date custcode deladdr1 custname delpostcode
global     y $appts $span i startdate enddate lastdate
global     $instruct $datelist changeftr #nextrec maxslots jobidx

MAIN
single-step off
file unload all
quiet off
'trace on
  existappt = "n"
  if entry <> "NEW"
    progress(fgp,bgp," Preparing files ... please wait ",0)
  else
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
  end if

'   LoadFiles()           ' LF

  Choice()              ' CH      L.149

  ReturnToMenu()        ' RTM

END MAIN

FUNCTION PrepareAppt()
  repaint off
  if entry == "RWA"
    slotnr = getrec			' needed in ReserveAppt()
    ftrname = getftr			' needed in ReserveAppt()
    viewdate = getday
    entry = "NEW"
  end if

  z = ""
  jobnr = ""
  $datelist = ""
  changeftr = "n"
'  viewdate = today

  while true
    vloadif(dpath|"ftr_list.vws")
    ftrcode = filelookup([Nickname],[Fitter_Code],ftrname)

    vloadif(dpath|"listappt.vw")
    order change physical

    smartpeek $_lastkey z              ' see if the loop started from {F}
    if changeftr == "n"                ' = fitters in bpopdb()
      startdate = viewdate
      enddate = date2(date(startdate)+1)

      data find "[Date]" equal startdate options ""
      if cerror
        messbox(" Appointments not yet created for this date ",0,0,1)
        return (-2)
      end if
      x = precord

      data find "[Date]" equal enddate options ""
      y = precord

      $span = y - x

      redimension ptary[$span]
      for i = 1 to $span
        ptary[i] = x 'str(precord)
        x = x + 1
      end for

      x = arytostr($span)
      if x = 0
        $appts = ptstr
      else
        message "x is:"&str(x)
      end if

      remove("day_ftrs.idx")

      makeidx("appntmnt","day_ftrs.idx",$appts,1)
      repaint off
    end if

    order change index "day_ftrs.idx"
    data query execute "list_ftr.dfq" index "findappt.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
' º   [Fitter_Code] = ftrcode                                          º
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
    if cerror
      message "Bad query"
    end if
    x = ReserveAppt() ' 0=ALL appts bkd; -1=Esc;
    if x = -1
      vunloadif("listappt.vw")
      vunloadif("ftr_list.vws")
      return (-1)
    end if
  end while
END FUNCTION 'PrepareAppt()


FUNCTION Choice()
local y1 y2 y3 y4 y5 y6 $list
  CurrentLastDay()
  existappt = "n"
  if entry = "RFA"          '³
    entry = "NEW"
  elseif entry = "RWA"      '³ no redirection necessary as it started here
    x = PrepareAppt()       '³ specific return with appt date & time
    if x = -2
      return (-1)
'    elseif x = -1
'      return (-1)
    end if
  else
    getrec = ""
    getday = ""
    getrec = ""
  end if                '

while true
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off

  y1="BROWSEÿappointments"
  y2="FirstÿappointmentÿALLÿfitters"
  y3="FirstÿappointmentÿSELECTÿfitter"
  y4="WHOLEÿdayÿONEÿfitter"
  y5="ChangeÿorÿDeleteÿappointments"

  y6=" Appointments created to"&date2(lastdate)|" "
  $list = y1&y2&y3&y4&y5

  x = popuplist(8,24,14,$list,y6,1,0)
  screen shortrestore dsa
  if x = 0
    if ptstr == y1
      screen shortrestore dsa
      execute "nav_appt.rf3" in-memory

    elseif ptstr == y2
      FirstApptAny()

    elseif ptstr == y3
      FirstApptChoose()

    elseif ptstr == y4
      ChooseDayAndFitter()     ' CDF

    elseif ptstr == y5
      ChangeDeleteAppt()
    end if
  else
    ReturnToMenu()
  end if
end while
END FUNCTION ' Choice()

FUNCTION ChangeDeleteAppt()        ' CA
local y2 slotnr
  while true
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    x = fentrybox("  Enter Job Nr for Appointment(s)  ",6,shopmask,"")
    if x = 0
      jobnr = ptstr
    elseif x = -1
      exit function
    else
      message "!!! Function failed !!! - ALT_APPT/l81"&str(x)
      continue while
    end if

    vloadif(dpath|"cust_ord.vws")
    if cerror
      messbox("CUST_ORD file not loaded",0,0,1)
    end if

    order change key "[Job_Nr]"
    data find "[Job_Nr]" equal jobnr options ""
    if cerror                               '   if none - then return
      x = messbox(" Job Number not found ",0,0,1)
      if x = 0
        screen clear box 17 1 18 scw 0 0 no-border
        vloadif(dpath|"listappt.vw")
        repaint on
        repaint
        Titles()
        continue while
      end if
    else
      maxslots = [Appt_Slots]
      #slotsrem = [Slots_Rem]
      custcode = [Customer_Code]
      deladdr1 = [Delivery_Address_1]
      delpostcode = [Del_Postcode]
      custname = [CustOrd_Name]
      exit while
    end if
    exit while
  end while

' show existing appt's
  vloadif(dpath|"findapp1.vw")

'   order change key "[Job_Nr]"
'   data query execute "findappt.dfq" index "chngappt.idx"
' ' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ' ³  [Job_Nr] = jobnr                                                  ³
' ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

  jobidx = "r_"|jobnr|".idx"         ' message "jobidx is:"&str(jobidx)
  error off
  if file(dpath|jobidx) = 1
    order change index dpath|jobidx
  else
    progress(15,12," Creating new index for"&jobnr|" ",0)
    order change key "[Job_Nr]"
    data query execute "job_reqn.dfq" index dpath|jobidx
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'     [Job_Nr] = jobnr
'     and
'     not(deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      makeidx("requsn",dpath|jobidx,"0",1)
      order change index dpath|jobidx
    end if
  end if


  if cerror
    messbox(" No appointments listed for"&jobnr,0,0,1)
    exit function
  end if
  if #slotsrem > 1
    z = (str(#slotsrem)&"more appointments")
  else
    z = case #slotsrem(1,"One more appointment")(0,"No more appointments")
  end if

  repaint on
  repaint
  screen print 7 26 bgs fgs (format("Fittings for"&jobnr,"M31"))
  screen print 15 26 bgs fgs (format("{E}rase appointment","M31"))
  repaint off
  ptval=0			' show navrec() to select
  while true
    ptval = navrecs()
    if ptval = {E} or ptval = {e}
      if [Job_Nr] == "None"
        continue while
      end if
      deljobnr = [Job_Nr]
      viewdate = [Date]
      slotnr = [Appointment_Order]
      messbox(" Erase Appointment Nr"&str(slotnr)&"on"&date2(viewdate)|"? (y/n)",1,0,1)
      if ptstr == "n"
        continue while
      end if

      repaint off
      if dayname([Date]) == "SUNDAY"
        lock-record
          [Job_Nr] = "SUNDAY"
          [DelAddr&Code] = ""
          [Entered_By] = userid
          [Date_Altered] = today
          [Time] = now
          [N_name] = ""
        write-record
      else
        lock-record
          [Job_Nr] = "None"
          [DelAddr&Code] = ""
          [Entered_By] = userid
          [Date_Altered] = today
          [Time] = now
          [N_name] = ""
        write-record
      end if

      vloadif(dpath|"cust_ord.vws")
      order change key "[Job_Nr]"
      data find "[Job_Nr]" equal deljobnr options ""
      lock-record
        [Slots_Rem] = [Slots_Rem] + 1 'increase slots remaining in CUST_ORD
      write-record
      #slotsrem = [Slots_Rem]

      vloadif(dpath|"findapp1.vw")
'       order change physical
      order change key "[Job_Nr]"
      data query execute "findappt.dfq" index "chngappt.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³  [Job_Nr] = jobnr                                                  ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
      if cerror             ' no more appts
        repaint off
        vloadif(dpath|"cust_ord.vws")
        order change key "[Job_Nr]"
        data find "[Job_Nr]" equal jobnr options ""
        lock-record
          [Fitting_Date] = blank	' remove Fitting_Date from CUST_ORD
        write-record

     '  remove viewdate as Expect_Fitting_Date in REQUSN's ****************
        vloadif(dpath|"REQUSN.vws")           ' load requsn file
        repaint off
'         order change physical
        order change key "[Job_Nr]"
        data query execute "delapreq.dfq"
  ' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
  ' ³ [Job_Nr] = jobnr replace [Expect_Fitting_Date] = blank             ³
  ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
        return (0)		' all appts deleted
      end if

'           else
'             repaint off
'             vloadif(dpath|"cust_ord.vws")
'             order change key "[Job_Nr]"
'             data find "[Job_Nr]" equal deljobnr options ""
'             lock-record
'               [Slots_Rem] = [Slots_Rem] + 1 'increase slots remaining in CUST_ORD
'             write-record
'             #slotsrem = [Slots_Rem]
'           end if
'         end if
'       end if

'     change back to appointment screen
      vloadif(dpath|"findapp1.vw")
      order change index "chngappt.idx"
      repaint on
      repaint
      screen print 7 26 bgs fgs (format("Fittings for"&jobnr,"M31"))
      screen print 15 26 bgs fgs (format("{E}rase appointment","M31"))
      repaint off
    end if
  end while
END FUNCTION ' ChangeDeleteAppt()        ' CA

FUNCTION UnloadScreens()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  data goto window 2
  vunloadif("ord_dets.vw")
  data goto window 1
  vunloadif("view_app.vw")
  window close
END FUNCTION


FUNCTION ChooseDayAndFitter()       ' CF - find single appt for fitter
' choose fitter +++++++++++++++++++++++++++++++++++++++++++++++++++++
  vloadif(dpath|"ftrlist1.vw")           ' show list of fitters
  order change physical
  bline = records
  if bline > 12
     bline = 22
  else
    bline = 5 + bline + 5
  end if

  screen clear box 1 1 sch scw 0 0 no-border
  screen print 6 29 bgs fgs (format("Choose Fitter","M24"))
  screen print 7 29 bgs fgs (format("{Enter} to select","M24"))
  screen print bline 29 bgs fgs (format("{Esc} to exit","M24"))

  bpop_ret = bpopdb("ftrlist1",6,"","[Fitter Name]","l40","[Fitter_Code]","L0","[Fitter_Code]",8,29,18,52,"",1)
    screen clear box 1 1 sch scw 0 0 no-border
    if bpop_ret = -1
      exit function
    end if
  ftrcode = ptstr
  ftrname = [Nickname]

' choose day & appt +++++++++++++++++++++++++++++++++++++++++++++++++
  while TRUE
    x = fentrybox(" Enter Date to View ",8,"##\/##\/##",today)
    if x = 0
      viewdate = ptstr
      if chkdate(viewdate,1) = -1
        messbox(" Incorrect date - re-enter ",0,0,1)
	continue while
      end if
    end if
    if dayname(viewdate) == "Sunday"
      if relperf() = 0
        $perf = ptval
      end if
      x=scrollbox(fge,bge," Are you really going to send"&ftrname&"to work on Sunday?",$perf)
      if x <> 0
        message "scrollbox error:"&str(x)
      end if
      screen clear box 1 1 sch scw 0 0 no-border
    '  repaint off
      messbox(" View"&dayname(viewdate)&date3(viewdate)|"? (y/n) ",1,0,1)
      if ptstr == "y"
        exit while
      else
        continue while
      end if
    end if
    exit while
  end while

  PrepareAppt()
  exit function
END FUNCTION ' ChooseDayAndFitter()       ' CF - find single appt for fitter

FUNCTION FirstApptChoose()  ' find single appt for chosen fitter
' choose fitter from bpopdb()
'  screen clear box 1 1 sch scw 0 0 no-border
'  repaint off
  vloadif(dpath|"ftrlist1.vw")           ' show list of fitters
  order change physical
  order sort now dictionary "ftrname.idx" fields "[Fitter Name]" ascending
  bline = records
  if bline > 12
     bline = 22
  else
    bline = 5 + bline + 5
  end if

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  screen print 6 29 bgs fgs (format("Choose Fitter","M24"))
  screen print 7 29 bgs fgs (format("{Enter} to select","M24"))
  screen print bline 29 bgs fgs (format("{Esc} to exit","M24"))

  bpop_ret = bpopdb("ftrlist1",6,"","[Fitter Name]","l40","[Fitter_Code]","L0","[Fitter_Code]",8,29,18,52,"",1)
  screen shortrestore dsa
  repaint off
    if bpop_ret = -1
      screen clear box 1 1 sch scw 0 0 no-border
'      vunloadif("ftrlist1.vw")
      exit function
    end if
  ftrcode = ptstr
  ftrname = [Nickname]

  vloadif(dpath|"findappt.vw")

'  if file(dpath|"#frstftg.idx") = 0	' if not there create it
'  if file("#frstftg.idx") = 0	' if not there create it
    order change physical
    order change key "[Date_Code]"
'    data query execute "firstftg.dfq" index dpath|"#frstftg.idx"
    data query execute "firstftg.dfq" index "#frstftg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³  Query is:   [Appointment_Order] ! "1" and [Job_Nr] ! "None"       ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      Messbox(" No appointments found in current diary ",0,0,1)
      exit function
    end if
'  end if

'  order change index dpath|"#frstftg.idx"
'  order change index "#frstftg.idx"

' then select subset of #frstftg.idx for chosen fitter
  remove("findftr.idx")
  data query execute "list_ftr.dfq" index "findftr.idx"
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
' º   [Fitter_Code] = ftrcode                                          º
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
  while true
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    order sort execute dictionary "firstftg.dfs" index "sortftg.idx"
    screen print 6 20 bgs fgs (format("Vacant First Appointments","M41"))
    screen print 7 20 bgs fgs (format("Press {Enter} to view whole day","M41"))
    screen print 19 20 bgs fgs (format("Press {Esc} to exit","M41"))
    bpop_ret = bpopdb("findappt",6,"","[Date_Name]","L40","[Fitter_Name]","L25","[Date_Code]",8,20,18,60,"",0)
    screen shortrestore dsa
'    screen clear box 1 1 sch scw 0 0 no-border
    if bpop_ret = -1
      exit function
    end if
    datecode = ptstr
    ftrname  = [Nickname]
'  message str(ftrname)
    viewdate = date2([Date])
    repaint off

    PrepareAppt()
    exit function
  end while
END FUNCTION '  FirstApptChoose()

FUNCTION FirstApptAny()       ' CF
  vloadif(dpath|"findappt.vw")

'  remove(dpath|"#frstftg.idx")
'  if file(dpath|"#frstftg.idx") = 0	' if not there create it
'  if file("#frstftg.idx") = 0	' if not there create it

  order change physical
  order change key "[Date_Code]"
'    data query execute "firstftg.dfq" index dpath|"#frstftg.idx"
  data query execute "firstftg.dfq" index "#frstftg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³  Query is:   [Appointment_Order] ! "1" and [Job_Nr] ! "None"       ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    Messbox(" No appointments found in current diary ",0,0,1)
    exit function
  end if

  while true
    order sort execute dictionary "firstftg.dfs" index "sortftg.idx"
    screen print 6 20 bgs fgs (format("Vacant First Appointments","M41"))
    screen print 7 20 bgs fgs (format("Press {Enter} to view whole day","M41"))
    screen print 19 20 bgs fgs (format("Press {Esc} to exit","M41"))
    bpop_ret = bpopdb("findappt",6,"","[Date_Name]","L40","[Fitter_Name]","L25","[Date_Code]",8,20,18,60,"",0)
    screen shortrestore dsa
    if bpop_ret = -1
      exit function
    end if
    datecode = ptstr
    ftrname  = [Nickname]
    viewdate = date2([Date])
    repaint off

    PrepareAppt()
    exit function
  end while
END FUNCTION ' FirstApptAny()       ' CF

FUNCTION ReturnToMenu()      ' RTM
  screen clear box 1 1 sch scw 0 0 no-border
  file unload all
'   transfer cpath|"pm_menu.psl" in-memory
END FUNCTION

FUNCTION LoadFiles()         ' LF
  repaint off
  apptcmmt = ""
  apptstatus = ""
  jobnr = ""
  requsnnr = ""
  ftrcode = ""

  vloadif(dpath|"REQUSN.vws")
  if cerror
    messbox("REQUSN file not loaded",0,0,1)
  end if

  vloadif(dpath|"findappt.vw")
  if cerror
    messbox("FINDAPPT file not loaded",0,0,1)
  end if

  vloadif(dpath|"FTR_LIST.vws")
  if cerror
    messbox("FITTER's file not loaded",0,0,1)
  end if

  vloadif(dpath|"CUST_ORD.vws")
  if cerror
    messbox("CUST_ORD file not loaded",0,0,1)
  end if

  vloadif(dpath|"viewdate.vw")
  if cerror
    messbox("VIEWDATE view not loaded",0,0,1)
  end if
END FUNCTION ' LoadFiles()

FUNCTION ReserveAppt()	' vloadif(dpath|"listappt.vw") & order by index first
local    slotsavail daysshown lastact
' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
' º used when an appt. is selected from S/S or this program            º
' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼
  for i = 1 to (value(slotnr)-1)
    data goto record next
  end for
  repaint off
  daysshown = 20
  lastact = ""
  while true
        repaint on
        repaint
    Titles()
    ptval=0
    while ptval <> {Esc}
      ptval = navrecs()
      if ptval = {B} or ptval = {b}		' {R} @ 747
        screen clear box 17 1 18 scw 0 0 no-border
        lastact = "b"
        if [Job_Nr] ! "None" or [Job_Nr] ! "SUNDAY" or [Job_Nr] ! "BNKHOL"
          repaint off
          x = entryline("   Enter Job Nr for Appointment(s)   ",6,shopmask,"",17,20,41)
          if x = 0
            jobnr = ptstr
          elseif x = -1
            continue while
          else
            message "!!! Function failed !!! - ALT_APPT/l81"&str(x)
            continue while
          end if
          vloadif(dpath|"cust_ord.vws")
          if cerror
            messbox("CUST_ORD file not loaded",0,0,1)
          end if
          order change key "[Job_Nr]"
          data find "[Job_Nr]" equal jobnr options ""
          if cerror                               '   if none - then return
            x = messbox(" Job Number not yet entered ",0,0,1)
            if x = 0
              screen clear box 17 1 18 scw 0 0 no-border
              vloadif(dpath|"listappt.vw")
              repaint on
              repaint
              Titles()
              continue while
            end if
          else
            $instruct = [Instructions]
            maxslots = [Appt_Slots]
            #slotsrem = [Slots_Rem]
            reqd_date = [Fitting_Date]
            custcode = [Customer_Code]
            deladdr1 = [Delivery_Address_1]
            delpostcode = [Del_Postcode]
            custname = [CustOrd_Name]
          end if

          if #slotsrem > 1
            z = (str(#slotsrem)&"more appointments")
          elseif #slotsrem = 1
            z = "One more appointment"
          elseif #slotsrem = 0	' check that all appt slots for job have been allocated
            messbox(" No more appointments needed for"&jobnr|" ",0,0,1)
            return (0)
          end if

          vloadif(dpath|"listappt.vw")
          Titles()
          screen print 17 5 bgs fgs (format(left(z&"needed for"&jobnr&"@"&deladdr1&delpostcode,70),"M70"))
          screen print 18 5 15 12 (format(left($instruct,70),"M70"))

          apptnr = [Job_Nr]	'check that APPNTMNT record is available
          if apptnr ! "None" or apptnr ! "SUNDAY" or apptnr ! "BNKHOL"
            lock-record
              [Job_Nr] = jobnr
              [DelAddr&Code] = left(deladdr1|","&delpostcode,30)
              [Entered_By] = userid
              [Date_Altered] = today
              [Time] = now
              [N_name] = ftrname
            write-record
            #rec = record
            #nextrec = record

' if this is a 1st fitting - remove record from dpath|"#frstftg.idx"

            repaint off
            vloadif(dpath|"cust_ord.vws")
            lock-record			  ' update CUST_ORD record
              [Slots_Rem] = [Slots_Rem] - 1
            write-record
            #slotsrem = [Slots_Rem]

            vloadif(dpath|"listappt.vw")  '  return to LISTAPPT
            repaint on
            repaint
          else
            repaint on
            repaint
            continue while
          end if

          if #slotsrem > 1
            z = (str(#slotsrem)&"more appointments")
          elseif #slotsrem = 1
            z = "One more appointment"
          elseif #slotsrem = 0	' check that all appt slots for job have been allocated
            while true
              screen clear box 17 1 18 scw 0 0 no-border
              Titles()
              messline(" Confirm"&date2(viewdate)&"as Fitting Date? (y/n)",1,1,1,17,21,40)
              repaint off
              if ptstr == "y"
                vloadif(dpath|"cust_ord.vws")
                order change key "[Job_Nr]"
                data find "[Job_Nr]" equal jobnr options ""
                lock-record
                  [Fitting_Date] = date2(viewdate)
                write-record

'  enter viewdate as expect ftg date in REQUSN's ************************
                vloadif(dpath|"REQUSN.vws")           ' load requsn file
'                 order change physical
                order change key "[Job_Nr]"
                data query execute "chngdate.dfq"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [Job_Nr] = jobnr replace [Expect_Fitting_Date] = viewdate          ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
                return (-1)                   ' Esc was pressed
              else
                messbox(" Fitting Date must be same as Appts - erase bookings and re-book? (y/n) ",1,1,1)
                if ptstr == "y"
                  exit while
                else
                  continue while
                end if
              end if
            end while
            continue while
          end if
          Titles()
          screen print 17 5 bgs fgs (format(z&"needed for"&jobnr&"@"&deladdr1&delpostcode,"M70"))
          screen print 18 5 15 12 (format(left($instruct,70),"M70"))
        else
          continue while
        end if

      elseif ptval = {R} or ptval = {r}
        if lastact = "b"
          if [Job_Nr] == "None"
            continue while
          end if

          data goto record next
          if [Job_Nr] ! "None" or [Job_Nr] ! "SUNDAY" or [Job_Nr] ! "BNKHOL"
            repaint off
            if #slotsrem > 1
              z = (str(#slotsrem)&"more appointments")
            elseif #slotsrem = 1
              z = "One more appointment"
            elseif #slotsrem = 0	' check that all appt slots for job have been allocated
              messbox(" No more appointments needed for"&jobnr|" ",0,0,1)
              return (0)
            end if

            vloadif(dpath|"listappt.vw")
            Titles()
            screen print 17 5 bgs fgs (format(left(z&"needed for"&jobnr&"@"&deladdr1&delpostcode,70),"M70"))
            screen print 18 5 15 12 (format(left($instruct,70),"M70"))

            apptnr = [Job_Nr]
    '       check that APPNTMNT record is available & update with details
            if apptnr ! "None" or apptnr ! "SUNDAY" or apptnr ! "BNKHOL"
              lock-record
                [Job_Nr] = jobnr
                [DelAddr&Code] = left(deladdr1|","&delpostcode,30)
                [Entered_By] = userid
                [Date_Altered] = today
                [Time] = now
                [N_name] = ftrname
              write-record
              #rec = record
              #nextrec = record
' if this is a 1st fitting - remove record from dpath|"#frstftg.idx"

              repaint off
              vloadif(dpath|"cust_ord.vws")    ' update CUST_ORD record
              lock-record
                [Slots_Rem] = [Slots_Rem] - 1
              write-record
              #slotsrem = [Slots_Rem]

              vloadif(dpath|"listappt.vw")     ' return to LISTAPPT
              repaint on
              repaint
            else
              repaint on
              repaint
              continue while
            end if

            if #slotsrem > 1
              z = (str(#slotsrem)&"more appointments")
            elseif #slotsrem = 1
              z = "One more appointment"
            elseif #slotsrem = 0	' check that all appt slots for job have been allocated
              while true
                screen clear box 17 1 18 scw 0 0 no-border
                Titles()
                messline(" Confirm"&date2(viewdate)&"as Fitting Date? (y/n)",1,1,1,17,21,40)
                repaint off
                if ptstr == "y"
                  vloadif(dpath|"cust_ord.vws")
                  order change key "[Job_Nr]"
                  data find "[Job_Nr]" equal jobnr options ""
                  lock-record
                    [Fitting_Date] = date2(viewdate)
                  write-record

'  enter viewdate as expect ftg date in REQUSN's ************************
                  vloadif(dpath|"REQUSN.vws")           ' load requsn file
'                   order change physical
                  order change key "[Job_Nr]"
                  data query execute "chngdate.dfq"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [Job_Nr] = jobnr replace [Expect_Fitting_Date] = viewdate          ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
                  return (-1)                   ' Esc was pressed
                else
                  messbox(" Fitting Date must be same as Appts - erase bookings and re-book? (y/n) ",1,1,1)
                  if ptstr == "y"
                    exit while
                  else
                    continue while
                  end if
                end if
              end while
              continue while
            end if
            Titles()
            screen print 17 5 bgs fgs (format(z&"needed for"&jobnr&"@"&deladdr1&delpostcode,"M70"))
            screen print 18 5 15 12 (format(left($instruct,70),"M70"))
          else
            continue while
          end if

        elseif lastact = "e"
          data goto record next
          if [Job_Nr] == "None"
            continue while
          end if
          deljobnr = [Job_Nr]
          messbox(" Delete appointment for"&deljobnr&"on"&viewdate|"? (y/n)",1,0,1)
          if ptstr == "n"
            continue while
          end if
          if dayname([Date]) == "SUNDAY"
            lock-record
              [Job_Nr] = "SUNDAY"
              [DelAddr&Code] = ""
              [Entered_By] = userid
              [Date_Altered] = today
              [Time] = now
              [N_name] = ""
            write-record
          else
            lock-record
              [Job_Nr] = "None"
              [DelAddr&Code] = ""
              [Entered_By] = userid
              [Date_Altered] = today
              [Time] = now
              [N_name] = ""
            write-record
          end if

    ' if this is a 1st fitting - add record to dpath|"#frstftg.idx"
    ' !!! index is deleted pending correct treatment by addidxrec()
    '      if [Appointment_Order] = "1"
    '        remove(dpath|"#frstftg.idx")
    ''        x = addidxrec(dpath|"#frstftg.idx",precord,3)
    '        if x <> 0
    '          message "Rebuild #frstftg.idx - addidxrec() failed"&str(x)
    '        end if
    '      end if

          repaint off
          vloadif(dpath|"cust_ord.vws")
          order change key "[Job_Nr]"
          data find "[Job_Nr]" equal deljobnr options ""
          lock-record
            [Slots_Rem] = [Slots_Rem] + 1  'increase slots in CUST_ORD
          write-record
          maxslots = [Appt_Slots]
          #slotsrem = [Slots_Rem]
          deladdr1 = [Delivery_Address_1]
          delpostcode = [Del_Postcode]
          $instruct = [Instructions]

          vloadif(dpath|"listappt.vw")
          if #slotsrem > 1
            z = (str(#slotsrem)&"more appointments")
          else
            z = case #slotsrem(1,"One more appointment")(0,"No more appointments")
          end if

          repaint on
          repaint
          Titles()
          screen print 17 5 bgs fgs (format(z&"needed for"&jobnr&"@"&deladdr1&delpostcode,"M70"))
          screen print 18 5 15 12 (format(left($instruct,70),"M70"))
        end if

      elseif ptval = {E} or ptval = {e}
        lastact = "e"
        if [Job_Nr] == "None"	  	'check that APPNTMNT record is available
          continue while
        end if

        deljobnr = [Job_Nr]
        messbox(" Delete appointment for"&deljobnr&"on"&viewdate|"? (y/n)",1,0,1)
        if ptstr == "n"		      	' confirm deletion?
          continue while
        end if
        if dayname([Date]) == "SUNDAY"
          lock-record
            [Job_Nr] = "SUNDAY"
            [DelAddr&Code] = ""
            [Entered_By] = userid
            [Date_Altered] = today
            [Time] = now
            [N_name] = ""
          write-record
        else
          lock-record
            [Job_Nr] = "None"
            [DelAddr&Code] = ""
            [Entered_By] = userid
            [Date_Altered] = today
            [Time] = now
            [N_name] = ""
          write-record
        end if

  ' if this is a 1st fitting - add record to dpath|"#frstftg.idx"
  ' !!! index is deleted pending correct treatment by addidxrec()
  '      if [Appointment_Order] = "1"
  '        remove(dpath|"#frstftg.idx")
  ''        x = addidxrec(dpath|"#frstftg.idx",precord,3)
  '        if x <> 0
  '          message "Rebuild #frstftg.idx - addidxrec() failed"&str(x)
  '        end if
  '      end if
      '       increase slots remaining in CUST_ORD
        repaint off
        vloadif(dpath|"cust_ord.vws")
        order change key "[Job_Nr]"
        data find "[Job_Nr]" equal deljobnr options ""
        lock-record
          [Slots_Rem] = [Slots_Rem] + 1
        write-record
        maxslots = [Appt_Slots]
        #slotsrem = [Slots_Rem]
        deladdr1 = [Delivery_Address_1]
        delpostcode = [Del_Postcode]
        $instruct = [Instructions]
  '       return to LISTAPPT
        vloadif(dpath|"listappt.vw")

        repaint off

        if #slotsrem = maxslots	' remove Fitting_Date from CUST_ORD & REQUSN
          screen clear box 17 1 18 scw 0 0 no-border
          vloadif(dpath|"cust_ord.vws")
          order change key "[Job_Nr]"
          data find "[Job_Nr]" equal jobnr options ""
          lock-record
            [Fitting_Date] = ""		' remove Fitting_Date from CUST_ORD
          write-record

  '  remove viewdate as Expect_Fitting_Date in REQUSN's ************************
          vloadif(dpath|"REQUSN.vws")           ' load requsn file
          repaint off
'           order change physical
          order change key "[Job_Nr]"
          data query execute "delapreq.dfq"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [Job_Nr] = jobnr replace [Expect_Fitting_Date] = ""                ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
          return (-1)
        elseif #slotsrem > 1
          z = (str(#slotsrem)&"more appointments")
        elseif #slotsrem = 1
          z = "One more appointment"
          continue while
        end if

        repaint on
        repaint
        Titles()
        screen print 17 5 bgs fgs (format(z&"needed for"&jobnr&"@"&deladdr1&delpostcode,"M70"))
        screen print 18 5 15 12 (format(left($instruct,70),"M70"))

      elseif ptval = {F} or ptval = {f}
        repaint off
        changeftr = "y"
        vloadif(dpath|"ftrlist1.vw")
        bpop_ret = bpopdb("ftrlist1",6,"","[Fitter Name]","L17","[Fitter_Code]","L0","[Fitter_Code]",7,61,15,80,"",0)
        if bpop_ret = -1
          vloadif(dpath|"listappt.vw")
          continue while
        end if
        ftrcode = ptstr
        ftrname = [Nickname]
        repaint off
        exit function
      elseif ptval = {D} or ptval = {d}

        repaint off
        vloadif(dpath|"minidate.vws")
        data find "[Ftg_Date]" equal date2(viewdate) options ""
        x = bpopdb("minidate",8,"","[Ftg_date]","D2","[Ftg_Date]","L0","[Ftg_Date]",7,9,15,19,"",0)
        if x = 0
          viewdate = date2(ptstr)
        elseif x = -1
          vloadif(dpath|"listappt.vw")
          continue while
        else
          message str(x)
        end if
        repaint off
        exit function
      end if
    end while				' end of NAVREC() loop
    return (-1)
  end while
END FUNCTION ' ReserveAppt()


FUNCTION CurrentLastDay()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"creatapp.vw")          '
  if precords = 0
    messbox(" No appointments yet created ",0,0,1)
  else
    data goto record last
    lastdate = [Date]                     ' find latest date in existing file
  end if
END FUNCTION ' CurrentLastDay()

FUNCTION Titles()
screen print 6 20 bgs fgs (format("Fittings for"&ftrname,"M41"))
screen print 7 20 bgs fgs (format("On"&dayname(viewdate)|","&date3(viewdate),"M41"))
screen print 15 20 bgs fgs (format("{B}ook / {R}epeat / {E}rase appointments","M41"))
screen print 16 20 bgs fgs (format("Change {F}itter or {D}ate - {Esc} exits","M41"))
END FUNCTION ' Titles()
