          SUBROUTINE (SEND.DATA,SORTFILE,ERROR,DOC.IDS,SOLAR.INTERACTIVE,SOLAR.PTR.ID)
** Version# 135.0002[6] - 01/14/2014 - 12:35pm - TSMITH - eclipse
*** V135.0002 Change - Custom Coding . - 01/14/2014 - TSMITH - eclipse

*** Subroutine: BATCH.DATA.SEND
*-------------------------------------------------------------------------*
*** Routine to print Batch Invoices (BATCH.INV.RUN) or Batch Statements
*** (BATCH.STMT.RUN).  This routine simply calls the appropriate program to
*** handle the output depending on the action type (Print, Email, EDI etc)
*-------------------------------------------------------------------------*
*** SEND.DATA     - Array of data used to determine how to send      [IN]
*** SEND.DATA<1>  - Batch Type
*** SEND.DATA<2>  - Action Override
*** SEND.DATA<3>  - Status
*** SEND.DATA<4>  - Print Style Override
*** SEND.DATA<5>  - DRPT
*** SEND.DATA<6>  - As of Date
*** SEND.DATA<7>  - Age as of date
*** SEND.DATA<8>  - Home Branches
*** SEND.DATA<9>  - Home Branch Option
*** SEND.DATA<10> - Sales Branches
*** SEND.DATA<11> - Customer IDS
*** SEND.DATA<12> - Print Invoices Flag
*** SEND.DATA<13> - Statement Option
*** SEND.DATA<14> - Sort By
*** SEND.DATA<15> - Customer Statement Cycle
*** SEND.DATA<16> - Batch Number
*** SEND.DATA<17> - Ftp Option
*** SEND.DATA<18> - Printing Override Flag
*** SEND.DATA<19> - Minimum Balance Flag
*** SORTFILE      - Tempfile which contains data required to batch   [IN]
***               - print invoice and/or statements.
*** ERROR         - Flag to indicate an error has occurred.          [OUT]
*** DOC.IDS       - AM delim list of documents for solar to print    (OUT)
*** SOLAR.INTERACTIVE - this is the flag that is used exclusively in Solar
*** invoice printing. To make sure that this is set correctly when it's
*** supposed to, it has to be set to "$SOLAR.PRINTING$". Any setting
*** diffErent than that will cause it to be set to a null value. The reason
*** we're doing this is if this flag is set, an interactive session will be
*** established between Solar and the host. The host will block waiting
*** for a response and Solar also expects continuous replies from the host
*** SOLAR.PTR.ID - If this is passed in with a value, then we         (IN)
***              - already have a printer ID to be used when
***              - printing in Solar.
*-------------------------------------------------------------------------*
*** Common Variables: CUS,CUSS,LED,ACTIVE.FORMS$
*-------------------------------------------------------------------------*
          * Initialize any data needed for this subroutine
          PRINT.COUNT = 0
          NUM.PGS     = 0
          IF SOLAR.INTERACTIVE #'$SOLAR.PRINTING$' THEN
             SOLAR.INTERACTIVE = ''
          END
          IF SOLAR.INTERACTIVE THEN
             *** Wait for a response from the client before proceeding.
             SOLAR.J2EE.REQ.DATA '   Preparing to Send Data ...',SOLAR.DATA,ERROR.MSG
          END
          GOSUB INIT.DATA

          * Generate the statements and/or invoices
          IF SOLAR.INTERACTIVE THEN
             *** Wait for a response from the client before proceeding.
             SOLAR.J2EE.REQ.DATA '  Generating Invoices ...',SOLAR.DATA,ERROR.MSG
          END
          GOSUB OUTPUT.DATA

          * Log what was actually outputted
          GOSUB LOG.DATA

FINISH:   *** exit this routine
          RETURN
*-------------------------------------------------------------------------*
INIT.DATA: *** Init data

          IF HAJOCA.SITE$ THEN
             UT.TEMPFILE.CREATE FLNM.ID,TMPFILE
          END

          UT.OPEN.FILE '&HOLD&',HOLDFILE,ERR.MSG,YES

          FAX.PRT      = NO
          DOC.IDS      = ''                      ;* Solar Only
          DOCS.PRDCD   = ''
          BATCH.TYPE   = SEND.DATA<1>            ;* 'STMT' or 'BATCH.INV'
          ACTION.OVRD  = SEND.DATA<2>            ;* Action Override
          STATUS       = SEND.DATA<3>            ;* Status
          PSTYLE.OVRD  = SEND.DATA<4>            ;* Print Style Override
          STMT.DATA    = SEND.DATA[AM,5,99]
          DRPT         = RAISE(STMT.DATA<1>)     ;* Standard reporting vars

          IF BATCH.TYPE = 'STMT' THEN
             END.DATE     = STMT.DATA<2>         ;* As of date
             AGE.END.DATE = STMT.DATA<3>         ;* Age as of date
             SALE.BRS     = RAISE(STMT.DATA<6>)  ;* Sales branches
             STMT.OPTS    = RAISE(STMT.DATA<8>)  ;* Statement Options
             BATCH.NUM    = STMT.DATA<12>        ;* Batch#
             EXT.FTP      = STMT.DATA<13>        ;* FTP option
             MIN.BAL      = STMT.DATA<15>        ;* Minimum Balance
          END ELSE
             END.DATE     = ''                   ;* As of date
             AGE.END.DATE = ''                   ;* Age as of date
             SALE.BRS     = STMT.DATA<2>         ;* Sales branches
             STMT.OPTS    = ''                   ;* Statement Options
             SEL.BR.VM    = STMT.DATA<7>         ;* Branch Option
             BATCH.NUM    = STMT.DATA<9>         ;* Batch#
             EXT.FTP      = STMT.DATA<11>        ;* FTP option
          END

          IF UNASSIGNED(SOLAR.PTR.ID) THEN SOLAR.PTR.ID = ''

          FILE         = ''
          FTP.ERROR    = NO
          ABORT.BATCH  = NO
          HOLD.FLAG    = NO
          PRT.ON       = NO
          PRT.ID       = PRINTER.ID
          PRT.SET      = NO
          FAX.DRPT     = ''
          OLD.EMAIL    = ''
          OLD.EM.MINVS = ''
          EM.MULT.INVS = ''
          EM.OIDS      = ''
          EM.GENS      = ''
          EM.INVS      = ''
          EM.BT        = ''
          EM.ST        = ''
          EM.GN.STAT   = ''
          OLD.FAX.NO   = ''
          OLD.FAX.BR   = ''
          OLD.BT.CN    = ''
          OLD.ST.CN    = ''
          ST.CN        = ''
          BT.CN        = ''
          CHECK.ALIGN  = YES
          STMTS.OUT    = ''
          FORM.INFO    = ''
          FORMS.OUT    = ''
          FSCP.OIDS    = ''
          FSCP.GIDS    = ''
          FSCP.GENS    = ''
          FSCP.FMTS    = ''
          FSCP.CNS     = ''
          FSCP.HDRS    = ''
          FSCP.ITMS    = ''
          FSCP.FTRS    = ''
          FSCP.NUM.CPS = ''
          ABC.IDS      = ''
          TITLE        = ''
          SAVE.TITLE   = ''
          SEND.FTP     = ''
          BT.VERSION   = ''
          PLOG.LIST    = ''
          PLOG.CT      = 0
          DOCS.PRDCD   = ''
          LAST.PRINT   = ''
          LAST.DOC     = "@@@"

          TIME.NOW    = TIME()
          DATE.NOW    = DATE()
          NOW         = OCONV(TIME.NOW,"MTS")
          HOUR        = FIELD(NOW,':',1)
          MIN         = FIELD(NOW,':',2)
          SEC         = FIELD(NOW,':',3)

          DATE.NOW    = DATE()
          TODAY       = OCONV(DATE.NOW,'D2/')
          YEAR        = FIELD(TODAY,'/',3)
          MO          = FIELD(TODAY,'/',2)
          DAY         = FIELD(TODAY,'/',1)

          OLD.FORM    = ''
          NEW.FORM    = ''

          READ DFLTS FROM CTRLFILE,'BATCH.DFLT.SETTINGS' ELSE DFLTS = ''
          INV.FTP.TEST  = DFLTS<4>
          STMT.FTP.TEST = DFLTS<5>
          FTP.ACTIONS   = DFLTS<6>

          * Read in the maximum number of forms to include per batch.
          CTRL.ID = "ECLIPSE.FORMS.DEFAULTS"
          READV FSCP.PRT.MAX FROM CTRLFILE,CTRL.ID,7 ELSE FSCP.PRT.MAX = 1

          INVN.LIST      = ""
          BATCH.FAX.INFO = ""

          BEGIN CASE
          CASE BATCH.TYPE = 'BATCH.INV'
             * Batch printing invoices
             IF EXT.FTP THEN
                READ FTP.INFO FROM CTRLFILE,'BATCH.INV.FTP.INFO' ELSE
                   ERR.MSG  = 'An error occurred when trying to read '
                   ERR.MSG := 'the Batch Invoice FTP Information. The '
                   ERR.MSG := 'record does not appear to exist. '
                   ERR.MSG := 'Please verify the settings by '
                   ERR.MSG := 'using the "FTP Maintenance" hot-key '
                   ERR.MSG := 'off of the "Print Invoices" screen. '
                   BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                   ERROR     = 'MINOR'
                   FTP.ERROR = YES
                END
                FILE.TYPE = BATCH.TYPE
                GOSUB CREATE.FILE
             END
          CASE BATCH.TYPE = 'STMT'
             * Batch printing statements
             STATUS = 'I'
             IF EXT.FTP THEN
                READ FTP.INFO FROM CTRLFILE,'BATCH.STMT.FTP.INFO' ELSE
                   ERR.MSG  = 'An error occurred when trying to read '
                   ERR.MSG := 'the Statement FTP Information. The '
                   ERR.MSG := 'record does not appear to exist. '
                   ERR.MSG := 'Please verify the settings by '
                   ERR.MSG := 'using the "FTP Maintenance" hot-key '
                   ERR.MSG := 'off of the "Print Statements" screen. '
                   BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                   ERROR     = 'MINOR'
                   FTP.ERROR = YES
                END
                FILE.TYPE = BATCH.TYPE
                GOSUB CREATE.FILE

                * If batch num exists, then we are outputting invoices
                * as well, we need to create a seperate file for that.
                IF BATCH.NUM THEN
                   READ FTP.INFO FROM CTRLFILE,'BATCH.INV.FTP.INFO' ELSE
                      ERR.MSG  = 'An error occurred when trying to read'
                      ERR.MSG := ' the Batch Invoice FTP Information. '
                      ERR.MSG := 'The record does not appear to exist. '
                      ERR.MSG := 'Please verify the settings by '
                      ERR.MSG := 'using the "FTP Maintenance" hot-key '
                      ERR.MSG := 'off of the "Print Invoices" screen. '
                      BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                      ERROR     = 'MINOR'
                      FTP.ERROR = YES
                   END
                   FILE.TYPE = 'BATCH.INV'
                   GOSUB CREATE.FILE
                END
             END
          CASE OTHERWISE
             ERROR = 'Invalid batch type ':BATCH.TYPE
             RETURN TO FINISH
          END CASE

          RETURN
*-------------------------------------------------------------------------*
OUTPUT.DATA: *** Iterate through the tempfile to batch print statements
          *** and/or invoices

          ACTION         = ''
          OLD.ACTION     = ''
          CURRENT.ACTION = ''

          SSELECT SORTFILE
          READLIST ACTION.LIST ELSE ACTION.LIST = ''
          TOTAL.CT = DCOUNT(ACTION.LIST,AM)
          MOREDATA = (ACTION.LIST#'')

          CT = 0
          LOOP WHILE MOREDATA
             REMOVE SORT.ID FROM ACTION.LIST SETTING MOREDATA
             READ ENTRY FROM SORTFILE,SORT.ID ELSE ENTRY = ''
             CURRENT.ACTION = ENTRY<1>

             GOSUB PRINTONE
             IF ABORT.BATCH THEN EXIT         ;* halt all batch processing

             CT += 1
             IF MOD(CT,100) = 0 THEN          ;* update the process status
                PERC = ICONV((CT/TOTAL.CT)*100,'MR0')
                CURRENT.ACTION = CONVERT(VM,',',CURRENT.ACTION)
                STAT = CURRENT.ACTION:'... ':CT:'/':TOTAL.CT:' (':PERC:'%)'
                IF SOLAR.INTERACTIVE THEN
                   *** Wait for a response from the client before proceeding.
                   SOLAR.J2EE.REQ.DATA CT:'~':TOTAL.CT,SOLAR.DATA,ERROR.MSG
                END
                PH.STATUS STAT
             END
          REPEAT

          IF USE.ECLIPSE.FORMS$ OR ST.CN = "4503" THEN
             * If the last action is email, make sure we send it off.
             IF CURRENT.ACTION = 'EMAIL' AND EM.MULT.INVS AND EM.OIDS # "" THEN
                GOSUB GEN.INV.EMAIL
             END ELSE
                GOSUB SEND.ECLIPSE.FORMS
             END
          END

          * Check to see if the printer is on, if so, turn it off
          IF PRT.ON THEN
             PRT.ON = NO
             PRINTER.OFF DOC.ID
             IF JAVA.PROC$ AND NOT(FAX.PRT) THEN
                DOC.IDS<-1> = DOC.ID
             END
             FAX.PRT       = NO
             ACTIVE.FORMS$ = ''
          END
          IF ABORT.BATCH THEN RETURN          ;* halt all batch processing

          * Check to see if we are sending external FTPs, if we are
          * then we need to send off the file at this time.
          IF EXT.FTP THEN
             FTP.ERR = ''
             LOG.MSG = ''
             BATCH.FTP.SEND BATCH.TYPE,FILE,FTP.ERR
             INV.FTP.ERR = NO
             IF BATCH.TYPE = 'STMT' AND BATCH.NUM THEN
                BATCH.FTP.SEND 'BATCH.INV',FILE,INV.FTP.ERR
             END

             IF FTP.ERR OR INV.FTP.ERR THEN
                OTHER.FTP.ERR = NO
                IF INV.FTP.ERR THEN
                   IF INV.FTP.ERR = 'NO ITEMS' THEN
                      LOG.MSG := 'No invoices were sent via FTP. '
                   END ELSE
                      OTHER.FTP.ERR = YES
                   END
                END
                IF FTP.ERR THEN
                   IF FTP.ERR = 'NO ITEMS' THEN
                      LOG.MSG := 'No items were sent via FTP. '
                   END ELSE
                      OTHER.FTP.ERR = YES
                   END
                END
                IF OTHER.FTP.ERR THEN
                   ERROR    = FTP.ERR
                   LOG.MSG := 'An error occured while trying to FTP the '
                   LOG.MSG := 'batch process. Fortunately, no data was '
                   LOG.MSG := 'lost and the file can be resent once the '
                   LOG.MSG := 'problem is resolved. '

                   IF FTP.ERR = 'FATAL' THEN
                      LOG.MSG := 'Please contact Eclipse immediately and '
                      LOG.MSG := 'do NOT run any batch processes until '
                      LOG.MSG := 'the issue is resolved. '
                   END
                END
                BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',LOG.MSG
             END
          END

          * Activity Based Logging
          CNT = DCOUNT(ABC.IDS,AM)
          OID.DATA$ = ''
          FOR X = 1 TO CNT
             OID.DATA$<4> = FIELD(ABC.IDS<X>,'~',3)
             SVIA         = FIELD(ABC.IDS<X>,'~',1)
             ABC.BR       = FIELD(ABC.IDS<X>,'~',2)

             * 1st read actual ABC Code from shipvia.  If none exists then
             * try to get auto.grp.code
             READV ACD FROM SVIAFILE,SVIA,27 ELSE
                READV ACD FROM SVIAFILE,SVIA,26 ELSE ACD = ''
             END

             ACD<1,2>  = ABC.BR
             SHP.DT    = FIELD(ABC.IDS<X>,'~',4)
             ADDL.ATTR = 18
             ADDL.DATA = SHP.DT
             GOSUB LOG.ABC
          NEXT X

          RETURN
*-------------------------------------------------------------------------*
LOG.DATA: *** Log what was actually outputted

          BEGIN CASE
          CASE BATCH.TYPE = 'BATCH.INV'
             * Batch Printing Invoices
             ITEM.TYPE = BATCH.TYPE
             TMP.OUT   = FORMS.OUT
             GOSUB LOG.ITEMS.CT
             IF NOT(INV.FTP.TEST) THEN
                PL.ID = 'SI':'*':BATCH.NUM
                WRITE PLOG.LIST ON PLOGFILE,PL.ID
                MSG = 'Batch : ':BATCH.NUM:' Invoices Processed '
                SYS.LOG.CMT MSG,'INVOICES'
             END
          CASE BATCH.TYPE = 'STMT'
             * Batch Printing Statements
             ITEM.TYPE = BATCH.TYPE
             TMP.OUT   = STMTS.OUT
             GOSUB LOG.ITEMS.CT
             IF BATCH.NUM THEN
                * Including Printing of Invoices
                ITEM.TYPE = 'BATCH.INV'
                TMP.OUT = FORMS.OUT
                GOSUB LOG.ITEMS.CT
                IF NOT(INV.FTP.TEST) THEN
                   PL.ID = 'SI':'*':BATCH.NUM
                   WRITE PLOG.LIST ON PLOGFILE,PL.ID
                   MSG = 'Batch : ':BATCH.NUM:' Invoices Processed '
                   SYS.LOG.CMT MSG,'INVOICES'
                END
             END
          END CASE

          LOG.MSG = TODAY:VM:NOW:VM:BATCH.NUM
          BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',LOG.MSG

          RETURN
*-------------------------------------------------------------------------*
PRINTONE: *** Print a single statement or invoice for a given customer
          PRINT.COUNT += 1
          NUM.PGS      = 0
          IF MOD(PRINT.COUNT,5)=0 THEN
             IF SOLAR.INTERACTIVE THEN
                *** Wait for a response from the client before proceeding.
                SOLAR.J2EE.REQ.DATA  ' ':PRINT.COUNT:' Invoices Output ...',SOLAR.DATA,ERROR.MSG
             END
          END
          SEND.FTP = NO
          BT.CN    = FIELD(SORT.ID,'~',4)   ;* Bill-to Customer
          ST.CN    = FIELD(SORT.ID,'~',6)   ;* Ship-to Customer
          GL.DATE  = FIELD(SORT.ID,'~',7)   ;* Post Date
          AR.ID    = FIELD(SORT.ID,'~',8)   ;* Accural Register @id
          IF NOT(ST.CN) THEN ST.CN = BT.CN

          * Get the action for the given record. The base statement
          * records will not contain a G/L Date or A/R id. This
          * is how the differenciation is happening right now
          ACTION       = ENTRY<1>             ;* Action to take
          ACT.STAT     = ENTRY<2>             ;* Action status
          ACT.CT       = ENTRY<3>             ;* Number of actions to take
          BATCH.FLG    = ENTRY<4>             ;* Batch Statement Customer
          EM.MULT.INVS = (ENTRY<5> = 'M')     ;* Multiple Inv Per Email
          IF NOT(ACT.CT) THEN ACT.CT = 1
          IF ACTION.OVRD THEN ACT.STAT = 'OK'

          * Check the actions, if they differ and the printer is on
          * then turn off the printer.
          IF (ACTION # OLD.ACTION) THEN
             IF USE.ECLIPSE.FORMS$ THEN
                BEGIN CASE
                CASE ((OLD.ACTION = "PRINT") OR (OLD.ACTION = "FAX"))
                   GOSUB SEND.ECLIPSE.FORMS
                CASE OLD.ACTION = "EMAIL" AND OLD.EM.MINVS
                   ACTION   = OLD.ACTION
                   ACT.STAT = OLD.ACT.STAT

                   GOSUB GEN.INV.EMAIL

                   ACTION       = ENTRY<1>
                   ACT.STAT     = ENTRY<2>
                   EM.MULT.INVS = (ENTRY<5> = 'M')
                END CASE
             END

             OLD.ACTION   = ACTION
             OLD.ACT.STAT = ACT.STAT
             IF PRT.ON THEN
                PRT.ON = NO
                PRINTER.OFF DOC.ID
                IF JAVA.PROC$ AND NOT(FAX.PRT) THEN
                   DOC.IDS<-1> = DOC.ID
                END
                FAX.PRT       = NO
                ACTIVE.FORMS$ = ''
             END
          END

          IF NOT(ACTION) THEN
             ACTION   = 'ERROR'
             ACT.STAT = 'NO ACTION DEFINED'
          END ELSE
             OK.REQUIRED = (ACTION # 'EMAIL') AND (ACTION # 'XML') AND (ACTION = 'EDI' AND BATCH.FLG # 'Trigger')
             IF (ACT.STAT # 'OK') AND OK.REQUIRED THEN
                ACTION = 'ERROR'
             END ELSE
                IF EXT.FTP THEN
                   FTP.CT = DCOUNT(FTP.ACTIONS<1>,VM)
                   FOR FTX = 1 TO FTP.CT
                      IF ACTION = FTP.ACTIONS<1,FTX> THEN
                         SEND.FTP = YES
                      END
                   NEXT FTX
                END
             END
          END

          * FTP test only sends out FTPs, therefore continue if
          * the action is anything other than FTP
          IF NOT(SEND.FTP) THEN
             BEGIN CASE
             CASE BATCH.TYPE = 'BATCH.INV'
                IF INV.FTP.TEST  THEN RETURN
             CASE BATCH.TYPE = 'STMT'
                IF STMT.FTP.TEST THEN RETURN
             END CASE
          END

          * Generate the apropriate document based on the batch type
          BEGIN CASE
          CASE BATCH.TYPE = 'BATCH.INV'
             * Batch Print Invoices
             ITEM.TYPE = 'BATCH.INV'
             FAX.REC   = 'INV.FAX.DATA'
             FAX.NAME  = 'INV.FAX'
             GOSUB OUTPUT.ORDER
          CASE BATCH.TYPE = 'STMT' AND AR.ID
             * Print Invoices with Statements
             IF LAST.PRINT = 'STMT' AND USE.ECLIPSE.FORMS$ THEN
                GOSUB SEND.ECLIPSE.FORMS
             END
             ITEM.TYPE = 'BATCH.INV'
             * We should be respecting the fax date/time settings on
             * the statement since that is what the user looks at/sets when
             * generating this.
             FAX.REC   = 'STMT.FAX.DATA'
             FAX.NAME  = 'INV.FAX'
             GOSUB OUTPUT.ORDER
             LAST.PRINT = 'STMT.INV'
          CASE BATCH.TYPE = 'STMT'
             * Batch Print Statements
             IF LAST.PRINT = 'STMT.INV' AND USE.ECLIPSE.FORMS$ THEN
                GOSUB SEND.ECLIPSE.FORMS
             END
             ITEM.TYPE = 'STMT'
             FAX.REC   = 'STMT.FAX.DATA'
             FAX.NAME  = 'STMT.FAX'
             PRINT.QID = ""
             GOSUB OUTPUT.STATEMENT
             LAST.PRINT = 'STMT'
          END CASE

          RETURN
*-------------------------------------------------------------------------*
SEND.ECLIPSE.FORMS: *** Call XML.RUN passing in list of OIDs & GIDS
          BEGIN CASE

          CASE FSCP.OIDS
             * Log invoice printing.
             * Setup archive information to indicate this is an invoice.
             ARCH.DATA      = "SOE-INV"
             ARCH.DATA<2,1> = OID
             ARCH.DATA<2,2> = GEN
             XML.RUN "ORDER.BATCH.INV",XML,ARCH.DATA,ERR.MSG,FSCP.OIDS,FSCP.GIDS,FSCP.FMTS
             FORMS.WRITE.XML "ORDER.BATCH.INV",XML

             FSCP.OIDS = ''
             FSCP.GIDS = ''
             FSCP.GENS = ''
             FSCP.FMTS = ''
             BATCH.FAX.INFO = ""
          CASE FSCP.CNS
             * Setup archive information to indicate this is a statement.
             ARCH.DATA    = "ACCT-STMT"
             ARCH.DATA<2> = FSCP.CNS
             AOD          = SEND.DATA<6>
             IF (AOD # "") THEN
                AOD = OCONV(AOD,"D4/")
             END
             ARCH.DATA<3> = "As Of ":AOD
             XML.RUN 'STMT.OUT',XML,ARCH.DATA,ERR.MSG,FSCP.CNS,FSCP.HDRS,FSCP.ITMS,FSCP.FTRS,FSCP.FMTS

             FORMS.WRITE.XML 'STMT.OUT',XML

             FSCP.CNS  = ''
             FSCP.HDRS = ''
             FSCP.ITMS = ''
             FSCP.FTRS = ''
             FSCP.FMTS = ''
             BATCH.FAX.INFO = ""
          END CASE

          RETURN
*-------------------------------------------------------------------------*
OUTPUT.ORDER: *** Print a single order/invoice

          NUM.PGS     = 0
          REFRESH.LED = NO

          * Get the OID and the GID
          OID  = FIELD(AR.ID,'.',1)
          INVN = FIELD(AR.ID,'.',2)+0

          * Read the ledger record in
          MATREAD LED FROM LEDFILE,OID ELSE RETURN

          * Set the generation
          LOCATE INVN IN LED(8)<1> SETTING GEN ELSE RETURN

          * Set the BT/ST
          BT.CN     = LED(1)<1,GEN>           ;* Bill-to customer
          ST.CN     = LED(5)<1,GEN>           ;* Ship-to customer
          INVN      = LED(8)<1,GEN>           ;* Invoice#
          GID       = LED(12)<1,GEN>          ;* Generation ID
          GN.STAT   = LED(6)<1,GEN>
          IF BATCH.TYPE = 'STMT' THEN
             GET.DFLT.BRANCH SELECT.BR
          END ELSE
             SELECT.BR = LED(2)<1,GEN,SEL.BR.VM> ;* Shipping/Pricing Branch
          END
          PRINT.QID = OID:'.':GID             ;* PRINT.QUEUE ID
          * Set up CUS and CUSS
          GET.CUS SELECT.BR,BT.CN,ST.CN,-1
          DISABLE.FTP = CUSS(50)<1,3>
          GOSUB SET.COMMON.DATA

          BEGIN CASE
          CASE STATUS = 'I' AND LED(30)<1,GEN> = 'CINV'
             * Consolidated Invoices
             SPEC  = "INVOICE"
             IF BATCH.NUM THEN
                TITLE = 'Consolidated Batch Invoice # ':BATCH.NUM
             END
             INVOICE.COPY.CT$ = 0 ;* used for indicating duplicates
             GOSUB SET.ABC.LOG
          CASE STATUS = 'I'
             * Single Invoices
             SPEC  = "INVOICE"
             IF BATCH.NUM THEN
                TITLE = 'Batch Invoice # ':BATCH.NUM
             END
             INVOICE.COPY.CT$ = 0 ;* used for indicating duplicates
             GOSUB SET.ABC.LOG
          CASE STATUS = 'B'
          CASE STATUS = 'O'
          CASE STATUS = 'T'
          CASE STATUS = 'A'
          CASE OTHERWISE
             SPEC = 'SALES.ORDER'
          END CASE

          * Output a single invoice
          BEGIN CASE
          CASE SEND.FTP AND NOT(DISABLE.FTP)
             * 3rd party FTP
             IF FTP.ERROR THEN
                ERR.MSG  = 'Due to previous FTP setup errors, order: '
                ERR.MSG := OID:'.':INVN:' was skipped and not sent. '
                ERR.MSG := 'The print flag has not been changed '
                ERR.MSG := 'and you can run the batch process again '
                ERR.MSG := 'once the FTP setup issue is fixed. '
                BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                ERROR = 'MINOR'
                RETURN
             END

             OUTPUT.VIA = ACTION
             IF ACTION = 'EMAIL' THEN
                OUTPUT.VIA = OUTPUT.VIA:VM:ACT.STAT
             END

             FTP.ERR = ''
             INVOICE.COPY.CT$ = 0
             FOR NPX = 1 TO ACT.CT
                * Log the ABC Info
                IF STATUS = 'I' THEN
                   ACD = 'INV.'
                   BEGIN CASE
                   CASE OUTPUT.VIA<1,1> = 'PRINT'
                      ACD := 'FTPP'
                   CASE OUTPUT.VIA<1,1> = 'FAX'
                      ACD := 'FTPF'
                   CASE OUTPUT.VIA<1,1> = 'EMAIL'
                      ACD := 'FTPE'
                   END CASE
                   GOSUB LOG.ABC
                   INVOICE.COPY.CT$ = NPX
                END
                BATCH.INV.FTP.SET OID,GID,OUTPUT.VIA,FILE,FTP.ERR
             NEXT NPX

             * No errors exist right now, so this is irrelevant
             IF FTP.ERR THEN RETURN
          CASE ACTION = 'ERROR'
             * There is an error with this inv, notify user
             ERR.MSG  = 'Order ':OID:'.':INVN:' had problems sending. '
             ERR.MSG := 'The print flag has not been changed '
             ERR.MSG := 'and you can run the batch process again '
             ERR.MSG := 'once the problem is fixed. The '
             ERR.MSG := 'error returned was: ':ACT.STAT
             BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
             ERROR = 'MINOR'
             RETURN
          CASE INV.FTP.TEST
             * If test invoices is set, then we do not want to perform
             * any non-FTP action.  Acknowledge the invoice was found
             * but don't process it.
             GOSUB SET.ITEM.CT
             RETURN
          CASE ACTION = 'EDI'
             * Handle standard EDI
             IF STATUS = 'I' THEN
                ACD = 'INV.EDI'
                GOSUB LOG.ABC
             END

             * For each unique BT customer, record in the EDI Activity Log
             IF OLD.BT.CN # BT.CN THEN
                EDI.MSG = 'Processing Invoices for Cust#:':BT.CN
                EDI.LOG EDI.MSG
                OLD.BT.CN = BT.CN
             END

             ERROR.MSG = ''
             IF BATCH.FLG = 'Trigger' THEN
                GOSUB DO.INV.ACT.TRIG
                IF NOT(DOCS.PRDCD) THEN
                   ERROR.MSG = 'ERROR'
                END
             END ELSE
                * generate outbound EDI invoices
                VALID.DOCS = RAISE(BATCH.FLG)
                DOC.COUNT = DCOUNT(VALID.DOCS,AM)
                ERROR.MSG = ''
                FOR DOC.INDEX = 1 TO DOC.COUNT
                   EDI.CREATE.OUT OID,GEN,EDI.ERROR,VALID.DOCS<DOC.INDEX>
                   IF EDI.ERROR THEN
                      IF ERROR.MSG THEN
                         ERROR.MSG := ' '
                      END
                      ERROR.MSG := EDI.ERROR
                   END
                NEXT DOC.INDEX
             END

             IF ERROR.MSG THEN
                ERR.MSG  = 'Order ':OID:'.':INVN:' had problems sending '
                ERR.MSG := 'via EDI. The print flag has not been '
                ERR.MSG := 'changed and you can run the batch process '
                ERR.MSG := 'again once the EDI problem is fixed. '
                IF ERROR.MSG # 'ERROR' THEN
                   ERR.MSG := 'The error returned was: ':ERROR.MSG
                END

                * log the error in the EDI Activity Log
                EDI.LOG ERR.MSG

                BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                ERROR = 'MINOR'
                RETURN
             END
          CASE ACTION = 'B2B'
             * Handle standard Eclipse B2B and BC-XML
             IF STATUS = 'I' THEN
                ACD = 'INV.B2B'
                GOSUB LOG.ABC
             END

             * For each unique BT customer, save in the Remote Activity Log
             IF OLD.BT.CN # BT.CN THEN
                RLOG      = ''
                RLOG<5>   = 'Processing Invoices for Cust#:':BT.CN
                RLOG<9>   = 'XML'
                EC.CREATE.LOG ,'O',,RLOG
                OLD.BT.CN = BT.CN
             END

             * generate an outbound B2B invoice
             ERROR.MSG = ''
             EC.SEND.DOC OID,GEN,ERROR.MSG,'Invoice'
             IF ERROR.MSG THEN
                ERR.MSG  = 'Order ':OID:'.':INVN:' had problems sending'
                ERR.MSG := 'via B2B. The print flag has not been '
                ERR.MSG := 'changed and you can run the batch process '
                ERR.MSG := 'again once the B2B problem is fixed. The '
                ERR.MSG := 'error returned was: ':ERROR.MSG

                * log the error in the Remote Activity Log
                RLOG      = ''
                RLOG<5>   = ERR.MSG
                RLOG<9>   = 'XML'
                EC.CREATE.LOG ,'O',,RLOG

                BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                ERROR = 'MINOR'
                RETURN
             END
          CASE ACTION = 'EMAIL'
             IF USE.ECLIPSE.FORMS$ OR ST.CN = "4503" THEN
                CURR.EMAIL = FIELD(SORT.ID,'~',1,4)
                IF CURR.EMAIL # OLD.EMAIL THEN
                   * If the email changed, save off the invoice
                   * information, generate the email for the accumulated
                   * invoices and then handle the current invoice.
                   IF EM.OIDS # '' THEN
                      SV.OID      = OID
                      SV.GEN      = GEN
                      SV.INV      = INVN
                      SV.BT       = BT.CN
                      SV.ST       = ST.CN
                      SV.GN.STAT  = GN.STAT
                      SV.EM.MINVS = EM.MULT.INVS

                      GOSUB GEN.INV.EMAIL

                      OID          = SV.OID
                      GEN          = SV.GEN
                      INVN         = SV.INV
                      BT.CN        = SV.BT
                      ST.CN        = SV.ST
                      GN.STAT      = SV.GN.STAT
                      EM.MULT.INVS = SV.EM.MINVS
                   END

                   EM.OIDS      = ''
                   EM.GENS      = ''
                   EM.INVS      = ''
                   EM.BT        = ''
                   EM.ST        = ''
                   EM.GN.STAT   = ''
                   OLD.EMAIL    = CURR.EMAIL
                   OLD.EM.MINVS = EM.MULT.INVS
                END

                * If the current email is set to generate multiple
                * invoices per email then save off the data and return.
                * Otherwise, generate the email.
                IF EM.MULT.INVS THEN
                   EM.OIDS<1,-1> = OID
                   EM.GENS<1,-1> = GEN
                   EM.INVS<1,-1> = INVN
                   EM.BT         = BT.CN
                   EM.ST         = ST.CN
                   EM.GN.STAT    = GN.STAT

                   RETURN
                END ELSE
                   GOSUB DO.INV.ACT.TRIG
                   IF NOT(DOCS.PRDCD) THEN RETURN
                END
             END ELSE
                GOSUB DO.INV.ACT.TRIG
                IF NOT(DOCS.PRDCD) THEN RETURN
             END
          CASE ACTION = 'XML'
             GOSUB DO.INV.ACT.TRIG
             IF NOT(DOCS.PRDCD) THEN RETURN
          CASE ACTION = 'PRINT'
             IF USE.ECLIPSE.FORMS$ THEN
                DOCUMENT = 'INVOICE'

                * If the document printing does not match 'INVOICE' then
                * we need to get the printer again (so we print to the
                * proper printer).
                IF (NOT(FORM.INFO) OR (DOCUMENT # LAST.DOC)) THEN
                   LAST.DOC = DOCUMENT
                   FORMS.GET.PRINTER.ID DOCUMENT,PTR.ID,YES
                   IF (PTR.ID = "") THEN PTR.ID = SOLAR.PTR.ID
                END

                * Send the price and ship branch in for the logo.
                BRCH    = LED(2)<1,GEN,1>        ;* Price Branch
                BRCH<2> = LED(2)<1,GEN,2>        ;* Ship Branch
                FORMS.GET.INFO DOCUMENT,PSTYLE.OVRD,PTR.ID,,,ACT.CT,,BRCH,FORM.INFO
             END

             * Loop over the number of prints
             FOR NPX = 1 TO ACT.CT
                NEW.FORM = 'INVOICE'
                IF NEW.FORM # OLD.FORM AND PRT.ON THEN
                   PRT.ON = NO
                   PRINTER.OFF DOC.ID
                   IF JAVA.PROC$ AND NOT(FAX.PRT) THEN
                      DOC.IDS<-1> = DOC.ID
                   END
                   FAX.PRT       = NO
                   ACTIVE.FORMS$ = ''
                END

                * Add this so the correct title is saved for duplicates
                IF NPX > 1 THEN
                   TITLE = SAVE.TITLE
                END
                SAVE.TITLE = TITLE

                IF NOT(PRT.ON) AND NOT(USE.ECLIPSE.FORMS$) THEN
                   PRT.ON = YES
                   IF DRPT<27> = 'HOLD' THEN
                      HOLD.FLAG = YES
                      DRPT<1> = SPEC
                   END ELSE
                      TITLE = ''
                      HOLD.FLAG = NO
                   END

                   ACTIVE.FORMS$ = YES
                   PRINTER.ON SPEC,TITLE,DOC.ID,RPT.DFLT=DRPT
                   PRT.ID  = PRINTER.ID
                   PRT.SET = YES
                   OLD.FORM = NEW.FORM
                END

                * Log the ABC Info
                IF STATUS = 'I' THEN
                   ACD = 'INV.PRT'
                   GOSUB LOG.ABC
                   INVOICE.COPY.CT$ = NPX
                END

                IF NOT(USE.ECLIPSE.FORMS$) THEN
                   SOE.PRINT.ORDER OID,GEN,NUM.PGS,LED(6)<1,GEN>,PSTYLE.OVRD,PRT.ON,DRPT,DOC.ID,,YES

                   IF CHECK.ALIGN AND NOT(PHANTOM.PROC) AND NUM.PGS>0 THEN
                      GOSUB VERIFY.ALIGN
                      IF ABORT.BATCH THEN EXIT ;* halt all batch processing
                   END

                   * SOE.PRINT.ORDER will return 0 pages if an activity
                   * trigger caused the invoice to be e-mailed instead of
                   * printed
                   IF NUM.PGS = 0 THEN ACTION = 'EMAIL'
                END ELSE
                   FOR FSC = 1 TO ACT.CT
                      FSCP.OIDS<-1> = OID
                      FSCP.GIDS<-1> = GID
                      FSCP.GENS<-1> = GEN

                      * Read in LED and get the price/ship branchs.
                      READ LEDREC FROM LEDFILE,OID ELSE LEDREC = ""
                      BRCH    = LEDREC<2,GEN,1>     ;* Price Branch
                      BRCH<2> = LEDREC<2,GEN,2>     ;* Ship Branch

                      FORM.INFO = ''
                      REPRINT.FLAG = ''
                      OE.LOG.PRINT OID,GEN,'I','INVOICE',REPRINT.FLAG
                      FORMS.GET.INFO DOCUMENT,PSTYLE.OVRD,PTR.ID,,,ACT.CT,REPRINT.FLAG,BRCH,FORM.INFO
                      FSCP.FMTS<-1> = FORM.INFO

                      NUM.PGS += 1
                   NEXT FSC
                   * We can exit out of the loop because we have all the
                   * information DFG needs to generate the invoice,
                   * including the number of copies.
                   EXIT
                END
             NEXT NPX

             IF USE.ECLIPSE.FORMS$ THEN
                * To prevent character limit threshold, send only a
                * certain number at a time.
                IF DCOUNT(FSCP.OIDS,AM) >= FSCP.PRT.MAX THEN
                   GOSUB SEND.ECLIPSE.FORMS
                END
             END
          CASE ACTION = 'FAX'
             * Fax the invoice
             IF OLD.ST.CN # ST.CN THEN
                * Always reset the fax data when the customer changes
                GOSUB SET.FAX.DRPT
             END

             IF USE.ECLIPSE.FORMS$ THEN
                DOCUMENT = 'INVOICE'

                * If the document printing does not match 'INVOICE' then
                * we need to get the printer again (so we print to the
                * proper printer).
                IF (NOT(FORM.INFO) OR (DOCUMENT # LAST.DOC)) THEN
                   LAST.DOC = DOCUMENT
                   FORMS.GET.PRINTER.ID DOCUMENT,PTR.ID,YES
                   IF (PTR.ID = "") THEN PTR.ID = SOLAR.PTR.ID
                END
             END
             IF FAX.DRPT<29,1>#OLD.FAX.NO OR SELECT.BR#OLD.FAX.BR THEN
                * If we are using Eclipse Forms, send the last batch
                * of invoices now.
                IF USE.ECLIPSE.FORMS$ THEN
                   GOSUB SEND.ECLIPSE.FORMS
                END

                * If any of these change, must send a new fax transmission
                OLD.ST.CN = ST.CN
                OLD.FAX.NO = FAX.DRPT<29,1>
                OLD.FAX.BR = SELECT.BR
                IF PRT.ON THEN
                   PRT.ON = NO
                   PRINTER.OFF DOC.ID
                   IF JAVA.PROC$ AND NOT(FAX.PRT) THEN
                      DOC.IDS<-1> = DOC.ID
                   END
                   FAX.PRT = NO
                END

                FAX.DRPT<29,9> = SELECT.BR
                FAX.DRPT<29,10> = USER.ID:'~':PRINT.QID

                FAX.ERROR = ''
                IF NOT(FAX.DRPT) OR FAX.ERROR THEN
                   * User has been notified, just continue
                   ERROR = FAX.ERROR
                   RETURN
                END
             END

             * This will be a 'batch' fax unless we turn the printer off
             MULTIPLE.FAX = YES

             * Loop over the number of faxes
             FOR NPX = 1 TO ACT.CT
                IF NOT(PRT.ON) AND NOT(USE.ECLIPSE.FORMS$) THEN
                   PRINTER.ON SPEC,,DOC.ID,RPT.DFLT=FAX.DRPT
                   FAX.PRT = YES
                   PRT.ON  = YES
                   MULTIPLE.FAX = NO
                END

                * Log the ABC Info, and duplicate copies
                IF STATUS = 'I' THEN
                   ACD = 'INV.FAX'
                   GOSUB LOG.ABC
                   INVOICE.COPY.CT$ = NPX
                END

                IF NOT(USE.ECLIPSE.FORMS$) THEN
                   SOE.PRINT.ORDER OID,GEN,NUM.PGS,LED(6)<1,GEN>,PSTYLE.OVRD,PRT.ON,FAX.DRPT,DOC.ID,,YES
                END ELSE
                   FOR FSC = 1 TO ACT.CT
                      FSCP.OIDS<-1> = OID
                      FSCP.GIDS<-1> = GID
                      FSCP.GENS<-1> = GEN

                      READ LEDREC FROM LEDFILE,OID ELSE LEDREC = ""
                      BRCH    = LEDREC<2,GEN,1>     ;* Price Branch
                      BRCH<2> = LEDREC<2,GEN,2>     ;* Ship Branch
                      BATCH.FAX.INFO<1> = OID:".":GID

                      FORM.INFO = ''
                      REPRINT.FLAG = ''
                      OE.LOG.PRINT OID,GEN,'I','INVOICE',REPRINT.FLAG
                      FORMS.GET.INFO DOCUMENT,PSTYLE.OVRD,PTR.ID,,FAX.DRPT,ACT.CT,REPRINT.FLAG,BRCH,FORM.INFO,,,BATCH.FAX.INFO
                      FSCP.FMTS<-1> = FORM.INFO

                      NUM.PGS += 1
                   NEXT FSC
                   * We can exit out of the loop because we have all the
                   * information DFG needs to generate the invoice,
                   * including the number of copies.
                   EXIT
                END
             NEXT NPX

             IF USE.ECLIPSE.FORMS$ THEN
                * To prevent character limit threshold, send only a
                * certain number at a time.
                IF DCOUNT(FSCP.OIDS,AM) >= FSCP.PRT.MAX THEN
                   GOSUB SEND.ECLIPSE.FORMS
                END
             END

             IF MULTIPLE.FAX AND NOT(USE.ECLIPSE.FORMS$) THEN
                FAX.ADD.OID DOC.ID,PRINT.QID
             END
          CASE ACTION = 'PREVIEW'
             * Log that this is a preview only
             TYPE = 'PI':VM:'1'
             OE.LOG.PRINT OID,GEN,TYPE,'* Previewed Invoice *'
          CASE OTHERWISE
             * Case where action is set to NONE, notify that
             * no action was taken but do not reset the print flag
             GOSUB SET.ITEM.CT
             RETURN
          END CASE
          IF ABORT.BATCH THEN RETURN          ;* halt all batch processing

          GOSUB UPD.INV.PROC

          RETURN
*-------------------------------------------------------------------------*
DO.INV.ACT.TRIG: *** Perform activity trigger to generate EMAIL/XML.

          * Save off LED for the current OID as it can get stepped on by
          * the call to ACTIVITY.TRIGGER if we use OLED.
          MATBUILD SAVE.LED FROM LED

          * If we are generating a single invoice for a customer that is
          * set up for multiple, make sure we have LED loaded for that
          * single OID so that the PO# will be reflected correctly in the
          * email subject.line.
          IF REFRESH.LED THEN
             MATREAD LED FROM LEDFILE,OID ELSE NULL
          END

          AT.ID      = ACT.STAT
          AT.DATA    = ''
          AT.DATA<2> = ST.CN:VM:BT.CN
          AT.DATA<3> = SELECT.BR
          IF NOT(EM.OIDS) THEN
             AT.DATA<4>  = OID
             AT.DATA<5>  = GEN
          END ELSE
             AT.DATA<13> = EM.OIDS
             AT.DATA<14> = EM.GENS
          END
          AT.DATA<7>  = GN.STAT
          AT.DATA<11> = NO

          * If user not authorized then because of printer on
          * the process may hang ... temporarily setting
          * PHANTOM.PROC so user prompt is not displayed. With
          * printer on this is like a background process.
          SV.PPROC     = PHANTOM.PROC
          PHANTOM.PROC = YES
          DOCS.PRDCD   = ''
          ACTIVITY.TRIGGER AT.ID,AT.DATA,DOCS.PRDCD

          * Reset to previous setting
          PHANTOM.PROC = SV.PPROC

          IF NOT(DOCS.PRDCD) THEN
             IF EM.MULT.INVS AND EM.OIDS THEN
                INV.CT = DCOUNT(EM.OIDS,VM)
             END ELSE
                INV.CT = 1
             END

             FOR INV.CTR = 1 TO INV.CT
                IF EM.MULT.INVS AND EM.OIDS THEN
                   OID  = EM.OIDS<1,INV.CTR>
                   INVN = EM.INVS<1,INV.CTR>
                END
                ERR.MSG  = 'Order ':OID:'.':INVN:' had problems sending'
                ERR.MSG := ' via ':ACTION:'. The print flag has not'
                ERR.MSG := ' been changed and you can run the batch'
                ERR.MSG := ' process again once the problem is fixed.'
                BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                ERROR = 'MINOR'
             NEXT INV.CTR
          END

          * Reset LED back to the current OID.
          MATPARSE LED FROM SAVE.LED

          RETURN
*-------------------------------------------------------------------------*
UPD.INV.PROC: *** Perform necessary updates for invoice generation.
          PLOG.CT += 1

          * Add the Order# and Gid to print list
          * which we eventually be written to PRINT.LOG
          * to enable batch invoice reprinting.
          REPRINT.ID    = OID:'.':LED(12)<1,GEN>
          PLOG.LIST<PLOG.CT> = REPRINT.ID
          IF (ACTION # 'PREVIEW') AND (ACTION # 'ERROR') THEN
             PLOG.LIST<PLOG.CT,2> = ACTION
          END

          FOR NPX = 1 TO ACT.CT
             GOSUB SET.ITEM.CT
          NEXT NPX

          * For Hajoca sites, update unpaid invoice list
          IF HAJOCA.SITE$ THEN
             GOSUB UPD.HAJO
          END

          * Update the print status
          IF BATCH.NUM AND NOT(INV.FTP.TEST) THEN
             DELETE PQFILE,PRINT.QID
             OE.UPD.PRINT.STAT OID,GEN,'N'
          END

          RETURN
*-------------------------------------------------------------------------*
GEN.INV.EMAIL: *** Generate the batch invoice email.
          IF DCOUNT(EM.OIDS,VM) = 1 THEN
             OID          = EM.OIDS
             GEN          = EM.GENS
             INVN         = EM.INVS
             EM.MULT.INVS = OLD.EM.MINVS
             EM.OIDS      = ''
             EM.GENS      = ''
             REFRESH.LED  = YES
          END

          BT.CN   = EM.BT
          ST.CN   = EM.ST
          GN.STAT = EM.GN.STAT

          GOSUB DO.INV.ACT.TRIG

          * Perform the necessary updates if the email generation was
          * successful.
          IF DOCS.PRDCD THEN
             MAT OLED = MAT LED

             IF NOT(EM.OIDS) THEN
                OID.CT = 1
             END ELSE
                OID.CT = DCOUNT(EM.OIDS,VM)
             END

             FOR OID.CTR = 1 TO OID.CT
                IF EM.OIDS THEN
                   OID = EM.OIDS<1,OID.CTR>
                   GEN = EM.GENS<1,OID.CTR>
                END

                MATREAD LED FROM LEDFILE,OID ELSE CONTINUE

                REPRINT.FLAG = ''
                OE.LOG.PRINT OID,GEN,'I','INVOICE',REPRINT.FLAG
                GOSUB UPD.INV.PROC
             NEXT OID.CTR

             MAT LED = MAT OLED
          END

          RETURN
*-------------------------------------------------------------------------*
OUTPUT.STATEMENT: *** Print a single statement
          NUM.PGS = 0

          * Setup CUS and CUSS
          GET.DFLT.BRANCH SELECT.BR,ST.CN
          GET.CUS SELECT.BR,BT.CN,ST.CN,-1
          DISABLE.FTP = CUSS(50)<1,3>

          * Output a single statement
          BEGIN CASE
          CASE SEND.FTP AND NOT(DISABLE.FTP)
             * 3rd party FTP
             IF FTP.ERROR THEN
                ERR.MSG  = 'Due to previous FTP setup errors, no '
                ERR.MSG := 'statement was generated for CN: ':ST.CN
                ERR.MSG := 'You can run the batch process again '
                ERR.MSG := 'once the FTP setup issue is fixed. '
                BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                ERROR = 'MINOR'
                RETURN
             END

             * The VM in the in the initialization of OUTPUT.VIA is
             * necessary to ensure the BATCH.FLAG is in the same position,
             * regardless of ACTION.
             OUTPUT.VIA = ACTION:VM
             IF ACTION = 'EMAIL' THEN
                OUTPUT.VIA := ACT.STAT
             END
             OUTPUT.VIA := VM:BATCH.FLG

             FTP.ERR = ''
             FOR NPX = 1 TO ACT.CT
                BATCH.STMT.FTP.SET ST.CN,STMT.DATA,OUTPUT.VIA,FILE,FTP.ERR
             NEXT NPX

             * If error is returned, then no item was added. It is
             * possible that items wont be added to statements as
             * statements have different criteria for sending.
             IF FTP.ERR THEN RETURN
          CASE ACTION = 'EDI'
             * Not supported for statements at this time
          CASE ACTION = 'B2B'
             * Not supported for statements at this time
          CASE ACTION = 'EMAIL'

             * If user not authorized then because of printer on
             * the process may hang ... temporarily setting
             * PHANTOM.PROC so user prompt is not displayed. With
             * printer on this is like a background process.
             SV.PPROC     = PHANTOM.PROC
             PHANTOM.PROC = YES

             TRIGGER.ERROR = NO
             FOR NPX = 1 TO ACT.CT
                DOCS.PRDCD    = ''
                AT.ID         = ACT.STAT         ;* Activity Trigger
                AT.DATA       = ''
                AT.DATA<2>    = ST.CN:VM:BT.CN   ;* Ship-to/Bill-to CN#s
                AT.DATA<3>    = SELECT.BR        ;* Pricing/Shipping Branch
                AT.DATA<8,1>  = LOWER(STMT.OPTS) ;* Stmts and/or Invs
                AT.DATA<8,2>  = END.DATE         ;* As of date
                AT.DATA<8,3>  = AGE.END.DATE     ;* Age as of date
                AT.DATA<8,4>  = LOWER(SALE.BRS)  ;* Sales branches
                AT.DATA<8,5>  = MIN.BAL          ;* Minimum Balance

                ACTIVITY.TRIGGER AT.ID,AT.DATA,DOCS.PRDCD

                * If a document was not generated or there was
                * an error sending the data log it now and abort.
                IF NOT(DOCS.PRDCD) OR DOCS.PRDCD = 'ERROR' THEN
                   * Guarantee that a customer number is displayed in the
                   * error message.
                   ERR.CN = ST.CN
                   IF (ERR.CN = '') THEN
                     ERR.CN = BT.CN
                   END
                   ERR.MSG  = 'A statement for CN: ':ERR.CN:' had trouble'
                   ERR.MSG := ' sending via E-mail and was not sent. '
                   ERR.MSG := 'You can run the batch process again '
                   ERR.MSG := 'once the E-mail problem is fixed. '
                   BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
                   ERROR = 'MINOR'
                   TRIGGER.ERROR = YES
                   EXIT
                END

                * If there was nothing that could be produced (ie balance
                * equals 0), this is NOT an error, but we do not need
                * to continue.  (Hence we set TRIGGER.ERROR to leave.)
                *
                * We must RETURN from this CASE before we get to the
                * END CASE statement. The code after the END CASE, ASSUMES
                * that Email were generated and increases the count of
                * the number of emails send in STMTS.OUT.
                IF DOCS.PRDCD = 'NOTHING.SELECTED' THEN
                   ERROR = ''
                   TRIGGER.ERROR = YES
                   EXIT
                END
             NEXT NPX

             * Reset to previous setting
             PHANTOM.PROC = SV.PPROC

             * If we were unable to generate the email then abort
             IF TRIGGER.ERROR THEN RETURN
          CASE ACTION = 'PRINT'
             ADDL.INFO = MIN.BAL:AM:BATCH.FLG
             * Loop over the number of prints
             FOR NPX = 1 TO ACT.CT
                NEW.FORM = 'STATEMENT'

                IF USE.ECLIPSE.FORMS$ THEN
                   DOCUMENT = NEW.FORM

                   * If the document printing does not match 'STATEMENT'
                   * then we need to get the printer again (so we print to
                   * the proper printer).
                   IF (NOT(FORM.INFO) OR (DOCUMENT # LAST.DOC)) THEN
                      LAST.DOC = DOCUMENT
                      FORMS.GET.PRINTER.ID DOCUMENT,PTR.ID,YES
                      IF (PTR.ID = "") THEN PTR.ID = SOLAR.PTR.ID
                   END

                   * Setup the home branch of the customer for the logo.
                   HBR = GET.DFLT.BRANCH(ST.CN)
                   FORMS.GET.INFO DOCUMENT,,PTR.ID,,DRPT,ACT.CT,,HBR,FORM.INFO
                END

                IF NEW.FORM # OLD.FORM AND PRT.ON THEN
                   PRT.ON = NO
                   PRINTER.OFF DOC.ID
                   IF JAVA.PROC$ AND NOT(FAX.PRT) THEN
                      DOC.IDS<-1> = DOC.ID
                   END
                   FAX.PRT       = NO
                   ACTIVE.FORMS$ = ''
                END
                IF NOT(PRT.ON) AND NOT(USE.ECLIPSE.FORMS$) THEN
                   IF DRPT<27>  = 'HOLD' THEN
                      HOLD.FLAG = YES
                      DRPT<1>   = 'STATEMENT'
                      STMT.DATE = OCONV(END.DATE,'D2/')
                      TITLE     = 'STATEMENTS as of ':STMT.DATE
                   END ELSE
                      HOLD.FLAG = NO
                      TITLE     = ''
                   END

                   PRT.ON = YES
                   PRINTER.ON 'STATEMENT',TITLE,DOC.ID,RPT.DFLT=DRPT
                   PRT.ID  = PRINTER.ID
                   IF PTR.ID="" AND SOLAR.PTR.ID THEN PTR.ID=SOLAR.PTR.ID
                   PRT.SET = YES
                   OLD.FORM = NEW.FORM
                END

                IF NOT(USE.ECLIPSE.FORMS$) THEN
                   AR.STATEMENT STMT.OPTS,ST.CN,END.DATE,AGE.END.DATE,SALE.BRS,PRT.ON,NUM.PGS,DRPT,1,,ADDL.INFO
                END ELSE
                   FORMS.CHECK.STMT.PRINT STMT.OPTS,ST.CN,END.DATE,AGE.END.DATE,SALE.BRS,PRT.FLAG,HDR,ITM,FTR,ADDL.INFO
                   IF PRT.FLAG THEN
                      FOR FSC = 1 TO ACT.CT
                         NUM.PGS = 1
                         FSCP.CNS<-1>  = ST.CN
                         FSCP.HDRS<-1> = LOWER(HDR)
                         FSCP.ITMS<-1> = LOWER(ITM)
                         FSCP.FTRS<-1> = LOWER(FTR)
                         FSCP.FMTS<-1> = FORM.INFO
                      NEXT FSC
                   END ELSE
                      NUM.PGS = 0
                   END
                   * We can exit out of the loop because we are either not
                   * going to generate the statement OR we have all the
                   * information DFG needs to generate the statement,
                   * including the number of copies.
                   EXIT
                END
             NEXT NPX

             IF USE.ECLIPSE.FORMS$ THEN
                * To prevent character limit threshold, send only a
                * certain number at a time.
                IF DCOUNT(FSCP.CNS,AM) >= FSCP.PRT.MAX THEN
                   GOSUB SEND.ECLIPSE.FORMS
                END
             END
          CASE ACTION = 'FAX'
             IF PRT.ON THEN
                PRT.ON = NO
                PRINTER.OFF DOC.ID
                IF JAVA.PROC$ AND NOT(FAX.PRT) THEN
                   DOC.IDS<-1> = DOC.ID
                END
                FAX.PRT = NO
             END

             GOSUB SET.FAX.DRPT

             FAX.DRPT<29,9> = SELECT.BR
             IF NOT(FAX.DRPT) THEN
                * user has been notified, just continue
                RETURN
             END

             ADDL.INFO = MIN.BAL:AM:BATCH.FLG
             * loop over the number of faxes
             FOR NPX = 1 TO ACT.CT
                AR.STATEMENT STMT.OPTS,ST.CN,END.DATE,AGE.END.DATE,SALE.BRS,PRT.ON,NUM.PGS,FAX.DRPT,1,,ADDL.INFO
             NEXT NPX
          CASE ACTION = 'ERROR'
             * there is an error with this inv, notify user
             ERR.MSG  = 'CN: ':ST.CN:' had problems sending. '
             ERR.MSG := 'You can run the batch process again '
             ERR.MSG := 'once the problem is fixed. The '
             ERR.MSG := 'error returned was: ':ACT.STAT
             BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
             ERROR = 'MINOR'
             RETURN
          CASE OTHERWISE
             * Case where action is set to NONE, notify that
             * no action was taken but do not reset the print flag
             GOSUB SET.ITEM.CT
             RETURN
          END CASE

          FOR NPX = 1 TO ACT.CT
             GOSUB SET.ITEM.CT
          NEXT NPX

          RETURN
*-------------------------------------------------------------------------*
SET.COMMON.DATA: *** Set common variables

          OID.DATA$    = OID
          OID.DATA$<2> = GEN
          OID.DATA$<4> = LED(5)<1,GEN>
          ABC.BR       = LED(2)<1,GEN,2>

          RETURN
*-------------------------------------------------------------------------*
SET.ABC.LOG: *** Setup ABC logging based on this LED

          SHP.BR = LED(2)<1,GEN,2>

          * Don't log ABC delivery charge on directs
          IF NOT(LED(33)<1,GEN>) THEN
             ABC.ID  = LED(70)<1,GEN>:'~':SHP.BR:'~'
             ABC.ID := LED(5)<1,GEN>:'~':LED(9)<1,GEN>

             READV MNFST.IDS FROM LEDLFILE,OID,17 ELSE MNFST.IDS = ''
             MNFST.ID = MNFST.IDS<1,GEN>

             ABC.ENABLED = NO
             READV WRK FROM CTRBFILE,'ABC.ENABLED~':SELECT.BR,1 THEN
                IF WRK THEN ABC.ENABLED = YES
             END

             IF ABC.ENABLED THEN
                READ ABC.DEL.CHG FROM CTRBFILE,'ABC.DEL.CHG~':SHP.BR ELSE
                   ABC.DEL.CHG = ''
                END

                IF ABC.DEL.CHG = 'Invoice Print' OR MNFST.ID = '' THEN
                   LOCATE ABC.ID IN ABC.IDS SETTING POS ELSE
                      INS ABC.ID BEFORE ABC.IDS<1>
                   END
                END
             END
          END

          RETURN
*-------------------------------------------------------------------------*
LOG.ABC:  *** Log Activity Based Costing

          ABC.ENABLED = NO
          READV WRK FROM CTRBFILE,'ABC.ENABLED~':ABC.BR,1 THEN
             IF WRK THEN
               ABC.ENABLED = YES
             END
          END

          IF ABC.ENABLED THEN
             ABC.LOG.AUTO ACD
          END

          RETURN
*-------------------------------------------------------------------------*
UPD.HAJO: *** For Hajoca sites. If the invoice just printed has terms
          *** cash/cod and the invoice does not have a payment entered
          *** or applied then update the id to a temporary report file.
          *** After all of the invoices have printed then print a report
          *** of the unpaid invoices.

          * Only update report list if the status is Invoice
          IF LED(6)<1,GEN> # 'I' THEN RETURN

          * Check if the term type is cash/cod and if the payments
          * have been updated and/or applied
          TERMS = LED(29)<1,GEN>
          TCHK  = NO
          FINDSTR 'CASH' IN TERMS SETTING X THEN
             TCHK = YES
          END ELSE
             FINDSTR 'COD' IN TERMS SETTING X THEN TCHK = YES
          END
          IF TCHK THEN
             OE.ORDER.TOTAL OID,GEN,QSIGN,INV.AMT
             SOE.CALC.CASH  OID,GEN,DUE.AMT,AMT.PAID
             OPN.AMT = INV.AMT - (-AMT.PAID)
             IF SUM(OPN.AMT) # 0 THEN
                WRITE '' ON TMPFILE,AR.ID
             END
          END

          RETURN
*-------------------------------------------------------------------------*
VERIFY.ALIGN: *** Get User to verify Form Alignment

          IF PHANTOM.PROC THEN RETURN

          * If this is a java process we want to keep each document
          * id for printed documents seperate. We must validate that the
          * document still exists in the hold file before we add it to
          * the DOC.IDS list. A document may return an DOC.ID and may no
          * longer have a document in the hold file if we have the
          * EMAIL.ORDER.TIFF control record set and an activity trigger
          * that forces email instead of print.
          IF JAVA.PROC$ THEN
             IF PRT.ON THEN
                PRT.ON = NO
                PRINTER.OFF DOC.ID
                IF NOT(FAX.PRT) THEN
                   READV NOTHING FROM HOLDFILE,DOC.ID,0 THEN
                      DOC.IDS<-1> = DOC.ID
                   END
                END
                FAX.PRT       = NO
                ACTIVE.FORMS$ = ''
             END
             RETURN
          END

          * turn off printer for prompt
          PRINTER.OFF DOC.ID
          PRT.ON        = NO
          ACTIVE.FORMS$ = ''

RE.VERF:  *** prompt user - 'Verify Form Alignment'
          VERIFY.ALIGN OPT

          BEGIN CASE
          CASE OPT = 1                  ;* <R>eprint
             SOE.PRINT.ORDER OID,GEN,NUM.PGS,LED(6)<1,GEN>,PSTYLE.OVRD,PRT.ON,DRPT,DOC.ID,,YES
             GOTO RE.VERF
          CASE OPT = 2                  ;* <C>ontinue
             CHECK.ALIGN = NO
          CASE OPT = 3                  ;* <N>ext
             NULL
          CASE OPT = 4                  ;* <A>bort
             ABORT.BATCH = YES
          END CASE

          * turn printer back on
          PRT.ON        = YES
          ACTIVE.FORMS$ = YES
          PRINTER.ON SPEC,,DOC.ID

          RETURN
*-------------------------------------------------------------------------*
SET.FAX.DRPT: *** Populate arrays with fax data required to complete the
          *** transmission

          FAX.DRPT = ''
          FAX.DATA = ''
          READ FAX.DATA FROM CTRLFILE,FAX.REC ELSE
             FAX.DATA      = ""
             FAX.DATA<1,1> = "1-999-999-9999"
             FAX.DATA<1,3> = "Accounts Payable"
             FAX.DATA<1,4> = "Accounts Receivable"
             FAX.DATA<1,5> = "Invoice"
          END

          FAX.PHONE = ''

          * Looks to see if the use ship-to address field
          * on alt bill screen is set to
          * yes OR if it is a ship-to only customer than
          * looks to see if it is flagged to use it's own AR
          IF CUSS(97) OR CUSS(40) THEN
             WHICH.FAX.CN = ST.CN
             CONTACTS = RAISE(OCONV(CUSS(16),'MCU'))
             LOCATE FAX.NAME IN CONTACTS SETTING FAX.VM ELSE
                * In release 7 we hardcoded fax number to VM 2. We need
                * to find the first contact name in the list that has FAX
                * in it which will ultimately be the second one in the list
                GOSUB FIND.FAX.CONTACT
             END
             IF FAX.VM THEN
                FAX.PHONE = CUSS(17)<1,FAX.VM>
             END
          END

          * Looks to see if the ship-to customer has an alt bill address
          IF CUSS(91) AND NOT(FAX.PHONE) THEN
             FAX.PHONE    = CUSS(121)
             WHICH.FAX.CN = ST.CN
             * If the fax phone is null check the main shipto
             IF NOT(FAX.PHONE) THEN
                CONTACTS = RAISE(OCONV(CUSS(16),'MCU'))
                LOCATE FAX.NAME IN CONTACTS SETTING FAX.VM ELSE
                   GOSUB FIND.FAX.CONTACT
                END
                IF FAX.VM THEN
                   FAX.PHONE = CUSS(17)<1,FAX.VM>
                END
             END
          END

          * Looks to see if the bill-to customer has an alt bill address
          IF CUS(91) AND NOT(FAX.PHONE) THEN
             FAX.PHONE    = CUS(121)
             WHICH.FAX.CN = BT.CN
          END

          * If none of the above are true the system will
          * use whatever # is specified
          IF NOT(FAX.PHONE) THEN
             WHICH.FAX.CN = BT.CN
             CONTACTS = RAISE(OCONV(CUS(16),'MCU'))
             LOCATE FAX.NAME IN CONTACTS SETTING FAX.VM ELSE
                * In release 7 we hardcoded fax number to VM 2. We need
                * to find the first contact name in the list that has FAX
                * in it which will ultimately be the second one in the list
                GOSUB FIND.FAX.CONTACT
             END
             IF FAX.VM THEN
                FAX.PHONE = CUS(17)<1,FAX.VM>
             END
          END

          * Last attempt at a fax number... Look at the parent customer
          * fax number
          PCUS.ID = CUS(80)<1,1>
          IF NOT(FAX.PHONE) AND PCUS.ID THEN
             READ PCUS FROM CUSFILE,PCUS.ID ELSE PCUS = ''
             IF PCUS<91> THEN
                FAX.PHONE    = PCUS<121>
                WHICH.FAX.CN = PCUS.ID
             END
             IF NOT(FAX.PHONE) THEN
                WHICH.FAX.CN = PCUS.ID
                CONTACTS = RAISE(OCONV(PCUS<16>,'MCU'))
                LOCATE FAX.NAME IN CONTACTS SETTING FAX.VM ELSE
                   GOSUB FIND.FAX.CONTACT
                END
                IF FAX.VM THEN
                   FAX.PHONE = PCUS<17,FAX.VM>
                END
             END
          END

          * Missing fax# so abort
          IF NOT(FAX.PHONE) THEN RETURN

          IF NOT(WHICH.FAX.CN) THEN
             WHICH.FAX.CN = ST.CN
          END

          * Add fax access code suffix to end of FAX.PHONE
          GOSUB ADD.FAX.SUFF

          FAX.DATA<1,1>  = FAX.PHONE
          FAX.DATA<1,2>  = WHICH.FAX.CN
          FAX.DATA<1,10> = USER.ID:'~':PRINT.QID

          IF FAX.DATA<1,9> = '' THEN
             FAX.DATA<1,9> = SECURITY<9,1>
          END

          FAX.DRPT<29> = FAX.DATA

          RETURN
*-------------------------------------------------------------------------*
FIND.FAX.CONTACT:  *** Look for the first 3 characters of the contact name.
          *** If it is "FAX", then use it.
          CONTACT.CNT = DCOUNT(CONTACTS,AM)
          FAX.VM = 0
          FOR CHK = 1 TO CONTACT.CNT
             IF CONTACTS<CHK>[1,3] = "FAX" THEN
                FAX.VM = CHK
                EXIT
             END
          NEXT CHK

          RETURN
*-------------------------------------------------------------------------*
ADD.FAX.SUFF: *** Add phone suffix

          JAVA.FAX.ADD.SUFF FAX.PHONE,USER.ID

          RETURN
*-------------------------------------------------------------------------*
CREATE.FILE: *** Create the file to store outbound invoice/statement data

          REMOTE.USER = FTP.INFO<3>
          LOCAL.PATH  = FTP.INFO<7>
          DOC.VER     = FTP.INFO<8>

          * File Name structure is going to be setup as follows:
          * name_type_date_time_batchnum
          * the internal csv file will be stored using universe stamps
          * name_type_date_time_batchnum e.g. 'test_inv_12323_123.810_123'
          * the external zip file will be stored using the above
          * name_type_date_time_batchnum e.g. 'test_inv_040212_165620_123'
          INTERNAL.NAME = REMOTE.USER
          EXTERNAL.NAME = REMOTE.USER

          BEGIN CASE
          CASE FILE.TYPE = 'BATCH.INV'
             TYPE  = '_INV_'
             INDEX = 2
          CASE FILE.TYPE = 'STMT'
             TYPE  = '_STMT_'
             INDEX = 1
          CASE OTHERWISE
             TYPE = '_MISC_'
          END CASE

          INTERNAL.NAME := TYPE:DATE.NOW:'_':TIME.NOW
          EXTERNAL.NAME := TYPE:YEAR:MO:DAY:'_':HOUR:MIN:SEC

          IF BATCH.NUM THEN
             INTERNAL.NAME := '_':BATCH.NUM
             EXTERNAL.NAME := '_':BATCH.NUM
          END

          FILE<INDEX,1> = INTERNAL.NAME
          FILE<INDEX,2> = EXTERNAL.NAME

          OPEN LOCAL.PATH TO FTP.FILE ELSE
             MOD = 101
             SEP = 1
             TYP = 19
             CMD  = "CREATE-FILE ":LOCAL.PATH:' 1 ':MOD:',':SEP:',':TYP
             EXECUTE CMD CAPTURING MSG

             UT.OPEN.FILE LOCAL.PATH,FTP.FILE,ERR.MSG
             IF ERR.MSG THEN
                MSG  = 'An error occurred when trying to create the '
                MSG := 'FTP file directories. This prevents Eclipse '
                MSG := 'from sending and saving the FTP files. '
                MSG := 'Please contact Eclipse immediately and '
                MSG := 'let them know the error returned was: '
                MSG := ERR.MSG
                BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',MSG
                ERROR     = 'MINOR'
                FTP.ERROR = YES
                RETURN
             END
          END

          * Set the version info
          BT.VERSION<1> = 'VER'
          READV REL.VER FROM CTRLFILE,'RELEASE.VER',1 ELSE REL.VER = ''
          BT.VERSION<2> = REL.VER
          BT.VERSION<3> = DOC.VER
          UT.REP.STR BT.VERSION,'"','""'
          UT.REP.STR BT.VERSION,AM,'","'
          BT.VERSION = '"':BT.VERSION:'"'

          CSV.NAME = FILE<INDEX,1>:'.csv'
          NWRITEV BT.VERSION ON FTP.FILE,CSV.NAME,1

          RETURN
*-------------------------------------------------------------------------*
SET.ITEM.CT: *** Build data required to log events

          IF SEND.FTP THEN
             OLD.ACTION = ACTION
             ACTION     = 'FTP'
          END

          BEGIN CASE
          CASE ITEM.TYPE = 'BATCH.INV'
             TMP.OUT = FORMS.OUT
             * If the 'Disable FTP Billing' flag has been set for this
             * customer and this invoice is set to be FTP'd, then reset
             * ACTION to be the original action.
             IF SEND.FTP AND DISABLE.FTP THEN
                ACTION = OLD.ACTION
             END
          CASE ITEM.TYPE = 'STMT'
             TMP.OUT = STMTS.OUT
             * If the 'Disable FTP Billing' flag has been set for this
             * customer and this invoice is set to be FTP'd, then reset
             * ACTION to be the original action.
             IF SEND.FTP AND DISABLE.FTP THEN
                ACTION = OLD.ACTION
             END
             IF ACTION = 'PRINT' OR ACTION = 'FAX' AND NUM.PGS = 0 THEN
                RETURN
             END
             IF ACTION = 'EMAIL' AND NOT(DOCS.PRDCD) THEN RETURN
          END CASE

          IO.CT = DCOUNT(TMP.OUT,AM)
          FOUND = NO
          FOR IOX = 1 TO IO.CT
             IF TMP.OUT<IOX,1> = ACTION THEN
                FOUND = YES
                TMP.OUT<IOX,2> += 1
                TMP.OUT<IOX,3> += NUM.PGS
                EXIT
             END
          NEXT IOX

          IF NOT(FOUND) THEN
             TMP.OUT<-1> = ACTION:VM:1:VM:NUM.PGS
          END

          IF SEND.FTP THEN
             ACTION = OLD.ACTION
          END

          BEGIN CASE
          CASE ITEM.TYPE = 'BATCH.INV'
             FORMS.OUT = TMP.OUT
          CASE ITEM.TYPE = 'STMT'
             STMTS.OUT = TMP.OUT
          END CASE

          RETURN
*-------------------------------------------------------------------------*
LOG.ITEMS.CT: *** Log the actions taken

          MSG.OUT = ''
          NUM.TYPES = DCOUNT(TMP.OUT,AM)
          IF NUM.TYPES = 0 THEN
             BEGIN CASE
             CASE ITEM.TYPE = 'BATCH.INV'
                MSG.OUT = 'No Invoices Found'
             CASE ITEM.TYPE = 'STMT'
                MSG.OUT = 'No Statements Found'
             END CASE
          END

          FOR NTX = 1 TO NUM.TYPES
             ACTION    = TMP.OUT<NTX,1>
             NUM.ITEMS = TMP.OUT<NTX,2>
             NUM.PGS   = TMP.OUT<NTX,3>

             * page count only gets set when printing or faxing invoices
             * or statements - set page count to 1 for any other action.
             IF ACTION # 'PRINT' AND ACTION # 'FAX' THEN
                NUM.PGS = 1
             END

             BEGIN CASE
             CASE ITEM.TYPE = 'BATCH.INV'
                INV.NOUN = 'invoice'
             CASE ITEM.TYPE = 'STMT'
                INV.NOUN = 'statement'
             END CASE

             IF NUM.ITEMS = 1 AND NUM.PGS > 0 THEN
                INV.VERB = 'was'
             END ELSE
                INV.VERB  = 'were'
                INV.NOUN := 's'
             END

             IF NUM.PGS = 1 THEN
                PGS.NOUN = 'page'
             END ELSE
                PGS.NOUN = 'pages'
             END

             IF NUM.PGS = 0 AND NUM.ITEMS > 0 THEN
                NUM.ITEMS = 'No'
             END

             BEGIN CASE
             CASE ACTION = 'PRINT'
                IF HOLD.FLAG THEN
                   INV.OUT = ' sent to the hold file.'
                END ELSE
                   INV.OUT = ' printed.'
                END

                MSG.LINE  = NUM.ITEMS:' ':INV.NOUN
                IF NUM.PGS > 0 AND NOT(USE.ECLIPSE.FORMS$) THEN
                   MSG.LINE := ' totaling ':NUM.PGS:' ':PGS.NOUN
                END
                MSG.LINE := ' ':INV.VERB:INV.OUT
                MSG.OUT<-1> = MSG.LINE
             CASE ACTION = 'FAX'
                MSG.LINE  = NUM.ITEMS:' ':INV.NOUN
                IF NUM.PGS > 0 AND NOT(USE.ECLIPSE.FORMS$) THEN
                   MSG.LINE := ' totaling ':NUM.PGS:' ':PGS.NOUN
                END
                MSG.LINE := ' ':INV.VERB:' faxed.'
                MSG.OUT<-1> = MSG.LINE
             CASE ACTION = 'EMAIL'
                MSG.LINE  = NUM.ITEMS:' ':INV.NOUN:' ':INV.VERB
                MSG.LINE := ' emailed.'
                MSG.OUT<-1> = MSG.LINE
             CASE ACTION = 'PREVIEW'
                MSG.LINE  = NUM.ITEMS:' ':INV.NOUN:' ':INV.VERB
                MSG.LINE := ' previewed.'
                MSG.OUT<-1> = MSG.LINE
             CASE ACTION = 'FTP'
                IF NOT(FTP.ERR) THEN
                   MSG.LINE  = NUM.ITEMS:' ':INV.NOUN:' ':INV.VERB
                   MSG.LINE := ' sent via ':ACTION:'.'
                   MSG.OUT<-1> = MSG.LINE
                   ADT.MSG = DATE():'_':TIME():'_':NUM.ITEMS
                   BATCH.LOG.MSG.SET BATCH.TYPE,'AUDIT',ADT.MSG
                END
             CASE OTHERWISE
                MSG.LINE  = NUM.ITEMS:' ':INV.NOUN:' ':INV.VERB
                MSG.LINE := ' sent via ':ACTION:'.'
                MSG.OUT<-1> = MSG.LINE
             END CASE
          NEXT NTX

          BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',MSG.OUT

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~01/14/14~12:35
