      program ApEncumberCls
***
* Abstract:
*    This program will be used to create a manual close of an
* encumbrance via a TC71 transaction to ARMS. Via the ENCUMBER.TICKLE
* file.
*
***
* R e v i s i o n   L o g
* Who..... When.... Why..................................................
* Andrew   09Nov92  Initial coding.
*
***

$INCLUDE PGM.FDEF ENCUMBRANCE.FDEF

      deffun Maxs(a, b)

      equ True to 1
      equ False to 0
      equ Others to 1
      prompt ''

      BatchDate = oconv(DATE(),"D2/")
      iBatchDate = DATE()
      convert "/" to "" in BatchDate

* The following code will determine when the next fiscal end and start
* dates will occurr.
      fYear = BatchDate[2]
      CriticalDate = "07/03/":fYear
      iCriticalDate = iconv(CriticalDate,"D")
      if iBatchDate ge iCriticalDate then fYear += 1
      FiscalStart = "07/01/":fYear
      FiscalEnd   = "06/30/":fYear
      iFiscalStart = iconv(FiscalStart,"D")
      iFiscalEnd = iconv(FiscalEnd,"D")
      convert "/" to "" in FiscalStart
      convert "/" to "" in FiscalEnd

      equ NoHelp to 0
      Help = ""
      equ FirstHelp to 1
      Help<FirstHelp> = "Enter '?' for help."
      equ PromptHelp to 2
      Help<PromptHelp> = "Enter encumbrance number as it exists in ARMS."
      equ BadEntryHelp to 3
      Help<BadEntryHelp> = "Invalid entry. Encumbrance# must be of the format NNNNNNN or NNNNNNNA"
      equ InvalidCmdHelp to 4
      Help<InvalidCmdHelp> = "Invalid command. Please enter Encumbrance#, 'FILE', 'EXIT', or 'DELETE'."
      equ InvalidPOHelp to 5
      Help<InvalidPOHelp> = "Encumbrance number selected was not found on PO file."
      equ DuplicateHelp to 6
      Help<DuplicateHelp> = "Encumbrance number has already been selected."
      equ InvalidLIHelp to 7
      Help<InvalidLIHelp> = "Line number selected is invalid or out of range."

      OpenErrors = ""
      open "","GARB" to GarbFile else OpenErrors := "GARB "
      open "","ENCUMBRANCE" to EncFile else OpenErrors := "ENCUMBRANCE "
      open "","ENCUMBER.TICKLE" to TickleFile else OpenErrors := "ENCUMBER.TICKLE "
      openseq "DATA-CR","GLEEMSTRI.M" to CurrentMasterFile else
         if status() then OpenErrors := "ARMSPA>DATA-CR>GLEEMSTRI.M "
      end
      openseq "DATA-PR","GLEEMSTRI.M" to PriorMasterFile else
         if status() then OpenErrors := "ARMSPA>DATA-PR>GLEEMSTRI.M "
      end
      if OpenErrors then
         crt "Unable to open the following files:"
         crt OpenErrors
         stop
      end

      POList = ""
      gosub BackgroundScreen
      crt PR:
      HelpNo = FirstHelp
      gosub HelpMe
      Again = False
      loop
         crt PR:
         input Response
         Response = trim(upcase(Response))
         Cmd = field(Response," ",1)
         Arg = field(Response," ",2)
         begin case
            case len(Cmd) eq 0
               HelpNo = BadEntryHelp
               gosub HelpMe
            case Cmd eq "?"
               HelpNo = PromptHelp
               gosub HelpMe
            case Cmd eq "FILE"[1, Maxs(2,len(Cmd))]
               exit
            case Cmd eq "DELETE"[1,Maxs(2,len(Cmd))]
               if num(Arg) then
                  if (Arg ge 1) and (Arg le dcount(POList,@FM)) then
                     del POList<Arg>
                     gosub DisplayList
                  end else
                     HelpNo = InvalidLIHelp
                     gosub HelpMe
                  end
               end else
                  HelpNo = InvalidLIHelp
                  gosub HelpMe
               end
            case Cmd eq "EXIT"[1,Maxs(2,len(Cmd))]
               stop @(-1)
            case alpha(Cmd)
               HelpNo = InvalidCmdHelp
               gosub HelpMe
            case Cmd matches "7N":@VM:"7N1A"
               Alpha = Cmd[1 * alpha(Cmd[1])]
               readv Suffixs from EncFile, Cmd[1,7], ENC$SUFFIX then
                  locate Alpha in Suffixs<1,-1> setting dummy else
                     HelpNo = InvalidPOHelp
                     gosub HelpMe
                     continue
                  end
               end else
                  HelpNo = InvalidPOHelp
                  gosub HelpMe
                  continue
               end
               locate Cmd in POList<1> setting Idx then
                  HelpNo = DuplicateHelp
                  gosub HelpMe
                  continue
               end
               ins Cmd before POList<Idx>
               gosub DisplayList
               HelpNo = NoHelp
               gosub HelpMe
            case Others
               HelpNo = BadEntryHelp
               gosub HelpMe
         end case
      repeat
      gosub CloseEncumbrances
      return

BackgroundScreen:
      BS = @(-1):"ApEncumberCls"
      Title = "Manual Close Encumbrance"
      BS := @((80-len(Title))/2):Title
      BS := @(0,1):str("-",80)
      CS = ""
      for i = 0 to 19
         Col = if (i lt 10) then 10 else 50
         BS := @(Col, 7+mod(i,10)): fmt(i+1,"R##.")
         CS := @(Col+3, 7+mod(i,10)): space(10)
      next i
      BS := @(0,22):"Enter PO# - FILE when done - EXIT to leave - DELETE to backup"
      PR = @(10,21):"Entry: ":@(-4)
      crt BS:
      return

DisplayList:
      Savei = i
      DS = ""
      crt CS:
      for i = 0 to 19
         Col = if i lt 10 then 14 else 54
         DS := @(Col, mod(i,10)+7):POList<i+1>
      next i
      crt DS:
      i = Savei
      return

HelpMe:
      if HelpNo then
         crt @(0,23):Help<HelpNo>[1,79]:@(-4):
      end else
         crt @(0,23):@(-4):
      end
      return

CloseEncumbrances:
      MorePO = POList ne ""
      loop while MorePO do
         remove PO from POList setting MorePO
         gosub ProcessPO
      repeat
      return

ProcessPO:
      readv Suffixs from EncFile, PO, ENC$SUFFIX else return
      MoreSuffix = Suffixs ne ""
      loop
         remove Suffix from Suffixs setting MoreSuffix
         gosub FindNextMaster
         if not(Found) then continue
         gosub BuildTC71
      while MoreSuffix
      repeat
      return

FindNextMaster:
      Found = False
      Prior = False
      loop
         readseq MasterRec from CurrentMasterFile else
            exit
         end
         if MasterRec[5,7] eq PO:Suffix then
            Found = True
            return
         end
         if MasterRec[5,7] gt PO then exit
      repeat
      loop
         readseq MasterRec from PriorMasterFile else
            exit
         end
         if MasterRec[5,7] eq PO:Suffix then
            Found = True
            Prior = True
            return
         end
         if MasterRec[5,7] gt PO then exit
      repeat
      if not(Found) then
         crt "Data integrity error. Encumbrance number ":PO:Suffix:" not found on"
         crt "current or prior year GLEEMSTRI.M files, and it should recide in at least one."
         crt "Skipping to next encumbrance number.  (apencumbrancecls)"
      end
      return

BuildTC71:
      gosub GetTickleKey
      TC71 = space(128)
      TC71[ 1, 2] = "71"
      TC71[ 3, 6] = BatchDate
      TC71[ 9, 4] = "MAPS"
      TC71[15, 1] = "D"
      TC71[16, 6] = MasterRec[13, 6]
      TC71[22, 5] = MasterRec[52, 5]
      TC71[27, 8] = PO:Suffix
      TC71[35,11] = (str("0",11):oconv(MasterRec[90, 6], "PD11"))[11]
      TC71[46, 6] = if Prior then FiscalEnd else BatchDate
      TC71[52, 1] = "C"
      TC71[53, 1] = if Prior then "P" else " "
      TC71[54,12] = MasterRec[60,12]
      TC71[66,25] = MasterRec[276,25]
      write TC71 to TickleFile, TickleKey
      crt "Encumbrance ":PO:Suffix:" scheduled for closure. (apencumbercls)"
      return

GetTickleKey:
      readu TickleKey from GarbFile, "NEXT.TICKLE.KEY" else
         TickleKey = 1
      end
      write TickleKey+1 to GarbFile, "NEXT.TICKLE.KEY"
      return

   end
