'ST_PRODS - used for entering, updating, printing Product details

external   cpath dpath navrecs() vloadif() messbox() fentrybox() pagerec()
external   vunloadif() sch scw bpopdb() progress() PrintReport()

public     ptval ptstr

global     NameToFind thisrecord viewrec() FindProd() ChooseRecord()
global     FindName() PrintRecs() AddRec() LoadFiles()
global     ReturnToMenu() EnterCode() newcode x ViewProducts()

MAIN
single-step off

  LoadFiles()

  ' to be confirmed with POPart as navrecs() wil not operate when ordered by key
  'messbox(" Sort by Name? (y/n) ",1,0,1)
'if ptstr ! "y"
'   let Key = "Code"
'   order change key [Name]
'end if

'  ChooseRecord()
  ViewProducts()

  ReturnToMenu()

END MAIN

FUNCTION  ViewProducts()
local x
while true
  progress(15,10," Files loading ... please wait ",0)
  vloadif(dpath|"prodview.vw")
  order change index "prodview"
  x = bpopdb("prodview",6,"fp~AU*5X~","[Code_Supp]", \
  "L45","[Product_MRC]","L25","[Product_Code]",2,5,20,76,"",1)
  if x = 0
    thisrecord = record
    ViewRec()
    continue while
  elseif x = -1
    exit function
  end if
end while
END FUNCTION ' ViewProducts()

FUNCTION ViewRec()
  data goto record record-number thisrecord
  screen clear box 1 1 sch scw 0 0 no-border
  repaint on
  repaint
  pagerec(1,1)
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
END FUNCTION

FUNCTION FindProd()
  while true
    x = fentrybox(" Enter Product Code to find ",8,"AU\/*6#","")
    if x = 0
      exit while
    end if
  end while
  NameToFind=ptstr
  data find "[Product_Code]" equal NameToFind options "gi"
  if cerror
'    tone("wrong")
    messbox("Product Code not found!",0,0,1)
  end if
END FUNCTION

FUNCTION PrintRecs()
  vloadif(dpath|"prodview.vw")
  PrintReport("prodpart.dfr","",1,2,1,1)
'   print report execute "prodpart.dfr" printer detail start 1 end 0 copies 1
  vloadif(dpath|"prodsel2.vw")
  repaint on
  repaint
END FUNCTION

FUNCTION AddRec()                      ' adds new record to the file
local    suppcode bp_ret
'  screen clear box 1 1 sch scw 0 0 no-border
while true
  repaint off
  vloadif(dpath|"prod_ent.vw")
  EnterCode()
  if x = -1
    vloadif(dpath|"prodsel2.vw")
    data goto record record-number 1
    repaint on
    repaint
    exit function
  end if
  vloadif(dpath|"supplier.vws")
  bp_ret = bpopdb("supplier",6,"","[Supplier_Code]","L10","[Name]","L35","[Supplier_Code]",5,20,15,61,"",1)
  if bp_ret = -1
    continue while
  end if
  suppcode = ptstr
  vloadif(dpath|"prod_ent.vw")
  data enter lock
    [Product_Code] = newcode
    [Supplier_Code] = suppcode
  Write-Record
  repaint on
  data update only-one
  vloadif(dpath|"prodsel2.vw")
  exit function
'  repaint on
'  repaint
end while
END FUNCTION

FUNCTION UpdateRec()
  thisrecord=precord
  vloadif(dpath|"prod_ent.vw")
  data goto record record-number thisrecord
  Lock-Record
  reply on nothing to 3015
  repaint on
  data update only-one
  repaint off
  Write-Record
  vloadif(dpath|"prodsel2.vw")
  data goto record record-number thisrecord
  repaint on
  repaint
END FUNCTION

FUNCTION FindName()
  while true
    x = fentrybox("Enter Product Name (first 6 letters)",6,"","")
    if x = 0
      exit while
    end if
  end while
  NameToFind=ptstr
  data find "[Product_MRC]" partial nametofind options "gi"
    if cerror
'      tone("wrong")
      messbox("Product not found!",0,0,1)
    end if
END FUNCTION

FUNCTION LoadFiles()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
'  vloadif(dpath|"prod_ent.vw")
'  vloadif(dpath|"prodsel2.vw")
  vloadif(dpath|"prodview.vw")
'  repaint on
'  repaint
END FUNCTION

FUNCTION ChooseRecord()
  vloadif(dpath|"prodsel2.vw")
  ptval=0
  while ptval <> {Esc}
     ptval = navrecs()
     if ptval = {Enter}
        viewrec()
'     elseif ptval = {A} or ptval = {a}
'        addrec()
     elseif ptval = {C} or ptval = {c}
        FindProd()
     elseif ptval = {N} or ptval = {n}
        FindName()
'     elseif ptval = {U} or ptval = {u}
'        updaterec()
'     elseif ptval = {P} or ptval = {p}
'        printrecs()
     elseif ptval = {Esc}
        ReturnToMenu()
     end if
  end while
END FUNCTION

FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
'  vunloadif("prodsel2.vw")
'  vunloadif("prod_ent.vw")
  vunloadif("prodview.vw")
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION

FUNCTION EnterCode()
while true
  while true
    x = fentrybox("Enter new Product Code",8,"AU\/*6#","")
    if x = -1
      exit function
    end if
  end while
  newcode=ptstr
' search to check that Code is not already used
  data find "[Product_Code]" equal newcode options "gi"
  if cerror
    return newcode
  else
    messbox("Product Code already used ... re-enter",0,0,1)
  end if
end while
END FUNCTION
