'ONE_RNGE - creates permanent index for product ranges for one Supplier

external   dpath vloadif() prpath sch scw progress() fgp bgp remove()
external   progtag() bgi fgi makeidx() $drive bpopdb() pipath messbox() cpath
external   messboxwait() popuplist()

public     ptstr suppcode

global     x i suppname supp_idx FindSupplier() y1 #bline NewIndexSupplier()


MAIN
single-step off
  while true
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    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"
        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
      NewIndexSupplier()
    end if
  end while

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all

END MAIN


FUNCTION NewIndexSupplier()
  progress(fgp,bgp," ˙˙˙˙Building new index files for Supplier's price lists ˙˙˙˙",1)
  progtag(fgi,bgi," Re-indexing"&suppname|" ")
  vloadif(dpath|"products.vws")
  order change key "[Supplier_Code]"
  data query execute "supprnge.dfq" index "x.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [Supplier_Code] = suppcode                                         ³
'   and
'   [Item_Type] <> "C"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    x=makeidx("products",prpath|"PR"|suppcode|".idx","0",5)
  else
    order sort now dictionary prpath|"PR"|suppcode|".idx" fields "[Product_MRC]" ascending
  end if
END FUNCTION ' NewIndexSupplier()


FUNCTION FindSupplier()
  vloadif(dpath|"supplier.vws")
  while true
    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
      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|"P_"|suppcode|".idx"
END FUNCTION 'FindSupplier()
