          PH.ARGS DRPT,BRCHS,BR,SD,R.SLSP,SORTBY,FISCAL.DT,SBRCHS,SBR,COST.TYPE,USE.BLBR,LEVEL,SLSIN.OUT,TPQ,SUPPR,ADDL.DATA
** Version# 43 - 06/05/2007 - 10:04am - NKURTH - main

*** Program: SLS.PHR.CUST.COMPR
*-------------------------------------------------------------------------*
*** Customer Sales Comparison Report.  This report lists the inside or
*** outside salesperson, customer name, and month-to-date, last year's
*** month-to-date, year-to-date and last year's year-to-date figures for
*** sales, GP$ and GP%.  It is based on a customer level.
*-------------------------------------------------------------------------*
*** Parameters:
***       BRCHS       -  Customer Home Branches                     (IN)
***       SD          -  As of Date                                 (IN)
***       R.SLSP      -  Salesperson                                (IN)
***       SORTBY      -  Sort by --                                 (IN)
***        Salesperson,Customer,Salesperson/SelCode,Salesperson Summary,
***        MTD Sales,YTD Sales,LYTD Sales,MTD-GP$,YTD-GP$,LYTD-GP$,
***        MTD-GP%,YTD-GP%,LYTD-GP%,Salesperson-MTD Sales,
***        Salesperson-YTD Sales,Salesperson-LYTD Sales,
***        Salesperson-MTD-GP$,Salesperson-YTD-GP$,Salesperson-LYTD-GP$,
***        Salesperson-YTD-GP%,Salesperson-LYTD-GP%
***       FISCAL.DT   -  Calendar or Fiscal Date?                   (IN)
***       SBRCHS      -  Sales Branches                             (IN)
***       COST.TYPE   -  Cost, COGS or None                         (IN)
***       USE.BLBR    -  Include blank Home branch                  (IN)
***       LEVEL       -  Customer Level (Bill-to, ship to, parent)  (IN)
***       SLSIN.OUT   -  Inside or Outside salesperson              (IN)
***       TPQ         -  Show this number of top customers          (IN)
***       SUPPR       -  Suppress accounts with no activity         (IN)
***       ADDL.DATA   -                                             [IN]
***                      XCURR - Report Currency
***                      SHOW.MISC - Inc/Exc/Only Misc Charges Option
*-------------------------------------------------------------------------*
          * Break out additional data
          XCURR     = ADDL.DATA<1,1> ;* Report Currency
          SHOW.MISC = ADDL.DATA<1,2> ;* Misc Charge option

          SLSP      = ''
*** Set flag if alternate currency requested
          CURR.FLG = NO
          TGT = ''
          IF XCURR THEN
             READ BASEC FROM CTRLFILE,"BASE.CURRENCY" ELSE BASEC = ''
             IF XCURR # BASEC THEN
                TGT = XCURR
                CURR.FLG = YES
                BASE.CUR = NO
             END ELSE
                BASE.CUR = YES
             END
          END

          IF LEVEL[1,1] = 'S' THEN ST.ONLY = YES ELSE ST.ONLY = NO

          SUMMARY = NO
          SDT = OCONV(SD,'D4/')

          SBRS = SBR

          BRS = BR

          IF OCONV(SORTBY,'MCU') = 'SALESPERSON SUMMARY' THEN SUMMARY = YES
          IF OCONV(R.SLSP,'MCU') = 'ALL' THEN R.SLSP = ''

          S.LEN = LEN(SORTBY)
          IF SORTBY[1,12] = 'Salesperson-' THEN SLSFLG=YES ELSE
             IF SORTBY[1,S.LEN] = 'Salesperson' THEN
                SLSFLG = YES
             END ELSE
                SLSFLG = NO
             END
          END
          IF INDEX(SORTBY,"TD",1) # 0 THEN TOTFLG = YES ELSE TOTFLG = NO
          IF SORTBY[LEN(SORTBY),1] = '%' THEN PERC = YES ELSE PERC = NO
*-------------------------------------------------------------------------*
          NUMPRT  = 0
          PRTIT   = YES
          AOD = SD
          WK.DT   = OCONV(AOD,'D4/')
          MODY    = WK.DT[1,6]
          YR      = WK.DT[7,4]
          YR      = YR - 1
          IF MODY = '02/28/' THEN MODY = '02/29/'
          LY.AOD  = ICONV(MODY:YR,'D')
          IF LY.AOD = '' THEN LY.AOD = ICONV('02/28/':YR,'D')

          IF FISCAL.DT[1,1]='F' THEN
             MBEG.DT  = REALDATE("MO/FB/YR",AOD)
             YBEG.DT  = REALDATE("FB/FB/YR",AOD)
             LMBEG.DT = REALDATE("MO/FB/YR",LY.AOD)
             LYBEG.DT = REALDATE("FB/FB/YR",LY.AOD)
          END ELSE
             MBEG.DT  = REALDATE("MO/01/YR",AOD)
             YBEG.DT  = REALDATE("01/01/YR",AOD)
             LMBEG.DT = REALDATE("MO/01/YR",LY.AOD)
             LYBEG.DT = REALDATE("01/01/YR",LY.AOD)
          END

          SORT.SLSP = (SORTBY[1,1]='S')
          CUS.SEL   = (FIELD(SORTBY,' ',3)[1,1]="S")
          TOT.AMTS  = ''
*-------------------------------------------------------------------------*
          WRITE 'Selecting...' ON PHSTFILE,PID$
          UT.TEMPFILE.CREATE SORTFLNM,SORTFILE
          GOSUB SEL.IDS
          WRITE 'Spooling...' ON PHSTFILE,PID$
*-------------------------------------------------------------------------*
          HDG = 'Customer Sales Comparison Report as of ':SDT:' - ':FISCAL.DT:SPACE(5):' by ':SORTBY "L#65":'Page :^#####'
          HDG<1,4>  = 'Printed Using : ':LEVEL:' Accounts'
          HDG<1,4> := SPACE(5):'Misc Charges : ':SHOW.MISC "L#10"
          IF TPQ THEN
             HDG<1,4> :=SPACE(10):'Showing Top ':TPQ
             IF SLSFLG THEN
                HDG<1,4> := ' per Salesperson'
             END
          END
          IF CURR.FLG THEN
             HDG<1,4> :=SPACE(10):'Report Currency:  ':TGT
          END
          HDG<1,5> = "                                                                                                Sales  GP$                                                                Sales  GP$  "

          IF SLSIN.OUT = 'Outside' THEN
             HDG<1,5> = "Br... Out Slspn Cust #  Customer Name............ MTD Sales... "
          END
          IF SLSIN.OUT = 'Inside' THEN
             HDG<1,5> = "Br... In Slspn  Cust #  Customer Name............ MTD Sales... "
          END

          IF COST.TYPE # 3 THEN
             HDG<1,5> := "MTD GP$..... GP%... "
          END
          HDG<1,5> := "LMTD Sls.... "
          IF COST.TYPE # 3 THEN
             HDG<1,5> := "LMTD GP$.... GP%... Var%.. "
          END
          HDG<1,5> := "Var%.. YTD Sales... "
          IF COST.TYPE # 3 THEN
             HDG<1,5> := "YTD GP$..... GP%... "
          END
          HDG<1,5> := "LYTD Sls.... "
          IF COST.TYPE # 3 THEN
             HDG<1,5> := "LYTD GP$.... GP%... Var%.. Var%.."
          END

          CTOLS.PRTD = NO
          LAST.SLSP  = '@@'
          LAST.SEL   = '@@'
          STOLS      = ''
          CTOLS      = ''
          GTOLS      = ''
          CCNT       = 0

          WDTH = LEN(HDG<1,5>)
          BRANCH.ALLOW = (WDTH - 16)
          IF LEN(BRS) > BRANCH.ALLOW THEN BRS = BRS[1,BRANCH.ALLOW]:'...'
          IF LEN(SBRS) > BRANCH.ALLOW THEN SBRS = SBRS[1,BRANCH.ALLOW]:'...'
          HDG<1,2> = 'Cust Hm Brs   : ':BRS
          HDG<1,3> = 'Sales Hm Brs  : ':SBRS
          IF DRPT<33> = '' THEN
             TITLE = 'Cust Sales Comparison as of ':SDT:' - ':FISCAL.DT
          END ELSE
             TITLE = DRPT<33>:' as of ':SDT
          END

            PRINTER.ON WDTH,TITLE,DOC.ID,HDG,RPT.DFLT=DRPT

          LOOP
          READNEXT ID ELSE EXIT
          GOSUB ONE.ID
          REPEAT

          IF CUS.SEL   THEN GOSUB PRT.CTOLS
          IF SORT.SLSP THEN GOSUB PRT.STOLS
          GOSUB PRT.GTOLS

          PRINTER.OFF DOC.ID
          UT.PH.CLEANUP

          SEND.MESSAGE 'Phantom',USER.ID,TITLE:' is Complete'

          UT.TEMPFILE.DELETE SORTFLNM

          STOP
*-------------------------------------------------------------------------*
ONE.ID:   READ AMTS   FROM SORTFILE,ID ELSE AMTS = ''
          CN = FIELD(ID,'~',2)
          GET.CUS.BR ,CN

          IF PRTIT THEN
             NUMPRT += 1
             IF TPQ AND NUMPRT > TPQ THEN PRTIT = NO
          END

          HM.BR      = CUS(53)

          IF SLSIN.OUT = 'Outside' THEN SLSP = CUS(41)
          IF SLSIN.OUT = 'Inside' THEN SLSP = CUS(44)

          SEL.CODE   = CUS(78)
          CTOLS.PRTD = NO
          IF CUS.SEL THEN
             IF LAST.SEL#SEL.CODE THEN GOSUB PRT.CTOLS
          END
          IF SORT.SLSP THEN
             IF LAST.SLSP#SLSP THEN GOSUB PRT.STOLS
          END

          IF NOT(PRTIT) THEN RETURN

          STOLS = ADDS(STOLS,AMTS)
          CTOLS = ADDS(CTOLS,AMTS)
          GTOLS = ADDS(GTOLS,AMTS)

          IF NOT(SUMMARY) THEN
             IF HM.BR = 0 THEN
                PRINT ''                         "R#5":' ':
             END ELSE
                PRINT HM.BR                      "R#5":' ':
             END
             PRINT SLSP                          "L#9":' ':
             PRNT.CN = "&":CN
             PRINT PRNT.CN                       "L#7":' ':
             PRINT CUS(1)                        "L#25":
             TOLS = AMTS
             GOSUB PRINT.TOLS
          END
          RETURN
*-------------------------------------------------------------------------*
PRT.STOLS:  *
          IF LAST.SLSP#'@@' THEN
             IF NOT(CTOLS.PRTD) AND NOT(SUMMARY) AND NOT(TOTFLG) THEN GOSUB PRT.CTOLS
             GOSUB PRT.LINES
             SNAME = TRIM(OCONV(LAST.SLSP,'TINITIALS;X;3;3'))
             IF SNAME = '' THEN SNAME = LAST.SLSP
             PRINT 'Totals for Salesperson : ':SNAME"L#21":' --':
             TOLS = STOLS
             GOSUB PRINT.TOLS
             IF NOT(SUMMARY) THEN PRINT CHAR(12)
             STOLS     = ''
          END
          NUMPRT = 1
          IF TPQ AND NUMPRT > TPQ THEN PRTIT = NO ELSE PRTIT = YES
          LAST.SLSP = SLSP

          RETURN
*-------------------------------------------------------------------------*
PRT.CTOLS:  *
          IF LAST.SEL#'@@' THEN
             GOSUB PRT.LINES
             PRINT 'Totals for Sel Code : ':LAST.SEL"L#22":'   --':
             TOLS = CTOLS
             GOSUB PRINT.TOLS
             PRINT
          END

          LAST.SEL   = SEL.CODE
          CTOLS.PRTD = YES
          CTOLS      = ''

          RETURN
*-------------------------------------------------------------------------*
PRT.GTOLS: *
          IF SLSP='' THEN PRINT CHAR(12)
          GOSUB PRT.LINES
          PRINT SPACE(25):'Grand Totals ---------  ':
          TOLS = GTOLS
          GOSUB PRINT.TOLS
          RETURN
*-------------------------------------------------------------------------*
PRT.LINES:PRINT SPACE(50):;*CHANGED FROM SPACE(39)
          GOSUB PRT.10.10.6.LINE
          GOSUB PRT.10.10.6.LINE
          PRINT '------ ':
          IF COST.TYPE # 3 THEN
             PRINT '------ ':
          END
          GOSUB PRT.10.10.6.LINE
          GOSUB PRT.10.10.6.LINE
          PRINT '------ ':
          IF COST.TYPE # 3 THEN
             PRINT '------ ':
          END
          PRINT
          RETURN
*-------------------------------------------------------------------------*
PRT.10.10.6.LINE:*
          IF COST.TYPE = 3 THEN
             PRINT '------------ ':
          END ELSE
             PRINT '------------ ------------ ------ ':
          END

          RETURN
*-------------------------------------------------------------------------*
PRINT.TOLS: *

          IF TOLS<1> + 0 = 0 THEN
             MTD.GP%   = 0
          END ELSE
             MTD.GP%   = TOLS<2> / TOLS<1> * 100
          END

          IF TOLS<5> + 0 = 0 THEN
             IF TOLS<1> + 0 = 0 THEN MSLS.VAR% = 0 ELSE MSLS.VAR% = 100
          END ELSE
             MSLS.VAR% = (TOLS<1> - TOLS<5>) / TOLS<5> * 100
          END

          IF TOLS<3> + 0 = 0 THEN
             YTD.GP% = 0
          END ELSE
             YTD.GP% = TOLS<4> / TOLS<3> * 100
          END

          IF TOLS<7> + 0 = 0 THEN
             IF TOLS<3> + 0 = 0 THEN YSLS.VAR% = 0 ELSE YSLS.VAR% = 100
          END ELSE
             YSLS.VAR% = (TOLS<3> - TOLS<7>) / TOLS<7> * 100
          END

          IF TOLS<5> + 0 = 0 THEN
             LMTD.GP% = 0
          END ELSE
             LMTD.GP% = TOLS<6> / TOLS<5> * 100
          END

          IF TOLS<7> + 0 = 0 THEN
             LYTD.GP% = 0
          END ELSE
             LYTD.GP% = TOLS<8> / TOLS<7> * 100
          END

          IF TOLS<6> + 0 = 0 THEN
             IF TOLS<2> + 0 = 0 THEN MGP.VAR% = 0 ELSE MGP.VAR% = 100
          END ELSE
             MGP.VAR% = (TOLS<2> - TOLS<6>) / TOLS<6> * 100
          END

          IF TOLS<8> + 0 = 0 THEN
             IF TOLS<4> + 0 = 0 THEN YGP.VAR% = 0 ELSE YGP.VAR% = 100
          END ELSE
             YGP.VAR% = (TOLS<4> - TOLS<8>) / TOLS<8> * 100
          END

          IF COST.TYPE = 3 THEN
             PRINT OCONV(TOLS<1>,'MR2')       "R2#12":' ':
             PRINT OCONV(TOLS<5>,'MR2')       "R2#12":' ':
             PRINT MSLS.VAR%                  "R1#6":'%':
             PRINT OCONV(TOLS<3>,'MR2')       "R2#12":' ':
             PRINT OCONV(TOLS<7>,'MR2')       "R2#12":' ':
             PRINT YSLS.VAR%                  "R1#6":'%'
          END ELSE
             PRINT OCONV(TOLS<1>,'MR2')       "R2#12":' ':
             PRINT OCONV(TOLS<2>,'MR2')       "R2#12":' ':
             PRINT MTD.GP%                    "R1#6":'%':
             PRINT OCONV(TOLS<5>,'MR2')       "R2#12":' ':
             PRINT OCONV(TOLS<6>,'MR2')       "R2#12":' ':
             PRINT LMTD.GP%                   "R1#6":'%':
             PRINT MSLS.VAR%                  "R1#6":'%':
             PRINT MGP.VAR%                   "R1#6":'%':
             PRINT OCONV(TOLS<3>,'MR2')       "R2#12":' ':
             PRINT OCONV(TOLS<4>,'MR2')       "R2#12":' ':
             PRINT YTD.GP%                    "R1#6":'%':
             PRINT OCONV(TOLS<7>,'MR2')       "R2#12":' ':
             PRINT OCONV(TOLS<8>,'MR2')       "R2#12":' ':
             PRINT LYTD.GP%                   "R1#6":'%':
             PRINT YSLS.VAR%                  "R1#6":'%':
             PRINT YGP.VAR%                   "R1#6":'%'
          END

          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:  *
          CMD = 'SELECT ENTITY'
          EXECUTE CMD RTNLIST CUST.LIST CAPTURING MSG

          LOOP
             READNEXT CN FROM CUST.LIST ELSE EXIT

             GET.CUS.BR ,CN
*** Filter records based on whether we're running for bill-to or ship-to
             IF NOT(CUS(7)<1,1> OR CUS(7)<1,2>)           THEN GOTO SKIPIT
             IF NOT(CUS(7)<1,1>) AND NOT(ST.ONLY)         THEN GOTO SKIPIT
             IF NOT(CUS(7)<1,2>) AND ST.ONLY              THEN GOTO SKIPIT
*** Check the branch input
             LOCATE CUS(53) IN BRCHS<1> SETTING XX ELSE
                IF CUS(53)#'' OR (CUS(53)='' AND NOT(USE.BLBR)) THEN
                   GOTO SKIPIT
                END
             END

*** If parent level has been selected, accumulate if there is a parent;
*** otherwise, report at bill to level
             BILL.TO = CUS(10)<1,1>
             IF BILL.TO = '' THEN BILL.TO = CN
             READV PID FROM CUSFILE,BILL.TO,80 ELSE PID = ''
             IF LEVEL[1,1] = 'P' AND PID # '' THEN
                READ PREC FROM CUSFILE,PID<1,1> ELSE PREC = ''
                C.REC.ID = PREC<8>:'~':PID<1,1>
                OUT.SLSM = PREC<41,1>
                IN.SLSM  = PREC<44,1>
             END ELSE
                GOSUB USE.DFLT
             END

             IF R.SLSP # '' THEN
                IF SLSIN.OUT = 'Outside' AND OUT.SLSM # R.SLSP THEN
                   GOTO SKIPIT
                END
                IF SLSIN.OUT = 'Inside' AND IN.SLSM # R.SLSP THEN
                   GOTO SKIPIT
                END
             END

             GOSUB SORT.ID

SKIPIT:   REPEAT

          EXECUTE 'SSELECT ':SORTFLNM CAPTURING MSG

          RETURN
*-------------------------------------------------------------------------*
SORT.ID:  *
          CN.ID = CN:VM:ST.ONLY

          GOSUB GET.SALES.AMT ; * Get the sales amount

          IF SUPPR THEN
             END.IT = YES
             IF MTD.SLS OR MTD.GP THEN END.IT=NO
             IF YTD.SLS OR YTD.GP THEN END.IT=NO
             IF LMTD.SLS OR LMTD.GP THEN END.IT=NO
             IF LYTD.SLS OR LYTD.GP THEN END.IT=NO
             IF END.IT THEN RETURN
          END

          IF PERC THEN
             *** We'll need to do an ICONV on our values so that
             *** they'll sort properly...
             IF MTD.SLS + 0 = 0 THEN MTD.GP% = 0 ELSE
                MTD.GP% = ICONV((MTD.GP / MTD.SLS) * 100,"MR1")
             END
             IF LYTD.SLS + 0 = 0 THEN LYTD.GP% = 0 ELSE
                LYTD.GP% = ICONV((LYTD.GP / LYTD.SLS) * 100,"MR1")
             END
             IF YTD.SLS + 0 = 0 THEN YTD.GP% = 0 ELSE
                YTD.GP% = ICONV((YTD.GP / YTD.SLS) * 100,"MR1")
             END
          END

*** Finish building the temporary record ID
          IF NOT(TOTFLG) THEN
             REC.ID = C.REC.ID
             IF SORT.SLSP THEN
                IF SLSIN.OUT = 'Outside' THEN
                   REC.ID = OUT.SLSM "L#10":'!':C.REC.ID
                END
                IF SLSIN.OUT = 'Inside' THEN
                   REC.ID = IN.SLSM "L#10":'!':C.REC.ID
                END
             END
             IF CUS.SEL THEN
                IF SLSIN.OUT = 'Outside' THEN
                   REC.ID = OUT.SLSM "L#10":'!':CUS(78):'!':C.REC.ID
                END
                IF SLSIN.OUT = 'Inside' THEN
                   REC.ID = IN.SLSM "L#10":'!':CUS(78):'!':C.REC.ID
                END
             END
          END ELSE
             GOSUB TOT.REC
          END

          READ AMTS FROM SORTFILE,REC.ID ELSE AMTS = ''
          AMTS<1> += MTD.SLS
          AMTS<2> += MTD.GP
          AMTS<3> += YTD.SLS
          AMTS<4> += YTD.GP
          AMTS<5> += LMTD.SLS
          AMTS<6> += LMTD.GP
          AMTS<7> += LYTD.SLS
          AMTS<8> += LYTD.GP

          WRITE AMTS ON SORTFILE,REC.ID

          RETURN
*-------------------------------------------------------------------------*
TOT.REC:  *
          REC.ID = ''
          IF SLSFLG THEN
             IF SLSIN.OUT = 'Outside' THEN
                REC.ID = OUT.SLSM "L#10":'!'
             END ELSE
                REC.ID = IN.SLSM "L#10":'!'
             END
          END

          BIGN = '500000000000000'

          BEGIN CASE
          CASE INDEX(SORTBY,'MTD Sales',1)
             REC.ID := (BIGN-MTD.SLS)    "R%15"
          CASE INDEX(SORTBY,'LYTD Sales',1)
             REC.ID := (BIGN-LYTD.SLS)   "R%15"
          CASE INDEX(SORTBY,'YTD Sales',1)
             REC.ID := (BIGN-YTD.SLS)    "R%15"
          CASE INDEX(SORTBY,'MTD-GP$',1)
             REC.ID := (BIGN-MTD.GP)     "R%15"
          CASE INDEX(SORTBY,'LYTD-GP$',1)
             REC.ID := (BIGN-LYTD.GP)    "R%15"
          CASE INDEX(SORTBY,'YTD-GP$',1)
             REC.ID := (BIGN-YTD.GP)     "R%15"
          CASE INDEX(SORTBY,'MTD-GP%',1)
             REC.ID := (BIGN-MTD.GP%)    "R%15"
          CASE INDEX(SORTBY,'LYTD-GP%',1)
             REC.ID := (BIGN-LYTD.GP%)   "R%15"
          CASE INDEX(SORTBY,'YTD-GP%',1)
             REC.ID := (BIGN-YTD.GP%)    "R%15"
          END CASE

          REC.ID:= '!':C.REC.ID

          RETURN
*-------------------------------------------------------------------------*
GET.SALES.AMT:*** Get the sales amount

          IF SHOW.MISC = 'Exclude' THEN
             GET.SLS.AMT CN.ID,MBEG.DT,AOD,SBRCHS,MTD.SLS,MTD.GP,TGT
             GET.SLS.AMT CN.ID,YBEG.DT,AOD,SBRCHS,YTD.SLS,YTD.GP,TGT
             GET.SLS.AMT CN.ID,LMBEG.DT,LY.AOD,SBRCHS,LMTD.SLS,LMTD.GP,TGT
             GET.SLS.AMT CN.ID,LYBEG.DT,LY.AOD,SBRCHS,LYTD.SLS,LYTD.GP,TGT

          END ELSE
             * Year Sales
             EDATE = AOD
             SDATE = YBEG.DT
             MDATE = MBEG.DT
             GOSUB GET.RANGE
             MTD.SLS  = MSALES           ;* Month sales
             MTD.GP   = MTD.SLS - MCOSTS ;* Month costs
             YTD.SLS  = SALES            ;* Year Sales
             YTD.GP   = SALES - COSTS    ;* Year Cost

             * Last Year Sales
             EDATE = LY.AOD
             SDATE = LYBEG.DT
             MDATE = LMBEG.DT
             GOSUB GET.RANGE
             LMTD.SLS = MSALES           ;* Last month sales
             LMTD.GP  = MSALES - MCOSTS  ;* Last month costs
             LYTD.SLS = SALES            ;* Last year sales
             LYTD.GP  = SALES - COSTS    ;* Last year costs

          END

          IF COST.TYPE = 1 THEN
             ETRX.AMT.GET MTDC,CN.ID,MBEG.DT,AOD,SBRCHS,'COM',TGT
             ETRX.AMT.GET YTDC,CN.ID,YBEG.DT,AOD,SBRCHS,'COM',TGT
             ETRX.AMT.GET LMTDC,CN.ID,LMBEG.DT,LY.AOD,SBRCHS,'COM',TGT
             ETRX.AMT.GET LYTDC,CN.ID,LYBEG.DT,LY.AOD,SBRCHS,'COM',TGT
             MTD.GP  = MTD.SLS + MTDC
             YTD.GP  = YTD.SLS + YTDC
             LMTD.GP = LMTD.SLS + LMTDC
             LYTD.GP = LYTD.SLS + LYTDC
          END

          IF COST.TYPE = 3 THEN
             MTD.GP = 0
             YTD.GP = 0
             LMTD.GP = 0
             LYTD.GP = 0
          END

          RETURN
*-------------------------------------------------------------------------*
GET.RANGE:
             MSALES = ''
             MCOSTS  = ''
             SALES  = ''
             COSTS  = ''
             QSIGN  = -1

             FOR DT = SDATE TO EDATE
                IDL =  JLI.READ.EMU('1*':DT)
                LLN = DCOUNT(IDL,AM)
                FOR LLX = 1 TO LLN

                   ID     = IDL<LLX>
                   *** Check the customer home branch.
                   AR.READV ID,10,ORDER.CN
                   IF ORDER.CN # CN THEN CONTINUE

                   INVN = FIELD(ID,'.',2)+0
                   OID  = FIELD(ID,'.',1)
                   MATREAD LED FROM LEDFILE,OID ELSE CONTINUE
                   LOCATE INVN IN LED(8)<1> SETTING GEN ELSE CONTINUE
                   IF LED(30)<1,GEN>='CV' THEN CONTINUE

                   *** Check the sales branch.
                   SLS.BR = LED(2)<1,GEN,1>
                   LOCATE SLS.BR IN SBRCHS<1> SETTING XX ELSE CONTINUE

                   *** Check the salesperson.
                   IF SLSP # '' THEN
                      SLS        = ''
                      SLS<1,1>   = LED(34)<1,GEN>
                      SLS<1,2>   = LED(72)<1,GEN>
                      SLS<1,3>   = LED(73)<1,GEN>
                      LOCATE SLSP IN SLS<1> SETTING XS   ELSE CONTINUE
                   END

                   *** Check each product...
                   LDIDS = LED(48)<1,GEN>
                   LD.CT = DCOUNT(LDIDS,SVM)
                   FOR LDN = 1 TO LD.CT
                      LDID = LDIDS<1,1,LDN>
                      LD.GET LDID

                      IF NOT(NUM(LD(1))) THEN CONTINUE

                      PN = LD(1)

                      MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ''
                      LN = PRD(9)

                      * Determine if Misc Charges should be Inc/Only
                      IF SHOW.MISC = 'Only' AND PRD(3) # 3 THEN CONTINUE

                      NUM     = SUMMATION(RAISE(LD(5)<1,GEN>))
                      NUM    += SUMMATION(RAISE(LD(6)<1,GEN>))
                      TSALES  = NUM * LD(8)<1,GEN>

                      SALES  += TSALES

                      BEGIN CASE
                      CASE COST.TYPE = 1;  TCOSTS = NUM*LD(27)<1,GEN>
                      CASE COST.TYPE = 2;  TCOSTS = NUM*LD(10)<1,GEN>
                      CASE OTHERWISE;      TCOSTS = 0
                      END CASE

                      IF DT >= MDATE THEN
                         MSALES += TSALES
                         MCOSTS += TCOSTS
                      END

                      COSTS += TCOSTS

                   NEXT LDN
                NEXT LLX
             NEXT DT
             * Month sales and cost
             MSALES = ICONV(OCONV((MSALES * QSIGN),'MR9'),'MR2')
             MCOSTS = ICONV(OCONV((MCOSTS * QSIGN),'MR9'),'MR2')

             * Year sales and cost
             SALES  = ICONV(OCONV((SALES * QSIGN),'MR9'),'MR2')
             COSTS  = ICONV(OCONV((COSTS * QSIGN),'MR9'),'MR2')

          RETURN
*-------------------------------------------------------------------------*
USE.DFLT: *
*** Use CUS values from the originally read CN (as opposed to parent)
          C.REC.ID = CUS(8):'~':CN
          OUT.SLSM = CUS(41)<1,1>
          IN.SLSM  = CUS(44)<1,1>

          RETURN
*-------------------------------------------------------------------------*
!NKURTH~06/05/07~10:04
