          PH.ARGS DRPT,SD,ED,BRCHS,BR,GLIDS,SL.ID,ORIG.ID,GL.GRP,SORT,DETAIL,BRKON,ZRO,XCURR,XCALC,DWNLD
** Version# 57.0002[5] - 11/22/2010 - 03:43pm - SMITJR - eclipse
*** V57.0002 Change - Custom Coding .. - 11/22/2010 - SMITJR - eclipse
** Copied from BP GL.PHR.GEN.JRL Version# 57 - 11/20/2008 - 10:29am - AGARZA - main

*** Subroutine - GL.PHR.GEN.JRL
*-------------------------------------------------------------------------*
*** Description: General Ledger Trial Balance Listing This report gives an
*** overview of the General Ledger for specific accounts or templates.  It
*** gives a running total balance and the transaction amount and
*** type/source.
*-------------------------------------------------------------------------*
*** DRPT    - Report defaults                                         [IN]
*** SD      - Start date of rpt                                       (IN)
*** ED      - End date of rpt                                         (IN)
*** BRCHS   - Branches                                                (IN)
*** BR      - Literal response to Br/Tr/All                           (IN)
*** GLIDS   - G/L Account#s                                           (IN)
*** ORIG.ID - G/L Account#                                            (IN)
*** SL.ID   - Sub Ledger ID                                           (IN)
*** GL.GRP  - G/L Lines                                               (IN)
*** DETAIL  - Data display option - Summary, Detail, OR Totals by Day (IN)
*** SORT    - SortBy option - Account# OR Account# by Branch          (IN)
*** BRKON   - Break on periods                                        (IN)
*** ZRO     - Not used                                                (IN)
*** XCURR   - Report currency                                         (IN)
*** XCALC   - Calculate exchange                                      (IN)
*** DWNLD   - Download Option - Puts report in downloadable format    (IN)
*-------------------------------------------------------------------------*
*** Common Variables: DRPT, LED
*-------------------------------------------------------------------------*
*** Notes:
*-------------------------------------------------------------------------*
          BRS          = BR

          BR.CT        = DCOUNT(BRCHS,VM)
          GL.GRP       = RAISE(GL.GRP)
          XRATE        = 1
          AOD          = ''
          GRAND.BALFWD = 0

          GOSUB INIT

          IF BRKON THEN
             PERIODS  = GET.PERIODS(SD,ED)
             PERN     = DCOUNT(PERIODS,VM)

             FOR PERX = 1 TO PERN
                SD    = PERIODS<1,PERX,1>
                ED    = PERIODS<1,PERX,2>
                GOSUB ONEPER
             NEXT PERX
          END ELSE GOSUB ONEPER

          UT.PH.CLEANUP
          SEND.MESSAGE 'Phantom',USER.ID,TITLE:' is complete'

          STOP
*-------------------------------------------------------------------------*
ONEPER:   *** For one period...

          *** Update our Phantom Status...
          WRITE 'Selecting...' ON PHSTFILE,PID$

          IF GLIDS = '' THEN
             IF ORIG.ID # '' THEN
                GOSUB SEL.ACCTS
             END ELSE
                IF SL.ID # '' THEN
                   *** Get all Accounts with this Subledger ID.
                   GOSUB GET.IDS
                END ELSE
                   * Shouldn't get here, driver program shouldn't have
                   * allowed it.
                   RETURN
                END
             END
          END

          *** Update our Phantom Status...
          WRITE 'Spooling...' ON PHSTFILE,PID$

          SDT = OCONV(SD,'D4/')
          EDT = OCONV(ED,'D4/')
          SYR = SDT[7,4]
          EYR = EDT[7,4]
          IDT = ICONV('01/01/':SDT[7,4],'D')

          DET.MODE = DETAIL[1,1]

          *** If download version then show that in heading.
          IF DWNLD THEN
             HDG  = DETAIL:' General Ledger for ':SDT:' to ':EDT
             HDG := ' (Download Version) '
          END ELSE
             HDG  = DETAIL:' General Ledger for ':SDT:' to ':EDT
          END

          IF XCURR THEN
             HDG := '    ':XCURR
          END

          IF ORIG.ID THEN
             HDG<1,2> = 'For Template : ':ORIG.ID
          END ELSE
             ACCT.NAME = OCONV(GLIDS<1>,'TGENLED;X;3;3'):" (":GLIDS<1>:")"
             HDG<1,2> = 'For Account : ':ACCT.NAME
             *** If SubLedger specified then show which ones
             IF SHOW.SL THEN
                IF DCOUNT(SL.ID,VM) > 1 THEN
                   HDG<1,2> := ' - ':'Multiple SubLedgers'
                END ELSE
                   HDG<1,2> := ' - ':SL.ID
                END
             END
          END
          HDG<1,3> = 'Sorted By : ':SORT

          IF XCALC # '' THEN
             HDG<1,3> := '  Calculate Exchange ':XCALC
          END

          BRANCH.ALLOW = 66
          IF LEN(BRS)  > BRANCH.ALLOW THEN BRS = BRS[1,BRANCH.ALLOW]:'...'
          HDG<1,4>     = 'Branch(es)  : ':BRS

          *** If Download version then omit the headings
          IF NOT(DWNLD) THEN
             HDG<1,5>  = 'Account Name..........................'

             IF SHOW.SL THEN HDG<1,5> := '  SubLedger.'

             *** Set up additional Header Information
             BEGIN CASE
                *** Detail
                CASE DET.MODE = 'D'
                   HDG<1,6>  = ' Order #....... Description.......'
                   HDG<1,6> := ' Br.. Source....  Curr Period...'
                   HDG<1,6> := '  Balance........'

                *** Branch Summary
                CASE DET.MODE = 'B'
                   HDG<1,5> := '  Br..  Curr Period...  Balance........'

                *** All Other Conditions
                CASE OTHERWISE
                   HDG<1,5> := '  Curr Period...  Balance........'

             END CASE

          END

          IF DET.MODE = 'D' THEN
             WDTH = LEN(HDG<1,6>)
          END ELSE
             WDTH = LEN(HDG<1,5>)
          END

          *** Stick our page number on at the very edge of the page...
          HDG<1,1> := SPACE(WDTH - LEN(HDG<1,1>) - 10):'Page: ^###'

          TITLE = DETAIL:' General Ledger ':SDT:' - ':EDT:' - Br : ':BRS

          PRINTER.ON WDTH,TITLE,DOC.ID,HDG,RPT.DFLT=DRPT

          PREV.DESC    = '@@@'
          OLD.BR       = '@@@'
          BRTOLS       = ''      ; * Branch Totals
          ACCTOLS      = ''      ; * Account Totals
          BR.ACCT.TOLS = ''      ; * Account Totals
          GTOLS        = ''      ; * Grand Totals
          GRAND.BALFWD = 0       ; * Balance Forward

          *** Go print the data for each of our GLIDs...
          GICT = DCOUNT(GLIDS,AM)
          FOR GIDN = 1 TO GICT
             GLID  = GLIDS<GIDN>
             GOSUB PRT.ACCT
          NEXT GIDN

          PREV.DESC = DESC[1,35]

          IF DET.MODE = 'T' THEN GOSUB PRT.BRTOLS

          IF DET.MODE # 'S' AND DET.MODE # 'B' THEN
             GOSUB PRT.ACCTOLS
             GOSUB PRT.GTOLS
          END

          IF HIST = 2 AND AVG.XRATE THEN
             PRINT
             PRINT "Average Exchange rate used for this report was ":AVG.XRATE
          END

          PRINTER.OFF DOC.ID

          RETURN
*-------------------------------------------------------------------------*
INIT:     *** Initialize data we'll need to run this Subroutine...

          SRC.DESCS     = ''
          SRC.DESCS<1>  = 'Stk Sls'
          SRC.DESCS<2>  = 'C/R'
          SRC.DESCS<3>  = 'Cashbox'
          SRC.DESCS<4>  = 'UB Purch'
          SRC.DESCS<5>  = 'Xfers'
          SRC.DESCS<6>  = 'Dir Purch'
          SRC.DESCS<7>  = 'Payable'
          SRC.DESCS<8>  = 'Disburse'
          SRC.DESCS<9>  = 'J/E'
          SRC.DESCS<10> = 'Inv Adj'
          SRC.DESCS<11> = 'Work Order'
          SRC.DESCS<12> = 'Rental'
          DESC          = ''

          READV FRMTS FROM CTRLFILE,'GL.CODEFORM',2 ELSE FRMTS = ''
          ACCT.FRMT = 'MT':FRMTS<1,1>
          SPAD      = 21 - LEN(FRMTS<1,1>)
          READV IBRK  FROM CTRLFILE,'GL.CODEFORM',3 ELSE IBRK  = 99999999
          IBRK = IBRK<1,1>

          *** Set flag whether to show SubLedgers or not
          IF SL.ID THEN SHOW.SL = YES ELSE SHOW.SL = NO

          *** Set the avg exchange rate calculation
          BEGIN CASE
             CASE XCALC = 'Historically'
                HIST = 1

             CASE XCALC = 'Average'
                HIST = 2

             CASE OTHERWISE
                HIST = ''
          END CASE

          RETURN
*-------------------------------------------------------------------------*
PRT.ACCT: *** Print data for a single GL Account...

          READ GLD FROM GLFILE,GLID ELSE GLD = ''
          ACCT = GLD<2,1>
          *** Get the Account name...
          DESC = GLD<3>

          IF DESC # PREV.DESC AND PREV.DESC#'@@@' AND DET.MODE = 'D' THEN
             GOSUB PRT.ACCTOLS
          END

          READV OS.CUT FROM CTRLFILE,'GL.CODEFORM',3  ELSE OS.CUT = ''
          READV ACCT   FROM GLFILE,GLID,2            ELSE ACCT = ''

*** Check the new rel6.0 flag to see if this is OS Account...
          READV REP.TYP FROM GLFILE,GLID,28 ELSE REP.TYP = ''
          IF REP.TYP THEN OS.ACCT = NO ELSE OS.ACCT = YES

          IF ACCT<1,1> >= OS.CUT<1,1> OR OS.ACCT THEN
             GL.SD = REALDATE('FB/FB/YR',SD)
             GL.ED = SD - 1
          END ELSE
             GL.SD = ''
             GL.ED = SD - 1
          END

          *** If this was a template coming in then we need to see if
          *** this GL Account allowed SubLedgers if it doesn't we null
          *** out the SubLedger List
          IF ORIG.ID AND SL.ID # '' THEN
             READV SL.ALLOWED FROM GLFILE,GLID,31 ELSE SL.ALLOWED = 'N'
             IF SL.ALLOWED = 'N' OR SL.ALLOWED = '' THEN
                SL.IDS = ''
             END ELSE
                SL.IDS = SL.ID
             END
          END ELSE
             SL.IDS = SL.ID
          END

          SL.CT = DCOUNT(SL.IDS,VM)
          IF SL.CT = 0 THEN SL.CT = 1
          FOR SLP = 1 TO SL.CT

             SUB.ID = SL.IDS<1,SLP>

             IF SUB.ID THEN
                DESC = DESC "L#38":'  ':SUB.ID
             END

             *** If SubLedger changed then print totals for last subled
             IF SLP > 1 AND DET.MODE = 'D' THEN
                GOSUB PRT.ACCTOLS
             END

             IF GL.ED < GL.SD THEN
                BALFWD = 0
             END ELSE

                *** This will get us our Balance for the GLID...
                GL.GET.AMT GLID,SUB.ID,BRCHS,GL.ED,BALFWD,GL.SD,XCURR,HIST,ED
             END

             *** Go get any Source Amounts and Sort our data...
             GOSUB GET.AMTS

             FIRST = YES

             BEGIN CASE
             CASE DET.MODE = 'S'
                IF SORT = 'Account#' THEN
                   *** We want to print Source Data...
                   FOR J = 1 TO JLI.LIMIT$
                      *** If there is a value for a Source Type...
                      IF SUM(SRC.AMTS<1,1,J>) THEN
                         *** Print the Account Name if necessary...
                         IF FIRST THEN GOSUB PRTHDR

                         IF SHOW.SL THEN
                            PRINT 'Source : ':SRC.DESCS<J>"L#41":
                            PRINT SUM(SRC.AMTS<1,1,J>) "R26,#16"
                         END ELSE
                            PRINT 'Source : ':SRC.DESCS<J>"L#29":
                            PRINT SUM(SRC.AMTS<1,1,J>) "R26,#16"
                         END
                      END
                   NEXT J
                END ELSE
                   ITEM.CT = DCOUNT(SORTBYS<1>,VM)
                   *** We want to print Branch Data...
                   FOR ITEM = 1 TO ITEM.CT
                      IF SUM(SRC.AMTS<1,ITEM>) THEN
                         *** Print the Account Name if necessary...
                         IF FIRST THEN GOSUB PRTHDR

                         IF SHOW.SL THEN
                            PRINT 'Branch : ':SORTBYS<1,ITEM>"L#41":
                            PRINT SUM(SRC.AMTS<1,ITEM>)  "R26,#16"
                         END ELSE
                            PRINT 'Branch : ':SORTBYS<1,ITEM>"L#29":
                            PRINT SUM(SRC.AMTS<1,ITEM>)  "R26,#16"
                         END
                         ACCTOLS += SUM(SRC.AMTS<1,ITEM>)
                      END
                   NEXT ITEM
                END

                IF FIRST THEN
                   IF BALFWD # 0 THEN GOSUB PRTHDR
                END ELSE
                   GOSUB PRT.ACCTOLS
                   BALFWD += SUMMATION(SRC.AMTS)
                   IF SHOW.SL THEN
                      PRINT SPACE(66):BALFWD"R26,#17"
                   END ELSE
                      PRINT SPACE(54):BALFWD"R26,#17"
                   END
                END

             *** Branch Summary Option
             CASE DET.MODE = 'B'
                BRCT = DCOUNT(BRCHS,VM)
                FOR BRX = 1 TO BRCT
                   FIRST = YES
                   IF GL.ED < GL.SD THEN
                      BALFWD = 0
                   END ELSE
                      *** This will get us our Balance for the GLID...
                      GL.GET.AMT GLID,SUB.ID,BRCHS<1,BRX>,GL.ED,BALFWD,GL.SD,XCURR,HIST,ED
                   END

                !  GL.GET.BALANCE GLID,BRCHS<1,BRX>,GL.ED,BALFWD,GL.SD
                   BR.AMTS = NO
                   AMT.CNT = DCOUNT(SRC.AMTS<1,BRX>,SVM)
                   FOR XX = 1 TO AMT.CNT
                      IF SRC.AMTS<1,BRX,XX>+0 # 0 THEN
                         BR.AMTS = YES
                         EXIT
                      END
                   NEXT XX

                   IF SUM(SRC.AMTS<1,BRX>) OR (BR.AMTS AND BALFWD) THEN
                      FOR J = 1 TO 10
                         IF SUM(SRC.AMTS<1,BRX,J>) THEN
                            IF FIRST THEN GOSUB PRTHDR
                            IF SHOW.SL THEN
                               PRINT 'Source: ':SRC.DESCS<J>"L#40    ":
                            END ELSE
                               PRINT 'Source: ':SRC.DESCS<J>"L#28    ":
                            END
                            PRINT BRCHS<1,BRX> "R#4":
                            PRINT SUM(SRC.AMTS<1,BRX,J>) "R26,#16"
                            BR.ACCT.TOLS<J> += SUM(SRC.AMTS<1,BRX,J>)
                         END
                      NEXT J
                   END
                   IF SUM(SRC.AMTS<1,BRX>) OR (BALFWD) THEN
                      IF FIRST THEN
                         IF BALFWD # 0 THEN GOSUB PRTHDR
                      END
                      **Print subtotal if there was any type of balfwd too.
                      GOSUB PRT.BR.STOL
                   END
                NEXT BRX
                GOSUB PRT.BR.ACCT.TOLS
                IF GIDN # GICT THEN PRINT CHAR(12)
             CASE DET.MODE = 'T'
                ITEM.CT = DCOUNT(SORTBYS<1>,VM)
                FOR ITEM = 1 TO ITEM.CT
                   AMTS    = SRC.AMTS<1,ITEM>

                   IF SORT = 'Account#' THEN
                      DT = FIELD(SORTBYS<1,ITEM>,'~',1)

                      FOR J = 1 TO JLI.LIMIT$
                         IF AMTS<1,1,J>#'' THEN
                            IF FIRST THEN
                               GRAND.BALFWD += BALFWD
                               GOSUB PRTHDR
                            END

                            SRC = OCONV(DT,'D4/'):' - Source : ':SRC.DESCS<J>

                            PRINT SRC                      "L#38":
                            IF SHOW.SL THEN PRINT ' '      "L#12":
                            PRINT AMTS<1,1,J>           "R26,#16":
                            BALFWD += AMTS<1,1,J>
                            PRINT BALFWD                "R26,#17"

                            GTOLS        += AMTS<1,1,J>
                            GRAND.BALFWD += AMTS<1,1,J>
                         END
                      NEXT J
                   END ELSE
                      DT = FIELD(SORTBYS<1,ITEM>,'~',2)
                      BR = FIELD(SORTBYS<1,ITEM>,'~',1)

                      *** If we have a source amount...
                      IF SUM(AMTS<1,1>) THEN
                         *** If we're reporting amounts for a new branch
                         *** go print Subtotals for the last one...
                         IF (BR # OLD.BR AND OLD.BR # '@@@') OR FIRST THEN
                            GOSUB PRT.BRTOLS

                            IF FIRST THEN
                               *** Go print Account Subtotals...
                               GOSUB PRT.ACCTOLS
                               *** Go print header for this next account...
                               GOSUB PRTHDR
                            END
                         END

                         TOT = OCONV(DT,'D4/'):' - For Branch : ':BR"R#4"

                         PRINT TOT                      "L#38":
                         IF SHOW.SL THEN PRINT ' '      "L#12":
                         PRINT SUM(AMTS<1,1>)           "R26,#16":

                         BALFWD += SUM(AMTS<1,1>)
                         PRINT BALFWD                   "R26,#17"

                         BRTOLS       += SUM(AMTS<1,1>)
                         ACCTOLS      += SUM(AMTS<1,1>)
                         GTOLS        += SUM(AMTS<1,1>)
                         ACC.BALFWD  = BALFWD

                         OLD.BR  = BR
                      END
                   END
                NEXT ITEM
             CASE DET.MODE = 'D'
                ITEM.CT   = DCOUNT(SORTBYS<1>,VM)
                FOR ITEM = 1 TO ITEM.CT
                   AMTS    = SRC.AMTS<1,ITEM>

                   IF SORT = 'Account#' THEN
                      DT = FIELD(SORTBYS<1,ITEM>,'~',1)
                   END ELSE
                      BR = FIELD(SORTBYS<1,ITEM>,'~',1)
                   END

                   *** For each of the Sources...
                   FOR J = 1 TO JLI.LIMIT$
                      *** Need to check for nulls because 0 means we do hav
                      *** have Accounts to display
                      IF AMTS<1,1,J>#'' THEN
                         *** We will recalculate in PRT.RECS
                         AMTS<1,1,J> = 0
                         IF SORT = 'Account#' THEN
                            IF FIRST THEN GRAND.BALFWD += BALFWD

                            *** Loop through our branches
                            FOR BRN = 1 TO BR.CT
                               BR = BRCHS<1,BRN>
                               GOSUB PRT.RECS
                            NEXT BRN

                            IF FIRST THEN GOSUB PRTHDR

                            *** Show Source info if not downloading.
                            IF NOT(DWNLD) THEN
                               SRC = OCONV(DT,'D4/'):' - Source : ':SRC.DESCS<J>

                               PRINT SPACE(12):'***** ':SRC       "L#32":
                               PRINT AMTS<1,1,J>               "R26,#16":
                               PRINT BALFWD                    "R26,#17"
                               PRINT
                            END
                         END ELSE
                            FOR YR = SYR TO EYR
                               IF YR = SYR THEN
                                  D1 = SD
                               END ELSE
                                  D1 = ICONV("01/01/":YR,'D')
                               END

                               IF YR = EYR THEN
                                  D2 = ED
                               END ELSE
                                  D2 = ICONV("01/01/":YR+1,'D')-1
                               END

                               FOR DT = D1 TO D2
                                  GOSUB PRT.RECS
                               NEXT DT
                            NEXT YR
                         END
                      END
                   NEXT J

                   IF SORT = 'Account#' THEN GOTO NEXT.ITEM

                   IF SUM(AMTS<1,1>) # 0 THEN
                      IF FIRST THEN GOSUB PRTHDR

                      TOT = ' **Subtotal for Branch :':BR  "R#4":SPACE(22)

                      PRINT SPACE(53):

                      PRINT '-------------  ---------------'
                      PRINT TOT:
                      PRINT SUM(AMTS<1,1>)                "R26,#16":
                      PRINT BALFWD                        "R26,#17"
                      PRINT
                   END
NEXT.ITEM:      NEXT ITEM
             END CASE
             IF FIRST AND BALFWD # 0 THEN
                GRAND.BALFWD += BALFWD
                GOSUB PRTHDR
                PRINT
             END

          NEXT SLP

          RETURN
*-------------------------------------------------------------------------*
PRT.RECS: ***

          *** Get all records for this GLID, BR and DT...
          GL.GET.DETAIL BR,GLID,SUB.ID,J,DT,AIDS,DET.AMTS,'G.4'

          *** Currency exchange
          IF XCURR THEN
             IF HIST = 2 THEN
                *** AVG.XRATE was calculate in GET.AMTS which would have
                *** been done prior to this routine.
                IF AVG.XRATE THEN XRATE = AVG.XRATE ELSE XRATE = 1
             END ELSE
                IF HIST THEN AOD = DT ELSE AOD = ED
                XRATE = XCURR.RATE.GET(XCURR,AOD)
                XRATE = OCONV(XRATE,'MR4')
             END
          END ELSE XRATE = 1

          SCT = DCOUNT(AIDS,AM)
          *** For each record...
          FOR SN  = 1 TO SCT
             AID     = AIDS<SN>
             SRC.AMT = ICONV(OCONV(DET.AMTS<SN>,'MR2') / XRATE,'MR2')
             *** We are going to reaccumulate the Amounts array here
             *** to prevent penny differences when doing exchange rates
             AMTS<1,1,J> += SRC.AMT
             MATREAD LED FROM LEDFILE,FIELD(AID,'.',1) ELSE MAT LED =''
             LOCATE FIELD(AID,'.',2)+0 IN LED(8)<1> SETTING GEN ELSE GEN = 1
             BT = LED(1)<1,GEN>
             NAME = ''

             IF BT+0 # 0 THEN
                READV NAME FROM ENTFILE,BT,1 ELSE NAME = ''
             END

             IF NAME = '' THEN
                OID  = FIELD(AID,'.',1)
                READV NAME FROM LEDFILE,OID,80 ELSE NAME=''
                ** Description may be multi gen (e.g. reversals)
                NAME = NAME<1,GEN>
             END

             IF FIRST THEN GOSUB PRTHDR

             IF DWNLD THEN
                PRINT OCONV(LED(23)<1,GEN>,'D4/') "L#10":' ':
                PRINT DESC                        "L#47":' ':
             END ELSE
                PRINT SPACE(1):
             END
             NOTES = LED(80)<1,GEN>
             PRINT NOTES                          "L#35":' ':
             PRINT AID                            "L#14":' ':
             PRINT NAME                           "L#18":' ':
             PRINT BR                             "R#4":' ':
             PRINT SRC.DESCS<J>                   "L#10":
             PRINT SRC.AMT                        "R26,#16":' ':


             BRTOLS        += SRC.AMT
             ACCTOLS       += SRC.AMT
             GTOLS         += SRC.AMT
             BALFWD        += SRC.AMT
             *** Our Account Balance Forward will always be the last one
             *** we pick up...
             ACC.BALFWD    = BALFWD

             IF SORT = 'Account#' THEN
                GRAND.BALFWD += SRC.AMT
             END

             PRINT BALFWD                         "R26,#17"

             *** print the detail notes stored in led(81)
             SRCH.ID   = BR :'~':GLID
             SPOS      = 1
             PRINT.HDR = NO    ;* don't print "Notes...." more than once
             LOOP
                LOCATE SRCH.ID IN LED(25)<1,GEN>,SPOS SETTING POS THEN
                   DET.NOTES = RAISE(RAISE(LED(81)<1,GEN,POS>))
                   NT.CNT    = DCOUNT(DET.NOTES,VM)
                   FOR NTS   = 1 TO NT.CNT
                      IF NTS = 1 AND NOT(PRINT.HDR) THEN
                         PRINT '  Notes.....':SPACE(2):DET.NOTES<1,NTS>
                         PRINT.HDR = YES
                      END ELSE
                         PRINT SPACE(14): DET.NOTES<1,NTS>
                      END
                   NEXT NTS
                   SPOS = POS + 1
                END ELSE EXIT
             REPEAT
          NEXT SN

          RETURN
*-------------------------------------------------------------------------*
PRTHDR:   *** Print Header

          *** If downloading then don't print header
          IF DWNLD THEN RETURN

          *** If Branch Summary print out branch
          IF DET.MODE = 'B' THEN
             IF SHOW.SL THEN
                PRINT DESC                           "L#52":
             END ELSE
                PRINT DESC                           "L#40":
             END
             PRINT BRCHS<1,BRX> "R#4":
          END ELSE
             IF SHOW.SL OR DET.MODE = 'D' THEN
                PRINT DESC                           "L#50":
             END ELSE
                PRINT DESC                           "L#38":
             END
          END

          PRINT '  As Of ':OCONV(SD-1,'D2/')   "L#9":
          PRINT BALFWD                         "R26,#16"

          PREV.DESC = DESC
          FIRST = NO

          RETURN
*-------------------------------------------------------------------------*
PRT.BRTOLS: * Print Branch Subtotals...

          IF SORT = 'Account#' THEN RETURN
          IF BRTOLS + 0 = 0     THEN RETURN

          *** If we're printing a Subtotal for 'Totals By Day' view...
          IF DET.MODE = 'T' THEN
             IF SHOW.SL THEN
                PRINT SPACE(53):'-------------  ---------------'
                PRINT " **Subtotal for - ":
                PRINT 'Branch: ':OLD.BR          "R#4":SPACE(20):
             END ELSE
                PRINT SPACE(41):'-------------  ---------------'
                PRINT " **Subtotal for - ":
                PRINT 'Branch: ':OLD.BR          "R#4":SPACE(8):
             END
          END ELSE
             IF SHOW.SL AND DET.MODE # 'D' THEN
                PRINT SPACE(57):'-------------  ---------------'
                PRINT " **Subtotal for - ":
                PRINT 'Branch: ':OLD.BR          "R#4":SPACE(20):
             END ELSE
                PRINT SPACE(35):'-------------  ---------------'
                PRINT " **Subtotal for - ":
                PRINT 'Branch: ':OLD.BR          "R#4":SPACE(8):
             END
          END

          PRINT BRTOLS                            "R26,#16 ":
          PRINT ACC.BALFWD                        "R26,#16"
          PRINT

          BRTOLS = ''

          RETURN
*-------------------------------------------------------------------------*
PRT.ACCTOLS: *

          *** If downloading skip Account Totals
          IF DWNLD THEN RETURN

          IF SORT = 'Account#' THEN RETURN
          IF ACCTOLS + 0 = 0   THEN RETURN

          *** If we're printing a Subtotal for 'Totals By Day' view...
          BEGIN CASE
          CASE DET.MODE = 'T'
             IF SHOW.SL THEN
                PRINT SPACE(53):
                PRINT '-------------  ---------------'
                PRINT " **Subtotal for - ":
                PRINT PREV.DESC                   "L#32":
             END ELSE
                PRINT SPACE(41):
                PRINT '-------------  ---------------'
                PRINT " **Subtotal for - ":
                PRINT PREV.DESC                   "L#20":
             END
          CASE DET.MODE = 'D'
                PRINT SPACE(53):
                PRINT '-------------  ---------------'
                PRINT " **Subtotal for - ":
                PRINT PREV.DESC                   "L#32":
          CASE OTHERWISE
             IF SHOW.SL THEN
                PRINT SPACE(52):
                PRINT '--------------'
                PRINT " **Subtotal for - ":
                PRINT DESC                        "L#32":
             END ELSE
                PRINT SPACE(40):
                PRINT '--------------'
                PRINT " **Subtotal for - ":
                PRINT DESC                        "L#20":
             END
          END CASE

          PRINT ACCTOLS                        "R26,#16 ":

          IF DET.MODE # 'S' THEN
             PRINT ACC.BALFWD                     "R26,#16"
             PRINT
             GRAND.BALFWD += ACC.BALFWD
          END ELSE
             PRINT
          END

          ACCTOLS = ''
          ACC.BALFWD = 0

          RETURN
*-------------------------------------------------------------------------*
PRT.GTOLS:* Grand Totals

          *** If downloading skip these Grand Totals
          IF DWNLD THEN RETURN

          IF DET.MODE = 'T' THEN
             LESS.SPACE =  12
          END ELSE
             LESS.SPACE = 0
          END

          IF SHOW.SL AND DET.MODE # 'D' THEN
             PRINT SPACE(65-LESS.SPACE):'=============  ==============='
             PRINT SPACE(45-LESS.SPACE):'Grand Totals --- ':
          END ELSE
             PRINT SPACE(53-LESS.SPACE):'=============  ==============='
             PRINT SPACE(33-LESS.SPACE):'Grand Totals --- ':
          END
          PRINT GTOLS                         "R26,#16 ":
          PRINT GRAND.BALFWD                  "R26,#16"

          RETURN
*-------------------------------------------------------------------------*
PRT.BR.ACCT.TOLS: * Print Branch Subtotal Account SubTotals

          *** If downloading skip these Totals
          IF DWNLD THEN RETURN

          IF SHOW.SL THEN
             PRINT DESC                           "L#52":
          END ELSE
             PRINT DESC                           "L#40":
          END
          PRINT 'All' "R#4":

          PRINT '  As Of ':OCONV(SD-1,'D2/')   "L#9":

          GL.GET.AMT GLID,SUB.ID,BRCHS,GL.ED,BALFWD,GL.SD,XCURR,HIST,ED

          PRINT BALFWD                         "R26,#16"

          FIRST = NO

          BR.STOL = SUM(BR.ACCT.TOLS)

          FOR J = 1 TO 10
          IF BR.ACCT.TOLS<J>+0 # 0 THEN
             IF SHOW.SL THEN
                PRINT 'Source: ':SRC.DESCS<J>"L#40    ":
             END ELSE
                PRINT 'Source: ':SRC.DESCS<J>"L#28    ":
             END
             PRINT ' All':
             PRINT BR.ACCT.TOLS<J> "R26,#16"
             BALFWD += BR.ACCT.TOLS<J>
          END
          NEXT J
          BR.ACCT.TOLS = ''


          PRINT "**Totals For All Branches":
          IF SHOW.SL THEN
             PRINT SPACE(34):'============='
          END ELSE
             PRINT SPACE(22):'============='
          END

          IF SHOW.SL THEN
             PRINT SPACE(56):BR.STOL "R26,#16":
          END ELSE
             PRINT SPACE(44):BR.STOL "R26,#16":
          END
          PRINT BALFWD"R26,#17"
          PRINT

          RETURN
*-------------------------------------------------------------------------*
PRT.BR.STOL: * Print Branch Summary Sub Totals

          *** If downloading skip these Totals
          IF DWNLD THEN RETURN

          BR.STOL = SUM(SRC.AMTS<1,BRX>)
          BALFWD += BR.STOL
          IF SHOW.SL THEN
             PRINT SPACE(59):
             PRINT '-------------  ---------------'
          END ELSE
             PRINT SPACE(47):
             PRINT '-------------  ---------------'
          END
          PRINT "**Subtotal for Branch: ":BRCHS<1,BRX>"R#4":
          IF SHOW.SL THEN
             PRINT SPACE(29):BR.STOL "R26,#16":
          END ELSE
             PRINT SPACE(17):BR.STOL "R26,#16":
          END
          PRINT BALFWD"R26,#17"
          PRINT
          RETURN
*-------------------------------------------------------------------------*
GET.AMTS: *
          SORTBYS  = ''
          SRC.AMTS = ''
          AVG.XRATE  = 0
          TOT.XRATES = 0

          FOR YR = SYR TO EYR
             IF SUB.ID THEN
                GLTID = GLID:'~':SUB.ID:'*':YR
             END ELSE
                GLTID = GLID:'*':YR
             END
             IF YR = SYR THEN D1 = SD ELSE D1 = ICONV("01/01/":YR,'D')
             IF YR = EYR THEN D2 = ED ELSE D2 = ICONV("01/01/":YR+1,'D')-1

             FOR BRX = 1 TO BR.CT
                BR = BRCHS<1,BRX>
                READ GLT FROM GLTFILE,BR:'~':GLTID THEN

                   FOR DAY = D1 TO D2
                      *** This will give us the Day Number (out of 365)...
                      AMC   = OCONV(DAY,'DJ')

                      SAMTS = GLT<AMC>

                      *** Currency exchange
                      IF XCURR THEN
                         IF HIST THEN AOD = DAY ELSE AOD = ED
                         XRATE = XCURR.RATE.GET(XCURR,AOD)
                         XRATE = OCONV(XRATE,'MR4')
                      END ELSE XRATE = 1

                      BEGIN CASE
                         CASE DET.MODE = 'T' AND SORT # 'Account#'
                            SORTBY = BR:'~':DAY

                         *** If Branch Summary Mode set POS equal Branch
                         CASE DET.MODE = 'B'
                            POS = BRX

                         CASE SORT = 'Account#'
                            SORTBY = DAY

                         CASE OTHERWISE
                            SORTBY = BR
                      END CASE

                      IF DET.MODE = 'S' AND SORT = 'Account#' THEN
                         POS = 1
                      END ELSE
                         *** If not Branch Summary they set up SortBy
                         IF DET.MODE # 'B' THEN
                            LOCATE SORTBY IN SORTBYS<1> BY 'AR' SETTING POS ELSE
                               SORTBYS = INSERT(SORTBYS,1,POS;SORTBY)
                               SRC.AMTS = INSERT(SRC.AMTS,1,POS;'')
                            END
                         END
                      END

                      *** If avg xchange rate calculation then we
                      *** need to accumulate the exchange rates to
                      *** get average which we'll do outside of for
                      *** loop below.
                      IF HIST = 2 AND SUMMATION(SAMTS<1>) THEN
                         TOT.XRATES += 1
                         AVG.XRATE += XRATE
                         *** Set XRATE back to 1 we'll apply the avg later
                         XRATE = 1
                      END

                      *** Add our Source Amounts for each Day(OR)Branch...
                      FOR J = 1 TO JLI.LIMIT$
                         IF SAMTS<1,J>#'' THEN
                            SRC.AMTS<1,POS,J> += SAMTS<1,J>/XRATE
                         END
                      NEXT J
                   NEXT DAY
                END
             NEXT BRX
          NEXT YR

          *** If using average exchange rate then divide the balance by
          *** that average here.
          IF HIST = 2 AND AVG.XRATE # 0 THEN
             AVG.XRATE = OCONV(ICONV(AVG.XRATE / TOT.XRATES,'MR4'),'MR4')
             SRC.CT = DCOUNT(SRC.AMTS,VM)

             FOR JSRC = 1 TO SRC.CT
                *** Loop through all JLI amounts and apply the avg.
                *** exchange rate.
                FOR J = 1 TO JLI.LIMIT$
                   IF SRC.AMTS<1,JSRC,J>+0 # 0 THEN
                      SRC.AMTS<1,JSRC,J> = ICONV(OCONV(SRC.AMTS<1,JSRC,J>,'MR2') / AVG.XRATE,'MR2')
                   END
                NEXT J
             NEXT JSRC
          END

          RETURN
*-------------------------------------------------------------------------*
SEL.ACCTS:*
          ORIG.IDS = ORIG.ID
          GL.REC.ACCTS GL.GRP,GLIDS,ORIG.IDS
          RETURN
*-------------------------------------------------------------------------*
GET.IDS:  *** Create an array of all the SubLedger/Branch records
          *** GLIDS will hold all Accounts with the specified SubLedger

          SL.CT = DCOUNT(SL.ID,VM)
          FOR SLP = 1 TO SL.CT
             *** Initialize IDs
             IDS    = ''

             SUB.ID = SL.ID<1,SLP>
             *** If only one branch then we can just select
             *** one branches records from GENLED.TRANS
             IF BR.CT=1 THEN
                GBR = BRCHS<1,1>
                EXECUTE 'SELECT GENLED.TRANS WITH &INDEX&.SUBL = "':SUB.ID:'~':GBR:'~]"' CAPTURING MSG
                READLIST IDS ELSE IDS = ''
                *** Make sure all IDS are unique
                ID.CT = DCOUNT(IDS,AM)
                FOR IDP = 1 TO ID.CT
                   GLT.ID = FIELD(IDS<IDP>,'~',2)
                   LOCATE GLT.ID IN GLIDS BY 'AR' SETTING POS ELSE
                      GLIDS = INSERT(GLIDS,POS;GLT.ID)
                   END
                NEXT IDP
             END ELSE

                *** Multiple Branches requires BSCAN
                *** Get the GenLed Trans Records that have this subledger
                ROOT = SUB.ID:'~'
                LOOP
                   BSCAN TMP.GLT,GLT.ID FROM GLTFILE,ROOT USING '&INDEX&.SUBL' ELSE EXIT
                   ROOT = ''
                   *** If not right subledger get out.
                   IF FIELD(TMP.GLT,'~',1)#SUB.ID THEN EXIT
                   BR = FIELD(TMP.GLT,'~',2)+0
                   *** Only get Branches that the user has specified
                   LOCATE BR IN BRCHS<1> SETTING POS ELSE CONTINUE

                   GLT.ID = FIELD(GLT.ID,'~',2)
                   *** Make sure all IDS are unique
                   LOCATE GLT.ID IN GLIDS BY 'AR' SETTING POS ELSE
                      GLIDS = INSERT(GLIDS,POS;GLT.ID)
                   END
                REPEAT
             END
          NEXT SLP

          RETURN
*-------------------------------------------------------------------------*
!SMITJR~11/22/10~15:43
