          PH.ARGS DRPT,BRCHS,BR,SD,ED,SEL.TYP,SEL.SORTBY,COST.TYPE,OPT,SEL.NSTKS,FITEM,SEL.CREDS,LEVEL,SLCT,SEL.LIST,SEL.OPTS
** Version# 31.0001[1] - 07/16/2010 - 09:38am - SMITJR - eclipse
** Copied from BP ORDERED.PHR.PROD.SUM Version# 31 - 10/22/2003 - 02:18pm - NELSONC - develop

*** Subroutine: ORDERED.PHR.PROD.SUM
*-------------------------------------------------------------------------*
*** This is the Phantom Routine for the Product Orders Report, which lists
*** all products that have been ordered between the Start and End Dates
*** for the specified branches with the customer who ordered them, order
*** number, quantity ordered, and extended price.
*-------------------------------------------------------------------------*
*** Parameters:
***  DRPT       - Standard Report printing defaults parameter.   [IN]
***  BRCHS      - Branches to report data for.                   [IN]
***  SD         - Start Date.                                    [IN]
***  ED         - End Date.
***               NOTE:  Any order with any order dates in the SD
***               to ED range will be included on this report.   [IN]
***  SEL.TYP    - Product Location Types. Only products having these
***               location types will be included on the report. (IN)
***  SEL.SORTBY - Sortby option.                                 [IN]
***  COST.TYPE  - Cost Type to display on the report.
***               1 - Cost
***               2 - COGS
***               3 - None                                       [IN]
***  OPT        - Summary/Detail report format option.           [IN]
***  SEL.NSTKS  - 'Include/Exclude/Only show Nonstocks' option.  [IN]
***  FITEM      - Additional Select criteria.                    (IN)
***  SEL.CREDS  - 'Include/Exclude/Only show Credits' option.    [IN]
***  LEVEL      - Ship-To, Bill-To or Parent.  Which customer to
***               display data for.                              [IN]
***  SLCT       - Price/Buy Line(s), Buy/Sell Group(s), Product(s),
***               Product Select Code(s); optional filters       (IN)
***  SEL.LIST   - Specific selection data, corresponds to above  (IN)
*** SEL.OPTS    - VM Separated selection options,                (IN)
***             - Value 1 = SEL.BIDS, Value 2 = SEL.OORD
*** SEL.BIDS    - Either include, exclude or only show bids      (IN)
*** SEL.OORD    - Either include, exclude or only show open order(IN)
*-------------------------------------------------------------------------*
***  COMMON Variables Used: DRPT, LED, LD, CUS, PRD
*-------------------------------------------------------------------------*

          SEL.BIDS = SEL.OPTS<1,1>
          SEL.OORD = SEL.OPTS<1,2>

          *** See if an ALTERNATE currency to be used has been passed in...
          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

          *** If they don't want to see Cost data...
          IF COST.TYPE = 3 THEN COST.TYPE = ''
          PH.COST.DESC COST.TYPE,COST.DESC

          *** Initialize other variables that we'll be using...
          ITEM.CT  = 0
          FLAG     = ''
          DETAIL   = (OPT[1,1] = 'D')
          SRT.BRK  = ''

          IF SEL.SORTBY[19,3] = 'Cus' OR SEL.SORTBY[20,3] = 'Cus' THEN
             SECOND.SORTBY.CUSTOMER = YES
          END ELSE
             SECOND.SORTBY.CUSTOMER = NO
          END

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

          *** Set up values for the report heading...
          BRS = BR

          SDT = OCONV(SD,'D4/')
          EDT = OCONV(ED,'D4/')

          *** Flag - if they wanted to Sort by 'Cust PO#'...
          PO.FLAG = (SEL.SORTBY[10,3] = 'PO#')

          *** Count the number of items they're selecting on...
          SEL.CT = DCOUNT(SEL.LIST,VM)

          *** Qty Types...
          SEL.TYPS = SEL.TYP
          CONVERT ',' TO VM IN SEL.TYPS

          SORTBY.MSG = ' by ':SEL.SORTBY
          NSTK.MSG   = SEL.NSTKS:' Nonstocks'
          CRED.MSG   = SEL.CREDS:' Credits'
          LEVEL.MSG  = LEVEL:' Customer Level'
          BID.MSG    = SEL.BIDS:' Bids'
          OORD.MSG   = SEL.OORD:' Open Orders'
          SEL.NSTKS  = SEL.NSTKS[1,1]
          SEL.CREDS  = SEL.CREDS[1,1]

          IF DETAIL THEN DET.MSG = 'Detail' ELSE DET.MSG = 'Summary'

          UT.TEMPFILE.CREATE FLNM.ID,TEMPFILE
*-------------------------------------------------------------------------*
          WRITE 'Selecting...' ON PHSTFILE,PID$
          GOSUB SEL.IDS
          WRITE 'Spooling...' ON PHSTFILE,PID$
*-------------------------------------------------------------------------*
*** Set up the Heading for the report...
          HDG = 'Product Orders ':DET.MSG:SORTBY.MSG:' for ':SDT:' to ':EDT
          HDG<1,3>  = NSTK.MSG:SPACE(5):CRED.MSG:SPACE(5):LEVEL.MSG:SPACE(5):BID.MSG:' ':OORD.MSG

          IF CURR.FLG THEN
             HDG<1,3>  := SPACE(5):'Report Currency:  ':TGT
          END

          SUB.FLAG = YES
          THDR     = ''
          HDR.LN   = 4

          IF SEL.CT THEN
             IF SEL.CT > 1 THEN
                WRK      = '*Multi*'
             END ELSE
                WRK      = SEL.LIST
                SUB.FLAG = NO
             END
             THDR := SLCT:'(s): ':WRK
          END

          IF SEL.TYP THEN
             IF DCOUNT(SEL.TYP,VM) > 1 THEN
                WRK = '*Multi*'
             END ELSE
                WRK = SEL.TYP
             END
             IF THDR # '' THEN THDR := ' - '
             THDR := 'Qty Type(s):  ':WRK
          END

          *** Stick the temp header (THDR) into our Heading...
          IF THDR THEN
             HDG<1,HDR.LN> = THDR
             HDR.LN += 1
          END

          END.HD = ' Qty Ordered'
          END.HD := '  Ext Amount......'

          IF COST.TYPE # '' THEN
             END.HD := '  Ext ':COST.DESC"L#4":'........  '
             END.HD := 'GP$..........  GP%....'
          END

          POSP = 0

          *** We'll print different Headings for the Detail
          *** and Summary formats...
          IF DETAIL THEN GOSUB DET.HDG; ELSE GOSUB SUM.HDG
*-------------------------------------------------------------------------*
DET.HDG:  *** Build the Detail format column headings...
          CUST.HD = 'Cust #  Customer Name.............  Order ID..  '
          WHS.HD  = 'PrcBr  OrdDate.  Product........................ '

          BEGIN CASE
          CASE SEL.SORTBY  = 'Customer PO#'
             HDG<1,HDR.LN> = CUST.HD:'Cust PO#....... ':WHS.HD
             POSP = 16
          CASE OTHERWISE
             HDG<1,HDR.LN> = CUST.HD:WHS.HD
          END CASE

          HDG<1,HDR.LN>   := END.HD

          RETURN TO HD.DONE
*-------------------------------------------------------------------------*
SUM.HDG:  *** Build the Summary format column headings...
          HDG<1,HDR.LN> = SPACE(66)

          BEGIN CASE
          CASE SEL.SORTBY   = 'Customer'
             HDG<1,HDR.LN>  = 'Cust #  Customer Name.............'
             HDG<1,HDR.LN> := SPACE(63)
          CASE SEL.SORTBY   = 'Product'
             HDG<1,HDR.LN> := 'Product Description' "L#31"
          CASE SEL.SORTBY   = 'Zip Code'
             HDG<1,HDR.LN> := 'Zip.. Price Line' "L#31"
          CASE SEL.SORTBY   = '3 Digit Zip'
             HDG<1,HDR.LN> := 'Zip-Price Line' "L#31"
          CASE SEL.SORTBY   = 'Price Line'
             HDG<1,HDR.LN> := 'Price Line' "L#31"
          CASE SEL.SORTBY   = 'Branch'
             HDG<1,HDR.LN>  = SPACE(35)
             HDG<1,HDR.LN> := 'Price Line Description' "L#62"
          CASE SEL.SORTBY   = 'Customer PO#'
             HDG<1,HDR.LN>  = 'Cust #  Customer Name.............'
             HDG<1,HDR.LN> := SPACE(16):'Cust PO#.......':SPACE(48)
             POSP = 16
          CASE SEL.SORTBY[16,11] = 'by Customer'
             HDG<1,HDR.LN>  = SEL.SORTBY[1,14] "L#31"
             HDG<1,HDR.LN> := 'Cust # Customer Name.............':SPACE(31)
          CASE SEL.SORTBY[17,11] = 'by Customer'
             HDG<1,HDR.LN>  = SEL.SORTBY[1,15] "L#31"
             HDG<1,HDR.LN> := 'Cust # Customer Name.............':SPACE(31)
          CASE OTHERWISE
             HDG<1,HDR.LN> := SPACE(2)
             HDG<1,HDR.LN> := SEL.SORTBY "L#29"
          END CASE

          HDG<1,HDR.LN>    := END.HD

          RETURN TO HD.DONE
*-------------------------------------------------------------------------*
HD.DONE:  *** We've finshed selecting/setting up data for the report, now
          *** we can print it out...

          IF DRPT<33> = '' THEN
             TITLE = 'Product Orders ':DET.MSG:' for ':SDT:' to ':EDT
          END ELSE
             TITLE = DRPT<33>:' - ':SDT:' to ':EDT
          END

          *** Calculate the width needed based on the report options
          IF COST.TYPE # '' THEN WDTH = 170 + POSP ELSE WDTH = 140 + POSP

          BRANCH.ALLOW = WDTH - 16
          IF BRANCH.ALLOW < LEN(BRS) THEN BRS = BRS[1,BRANCH.ALLOW]:'...'
          HDG<1,2>  = 'Branch(es):  ':BRS


          *** Tack on our Page Number to the first Heading line...
          HDG1.WDTH = LEN(HDG<1,1>)
          HDG<1,1> := SPACE(WDTH - HDG1.WDTH - 12):'Page :^#####'

*** Turn our printer on and print out all the records we selected...
          PRINTER.ON WDTH,TITLE,DOC.ID,HDG,RPT.DFLT=DRPT
          FILTER.PRINT "S",FITEM

          *** Initialize more variables...
          CCT         = 0
          STOL        = ''
          CTOL        = ''
          BTOL        = ''
          GTOL        = ''
          PREV.CN     = '@@@'
          PREV.TID    = '@@@'
          PREV.TD2    = '@@@'
*-------------------------------------------------------------------------*
          SSELECT TEMPFILE
          *** Loop through each line and print the data
          LOOP
             READNEXT TID ELSE EXIT
             READ TMP FROM TEMPFILE,TID ELSE TMP = ''
             TID1 = FIELD(TID,'|',1)
             TID2 = FIELD(TID,'|',2)
             CN   = FIELD(TID,'~',2)

             *** Print branch and subtotals if needed...
             IF SEL.SORTBY[1,2] = 'Br' THEN
                IF TID1 # PREV.TID THEN
                   GOSUB PRT.BTOL
                   IF PREV.TID#'@@@' THEN PREV.TID += 0
                   GOSUB PRT.STOL
                END ELSE
                   IF TID2 # PREV.TD2 THEN GOSUB PRT.BTOL
                END
             END ELSE
                BEGIN CASE
                CASE SECOND.SORTBY.CUSTOMER
                   IF PREV.CN # CN THEN
                      GOSUB PRT.CTOL
                   END
                   IF TID1 # PREV.TID THEN GOSUB PRT.STOL
                CASE OTHERWISE
                   IF TID1 # PREV.TID THEN GOSUB PRT.STOL
                END CASE
             END

             *** Go print a line of the report...
             GOSUB PRT.ONE
          REPEAT

*** If necessary, go print Branch Totals...
          IF SEL.SORTBY[1,2] = 'Br' THEN
             GOSUB PRT.BTOL
             PREV.TID += 0
          END

          *** We have a secondary sort of Customer...
          IF SECOND.SORTBY.CUSTOMER THEN
             GOSUB PRT.CTOL
          END

*** Go print Sortby Subtotals...
          GOSUB PRT.STOL

*** If necessary, go print Customer Totals...
          IF SEL.SORTBY = 'Customer' THEN
             GOSUB PRT.CTOL
          END

*** Go print the Grand Totals...
          GOSUB PRT.GTOL

*** Turn the printer off and clean up after ourselves...
          PRINTER.OFF DOC.ID

          UT.TEMPFILE.DELETE FLNM.ID
          UT.PH.CLEANUP

          IF ITEM.CT = 0 THEN
             COUNT.MSG = ' - No Items Found'
          END ELSE
             COUNT.MSG = ' - is Complete'
          END

          SEND.MESSAGE 'Phantom',USER.ID,TITLE:COUNT.MSG

          STOP
*-------------------------------------------------------------------------*
PRT.ONE:  *** Print one line item to the report...

          ID      = FIELD(TID,'~',3)
          OID     = FIELD(ID,'.',1)
          ORD.DT  = TMP<4,1>
          PROV    = TMP<4,2>
          CSOV    = TMP<4,3>
          CUST.PO = TMP<4,4>
          IF PROV THEN PROV = '*' ELSE PROV = ' '
          IF CSOV THEN CSOV = '*' ELSE CSOV = ' '

          PBR   = TMP<4,5>
          PUM   = TMP<3,1>
          PN    = TMP<2,1>
          EXT   = TMP<1,1>
          CEXT  = TMP<1,2>
          PQTY  = TMP<1,3>

          *** Only print customer name and number if it is a new customer
          *** OR if we are not sorting by one of the customer options...
          IF PREV.CN # CN OR SEL.SORTBY[1,3] # 'Cus' THEN
             IF SEL.SORTBY[1,3] = 'Cus' AND NOT(PO.FLAG) THEN
                GOSUB PRT.CTOL
             END

             MATREAD CUS FROM CUSFILE,CN ELSE MAT CUS = ''
             IF DETAIL THEN
                PRNT.CN = "&":CN
                PRINT PRNT.CN                 "L#7":' ':
                PRINT CUS(1)                  "L#26":'  ':
             END

          *** If the customer is not new just indent (don't print the name)
          END ELSE
             IF DETAIL THEN
                PRINT ''                      "L#34":'  ':
             END
          END

          PREV.CN = CN
          CCT += 1

          MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ''
          DESC = PRD(1)
          CONVERT VM TO ' ' IN DESC

          IF DETAIL THEN
             PRINT OID                         "L#10":'  ':
             IF PO.FLAG THEN
                PRINT CUST.PO                  "L#15 ":
             END
             PRINT PBR                         "R#5":'  ':
             PRINT OCONV(ORD.DT,'D2/')         "L#8":'  ':
             PRINT DESC                        "L#31":' ':
             PRINT.PQTY = (PQTY"R2")+0
             PRINT PRINT.PQTY                  "R#10":PUM "L#2":'  ':
             PRINT OCONV(EXT,'MR2')            "R#16":
             PRINT PROV                        "L#1":' ':
             IF COST.TYPE # '' THEN
                PRINT OCONV(CEXT,'MR2')        "R#16":
                PRINT CSOV                     "L#1":' ':

                *** Gross Profit Dollars...
                GPD = EXT - CEXT
                PRINT OCONV(GPD,'MR2')         "R#13":' ':

                IF EXT + 0 = 0 THEN
                   GP% = 0
                END ELSE
                   GP% = (EXT-CEXT)/EXT*100
                END
                PRINT GP%                      "R1#7":'%'
             END ELSE PRINT
          END

*** Increment our Totals...
          ITEM.CT   += 1

          CTOL<1,1> += EXT
          CTOL<1,2> += CEXT
          CTOL<1,3> += PQTY
          LOCATE PUM IN CTOL<1,4> SETTING POS ELSE CTOL<1,4,POS> = PUM

          BTOL<1,1> += EXT
          BTOL<1,2> += CEXT
          BTOL<1,3> += PQTY
          LOCATE PUM IN BTOL<1,4> SETTING POS ELSE BTOL<1,4,POS> = PUM

          STOL<1,1> += EXT
          STOL<1,2> += CEXT
          STOL<1,3> += PQTY
          LOCATE PUM IN STOL<1,4> SETTING POS ELSE STOL<1,4,POS> = PUM

          GTOL<1,1> += EXT
          GTOL<1,2> += CEXT
          GTOL<1,3> += PQTY
          LOCATE PUM IN GTOL<1,4> SETTING POS ELSE GTOL<1,4,POS> = PUM

          RETURN
*-------------------------------------------------------------------------*
PRT.STOL: *** Display the subtotal lines based on temp ID

          IF PREV.TID # '@@@' THEN
             ***Check to see if we are in detail mode
             IF DETAIL THEN
                PRINT SPACE(92):SPACE(POSP):'      ===========':
                PRINT '  ================ ':

                IF COST.TYPE # '' THEN
                   PRINT ' ================  =============  ======='
                END ELSE PRINT

                IF SEL.SORTBY[1,3] # 'Cus' AND SEL.SORTBY # 'Product' THEN
                   FLAG = 1
                   PREV.TID = TRIM(PREV.TID)
                   LN       = LEN(PREV.TID)
                   DASHES   = 10 - LN
                   PREV.ID = PREV.TID:' ':STR('-',DASHES)

                   PRINT SPACE(76):SPACE(POSP):'Total for ':PREV.ID"L#10":

                END ELSE
                   PRINT SPACE(76):SPACE(POSP):'Total --------------':
                END

             *** If not in detail mode, we must be in summary mode
             END ELSE
                BEGIN CASE
                CASE SEL.SORTBY[1,3] = 'Cus'
                   IF NOT(PO.FLAG) THEN
                      PL.ID = TRIM(FIELD(PREV.TID,'@',3))
                   END ELSE
                      PL.ID = TRIM(FIELD(PREV.TD2,'@',3))
                   END
                   READV PDESC FROM PLNEFILE,PL.ID,1 ELSE PDESC = ''
                   IF PDESC = '' THEN PDESC = '* Not Found *'
                   SPCR  = ' ------ '
                   PRNT.CN = "&":PREV.CN
                   IF NOT(PO.FLAG) THEN
                      PRINT PRNT.CN                   "L#7":' ':
                      PRINT CUS(1)                    "L#30":
                      PRINT SPACE(POSP):SPCR:
                      PRINT PDESC                     "L#31":SPACE(19):
                   END ELSE
                      PRINT PRNT.CN                   "L#7":' ':
                      PRINT CUS(1)                    "L#30":
                      PRINT SPACE(12):
                      PRINT PREV.TID                  "L#15":SPCR:
                      PRINT PDESC                     "L#39":
                   END
                CASE SEL.SORTBY = 'Product'
                   PRINT SPACE(66):DESC               "L#28":'  ':
                CASE OTHERWISE
                   PRINT SPACE(44):
                   PRINT 'SubTotal For ---------  '   "L#24":
                   PRINT PREV.TID                     "L#28":
                END CASE
             END

             IF STOL<1,4,2> = '' THEN TUM = STOL<1,4> ELSE TUM = '**'

             PRINT.STOL = (STOL<1,3>"R2")+0
             PRINT PRINT.STOL                "R#11":
             PRINT TUM                       "L#2":

             PRINT OCONV(STOL<1,1>,'MR2') "R#18":'  ':

             IF COST.TYPE # '' THEN
                PRINT OCONV(STOL<1,2>,'MR2') "R#16":'  ':

                GPD    = STOL<1,1>-STOL<1,2>

                PRINT OCONV(GPD,'MR2')       "R#13":' ':

                IF STOL<1,1>+0=0 THEN
                   GP% = 0
                END ELSE
                   GP% = (STOL<1,1>-STOL<1,2>)/STOL<1,1>*100
                END

                PRINT GP%                    "R1#7":'%'
             END ELSE
                PRINT
             END

             PRINT
             STOL  = ''
          END
          PREV.TID = TID1
          PREV.TD2 = TID2

          RETURN
*-------------------------------------------------------------------------*
PRT.BTOL: *** Print Selection subtotals if sorting by Branch...

          IF PREV.TD2 # '@@@' THEN
             IF DETAIL THEN
                IF SUB.FLAG THEN
                   PRINT SPACE(92):SPACE(POSP):
                   PRINT '      ===========  ================ ':
                   IF COST.TYPE # '' THEN
                      PRINT ' ================  =============  ======='
                   END ELSE PRINT

                   PREV.TD2 = TRIM(PREV.TD2)
                   LN       = LEN(PREV.TD2)
                   DASHES   = 8 - LN
                   PREV.SRT = PREV.TD2:' ':STR('-',DASHES):'-- '

                   PRINT SPACE(70):SPACE(POSP):'Total for ':PREV.SRT"L#16":
                END ELSE PRINT
             END ELSE
                PREV.TD2 = TRIM(PREV.TD2)
              * GOSUB GET.DESC
                PRT.DESC = TID2
                PRINT '' "L#34":SPACE(POSP):' ------ ':PRT.DESC"L#32":
                PRINT SPACE(22):
             END

             IF SUB.FLAG OR NOT(DETAIL) THEN
                IF BTOL<1,4,2> = '' THEN TUM = BTOL<1,4> ELSE TUM = '**'

                PRINT.BTOL = (BTOL<1,3>"R2")+0
                PRINT PRINT.BTOL                "R#11":
                PRINT TUM                       "L#2":

                PRINT OCONV(BTOL<1,1>,'MR2') "R#18":'  ':

                IF COST.TYPE # '' THEN
                   PRINT OCONV(BTOL<1,2>,'MR2') "R#16":'  ':

                   GPD   = BTOL<1,1>-BTOL<1,2>

                   PRINT OCONV(GPD,'MR2')       "R#13":'  ':

                   IF BTOL<1,1> + 0 = 0 THEN
                      GP% = 0
                   END ELSE
                      GP% = (BTOL<1,1>-BTOL<1,2>)/BTOL<1,1>*100
                   END

                   PRINT GP% "R1#6":'%'
                END ELSE PRINT

                PRINT
                BTOL  = ''
             END
          END

          PREV.TD2 = TID2

          RETURN
*-------------------------------------------------------------------------*
PRT.CTOL: *** Print the Customer Totals...

          IF PREV.CN = '@@@' THEN RETURN
          IF CCT > 1 OR NOT(DETAIL) THEN
             PRINT SPACE(92):
             PRINT SPACE(POSP):'      -----------  ----------------':

             IF COST.TYPE # '' THEN
                PRINT '  ----------------  -------------  -------'
             END ELSE
                PRINT
             END
          END
*** Pad the Customer name with dashes, to fill a set string length...
          C1     = CUS(1)[1,30]
          LN     = LEN(C1)
          DASHES = 28 - LN
          CUST   = C1:' ':STR('-',DASHES)

          WRK    = '** Subtotal for : ':CUST:' '
          PRINT SPACE(48):SPACE(POSP):WRK"L#48":

          IF CTOL<1,4,2> = '' THEN TUM = CTOL<1,4> ELSE TUM = '**'

          PRINT.CTOL = (CTOL<1,3>"R2")+0
          PRINT PRINT.CTOL                "R#11":
          PRINT TUM                       "L#2":

          PRINT OCONV(CTOL<1,1>,'MR2') "R#18":'  ':

          IF COST.TYPE # '' THEN
             PRINT OCONV(CTOL<1,2>,'MR2') "R#16":'  ':
             GPD = CTOL<1,1>-CTOL<1,2>
             PRINT OCONV(GPD,'MR2')       "R#13":' ':
             IF CTOL<1,1> + 0 = 0 THEN
                GP% = 0
             END ELSE
                GP% = (CTOL<1,1>-CTOL<1,2>)/CTOL<1,1>*100
             END
             PRINT GP%                    "R1#7":'%'
          END ELSE
             PRINT
          END
          PRINT
          PRINT

          CTOL = ''
          CCT  = 0

          RETURN
*-------------------------------------------------------------------------*
PRT.GTOL: *** Print the Grand Totals...

          PRINT SPACE(93):SPACE(POSP):'    ============  ================ ':
          IF COST.TYPE # '' THEN
             PRINT ' ================  =============  ======='
          END ELSE PRINT

          IF GTOL<1,4,2> = '' THEN TUM = GTOL<1,4> ELSE TUM = '**'

          PRINT SPACE(76):SPACE(POSP):
          PRINT.GTOL = (GTOL<1,3>"R2")+0
          PRINT 'Grand Total -------- ':PRINT.GTOL "R#10":
          PRINT TUM                           "L#2":

          PRINT OCONV(GTOL<1,1>,'MR2')     "R#18":'  ':

          IF COST.TYPE # '' THEN
             PRINT OCONV(GTOL<1,2>,'MR2')     "R#16":'  ':
             GPD    = GTOL<1,1>-GTOL<1,2>
             PRINT OCONV(GPD,'MR2')           "R#13":' ':
             IF GTOL<1,1> + 0 = 0 THEN
                GP% = 0
             END ELSE
                GP% = (GTOL<1,1>-GTOL<1,2>)/GTOL<1,1>*100
             END
             PRINT GP%                        "R1#7":'%'
          END ELSE PRINT

          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:  *** Select Ledger Records and filter them for the report...

          COMMAND = 'SELECT LEDGER = "S]" WITH $ORDER_DATE$ >= "':SDT:'"'
          COMMAND := ' AND WITH $ORDER_DATE$ <= "':EDT:'"'

          EXECUTE COMMAND CAPTURING MSG
          LOOP
             READNEXT OID ELSE EXIT
             MATREAD LED FROM LEDFILE,OID ELSE CONTINUE
             ORDER.DATES = LED(4)
             GEN.CT      = DCOUNT(ORDER.DATES,VM)
             FOR GEN     = 1 TO GEN.CT
                STATUS   = LED(6)<1,GEN>
                *** Disregard cancelled, payment, PO side of directs
                IF STATUS = 'X' OR STATUS = '$' OR STATUS = 'Y' THEN
                   CONTINUE
                END
                BEGIN CASE
                CASE SEL.BIDS[1,1] = 'E'
                   IF STATUS = 'B' THEN CONTINUE
                CASE SEL.BIDS[1,1] = 'O'
                   IF STATUS # 'B' THEN CONTINUE
                END CASE

                *** Check whether the user wanted to include open orders
                IF STATUS # "B" THEN
                   BEGIN CASE
                   CASE SEL.OORD[1,1] = 'E'
                      IF NOT(LED(8)<1,GEN>) THEN CONTINUE
                   CASE SEL.OORD[1,1] = 'O'
                      IF LED(8)<1,GEN> THEN CONTINUE
                   END CASE
                END

                *** If the Order Date is outside our Start or End Dates
                *** then skip it...
                ORDER.DATE = ORDER.DATES<1,GEN>
                IF ORDER.DATE < SD OR ORDER.DATE > ED THEN CONTINUE
                PRC.BR  = LED(2)<1,GEN,1>
                LOCATE PRC.BR IN BRCHS<1> SETTING XX ELSE CONTINUE
                *** Filter against any 'Additional Select' options...
                IF FILTER.FLAG THEN
                   FILTER.SELECT SKIP.FLAG,OID,GEN,FITEM
                   IF SKIP.FLAG THEN CONTINUE
                END

                *** Filter each of the Line Items on this Order Gen...
                LDIDS = LED(48)<1,GEN>
                LD.CT = DCOUNT(LDIDS,SVM)
*** FOR each Line Item on the order...
                FOR LDN = 1 TO LD.CT
                   SRT.BRK = ''
                   LDID = LDIDS<1,1,LDN>
                   LD.GET LDID
                   PN = LD(1)
                   MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ''

                   IF NOT(NUM(PN)) OR PN = ''      THEN CONTINUE

                   PRC.LN  = PRD(9)
                   BUY.LN  = PRD(12)
                   PRD.BR.GET.VAL PRC.BR,PN,24,SEL.GRP
                   PRD.BR.GET.VAL PRC.BR,PN,23,BUY.GRP
                   CCODE = PRD(19)

                   *** Filter against selection list criteria
                   IF SEL.CT THEN
                      BEGIN CASE
                      CASE SLCT = 'Product'
                         LOCATE PN IN SEL.LIST<1> SETTING XX ELSE CONTINUE
                         SRT.BRK = PN
                      CASE SLCT = 'Price Line'
                         LOCATE PRC.LN IN SEL.LIST<1> SETTING XX ELSE CONTINUE
                         SRT.BRK = PRC.LN
                      CASE SLCT = 'Buy Line'
                         LOCATE BUY.LN IN SEL.LIST<1> SETTING XX ELSE CONTINUE
                        SRT.BRK = BUY.LN
                      CASE SLCT = 'Sell Group'
                         LOCATE SEL.GRP IN SEL.LIST<1> SETTING XX ELSE CONTINUE
                         SRT.BRK = SEL.GRP
                      CASE SLCT = 'Buy Group'
                         LOCATE BUY.GRP IN SEL.LIST<1> SETTING XX ELSE CONTINUE
                         SRT.BRK = BUY.GRP
                      CASE SLCT = 'Product Select Code'
                         LOCATE CCODE IN SEL.LIST<1> SETTING XX  ELSE CONTINUE
                         SRT.BRK = CCODE
                      END CASE
                   END

                   *** If they set the 'Nonstocks' option to
                   *** something other than 'Include'...
                   IF SEL.NSTKS # 'I' THEN
                      STOCK = PRD(3)

                      *** If they want to 'Exclude' Nonstocks...
                      IF SEL.NSTKS = 'E' THEN
                         *** If this is a Nonstock...
                         IF STOCK = 2 THEN CONTINUE
                      *** If they Only want Nonstocks...
                      END ELSE
                         IF STOCK # 2 THEN CONTINUE
                      END
                   END

                   *** If we need to convert our currency on the report...
                   IF CURR.FLG THEN
                      CONV.CURR.LD TGT,GEN
                   END

                   *** If the User hasn't selected certain Product
                   *** location types...
                   IF SEL.TYPS = '' THEN
                      OQTY = -SUM(LD(5)<1,GEN>) + -SUM(LD(6)<1,GEN>)
                      *** Filter Credits...
                      BEGIN CASE
                      CASE SEL.CREDS = 'O'
                         IF OQTY >= 0 THEN CONTINUE
                      CASE SEL.CREDS = 'E'
                         IF OQTY < 0 THEN CONTINUE
                      END CASE

                      *** We passed all are filters so we can
                      *** go add this one for the report...
                      GOSUB ADD.ID
                   END ELSE
                      *** Filter against the product location...
                      TYP.LOCS = LD(7)<1,GEN>
                      LOC.CT   = DCOUNT(TYP.LOCS,SVM)
                      FOR JJ   = 1 TO LOC.CT
                         TYP   = FIELD(TYP.LOCS<1,1,JJ>,'~',1)
                         LOCATE TYP IN SEL.TYPS<1> SETTING POS THEN
                            OQTY = -LD(5)<1,GEN,JJ> + -LD(6)<1,GEN,JJ>

                            BEGIN CASE
                            CASE SEL.CREDS = 'O'
                               IF OQTY >= 0 THEN CONTINUE
                            CASE SEL.CREDS = 'E'
                               IF OQTY < 0 THEN CONTINUE
                            END CASE

                            *** We passed all are filters so we can
                            *** go add this one for the report...
                            GOSUB ADD.ID
                         END
                      NEXT JJ
                   END
                NEXT LDN
             NEXT GEN
          REPEAT

          RETURN
*-------------------------------------------------------------------------*
ADD.ID:   *** Add this Ordered Product to our report data...

          ID   = OID:'.':LDID
          PRC  = LD(8)<1,GEN>        ;*Sell price
          PROV = (LD(9)<1,GEN> # '') ;*Override

          *** If the User wants to see Comm Cost...
          IF COST.TYPE = '1' THEN
             CST  = LD(27)<1,GEN>
             CSOV = (LD(28)<1,GEN> # '')
          *** If the User wants to see COGS...
          END ELSE
             CST  = LD(10)<1,GEN>
             CSOV = (LD(11)<1,GEN> # '')
          END

          READV PRC.LN FROM PRDFILE,PN,9 ELSE PRC.LN = ''
          READV PRC.LN.DESC FROM PLNEFILE,PRC.LN,1 ELSE PRC.LN.DESC = ''

          BEGIN CASE
          CASE LEVEL[1,1] = 'B'
             CN   = LED(1)<1,GEN>
          CASE LEVEL[1,1] = 'S'
             CN   = LED(5)<1,GEN>
          CASE LEVEL[1,1] = 'P'
             READV PID FROM CUSFILE,LED(1)<1,GEN>,80 ELSE PID = ''
             IF PID = '' THEN
                CN  = LED(1)<1,GEN>
             END ELSE
                CN  = PID<1,1>
             END
          END CASE

          ORD.DT   = LED(4)<1,GEN>
          CUST.PO  = LED(13)<1,GEN>
          SCUST.PO = OCONV(LED(13)<1,GEN>,'MCU')

          READV CSORTBY FROM CUSFILE,CN,8 ELSE CSORTBY = ''
          CSORTBY2 = CSORTBY:'@':CN
          CSORTBY  = CSORTBY:'@':CN:'@':PRC.LN
          ** If we are sorting by branch then we sort by priceline
          ** after sorting by branch.  We make room for the full
          ** full priceline desc on the report, so we need to
          ** sort by the full description.
          IF SEL.SORTBY = 'Branch' AND PRC.LN.DESC # '' THEN
             IF SRT.BRK = '' THEN SRT.BRK = PRC.LN.DESC
          END ELSE
             IF SRT.BRK = '' THEN SRT.BRK = PRC.LN
          END

          *** Set up the TID based on the SEL.SORTBY
          TID = ''
          BEGIN CASE
          *** Branch...
          CASE SEL.SORTBY = 'Branch'
             TID    = PRC.BR "R%4":'|':SRT.BRK:' '
          *** Customer PO#...
          CASE PO.FLAG
             TID    = SCUST.PO:' |':CSORTBY
          *** Buy Line...
          CASE SEL.SORTBY = 'Buy Line'
             READV DESC FROM PRDFILE,PN,1 ELSE DESC = ''
             TID    = BUY.LN:' |':DESC<1,1>
          *** Price Line...
          CASE SEL.SORTBY = 'Price Line'
             READV SEQ FROM PRDFILE,PN,20 ELSE SEQ=''
             TID    = PRC.LN:' |':SEQ
          *** State...
          CASE SEL.SORTBY = 'State'
             *Check for non USA zipcode
             ZIP    = LED(75)<1,GEN>
             IF LEN(FIELD(ZIP,'-',1))=5 AND NUM(ZIP[1,5]) THEN ZIP=ZIP[1,5]
             READV ST.CODE FROM ZIPFILE,ZIP,1 ELSE ST.CODE = ''
             TID    = ST.CODE
          *** Select Code...
          CASE SEL.SORTBY = 'Select Code'
             READV SEL.CODE FROM CUSFILE,CN,78 ELSE SEL.CODE = ''
             IF SEL.CODE = '' THEN SEL.CODE='** NF: ':CN
             TID    = SEL.CODE
          *** Product Select Code...
          CASE SEL.SORTBY[1,14] = 'Product Select'
             READV PSEL.CODE FROM PRDFILE,PN,19 ELSE PSEL.CODE=''
             IF PSEL.CODE = '' THEN PSEL.CODE='** NF: ':PN
             TID    = PSEL.CODE
          *** 3 Digit Zip...
          CASE SEL.SORTBY[1,1] = '3'
             IF SEL.CT = 1 THEN
                TID = LED(75)<1,GEN>[1,3]
             END ELSE
                TID = LED(75)<1,GEN>[1,3]:'-':SRT.BRK
             END
          *** Zip Code...
          CASE SEL.SORTBY[1,3] = 'Zip'
             TZIP   = LED(75)<1,GEN>
             IF LEN(FIELD(TZIP,'-',1))=5 AND NUM(TZIP[1,5]) THEN TZIP = TZIP[1,5]
             IF SEL.CT = 1 THEN
                TID = TZIP
             END ELSE
                TID = TZIP:'-':SRT.BRK
             END
          *** Product...
          CASE SEL.SORTBY = 'Product'
             READV DESC FROM PRDFILE,PN,1 ELSE DESC = ''
             *** Our description might be VM delimited, which will cause
             *** us problems. Therefor we'll just use the first value...
             DESC = DESC<1,1>
             IF SEL.CT > 1 AND SRT.BRK # 'PN' THEN
                TID = DESC:' |':PN:'|':SRT.BRK
             END ELSE
                TID = DESC:' |':PN
             END
          *** Writer...
          CASE SEL.SORTBY = 'Writer'
             TID    = LED(73)<1,GEN>:' |':CSORTBY
          *** Out Salesperson or Out Salesperson by Customer...
          CASE SEL.SORTBY[1,3] = 'Out'
             TID    = LED(72)<1,GEN>:' |':CSORTBY
          *** In Salesperson or In Salesperson by Customer
          CASE SEL.SORTBY[1,2] = 'In'
             TID    = LED(34)<1,GEN>:' |':CSORTBY
          CASE SEL.SORTBY[1,6] = 'Sell G'
             READV DESC FROM PGRPFILE,SEL.GRP,1 ELSE DESC = ''
             TID    = SEL.GRP:' |':DESC<1,1>
          CASE SEL.SORTBY[1,6] = 'Buy Gr'
             READV DESC FROM PGRPFILE,BUY.GRP,1 ELSE DESC = ''
             TID    = BUY.GRP:' |':DESC<1,1>
          *** Customer...
          CASE OTHERWISE
             READV DESC FROM PRDFILE,PN,1 ELSE DESC = ''
             TID    = CSORTBY:'|':DESC<1,1>:' |':PN
             IF SEL.CT > 1 THEN
                TID := '|':SRT.BRK
             END
          END CASE

          TID := ' |~':CN
          IF DETAIL THEN TID := '~':ID

          READ TMP FROM TEMPFILE,TID ELSE TMP = ''

          TMP<1,1> += ICONV(OCONV(OQTY*PRC,'MR9'),'MR2')
          TMP<1,2> += ICONV(OCONV(OQTY*CST,'MR9'),'MR2')

          MATREAD PLNE FROM PLNEFILE,PRC.LN ELSE MAT PLNE = ''
          IF PRD(15) # '' THEN UM.TBL = PRD(15) ELSE UM.TBL = PLNE(3)
          IQ.TO.ALPHA UM.TBL,PRD(7),LD(23),OQTY,Q1,U1,Q2,U2,ALPHA
          IF Q2 > 0 THEN
             TUM = U2
             IQ.TO.ALPHA UM.TBL,PRD(7),TUM,OQTY,Q1,U1,Q2,U2,ALPHA
          END

          PQTY = Q1
          PUM  = U1
          TMP<1,3> += PQTY

          IF NOT(DETAIL) THEN
             ***If from multiple PNs or UMs, UoM will not be printed
             ***in the totals line; instead a '**' will
             LOCATE PN IN TMP<2,1> SETTING POS ELSE
                IF POS < 3 THEN TMP = INSERT(TMP,2,1,POS;PN)
             END
             LOCATE PUM IN TMP<3,1> SETTING POS ELSE
                IF POS < 3 THEN TMP = INSERT(TMP,3,1,POS;PUM)
             END
          END ELSE
             TMP<2,1> = PN
             TMP<3,1> = PUM
             TMP<4,1> = ORD.DT
             TMP<4,2> = PROV
             TMP<4,3> = CSOV
             TMP<4,4> = CUST.PO
             TMP<4,5> = PRC.BR
          END

          WRITE TMP ON TEMPFILE,TID

          RETURN
*-------------------------------------------------------------------------*
GET.DESC: ***Get description for Branch sort, summary print
          PRT.DESC = ''

          BEGIN CASE
             CASE SRT.BRK = 'PN'
                READV PRT.DESC FROM PRDFILE,PREV.TD2,1 ELSE PRT.DESC = ''
             CASE SRT.BRK = PRC.LN
                READV PRT.DESC FROM PLNEFILE,PREV.TD2,1 ELSE PRT.DESC = ''
             CASE SRT.BRK = BUY.LN
                READV PRT.DESC FROM BLNEFILE,PREV.TD2,1 ELSE PRT.DESC = ''
             CASE SRT.BRK = SEL.GRP OR SRT.BRK = BUY.GRP
                READV PRT.DESC FROM PGRPFILE,PREV.TD2,1 ELSE PRT.DESC = ''
             CASE SRT.BRK = CCODE
                PRT.DESC = PREV.TD2
          END CASE

          IF PRT.DESC = '' THEN PRT.DESC = '* Not Found *'

          RETURN
*-------------------------------------------------------------------------*
!SMITJR~07/16/10~09:38
