          SUBROUTINE (OID,FROM.BR,YDATA,ERR.MSG)
** Version# 8.0203[1] - 10/18/2011 - 12:51pm - SMITJR - eclipse
** Copied from UBP 820.004.010.O.A-D.MU92 Version# 8.0201[1] - 10/18/2011 - 12:42pm - SMITJR - eclipse
** Copied from UBP 820.004.010.O.A-D Version# 8.02 - 03/29/2007 - 04:12pm - BABS - develop

*** Subroutine: 820.004.010.O.A-D
*-------------------------------------------------------------------------*
*** This subroutine will generate an ANSI X12 820 (Remittance Advice)
*** document in version 4010 to be sent to Affiliated Distributors.
*-------------------------------------------------------------------------*
*** Affiliate Numbers define the distributor sending the check
*** The Affiliate Number is sent in the N1*PR line
*** There are several ways to setup the Affiliate Numbers.
***       1.) By Branch if each branch has a unique Affiliate number
***       2.) By Vendor if the distributor has different Affiliate
***       numbers based on sector (plumbing vs electrical for ex.)
***       3.) By branch via Internal Account #'s.
***       If no Affiliate Number is found, we send the EDI branch ID
***
*** The Supplier Number defines the vendor receiving payment
*** The Supplier Number is sent in the N1*PE line
***       The Supplier Number should be setup in vendor classify AD#
***       If no Supplier Number is found the Pay-To entity number is sent
*-------------------------------------------------------------------------*
*** OID - AR ID of the check we are creating the remittance for    (IN)
*** FROM.BR - Branch that issued the check                         (IN)
*** YDATA - Accumulated data to be put into the document and ANSI
***         X12 formatted document to be sent to vendor.           (IN/OUT)
*** ERR.MSG - Any errors encountered within this subroutine        (OUT)
*-------------------------------------------------------------------------*

          GOSUB INIT.VARS
          IF ERR.MSG THEN RETURN

          GOSUB GET.AD.NUMS
          GOSUB ADD.HEADER
          GOSUB ADD.DETAIL
          YDATA = XDATA

          RETURN
*-------------------------------------------------------------------------*
INIT.VARS: *** Initial reads and initializing variables

          PR.BR    = LED(2)<1,1,1>
          READV PR.BR.ID FROM CTRBFILE,'EDI.BR.IDS~':PR.BR,1 ELSE
             PR.BR.ID = ''
          END
          IF PR.BR.ID = '' THEN
             ERR.MSG = 'Br EDI ID not defined'
             RETURN
          END

          READV PR.ENT    FROM TERRFILE,PR.BR,4 ELSE PR.ENT    = ''
          READ  PR.BR.REC FROM CUSFILE,PR.ENT   ELSE PR.BR.REC = ''

          PR.NAME  = PR.BR.REC<1>
          PR.ADDR1 = PR.BR.REC<2,1>
          PR.ADDR2 = PR.BR.REC<2,2>
          PR.CITY  = PR.BR.REC<3>
          PR.ST    = TRIM(PR.BR.REC<4>)
          PR.ZIP   = CONVERT("-","",PR.BR.REC<5>)

          PT.CN    = LED(1)<1,1,1>
          SF.CN    = LED(5)<1,1,1>

          READV PE.NAME FROM CUSFILE,PT.CN,1  ELSE PE.NAME = ''

          TPAY.AMT = YDATA<2>
          CHK.NO   = YDATA<3>
          CHK.DT   = YDATA<4>
          INV.INFO = RAISE(YDATA<5>)

          SEG      = ''
          XDATA    = ''
          INV.AMT  = 0

          RETURN
*-------------------------------------------------------------------------*
GET.AD.NUMS:  *** Find Affiliate and Supplier numbers

          *** See if a branch (customer) specific Affiliate Number exists
          *** assign this to CAFN (Customer Affiliate Number)
          READ AF.DICT FROM EDICTFILE,'CUST.CLASS~AD.AFFIL#' ELSE AF.DICT = ''
          IF AF.DICT THEN AF.AFFIL = AF.DICT<2,1> ELSE AF.AFFIL = '1'
          UT.OPEN.FILE 'CUST.CLASS',CCLSFILE,ERR.MSG,YES
          IF ERR.MSG THEN RETURN
          READV CAFN FROM CCLSFILE,PR.ENT,AF.AFFIL ELSE CAFN = ''

          *** See if a vendor specific Affiliate number exists
          *** assign this to VAFN (Vendor Affiliate Number)
          *** Vendor classify will also have the Vendor Supplier number
          *** If both numbers are in Vendor Classify, the format is:
          *** SUPPLIER-AFFILIATE
          *** this also assigns SUP.NUM (A-D Supplier Number)
          READ AD.DICT FROM EDICTFILE,'VEND.CLASS~AD#' ELSE AD.DICT = ''
          IF AD.DICT THEN AD.ATT = AD.DICT<2,1> ELSE AD.ATT = '1'
          UT.OPEN.FILE 'VEND.CLASS',VCLSFILE,ERR.MSG,YES
          IF ERR.MSG THEN RETURN

          READV AD.NUM FROM VCLSFILE,PT.CN,AD.ATT ELSE AD.NUM = ''
          VAFN      = FIELD(AD.NUM,'-',2)      ;* Vendor Affiliate #
          SUP.NUM   = FIELD(AD.NUM,'-',1)      ;* A-D Supplier Number

          *** See if an EDI branch specific Affiliate Number exists
          *** assign this to EAFN (EDI branch Affiliate Number)
          ISA.GRP       = 3                   ;* Set flag for ISA info
          UD.ENTRY      = 20                  ;* Get Attribute 20 of ISA1
          INT.ACCT.NUMS = EDI.GET.UD.ENTRY(PT.CN,SF.CN,ISA.GRP,UD.ENTRY)
          INT.ACCT.NUMS = RAISE(INT.ACCT.NUMS)

          INT.ACCT.NUM = ''
          LOCATE PR.BR IN INT.ACCT.NUMS<2> SETTING POS THEN
             INT.ACCT.NUM = INT.ACCT.NUMS<1,POS>
          END

          *** Based on the types of numbers found, assign the Affiliate #
          *** Assign AFF.NUM (Affiliate Number)
          AFF.NUM = ''
          BEGIN CASE
          CASE CAFN
             AFF.NUM = CAFN                   ;* Cust Classify #
          CASE VAFN
             AFF.NUM = VAFN                   ;* Vend Classify #
          CASE INT.ACCT.NUM
             AFF.NUM = INT.ACCT.NUM           ;* EDI Branch #
          END CASE

          RETURN
*-------------------------------------------------------------------------*
ADD.HEADER:  *** Adds all header level segments to the document.

*** BPR - Beginning Segment for Payment Order/Remittance Advice
          CKDATE  = EDI.Y2K.CONV2(CHK.DT)

          SEG.ID  = 'BPR'
          SEG<1>  = 'I'
          SEG<2>  = OCONV(TPAY.AMT,'MR2')
          SEG<3>  = 'D'
          SEG<4>  = 'ACH'
          SEG<16> = CKDATE
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** TRN - Trace
          SEG.ID = 'TRN'
          SEG<1> = '1'
          SEG<2> = CHK.NO
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** CUR - Currency
          READV CURR.TYP FROM LEDFILE,OID,92 ELSE CURR.XCH = ''

          SEG.ID = 'CUR'
          SEG<1> = 'BY'
          BEGIN CASE
             CASE CURR.TYP<1,1,1> = 'CAN$'
                SEG<2> = 'CAD'
             CASE CURR.TYP<1,1,1> = 'PESOS'
                SEG<2> = 'MXP'
             CASE OTHERWISE
                SEG<2> = 'USD'
          END CASE
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** REF - Reference Identification (Check #)
          SEG.ID = 'REF'
          SEG<1> = 'CK'
          SEG<2> = CHK.NO
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** DTM - Date/Time Reference (Creation Date)
          CDATE  = EDI.Y2K.CONV2(DATE())
          SEG.ID = 'DTM'
          SEG<1> = '097'
          SEG<2> = CDATE
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** N1 - Name (Payer)
          SEG.ID = 'N1'
          SEG<1> = 'PR'
          SEG<2> = PR.NAME
          BEGIN CASE
          CASE AFF.NUM
             SEG<3> = 'ZZ'
             SEG<4> = 'MU93'
          CASE OTHERWISE
             SEG<3> = TRIM(FIELD(PR.BR.ID,'~',1),'0','L')
             SEG<4> = 'MU93'
          END CASE
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** N3 - Address Information (Payer)
          SEG.ID = 'N3'
          SEG<1> = PR.ADDR1
          SEG<2> = PR.ADDR2
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** N4 - City, State & Zip (Payer)
          SEG.ID = 'N4'
          SEG<1> = PR.CITY
          SEG<2> = TRIM(PR.ST)
          SEG<3> = PR.ZIP
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** N1 - Name (Payee)
          SEG.ID = 'N1'
          SEG<1> = 'PE'
          SEG<2> = PE.NAME
          IF SUP.NUM THEN
             SEG<3> = 'ZZ'
             SEG<4> = SUP.NUM
          END ELSE
             SEG<3> = 'ZZ'
             SEG<4> = PT.CN
          END
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** ENT - Entity
          SEG.ID = 'ENT'
          SEG<1> = '1'
          EDI.ADD.SEG SEG.ID,SEG,XDATA

          RETURN
*-------------------------------------------------------------------------*
ADD.DETAIL: *** Loop through invoices and add detail segments
          CTR = 0

          LOOP
             CTR += 1
             WRK = INV.INFO<CTR>
             IF WRK = '' THEN EXIT

             INV.NO   = WRK<1,1>
             INV.DT   = WRK<1,2>
             INV.PT   = WRK<1,3>
             INV.BR   = WRK<1,4>
             INV.ST   = WRK<1,5>
             INV.SF   = WRK<1,6>
             INV.AMT  = WRK<1,7>
             PAY.AMT  = WRK<1,8>
             DIS.AMT  = WRK<1,9>
             DCT.AMT  = WRK<1,10>
             DCT.DESC = OCONV(WRK<1,11>,'MCU')
             GOSUB ADD.ONE
          REPEAT

          RETURN
*-------------------------------------------------------------------------*
ADD.ONE: *** Add invoice specific detail segments to document

*** RMR - Remittance Advice AR Open Items Reference
          SEG.ID = 'RMR'
          SEG<1> = 'IV'
          SEG<2> = INV.NO
          SEG<3> = 'PO'
          SEG<4> = OCONV(PAY.AMT,'MR2')
          SEG<5> = OCONV(INV.AMT,'MR2')
          SEG<6> = OCONV(DIS.AMT,'MR2')
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** DTM - Date/Time Reference (Invoice Date)
          SEG.ID = 'DTM'
          IDATE  = EDI.Y2K.CONV2(INV.DT)
          SEG<1> = '003'
          SEG<2> = IDATE
          EDI.ADD.SEG SEG.ID,SEG,XDATA

*** ADX - Adjustments
          IF DCT.AMT+0 # 0 THEN
             ADJ.CD = ''

             FINDSTR 'PRIC' IN DCT.DESC SETTING POS THEN
                ADJ.CD = '01'
             END ELSE
                FINDSTR 'DAMAGE' IN DCT.DESC SETTING POS THEN
                   ADJ.CD = '04'
                END ELSE
                   FINDSTR 'SHORT' IN DCT.DESC SETTING POS THEN
                      ADJ.CD = '06'
                   END ELSE
                      FINDSTR 'TAX' IN DCT.DESC SETTING POS THEN
                         ADJ.CD = '55'
                      END ELSE
                         FINDSTR 'FREIGHT' IN DCT.DESC SETTING POS THEN
                            ADJ.CD = '54'
                         END
                      END
                   END
                END
             END

             IF ADJ.CD = '' THEN
                ADJ.CD = '06'
             END

             SEG.ID = 'ADX'
             SEG<1> = OCONV(-1*DCT.AMT,'MR2')
             SEG<2> = ADJ.CD
             EDI.ADD.SEG SEG.ID,SEG,XDATA

*** NTE - Note/Special Instruction (Adjustment Description)
             IF DCT.DESC # '' THEN
                SEG.ID   = 'NTE'
                CONVERT VM TO ' ' IN DCT.DESC
                DESC     = TRIM(DCT.DESC)
                DESC.LEN = LEN(DCT.DESC)
                FOLD.STRING DCT.DESC,60,WRK,CT
                FOR J = 1 TO CT
                   SEG<1> = 'OTH'
                   SEG<2> = WRK<1,J>
                   EDI.ADD.SEG SEG.ID,SEG,XDATA
                NEXT J
             END
          END

          RETURN
*-------------------------------------------------------------------------*
!SMITJR~10/18/11~12:51
