'BLDINDEX - maintenance program for rebuilding all product related indices

' ipath = L:\INDEX


external   vloadif() vunloadif() messbox() fentrybox() sch scw cpath dpath
external   bpopdb() progress() fgp bgp scr popuplist() fgi bgi remove()
external   progtag() prpath ipath spath sipath makeidx()

' external   path sch scw progress() fgp bgp remove()
' external   progtag() bgi fgi makeidx() sipath


public     ptstr prodcode suppcode $itemtype dsa psa $ccwidx $ccwcode #prec

global     mess1 ItemtypeIndex() OneItemtype() s1 s2 s3 x AllItemtype() s4 s5 s6
global     CCW_Code() Collections() Suppliers() i suppname PISuppInv()


MAIN
' quiet off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  single-step off

' sipath=\indices\supp_inv\" 'updates to show latest GdsRcvd for checking to invoices
' pipath=\indices\os_purch\" 'updates for Goods to be received
' ipath =\index\"            'index path for sundry idx's
  while true
    s1 = "˙˙˙˙ITEMTYPES"
    s2 = "˙SUPPLIER'S˙RANGE"                   'will show latest Gds Rcvd
    s3 = "˙˙O/S˙PURCHASES"                     'will show latest
    s4 = "˙˙˙˙CCW˙Codes"
    s5 = "˙˙Trade˙Accounts"
    s6 = "Supplier's˙Invoices"                'will show latest Gds Rcvd
    x = popuplist(8,30,15,s1&s2&s3&s4&s5&s6,"",1,0)
    if x = -1
      exit while
    end if
    if ptstr = s1
      ItemtypeIndex()
    elseif ptstr = s2
      execute "one_rnge.rf3" in-memory     'select ONE supplier only
    elseif ptstr = s3                  'OS_PURCHASE
      execute "one_supp.rf3" in-memory     'select ONE supplier only
    elseif ptstr = s4
      CCW_Code()
    elseif ptstr = s5
      Collections()
    elseif ptstr = s6                  'OS_PURCHASE
      PISuppInv()
    end if
  end while

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  smartpoke $_ins 1
'   transfer cpath|"pm_menu.psl" in-memory
END MAIN


FUNCTION ItemtypeIndex()
local s1 s2 s3 s4 s5 s6 s7 s8
  while true
    vloadif(dpath|"products.vws")
    s1 = "˙˙˙˙˙˙˙ALL"
    s2 = "˙˙Stock˙Carpet"
    s3 = "˙Bespoke˙Carpet"
    s4 = "˙Stock˙Ancillary"
    s5 = "Bespoke˙Ancillary"
    s6 = "˙˙˙˙˙Vinyls"
    s7 = "˙˙˙˙˙Tiles"
    s8 = "˙˙˙˙˙Labour"
    x = popuplist(8,33,16,s1&s2&s3&s4&s5&s6&s7&s8,"",1,0)
    if x = -1
      return (-1)
    end if
    if ptstr = s1
      AllItemtype()
    elseif ptstr = s2
      OneItemtype("stckcarp.idx","C","itemtyp1.dfq","Stock Carpet")

    elseif ptstr = s3
      OneItemtype("bespcarp.idx","B","itemtyp1.dfq","Bespoke Carpet")

    elseif ptstr = s4
      OneItemtype("stckancl.idx","A","itemtyp1.dfq"," Stock Ancillaries ")

    elseif ptstr = s5
      $itemtype = "J" or "O"     ' F5 - Bespoke Ancillaries    J or O
      remove(prpath|"bespancl.idx")
      progress(fgp,bgp," Building new index file ",1)
      progtag(fgi,bgi," Bespoke Ancillaries ")
      order change key "[Product_Code]"
      data query execute "itemtyp4.dfq" index "x.idx"
'       order sort now dictionary prpath|"bespancl.idx" fields "[Product_MRC]" ascending
      order sort now dictionary ipath|"bespancl.idx" fields "[Product_MRC]" ascending

    elseif ptstr = s6
      remove(prpath|"vinyl.idx")
      progress(fgp,bgp," Building new index file ",1)
      progtag(fgi,bgi," Vinyls ")
      order change key "[Product_Code]"
      data query execute "itemtyp2.dfq" index "x.idx"
'       order sort now dictionary prpath|"vinyl.idx" fields "[Product_MRC]" ascending
      order sort now dictionary ipath|"vinyl.idx" fields "[Product_MRC]" ascending


    elseif ptstr = s7
      remove(prpath|"Stk_tile.idx")
      progress(fgp,bgp," Building new index file ",1)
      progtag(fgi,bgi," Stock Tiles ")
      order change key "[Product_Code]"
      data query execute "itemtyp3.dfq" index "x.idx"
'       order sort now dictionary prpath|"Stk_tile.idx" fields "[Product_MRC]" ascending
      order sort now dictionary ipath|"Stk_tile.idx" fields "[Product_MRC]" ascending

      remove(prpath|"Bsp_tile.idx")
      progress(fgp,bgp," Building new index file ",1)
      progtag(fgi,bgi," Bespoke Tiles ")
      order change key "[Product_Code]"
      data query execute "itemtyp5.dfq" index "x.idx"
'       order sort now dictionary prpath|"Bsp_tile.idx" fields "[Product_MRC]" ascending
      order sort now dictionary ipath|"Bsp_tile.idx" fields "[Product_MRC]" ascending

    elseif ptstr = s8
      $itemtype = "F"            ' F8 - Labour                 F
      remove(prpath|"labour.idx")
      progress(fgp,bgp," Building new index file ",1)
      progtag(fgi,bgi," Labour ")
      order change key "[Product_Code]"
      data query execute "itemtyp1.dfq" index "x.idx"
'       order sort now dictionary prpath|"labour.idx" fields "[Product_MRC]" ascending
      order sort now dictionary ipath|"labour.idx" fields "[Product_MRC]" ascending

    end if
  end while
END FUNCTION ' ItemtypeIndex()


FUNCTION OneItemtype(idxname,$itype,dquery,$titlename)
  file unload all
  progress(fgp,bgp," Building new index file ",1)
  vloadif(dpath|"products.vws")
  progtag(fgi,bgi,$titlename)
  $itemtype = $itype
  order change key "[Product_Code]"
  data query execute dquery index "x.idx"
'   order sort now dictionary prpath|idxname fields "[Product_MRC]" ascending
  order sort now dictionary ipath|idxname fields "[Product_MRC]" ascending
END FUNCTION ' OneItemtype()


FUNCTION AllItemtype()
  file unload all
  progress(fgp,bgp," Building new index files ",1)
  vloadif(dpath|"products.vws")

  $itemtype = "C"            ' F2 - Stock Carpet           C
  remove(prpath|"stckcarp.idx")
  progtag(fgi,bgi," Stock Carpet ")
  order change key "[Product_Code]"
  data query execute "itemtyp1.dfq" index "x.idx"
'   order sort now dictionary prpath|"stckcarp.idx" fields "[Product_MRC]" ascending
  order sort now dictionary ipath|"stckcarp.idx" fields "[Product_MRC]" ascending

  $itemtype = "B"            ' F3 - Bespoke Carpet         B
  remove(prpath|"bespcarp.idx")
  progtag(fgi,bgi," Bespoke Carpet ")
  order change key "[Product_Code]"
  data query execute "itemtyp1.dfq" index "x.idx"
'   order sort now dictionary prpath|"bespcarp.idx" fields "[Product_MRC]" ascending
  order sort now dictionary ipath|"bespcarp.idx" fields "[Product_MRC]" ascending

  $itemtype = "A"            ' F4 - Stock Ancillaries      A
  remove(prpath|"stckancl.idx")
  progtag(fgi,bgi," Stock Ancillaries ")
  order change key "[Product_Code]"
  data query execute "itemtyp1.dfq" index "x.idx"
'   order sort now dictionary prpath|"stckancl.idx" fields "[Product_MRC]" ascending
  order sort now dictionary ipath|"stckancl.idx" fields "[Product_MRC]" ascending

  $itemtype = "J" or "O"     ' F5 - Bespoke Ancillaries    J or O
  remove(prpath|"bespancl.idx")
  progtag(fgi,bgi," Bespoke Ancillaries ")
  order change key "[Product_Code]"
  data query execute "itemtyp4.dfq" index "x.idx"
'   order sort now dictionary prpath|"bespancl.idx" fields "[Product_MRC]" ascending
  order sort now dictionary ipath|"bespancl.idx" fields "[Product_MRC]" ascending

  $itemtype = "F"            ' F8 - Labour                 F
  remove(prpath|"labour.idx")
  progtag(fgi,bgi," Labour ")
  order change key "[Product_Code]"
  data query execute "itemtyp1.dfq" index "x.idx"
'   order sort now dictionary prpath|"labour.idx" fields "[Product_MRC]" ascending
  order sort now dictionary ipath|"labour.idx" fields "[Product_MRC]" ascending

'   $itemtype = "V" or "W"     ' F6 - Vinyls                 V & W
  remove(prpath|"vinyl.idx")
  progtag(fgi,bgi," Vinyls ")
  order change key "[Product_Code]"
  data query execute "itemtyp2.dfq" index "x.idx"
'   order sort now dictionary prpath|"vinyl.idx" fields "[Product_MRC]" ascending
  order sort now dictionary ipath|"vinyl.idx" fields "[Product_MRC]" ascending

  remove(prpath|"Stk_tile.idx")
  progtag(fgi,bgi," Stock Tiles ")
  order change key "[Product_Code]"
  data query execute "itemtyp3.dfq" index "x.idx"
'   order sort now dictionary prpath|"Stk_tile.idx" fields "[Product_MRC]" ascending
  order sort now dictionary ipath|"Stk_tile.idx" fields "[Product_MRC]" ascending

  remove(prpath|"Bsp_tile.idx")
  progtag(fgi,bgi," Bespoke Tiles ")
  order change key "[Product_Code]"
  data query execute "itemtyp5.dfq" index "x.idx"
  order sort now dictionary ipath|"Bsp_tile.idx" fields "[Product_MRC]" ascending

END FUNCTION ' AllItemtype()


FUNCTION CCW_Code()
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ create new indices for CCW codes                                   ³
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  progress(fgp,bgp," Deleting old indices on"&spath|"˙",0)
  file unload all
  remove(spath|"*.idx")                'delete old indices
  screen shortrestore psa

  progress(fgp,bgp," Building new index files ",1)

  vloadif(dpath|"colours.vws")
  data goto record first
  while precord <= precords
    $ccwcode = [CCW_Code]
' if $ccwcode="000283"
' message "$ccwcode is:"&str($ccwcode)
' repaint on
' repaint
' single-step on
' end if
    $ccwidx = $ccwcode|".idx"
    if file(spath|$ccwidx) = 1
      data goto record next
      continue while         ' if index for this Colours.Code exists - goto next record
    end if

'     progtag(fgi,bgi,spath|$ccwidx)
    progtag(fgi,bgi,"Rebuilding"&str(precord)&"of"&str(precords))
    #prec = precord
    vloadif(dpath|"stk_carp.vws")
    order change key "[CCW_Code]"
    data query execute "ccw_indx.dfq" index spath|$ccwidx
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [CCW_Code] = $ccwcode and [Active] = "Y"
'   and
'   not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
    if cerror
' message "ERROR!!"
    end if

    vloadif(dpath|"colours.vws")
    order change physical
    data goto record record-number #prec
    if #prec = precords
      exit while
    end if
    data goto record next
  end while
END FUNCTION 'CCW_Code()


FUNCTION Collections()
  progress(15,10," Please wait ... finding A/c's for Collections ",0)
'   progtag(fgi,bgi,"Creating index of Aged Drs ")
  vloadif(dpath|"customer.vws")
  data query execute "collectn.dfq" INDEX "collect1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³  [Credit_Status]="N"
'    and
'    not (deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No accounts for Collection status ",0,0,1)
    return (1)
  end if
  order sort now dictionary ipath|"collectn.idx" fields "[Customer_Name]" ascending
  file unload all
END FUNCTION ' Collections()


FUNCTION PISuppInv()
single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  progress(fgp,bgp," Deleting old Suppliers' indices on"&sipath|"˙",0)
  file unload all
  remove(sipath|"*.*")
  vloadif(dpath|"supplier.vws")
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  for i = 1 to precords
    suppcode = [Supplier_Code]
    suppname = [Name]
    if not (deleted)
      Suppliers()
    end if
    vloadif(dpath|"supplier.vws")
    data goto record next
  end for
  file unload all
END FUNCTION ' PISuppInv()


FUNCTION Suppliers()
  progress(fgp,bgp," ˙˙˙˙Building new index files for Supplier's invoices ˙˙˙˙",1)
  progtag(fgi,bgi," Re-indexing"&suppname&"("|suppcode|") ")
if suppcode="CUL001"
' repaint on
' repaint
' single-step on
end if
  vloadif(dpath|"suppinv1.vw")
  order change key "[Supplier_Code]"
  data query execute "supp_inv.dfq" index "suppinv1.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Supplier_Code] = suppcode and len([Invoice_Nr])=0 and not(deleted)
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
'   if cerror
'       messbox(" No Goods Received to check for"&suppname,0,0,1)
'     screen clear box 1 1 sch scw 0 0 no-border
'     return (1)
'   end if
'   order sort now dictionary sipath|"si"|suppcode fields "[Order_Nr]" ascending

  if cerror
    x=makeidx("gds_rcvd",sipath|"si"|suppcode|".idx","0",5)
  else
    order sort now dictionary sipath|"si"|suppcode|".idx" fields "[Order_Nr]" ascending
'     order sort now dictionary prpath|"PR"|suppcode|".idx" fields "[Product_MRC]" ascending
  end if
END FUNCTION ' Suppliers()


