'PCSH_PRC - Head Office; produces summaries of unpaid petty cash for each shop;
'           processes list to show it is paid and creates text file for reading
'	    into SUN Accounts

'090806 - temp. entry for title of report to clear old Petty Cash returns
'090806 - create backup file of SUN tfr file

external   vkeybox() sch scw progress() messbox() vloadif() base $menu userid
external   vunloadif() dpath fentrybox() chkdate() areas greeting cpath $drive
external   popuplist() fgp bgp ChooseBranch() remove() Background() vatrate
external   _SWIP_Crystal() Xreppath X_path bpopdb() increment() messboxwait()

public     ptstr ptval #total #vattotal #net #value z $rephdr
public     $shop_name $shop_init $pcref

global     x ReturnToMenu() Report() nr ProcessPayment() i SUN_File()
global     #lines $filename $today a baccpath $bxtfile
global     accpath WriteLine() acc_code acc_period thisday $lineval $dr_cr
global     $jnl_type $operator $jnl_ref $jnl_desc $entry_date $entry_period
global     blank1 fixed1 y $line_end $value $jnl_nr $jnl_line CalcVal()
global     $due_date SetConstants() $hdr $txtfile acctgperiod ChooseSUNAccgPeriod()

MAIN
  single-step off
  Background()
  file unload all

  ChooseSUNAccgPeriod()			'enter SUN accounting period

  while true
    x = fentrybox(" !!TEMP!! - Enter Petty Cash reference ",7,"","")
    if x = -1
      continue while
    end if
    $pcref = upper(ptstr)
    exit while
  end while


  while true
    x = popuplist(8,33,13,"HeadÿOffice Fulham Sheen Raynes Putney","",1,0)
    if x = -1
      ReturnToMenu()
    end if
    $shop_init = upper(left(ptstr,1))		'message "$shop_init) is:"&str($shop_init)
    $shop_name = ptstr				'message "$shop_name is:"&str($shop_name)

'     while true
'       x = fentrybox(" !!TEMP!! - Enter Petty Cash reference ",7,"","")
'       if x = -1
'         continue while
'       end if
'       $rephdr = $shop_init|upper(ptstr)
'       exit while
'     end while
    $rephdr = $shop_init|$pcref		'message "$rephdr is:"&str($rephdr)

    x = Report()			' listing of P/cash NOT re-imbursed
    if x=-1
      continue while
    elseif x=0				' draft correct
'     vloadif(dpath|"pcshproc.vw")		'message "temp file"
      vloadif(dpath|"$pcshprc.vw")		'
'       vloadif(dpath|"pcshproc.vw")		'
'       vloadif(dpath|"pcsh_ent.vws")		'
      order change index "pcash1.idx"
      #lines=records				'message "#lines is:"&str(#lines)
      file unload all
'     vloadif(dpath|"pcshproc.vw")		'message "temp file"
    vloadif(dpath|"$pcshprc.vw")		'

'       vloadif(dpath|"pcsh_ent.vws")		'
      order change index "pcash1.idx"
      #total=filesum([Gross Amount])		'message "#total is:"&str(#total)
      #vattotal=fixed(filesum([VAT]),2)		'message "#vattotal is:"&str(#vattotal)

      messbox(" Confirm total on summary is"&str(currency(#total))&"? (y/n) ",1,1,1)
      if ptstr == "n"
        messboxwait(" Totals do not agree - resolve problem and retry ",0,0,1)
        exit while
      end if

      messbox(" Continue and process these payments? (y/n) ",1,1,1)
      if ptstr == "y"
        ProcessPayment()
      else
        continue while
      end if

    end if
  end while

  ReturnToMenu()

END MAIN


FUNCTION ReturnToMenu()
  Background()
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION Report()
  messbox(" Print list of unpaid petty cash for"&$shop_name&"? (y/n) ",1,1,1)
  if ptstr == "n"
    return (-1)
  else				'message "print list of unpaid items"

'     vloadif(dpath|"pcshproc.vw")		'message "temp file"
    vloadif(dpath|"$pcshprc.vw")		'
'     vloadif(dpath|"pcsh_ent.vws")
    order change physical
    data query execute "pcshrep1.dfq" index "pcash1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Shop]=$shop_init
' and
' not (deleted)
' and
' [ProcessDate]=blank
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    if cerror
      messboxwait(" No unpaid Petty Cash items for"&$shop_name,0,0,1)
      return (1)
    else
      remove(X_path|"Xpcshlst.*")
      data query execute "not_del.dfq" Smart4 X_path|"Xpcshlst" fields "[Date;ShopName;Shop;Ref_Nr;Category;SunCode;Narrative;VAT;Gross Amount;ProcessDate;FileNr]"
      vunloadif("Xpcshlst.vws")
' message "report rem'd out"
      _SWIP_Crystal(Xreppath|"pcsh_lst","P",0,1,"")
    end if
  end if
  return (0)
END FUNCTION 'Report()


FUNCTION ProcessPayment()
  file unload all

'     vloadif(dpath|"pcshproc.vw")		'message "temp file"
    vloadif(dpath|"$pcshprc.vw")		'

'   vloadif(dpath|"pcsh_ent.vws")		'
  order change index "pcash1.idx"

' message "write rem'd out"
    for i = 1 to records
      lock-record                        ' update record with inv nr & date
        [ProcessDate]=today
      write-record
      data goto record next
    end for

'print file copy of report - ref nr on report will be shopinit & date (e.g. H040806)
' message "report rem'd out"
  _SWIP_Crystal(Xreppath|"pcshproc","P",0,1,"")

  $hdr=[filenr]				'message "$hdr is:"&str($hdr)

  SUN_File()				'create text file for posting in SUN

END FUNCTION 'ProcessPayment()


FUNCTION SetConstants()
' message "thisday is:"&str(thisday)
  blank1	="  "
  fixed1	="N"
  $jnl_nr	="0000000"
  $jnl_line	="00000"
  $jnl_type	="PETC "
  $operator	="JDC"
  $today	=left(today,2)|mid(today,4,2)|right(today,4)
  $jnl_ref	=left("pe"&$today|"             ",13)	'message "$jnl_ref) is:"&str($jnl_ref)
  $entry_date	=thisday
  $entry_period	=acc_period
  $due_date	="        "
  $line_end="      000000000000000000000000                     000000000000000000000000000000000000"
END FUNCTION 'SetConstants()


FUNCTION ChooseSUNAccgPeriod()
local y ld
  while true
    acctgperiod = right(date2(days(today)-30+240),7)
    x = fentrybox(" Enter SUN accounting period ",7,"*2#\/*4#",acctgperiod)
    if x = 0
      acc_period=right(ptstr,4)|"0"|left(ptstr,2) 'message "acc_period is:"&str(acc_period)
      return (0)
    else
'       return (-1)
    end if
  end while
END FUNCTION ' ChooseSUNAccgPeriod()


FUNCTION WriteLine(acc_code,acc_period,thisday,blank1,fixed1,$jnl_nr,$jnl_line,blank1,$lineval,$dr_cr,blank1,$jnl_type,$operator,blank1,$jnl_ref,blank1,$jnl_desc,$entry_date,$entry_period,$due_date,$line_end)
'   $lineval=""
  fwrite 1 length 15 from acc_code
  fwrite 1 length 7 from acc_period
  fwrite 1 length 8 from thisday
  fwrite 1 length 2 from blank1
  fwrite 1 length 1 from fixed1
  fwrite 1 length 7 from $jnl_nr
  fwrite 1 length 5 from $jnl_line
  fwrite 1 length 2 from blank1
  fwrite 1 length 18 from $lineval
  fwrite 1 length 1 from $dr_cr
  fwrite 1 length 1 from blank1
  fwrite 1 length 5 from $jnl_type
  fwrite 1 length 3 from $operator
  fwrite 1 length 2 from blank1
  fwrite 1 length 13 from $jnl_ref
  fwrite 1 length 2 from blank1
  fwrite 1 length 25 from $jnl_desc
'   fwrite 1 length 2 from blank1
  fwrite 1 length 8 from $entry_date
  fwrite 1 length 7 from $entry_period
  fwrite 1 length 8 from $due_date
  fwrite 1 from $line_end

  fwrite 2 length 15 from acc_code
  fwrite 2 length 7 from acc_period
  fwrite 2 length 8 from thisday
  fwrite 2 length 2 from blank1
  fwrite 2 length 1 from fixed1
  fwrite 2 length 7 from $jnl_nr
  fwrite 2 length 5 from $jnl_line
  fwrite 2 length 2 from blank1
  fwrite 2 length 18 from $lineval
  fwrite 2 length 1 from $dr_cr
  fwrite 2 length 1 from blank1
  fwrite 2 length 5 from $jnl_type
  fwrite 2 length 3 from $operator
  fwrite 2 length 2 from blank1
  fwrite 2 length 13 from $jnl_ref
  fwrite 2 length 2 from blank1
  fwrite 2 length 25 from $jnl_desc
'   fwrite 1 length 2 from blank1
  fwrite 2 length 8 from $entry_date
  fwrite 2 length 7 from $entry_period
  fwrite 2 length 8 from $due_date
  fwrite 2 from $line_end

END FUNCTION ' WriteLine(x)


FUNCTION SUN_File()
' save using sheet ref nr (eg H040806.txt)
' structure of SUN import file
' line 1 - total amount			621
' line 2 - total VAT			822
' line 3 etc - P&L a/c items		460000 etc

'   ChooseSUNAccgPeriod()			'enter SUN accounting period

  thisday	=right(today,4)|mid(today,4,2)|left(today,2)

  accpath="T:\"      		' message "accpath is:"&str(accpath)
' message "$hdr is:"&str($hdr)
  $txtfile=$hdr|".txt"			' message "$txtfile is:"&str($txtfile)
  remove(accpath|$txtfile)

  baccpath=$drive|"reports\PET_CASH\"   'message "baccpath is:"&str(baccpath)
  $bxtfile=$hdr|".bxt"			'message "$bxtfile is:"&str($bxtfile)
  remove(baccpath|$bxtfile)

'   cplpath      = $drive|"reports\cpl_reps\"     'stores CPL reports

  SetConstants()

  fopen accpath|$txtfile as 1
  fopen baccpath|$bxtfile as 2

' write line 1				'Cr entry to post to 621
  acc_code	="621"
  acc_code	=left(acc_code|"               ",15)

  CalcVal(#total)			' message "in write line 1 - $lineval is:"&str($lineval)

  $dr_cr	="C"
  $jnl_desc	=left("Petty Cash"|"                         ",25)
  WriteLine(acc_code,acc_period,thisday,blank1,fixed1,$jnl_nr,$jnl_line,blank1,$lineval,$dr_cr,blank1,$jnl_type,$operator,blank1,$jnl_ref,blank1,$jnl_desc,$entry_date,$entry_period,$due_date,$line_end)

' write line 2				'Dr entry to post to 822
  acc_code	="822"
  acc_code	=left(acc_code|"               ",15)

  CalcVal(val(#vattotal))		' message "in write line 2 - $lineval is:"&str($lineval)
  $dr_cr	="D"
  $jnl_desc	=left("Petty Cash"|"                         ",25)
  WriteLine(acc_code,acc_period,thisday,blank1,fixed1,$jnl_nr,$jnl_line,blank1,$lineval,$dr_cr,blank1,$jnl_type,$operator,blank1,$jnl_ref,blank1,$jnl_desc,$entry_date,$entry_period,$due_date,$line_end)

  data goto record first
  for i=1 to #lines			' write P&L code lines
    acc_code	=[SunCode]
    acc_code	=left(acc_code|"               ",15)	'message "acc_code is:"&str(acc_code)
    #net	=fixed(([Gross Amount]-[VAT]),2)	'message "#net is:"&str(#net)
    CalcVal(val(#net))			' message "in write PL lines - $lineval is:"&str($lineval)
    $dr_cr	="D"
    if len([Narrative])=0
      $jnl_desc	=left("Petty Cash"|"                         ",25)
    else
      $jnl_desc	=left([Narrative]|"                         ",25)
    end if
    WriteLine(acc_code,acc_period,thisday,blank1,fixed1,$jnl_nr,$jnl_line,blank1,$lineval,$dr_cr,blank1,$jnl_type,$operator,blank1,$jnl_ref,blank1,$jnl_desc,$entry_date,$entry_period,$due_date,$line_end)
    data goto record next
  end for
  fclose 1
  fclose 2
END FUNCTION 'SUN_File()


FUNCTION CalcVal(z)
  $value=str(z*1000)		'
  $lineval=right("000000000000000000"|$value,18)	' message "in CalcVal $lineval is:"&str($lineval)
END FUNCTION ' CalcVal()

