      program ApEncumberRlv

***
* Abstract:
*    This program will be used to interface to the ENCUMBRANCE portion
* of the GL system. Every record of the ENCUMBER.TICKLE file will be
* written out to APENCDLY.D. Input to the ENCUMBER.TICKLE file is from
* the AP.ENCUMBER.CLS and AP.ENCUMBER.CLS.SUB programs. Output is to
* the APENCDLY.D file and is sequentially appended to the end in case
* there is still old business that has not been transfered yet.
*
***
* R e v i s i o n   L o g
* Who..... When.... Why..................................................
* Andrew   10/26/92 Initial coding.
*
***

      equ True to 1
      equ False to 0
      equ Others to 1

      TotalRecords = 0
      TotalDollars = 0.00

      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")

      OpenErrors = ""
      open "","ENCUMBER.TICKLE" to TickleFile else
         OpenErrors := "ENCUMBER.TICKLE "
      end
      open "","GARB" to GarbFile else OpenErrors := "GARB "
      if OpenErrors then
         crt "Unable to open the following files:"
         crt OpenErrors:"(ApEncumberRlv)"
         stop
      end

      openseq "&UFD&","APENCDLY.D" to SeqFile else
         if status() ne 0 then
            stop "Unable to open the APENCDLY.D file."
         end
      end
      gosub SeekEOF

      read NextBatchRec from GarbFile, "NEXT.ENCUMBER.BATCH" else
         NextBatchRec = ""
      end
*** If the date that the batch sequence number was reset is less than
*** the first of this month then reset it and the date.
      FirstDay = oconv(date(),"D2-")
      FirstDay[4,2] = "01"
      FirstDay = iconv(FirstDay,"D")
      if NextBatchRec<2> lt FirstDay then
         NextBatchRec = 1:@FM:date()
      end
      write NextBatchRec to GarbFile, "NEXT.ENCUMBER.BATCH"
      BatchSequence = NextBatchRec<1>

      execute "SELECT ENCUMBER.TICKLE BY TC.CODE BY ENC.NO"
      SaveTCCode = ""
      if @SYSTEM.RETURN.CODE eq 0 then stop "Aborting.  (apencumberrlv)"
      if @(0,0) then null
      crt "Starting AP.ENCUMBER.RLV.  (apencumberrlv)"

      execute "SETPTR ,,,,,,BRIEF,FORM <<F(GARB,FORM)>>,BANNER AP.ENCUMBER.RLV"
      printer on
      heading "AP.ENCUMBER.RLV -      Batch Header Report'L'"
      FirstRecord = True
      loop
         readnext TickleKey else exit
         TotalRecords += 1
         if mod(TotalRecords,10) eq 0 then crt "*":
         read TransRec from TickleFile, TickleKey else
            continue
         end
         TCCode = TransRec[1, 2]
         if TCCode ne SaveTCCode then
            if FirstRecord then
               FirstRecord = False
            end else
               gosub BuildHeader
            end
            SaveTCCode = TCCode
         end
         TransRec[13, 2] = BatchSequence
         gosub AddDollars
         writeseq TransRec to SeqFile else
            stop "Unable to write to APENCDLY.D file.  (apencumberrlv)"
         end
         delete TickleFile, TickleKey
      repeat
      gosub BuildHeader
      crt
      crt "AP.ENCUMBER.RLV complete.  (apencumberrlv)"
      return

BuildHeader:
      Hdr = SPACE(128)
      Hdr[ 1, 2] = TCCode
      Hdr[ 3, 6] = convert("/","",oconv(date(),"D2/"))
      Hdr[ 9, 4] = "MAPS"
      Hdr[13, 2] = BatchSequence
      Hdr[15, 1] = "B"
      Hdr[16, 5] = ("00000":TotalRecords)[5]
      Hdr[21,11] = (str("0",11):TotalDollars)[11]
      TotalRecords = 0
      TotalDollars = 0

      writeseq Hdr to SeqFile else
         stOpR"Uw)ble to write to APENCDLY.D file.  (apencumberrlv)"
      end
      print "Trans Code: ":TCCode, "Total Records: ":TotalRecords, "Total Amount: ":oconv(TotalDollars,"MD2$,")
      return

SeekEOF:
      loop
         readseq dummy from SeqFile else
            exit
         end
      repeat
      return

AddDollars:
      begin case
         case TCCode eq "50"
            TotalDollars += TransRec[61,11]
         case (TCCode eq "70") or (TCCode eq "71") or (TCCode eq "72")
            TotalDollars += TransRec[35,11]
      end case
      return

   end
