'DRSCHASE - controls "chasing" letters sent to Aged Drs
'230407 - new version; Crystal Report for 1st letter; RED statement instead
'         of 2nd letter; list only (no letter) for Higgins

external   dpath sch scw vloadif() ipath fgp bgp cpath messboxwait() adpath
external   messbox() increment() remove() makeidx() vunloadif() PrintReport()
external   colpopup() addidxrec() progress() getidxrecs() delidxrec() userid
external   entryline() bgi bgs fgi fgs $actport
external   ChooseBranch() progtag()
external   X_path _SWIP_Crystal() Xreppath

public     ptstr ptval jobnr deladdr1 ftgdate addr1 addr2 custcity custcode
public     #baldue custname $title $phone $br_name $branch
public     psa dsa plist[1,1]

global     x RecsScroll() NavMess_A() y1 y2 y3 ViewNotes() ChooseOrder() p $ord
global     ReturnToMenu() Action() Titles() SendLetter()
'  LegalFile()
global     PrintLetter() drs_chasnr p1 p2 p3 p4 p5 p6 Approve() SelectDrs()
global     ReplaceHardSpace() $branchinits UpdateCustomer() idxname
global     ResetIndices() #rec abbrv_name #tot_drs #tot_fitted
global     Update() popuplist()  uistrcnt() EnterComment() RedStatement()
global     recs mr sym blen l c c2 r2 dc lc sc pl i pc rec drows k refresh()
global     udelstr() pg tr b1 PrintOrder() Select() EnterNote() #ord_val


MAIN
single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  error off

  while true
    window close
    if cerror
      exit while
    end if
  end while

  p2 = ""               ' p2 = title at top of choice popup ("LABEL")
  p3 = 1                ' p3 = printer to be used (1=HPIII_QC; 2=GEN_EPSN etc)
'   p4 = 2                ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
  p4 = 1                ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
  p5 = 1                ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1                ' p6 = nr of copies

  x = popuplist(10,32,13,"Select˙Orders Print˙Letters","Debtors letters",1,0)
  screen shortrestore dsa
  if ptstr = "Select˙Orders"
    progress(15,10," Preparing files ",0)
    ResetIndices()
    SelectDrs()
    ChooseOrder()
  else
    while true
      $br_name = ChooseBranch(8,49,1)    ' message "$br_name is:"&str($br_name)
      if $br_name = -1
        exit while
      end if
      $branch = left($br_name,1)
      if file(ipath|$branch|"_drsact.idx") = 0
        messboxwait(" No debtors listed for action for"&$br_name|" ",0,0,1)
        continue while
      end if
      x = PrintOrder()
      if x = 1
        continue while
      end if
    end while
  end if

  ReturnToMenu()

END MAIN


FUNCTION RecsScroll()
local x bot psmode
  screen save scrheight 1 scrheight scrwidth bot
  smartpeek $_spndmes psmode
  if psmode = 1
    smartpoke $_spndmes 0
  end if
  while TRUE
    NavMess_A()
    x = inchar
    if x = {Down}
      data goto record next
      NavMess_A()

    elseif x = {Up}
      data goto record previous
      NavMess_A()

    elseif x = {PgDn}
      data goto page next
      NavMess_A()

    elseif x = {PgUp}
      data goto page previous
      NavMess_A()

    elseif x = {^End}
      data goto record last
      NavMess_A()

    elseif x = {^Home}
      data goto record first
      NavMess_A()

    elseif x = {Home}
      suspendone
      keys Home,F8
      screen shortrestore bot
      NavMess_A()

    elseif x = {End}
      suspendone
      keys End,F8
      screen shortrestore bot
      NavMess_A()

    else
      exit while
    end if
  end while
  if psmode = 1
    smartpoke $_spndmes 1
  end if
  return (x)
END FUNCTION ' RecsScroll()


FUNCTION NavMess_A()
local ftgdate ordstat mess1 col1 mess2 bal_os
  col1 = 12
  ftgdate = [Fitting_Date]             'message "ftgdate) is:"&date2(ftgdate)
'   invdate = [Invoice_Date]
  ordstat = [Order_Status]             ' message "ordstat) is:"&str(ordstat)
  jobnr   = [Job_Nr]                   ' message "jobnr) is:"&str(jobnr)
  bal_os = currency([Balance_Due])
  #ord_val = currency([Invoice_Total])
' message "#ord_val is:"&str(#ord_val)
' mess1 = format([Job_Nr]&"- Ordered:"&format(@if([Date_Of_Order]=blank,"Unknown!",date2([Date_Of_Order])),"L8")&"- Fitted:"&format(@if([Fitting_Date]=blank,"Unknown!",date2([Fitting_Date])),"L8")&"- Order value"&right("        "|#ord_val,10),"M78")
  mess1=format([Job_Nr]&"- Invoiced:"&format(date2([Invoice_Date]),"L10")&"- Inv Nr:"&format(str([Inv_Nr]),"L6")&"- Invoice value"&right("        "|#ord_val,10),"M78")
'   mess2   =  format("Balance o/s is:"&currency([Balance_Due]),"M71")
  screen print 17 2 15 col1 mess1
'   screen print 18 6 15 col1 mess2
END FUNCTION   'NavMess_A()


FUNCTION ViewNotes()
END FUNCTION 'ViewNotes()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  error off
  while true
    window close
    if cerror
      exit while
    end if
  end while
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION Approve()
local   t1 t2 t3 t4 t5 t6 t7 y7 chkbal
'   screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  while true
    y7 = format("Ensure Headed paper is loaded","M43")
    screen print 9 20 fgp bgp y7
    t1 = ReplaceHardSpace($title)
    t2 = ReplaceHardSpace(custname)
    t3 = ReplaceHardSpace(addr1)
    t4 = ReplaceHardSpace(addr2)
    t5 = ReplaceHardSpace(custcity)
    t6 = ReplaceHardSpace(custcode)
    x = popuplist(10,20,17,t1&t2&t3&t4&t5&t6,chr(24)&chr(25)&"{Enter} amends - {F10} to print",1,0)
    if x = -1
      messbox(" Abandon this letter? (y/n) ",1,0,1)
      if ptstr == "y"
        return (-1)
      end if
    elseif x = 1
      return (0)
    end if
    if ptstr = t1
      $title = Update($title)

    elseif ptstr = t2
      custname = Update(custname)

    elseif ptstr = t3
      addr1 = Update(addr1)

    elseif ptstr = t4
      addr2 = Update(addr2)

    elseif ptstr = t5
      custcity = Update(custcity)

    elseif ptstr = t6
      custcode = Update(custcode)

'     elseif ptstr == t7
'       screen clear box 1 1 sch scw 0 0 no-border
'       repaint off
'       return (0)
    end if
  end while
END FUNCTION ' Approve()


FUNCTION Update(e)
  screen shortrestore dsa
  while true
    x = entryline(" Correct as necessary ",35,"",e,15,20,43)
    if len(ptstr)=0
      continue while
    end if
    return (ptstr)
  end while
END FUNCTION ' Update(e)


FUNCTION ReplaceHardSpace(str1)
local j r m bw l_last #addn
  bw = 39                              ' boxwidth
'   bw = 43                              ' boxwidth
  m = ""
  for j = 1 to len(str1)
    r = mid(str1,j,1)
    if r = " "
      r = "˙"                          ' replace hard space
    end if
    m = m|r
  end for

  if len(m) < bw
    #addn = bw-len(m)
  else
    #addn = mod(len(m),bw)
  end if
  m = m|repeat("˙",#addn)
  return (m)
END FUNCTION ' ReplaceHardSpace()


function popuplist(r1,c1,br,list,msg,num,mnu)
local t hml hm cnum mscn pad padc ret

if exact(trim(list),NULL)=FALSE
     recs = uistrcnt(list)
     if recs = 0
          return (-3)
     end if
else
     return (-2)
end if

redimension plist[recs,3]
smartpeek $_l1 hml

if br-r1<1
     return (-4)
elseif br+1 > scrheight
     mr=scrheight-1
     msg = ""
else
     mr=br
end if
if br >= hml
     mnu = 0
end if

screen save hml 1 hml scrwidth mscn
if recs > scrheight
     if mnu = 1
          screen clear box hml 1 hml scrwidth 0 0 no-border
          screen print hml 1 bgi bgs "Building list..."
     end if
end if
ptstr=NULL
if mnu = 1
     hm =  case num (0,"Enter = select/unselect   F10 = done   Esc = exit") \
                    (1,"Enter = select   Esc = exit      (select: 1 item)") \
                    else "Enter = select/unselect   F10 = done   Esc = exit  " & \
                         "   (select up to:" & str(num) & "items)"
else
     hm = NULL
end if
sym = spsymmap(28)
cnum=0
blen=0
l=blen
for c=1 to recs
     plist[c,2]=group(list,c)
     l=len(plist[c,2])
     plist[c,1]=0
     if l>blen
        blen=l
     end if
end for
c2=c1+blen+2
r2=r1+recs
if r2>mr
     r2=mr
end if
dc=(c2-c1)
lc=c1+1
pad = case num (1,1) else 2
sc=c1+pad-1
pl=(r2-r1)
padc = repeat(chr(32),pad)
for i = 1 to recs
     pc = 1
     plist[i,2]=padc|format(plist[i,2],"l",dc-1)
     plist[i,3] = i
     if i = pl
          pc=pc+1
     end if
end for
if recs > scrheight
    screen shortrestore mscn
end if
screen save r1 c1 r2+2 c2+pad psa
screen clear box r1 c1 r2+1 c2+pad fgp bgp
pc=1
for c=1 to pl
     screen print c+r1 lc fgp bgp plist[c,2]
  end for
  if msg > null
    screen print r2+2 c1 fgi bgi str(format(left(msg,dc+pad+1),"M"|str(dc+pad+1)))
  end if
  if mnu = 1
    screen clear box hml 1 hml scrwidth fgs bgs no-border
    screen print hml 1 fgs bgs (format(hm,"L"|str(scrwidth)))
  end if

  c=1
  rec=1
  screen print r1+c lc fgi bgi plist[rec,2]
  drows = pl

  while TRUE
    k=inchar
    screen print r1+c lc fgp bgp plist[rec,2]
    if plist[rec,1]=1
      screen print r1+c sc fgp bgp sym
    end if
    if k={Down}
      if rec=recs
        if recs<=pl
          rec=1
          c=1
        else
          beep
        end if
      else
        if c = pl
          screen scroll up r1+1 lc r2 (c2+case num (1,0) else 1) fgp bgp 1
        end if
        c= case c (pl,c) else (c+1)
        rec=rec+1
      end if
    elseif k={Up}
      if rec=1
        if recs <= pl
          rec = recs
          c = pl
               else
                    beep
               end if
          else
               if c = 1
                    screen scroll down r1+1 lc r2 (c2+case num (1,0) else 1) fgp bgp 1
               end if
               c= case c (1,c) else (c-1)
               rec=rec-1
          end if
     elseif k={Home}
          if c>1
               if rec =(rec-c)+1
                    rec = 1
               else
                    rec =(rec-c)+1
               end if
               c=1
          else
               rec=1
               c=1
          end if
     elseif k={^Home}
          if rec = c
               rec = 1
               c=1
          else
               rec = 1
               c=1
               refresh(c,r1,c1,r2,c2,pad)
          end if
     elseif k={End}
          if rec < recs and c < pl
               if drows < pl
                    rec = recs-pl+1
                    refresh(c,r1,c1,r2,c2,pad)
                    rec = recs
                    c = pl
               else
                    if rec+(pl-c) < recs
                         rec = rec+(pl-c)
                         c = pl
                    else
                         rec = recs
                         c = pl
                    end if
               end if
          end if
     elseif k={^End}
          rec = recs-pl+1
          c = 1
          refresh(c,r1,c1,r2,c2,pad)
          c = pl
          rec = recs
     elseif k={PgDn}
          if rec = recs and c = pl
               beep
          elseif c <= pl
               if rec = recs or rec+pl >= recs
                    rec = recs-pl+1
                    c = 1
                    refresh(c,r1,c1,r2,c2,pad)
                    c = pl
                    rec = recs
               else
                    rec = rec+pl
                    refresh(c,r1,c1,r2,c2,pad)
               end if
          end if
     elseif k={PgUp}
          if rec = 1 and c = 1
               beep
          else
               if recs > pl
                    if (rec-pl)-c <= 1
                         c = rec-pl
                         if c < 1
                              c = 1
                         end if
                         rec = 1
                         refresh(1,r1,c1,r2,c2,pad)
                         rec = c
                    else
                         rec=(rec-pl)
                         refresh(1,r1,c1,r2,c2,pad)
                    end if
               else
                    if rec > 1
                         rec=1
                         c=1
                    end if
               end if
          end if
     elseif k={Enter}
          screen print r1+c lc fgi bgi plist[rec,2]
          if num = 1
                    ret=trim(plist[rec,2])
                    exit while
          end if
          if plist[rec,1] = 1
               if udelstr(trim(plist[rec,2]),ret) = 0
                    ret = ptstr
               end if
               plist[rec,1] = 0
               cnum=cnum-1
          else
               if cnum = num and not(num=0)
                    beep
               else
                    ret=trim(ret&plist[rec,2])
                    plist[rec,1] = 1
                    cnum=cnum+1
               end if
          end if
          if rec < recs
               smartpoke $_key {Down}
          end if
     elseif k={Esc}
               ret=null
               exit while
     elseif k={F10}
'          for c=recs to 1 step -1
'               if plist[c,1]=1
'                    ret=ret & trim(plist[c,2])
'               end if
'          end for
'           exit while
      return (1)
    end if
  if k<> {Enter}
    screen print r1+c lc fgi bgi plist[rec,2]
  end if
    if plist[rec,1]=1
      screen print r1+c sc fgi bgi sym
    end if
  end while
  screen save r1 c1 r2+2 c2+1+pad dsa
  screen shortrestore mscn
  screen shortrestore psa
  clear c k dc lc sc recs c2 r2 l blen pl mr pc sym pg i rec tr drows
  redimension  plist[1,3]
  if trim(ret) = NULL
    ptstr = NULL
    if k={F10}
      return (0)
    else
      return (-1)
    end if
  else
    ptstr = trim(ret)
    return (0)
  end if
end function  'popuplist()


function uistrcnt(sl)
local i s lo hi c
s=20
while exact(group(sl,s),NULL)=FALSE
     s=s+20
end while
hi = s
lo = 1
while lo <= hi
     i = int((lo+hi)/2)
     c = group(sl,i)
     if c = NULL
          hi = i-1
     else
          lo = i+1
     end if
end while
while (exact(group(sl,i),NULL)=TRUE and i>0)
     i=i-1
end while
return (i)
end function  'uistrcnt()


function udelstr(s,sl)
local t i n f
  f=0
  i=0
  n=NULL
  ptstr = NULL
  while TRUE
    i=i+1
    t = group(sl,i)
    if exact(t,NULL)=TRUE
      exit while
    elseif t = s
      f=1
    else
      n=n&t
    end if
  end while
  if f = 1
    ptstr = trim(n)
    return (0)
  end if
  ptstr = sl
  return (-1)
end function  'udelstr()


function refresh(z,r1,c1,r2,c2,pad)
local x t
  screen clear box r1 c1 r2+1 c2+pad fgp bgp
  drows = 0
  for x=0 to pl-1
  t = rec-z+x+1
    if t > recs
      exit for
    else
      if t > 0
        screen print x+1+r1 lc fgp bgp plist[t,2]
        drows=drows+1
        if plist[t,1]=1
          screen print x+1+r1 sc fgp bgp sym
        end if
      end if
    end if
  end for
end function  'refresh()


FUNCTION SelectDrs()
  progress(15,10," Please wait ... finding balances ",1)
  progtag(fgi,bgi,"Creating index of Aged Drs ")
  vloadif(dpath|"cust_ord.vws")
  data query execute "drs_full.dfq" INDEX "first.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³  [Balance_Due]>.01 or [Balance_Due]<.01                            ³
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No outstanding debtors to list ",0,0,1)
    return (1)
  end if

  data query execute "ageddrs1.dfq" index "third.idx"   ' choose o/s balances
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   [Branch] <> "C"
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
  if cerror
    messbox(" No outstanding debtors at all!! ",0,0,1)
    return (1)
  end if
  progress(15,10," Please wait ... sorting balances ",0)
  order sort now dictionary ipath|"aged_drs.idx" fields "[Branch;Job_Nr]" ascending
END FUNCTION ' SelectDrs()


FUNCTION EnterNote()
' look for existing file
END FUNCTION ' EnterNote()


FUNCTION ChooseOrder()
  while true
    vloadif(dpath|"drschas1.vw")
    order change index ipath|"aged_drs.idx"
    window split horizontal 18
    data goto window 2
    vloadif(dpath|"drschas2.vw")
    window split vertical 68
    data goto window 3
    vloadif(dpath|"ordnotes.vw")
    data goto window 2
    window link "[Job_Nr]" "ordnotes.vw" "[JobNr]"
    data goto window 1
    window link "[Job_Nr]" "drschas2.vw" "[Job_Nr]"

    repaint on
    repaint
    ptval=0
    Titles("c")
    while true
      ptval = RecsScroll()
      if ptval = {s}
        repaint off
        if [Balance_Due] < 0
          messboxwait(" We owe them money!! ",0,0,1)
          repaint on
          repaint
          Titles("c")
        else
          custname = [CustOrd_Name]    'message "[Marker] is:"&str([Marker])
          if [Marker] = "Y"
            Select("")
          else
            Select("Y")
          end if
          repaint on
          repaint
          Titles("c")
        end if

      elseif ptval = {F10}
        progress(15,10," Preparing report ... ",0)
        repaint off
        window close
        window close
        beep off
        vloadif(dpath|"drschas1.vw")
' order change index ipath|"aged_drs.idx"
        $branchinits = "F P R S T W"
        for k = 1 to 6
          $branch = group($branchinits,k) 'message "$branch is:"&str($branch)
          $br_name = case $branch ("F","Fulham")("P","Putney")("R","Raynes")("S","Sheen")\
          ("T","Trade")("W","Warehouse") else ""
          idxname = ipath|$branch|"_drsact.idx" 'message "idxname is:"&str(idxname)
          data query execute "drs_act.dfq" index "temp.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
' ³ [Marker] = "Y" and
' ³ [Branch] = $branch
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
          if cerror
            order change physical
            continue for
          end if
          order sort now dictionary idxname fields "[Date_Of_Order;Job_Nr]" ascending

          vloadif(dpath|"drschas3.vw")
          order change index idxname
          p1 = "drsnotif.dfr"
          if records > 0
message "Printing at L705"
            PrintReport(p1,p2,p3,p4,p5,p6)
          end if
          vloadif(dpath|"drschas1.vw")
          order change physical
        end for
        return (0)

      elseif ptval = {Esc}
        return (0)

      end if
    end while
  end while
END FUNCTION ' ChooseOrder()


FUNCTION  PrintOrder()
  while true
    vloadif(dpath|"drschas1.vw")
    order change index ipath|$branch|"_drsact.idx"
    window split horizontal 18
    data goto window 2
    vloadif(dpath|"drschas2.vw")
    data goto window 1
    window link "[Job_Nr]" "drschas2.vw" "[Job_Nr]"

    repaint on
    repaint
    ptval=0
    Titles("p")
    while true
      ptval = RecsScroll()

      if ptval = {a}
        repaint off
        if [Balance_Due] < 0
          messboxwait(" We owe them money!! ",0,0,1)
          repaint on
          repaint
          Titles("p")
        else
          custname = [CustOrd_Name]
          Action()
          if records = 0
            screen clear box 1 1 sch scw 0 0 no-border
            repaint off
            return (1)
          end if
          Titles("p")
        end if

      elseif ptval = {n}
        repaint off
        if [Balance_Due] < 0
          messboxwait(" We owe them money!! ",0,0,1)
          repaint on
          repaint
          Titles("p")
        else
          custname = [CustOrd_Name]
          EnterNote()
          if records = 0
            screen clear box 1 1 sch scw 0 0 no-border
            repaint off
            return (1)
          end if
          Titles("p")
        end if

      elseif ptval = {F10}
        screen clear box 1 1 sch scw 0 0 no-border
        repaint off
        messboxwait(" Must update note files for remainder ",0,0,1)
        return (1)
      end if
    end while
  end while
END FUNCTION ' PrintOrder()


FUNCTION Select(sel)
' add to index of orders for each shop 'message "sel is:"&str(sel)
  if sel = "Y"
    lock-record
      [Marker] = "Y"
    write-record
  else
    lock-record
      [Marker] = ""
    write-record
  end if
  data goto record next
END FUNCTION 'Select()


FUNCTION Titles(t)
'   y1 = format(" Total Debtors are"&currency(#tot_drs)&"(fitted"&currency(#tot_fitted)|")","M78")
  y2 = format(" Name & Delivery Address                                            Bal o/s","L78")
  if t = "p"
    y3 = format(" {A}ction - {F10} to finish ","M78")
  else
    y3 = format(" {S}elect/deselect - {F10} to finish - {Esc} to abandon ","M78")
  end if
'   screen print 1 2 fgp bgp y1
  screen print 2 2 fgp bgp y2
  screen print 16 2 fgp bgp y3
END FUNCTION ' Titles()


FUNCTION UpdateCustomer()
  data goto window 2
  window zoom
  abbrv_name = proper(left(custname,7))
  if len(custname) = 0
    messboxwait(" Customer's name has been omitted - pls contact David @ HO ",0,0,1)
  end if
  if len(abbrv_name) = 0
    messboxwait(" Customer's abbrv'd name has been omitted - pls contact David @ HO ",0,0,1)
  end if
  lock-record
    [Title]         = $title
    [Customer_Name] = custname
    [CustOrd_Name]  = custname
    [Abbrv_Name]    = abbrv_name
    [Address_1]     = addr1
    [Address_2]     = addr2
    [City/Town]     = custcity
    [Postcode]      = custcode
  write-record
  window zoom
  data goto window 1
END FUNCTION ' UpdateCustomer()


FUNCTION ResetIndices()
local bran
  vloadif(dpath|"branches.vws")
  for i = 1 to precords
    $branch = [Br_Init]
    idxname = ipath|$branch|"_drsact.idx"  'message "idxname is:"&str(idxname)
    if fileinfo(idxname,0) <> 0
      x = remove(idxname)                'message "x) is:"&str(x)
    end if
    data goto record next
  end for
  vunloadif("branches.vws")

  vloadif(dpath|"cust_ord.vws")
'   order change key "[Marker]"
  data query execute "clearmkr.dfq" index "temp.idx"
' ŚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄæ
'   len([Marker])>0 replace [Marker] = ""
' ĄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄŁ
END FUNCTION ' ResetIndices()


FUNCTION Action()
local j
  error off
  data goto window 2
  j = tablemax([Order])                '
  if cerror
    j=0
  end if                               'message "j is:"&str(j)
  $ord=str(j)                          'message "$ord is:"&str($ord)
  data goto window 1
  case j
    when 0
      SendLetter("FIRST","drschas1.dfr",1)   '       Send1st()
    when 1
      SendLetter("SECOND","drschas2.dfr",2)  '       Send2nd()
    when 2
'       SendLetter("THIRD","drschas3.dfr",3)   '       Send3rd()
      RedStatement()
    when 3
'       LegalFile()
    when 4
      messboxwait(" Refer to legal file already opened ",0,0,1)
    otherwise
      SendLetter("FIRST","drschas1.dfr",1)   '       Send1st()
  end case
  repaint on
  repaint
END FUNCTION ' Action()


FUNCTION PrintLetter(z)
  p1 = z
' message "#rec is:"&str(#rec)
  x = remove("printme.idx")
  x = makeidx("cust_ord","printme.idx",precord,3)
  order change index "printme.idx"
  custcode = [Customer_Code]
  deladdr1 = [Delivery_Address_1]
  ftgdate  = [Fitting_Date]
  #baldue  = [Balance_Due]
  $branch  = [Branch]

  data goto window 2
  $title   = [Title]
  addr1    = [Address_1]
  addr2    = [Address_2]
  custcity = [City/Town]
  custcode = [Postcode]
  data goto window 1

  vloadif(dpath|"branches.vws")
  $phone   = filelookup([Br_Init],[Br_Phone],$branch)
  vunloadif("branches.vws")

  vloadif(dpath|"drschas1.vw")
  x = Approve()
  if x = -1
    return (-1)
  end if
  UpdateCustomer()
'   messboxwait(" Headed paper must be loaded in printer! ",0,1,1)
  repaint off
message "Printing at L924"
  PrintReport(p1,p2,p3,p4,p5,p6)
  repaint off
END FUNCTION ' PrintLetter()


FUNCTION SendLetter(ms,prnrep,#ord)
local m1 m2 #recnr
' message "#ord is:"&str(#ord)
  repaint off
  x = PrintLetter(prnrep)
  if x = -1
    vloadif(dpath|"drschas1.vw")
    order change index ipath|"aged_drs.idx"
    return (0)
  end if
  increment(dpath|"drschase.dat",1)
  drs_chasnr = right("000000"|str(ptval),6)
  vloadif(dpath|"drschase.vws")
  window zoom
  data enter lock
    [Job_Nr]      = jobnr
    [Action_Date] = today
    [Order]       = #ord
    [Act_Nr]      = drs_chasnr
  write-record
  window zoom

  EnterComment()

  vloadif(dpath|"drschas1.vw")
  #recnr = record
  lock-record
    [Marker] = ""
  write-record
  order change physical
  x = delidxrec(ipath|$branch|"_drsact.idx",#recnr,2) 'message "x is:"&str(x)
  order change index ipath|$branch|"_drsact.idx"
END FUNCTION ' SendLetter()


FUNCTION EnterComment()
local $ordnote
' message "$ord is:"&str($ord)

  $ordnote = case $ord ("0","1st letter printed")("1","2nd letter printed")("2","3rd letter printed")("3","To be sent to Higgins")
  vloadif(dpath|"comments.vws")
' message "jobnr is:"&str(jobnr)
' message "$ordnote is:"&str($ordnote)
  data enter lock
    [Job_Nr]   =jobnr
    [EnterDate]=today
    [Comment]  =$ordnote
    [Author]   =userid
    [Category] = "C"
  write-record
  vunloadif("comments.vws")
END FUNCTION ' EnterComment()


FUNCTION RedStatement()
'   p1 = z
' message "#rec is:"&str(#rec)
  x = remove("printme.idx")
  x = makeidx("cust_ord","printme.idx",precord,3)
  order change index "printme.idx"
  custcode = [Customer_Code]
  deladdr1 = [Delivery_Address_1]
  ftgdate  = [Fitting_Date]
  #baldue  = [Balance_Due]
  $branch  = [Branch]

  data goto window 2
  $title   = [Title]
  addr1    = [Address_1]
  addr2    = [Address_2]
  custcity = [City/Town]
  custcode = [Postcode]
  data goto window 1

  vloadif(dpath|"branches.vws")
  $phone   = filelookup([Br_Init],[Br_Phone],$branch)
  vunloadif("branches.vws")

  vloadif(dpath|"drschas1.vw")
  x = Approve()
  if x = -1
    return (-1)
  end if
  UpdateCustomer()
'   messboxwait(" Headed paper must be loaded in printer! ",0,1,1)
  repaint off
message "Printing at L1016"
  PrintReport(p1,p2,p3,p4,p5,p6)
  repaint off
END FUNCTION ' RedStatement()


'   remove(X_path|"X_mth_o.*")
'
'   vunloadif("X_inv_a.vws")
'   ClearHardSpaces()
'   data query execute "not_del.dfq" Smart4 X_path|"X_mth_o" fields\
'   "[Var_Nr;Date;CustOrd_Name;Amount_Gross;shop;Date_To;Nr_Orders;Avg_Order;Tot_Order_Net;Tot_Order_Gross]"
'   _SWIP_Crystal(Xreppath|"$inv_1","P",0,1,"")
'   _SWIP_Crystal(Xreppath|"$inv_1","EP",0,1,invpath|$nextinvnr|".pdf")

