          SUBROUTINE (OID,GEN,LDID,LDID.LIST,QSIGN,REDISP,PRC.KEY,VIEW.ONLY,SUB.F12,LOG.MV)
** Version# 95 - 02/15/2008 - 12:33pm - BABS - main

*** Subroutine : OE.SUBTOTALS
*-------------------------------------------------------------------------*
*** This routine is used to for calculating subtotals on an Order. A user
*** can simply display a subtotal for a certain portion of an order or
*** they may change the subtotal (or COGS, cost, etc) and have the new
*** value be distributed (either flat rate or weighted rate) over the
*** selected portion of the order.
*-------------------------------------------------------------------------*
*** Parameters:
*** OID       - Order ID                                              [IN]
*** GEN       - Generation ID                                         [IN]
*** LDID      - Current Line Item (Ledger Detail ID)                  [IN]
*** LDID.LIST - List of Current Line Items on GEN                     [IN]
*** QSIGN     - Quantity Sign (-1=Sales,Xfers (Ship GEN),1=other)     [IN]
*** REDISP    - Indicates if order should be redisplayed upon return  (OUT)
*** PRC.KEY   - Indicates if user has authorization to change price   [IN]
*** VIEW.ONLY - Display as View Only                                  [IN]
*** SUB.F12   - Indicates if F12 was used to abort from screen        (OUT)
*-------------------------------------------------------------------------*
*** Global Variables Changed: -None-
*-------------------------------------------------------------------------*
*** Global Variables Used:
*** PRC.PRECI$ - Price Precision, from CONTROL file 'PRICING.PRECISION'
*** PRC.ROUND$ - Price Rounding, set based on PRC.PRECI$
*** PRC.PRECI.SLS$ - Sales Pricing Precision, from CONTROL file
***                  'PRICING.PRECISION.SALES'
*** OE.LOCKED.DIR$ - Locked direct meaning PO side of direct CANNOT be upd
*-------------------------------------------------------------------------*
*** Phantom Friendly : NO
*** Java Friendly : NO
*-------------------------------------------------------------------------*
          * PRECISION 9
          DIM SV.LD(200)
          DIM LD2(200)

          MODE       = OID[1,1]
          SUB.F12    = NO
          IF NUM(LD(43)) AND LD(43) # '' AND LD(43) # 0 THEN
             LD.GET2 MAT LD2, LD(43)
             TOT.TO  = TRIM(LD2(3)<1,1> "L#20"):' '
             TOT.TO := OCONV(LD2(8)<1,GEN>*QSIGN,'MR2#10')
          END ELSE
             TOT.TO = LD(43)
          END
          GP.SUB = ''
          CHG.ORIG  = YES
          CHG.BKTO  = NO
          SV.COGS = ''
          SV.COST = ''
          CCODE   = ''
          IF TOT.TO = '' OR TOT.TO = 0 THEN TOT.TO = 'Last Subtotal'
          MAT SV.LD = MAT LD

          REDISP = NO

          * Get Override flag from control file
          * COPYCOST:
          * From Control Record: Change Both COGS And Comm Cost On Ovrd
          * Possible Values:  0-No, 1-Yes, 2-Direct, 3-Tagged, 4-Dir/Tag
          CTRB.ID = "COST.OVERRIDE~":LED(2)<1,GEN,1>
          READV COPYCOST FROM CTRBFILE,CTRB.ID,1 ELSE COPYCOST = ''

          WINDOW 17,6,43,17,9
          DISPLAY.SCREEN "OE.SUBTOTALS"

          IF LED(92)<1,GEN,2> THEN
             XRATE = OCONV(LED(92)<1,GEN,2>,'MR4')
          END ELSE
             XRATE = 1
          END

          GOSUB GET.TOT.LIST
          GOSUB INIT
          GOSUB DISPLAY

          IF LED(92)<1,GEN,2> THEN
             PRINT @(28,0):BLINK$:LED(92)<1,GEN,1>:' - ':XRATE:NORM$
          END

          IF VIEW.ONLY THEN
             PRINT @(2,0):BLINK$:'View Only':NORM$
IN$$1:       INP A,,,0
             GOTO FINISH
          END

          IF PRC.KEY THEN PRC.KEY = PRC.KEY.OVRD
          IF NOT(PRC.KEY) THEN GOTO IN.CMT
*-------------------------------------------------------------------------*
IN.SLS:   SV.SLS = SLS.SUB
IN.SLSUB: INP SLS.SUB,1,1,12,'N2'
          IF F12 THEN GOTO FILEIT
          IF SLS.SUB THEN
             IF ORIG.SLS.SUB/SLS.SUB < 0 THEN
                ERR.MESS 1,4,"Subtotal Sales must be same sign!"
                SLS.SUB = SV.SLS
                GOSUB DISP.TOLS
                GOTO IN.SLS
             END
          END
          IF CHANGED THEN
             SLS.SUB    = ICONV(SLS.SUB*XRATE,'MR0')
             GOSUB DISP.TOLS
          END ELSE
             SLS.SUB = SV.SLS
          END

          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.SLS, IN.SLS, IN.SLS, IN.COGS, IN.SHOW, IN.SHOW
*-------------------------------------------------------------------------*
IN.COGS:  IF VIEW    = 2 THEN GOTO IN.COST
IN.COGS1: IF NOT(COGS.EDIT.OK) OR OE.LOCKED.DIR$ THEN
             IF MOVE = 1 THEN GOTO IN.SLS ELSE GOTO IN.GP
          END
          SV.COGS    = COGS.SUB
          SV.COST    = COST.SUB
          COST.INFO  = MODE:'~':LED(2)<1,GEN,2>:'~':LED(6)<1,GEN>:'~':1
          COST.INFO := '~':COGS.SUB:'~':GEN:'~':0:'~':2:'~':LDID.LST
          TCOGS.SUB  = COGS.SUB
INCOGS2:  INP TCOGS.SUB,14,1,12,'N2',V_'S:VERF.COST.GLVL,':COST.INFO

          * User switched views to COST, but returns to the COGS input so
          * just switch the variables to where the user thinks he/she is
          IF TCOGS.SUB = '' THEN COGS.SUB = SV.COGS
          IF VIEW      = 2 THEN GOTO IN.COST

SWITCH1:  IF F12 THEN GOTO FILEIT
          IF TCOGS.SUB THEN
             IF ORIG.COGS.SUB/TCOGS.SUB < 0 THEN
                ERR.MESS 1,4,"Subtotal COGS must be same sign!"
                COGS.SUB = SV.COGS
                GOSUB DISP.TOLS
                GOTO IN.COGS
             END
          END

          IF CHANGED THEN
             COGS.SUB         = TCOGS.SUB
             COGS.SUB         = ICONV(COGS.SUB*XRATE,'MR0')
             IF DO.COPY.COST THEN
                COST.SUB      = COGS.SUB
             END
             DISP.TAG.WARNING = YES
             GOSUB DISP.TOLS
          END ELSE
             COGS.SUB         = SV.COGS
             DISP.TAG.WARNING = NO
             GOSUB DISP.TOLS
          END

          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.COGS, IN.SLS, IN.COGS, IN.GP, IN.CMT, IN.GP
*-------------------------------------------------------------------------*
IN.COST:  IF NOT(COST.EDIT.OK) OR OE.LOCKED.DIR$ THEN
             IF MOVE = 1 THEN GOTO IN.SLS ELSE GOTO IN.GP
          END
          SV.COST    = COST.SUB
          SV.COGS    = COGS.SUB
IN.COST2: *
          COST.INFO  = MODE:'~':LED(2)<1,GEN,2>:'~':LED(6)<1,GEN>:'~':25
          COST.INFO := '~':COST.SUB:'~':GEN:'~':0:'~':2:'~':LDID.LST
INCOST2:  INP COST.SUB,14,1,12,'N2',V_'S:VERF.COST.GLVL,':COST.INFO

          * User switched views to COGS, but returns to the COST input so
          * just switch the variables to where the user thinks he/she is
          IF COST.SUB    = '' THEN COST.SUB = SV.COST
          IF VIEW        = 1 THEN GOTO IN.COGS1

SWITCH2:  IF F12 THEN GOTO FILEIT
          IF COST.SUB THEN
             IF ORIG.COST.SUB/COST.SUB < 0 THEN
                ERR.MESS 1,4,"Subtotal Cost must be same sign!"
                COST.SUB = SV.COST
                GOSUB DISP.TOLS
                GOTO IN.COST
             END
          END

          IF CHANGED THEN
             COST.SUB         = ICONV(COST.SUB*XRATE,'MR0')
             IF DO.COPY.COST THEN
                COGS.SUB      = COST.SUB
             END
             DISP.TAG.WARNING = YES
             GOSUB DISP.TOLS
          END ELSE
             COST.SUB         = SV.COST
             DISP.TAG.WARNING = NO
             GOSUB DISP.TOLS
          END

          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.COST, IN.SLS, IN.COST, IN.GP, IN.CMT, IN.GP
*-------------------------------------------------------------------------*
IN.GP:    INP GP.SUB,32,1,6,'N2'
          IF F12 THEN GOTO FILEIT

          * Update the sales amount to the new GP amount...
          IF CHANGED THEN
             IF VIEW = 1 THEN
                SLS.SUB = COGS.SUB / (1 - OCONV(GP.SUB,'MR2')/100)
             END ELSE
                SLS.SUB = COST.SUB / (1 - OCONV(GP.SUB,'MR2')/100)
             END
             GOSUB DISP.TOLS
          END
          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.GP, IN.COGS, IN.GP, IN.GP, IN.CMT, IN.SHOW
*-------------------------------------------------------------------------*
IN.SHOW:  INP SHOW.PRC,6,3,1,'YN'
          IF QUIT THEN GOTO FILEIT
          ON MOVE+1 GOTO IN.SHOW, IN.SHOW, IN.SLS, IN.SHOW
*-------------------------------------------------------------------------*
IN.LAST:  INP TOT.TO,10,5,31,V_'D:':VALID.TOTS
          IF QUIT THEN GOTO FILEIT
          IF CHANGED THEN
             IF TOT.TO # 'Top of Order' THEN
                LOCATE TOT.TO IN VALID.TOTS<1> SETTING TPOS THEN
                   TOT.TO.LDID = VALID.TLDIDS<1,TPOS>
                   LD(43) = TOT.TO.LDID
                END ELSE
                   LD(43) = TOT.TO
                END
             END ELSE
                LD(43) = TOT.TO
             END
             CHG.ORIG  = YES
             GOSUB INIT
             GOSUB DISPLAY
             LAST.CHANGED = YES
          END
          CHG.BKTO = NO
          ON MOVE+1 GOTO IN.LAST,IN.LAST,IN.SHOW,IN.LAST
*-------------------------------------------------------------------------*
IN.CMT:   INPWP LD(3),2,7,35,7,10,'0110'
          IF QUIT THEN GOTO FILEIT
          IF NOT(PRC.KEY) THEN GOTO IN.CMT
          ON MOVE GOTO IN.CMT, IN.SHOW, IN.CMT, IN.CMT
          GOTO IN.CMT
*-------------------------------------------------------------------------*
DISPLAY:  *
          IF VIEW       = 1 THEN
             PRINT @(18,0):'COGS'
             COST.INFO  = MODE:'~':LED(2)<1,GEN,2>:'~':LED(6)<1,GEN>:'~':1
             COST.INFO := '~':COGS.SUB:'~':GEN:'~':0:'~':2:'~':LDID.LST
          END ELSE
             PRINT @(18,0):'Cost'
             COST.INFO  = MODE:'~':LED(2)<1,GEN,2>:'~':LED(6)<1,GEN>:'~'
             COST.INFO := 25:'~':COST.SUB:'~':GEN:'~':0:'~':2:'~':LDID.LST
          END

          METH = LED(97)<1,GEN,3>

          BEGIN CASE
          CASE METH = 'Weighted Avg'; DMETH = 'Weighted'; GP.VIEW = 1
          CASE METH = 'Flat GP%'; DMETH = 'Í Flat Í'; GP.VIEW = 2
          CASE OTHERWISE
             METH = 'Flat GP%'
             DMETH = 'Í Flat Í'
             GP.VIEW = 2
          END CASE
          PRINT @(33,0):DMETH

          IF NOT(CHG.BKTO) THEN
             FORM          = LD(15)<1,GEN,1>
             BASN          = LD(15)<1,GEN,2>
             SLS.SUB       = LD(8)<1,GEN>*QSIGN
             COGS.SUB      = LD(10)<1,GEN>*QSIGN
             COST.SUB      = LD(27)<1,GEN>*QSIGN
             SHOW.PRC      = LD(21)
          END
          IF CHG.ORIG THEN
             ORIG.SLS.SUB  = SLS.SUB
             ORIG.COGS.SUB = COGS.SUB
             ORIG.COST.SUB = COST.SUB
             CHG.ORIG      = NO
          END

          GOSUB DISP.TOLS
          PRINT @(6,3):YN[SHOW.PRC+1,1]"L#1"
          FOR JX = 1 TO 7
             PRINT @(2,JX+6):LD(3)<1,JX>"L#35"
          NEXT JX
          PRINT @(10,5):TOT.TO     "L#31"
*-------------------------------------------------------------------------*
MENUS:    MENU.CLEAR

          IF VIEW = 1 THEN
             PRINT @(2,15):'Cost'
          END ELSE
             PRINT @(2,15):'COGS'
          END
          MENU.LOAD  2,15, 9,6,'V'

          IF GP.VIEW = 1 THEN
             PRINT @(14,15):'  Flat GP%  '
             MENU.LOAD 16,15, 7,6,'G'
          END ELSE
             PRINT @(14,15):'Weighted GP%'
             MENU.LOAD 14,15,11,10,'G'
          END

          RETURN
*-------------------------------------------------------------------------*
DISP.TOLS: * Display Subtotals
          PRINT @(1,1):OCONV(ICONV(SLS.SUB/XRATE,'MR0'),'MR22#12')
          FMT  = 'MR':PRD.WGHT$:"#11"
          PRINT @(14,3):OCONV(WGHT.SUB.GEN,FMT)
          PRINT @(27,3):OCONV(LOAD.SUB.GEN,'MR24#12')
          * Support either COGS view or Cost view....
          IF VIEW = 1 THEN
             TEMP.COST = COGS.SUB
             IF SLS.SUB+0=0 THEN PERC = 0 ELSE
                PERC = ICONV((SLS.SUB-COGS.SUB)/SLS.SUB*100,"MR2")
             END
          END ELSE
             TEMP.COST = COST.SUB
             IF SLS.SUB+0=0 THEN PERC = 0 ELSE
                PERC = ICONV((SLS.SUB-COST.SUB)/SLS.SUB*100,"MR2")
             END
          END
          IF NOT(COGS.VIEW.OK) AND NOT(COST.VIEW.OK) THEN RETURN
          PRINT @(14,1):OCONV(ICONV(TEMP.COST/XRATE,'MR0'),'MR22#12')
          PRINT @(30,1):OCONV(PERC,'MR22#8')
          RETURN
*-------------------------------------------------------------------------*
SUBS:     ON OPTION GOTO CHG.VIEW,GP.SET
*-------------------------------------------------------------------------*
CHG.VIEW: VIEW.OPTS = ''
          SV.VIEW   = VIEW

          * Set up the views that they are auth'd for.
          IF COGS.VIEW.OK THEN
             VIEW.OPTS = 'COGS'
          END
          IF COST.VIEW.OK THEN
             VIEW.OPTS<1,-1> = 'Cost'
          END

          * Display the tables for the auth'd views.
          IF DCOUNT(VIEW.OPTS,VM) < 2 THEN RETURN
          MENU.TABLE VIEW.CH,,,1,2,4,,,VIEW.OPTS,'View',VIEW
          LOCATE VIEW.CH IN VIEW.OPTS<1> SETTING VIEW ELSE VIEW = SV.VIEW
          IF VIEW = SV.VIEW THEN RETURN

          CHG.BKTO = YES
          GOSUB DISPLAY
          CHG.BKTO = NO

          IF (COPYCOST = 3 OR (COPYCOST = 4 AND NOT(LED(33)<1,GEN>))) THEN
             IF TAG.FOUND AND DISP.TAG.WARNING THEN

             * To avoid code dup, I Have created CST.OPS with both opts
             * (COGS and Cost) for text disaplyed in the following message.
             * CST.OPS<1,VIEW> is current view. CST.OPS<1,(NOT(VIEW-1))+1>
             * reflects the non-current view by this logic:
             * IF VIEW=1 :  (NOT(VIEW-1))+1 => (NOT(1-1))+1
             *                              => (NOT(0))+1 => (1)+1 => 2
             * IF VIEW=2 :  (NOT(VIEW-1))+1 => (NOT(2-1))+1
             *                              => (NOT(1))+1 => (0)+1 => 1
             * This will pull the opposite option from the list

                CST.OPS   = 'COGS':VM:'Cost'
                TAG.MESS  = 'Tagged Items Have Been Found On the':AM
                TAG.MESS := 'Order. The ':CST.OPS<1,(NOT(VIEW-1))+1>
                TAG.MESS := ' Will Flow To ':CST.OPS<1,VIEW>:' For':AM
                TAG.MESS := 'Tagged Items Upon Returning To The Body':AM
                TAG.MESS := 'Of The Order.':AM:' ':AM:'The Current '
                TAG.MESS := CST.OPS<1,VIEW>:' Displayed Here May':AM
                TAG.MESS := 'Not Be Accurate.'
                MESS 2,5,TAG.MESS
IN$$2:          INP A,,,0
                DISP.TAG.WARNING = NO
             END
          END

          RETURN
*-------------------------------------------------------------------------*
GP.SET:   BEGIN CASE
             CASE DMETH = 'Weighted'; DMETH = 'Í Flat Í'; GP.VIEW = 2
             CASE DMETH = 'Í Flat Í'; DMETH = 'Weighted'; GP.VIEW = 1
          END CASE
          PRINT @(33,0):DMETH
          REDISP = YES
          IF DMETH = 'Í Flat Í' THEN METH = 'Flat GP%'
          IF DMETH = 'Weighted' THEN METH = 'Weighted Avg'
          LED(97)<1,GEN,3> = METH
          GOSUB MENUS
          RETURN
*-------------------------------------------------------------------------*
FILEIT:   IF F12 THEN
             CONFIRM.ABORT SURE
             IF NOT(SURE) THEN GOTO IN.SLS
             MAT LD = MAT SV.LD
             UPDATE.LEDGER.DET OID,LDID,QSIGN,,LOG.MV
             SUB.F12 = YES
             GOTO FINISH
          END
          DO.UPD =           (SLS.SUB  # ORIG.SLS.SUB)
          DO.UPD = DO.UPD OR (COGS.SUB # ORIG.COGS.SUB)
          DO.UPD = DO.UPD OR (COST.SUB # ORIG.COST.SUB)
          IF DO.UPD THEN
             * Check for price change restrictions
             LDIDS = FIELD(LDID.LIST<1>,VM,ST.POS,(END.POS-ST.POS+1))
             OE.CHECK.PRC.AUTH OID,GEN,LDIDS,CHG.PRC.OK
             IF NOT(CHG.PRC.OK) THEN
                SLS.SUB  = ORIG.SLS.SUB
                IF NOT(OE.LOCKED.DIR$) THEN
                   COGS.SUB = ORIG.COGS.SUB
                   COST.SUB = ORIG.COST.SUB
                END
                GOSUB DISP.TOLS
                GOTO IN.SLS
             END
          END
          IF DMETH = "Í Flat Í" THEN
             GP.METH = "Flat"
          END ELSE
             GP.METH = "Weighted"
          END

          MSGS = ''
          RESPS = ''

          OE.SUBTOTALS.UPD OID,GEN,LDID,LDID.LIST,QSIGN,ORIG.SLS.SUB,ORIG.COGS.SUB,ORIG.COST.SUB,SLS.SUB,COGS.SUB,COST.SUB,PERC,SHOW.PRC,TOT.TO,VIEW,VALID.TOTS,VALID.TLDIDS,ST.POS,END.POS,GP.METH,LD(3),REDISP,PRC.ERROR,,MSGS,RESPS,DO.TAG.COPY.COST,LAST.CHANGED,LOG.MV

          IF PRC.ERROR THEN
             GOSUB DISP.TOLS
             GOTO IN.SLS
          END
*-------------------------------------------------------------------------*
FINISH:   WINDOW.CLOSE
          PRC.KEY = SV.PRC.KEY
          RETURN
*-------------------------------------------------------------------------*
INIT:
          DISP.TAG.WARNING = NO
          LAST.CHANGED = NO
          SV.PRC.KEY = PRC.KEY
          OE.SUBTOTALS.INIT OID,GEN,LDID,LDID.LIST,QSIGN,COGS.VIEW.OK,COGS.EDIT.OK,COST.VIEW.OK,COST.EDIT.OK,LDID.LST,SLS.SUB.GEN,COGS.SUB.GEN,COST.SUB.GEN,WGHT.SUB.GEN,LOAD.SUB.GEN,ST.POS,END.POS,POE.BID.EDIT.OK,POE.OPEN.EDIT.OK,PRC.KEY,PRC.KEY.OVRD,CHG.BKTO,COPYCOST,DO.TAG.COPY.COST,DO.COPY.COST,TAG.FOUND
          IF COGS.VIEW.OK THEN VIEW = 1 ELSE VIEW = 2
          SLS.SUB  = LD(8)<1,GEN>
          COGS.SUB = LD(10)<1,GEN>
          COST.SUB = LD(27)<1,GEN>

          RETURN
*-------------------------------------------------------------------------*
GET.TOT.LIST: *** Get a list of all valid totals in this GEN (not this one)

          VALID.TLDIDS = ''
          VALID.TOTS   = ''
          ID.LIST=LDID.LIST
          ID.CT        = DCOUNT(ID.LIST<1>,VM)
          LOCATE LDID IN ID.LIST<1> SETTING LDID.LN ELSE LDID.LN=0
          FOR IDN      = 1 TO LDID.LN
             ID        = ID.LIST<1,IDN>
             IF ID     = LDID THEN GOTO NEXT.IDN
             LD.GET2 MAT LD2, ID
             IF LD2(1) = 'S' THEN
                IF VALID.TLDIDS    ='' THEN
                   VALID.TLDIDS    = '-1'
                   VALID.TOTS      = 'Last Subtotal'
                END
                VALID.TLDIDS<1,-1> = ID

                VTOTS  = TRIM(LD2(3)<1,1>'L#20'):' '
                VTOTS :=OCONV(LD2(8)<1,GEN>*QSIGN/XRATE,'MR2#10')
                VALID.TOTS<1,-1>   = VTOTS
             END
NEXT.IDN: NEXT IDN

          VALID.TOTS<1,-1>         = 'Top of Order'
          RETURN
*-------------------------------------------------------------------------*
!BABS~02/15/08~12:33
