       PH.ARGS DRPT,BRCHS,BR.DISP,SD,ED,SEL.LN.TYP,SEL.LINES,SEL.TYP,SEL.SORTBY,OPT,FITEM,VCSGN,CCN,INCL.DIRS,INCL.RETS
** Version# 40.0004[117] - 05/17/2016 - 10:20am - TSMITH - eclipse
*** V40.0004 Change - Custom Coding . - 05/17/2016 - TSMITH - eclipse
*** V40.0003 Change - Custom Coding . - 12/23/2014 - TSMITH - eclipse
** Copied from CBP PURCH.PHR.PROD.SUM.QUART Version# 40.0002[289] - 03/27/2014 - 10:40am - TSMITH - eclipse
*** V40.0002 Change - Custom Coding . - 03/27/2014 - TSMITH - eclipse
** Copied from BP PURCH.PHR.PROD.SUM Version# 40 - 10/03/2008 - 12:25pm - BABS - main

*-------------------------------------------------------------------------*
*** Subroutine: PURCH.PHR.PROD.SUM
*-------------------------------------------------------------------------*
*** Description: For each product selected for the report, the Product
*** Purchases Summary Report lists the quantity and dollar amount of each
*** product purchased during a designated period of time.
*-------------------------------------------------------------------------*
*** Parameters:
***       BRCHS      - Branch(es)                                    (IN)
***       BR.DISP    - Branch display string                         (IN)
***       SD         - Start Date                                    (IN)
***       ED         - End Date                                      (IN)
***       SEL.LN.TYP - Selected list type (pline,bline,pgrp,sgrp,pn) (IN)
***       SEL.LINES  - Selected list                                 (IN)
***       SEL.TYP    - Product location quantity type                (IN)
***       SEL.SORTBY - Sortby                                        (IN)
***       OPT        - Detail/Summary                                (IN)
***       FITEM      - Additional Filtering data                     (IN)
***       VCSGN      - Include/Exclude/Only Vendor Consignments      (IN)
***       CCN        - Vendor Name                                   (IN)
***       INCL.DIRS  - Include/Exclude/Only Directs                  (IN)
***       INCL.RETS  - Include/Exclude/Only Returns                  (IN)
*-------------------------------------------------------------------------*
*** Common:
***       DRPT - Standard Report array with OPT info                 (IN)
*-------------------------------------------------------------------------*
*** Notes:
*-------------------------------------------------------------------------*
          UT.OPEN.FILE 'ENTITY.GPS',ENTGFILE,ERR.MSG,,HNDL.NUMBER
          UT.OPEN.FILE "PROD.SEQ",PROD.SEQFILE,ERR.MSG
          IF ERR.MSG THEN RETURN

          TGT      = ''
          CURR.FLG = NO
          IF DRPT<51> THEN
             READ BASEC FROM CTRLFILE,"BASE.CURRENCY" ELSE BASEC = ''
             IF DRPT<51> # BASEC THEN
                TGT      = DRPT<51>
                CURR.FLG = YES
             END
          END

          SDT    = OCONV(SD,'D4/')
          EDT    = OCONV(ED,'D4/')
          DETAIL = (OPT[1,1]='D')



          SEL.LINE.CT = DCOUNT(SEL.LINES,VM)

          PSORTBY = SEL.SORTBY

          IF SEL.SORTBY = 'Price Line' THEN SEL.SORTBY = 'L'
          IF SEL.SORTBY = 'Buy Group' THEN SEL.SORTBY = 'G'
          IF SEL.SORTBY = 'Vendor by Price Line' THEN SEL.SORTBY = 'D'
          SEL.SORTBY = SEL.SORTBY[1,1]

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

          *** This will allow us to select different types
          *** of records from the AR file using an Index...
          BEGIN CASE
          CASE INCL.DIRS[1,1] = 'E'
             *** We'll only be selecting Purchase Orders...
             JLI.CODES = 4
             CD.CT     = 1
          CASE INCL.DIRS[1,1] = 'O'
             *** We'll only be selecting Directs...
             JLI.CODES = 6
             CD.CT     = 1
          CASE OTHERWISE
             *** We'll be selecting both Purchase Orders and Directs...
             JLI.CODES = 4:AM:6
             CD.CT     = 2
          END CASE

          UT.TEMPFILE.CREATE TEMPFLNM,TEMPFILE

          WRITE 'Selecting...' ON PHSTFILE,PID$
          GOSUB SEL.IDS
          WRITE 'Spooling...' ON PHSTFILE,PID$
*-------------------------------------------------------------------------*
          IF SEL.LINE.CT>1 THEN MSG.LN='*Multi*' ELSE
             MSG.LN = SEL.LINES
          END

          HDG = 'Equity Rebate ':OPT:' for ':SDT:' to ':EDT
          HDG = HDG"L#100":'  Page :^#####'
          HDG<1,2> = 'Sorted By:  ':PSORTBY

          IF SEL.LINES#'' THEN
             BEGIN CASE
             CASE SEL.LN.TYP='Price Line'
                HDG<1,2> := ' - Price Line(s): ':MSG.LN:SPACE(5)
             CASE SEL.LN.TYP='Buy Line'
                HDG<1,2> := ' - Buy Line(s): '  :MSG.LN:SPACE(5)
             CASE SEL.LN.TYP = 'Sell Group'
                HDG<1,2> := ' - Sell Group(s): '  :MSG.LN:SPACE(5)
             CASE SEL.LN.TYP = 'Buy Group'
                HDG<1,2> := ' - Buy Group(s): '  :MSG.LN:SPACE(5)
             CASE SEL.LN.TYP = 'Product'
                HDG<1,2> := ' - Product(s): '  :MSG.LN:SPACE(5)
             END CASE
          END

          IF SEL.TYP THEN HDG<1,2> := ' - Qty Types:  ':SEL.TYP:SPACE(5)

          IF CURR.FLG THEN
             HDG<1,2> := ' - Report Currency:  ':TGT
          END

          VEND.NAME = ''

          IF CCN THEN
             READV VEND.NAME FROM CUSFILE,CCN,1 ELSE VEND.NAME = ''
             IF VEND.NAME = '' THEN VEND.NAME = CCN
             HDG<1,3> = '  for ':VEND.NAME
          END

          BRANCH.ALLOW = 121
          IF LEN(BR.DISP) > BRANCH.ALLOW THEN
             BR.DISP = BR.DISP[1,BRANCH.ALLOW]:'...'
          END

          HDG<1,-1> = 'Branches: ':BR.DISP
          HDG<1,-1> = INCL.DIRS:' Directs  ':INCL.RETS:' Returns  ':VCSGN:' Vendor Consignment'
          IF SEL.SORTBY = 'D' THEN
             HDG<1,-1> = "Vendor Name...............  Price Line  Invoice#......  Whse  ShipDate  Prod No..  Product........................  Qty Recvd..  Ext Amount......Rebate %....................Rebate % Original Ext     Terms"
          END ELSE
             HDG<1,-1> = "Vendor Name...............  Invoice#......  Whse  ShipDate  Prod No..  Product........................  Qty Recvd..  Ext Amount...... Rebate%.. Rebate....... Rebate Program......................... Original Ext Terms"
          END

             PRINT EXT                        "R2,#16 ":
             PRINT TERMS                      "L#13":
             PRINT TERMS.PERC


          IF DRPT<33> = '' THEN
             TITLE = 'Equity Rebate ':OPT:' for ':SDT:' to ':EDT
          END ELSE
             TITLE = DRPT<33>:'  ':SDT:' to ':EDT
          END

          HCT   = DCOUNT(HDG<1>,VM)
          LGTH  = 0
          FOR J = 1 TO HCT
             IF LEN(HDG<1,J>) > LGTH THEN LGTH = LEN(HDG<1,J>)
          NEXT J

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

          FILTER.PRINT "P",FITEM
          SSELECT TEMPFILE

          STOL     = ''
          GTOL     = ''
          OLD.ITEM = '@@@'
          PREV.CN  = '@@@'
          SPER.UOM = '@@@'
          SPER.QTY = 0
          GPER.UOM = '@@@'
          GPER.QTY = 0


          LOOP
             READNEXT TID ELSE EXIT
             READ TMP FROM TEMPFILE,TID ELSE TMP=''
             *** Get TITEM for subtotaling
             BEGIN CASE
             CASE SEL.SORTBY = 'V'
                TITEM = FIELD(TID,'~',2)+0
             CASE SEL.SORTBY = 'P'
                TITEM = FIELD(TID,'~',2)+0
             CASE SEL.SORTBY = 'W' OR SEL.SORTBY = 'S' OR SEL.SORTBY = 'G'
                TITEM = FIELD(TID,'~',1)
             CASE SEL.SORTBY = 'B' OR SEL.SORTBY = 'L'
                TITEM = FIELD(TID,'~',2)
             CASE SEL.SORTBY = 'D'
                TITEM = FIELD(TID,'~',2,2)
             END CASE
             TITEM = TRIM(TITEM)

             IF TITEM # OLD.ITEM AND OLD.ITEM # '@@@' THEN GOSUB PRT.STOL
             GOSUB PRT.ONE
             OLD.ITEM = TITEM
          REPEAT

          IF OLD.ITEM # '@@@' THEN GOSUB PRT.STOL

          GOSUB PRT.GTOL

          PRINTER.OFF DOC.ID

          UT.TEMPFILE.DELETE TEMPFLNM
          UT.PH.CLEANUP
          SEND.MESSAGE 'Phantom',USER.ID,TITLE:' is Complete'

          STOP
*-------------------------------------------------------------------------*
PRT.ONE:
          IVNO     = TMP<1>
          WHSE     = TMP<3>
          QTY      = TMP<4>
          EACH.QTY = TMP<4>
          PRC      = TMP<5>
          CST      = TMP<6>
          PN       = TMP<7>
          SHP.DT   = TMP<8>
          CN       = TMP<9>
          BUY.LN   = TMP<10>
          PRC.LN   = TMP<11>
          UOM      = TMP<12>
          JM.ITEM  = TMP<13>
          JM.DESC  = TMP<14>



*-------------------------------------------------------------------------*
*** Rebate Code



          ENT.CAT<1,1>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,1>
          ENT.CAT<1,2>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,2>
          ENT.CAT<1,3>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,3>
          ENT.CAT<1,4>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,4>
          ENT.CAT<1,5>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,5>
          ENT.CAT<1,6>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,6>
          ENT.CAT<1,7>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,7>
          ENT.CAT<1,8>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,8>
          ENT.CAT<1,9>  = TRANS('ENTITY.GPS',CN,1,'X')<1,1,9>
          ENT.CAT<1,10> = TRANS('ENTITY.GPS',CN,1,'X')<1,1,10>

          PROD.CAT<1,1>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,1>
          PROD.CAT<1,2>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,2>
          PROD.CAT<1,3>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,3>
          PROD.CAT<1,4>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,4>
          PROD.CAT<1,5>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,5>
          PROD.CAT<1,6>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,6>
          PROD.CAT<1,7>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,7>
          PROD.CAT<1,8>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,8>
          PROD.CAT<1,9>  = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,9>
          PROD.CAT<1,10> = TRANS('PRODUCT.GPS',PN,1,'X')<1,1,10>


          DISC.AMT   = OCONV(TRANS('AR',FIELD(TRANS('AR',IVNO,2,'X'),'.',2):'.':FIELD(TRANS('AR',IVNO,2,'X'),'.',3),9,'X'),'MR2')
          TERMS      = TRANS('LEDGER',FIELD(IVNO,'.',1),29,'X')<1,1,PGEN>
          TERMS.PERC = OCONV(TRANS('TERMS',TERMS,2,'X'),'MR2')/100

          LOCATE PROD.CAT<1,10> IN ENT.CAT<1> SETTING POS ELSE NULL
          LOCATE PROD.CAT<1,9> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,10>
          LOCATE PROD.CAT<1,8> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,9>
          LOCATE PROD.CAT<1,7> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,8>
          LOCATE PROD.CAT<1,6> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,7>
          LOCATE PROD.CAT<1,5> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,6>
          LOCATE PROD.CAT<1,4> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,5>
          LOCATE PROD.CAT<1,3> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,4>
          LOCATE PROD.CAT<1,2> IN ENT.CAT<1> SETTING POS ELSE POS=PROD.CAT<1,3>
          LOCATE PROD.CAT<1,1> IN ENT.CAT<1> SETTING POS ELSE NULL


          ENT.CAT     = TRIM(ENT.CAT<1,POS>,VM)

          FREQ.MTH    = TRANS('ENTITY.GPS',CN,5,'X')<1,1,1>
          FREQ.QTR    = TRANS('ENTITY.GPS',CN,5,'X')<1,1,2>
          FREQ.YR     = TRANS('ENTITY.GPS',CN,5,'X')<1,1,3>
          BEGIN CASE
          CASE LEN(ENT.CAT) < 1 OR NOT(ENT.CAT<1,1>)
          PERC.MTH    = 0
          PERC.QTR    = 0
          PERC.YR     = 0
          CASE OTHERWISE
          PERC.MTH    = TRANS('ENTITY.GPS',CN,2,'X')<1,1,POS>/10000
          PERC.QTR    = TRANS('ENTITY.GPS',CN,3,'X')<1,1,POS>/10000
          PERC.YR     = TRANS('ENTITY.GPS',CN,4,'X')<1,1,POS>/10000
          END CASE
          REB.MTH.EXT = ''
          REB.QTR.EXT = ''
          REB.YR.EXT  = ''

          IF FREQ.MTH='1' AND PROD.CAT#'' THEN
             REB.MTH.EXT = EXT*PERC.MTH
             MTH = PERC.MTH*100
             END
           IF FREQ.QTR='1' AND PROD.CAT#'' THEN
             REB.QTR.EXT = EXT*PERC.QTR
             QTR = PERC.QTR*100
             END
          IF FREQ.YR='1' AND PROD.CAT#'' THEN
             REB.YR.EXT = EXT*PERC.YR
             YR = PERC.YR*100
             END

*-------------------------------------------------------------------------*

          EXT      = OCONV(QTY*PRC,'MR9')

          IF SEL.SORTBY # 'D' THEN
             IF OLD.ITEM # CN OR SEL.SORTBY # 'V' THEN
                MATREAD CUS FROM CUSFILE,CN ELSE MAT CUS = ''
                IF DETAIL THEN PRINT CUS(1)                  "L#26":'  ':
             END ELSE
                IF DETAIL THEN PRINT ''                      "L#26":'  ':
             END
          END ELSE
             NEW.ITEM = CN:'~':PRC.LN
             IF OLD.ITEM # NEW.ITEM THEN
                MATREAD CUS FROM CUSFILE,CN ELSE MAT CUS = ''
                IF DETAIL THEN PRINT CUS(1)                  "L#26":'  ':
                IF DETAIL THEN PRINT PRC.LN                  "L#10":'  ':
             END ELSE
                IF DETAIL THEN PRINT ''                      "L#38":'  ':
             END
          END
          MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ''
          IF NOT(JM.ITEM) THEN
             DESC = PRD(1)
          END ELSE
             DESC = JM.DESC
          END
          CONVERT VM TO ' ' IN DESC

          PRC.LN = PRD(9)
          MATREAD PLNE FROM PLNEFILE,PRC.LN ELSE MAT PLNE = ''
          DFLT.PER.GET "P",PER.QTY,PER.UOM
          IF PER.UOM # UOM THEN
             PER.UOM = UOM
             IF PRD(15) = '' THEN UMTBL = PLNE(3) ELSE UMTBL = PRD(15)
             IQ.TO.ALPHA UMTBL,PRD(7),PER.UOM,QTY,Q1,U1,Q2,U2
             QTY = Q1
             IF Q2 THEN QTY = QTY:'.':Q2
             LOCATE PER.UOM IN PRD(15)<1> SETTING UPOS THEN
                PER.QTY = PRD(7)<1,UPOS>
             END
          END ELSE
             IF PER.QTY > 1 THEN QTY = QTY / PER.QTY
          END


          IF DETAIL THEN
             PRINT IVNO                       "L#14  ":
             PRINT WHSE                       "R#4  ":
             PRINT OCONV(SHP.DT,'D2/')        "L#8  ":
             PRINT "^":PN                     "L#8  ":
             PRINT DESC                       "L#31  ":

             IF INDEX(QTY,'.',1) THEN FMT = "R1,#9" ELSE FMT = "R,#9"
             PRINT QTY FMT:PER.UOM "L#2  ":


             IF DISC.AMT # '' AND PROD.CAT # '' THEN
             EXT2 = EXT - (EXT * TERMS.PERC)
             END ELSE
             EXT2 = EXT
             END
             PRINT EXT2                       "R2,#16 ":


             BEGIN CASE
             CASE FREQ.MTH='1' AND PROD.CAT#''
             REB.FREQ.EXT = EXT*PERC.MTH
             FREQ = PERC.MTH*100
             CASE FREQ.QTR='1' AND PROD.CAT#''
             REB.FREQ.EXT = EXT*PERC.QTR
             FREQ = PERC.QTR*100
             CASE FREQ.YR='1' AND PROD.CAT#''
             REB.FREQ.EXT = EXT*PERC.YR
             FREQ = PERC.YR*100
             CASE OTHERWISE
             REB.FREQ.EXT = 0
             FREQ = 0
             END CASE
             PRINT FREQ                   "R1#5":'% ':
             PRINT REB.FREQ.EXT                   "R2,#16 ":



*-------------------------------------------------------------------------*
             *** Proofing Code
             *PRINT PRC.LN                     "L#20 ":
             *PRINT PROD.CAT                 "L#35":
             PRINT ENT.CAT                  "L#70 ":
             PRINT EXT                        "R2,#16 ":
             PRINT TERMS                      "L#13":
             PRINT TERMS.PERC

             *"R1#5":'% '

          END

          STOL<1,1> += EXT2
          STOL<1,2> += EACH.QTY
          STOL<1,3> += QTY
          STOL<1,4> += REB.FREQ.EXT


          GTOL<1,1> += EXT2
          GTOL<1,2> += EACH.QTY
          GTOL<1,3> += QTY
          GTOL<1,4> += REB.FREQ.EXT


          IF SPER.UOM # PER.UOM OR SPER.QTY # PER.QTY THEN
             IF SPER.UOM = '@@@' THEN;* first pass
                SPER.UOM = PER.UOM
                SPER.QTY = PER.QTY
             END ELSE
                SPER.UOM = ''
                SPER.QTY = ''
             END
          END
          IF GPER.UOM # PER.UOM OR GPER.QTY # PER.QTY THEN
             IF GPER.UOM = '@@@' THEN;* first pass
                GPER.UOM = PER.UOM
                GPER.QTY = PER.QTY
             END ELSE
                GPER.UOM = ''
                GPER.QTY = ''
             END
          END

          RETURN
*-------------------------------------------------------------------------*
PRT.STOL:  *
          IF DETAIL THEN
             IF SEL.SORTBY # 'D' THEN
                PRINT SPACE(98):'      ===========  ================        ================        '
             END ELSE
                PRINT SPACE(110):'      ===========  ================        ================        '
             END
             BEGIN CASE
             CASE SEL.SORTBY = 'V'
                READV CNAME FROM CUSFILE,OLD.ITEM,1 ELSE CNAME = ''
                IF CNAME = '' THEN CNAME = OLD.ITEM
                PRINT SPACE(61):'Total for ':CNAME"L#20":' ----------  ':
             CASE SEL.SORTBY = 'P'
                PRINT SPACE(51):'Total for ':DESC"L#30" :' ----------  ':
             CASE SEL.SORTBY = 'D'
                CID = FIELD(OLD.ITEM,'~',2)
                READV CNAME FROM CUSFILE,CID,1 ELSE CNAME = ''
                PRINT SPACE(61):'Total for ':CNAME"L#20":", ":PRC.LN"L#10":" ----------  ":
             CASE OTHERWISE
                PRINT SPACE(60):'Total for ':OLD.ITEM"L#10":' ----------  ':
             END CASE
          END ELSE
             BEGIN CASE
             CASE SEL.SORTBY = 'P'
                PRINT SPACE(71):DESC "L#30":'   ':
             CASE SEL.SORTBY = 'B'
                DESC = "Buy Line -- ":OLD.ITEM
                PRINT SPACE(71):DESC "L#30":'   ':
             CASE SEL.SORTBY = 'L'
                DESC = "Price Line -- ":OLD.ITEM
                PRINT SPACE(71):DESC "L#30":'   ':
             CASE SEL.SORTBY = 'V'
                READV CNAME FROM CUSFILE,OLD.ITEM,1 ELSE CNAME = ''
                IF CNAME = '' THEN CNAME = OLD.ITEM
                PRINT CNAME "L#26":SPACE(75):'   ':
             CASE SEL.SORTBY = 'W'
                DESC = "Writer -- ":OLD.ITEM
                PRINT SPACE(71):DESC "L#30":'   ':
             CASE SEL.SORTBY = 'S'
                DESC = "Sell Group -- ":OLD.ITEM
                PRINT SPACE(71):DESC "L#30":'   ':
             CASE SEL.SORTBY = 'G'
                DESC = "Buy Group -- ":OLD.ITEM
                PRINT SPACE(71):DESC "L#30":'   ':
             CASE SEL.SORTBY = 'D'
                CID = FIELD(OLD.ITEM,'~',2)
                PLINE = FIELD(OLD.ITEM,'~',1)
                READV CNAME FROM CUSFILE,CID,1 ELSE CNAME = ''
                PRINT CNAME "L#26  ":PLINE "L#10":SPACE(78):
             CASE OTHERWISE
                PRINT SPACE(71):OLD.ITEM "L#30":'   ':
             END CASE
          END

          IF SPER.UOM = '' OR SPER.UOM = '@@@' THEN
             SPER.UOM = 'ea'
             SQTY     = STOL<1,2>
          END ELSE
             SQTY     = STOL<1,3>
          END
          IF INDEX(SQTY,'.',1) THEN FMT = "R1,#9" ELSE FMT = "R,#9"

          PRINT SQTY FMT:SPER.UOM "L#2  ":STOL<1,1> "R2,#16        ":STOL<1,4> "R2,#16        "
          PRINT

          STOL     = ''
          SPER.UOM = '@@@'
          SPER.QTY = 0

          RETURN
*-------------------------------------------------------------------------*
PRT.GTOL: *** Print grand totals

          IF SEL.SORTBY # 'D' THEN
             PRINT SPACE(100):'    ===========  ================        ================        '
             PRINT SPACE(87):'Grand Total ---- ':
          END ELSE
             PRINT SPACE(112):'    ===========  ================        ================        '
             PRINT SPACE(99):'Grand Total ---- ':
          END

          IF GPER.UOM = '' OR GPER.UOM = '@@@' THEN
             GPER.UOM = 'ea'
             GQTY     = GTOL<1,2>
          END ELSE
             GQTY     = GTOL<1,3>
          END
          IF INDEX(GQTY,'.',1) THEN FMT = "R1,#9" ELSE FMT = "R,#9"

          PRINT GQTY FMT:GPER.UOM "L#2 ":GTOL<1,1> "R2,#17        ":GTOL<1,4> "R2,#16        "
          PRINT

          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:
*** FOR each day between our Start Date and End Date...
          FOR D = SD TO ED

*** FOR each of the Record Types (JLI.CODES) we want to select on...
             FOR CD.INDX = 1 TO CD.CT

             *** Atrributes 1 AND 2 will be valid when selecting
             *** Purchase Orders AND Direct Orders Indexes.
             *** NXT.CD will be 4 for Purchase Orders Code...
             *** NXT.CD will be 7 for Direct Orders Code...
             NXT.CD = JLI.CODES<CD.INDX>

             *** Get our next batch of records with this Index...
             L = JLI.READ.EMU(NXT.CD:'*':D)

             *** Count the number of records in our batch...
             LLN = DCOUNT(L,AM)

*** FOR each of the records in our Index batch...
             FOR LLX = 1 TO LLN
                *** Break out the values for this record...
                ID   = L<LLX>
                OID  = FIELD(ID,'.',1)
                INVN = FIELD(ID,'.',2)+0

                MATREAD LED FROM LEDFILE,OID            ELSE GOTO SKIPIT

                LOCATE INVN IN LED(8)<1> SETTING GEN    ELSE GOTO SKIPIT

                OE.GET.QSIGN QSIGN,OID,GEN

                PGEN = GEN     ;* Purchase Order GEN is current GEN

                *** In case we're looking at a DIRECT, we need to set
                *** the proper Sales Order Gen in order to get certain
                *** data. Otherwise the SGEN is just our current GEN...
                LOCATE LED(12)<1,PGEN> IN LED(33)<1> SETTING SGEN THEN
                   DIRECT = YES
                END ELSE
                   DIRECT = NO
                   SGEN = PGEN
                END

                LOCATE LED(2)<1,PGEN,1> IN BRCHS<1> SETTING XX ELSE
                   GOTO SKIPIT
                END

                IF CCN # '' AND LED(5)<1,PGEN> # CCN THEN GOTO SKIPIT

                IF FILTER.FLAG THEN
                   FILTER.SELECT SKIP.FLAG,OID,PGEN,FITEM
                   IF SKIP.FLAG THEN GOTO SKIPIT
                END

                IF VCSGN = 'Only' AND LED(110)<1,1> = "" THEN GOTO SKIPIT
                IF VCSGN = 'Exclude' AND LED(110)<1,1>#"" THEN GOTO SKIPIT

                CST.ATB = 8
                LDIDS   = LED(48)<1,SGEN>
                LD.CT   = DCOUNT(LDIDS,SVM)

                FOR LDN = 1 TO LD.CT
                   LDID = LDIDS<1,1,LDN>
                   LD.GET LDID

                   IF CURR.FLG THEN
                      CONV.CURR.LD TGT,SGEN
                   END

                   PN = LD(1)

                   * If Job Management item then get template product
                   IF PN[1,2] = "L#" THEN
                      IF PN = "L#LPAY" THEN GOTO SKIPLDN
                      * LD(70) will have template id on new JM orders
                      * otherwise we can get it from the vendor number
                      IF LD(70) # "" THEN
                         PN = LD(70)
                      END ELSE
                         JVN = LED(5)<1,PGEN>
                         PN  = JOBMGMT.GET.PN(JVN)
                      END
                   END

                   BEGIN CASE
                   CASE NOT(NUM(PN)) OR PN = ''
                      GOTO SKIPLDN
                   CASE SEL.LINES # '' AND SEL.LN.TYP = 'Product'
                      LOCATE PN IN SEL.LINES<1> SETTING XX ELSE
                         GOTO SKIPLDN
                      END
                   CASE SEL.LINES # '' AND SEL.LN.TYP = 'Price Line'
                      READV PRC.LN FROM PRDFILE,PN,9 ELSE PRC.LN = ''
                      LOCATE PRC.LN IN SEL.LINES<1> SETTING XX ELSE
                         GOTO SKIPLDN
                      END
                   CASE SEL.LINES # '' AND SEL.LN.TYP = 'Buy Line'
                      READV BUY.LN FROM PRDFILE,PN,12 ELSE BUY.LN = ''
                      LOCATE BUY.LN IN SEL.LINES<1> SETTING XX ELSE
                         GOTO SKIPLDN
                      END
                   CASE SEL.LINES # '' AND SEL.LN.TYP = 'Sell Group'
                      SEL.GRPS = ''
                      PRD.BR.GET.VAL LED(2)<1,GEN,1>,PN,24,SEL.GRPS
                      FOUND.GROUP = NO
                      FOR SGRP.IDX = 1 TO SEL.LINE.CT
                         SEL.LN = SEL.LINES<1,SGRP.IDX>
                         LOCATE SEL.LN IN SEL.GRPS<1> SETTING SGPS THEN
                            FOUND.GROUP = YES
                            EXIT
                         END
                      NEXT SGRP.IDX

                      IF NOT(FOUND.GROUP) THEN
                         GOTO SKIPLDN
                      END
                   CASE SEL.LINES # '' AND SEL.LN.TYP = 'Buy Group'
                      BUY.GRPS = ''
                      PRD.BR.GET.VAL LED(2)<1,GEN,1>,PN,23,BUY.GRPS
                      FOUND.GROUP = NO
                      FOR BGRP.IDX = 1 TO SEL.LINE.CT
                         SEL.LN = SEL.LINES<1,BGRP.IDX>
                         LOCATE SEL.LN IN BUY.GRPS<1> SETTING BGPS THEN
                            FOUND.GROUP = YES
                            EXIT
                         END
                      NEXT BGRP.IDX

                      IF NOT(FOUND.GROUP) THEN
                         GOTO SKIPLDN
                      END
                   END CASE

                   IF SEL.TYP = '' THEN
                      QTY = (SUM(LD(5)<1,SGEN>) + SUM(LD(6)<1,SGEN>))*QSIGN
                      GOSUB ADD.ID
                   END ELSE
                      TYP.LOCS = LD(7)<1,SGEN>
                      LOC.CT   = DCOUNT(TYP.LOCS,SVM)

                      FOR JJ   = 1 TO LOC.CT
                         TYP = FIELD(TYP.LOCS<1,1,JJ>,'~',1)
                         IF TYP = SEL.TYP THEN
                            QTY=(LD(5)<1,SGEN,JJ> + LD(6)<1,SGEN,JJ>)*QSIGN
                            GOSUB ADD.ID
                         END
                      NEXT JJ
                   END
SKIPLDN:        NEXT LDN
SKIPIT:      NEXT LLX
          NEXT CD.INDX
          NEXT D

          RETURN
*-------------------------------------------------------------------------*
ADD.ID: *Add ID to the tempfile.  The TID set below will be used to sort

          * apply the return filter.
          IF QTY > 0 THEN
             IF INCL.RETS = 'Only' THEN RETURN TO SKIPLDN
          END ELSE
             IF INCL.RETS = 'Exclude' THEN RETURN TO SKIPLDN
          END

          * Job Management Item
          JM.ITEM = FIELD(LD(1),'#',2)

          IF DIRECT THEN
             IF (LD(10)<1,SGEN>) THEN PRC = LD(10)<1,SGEN> ELSE PRC = LD(10)<1,PGEN>
          END ELSE
             PRC = LD(8)<1,SGEN>
          END
          CST = LD(10)<1,SGEN> * QSIGN
          CN  = LED(5)<1,PGEN>
          SHP.DT = LED(9)<1,PGEN>
          READV PRC.LN  FROM PRDFILE,PN,9  ELSE PRC.LN  = ''
          READV BUY.LN  FROM PRDFILE,PN,12 ELSE BUY.LN  = ''
          * If not Job Mangement item then description from product file.
          IF NOT(JM.ITEM) THEN
             READV DESC    FROM PRDFILE,PN,1  ELSE DESC    = ''
          END ELSE
             IF NUM(JM.ITEM) THEN
                DESC = OE.DESC.GET
             END ELSE
                * We have a lot specified
                DESC = 'LOT:':JM.ITEM
             END
          END
          CONVERT VM  TO ' ' IN DESC
          CONVERT '~' TO ' ' IN DESC

          READV CSORTBY FROM CUSFILE,CN,8  ELSE CSORTBY = ''
          SHP.DT = LED(9)<1,PGEN>

          ID  = OID:'.':LED(8)<1,PGEN>"R%3":'~':LDID"R%5"

          BEGIN CASE
          CASE SEL.SORTBY = 'P'
             TID = DESC<1,1>"L#35":'~':PN"R%8":'~':ID
          CASE SEL.SORTBY = 'W'
             TID = LED(73)<1,PGEN>"L#12":'~':DESC<1,1>"L#35":'~':PN"R%8":'~':ID
          CASE SEL.SORTBY = 'B'
             READV LNDESC FROM BLNEFILE,BUY.LN,1 ELSE LNDESC = ''
             TID = LNDESC"L#35":'~':BUY.LN:'~':CSORTBY"L#11":'~':CN"R%8":'~':ID
          CASE SEL.SORTBY = 'L'
             READV LNDESC FROM PLNEFILE,PRC.LN,1 ELSE LNDESC = ''
             TID = LNDESC"L#35":'~':PRC.LN:'~':CSORTBY"L#11":'~':CN"R%8":'~':ID
          CASE SEL.SORTBY = 'S'
             SEL.GRP = ''
             PRD.BR.GET.VAL LED(2)<1,GEN,1>,PN,24,SEL.GRP.LIST

             GOSUB GET.GRP.SBY

             TID = SEL.GRP"L#35":'~':CSORTBY"L#11":'~':CN"R%8":'~':ID
          CASE SEL.SORTBY = 'G'
             SEL.GRP = ''
             PRD.BR.GET.VAL LED(2)<1,GEN,1>,PN,23,SEL.GRP.LIST

             GOSUB GET.GRP.SBY

             TID = SEL.GRP"L#35":'~':CSORTBY"L#11":'~':CN"R%8":'~':ID
          CASE SEL.SORTBY = 'D'
             TID = CSORTBY"L#11":'~':PRC.LN:'~':CN:'~':DESC<1,1>"L#35":'~':PN"R%8":'~':ID
          CASE OTHERWISE
             TID = CSORTBY"L#11":'~':CN"R%8":'~':DESC<1,1>"L#35":'~':PN"R%8":'~':ID
          END CASE

          READ TMP FROM TEMPFILE,TID ELSE TMP = ''
          TMP<1>   = OID:'.':LED(8)<1,PGEN>"R%3" ;* ID
          TMP<2>   = LDID                        ;* LDID
          TMP<3>   = LED(2)<1,PGEN,2>            ;* Ship branch/warehouse
          TMP<4>   = QTY                         ;* Quantity Shipped
          TMP<5>   = PRC                         ;* Price
          TMP<6>   = CST                         ;* Cost
          TMP<7>   = PN                          ;* Part Number
          TMP<8>   = SHP.DT                      ;* Ship Date
          TMP<9>   = CN                          ;* Customer Number
          TMP<10>  = BUY.LN                      ;* Buy Line
          TMP<11>  = PRC.LN                      ;* Price Line
          TMP<12>  = LD(23)                      ;* uom
          * If Job Management item then send what's in LD(1)
          IF JM.ITEM THEN
             TMP<13>  = JM.ITEM                  ;* job mgmt id
             TMP<14>  = DESC
          END ELSE
             TMP<13>  = ''
             TMP<14>  = ''
          END

          WRITE TMP ON TEMPFILE,TID
          RETURN
*-------------------------------------------------------------------------*
GET.GRP.SBY:*** This will set the appropriate sort-by value if the user
          *** is sorting by sell group or buy group.  Because there can be
          *** multiple groups per product, we have different scenarios
          *** for sorting.

          *** see how many groups there are for the product
          SG.CT = DCOUNT(SEL.GRP.LIST,VM)

          *** If the product only has one group, use it.
          IF SG.CT = 1 THEN
             SEL.GRP = SEL.GRP.LIST<1,1>
             RETURN
          END

          *** If they are sorting by the same group that they selected on,
          *** we should use the first group that matched the selection.
          IF ((SEL.SORTBY='S' AND SEL.LN.TYP='Sell Group') OR (SEL.SORTBY='B' AND SEL.LN.TYP='Buy Group') AND SEL.LINES # '') THEN

             FOUND.GROUP = NO
             FOR SG.IDX = 1 TO SG.CT
                SRCH.GRP = SEL.GRP.LIST<1,SG.IDX>
                LOCATE SRCH.GRP IN SEL.LINES<1> SETTING SGPS THEN
                   SEL.GRP = SRCH.GRP
                   FOUND.GROUP = YES
                   EXIT
                END
             NEXT SGRP.IDX

             IF FOUND.GROUP THEN RETURN
          END

          *** Otherwise, just use the first group in the product
          SEL.GRP = SEL.GRP.LIST<1,1>

          RETURN
*-------------------------------------------------------------------------*

!TSMITH~05/17/16~10:20
