'DLY_PSTG - daily sales & cash/chqs analysis & posting summary

external   dpath PrintReport() sch scw vloadif() progress() progtag() fgi bgi
external   vatrate remove() makeidx() fentrybox() chkdate() messbox() cpath
external   nr8 messboxwait() dly_pstgpath

public     ptstr rel_date from_date chk_cb
public     ob                          ' opening balance from "op_bal.dat"
public     cr                          ' cash rec'd - calc'd from data
public     tot_cash                    ' total cash/chq's
public     tot_pdq                     ' total pdq's
public     dep_rcvd                    ' deposits rec'd prior to invoice
public     totsal                      ' daily sales - calc'd from data
public     sal_F                       ' Fulham sales - 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_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                      ' Putney,Clapham orders etc
public     csh_F                       ' Fulham receipts cash/chqs - calc'd
public     csh_H                       ' H/O 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_R                       ' Raynes receipts PDQ   - calc'd
public     pdq_S                       ' Sheen receipts PDQ   - calc'd
public     rcv_F                       ' Fulham receipts
public     rcv_R                       ' Raynes receipts
public     rcv_S                       ' Sheen receipts
public     rcv_T                       ' Trade receipts
public     rcv_W                       ' W'hse receipts
public     rcv_Y                       ' Sundry receipts
' public     NetVATsales                 '
' public     VATdep_held
public     #dep_held
public     #bal_due
public     bal_F                       ' Fulham balances
public     bal_R                       ' Raynes   "
public     bal_S                       ' Sheen    "
public     bal_T                       ' Trade    "
public     bal_W                       ' W'hse    "
public     bal_Y                       ' sundry   "
public     bal_X                       ' Putney,Clapham orders etc
public     #total #precs               ' totals of stock balances

global     x Calc_Bals() Sales() Receipts() PostDate() SelectDate()
global     ReturnToMenu() StoreReport() repdate Reports() Stk_Bals()
global     #chk #chk1 p1 p2 p3 p4 p5 p6 cm CheckMonth() last_m_e


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("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 = 3                ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1                ' p6 = nr of copies
message "Using local printer port"

'   Stk_Bals()
message "No 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
    ReturnToMenu()
  end if

  messbox(" Post these figures to CUST_ORD & CASHRCVD? (y/n) ",1,1,1)
  if ptstr == "y"
    PostDate()
  end if

  remove("printme.idx")
  vloadif(dpath|"cust_ord.vws")
  makeidx("cust_ord","printme.idx",precord,3)
  order change index "printme"

  Reports()

  ReturnToMenu()

END MAIN


FUNCTION SelectDate()
  vloadif(dpath|"cust_ord.vws")
  from_date = date2(filemax([Posted])+1)
  from_date = @if(days(from_date)>=days(rel_date),rel_date,from_date)
  while true
    x = fentrybox(" Confirm Date for Cash Summary ",8,"##\/##\/##",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)
  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(rel_date) > days(last_m_e)   ' YES - return 1
    return (1)
  else
    return (0)                         ' NO  - return 0
  end if
END FUNCTION ' CheckMonth()


FUNCTION Reports()
  p1 = "dlypstg1.dfr"
  p2 = "dlypstg1"
  PrintReport(p1,p2,p3,p4,p5,p6)
  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 "dlypstg2.idx" fields "[Job_Nr]" ascending
    p1 = "dlypstg2.dfr"
    p2 = "dlypstg2"
    PrintReport(p1,p2,p3,p4,p5,p6)
    StoreReport(p1,"001")
  end if

  if file("csh_pstg.idx")=1
    vloadif(dpath|"dly_cash.vw")
    order change index "csh_pstg.idx"
    order sort now dictionary "dlypstg3.idx" fields "[CP]" ascending
    p1 = "dlypstg3.dfr"                  'unposted receipts by DATE
    p2 = "dlypstg3"
    PrintReport(p1,p2,p3,p4,p5,p6)
    StoreReport(p1,"002")
    order sort now dictionary "dlypstg4.idx" fields "[SalesAnalysis]" ascending
    p2 = "dlypstg4"
    p1 = "dlypstg4.dfr"
    PrintReport(p1,p2,p3,p4,p5,p6)
    StoreReport(p1,"003")
  end if

'   vloadif(dpath|"dly_cash.vw")
'   order change index "csh_pstg.idx"
'   data query execute "contras.dfq" index "contras.idx"
' ' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' '   [Abbrv_M] = "O"
' ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
'   if records > 0
'     order sort now dictionary "dlypstg5.idx" fields "[Method_Of_Payment]" ascending
'     p1 = "dlypstg5.dfr"
'     p2 = "dlypstg5"
'     PrintReport(p1,p2,p3,p4,p5,p6)
'     StoreReport(p1,"004")
'   end if
END FUNCTION ' Reports()


FUNCTION StoreReport(p1,ext)
local dlyrep
  dlyrep = dly_pstgpath|"DP"|repdate|"."|ext  'message "dlyrep is:"&str(dlyrep)
  print report execute p1 disk dlyrep detail start 1 end 0 copies 1
END FUNCTION ' StoreReport()


FUNCTION Sales()
local q
  progtag(fgi,bgi," Selecting Sales Orders ")
  vloadif(dpath|"cust_ord.vws")
'   from_date = date2(filemax([Posted])+1)
'   from_date = @if(days(from_date)>=days(rel_date),rel_date,from_date)
  data query execute "sal_pstg.dfq" index "sal_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Date_Of_Order]) <= days(rel_date) and [Posted] = blank
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    sal_F = 0
    sal_R = 0
    sal_S = 0
    sal_W = 0
    sal_T = 0
    sal_Y = 0
    sal_V = 0
    sal_FG = 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_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 = filesum([Invoice_Total])                  'message "#chk is:"&str(#chk)
  sal_V = sal_FG+sal_RG+sal_SG+sal_WG+sal_YG+sal_TG - (sal_F+sal_R+sal_S+sal_W+sal_Y+sal_T)
' message "sal_V is:"&str(sal_V)

  totsal = sal_FG+sal_RG+sal_SG+sal_WG+sal_YG+sal_TG
' message "totsal:"&str(totsal)

  q=#chk-totsal+sal_XG
' message "q is:"&str(q)
  if abs(#chk-totsal+sal_XG)>.01
' message " More than 1p diff"
' message "#chk is:"&str(#chk)
' message "totsal+sal_XG is:"&str(totsal+sal_XG)
  end if

'   closebal = round(filesum([Balance_Due],[SalesAnalysis]<>"X"),2)  '
' message "closebal is:"&str(closebal)
'   #bal_due = round(filesum([Balance_Due],[SalesAnalysis]<>"X"),2)  '
' message "#bal_due is:"&str(#bal_due)
  #dep_held = totsal - #bal_due
' message "#dep_held is:"&str(#dep_held)

' calc VAT element of Deposits already rec'd for Orders (ie.VAT paid when rec'd)
'   NetVATsales = totsal-sal_XG-sal_V
'   VATdep_held = #dep_held*vatrate/(100+vatrate)
END FUNCTION ' Sales()


FUNCTION Receipts()
local postcash
  progtag(fgi,bgi," Selecting Cash Receipts ")
'   vloadif(dpath|"cashrcvd.vws")
  vloadif(dpath|"dly_cash.vw")
  order change key "[Date_Of_Receipt]"
  data query execute "csh_pstg.dfq" index "csh_pstg.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days([Date_Of_Receipt]) <= days(rel_date) and [Posted] = blank
'   and
'   [SalesAnalysis]<>"X"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    csh_F = 0
    csh_H = 0
    csh_R = 0
    csh_S = 0
    pdq_F = 0
    pdq_H = 0
    pdq_R = 0
    pdq_S = 0
    return(0)
  end if

  csh_F = round(filesum([Amount_Received],[Branch]="F" and [Abbrv_M]="C"),2)  ' message "csh_F is:"&str(csh_F)
  csh_H = round(filesum([Amount_Received],[Branch]="H" and [Abbrv_M]="C"),2)  ' message "csh_F is:"&str(csh_F)
  csh_R = round(filesum([Amount_Received],[Branch]="R" and [Abbrv_M]="C"),2)  ' message "csh_F is:"&str(csh_F)
  csh_S = round(filesum([Amount_Received],[Branch]="S" and [Abbrv_M]="C"),2)  ' message "csh_F is:"&str(csh_F)

  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_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)

  rcv_F = round(filesum([Amount_Received],[SalesAnalysis]="F"),2)  ' message "csh_F is:"&str(csh_F)
  rcv_R = round(filesum([Amount_Received],[SalesAnalysis]="R"),2)  ' message "csh_R is:"&str(csh_R)
  rcv_S = round(filesum([Amount_Received],[SalesAnalysis]="S"),2)  ' message "csh_S is:"&str(csh_S)
  rcv_T = round(filesum([Amount_Received],[SalesAnalysis]="T"),2)  ' message "csh_W is:"&str(csh_T)
  rcv_W = round(filesum([Amount_Received],[SalesAnalysis]="W"),2)  ' message "csh_W is:"&str(csh_W)
  rcv_Y = round(filesum([Amount_Received],[SalesAnalysis]="Y"),2)  ' message "csh_Y is:"&str(csh_Y)

  #chk = filesum([Amount_Received])    '
' message "Cash rec'd from filesum is:"&str(#chk)
  tot_cash = csh_F+csh_S+csh_H+csh_R
  tot_pdq  = pdq_F+pdq_S+pdq_H+pdq_R
  cr = tot_cash + tot_pdq              '
' message "Cash,cheque,PDQ rec'd is:"&str(cr)
  postcash = rcv_F+rcv_S+rcv_W+rcv_R+rcv_Y+rcv_T
' message "Total cash to post is:"&str(postcash)

  dep_rcvd = round(filesum([Amount_Received],[Inv_Nr]=blank),2)  ' message "deprcvd:"&str(pdq_Y)
'   #chk = filesum([Amount_Received])    'message "#chk is:"&str(#chk)
'   osbal_rcvd = cr - dep_rcvd

  if abs(#chk-cr)>.01
message " More than 1p diff"
message "#chk is:"&str(#chk)
message "#chk1 is:"&str(#chk1)
  end if

END FUNCTION ' Receipts()


FUNCTION PostDate()
local  newbal
  newbal = ob + totsal - cr
' message "ob is:"&str(ob)
' message "totsal is:"&str(totsal)
' message "cr is:"&str(cr)
' message "newbal is:"&str(newbal)
  vloadif(dpath|"cust_ord.vws")
  order change physical
'   cb = filesum([Balance_Due])
' message "Bal Due is:"&str(cb)

' 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 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
END FUNCTION ' PostDate()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  clear public
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


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
' message "ob is:"&str(ob)
  ob = value(ob)
  if ob = 0
    while true
      x = fentrybox(" Confirm Opening Sales Ledger Balance ",8,nr8,"")
' message "x) is:"&str(x)
      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

  Sales()

  Receipts()

  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"),2)
  bal_R = round(filesum([Balance_Due],[SalesAnalysis]="R"),2)
  bal_S = round(filesum([Balance_Due],[SalesAnalysis]="S"),2)
  bal_T = round(filesum([Balance_Due],[SalesAnalysis]="T"),2)
  bal_W = round(filesum([Balance_Due],[SalesAnalysis]="W"),2)
  bal_Y = round(filesum([Balance_Due],[SalesAnalysis]="Y"),2)
  bal_X = round(filesum([Balance_Due],[SalesAnalysis]="X"),2)
END FUNCTION ' Calc_Bals()


FUNCTION Stk_Bals()
' local y1 y2 y3 y4 y5 y6 $list
' while true
  progress(15,10," Please wait .... searching stock list ",0)
  vloadif(dpath|"stk_carp.vws")
  data utilities recalc-all
'   y1="ÿÿÿÿShowÿTOTALÿonly"
  #total = filesum([Balance_Value])
  #precs = precords
'   screen clear box 1 1 sch scw 0 0 no-border
'   messboxwait(fixed(#precs,0)&"items totalling"&currency(#total)|"ÿ",0,0,1)
END FUNCTION ' Stk_Bals()





