          SUBROUTINE (OID,GID,OUTPUT.VIA,FILE,ERROR)
** Version# 61.0003[26] - 09/25/2011 - 11:15pm - SMITJR - eclipse
*** V63.0002 Change - Custom Coding . - 09/23/2011 - SMITJR - eclipse
*** V61.0003 Change - Custom Coding . - 09/25/2011 - SMITJR - eclipse
*** V63.0001 Change - Custom Coding .. - 06/01/2011 - SMITJR - eclipse
*** V61.0002 Change - Custom Coding .. - 06/01/2011 - SMITJR - eclipse
*** V61.0001 Change - Custom Coding .. - 05/15/2011 - SMITJR - eclipse
*** Subroutine - BATCH.INV.FTP.SET.BT
*-------------------------------------------------------------------------*
***  This routine sets up the output for BillTrust.  The BillTrust data is
*** segmented by Header, Body, Footer etc.  This routine sets the data in
*** those segments.
*-------------------------------------------------------------------------*

          GOSUB INIT

          GOSUB GET.TOTALS

          GOSUB SET.HEADER

          GOSUB SET.BODY

        *  GOSUB SET.PAYMENTS

        *  GOSUB SET.FOOTER

          GOSUB SET.FILE

          IF NOT(FTP.TEST) THEN
             OE.LOG.PRINT OID,GEN,'I':VM:YES,'Ops Technology',REPRINT
          END

          RETURN
*-------------------------------------------------------------------------*
INIT:

          UT.OPEN.FILE 'ENTITY.TRIGGER',ENTTFILE,ERR.MSG,YES
          IF ERR.MSG THEN
             RETURN
          END

          UT.OPEN.FILE 'CONTACT',CONTFILE,ERR.MSG,YES
          IF ERR.MSG THEN
             RETURN
          END

          *Determine if we are Release 7 software site or higher
          READV REL.LVL FROM CTRLFILE,'RELEASE.VER',1 ELSE REL.LVL = 0
          REL.LVL = FIELD(REL.LVL,'-',1)
          CONVERT '.' TO '' IN REL.LVL
          CONVERT '+' TO '' IN REL.LVL
          * Make sure the release levels in a consistant format.
          REL.LEN = LEN(REL.LVL)
          IF REL.LEN < 4 THEN
             DIFF = 4 - REL.LEN
             REL.LVL = REL.LVL:STR(0,DIFF)
          END
          IF REL.LVL GE 7200 THEN
             REL8.FLAG = YES
                    END ELSE REL8.FLAG = NO


          BT.HEADER      = ''
          BT.FOOTER      = ''
          BT.PAYMENT     = ''
          BT.LINE.ITEM   = ''
          BT.LINE.DESC   = ''
          RUNNING.TOTAL  = 0
          RUNNING.DISC   = 0
          ITEM.CT        = 0
          SUBT.LINES     = 0
          ALT.DESC       = ''
          SUB.TOTAL      = 0
          FAX.DATA       = ''
          EMAIL.DATA     = ''
          BR.TYPE        = ''
          BT.LD.NO       = 0

          READV FTP.TEST FROM CTRLFILE,'BATCH.DFLT.SETTINGS',4 ELSE
             FTP.TEST = NO
          END

          READ FTP.INFO FROM CTRLFILE,'BATCH.INV.FTP.INFO' ELSE FTP.INFO=''
          LOCAL.PATH = FTP.INFO<7>
          CSV.NAME   = FILE<2,1>:'.txt'

          UT.OPEN.FILE LOCAL.PATH,FTP.FILE,ERR.MSG

          MATREAD LED FROM LEDFILE,OID ELSE
             RETURN
          END

          LOCATE GID IN LED(12)<1> SETTING GEN ELSE
             RETURN
          END

          GOSUB GET.UD.INFO

          * Set dict common for retrieving User Defined data, using I-desc
          DICT.COMMON<1> = LED(2)<1,GEN,2>
          DICT.COMMON<2> = LED(2)<1,GEN,1>
          DICT.COMMON<3> = DATE()
          DICT.COMMON<4> = DATE()

          DICT.COMMON = ''
          BEGIN CASE
             CASE BR.TYPE = 'Prc';   DICT.COMMON<1> = LED(2)<1,GEN,1>
             CASE BR.TYPE = 'GL' ;   DICT.COMMON<1> = LED(2)<1,GEN,3>
             CASE OTHERWISE;         DICT.COMMON<1> = LED(2)<1,GEN,2>
          END CASE

          * Lot
          IF LED(98)<1,GEN> THEN LOT.FLAG = YES ELSE LOT.FLAG = NO

*-------------------------------------------------------------------------*
          **OPs Info
          GENERATION = LED(8)<1,GEN,1>
          AR.ID = OID:'.':GENERATION "R%3"

          PN      = LD(1)
          BR    = LED(2)<1,GEN,1>
          BT.CN = LED(1)<1,GEN>
          MSC.CODE = FIELD(TRANS('PRODUCT',PN,4,'X'),' ',2)


          ST.CN = LED(5)<1,GEN,1>
          GET.CUS BR,BT.CN,ST.CN,QSIGN
          SH.DT = OCONV(LED(9)<1,GEN,1>,'D4/')
          DUE.DT = OCONV(LED(21)<1,GEN,1>,'D4/')
          TERMS = LED(29)<1,GEN,1>
          IF TERMS = "NET 5" THEN
          TERMS = "NET 5"
          END ELSE
          IF TERMS = "N10" THEN
          TERMS = "NET 10"
          END ELSE
          IF TERMS = "N15NS" THEN
          TERMS = "NET 15"
          END ELSE
          IF TERMS = "N30" OR "N30NS" OR "N30S" THEN
          TERMS = "NET 30"
          END ELSE
          TERMS = "NA"
          END
          END
          END
          END

          COMPANY = TRANS('ENTITY',BT.CN,1,'X')<1,GEN,1>
          PROPERTY = TRANS('ENTITY',ST.CN,1,'X')<1,GEN,1>
          ORDERED.BY = LED(68)<1,GEN,1>
          PO = LED(13)<1,GEN,1>
          ORD.DT = OCONV(LED(4)<1,GEN,1>,'D4/')
          SHIP.VIA = LED(70)<1,GEN,1>
          IF SHIP.VIA[1,1] = "Z" OR FIELD(SHIP.VIA,'-',1)="OT" OR FIELD(SHIP.VIA,' ',1)="OT" THEN
          SHIP.VIA = "FREIGHT"
          END ELSE
          IF FIELD(SHIP.VIA,' ',1)="UPS" OR FIELD(SHIP.VIA,' ',2)="UPS" FIELD(SHIP.VIA,'-',1)="UPS" THEN
          SHIP.VIA = "UPS"
          END ELSE
          IF SHIP.VIA[1,2]="E" THEN
          SHIP.VIA = "FEDEX"
          END ELSE
          SHIP.VIA = ""
          END
          END
          END
          OUTSIDE.SLS = TRANS('INITIALS',LED(72)<1,GEN,1>,3,'X')
          SUBTOTAL = OCONV(LED(15)<1,GEN,1>,'MR2')*-1
          TAX.AMT = OCONV(LED(64)<1,GEN,1>,'MR2')
          DISC.AMT = OCONV(TRANS('AR',AR.ID,9,'X'),'MR2')
          GRAND.TOT = SUBTOTAL+TAX.AMT-DISC.AMT
          SHIP.INSTR = LED(74)<1,GEN,1>
          *:' ':LED(74)<1,GEN,2>:' ':LED(74)<1,GEN,3>:' ':LED(74)<1,GEN,4>:' ':LED(74)<1,GEN,5>


          * Set the BT/ST
          BT.CN   = LED(1)<1,GEN>
          ST.CN   = LED(5)<1,GEN>
          PRC.BR  = LED(2)<1,GEN,1>
          SHIP.BR = LED(2)<1,GEN,2>
          INVN    = LED(8)<1,GEN>
          ORD.ID  = OID:'.':INVN"R%3"
          OE.GET.QSIGN QSIGN,OID,GEN

          * Set up CUS and CUSS
          GET.CUS PRC.BR,BT.CN,ST.CN,-1

          IF LED(8)<1,GEN> = '' THEN
             LDIDS = LED(49)
             CONVERT VM TO SVM IN LDIDS
          END ELSE
             LDIDS = LED(48)<1,GEN>
          END

          READV B.NINE FROM TERRFILE,PRC.BR,5 ELSE B.NINE = ''
          IF B.NINE THEN
             BR.EN = B.NINE
          END ELSE
             READV BR.EN FROM TERRFILE,PRC.BR,4 ELSE BR.EN = ''
          END

          MATBUILD CUS.SAVE FROM CUS
          MATBUILD CUSS.SAVE FROM CUSS
          GET.CUS PRC.BR,BR.EN
          MATBUILD REM.BR FROM CUS
          MATPARSE CUS FROM CUS.SAVE
          MATPARSE CUSS FROM CUSS.SAVE

          * Check whether this Order is flagged for 'Single Invoicing'
          IF LED(86) AND LED(30)<1,GEN>[1,2] # 'CR' THEN
             SNGL.INV = 1
             GN.CT    = DCOUNT(LED(12),VM)

             * Loop through each Gen on the Order to determine if the
             * Order Gen we're printing the Invoice for is the first
             * shipment...
             FOR J = 1 TO GN.CT
                * Get the Invoice Status...
                ST = LED(6)<1,J>
                STATUS.OK = (ST # 'X' AND ST # 'Y' AND ST # '$' AND ST#'B')
                IF LED(8)<1,J> AND STATUS.OK THEN
                   * If the Order Gen we're printing an Invoice for
                   * is NOT the first shipment, we'll set our
                   * variable designating that fact. Subsequently we'll
                   * only include certain data on the document we're
                   * printing since the Customer was technically already
                   * 'Invoiced' when the first shipment went out.
                   IF LED(8)<1,J> < LED(8)<1,GEN> THEN SNGL.INV = 2; EXIT
                END
             NEXT J
          END ELSE
             SNGL.INV = 0
          END

          MATREAD AR FROM ARFILE,ORD.ID ELSE MAT AR = ''

          IF AR(9) THEN
             MATBUILD ARREC FROM AR
             DISC.AMT = AR.GET.DBAL(ARREC,DATE())
             AOD.BAL = AR.GET.BAL(ARREC,DATE())
             IF AOD.BAL = 0 THEN DISC.AMT = 0
          END ELSE
             DISC.AMT = 0
          END

          *DISC.DT  = AR(11)
          *DUE.DT   = AR(12)

          RETURN
*-------------------------------------------------------------------------*
GET.UD.INFO: *** Read all user defined information from their respective
             *** control records
          UD.INFO     = ''
          UD.LN.TYPS  = 'H':AM:'D'

          FOR UD.CTR = 1 TO DCOUNT(UD.LN.TYPS,AM)
             LN.TYP = UD.LN.TYPS<UD.CTR>
             READ UDREC FROM CTRLFILE,'BATCH.UD.INV.':LN.TYP THEN
                UD.INFO<UD.CTR> = LOWER(UDREC)
             END
          NEXT UD.CTR

          RETURN
*-------------------------------------------------------------------------*
SET.HEADER:
          CR = CHAR(13)
          LDID          = ''
          BT.HEADER<1>  = 'H'
          BT.HEADER<2>  = AR.ID
          BT.HEADER<3>  = ''
          BT.HEADER<4>  = SH.DT
          BT.HEADER<5>  = ''
          BT.HEADER<6>  = DUE.DT
          BT.HEADER<7>  = TERMS
          BT.HEADER<8>  = ST.CN
          BT.HEADER<9>  = COMPANY
          BT.HEADER<10> = PROPERTY
          BT.HEADER<11> = ORDERED.BY
          BT.HEADER<12> = PO
          BT.HEADER<13> = ''
          BT.HEADER<14> = ORD.DT
          BT.HEADER<15> = SHIP.VIA
          BT.HEADER<16> = OUTSIDE.SLS
          BT.HEADER<17> = SUBTOTAL
          BT.HEADER<18> = ''
          BT.HEADER<19> = TAX.AMT
          BT.HEADER<20> = GRAND.TOT
          BT.HEADER<21> = SHIP.INSTR


          * Build the Ship Branch address info
          READV BR.EN FROM TERRFILE,SHIP.BR,4 ELSE BR.EN = ''
          READ S.ADDR FROM CUSFILE,BR.EN ELSE S.ADDR = ''

*          BT.HEADER<14> = S.ADDR<1>
*          BT.HEADER<15> = S.ADDR<2,1>
*          BT.HEADER<16> = S.ADDR<2,2>
*          BT.HEADER<17> = ''
*          BT.HEADER<18> = S.ADDR<3>
*          BT.HEADER<19> = S.ADDR<4>
*          BT.HEADER<20> = S.ADDR<5>
*          CONTACTS = RAISE(OCONV(S.ADDR<16>,'MCU'))
*          FINDSTR 'MAIN' IN CONTACTS SETTING POS ELSE
*             FINDSTR 'PHONE' IN CONTACTS SETTING POS ELSE
*                POS = 1
*             END
*          END
*          BT.HEADER<21> = S.ADDR<17,POS>
*          FAX.PHONE = ''
*          LOCATE 'INV.FAX' IN CONTACTS SETTING FAX.VM ELSE
*             GOSUB FIND.FAX.CONTACT
*          END
*          IF FAX.VM THEN
*             FAX.PHONE = S.ADDR<17,FAX.VM>
*          END
*          BT.HEADER<22> = FAX.PHONE

          * Build the Ship-to Addr
*          BT.HEADER<30> = LED(78)<1,GEN,6>
*          IF NOT(BT.HEADER<30>) THEN BT.HEADER<30> = CUSS(1)
*          BT.HEADER<31> = LED(78)<1,GEN,1>
*          BT.HEADER<32> = LED(78)<1,GEN,2>
*          BT.HEADER<33> = ''
*          BT.HEADER<34> = LED(78)<1,GEN,4>
*          BT.HEADER<35> = LED(78)<1,GEN,5>
*          BT.HEADER<36> = LED(75)<1,GEN>

          * Build the Bill-to Addr
*          BEGIN CASE
*             CASE CUSS(97)
                * ShipTo is flagged to use the ShipTo for the billing
*                BT.HEADER<23> = CUSS(1)<1,1>
*                BT.HEADER<24> = CUSS(2)<1,1>
*                BT.HEADER<25> = CUSS(2)<1,2>
*                BT.HEADER<26> = ''
*                BT.HEADER<27> = CUSS(3)
*                BT.HEADER<28> = CUSS(4)
*                BT.HEADER<29> = CUSS(5)
*
*                CONTACTS = RAISE(OCONV(CUSS(16)<1>,'MCU'))
*                LOCATE 'INV.FAX' IN CONTACTS SETTING FAX.VM ELSE
*                   GOSUB FIND.FAX.CONTACT
*                END
*                IF FAX.VM THEN
*                   FAX.DATA<2> = CUSS(17)<1,FAX.VM>
*               END
*             CASE CUSS(91)<1,1>
*                * If the ShipTo is flagged to use an Alternate ShipTo...
*                BT.HEADER<23> = CUSS(91)<1,1>
*                BT.HEADER<24> = CUSS(92)<1,1>
*                BT.HEADER<25> = CUSS(92)<1,2>
*                BT.HEADER<26> = ''
*                BT.HEADER<27> = CUSS(93)
*                BT.HEADER<28> = CUSS(94)
*                BT.HEADER<29> = CUSS(95)

                FAX.DATA<2>   = CUSS(121)
                IF NOT(FAX.DATA<2>) THEN
                   CONTACTS = RAISE(OCONV(CUSS(16)<1>,'MCU'))
                   LOCATE 'INV.FAX' IN CONTACTS SETTING FAX.VM ELSE
                      GOSUB FIND.FAX.CONTACT
                   END
                   IF FAX.VM THEN
                      FAX.DATA<2> = CUSS(17)<1,FAX.VM>
                   END
                END

*             CASE CUS(91)<1,1>
                * If the BillTo is flagged to use an Alternate BillTo...
*                BT.HEADER<23> = CUS(91)<1,1>
*                BT.HEADER<24> = CUS(92)<1,1>
*                BT.HEADER<25> = CUS(92)<1,2>
*                BT.HEADER<26> = ''
*                BT.HEADER<27> = CUS(93)
*                BT.HEADER<28> = CUS(94)
*                BT.HEADER<29> = CUS(95)
*
*               FAX.DATA<2>   = CUS(121)
*                IF NOT(FAX.DATA<2>) THEN
*                   CONTACTS = RAISE(OCONV(CUS(16)<1>,'MCU'))
*                   LOCATE 'INV.FAX' IN CONTACTS SETTING FAX.VM ELSE
*                      GOSUB FIND.FAX.CONTACT
*                   END
*                   IF FAX.VM THEN
*                      FAX.DATA<2> = CUS(17)<1,FAX.VM>
*                   END
*                END


*             CASE OTHERWISE
*                * Otherwise, use normal billing address...
*                BT.HEADER<23> = CUS(1)<1,1>
*                BT.HEADER<24> = CUS(2)<1,1>
*                BT.HEADER<25> = CUS(2)<1,2>
*                BT.HEADER<26> = ''
*                BT.HEADER<27> = CUS(3)
*                BT.HEADER<28> = CUS(4)
*                BT.HEADER<29> = CUS(5)
*
*                CONTACTS = RAISE(OCONV(CUS(16)<1>,'MCU'))
*                LOCATE 'INV.FAX' IN CONTACTS SETTING FAX.VM ELSE
*                   GOSUB FIND.FAX.CONTACT
*                END
*                IF FAX.VM THEN
*                   FAX.DATA<2> = CUS(17)<1,FAX.VM>
*                END
*          END CASE

          * order date
          *BT.HEADER<37> = OCONV(LED(4)<1,GEN>,'D2/')

          * ship date
          *BT.HEADER<38> = OCONV(LED(9)<1,GEN>,'D2/')

          * Customer PO number
          *BT.HEADER<39> = LED(13)<1,GEN>

          * Order By
          ORD.BY = LED(68)<1,GEN>
          READ VAL.OBY FROM CTRLFILE,'OE.VAL.CUS.CONTACT' ELSE VAL.OBY = ''
          IF VAL.OBY THEN
             IF ORD.BY[1,1] = '#' THEN
                ORD.BY = TRIM(ORD.BY,'#')
                READ OB.REC FROM CONTFILE,ORD.BY THEN
                   ORD.BY = OB.REC<1>:' ':OB.REC<3>
                END
             END
          END
          *BT.HEADER<40> = ORD.BY

          * Salesman
          SALESMAN = LED(72)<1,GEN>
          IF SALESMAN # '' THEN
             READV SALESMAN FROM INIFILE,SALESMAN,3 ELSE NULL
          END
         * BT.HEADER<41> = SALESMAN

          * Ship Via
         * BT.HEADER<42> = LED(70)<1,GEN>

          * Terms Description
          TERMS.ID = LED(29)<1,GEN>
          READV TERMS.DESC FROM TERMSFILE,TERMS.ID,1 ELSE
             TERMS.DESC = TERMS.ID
          END
          READV TERMS.DISC FROM TERMSFILE,TERMS.ID,2 ELSE TERMS.DISC = ''
          IF TERMS.DISC[1,1] = "P" THEN PASS.THRU = YES ELSE PASS.THRU=NO
         * BT.HEADER<43> = TERMS.DESC

          * Writer
          WRITER = LED(73)<1,GEN>
          READV WRITER FROM INIFILE,WRITER,3 ELSE NULL
         * BT.HEADER<44> = WRITER

          * Customer Release
         * BT.HEADER<45> = LED(65)<1,GEN>

          * Price Branch
          *BT.HEADER<46> = PRC.BR

          * Phone Number
          *BT.HEADER<47> = CUS(17)<1,1>

          * Duplicate Flag
          DUPLICATE.INV = 0
          IF INVOICE.COPY.CT$+0 > 1 THEN DUPLICATE.INV = 1
          *BT.HEADER<48> = DUPLICATE.INV

          * Due Date
         * BT.HEADER<49> = OCONV(AR(12),'D2/')

          * Send via
          *BT.HEADER<50> = OUTPUT.VIA<1,1>

          OUTPUT.INFO = ''
          BEGIN CASE
             CASE OUTPUT.VIA<1,1> = 'FAX'
                READ FAX.INFO FROM CTRLFILE,'INV.FAX.DATA' THEN
                   FAX.DATA<1> = FAX.INFO<1,3>
                   FAX.DATA<3> = FAX.INFO<1,5>
                   CONVERT SVM TO CHAR(9) IN FAX.DATA<3>
                   FAX.DATA<4> = FAX.INFO<1,4>
                   FAX.DATA<5> = OCONV(TIME(),'MTS')
                   FAX.DATA<6> = OCONV(DATE(),'D2/')
                END ELSE
                   FAX.DATA<1> = 'Accounts Payable'
                   FAX.DATA<3> = ''
                   FAX.DATA<4> = 'Accounts Receivable'
                   FAX.DATA<5> = OCONV(TIME(),'MTS')
                   FAX.DATA<6> = OCONV(DATE(),'D2/')
                END
             CASE OUTPUT.VIA<1,1> = 'EMAIL'
                READ TRIG.DET FROM ENTTFILE,ST.CN:'~':OUTPUT.VIA<1,2> ELSE
                   READ TRIG.DET FROM ENTTFILE,BT.CN:'~':OUTPUT.VIA<1,2> ELSE
                      TRIG.DET = ''
                   END
                END
                IF TRIG.DET THEN
                   EMAIL.DATA<1> = TRIG.DET<6,1,1>
                   CONVERT SSVM TO CHAR(9) IN EMAIL.DATA<1>
                   EMAIL.DATA<2> = TRIG.DET<6,1,7>
                   CONVERT SSVM TO CHAR(9) IN EMAIL.DATA<2>
                   EMAIL.DATA<3> = TRIG.DET<6,1,8>
                   CONVERT SSVM TO CHAR(9) IN EMAIL.DATA<3>
                   EMAIL.DATA<4> = TRIG.DET<6,1,5>
                   EMAIL.DATA<5> = TRIG.DET<6,1,14>
                   EMAIL.DATA<6> = TRIG.DET<6,1,12>
                   IF TRIG.DET<1> = 'S003' THEN
                      EMAIL.DATA<7> = 'TEXT'
                   END ELSE
                      EMAIL.DATA<7> = 'HTML'
                   END
                END
          END CASE

          * Email - TO
         * BT.HEADER<51> = EMAIL.DATA<1>

          * Email - CC
         * BT.HEADER<52> = EMAIL.DATA<2>

          * Email - BCC
         * BT.HEADER<53> = EMAIL.DATA<3>

          * Email - Subject
         * BT.HEADER<54> = EMAIL.DATA<4>

          * Email - Body
          *BT.HEADER<55> = EMAIL.DATA<5>

          * Email - From
          *BT.HEADER<56> = EMAIL.DATA<6>

          * Format of Email
          *BT.HEADER<57> = EMAIL.DATA<7>

          * Fax - To Contact
          *BT.HEADER<58> = FAX.DATA<1>

          * Fax - To Number
         * BT.HEADER<59> = FAX.DATA<2>

          * Fax - To Message
          *BT.HEADER<60> = FAX.DATA<3>

          * Fax - From Contact
         * BT.HEADER<61> = FAX.DATA<4>

          * Fax - Send Time
          *BT.HEADER<62> = FAX.DATA<5>

          * Fax - Send Date
          *BT.HEADER<63> = FAX.DATA<6>

          * COD
          *IF CUS(28) = "COD" THEN
          *   BT.HEADER<64> = YES
          *END ELSE
          *   BT.HEADER<64> = NO
          *END

          * Ship instructions
          SHP.INST  = LED(74)<1,GEN>
          IF SHP.INST THEN
             SHP.CT    = DCOUNT(SHP.INST,SVM)
             SHIP.INST = ''
             FOR XXX = 1 TO SHP.CT
                SHIP.INST := SHP.INST<1,1,XXX>:' '
             NEXT XXX
            * BT.HEADER<65> = SHIP.INST:''
          END

          READ LEDL.REC FROM LEDLFILE, OID THEN
             IF LEDL.REC<23,GEN>[1,3] = 'UPS' AND LEDL.REC<24,GEN> # '' THEN
              *  BT.HEADER<66> = LEDL.REC<24,GEN>
             END
          END

          * Shipping Branch Number
         * BT.HEADER<67> = SHIP.BR

          * Pricing Branch Information
          READV BR.EN FROM TERRFILE,PRC.BR,4 ELSE BR.EN = ''
          READ P.ADDR FROM CUSFILE,BR.EN ELSE P.ADDR = ''

          *BT.HEADER<68> = P.ADDR<1>
          *BT.HEADER<69> = P.ADDR<2,1>
          *BT.HEADER<70> = P.ADDR<2,2>
          *BT.HEADER<71> = ''
          *BT.HEADER<72> = P.ADDR<3>
          *BT.HEADER<73> = P.ADDR<4>
          *BT.HEADER<74> = P.ADDR<5>
          CONTACTS = RAISE(OCONV(P.ADDR<16>,'MCU'))
          FINDSTR 'MAIN' IN CONTACTS SETTING POS ELSE
             FINDSTR 'PHONE' IN CONTACTS SETTING POS ELSE
                POS = 1
             END
          END
         * BT.HEADER<75> = P.ADDR<17,POS>
          FAX.PHONE = ''
          LOCATE 'INV.FAX' IN CONTACTS SETTING FAX.VM ELSE
             GOSUB FIND.FAX.CONTACT
          END
          IF FAX.VM THEN
             FAX.PHONE = P.ADDR<17,FAX.VM>
          END
         * BT.HEADER<76> = FAX.PHONE

          LN.TYP = 'HDR'
          LDID   = ''

          GOSUB LOAD.UD.VALS

          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
*-------------------------------------------------------------------------*
SET.BODY:

          SV.GEN   = GEN
          ATTACHED = ''
          LD.GCT   = DCOUNT(LDID.GENS,AM)

          BT.LD.NO = 0
          FOR LD.GEN = 1 TO LD.GCT

             GEN = LDID.GENS<LD.GEN>

             IF LED(8)<1,GEN> = '' THEN
                LDIDS = LED(49)
                CONVERT VM TO SVM IN LDIDS
             END ELSE
                LDIDS = LED(48)<1,GEN>
             END

             * Prepass for subtotal pricing detail Y/N
             NO.PRICING = ''
             FRST.DET   = 1
             LD.CT      = DCOUNT(LDIDS,SVM)

             FOR LD.NO = 1 TO LD.CT
                LDID = LDIDS<1,1,LD.NO>
                LD.GET LDID
                IF LD(1) = 'S' THEN
                   IF LD(21) = '' THEN
                      * NO PRICING
                      FOR DL = FRST.DET TO LD.NO
                         NO.PRICING<DL> = YES
                      NEXT DL
                   END
                   FRST.DET = LD.NO + 1
                END
             NEXT LD.NO

             LDID.CT = DCOUNT(LDIDS,SVM)
             FOR LD.NO = 1 TO LDID.CT
                LDID = LDIDS<1,1,LD.NO>
                LD.GET LDID

                PN = LD(1)

                * Include Job Management Items
                JM.ITEM = (PN[1,2] = "L#")

                BEGIN CASE
                   CASE NUM(PN) OR JM.ITEM

                      QS = (SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)) * QSIGN

                      IF QS # 0 THEN
                          IF LED(92)<1,GEN,2> THEN
                XRATE = OCONV(LED(92)<1,GEN,2>,'MR4')
             END ELSE
                XRATE = 1
             END


DFLT.UM = TRANS('PRODUCT',PN,16,'X')<1,1,1>
TOL.QTY = (LD(5)<1,GEN>*-1)
IF TOL.QTY < 0 THEN TOL.QTY = LD(5)<1,GEN>

CS.UM =  TRANS('PRODUCT',PN,7,'X')<1,1,2>
IF DFLT.UM="2" THEN TOL.QTY = (LD(5)<1,GEN>*-1)/CS.UM

PER.UM = LD(23)<1,1>
UOM.PER.GET PER.UM,PER                 ;* Per Unit of Measure
PL.UM = TRANS('PRICE.LINE',PRD(9),3,'X')
IF PER.UM='' THEN PER.UM = PL.UM

SEL.PRC = OCONV((LD(8)<1,GEN>+0)/ XRATE,'MR9')  "R9#15"
IF DFLT.UM="2" THEN SEL.PRC = OCONV((LD(8)<1,GEN>+0)*CS.UM /XRATE,'MR9')  "R9#15"



                          COGS = '$':COGS
                          PSC = TRANS('PRODUCT',PN,19,'X')
                          SERIAL = LD(32)<1,GEN>
                          COST.CODE = LD(2)<1,GEN>

                          DESC = TRANS('PRODUCT',PN,1,'X')<1,1,1>
                          COMM = FIELD(TRANS('ENTITY.PN.IDS',BT.CN:'~':MSC.CODE:'~~':PN,9,'X'),': ',2)


                         BT.LD.NO += 1
                         BT.LINE.ITEM<BT.LD.NO,1> = 'D'
                         BT.LINE.ITEM<BT.LD.NO,2> = ''
                         BT.LINE.ITEM<BT.LD.NO,3> = AR.ID
                         BT.LINE.ITEM<BT.LD.NO,4> = PN
                         BT.LINE.ITEM<BT.LD.NO,5> = DESC
                         BT.LINE.ITEM<BT.LD.NO,6> = TOL.QTY
                         BT.LINE.ITEM<BT.LD.NO,7> = PER.UM
                         BT.LINE.ITEM<BT.LD.NO,8> = SEL.PRC
                         BT.LINE.ITEM<BT.LD.NO,9> = ''
                         BT.LINE.ITEM<BT.LD.NO,10> = COMM
                         BT.LINE.ITEM<BT.LD.NO,11> = ''
                         BT.LINE.ITEM<BT.LD.NO,12> = SERIAL
                         *BT.LINE.ITEM<BT.LD.NO,13> = CR



                         SUBT.LINES += 1

                         * Load Product and Pricing arrays
                         GET.ALL.PRD PRC.BR,PN,QSIGN,GROUP

                         OE.DESC.GET DESC,ALT.DESC,"Invoice Printing"
                         IF LD(36)<1,GEN,1>  THEN
                            DESC<1,-1> = LD(36)<1,GEN,1>
                         END

                         * If there is customer pricing for this line item
                         * we will add it to the description
                         IF LED(144)<1,2> THEN
                            IF LD(127)<1,GEN> THEN
                               READV CUSCL FROM ENTFILE,LED(144)<1,2>,1 ELSE
                                  CUSCL = LED(144)<1,2>
                               END
                               CUSCL   = CUSCL<1,1>
                               CUS.PRC = ICONV(OCONV(LD(127)<1,GEN>,'MR9'),'MR3')
                               CUS.PRC = OCONV(CUS.PRC,'MR3')
                               DESC<1,-1> = CUSCL:"'s price - ":CUS.PRC
                            END
                         END

                         OE.CALC.QOPEN OID,QSIGN,QOPEN,GEN

                         * Check whether the product is flagged for Serial
                         * Number Tracking at the Shipping Branch...
                         PRD.BR.GET.VAL SHIP.BR,PN,25,SERIAL.TRACKING

                         IF SERIAL.TRACKING AND SERIAL.TRACKING # "N" THEN
                            SN.LIST = LD(32)<1,GEN>
                            SN.CT   = DCOUNT(SN.LIST,SVM)
                            FOR SN  = 1 TO SN.CT
                               IF SN.LIST<1,1,SN> THEN
                                  DESC<1,-1> = 'Serial#: ':SN.LIST<1,1,SN>
                               END
                            NEXT SN
                         END

                         * Kits
                         BEGIN CASE
                            CASE CUS(74) = "Yes"
                               IF LD(31) THEN
                                  GOSUB SET.KIT.DESCRIPTION
                               END
                            CASE CUS(74) = "Default" OR CUS(74) = ''
                               IF LD(31) AND LD(38)<1,2> = '1' THEN
                                  GOSUB SET.KIT.DESCRIPTION
                               END
                            CASE OTHERWISE
                               NULL
                         END CASE

                         ITEM.CT += 1

                         * LOT
                         IF LOT.FLAG THEN
                            BLINE = 0
                            BOD   = ''
                            SSPC  = 1
                            OE.LOT.PRINT.SETUP OID,GEN,'S','I',BLINE,SSPC,BOD,,'',LDID
                            DESC<1,-1> = LOWER(BOD)
                         END
                         GOSUB SET.LINE.DESCRIPTION

                         UOM = LD(23)

                         * LOT
                         IF LOT.FLAG THEN
                            QS    = 1
                            QOPEN = 1
                         END

                         IQ.TO.ALPHA PLNE(3),PRD(7),UOM,QS,QS1,US1,QS2,US2,QS.ALPHA
                         IQ.TO.ALPHA PLNE(3),PRD(7),UOM,QOPEN,QO1,UO1,QO2,UO2,QOPEN.ALPHA

                         QTY.ORD = ''
                         QTY.SHP = ''
                         UOMS    = ''
                         IF SNGL.INV = 2 THEN
                            QTY.ORD = QS1
                            UOMS    = US1
                            IF QS2 THEN
                               QTY.ORD<2> = QS2
                               UOMS<2>    = US2
                            END
                         END ELSE
                            QTY.ORD = QO1
                            UOMS    = UO1
                            IF QO2 THEN
                               QTY.ORD<2> = QO2
                               UOMS<2>    = UO2
                            END
                            LOCATE US1 IN UOMS SETTING POS ELSE
                               UOMS<POS> = US1
                            END
                            QTY.SHP<POS> = QS1
                            IF QS2 THEN
                               LOCATE US2 IN UOMS SETTING POS ELSE
                                  UOMS<POS> = US2
                               END
                               QTY.SHP<POS> = QS2
                            END
                         END
                      END ELSE
                         CONTINUE
                      END

                      * Part Number
                      *BT.LINE.ITEM<BT.LD.NO,4>  = PN

                      * Quantity Ordered
                      IF DCOUNT(QTY.ORD,AM) > 1 THEN
                         CONVERT AM TO '|' IN QTY.ORD
                      END
                      *BT.LINE.ITEM<BT.LD.NO,5>  = QTY.ORD

                      * Quantity Shipped
                      IF DCOUNT(QTY.SHP,AM) > 1 THEN
                         CONVERT AM TO '|' IN QTY.SHP
                      END
                      *BT.LINE.ITEM<BT.LD.NO,6>  = QTY.SHP

                      * Unit of Measure
                      IF DCOUNT(UOMS,AM) > 1 THEN
                         CONVERT AM TO '|' IN UOMS
                      END
                      *BT.LINE.ITEM<BT.LD.NO,7>  = UOMS

                      * Price per Unit of Measure
                      PRICE.PER.GET PER.QTY,PER.UM,PN,PRC.BR,LED(4)<1,GEN>

                      * Net Price
                      NET = LD(8)<1,GEN>
                      * The net price in ledger(8) is based on a single unit
                      * The net price needs to be multiplied by the per.qty
                      * to get the unit price that is shown on the order
                      * screen.  i.e. if an item is $10 for a box of 5 the
                      * net price in ledger will be $2 and the unit price
                      * would be $10.
                      UNITPRICE = NET*PER.QTY
                      IF LED(92)<1,GEN,2> THEN
                         UNITPRICE = INT(UNITPRICE / OCONV(LED(92)<1,GEN,2>,'MR4'))
                      END

                      * Hide pricing if Subtotal Show Pricing is No
                      **IF (NO.PRICING<LD.NO>) THEN
                      *   BT.LINE.ITEM<BT.LD.NO,8> = ''
                      *END ELSE
                      *   BT.LINE.ITEM<BT.LD.NO,8> = OCONV(UNITPRICE,'MR9')
                      *END

                      * Hide pricing if Subtotal Show Pricing is No
                      *IF (NO.PRICING<BT.LD.NO>) THEN
                      *   BT.LINE.ITEM<BT.LD.NO,9> = ''
                      *END ELSE
                      *   BT.LINE.ITEM<BT.LD.NO,9> = PER.UM
                      *END

                      * Extended Price (subtotal)
                      * Extended price is the price for a single unit times
                      * the quantity sold so you need to you the net stored
                      * in ledger rather than unitprice.
                      EXT = ICONV(OCONV(NET * QS,'MR9'),'MR2')

                      * Hide pricing if Subtotal Show Pricing is No
                      * Transmit "$0.00" instead null for single invoices
                      *BEGIN CASE
                      *   CASE (NO.PRICING<LD.NO>)
                      *      BT.LINE.ITEM<BT.LD.NO,10> = ''
                      *   CASE (SNGL.INV = 2)
                      *      BT.LINE.ITEM<BT.LD.NO,10> = '0.00'
                      *   CASE OTHERWISE
                      *      BT.LINE.ITEM<BT.LD.NO,10> = OCONV(EXT,'MR2')
                      *END CASE

                      * List Override Pricing
                      * Hide pricing if Subtotal Show Pricing is No
                      *IF (NO.PRICING<LD.NO>) THEN
                      *   BT.LINE.ITEM<BT.LD.NO,11> = ''
                      *END ELSE
                       *  BT.LINE.ITEM<BT.LD.NO,11> = OCONV(LD(86)<1,GEN>,'MR3')
                      *END

                      * Largest UOM
                     * BT.LINE.ITEM<BT.LD.NO,12> = LD(23)<1,GEN>

                      * Tax Flag
                      *BT.LINE.ITEM<BT.LD.NO,13> = LD(22)<1,GEN>

                      * Line Item Freight
                      *BT.LINE.ITEM<BT.LD.NO,14> = LD(34)<1,GEN>

                      * Price Line
                      *BT.LINE.ITEM<BT.LD.NO,15> = PRD(9)<1>

                      * Cash Discount Percent
                      *TERMS.ID = LED(29)<1,GEN>
                      *READV TERMS.DISC FROM TERMSFILE,TERMS.ID,2 ELSE TERMS.DISC = ''
                      *IF TERMS.DISC[1,1] = "P" THEN
                      *   PASS.THRU = YES
                      *END ELSE
                      *   PASS.THRU = NO
                      *END

                      CD = ''
                      IF PASS.THRU THEN
                         * Get the pass along percent
                         IF REL8.FLAG THEN
                            * Begin release 7.2+, 8.0 code
                            PRD.BR.GET.VAL PRC.BR,PN,38,CD
                            IF CD = '' THEN
                               PLNE.BR.GET.VAL PRC.BR,PRD(9),3,CD
                            END
                         END ELSE
                            * Begin release 7 code
                            CD = PRD(68)<1,1>
                            IF CD = '' THEN
                               CD = PLNE(14)<1,PRC.BR>
                            END
                         END
                         IF CD # '' THEN
                            CD = OCONV(CD,"MR1")
                         END
                      END

                      *BT.LINE.ITEM<BT.LD.NO,16> = CD

                      IF (SNGL.INV=2) THEN
                         RUNNING.TOTAL = 0
                         SUB.TOTAL     = 0
                      END ELSE
                         RUNNING.TOTAL += EXT
                         SUB.TOTAL     += EXT
                      END
                      LN.TYP         = 'ITM'
                      GOSUB LOAD.UD.VALS
                   CASE PN = 'C'
                      BT.LD.NO += 1
                      BT.LINE.ITEM<BT.LD.NO,1> = 'CMT'
                      BT.LINE.ITEM<BT.LD.NO,2> = BT.CN
                      BT.LINE.ITEM<BT.LD.NO,3> = ORD.ID

                      LN.TYP                = 'CMT'
                      GOSUB LOAD.UD.VALS

                      IF SNGL.INV = 1 THEN
                         LOCATE LDID IN ATTACHED SETTING NOTHING THEN
                            CONTINUE
                         END
                      END
                      OE.DESC.GET DESC,ALT.DESC,"Invoice Printing"

                      GOSUB SET.LINE.DESCRIPTION

                   CASE PN = 'S'
                      BT.LD.NO += 1
                      BT.LINE.ITEM<BT.LD.NO,1> = 'SUB'
                      BT.LINE.ITEM<BT.LD.NO,2> = BT.CN
                      BT.LINE.ITEM<BT.LD.NO,3> = ORD.ID
                      IF SUBT.LINES THEN
                         SUBT.SW = YES
                         OE.DESC.GET DESC,ALT.DESC,"Invoice Printing"

                         GOSUB SET.LINE.DESCRIPTION

                         BT.LINE.ITEM<BT.LD.NO,4> = OCONV(SUB.TOTAL,'MR2')

                         SUBT.LINES = 0
                         SUBT.SW    = NO
                         SUB.TOTAL  = 0
                      END

                      LN.TYP                = 'SUB'
                      GOSUB LOAD.UD.VALS
                END CASE
             NEXT LD.NO
          NEXT LD.GEN

          GEN = SV.GEN

          RETURN
*-------------------------------------------------------------------------*
SET.FOOTER:

          RETURN
*-------------------------------------------------------------------------*
SET.PAYMENTS:

          PMT      = ''
          PMT.INDX = 1
          LN.TYP   = 'PMT'

          US.FUNDS = LED(92)<1,GEN,2>

          SOE.PAYMENTS.DISC ORD.ID,PAY.IDS,PAY.DTS,PAY.AMTS,DISC.TAKEN,PAY.CODES

          * Check if it is a split term
          GOSUB CHECK.SPLIT

          PCT = DCOUNT(PAY.IDS,VM)
          FOR PYN = 1 TO PCT
             PAY.ID   = PAY.IDS<1,PYN>
             PAY.DT   = PAY.DTS<1,PYN>
             PAY.AMT  = PAY.AMTS<1,PYN>

             IF IS.SPLIT AND PYN <= DCOUNT(SPLIT.ORDERS,VM) THEN CONTINUE

             PAY.CODE = FIELD(PAY.CODES<1,PYN>,'~',1)
             PAY.MORE = FIELD(PAY.CODES<1,PYN>,'~',2)

             IF US.FUNDS THEN
                PAY.AMT = ICONV(PAY.AMT/OCONV(US.FUNDS,'MR4'),"MR0")
             END

             BEGIN CASE
                CASE PAY.CODE = 1
                   PAY.TYPE = 'CASH'
                   PAY.MORE = ''
                CASE PAY.CODE = 2
                   PAY.TYPE = 'CHECK'
                   PMT<PMT.INDX,7> = PAY.MORE
                CASE PAY.CODE = 3
                   * handle cc's later
                   CONTINUE
                CASE OTHERWISE
                   PAY.TYPE = 'OTHER'
                   PAY.MORE = ''
             END CASE

             PMT<PMT.INDX,1> = 'PMT'
             PMT<PMT.INDX,2> = BT.CN
             PAY.AMT         = OCONV(PAY.AMT,'MR2')
             PMT<PMT.INDX,3> = PAY.AMT "R2#19"
             PMT<PMT.INDX,4> = OCONV(PAY.DT,'D2/')
             PMT<PMT.INDX,5> = PAY.TYPE
             PMT<PMT.INDX,6> = PAY.ID

             GOSUB LOAD.UD.VALS

             PMT.INDX       += 1
             RUNNING.DISC   += PAY.AMT
          NEXT PYN

          * add in split payments
          SPLIT.DT  = ''
          SPLIT.AMT = ''
          IF IS.SPLIT THEN
             IF LED(48)<1,GEN> = '' THEN
                READ AR.INFO FROM ARFILE,ORD.ID ELSE RETURN
                IF AR.INFO<18> = 'SPT' THEN
                   SPLIT.DT  = AR.INFO<12>
                   SPLIT.AMT = AR.INFO<8>
                   PMT<PMT.INDX,1> = 'PMT'
                   PMT<PMT.INDX,2> = BT.CN
                   SPLIT.AMT       = OCONV(SPLIT.AMT,'MR2')
                   PMT<PMT.INDX,3> = SPLIT.AMT "R2#19"
                   PMT<PMT.INDX,4> = OCONV(SPLIT.DT,'D2/')
                   PMT<PMT.INDX,5> = 'Amount Due on '

                   GOSUB LOAD.UD.VALS

                   PMT.INDX       += 1
                END
             END ELSE
                SPLIT.ORDERS = ORD.ID:VM:SPLIT.ORDERS
                NUM.ORDERS = DCOUNT(SPLIT.ORDERS,VM)
                FOR CTR = 1 TO NUM.ORDERS
                   AR.KEY = SPLIT.ORDERS<1,CTR>
                   READ AR.INFO FROM ARFILE,AR.KEY ELSE CONTINUE
                   SPLIT.DT  = AR.INFO<12>
                   SPLIT.AMT = AR.INFO<8>
                   PMT<PMT.INDX,1> = 'PMT'
                   PMT<PMT.INDX,2> = BT.CN
                   SPLIT.AMT       = OCONV(SPLIT.AMT,'MR2')
                   PMT<PMT.INDX,3> = SPLIT.AMT "R2#19"
                   PMT<PMT.INDX,4> = OCONV(SPLIT.DT,'D2/')
                   PMT<PMT.INDX,5> = 'Amount Due on '

                   GOSUB LOAD.UD.VALS

                   PMT.INDX       += 1
                NEXT CTR
             END
          END

          * handle cc's differently
          UT.SEC3 9,AUTH.OK,,NO
          CC.AMT = OCONV(OE.TOTL,'MR2')
          IF AUTH.OK THEN
             SOE.FORMS.GET.CC.INFO OID,GEN,CC.AMT,PAYMENT.IDS
             UT.OPEN.FILE "CC.PAYMENTS",CCPFILE,ERR.MSG
             IF ERR.MSG THEN RETURN

             PAYMENT.CT = DCOUNT(PAYMENT.IDS,VM)
             FOR PC = 1 TO PAYMENT.CT
                PAY.ID = PAYMENT.IDS<1,PC>
                READ CC FROM CCPFILE,PAY.ID ELSE CC = ''
                PAY.TYPE = 'CREDIT'
                CCARD.NO = CC<4,1>

                CREDIT.CARD.GET.DEFINE CCARD.NO,TYPE

                BEGIN CASE
                   CASE TYPE    = 'AX'
                      DISP.TYPE = 'AMEX'
                   CASE TYPE    = 'VI'
                      DISP.TYPE = 'VISA'
                   CASE TYPE    = 'DC'
                      DISP.TYPE = 'DINERS'
                   CASE TYPE    = 'DI'
                      DISP.TYPE = 'DISCOVER'
                   CASE OTHERWISE
                      DISP.TYPE = TYPE
                END CASE

                CCARD.NO = STR('X',LEN(CCARD.NO)-4):RIGHT(CCARD.NO,4)

                * Use the amounts calculated above which are relative
                * to the invoice rather than the full payment amount
                * which maybe spilt across different invoices
                LOCATE PAY.ID IN PAY.IDS<1> SETTING VPOS THEN
                   PMT.AMT = OCONV(PAY.AMTS<1,VPOS>,'MR2')
                END ELSE
                   PMT.AMT = OCONV(CC<6>,'MR2') * -1
                END

                PMT<PMT.INDX,1>  = 'PMT'
                PMT<PMT.INDX,2>  = BT.CN
                PMT<PMT.INDX,3>  = PMT.AMT "R2#19"
                PMT<PMT.INDX,4>  = OCONV(CC<2>,'D2/')
                PMT<PMT.INDX,5>  = 'CREDIT'
                PMT<PMT.INDX,6>  = ''
                PMT<PMT.INDX,8>  = CC<16>
                PMT<PMT.INDX,9>  = CCARD.NO
                PMT<PMT.INDX,10> = CC<32>
                PMT<PMT.INDX,11> = PMT.AMT "R2#19"
                PMT<PMT.INDX,12> = TIMEDATE()
                PMT<PMT.INDX,13> = DISP.TYPE
                PMT<PMT.INDX,14> = CC<3>
                PMT<PMT.INDX,15> = OCONV(CC<2>,'D2/')

                GOSUB LOAD.UD.VALS

                RUNNING.DISC    += PMT.AMT
                PMT.INDX        += 1
             NEXT PC
          END

          IF DISC.TAKEN # 0 THEN
             IF US.FUNDS THEN
                DISC.TAKEN = ICONV(DISC.TAKEN/OCONV(US.FUNDS,'MR4'),"MR0")
             END

             PMT<PMT.INDX,1>  = 'PMT'
             PMT<PMT.INDX,2>  = BT.CN
             DISC.TAKEN       = OCONV(DISC.TAKEN,'MR2')
             PMT<PMT.INDX,3>  = DISC.TAKEN "R2#19"
             PMT<PMT.INDX,5>  = 'DISCOUNT'

             GOSUB LOAD.UD.VALS

             RUNNING.DISC    += DISC.TAKEN
             PMT.INDX        += 1
          END

          * If there is a web order discount then display the web order
          * discount
          SOE.CALC.DISC OID,GEN,,,WOE.DISC
          IF WOE.DISC THEN
             PMT<PMT.INDX,1>  = 'PMT'
             PMT<PMT.INDX,2>  = BT.CN
             WOE.DISC         = OCONV(WOE.DISC,'MR2')
             PMT<PMT.INDX,3>  = WOE.DISC "R2#19"
             PMT<PMT.INDX,5>  = 'WEB ORDER DISCOUNT'

             GOSUB LOAD.UD.VALS

             RUNNING.DISC    += WOE.DISC
             PMT.INDX        += 1
          END

          * Discount amount on a credit invoice.
          IF DISC.AMT > 0 AND RUNNING.TOTAL < 0 THEN
             DISC.AMT = OCONV(DISC.AMT,'MR2')
             IF US.FUNDS THEN
                DISC.AMT = DISC.AMT/OCONV(US.FUNDS,'MR4') "MR2"
             END
             PMT<PMT.INDX,1> = 'PMT'
             PMT<PMT.INDX,2> = BT.CN
             PMT<PMT.INDX,3> = DISC.AMT "R2#19"
             PMT<PMT.INDX,5> = 'Discount Given'

             GOSUB LOAD.UD.VALS

             RUNNING.DISC    += DISC.AMT
             PMT.INDX        += 1
          END

          * Take the discounts/payments out of the subtotal.
          RUNNING.TOTAL += ICONV(RUNNING.DISC,  'MR2')

          BT.PAYMENT = PMT

          RETURN
*-------------------------------------------------------------------------*
SET.LINE.DESCRIPTION:

*          DESC.CT    = DCOUNT(DESC,VM)
*          DESC.ARRAY = ''
*
*          FOR DLN = 1 TO DESC.CT
*             DESC.ARRAY<DLN,1> = 'DSC'
*             DESC.ARRAY<DLN,2> = BT.CN
*             DESC.ARRAY<DLN,3> = DESC<1,DLN>
*
*             LN.TYP            = 'DSC'
*             GOSUB LOAD.UD.VALS
*          NEXT DLN
*
*          BT.LINE.DESC<BT.LD.NO> = LOWER(DESC.ARRAY)

          RETURN
*-------------------------------------------------------------------------*
SET.KIT.DESCRIPTION:


          RETURN
*-------------------------------------------------------------------------*
GET.TOTALS:

          SOE.CALC.CASH OID,GEN,AMT.DUE,AMT.PAID,CASH.DISC

          * Get WOE.DISC for Web orders
          SOE.CALC.DISC OID,GEN,,,WOE.DISC

          OE.ORDER.TOTAL OID,GEN,QSIGN,OE.TOTL,,FREIGHT,HANDLING,TAX.AMT,FET.AMT

          FREIGHT   = OCONV(FREIGHT,'MR2')
          HANDLING  = OCONV(HANDLING,'MR2')
          TAX.AMT   = OCONV(TAX.AMT,'MR2')
          FET.AMT   = OCONV(FET.AMT,'MR2')
          CASH.DISC = OCONV(CASH.DISC,'MR2')

          LDID.GENS = GEN
          IF SNGL.INV = 1 THEN
             GN.CT = DCOUNT(LED(12),VM)
             FOR J = 1 TO GN.CT
                ST = LED(6)<1,J>
                IF J#GEN AND ST#'X' AND ST#'B' AND ST#'$' AND ST#'Y' THEN
                   LDID.GENS<-1> = J
                   OE.ORDER.TOTAL OID,J,QSIGN,,,FRT.TOL,HAND.TOL,TAX.TOL,FET.TOL
                   TAX.AMT  += OCONV(TAX.TOL,'MR2')
                   FREIGHT  += OCONV(FRT.TOL,'MR2')
                   HANDLING += OCONV(HAND.TOL,'MR2')
                END
             NEXT J
          END

          * Find service Charges, put in OE.ORDER.TOTAL when possible
          LOCATE 'SC' IN LED(24)<1,GEN> SETTING POS THEN
             SERV.CHRG = -OCONV(LED(26)<1,GEN,POS>,'MR2')
          END ELSE
             SERV.CHRG = 0
          END

          IF SNGL.INV = 1 THEN
             UNEARN.AMT = 0
          END ELSE
             LOCATE 'UNEARN' IN LED(24)<1,GEN> SETTING POS THEN
                UNEARN.AMT = OCONV(LED(26)<1,GEN,POS>,'MR2')
             END ELSE
                UNEARN.AMT = 0
             END
          END

          * Never want Amounts to
          IF SNGL.INV = 2 THEN
             UNEARN.AMT = 0
             TAX.AMT    = 0
             FREIGHT    = 0
             HANDLING   = 0
          END

          RETURN
*-------------------------------------------------------------------------*
LOAD.UD.VALS: *** Load the user defined values based on the dictionary
              *** information set up for the document and line type we are
              *** working with.
          LOCATE LN.TYP IN UD.LN.TYPS SETTING UD.POS ELSE RETURN
          UDREC = RAISE(UD.INFO<UD.POS>)

          IF NOT(UDREC<1>) THEN RETURN

          ITM.CNT = 0
          UD.CNT  = DCOUNT(UDREC<1>,VM)
          FOR UD.CTR = 1 TO UD.CNT
             IF NOT(UDREC<1,UD.CTR>) THEN CONTINUE

             ITM.CNT  += 1
             BR.TYPE   = UDREC<4,UD.CTR>
             DICT.KEY  = UDREC<1,UD.CTR>:'~':UDREC<2,UD.CTR>:'~'
             DICT.KEY := UDREC<3,UD.CTR>
             BATCH.GET.DICT.VALUE UD.VAL,OID,GEN,LDID,'',BR.TYPE,DICT.KEY

             BEGIN CASE
             CASE LN.TYP = 'HDR'; BT.HEADER<76+ITM.CNT>          = UD.VAL
             CASE LN.TYP = 'ITM'; BT.LINE.ITEM<BT.LD.NO,16+ITM.CNT> = UD.VAL
     *        CASE LN.TYP = 'CMT'; BT.LINE.ITEM<BT.LD.NO,3+ITM.CNT>  = UD.VAL
     *        CASE LN.TYP = 'SUB'; BT.LINE.ITEM<BT.LD.NO,4+ITM.CNT>  = UD.VAL
     *        CASE LN.TYP = 'DSC'; DESC.ARRAY<DLN,3+ITM.CNT>      = UD.VAL
     *        CASE LN.TYP = 'PMT'; PMT<PMT.INDX,15+ITM.CNT>       = UD.VAL
     *        CASE LN.TYP = 'FTR'; BT.FOOTER<22+ITM.CNT>          = UD.VAL
             END CASE
          NEXT UD.CTR

          RETURN
*-------------------------------------------------------------------------*
SET.FILE:
          BODY.FINAL = ''

          * Header - add in the commas and "" for the csv file
          UT.REP.STR BT.HEADER,CHAR(9)
          UT.REP.STR BT.HEADER,AM,CHAR(9)
          BT.HEADER = '':BT.HEADER:''
          BODY.FINAL<-1> = BT.HEADER

          * Combine line items with their descriptions
          LINE.ITEM.CT = DCOUNT(BT.LINE.ITEM,AM)
          FOR LIX = 1 TO LINE.ITEM.CT
             LINE.ITEM      = BT.LINE.ITEM<LIX>
             UT.REP.STR LINE.ITEM,'' ''
             UT.REP.STR LINE.ITEM,VM,CHAR(9)
             LINE.ITEM      = '':LINE.ITEM:''
             BODY.FINAL<-1> = LINE.ITEM
             LINE.DESC      = RAISE(BT.LINE.DESC<LIX>)
             LINE.DESC.CT   = DCOUNT(LINE.DESC,AM)
             FOR LDX = 1 TO LINE.DESC.CT
                LINE.ITEM      = LINE.DESC<LDX>
                UT.REP.STR LINE.ITEM,'' ''
                UT.REP.STR LINE.ITEM,VM,' '
                LINE.ITEM      = '':LINE.ITEM:''
                BODY.FINAL<-1> = LINE.ITEM
             NEXT LDX
          NEXT LIX

          * Sort payment info by date
          TMP.PMT = ''
          TMP.DTS = ''
          PMT.CT = DCOUNT(BT.PAYMENT,AM)
          FOR PX = 1 TO PMT.CT
             PMT.DATE = ICONV(BT.PAYMENT<PX,4>,'D2/')
             LOCATE PMT.DATE IN TMP.DTS BY 'AL' SETTING POS ELSE NULL
             TMP.PMT = INSERT(TMP.PMT,POS;BT.PAYMENT<PX>)
             TMP.DTS = INSERT(TMP.DTS,POS;PMT.DATE)
          NEXT PX
          BT.PAYMENT = TMP.PMT

          * Add in payment information
          PMT.CT = DCOUNT(BT.PAYMENT,AM)
          FOR PX = 1 TO PMT.CT
             PMT = BT.PAYMENT<PX>
             UT.REP.STR PMT,'' ''
             UT.REP.STR PMT,VM,' '
             PMT = '':PMT:''
             BODY.FINAL<-1> = PMT
          NEXT PX

          * Footer - add in the commas and "" for the csv file
          UT.REP.STR BT.FOOTER,'' ''
          UT.REP.STR BT.FOOTER,AM,' '
          BT.FOOTER = '':BT.FOOTER:''
          BODY.FINAL<-1> = BT.FOOTER

          READ LINES FROM FTP.FILE,CSV.NAME ELSE LINES = ''
          NEXT.ID = DCOUNT(LINES,AM)+0
          NWRITEV BODY.FINAL ON FTP.FILE,CSV.NAME,NEXT.ID

          RETURN
*-------------------------------------------------------------------------*
CHECK.SPLIT: * This routine will go through the AR record for each gen on
             * on an order loop through the cash receipt record back to
             * the sales order to check for the 'SPT' flag in the ledger
          IS.SPLIT     = NO
          SPLIT.ORDERS = ''

          READV AR.TWO FROM ARFILE,ORD.ID,2 ELSE RETURN

          CT = DCOUNT(AR.TWO,VM)
          FOR I = 1 TO CT
             *** check the ar record for this gen on the OID.
             IF AR.TWO<1,I> = '' THEN CONTINUE

             *** setup the second AR ID to read from the arfile.
             AR.TWO.CR     = FIELD(AR.TWO<1,I>,'.',2,3)
             READV AR.THIRD FROM ARFILE,AR.TWO.CR,2 ELSE RETURN

             S.CT = DCOUNT(AR.THIRD,VM)
             FOR J = 1 TO S.CT
                *** check the ar record for the CR of this OID GEN.
                IF AR.THIRD<1,J> = '' THEN CONTINUE
                AR.KEY = FIELD(AR.THIRD<1,J>,'.',2,3)
                IF FIELD(AR.KEY,'.',1,1) # OID THEN CONTINUE

                READ AR.INFO FROM ARFILE,AR.KEY ELSE CONTINUE
                IF AR.INFO<18> = 'SPT' THEN
                   IS.SPLIT = YES
                   LOCATE AR.KEY IN SPLIT.ORDERS<1> SETTING POS ELSE
                      SPLIT.ORDERS<1,-1> = AR.KEY
                   END
                END
             NEXT J
          NEXT I

          RETURN
*-------------------------------------------------------------------------*
!SMITJR~09/25/11~23:15
