          SUBROUTINE (CN.ID,SDT,EDT,IN.BRS,SLS,GP,TGT.CURR)
** Version# 16.0001[3] - 09/26/2011 - 04:48pm - SMITJR - eclipse
*** V16.0001 Change - Custom Coding . - 09/26/2011 - SMITJR - eclipse

*** Subroutine: GET.SLS.AMT
*-------------------------------------------------------------------------*
*** This subroutine gets the Salesd Amounts for a specific customer.
*** This program is called by several programs for reporting/inquiry.
*-------------------------------------------------------------------------*
*** CN.ID    -  Customer Number                                 (IN)
*** SDT      -  Start Date                                      (IN)
*** EDT      -  End Date                                        (IN)
*** IN.BRS   -  Branches, '' means use ALL                      (IN)
*** SLS      -  Sales Amount                                    (OUT)
*** GP       -  Sales Amount - Cost of Goods                    (OUT)
*** SLS      - G/L Account you are after.                       (IN)
*** TGT.CURR - Currency the amount will be returned in          (IN)
*-------------------------------------------------------------------------*
*** Common Variables - None Used.
*-------------------------------------------------------------------------*

          CONVERT = NO
          IF TGT.CURR # '' THEN
             READ BASEC FROM CTRLFILE,"BASE.CURRENCY" ELSE BASEC = ''
             IF TGT.CURR # BASEC THEN
                CONVERT = YES
             END ELSE
                CONVERT = NO
             END
          END

          DIM ET(370)
          CN       = FIELD(CN.ID,VM,1)
          ST.ONLY  = FIELD(CN.ID,VM,2)

OLD:      SLS   = ''
          GP    = ''
          CGS   = ''
          BT    = GET.BT.AR(CN)
    *     SGAS  = GET.GAS('SLS')
    *     CGAS  = GET.GAS('COGS')

          SGAS  = 'SLS'
          CGAS  = 'COM'

          WORK  = ''
          IF EDT='' THEN ENDT = DATE() ELSE ENDT = EDT
          GOSUB FAST

          RETURN
*-------------------------------------------------------------------------*
FAST:     SYR = OCONV(SDT,'D4/')[7,4]
          EYR = OCONV(ENDT,'D4/')[7,4]

          FOR YR = SYR TO EYR
             FDYR  = ICONV('01/01/':YR,'D')
             * Get a list of the branches that were active for this
             * Bill-To in the appropriate year.
             READV VBRS FROM ETFILE,BT:'*':YR,1 ELSE VBRS =''
             IF YR = SYR THEN SJ = OCONV(SDT,'DJ')+4  ELSE SJ = 5
             IF YR = EYR THEN EJ = OCONV(ENDT,'DJ')+4 ELSE EJ = 370

             BRS.OK = ''
             *** If no branches passed in then check all branches.
             IF NOT(IN.BRS) THEN
                * No branches were passed in so use ONLY the branches that
                * had activity for the appropriate year to make this
                * run faster.
                BRS = VBRS
             END ELSE
                * Some specific branches were passed in so use just them
                BRS = IN.BRS
             END
             BRS.CT = DCOUNT(BRS,VM)
             FOR B  = 1 TO BRS.CT
                CMASK = ''
                SMASK = ''
                STMASK= ''

                LOCATE BRS<1,B> IN VBRS<1> SETTING POS THEN BRS.OK = YES ELSE CONTINUE
                ET.ID = BRS<1,B>:'~':BT:'*':YR
                MATREAD ET FROM ETFILE,ET.ID ELSE MAT ET=''
                STN = DCOUNT(ET(2),VM)
                ANY = NO
                FOR STS = 1 TO STN
                   AST=ET(2)<1,STS>
                   IF CN = BT AND ST.ONLY AND AST#CN THEN OK = NO ELSE
                      OK = AR.CHK.ST.OK(BT,CN,AST,WORK)
                   END
                   IF OK THEN STMASK<1,1,STS> = YES; ANY=YES
                NEXT STS

                IF NOT(ANY) THEN CONTINUE
                NN = 0
                ANY= NO

                *** Need to add branch check here so it isn't stepped on
                *** by the ship-to check.
                IF BRS.OK THEN OK = YES

                LOOP
                   REMOVE GLID FROM ET(1) SETTING DELIM
                   NN += 1
                   IF OK THEN
                      FIND GLID IN SGAS SETTING DUMMY THEN
                         SMASK<1,NN>=STMASK
                         ANY=YES
                      END
                      FIND GLID IN CGAS SETTING DUMMY THEN
                         CMASK<1,NN>=STMASK
                         ANY=YES
                      END
                   END
                   IF DELIM<2 THEN EXIT
                REPEAT

                IF NOT(ANY) THEN CONTINUE

                FOR A = SJ TO EJ
                   IF CONVERT THEN
                      XDATE  = FDYR + (A-5)
                      XCURR.RATE.GET XRATE,TGT.CURR,XDATE
                      XRATE  = OCONV(XRATE,'MR4')
                      IF XRATE+0=0 THEN XRATE=1
                      MULT   = 1/XRATE
                      TSMASK = SMASK
                      TCMASK = CMASK
                      TSMASK = EREPLACE(TSMASK,'1',MULT,0)
                      TCMASK = EREPLACE(TCMASK,'1',MULT,0)
                      SLS   -= SUMMATION(FMTS(MULS(ET(A),TSMASK),'R0'))
                      CGS   -= SUMMATION(FMTS(MULS(ET(A),TCMASK),'R0'))
                   END ELSE
                      SLS   -= SUMMATION(MULS(ET(A),SMASK))
                      CGS   -= SUMMATION(MULS(ET(A),CMASK))
                   END
                NEXT A
             NEXT B
          NEXT YR

          GP = SLS - CGS
          RETURN
*-------------------------------------------------------------------------*
!SMITJR~09/26/11~16:48
