          SUBROUTINE (KEY,VALUE,VARS,LED.REC,MSGS,RESPS,GL.SALES.TYPE)
** Version# 168.0002[3] - 04/15/2016 - 04:04pm - TSMITH - eclipse
*** V168.0002 Change - Custom Coding . - 04/15/2016 - TSMITH - eclipse
*** V168.0001 Change - Custom Coding . - 04/01/2016 - TSMITH - eclipse

*** Subroutine - OE.COPY.BID.UPD
*-------------------------------------------------------------------------*
*** This routine takes the current LED files and copies some (or possibly
*** all) of its contents to a new order. The new order can be of the same
*** type (Sales to Sales) or of a different type (Sales to Purchase). The
*** status of the new order will be Bid (B). The user has the option of
*** selecting if the current price/cost information will be kept as
*** overrides or if they will be recalced for new order.
*-------------------------------------------------------------------------*
*** Parameters:
*** KEY     - Indicates what we are doing in this routine            (BOTH)
***           (setting up variables, copying order, etc)
*** VALUE   - Stores information when confirming deletion of         (BOTH)
***           an item from the order
*** VARS    - Indicates information about the order such as          (BOTH)
***           OID, branches, etc.
*** LED.REC - Ledger Record that is built and parsed                  (OUT)
*** MSGS    - Actions for action array. Used only for JAVA.PROC       (OUT)
*** RESPS   - Response data structure.  Only required with JAVA.PROC   (IN)
*** GL.SALES.TYPE - Sends list of valid sales sources to Solar        (OUT)
*-------------------------------------------------------------------------*
*** Common variables changed: - None -
*-------------------------------------------------------------------------*
*** Comprehension Help:  This comment is help programmers understand the
*** flow of the subroutines for the different types of copies.
***
*** ModeChange (ie. S->P), Copy Current --> CHANGE.MODE --> SINGLE.GEN
*** ModeChange (ie. S->P), Copy All, Open --> CHANGE.MODE --> MULTIPLE.GEN
*** Copy Current --> CHANGE.MODE --> SINGLE.GEN
*** No ModeChange (ie. S->S), Copy All, Open  --> COPY.IT
*-------------------------------------------------------------------------*

          GOSUB PARSE.VARS.COMMON
          IF JAVA.PROC$ THEN
             MATREAD LED FROM LEDFILE, OID ELSE MAT LED = ''
             MATREAD OLED FROM LEDFILE, OID ELSE MAT OLED = ''
             GOSUB PARSE.VARS.JAVA
          END ELSE
             GOSUB PARSE.VARS
          END
          BEGIN CASE
          CASE KEY = 'PRE.CHECK';          GOSUB PRE.CHECK
          CASE KEY = 'COPY.IT';            GOSUB COPY.IT
          CASE KEY = 'COPY.IT2';           GOSUB COPY.IT2
          CASE KEY = 'COPY.IT3';           GOSUB COPY.IT3
          CASE KEY = 'CHANGE.MODE';        GOSUB CHANGE.MODE
          CASE KEY = 'CHANGE.MODE2';       GOSUB CHANGE.MODE2
          CASE KEY = 'CHANGE.MODE3';       GOSUB CHANGE.MODE3
          END CASE
FINISH:
          GOSUB BUILD.VARS
          RETURN
*-------------------------------------------------------------------------*
PARSE.VARS:
          GEN.LIST       = VALUE<1>
          ORD.BY         = VALUE<2>
          DEL.LIST       = ''
          CERTIFIED      = ''
          RETURN
*-------------------------------------------------------------------------*
PARSE.VARS.JAVA:
          AUTH.SEL = ''
          GL.SALES.TYPE = ''
          HAS.GEN.LIST = ''
          LOCATE 'SALES.SOURCE' IN RESPS<1> SETTING POS THEN
             GEN.LIST = RAISE(RESPS<2,POS>)
             HAS.GEN.LIST = YES
          END

          LOCATE 'AUTH.SEL' IN RESPS<1> SETTING POS THEN
             AUTH.SEL = RESPS<2,POS>
             ORD.BY = AUTH.SEL
          END
          LOCATE 'DEL.LIST' IN RESPS<1> SETTING POS THEN
             DEL.LIST = RESPS<2,POS>
          END
          LOCATE 'CERTIFY' IN RESPS<1> SETTING POS THEN
             CERTIFIED = YES
          END
          QSIGN          = OE.GET.QSIGN(OID)
          * Reload CUS and CUSS arrays for Solar
          GET.CUS LED(2)<1,GEN,1>,LED(1)<1,GEN>,LED(5)<1,GEN>,QSIGN
          MSGS = ''
          RETURN
*-------------------------------------------------------------------------*
PARSE.VARS.COMMON:  *
          *When encountering a need to prompt the user for information,
          *this program will terminate early and later be reinvoked,
          *resuming after the termination point.  State within the program
          *can be preserved across the 'quit+restart' by placing it here.
          OID            = VARS<1>
          GEN            = VARS<2>
          NEW.MODE       = VARS<3>
          OLD.MODE       = VARS<4>
          COPT           = VARS<5>
          SAME.CN        = VARS<6>
          ST.CN          = VARS<7>
          BT.CN          = VARS<8>
          GENS           = VARS<9>
          DELETED.PNS    = VARS<10>
          GEN.MV         = VARS<11>
          OLD.OID        = VARS<12>
          CERT.IDS       = VARS<13>
          PRC.BR         = VARS<14>
          RCV.BR         = VARS<15>
          COPY.PRC       = VARS<16>  ;*Yes/No/Manual -> 1/''/2
          CNSGN          = VARS<17>
          COPY.COST      = VARS<18>  ;*Yes/No/Manual -> 1/''/2
          COPY.COGS      = VARS<19>  ;*Yes/No/Manual -> 1/''/2
          NETS           = VARS<20>  ;*List of line item prices
          COGS           = VARS<21>  ;*List of line item COGS
          LD.DATA        = RAISE(VARS<22>)

          BEGIN CASE
          CASE COPY.PRC = 'Yes'                   ; COPY.PRC = 1
          CASE COPY.PRC = 'No'                    ; COPY.PRC = ''
          CASE COPY.PRC = 'Manual Overrides Only' ; COPY.PRC = 2
          END CASE
          BEGIN CASE
          CASE COPY.COST = 'Yes'                   ; COPY.COST = 1
          CASE COPY.COST = 'No'                    ; COPY.COST = ''
          CASE COPY.COST = 'Manual Overrides Only' ; COPY.COST = 2
          END CASE
          BEGIN CASE
          CASE COPY.COGS = 'Yes'                   ; COPY.COGS = 1
          CASE COPY.COGS = 'No'                    ; COPY.COGS = ''
          CASE COPY.COGS = 'Manual Overrides Only' ; COPY.COGS = 2
          END CASE
          DIM LED2(200)

          RETURN
*-------------------------------------------------------------------------*
BUILD.VARS:  *
          * This needs to match the usage in PARSE.VARS.COMMON
          VARS<1>  = OID
          VARS<2>  = GEN
          VARS<3>  = NEW.MODE
          VARS<4>  = OLD.MODE
          VARS<5>  = COPT
          VARS<6>  = SAME.CN
          VARS<7>  = ST.CN
          VARS<8>  = BT.CN
          VARS<9>  = GENS
          VARS<10> = DELETED.PNS
          VARS<11> = GEN.MV
          VARS<12> = OLD.OID
          VARS<13> = CERT.IDS
          VARS<14> = PRC.BR
          VARS<15> = RCV.BR
          VARS<16> = COPY.PRC
          VARS<17> = CNSGN
          VARS<18> = COPY.COST
          VARS<19> = COPY.COGS
          VARS<20> = NETS
          VARS<21> = COGS
          VARS<22> = LOWER(LD.DATA)
          RETURN
*-------------------------------------------------------------------------*
CHANGE.MODE: *
          PNS            = ''
          QTYS           = ''
          CMNTS          = ''
          RECST          = ''
          NETS           = ''
          COGS           = ''
          COST           = ''
          LD.DATA        = ''
          LOT.PNS        = ''
          LOT.PN.LIST    = ''
          LOT.QTY.ORD    = ''
          LOT.PRC.LIST   = ''
          LOT.CST.LIST   = ''
          LOT.TYP.LIST   = ''
          LOT.DT.LIST    = ''
          LOT.SUM.LIST   = ''
          DELETED.PNS    = ''

          IF OLD.MODE = 'T' THEN GEN = '1'

          MAT LED = MAT OLED

          * Make sure we remember what "Job" this belongs to...
          NQN = LED(95)

          * If this is a transfer than the new mode is Open
          * otherwise its a bid
          IF NEW.MODE = 'T' THEN STAT = 'O' ELSE STAT = 'B'

          * Get a list of pns and qtys.
          GN.CT = DCOUNT(OLED(12),VM)
          IF GENS = 'C' OR OLD.MODE = 'T' THEN GN.CT = 1

          IF GN.CT = 1 THEN
             GOSUB SINGLE.GEN
          END ELSE
             GOSUB MULTIPLE.GENS
          END

          DEL.LIST          = ''
          NEED.CONFIRMATION = NO
          PN.CT             = DCOUNT(PNS<1>, VM)

          FOR ID = 1 TO PN.CT
             PN = PNS<1,ID>
             MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ""

             * If the prod. is scheduled for deletion, go check whether
             * there's anything left...
             DEL.FLAG = NO
             IF PRD(3) = 4 THEN
                OE.CHK.DEL.PROD PN,GEN,PRC.BR,DEL.FLAG,PRMPT
             END

             * If there's nothing left, check whether user has auth
             * to add Delete status item, and if they even want to...
             IF DEL.FLAG THEN
                DEL.LIST<1,ID>    = LOWER(LOWER(PRMPT))
                NEED.CONFIRMATION = YES
             END
          NEXT ID

          IF NEED.CONFIRMATION THEN
             KEY       = 'CHANGE.MODE2'
             VALUE     = DEL.LIST
             VALUE<2>  = PNS
             VALUE<3>  = QTYS
             VALUE<4>  = CMNTS
             VALUE<5>  = RECST
             VALUE<6>  = COST
             VALUE<7>  = LOT.PNS
             VALUE<8>  = LOT.PN.LIST
             VALUE<9>  = LOT.QTY.ORD
             VALUE<10> = LOT.PRC.LIST
             VALUE<11> = LOT.CST.LIST
             VALUE<12> = LOT.TYP.LIST
             VALUE<13> = LOT.DT.LIST
             VALUE<14> = LOT.SUM.LIST
             VALUE<15> = NQN
             VALUE<16> = STAT
             VALUE<17> = GN.CT
             VALUE<18> = NETS
             VALUE<19> = COGS

             MATBUILD LED.REC FROM LED
             IF JAVA.PROC$ THEN
                * Action needs to be added so user can indicate what they
                * want to do with these items
                IF DEL.LIST THEN
                   ACTION.ADD MSGS,"DEL.LIST",,,,,LOWER(LOWER(DEL.LIST))
                END

                *** WRITE LED TO DISK HERE
                UPDATE.LEDGER OID
             END

             RETURN TO FINISH
CHANGE.MODE2:*
             MATPARSE LED FROM LED.REC

             DEL.LIST       = VALUE<1>
             IF JAVA.PROC$ THEN
                LOCATE 'DEL.LIST' IN RESPS<1> SETTING POS THEN
                   DEL.LIST = RAISE(RESPS<2,POS>)
                END
             END
             * Note: Line Item level additions to Value will likely need
             *       to be matched in the 'Delete Product' logic found
             *       immedately below.
             PNS            = VALUE<2>
             QTYS           = VALUE<3>
             CMNTS          = VALUE<4>
             RECST          = VALUE<5>
             COST           = VALUE<6>
             LOT.PNS        = VALUE<7>
             LOT.PN.LIST    = VALUE<8>
             LOT.QTY.ORD    = VALUE<9>
             LOT.PRC.LIST   = VALUE<10>
             LOT.CST.LIST   = VALUE<11>
             LOT.TYP.LIST   = VALUE<12>
             LOT.DT.LIST    = VALUE<13>
             LOT.SUM.LIST   = VALUE<14>
             NQN            = VALUE<15>
             STAT           = VALUE<16>
             GN.CT          = VALUE<17>
             NETS           = VALUE<18>
             COGS           = VALUE<19>
          END
          QSIGN = OE.GET.QSIGN(OLD.OID)
          PN.CT = DCOUNT(PNS<1>, VM)
          FOR ID = PN.CT TO 1 STEP -1
             DEL.ITEM = DEL.LIST<1,ID>
             PN       = PNS<1,ID>
             MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ""

             GET.ALL.PRD PRC.BR,LD(1),QSIGN
             IF PRD(3) # '9' THEN
                IF DEL.ITEM THEN
                   LOCATE PN IN DELETED.PNS<1> SETTING DPOS ELSE
                      DELETED.PNS<1,-1> = PN
                   END
                   *Update as needed for changes to VALUES
                   PNS   = DELETE(PNS,1,ID)
                   PNS   = DELETE(PNS,2,ID)
                   PNS   = DELETE(PNS,3,ID)
                   PNS   = DELETE(PNS,4,ID)
                   PNS   = DELETE(PNS,5,ID)
                   PNS   = DELETE(PNS,6,ID)
                   QTYS  = DELETE(QTYS,1,ID)
                   CMNTS = DELETE(CMNTS,1,ID)
                   COST  = DELETE(COST,1,ID)
                   NETS  = DELETE(NETS,1,ID)
                   COGS  = DELETE(COGS,1,ID)
                END
             END
          NEXT ID

          * set up the information we want to remain intact after
          * the copy
          GOSUB COPY.GEN

          IF NEW.MODE = 'T' THEN NETS = ''

          OE.CREATE.LEDGER NEW.MODE,0,OID,GEN,PRC.BR,BT.CN,ST.CN,STAT,DATE(),DATE(),PNS,QTYS,CMNTS,,,,,RCV.BR,DATE(),,,ERR.MSG,NETS,COGS,,,RECST,COST,CNSGN,LOT.PNS,LOT.PN.LIST,LOT.QTY.ORD,LOT.PRC.LIST,LOT.CST.LIST,LOT.DT.LIST,LOT.SUM.LIST,,,,LED.DATA,,LD.DATA,LOT.TYP.LIST
          * If there is an error show the user what happened.
          IF ERR.MSG AND ERR.MSG # 0 THEN
             BEGIN CASE
             CASE ERR.MSG = 1
                VALUE = 'Entity ID not found! Unable to copy.'
             CASE ERR.MSG = 2
                VALUE = 'No items found! Copy aborted.'
             CASE ERR.MSG
                VALUE = 'Ledger locked by another user. Operation Aborted.'
             END CASE

             KEY = 'CREATE.ERROR'
          END ELSE
             MAT OLED = MAT LED

             READV OLD.BRS FROM LEDFILE,OID,1 ELSE OLD.BRS = ''

             LED(95) = NQN
             IF OLD.BRS#LED(1)<1,1> AND GN.CT = 1 THEN
                LED(31)<1,1> = DATE()-1
             END

             * Set up foreign currency correctly for PO
             LED(92)<1,GEN> = ''
             IF CUS(90) THEN
                LED(92)<1,GEN,1> = CUS(90)
                XCURR.RATE.GET XRATE,LED(92)<1,GEN,1>,LED(9)<1,GEN>
                LED(92)<1,GEN,2> = XRATE
             END

             IF CUSS(72) # '' THEN
                LED(3)<1,GEN> = CUSS(72)
             END ELSE
                KEY = 'CHANGE.MODE3'
                MATBUILD LED.REC FROM LED
                RETURN TO FINISH
CHANGE.MODE3:   *
                MATPARSE LED FROM LED.REC

                * Set the Order Sales Source
                SGEN = 1              ;* Single gen processing, set to 1
                TARGET.GEN = GEN      ;* Gen that we will set source for
                GOSUB SET.SOURCE
             END

             IF NEW.MODE = 'T' THEN
                GOSUB CHECK.XFER.AVAIL ;* Check avail for the transfer
             END

             GOSUB FINAL.CMTS

             QSIGN = OE.GET.QSIGN(OID)
             OE.TAX.CALC OID,GEN,QSIGN

             UPDATE.LEDGER OID

             * This will automatically combine any combine-able gens.
             GOSUB COMBINE.GENS
             KEY = 'CHANGE.MODE4'
          END

          RETURN TO FINISH
*-------------------------------------------------------------------------*
COPY.IT:  *
          QSIGN          = OE.GET.QSIGN(OID)
          DELETED.PNS    = ''

          MATREAD LED2 FROM LEDFILE, OLD.OID ELSE MAT LED2 = ''
          MAT LED = MAT LED2
          MAT OLED = ''
          LED(3)   = ''
          LED(7)   = 1
          LED(8)   = ''
          LED(14)  = ''
          LED(15)  = ''
          LED(16)  = ''
          LED(17)  = ''
          LED(18)  = ''
          LED(24)  = ''
          LED(25)  = ''
          LED(26)  = ''
          LED(33)  = ''
          LED(35)  = ''
          LED(36)  = ''
          LED(40)  = ''
          LED(41)  = ''
          LED(42)  = ''
          LED(60)  = ''
          IF LED(61)<1,GEN,1> = 'CONVERTED' THEN
             LED(61) = ''
          END
          LED(63)  = ''
          LED(64)  = ''
          LED(66)  = ''
          LED(68)  = ''
          LED(79)  = ''
          LED(82)  = OID
          * Don't Copy Job Management Order setting.
          IF LED(83)<1,GEN>[1,7] = "@JOBNO=" THEN
             LED(83) = ''
          END
          LED(88)  = ''
          LED(99)  = ''
          LED(100) = ''
          LED(102) = ''
          LED(110) = CNSGN
          LED(114) = '' ;* Remote discount
          LED(121) = ''
          LED(126) = ''
          LED(128) = ''
          LED(129) = ''
          IF NEW.MODE = 'P' THEN
             * Don't copy EDI send and received flags
             LED(96) = ''
             LED(147) = ''
          END
          * Don't copy ship via route types to a new bid.
          LED(137) = ''
          GET.CUS PRC.BR,BT.CN,ST.CN,QSIGN

          LED(79)<1,GEN,5> = CUSS(183)

          GEN.MV = ''
          GN.CT  = DCOUNT(LED(12),VM)
          FOR GN = GN.CT TO 1 STEP -1
             * Do not copy from status code from Credit/Rebill GENs or from
             * Post Order Entry GENs going onto Transfers
             * Do not copy from History conversions or G/L will reverse
             BEGIN CASE
             CASE LED(30)<1,GN>[1,2] = 'CR'
                LED(30)<1,GN> = ""
             CASE LED(30)<1,GN> = 'HST'
                LED(30)<1,GN> = ""
             CASE LED(30)<1,GN> = 'PE' AND NEW.MODE = 'T'
                LED(30)<1,GN> = ""
             CASE LED(30)<1,GN> = 'WOE'
                LED(30)<1,GN> = ""
             END CASE
             IF GEN.LIST<1,GN> = '&DELETE&' THEN
                LED(48)<1,GN> = ''
                IF GENS = 'OG' THEN
                   DEL.INVOICED = YES
                END ELSE
                   DEL.INVOICED = NO
                END
                OE.DEL.GEN OID,GN,DEL.INVOICED
             END ELSE
                GEN.MV<1,-1> = GN
                * If this is a transfer than we need to make sure
                * they xref each other.
                IF NEW.MODE = 'T' AND GN > 1 THEN
                   LED(1)<1,GN> = ST.CN
                END ELSE
                   LED(1)<1,GN> = BT.CN
                END

                * Need to set writer so that G/L branch is
                * properly assigned.
                WRITER = TRANS('LEDGER',OLD.OID,73,'X')<1,1,1>
                LED(73)<1,GN> = WRITER

                *LED(73)<1,GN> = USER.ID

                * Make sure the shipping/pricing branches show correctly.
                * and account for any customer specific overrides at
                IF NEW.MODE # 'T' THEN
                   LED(2)<1,GN,1> = PRC.BR   ;* Default price branch
                   LED(2)<1,GN,2> = RCV.BR   ;* Default ship  branch

                   * Customer specific overrides
                   IF CUSS(73)<1,2> THEN LED(2)<1,GN,1> = CUSS(73)<1,2>
                   IF CUSS(73)<1,1> THEN LED(2)<1,GN,2> = CUSS(73)<1,1>

                   * Find GL branch based on above
                   GET.GL.BRANCH OID,GN,LED(2)<1,GN,1>,LED(2)<1,GN,2>,GL.BR
                   LED(2)<1,GN,3> = GL.BR
                END ELSE
                   IF GN > 1 THEN
                      LED(2)<1,GN,1> = RCV.BR
                      LED(2)<1,GN,2> = RCV.BR
                      LED(2)<1,GN,3> = RCV.BR
                   END ELSE
                      LED(2)<1,GN,1> = PRC.BR
                      LED(2)<1,GN,2> = PRC.BR
                      LED(2)<1,GN,3> = PRC.BR
                   END
                END

                * Setting the already selected, or not, g/l source
                IF CUSS(72) # '' THEN
                   LED(3)<1,GN> = CUSS(72)
                END ELSE
                   SGEN = GN             ;* All gen processing, set to GN
                   TARGET.GEN = GN       ;* Gen that we will set source for
                   GOSUB SET.SOURCE
                END

                LED(4)<1,GN>  = DATE()  ;* Order Date
                LED(9)<1,GN>  = DATE()  ;* Ship Date
                LED(10)<1,GN> = DATE()  ;* Required Date

                IF NEW.MODE = 'T' AND GN > 1 THEN
                   LED(5)<1,GN> = BT.CN
                END ELSE
                   LED(5)<1,GN> = ST.CN
                END

                IF LED(6)<1,GN> # 'X' THEN
                   IF NEW.MODE # 'T' THEN
                      LED(6)<1,GN> = 'B'
                   END ELSE
                      LED(6)<1,GN> = 'O'
                   END
                END
                OE.STATUS.INIT OID,GN,LED(6)<1,GN>

                * If we are copying the price, expire the pricing
                IF COPY.PRC THEN
                   LED(31)<1,GN> = (DATE() - 1)
                END
                IF LED2(1)<1,1>#LED(1)<1,1> THEN
                   LED(31)<1,GN> = DATE()-1
                END
                * If we are not copying price from the orginal order
                * or we are only copying manual overrides then we
                * need to set the price date to todays date
                IF NOT(COPY.PRC) OR COPY.PRC = 2 THEN
                   LED(22)<1,GN> = DATE()
                END

                IF NEW.MODE = 'P' THEN
                   LED(29)<1,GN> = CUS(58)
                END ELSE
                   LED(29)<1,GN> = CUSS(28)
                END

                IF NOT(COPY.PRC) AND NOT(COPY.COST) AND NOT(COPY.COGS) THEN
                   READV PRC.EXP.DAYS FROM CTRLFILE,'BID.EXP.DAYS',1 ELSE
                      PRC.EXP.DAYS = 30
                   END
                   LED(31)<1,GN> = LED(22)<1,GN> + PRC.EXP.DAYS
                END

                LED(37)<1,GN,1> = CUSS(13)
                LED(37)<1,GN,2> = LOWER(LOWER(CUSS(35)))
                LOCATE CUSS(4) IN CUSS(33)<1> SETTING POS ELSE POS = 1

                IF ORD.BY # '' THEN
                   LED(68)<1,GN> = ORD.BY
                END

                LED(72)<1,GN>   = CUSS(41)
                LED(34)<1,GN>   = CUSS(44)
                LED(73)<1,GN>    = WRITER
                *LED(73)<1,GN>   = USER.ID

                * Copy Print Style / Form Type if site uses Eclipse Forms
                IF USE.ECLIPSE.FORMS$ THEN
                   LED(77)<1,GN> = LOWER(CUSS(195))
                END ELSE
                   LED(77)<1,GN>  = CUSS(42)
                END

                * Find correct salespeople for split commisions
                SLS.LIST    = ''
                SLS.PCT     = ''
                SPLIT.TYPES = CUSS(110)
                SPLIT.SLS   = RAISE(CUSS(111))
                SPLIT.PCT   = RAISE(CUSS(112))

                * Comm.Ct should always be 2, Inside/Outside are the types
                COMM.CT = DCOUNT(SPLIT.TYPES,VM)

                FOR X = 1 TO COMM.CT
                   SLSMN.CT = DCOUNT(SPLIT.SLS<X>,VM);* # of Salespeople
                   SLS.POS = 1
                   * Go through split commision by salesperson
                   FOR Z = 1 TO SLSMN.CT
                      IF SPLIT.SLS<X,Z,2> = '' OR SPLIT.SLS<X,Z,2> = PRC.BR THEN
                         * SLS.LIST = Salesperson + Commission group
                         SLS.LIST<X,SLS.POS> = SPLIT.SLS<X,Z,1>:SVM:SPLIT.SLS<X,Z,3>
                         SLS.PCT<X,SLS.POS>  = SPLIT.PCT<X,Z> ;* Percents
                         SLS.POS += 1
                      END
                   NEXT Z
                NEXT X

                LED(102)<1,GN,1> = LOWER(LOWER(CUSS(110)))
                LED(102)<1,GN,2> = LOWER(LOWER(LOWER(SLS.LIST)))
                LED(102)<1,GN,3> = LOWER(LOWER(LOWER(SLS.PCT)))

                IF NOT(SAME.CN) OR NEW.MODE = "T" THEN
                   EXP.DT       = TRIM(CUS(172)<1,POS>)
                   IF EXP.DT    = '' OR EXP.DT >= LED(9)<1,GN> THEN
                      LED(61)<1,GN>   = TRIM(CUS(34)<1,POS>)
                   END
                   LED(70)<1,GN>   = CUSS(46)
                   LED(71)<1,GN,1> = CUSS(17)<1,1>
                   LED(74)<1,GN>   = LOWER(CUSS(52))
                   LED(78)<1,GN,1> = CUSS(2)<1,1>
                   LED(78)<1,GN,2> = CUSS(2)<1,2>
                   LED(78)<1,GN,3> = CUSS(3)[1,22]:','
                   LED(78)<1,GN,3> = LED(78)<1,GN,3>"L#23 ":CUSS(4)"L#4"
                   LED(78)<1,GN,4> = CUSS(3)
                   LED(78)<1,GN,5> = CUSS(4)
                   LED(79)<1,GN,5> = CUSS(183)
                   LED(80)<1,GN>   = LOWER(CUSS(122))
                   LED(75)<1,GN>   = CUSS(5)
                   LED(92)<1,GN>   = CUS(90)
                END

                SV.SPLT.TAX = LED(79)<1,GN,5>
                LED(79)<1,GN> = ''
                LED(79)<1,GN,5> = SV.SPLT.TAX

                OE.GET.TAXJUR OID,GN
                OE.TAX.CLEAR OID,GN
                OE.TAX.CALC OID,GN,QSIGN,TAX.AMTS,GL.IDS

                * Based on the currency type, we need to calculate the
                * exchange rate at the time the order is copied
                IF LED(92)<1,GN,1> # '' THEN
                   XCURR.RATE.GET XRATE,LED(92)<1,GN,1>,LED(9)<1,GN>
                   LED(92)<1,GN,2> = XRATE
                END

                * Locate all LDID from this GEN Attb#49 so the
                * original Seq is presereed as much as possible.
                LDIDS   = LED(48)<1,GN>
                LD.CT   = DCOUNT(LDIDS,SVM)
                FOR LDN = 1 TO LD.CT
                   LDID    = LDIDS<1,1,LDN>
                   LOCATE LDID IN LED(49)<1> SETTING POS ELSE
                      LED(49)<1,-1> = LDID
                   END
                NEXT LDN
             END
          NEXT GN

          LDID.CT = LED(50) - 1
          FOR LDID = 1 TO LDID.CT
             LD.GET LDID
             * Do not copy Box# or Shipment#
             IF LD(35) THEN
                LD(35) = ''
                LD.PUT LDID
             END

             LD(118)<1,4> = ''

             * Don't copy Vendor Scorecard Shipping Accuracy data
             IF LD(133) THEN
                LD(133) = ''
                LD.PUT LDID
             END

             * Do not copy Book Letter of Credit Rebate Information
             IF LD(134) OR LED(137) THEN
                LD(134) = ""
                LD(137) = ""
                LD.PUT LDID
             END

             * Do not copy award date to new bid.
             IF LD(139) THEN
                LD(139) = ""
                LD.PUT LDID
             END

             IF LD(141) THEN
                LD(141) = ''
                LD.PUT LDID
             END

             IF LD(1) = 'S' OR (LD(5)='' AND LD(6) = '') THEN
                LD(4)  = 0
                LD(8)  = 0
                LD(10) = 0
                LD(12) = 0
                LD(27) = 0
                LD.PUT LDID
             END
          NEXT LDID

          DEL.LIST          = ''
          NEED.CONFIRMATION = NO

          FOR LDID = 1 TO LDID.CT
             LD.GET LDID
             IF LD(4)+0#0 THEN
                PN = LD(1)
                MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ""
                * If the prod. is scheduled for deletion, go check
                * whether there's anything left...
                DEL.FLAG = NO
                IF PRD(3) = 4 THEN
                   OE.CHK.DEL.PROD PN,GEN,PRC.BR,DEL.FLAG,PRMPT
                END

                * If there's nothing left, check whether user has auth
                * to add Delete status item, and if they even want to...
                IF DEL.FLAG THEN
                   NEED.CONFIRMATION = YES
                   DEL.LIST<LDID>    = LOWER(PRMPT)
                END
                DEL.FLAG = NO
             END
          NEXT LDID

          OE.CHECK.CERT OID,GEN,CERT.IDS,CERT.OK,,CERT.INFO
          IF NOT(CERT.OK) THEN
             IF JAVA.PROC$ THEN
                * The order has to be updated here because the if the
                * items aren't written to disk, they won't be remembered
                * upon recalling this routine.
                UPDATE.LEDGER OID,GEN.MV

                KEY      = 'CHECK.CERTIFICATION'
                VALUE<1> = CERT.IDS
                VALUE<2> = CERT.INFO
                RETURN TO FINISH
             END ELSE
                OE.AUTH.CERT OID,CERT.OK,CERT.IDS,CERT.INFO
             END
          END

          * If the certification was rejected, then we must generate an
          * error and the creation of the order cannot continue.
          IF NOT(CERT.OK) THEN
             KEY = "ERROR"
             UT.GET.PROMPT "%383",VALUE
             LED(88)<1,1> = LOWER(CERT.IDS)
             OE.UNLOCK.LED OID
             VALUE<3> = ""
             RETURN TO FINISH
          END
          * Checking GP
          GOSUB CHK.GP

          IF JAVA.PROC$ AND (KEY = "ERROR") THEN
             OE.UNLOCK.LED OID
             RETURN TO FINISH
          END

          IF (PRC.ERROR) THEN
             * Revert to old mode
             KEY = "ERROR"
             VALUE = 'GP% check failed! Copy aborted.'
             NEW.MODE = OLD.MODE
             OE.UNLOCK.LED OID
             RETURN TO FINISH
          END
          KEY   = 'COPY.IT2'
          VALUE = DEL.LIST

          IF NEED.CONFIRMATION THEN
             !WRITE LED TO DISK HERE
             UPDATE.LEDGER OID
             RETURN TO FINISH
          END ELSE
             GOTO COPY.IT2
          END
*-------------------------------------------------------------------------*
COPY.IT2: *
          DEL.LIST = RAISE(VALUE<1>)
          * If this is coming from Solar, we need to reload some variables
          * from the resps array.
          IF JAVA.PROC$ THEN
             LOCATE 'DEL.LIST' IN RESPS<1> SETTING POS THEN
                DEL.LIST = RAISE(RAISE(RESPS<2,POS>))
             END
          END
          QSIGN    = OE.GET.QSIGN(OID)

          MATREAD LED2 FROM LEDFILE,OLD.OID ELSE MAT LED2 = ''

          * See if we're copying from a MJB or a CHG order.
          OLD.SHARE.GEN              = NO
          HOLD.FLAG                  = ''
          IF LED2(128)<1,1> OR LED2(129)<1,2> THEN
             OLD.SHARE.GEN           = YES

             * Figure out the default hold flag.
             CTRL.ID                 = 'DFLT.LOT.ITEM.HOLD'
             READV HOLD.IT FROM CTRLFILE,CTRL.ID,1 ELSE HOLD.IT = 'N'
             IF HOLD.IT = 'S' OR HOLD.IT = 'B' THEN HOLD.FLAG   = YES
          END

          LDID.CT = LED(50) - 1
          FOR LDID = 1 TO LDID.CT
             LD.GET LDID
             BEGIN CASE
             CASE LD(1) = 'C' OR LD(1) = 'S'
                LD(24)      = OID:VM:LDID:VM:1
                MAT OLD.LD  = ''
                OLD.LD(24)  = LD(24)

                TEMP.GEN.MV = GEN.MV
                UPDATE.LEDGER.DET OID,LDID,QSIGN,TEMP.GEN.MV
             CASE LD(4)+0#0
                DEL.ITEM = DEL.LIST<LDID>

                GET.ALL.PRD PRC.BR,LD(1),QSIGN,GROUP
                GN.CT  = DCOUNT(LED(12),VM)
                FOR GN = 1 TO GN.CT

                   * Determine if Manual Override was done.
                   MAN.OVRD = NO
                   MAN.OVRD.COGS  = NO
                   MAN.OVRD.COST  = NO

                   * Check to see if there are any manual price, cogs
                   * or cost overrides and set the flags accordingly.
                   IF LD(9)<1,GN>  # "" THEN MAN.OVRD = YES
                   IF LD(11)<1,GN> # "" THEN MAN.OVRD.COGS=YES
                   IF LD(28)<1,GN> # "" THEN MAN.OVRD.COST=YES

                   * Check other overrides
                   LD.DATA = ''
                   PLC = ''
                   COST = ''
                   GOSUB CHECK.OVRDS

                   IF PRD(3) # '9' THEN
                      IF NOT(DEL.ITEM) THEN
                         LD(5)<1,GN> = SUM(LD(5)<1,GN>) + SUM(LD(6)<1,GN>)
                      END ELSE
                         DEL.QTY = SUM(LD(5)<1,GN>) + SUM(LD(6)<1,GN>)
                         LD(5)<1,GN> = ''
                         LOCATE LD(1) IN DELETED.PNS<1> SETTING DPOS ELSE
                            DELETED.PNS<1,-1> = LD(1)
                         END
                      END

                      LD(6)<1,GN> = ''

                      * Make sure we clear out kit component qtys/locs
                      * since this new gen will be open - we only want
                      * to have kit component qtys/locs on invoiced gens
                      IF LD(31) THEN
                         COMP.CT = DCOUNT(LD(31),VM)
                         FOR CMP = 1 TO COMP.CT
                            LD(93)<1,CMP,GN> = ''
                            LD(94)<1,CMP,GN> = ''
                         NEXT CMP
                      END

                      * For transfers, LD4 != LD5 + LD6
                      BEGIN CASE
                      CASE OLD.MODE = 'T' AND NEW.MODE = 'T'
                         TOTS = SUM(LD(5)<1,GN>) + SUM(LD(6)<1,GN>)
                         IF TOTS < 0 THEN
                            LD(4) = TOTS
                         END
                      CASE OTHERWISE
                         LD(4) = SUMMATION(LD(5)) + SUMMATION(LD(6))
                      END CASE

                      BEGIN CASE
                      * If manual override and user
                      * wants to copy pricing then we need override
                      * LD(8). Clear out LD(15) but
                      * need to maintain the Formula.
                      CASE COPY.PRC AND MAN.OVRD
                         TEMP.LD15  = LD(15)<1,GN>
                         LD(15)<1,GN> = ''
                         LD(15)<1,GN,1> = TEMP.LD15<1,1,1>
                         LD(15)<1,GN,2> = TEMP.LD15<1,1,2>

                         LD(9)<1,GN>  = LD(8)<1,GN>
                      * if copying pricing and not a manual override, then
                      * LD(8) needs to be populated.
                      * Clear out 15 to get rid of matrix.
                      CASE COPY.PRC = 1
                         * COPY.PRC is now a tri-state variable. 1 means
                         * always copy price, 2 copies manual overrides
                         * only which will be handled in first case stmt
                         LD(15)<1,GN> = ''
                         LD(9)<1,GN>  = LD(8)<1,GN>
                      * Not copying price, clear: Overrides,LD(15)
                      CASE OTHERWISE
                         LD(15)<1,GN> = ''
                         LD(9)<1,GN>  = ''
                         QO = LD(4) * QSIGN
                         OE.PRICE.ITEM OID,GEN,LDID,LD(1),QO,QSIGN,GROUP,,,,,,,YES
                      END CASE

                      * Recalculate COGS unless the user choose to copy
                      * COGS
                      IF (COPY.COGS # 1) AND NOT((COPY.COGS = 2) AND (MAN.OVRD.COGS = YES)) THEN
                         LD(10)<1,GN>            = ''
                         LD(11)<1,GN>            = ''
                         OE.UPD.COGS GN,,,YES,OID,LDID
                      END

                      * Recalculate COST unless the user choose to copy
                      * COST
                      IF (COPY.COST # 1) AND NOT((COPY.COST = 2) AND (MAN.OVRD.COST = YES)) THEN
                         LD(27)<1,GN>            = ''
                         LD(28)<1,GN>            = ''
                         OE.UPD.COMM.COST GN,,,YES,OID
                      END

                      LD(7)<1,GN> = 'S~'
                      IF LED(110) = 'B' THEN
                         OE.GET.CONSIGN.TYPE ST.CN,LED(110),CONSIGN.TYPE
                         LD(7)<1,GN> = CONSIGN.TYPE:'~'
                      END

                      LD.PUT LDID
                      SOE.CALC.DEPOSIT GN
                   END ELSE
                      LD(8)<1,GN> = 0
                      LD(10)<1,GN> = 0
                   END
                NEXT GN
                IF PRD(3) # '9' THEN
                   TP.CMTS     = LD(2)
                   PN.CMTS     = LD(3)
                   CLN.SYSTEM  = NO
                   GOSUB CLN.CMTS

                   IF PN.CMTS#"" THEN
                      LD(2) = TP.CMTS
                      LD(3) = PN.CMTS
                   END ELSE
                      LD(2) = ""
                      LD(3) = ""
                   END

                   OE.CUS.PN.CMT.GET BT.CN,ST.CN,CUS(66),LD(1),CMT
                   IF CMT # "" THEN
                      LOCATE "1" IN LD(2)<1> SETTING CPOS ELSE
                         CPOS = DCOUNT(LD(2)<1>,VM)+1
                         LD(2)<1,CPOS> = "1"
                      END
                      IF CMT<1> AND CMT<1>[1,1]#'@' THEN
                         LD(3)<1,CPOS,-1> = 'Your # ':CMT<1>
                         ADD.GEN.CMT = 1
                      END
                      IF CMT<2> THEN
                         LD(3)<1,CPOS,-1> = 'Your Location : ':CMT<2>
                         ADD.GEN.CMT = 1
                      END
                      IF CMT<3> THEN
                         LOCATE CMT<3> IN LD(3)<1,CPOS> SETTING XPOS ELSE
                            LD(3)<1,CPOS,-1> = CMT<3>
                            ADD.GEN.CMT = 1
                         END
                      END
                   END

                   LD(24)      = OID:VM:LDID:VM:1
                   LD(25)      = ''
                   LD(26)      = ''
                END ELSE
                   LD(4) = -1

                   * Need to set LD(24) for LotItems because down below
                   * OE.CREATE.NEW.GEN goes through UPDATE.LEDGER.DET
                   * which requires LD(24) to be current otherwise throws
                   * a fatal error.
                   LD(24) = OID:VM:LDID:VM:1
                   MAT OLD.LD = ''
                   OLD.LD(24) = LD(24)
                   UPDATE.LEDGER.DET OID,LDID,QSIGN,GEN.MV

                   * Figure out which gen this lot item is going on, and
                   * make sure that gen's LD5 is what has the total.
                   LOT.GN = 1
                   FOR GNX = 1 TO GN.CT
                      LOCATE LDID IN LED(48)<1,GNX> SETTING TRASH THEN
                         LOT.GN = GNX
                      END
                   NEXT GNX

                   * If we're coming from a MJB or CHG order, we need to
                   * create a new bid gen for every Lot Item.
                   IF OLD.SHARE.GEN THEN
                      * Since this is a brand new Bid, we'll assume that
                      * GEN 1 is the one we are basing the new gen on...
                      OE.CREATE.NEW.GEN NEW.GN,OID,'B',LED(9)<1,1>,LED(2)<1,1,1>,1

                      * Remove the old indicators.
                      IF LOT.GN THEN
                         LOCATE LDID IN LED(48)<1,LOT.GN> SETTING XX THEN
                            LED(48)     = DELETE(LED(48),1,LOT.GN,XX)
                         END
                      END

                      * Change the Lot.GN.
                      LOT.GN            = NEW.GN

                      * Set up the gen pointer.
                      LED(48)           = INSERT(LED(48),1,LOT.GN;LDID)
                      LED(98)<1,LOT.GN> = LDID
                      LD(91)<1,LOT.GN>  = HOLD.FLAG
                      GEN.MV<1,-1>      = GN
                   END

                   LD(5)                = ''
                   LD(5)<1,LOT.GN>      = -1
                   LD(6)<1>             = 0
                   LD(7)<1,LOT.GN>      = 'S~'

                   * Set Ship Qty to equal Ord Qty
                   FFCT    = DCOUNT(LD(60),VM)
                   FOR FFX = 1 TO FFCT
                      LD(61)<1,FFX,LOT.GN> = LD(60)<1,FFX>
                   NEXT FFX

                   * Make sure we're not allowing MJB, JR, or CHG order
                   * be copied over.
                   LD(105)     = ''
                   GOSUB NEW.LOT.CMT
                END
                MAT OLD.LD = ''
                OLD.LD(24)  = LD(24)
                LD(32)      = ''        ;*  Don't copy serial #'s
                LD(98)      = ''        ;*  Don't copy additional/secondary
                                         *  serial numbers
                LD(108)     = ''        ;*  Don't copy MJB linkers.

                TEMP.GEN.MV = GEN.MV
                UPDATE.LEDGER.DET OID,LDID,QSIGN,TEMP.GEN.MV

                * We have to call override logging directly from here
                * because OLD.LD(8) is set to null and UPDATE.LEDGER.DET
                * won't recognize that there may be a pricing override.
                * Using GEN so that each override is logged once for
                * each LDID.
                OE.PRC.OVRD.LOG OID,GEN,LDID

             CASE LD(1)#'' AND NUM(LD(1))
                * Make sure the new ledger detail gets updated with the
                * correct oid for UPDATE.LEDGER.DET integrity checking.
                LD(24)      = OID:VM:LDID:VM:1
                MAT OLD.LD  = ''
                OLD.LD(24)  = LD(24)
                LD(32)      = ''     ;*  Don't copy serial #'s
                LD(98)      = ''     ;*  Don't copy additional/secondary
                                      *  serial numbers
                LD(108)     = ''     ;*  Don't copy MJB linkers.

                TEMP.GEN.MV = GEN.MV
                UPDATE.LEDGER.DET OID,LDID,QSIGN,TEMP.GEN.MV
             END CASE
          NEXT LDID

          * Copy delivery labels
          TAG.LDIDS = ''
          LBL.PERS = ''
          TAGS = ''
          ERR.MSG = ''
          OE.ITEM.TAG.READ OLD.OID,,TAG.LDIDS,LBL.PERS,TAGS,ERR.MSG
          IF ERR.MSG = '' AND TAG.LDIDS # '' THEN
             OE.ITEM.TAG.WRITE OID,TAG.LDIDS,LBL.PERS,TAGS,ERR.MSG
          END


COPY.IT3: *
          GOSUB FINAL.CMTS

          UPDATE.LEDGER OID,GEN.MV

          * This will automatically combine any combine-able gens.
          GOSUB COMBINE.GENS
          KEY = 'UNLOCK.COPY'

          RETURN TO FINISH
*-------------------------------------------------------------------------*
PRE.CHECK:*** Make sure the copy can happen
          *** VALUE IN  -> Not used
          *** VALUE OUT -> CASE KEY = COPY.IT
          ***                 VALUE<1>   = Auth Selections (CUS(29))
          ***                 VALUE<2..> = LED
          ***              CASE KEY = 'ERROR'
          ***                 VALUE      = Error Message
          VALUE   = ''
          QSIGN   = OE.GET.QSIGN(OID)
          IF SAME.CN THEN ST.CN = LED(5)<1,GEN>
          IF NEW.MODE#'T' AND ST.CN=''  THEN
             KEY   = 'ERROR'
             VALUE = 'The entity field must be filled in!'
             RETURN TO FINISH
          END

          * Set up the ship to and billto customer,
          * also define the control id.
          BEGIN CASE
          CASE NEW.MODE = 'S'
             READV CTYPES FROM CUSFILE,ST.CN,7 ELSE CTYPES = ''
             IF CTYPES<1,1> THEN BT.CN = ST.CN ELSE
                READV BT.CN FROM CUSFILE,ST.CN,10 ELSE
                   KEY   = 'ERROR'
                   VALUE = 'The ship-to is invalid.'
                   RETURN TO FINISH
                END
             END

             * See if the shipto is authorized for this branch
             ENTITY.BR.CHK.AUTH ST.CN,PRC.BR,AUTH.OK
             IF NOT(AUTH.OK) THEN
                * See if the billto is authorized for this branch
                ENTITY.BR.CHK.AUTH BT.CN,PRC.BR,AUTH.OK
                IF NOT(AUTH.OK) THEN
                   * If still not authorized, prompt user if they would
                   * like to activate for one time use, activate for branch
                   * or to not activate at all.
                   ENT.AUTH.BR.OVERRIDE ST.CN,PRC.BR,AUTH.OK,BR.AUTH.MSG

                   * User selected not to activate.
                   IF NOT(AUTH.OK) THEN
                      IF JAVA.PROC$ THEN
                         KEY   = 'ERROR'
                         VALUE=BR.AUTH.MSG
                      END
                      RETURN TO FINISH
                   END

                END
             END

             * See if the user has the proper authorization to create a
             * new Sales Order.
             KEY.ID = "SOE.ALLOWED"

             CHECK.KEY KEY.ID,OE.ALLOW,OE.LEVEL
             *regardless of the level, if SOE.ALLOWED is assigned
             * allow them to create the bid
             IF NOT(OE.ALLOW) THEN RETURN TO FINISH

             !IF (OE.LEVEL < 2) THEN
             !   KEY    = "ERROR"
             !   VALUE  = "You Are Not Authorized to Create New Sales"
             !   VALUE := " Orders."
             !   RETURN TO FINISH
             !END
          CASE NEW.MODE = 'P'
             READV CTYPES FROM CUSFILE,ST.CN,7 ELSE CTYPES = ''
             IF CTYPES<1,5> THEN BT.CN = ST.CN ELSE
                READV BT.CN FROM CUSFILE,ST.CN,11 ELSE
                   KEY   = 'ERROR'
                   VALUE = 'The ship-from is invalid.'
                   RETURN TO FINISH
                END
             END

             * See if the user has the proper authorization to create a
             * new Purchase Order.
             KEY.ID = "POE.ALLOWED"
             CHECK.KEY KEY.ID,OE.ALLOW,OE.LEVEL
             *if poe is allowed, allow them to create thepurchase order bid

             IF NOT(OE.ALLOW) THEN RETURN TO FINISH
             !IF (OE.LEVEL < 2) THEN
             !   KEY    = "ERROR"
             !   VALUE  = "You Are Not Authorized to Create New Pruchase"
             !   VALUE := " Orders."
             !   RETURN TO FINISH
             !END
          CASE NEW.MODE = 'T'
             * These lines will get the bill-to and ship-to
             * numbers from the terrfile for the given branches
             READV BT.CN FROM TERRFILE,PRC.BR,4 ELSE BT.CN = ''
             READV ST.CN FROM TERRFILE,RCV.BR,4 ELSE ST.CN = ''

             IF RCV.BR = '' OR RCV.BR = PRC.BR THEN
                KEY   = 'ERROR'
                VALUE = 'The receiving branch is invalid.'
                RETURN TO FINISH
             END
             LOCATE RCV.BR IN SECURITY<9> SETTING POS ELSE
                KEY   = 'ERROR'
                VALUE = 'The receiving branch is invalid.'
                RETURN TO FINISH
             END

             * See if the user has the proper authorization to create a
             * new Transfer Order.
             KEY.ID = "TOE.ALLOWED"
             CHECK.KEY KEY.ID,OE.ALLOW,OE.LEVEL
             IF (OE.LEVEL < 2) THEN
                KEY    = "ERROR"
                VALUE  = "You Are Not Authorized to Create New Transfer"
                VALUE := " Orders."
                RETURN TO FINISH
             END
          END CASE
          LDID.CT = LED(50) - 1
          FOR LDID = 1 TO LDID.CT
             LD.GET LDID
             IF LD(4)+0#0 THEN
                PN = LD(1)
                MATREAD PRD FROM PRDFILE,PN ELSE MAT PRD = ""
                * Validate that product is authorized at this branch
                PRD.VAL.BRCH PN,RCV.BR,VAL.LIST,INV.LIST
                IF INV.LIST THEN
                   KEY    = 'ERROR'
                   VALUE  = 'Product ':PN:'/':PRD(1):' not valid for '
                   VALUE := 'branch ':RCV.BR:'.'
                   RETURN TO FINISH
                END ELSE
                   PRD.VAL.BRCH PN,PRC.BR,VAL.LIST,INV.LIST
                   IF INV.LIST THEN
                      KEY    = 'ERROR'
                      VALUE  = 'Product ':PN:'/':PRD(1):' not valid for '
                      VALUE := 'branch ':PRC.BR:'.'
                      RETURN TO FINISH
                   END
                END
             !-------------
                * If the prod. is scheduled for deletion, go check
                * whether there's anything left...
                IF JAVA.PROC$ THEN
                   DEL.FLAG = NO
                   IF PRD(3) = 4 THEN
                      OE.CHK.DEL.PROD PN,GEN,PRC.BR,DEL.FLAG,PRMPT
                   END

                   * If there's nothing left, check whether user has auth
                   * to add Delete status item, and if they even want to...
                   IF DEL.FLAG THEN
                      CONVERT AM TO ' ' IN PROMPT
                      CONVERT VM TO ' ' IN PROMPT
                      DEL.LIST<LDID>    = LOWER(LOWER(PRMPT))
                   END
                END
             !-------------
             END
          NEXT LDID
          IF GENS[1,1] = 'C' THEN GENS = 'C'
* See comments at top of code for information about how which copy
* routine is chosen.


          IF NEW.MODE # OLD.MODE OR GENS = 'C' THEN
             GN.CT = DCOUNT(OLED(12),VM)
             IF GENS = 'C' OR OLD.MODE = 'T' THEN GN.CT = 1
             IF GN.CT = 1 AND OLED(8)<1,GEN> AND NOT(JAVA.PROC$) THEN
                KEY   = 'GEN.PROMPT'
                VALUE = ''
             END ELSE
                IF JAVA.PROC$ AND NEW.MODE = 'S' THEN
                   GET.CUS PRC.BR,BT.CN,ST.CN,QSIGN
                   IF CUSS(72)='' THEN
                      GEN.LIST<1,1> = '&PROMPT&'
                   END ELSE
                      GEN.LIST<1,1> = CUSS(72)
                   END
                   KEY      = 'CHANGE.MODE'
                   VALUE    = ''
                   VALUE<2> = GEN.LIST
                   IF GEN.LIST AND NOT(HAS.GEN.LIST) THEN
                      KEY    = 'SINGLE.GEN.PROMPT'
                      ACTION.ADD MSGS,"SALES.SOURCE",,,,,LOWER(GEN.LIST)
                      GOSUB GET.STYPE.LIST
                   END
                   RETURN TO FINISH
                END ELSE
                   GOTO CHANGE.MODE
                END
             END
          END ELSE
             CERT.IDS = RAISE(LED(88)<1,1>)
             GET.CUS PRC.BR,BT.CN,ST.CN,QSIGN

             GOSUB BUILD.GEN.LIST
             GOSUB GET.NEW.OID

             KEY      = 'COPY.IT'
             VALUE    = CUS(29)
             VALUE<2> = GEN.LIST
             VALUE<3> = OID
          END

          IF JAVA.PROC$ THEN
             CERT.OK = YES
             CERT.IDS = RAISE(LED(88)<1,1>)
             OE.CHECK.CERT OID,GEN,CERT.IDS,CERT.OK,,CERT.INFO

             IF GEN.LIST AND NOT(HAS.GEN.LIST) THEN
                KEY    = 'PROMPT'
                ACTION.ADD MSGS,"SALES.SOURCE",,,,,LOWER(GEN.LIST)
                GOSUB GET.STYPE.LIST
             END
             IF CUS(29) THEN
                KEY    = 'PROMPT'
                ACTION.ADD MSGS,"AUTH.SEL",,,,,LOWER(CUS(29))
             END
             IF DEL.LIST THEN
                KEY    = 'PROMPT'
                ACTION.ADD MSGS,"DEL.LIST",,,,,LOWER(LOWER(DEL.LIST))
             END
             IF NOT(CERT.OK) THEN
                KEY    = 'PROMPT'
                ACTION.ADD MSGS,"CERTIFY",,,,,CERT.INFO
             END
          END
          RETURN TO FINISH
*-------------------------------------------------------------------------*
*- Utility Routines ------------------------------------------------------*
*-------------------------------------------------------------------------*
GET.STYPE.LIST: * Get sales type list
          READ GL.SALES.TYPE FROM CTRLFILE, 'GL.SALES.TYPE' ELSE GL.SALES.TYPE = ''
          LOCATE 'DIR' IN GL.SALES.TYPE<2> SETTING POS THEN
             GL.SALES.TYPE = DELETE(GL.SALES.TYPE, 1, POS);
             GL.SALES.TYPE = DELETE(GL.SALES.TYPE, 2, POS);
          END
          LOCATE 'CREDIT' IN GL.SALES.TYPE<2> SETTING POS THEN
             GL.SALES.TYPE = DELETE(GL.SALES.TYPE, 1, POS);
             GL.SALES.TYPE = DELETE(GL.SALES.TYPE, 2, POS);
          END

          RETURN
*-------------------------------------------------------------------------*
BUILD.GEN.LIST: *
          *** Build an array, GEN.LIST, corresponding to all the gens.
          *** The value for the gen in question can have 3 different
          *** value types:
          ***    &DELETE& - This gen needs to be deleted
          ***    &PROMPT& - Need to call OE.INIT.MODE to get the gl src
          ***    CUSS(72) - The gl source to use for this gen
          *** This same list will be passed back to this update routine
          *** where each gen value has either &DELETE& or the gl source.
          *** This update routine will do the actual deleting of a gen.

          GN.CT       = DCOUNT(LED(12),VM)
          LOT.DISCARD = ''
          GEN.LIST    = ''

          FOR GX = GN.CT TO 1 STEP -1
* Check LED(98) to see if this gen has a lot item on it.  If there is a
* lot item on this gen, then see if it's the first time this specific
* lot item has been found.  If it is the first time this lot item has
* been found, then don't delete it, because the lot item has to get
* copied over.  If this isn't the first time this lot item has been
* found, then set this gen to be deleted...  Later logic will have to
* make sure to copy over all the correct detail totals for the lot item.
             LOT.LD = LED(98)<1,GX>
             IF NOT(LOT.LD)           THEN CONTINUE
             LOCATE LOT.LD IN LOT.DISCARD<2> SETTING TRASH THEN
                LOT.DISCARD<1,GX> = 1
             END ELSE
                LOT.DISCARD<2,-1> = LOT.LD
             END
          NEXT GX

          FOR GN = GN.CT TO 1 STEP -1
             IF LED(6)<1,GN>='$' OR LED(48)<1,GN>='DIRECT' OR (GENS='C' AND GN#GEN) OR (GENS = 'OG' AND LED(8)<1,GN>) OR LOT.DISCARD<1,GN> THEN
                GEN.LIST<1,GN> = '&DELETE&'
             END ELSE
                IF CUSS(72)='' THEN
                   GEN.LIST<1,GN> = '&PROMPT&'
                END ELSE
                   GEN.LIST<1,GN> = CUSS(72)
                END
             END
          NEXT GN

          RETURN
*-------------------------------------------------------------------------*
GET.NEW.OID: *
          BEGIN CASE
          CASE NEW.MODE = 'P'  ; CTRL.ID = 'NEXT.PON'
          CASE NEW.MODE = 'T'  ; CTRL.ID = 'NEXT.TRN'
          CASE OTHERWISE       ; CTRL.ID = 'NEXT.ORN'
          END CASE

RET.OID:  GET.NEW.ID CTRL.ID,OID,NEW.MODE,'R%':(OID.LGTH$-1),LEDFILE
          OE.LOCK.LED OID,LOCK.ERR
          IF LOCK.ERR THEN GOTO RET.OID

          RETURN
*-------------------------------------------------------------------------*
ASSEMBLE.CMTS:*
          * Assemble the comment/comment type pairs into the format req'd
          * by OE.CREATE.LEDGER.  SSVM 1 = Comment Line, SSVM 2 = Type.
          ORIG.CMTS = PN.CMTS
          PN.CMTS = ''
          PN.IX = 0

          TP.CT = DCOUNT(TP.CMTS,VM)
          FOR TPX = 1 TO TP.CT
             TP = TP.CMTS<1,TPX>
             O.CT = DCOUNT(ORIG.CMTS<1,TPX>,SVM)
             FOR OX = 1 TO O.CT
                PN.IX +=1
                PN.CMTS<1,1,PN.IX> = LOWER(ORIG.CMTS<1,TPX,OX>:SVM:TP)
             NEXT OX
          NEXT TPX

          RETURN
*-------------------------------------------------------------------------*
CLN.CMTS: * Routine to get rid of 'your #' and 'your loc' comments.
          * This routine assumes PN.CMTS,TP.CMTS, and CLN.SYSTEM are valid.
          * This routine will also make sure any (most) non-Rel7 compliant
          * set of comments are represented in a Rel7 manner.  (I.e., the
          * comment lines themselves are at the SVM level, with VM's
          * seperating comment types in both PN.CMTS and TP.CMTS.)
          OUT.TYPS = ''
          OUT.CMTS = ''

          TP.CT = DCOUNT(PN.CMTS,VM)
          FOR TP = 1 TO TP.CT
             * If there's not a Comment Type, default it to '1 - General'.
             IF NOT(TP.CMTS<1,TP>) THEN TP.VAL = 1 ELSE
                TP.VAL = TP.CMTS<1,TP>
             END
             CMT.CT = DCOUNT(PN.CMTS<1,TP>,SVM)
             FOR CC = 1 TO CMT.CT
                ONE.CMT = PN.CMTS<1,TP,CC>
                TST.CMT = OCONV(ONE.CMT,'MCU')
                * We're not including the cust specific pn's or loc's.
                IF TRIM(ONE.CMT) = ''                  THEN CONTINUE
                IF TST.CMT[1,6]='YOUR #'               THEN CONTINUE
                IF TST.CMT[1,13]='YOUR LOCATION'       THEN CONTINUE

                LOCATE TP.VAL IN OUT.TYPS<1> SETTING DEST.POS ELSE
                   * If TP.VAL isn't in the list, the position will be
                   * set to the next open value.
                   OUT.TYPS<1,DEST.POS> = TP.VAL
                END
                DEST.SVM = DCOUNT(OUT.CMTS<1,DEST.POS>,SVM)+1
                OUT.CMTS<1,DEST.POS,DEST.SVM> = ONE.CMT
             NEXT CC
          NEXT TP

          TP.CMTS = OUT.TYPS
          PN.CMTS = OUT.CMTS

          * Remove standard system comments.
          IF CLN.SYSTEM THEN
             * NONSTOCK.COMMENT - read from CONTROL.BR
             * Need to clean out standard comments for each ship branch
             * in OLED
             CLN.BRS = ''  ;* Branches to clean comments for
             GN.CNT = DCOUNT(OLED(2),VM)
             FOR GNC = 1 TO GN.CNT
                GNSB = OLED(2)<1,GNC,2>
                IF NOT(GNSB) THEN CONTINUE
                LOCATE GNSB IN CLN.BRS SETTING NADA ELSE
                   CLN.BRS<-1> = GNSB
                END
             NEXT GNC
             CB.CNT = DCOUNT(CLN.BRS,AM)
             FOR CBC = 1 TO CB.CNT
                SHP.BR  = CLN.BRS<CBC>
                CTRL.ID = "NONSTOCK.COMMENT~":SHP.BR
                READ STD.CMNTS FROM CTRBFILE,CTRL.ID ELSE STD.CMNTS = ""
                NONSTOCK.COMMENTS.API STD.CMNTS,SHP.BR
                GOSUB STD.CMNT
             NEXT CBC

             * HAZARD.COMMENT - read from CONTROL
             CTRL.ID = "HAZARD.COMMENT"
             READ STD.CMNTS FROM CTRLFILE,CTRL.ID ELSE STD.CMNTS = ""
             GOSUB STD.CMNT
          END

          RETURN
*-------------------------------------------------------------------------*
STD.CMNT: * Loop through the comment VM's and SVM's to remove any
          * standard comments.
          FOR CMX = 1 TO DCOUNT(STD.CMNTS,VM)
             CMNT = STD.CMNTS<1,CMX>
             IF CMNT#'' THEN
                * Have to step through each comment type.
                CX.CT = DCOUNT(PN.CMTS,VM)
                FOR CX = CX.CT TO 1 STEP -1
                   LOCATE CMNT IN PN.CMTS<1,CX> SETTING CLOC THEN
                      PN.CMTS = DELETE(PN.CMTS,1,CX,CLOC)
                   END
                   * Don't perpetuate blanked out comment types.
                   IF NOT(PN.CMTS<1,CX>) THEN
                      PN.CMTS = DELETE(PN.CMTS,1,CX)
                      TP.CMTS = DELETE(TP.CMTS,1,CX)
                   END
                NEXT CX
             END
          NEXT CMX

          RETURN
*-------------------------------------------------------------------------*
NEW.LOT.CMT: * Add the created comment to the lot item copied

          * Log a General Comment (Code 1000)
          CMT = "** Lot Item Copied From ":OLD.OID:" **"
          OE.LOT.ADD.COMMENT OID,,LDID,CMT,,,,"LI.GCOM"

          OCDT   = LD(66)<1,1>
          OFDT   = LD(66)<1,2>
          OPRC   = ICONV(OCONV(LD(67)<1,1>,"MR9"),"MR2")
          OCST   = ICONV(OCONV(LD(67)<1,2>,"MR9"),"MR2")

          * Log an Original Lot Price (Code 1001)
          CMT    = "Original Lot Price $ ":OCONV(OPRC,"MR2")
          OE.LOT.ADD.COMMENT OID,,LDID,CMT,OPRC,0,,"LI.OPRC"

          * Log an Original Lot Cost (Code 1002)
          CMT    = "Original Lot Cost  $ ":OCONV(OCST,"MR2")
          OE.LOT.ADD.COMMENT OID,,LDID,CMT,OCST,0,,"LI.OCST"

          * Log a General Comment (Code 1000)
          CMT      = "Original Expected Date : ":OCONV(OCDT,"D4/")
          CMT<1,2> = "Original Followup Date : ":OCONV(OFDT,"D4/")
          OE.LOT.ADD.COMMENT OID,,LDID,CMT,,,,"LI.GCOM"

          RETURN
*-------------------------------------------------------------------------*
MULTIPLE.GENS:
          QSIGN   = OE.GET.QSIGN(OID)
          LDID.CT = LED(50)-1
          * Loop through each LDID and see if this
          * product should be added to the order
          FOR LDID = 1 TO LDID.CT
             LD.GET LDID
             BEGIN CASE
             CASE LD(1) = 'C' AND NOT(NEW.MODE = 'P' AND OLD.MODE = 'S')
                   PNS<1,-1> = 'C'
                   PLC = DCOUNT(PNS,VM)
                   QTYS<1,PLC> = LOWER(LOWER(LD(2)))
                   CMNTS<1,PLC> = LOWER(LD(3))
             CASE LD(4)+0#0
                GET.ALL.PRD PRC.BR,LD(1),QSIGN

                * Don't copy lot items if the customer/mode are
                * different (I don't believe any gens can make it
                * to this part of the code unless the mode has
                * been changed, but we don't do lot items on any
                * modes other than 'S' modes.)
                IF PRD(3) = '9' THEN CONTINUE

                PN      = LD(1)
                PN.QTYS = 0

                * Copy OPEN GENS only
                IF GENS = 'OG' THEN
                   GEN.CT = DCOUNT(LED(12),VM)
                   OPEN.FOUND = NO
                   FOR GN = 1 TO GEN.CT
                      QS     = (SUM(LD(5)<1,GN>) + SUM(LD(6)<1,GN>))
                      STATUS = LED(6)<1,GN>
                      IF QS AND STATUS#'X' AND NOT(LED(8)<1,GN>) THEN
                         OPEN.FOUND = YES
                      END
                   NEXT GN
                   IF NOT(OPEN.FOUND) THEN CONTINUE
                END

                * Make sure the your loc and your # comments are removed.
                TP.CMTS = LD(2)
                PN.CMTS = LD(3)
                CLN.SYSTEM = YES
                GOSUB CLN.CMTS
                FOR GN = 1 TO GN.CT
                   IF GENS = 'C' OR OLD.MODE = 'T' THEN GN = GEN
                   PN.QTYS += QSIGN*(SUM(LD(5)<1,GN>) + SUM(LD(6)<1,GN>))
                NEXT GN


                IF PN.QTYS # 0 THEN
                   PNS<1,-1>  = PN
                   PLC = DCOUNT(PNS,VM)
                   IF NEW.MODE = 'T' THEN PN.QTYS = ABS(PN.QTYS)
                   QTYS<1,PLC> = PN.QTYS
                   GOSUB ASSEMBLE.CMTS
                   CMNTS<1,PLC> = PN.CMTS
                END
             END CASE
             IF NEW.MODE = 'P' OR NEW.MODE = 'T' THEN
                BEGIN CASE
                   CASE COPT = 2
                      RECOST = (LD(11)<1,GEN> # '') OR (LD(28)<1,GEN> # '')
                   CASE COPT = 3
                      RECOST = YES
                   CASE OTHERWISE
                      COGS = ''
                      RECOST = NO
                END CASE

                PN.CNT = DCOUNT(PNS,VM)
                IF (SUMMATION(LD(5)) + SUMMATION(LD(6))) # 0 THEN
                   IF NEW.MODE = 'P' THEN
                      RECST<1,PN.CNT> = RECOST
                      FOR GN2 = 1 TO GEN
                         IF LD(10)<1,GN2> # '' THEN
                            COST<1,PN.CNT> = LD(10)<1,GN2>
                            EXIT
                         END
                      NEXT GN2
                      IF COST<1,PN.CNT> = '' THEN
                         COST<1,PN.CNT> = 0
                      END
                   END ELSE
                      RECST = ''
                      FOR GN2 = 1 TO GEN
                         IF LD(10)<1,GN2> # '' OR LD(27)<1,GN2> # '' THEN
                            IF RECOST = NO THEN
                               COST<1,PN.CNT> = ''
                               COGS<1,PN.CNT> = ''
                            END ELSE
                               IF COPT = 3 THEN
                                  COGS<1,PN.CNT> = LD(10)<1,GN2>
                                  COST<1,PN.CNT> = LD(27)<1,GN2>
                               END ELSE
                                  IF LD(11)<1,GN2> # '' THEN
                                     COGS<1,PN.CNT> = LD(10)<1,GN2>
                                  END ELSE
                                     COGS<1,PN.CNT> = ''
                                  END
                                  IF LD(28)<1,GN2> # '' THEN
                                     COST<1,PN.CNT> = LD(27)<1,GN2>
                                  END ELSE
                                     COST<1,PN.CNT> = ''
                                  END
                               END
                            END
                            EXIT
                         END
                      NEXT GN2
                   END
                END
             END
SKIPLD:   NEXT LDID

          RETURN
*-------------------------------------------------------------------------*
SINGLE.GEN:  *

          IF LED(8)<1,GEN> THEN
             LDIDS = RAISE(LED(48)<1,GEN>)
          END ELSE
             LDIDS = LED(49)
          END
          LDID.CT = DCOUNT(LDIDS<1>,VM)
          QSIGN   = OE.GET.QSIGN(OID)

          * Loop through each LDID and see if this product
          * should be added to the order
          FOR LDN = 1 TO LDID.CT
             LDID    = LDIDS<1,LDN>
             LD.GET LDID

             * If there is not a quantity on the gen then we should skip
             * the line item. We still need to do the remaining checks
             * for copying sales to sales.
             GEN.QTY = QSIGN*(SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>))
             IF OLD.MODE="S" AND NEW.MODE="P" AND GEN.QTY=0 THEN CONTINUE

             BEGIN CASE
             CASE LD(1) = 'C' AND NOT(NEW.MODE = 'P' AND OLD.MODE = 'S')
                   PNS<1,-1> = 'C'
                   PLC = DCOUNT(PNS,VM)
                   QTYS<1,PLC> = LOWER(LOWER(LD(2)))
                   CMNTS<1,PLC> = LOWER(LD(3))
             CASE LD(4)+0#0
                GET.ALL.PRD PRC.BR,LD(1),QSIGN

                * Don't copy lot items if the customer/mode are different
                * (I believe that lot item generations are able to reach
                * this section of code, but we will only copy to a sales
                * order.)
                IF NEW.MODE # 'S' AND PRD(3) = '9' THEN GOTO SKIP.ONE

                PN      = LD(1)
                PN.QTYS = 0

                * Make sure the your loc and your # comments are removed.
                TP.CMTS = LD(2)
                PN.CMTS = LD(3)
                CLN.SYSTEM = YES
                GOSUB CLN.CMTS

                OE.CUS.PN.CMT.GET BT.CN,ST.CN,CUS(66),PN,CMT
                * CMT<1> = CUS.PN
                * CMT<2> = LOCATION CODE
                * CMT<3> = COMMENTS SPEC PNS.
                * CMT<1> and CMT<2> are cleaned out in CLN.CMTS,
                * CMT<3> has no individual indicators for it's
                * existence, so we find in in OE.CUS...,
                * and then we chop it outta PN.CMTS.

                CUS.CMT.CT = DCOUNT(CMT<3>,VM)
                IF CUS.CMT.CT THEN
                   TP.CT = DCOUNT(TP.CMTS,VM)
                   FOR TPX = TP.CT TO 1 STEP -1
                      FOR CCX = 1 TO CUS.CMT.CT
                         CUS.CMT = CMT<3,CCX>
                         LOCATE CUS.CMT IN PN.CMTS<1,TP> SETTING CUS.CMT.POS THEN
                            PN.CMTS = DELETE(PN.CMTS,1,TP,CUS.CMT.POS)
                         END
                      NEXT CCX
                      * Be sure there are no blank comments.
                      IF PN.CMTS<1,TP> = '' THEN
                         PN.CMTS = DELETE(PN.CMTS,1,TP)
                         TP.CMTS = DELETE(TP.CMTS,1,TP)
                      END
                   NEXT TPX
                END

                PN.QTYS += QSIGN*(SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>))

                IF PN.QTYS # 0 THEN
                   PNS<1,-1>  = PN
                   PLC = DCOUNT(PNS,VM)
                   IF NEW.MODE = 'T' THEN PN.QTYS = ABS(PN.QTYS)
                   QTYS<1,PLC> = PN.QTYS
                   * Determine if Manual Override was done.
                   MAN.OVRD = NO
                   MAN.OVRD.COGS = NO
                   MAN.OVRD.COST = NO

                   * Check to see if there are any price, cogs or cost
                   * overrides and set the flags accordingly.
                   IF LD(9)<1,GEN>  # "" THEN MAN.OVRD = YES
                   IF LD(11)<1,GEN> # "" THEN MAN.OVRD.COGS=YES
                   IF LD(28)<1,GEN> # "" THEN MAN.OVRD.COST=YES

                   IF NEW.MODE # 'T' THEN GOSUB CHECK.OVRDS
                   GOSUB ASSEMBLE.CMTS
                   CMNTS<1,PLC> = PN.CMTS
                   IF NEW.MODE = 'S' AND PRD(3) = 9 THEN
                      LOT.PNS<-1>       = PN
                      LPLC = DCOUNT(LOT.PNS,AM)
                      LOT.PN.LIST<LPLC>   = LD(59)
                      LOT.QTY.ORD<LPLC>   = LD(60)
                      LOT.PRC.LIST<LPLC>  = LD(62)
                      LOT.CST.LIST<LPLC>  = LD(63)
                      LOT.TYP.LIST<LPLC>  = LD(109)
                      LOT.DT.LIST<LPLC>   = LD(66)
                      LOT.SUM.LIST<LPLC>  = LD(67)
                   END
                   * Get kit component info
                   IF LD(30) # '' THEN
                      PNS<1,PLC,4> = LOWER(LOWER(LD(30)))
                      PNS<1,PLC,5> = LOWER(LOWER(LD(31)))
                      PNS<1,PLC,6> = LOWER(LOWER(LD(37)))
                      PNS<1,PLC,7> = LOWER(LOWER(LD(38)))
                      PNS<1,PLC,8> = LOWER(LOWER(LD(39)))
                   END
                END
             END CASE
             PN.CNT = DCOUNT(PNS,VM)
             IF NEW.MODE = 'P' OR NEW.MODE = 'T' THEN
                BEGIN CASE
                   CASE COPT = 2
                      RECOST = (LD(11)<1,GEN> # '') OR (LD(28)<1,GEN> # '')
                   CASE COPT = 3;  RECOST = YES
                   CASE OTHERWISE
                      RECOST = NO
                      IF NEW.MODE = 'T' THEN
                         COGS = ''
                      END
                END CASE
                IF NEW.MODE = 'P' THEN
                   RECST<1,PN.CNT> = RECOST
                   COST<1,PN.CNT>  = LD(10)<1,GEN>
                END ELSE
                   IF RECOST = NO THEN
                      COGS<1,PN.CNT> = ''
                      COST<1,PN.CNT> = ''
                   END ELSE
                      IF COPT = 3 THEN
                         COGS<1,PN.CNT> = LD(10)<1,GEN>
                         COST<1,PN.CNT> = LD(27)<1,GEN>
                      END ELSE
                         IF LD(11)<1,GEN> # '' THEN
                            COGS<1,PN.CNT> = LD(10)<1,GEN>
                         END ELSE
                            COGS<1,PN.CNT> = ''
                         END
                         IF LD(28)<1,GEN> # '' THEN
                            COST<1,PN.CNT> = LD(27)<1,GEN>
                         END ELSE
                            COST<1,PN.CNT> = ''
                         END
                      END
                   END
                   RECST = ''
                END
             END
SKIP.ONE: NEXT LDID

          RETURN
*-------------------------------------------------------------------------*
CHECK.OVRDS:*** Preserve overrides, but only for the same type of order and
            *** only if we are copying the price.

          IF OLD.MODE # NEW.MODE THEN RETURN
          BEGIN CASE
          CASE COPY.PRC AND MAN.OVRD
             TEMP.LD15  = LD(15)<1,GEN>
             LD(15)<1,GEN> = ''
             LD(15)<1,GEN,1> = TEMP.LD15<1,1,1>
             LD(15)<1,GEN,2> = TEMP.LD15<1,1,2>

             LD(9)<1,GEN>  = LD(8)<1,GEN>
          CASE COPY.PRC = 1
             * COPY.PRC is now a tri-state variable. 1 means
             * always copy price, 2 copies manual overrides
             * only which will be handled in first case stmt
             LD.DATA<PLC,8>  = LD(8)<1,GEN>
             LD.DATA<PLC,15> = LOWER(LD(15)<1,GEN>)
             LD.DATA<PLC,9>  = LD(8)<1,GEN>
          CASE OTHERWISE
             * Not copying price, clear: Overrides,LD(15),LD(29)
             * and recost.
             LD(15)<1,GEN> = ''
             LD(9)<1,GEN>  = ''
          END CASE
          IF LD(9)<1,GEN> # '' THEN
             NETS<1,PLC> = LD(8)<1,GEN>
          END

          * Recalculate COGS unless the user choose to copy
          * COGS

          IF (COPY.COGS # 1) AND NOT((COPY.COGS = 2) AND (MAN.OVRD.COGS = YES)) THEN
             LD(10)<1,GEN>            = ''
             LD(11)<1,GEN>            = ''
             OE.UPD.COGS GEN,,,YES,OID,LDID
          END ELSE
             IF COPY.COGS = 1 THEN
                LD(11)<1,GEN> = LD(10)<1,GEN>
             END
          END

          * Recalculate COST unless the user choose to copy
          * COST
          IF (COPY.COST # 1) AND NOT((COPY.COST = 2) AND (MAN.OVRD.COST = YES)) THEN
             LD(27)<1,GEN>            = ''
             LD(28)<1,GEN>            = ''
             OE.UPD.COMM.COST GEN,,,YES,OID
          END ELSE
             IF COPY.COST = 1 THEN
                LD(28)<1,GEN> = LD(27)<1,GEN>
             END
          END

          IF LD(11)<1,GEN> # "" THEN
             COGS<1,PLC> = LD(10)<1,GEN>
          END

          IF LD(28)<1,GEN> # "" THEN
             COST<1,PLC> = LD(27)<1,GEN>
          END

          IF COPY.COST = 1 THEN
             * set up LD.DATA which will later be passed to
             * OE.CREATE.LEDGER.  This will assure that cost
             * cost information is copied without showing as
             * overrides.
             LD.DATA<PLC,27> = LD(27)<1,GEN>
             * if cost override information exists we want to also
             * copy that to the new order.
             IF LD(29)<1,GEN> THEN
                WRK = RAISE(LD.DATA<PLC,29>)
                WRK<1,1,8>  = LD(29)<1,GEN,8>
                WRK<1,1,9>  = LD(29)<1,GEN,9>
                WRK<1,1,10> = LD(29)<1,GEN,10>
                WRK<1,1,11> = LD(29)<1,GEN,11>
                WRK<1,1,12> = LD(29)<1,GEN,12>
                WRK<1,1,13> = LD(29)<1,GEN,13>
                WRK<1,1,14> = LD(29)<1,GEN,14>
                LD.DATA<PLC,29> = LOWER(WRK)
             END
          END

          IF COPY.COGS = 1 THEN
             * set up LD.DATA which will later be passed to
             * OE.CREATE.LEDGER.  This will assure that cost
             * cost information is copied without showing as
             * overrides.
             LD.DATA<PLC,10> = LD(10)<1,GEN>
             * if cost override information exists we want to also
             * copy that to the new order.
             IF LD(29)<1,GEN> THEN
                WRK = RAISE(LD.DATA<PLC,29>)
                WRK<1,1,1> = LD(29)<1,GEN,1>
                WRK<1,1,2> = LD(29)<1,GEN,2>
                WRK<1,1,3> = LD(29)<1,GEN,3>
                WRK<1,1,4> = LD(29)<1,GEN,4>
                WRK<1,1,5> = LD(29)<1,GEN,5>
                WRK<1,1,6> = LD(29)<1,GEN,6>
                WRK<1,1,7> = LD(29)<1,GEN,7>
                LD.DATA<PLC,29> = LOWER(WRK)
             END
          END

          RETURN
*-------------------------------------------------------------------------*
FINAL.CMTS:*
          * Add this comment to the ledger log.
          COMMENT = 'Copied from : ':OLD.OID
          OE.ADD.COMMENT OID,'ALL',COMMENT

          * Add this comment for omitted items marked as "Delete"
          FOR I = 1 TO DCOUNT(DELETED.PNS,VM)
             DPN      = DELETED.PNS<1,I>
             COMMENT  = 'PN#':DPN:' was omitted from the order '
             COMMENT := 'during the copy process'
             OE.ADD.COMMENT OID,'ALL',COMMENT
          NEXT I

          RETURN
*-------------------------------------------------------------------------*
COPY.GEN: *** copy any data over that might need to be carried over.

          LED.DATA       = ''

          LED.DATA<22>   = LED(22)<1,GEN>         ;* Price Date
          LED.DATA<31>   = LED(31)<1,GEN>         ;* Price Exp Date

          * Only copy when copying to the same mode (ie. Sales Order to
          * Sales Order)
          IF NEW.MODE = OLD.MODE THEN
             LED.DATA<13>  = LED(13)<1,GEN>      ;* Trans ID
             LED.DATA<28>  = LED(28)<1,GEN>      ;* Batch #
             LED.DATA<29>  = LED(29)<1,GEN>      ;* Terms

             * Do not copy from status code from Credit/Rebill GENs or from
             * Post Order Entry GENs going onto Transfers
             * Do not copy from History conversions or G/L will reverse
             BEGIN CASE
             CASE LED(30)<1,GEN>[1,2] = 'CR'
             CASE LED(30)<1,GEN> = 'HST'
             CASE LED(30)<1,GEN> = 'PE' AND NEW.MODE = 'T'
             CASE OTHERWISE
                LED.DATA<30>  = LED(30)<1,GEN>
             END CASE

             LED.DATA<37>     = LED(37)<1,GEN>   ;* Class
             LED.DATA<38>     = LED(38)<1,GEN>   ;* Global Base
             LED.DATA<39>     = LED(39)<1,GEN>   ;* Global Form
             IF LED(61)<1,GEN,1> # 'CONVERTED' THEN
                LED.DATA<61>     = LED(61)<1,GEN>   ;* Tax Exempt #
             END
             LED.DATA<65>     = LED(65)<1,GEN>   ;* Release #
             LED.DATA<101>    = LED(101)<1,GEN>  ;* Tax Code
             LED.DATA<130>    = LED(130)<1,GEN>  ;* Consolidated invoice

             * Header Info.
             FOR J = 67 TO 80
                * Does not copy Ordered With<68>,Freight In/Out Exempt<69>,
                * Writer<73>, Tax Jurisdition<79>, Shipping Address<78>,
                * or Shipping zip
                IF J = 68 OR J=73 OR J = 79 OR J = 69 OR J=78 OR J=75 THEN CONTINUE
                LED.DATA<J> = LED(J)<1,GEN>
             NEXT J

             FOR J = 91 TO 94
                * Max Collect Days<91>,Currency Data<92>,Frt Dist<93>,
                * Exceptional flag<94>.
                LED.DATA<J> = LED(J)<1,GEN>
             NEXT J

             LED.DATA<108>  = LED(108)<1,GEN>    ;* Commission plan
             LED.DATA<123>  = LED(123)<1,GEN>    ;* Rental ID

             * Get customer information if customer has changed
             IF NOT(SAME.CN) THEN
                GET.CUS PRC.BR,BT.CN,ST.CN,QSIGN
                LED.DATA<72>   = CUSS(41)
                LED.DATA<34>   = CUSS(44)
                LED.DATA<73>   = USER.ID

                * Copy Print Style / Form Type if site uses Eclipse Forms
                IF USE.ECLIPSE.FORMS$ THEN
                   LED.DATA<77> = LOWER(CUSS(195))
                END ELSE
                   LED.DATA<77> = CUSS(42)
                END

                * Find correct salespeople for split commisions
                SLS.LIST    = ''
                SLS.PCT     = ''
                SPLIT.TYPES = CUSS(110)
                SPLIT.SLS   = RAISE(CUSS(111))
                SPLIT.PCT   = RAISE(CUSS(112))

                * Comm.Ct should always be 2, Inside/Outside are the types
                COMM.CT = DCOUNT(SPLIT.TYPES,VM)

                FOR X = 1 TO COMM.CT
                   SLSMN.CT = DCOUNT(SPLIT.SLS<X>,VM);* # of Salespeople
                   SLS.POS = 1
                   * Go through split commision by salesperson
                   FOR Z = 1 TO SLSMN.CT
                      IF SPLIT.SLS<X,Z,2> = PRC.BR THEN
                         * SLS.LIST = Salesperson + Commission group
                         SLS.LIST<X,SLS.POS> = SPLIT.SLS<X,Z,1>:SVM:SPLIT.SLS<X,Z,3>
                         SLS.PCT<X,SLS.POS>  = SPLIT.PCT<X,Z> ;* Percents
                         SLS.POS += 1
                      END
                   NEXT Z
                NEXT X

                LED.DATA<102,1,1> = LOWER(LOWER(CUSS(110)))
                LED.DATA<102,1,2> = LOWER(LOWER(LOWER(SLS.LIST)))
                LED.DATA<102,1,3> = LOWER(LOWER(LOWER(SLS.PCT)))
             END

          END

          FOR J = 141 TO 143
             LED.DATA<J> = LED(J)<1,GEN>
          NEXT J

          FOR J = 145 TO 149
             LED.DATA<J> = LED(J)<1,GEN>
          NEXT J

          RETURN

*-------------------------------------------------------------------------*
CHK.GP:   *
          * Checking GP on all gens
          QSIGN   = OE.GET.QSIGN(OID)
          LDID.CT = LED(50) - 1
          GEN.CT = DCOUNT(LED(12),VM)
          PRC.ERROR = NO
          LOG.CMT = ''

          FOR GX = 1 TO GEN.CT
             *  Locate all LDID from this GEN Attb#49 so the
             * original Seq is presereed as much as possible.
             LDIDS   = LED(48)<1,GX>
             LD.CT   = DCOUNT(LDIDS,SVM)
             FOR LDN = 1 TO LD.CT
                LDID    = LDIDS<1,1,LDN>
                LOCATE LDID IN LED(49)<1> SETTING POS ELSE
                   LED(49)<1,-1> = LDID
                END
             NEXT LDN

             * set gp check base flag
             CTRB.ID = 'OE.MIN.GP.COST~':LED(2)<1,GX,1>
             READ GP.COST FROM CTRBFILE,CTRB.ID ELSE GP.COST = NO

             * Scans through all gens in the order.
             * If any line item is below minimum GP, it will stop scan.
             FOR LDID = 1 TO LDID.CT
                LD.GET LDID
                * skip non-pn lines
                IF LD(1) = '' OR NOT(NUM(LD(1))) THEN CONTINUE
                MATREAD PRD FROM PRDFILE,LD(1) ELSE MAT PRD = ""

                * if no price or cost, skip gp check
                IF GP.COST THEN
                   CHK.COST = LD(27)<1,GX>
                END ELSE
                   CHK.COST = LD(10)<1,GX>
                END
                IF CHK.COST = '' AND LD(8)<1,GX> = '' THEN CONTINUE

                * only check gp on lines that we are keeping all pricing.
                IF NOT(COPY.PRC) THEN CONTINUE
                IF COPY.PRC = 2 AND LD(9)<1,GX> = '' THEN CONTINUE
                IF GP.COST THEN
                   IF NOT(COPY.COST) THEN CONTINUE
                   IF COPY.COST = 2 AND LD(28)<1,GX> = '' THEN CONTINUE
                END ELSE
                   IF NOT(COPY.COGS) THEN CONTINUE
                   IF COPY.COGS = 2 AND LD(11)<1,GX> = '' THEN CONTINUE
                END
                COGS  = LD(10)<1,GX>
                PRICE = LD(8)<1,GX>

                IF JAVA.PROC$ THEN
                   OE.GP.CHECK OID,GX,LDID,,PRC.ERROR,,,0,,LOG.CMT,,,DO.ACTS,ACTIONS,RESPS,STOP.FLOW

                   IF LOG.CMT # '' THEN
                      KEY = "GP.ERR"
                      ACTION.ADD MSGS,KEY,,,,,LOG.CMT
                      VALUE = LOG.CMT
                      RETURN
                   END
                END ELSE
                   OE.GP.CHECK OID,GX,LDID,,PRC.ERROR

                   IF PRC.ERROR THEN
                      RETURN
                   END
                END

             NEXT LDID
          NEXT GX

          RETURN
*-------------------------------------------------------------------------*
CHECK.XFER.AVAIL:
          *Check to see if there is enough stock available for the xfer.
          SV.GEN = GEN
          GEN = 1
          LDID.CT = LED(50) - 1
          FOR LDID = 1 TO LDID.CT
             LD.GET LDID
             MAT OLD.LD = MAT LD
             PN = LD(1)
             SHP.QSIGN = OE.GET.QSIGN(OID,1)
             REC.QSIGN = OE.GET.QSIGN(OID,2)

             CALC.AVAIL AVAIL,LED(9)<1,GEN>,LED(2)<1,1,2>,PN
             SHP.QTY = SUM(LD(5)<1,1>) + SUM(LD(6)<1,1>)

             * We need to add back the order quatity to the availability
             * total since we want to know what is available. At this point
             * in the code the transfer has been created and the qty has
             * been "requisitioned". This could then show none left when
             * in fact there is still quantity available.

             AVAIL += (SHP.QSIGN * SHP.QTY)

             STK.QTY  = SHP.QSIGN * SUM(LD(5)<1,1>)
             NSTK.QTY = SHP.QSIGN * SUM(LD(6)<1,1>)
             DIFF     = (STK.QTY + NSTK.QTY) - AVAIL

             * Only check if the QTY is more than what is available
             IF DIFF > 0 THEN
                IF (DIFF < NSTK.QTY) THEN
                   LD(6)<1,1> -= (DIFF * SHP.QSIGN)
                   LD(6)<1,2> -= (DIFF * REC.QSIGN)
                   DIFF = 0
                END ELSE
                   DIFF = DIFF - NSTK.QTY
                   LD(6)<1,1> = 0
                   LD(6)<1,2> = 0
                END

                IF DIFF > 0 THEN
                   IF STK.QTY> DIFF THEN
                      LD(5)<1,1> -= (DIFF * SHP.QSIGN)
                      LD(5)<1,2> += (DIFF * REC.QSIGN)
                   END ELSE
                      LD(5)<1,1> = 0
                      LD(5)<1,2> = 0
                   END
                END

                OE.UPDATE.LDID LDID,OID,GEN,,SHP.QSIGN
             END
          NEXT LDID
          GEN = SV.GEN
          RETURN
*-------------------------------------------------------------------------*
COMBINE.GENS:   *** Combine all bid generations together if they can
          GN.CT = DCOUNT(LED(12)<1>,VM)
          QSIGN = OE.GET.QSIGN(OID)
          FOR GN = 1 TO GN.CT
             IF LED(6)<1,GN> THEN
                OE.CHANGE.SHIPDATE OID,GN,LED(9)<1,GN>,QSIGN,,NO
                UPDATE.LEDGER OID
             END
          NEXT GN
          RETURN
*-------------------------------------------------------------------------*
SET.SOURCE: *** Set the Order Sales Source
          IF JAVA.PROC$ THEN
             READ GL.TYPES FROM CTRLFILE,'GL.SALES.TYPE' ELSE GL.TYPES = ''
             LOCATE GEN.LIST<1,SGEN> IN GL.TYPES<1> SETTING POS THEN
                SOURCE = GL.TYPES<2,POS>
             END
          END ELSE
             SOURCE = GEN.LIST<1,SGEN>
          END
          LED(3)<1,TARGET.GEN> = SOURCE

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~04/15/16~16:04
