'DLY_PSTG - daily sales & cash/chqs analysis & posting summary

' 19/07/01 - new version to include Putney (from 01/09/01)

external   dpath PrintReport() sch scw vloadif() progress() progtag() fgi bgi
external   vatrate remove() makeidx() fentrybox() chkdate() messbox() cpath
external   nr8 messboxwait() dly_pstgpath vunloadif()

public     ptstr rel_date from_date chk_cb $month VATdep_held
public     ob                          ' opening balance from "op_bal.dat"
public     openstck                    ' opening balance from "stockbal.dat"
public     #sd
public     #sv                         ' stock diff in currency (absolute)

'#### SALES variables
public     totsal                      ' daily sales - calc'd from data
public     sal_F                       ' Fulham sales - calc'd from data
public     sal_P                       ' Putney   "   - calc'd from data
public     sal_R                       ' Raynes   "   - calc'd from data
public     sal_S                       ' Sheen    "   - calc'd from data
public     sal_T                       ' Trade    "   - calc'd from data
public     sal_W                       ' W'hse    "   - calc'd from data
public     sal_Y                       ' sundry   "   - calc'd from data
public     sal_V                       ' VAT on   "   - calc'd from data
public     sal_FG                      ' Fulham sales - calc'd from data GROSS
public     sal_PG                      ' Putney   "   - calc'd from data GROSS
public     sal_RG                      ' Raynes   "   - calc'd from data GROSS
public     sal_SG                      ' Sheen    "   - calc'd from data GROSS
public     sal_TG                      ' Trade    "   - calc'd from data GROSS
public     sal_WG                      ' W'hse    "   - calc'd from data GROSS
public     sal_YG                      ' sundry   "   - calc'd from data GROSS
public     sal_XG                      ' Other sales etc

'#### ORDERS variables
public     totord                      ' daily orders - calc'd from data
public     ord_F                       ' Fulham orders  calc'd from data GROSS
public     ord_P                       ' Putney   "   - calc'd from data GROSS
public     ord_R                       ' Raynes   "   - calc'd from data GROSS
public     ord_S                       ' Sheen    "   - calc'd from data GROSS
public     ord_T                       ' Trade    "   - calc'd from data GROSS
public     ord_W                       ' W'hse    "   - calc'd from data GROSS
public     ord_Y                       ' sundry   "   - calc'd from data GROSS
public     ord_X                       ' Other orders etc

'#### CASH variables
public     cr                          ' cash rec'd - calc'd from data
public     tot_con                     ' total contras
public     tot_cash                    ' total cash/chq's
public     tot_pdq                     ' total pdq's
' public     dep_rcvd                    ' deposits rec'd prior to invoice
public     csh_F                       ' Fulham receipts cash/chqs - calc'd
public     csh_H                       ' H/O receipts cash/chqs - calc'd
public     csh_P                       ' Putney receipts cash/chqs - calc'd
public     csh_R                       ' Raynes receipts cash/chqs - calc'd
public     csh_S                       ' Sheen receipts cash/chqs - calc'd
public     pdq_F                       ' Fulham receipts PDQ   - calc'd
public     pdq_H                       ' H/O receiipts PDQ   - calc'd
public     pdq_P                       ' Putney receipts PDQ   - calc'd
public     pdq_R                       ' Raynes receipts PDQ   - calc'd
public     pdq_S                       ' Sheen receipts PDQ   - calc'd
public     con_F                       ' Fulham contras - calc'd
public     con_H                       ' H/O contras - calc'd
public     con_P                       ' Putney contras - calc'd
public     con_R                       ' Raynes contras - calc'd
public     con_S                       ' Sheen contras - calc'd
public     con_F_B                     ' Fulham contras
public     con_P_B                     ' Putney contras
public     con_R_B                     ' Raynes contras
public     con_S_B                     ' Sheen contras
public     con_T_B                     ' Trade contras
public     con_W_B                     ' W'hse contras
public     con_Y_B                     ' Sundry contras
public     rcv_F_D                     ' Fulham receipts - DEPOSITS
public     rcv_P_D                     ' Putney receipts -    "
public     rcv_R_D                     ' Raynes receipts -    "
public     rcv_S_D                     ' Sheen receipts  -    "
public     rcv_T_D                     ' Trade receipts  -    "
public     rcv_W_D                     ' W'hse receipts  -    "
public     rcv_Y_D                     ' Sundry receipts -    "
public     rcv_F_B                     ' Fulham receipts - BALANCES
public     rcv_P_B                     ' Putney receipts -    "
public     rcv_R_B                     ' Raynes receipts -    "
public     rcv_S_B                     ' Sheen receipts  -    "
public     rcv_T_B                     ' Trade receipts  -    "
public     rcv_W_B                     ' W'hse receipts  -    "
public     rcv_Y_B                     ' Sundry receipts -    "
public     tfr_D2F                     ' Fulham tfr's from Deposit to Drs
public     tfr_D2P                     ' Putney tfr's from Deposit to Drs
public     tfr_D2R                     ' Raynes tfr's from Deposit to Drs
public     tfr_D2S                     ' Sheen  tfr's from Deposit to Drs
public     tfr_D2T                     ' Trade  tfr's from Deposit to Drs
public     tfr_D2W                     ' W'h'se tfr's from Deposit to Drs
public     tfr_D2Y                     ' Sundry tfr's from Deposit to Drs

'#### OTHER variables
public     $unposted
' public     VATdep_held
public     #dep_held
public     #bal_due
public     bal_F                       ' Fulham balances
public     bal_P                       ' Putney   "
public     bal_R                       ' Raynes   "
public     bal_S                       ' Sheen    "
public     bal_T                       ' Trade    "
public     bal_W                       ' W'hse    "
public     bal_Y                       ' sundry   "
public     bal_X                       ' Other orders etc
public     #total #precs               ' totals of stock balances

global     x Calc_Bals() Sales() Receipts() PostDate() SelectDate() Orders()
global     ReturnToMenu1() ReturnToMenu2() StoreReport() repdate Reports()
global     #chk_ord #chk_sal #chk_csh #chk1 p1 p2 p3 p4 p5 p6 cm CheckMonth() last_m_e
global     UnpostedCash() Print2and3()
'  Stk_Bals()


MAIN
single-step off
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  remove("sal_pstg.idx")
  remove("salpstg1.idx")
  remove("dly_cash.idx")
  remove("dlypstg3.idx")
  remove("dlypstg4.idx")
  remove("csh_pstg.idx")
  remove("cshpstg1.idx")
  remove("contras.idx")

  p2 = ""               ' p2 = title at top of choice popup ("LABEL")
  p3 = 1                ' p3 = printer to be used (1=HPIII_QC; 2=GEN_EPSN etc)
  p4 = 1                ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
  p5 = 1                ' choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1                ' p6 = nr of copies

'  messboxwait(" Using local printer port ",0,0,1)

  UnpostedCash()

'   Stk_Bals()

  rel_date = date2(days(today)-1)
  SelectDate()

  progress(15,10," Preparing data for Daily Posting Journals ",1)

  x = Calc_Bals()
  if x = -1
    ReturnToMenu2()
  end if

' print test - page 1
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  remove("printme.idx")
  vloadif(dpath|"cust_ord.vws")
  x=makeidx("cust_ord","printme.idx",precord,3)  'message "x is:"&str(x)
  order change index "printme"
  p1 = "dlypst1a.dfr"
  p2 = "Summary"
  PrintReport(p1,p2,p3,p4,p5,p6)

  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  messbox(" Is the Summary correct? (y/n) ",1,1,1)
  if ptstr == "n"
    messboxwait(" Correct the problem and run this program again ",0,0,1)
    ReturnToMenu2()
  end if

  while true
    messbox(" Post these figures to CUST_ORD & CASHRCVD? (y/n) ",1,1,1)
    if ptstr == "y"
      PostDate()
      exit while
    else
      messbox(" Abandon process and return to Menu? (y/n) ",1,1,1)
      if ptstr == "n"
        continue while
      else
        ReturnToMenu2()
      end if
    end if
  end while

  Reports()

  ReturnToMenu1()

END MAIN


FUNCTION Reports()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  vloadif(dpath|"cust_ord.vws")
  order change index "printme"
  p1 = "dlypst1a.dfr"
  p2 = "Summary"
  StoreReport(p1,"sum")
  if file("sal_pstg.idx")=1
    vloadif(dpath|"cust_ord.vws")
    order change index "sal_pstg.idx"
    data query execute "salpstg1.dfq" index "salpstg1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [SalesAnalysis]<>"X"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
'     order sort now dictionary "a" fields "[SalesAnalysis;Inv_Nr]" ascending
    order sort now dictionary "dlypstg2.idx" fields "[SalesAnalysis;Inv_Nr]" ascending
    p1 = "dlypstg2.dfr"
    p2 = "Sales Invoices"
    PrintReport(p1,p2,p3,p4,p5,p6)
    StoreReport(p1,"001")
    vunloadif("cust_ord.vws")
  end if

  if file("csh_pstg.idx")=1
    vloadif(dpath|"dly_cash.vw")
    order change index "csh_pstg.idx"
    data query execute "notcntra.dfq" index "notcntra.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Abbrv_M] <> "O"
'   and
'   [Banked] <> blank
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if records > 0
      Print2and3()
    end if
  end if

  if file("csh_pstg.idx")=1
    vloadif(dpath|"dly_cash.vw")
    order change index "csh_pstg.idx"    'query to include "O" [Abbrv_Method] records
    data query execute "contras.dfq" index "contras.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Abbrv_M] = "O"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if not (cerror)
      order sort now dictionary "dlypstg5.idx" fields "[Method_Of_Payment]" ascending
      p1 = "dlypstg5.dfr"
      p2 = "Contras List"
      PrintReport(p1,p2,p3,p4,p5,p6)
      StoreReport(p1,"004")
    end if

    if file("cshpstg1.idx")=1
      order change index "cshpstg1.idx"
      p1 = "csh_deps.dfr"
      p2 = "VAT on Deposits"
      PrintReport(p1,p2,p3,p4,p5,p6)
      StoreReport(p1,"005")
    end if

    if file("sal_pstg.idx")=1
      vloadif(dpath|"vatontfr.vw")
      order change index "vatontfr.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [VAT_on_deposit] > 0
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
      if cerror
      else
        p1 = "csh_tfrd.dfr"
        p2 = "VAT on Tfr'd Dep's"
        PrintReport(p1,p2,p3,p4,p5,p6)
        StoreReport(p1,"007")
      end if
    end if
  end if
END FUNCTION ' Reports()


FUNCTION Print2and3()
  order sort now dictionary "dlypstg3.idx" fields "[CP]" ascending
  p1 = "dlypstg3.dfr"                  'unposted receipts by DATE
  p2 = "Daily Receipts"
  PrintReport(p1,p2,p3,p4,p5,p6)
  StoreReport(p1,"002")
  order sort now dictionary "dlypstg4.idx" fields "[SalesAnalysis]" ascending
  p2 = "Branch Receipts"
  p1 = "dlypstg4.dfr"
  StoreReport(p1,"003")
END FUNCTION ' Print2&3()


FUNCTION SelectDate()
local mth ld
  vloadif(dpath|"cust_ord.vws")
  order change physical
  data goto record last
  while true
    from_date = days([Posted])
    if from_date <> 0
      exit while
    else
      data goto record previous
    end if
  end while
  from_date = date2([Posted]+1)        '
message "from_date is:"&str(from_date)

  from_date = @if(days(from_date)>=days(rel_date),rel_date,from_date)
  mth = right(date2(from_date),7)
  for ld = 31 to 28 step -1
    last_m_e = str(ld)|"/"|mth         ' message "last_m_e is:"&str(last_m_e)
    if chkdate(last_m_e,1) = -1
      continue for
    else
      exit for
    end if
  end for

  if days(last_m_e) < days(rel_date)
    rel_date = last_m_e
  end if
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  while true
    x = fentrybox(" Confirm Date for Cash Summary ",10,"##\/##\/####",rel_date)
    if x = 0
      rel_date = ptstr
      if chkdate(rel_date,1) = -1
        messbox(" Incorrect date - re-enter ",0,0,1)
        continue while
      end if
      repdate = left(rel_date,2)|mid(rel_date,4,2)|right(rel_date,2) 'message "repdate) is:"&str(repdate)
      cm = CheckMonth()
      if cm = 0
        return (0)
      else
        messboxwait(" Prepare report to"&last_m_e&"before any for this month ",0,0,1)
        continue while
      end if
    end if
  end while
END FUNCTION ' SelectDate()


FUNCTION CheckMonth()
local mth ld
'   mth = right(date2(from_date),5)
  mth = right(date2(from_date),7)
  for ld = 31 to 28 step -1
    last_m_e = str(ld)|"/"|mth         'message "last_m_e is:"&str(last_m_e)
    if chkdate(last_m_e,1) = -1
      continue for
    else
      exit for
    end if
  end for
  $month = mid(date2(from_date),4,2)|right(date2(from_date),4)
  if days(rel_date) > days(last_m_e)   ' YES - return 1
    return (1)
  else
    return (0)                         ' NO  - return 0
  end if
END FUNCTION ' CheckMonth()


FUNCTION StoreReport(p1,ext)
local dlyrep
  dlyrep = dly_pstgpath|"DP"|repdate|"."|ext  '
  print report execute p1 disk dlyrep detail start 1 end 0 copies 1
END FUNCTION ' StoreReport()


FUNCTION PostDate()
local newbal
  newbal = ob + totsal - cr
  vloadif(dpath|"cust_ord.vws")
  order change physical

' write posting date to cust_ord
  if file("sal_pstg.idx")=1
    order change index "sal_pstg.idx"
    data query execute "postdate.dfq"  'index "csh_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   replace [Posted] = rel_date
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  end if

' write posting date to variat_n
  if file("ord_pstg.idx")=1
    vloadif(dpath|"dly_varn.vw")
    order change index "ord_pstg.idx"
    data query execute "postdate.dfq"   '  index "csh_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   replace [Posted] = rel_date
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  end if

' write posting date to cashrcvd
  vloadif(dpath|"cashrcvd.vws")
  if file("csh_pstg.idx")=1
    order change index "csh_pstg.idx"
    data query execute "postdate.dfq"   '  index "csh_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   replace [Posted] = rel_date
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  end if

' write new bal to op_bal.dat
  fopen dpath|"op_bal.dat" as 1
  fwrite 1 from newbal
  fclose 1

' write new bal to stockbal.dat
  fopen dpath|"stockbal.dat" as 1
  fwrite 1 from #total
  fclose 1

END FUNCTION ' PostDate()


FUNCTION Calc_Bals()
' check op_bal.dat is present otherwise prompt for new figure
  fopen dpath|"op_bal.dat" as 1
  fread 1 into ob
  fclose 1
  ob = value(ob)
  if ob = 0
    while true
      x = fentrybox(" Confirm Opening Sales Ledger Balance ",8,nr8,"")
      if x = -1
        continue while
      end if
      ob = value(ptstr)
      if ob = 0
        x = messboxwait(" Opening Balance cannot be ZERO ",0,0,0)
        if x = -1
          return (-1)
        end if
        continue while
      end if
    end while
  end if

  x = Sales()
  if x = 1
    return (-1)
  end if

  x = Orders()
  if x = 1
    return (-1)
  end if

  x = Receipts()
  if x = 1
    return (-1)
  end if

  vloadif(dpath|"cust_ord.vws")
  order change physical
  chk_cb = round(filesum([Balance_Due],[SalesAnalysis]<>"X"),2) 'message "chk_cb is:"&str(chk_cb)
  bal_F = round(filesum([Balance_Due],[SalesAnalysis]="F" and [Invoice_Date]<>blank),2)
  bal_P = round(filesum([Balance_Due],[SalesAnalysis]="P" and [Invoice_Date]<>blank),2)
  bal_R = round(filesum([Balance_Due],[SalesAnalysis]="R" and [Invoice_Date]<>blank),2)
  bal_S = round(filesum([Balance_Due],[SalesAnalysis]="S" and [Invoice_Date]<>blank),2)
  bal_T = round(filesum([Balance_Due],[SalesAnalysis]="T" and [Invoice_Date]<>blank),2)
  bal_W = round(filesum([Balance_Due],[SalesAnalysis]="W" and [Invoice_Date]<>blank),2)
  bal_Y = round(filesum([Balance_Due],[SalesAnalysis]="Y" and [Invoice_Date]<>blank),2)
END FUNCTION ' Calc_Bals()


FUNCTION Orders()
local q
  progtag(fgi,bgi," Selecting Sales Orders ")
  vloadif(dpath|"dly_varn.vw")
  data query execute "ord_pstg.dfq" index "ord_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Date]) <= days(rel_date) and [Posted] = blank
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    ord_F = 0
    ord_P = 0
    ord_R = 0
    ord_S = 0
    ord_W = 0
    ord_T = 0
    ord_Y = 0
    return(0)
  end if

  ord_F  = filesum([Amount_Gross],[SalesAnalysis]="F")   'message "ord_F is:"&str(ord_F) '
  ord_P  = filesum([Amount_Gross],[SalesAnalysis]="P")   'message "ord_R is:"&str(ord_R)
  ord_R  = filesum([Amount_Gross],[SalesAnalysis]="R")   'message "ord_R is:"&str(ord_R)
  ord_S  = filesum([Amount_Gross],[SalesAnalysis]="S")   'message "ord_S is:"&str(ord_S)
  ord_T  = filesum([Amount_Gross],[SalesAnalysis]="T")   'message "ord_T is:"&str(ord_T)
  ord_W  = filesum([Amount_Gross],[SalesAnalysis]="W")   'message "ord_W is:"&str(ord_W)
  ord_Y  = filesum([Amount_Gross],[SalesAnalysis]="Y")   'message "ord_Y is:"&str(ord_Y)

  #chk_ord = filesum([Amount_Gross])   'message "#chk_ord is:"&str(#chk_ord)
  totord = ord_F+ord_R+ord_S+ord_W+ord_Y+ord_T 'message "totord:"&str(totord)

  order sort now dictionary "ftrname.idx" fields "[Job_Nr]" ascending
  p1 = "dlypstg6.dfr"
  p2 = "Sales Orders"
  PrintReport(p1,p2,p3,p4,p5,p6)
  StoreReport(p1,"006")
  vunloadif("dly_varn.vw")

END FUNCTION ' Orders()


FUNCTION ReturnToMenu1()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  $month = mid(date2(from_date),4,2)|right(date2(from_date),2)
  lock system $month
  file unload all
  quit spreadsheet project-file "dlypstg.rf1"
END FUNCTION ' ReturnToMenu1()


FUNCTION ReturnToMenu2()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu2()



FUNCTION UnpostedCash()
  vloadif(dpath|"cust_ord.vws")
  order change key "[Job_Nr]"
  data find "[Job_Nr]" equal "Y00000" options ""
  if cerror                               '   if none - then return
    x = messboxwait(" Y00000 not found ",0,0,1)
    $unposted = "ÿ"
  elseif [Balance_Due] > 0.01
    messboxwait(" Y00000 contains unallocated cash ",0,0,1)
    $unposted = "Unallocated cash in Y00000"
  end if
END FUNCTION ' UnpostedCash()


FUNCTION Sales()
local q
  progtag(fgi,bgi," Selecting Sales Orders ")
  vloadif(dpath|"cust_ord.vws")
  data query execute "sal_pstg.dfq" index "sal_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Completed]="Y"
'   and
'   days([Invoice_Date]) <= days(rel_date)
'   and
'   [Posted] = blank
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    sal_F = 0
    sal_P = 0
    sal_R = 0
    sal_S = 0
    sal_W = 0
    sal_T = 0
    sal_Y = 0
    sal_V = 0
    sal_FG = 0
    sal_PG = 0
    sal_RG = 0
    sal_SG = 0
    sal_WG = 0
    sal_TG = 0
    sal_YG = 0
    return(0)
  end if

  sal_FG = filesum([Invoice_Total],[SalesAnalysis]="F") 'message "sal_FG is:"&str(sal_FG)
  sal_F  = filesum([Net_Invoice],[SalesAnalysis]="F")   'message "sal_F is:"&str(sal_F) '

  sal_PG = filesum([Invoice_Total],[SalesAnalysis]="P") 'message "sal_PG is:"&str(sal_RG)
  sal_P  = filesum([Net_Invoice],[SalesAnalysis]="P")   'message "sal_P is:"&str(sal_R)

  sal_RG = filesum([Invoice_Total],[SalesAnalysis]="R") 'message "sal_RG is:"&str(sal_RG)
  sal_R  = filesum([Net_Invoice],[SalesAnalysis]="R")   'message "sal_R is:"&str(sal_R)

  sal_SG = filesum([Invoice_Total],[SalesAnalysis]="S") 'message "sal_SG is:"&str(sal_SG)
  sal_S  = filesum([Net_Invoice],[SalesAnalysis]="S")   'message "sal_S is:"&str(sal_S)

  sal_TG = filesum([Invoice_Total],[SalesAnalysis]="T") ' message "sal_TG is:"&str(sal_TG)
  sal_T  = filesum([Net_Invoice],[SalesAnalysis]="T")   ' message "sal_TG is:"&str(sal_TG)

  sal_WG = filesum([Invoice_Total],[SalesAnalysis]="W") ' message "sal_WG is:"&str(sal_WG)
  sal_W  = filesum([Net_Invoice],[SalesAnalysis]="W")   ' message "sal_W is:"&str(sal_W)

  sal_YG = filesum([Invoice_Total],[SalesAnalysis]="Y") ' message "sal_YG is:"&str(sal_YG)
  sal_Y  = filesum([Net_Invoice],[SalesAnalysis]="Y")   ' message "sal_Y is:"&str(sal_Y)

  sal_XG = filesum([Invoice_Total],[SalesAnalysis]="X") 'message "sal_XG is:"&str(sal_XG)

  #chk_sal = filesum([Invoice_Total])                  'message "#chk is:"&str(#chk)

  sal_V  = sal_FG+sal_PG+sal_RG+sal_SG+sal_WG+sal_YG+sal_TG - (sal_F+sal_R+sal_S+sal_W+sal_Y+sal_T)

  totsal = sal_FG+sal_PG+sal_RG+sal_SG+sal_WG+sal_YG+sal_TG 'message "totsal:"&str(totsal)

  if abs(#chk_sal-totsal-sal_XG)>.01
    screen clear box 1 1 sch scw 0 0 no-border
    messboxwait(" Difference on Sales of"&currency(#chk_sal-totsal+sal_XG)&" check & rerun",0,0,1)
    return (1)
  end if
  #dep_held = totsal - #bal_due        ' message "#dep_held is:"&str(#dep_held)

' calc VAT element of Deposits already rec'd (and VAT accounted for) for each invoiced Order
  vunloadif("cust_ord.vws")
  vloadif(dpath|"vatondep.vw")
  order change index "sal_pstg.idx"
  VATdep_held = round(filesum([VAT_on_deposit]),2)
  vunloadif("vatondep.vw")
  vloadif(dpath|"cust_ord.vws")
  order change index "sal_pstg.idx"

  vloadif(dpath|"vatontfr.vw")
  order change index "sal_pstg.idx"
  order sort now dictionary "vattfr_1.idx" fields "[SalesAnalysis;Inv_Nr]" ascending
  data query execute "vatontfr.dfq" index "vatontfr.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [VAT_on_deposit] > 0
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
  else
    tfr_D2F = round(filesum([Total_Deposits],[SalesAnalysis]="F"),2)  ' message "tfr_D2F is:"&str(tfr_D2F)
    tfr_D2P = round(filesum([Total_Deposits],[SalesAnalysis]="P"),2)  ' message "tfr_D2R is:"&str(tfr_D2R)
    tfr_D2R = round(filesum([Total_Deposits],[SalesAnalysis]="R"),2)  ' message "tfr_D2R is:"&str(tfr_D2R)
    tfr_D2S = round(filesum([Total_Deposits],[SalesAnalysis]="S"),2)  ' message "tfr_D2S is:"&str(tfr_D2S)
    tfr_D2T = round(filesum([Total_Deposits],[SalesAnalysis]="T"),2)  ' message "tfr_D2T is:"&str(tfr_D2T)
    tfr_D2W = round(filesum([Total_Deposits],[SalesAnalysis]="W"),2)  ' message "tfr_D2W is:"&str(tfr_D2W)
    tfr_D2Y = round(filesum([Total_Deposits],[SalesAnalysis]="Y"),2)  ' message "tfr_D2Y is:"&str(tfr_D2Y)
  end if
END FUNCTION ' Sales()


FUNCTION Receipts()
local postcash
  progtag(fgi,bgi," Selecting Cash Receipts ")
  vloadif(dpath|"dly_cash.vw")
  order change key "[Date_Of_Receipt]"
  data query execute "cshpstg0.dfq" index "csh_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Date_Of_Receipt]) <= days(rel_date) and [Posted] = blank
'   and
'   [SalesAnalysis]<>"X"
'   and
'   [Abbrv_M] <> "O"
'   and
'   [Banked] <> blank
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    csh_F = 0
    csh_H = 0
    csh_P = 0
    csh_R = 0
    csh_S = 0
    pdq_F = 0
    pdq_H = 0
    pdq_P = 0
    pdq_R = 0
    pdq_S = 0
    con_F = 0
    con_H = 0
    con_P = 0
    con_R = 0
    con_S = 0
    return(0)
  end if

  csh_F = round(filesum([Amount_Received],[Branch]="F" and [Abbrv_M]="C"),2)+round(filesum([Amount_Received],[Branch]="F" and [Abbrv_M]="D"),2)
  csh_H = round(filesum([Amount_Received],[Branch]="H" and [Abbrv_M]="C"),2)+round(filesum([Amount_Received],[Branch]="H" and [Abbrv_M]="D"),2)'message "csh_H is:"&str(csh_H)
  csh_P = round(filesum([Amount_Received],[Branch]="P" and [Abbrv_M]="C"),2)+round(filesum([Amount_Received],[Branch]="R" and [Abbrv_M]="D"),2)
  csh_R = round(filesum([Amount_Received],[Branch]="R" and [Abbrv_M]="C"),2)+round(filesum([Amount_Received],[Branch]="R" and [Abbrv_M]="D"),2)
  csh_S = round(filesum([Amount_Received],[Branch]="S" and [Abbrv_M]="C"),2)+round(filesum([Amount_Received],[Branch]="S" and [Abbrv_M]="D"),2)

  pdq_F = round(filesum([Amount_Received],[Branch]="F" and [Abbrv_M]="P"),2)  ' message "pdq_F is:"&str(pdq_F)
  pdq_H = round(filesum([Amount_Received],[Branch]="H" and [Abbrv_M]="P"),2)  ' message "pdq_F is:"&str(pdq_F)
  pdq_P = round(filesum([Amount_Received],[Branch]="P" and [Abbrv_M]="P"),2)  ' message "pdq_F is:"&str(pdq_F)
  pdq_R = round(filesum([Amount_Received],[Branch]="R" and [Abbrv_M]="P"),2)  ' message "pdq_F is:"&str(pdq_F)
  pdq_S = round(filesum([Amount_Received],[Branch]="S" and [Abbrv_M]="P"),2)  ' message "pdq_F is:"&str(pdq_F)

  con_F = round(filesum([Amount_Received],[Branch]="F" and [Abbrv_M]="O"),2)  ' message "pdq_F is:"&str(pdq_F)
  con_H = round(filesum([Amount_Received],[Branch]="H" and [Abbrv_M]="O"),2)  ' message "pdq_F is:"&str(pdq_F)
  con_P = round(filesum([Amount_Received],[Branch]="P" and [Abbrv_M]="O"),2)  ' message "pdq_F is:"&str(pdq_F)
  con_R = round(filesum([Amount_Received],[Branch]="R" and [Abbrv_M]="O"),2)  ' message "pdq_F is:"&str(pdq_F)
  con_S = round(filesum([Amount_Received],[Branch]="S" and [Abbrv_M]="O"),2)  ' message "pdq_F is:"&str(pdq_F)

  rcv_F_D = round(filesum([Amount_Received],[SalesAnalysis]="F" and [Invoiced]="N"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_P_D = round(filesum([Amount_Received],[SalesAnalysis]="P" and [Invoiced]="N"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_R_D = round(filesum([Amount_Received],[SalesAnalysis]="R" and [Invoiced]="N"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_S_D = round(filesum([Amount_Received],[SalesAnalysis]="S" and [Invoiced]="N"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_T_D = round(filesum([Amount_Received],[SalesAnalysis]="T" and [Invoiced]="N"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_W_D = round(filesum([Amount_Received],[SalesAnalysis]="W" and [Invoiced]="N"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_Y_D = round(filesum([Amount_Received],[SalesAnalysis]="Y" and [Invoiced]="N"),2)  ' message "csh_F_D is:"&str(csh_F_D)

  rcv_F_B = round(filesum([Amount_Received],[SalesAnalysis]="F" and [Invoiced]="Y"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_P_B = round(filesum([Amount_Received],[SalesAnalysis]="P" and [Invoiced]="Y"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_R_B = round(filesum([Amount_Received],[SalesAnalysis]="R" and [Invoiced]="Y"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_S_B = round(filesum([Amount_Received],[SalesAnalysis]="S" and [Invoiced]="Y"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_T_B = round(filesum([Amount_Received],[SalesAnalysis]="T" and [Invoiced]="Y"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_W_B = round(filesum([Amount_Received],[SalesAnalysis]="W" and [Invoiced]="Y"),2)  ' message "csh_F_D is:"&str(csh_F_D)
  rcv_Y_B = round(filesum([Amount_Received],[SalesAnalysis]="Y" and [Invoiced]="Y"),2)  ' message "csh_F_D is:"&str(csh_F_D)

  con_F_B = round(filesum([Amount_Received],[SalesAnalysis]="F" and [Abbrv_M]="O"),2)  ' message "con_F_B is:"&str(con_F_B)
  con_P_B = round(filesum([Amount_Received],[SalesAnalysis]="P" and [Abbrv_M]="O"),2)  '
  con_R_B = round(filesum([Amount_Received],[SalesAnalysis]="R" and [Abbrv_M]="O"),2)  '
  con_S_B = round(filesum([Amount_Received],[SalesAnalysis]="S" and [Abbrv_M]="O"),2)  '
  con_T_B = round(filesum([Amount_Received],[SalesAnalysis]="T" and [Abbrv_M]="O"),2)  '
  con_W_B = round(filesum([Amount_Received],[SalesAnalysis]="W" and [Abbrv_M]="O"),2)  '
  con_Y_B = round(filesum([Amount_Received],[SalesAnalysis]="Y" and [Abbrv_M]="O"),2)  '

  #chk_csh = filesum([Amount_Received])    'message "Cash rec'd from filesum[#chk] is:"&str(#chk)
  tot_cash = csh_F+csh_S+csh_H+csh_P+csh_R   'message "tot_cash is:"&str(tot_cash)
  tot_pdq  = pdq_F+pdq_S+pdq_H+pdq_P+pdq_R   'message "tot_pdq is:"&str(tot_pdq)
  tot_con  = con_F+con_S+con_H+con_P+con_R   'message "tot_con is:"&str(tot_con)
  cr = tot_cash + tot_pdq + tot_con    '
' message "Cash,cheque,PDQ rec'd (cr) is:"&str(cr)

  postcash = rcv_F_D+rcv_S_D+rcv_W_D+rcv_R_D+rcv_Y_D+rcv_P_D+rcv_T_D+rcv_F_B+rcv_S_B+rcv_W_B+rcv_R_B+rcv_Y_B+rcv_P_B+rcv_T_B
' message "Total cash to post is:"&str(postcash)

  data query execute "cshpstg1.dfq" index "cshpstg1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Invoiced]="N"
'   and
'   [SalesAnalysis]<>"X"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    csh_F = 0
    csh_H = 0
    csh_P = 0
    csh_R = 0
    csh_S = 0
    pdq_F = 0
    pdq_H = 0
    pdq_P = 0
    pdq_R = 0
    pdq_S = 0
    con_F = 0
    con_H = 0
    con_P = 0
    con_R = 0
    con_S = 0
  end if
END FUNCTION ' Receipts()




