'ONE_SUPP - rebuild index for o/s PURCHORD records for selected supplier
'           to be used when GDS_RCV1 program has to show req'ns entered
'           since the previous automatic rebuild

external   dpath vloadif() prpath sch scw progress() fgp bgp remove()
external   progtag() bgi fgi makeidx() bpopdb() pipath messbox() cpath
external   messboxwait() Background() popuplist()

public     ptstr suppcode dsa

global     x i suppname FindSupplier() ReturnToMenu() y1 #bline Suppliers()
global     y2 y3 supp_idx #rec


MAIN
single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  #rec = 1

  while true
    vloadif(dpath|"supplier.vws")
    order change physical
    order sort now dictionary "suppname" fields "[Name]" ascending
    repaint off
    x = popuplist(8,33,13,"ALL˙suppliers Chosen˙supplier","",1,0)
    if x = -1
      exit while
    end if
    if ptstr = "ALL˙suppliers"
      data goto record first
      for i = 1 to records
        suppcode = [Supplier_Code]
        suppname = [Name]
        supp_idx = pipath|"P_"|suppcode|".idx"
        Suppliers()
'         NewIndexSupplier()
        vloadif(dpath|"supplier.vws")
        data goto record next
      end for
    else
      x = FindSupplier()
      if x = -1
        continue while
      end if
'       screen clear box 1 1 sch scw 0 0 no-border
'       repaint off
'       file unload all
'       if file(supp_idx) = 1
'         x = remove(supp_idx)
'         if x = -1
'           messboxwait(" Index for"&suppname&"is in use - cannot rebuild ",0,0,1)
'           continue while
'         end if
'       end if
      Suppliers()
    end if
  end while

  ReturnToMenu()

END MAIN


FUNCTION FindSupplier()
  vloadif(dpath|"supplier.vws")
  while true
    data goto record record-number #rec
    y1 = format(" Choose Supplier and press {Enter} ","M38")
    screen print 7 21 15 1 y1
    screen print 20 21 15 1 (format(" {Enter} views orders - {Esc} exits ","M38"))
    x = bpopdb("supplier",6,"","[Name]","l35","[Supplier_Code]","L6","[Supplier_Code]",8,21,19,58,"",0)
    if x = 0
      #rec=record+1
      exit while
    elseif x = -1
      screen clear box 1 1 sch scw 0 0 no-border
      return (-1)
    end if
  end while
  suppcode = ptstr
  suppname = [Name]
  supp_idx = pipath|"SI"|suppcode|".idx"
  x=remove(supp_idx)
  Background()
  file unload all
END FUNCTION 'FindSupplier()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
END FUNCTION ' ReturnToMenu()


FUNCTION Suppliers()
  progress(fgp,bgp," ˙˙˙˙Building new index files for Supplier's invoices ˙˙˙˙",1)
  progtag(fgi,bgi," Re-indexing"&suppname&"("|suppcode|") ")
  vloadif(dpath|"os_purch.vw")
  order change key "[Supplier_Code]"
  data query execute "ospurchB.dfq" index "ospurch1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Supplier_Code] = suppcode and len([Invoice_Nr])=0 and not(deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    x=makeidx("gds_rcvd",pipath|"P_"|suppcode|".idx","0",5)
  else
    order sort now dictionary pipath|"P_"|suppcode|".idx" fields "[Order_Nr]" ascending
  end if
END FUNCTION ' Suppliers()
