          PH.ARGS DRPT,CT.ID,START.ID,END.ID,SORT,GBASIS,SEL.PLNE,PRC.DATE,SUM.DET,NULL0,TAGS,DOL.VAR,EXPR,PRCT.VAR,SORT.POS
** Version# 71.0002[3] - 10/28/2010 - 04:55pm - SMITJR - eclipse
*** V71.0002 Change - Custom Coding .. - 10/28/2010 - SMITJR - eclipse
** Copied from BP PHYS.PHR.VARIANCE Version# 71 - 10/10/2008 - 03:27pm - FHELLER - main

*** Subroutine - PHYS.PHR.VARIANCE
*-------------------------------------------------------------------------*
*** This routine creates the Count Variance Report, which recaps full
*** physical inventory counts and displays the on-hand quantities
*** presently shown in the system.
*-------------------------------------------------------------------------*
*** DRPT     - Parameters for how user wants the report to be run.    (IN)
*** CT.ID    - Count ID.                                              (IN)
*** START.ID - The first control # user would like to see on report.  (IN)
*** END.ID   - The last control # user would like to see on report.   (IN)
*** SORT     - How the file being SORT-selected WILL GET selected,
***            by Ctrl# or by Prc Line.                               (IN)
*** GBASIS   - Global Basis used to set how the inventory will be
***            valued, for evalutaing the differences on the report.  (IN)
*** SEL.PLNE - Price Line file is to be sort-selected by if user
***            chooses 'Prc Line' as their SORT.                      (IN)
*** PRC.DATE - The date of the count.                                 (IN)
*** SUM.DET  - The way the report will be printed
***            'Summary/Detail/Variance Only/Product Variance Only'   (IN)
*** NULL0    - <1,1> Boolean value for whether user wishes to displqy null
***            entries on the report                                  (IN)
***            <1,2> Boolean value for PostDated Items Picked         (IN)
*** TAGS     - How user wants to treat 'non-S tagged locations' on
***            the report. 'Include/Exclude/Only'                     (IN)
***
*** The next THREE parameters are used to control the range of allowable
*** variance for items appearing ont he report.
***
*** DOL.VAR  - A minimum variance in dollars                          (IN)
*** EXPR     - This will be an 'And/Or' value, passed in when both
***            the DOL.VAR and PRCT.VAR values are set.               (IN)
*** PRCT.VAR - A minimum variance by percentage.
***
*** SORT.POS - An additional assending/descending Sort.
***            'By-Asnd Ctrl# / By-Dsnd Count / By-Dsnd Count$ / By
***             Product by Loc / By-Dsnd Cnt Diff / By-Dsnd $ Diff'
*-------------------------------------------------------------------------*
*** COMMON VARIABLES USED:
*** PRD  - PRODUCT File
*** PLNE - PRICE.LINE File
*-------------------------------------------------------------------------*
          OPEN '&SAVEDLISTS&' TO SLFILE ELSE STOP

          *** Translate Global Basis number
          GLOBAL.BNAME.GET 0,GBNAMES
          LOCATE GBASIS IN GBNAMES<1> SETTING GBASN ELSE GBASN = 0
          IF GBASIS = '' THEN GBASN = 0

          SV.EXPR = EXPR
          EXPR    = TRIM(OCONV(EXPR,'MCU'))
          IF EXPR = '' THEN EXPR = 'AND'

          SV.UM   = ''

          PHYS.ID = 'PHYS:':CT.ID"R%5"
          OPEN PHYS.ID TO PHYSFILE ELSE
             SEND.MESSAGE 'Phantom',USER.ID,'Count Control File not Found : ':PHYS.ID
             STOP
          END

          POST.DATED.PICKED = NULL0<1,2>
          NULL0             = NULL0<1,1>

          *** Check variance ALWAYS for Location/Product, only when
          *** specified for Detail/Summary
          VAR.FLAG = YES
          IF SUM.DET[1,1] = 'D' OR SUM.DET[1,1] = 'S' THEN
             IF DOL.VAR ='' AND PRCT.VAR ='' THEN VAR.FLAG = NO
          END

          READ PHYS FROM WORKFILE,PHYS.ID ELSE PHYS = ''
          BR      = PHYS<2>
          DFLT.DT = PHYS<7>

          SUM.DET.TITLE = SUM.DET
          SUM.DET = SUM.DET[1,1]
          POST.DET= (PHYS<10>[1,1] = 'D')

          MSG = 'Initializing'
          GOSUB WRT.STAT

          BEGIN CASE
          CASE SUM.DET = 'L'; VAR.HDG = 'Location Variance'
          CASE SUM.DET = 'P'; VAR.HDG = 'Product Variance'
          CASE SUM.DET = 'S'; VAR.HDG = 'Variance Summary'
          CASE SUM.DET = 'D'; VAR.HDG = 'Variance Detail'
          END CASE

          IF NULL0 THEN NULL.INFO = 'Y' ELSE NULL.INFO = 'N'

          BEGIN CASE
          CASE SORT.POS = 1 ;SORT.NAME = 'Asnd Ctrl#'
          CASE SORT.POS = 2 ;SORT.NAME = 'Dsnd Count'
          CASE SORT.POS = 3 ;SORT.NAME = 'Dsnd Count$'
          CASE SORT.POS = 4 ;SORT.NAME = 'Prod by Loc'
          CASE SORT.POS = 5 ;SORT.NAME = 'Dsnd Cnt Diff'
          CASE SORT.POS = 6 ;SORT.NAME = 'Dsnd $ Diff'
          END CASE

          IF DCOUNT(SEL.PLNE,VM) > 1 THEN
             PLINE.MSG = "*Multi*"
          END ELSE
             PLINE.MSG = SEL.PLNE
          END

          IF SORT = 2 THEN
             PLNE.CT = DCOUNT(SEL.PLNE,VM)
             IF PLNE.CT <= 6 AND PLNE.CT # 0 THEN
                DISP.PLNES = SEL.PLNE
                CONVERT VM TO ',' IN DISP.PLNES
                RANGE.INFO = ('Price Line : ':DISP.PLNES)
             END ELSE
                RANGE.INFO = ('Price Line : ':'*Multi*')
             END
          END ELSE
             RANGE.INFO = ('Control #s : ':START.ID:' to ':END.ID)
          END

          HDR       = ''
          HDR<1,2>  = 'Count Date : ':OCONV(DFLT.DT,'D4/')
          HDR<1,2> := '  ':TAGS"R#7":' Tagged Locations  Nulls to 0 : '
          HDR<1,2> := NULL.INFO

          BASIS.HDR = 'Valued at : '
          IF GBASN THEN
             BASIS.HDR := GBASIS
             BASIS.HDR := ' - Prc Date : ':OCONV(PRC.DATE,'D4/')
          END ELSE
             BASIS.HDR := 'No Cost Basis Selected'
          END

          HDR<1,3> = RANGE.INFO
          IF DOL.VAR OR PRCT.VAR THEN
             HDR<1,3> := 'Variance Greater Than : $':DOL.VAR
             HDR<1,3> := ' ':SV.EXPR:' ':PRCT.VAR:'%'
          END

          HDR<1,3> := '   Sort by : ':SORT.NAME
          HDR<1,3> := '   ':BASIS.HDR

          HDR<1,4>  = 'Ctrl#  Product Description                 Prc Line'
          HDR<1,4> := ' Location       UM        Count       Onhand'
          HDR<1,4> := '         Diff    Diff%'

          IF GBASN THEN
             HDR<1,4> := '        Unit$         Count$        Onhand$    '
             HDR<1,4> := 'Difference$'
          END

          HDR<1,5> = '------ ----------------------------------- --------'
          HDR<1,5> := ' -------------- -- ------------ ------------ '
          HDR<1,5> := '------------ --------'

          IF GBASN THEN
             HDR<1,5> := ' ------------  ------------- -------------- '
             HDR<1,5> := '--------------'
          END

          TITLE = PHYS<1>:' ':SUM.DET.TITLE:' Variance Report'

          WDTH = LEN(HDR<1,4>)

          *** Now that we know the width of our report, we'll build the
          *** first line of the report heading...
          COUNT.ID.HDR = 'Count ID #':CT.ID"R%5":'   ':PHYS<1>
          COUNT.ID.HDR.LNGTH = LEN(COUNT.ID.HDR)

          DETAIL.HDR   = '  *** ':SUM.DET.TITLE:' Variance Report For Br '
          DETAIL.HDR  := '# ':BR:' ***'
          DETAIL.HDR.LNGTH = LEN(DETAIL.HDR)

          PAGE.NUM = 'Page : ^#####'

          SPACES.LEFT = WDTH - COUNT.ID.HDR.LNGTH - DETAIL.HDR.LNGTH - 13

          *** If we have an even number of spaces to print in between
          *** the Detail info portion and the rest of what we want to
          *** put on the same line...
          IF MOD(SPACES.LEFT,2) = 0 THEN
             SPACES1 = SPACE(SPACES.LEFT / 2)
          END ELSE
             SPACES1 = SPACE(FIELD((SPACES.LEFT / 2),'.',1) + 1)
          END

          SPACES2 = SPACE(FIELD((SPACES.LEFT / 2),'.',1))

          HDR<1,1> = COUNT.ID.HDR:SPACES1:DETAIL.HDR:SPACES2:PAGE.NUM

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

          LAST.PLINE = '@@'
          ITOL.OH    = '';     TOL.OH     = '';     GTOL.OH     = ''
          ITOL.CNT   = '';     TOL.CNT    = '';     GTOL.CNT    = ''
          ITOL.DIFF  = '';     TOL.DIFF   = '';     GTOL.DIFF   = ''
          ITOL.GDIFF = '';     TOL.GDIFF  = '';     GTOL.GDIFF  = ''
          ITOL.OH$   = '';     TOL.OH$    = '';     GTOL.OH$    = ''
          ITOL.CNT$  = '';     TOL.CNT$   = '';     GTOL.CNT$   = ''
          ITOL.DIFF$ = '';     TOL.DIFF$  = '';     GTOL.DIFF$  = ''
          ITOL.GDIFF$= '';     TOL.GDIFF$ = '';     GTOL.GDIFF$ = ''

          CT             = 0
          DET.PRINTED.CT = 0
          PRD.SAVED      = NO
          TAGS = TAGS[1,1]
          LAPN = '@@@'
          *
          * If By-Dsnd Qty or Value sort IDs
          *
          BEGIN CASE
          CASE SORT = 1
             STARTX  = START.ID
             ENDX    = END.ID
             ID.LIST = ''

             CTRX = 0
             FOR S = STARTX TO ENDX
                CTRX += 1
                ID.LIST<CTRX> = S
             NEXT S

             WRITE ID.LIST ON SLFILE,'ID.LIST':PID$
             EXEC = 'GET-LIST ID.LIST':PID$
             GOSUB SELECT.ITEMS
             GOSUB SORT.IDS

             LOOP
                READNEXT CN FROM IN.LIST ELSE EXIT
                GOSUB PRT.SHEET
             REPEAT

          CASE SORT = 2
             FF.ADD = NO
             GOSUB SEL.IDS

             LOOP
                READNEXT CN FROM IN.LIST ELSE EXIT
                GOSUB PRT.SHEET
             REPEAT

             IF SUM.DET = 'P' OR SORT.POS = 4 THEN
                GOSUB PROD.TOL
             END

             GOSUB PLINE.TOL
          END CASE

          GOSUB GTOL

          PRINTER.OFF DOC.ID

          MSG = PHYS<1>:' Variance Report Complete - ':CT:' Items Printed'
          SEND.MESSAGE 'Phantom',USER.ID,MSG
          UT.PH.CLEANUP
          DELETE SLFILE,'ID.LIST':PID$

          STOP
*-------------------------------------------------------------------------*
PRT.SHEET:*** Print Sheet
          READ CREC FROM PHYSFILE,CN ELSE CREC = ''
          PN      = CREC<1>
          * Do not include blank count sheets in the final report
          IF PN = '' THEN RETURN

          LOC     = CREC<2>
          CNT.QTY = CREC<3>
          CNT.DT  = CREC<4>
          NEW.PN  = CREC<5>
          UPD.QTY = CREC<6>
          SV.UM   = UM.PER
          UM.PER  = CREC<8>

          IF LOC    = '' THEN LOC    = 'S~'
          IF CNT.DT = '' THEN CNT.DT = DFLT.DT

          TAGGED = (FIELD(LOC,'~',3)#'' OR INDEX(LOC,'^',1))

          BEGIN CASE
          CASE TAGS = 'E' AND TAGGED      ; RETURN
          CASE TAGS = 'O' AND NOT(TAGGED) ; RETURN
          END CASE

          IF SUM.DET = 'P' OR SORT.POS = 4 THEN
             IF PN # LAPN THEN
                IF LAPN # '@@@' THEN
                   GOSUB PROD.TOL
                END
                PROD.OK = YES
                PRINT.PROD.TOTALS = NO
             END ELSE
                * Check variance when sorted by product by location
                * and report is either detail or location variance.
                IF SORT.POS = 4 AND (SUM.DET = 'D' OR SUM.DET = 'L') THEN
                   PROD.OK = YES
                END
             END
          END ELSE
             PROD.OK = YES
          END

          GOSUB GET.MAX.UM
          LAPN = PN
          READV DESC FROM PRDFILE,PN,1 ELSE DESC = '.':PN:' ** NOT IN PRODUCT FILE **'; CNT.QTY = ''
          READV PLINE FROM PRDFILE,PN,9 ELSE PLINE = ''
          IF SORT = 2 THEN
             IF PLINE # LAST.PLINE THEN
                IF LAST.PLINE#'@@' THEN
                   GOSUB PLINE.TOL
                END
                LAST.PLINE = PLINE
             END
          END

          IF PN # '' THEN
             IF CNT.DT # '' THEN
                *** Loop through prod's CNs to find the first with the
                *** same loc. If it was added to an earlier CN do not
                *** add it again.
                READ XREF FROM PHYSFILE,'~':PN ELSE XREF = ''
                XCT  = DCOUNT(XREF<1>,VM)
                QPOS = 0

                FOR J = 1 TO XCT
                   IF LOC = XREF<1,J> THEN
                      QPOS = J
                      EXIT
                   END
                NEXT J

                IF QPOS # 0 AND XREF<2,QPOS> # CN THEN
                    *** If we are looking for a dollar or percent varian
                    *** don't print lines where quantity was added to a
                    *** different control record.
                    IF DOL.VAR = '' AND PRCT.VAR = '' THEN
                       IF SUM.DET # 'S' THEN
                          PRINT CN             "R%6":' ':
                          PRINT DESC<1,1>      "L#35":' ':
                          PRINT PLINE          "L#8":' ':
                          PRINT LOC            "L#14":' ':
                          IF NOT(NULL0) AND CNT.QTY = '' THEN
                             PRINT SPACE(8):'*NoCnt*':' ':
                          END ELSE
                             PRINT UM.PER             "R#2":' ':
                             PRINT CNT.QTY/UM.QTY     "R2#12":' ':
                          END
                          ADD.QTY = '      *** Qty added to Ctrl # :'
                          ADD.QTY := XREF<2,QPOS>"R%6":' **'
                          PRINT ADD.QTY:
                          PRINT SPACE(85):'^':FIELD(PRD(4),' ', 2)[1,7] "L#7"
                       END
                   END
                   IF CNT.QTY # '' THEN
                      SP.QTY = CNT.QTY
                   END

                   RETURN
                END
             END

             OH      = 0
             PRD.OH  = 0
             PRD.QTY = 0
             OH.LOCS = ''
             IF CNT.DT # '' THEN
                GET.PREV.ONHANDS BR,PN,CNT.DT,OH.QTYS,OH.LOCS,POST.DATED.PICKED,ORD.IDS

                OHCT = DCOUNT(OH.QTYS,VM)

                FOR POS = 1 TO OHCT
                   IF LOC = OH.LOCS<1,POS> THEN OH = OH.QTYS<1,POS>; EXIT
                NEXT POS

**              OH += UPD.QTY

                AOE.QTY = 0
                PRD.QTY = 0
                PRD.OH  = 0

                LCT = DCOUNT(XREF<1>,VM)

                FOR L = 1 TO LCT
                   IF SUM.DET = 'P' THEN
                      LOC.LOC = XREF<1,L>
                      LOC.OH  = 0
                      FOR POS = 1 TO OHCT
                         IF LOC.LOC = OH.LOCS<1,POS> THEN
                            LOC.OH = OH.QTYS<1,POS>; EXIT
                         END
                      NEXT POS
                      PRD.OH += LOC.OH
                   END

                   *** If this is the first CN for this prod/loc and has
                   *** a valid date, add other location qtys to it.
                   IF XREF<1,L> = LOC AND XREF<2,L> # CN THEN
                      READ CREC2 FROM PHYSFILE,XREF<2,L> ELSE CREC2 = ''
                      ADD.QTY  = CREC2<3>
                      AOE.QTY += CREC2<6>
                      IF ADD.QTY#'' THEN
                         CNT.QTY += ADD.QTY
                         SP.QTY   = ADD.QTY
                         ADD.QTY  = ADD.QTY/UM.QTY
                         DESC<1,-1> = '** Ctrl #':XREF<2,L>"R%6":' - Addl Qty : ':ADD.QTY"R#5":UM.PER"L#2":' **'
                      END
                   END
                NEXT L

             END

             NO.CNT = (CNT.QTY='')
             IF NO.CNT THEN IF NULL0 THEN CNT.QTY=0 ELSE CNT.QTY=OH
             OH.DIFF = CNT.QTY - OH
             LOCATE PN IN PRD.IDS<1> SETTING POS THEN
                PRD.QTY = PRD.CNTS<1,POS>
             END
             PRD.DIFF = PRD.QTY - PRD.OH

             IF OH+0=0 THEN
                IF OH.DIFF  = 0 THEN OH.QPERC  = 0 ELSE OH.QPERC  = '**'
                IF PRD.DIFF = 0 THEN PRD.QPERC = 0 ELSE PRD.QPERC = '**'
             END ELSE
                OH.QPERC  = (OH.DIFF  / OH) * 100
                IF PRD.OH > 0 THEN
                   PRD.QPERC = (PRD.DIFF / PRD.OH) * 100
                END ELSE
                   PRD.QPERC = 0
                END
             END
             SP.QTY = CNT.QTY
             SP.QTY = OH
             SP.QTY = OH.DIFF
          END ELSE
             OH       = ''
             OH.DIFF  = ''
             PRD.DIFF = ''
             CNT.QTY  = ''
             PRD.QTY  = ''
             UM.PER   = ''
             OH.LOCS  = ''
          END

          IF GBASN THEN
             GET.ALL.PRD BR,PN
             GLOBAL.BASN.GET GBASN,BASN

             KOPTS = PRD(86)
             IF GBASN=3 AND KOPTS<1,3>#'' THEN KOPTS<1,1>= KOPTS<1,3>

             GET.BASE PN,BR,BASN,PRC.DATE,BASE,PER,KOPTS,PRD(52),PRD(53),PRD(87)
             BASE = (OCONV(BASE,'MR3')/PER)*UM.QTY
          END ELSE
             BASE = 0
          END
          GOSUB CHECK.VARIANCE
          IF PROD.OK AND SUM.DET # 'S' THEN
             GOSUB PRT.DET
          END

          IF CNT.DT # '' AND PROD.OK THEN
             IF GBASN THEN
                ITOL.OH$    += BASE*(OH/UM.QTY)
                ITOL.CNT$   += BASE*(CNT.QTY/UM.QTY)
                ITOL.DIFF$  += BASE*(OH.DIFF/UM.QTY)
                ITOL.GDIFF$ += ABS(BASE*(OH.DIFF/UM.QTY))
                TOL.OH$     += BASE*(OH/UM.QTY)
                TOL.CNT$    += BASE*(CNT.QTY/UM.QTY)
                TOL.DIFF$   += BASE*(OH.DIFF/UM.QTY)
                TOL.GDIFF$  += ABS(BASE*(OH.DIFF/UM.QTY))
                GTOL.CNT$   += BASE*(CNT.QTY/UM.QTY)
                GTOL.OH$    += BASE*(OH/UM.QTY)
                GTOL.DIFF$  += BASE*(OH.DIFF/UM.QTY)
                GTOL.GDIFF$ += ABS(BASE*(OH.DIFF/UM.QTY))
             END

             ITOL.OH    += OH/UM.QTY
             ITOL.CNT   += CNT.QTY/UM.QTY
             ITOL.DIFF  += OH.DIFF/UM.QTY
             ITOL.GDIFF += ABS(OH.DIFF/UM.QTY)
             TOL.OH     += OH/UM.QTY
             TOL.CNT    += CNT.QTY/UM.QTY
             TOL.DIFF   += OH.DIFF/UM.QTY
             TOL.GDIFF  += ABS(OH.DIFF/UM.QTY)
             GTOL.CNT   += CNT.QTY/UM.QTY
             GTOL.OH    += OH/UM.QTY
             GTOL.DIFF  += OH.DIFF/UM.QTY
             GTOL.GDIFF += ABS(OH.DIFF/UM.QTY)
          END
SKIP.PRT:    *
          IF MOD(CT,50) = 0 THEN
             MSG = 'Spooling Report : ':CT
             GOSUB WRT.STAT
          END

          RETURN
*-------------------------------------------------------------------------*
PRT.DET:  ***
          IF NEW.PN THEN DESC = '+':DESC

          DET.PRINTED.CT += 1
          CT += 1
          PRINT CN                            "R%6":' ':
          PRINT DESC<1,1>                     "L#35":' ':
          PRINT PLINE                         "L#8":' ':
          PRINT LOC                           "L#14":' ':
          PRINT UM.PER                        "R#2":' ':
          IF CNT.DT='' THEN
             PRINT '** No Count Date - Value Not added to Totals **'
          END ELSE
             PRINT.PROD.TOTALS = YES
             IF NO.CNT AND NOT(NULL0) THEN
                PRINT SPACE(5):'*NoCnt*':' ':
             END ELSE
                PRINT CNT.QTY/UM.QTY          "R2#12":' ':
             END
             PRINT OH/UM.QTY                  "R2#12":' ':
             PRINT OH.DIFF/UM.QTY             "R2#12":' ':
             IF OH.DIFF#0 THEN
                PRINT OH.QPERC                "R1#7":'%':' ':
             END ELSE
                PRINT ''                      "L#8":' ':
             END
             IF GBASN THEN
                PRINT BASE                    "R2#12":' ':
                PRINT BASE*(CNT.QTY/UM.QTY)   "R2#14":' ':
                PRINT BASE*(OH/UM.QTY)        "R2#14":' ':
                PRINT BASE*(OH.DIFF/UM.QTY)   "R2#14":' ':
             END ELSE
                PRINT SPACE(45):
             END
             PRINT SPACE(40):'^':FIELD(PRD(4),' ', 2)[1,7] "L#7" "L#7":
             PRINT
             IF ORD.IDS THEN
                IF POST.DET THEN
                   PRINT SPACE(68):'PostDated Invoices:' "L#23":'ShipQty' "L#7"
                END
                TOT.QTY = 0
                DCNT = DCOUNT(ORD.IDS,AM)
                FOR D = 1 TO DCNT
                   AR.ID = FIELD(ORD.IDS<D,1>,'~',1)
                   LDID  = FIELD(ORD.IDS<D,1>,'~',2)
                   OID   = FIELD(AR.ID,'.',1)
                   GID   = FIELD(AR.ID,'.',2) + 0
                   MATREAD LED FROM LEDFILE,OID ELSE MAT LED = ""
                   LOCATE GID IN LED(12)<1> SETTING GEN ELSE GEN = 1
                   INVN = LED(8)<1,GEN>
                   TOID = OID:'.':INVN "R%3"
                   SQTY  = ORD.IDS<D,2> + 0
                   TOT.QTY = TOT.QTY + SQTY
                   IF POST.DET THEN
                      PRINT SPACE(70):TOID "L#21":SQTY "R#6"
                   END
                NEXT D
                IF POST.DET THEN
                   PRINT SPACE(81):'Total: '"L#10":TOT.QTY 'R#6'
                END ELSE
                   PRINT 'PostDated Invoices: ' "R#91":TOT.QTY 'R#6'
                END
                PRINT
             END
          END

          XCT   = DCOUNT(DESC,VM)
          FOR J = 2 TO XCT
             IF DESC<1,J> # '' THEN
                PRINT SPACE(7):DESC<1,J>               "L#40"
             END
          NEXT J

          LCT = DCOUNT(OH.LOCS,VM)

          FOR LCN = 1 TO LCT
             IF OH.LOCS<1,LCN>[9,1]='.' THEN
                SP.QTY = OH.QTYS<1,LCN>
                PRINT SPACE(10):'*** Tag : ':OH.LOCS<1,LCN>"L#8":' - Qtys : ':SP.QTY/UM.QTY:UM.PER"L2":' ***'
             END
          NEXT LCN

          RETURN
*-------------------------------------------------------------------------*
PROD.TOL: *** Totals for Product
          IF PRINT.PROD.TOTALS THEN
             PRINT ' *** Totals for Product: ':DESC<1,1>"L#38":' -- ':

             PRINT SV.UM                   "R#2":' ':
             PRINT ITOL.CNT                "R2#12":' ':
             PRINT ITOL.OH                 "R2#12":' ':
             PRINT ITOL.DIFF               "R2#12":' ':
             IF ITOL.OH+0=0 THEN PERC=0 ELSE PERC=(ITOL.DIFF/ITOL.OH)*100
             PRINT PERC                    "R1#7":'%':' ':
             IF GBASN THEN
                PRINT ''                   "L#12":' ':
                PRINT ITOL.CNT$            "R2#14":' ':
                PRINT ITOL.OH$             "R2#14":' ':
                PRINT ITOL.DIFF$           "R2#14":' ':
             END
             PRINT
             PRINT
          END

PROD.TOL.CLR: *

          ITOL.CNT     = ''
          ITOL.OH      = ''
          ITOL.DIFF    = ''
          ITOL.GDIFF   = ''
          ITOL.CNT$    = ''
          ITOL.OH$     = ''
          ITOL.DIFF$   = ''
          ITOL.GDIFF$  = ''

          RETURN
*-------------------------------------------------------------------------*
PLINE.TOL:*** Totals for Price Line
          IF VAR.FLAG AND DET.PRINTED.CT < 1 THEN RETURN
          PRINT SPACE(70):STR('-',105)
          PRINT '         ****       Totals for Price Line : ':LAST.PLINE"L#8":'    -------    ':

          PRINT '**'                    "R#2":' ':
          PRINT TOL.CNT                 "R2#12":' ':
          PRINT TOL.OH                  "R2#12":' ':
          PRINT TOL.DIFF                "R2#12":' ':
          IF TOL.OH+0=0 THEN PERC=0 ELSE PERC=(TOL.DIFF/TOL.OH)*100
          PRINT PERC                    "R1#7":'%':' ':
          IF GBASN THEN
             PRINT ''                   "L#12":' ':
             PRINT TOL.CNT$             "R2#14":' ':
             PRINT TOL.OH$              "R2#14":' ':
             PRINT TOL.DIFF$            "R2#14":' ':
          END
          PRINT
          PRINT SPACE(58):' Gross Qty Difference --- ':SPACE(12):
          PRINT TOL.GDIFF               "R#12":' ':
          IF TOL.OH+0=0 THEN PERC = 0 ELSE PERC=(TOL.GDIFF/TOL.OH)*100
          PRINT PERC                    "R1#7":'%':'      ':
          IF GBASN THEN
             PRINT '    Gross Dollar Difference --- '   "L#38":
             PRINT TOL.GDIFF$            "R2#14":
          END
          PRINT
          PRINT

          TOL.CNT     = ''
          TOL.OH      = ''
          TOL.DIFF    = ''
          TOL.GDIFF   = ''
          TOL.CNT$    = ''
          TOL.OH$     = ''
          TOL.DIFF$   = ''
          TOL.GDIFF$  = ''
          DET.PRINTED.CT = 0
          IF SUM.DET # 'S' THEN
             PRINT CHAR(12)
          END
          IF SUM.DET = 'S' THEN CT += 1

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

          IF GBASN THEN
             PRINT SPACE(70):STR('-',105)
          END ELSE
             PRINT SPACE(70):STR('-',47)
          END

          PRINT SPACE(13):'****':SPACE(13):'Grand Totals':SPACE(9):
          PRINT '-------         ':

          PRINT '**'                    "R#2":' ':
          PRINT GTOL.CNT                "R2#12":' ':
          PRINT GTOL.OH                 "R2#12":' ':
          PRINT GTOL.DIFF               "R2#12":' ':
          IF GTOL.OH+0=0 THEN PERC=0 ELSE PERC=(GTOL.DIFF/GTOL.OH)*100
          PRINT PERC                    "R1#7":'%':' ':
          IF GBASN THEN
             PRINT ''                    "L#12":' ':
             PRINT GTOL.CNT$             "R2#14":' ':
             PRINT GTOL.OH$              "R2#14":' ':
             PRINT GTOL.DIFF$            "R2#14":' ':
          END
          PRINT
          PRINT SPACE(58):' Gross Qty Difference --- ':SPACE(12):
          PRINT GTOL.GDIFF               "R#12":' ':
          IF GTOL.OH + 0 = 0 THEN PERC = 0 ELSE PERC=(GTOL.GDIFF/GTOL.OH)*100
          PRINT PERC                    "R1#7":'%':'      ':
          IF GBASN THEN
             PRINT '    Gross Dollar Difference --- '   "L#38":
             PRINT GTOL.GDIFF$            "R2#14":
          END
          PRINT

          RETURN
*-------------------------------------------------------------------------*
GET.MAX.UM: *** Gets the UM used in Count Load-In for each product
          MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ''
          MATREAD PLNE FROM PLNEFILE,PRD(9) ELSE MAT PLNE = ''
          UOM.PER.GET CREC<8>,UM.QTY
          IF UM.QTY+0 = 0 THEN UM.QTY = 1

          RETURN
*-------------------------------------------------------------------------*
TRACK.UM: *** Keeps lists of UMs in order to use highest for totals
          LOCATE UM.PER IN BEST.PPER SETTING POS ELSE
             BEST.PPER<POS> = UM.PER
             BEST.PQTY<POS> = UM.QTY
          END
          LOCATE UM.PER IN BEST.LPER SETTING POS ELSE
             BEST.LPER<POS> = UM.PER
             BEST.LQTY<POS> = UM.QTY
          END
          LOCATE UM.PER IN BEST.GPER SETTING POS ELSE
             BEST.GPER<POS> = UM.PER
             BEST.GQTY<POS> = UM.QTY
          END
          RETURN
*-------------------------------------------------------------------------*
CHECK.VARIANCE: *** Check the product or location variance specified.
          IF VAR.FLAG THEN
             CHK.OH.QPERC = OH.QPERC
             CHK.OH.DIFF  = OH.DIFF/UM.QTY

             IF SUM.DET = 'P' THEN
                CHK.OH.QPERC = PRD.QPERC
                CHK.OH.DIFF  = PRD.DIFF/UM.QTY
             END
             IF DOL.VAR OR PRCT.VAR THEN
                IF NOT(GBASN) THEN BASE = 0
                SV.QPERC = CHK.OH.QPERC
                IF NOT(NUM(CHK.OH.QPERC)) THEN CHK.OH.QPERC = 100000
                CHK.OH.QPERC = ABS(CHK.OH.QPERC)
                BEGIN CASE
                CASE DOL.VAR  = ''
                   IF CHK.OH.QPERC < PRCT.VAR         THEN PROD.OK = NO
                CASE PRCT.VAR = ''
                   IF BASE*ABS(CHK.OH.DIFF) < DOL.VAR THEN PROD.OK = NO
                CASE EXPR = 'AND'
                   IF BASE*ABS(CHK.OH.DIFF)<DOL.VAR OR CHK.OH.QPERC<PRCT.VAR THEN
                      PROD.OK = NO
                   END
                CASE EXPR = 'OR'
                   IF BASE*ABS(CHK.OH.DIFF)<DOL.VAR AND CHK.OH.QPERC<PRCT.VAR THEN
                      PROD.OK = NO
                   END
                END CASE
             END ELSE
                IF CHK.OH.DIFF+0 = 0 THEN PROD.OK = NO
             END
          END

          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:  *** Try and select data for the report...
          MSG = 'Selecting Prc Line : ':PLINE.MSG

          GOSUB WRT.STAT
          OPEN 'DICT ':PHYS.ID TO DPHYSFILE ELSE RETURN

          D = 'S':AM:'1':AM:AM:AM:AM:AM:AM:'TPRODUCT;X;9;9':AM:'L':AM:'10'

          WRITE D ON DPHYSFILE,'PLINE'
          D = 'A':AM:'1':AM:AM:AM:AM:AM:AM:AM:'L':AM:'10'
          WRITE D ON DPHYSFILE,'PN'
          D     = ''
          D<1>  = 'A'
          D<2>  = '0'
          D<9>  = 'R'
          D<10> = '10'
          WRITE D ON DPHYSFILE,'CTRL.NO'
          EXEC = 'SSELECT ':PHYS.ID:' WITH PLINE AND WITH PN BY PLINE BY CTRL.NO '
          IF SEL.PLNE#'' THEN
             PLNE.LST = ''
             PLNE.CT = DCOUNT(SEL.PLNE,VM)
             FOR XX = 1 TO PLNE.CT
                PLNE.LST := ' "':SEL.PLNE<1,XX>:'"'
             NEXT XX
             EXEC := ' AND WITH PLINE =':PLNE.LST
          END
          GOSUB SELECT.ITEMS
          GOSUB SORT.IDS

          RETURN
*-------------------------------------------------------------------------*
SORT.IDS: *** Sort Ids

          IDS       = ''
          IDS.VAL   = ''
          IDS.LINES = ''

          PRD.IDS   = ''
          PRD.CNTS  = ''
          PRD.DOLS  = ''

          DCNT = 1
          LOOP
             READNEXT CN FROM IN.LIST ELSE EXIT
             IF SORT.POS = 1 THEN
                IDS<1,DCNT> = CN
                DCNT = DCNT + 1
             END ELSE
                READ CREC FROM PHYSFILE,CN ELSE CREC = ''
                PN      = CREC<1>
                LOC     = CREC<2>
                CNT.QTY = CREC<3>
                CNT.DT  = CREC<4>
                NEW.PN  = CREC<5>
                UPD.QTY = CREC<6>
                TAGGED  = (FIELD(LOC,'~',3) # "" OR INDEX(LOC,'^',1))
                OK      = YES
                IF TAGS = 'E' AND TAGGED THEN OK = NO
                IF TAGS = 'O' AND NOT(TAGGED) THEN OK = NO

                IF OK THEN
                   IF GBASN THEN
                      GET.ALL.PRD BR,PN
                      GLOBAL.BASN.GET GBASN,BASN

                      KOPTS = PRD(86)
                      IF GBASN=3 AND KOPTS<1,3>#'' THEN KOPTS<1,1>= KOPTS<1,3>

                      GET.BASE PN,BR,BASN,PRC.DATE,BASE,PER,KOPTS,PRD(52),PRD(53),PRD(87)
                      GOSUB GET.MAX.UM
                      BASE = (OCONV(BASE,'MR3')/PER)*UM.QTY
                   END ELSE
                      BASE = 0
                   END
                   EXT = BASE * CNT.QTY
                   READV PLN FROM PRDFILE,PN,9 ELSE
                      PLN = ''
                   END

                   OH = 0
                   IF CNT.DT = '' THEN CNT.DT = DFLT.DT
                   IF CNT.DT # '' THEN
                      GET.PREV.ONHANDS BR,PN,CNT.DT,OH.QTYS,OH.LOCS
                      OHCT = DCOUNT(OH.QTYS,VM)
                      FOR POS = 1 TO OHCT
                      IF LOC = OH.LOCS<1,POS> THEN OH = OH.QTYS<1,POS>; EXIT
                      NEXT POS
                   END

                   NO.CNT = (CNT.QTY = '')
                   IF NO.CNT THEN
                      IF NULL0 THEN
                         CNT.QTY = 0
                      END ELSE
                         CNT.QTY = OH
                      END
                   END

                   OH.DIFF  = (CNT.QTY - OH) + 0
                   DLR.DIFF = BASE*OH.DIFF

                   GET.LINE.IDS , LINE.LIST,PLN
                   LOCATE PN IN LINE.LIST<1> SETTING PN.SEQ ELSE PN.SEQ = 1

                   SORT.KEY = ''

                   BEGIN CASE
                   CASE SORT.POS = 2
                      IF CNT.DT # '' AND (NULL0 OR NOT(NO.CNT))  THEN
                         SORT.KEY = CNT.QTY
                      END
                   CASE SORT.POS = 3
                      SORT.KEY = EXT
                   CASE SORT.POS = 4
                      SORT.KEY = PN.SEQ "R%6":'*':LOC
                   CASE SORT.POS = 5
                      SORT.KEY = OH.DIFF
                   CASE SORT.POS = 6
                      SORT.KEY = DLR.DIFF
                   END CASE

                   IF SORT = 2 THEN
                      LOCATE PLN IN IDS.LINES<1> BY 'AL' SETTING POS ELSE
                         INS PLN BEFORE IDS.LINES<1,POS>
                      END
                      IF SORT.POS = 4 THEN
                         LOCATE SORT.KEY IN IDS.VAL<1,POS> BY 'AL' SETTING PPOS ELSE NULL
                      END ELSE
                         LOCATE SORT.KEY IN IDS.VAL<1,POS> BY 'DR' SETTING PPOS ELSE NULL
                      END
                      INS CN BEFORE IDS<1,POS,PPOS>
                      INS SORT.KEY BEFORE IDS.VAL<1,POS,PPOS>
                   END ELSE
                      IF SORT.POS = 4 THEN
                         LOCATE(SORT.KEY,IDS.VAL,1;POS;'AR') ELSE NULL
                      END ELSE
                         LOCATE(SORT.KEY,IDS.VAL,1;POS;'DR') ELSE NULL
                      END

                      INS CN BEFORE IDS<1,POS>
                      INS SORT.KEY BEFORE IDS.VAL<1,POS>
                   END

                   * Maintain Product Totals record for variance *
                   LOCATE(PN,PRD.IDS,1;POS) ELSE NULL
                   PRD.IDS<1,POS> = PN
                   PRD.CNTS<1,POS> = PRD.CNTS<1,POS> + CNT.QTY
                   PRD.DOLS<1,POS> = PRD.DOLS<1,POS> + EXT

                END
             END
          REPEAT

          IF SORT = 2 THEN
             NIDS = ''
             CNT = DCOUNT(IDS,@VM)
             FOR C = 1 TO CNT
                SCNT = DCOUNT(IDS<1,C>,@SVM)
                FOR S = 1 TO SCNT
                   NIDS<1,-1> = IDS<1,C,S>
                NEXT S
             NEXT C
             IDS = NIDS
          END

          IDS = RAISE(IDS)
          WRITE IDS ON SLFILE,'ID.LIST':PID$
          EXEC = 'GET-LIST ID.LIST':PID$
          GOSUB SELECT.ITEMS

          RETURN
*-------------------------------------------------------------------------*
SELECT.ITEMS: ***

          EXECUTE EXEC RTNLIST IN.LIST CAPTURING MSG

          RETURN
*-------------------------------------------------------------------------*
WRT.STAT: *** Update the Phantom Status...
          WRITE MSG ON PHSTFILE,PID$
          RETURN
*-------------------------------------------------------------------------*
!SMITJR~10/28/10~16:55
