'FTR_PAYT - reviews FTR_PAYT records for a fitter and then makes out Remittance
'           advice for fitter. A/c's are posted from this printout

'N.B.       [Balance_Status] - shows if payment is held because Balance NOT
'           paid unless ACCOUNT customer.
'N.B.       [Action]         - shows if payment is held thru' mgt choice.

' 0 - select fitter
' 1 - query for FTR_PAYT records where [Released]-[Paid]>.01
' 2 - print out report
' 3 - review report and if correct print "self-billing" invoice (on headed?
'     paper) and to file and alter [Paid] to equal [Released]


external  cpath dpath Background() vloadif() vunloadif() bpopdb() popuplist()
external  messboxwait() fgp bgp navrecs() messline() PrintReport() messbox()
external  fentrybox() chkdate() progress() remove()
external   X_path _SWIP_Crystal() Xreppath
'  ftrspath

public    ptstr dsa ftrcode $nickname ptval prepdate $name $accscode c4h

global    x ChooseFitter() ReturnToMenu() ProcessFitter() ppl ProcessRelease()
global    PrintPayts() i CheckJobs2Date() ProcessPayment_A() pf
global    Check4Held() upd StoreReport() p1 p2 p3 p4 p5 p6


MAIN
single-step off
  file unload all
  Background()

  x = CheckJobs2Date()
  if x = -1
    ReturnToMenu()
  end if

  x = ChooseFitter("a")            ' "a" for ALL; "v" for ACTIVE only
'     if x = -1
'       exit while
'     end if
'   end while

  ReturnToMenu()

END MAIN


FUNCTION CheckJobs2Date()
local f
  f = case dayname(today)("Monday",1)("Tuesday",2)("Wednesday",3)("Thursday",4)("Friday",5)("Saturday",6) else 0
  prepdate = date2(days(today)-f)      'message "prepdate is:"&str(prepdate)
  while true
    x = fentrybox(" Date for preparing Fitter's payments upto ",10,"##\/##\/####",date2(prepdate))
    if x = -1
      return (-1)
    end if
    prepdate = ptstr
    if chkdate(prepdate,1) = -1
      messbox(" Incorrect date - re-enter ",0,0,1)
      continue while
    end if
    exit while
  end while
END FUNCTION ' CheckFitDate()


FUNCTION ProcessRelease()
local accstat custcode balos $action
  vloadif(dpath|"customer.vws")
  vloadif(dpath|"ftrpayt1.vw")
  order change index "ftrpayt2.idx"

  for i = 1 to records                 ' update [Balance_Status]
    custcode = [Customer_Code]
    accstat  = filelookup([customer.Customer_Code],[customer.Credit_Status],custcode)
' message "accstat is:"&str(accstat)
    balOS    = [Balance_Due]
    case accstat                       '[Balance_Status] is "C" clear to pay ftr
      when  "A"    ' Retail ACCOUNT    ' or "U"ncleared NOT to pay ftr
        lock-record
          [Balance_Status]="C"
        write-record
      when  "N"    ' Trade ACCOUNT
        lock-record
          [Balance_Status]="C"
        write-record
      when  "D"    ' Delayed
        lock-record
          [Balance_Status]=@if(balOS>1,"U","C")
        write-record
      when  "C"    ' Retail CASH
        lock-record
          [Balance_Status]=@if(balOS>1,"U","C")
        write-record
      when  "T"    ' Trade CASH
        lock-record
          [Balance_Status]=@if(balOS>1,"U","C")
        write-record
    end case
    data goto record next
  end for

  data goto record first
  for i = 1 to records
    custcode = [Customer_Code]
    accstat  = filelookup([customer.Customer_Code],[customer.Credit_Status],custcode)
    balOS    = [Balance_Due]
' message "[Action] is:"&str([Action])
' message "[Balance_Status] is:"&str([Balance_Status])
    if [Action]="H" or [Balance_Status]="U"
      if balOS<1
        lock-record
          [Action] = blank
          [Released] = [Amount]
          [Address]  = [Delivery_Address_1]
          [Notes]    = blank
        write-record
      else
        lock-record
          [Released] = blank
        write-record
      end if
      data goto record next
      continue for
    end if

    case accstat
      when  "A"                        ' [Released] = [Amount]
        lock-record
          [Released] = [Amount]
          [Address]  = [Delivery_Address_1]
          [Notes]    = blank
        write-record

      when  "N"                        ' [Released] = [Amount]
        lock-record
          [Released] = [Amount]
          [Address]  = [Delivery_Address_1]
          [Notes]    = blank
        write-record

      when  "D"                        ' [Released] = [Amount]
        lock-record
          [Released] = [Amount]
          [Address]  = [Delivery_Address_1]
          [Notes]    = blank
        write-record

      when  "C"                        ' [Released] = [Amount] when Bal_OS = 0
' $action = @if(balOS>1,"H",blank)
' message "$action is:"&str($action)
        lock-record
          [Released] = @if(balOS>1,0,[Amount])
          [Address]  = [Delivery_Address_1]
          [Notes]    = @if(balOS>1,"Balance o/s"&currency(balOS),blank)
          [Action]   = @if(balOS>1,"H",blank)
        write-record

      when  "T"                        ' [Released] = [Amount] when Bal_OS = 0
        lock-record
          [Released] = @if(balOS>1,0,[Amount])
          [Address]  = [Delivery_Address_1]
          [Notes]    = @if(balOS>1,"Balance o/s"&currency(balOS),blank)
          [Action]   = @if(balOS>1,"H",blank)
        write-record

    end case
    data goto record next
  end for
  vunloadif("ftrpayt1.vw")
END FUNCTION ' ProcessRelease()


FUNCTION ReturnToMenu()
  Background()
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION ChooseFitter(t)
  vloadif(dpath|"ftr_list.vws")
  if t = "v"                           'active fitters
    data query execute "actv_ftr.dfq" index "actv_ftr.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Active]="YES"
'   and
'   not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  else
    data query execute "not_del.dfq" index "actv_ftr.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  end if
  order sort now dictionary "nickname" fields "[Nickname]" ascending
  while true
    x = bpopdb("ftr_list",5,"","[Nickname]","L8","[Fitter_Name]","L0","[Fitter_Code]",5,35,21,45,"",0)
    if x = -1
      repaint off
      vunloadif("ftr_list.vws")
      return(-1)
    else
      ftrcode   = ptstr                'message "ptstr is:"&str(ptstr)
      $name     = [Fitter_Name]        'message "$name is:"&str($name)
      $nickname = [Nickname]
      $accscode = [ACC_code]
      pf=ProcessFitter()                'message "pf) is:"&str(pf)
      if pf = 1
        Background()
        vloadif(dpath|"ftr_list.vws")
        continue while
      end if
      c4h=Check4Held()                  'message "1=some HELD; c4h is:"&str(c4h)
      if c4h = 1                         ' some HELD, print report
        messbox(" Do you want to print the HELD Payments for"&$nickname&"? (y/n) ",1,1,1)
        if ptstr == "n"
          vloadif(dpath|"ftr_list.vws")
          continue while
        else                          ' find HELD payments for "ftrcode"
          vloadif(dpath|"preppayt.vw")
          order change index "ftrpayt4.idx"
          while true
            vunloadif("X_ftrpyt.vws")
            remove(X_path|"X_ftrpyt.*")
            data query execute "not_del.dfq" Smart4 X_path|"X_ftrpyt" fields "[JobNr|acc_code]"
            _SWIP_Crystal(Xreppath|"X_hldpyt","P",0,1,"")
            messbox(" Has the HELD Payment List for"&$nickname&"printed? (y/n) ",1,1,1)
            if ptstr == "y"
              vloadif(dpath|"preppayt.vw")
              order change index "ftrpayt4.idx"
              exit while
            end if
            vloadif(dpath|"preppayt.vw")
          end while
        end if
      end if
      Background()
      vloadif(dpath|"ftr_list.vws")
    end if
  end while
  repaint off
  screen shortrestore dsa
  vunloadif("ftr_list.vws")
  return (-1)
END FUNCTION  ' ChooseFitter()


FUNCTION ProcessFitter()
local recnr jobseq ord bl m1
  Background()
  progress(15,10," Finding outstanding payments for"&$nickname&" ",0)
  vloadif(dpath|"ftr_payt.vws")
  order change key "[Ftr_Code]"
  data query execute "ftrpayt2.dfq" index "ftrpayt1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Ftr_Code]=ftrcode
' and
' not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait("(265) No records found for"&$nickname,0,0,1)
    upd = 1
    return (1)
  end if

  data query execute "ftrpayt4.dfq" index "ftrpayt5.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' abs(([Released]-[Paid]))>.01
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait("(275) No unpaid records found for"&$nickname,0,0,1)
'     messboxwait(" No unpaid records found for"&$nickname,0,0,1)
    order change physical
    upd = 1
    return (1)
  end if

  order sort now dictionary "ftrpayt2.idx" fields "[JobNr]" ascending

  x = ProcessPayment_A()
'   if x = 1                             'if not printed then remove [Released]
'
'   end if
END FUNCTION ' ProcessFitter()


FUNCTION  ProcessPayment_A()
  vloadif(dpath|"preppayt.vw")
  order change index "ftrpayt2.idx"
  PrintPayts()
'   return(x)
END FUNCTION ' ProcessPayment_A()

' "ftrpayt1.idx" - ALL undeleted records for this fitter
' "ftrpayt5.idx" - ALL unpaid records for this fitter
' "ftrpayt2.idx" - ALL unpaid records for this fitter sorted by JobNr
' "ftrpayt4.idx" - ALL unpaid HELD records for this fitter sorted by JobNr

FUNCTION Check4Held()            ' return 1 for some HELD; 0 for NONE HELD
  order change index "ftrpayt1.idx"
  data query execute "heldpayt.dfq" index "fp4.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [Action]="H"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
'     messboxwait(" No payments to make for"&$nickname,0,0,1)
    return (0)
  else
    order sort now dictionary "ftrpayt4.idx" fields "[JobNr]" ascending
    return (1)
  end if
END FUNCTION ' Check4Held()


FUNCTION PrintPayts()
' "ftrpayt1.idx" - ALL undeleted records for this fitter
' "ftrpayt5.idx" - ALL unpaid records for this fitter
' "ftrpayt2.idx" - ALL unpaid records for this fitter sorted by JobNr
' "ftrpayt4.idx" - ALL unpaid HELD records for this fitter sorted by JobNr
' screen loaded is: vloadif(dpath|"preppayt.vw")

  repaint off
  if upd = 0                            ' unpaid records exist
    order change index "ftrpayt2.idx"
    order sort now dictionary "ftrpayt3.idx" fields "[ViewDesc]" ascending

    vunloadif("X_ftrpyt.vws")
    remove(X_path|"X_ftrpyt.*")
    data query execute "not_del.dfq" Smart4 X_path|"X_ftrpyt" fields "[JobNr|acc_code]"
    _SWIP_Crystal(Xreppath|"Xftrpyt1","P",0,1,"")

'   _SWIP_Crystal(Xreppath|"$inv_1","EP",0,1,invpath|$nextinvnr|".pdf")

    messbox(" Has the draft Payment List for"&$nickname&"printed? (y/n) ",1,1,1)
    if ptstr == "n"
      messboxwait(" Amend the payments and run this program again ",0,0,1)
      vloadif(dpath|"preppayt.vw")
      return (0)
    end if

    _SWIP_Crystal(Xreppath|"Xftrpyt2","P",0,1,"")
    messbox(" Has the final Payment List for"&$nickname&"printed? (y/n) ",1,1,1)
    if ptstr == "n"
      messboxwait(" Amend the payments and run this program again ",0,0,1)
      vloadif(dpath|"preppayt.vw")
      return (0)
    end if

    vloadif(dpath|"preppayt.vw")
    data goto record first
    while record <= records
      lock-record
'        [Paid]      = [Paid]+[Released]
        [Paid]      = [Released]
'        [Released]  = 0
        [Date_Paid] = today
        [Notes]     = blank
      write-record
      data goto record next
    end while

'     StoreReport(p1,ext)
  end if
END FUNCTION ' PrintPayts()


FUNCTION StoreReport(p1,ext)   ' ext = FTR_CODE and Date i.e. "AAA001dd.mmy"
local ftrreps repdate
message "prepdate is:"&str(prepdate)
repdate = left(date2(prepdate),2)
message "repdate is:"&str(repdate)
message "ftrcode is:"&str(ftrcode)

'   ftrreps = ftrspath|ftrcode|repdate|"."|

message "ftrreps is:"&str(ftrreps)

  print report execute p1 disk ftrreps detail start 1 end 0 copies 1
END FUNCTION ' StoreReport()
'   remove(X_path|"X_ftrpyt.*")
'
'   vunloadif("X_ftrpyt.vws")
'   data query execute "not_del.dfq" data-file X_path|"X_ftrpyt" fields\
'   "[Var_Nr;Date;CustOrd_Name;Amount_Gross;shop;Date_To;Nr_Orders;Avg_Order;Tot_Order_Net;Tot_Order_Gross]"
'   _SWIP_Crystal(Xreppath|"ftrpayt_1","P",0,1,"")
' '   _SWIP_Crystal(Xreppath|"$inv_1","EP",0,1,invpath|$nextinvnr|".pdf")


