'SHUTWHSE - wareh'se shutdown routine - return to BOSS menu if allowed

external   cpath sch scw $menu progress() remove() strcount() dpath vloadif()
external   messboxwait() messbox() chkstr() vunloadif() SetPRN_JNR() jobs[6]
external   arytostr() userid shoplist

public $shop ptstr $br ptval ptary[1]

global x SendFax() $fax FaxCheck() i $jobstr

MAIN

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
'   $branches = "F S P R T W"           ' list of branches to fax to
  if file("bespfax.idx") = 1
    vloadif(dpath|"fax_rcvd.vw")
    order change index "bespfax.idx"
    if records > 0
      messboxwait(" Faxes to send re Goods Received - this will take a few minutes ",0,0,1)
      SendFax()
      vunloadif("fax_rcvd.vw")
'       remove("*.idx")
    end if
  end if


  if $menu ! "boss"
    screen clear box 1 1 sch scw 0 0 no-border
    execute "bossmenu.pop" in-memory
    transfer cpath|"pm_menu.psl" in-memory
  elseif $menu ! "offc"
    screen clear box 1 1 sch scw 0 0 no-border
    execute "offcmenu.pop" in-memory
    transfer cpath|"pm_menu.psl" in-memory
  else
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    progress(15,10," Deleting all local indices ",0)

    redimension ptary[6]
    for i = 1 to 6
      ptary[i] = jobs[i]
    end for
    x = arytostr(6)
    $jobstr = ptstr                      'message "Job string to unload is:"&str($jobstr)

    if $jobstr <> 0
      fopen dpath|userid|".jnr" as 1
      fwrite 1 from $jobstr
      fclose 1
    end if

    file unload all
    SetPRN_JNR()
    remove("*.idx")
'     execute "logout" in-memory
    quit quit
  end if

END MAIN


FUNCTION SendFax()
local  $faxnr $comport #nrrecip i j

  x = strcount(shoplist)
  #nrrecip = ptval

  fopen dpath|"beerport.dat" as 1
  fread 1 into $comport                ' message "$comport) is:"&str($comport)
  fclose 1

  vloadif(dpath|"fax_rcvd.vw")
  for i = 1 to #nrrecip
    order change index "bespfax.idx"
    remove("shop.idx")
    $br = group(shoplist,i)          ' message "$br) is:"&str($br)
    $shop = case $br ("S","Sheen")("W","DavidG")("T","DavidG")("P","Putney")("F","Fulham")("R","Raynes")
    fopen dpath|$br|"_fax.dat" as 1
    fread 1 into $faxnr                ' message "$faxnr) is:"&str($faxnr)
    fclose 1

    data query execute "faxshops.dfq" index "shop.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ QUERY is:  [Branch] = $br                                          ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      $fax = "C:\ssfaxer.exe"&$comport&$faxnr&"C:\programs\nobespfx.txt"
    else
      print report execute "besp_fax.dfr" disk "bespfax.txt" detail start 1 end 0 copies 1
      $fax = "C:\ssfaxer.exe"&$comport&$faxnr&"C:\programs\bespfax.txt > C:\programs\faxreslt.txt"
    end if

    messbox(" Send fax to"&$shop&"at"&$faxnr|"? (y/n) ",1,0,1)
    if ptstr == "y"
      for j = 1 to 5
        messbox(" Sending fax to"&$shop&"- try nr:"&str(j),0,1,1)
        tools os $fax
        if FaxCheck() = 1
' message "SENT!!"
          exit for
        else
          wait 15
          continue for
' message "FAILED"
        end if
      end for
    end if

    remove("bespfax.txt")
    remove("faxreslt.txt")

  end for
END FUNCTION ' SendFax()


FUNCTION FaxCheck()
local   $line  $oldline
  fopen "C:\programs\faxreslt.txt" as 1
  while eof(1)=0
    $oldline = $line
    fread 1 into $line                   '   message "$line) is:"&str($line)
  end while
  fclose 1                               ' message "after while - $oldline is:"&str($oldline)
  if chkstr("Completed",$oldline) = 0
    return (1)
  end if
  return (0)
END FUNCTION ' FaxCheck()

