'REPREN01 - entering & updating Rep's records

external   cpath dpath navrecs() vloadif() messbox() fentrybox() pagerec()
external   vunloadif() sch scw PrintReport()

public     ptval ptstr

global     viewrec() updaterec() reprcode printrecs() addrec() x reprname

MAIN
single-step off
'   quiet off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"reprview.vw")
  vloadif(dpath|"reprsel2.vw")
  repaint on
  repaint

  ptval=0
  while ptval <> {Esc}
     ptval = navrecs()
     if ptval = {Enter}
        viewrec()
     elseif ptval = {A} or ptval = {a}
        addrec()
     elseif ptval = {U} or ptval = {u}
        updaterec()
'      elseif ptval = {P} or ptval = {p}
'         printrecs()
     elseif ptval = {Esc}
       messbox("Return to Menu? (y/n)",1,0,1)
       if ptstr ! "y"
         vunloadif("reprview.vw")
         vunloadif("reprsel2.vw")
         transfer cpath|"pm_menu.psl" in-memory
       end if
       ptval = {^Home}
     end if
  end while
  vunloadif("reprsel2.vw")
  vunloadif("reprview.vw")
  transfer cpath|"pm_menu.psl" in-memory
END MAIN


FUNCTION addrec()
local z
  reprname = ""
  repaint off
  vloadif(dpath|"repr_add.vw")
  while true			' enter name of company
    x = fentrybox(" Enter name of representative ",35,"AU*34{x}",reprname)
    if x = 0
      reprname = proper(ptstr)
      while true
        messbox(" Confirm"&reprname|"? (y/n) ",1,1,1)
        if ptstr == "y"
            exit while
        else
          continue while
        end if
      end while
    end if

    while true
      data find "[Rep's_Name]" partial reprname options "gw"  ' check name
      if cerror
        exit while
      else
        messbox(" Confirm similar name"&reprname&"not already in List? (y/n) ",1,1,1)
        if ptstr == "y"
          exit while
        else
          exit function
        end if
      end if
    end while
    exit while
  end while

  x = 1
  while true
    reprcode = upper(left(reprname,3)|right("000"|str(x),3))
    data find "[Reps_Code]" equal reprcode options "g"  ' check code
    if cerror
      exit while
    end if
    x = x + 1
  end while

  while true
    x = fentrybox(" Confirm Representative Code ",6,"*3AU*3#",reprcode)
    if x = 0
      reprcode = ptstr
      data find "[Reps_Code]" partial reprcode options "g"  ' check code
      if cerror
        exit while
      else
        messbox(" Code already exists ",0,1,1)
        continue while
      end if
    end if
  end while

  while true
    data goto view (dpath|"repr_add.vw")
    data enter lock
      [Reps_Code] = reprcode
      [Rep's_Name] = reprname
      repaint on
      data update only-one
      repaint off
    write-record
    smartpeek $_lastkey z
    if z = {Esc}
      messbox(" This will not be saved - press {Ctrl}{Enter} when finished ",0,0,1)
      continue while
    else
      exit while
    end if
  end while

  data goto view "reprsel2.vw"
  repaint on
  repaint
END FUNCTION


FUNCTION viewrec()
local z #prec
  #prec = precord
  vloadif(dpath|"reprview.vw")
  data goto record record-number #prec
  repaint on
  pagerec(1,1)
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"reprsel2.vw")
  repaint on
  repaint
END FUNCTION


FUNCTION printrecs()
'   vloadif(dpath|"reprview.vw")
'   messbox(" Printing brief details of all Suppliers ",0,1,1)
' '   PrintReport("suppart.dfr","",1,2,1,1)
' '   print report execute "suppart.dfr" printer detail start 1 end 0 copies 1
'   vloadif(dpath|"reprsel2.vw")
'   repaint on
'   repaint
END FUNCTION


FUNCTION updaterec()
local z #prec
  #prec = precord
  vloadif(dpath|"repr_add.vw")
  data goto record record-number #prec
  while true
    lock-record
      reply on nothing to 3015
      repaint on
      data update only-one
      repaint off
    write-record
    smartpeek $_lastkey z
    if z = {Esc}
      messbox(" This will not be saved - press {Ctrl}{Enter} when finished ",0,0,1)
      continue while
    else
      exit while
    end if
  end while
  data goto view "reprsel2.vw"
  repaint on
  repaint
END FUNCTION
