          SUBROUTINE (CNS,SELECT.BR,BATCH.TYPE,ACTION,ERROR,ENTTFILE)
** Version# 46.0002[1] - 01/14/2014 - 12:19pm - TSMITH - eclipse

*** Subroutine: BATCH.ACTION.GET
*-----------------------Description---------------------------------------*
*** This program is responsible for figuring out what action to take for
*** the item that is passed in. The item that is passed in can be either a
*** OID.GID or a CN. Additionally, if the selecting branch isn't passed in,
*** this program will attempt to determine what branch to use. This is
*** needed to determine if there are activity triggers for that branch
*** which would supercede the action set up for the item.
*-----------------------Additional Notes----------------------------------*
*** This program is used in conjuntion with other BATCH programs. It
*** is independant enough that changes made here or in the others should
*** not affect anything. The original design and documentation can be
*** found on tracker CRJ661.
*-----------------------Paramenters---------------------------------------*
*** CNS        - Bill-to and Ship-to customers to get action for       [IN]
***              based on the batch type (STMT or BATCH.INV)
***              <1> = Bill-To
***              <2> = Ship-To
***              <3> = Boolean flag whether CUS and CUSS are
***                    already active
***              <4> = Boolean flag whether user defined a list
***                    of customers they wanted to print statements
***                    and/or invoices for as these should print
***                    regardless of individual batch settings
***              <5> = Boolean flag whether statement action
***                    should be overriden
***              <6> = Boolean flag indicating that they are selecting
***                    STATEMEMTS for options other than 'Individual
***                    Customer(s)', 'All Batch Statement Customers', and
***                    'Positive Balance'
*** SELECT.BR  - The branch to determine what, if any, activity        [IN]
***              triggers to use.
*** BATCH.TYPE - String which define the form type being generated     [IN]
***              Valid options are:
***              BATCH.INV  = Batch Printing Invoices
***              STMT.INV   = Printing Invoices in Statement Run
***              BATCH.STMT = Batch Statements
*** ACTION     - The action to take. The format of the action is:     [OUT]
***              ACTION:VM:STATUS:VM:COUNT:AM:ACITON... where
***              ACTION = Action to take (i.e. PRINT, FAX, ETC)
***              STATUS = Status of action (i.e. OK, <err.msg>, or
***                       <trig.id>
***              COUNT  = Number of actions to take (i.e. 1,2,3,etc)
***              (example: PRINT:VM:OK:VM:2 or EMAIL:VM:T008:1 or
***                        FAX:NUMBER NOT DEFINED:1)
*** ERROR      - Flag to indicate that an error occured during this   [OUT]
***              process. (Not Used)
*** ENTTFILE   - The handle to the entity trigger file.  If unassigned
***              this routine will open it.                           (IN)
*-------------------------------------------------------------------------*
*** Common Variables: CUS,CUSS,CORPCN$
*-------------------------------------------------------------------------*
          GOSUB INIT
          IF ERROR THEN RETURN

          * Get the actions based on the batch type
          BEGIN CASE
          CASE BATCH.TYPE = 'BATCH.INV'
             * Batch Print Invoices
             GOSUB SET.FORM.ACTION
          CASE BATCH.TYPE = 'STMT.INV'
             * Include Invoices in Statement Run
             GOSUB SET.FORM.ACTION
          CASE BATCH.TYPE = 'STMT'
             * Batch Print Statements
             GOSUB SET.STMT.ACTION
          CASE OTHERWISE; NULL
          END CASE
          RETURN
*-------------------------------------------------------------------------*
INIT:     *** Initialize all variables used in this subroutine
          ACTION            = ''
          ACT.IDX           = 1
          ERROR             = ''
          STMT.WILL.FAX     = NO
          PROCESS.AS.BILLTO = NO
          ACT.TRIG.STATUS   = ''
          ACT.CT            = 0

          READ DFLTS FROM CTRLFILE,'BATCH.DFLT.SETTINGS' ELSE DFLTS = ''
          DFLT.IPRT = DFLTS<1>      ;* # of invoice copies to print
          DFLT.IFAX = DFLTS<2>      ;* # of invoice copies to fax
          DFLT.STMT = DFLTS<3>      ;* Statement Mode

          * Batch print statements will be passing in just the
          * major and minor accounts to generate the statement
          * for where invoices will pass in a specific invoice
          * from the PRINT.QUEUE or A/R file
          BT.CN         = CNS<1> ;* Bill-to customer
          ST.CN         = CNS<2> ;* Ship-to customer
          ACTIVE.ARRAYS = CNS<3> ;* Boolean flag whether CUS is active
          DEF.CN.LIST   = CNS<4> ;* Boolean flag whether user CN list
          ACTION.OVRD   = CNS<5> ;* Boolean flag to override batch setting
          OTH.STMT.OPTS = CNS<6> ;* Boolean flag to allow non-batch CUS

          * If CUS and CUSS are not already active load them
          IF NOT(ACTIVE.ARRAYS) THEN
             GET.CUS SELECT.BR,BT.CN,ST.CN,-1
          END

          IF NOT(ASSIGNED(ENTTFILE)) THEN
             UT.OPEN.FILE 'ENTITY.TRIGGER',ENTTFILE,ERR.MSG
             ERR.MSG  = '!!! Fatal Error !!! - '
             ERR.MSG := 'ENTITY.TRIGGER could not be opened. '
             BATCH.LOG.MSG.SET BATCH.TYPE,'STATUS',ERR.MSG
             ERROR = 'FATAL'
             RETURN
          END

          CTRL.ID = "EF.MULTI.INVS"
          READ INV.PER.EMAIL FROM CTRLFILE,CTRL.ID ELSE INV.PER.EMAIL = ''
          IF NOT(INV.PER.EMAIL) THEN INV.PER.EMAIL = 'Single'

          RETURN
*-------------------------------------------------------------------------*
SET.STMT.ACTION:*** Get the statement action for the customer

          FAX.REC  = 'STMT.FAX.DATA'
          FAX.TYPE = 'STMT.FAX'

          ST.ACTION = CUSS(64)<1,1>
          BT.ACTION = CUS(64)<1,1>

          * If bill-to and ship-to cn are the same then we are
          * processing the bill-to customer
          IS.BILL.TO = (BT.CN = ST.CN)

          * Figure out between the ship-to customer,
          * the bill-to customer and control file a
          * valid action to generate a statement. The options
          * are F = Fax, 0 - Do not generate, # > 0 times to print
          VALID.ACTION = ''; SOURCE = ''
          BEGIN CASE
          CASE ACTION.OVRD
             * Print a single statement when this flag is set
             * we assume the user wants to force a print regardless
             * of ship to or bill to
             VALID.ACTION = 1
          CASE ST.ACTION = '' AND NOT(IS.BILL.TO) AND NOT(PROCESS.AS.BILLTO)
             * If this customer is not a bill-to and does
             * not have an action explictly set then do not print
             VALID.ACTION = 0
          CASE ST.ACTION # '' AND (NUM(ST.ACTION) OR ST.ACTION = 'F')
             * Use Ship-to level setting
             VALID.ACTION = ST.ACTION
             SOURCE = 'Entity'
          CASE BT.ACTION # '' AND (NUM(BT.ACTION) OR BT.ACTION = 'F')
             * Use Bill-to level setting
             VALID.ACTION = BT.ACTION
             SOURCE = 'Entity'
          CASE DFLT.STMT # '' AND (NUM(DFLT.STMT) OR DFLT.STMT = 'F')
             * Control record setting (Bill-to Only)
             VALID.ACTION = DFLT.STMT
             SOURCE = 'Control'
          CASE IS.BILL.TO AND OTH.STMT.OPTS
             * If this customer is a bill-to and they are selecting by
             * a statement option other than 'Individual Customer(s)',
             * 'All Batch Stmt Customers', or 'Positive Balance' then we
             * need to set the action so that it can be filtered later.
             VALID.ACTION = 1
             SOURCE       = 'StmtOpts'
          CASE OTHERWISE; NULL
          END CASE

          IF DEF.CN.LIST AND NOT(VALID.ACTION) THEN
             * User specifically asked to print a statement
             * for this customer so make sure to generate something
             * in this case a printed statement
             VALID.ACTION = 1
          END

          * Populate the action to return
          BEGIN CASE
          CASE VALID.ACTION = 'F'
             * Fax 1 copy
             GOSUB GET.FAX.STATUS
             ACTION<ACT.IDX> = 'FAX':VM:FAX.STATUS:VM:1:VM:SOURCE
             STMT.WILL.FAX = YES
          CASE VALID.ACTION = 0
             * Do nothing
             ACTION<ACT.IDX> = ''
          CASE NUM(VALID.ACTION) AND (VALID.ACTION # '')
             * Print x# of times
             ACTION<ACT.IDX> = 'PRINT':VM:'OK':VM:VALID.ACTION:VM:SOURCE
          CASE OTHERWISE; NULL
          END CASE

          IF ACTION<ACT.IDX,1> = 'PRINT' AND NOT(ACTION.OVRD) THEN
             * Check for act trigger since we base our triggers off of
             * print. In the future we should not use act trigs for
             * anything other than defining properties of an action

             * Check for STMT.INV then STMT triggers.
             TRIG = 'T008'
             GOSUB GET.ACT.TRIG.STATUS
             IF ACT.TRIG.STATUS<1,1> = 'NONE' THEN
                TRIG = 'T007'
                GOSUB GET.ACT.TRIG.STATUS
             END

             IF ACT.TRIG.STATUS<1,1> = 'EMAIL' THEN
                * Replace just the action and the status.  We need to
                * maintain the number of copies from the 'PRINT' action.
                ACTION<ACT.IDX,1> = 'EMAIL'
                ACTION<ACT.IDX,2> = ACT.TRIG.STATUS<1,2>
                ACTION<ACT.IDX,4> = 'Trigger'
             END
          END

          * Increment counter as their may be more actions in the future
          IF ACTION<ACT.IDX> THEN
             ACT.IDX += 1
          END
          RETURN
*-------------------------------------------------------------------------*
SET.FORM.ACTION: *** Get the form action for the customer
          IF BATCH.TYPE = 'STMT.INV' THEN
             * In release 7, the print/fax invoice copies setting was
             * ignored when printing statements - no matter what the
             * setting was, it would always print/fax ONE copy of each
             * invoice.  In order to duplicate this functionality, we
             * need to see whether the statement is going to be printed
             * or faxed. The SET.STMT.ACTION subroutine will set the
             * STMT.WILL.FAX flag if the statement will be faxed.

             PROCESS.AS.BILLTO = YES
             GOSUB SET.STMT.ACTION      ;* get the action for the statement

             * Reset the action array because it contains the statment
             ACTION  = ''
             ACT.IDX = 1
          END

          FAX.REC  = 'INV.FAX.DATA'
          FAX.TYPE = 'INV.FAX'

          ST.ACTION = CUSS(50)<1,1>
          BT.ACTION = CUS(50)<1,1>

          * Figure out between the ship-to customer,
          * the bill-to customer and control file a
          * valid action to generate a invoice. The options
          * are -1 = Preview, 0 - Do not generate, # > 0 times to print
          VALID.ACTION = ''; SOURCE = ''
          BEGIN CASE
          CASE ST.ACTION # '' AND NUM(ST.ACTION)
             * Use Ship-to level setting
             VALID.ACTION = ST.ACTION
             IF BT.CN # ST.CN THEN
                SOURCE = 'Ship-to'
             END ELSE SOURCE = 'Bill-to'
          CASE BT.ACTION # '' AND NUM(BT.ACTION)
             * Use Bill-to level setting
             VALID.ACTION = BT.ACTION
             SOURCE = 'Bill-to'
          CASE DFLT.IPRT # '' AND NUM(DFLT.IPRT)
             * Control record
             VALID.ACTION = DFLT.IPRT
             SOURCE = 'Control'
          CASE OTHERWISE; NULL
          END CASE

          * If they are printing a statment, print 1 copy of the invoices
          IF BATCH.TYPE = 'STMT.INV' AND NOT(STMT.WILL.FAX) THEN
             VALID.ACTION = 1
          END

          * Populate the action to return
          BEGIN CASE
          CASE VALID.ACTION = -1
             * Preview queue only
             ACTION<ACT.IDX> = 'PREVIEW':VM:'OK':VM:1:VM:SOURCE
          CASE VALID.ACTION = 0
             * Do nothing
             ACTION<ACT.IDX> = ''
          CASE NUM(VALID.ACTION) AND (VALID.ACTION # '')
             * Print x# of times
             ACTION<ACT.IDX> = 'PRINT':VM:'OK':VM:VALID.ACTION:VM:SOURCE
          CASE OTHERWISE; NULL
          END CASE

          INV.PRT.ACT = ''
          IF ACTION<ACT.IDX,1> = 'PRINT' THEN
             * check for act trigger since we base our triggers off of
             * print. In the future we should not use act trigs for
             * anything other than defining properties of an action

             * if this is a stmt run, then invoices need to respect
             * a different trigger first: the Stmt-Invs trig.
             IF BATCH.TYPE = 'STMT.INV' THEN
                IF NOT(ACTION.OVRD) THEN
                   TRIG = 'T008'
                   GOSUB GET.ACT.TRIG.STATUS
                   IF ACT.TRIG.STATUS<1,1> = 'NONE' THEN
                      TRIG = 'T009'
                      GOSUB GET.ACT.TRIG.STATUS
                   END
                END
             END ELSE
                TRIG = 'T009'
                GOSUB GET.ACT.TRIG.STATUS
                IF ACT.TRIG.STATUS<1,1> = 'NONE' THEN
                   TRIG = 'T022'
                   GOSUB GET.ACT.TRIG.STATUS
                END
             END

             CPYS       = ACTION<ACT.IDX,3>
             INV.PRT.CT = 0

             * Loop through and parse out all 'Invoice Print' so that they
             * can be tacked on at the end of the actions.
             FOR ACX = ACT.CT TO 1 STEP -1
                TRIG.ACT = ACT.TRIG.STATUS<ACX,1>
                IF ACT.TRIG.STATUS<ACX,2> # 'T022' THEN CONTINUE
                IF TRIG.ACT#'EMAIL' AND TRIG.ACT#'XML' AND TRIG.ACT#'EDI' THEN CONTINUE

                INV.PRT.CT               += 1
                INV.PRT.ACT<INV.PRT.CT>   = ACT.TRIG.STATUS<ACX>
                INV.PRT.ACT<INV.PRT.CT,4> = 'Trigger'
                IF TRIG.ACT = 'EMAIL' THEN
                   INV.PRT.ACT<INV.PRT.CT,3> = CPYS
                END ELSE
                   INV.PRT.ACT<INV.PRT.CT,3> = 1
                END

                ACT.TRIG.STATUS = DELETE(ACT.TRIG.STATUS,ACX)
             NEXT ACX
             ACT.CT = DCOUNT(ACT.TRIG.STATUS,AM)

             * Loop through and handle all the 'In-lieu of Prt' triggers.
             FOR ACX = 1 TO ACT.CT
                BEGIN CASE
                CASE ACT.TRIG.STATUS<ACX,1> = 'EMAIL'
                   ACTION<ACX,1> = 'EMAIL'
                   ACTION<ACX,2> = ACT.TRIG.STATUS<ACX,2>
                   ACTION<ACX,3> = CPYS
                   ACTION<ACX,4> = 'Trigger'
                CASE ACT.TRIG.STATUS<ACX,1> = 'XML'
                   ACTION<ACX,1> = 'XML'
                   ACTION<ACX,2> = ACT.TRIG.STATUS<ACX,2>
                   ACTION<ACX,3> = 1
                   ACTION<ACX,4> = 'Trigger'
                CASE ACT.TRIG.STATUS<ACX,1> = 'EDI'
                   ACTION<ACX,1> = 'EDI'
                   ACTION<ACX,2> = ACT.TRIG.STATUS<ACX,2>
                   ACTION<ACX,3> = 1
                   ACTION<ACX,4> = 'Trigger'
                CASE OTHERWISE
                   * Do nothing, no triggers set
                END CASE
             NEXT ACX

             * Reset the counter in case there are secondary actions
             ACT.IDX = DCOUNT(ACTION,AM)
          END

          * Increment the index since there can be multiple actions*
          IF ACTION<ACT.IDX> THEN
             ACT.IDX += 1
          END

          ST.ACT = CUSS(50)<1,2>
          BT.ACT = CUS(50)<1,2>

          * Check for a secondary action
          VALID.ACTION = ''; SEC.SOURCE = ''
          EDI.PROFILE  = ''
          BEGIN CASE
          CASE ST.ACT # '' AND (NUM(ST.ACT) OR INDEX('EB',ST.ACT,1))
             * Use Ship-to level setting
             VALID.ACTION = ST.ACT
             IF BT.CN # ST.CN THEN
                SEC.SOURCE = 'Ship-to'
             END ELSE SEC.SOURCE = 'Bill-to'
             EDI.PROFILE = CUSS(15)
          CASE BT.ACT # '' AND (NUM(BT.ACT) OR INDEX('EB',BT.ACT,1))
             * Use Bill-to level setting
             VALID.ACTION = BT.ACT
             SEC.SOURCE = 'Bill-to'
             EDI.PROFILE = CUS(15)
          CASE DFLT.IFAX # '' AND (NUM(DFLT.IFAX) OR INDEX('EB',DFLT.IFAX,1))
             VALID.ACTION = DFLT.IFAX
             SEC.SOURCE = 'Control'
          CASE OTHERWISE; NULL
          END CASE

          * If we are doing a statement run, then we only want to either
          * print or fax one copy of the invoices
          IF BATCH.TYPE = 'STMT.INV' THEN
             IF STMT.WILL.FAX THEN
                * Stmt will fax, so we only want it to fax 1 invoice
                ACTION  = ''
                ACT.IDX = 1
                VALID.ACTION = 1
             END ELSE
                * We are just printing 1 invoice, so return
                RETURN
             END
          END

          * Do not inherit control print action if fax is
          * explictly set at the customer level
          NON.CONTROL = (SOURCE = 'Control' AND SEC.SOURCE # 'Control')
          IF VALID.ACTION AND NON.CONTROL THEN
             * Step on the primary action in this case since
             * it is assumed that the user will not want a printed
             * an fax invoice. So in this case if the print
             * action is derived from the control file it will
             * be overriden.
             ACTION<1> = ''
          END

          BEGIN CASE
          CASE VALID.ACTION = 'E'
             * EDI
             GOSUB GET.EDI.STATUS
             ACTION<ACT.IDX> = 'EDI':VM:EDI.STATUS:VM:1:VM:LOWER(LOWER(VALID.DOCS))
             *** Pass valid EDI coduments if sending via EDI
          CASE VALID.ACTION = 'B'
             * B2B
             GOSUB GET.B2B.STATUS
             ACTION<ACT.IDX> = 'B2B':VM:B2B.STATUS:VM:1:VM:SEC.SOURCE
          CASE VALID.ACTION = 0
             * Do nothing
             ACTION<ACT.IDX> = ''
          CASE NUM(VALID.ACTION) AND (VALID.ACTION # '')
             * Fax x# of times
             GOSUB GET.FAX.STATUS
             ACTION<ACT.IDX> = 'FAX':VM:FAX.STATUS:VM:VALID.ACTION:VM:SEC.SOURCE
          CASE OTHERWISE; NULL
          END CASE

          * If we are printing or faxing an invoice add the 'Invoice Print'
          * triggers here.
          IF ACTION # '' AND NUM(VALID.ACTION) AND VALID.ACTION THEN
             ACTION<-1> = INV.PRT.ACT
          END

          RETURN
*-------------------------------------------------------------------------*
GET.ACT.TRIG.STATUS: *** get the status for the activity triggers
          ACT.TRIG.STATUS = ''
          ACT.TRIG.STATUS<1,1> = 'NONE'
          ACT.TRIG.STATUS<1,2> = 'NONE'
          EMAIL           = ''
          ACT.CT          = 0

          * Determine what inheritance rules they want for triggers
          * based on the control record
          READV DFLT.INH FROM CTRLFILE,'ACT.TRIGGER.INHERIT',1 ELSE
             DFLT.INH = ''
          END

          * If control has inheritance enabled and customer is not
          * explictly set to not inherit triggers check levels
          IF DFLT.INH[1,1] = 'Y' AND CUSS(182)[1,1] # 'N' THEN
             * Try and read the trigger details from the enttfile based
             * on ST->BT->BR->CORP.CUS
             READ TRIG.DET FROM ENTTFILE,ST.CN:'~':TRIG ELSE
                READ TRIG.DET FROM ENTTFILE,BT.CN:'~':TRIG ELSE
                   TRIG.DET = ''
                   IF NUM(SELECT.BR) AND SELECT.BR THEN
                      READV BR.EN FROM TERRFILE,SELECT.BR,4 THEN
                         READ TRIG.DET FROM ENTTFILE,BR.EN:'~':TRIG ELSE
                            TRIG.DET = ''
                         END
                      END
                   END
                   IF NOT(TRIG.DET) THEN
                      READ TRIG.DET FROM ENTTFILE,CORPCN$:'~':TRIG ELSE
                         TRIG.DET = ''
                      END
                   END
                END
             END
          END ELSE
             READ TRIG.DET FROM ENTTFILE,ST.CN:'~':TRIG ELSE
                TRIG.DET = ''
                * If customer has inheritance enabled check the bill-to
                IF CUSS(182)[1,1] = 'Y' THEN
                   READ TRIG.DET FROM ENTTFILE,BT.CN:'~':TRIG ELSE
                      TRIG.DET = ''
                   END
                END
             END
          END

          ACT.CT = DCOUNT(TRIG.DET,VM)
          FOR ACX = 1 TO ACT.CT
             BEGIN CASE
             CASE TRIG.DET<5,ACX,3>
                * Do nothing, trigger is disabled
             CASE TRIG.DET<1,ACX> = 'S005' OR TRIG.DET<1,ACX> = 'S001' OR TRIG.DET<1,ACX> = 'S025' OR TRIG.DET<1,ACX> = 'S003' OR TRIG.DET<1,ACX> = 'S040' OR TRIG.DET<1,ACX> = 'S039'
                * Check the ATD to make sure it can produce an 'EMAIL' doc.
                * S005(HTML) and S001(TXT) are for Invoices.
                * S025(HTML) and S003(TXT) are for stmts.
                * S040(HTML) and S039(TXT) are for combo STMT.INV.
                ACT.TRIG.STATUS<ACX,1> = 'EMAIL'
                IF NOT(TRIG.DET<6,ACX,1>) THEN
                   ACT.TRIG.STATUS<ACX,2> = 'EMAIL ADDRESS UNDEFINED'
                END ELSE
                   ACT.TRIG.STATUS<ACX,2> = TRIG
                END

                * Only set Multiple Invoices per Email flag for Invoices
                * via Eclipse Forms.
                IF USE.ECLIPSE.FORMS$ OR ST.CN='4503' AND TRIG.DET<1,ACX> # 'S025' AND TRIG.DET<1,ACX> # 'S003' THEN
                   BEGIN CASE
                   CASE CUSS(50)<1,4>
                      ACTION<ACX,5> = CUSS(50)<1,4>[1,1]
                   CASE CUS(50)<1,4>
                      ACTION<ACX,5> = CUS(50)<1,4>[1,1]
                   CASE OTHERWISE
                      ACTION<ACX,5> = INV.PER.EMAIL[1,1]
                   END CASE
                END
             CASE TRIG.DET<1,ACX> = 'S029' OR TRIG.DET<1,ACX> = 'S030' OR TRIG.DET<1,ACX> = 'S033' OR TRIG.DET<1,ACX> = 'S034'
                ACT.TRIG.STATUS<ACX,1> = 'XML'
                ACT.TRIG.STATUS<ACX,2> = TRIG
             CASE TRIG.DET<1,ACX> = 'S041'
                ACT.TRIG.STATUS<ACX,1> = 'EDI'
                ACT.TRIG.STATUS<ACX,2> = TRIG
             CASE OTHERWISE
                ACT.TRIG.STATUS<ACX,1> = 'NONE'
                ACT.TRIG.STATUS<ACX,2> = 'NONE'
             END CASE
          NEXT ACX

          RETURN
*-------------------------------------------------------------------------*
GET.EDI.STATUS: *** Get the status for the EDI customer.
          EDI.STATUS = 'OK'
          UT.EDI.OUTBOUND.GRP.OK EDI.OK,EDI.PROFILE,0,VALID.DOCS
          IF NOT(EDI.OK) THEN
             EDI.STATUS  = 'NO OUTBOUND 810, 857 OR 880 SETUP IN TRADING '
             EDI.STATUS := 'PARTNER MAINTENANCE'
          END
          RETURN
*-------------------------------------------------------------------------*
GET.B2B.STATUS: *** Get the status for B2B
          B2B.STATUS = 'OK'
          RETURN
*-------------------------------------------------------------------------*
GET.FAX.STATUS: *** Get the status for Faxing
          FAX.STATUS = ''
          FAX.PHONE  = ''
          WHICH.CN   = ''
          PTR.ID     = ''

          READ FAX.DATA FROM CTRLFILE,FAX.REC ELSE FAX.DATA = ''

          PTR.ID = ''
          FAX.PTR.SELECT FAX.DATA<1,1>,SELECT.BR,PTR.ID
          IF PTR.ID = '' THEN
             * branch is not set up for faxing
             FAX.STATUS := 'BRANCH ':SELECT.BR
             FAX.STATUS := ' IS NOT SETUP FOR FAXING'
          END

          * Check if fax number is defined for customer
          * If no valid fax found go to FIND.FAX.CONTACT to use the first
          * fax number found to begin with "FAX". Upper/Lower case does
          * not matter due to MCU.
          BEGIN CASE
             CASE CUSS(97) OR CUSS(40)
                *use shipto flag (set in alt-bill) or use seperate AR flag.
                WHICH.CN = 'SHIP'
                CONTACTS = RAISE(OCONV(CUSS(16),'MCU'))
                LOCATE FAX.TYPE IN CONTACTS SETTING FAX.VM ELSE
                   GOSUB FIND.FAX.CONTACT
                END
                IF FAX.VM THEN
                   FAX.PHONE = CUSS(17)<1,FAX.VM>
                END
             CASE CUSS(91)<1,1>
                * use Alt ship-to address
                WHICH.CN = 'ALT-ST'
                FAX.PHONE = CUSS(121)
                * If the fax phone is null check the main shipto
                IF NOT(FAX.PHONE) THEN
                   CONTACTS = RAISE(OCONV(CUSS(16),'MCU'))
                   LOCATE FAX.TYPE IN CONTACTS SETTING FAX.VM ELSE
                      GOSUB FIND.FAX.CONTACT
                   END
                   IF FAX.VM THEN
                      FAX.PHONE = CUSS(17)<1,FAX.VM>
                   END
                END
             CASE CUS(91)<1,1>
                * use Alt bill-to address
                WHICH.CN = 'ALT-BT'
                FAX.PHONE = CUS(121)
             CASE OTHERWISE
                WHICH.CN = 'BILL'
          END CASE

          * If the fax phone is null use the main billto fax
          IF NOT(FAX.PHONE) THEN
             CONTACTS = RAISE(OCONV(CUS(16),'MCU'))
             LOCATE FAX.TYPE IN CONTACTS SETTING FAX.VM ELSE
                GOSUB FIND.FAX.CONTACT
             END
             IF FAX.VM THEN
                FAX.PHONE = CUS(17)<1,FAX.VM>
             END
          END

          IF NOT(FAX.PHONE) THEN
             IF FAX.STATUS THEN FAX.STATUS := ' AND '
             FAX.STATUS := 'Customer number '
             BEGIN CASE
                CASE WHICH.CN = 'SHIP'
                   FAX.STATUS := ST.CN
                CASE WHICH.CN = 'BILL'
                   FAX.STATUS := BT.CN
                CASE WHICH.CN = 'ALT-BT'
                   FAX.STATUS := BT.CN:' which is to use an alternate '
                   FAX.STATUS := 'fax number'
                CASE WHICH.CN = 'ALT-ST'
                   FAX.STATUS := ST.CN:' which is to use an alternate '
                   FAX.STATUS := 'fax number'
             END CASE
             FAX.STATUS := ' does NOT have a fax number defined.'
          END

          IF NOT(FAX.STATUS) THEN
             FAX.STATUS = 'OK'
          END
          RETURN
*-------------------------------------------------------------------------*
FIND.FAX.CONTACT:  *** Look for the first 3 characters of the contact name.
          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
*-------------------------------------------------------------------------*
!TSMITH~01/14/14~12:19
