          PH.ARGS DRPT,BRCHS,BR,AOD,SLSP,SLSM.TYPE,BR.DET,FITEM,CST
** Version# 4.0002[200] - 09/26/2014 - 10:45am - TSMITH - eclipse
*** V4.0002 Change - Custom Coding . - 09/26/2014 - TSMITH - eclipse
** Copied from BP SLS.PHR.SLSM.COMP Version# 4 - 09/29/2009 - 04:49pm - DOUGW - main

*** Subroutine:  SLS.PHR.SLSM.COMP
*-------------------------------------------------------------------------*
*** This is the phantom for the Gross Profit Salesperson Comparison Report
*** to display the GP and sales information YTD and MTD for each slsperson
*-------------------------------------------------------------------------*
*** BRCHS - The VM separated list of branches to run the report on    [IN]
*** BR    - Literal entered at branch prompt                          (IN)
*** AOD   - The as of date to accumulate date from                    [IN]
*** SLSP  - The salesperon selected to run the report for             (IN)
*** SLSM.TYPE - Inside/Outside or Writer of the order                 [IN]
*** BR.DET- Whether to include the branch detail or not               [IN]
*** FITEM - The standard ledger filter selection data for sales orders(IN)
*** CST   - Run report based on COGS or comm cost on the order.       [IN]
*-------------------------------------------------------------------------*

          GOSUB INIT

          WRITE 'Selecting...' ON PHSTFILE,PID$
          SORTFLNM = 'TEMP:':PID$
          EXECUTE 'CREATE-FILE ':SORTFLNM:' 1 1009' CAPTURING MSG
          OPEN SORTFLNM TO SORTFILE ELSE
             SEND.MESSAGE 'Phantom',USER.ID,'Unable to create temporary for file - Report Aborted'
             STOP
          END

          GOSUB SEL.IDS

          WRITE 'Spooling...' ON PHSTFILE,PID$
*-------------------------------------------------------------------------*
          HDG = 'Sales Stack Ranking Report for ':AODT:' - ':COST.DESC "L#21":'Page :^#####'

          BRANCH.ALLOW = 62

          IF LEN(BRS) > BRANCH.ALLOW THEN BRS = BRS[1,BRANCH.ALLOW]:'...'

          HDG<1,2> = "Month Start : ":MDT:"  Fiscal Year Start : ":BDT:" Through : ":AODT
          IF CURR.FLG THEN
             HDG<1,3> = 'Report Currency: ':TGT:' Branches : ':BRS
          END ELSE
             HDG<1,3> = 'Branches : ':BRS
          END

          HDG<1,4> = "Br.. Slsperson MTD-Sales...MTD-Cost... MTD-GP$     YTD-Sales... YTD-Cost.... YTD-GP$      YTD Target % Target   Target     % Target"

          IF DRPT<33>='' THEN
             TITLE = 'Sales Stack Ranking Report for ':AODT
          END ELSE
             TITLE = DRPT<33>:' - ':AODT
          END

          PRINTER.ON 140,TITLE,DOC.ID,HDG,RPT.DFLT=DRPT
          FILTER.PRINT 'S',FITEM

          SSELECT SORTFILE

          LOOP
             READNEXT TID ELSE EXIT
             GOSUB PRT.ONE
          REPEAT

          GOSUB PRT.TOLS
          GOSUB PRT.GTOLS

          PRINTER.OFF DOC.ID,LOCATION
          DELETE PHSTFILE,PID$

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

          CLOSE SORTFILE
          EXECUTE 'DELETE-FILE ':SORTFLNM CAPTURING MSG

          STOP
*-------------------------------------------------------------------------*
PRT.ONE:  *** Print a single line on the report.
          READ DATA FROM SORTFILE,TID ELSE RETURN


          SLSM = TRIM(FIELD(TID,'~',1))

          BR   = TRIM(FIELD(TID,'~',2))
          IF SLSM # LAST.SLSM THEN
             IF LAST.SLSM # "@@" THEN
                GOSUB PRT.TOLS
             END
             LAST.SLSM = SLSM
          END


          MTD.SLS      = DATA<1>
          MTD.COGS     = DATA<2>
          YTD.SLS      = DATA<3>
          YTD.COGS     = DATA<4>
          QTR.TARGET   = DATA<5>
          TARGET       = DATA<6>


          IF BR.DET THEN
             *** Print the branch/salesman info
             PRINT BR                   "R#4 ":
             PRINT SLSM                 "L#10":
             GOSUB PRT.LINE
          END





          *** Increment the total amounts.
          STOLS = ADDS(STOLS,DATA)
          GTOLS = ADDS(GTOLS,DATA)
          RETURN
*-------------------------------------------------------------------------*
PRT.GTOLS: *** Print the overall report totals.
          GOSUB PRT.LINES
          PRINT 'Grand Totals - ':

          MTD.SLS    = GTOLS<1>
          MTD.COGS   = GTOLS<2>
          YTD.SLS    = GTOLS<3>
          YTD.COGS   = GTOLS<4>


          BEGIN CASE
          CASE SLSM.TYPE[1,1] = "W"
          QTR.TARGET = QTR.TARGET * 8
          CASE SLSM.TYPE[1,1] = "O"
          QTR.TARGET = QTR.TARGET * 4
          END CASE



          TARGET     = '5040000'




          GOSUB PRT.LINE
          RETURN
*-------------------------------------------------------------------------*
PRT.LINES: *** Print total lines
          PRINT SPACE(15):
          PRINT STR('-',11):" ":
          PRINT STR('-',11):" ":
          PRINT STR('-',11):" ":
          PRINT STR('-',12):" ":
          PRINT STR('-',12):" ":
          PRINT STR('-',12):" ":
          PRINT STR('-',10):" ":
          PRINT STR('-',10):" ":
          PRINT STR('-',10):" ":
          PRINT STR('-',10):" "
          RETURN
*-------------------------------------------------------------------------*
PRT.TOLS: *** Print the salesman totals
          IF BR.DET THEN GOSUB PRT.LINES
          PRINT SPACE(5):
          PRINT LAST.SLSM         "L#10":

          MTD.SLS    = STOLS<1>
          MTD.COGS   = STOLS<2>
          YTD.SLS    = STOLS<3>
          YTD.COGS   = STOLS<4>
          QTR.TARGET = ''
          BEGIN CASE
          CASE QM='01' OR QM='02' OR QM='03'
          QTR.TARGET     = TRANS('INITIALS',LAST.SLSM,103,'X')/4
          CASE QM='04' OR QM='05' OR QM='06'
          QTR.TARGET     = TRANS('INITIALS',LAST.SLSM,103,'X')/2
          CASE QM='07' OR QM='08' OR QM='09'
          QTR.TARGET     = TRANS('INITIALS',LAST.SLSM,103,'X')*.75
          CASE QM='10' OR QM='11' OR QM='12'
          QTR.TARGET     = TRANS('INITIALS',LAST.SLSM,103,'X')
          END CASE

          IF SLSM.TYPE[1,1] = 'O' THEN
          TARGET     = '1260000'
          END ELSE
          TARGET     = '630000'
          END


          STOLS = ""
          GOSUB PRT.LINE

          IF BR.DET THEN PRINT

          RETURN
*-------------------------------------------------------------------------*
PRT.LINE: *** Print a single line on the report.


          IF MTD.SLS THEN
             MTD.GP = (MTD.SLS - MTD.COGS)
          END ELSE
             MTD.GP = 0
          END
          IF YTD.SLS THEN
             YTD.GP = (YTD.SLS - YTD.COGS)
          END ELSE
             YTD.GP = 0
          END

          PRINT OCONV(MTD.SLS,'MR2,') "R#11 ":
          PRINT OCONV(MTD.COGS,'MR2,')"R#11 ":
          PRINT OCONV(MTD.GP,'MR2,')  "R#11 ":
          PRINT OCONV(YTD.SLS,'MR2,') "R#12 ":
          PRINT OCONV(YTD.COGS,'MR2,')"R#12 ":
          PRINT OCONV(YTD.GP,'MR2,')  "R#12 ":
          PRINT OCONV(QTR.TARGET,'MR,') "R#10 ":
          PRINT OCONV(YTD.GP/QTR.TARGET*10,'MR1') "R#10 ":
          PRINT OCONV(TARGET,'MR,')    "R#10 ":
          PRINT OCONV(YTD.GP/TARGET*10,'MR1')     "R#10 "

          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:  *** Select the ledger records to include in the calculations
          JLI.SELECT ,SD,AOD,BRCHS,1
          LOOP
             READNEXT ID ELSE EXIT
             GOSUB SORT.ID
          REPEAT
          RETURN
*-------------------------------------------------------------------------*
SORT.ID:  *** Add the invoice to the calculated totals, if it matches the
          *** selection criteria
          OID  = FIELD(ID,'.',1)
          INVN = FIELD(ID,'.',2)+0

          MATREAD LED FROM LEDFILE,OID                    ELSE RETURN
          LOCATE INVN IN LED(8)<1> SETTING GEN            ELSE RETURN


          SLSM = LED(SLS.ATTB)<1,GEN>
          IF SLSP#'' AND SLSP # SLSM                      THEN RETURN

          IF FILTER.FLAG THEN
             FILTER.SELECT SKIP.FLAG,OID,GEN,FITEM
             IF SKIP.FLAG THEN RETURN
          END

          *** Calculate the order totals
          GOSUB CALC.AMT

          *** Add the totals to the salesman records


          REC.ID = SLSM "L#10":'~':LED(2)<1,GEN,1>
          READ DATA FROM SORTFILE,REC.ID ELSE DATA = ''

          *** If the order was in the current month, increase those amts
          *** making sure we select beginning on month as well.
          IF LED(9)<1,GEN> >= MD THEN
             DATA<1> += NET
             DATA<2> += COGS
          END
          *** Always include the amounts in the years amts
          DATA<3> += NET
          DATA<4> += COGS

          WRITE DATA ON SORTFILE,REC.ID
          RETURN
*-------------------------------------------------------------------------*
CALC.AMT: *** Calculate the sales and cost amount for this order
          IF CURR.FLG THEN
             CONV.CURR.LED TGT,GEN
          END

          OE.GET.QSIGN QSIGN,OID,GEN

          NET      = 0
          COGS     = 0
          LDIDS    = LED(48)<1,GEN>
          LD.CT    = DCOUNT(LDIDS,SVM)

          *** Calculate the sales and cost based on all items
          FOR LDN  = 1 TO LD.CT
             LD.GET LDIDS<1,1,LDN>
             IF CURR.FLG THEN
                CONV.CURR.LD TGT,GEN
             END
             PN = LD(1)

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

             *** Don't include comments
             IF (NUM(PN) OR JM.PN) AND PN#'' THEN

                IF NOT(JM.PN) THEN
                   * Don't include misc charge products.
                   READV PTYPE FROM PRDFILE,PN,3 ELSE PTYPE = ''
                   IF PTYPE = 3 THEN CONTINUE
                END

                QTY  = -(SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>))
                AMT  = ICONV(OCONV(LD(8)<1,GEN>,'MR9')*QTY,'MR2')
                NET += AMT
                IF COST.BASE THEN
                   IF COST.BASE = 27 AND LD(27)<1,GEN> = '' THEN
                      WRK = LD(10)<1,GEN>
                   END ELSE
                      WRK = LD(COST.BASE)<1,GEN>
                   END
                   COGS  += ICONV(OCONV(QTY*WRK,'MR9'),'MR2')
                END
             END
          NEXT LDN
          RETURN
*-------------------------------------------------------------------------*
INIT:     *** Initialize the required variables for the report.
          TGT      = ''
          CURR.FLG = NO
          BASE.CUR = YES
          IF DRPT<51> THEN
             READ BASEC FROM CTRLFILE,"BASE.CURRENCY" ELSE BASEC = ''
             IF DRPT<51> # BASEC THEN
                TGT      = DRPT<51>
                CURR.FLG = YES
                BASE.CUR = NO
             END
          END

          *** Calculate the month and year fiscal begin for the start dates
          AODT = OCONV(AOD,'D4/')
          YR   = FIELD(AODT,'/',3)
          QM   = FIELD(AODT,'/',1)
          BDT  = "FB/FB/":YR
          REALDATE SD,BDT,AOD
          MDT  = "MO/FB/":YR
          REALDATE MD,MDT,AOD
          MDT  = OCONV(MD,'D4/')
          BDT  = OCONV(SD,'D4/')

          IF FITEM = '' THEN FILTER.FLAG = NO ELSE FILTER.FLAG = YES

          BRS = BR

          IF OCONV(SLSP,'MCU')='ALL' THEN SLSP=''
          BEGIN CASE
          CASE CST=1;       COST.BASE = 27
          CASE CST=2;       COST.BASE = 10
          CASE OTHERWISE;   COST.BASE = 0
          END CASE

          PH.COST.DESC CST,COST.DESC

          LAST.SLSM = '@@'
          STOLS     = ''
          GTOLS     = ''
          CCNT      = 0

          BEGIN CASE
          CASE SLSM.TYPE[1,1] = "W"
             SLS.ATTB = 73
          CASE SLSM.TYPE[1,1] = "I"
             SLS.ATTB = 34
          CASE OTHERWISE
             SLS.ATTB = 72
          END CASE
          RETURN
!TSMITH~09/26/14~10:45
