'SUPPLR01 - used for entering, updating, printing Supplier details

external   cpath dpath navrecs() vloadif() messbox() fentrybox() pagerec()
external   vunloadif() sch scw PrintReport() progress() remove() ipath

public     ptval ptstr

global     viewrec() updaterec() suppcode printrecs() addrec() x suppname

MAIN
single-step off
'   quiet off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"suppview.vw")
  vloadif(dpath|"suppsel2.vw")
  order change key [Supplier_Code]
  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()
    end if
  end while
  ' create new sort index
'   progress(15,10," Creating new Suppliers' index ... please wait ",0)
'   vloadif(dpath|"suppsel2.vw")
'   order change physical
'   remove(ipath|"suppname.idx")
'   order sort execute dictionary "upd_supp.dfs" index ipath|"suppname.idx"
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END MAIN


FUNCTION addrec()
local z
  suppname = ""
  repaint off
  vloadif(dpath|"supp_add.vw")
  while true			' enter name of company
    x = fentrybox(" Enter name of Company ",35,"AU*34{x}",suppname)
    if x = 0
      suppname = proper(ptstr)
      while true
        messbox(" Confirm"&suppname|"? (y/n) ",1,1,1)
        if ptstr == "y"
          if right(suppname,3) <> "Ltd"
            messbox(" Is this a Limited Company? (y/n) ",1,1,1)
            if ptstr == "y"
              suppname = suppname&"Ltd"
              continue while
            elseif ptstr == "n"
              exit while
            end if
          else
            exit while
          end if
        else
          continue while
        end if
      end while
    end if

    while true
      data find "[Name]" partial suppname options "gw"  ' check name
      if cerror
        exit while
      else
        messbox(" Confirm similar name"&suppname&"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
    suppcode = upper(left(suppname,3)|right("000"|str(x),3))
    data find "[Supplier_Code]" equal suppcode options "g"  ' check code
    if cerror
      exit while
    end if
    x = x + 1
  end while

  while true
    x = fentrybox(" Confirm Supplier Code ",6,"*3AU*3#",suppcode)
    if x = 0
      suppcode = ptstr
      data find "[Supplier_Code]" partial suppcode options "g"  ' check code
      if cerror
        exit while
      else
        messbox(" Code already exists ",0,1,1)
        continue while
      end if
    else
      message "FENTRY Error:"&str(x)
    end if
  end while

  while true
    data goto view (dpath|"supp_add.vw")
    data enter lock
      [Supplier_Code] = suppcode
      [Name] = suppname
    write-record
      error off
      repaint on
      data update only-one
      repaint off
'     write-record
    smartpeek $_lastkey z
' message "z is:"&str(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 "suppsel2.vw"
  repaint on
  repaint
END FUNCTION


FUNCTION viewrec()
local z #prec
  #prec = precord
  vloadif(dpath|"suppview.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|"suppsel2.vw")
  repaint on
  repaint
END FUNCTION


FUNCTION printrecs()
local z #prec
'   #prec = precord
'   vloadif(dpath|"suppview.vw")
  messbox(" This is a full list of"&str(precords)&"records - continue? (y/n) ",1,0,1)
  if ptstr == "n"
    vloadif(dpath|"suppsel2.vw")
    repaint on
    repaint
    return (0)
  end if
  PrintReport("suppart.dfr","",1,2,1,1)
'   print report execute "suppart.dfr" printer detail start 1 end 0 copies 1
  vloadif(dpath|"suppsel2.vw")
  repaint on
  repaint
END FUNCTION


FUNCTION updaterec()
local z #prec
  #prec = precord
  vloadif(dpath|"supp_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 "suppsel2.vw"
  repaint on
  repaint
END FUNCTION

