          SUBROUTINE (DRPT,BRCHS,CHOICE,REP.ARRAY,ID,VIEW,BR.NAME,DOC.ID)
** Version# 35.0003[88] - 02/12/2014 - 10:03am - TSMITH - eclipse
*** V35.0003 Change - Custom Coding . - 02/12/2014 - TSMITH - eclipse
*** V35.0002 Change - Custom Coding . - 02/07/2014 - TSMITH - eclipse
*** V35.0001 Change - Custom Coding . - 10/29/2013 - TSMITH - eclipse

*** Subroutine: GL.REP3
*-------------------------------------------------------------------------*
*** This routine is used for G/L Reporting. It can be used to either:
*** 1) Generate a report -> when it is called from 'GL.REP.PHR.MASK'
***  OR
*** 2) Create a Preview of what the report will look like -> when it is
***    called from 'GL.REPORT3'.
***
*-------------------------------------------------------------------------*
*** Parameters:
***   DPRT      - Standard Report paramater.                         [IN]
***   BRCHS     - Branches to report data for.                       [IN]
***   CHOICE    - Consolidation Option                               [IN]
***                > 1 - 'Individual Reports by Branch'
***                > 2 - 'Individual Branch Consolidated'
***                > 3 - 'Consolidated with Branch Detail'
***                > 4 - 'Consolidated, no Branch Detail'
***   REP.ARRAY - Report Array built from the GENLED.RPT File.       [IN]
***   ID        - Report Id stored in the GENLED.RPT File.           [IN]
***   VIEW      - Preview flag.                                      [IN]
***                > 0 - Actually want to run the report.
***                > 1 - Just want to Preview the report.
***   DOC.ID    - the document ID (used for Solar)                   (OUT)

*-------------------------------------------------------------------------*

          DIM REPORT(30),TOTS(30),GLLEN(30),AFM(30),DIVDR(30)


          GL.REC    = ''
          CTOT.AMTS = ''
          VALIDATED = NO

          MATPARSE REPORT FROM RAISE(REP.ARRAY)

          BRCHS.CT = DCOUNT(BRCHS,VM)

          READ GL.CODEFORM FROM CTRLFILE, "GL.CODEFORM" ELSE GL.CODEFORM=''
          BEGIN CASE
             CASE REPORT(3) = 'No';        NUM.LEN = 0; GL.CODE = ''
             CASE REPORT(3) = 'Internal';  NUM.LEN = 5; GL.CODE = ''
             CASE OTHERWISE
                NUM.LEN = 20
                LOCATE REPORT(3) IN GL.CODEFORM<1> SETTING POS THEN
                   GL.CODE    = GL.CODEFORM<2,POS>
                   GL.CODE<2> = POS
                END ELSE GL.CODE = ''
          END CASE

          *** Turn Printer On flag
          PRT.ON  = YES
          PRT.OFF = YES
          IF CHOICE < 3 AND CHOICE > 0 THEN
             *** If branch reports
             GOSUB GEN.BRCH
          END ELSE
             *** If consolidated reports
             GOSUB GEN.CONS
          END
          UT.PH.CLEANUP

          RETURN
*-------------------------------------------------------------------------*
GEN.BRCH: * Generate a report for each Branch...

          FOR BRCH.CNT = 1 TO BRCHS.CT
             *** If branch consolidated then put all on one report
             *** So we don't want to restart printer
             IF CHOICE = 2 THEN
                IF BRCH.CNT > 1 THEN
                  PRT.ON = NO
                END
                IF BRCH.CNT = BRCHS.CT THEN
                  PRT.OFF = YES
                END ELSE
                  PRT.OFF = NO
                END
             END
             BRANCHES = BRCHS<1,BRCH.CNT>
             *** Go Generate the report...
             GOSUB GENERATE
          NEXT BRCH.CNT

          RETURN
*-------------------------------------------------------------------------*
GEN.CONS: * Genereate a consolidated report...

          BRANCHES = BRCHS

          *** Go Generate the report...
          GOSUB GENERATE

          RETURN
*-------------------------------------------------------------------------*
GENERATE: * Generate a report...
          IF PRT.ON THEN

             BRCH.LIST = BRANCHES
             *** Make 1 report with all branches
             IF CHOICE = 2 THEN BRCH.LIST = BRCHS

             CONVERT VM TO ',' IN BRCH.LIST
             DISP.BRCHS = COMMA.COMPRESS(BRCH.LIST)

             TYPES    = 'Listing of Accounts':VM:'Balance Sheet':VM:'Operating Statement':VM:'Change in Financial Condition':VM:'Trial Balance'

             IF REPORT(21) # '' THEN TITLE = REPORT(21) ELSE
                TITLE = TYPES<1,REPORT(1)>
             END



             REP.TL  = TITLE
             IF BR.NAME # '' AND (CHOICE = 2 OR CHOICE = 4) THEN
               REP.TL :=' - Br : ':BR.NAME
             END ELSE
               REP.TL :=' - Br : ':DISP.BRCHS
             END

             WRITE REP.TL ON PHSTFILE,PID$

             GOSUB MAKE.DATES
             GOSUB MAKE.HDR

          END ELSE
             *** Consolidate Branch report put page break between branches
             PRINT CHAR(12)
          END

          *** If they just want a report Preview...
          IF VIEW THEN
             *** Show the results in a window.
             WIDTH = "VIEW"
             DRPT  = ''
          END

          GOSUB INIT

          USER.OPTS=''
          GL.GRP   =''
       *** Report(22) contains the template for the report ie. "Operating
       *** Statement".
          GL.READ REPORT(22),GL.REC,GL.GRP,GROUP,USER.OPTS,GL.ERR
          *** If the GL Account is a 'Trial Balance' Type...
          IF REPORT(1) = 5 THEN
             GOSUB MAKE.TOTAL
             *** If we are consolidating all branches onto one report
             *** then ask for a consolidated report total.
             IF CHOICE = 2 AND PRT.OFF THEN
                ACCT.AMTS = CTOT.AMTS
                GOSUB CONS.TOTAL
             END
          END

          TMP.GLID = REPORT(22)<1,1>

          IF TMP.GLID = '' AND REPORT(22)<1,2> # '' THEN TMP.GLID = "XXX"

          *** If it's NOT a preview and the Group Option is set for this
          IF NOT(VIEW) AND REPORT(25) THEN
             VALIDATE = ''

             GL.REC.REPORT TOT.AMTS,GL.GRP,USER.OPTS<1,1>,0,REPORT(6),0,TMP.GLID,NO,BRANCHES,REP.ARRAY,ACCT.AMTS,VALIDATE,3,1


             TOT.AMTS = ''

             IF DRPT<27> = 'HOLD' THEN OPTION = 2 ELSE OPTION = 1

             *** If we've validated before there is no reason to do it
             *** again because we validate against all branches on the
             *** report when we validate the first time
             IF NOT(VALIDATED) THEN
                *** Using Reporting branches so we can check Cost Centers
                *** Note:  Start and End Date have never been used
                PH.EXE 'GL.VALIDATION',TITLE,REPORT(1),VALIDATE,,,BRCHS
                VALIDATED = YES
             END
          END

          VALIDATE = ''

          IF PRT.ON THEN
             PRINTER.ON WIDTH,REP.TL,DOC.ID,HDR,FDR,RPT.DFLT=DRPT
          END

          *** If Consolidated then print the branch
          IF FIELD(ID,'.',1) = 'BUSINESS' THEN
          IF BRANCHES = '1' THEN
          BRNCH = 'Greensboro'
          END ELSE
          IF BRANCHES = '2' THEN
          BRNCH = 'Winston-Salem'
          END ELSE
          IF BRANCHES = '3' THEN
          BRNCH = 'Renewable Energy Division'
          END ELSE
          IF BRANCHES = '4' THEN
          BRNCH = 'FM Supplies'
          END ELSE
          IF BRANCHES = '5' THEN
          BRNCH = 'Kivett Industrial'
          END ELSE
          IF BRANCHES = '6' THEN
          BRNCH = 'GPS Nuclear'
          END ELSE
          BRNCH = 'GPS Arizona'
          END
          END
          END
          END
          END
          END
          END





          IF CHOICE = 2 THEN
             BR.TOT  = TRIM(TITLE)
             BR.TOT :=' - ':BRNCH
             PRINT BR.TOT
             PRINT
          END

          IF GL.REC<1,1> THEN SIGN = 1 ELSE SIGN = -1

          ** If preview, ignore 'Show Zero Lines' flag
          IF VIEW THEN
             SV.ZFLAG = REP.ARRAY<1,5>
             REP.ARRAY<1,5> = YES
          END

          GL.REC.REPORT TOT.AMTS,GL.GRP,USER.OPTS<1,1>,0,REPORT(6),0,TMP.GLID,YES,BRANCHES,REP.ARRAY,ACCT.AMTS,VALIDATE,CHOICE,SIGN


          IF CHOICE = 2 THEN
             CTOT.AMTS = ADDS(CTOT.AMTS,TOT.AMTS)
          END

          ** If view then restore 'Show Zero Lines' flag
          IF VIEW THEN
             REP.ARRAY<1,5> = SV.ZFLAG
          END

          IF PRT.OFF THEN
             PRINTER.OFF DOC.ID
             IF NOT(VIEW) THEN
                SEND.MESSAGE 'Phantom',USER.ID,REP.TL:' is Complete'
             END
          END


          RETURN
*-------------------------------------------------------------------------*
MAKE.TOTAL: *
          LINE       = 'Total'
          *** If Individual Branch Consolidated then print as Branch Totals
          IF ID = 'BR5 P&L' THEN
          RETURN
          END

          IF CHOICE # 2 THEN
             LINE<1,3>  = 'Total for ':TITLE:':'
          END ELSE
             LINE<1,3>  = 'Total for Branch ':BRCHS<1,BRCH.CNT>:':'
          END
          LINE<1,10> = USER.OPTS<1,3>

          IF FIELD(ID,'.',1) # 'BUSINESS' THEN
          GL.GRP<-1> = LINE
          END

          RETURN
*-------------------------------------------------------------------------*
CONS.TOTAL: *
          LINE       = 'CTotal'
          LINE<1,3>  = 'Consolidated Total for ':TITLE:':'
          LINE<1,10> = USER.OPTS<1,3>

*          GL.GRP<-1> = LINE

          RETURN
*-------------------------------------------------------------------------*
MAKE.DATES: *
          IF NOT(NUM(REPORT(9))) THEN
             REALDATE AOD,REPORT(9),DATE()
          END ELSE
             AOD = REPORT(9)
          END
          FOR D = 1 TO DCOUNT(REP.ARRAY<1,12>,SVM)
             REALDATE TMP1,REPORT(12)<1,D>,AOD
             REP.ARRAY<1,17,D> = TMP1
             REALDATE TMP2,REPORT(11)<1,D>,AOD
             REP.ARRAY<1,16,D> = TMP2
          NEXT D

          RETURN
*-------------------------------------------------------------------------*
INIT:     *
          ACCT.AMTS = ''
          FOR T = 1 TO DCOUNT(REPORT(15),VM)
             GID = REPORT(15)<1,T>
             IF NUM(GID) AND GID#'' THEN
                RET.AMTS=0
                GL.READ GID,TMP.REC,TMP.GRP,TMP.GRP,TMP.OPTS,TMP.ERR
                IF NOT(TMP.GRP) THEN
                   BEGIN CASE
                   *** Report Type = 'Listing of Accounts'...
                   CASE REPORT(1) = 1
                      SDATE = ''
                      EDATE = ''
                   *** Report Type = 'Balance Sheet'...
                   CASE REPORT(1) = 2
                      SDATE = ''
                      EDATE = REP.ARRAY<1,17,T>
                   CASE OTHERWISE
                      SDATE = REP.ARRAY<1,16,T>
                      EDATE = REP.ARRAY<1,17,T>
                   END CASE

                   IF EDATE='' THEN EDATE = AOD
                   GL.GET.AMT GID,'',BRANCHES,EDATE,RET.AMTS,SDATE,REPORT(27)
                END ELSE
          *** This section builds any Template account column totals
                   TMP.ARRAY = REP.ARRAY
                   TMP.ARRAY<1,10> = ''
                   TMP.ARRAY<1,11> = REP.ARRAY<1,11,T>
                   TMP.ARRAY<1,12> = REP.ARRAY<1,12,T>
                   TMP.ARRAY<1,13> = ''
                   TMP.ARRAY<1,15> = ''
                   TMP.ARRAY<1,16> = REP.ARRAY<1,16,T>
                   TMP.ARRAY<1,17> = REP.ARRAY<1,17,T>
                   TMP.ARRAY<1,18> = 'A'
              *** RET.AMTS is going to return the amount for the template
              *** TMP.GRP is being passed in and makes up the Accounts
              *** that will be computed in RET.AMTS
              *** GID is the ID of the template found in GENLED
              *** TMP.ARRAY is a mocked up report to fake GL.REC.REPORT to
              *** set up A.
                   GL.REC.REPORT RET.AMTS,TMP.GRP,0,0,0,,GID,NO,BRANCHES,TMP.ARRAY,,,,1
                END

                ACCT.AMTS<1,T,1> = RET.AMTS<4>
                IF RET.AMTS<4> = 0 OR RET.AMTS<4> = '' THEN
                   ACCT.AMTS<1,T,2> = 0
                END ELSE
                   ACCT.AMTS<1,T,2> = RET.AMTS<1>/RET.AMTS<4>
                END
             END

          NEXT T
          RETURN
*-------------------------------------------------------------------------*
MAKE.HDR: *
          HDR     = ''
          FDR     = ''
          COL.HDG = ''
          HDR.WIDTH = 30
          FOR J = 1 TO DCOUNT(REPORT(13),VM)
             FMT.LEN = REPORT(29)<1,J>
             IF NOT(FMT.LEN) THEN FMT.LEN = 15

             BEGIN CASE
                *** Account #...
                CASE REPORT(18)<1,J>      = 'AC';   FMT = "L#":FMT.LEN
                *** Account Segment...
                CASE REPORT(18)<1,J>      = 'SEG';  FMT = "L#":FMT.LEN
                *** Full Description...
                CASE REPORT(18)<1,J>      = 'FD';   FMT = "L#":FMT.LEN
                *** Internal Description...
                CASE REPORT(18)<1,J>      = 'ID';   FMT = "L#":FMT.LEN
                *** Short Description...
                CASE REPORT(18)<1,J>      = 'SD';   FMT = "L#":FMT.LEN
                *** Keyword...
                CASE REPORT(18)<1,J>[1,1] = 'K';    FMT = "L#":FMT.LEN
                *** Internal Description...
                CASE REPORT(18)<1,J>      = 'IN';   FMT = "L#":FMT.LEN
                *** Report Type = 'Listing of Accounts'...
                CASE REPORT(1)            = 1;      GOTO NEXT.J
                *** Otherwise...
                CASE OTHERWISE;                     FMT = "L#":FMT.LEN
             END CASE

             COL.HDG := REPORT(13)<1,J>  FMT
NEXT.J:   NEXT J

          WIDTH = LEN(COL.HDG)
          IF FIELD(ID,'.',1)='BUSINESS' THEN
          IF WIDTH < 132 THEN WIDTH = 80
          END ELSE
          IF WIDTH < 132 THEN WIDTH = 132
          END


          IF WIDTH > 200 THEN WIDTH = 200




          CNTR = TITLE

          *** Go Center the Title...
          GOSUB CNTRIZE

          HDR<1,1> = CNTR

          FMT = "L#":(WIDTH - 6)
          HDR<1,1> = HDR<1,1> FMT:REPORT(27)"R#6"

          IF NOT(VIEW) THEN
             CNTR     = REPORT(23)
             GOSUB      CNTRIZE
             HDR<1,2> = CNTR

             NXT.HD = 3

             *** If BR.NAME specified use it for the header
             IF BR.NAME # '' AND (CHOICE = 2 OR CHOICE = 4) THEN
                CNTR = 'Branches: ':BR.NAME
                GOSUB CNTRIZE
                HDR<1,NXT.HD> = CNTR
                NXT.HD += 1
             END ELSE
                *** If Length of branches is large than break it down.
                IF LEN(DISP.BRCHS) < 60 THEN
                   CNTR     = 'Branches: ':DISP.BRCHS
                   GOSUB      CNTRIZE
                   HDR<1,NXT.HD> = CNTR
                   NXT.HD += 1
                END ELSE
                   FIRST.CHAR = 1
                   LEN.CT = LEN(DISP.BRCHS)
                   FOR ILEN = 1 TO LEN.CT
                      *** If first pass then limit to 60 characters
                      *** otherwise print out 70 characters
                      IF FIRST.CHAR = 1 THEN
                         IF (ILEN > 60 AND DISP.BRCHS[ILEN,1] = ',') OR ILEN = LEN.CT THEN
                            IF ILEN = LEN.CT THEN ILEN += 1
                            CNTR  = 'Branches: ':DISP.BRCHS[1,ILEN-1]
                            CONVERT ',' TO ' ' IN CNTR
                            *** Now set up the Heading
                            GOSUB CNTRIZE
                            FIRST.CHAR = ILEN + 1
                            HDR<1,NXT.HD> = CNTR
                            NXT.HD += 1
                         END
                      END ELSE
                         *** Now loop through rest limiting to 70 chars
                         *** per line.
                         IF (ILEN > (70 + FIRST.CHAR) AND DISP.BRCHS[ILEN,1] = ',') OR ILEN = LEN.CT THEN
                            IF ILEN = LEN.CT THEN ILEN += 1
                            CHAR.LEN = ILEN - FIRST.CHAR
                            CNTR = DISP.BRCHS[FIRST.CHAR,CHAR.LEN]
                            *** Now set up the Heading
                            GOSUB CNTRIZE
                            FIRST.CHAR = ILEN + 1
                            HDR<1,NXT.HD> = CNTR
                            NXT.HD += 1
                         END
                      END
                   NEXT ILEN
                END
             END
             CNTR     = 'As of Date: ':OCONV(AOD,'D4/')
             GOSUB      CNTRIZE
             HDR<1,NXT.HD> = CNTR
             NXT.HD += 1

             IF NXT.HD > 6 THEN
                HDR<1,NXT.HD> = COL.HDG
             END ELSE
                HDR<1,6> = COL.HDG
             END


             IF FIELD(ID,'.',1)='BUSINESS' THEN
             CONVERT ',' TO ' ' IN HDR<1,3>
             END

             CNTR = COL.HDG
             GOSUB CNTRIZE


             IF ID = 'BR5 P&L' OR FIELD(ID,'.',1)= 'BUSINESS' THEN
             CNTR = ''
             END ELSE
             CNTR     = 'Page :^#####'
             END
             GOSUB      CNTRIZE
             FDR<1,2> = CNTR
          END

          RETURN
*-------------------------------------------------------------------------*
CNTRIZE:  *
          CNTR = TRIM(CNTR,' ')
          WIDE = WIDTH - LEN(CNTR)
          WIDE = WIDE/2
          CNTR = SPACE(WIDE):CNTR

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~02/12/14~10:03
