'TFR_RCPT - transfer receipts from one record to another

external   messbox() sch scw vloadif() dpath fentrybox() shopmask progress()
external   fgp bgp navrecs() $menu scr addidxrec() delidxrec()

public     ptstr ptval jobnr

global     CheckFirstNr() CheckSecondNr() x Show1stCash() Titles_2()
global     #1stbalance #2ndbalance $1stcustname $2ndcustname
global     TransferAmount() #1strecord #2ndrecord
global     Titles_1() #count $1stdeladdr $2nddeladdr
global     ShowBoth() firstjobnr secondjobnr CreateCashRcvd() #receipt
global     #1stinvtot #2ndinvtot datereceipt $method receiptnr rcvd locn
global     $authcode $userid #now $delrec $addrec ResetWindows()

MAIN
  single-step off
'   quiet off
  repaint off
  #1stbalance = 0
  #2ndbalance = 0

  while true
    x = CheckFirstNr()                   ' get 1st nr
    if x = -1
      exit while
    end if

    x = Show1stCash()
    if x = -1
      exit while
    end if

    x = CheckSecondNr()        ' get 2nd nr
    if x = -1
      exit while
    end if

    x = ShowBoth()
    if x = -1
      exit while
    end if

  end while
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
END MAIN

FUNCTION CheckFirstNr()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"cust_ord.vws")

  while true
    while true
      x = fentrybox(" Job Nr to check receipts for ",6,shopmask,"")
      if x = -1
        return (-1)
      elseif x = 0
        firstjobnr = ptstr
        exit while
      end if
    end while

    progress(fgp,bgp," Checking for existing order ",0)

    order change key "[Job_Nr]"
    data find "[Job_Nr]" equal firstjobnr options ""
    if cerror                               '   if none - then return
      messbox(" Job Nr not found - re-enter ",0,0,1)
      continue while
    else
      #1stbalance  = [Balance_Due]
      $1stcustname = [CustOrd_Name]
      #1strecord   = precord
      #1stinvtot   = [Invoice_Total]
      $1stdeladdr  = [Delivery_Address_1]
    end if
    exit while
  end while
END FUNCTION ' CheckFirstNr()

FUNCTION CheckSecondNr()
  while true
    while true
      scr = scr + 7
      x = fentrybox(" Job Nr to transfer receipt TO ",6,shopmask,"")
      scr = scr - 7
      if x = -1
        return (-1)
      elseif x = 0
        secondjobnr = ptstr
        exit while
      end if
    end while

    progress(fgp,bgp," Checking for existing order ",0)

    vloadif(dpath|"cust_ord.vws")
    order change key "[Job_Nr]"
    data find "[Job_Nr]" equal secondjobnr options ""
    if cerror                               '   if none - then return
      messbox(" Job Nr not found - re-enter ",0,0,1)
      continue while
    else
      #2ndbalance  = [Balance_Due]
      $2ndcustname = [CustOrd_Name]
      #2ndrecord   = precord
      #2ndinvtot   = [Invoice_Total]
      $2nddeladdr  = [Delivery_Address_1]
    end if
    exit while
  end while
END FUNCTION ' CheckSecondNr()

FUNCTION CreateCashRcvd()
local y1 y2 y3 y4
'   screen clear box 1 1 sch scw 0 0 no-border
  while true
    x = messbox(" Confirm transfer of"&currency(#receipt)|"from"&firstjobnr&"to"&secondjobnr|"? (y/n) ",1,1,1)
    if x = 0
      if ptstr = "n"
        return (-1)
      end if
      exit while
    end if
  end while

' create new cashrcvd record to add to NEW custord
  vloadif(dpath|"cashrcvd.vws")
  data enter blank
  lock-record
    [Date_Of_Receipt] = datereceipt
    [Job_Nr] = secondjobnr
    [Method_Of_Payment] = $method
    [Receipt_Nr] = receiptnr
    [Amount_Received] = rcvd
    [Branch] = locn
    [Time] = #now
    [Authorisation] = $authcode
    [Entered_By] = $userid
  write-record
  $addrec = precord
  x = addidxrec("#2ndcash.idx",$addrec,4)

  rcvd = rcvd*(-1)
'   data enter lock
  data enter blank          ' create new cashrcvd record to deduct from OLD custord
  lock-record
    [Date_Of_Receipt] = datereceipt
    [Job_Nr] = firstjobnr
    [Method_Of_Payment] = $method
    [Receipt_Nr] = receiptnr
    [Amount_Received] = rcvd
    [Branch] = locn
    [Time] = #now
    [Authorisation] = $authcode
    [Entered_By] = $userid
  write-record
  x = delidxrec("#1stcash.idx",$delrec,3)

END FUNCTION  'CreateCashRcvd()

FUNCTION TransferAmount()
  vloadif(dpath|"cust_ord.vws")
  data goto record record-number #1strecord
  lock-record
    [Balance_Due] = [Balance_Due] - rcvd
  write-record
  #1stbalance = [Balance_Due]

  data goto record record-number #2ndrecord
  lock-record
    [Balance_Due] = [Balance_Due] + rcvd
  write-record
  #2ndbalance = [Balance_Due]

  ResetWindows()
'   data goto window 2                   ' reset windows
END FUNCTION ' TransferAmount()

FUNCTION Show1stCash()
local $puar
  ptval=0
  jobnr = firstjobnr
  vloadif(dpath|"vu_cash.vw")
  order change key "[Job_Nr]"
  data query execute "vu_cash" index  "#1stcash.idx"
' 浜様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様融
'   [Job_Nr] = jobnr
' 藩様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様夕
  if cerror
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    messbox(" No cash received for this order ",0,0,1)
    return (-2)
  end if
  repaint on
  repaint
  Titles_1()

  #count = records
  if #count > 9
    screen print 20 27 15 1 (format(" "|chr(24)&chr(25)&" more receipts ","M27"))
  end if

  while ptval <> {Esc}
    ptval = navrecs()
    if ptval = {T} or ptval = {t}
      repaint off
      if $menu <> "boss"
        continue while
      end if
      return (0)
    end if
  end while
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  return (-1)
END FUNCTION ' ShowCash()

FUNCTION Titles_1()
local y1 y2 y3 y4 y5
  y1 = format("Receipts for"&firstjobnr&"- Order"&currency(#1stinvtot),"M38")
  y2 = format($1stcustname,"M38")
  y3 = format($1stdeladdr,"M38")
  y4 = format("Balance o/s is"&currency(#1stbalance),"M27")
  if $menu == "boss"
    y5 = format("{T}ransfer - {Esc}","M27")
  else
    y5 = format("{Esc} to exit","M27")
  end if
'   repaint on
'   repaint
  screen print 5 22 fgp bgp y1
  screen print 6 22 fgp bgp y2
  screen print 7 22 fgp bgp y3
  screen print 19 27 fgp 12 y4
  screen print 21 27 fgp bgp y5
END FUNCTION ' Titles()

FUNCTION ShowBoth()
  error off
  while true
    window close
    if cerror
      exit while
    end if
  end while

  window split vertical 40
  data goto window 1
  vloadif(dpath|"vu_cash1.vw")
  order change index "#1stcash.idx"
  data goto window 2
  jobnr = secondjobnr
  vloadif(dpath|"vu_cash2.vw")
  order change key "[Job_Nr]"
  data query execute "vu_cash" index  "#2ndcash.idx"
' 浜様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様融
'   [Job_Nr] = jobnr
' 藩様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様様夕
  if cerror
    screen clear box 1 1 sch scw 0 0 no-border
    repaint off
    messbox(" No stock held ",0,0,1)
    return (-2)
  end if

  data goto window 1
  repaint on
  repaint
  Titles_2()

  while ptval <> {Esc}
    ptval = navrecs()
    if ptval = {S} or ptval = {s}
      repaint off
      #receipt    = [Amount_Received]
      datereceipt = [Date_Of_Receipt]
      jobnr       = [Job_Nr]
      $method     = [Method_Of_Payment]
      receiptnr   = [Receipt_Nr]
      rcvd        = [Amount_Received]
      locn        = [Branch]
      #now        = [Time]
      $authcode   = [Authorisation]
      $userid     = [Entered_By]
      $delrec = record

      x = CreateCashRcvd()             	' moving cash receipt
      if x = -1
        ResetWindows()
        repaint on
        repaint
        Titles_2()
        continue while
      end if

      TransferAmount()         		' tfr amount

      repaint on
      repaint
      Titles_2()
    end if
  end while
  window close
  return (-1)
END FUNCTION 'ShowBoth()

FUNCTION Titles_2()
local y1 y2 y3 y4 y5 y6 y7 y8 y9
  y1 = format("Receipts for"&firstjobnr&"- Order"&currency(#1stinvtot),"M38")
  y2 = format($1stcustname,"M38")
  y3 = format($1stdeladdr,"M38")
  y4 = format("Balance o/s is"&currency(#1stbalance),"M27")
  y5 = format("{S}elect - {Esc}","M27")
  y6 = format("Receipts for"&secondjobnr&"- Order"&currency(#2ndinvtot),"M38")
  y7 = format($2ndcustname,"M38")
  y8 = format($2nddeladdr,"M38")
  y9 = format("Balance o/s is"&currency(#2ndbalance),"M27")
  screen print 5 2 fgp bgp y1
  screen print 5 42 7 bgp y6
  screen print 6 2 fgp bgp y2
  screen print 6 42 7 bgp y7
  screen print 7 2 fgp bgp y3
  screen print 7 42 7 bgp y8
  screen print 19 7 fgp 12 y4
  screen print 19 47 7 12 y9
  screen print 21 27 fgp bgp y5
END FUNCTION ' Titles_2()

FUNCTION ResetWindows()
  data goto window 2
  vloadif(dpath|"vu_cash2.vw")
  order change index "#2ndcash.idx"
  data goto window 1
  vloadif(dpath|"vu_cash1.vw")
  order change index "#1stcash.idx"
END FUNCTION 'ResetWindows()


