'VU_ARCHV - view order details, req'ns & receipts for archived CUST_ORD records

external   fentrybox() messbox() vloadif() vunloadif() dpath shopmask scr
external   sch scw progress() fgp bgp messline() wraptext() popuplist()
external   userid menuchoice cpath entryline() navrecs() messboxwait() apath
external   base ipath bpopdb()

public     invtot ptstr custcode dsa abbrv_name ptval jobnr $refnr $ftrname

global     CheckNr() ShowAllReqs() S_details #count navreqns() FindJob()
global     ReturnToMenu() jobdesc ftgcomm ftginstr ftgscrn y z slotrec y1 y2
global     x $ctgplan ftginit #lenareas #startc #startr m_f m_b
global     $instruct #upper #rem m1 m5 S_status
global     $delonly ftgplan ShowText() mess5 $ordstat EnterCustName()
global     custname bot psmode y3 ftrcode y4

MAIN
single-step off
  m_f = 4
  m_b = 3
'   apath = "C:\archive\"              'message "apath) is:"&str(apath)
  file unload all
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  error off
  bot = 8

  while true
    window close
    if cerror
      exit while
    end if
  end while

  while true
    x = CheckNr()                          ' check Job Nr exists in CUST_ORD
    if x = -1
      exit while
    end if
  end while

  ReturnToMenu()

END MAIN


FUNCTION FindJob()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  $ftrname = ""
  progress(fgp,bgp," Checking for existing order ",0)
  data find "[Job_Nr]" equal jobnr options "gw"
  if cerror                               '   if none - then return
    messbox(" Job not found in Archive files ",0,0,1)
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    return (-1)
  else
    jobdesc  = [Description]
    ftginstr = [Instructions]
    ftgcomm  = [Fitting_Comment]
    jobdesc  = @if(len(jobdesc)=0,"Not known",jobdesc)
    ftginstr = @if(len(ftginstr)=0,"Not known",ftginstr)
    ftgcomm  = @if(len(ftgcomm)=0,"Not known",ftgcomm)
    slotrec  = [Appt_Slots]
    ftrcode  = [Ftr_Code]

    if len(ftrcode) = 0
      $ftrname = "Not known"
    elseif ftrcode = blank
      $ftrname = "Not known"
    else
      vloadif(dpath|"ftr_list.vws")
      error off
      $ftrname = filelookup([Fitter_Code],[Nickname],ftrcode)
      if cerror
        $ftrname = "Not known"
      end if
      vunloadif("ftr_list.vws")
      vloadif(apath|"vu_arch1.vw")
    end if

    repaint on
    repaint
    y3 = format(" Fitter:"&$ftrname,"M30")
    screen print 7 25 15 11 y3

'     $ordstat = [Order_Status]
    screen save 1 1 6 scw S_details
    repaint off

' only if more than screen can show
    if tablecount([Product_MRC])>12
      x = messline(" Show ALL requisitions? (y/n) ",1,1,1,22,5,71)
      if ptstr == "y"
        ShowAllReqs()
      end if
    else
' all requsn's shown
      y3 = format(" ALL requisitions shown ","M70")
      screen print 22 5 15 1 y3
      wait 2
    end if

    x = messline(" View Instructions? (y/n) (No=exit) ",1,1,1,22,5,71)
    if x = 0
      if ptstr == "y"
        ShowText()
        y3 = format(" Press any key to exit ... ","M71")
        screen print 22 5 15 1 y3
        inchar
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        return (0)
      else
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        return (0)                 ' = exit
      end if
    end if
  end if
END FUNCTION ' FindJob()


FUNCTION ShowAllReqs()
  vloadif(apath|"arc_reqn.vw")
  order change key "[Job_Nr]"
  data query execute "job_reqn.dfq" index "all_req1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Job_Nr] = jobnr                                        ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messbox(" No requisitions found ",0,0,1)
    return (-1)
  end if

' order by ListOrder & prodMRC
'   order sort now dictionary "all_reqn.idx" fields "[Lst_Stck;Product_MRC]" ascending
  #count = records
  repaint on
  repaint
  screen shortrestore S_details
  y3 = format(" Scroll to check all requisitions ("|str(#count)|") ","M71")
  screen print 8 5 15 1 y3
  y4 = format(" {Esc} to finish ","M70")
  screen print 22 5 15 1 y4

  ptval=0
  while true
    ptval = navreqns()
    if ptval = {Esc}
      exit while
    end if
  end while
  repaint off
  vloadif(apath|"vu_arch1.vw")
END FUNCTION ' ShowAllReqs()


FUNCTION CheckNr()                     ' checks for JobNr - if not found
  while true
    x = popuplist(9,33,13,"ÿÿÿJobÿNr CustomerÿName","{Esc} to exit",1,0)
    if x = 0
      if ptstr = "CustomerÿName"
        x = EnterCustName()
        if x = -1
          continue while
        end if
        jobnr = [Job_Nr]
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        vloadif(apath|"vu_arch1.vw")
        order change key "[Job_Nr]"
        exit while
      else
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        vloadif(apath|"vu_arch1.vw")
        order change key "[Job_Nr]"
        x = fentrybox(" Enter Order Nr or {Esc} to exit ",6,shopmask,"")
        if x = -1
          continue while
        elseif x = 0
          jobnr = ptstr
          exit while
        end if
      end if
    elseif x = -1
      ReturnToMenu()
    end if
  end while
  FindJob()
  return (1)
END FUNCTION ' CheckNr()


FUNCTION ShowText()
  x=wraptext(9,14,13,66,m_f,m_b,jobdesc,"L",1,0,0)  '   message "x is:"&str(x)
  mess5 = "Job details"|@if(slotrec=0,""," - (timeslots"&str(slotrec)|")")
  screen print  9 16 m_f m_b mess5

  x = wraptext(14,14,17,66,m_f,m_b,ftginstr,"L",1,0,0)
  screen print 14 16 m_f m_b "Other comments (eg appointment times etc)"

  x = wraptext(18,14,21,66,m_f,m_b,ftgcomm,"L",1,0,0)
  screen print 18 16 m_f m_b "Comments re Fitting"
END FUNCTION ' ShowText()


FUNCTION ReturnToMenu()
 file unload all
 transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION EnterCustName()
  vloadif(apath|"A_cstsel.vw")
  if base = "O" or base = "W"
    order change index apath|"cust_ord.idx"
  else
    order change index apath|base|"_orders.idx"
  end if

  x = fentrybox(" Enter 1st SEVEN letters of name ",7,"","")
  abbrv_name = ptstr
  screen clear box 22 1 sch scw 0 0 no-border

  x = bpopdb("A_cstsel",5,"fi"&abbrv_name,"[Name]","L37","[Abbrv_Name]","L7","[Job_Nr]",3,42,14,80,"",0)
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  if x = -1
    return(-1)
  else
    jobnr = ptstr                      'message "ptstr) is:"&str(ptstr)
'     title1 = [Name]
    return(1)
  end if
END FUNCTION 'EnterCustName()


' FUNCTION EnterCustName()
'   while true
'     while true
'       x = fentrybox(" Enter first five letters of Customer's Name or {Esc} ",5,"","")
'       if x = 0
'         if len(ptstr)=0
'           continue while
'         end if
'         exit while
'       elseif x = -1
'         return (-1)
'       end if
'     end while
'     abbrv_name = proper(ptstr)
'
'     screen clear box 1 1 sch scw 0 0 no-border
'     repaint off
'     vloadif(apath|"custsel5.vw")
'
'     order change key "[Abbrv_Name]"
'     data find "[Abbrv_Name]" equal abbrv_name options ""
'     if cerror
'       messbox(" Name"&chr(34)|abbrv_name|chr(34)|" not held on file ",0,0,1)
'       continue while
'     else
'       repaint on
'       repaint
'       ptval=0
'       y1 = format(" Name                                Delivery Address","L71")
'       y2 = format(" {Enter} to select - {Esc} exits ","M71")
'       screen print 4 6 fgp bgp y1
'       screen print 20 6 fgp bgp y2
'
'       while true
'         ptval = navrecs()
'         if ptval = {Enter}
'           repaint off
'           jobnr = [Job_Nr]
'           return (0)
'         elseif ptval = {Esc}
'           screen clear box 1 1 sch scw 0 0 no-border
'           repaint off
'           return (-1)
'         end if
'       end while
'     end if
'   end while
' END FUNCTION 'EnterCustName()


FUNCTION Navreqns()
local x psmode pd pq
  screen save scrheight 1 scrheight scrwidth bot
  smartpeek $_spndmes psmode
  if psmode = 1
    smartpoke $_spndmes 0
  end if

  while TRUE
    x = inchar
    if x = {Down}
      data goto record next

    elseif x = {Up}
      data goto record previous

    elseif x = {PgDn}
      data goto page next

    elseif x = {PgUp}
      data goto page previous

    elseif x = {^End}
      data goto record last

    elseif x = {^Home}
      data goto record first

    else
      exit while
    end if
  end while
  if psmode = 1
    smartpoke $_spndmes 1
  end if
  return (x)
END FUNCTION   'Navreqns()

