subroutine ApEncumberClsSub(PONo)
***
* Abstract:
*    This program will be used to create a set of close transactions
* for all encumbrances associated with the PO number passed. This
* routine assumes that the PO and VOUCHERS record have all been updated
* and written out before this routine is called.
*
* Argument List:
*    PONo        - The purchase order number
***
* R e v i s i o n   L o g
* Who..... When.... Why..................................................
* Andrew   09Nov92  Initial coding.
*
***

$INCLUDE PGM.FDEF ENCUMBRANCE.FDEF
$INCLUDE PGM.FDEF VOUCHERS.FDEF
$INCLUDE PGM.FDEF VENDORS.FDEF
$INCLUDE PGM.FDEF PO.FDEF

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

      CMasterRec = ""
      PMasterRec = ""

      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

      OpenErrors = ""
      open "","GARB" to GarbFile else OpenErrors := "GARB "
      open "","ENCUMBRANCE" to EncFile else OpenErrors := "ENCUMBRANCE "
      open "","ENCUMBER.TICKLE" to TickleFile else OpenErrors := "ENCUMBER.TICKLE "
      open "","VENDORS" to VendorsFile else OpenErrors := "VENDORS "
      open "","VOUCHERS" to VoucherFile else OpenErrors := "VOUCHERS "
      open "","PO" to POFile else OpenErrors := "PO "
      open "DATA-CR","GLEEMSTRI.M" to CurrentMasterFile else
         OpenErrors := "ARMSPA>DATA-CR>GLEEMSTRI.M "
      end
      open "DATA-PR","GLEEMSTRI.M" to PriorMasterFile else
         OpenErrors := "ARMSPA>DATA-PR>GLEEMSTRI.M "
      end
      if OpenErrors then
         crt "Unable to open the following files:"
         crt OpenErrors
         return
      end

      read PORec from POFile, PONo else
         crt "Unable to read ":PONo:" from PO file. (apencumberslssub)"
         crt "This is a data integrity error."
         return
      end
      MoreLI = PORec<PO$GL.NO> ne ""
      GLs = PORec<PO$GL.NO>
      Vouchers = PORec<PO$VOU.NO>
      Projs = PORec<PO$PROJ.NO>
      Idx = 0
      read VendorName from VendorsFile, PORec<PO$VEND.NO> else
         VendorName = "Unknown"
      end
      Processed = ""
      loop while MoreGLs do
         remove GLNo from GLs setting MoreGLs
         remove VoucherNo from Vouchers setting dummy
         remove ProjNo from Projs setting dummy
         GLProjNo = GLNo:"-":ProjNo
         locate GLProjNo in Processed<1> setting Idx else
            ins GLProjNo before Processed<Idx>
            gosub BuildTC50
         end
      repeat
      gosub ProcessPO
      return

BuildTC50:
      TC50 = space(128)
      TC50[ 1, 2] = "50"
      TC50[ 3, 6] = BatchDate
      TC50[ 9, 4] = "MAPS"
      TC50[15, 1] = "D"
      TC50[16, 8] = fmt(VoucherNo, "L#8")
      TC50[24, 6] = convert("/","",oconv(date(),"D2/"))
      TC50[30, 6] = "0":field(GLNo,"-",1)
      TC50[36, 5] = field(GLNo, "-", 2)
      TC50[41, 3] = field(GLNo, "-", 3)[1,3]
      TC50[44, 3] = "  ":field(GLNo, "-", 3)[1]
      TC50[47, 6] = ProjNo
      TC50[53, 8] = fmt(PONo, "L#8")
      TC50[72, 1] = "C"  ;* Close this puppy.
      TC50[73,25] = VendorName
      gosub GetTickleKey
      write TC50 to TickleFile, TickleKey
      return

ProcessPO:
      readv Suffixs from EncFile, PONo, 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
         if CMasterRec then
            MasterRec = CMasterRec
            CMasterRec = ""
         end else
            readseq MasterRec from CurrentMasterFile else
               exit
            end
         end
         if MasterRec[5,8] eq PONo:Suffix then
            Found = True
            return
         end
         if MasterRec[5,7] gt PONo then
            CMasterRec = MasterRec
            exit
         end
      repeat
      loop
         if PMasterRec then
            MasterRec = PMasterRec
            PMasterRec = ""
         end else
            readseq MasterRec from PriorMasterFile else
               exit
            end
         end
         if MasterRec[5,8] eq PONo:Suffix then
            Found = True
            Prior = True
            return
         end
         if MasterRec[5,7] gt PONo then
            PMasterRec = MasterRec
            exit
         end
      repeat
      if not(Found) then
         crt "Data integrity error. Encumbrance number ":PONo:Suffix:" not found on"
         crt "current or prior year GLEEMSTRI.M files, and it should reside in at least one."
         crt "Skipping to next encumbrance number.  (apencumbrancecls)"
      end
      return

BuildTC71:
      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] = PONo: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]
      gosub GetTickleKey
      write TC71 to TickleFile, TickleKey
      crt "Encumbrance ":PONo: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
