          SUBROUTINE (AGT.MERGE,DTIDS.MERGE,AINFO.MERGE,AMTS.MERGE,CN,AOD,AGD,BRS,GLAS,SCONLY,LME,XCUR,AGT.PTRS,EXCL.STATS)
** Version# 9.0002[1] - 06/05/2014 - 12:25pm - TSMITH - eclipse
** Copied from BP ASUB.DATA.GET.PARENT Version# 9 - 08/21/2012 - 12:54pm - EILEENM - main

*** Subroutine: ASUB.DATA.GET.PARENT
*-------------------------------------------------------------------------*
*** This routine will be a wrapper for asub.data.get. It will find all
*** customers assoicated with the parent customer and call asub.data.get
*** for each customer and merge all of the results.
***
*** ASUB.DATA.GET comments - It will go out to the A/R file for the
*** customer you supply (CN) and the asof date (AOD) you send in and pull
*** back all open ar/ap items. It will also pull back all of the "aging"
*** information for this customer/vendor in summary format within the AGT
*** array (broken down by attb as to what age bucket it falls into). This
*** routine can be used for a/r and a/p information so you should set GLAS
*** appropriately for the information you want back.
***
*** If the Customer Number passed in is Attribute Mark delimited, the
*** calling routine is only trying to get Open AR data for the ShipTo
*** Customer in CN<2>. Another words, if the ShipTo Customer on the AR
*** item does not match the ShipTo Customer passed in it will NOT be
*** included in the data returned.
*-------------------------------------------------------------------------*
*** AGT.MERGE   - Aging summary information (attb 1 = bucket 1 etc.)  (OUT)
*** DTIDS.MERGE - AR IDs (with dt. in front) that are open to this CN (OUT)
*** AINFO.MERGE - Add'l information (shipdate, status,St) about this
***               ID                                                  (OUT)
*** AMTS.MERGE  - Open a/r or a/p amount for this "invoice"           (OUT)
*** CN          - Customer/Vendor (ST or SF) you want information for  (IN)
***              > When this value is Attribute Mark delimited, only those
***              Open AR items whose ShipTo MATCHES the Customer Number
***              passed in Attribute 2 (CN<2>) will be included.
*** AOD         - Asof date you want the information for ('' = current)(IN)
*** AGD         - Aging date to use when calculating AGT information   (IN)
*** BRS         - Branches you want information for ('' = ALL)         (IN)
*** GLAS        - A/R or A/P items (1 = A/R, 2 = A/P , 3 = UBAP)       (IN)
*** SCONLY      - AM separated: 1 - Service Charges 2 - Exclude Credits(IN)
***              1 = You only want service changes passed back
***              1 = You want to exclude credits from Aging buckets
*** LME         - Last Month End,Cutoff dt for records to return
***                (''=ALL)                                            (IN)
*** XCUR        - Currency you want this to be displayed in ('' = base)(IN)
*** AGT.PTRS    - Y/N to add aging pointers to AINFO<1,5>              (IN)
*** EXCL.STATS  - Exclude invoice status                               (IN)

*** NOTE : CN & GLAS are the only req'd variables to pass in. If any other
*** "IN" params are left null it will default to ALL or CURRENT
*** information. To speed this routine up you should always pass in a null
*** ('') AOD if you want the current information so it can use ENTDFILE
*** rather than looping through the AR file.
*-------------------------------------------------------------------------*
*** COMMON VARIABLES :
*** CACHE$CN and CACHE$ST are set to '' in this routine.
*-------------------------------------------------------------------------*
          GOSUB INIT
          GOSUB FIND.LIST.OF.CUSTOMERS
          CUST.CT = DCOUNT(LIST.CUSTOMERS,AM)
          IF BILL.TO.ONLY THEN
             SINGLE.CN = CN
             GOSUB APPEND.CUST.DATA
          END ELSE
             FOR CUST.I = 1 TO CUST.CT
                SINGLE.CN = LIST.CUSTOMERS<CUST.I>
                GOSUB APPEND.CUST.DATA
             NEXT CUST.I
          END

          * List the credits first if they exist.
          IF DTIDS.CR.MERGE # "" THEN
             DTIDS.MERGE = DTIDS.CR.MERGE:AM:DTIDS.MERGE
             AINFO.MERGE = AINFO.CR.MERGE:AM:AINFO.MERGE
             AMTS.MERGE  = AMTS.CR.MERGE:AM:AMTS.MERGE
          END

          * Trim off extra AM
          DTIDS.MERGE = TRIM(DTIDS.MERGE,AM)
          AINFO.MERGE = TRIM(AINFO.MERGE,AM)
          AMTS.MERGE  = TRIM(AMTS.MERGE,AM)

          RETURN
*-------------------------------------------------------------------------*
INIT:     *** Initialize all variables used in this routine
          AGT.MERGE   = ''
          DTIDS.MERGE = ''
          AINFO.MERGE = ''
          AMTS.MERGE  = ''

          DTIDS.CR.MERGE = ''
          AINFO.CR.MERGE = ''
          AMTS.CR.MERGE  = ''

          SINGLE.CN   = ''
          LIST.CUSTOMERS = ''
          BILL.TO.ONLY = ''
          RETURN
*-------------------------------------------------------------------------*
FIND.LIST.OF.CUSTOMERS:  *** Determine if this customer is a parent.  If it
          *** is, then find all corresponding "children".  If not, then
          *** simply add this single customer to the cust.list so the call
          *** to append.cust.data will call asub.data.get as it did before

          * If cn comes in as attribute marked, then they want bill to
          * only and we should only do this once.
          CN.COUNT = DCOUNT(CN,AM)
          IF CN.COUNT > 1 THEN
             BILL.TO.ONLY = YES
          END ELSE
             BILL.TO.ONLY = NO
             * The parent's list of children is stored in cus<80,2>
             READV PARENT.INFO FROM CUSFILE,CN,80 ELSE PARENT.INFO = ""
             CHILD.CT = DCOUNT(PARENT.INFO<1,2>,SVM)
             IF CHILD.CT <= 0 THEN
                LIST.CUSTOMERS = CN
             END ELSE
                LIST.CUSTOMERS = CN
                LIST.CUSTOMERS := AM : TRIM(RAISE(RAISE(PARENT.INFO<1,2>)))
             END
          END
          RETURN
*-------------------------------------------------------------------------*
APPEND.CUST.DATA:  *** Calls asub.data.get for current customer and appends
          *** the result to each of the out variables.  If only a single
          *** customer exists in this parent/cust relation, then this will
          *** simply call asub.data.get once and return as it used to.
          AGT   = ''
          DTIDS = ''
          AINFO = ''
          AMTS  = ''

          ASUB.DATA.GET.GPS AGT,DTIDS,AINFO,AMTS,SINGLE.CN,AOD,AGD,BRS,GLAS,SCONLY,LME,XCUR,AGT.PTRS,EXCL.STATS

          AGT.MERGE= ADDS(AGT,AGT.MERGE)
          * Order this data correcty - instead of just merging all the
          * results.
          DTIDS.CT = DCOUNT(DTIDS,AM)
          FOR DTIDS.I = 1 TO DTIDS.CT
             DTID = DTIDS<DTIDS.I>
             INFO = AINFO<DTIDS.I>
             BAL = AMTS<DTIDS.I>
             * Sort out the credits in a different bucket. This will allow
             * us to list the credits on the top of the list regardless
             * of the date.
             IF BAL < 0 THEN
                LOCATE DTID IN DTIDS.CR.MERGE BY 'AL' SETTING POS ELSE
                   DTIDS.CR.MERGE = INSERT(DTIDS.CR.MERGE,POS;DTID)
                   AINFO.CR.MERGE = INSERT(AINFO.CR.MERGE,POS;INFO)
                   AMTS.CR.MERGE  = INSERT(AMTS.CR.MERGE,POS;BAL)
                END
             END ELSE
                LOCATE DTID IN DTIDS.MERGE BY 'AL' SETTING POS ELSE
                   DTIDS.MERGE = INSERT(DTIDS.MERGE,POS;DTID)
                   AINFO.MERGE = INSERT(AINFO.MERGE,POS;INFO)
                   AMTS.MERGE  = INSERT(AMTS.MERGE,POS;BAL)
                END
             END
          NEXT DTIDS.I

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~06/05/14~12:25
