'COSTINGS - prepares reports of breakdown of req'ns

external  dpath sch scw ChooseBranch() PrintReport() vloadif() progress()
external  remove() fentrybox() chkdate() messbox() cpath popuplist()
external  messboxwait() vunloadif() fgi bgi progtag()

public    ptstr from_date last_m_e mthsales $mth $yr mthcash mthreqn dsa
public    $branch

global    x br_name mth ld ReturnToMenu() Sales() Receipts() Reqns()
global    $output $current1 $current2


MAIN
single-step off
  file unload all

while true
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  br_name = ChooseBranch(5,25,1)         'message "br_name is:"&str(br_name)
  if br_name = -1
    exit while
  elseif br_name = "All"
    messboxwait(" Cannot run this process for ALL shops ",0,0,1)
    continue while
  end if
  $branch = left(br_name,1)            ' message "$branch is:"&str($branch)
  screen shortrestore dsa

' choose period
  x = popuplist(5,38,21,"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec","",1,0)
  if x = 0
    $mth = ptstr
  else
    continue while
  end if
  screen shortrestore dsa
  x = popuplist(5,45,13,"1995 1996 1997 1998 1998","",1,0)
  if x = 0
    $yr = ptstr
  else
    continue while
  end if
  from_date = date2("01-"|$mth|"-"|right($yr,2))
'   from_date = date2(from_date)               '
  screen shortrestore dsa

  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

'   Sales()
'
'   Receipts()

  Reqns()

  $output = $branch|"_R_"|left(mth,2)|right(mth,2)|".txt"
' message "$output is:"&str($output)
  remove($output)
  remove("current.txt")

  data cross-tab execute "mthreqns.dfw" smart "current.txt"

' write cash & sales to file
  fopen "current.txt" as 1
  fread 1 into $current1
  fread 1 into $current2
  fclose 1

' message "mthcash is:"&str(mthcash)
  fopen $output as 2
  fwrite 2 from $current1
  fwrite 2 from $current2
  fwrite 2 length 1 from chr(34)
  fwrite 2 length 5 from "SALES"
  fwrite 2 length 1 from chr(34)
  fwrite 2 length 1 from chr(32)
  fwrite 2 from mthsales
  fwrite 2 length 1 from chr(34)
  fwrite 2 length 4 from "CASH"
  fwrite 2 length 1 from chr(34)
  fwrite 2 length 1 from chr(32)
  fwrite 2 from mthcash
  fclose 2

  file unload all
  messboxwait(" Details written to file"&$output,0,0,1)

end while

  ReturnToMenu()

END MAIN


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(from_date) > days(last_m_e)   ' YES - return 1
    return (1)
  else
    return (0)                         ' NO  - return 0
  end if
END FUNCTION ' CheckMonth()


FUNCTION Sales()
local q
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  progress(15,10," Selecting Customer's orders ",0)
  vloadif(dpath|"cust_ord.vws")
  data query execute "mthsales.dfq" index "mthsales.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days(date2([Date_Of_Order]))>=days(date2(from_date))
'   and
'   days(date2([Date_Of_Order]))<=days(date2(last_m_e))
'   and
'   [SalesAnalysis]=$branch
'   and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No sales orders in current file for"&mth,0,0,1)
    mthsales = "NONE"
    return(0)
  end if
  mthsales = round(filesum([Net_Invoice]),2) 'message "mthsales is:"&str(mthsales)
END FUNCTION ' Sales()


FUNCTION Receipts()
' local postcash
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  progress(15,10," Selecting Cash Receipts ",0)
  vloadif(dpath|"cashrcvd.vws")
  order change key "[Date_Of_Receipt]"
  data query execute "mthcash.dfq" index "mthcash.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days(date2([Date_Of_Receipt]))>=days(date2(from_date))
'   and
'   days(date2([Date_Of_Receipt]))<=days(date2(last_m_e))
'   and
'   [SalesAnalysis]=$branch
'   and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No cash receipts in current file for"&mth,0,0,1)
    mthcash = "NONE"
    return(0)
  end if
  mthcash = round(filesum([Amount_Received]),2)  'message "mthcash is:"&str(mthcash)
END FUNCTION ' Receipts()


FUNCTION Reqns()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  progress(15,10," Selecting Requisitions ",0)
  vloadif(dpath|"requsn.vws")
  data query execute "mthreqns.dfq" index "mthreqns.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   days(date2([Date_Requisitioned]))>=days(date2(from_date))
'   and
'   days(date2([Date_Requisitioned]))<=days(date2(last_m_e))
'   and
'   [Branch]=$branch
'   and not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No req'ns in current file for"&mth,0,0,1)
    mthreqn = "NONE"
    return(0)
  end if
'   mthcash = round(filesum([Amount_Received]),2)  'message "mthcash is:"&str(mthcash)
END FUNCTION ' Reqns()


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()


