          SUBROUTINE (OID,INIT.GEN,QSIGN)
** Version# 111.0001[8] - 06/14/2016 - 01:25pm - TSMITH - eclipse
*** V111.0001 Change - Custom Coding . - 06/14/2016 - TSMITH - eclipse

*** Subroutine - OE.UPD.GL
*-------------------------------------------------------------------------*
*** This routine is responsible for updating the G/L Postings on the
*** Ledger Record during invoicing or during the updating of an invoiced
*** order.
*-------------------------------------------------------------------------*
*** Parameters:
*** OID      - Order ID                                               [IN]
*** INIT.GEN - Initial GEN                                            [IN]
*** QSIGN    - Quantity Sign (1 or -1)                                [IN]
*-------------------------------------------------------------------------*
*** Globals:
*** LED - Ledger
*** LD  - Ledger Detail
*** PRD - Product
*-------------------------------------------------------------------------*
          DIM TPRD(200)

          *** DO NOT DELETE OR ALTER the following three lines they are in
          *** a specific order, and must not change unless you really
          *** know what you are doing!  Doing so will cause havoc with
          *** Dynamic Auto Postings (CQV266)

          EXT.IDS  = "SLS,COGS,PURCH,CM.CGS.DIFF,CSGN.PURCH,WO.PURCH,"
          EXT.IDS := "XFER.SLS,XFER.COGS,XFER.PURCH,INVADJ"
          CONVERT ',' TO AM IN EXT.IDS

          **************************************************************
          **************************************************************

          GEN  = INIT.GEN
          MODE = OID[1,1]

          GOSUB UPD.GEN

          IF LED(33)<1,GEN> THEN
             LOCATE LED(33)<1,GEN> IN LED(12)<1> SETTING GEN THEN
                * We don't want to update if the complementing side is a
                * 'Y' Gen, and that gen has been paid.
                BEGIN CASE
                CASE LED(6)<1,GEN>   = 'Y'
                   IN.DATA           = OID
                   IN.DATA<2>        = GEN
                   OE.CHECK.GEN.PAID IN.DATA,OUT.DATA

                   * If it's not paid, then go ahead and update!
                   IF NOT(OUT.DATA<2>) THEN
                      GOSUB UPD.GEN
                   END
                CASE OTHERWISE
                   IF LED(83)<1,GEN>[1,7] # "@JOBNO=" THEN
                      GOSUB UPD.GEN
                   END ELSE
                      * Make sure we are invoiced on JM SO side directs.
                      IF LED(8)<1,GEN> # "" THEN
                         GOSUB UPD.GEN
                      END
                   END
                END CASE
             END
          END

          RETURN
*-------------------------------------------------------------------------*
UPD.GEN:  DGL.IDS  = LD(33)<1,GEN>
          DGL.NOS  = LD(25)<1,GEN>
          DGL.AMTS = LD(26)<1,GEN>
          GL.IDS   = LED(24)<1,GEN>
          GL.NOS   = LED(25)<1,GEN>
          GL.AMTS  = LED(26)<1,GEN>
          IF MODE = 'W' AND LED(6)<1,GEN> = 'I' THEN
             QR = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
             IF QR > 0 THEN RETURN
          END
          OE.UNDO.PREV.GL OID,GEN,DGL.IDS,DGL.NOS,DGL.AMTS,GL.IDS,GL.NOS,GL.AMTS

          INVTY.ID  = 'INVTY'
          INVTY.NO  = GL.AUTO.INVTY
          *** Check for Inventory Account Override
          IF MODE # 'W' AND LD(114) THEN
             INVTY.ID  = 'INVTY+'
             INVTY.NO  = LD(114)
          END

          DIR.CONSIGN = (LED(110)<1,1> = 'S' AND LED(33)<1,GEN>)
          BEGIN CASE
          CASE DIR.CONSIGN; * Direct consignment transfers
             * Update only direct generation for direct consign transfers
             IF LED(6)<1,GEN> = 'Y' THEN
                GOSUB UPD.DIRECT
             END
          CASE MODE = 'S' AND LED(6)<1,GEN>='Y'
             GOSUB UPD.DIRECT
          CASE MODE = 'S' OR MODE = 'R'
             GOSUB UPD.AR
          CASE MODE = 'P' AND LED(110)<1,1>='R'
             GOSUB UPD.CNSGN.RCPT
          CASE MODE = 'P'
             GOSUB UPD.AP
          CASE MODE = 'T'
             GOSUB UPD.TRANS
          CASE MODE = 'A'
             GOSUB UPD.ADJ
          CASE MODE = 'W'
             GOSUB UPD.WIP
          END CASE
          LD(33)<1,GEN>  = DGL.IDS
          LD(25)<1,GEN>  = DGL.NOS
          LD(26)<1,GEN>  = DGL.AMTS
          LED(24)<1,GEN> = GL.IDS
          LED(25)<1,GEN> = GL.NOS
          LED(26)<1,GEN> = GL.AMTS

          RETURN
*-------------------------------------------------------------------------*
*** Update GL for a Transfer transaction **
UPD.TRANS:
          SHP.BR = LED(2)<1,GEN,3>
          SOURCE = LED(3)<1,GEN>
          PTYPE  = PRD(2)

          *** Find vendor consignment qtys
          CQS  = 0
          L.CT = DCOUNT(LD(7)<1,GEN>,SVM)
          FOR L = 1 TO L.CT
             TYPE = FIELD(LD(7)<1,GEN,L>,'~',1)
             IF LEN(TYPE) > 1 AND TYPE[1,1] = 'S' THEN
                CQS += LD(5)<1,GEN,L> + LD(6)<1,GEN,L>
             END
          NEXT L

          QS           = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
          SLS.AMT      = ICONV(OCONV(QS*LD(8)<1,GEN>,  'MR9'),'MR2')
          CGN.SLS.AMT  = ICONV(OCONV(CQS*LD(8)<1,GEN>, 'MR9'),'MR2')
          COGS.AMT     = ICONV(OCONV(QS*LD(10)<1,GEN>, 'MR9'),'MR2')
          CGN.COGS.AMT = ICONV(OCONV(CQS*LD(10)<1,GEN>,'MR9'),'MR2')

          BEGIN CASE
          CASE GEN = 1
             GL.AMT = SLS.AMT;   DGL.AMT = SLS.AMT
             GL.BR  = SHP.BR
             IF PRD(3) = 3 THEN
*---------Update Misc Charge
                SP.ID = 'MISC'; GL.NO = PRD(2)
                GOSUB UPD.GL
             END ELSE
*---------Update Xfer Sales
                SP.ID = 'XFER.SLS'
                GOSUB EXT.SP.ID
                GOSUB UPD.GL
*---------Update Cost of Goods Sold
                SP.ID = 'XFER.COGS'; GL.AMT = -COGS.AMT;DGL.AMT = -COGS.AMT
                GL.BR  = SHP.BR
                GOSUB EXT.SP.ID
                GOSUB UPD.GL
*---------Update Inventory
                GL.AMT = COGS.AMT; DGL.AMT = COGS.AMT
                GL.AMT = GL.AMT - CGN.COGS.AMT
                DGL.AMT= GL.AMT

                *** Update non-consignment inventory
                IF CQS = 0 OR GL.AMT # 0 THEN
                   SP.ID  = INVTY.ID
                   GL.NO  = INVTY.NO
                   GL.BR  = SHP.BR
                   GOSUB UPD.GL
                END

                *** Update consignment inventory
                IF CQS # 0 THEN
                   GL.AMT = CGN.COGS.AMT;  DGL.AMT = CGN.COGS.AMT
                   SP.ID  = 'CSGN.INVTY'
                   GL.BR  = SHP.BR
                   GOSUB EXT.SP.ID
                   GOSUB UPD.GL
                END
             END

*---------Update XFER.AR and UBC
             GL.BR  = SHP.BR

             SP.ID  = 'XFER.AR'
             GL.NO  = GL.AUTO.XFER.AR
             GOSUB DELETE.ALL.REFS
             GL.AMT = -SUM(GL.AMTS); DGL.AMT = ''
             GOSUB UPD.GL

          CASE GEN = 2
*---------Update Xfer Sales
             GL.BR  = SHP.BR
             GL.AMT = SLS.AMT;   DGL.AMT = SLS.AMT
             IF PRD(3)=3 THEN
                SP.ID = 'MISC'; GL.NO = PRD(2)
                GOSUB UPD.GL
             END ELSE
                SP.ID = 'XFER.PURCH'
                GOSUB EXT.SP.ID
                GOSUB UPD.GL

*---------Update Cost of Goods Sold
                SP.ID  = 'XFER.CXTI'; GL.AMT = -SLS.AMT; DGL.AMT = -SLS.AMT
                GL.BR  = SHP.BR
                GOSUB EXT.SP.ID
                GOSUB UPD.GL

*---------Update Inventory
                GL.AMT  = SLS.AMT; DGL.AMT = SLS.AMT
                GL.AMT  = GL.AMT - CGN.SLS.AMT
                DGL.AMT = GL.AMT

                *** Update non-consignment inventory
                IF CQS = 0 OR GL.AMT # 0 THEN
                   SP.ID  = INVTY.ID
                   GL.NO  = INVTY.NO
                   GL.BR  = SHP.BR
                   GOSUB UPD.GL
                END

                *** Update consignment inventory
                IF CQS # 0 THEN
                   GL.AMT = CGN.SLS.AMT;  DGL.AMT = CGN.SLS.AMT
                   SP.ID  = 'CSGN.INVTY'
                   GL.BR  = SHP.BR
                   GOSUB EXT.SP.ID
                   GOSUB UPD.GL
                END
             END

*---------Update XFER.AP and UBC
             GL.BR  = SHP.BR

             SP.ID  = 'XFER.AP'
             GL.NO  = GL.AUTO.XFER.AP
             GOSUB DELETE.ALL.REFS
             GL.AMT = -SUM(GL.AMTS); DGL.AMT = ''
             GOSUB UPD.GL
          END CASE

          RETURN
*-------------------------------------------------------------------------*
UPD.CNSGN.RCPT: ** Update Gl for a Purchase transaction **
          STK.BR = LED(2)<1,GEN,2>
          PAY.BR = LED(2)<1,GEN,3>
          QR     = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
          AP.AMT = ICONV(OCONV(QR*LD(8)<1,GEN>,'MR9'),'MR2')
          SOURCE = LED(3)<1,GEN>
          PTYPE  = PRD(2)

*---------Update Consigned Inventory Amount
          GL.AMT = AP.AMT;   DGL.AMT = AP.AMT
          IF LED(93)<1,GEN,1>#"None" AND LED(93)<1,GEN,1>#"" THEN
             FGHT = ICONV(OCONV((LD(34)<1,GEN,1>*QR),'MR9'),"MR2")
             GL.AMT += FGHT; DGL.AMT += FGHT
          END
          FET    = ICONV(OCONV((LD(55)<1,GEN>*QR),'MR9'),"MR2")
          GL.AMT+= FET; DGL.AMT += FET
          GL.BR  = STK.BR
          IF PRD(3)=3 THEN
             SP.ID = 'MISC';   GL.NO = PRD(2)
             GOSUB UPD.GL
          END ELSE
             SP.ID  = 'CSGN.INVTY'
             GOSUB EXT.SP.ID
             GOSUB UPD.GL
*---------Update FET Reserve for items that are not prepaid FET
        * not on consignments
        *    IF LD(57)<1,GEN> # '1' THEN
        *       SP.ID  = 'FET';  GL.AMT = -FET;   DGL.AMT = -FET
        *       IF GL.AMT # 0 THEN
        *          GOSUB EXT.SP.ID
        *          GL.BR  = PAY.BR
        *          GOSUB UPD.GL
        *       END
        *    END
*---------Update Consign Purchases
             SP.ID  = 'CSGN.PURCH';  GL.AMT = AP.AMT;   DGL.AMT = AP.AMT
             GL.BR  = PAY.BR
             GOSUB EXT.SP.ID
             GOSUB UPD.GL
*---------Update Clear Consigned to Inventory
             SP.ID  = 'CCTI';   GL.AMT = -AP.AMT;  DGL.AMT = -AP.AMT
        *    GL.NO  = GL.AUTO.CPTI
             GL.BR  = PAY.BR
             GOSUB EXT.SP.ID
             GOSUB UPD.GL
          END

*---------Update Unbilled Consigned with balancing entry
          READV VEN.GL FROM CUSFILE,LED(1)<1,GEN>,100 ELSE VEN.GL = ''
          IF VEN.GL = '' THEN
             READV VEN.GL FROM CUSFILE,LED(5)<1,GEN>,100 ELSE VEN.GL = ''
          END
          SP.ID  = 'UBC'
          GL.BR  = PAY.BR
          GOSUB EXT.SP.ID
          GOSUB DELETE.ALL.REFS
          GL.AMT  = -SUM(GL.AMTS)
          DGL.AMT = ''
          GOSUB UPD.GL

          RETURN
*-------------------------------------------------------------------------*
UPD.AP:   ** Update Gl for a Purchase transaction **
          STK.BR = LED(2)<1,GEN,2>
          PAY.BR = LED(2)<1,GEN,3>
          QR     = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
          AP.AMT = ICONV(OCONV(QR*LD(8)<1,GEN>,'MR9'),'MR2')
          SOURCE = LED(3)<1,GEN>
          PTYPE  = PRD(2)

*---------Update Inventory Amount
          GL.AMT = AP.AMT;   DGL.AMT = AP.AMT
          IF LED(93)<1,GEN,1>#"None" AND LED(93)<1,GEN,1>#"" THEN
             FGHT = ICONV(OCONV((LD(34)<1,GEN,1>*QR),'MR9'),"MR2")
             GL.AMT += FGHT; DGL.AMT += FGHT
          END
*** Calculate the duty amount and add to inventory.
          DUTY.PER= OCONV((LD(78)<1,GEN>+0),'MR4')
          IF (DUTY.PER+0) # 0 THEN
             TAP.AMT = OCONV(AP.AMT,"MR2")
             DUTY.AMT= ICONV(DUTY.PER*TAP.AMT/100,'MR2')
             GL.AMT += DUTY.AMT; DGL.AMT += DUTY.AMT
          END
*** Calculate Other amount and add to inventory
          OTH.PER = OCONV((LD(88)<1,GEN>+0),'MR4')
          IF OTH.PER+0 # 0 THEN
             TAP.AMT = OCONV(AP.AMT,'MR2')
             OTH.AMT = ICONV(OTH.PER*TAP.AMT/100,'MR2')
             GL.AMT += OTH.AMT; DGL.AMT += OTH.AMT
          END
          FET    = ICONV(OCONV((LD(55)<1,GEN>*QR),'MR9'),"MR2")
          GL.AMT+= FET; DGL.AMT += FET
          GL.BR  = STK.BR
          IF PRD(3)=3 THEN
             SP.ID  = 'MISC';   GL.NO = PRD(2)
             GOSUB UPD.GL
          END ELSE
             SP.ID  = INVTY.ID;  GL.NO = INVTY.NO
             GOSUB UPD.GL
*---------Update FET Reserve for items that are not prepaid FET
             IF LD(57)<1,GEN> # '1' THEN
                SP.ID  = 'FET';  GL.AMT = -FET;   DGL.AMT = -FET
                IF GL.AMT # 0 THEN
                   GL.BR  = PAY.BR
                   GOSUB EXT.SP.ID
                   GOSUB UPD.GL
                END
             END
*---------Update Duty if there's a duty at the line item
             DUTY.PER= OCONV((LD(78)<1,GEN>+0),'MR4')
             IF (DUTY.PER+0) # 0 THEN
                SP.ID  = 'DUTY';   GL.AMT = (DUTY.AMT*-1); DGL.AMT = GL.AMT
                GL.BR  = PAY.BR
                GOSUB EXT.SP.ID
                GOSUB UPD.GL
             END
*---------Update Other charges on line item
             OTH.PER = OCONV((LD(88)<1,GEN>+0),'MR4')
             IF OTH.PER + 0 # 0 THEN
                SP.ID = 'OTHER'; GL.AMT = (OTH.AMT*-1); DGL.AMT = GL.AMT
                GL.BR = PAY.BR
                GOSUB EXT.SP.ID
                GOSUB UPD.GL
             END
*---------Update Purchases
             SP.ID  = 'PURCH';  GL.AMT = AP.AMT;   DGL.AMT = AP.AMT
             GL.BR  = PAY.BR
             GOSUB EXT.SP.ID
             GOSUB UPD.GL
*---------Update Clear Purchases to Inventory
             SP.ID  = 'CPTI';   GL.AMT = -AP.AMT;  DGL.AMT = -AP.AMT
             GL.NO  = GL.AUTO.CPTI
             GL.BR  = PAY.BR
             GOSUB UPD.GL
          END

          GOSUB POST.UBAP
          RETURN
*-------------------------------------------------------------------------*
POST.UBAP:*** Post freight and material amounts to UBAP handling any
          *** vendor specific G/L overrides
          * Delete all possible SP.IDS as the material vendor
          * may have changed
          SP.ID = 'UBAP'
          GOSUB DELETE.ALL.REFS
          SP.ID = 'XAP'
          GOSUB DELETE.ALL.REFS
          SP.ID = 'XFAP'
          GOSUB DELETE.ALL.REFS

          * If there is a freight vendor, we need to post the freight
          * balance to either UBAP or XFAP depending upon if the
          * freight vendor has a GL override
          IF LED(93)<1,GEN,2> THEN
             FGHT.VN = LED(93)<1,GEN,2>
             READV FGHT.GL FROM CUSFILE,FGHT.VN,160 ELSE FGHT.GL = ''
             IF FGHT.GL = '' THEN
                FVEN.BT = GET.PT.AP(FGHT.VN)
                READV FGHT.GL FROM CUSFILE,FVEN.BT,160 ELSE FGHT.GL = ''
             END

             IF FGHT.GL = '' THEN
                SP.ID = 'UBAP'
                GL.NO = GL.AUTO.UBAP
             END ELSE
                SP.ID = 'XFAP'
                GL.NO = FGHT.GL
             END

             OE.CALC.FGHT OID,GEN,FGHT
             GL.BR   = PAY.BR
             GL.AMT  = -FGHT
             DGL.AMT = ''
             GOSUB UPD.GL
          END

*---------Update Accounts Payable with balancing entry
          READV VEN.GL FROM CUSFILE,LED(1)<1,GEN>,100 ELSE VEN.GL = ''
          IF VEN.GL = '' THEN
             READV VEN.GL FROM CUSFILE,LED(5)<1,GEN>,100 ELSE VEN.GL = ''
          END
          IF VEN.GL = '' THEN
             SP.ID  = 'UBAP'
             GL.NO  = GL.AUTO.UBAP
          END ELSE
             SP.ID  = 'XAP'
             GL.NO  = VEN.GL
          END

          GL.BR   = PAY.BR
          GL.AMT  = -SUM(GL.AMTS)
          DGL.AMT = ''
          GOSUB UPD.GL
          RETURN
*-------------------------------------------------------------------------*
UPD.AR:   ** Update GL for a Sale transaction **
          STK.BR = LED(2)<1,GEN,2>
          SLS.BR = LED(2)<1,GEN,3>

          CQS  = 0
          L.CT = DCOUNT(LD(7)<1,GEN>,SVM)
          FOR L = 1 TO L.CT
             TYPE = FIELD(LD(7)<1,GEN,L>,'~',1)
             IF LEN(TYPE) > 1 AND TYPE[1,1] = 'S' THEN
                CQS += LD(5)<1,GEN,L> + LD(6)<1,GEN,L>
             END
          NEXT L
          QS            = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
          SLS.AMT       = ICONV(OCONV(QS*LD(8)<1,GEN>,'MR9'),'MR2')
          COGS.AMT      = ICONV(OCONV(QS*LD(10)<1,GEN>,'MR9'),'MR2')
          CGN.COGS.AMT  = ICONV(OCONV(CQS*LD(10)<1,GEN>,'MR9'),'MR2')
          PASS.FGHT     = ICONV(OCONV(LD(111)<1,GEN,1>+0,'MR9'),'MR2')

          *** If there is no qty shipped on this generation, pass along
          *** freight will have to be set to $0.00
          IF QS = 0 THEN PASS.FGHT = 0

          CHK.CR   = QS
          IF LED(30)<1,GEN> = 'CRC' THEN CHK.CR = CHK.CR*-1

          SOURCE = LED(3)<1,GEN>
          BEGIN CASE
          CASE LED(33)<1,GEN>
             *** Use old method if not Dynamically autoposting
             FINDSTR '~DIR~' IN GL.AUTO<1> SETTING XX THEN
                SOURCE = 'DIR'
             END
          CASE CHK.CR > 0
             *** Use old method if not Dynamically autoposting
             FINDSTR '~CREDIT~' IN GL.AUTO<1> SETTING XX THEN
                SOURCE = 'CREDIT'
             END
          END CASE
          PTYPE  = PRD(2)

*---------Read In G/L Kit Component Control Record
          READ COMP.POST FROM CTRLFILE,'KIT.COMP.GL.POST' ELSE COMP.POST=NO

          IF COMP.POST THEN
             *** If the components that make up the kit do not have costs
             *** associated with them, we cannot break out the GL postings
             *** by component. We calculate the TOTCOST and if 0, we will
             *** post according to the kit rather than its components.
             *** Save off ledger record b/c CALC.KIT.TOT will indirectly
             *** change it in CUS.PRICE.GET and we don't want to keep
             *** the change
             MATBUILD SAVELED FROM LED
             GOSUB CALC.KIT.TOT
             MATPARSE LED FROM SAVELED
          END

          IF COMP.POST AND DCOUNT(LD(31),VM) > 0 AND CQS=0 AND TOTCOST THEN
             GOSUB UPD.KIT.AR
          END ELSE
*---------Update Sales Amount
             GL.AMT = SLS.AMT;   DGL.AMT = SLS.AMT
             GL.BR  = SLS.BR
             BEGIN CASE
             CASE PRD(3)=3
                SP.ID = 'MISC';    GL.NO = PRD(2)
                GOSUB UPD.GL
             CASE MODE = 'R'
                GOSUB UPD.RENTAL
             CASE OTHERWISE
                SP.ID  = 'SLS'
                FET    = ICONV(OCONV((LD(55)<1,GEN>*QS),'MR9'),"MR2")
                GL.AMT+= FET; DGL.AMT += FET
                GOSUB EXT.SP.ID
                GOSUB UPD.GL

*---------Update Cost of Goods Sold
                SP.ID  = 'COGS'
                GL.AMT = -COGS.AMT; DGL.AMT = -COGS.AMT
                GL.AMT-= FET; DGL.AMT -= FET

                GL.BR  = SLS.BR
                GOSUB EXT.SP.ID
                GOSUB UPD.GL

                GL.AMT = COGS.AMT; DGL.AMT = COGS.AMT
                GL.AMT = GL.AMT - CGN.COGS.AMT
                DGL.AMT= GL.AMT
                * Add pass Along Freight to COGS to get Inventory if there
                * is any
                IF PASS.FGHT THEN
                   GL.AMT  += -PASS.FGHT
                   DGL.AMT += -PASS.FGHT
                END
                GL.AMT+= FET; DGL.AMT += FET

*---------Update Inventory and/or consigned inventory
                IF CQS = 0 OR GL.AMT # 0 THEN
                   SP.ID  = INVTY.ID
                   GL.NO  = INVTY.NO
                   GL.BR  = STK.BR
                   GOSUB UPD.GL
                END
                IF CQS # 0 THEN
                   GL.AMT = CGN.COGS.AMT;  DGL.AMT = CGN.COGS.AMT
                   SP.ID  = 'CSGN.INVTY'
                   GL.BR  = STK.BR
                   GOSUB EXT.SP.ID
                   GOSUB UPD.GL
                END

                COMM.COGS.DIFF = LD(27)<1,GEN> - LD(10)<1,GEN>
                COMM.COGS.DIFF = OCONV(QS*COMM.COGS.DIFF,'MR07')
                IF COMM.COGS.DIFF # 0 THEN
                   SP.ID   = 'CM.CGS.DIFF'
                   OS.SP.ID= 'CCCDTPL'            ; * Offset
                   GL.BR   = SLS.BR
                   GL.AMT  = -COMM.COGS.DIFF
                   DGL.AMT = -COMM.COGS.DIFF
                   GOSUB EXT.SP.ID
                   LOCATE SP.ID IN GL.AUTO<1> SETTING POS THEN
                      * Verify offset exists before doing any updates
                      LOCATE OS.SP.ID IN GL.AUTO<1> SETTING XPOS THEN
                         * Both GL accounts must be specified
                         IF GL.AUTO<2,POS> AND GL.AUTO<2,XPOS> THEN
                            * Ensure diff and clearing accts are not same
                            IF GL.AUTO<2,POS> # GL.AUTO<2,XPOS> THEN
                               GL.NO = GL.AUTO<2,POS>
                               GOSUB UPD.GL

                               SP.ID   = OS.SP.ID
                               POS     = XPOS
                               GL.BR   = SLS.BR
                               GL.AMT  = COMM.COGS.DIFF
                               DGL.AMT = COMM.COGS.DIFF
                               GL.NO = GL.AUTO<2,POS>
                               GOSUB UPD.GL
                            END
                         END
                      END
                   END
                END
             END CASE
          END

*---------Update ROE Discount
          SOE.CALC.DISC OID,GEN,CASH.DISC,,ROE.DISC

          IF ROE.DISC THEN
             IF LED(30)<1,GEN> = 'EDI' THEN
                SP.ID = 'EDIDISCG'; GL.AMT = -ROE.DISC; DGL.AMT = -ROE.DISC
             END ELSE
                SP.ID = 'WOEDISCG'; GL.AMT = -ROE.DISC; DGL.AMT = -ROE.DISC
             END

             GL.BR = SLS.BR
             GOSUB EXT.SP.ID
             GOSUB DELETE.ALL.REFS
             GOSUB UPD.GL
          END

*---------Update Accounts Receivable with balancing entry
          OE.UPD.GL.AR OID,GEN,GL.IDS,GL.NOS,GL.AMTS

          RETURN
*-------------------------------------------------------------------------*
UPD.RENTAL:  * Update the rental revenue account (just like misc charge"
          UT.OPEN.FILE "RENTAL.RATE",RRTFILE,OPEN.ERR,YES

          GL.ACCT = ""

          IF NOT(OPEN.ERR) THEN
             READV GL.ACCT FROM RRTFILE,LD(1),1 ELSE GL.ACCT = ""
          END

          IF GL.ACCT = "" THEN
             SP.ID = "RENTAL.REV"
             GOSUB EXT.SP.ID
          END ELSE
             SP.ID = "RENTAL.REV"; GL.NO = GL.ACCT
          END

          GOSUB UPD.GL
          QR     = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
          FET    = ICONV(OCONV((LD(55)<1,GEN>*QR),'MR9'),"MR2")
          **** TEST CODE
          *---------Update Cost of Goods Sold
                SP.ID  = 'COGS'
                GL.AMT = -COGS.AMT; DGL.AMT = -COGS.AMT
                GL.AMT-= FET; DGL.AMT -= FET

                GL.BR  = SLS.BR
                GOSUB EXT.SP.ID
                GOSUB UPD.GL

                GL.AMT = COGS.AMT; DGL.AMT = COGS.AMT
                GL.AMT = GL.AMT - CGN.COGS.AMT
                DGL.AMT= GL.AMT
                * Add pass Along Freight to COGS to get Inventory if there
                * is any
                IF PASS.FGHT THEN
                   GL.AMT  += -PASS.FGHT
                   DGL.AMT += -PASS.FGHT
                END
                GL.AMT+= FET; DGL.AMT += FET

*---------Update Inventory and/or consigned inventory
   *             IF CQS = 0 OR GL.AMT # 0 THEN
   *                SP.ID  = INVTY.ID
   *                GL.NO  = INVTY.NO
   *                GL.BR  = STK.BR
   *                GOSUB UPD.GL
   *             END
   *             IF CQS # 0 THEN
   *                GL.AMT = CGN.COGS.AMT;  DGL.AMT = CGN.COGS.AMT
   *                SP.ID  = 'CSGN.INVTY'
   *                GL.BR  = STK.BR
   *                GOSUB EXT.SP.ID
   *                GOSUB UPD.GL
   *             END

                COMM.COGS.DIFF = LD(27)<1,GEN> - LD(10)<1,GEN>
                COMM.COGS.DIFF = OCONV(QS*COMM.COGS.DIFF,'MR07')
                IF COMM.COGS.DIFF # 0 THEN
                   SP.ID   = 'CM.CGS.DIFF'
                   OS.SP.ID= 'CCCDTPL'            ; * Offset
                   GL.BR   = SLS.BR
                   GL.AMT  = -COMM.COGS.DIFF
                   DGL.AMT = -COMM.COGS.DIFF
                   GOSUB EXT.SP.ID
                   LOCATE SP.ID IN GL.AUTO<1> SETTING POS THEN
                      * Verify offset exists before doing any updates
                      LOCATE OS.SP.ID IN GL.AUTO<1> SETTING XPOS THEN
                         * Both GL accounts must be specified
                         IF GL.AUTO<2,POS> AND GL.AUTO<2,XPOS> THEN
                            * Ensure diff and clearing accts are not same
                            IF GL.AUTO<2,POS> # GL.AUTO<2,XPOS> THEN
                               GL.NO = GL.AUTO<2,POS>
                               GOSUB UPD.GL

                               SP.ID   = OS.SP.ID
                               POS     = XPOS
                               GL.BR   = SLS.BR
                               GL.AMT  = COMM.COGS.DIFF
                               DGL.AMT = COMM.COGS.DIFF
                               GL.NO = GL.AUTO<2,POS>
                               GOSUB UPD.GL
                            END
                         END
                      END
                   END
                END

          RETURN
*-------------------------------------------------------------------------*
CALC.KIT.TOT:   *** Calculate the total of costs and prices for individual
          *** kit components
          PN = LD(1)
          READV PRD3 FROM PRDFILE,PN,3 ELSE PRD3 = ''
          READV PRD2 FROM PRDFILE,PN,2 ELSE PRD2 = ''
          IF PRD3=3 THEN
             FET = 0
          END ELSE
             FET = (LD(55)<1,GEN> * -QS)
          END

          COMP.PRCS  = ''
          COMP.COSTS = ''
          COMP.MS    = ''
          TOTCOST    = 0
          TOTPRCS    = 0

          COMP.QTYS = LD(30)
          COMP.PNS  = LD(31)
          COMP.SPLS = LD(39)
          CP.CT = DCOUNT(COMP.PNS,VM)
          TYP.LOCS = LD(7)<1,GEN>
          QTY.SIGN = 1
          PDATA    = ''


          FOR CP=1 TO CP.CT
             TPN = COMP.PNS<1,CP>
             PTYP = ''
             READV PTYP FROM PRDFILE,TPN,2 ELSE PTYP = ''
             PDATA<1,CP> = PTYP
             GET.ALL.PRD STK.BR,TPN,QSIGN

             COMP.MULT = COMP.QTYS<1,CP>
             COMP.SPL  = COMP.SPLS<1,CP>
             TYP.CT    = DCOUNT(TYP.LOCS,SVM)
             COMP.QTY  = 0

             FOR TN = 1 TO TYP.CT
                QTY = QTY.SIGN*COMP.MULT
                IF COMP.SPL THEN
                   QTY = CALC.KIT.QTY.WITH.SPOIL(QTY,COMP.SPL)
                END
                COMP.QTY += QTY
             NEXT TN
***

             CUS.PRICE.GET LED(5)<1,GEN>,TPN,STK.BR,COMP.QTY,LED(22)<1,GEN>,SELL.PRC,COST.PRC

             *** Make absolute value so that in case a component is
             *** deducted from the kit, it's proportionate amount can be
             *** re-distributed later in this routine
             COMP.PRCS<1,CP>  = ABS(COMP.QTY * SELL.PRC * QS)
             COMP.COSTS<1,CP> = ABS(COMP.QTY * COST.PRC * QS)
***

             COMP.MS<1,CP>    = COMP.QTY
          NEXT CP

          *** Note:
          *** From this point in UPD.AR.KIT positive numbers are used.
          *** This was done to keep the calculations the same up until
          *** we get ready to make the posting entries then
          *** the sign of the SLS.AMT and COGS.AMT to be posted will
          *** be switched based on QS (number of kits being shipped) and
          *** COMP.MS<1,CP> which indicates if the component is being
          *** added or removed from the kit.

          * Reload the customers at this point. CUS.PRICE.GET may have
          * loaded the Bill-To in both CUS and CUSS. This will properly
          * restore it so pricing is correct.
          PRC.BR = LED(2)<1,GEN,1>
          BT.CN  = LED(1)<1,GEN>
          ST.CN  = LED(5)<1,GEN>
          GET.CUS PRC.BR,BT.CN,ST.CN,QSIGN

          * Reload the product.
          GET.ALL.PRD STK.BR,PN,QSIGN

          TOTCOST = SUM(COMP.COSTS<1>)
          TOTPRCS = SUM(COMP.PRCS<1>)

          RETURN
*-------------------------------------------------------------------------*
UPD.KIT.AR: *** This Subroutine is called when Control Paramater in Eclipse
          *** Hit Individual Components G/L Accounts for Kits is set to "Y"
          ***
          COMP.SELL = ''
          COMP.COST = ''
          COMP.SOVR = ''
          COMP.COVR = ''

          IF TOTCOST THEN
             *** Creating the sales and cost postings to update GL
             *** based on the components on the Kit rather than the
             *** kit itself.  Since the selling price has already been
             *** loaded into LD(8) in MR9 format we need calculate  the
             *** extended amount to MR2 format then back to MR9.
             *** This needs to be done because we have to equal the
             *** extended amount exactly.

             *** Note the nesting of the MR9 in the MR2 ICONV is important
             *** here and should not be unnested as it can cause different
             *** results.

             KITSP = ABS(ICONV(OCONV((LD(8)<1,GEN> * -QS) + FET,'MR9'),'MR2'))
             KITSP = ICONV(OCONV(KITSP,"MR2"),"MR9")


             KITPC = ABS(ICONV(OCONV((LD(10)<1,GEN> * -QS) + FET,'MR9'),'MR2'))
             KITPC = ICONV(OCONV(KITPC,"MR2"),"MR9")

             *** Kit accumulators for Sales seperating Adds and Negatives
             KITCPS = ''   ; * This will hold list of positive components
             KITADDS = 0
             KITNEGS = 0

             *** Kit accumulators for Costs seperating Adds and Negatives
             KITADDC = 0
             KITNEGC = 0

             TMP.TC = KITSP
             TMP.PR = KITPC

             FOR CP=1 TO CP.CT
                COMPM = COMP.MS<1,CP>
                COMPC = COMP.COSTS<1,CP>
                COMSP = COMP.PRCS<1,CP>
                IF COMPM THEN

                   *** Make sure totprcs is not null so division by zero
                   *** will not create error message
                   IF TOTPRCS THEN
                      COMP.SELL<CP> = ABS(INT(((COMSP/TOTPRCS)*KITSP)+.5))
                   END ELSE
                      COMP.SELL<CP> = 0
                   END

                   COMP.COST<CP>=ABS(INT(((COMPC/TOTCOST)*KITPC)+.5))

                   *** Rounding to the nearest penny in order to insure
                   *** the distribution will match the current extended
                   *** sell and cost on this line item.
                   COMP.SELL<CP> = ICONV(OCONV(COMP.SELL<CP>,"MR9"),"MR2")
                   COMP.SELL<CP> = ICONV(OCONV(COMP.SELL<CP>,"MR2"),"MR9")

                   COMP.COST<CP> = ICONV(OCONV(COMP.COST<CP>,"MR9"),"MR2")
                   COMP.COST<CP> = ICONV(OCONV(COMP.COST<CP>,"MR2"),"MR9")

                   IF CP=CP.CT THEN
                      COMP.SELL<CP> = TMP.TC
                      COMP.COST<CP> = TMP.PR
                   END

                   *** Doing this so if we have a component coming off the
                   *** kit, we can take it's respective $ amount that it
                   *** contributed to the TOTPRCS and re-distribute it to
                   *** the components are being added to the kit
                   IF COMPM < 0 THEN
                      KITNEGS += COMP.SELL<CP>
                      KITNEGC += COMP.COST<CP>
                   END ELSE
                      *** Keep track of positive components, may need later
                      *** to re-distribute componets that are removed.
                      KITCPS<1,-1>=CP
                      KITADDS += COMP.SELL<CP>
                      KITADDC += COMP.COST<CP>
                   END

                   TMP.TC -= COMP.SELL<CP>
                   TMP.PR -= COMP.COST<CP>
                END
             NEXT CP

             *** If we had a negative qty, ie a component coming off the
             *** kit, we need to distribute it's contribution to the
             *** previous distribution back into the positive qty
             *** components on the kit.
             *** NOTE: Its value must be doubled in order to stay
             *** in balance when postings are created later.
             IF KITNEGS # 0 THEN
                KCP.CT = DCOUNT(KITCPS,VM)

                *** need to get to whole penny for distribution purposes
                KITNEGS = ICONV(OCONV(KITNEGS,"MR9"),"MR2")
                KITNEGS = ICONV(OCONV(KITNEGS,"MR2"),"MR9")*2

                KITNEGC = ICONV(OCONV(KITNEGC,"MR9"),"MR2")
                KITNEGC = ICONV(OCONV(KITNEGC,"MR2"),"MR9")*2

                TMP.KS  = KITNEGS
                TMP.KC  = KITNEGC

                FOR KCP = 1 TO KCP.CT
                   CP = KITCPS<1,KCP>


                   IF KITADDS THEN
                      COMP.SELLADD = ABS(INT(((COMP.SELL<CP>/KITADDS)*KITNEGS)))
                   END ELSE
                      COMP.SELLADD = 0
                   END
                   COMP.COSTADD=ABS(INT(((COMP.COST<CP>/KITADDC)*KITNEGC)))

                   COMP.SELLADD = ICONV(OCONV(COMP.SELLADD,"MR9"),"MR2")
                   COMP.SELLADD = ICONV(OCONV(COMP.SELLADD,"MR2"),"MR9")

                   COMP.COSTADD = ICONV(OCONV(COMP.COSTADD,"MR9"),"MR2")
                   COMP.COSTADD = ICONV(OCONV(COMP.COSTADD,"MR2"),"MR9")

                   IF KCP=KCP.CT THEN
                      COMP.SELLADD = TMP.KS
                      COMP.COSTADD = TMP.KC
                   END

                   TMP.KS -= COMP.SELLADD
                   TMP.KC -= COMP.COSTADD

                   COMP.SELL<CP>+= COMP.SELLADD
                   COMP.COST<CP>+= COMP.COSTADD
                NEXT KCP
             END
          END

*---------Update Sales Amount
          FOR GLP=1 TO CP.CT
             CPN = COMP.PNS<1,GLP>
             READV PRD2 FROM PRDFILE, CPN, 2 ELSE PRD2 = ''
             READV PRD3 FROM PRDFILE, CPN, 3 ELSE PRD3 = ''
             SLS.AMT = ICONV(OCONV(COMP.SELL<GLP>,'MR9'),'MR2')

             IF QS > 0 THEN
                SLS.AMT = SLS.AMT * -1
             END

             *** if this component is being taken off the kit the
             *** sign of the SLS.AMT needs to be switched
             IF COMP.MS<1,GLP> < 0 THEN
                SLS.AMT = SLS.AMT * -1
             END

             GL.BR = SLS.BR
             GL.AMT = -SLS.AMT;   DGL.AMT = -SLS.AMT
             PTYPE = PDATA<1,GLP>

             IF PRD3=3 THEN
                SP.ID  = 'MISC';    GL.NO = PRD2
             END ELSE
                SP.ID  = 'SLS'
                GOSUB EXT.SP.ID
             END

             GOSUB UPD.GL
          NEXT GLP

*---------Update Cost Of Goods Sold
          FOR GLP=1 TO CP.CT
             COGS.AMT = ICONV(OCONV(COMP.COST<GLP>,'MR9'),'MR2')

             IF QS > 0 THEN
                COGS.AMT = COGS.AMT * -1
             END
             *** if this component is being taken off the kit the
             *** sign of the COGS.AMT needs to be switched
             IF COMP.MS<1,GLP> < 0 THEN
                COGS.AMT = COGS.AMT * -1
             END

             SP.ID  = 'COGS'
             GL.AMT = COGS.AMT;   DGL.AMT = COGS.AMT
             PTYPE = PDATA<1,GLP>
             GL.BR = SLS.BR
             GOSUB EXT.SP.ID
             GOSUB UPD.GL

*---------Update Inventory
             GL.AMT = -COGS.AMT;  DGL.AMT = -COGS.AMT
             SP.ID  = INVTY.ID
             GL.NO  = INVTY.NO
             GL.BR  = STK.BR
             GOSUB UPD.GL

*---------Update Accounts Receivable with balancing entry
             OE.UPD.GL.AR OID,GEN,GL.IDS,GL.NOS,GL.AMTS
          NEXT GLP

          * Add pass Along Freight
          IF PASS.FGHT THEN
             SP.ID   = INVTY.ID
             GL.NO   = INVTY.NO
             GL.BR   = STK.BR
             GL.AMT  = -PASS.FGHT
             DGL.AMT = GL.AMT
             GOSUB UPD.GL
          END

          * Update Comm-Cost / Cost Of Goods Sold Difference
          COMM.COGS.DIFF = LD(27)<1,GEN> - LD(10)<1,GEN>
          COMM.COGS.DIFF = OCONV(QS*COMM.COGS.DIFF,'MR07')
          IF COMM.COGS.DIFF+0 # 0 THEN
             SP.ID   = 'CM.CGS.DIFF'
             OS.SP.ID= 'CCCDTPL'            ; * Offset
             GL.BR   = SLS.BR
             GL.AMT  = -COMM.COGS.DIFF
             DGL.AMT = -COMM.COGS.DIFF
             GOSUB EXT.SP.ID
             LOCATE SP.ID IN GL.AUTO<1> SETTING POS THEN
                * Verify offset exists before doing any updates
                LOCATE OS.SP.ID IN GL.AUTO<1> SETTING XPOS THEN
                   * Both GL accounts must be specified
                   IF GL.AUTO<2,POS> AND GL.AUTO<2,XPOS> THEN
                      * Ensure diff and clearing accts are not same
                      IF GL.AUTO<2,POS> # GL.AUTO<2,XPOS> THEN
                         GL.NO = GL.AUTO<2,POS>
                         GOSUB UPD.GL

                         SP.ID   = OS.SP.ID
                         POS     = XPOS
                         GL.BR   = SLS.BR
                         GL.AMT  = COMM.COGS.DIFF
                         DGL.AMT = COMM.COGS.DIFF
                         GL.NO = GL.AUTO<2,POS>
                         GOSUB UPD.GL
                      END
                   END
                END
             END
          END

          RETURN
*-------------------------------------------------------------------------*
UPD.DIRECT: * Update G/L for a direct shipment (P/O Side)
*** NOTE: we do NOT take into account line item freight on a direct PO
*** because we would be increasing INVTY by an amount that the sales side
*** would not know how to relieve it. They can put FGHT on the PO side and
*** also a freight vendor but they can NOT capitalize the freight into
*** INVTY...
          PAY.BR = LED(2)<1,GEN,3>
          LOCATE LED(33)<1,GEN> IN LED(12)<1> SETTING GN ELSE RETURN

          STK.BR = LED(2)<1,GN,2>
          QS       = SUM(LD(5)<1,GN>) + SUM(LD(6)<1,GN>)
          COGS.AMT = ICONV(OCONV(QS*LD(10)<1,GEN>,'MR9'),'MR2')
          ** Next check is put in because we normally store the cost on
          ** the PO side which the previous statement picks up. However
          **for release 7.0.1.6 and lower the cost was not on the po side
          READV REL.LVL FROM CTRLFILE,"RELEASE.VER",1 ELSE REL.LVL = 0
          REL.LVL = FIELD(REL.LVL,'-',1)
          CONVERT '.' TO '' IN REL.LVL
          CONVERT '+' TO '' IN REL.LVL
          *** Make sure the release levels in a consistant format.
          REL.LEN = LEN(REL.LVL)
          IF REL.LEN < 4 THEN
             DIFF = 4 - REL.LEN
             REL.LVL = REL.LVL:STR(0,DIFF)
          END
          IF REL.LVL < 7017 THEN
            COGS.AMT = ICONV(OCONV(QS*LD(10)<1,GN>,'MR9'),'MR2')
          END
          AP.AMT = -COGS.AMT
          PTYPE  = PRD(2)
          SOURCE = LED(3)<1,GEN>

*---------Update Inventory Amount
          GL.AMT = AP.AMT;   DGL.AMT = AP.AMT
          FET    = ICONV(OCONV((LD(55)<1,GN>*QS),'MR9'),'MR2')
          FET    = FET * -1
          GL.AMT+= FET; DGL.AMT += FET
          GL.BR  = STK.BR

*** NOTE: we do NOT take into account line item freight on a direct PO
*** because we would be increasing INVTY by an amount that the sales side
*** would not know how to relieve it. They can put FGHT on the PO side and
*** also a freight vendor but they can NOT capitalize the freight into
*** INVTY...
          IF PRD(3)=3 THEN
             SP.ID = 'MISC';     GL.NO = PRD(2)
             GOSUB UPD.GL
          END ELSE
             SP.ID = INVTY.ID;    GL.NO = INVTY.NO
             GOSUB UPD.GL

*---------Update FET Reserve for items that are not prepaid FET
             IF LD(57)<1,GEN> # '1' THEN
                SP.ID = 'FET'; GL.AMT = -FET;    DGL.AMT = -FET
                IF GL.AMT # 0 THEN
                  GL.BR = PAY.BR
                  GOSUB EXT.SP.ID
                  GOSUB UPD.GL
                END
             END

*---------Update Purchases
             SP.ID  = 'PURCH';  GL.AMT = AP.AMT;   DGL.AMT = AP.AMT
             GL.NO  = GL.AUTO.PURCH
             GL.BR  = PAY.BR
             GOSUB UPD.GL

*---------Update Clear Purchases to Inventory
             SP.ID  = 'CPTI';   GL.AMT = -AP.AMT;  DGL.AMT = -AP.AMT
             GL.NO  = GL.AUTO.CPTI
             GL.BR  = PAY.BR
             GOSUB UPD.GL
          END

*---------Update Accounts Payable with balancing entry
          GOSUB POST.UBAP

          RETURN
*-------------------------------------------------------------------------*
UPD.ADJ:  ** Update Gl for a Inventory Adjustments **

          SOURCE = LED(3)<1,GEN>
          PTYPE  = PRD(2)
          STK.BR  = LED(2)<1,GEN,2>
          GL.BR   = LED(2)<1,GEN,3>
          QA      = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
          ADJ.AMT = ICONV(OCONV(QA*LD(8)<1,GEN>,'MR9'),'MR2')
*---------Update Inventory Amount

          * Need to allow for adjustments to vendor consignment
          LOCA    = FIELD(LD(7)<1,GEN>,'~',1)
          LOC.TYP = FIELD(LOCA,'~',1)[1,1]
          VCNSGN  = TRIM(FIELD(LOCA,'~',1)[2,99])
          GL.BR  = STK.BR
          IF VCNSGN THEN
             SP.ID = 'CSGN.INVTY'
             SOURCE = LED(3)<1,GEN>
             PTYPE  = PRD(2)
             GOSUB EXT.SP.ID
          END ELSE
             SP.ID  = INVTY.ID
             GL.NO  = INVTY.NO
          END
          GL.AMT = ADJ.AMT
          DGL.AMT = ADJ.AMT
          GOSUB UPD.GL
*---------Update Purchases
          IF LED(62)<1,GEN,1> # GL.AUTO.INVADJ THEN
             GL.NO  = LED(62)<1,GEN,1>
             SP.ID  = LED(62)<1,GEN,2>
          END ELSE
             *** See if there is an override
             SP.ID = 'INVADJ'
             GOSUB EXT.SP.ID
          END
          GL.AMT = -ADJ.AMT;   DGL.AMT = -ADJ.AMT
          GL.BR  = GL.BR
          GOSUB UPD.GL

          RETURN
*-------------------------------------------------------------------------*
UPD.WIP:  ** Update Gl for a work order transaction **

          STK.BR = LED(2)<1,GEN,2>
          PAY.BR = LED(2)<1,GEN,3>
          QR     = SUM(LD(5)<1,GEN>) + SUM(LD(6)<1,GEN>)
IN$$1:    INPROC = (LED(6)<1,GEN> = 'I')
          WAS.COMP = OLED(6)<1,GEN> = 'C'

          IF INPROC AND QR > 0 THEN RETURN

          AP.AMT = ICONV(OCONV(QR*LD(8)<1,GEN>,'MR9'),'MR2')
          SOURCE = LED(3)<1,GEN>

          * Make sure comments don't figure into any inventory
          IF LD(1) = 'C' THEN AP.AMT = 0

*---------Update Inventory Amount
          GL.AMT = AP.AMT;   DGL.AMT = AP.AMT
          GL.BR  = STK.BR

          IF PRD(3)=3 THEN
             SP.ID  = 'MISC';   GL.NO = PRD(2)
             GOSUB UPD.GL
          END ELSE
             SP.ID  = INVTY.ID;  GL.NO = INVTY.NO
             GOSUB UPD.GL
          END

*---------Delete all UBAP ref's so that we can reload them (or leave alone)
          SP.ID  = 'CFTI'
          GOSUB DELETE.ALL.REFS

          *** Delete both SP.IDS because they may have changed the
          *** vendor between time
          SP.ID = 'UBAP'
          GOSUB DELETE.ALL.REFS
          SP.ID = 'XAP'
          GOSUB DELETE.ALL.REFS

          READV VEN.GL FROM CUSFILE,LED(1)<1,GEN>,100 ELSE VEN.GL = ''
          IF VEN.GL = '' THEN
             READV VEN.GL FROM CUSFILE,LED(5)<1,GEN>,100 ELSE VEN.GL=''
          END
          IF VEN.GL = '' THEN
             SP.ID  = 'UBAP'
             GL.BR  = PAY.BR
             GL.NO  = GL.AUTO.UBAP
          END ELSE
             SP.ID  = 'XAP'
             GL.BR  = PAY.BR
             GL.NO  = VEN.GL
          END


*---------Update Accounts Payable with balancing entry
          *** setup freight value
          OVRD.FRT = NO
          LOCATE 'FGHT.IN' IN GL.IDS<1,1> SETTING FPOS THEN
             WIP.FREIGHT = GL.AMTS<1,1,FPOS>
             *** See if there is a freight override so that we can
             *** back out freight from UBAP when we post.
             LOCATE 'XFAP' IN GL.IDS<1,1> SETTING XPOS THEN
                IF GL.AMTS<1,1,XPOS>+0 # 0 THEN
                   OVRD.FRT = YES
                END
             END
          END ELSE
             WIP.FREIGHT = 0
          END

          *** setup handling value
          LOCATE 'HNDL.IN' IN GL.IDS<1,1> SETTING HPOS THEN
             WIP.HANDLING = GL.AMTS<1,1,HPOS>
          END ELSE
             WIP.HANDLING = 0
          END

          * See if WO.PURCH posting already exists if it does then we
          * have to update
          WO.PURCH.EXISTS = NO
          FINDSTR 'WO.PURCH' IN GL.IDS<1,1> SETTING X,Y,GL.POS THEN
             WO.PURCH.EXISTS = YES
          END

          *** check FRIEGHT, HANDLING, SETUP COST, PROCESS COST to see
          *** if GL should be updated. Handling and Freight were added to
          *** this check because we want to post to GL if there is
          *** Freight or Handling just like Setup Cost and Process Cost.
          IF (LED(105)<1,GEN> OR LED(106)<1,GEN> OR WO.PURCH.EXISTS OR WIP.FREIGHT OR WIP.HANDLING) AND NOT(INPROC) THEN

             WIP.CALC.PROC.COSTS GEN,TPROC.COST
             LED(109)<1,GEN> = LOWER(TPROC.COST)
             *** reset process cost in case it calculated it differently
             PROC.AMT = LED(109)<1,GEN,1>+0

             SETUP.AMT = LED(106)<1,GEN>+0
             PROC.COST = PROC.AMT + SETUP.AMT

             LOCATE SP.ID IN GL.IDS<1,1> SETTING POS ELSE

                *** Remove freight from UBAP posting if they have XFAP
                IF OVRD.FRT THEN
                   GL.AMT = -(PROC.COST + WIP.HANDLING)
                END ELSE
                   GL.AMT = -(PROC.COST + WIP.FREIGHT + WIP.HANDLING)
                END
                IF GL.AMT+0 # 0 THEN
                   GOSUB UPD.GL
                END

                SP.ID  = 'CFTI'
                GL.AMT = -(WIP.FREIGHT+WIP.HANDLING)
                GOSUB DELETE.ALL.REFS

                IF GL.AMT+0 # 0 THEN
                  SP.ID  = 'CFTI'
                  LOCATE SP.ID IN GL.AUTO<1> SETTING POS THEN
                     GL.NO = GL.AUTO<2,POS>
                     GOSUB UPD.GL
                  END
                END
             END

             GL.BR  = PAY.BR

*---------Update Work Order Purchases
             SP.ID  = 'WO.PURCH';  GL.AMT = PROC.COST;   DGL.AMT = PROC.COST

             LOOP
                FINDSTR 'WO.PURCH' IN GL.IDS<1,1> SETTING X,Y,GL.POS THEN
                   GL.IDS  = DELETE(GL.IDS,1,1,GL.POS)
                   GL.NOS  = DELETE(GL.NOS,1,1,GL.POS)
                   GL.AMTS = DELETE(GL.AMTS,1,1,GL.POS)
                END ELSE EXIT
             REPEAT

             PTYP.CT = DCOUNT(LED(109)<1,GEN>, SVM)

             *** If there are no product types still defined then post
             *** the entire amount to a null product type.
             IF PTYP.CT = 1 THEN
                PTYPE = ''
                GOSUB EXT.SP.ID
                IF GL.AMT+0 # 0 THEN
                   GOSUB UPD.GL
                END
             END ELSE
                *** Loop through all the product types for this gen
                PROC.BAL = 0
                FOR PT = 2 TO PTYP.CT
                   PTYPE = FIELD(LED(109)<1,GEN,PT>,'~',2)
                   IF PT = PTYP.CT THEN
                      * PROC.COST below contains both setup and process
                      * costs.
                      GL.AMT  = PROC.COST - PROC.BAL
                      DGL.AMT = GL.AMT
                   END ELSE
                      PRO.PCT = OCONV(FIELD(LED(109)<1,GEN,PT>,'~',1)+0,'MR6')
                      *** Prorate the setup costs.
                      GL.AMT   = ICONV(OCONV(SETUP.AMT,'MR2') * PRO.PCT,'MR2')
                      *** Now add the process costs.
                      GL.AMT  += ICONV(OCONV(PROC.AMT,'MR2') * PRO.PCT,'MR2')
                      DGL.AMT  = GL.AMT
                      PROC.BAL+= GL.AMT
                   END
                   GOSUB EXT.SP.ID
                   IF GL.AMT+0 # 0 THEN
                      GOSUB UPD.GL
                   END
                NEXT PT
             END

*---------Update Clear Work Order to Inventory
             SP.ID  = 'CWOTI';   GL.AMT = -PROC.COST;  DGL.AMT = -PROC.COST
             GOSUB EXT.SP.ID
             GOSUB DELETE.ALL.REFS
             IF GL.AMT+0 # 0 THEN
                GOSUB UPD.GL
             END
          END

*---------Clear any WIP Write Off entries
          SP.ID  = 'CWTLI'
          GOSUB DELETE.ALL.REFS

*---------Update clearing account to make balancing entry...
          SP.ID  = 'CWTI'
          LOCATE SP.ID IN GL.AUTO<1> SETTING POS THEN
             GL.NO = GL.AUTO<2,POS>
          END
          GOSUB DELETE.ALL.REFS
          GL.AMT  = -SUM(GL.AMTS)


          *** If there is no incoming material and all steps are closed
          *** then we are going to hit the write off account if one
          *** exists
          WO.COMPLETE = NO
          READV GL.ACCT FROM CTRLFILE,"WIP.WRITE.OFF.ACCT",1 ELSE GL.ACCT = ""
          IF GL.ACCT # "" AND GL.ACCT # GL.NO THEN
             ***If there is a balance and this step is complete
             ***Check to see if we need to write material costs off
             MATBUILD SV.LD FROM LD
             WO.COMPLETE = YES
             GN.CT = DCOUNT(LED(6)<1>,VM)
             ***Check the rest of the gens to see if entire w/o is complt
             FOR GN = 1 TO GN.CT
                IF LED(6)<1,GN> # "C" AND LED(6)<1,GN> # "X" THEN
                   WO.COMPLETE = NO
                   EXIT
                END
                IF LED(6)<1,GN>#"X" THEN
                   LDIDS = LED(48)<1,GN>
                   LD.CT = DCOUNT(LDIDS<1,1>,SVM)
                   *** Loop through all the LDIDS on this GN
                   *** and add up the costs
                   FOR LDN = 1 TO LD.CT
                      LDID = LDIDS<1,1,LDN>
                      LD.GET LDID
                      QS.IN =  SUM(LD(5)<1,GN>)+SUM(LD(6)<1,GN>)
                      IF QS.IN > 0 THEN
                         ***If there is material coming in we are going to
                         ***set WO.COMPLETE to No so it won't write to the
                         ***Write Off Account
                         WO.COMPLETE = NO
                         EXIT
                      END
                   NEXT LDN
                   IF NOT(WO.COMPLETE) THEN EXIT
                END
             NEXT GN
             *** Reset LD back
             MATPARSE LD FROM SV.LD
             ***If entire w/o is complete and no incoming material then
             ***change GL Account to what they designate for write off.
             IF WO.COMPLETE THEN GL.NO = GL.ACCT; SP.ID = 'CWTLI'
          END
          IF GL.AMT+0 # 0 THEN
             DGL.AMT = ''
             GOSUB UPD.GL
          END

          ***If we sent values to the write off account then do it for
          ***the rest of the gens as well.
          IF WO.COMPLETE THEN
             GN.CT = DCOUNT(LED(6)<1>,VM)
             FOR GN = 1 TO GN.CT
               ***Check the rest of the gens and move their holding account
               ***entries to the destructive GL no.
               IF LED(6)<1,GN>="C" AND GN#GEN THEN
                 LOCATE 'CWTI' IN LED(24)<1,GN> SETTING POS THEN
                    LED(24)<1,GN,POS> = SP.ID
                    LED(25)<1,GN,POS> = GL.NO
                 END
              END
            NEXT GN
          END
          RETURN
*-------------------------------------------------------------------------*
EXT.SP.ID: *
     * Get G/L number for standard posting extensions....
          GL.NO  = ''

          *** Need to see if SP.ID is a valid Posting ID for sales source,
          *** product, buy line, price line or Entity overrides
          LOCATE SP.ID IN EXT.IDS SETTING EPOS THEN
             GOSUB CREATE.GLNO
          END ELSE
             LOCATE SP.ID IN GL.AUTO<1> SETTING POS THEN
                GL.NO = GL.AUTO<2,POS>
             END
          END

          RETURN
*-------------------------------------------------------------------------*
CREATE.GLNO: *** Routine that will create dynamic autopost.  If they are
             *** not using Dynamic creation then it will use the old method
             *** of getting the id from Control File GL.AUTO

          IF GL.DYN.AUTO.POST$ THEN
             *** Get BuyLine Override
             READV BUYOVRD FROM BLNEFILE,PRD(12),25 ELSE BUYOVRD = ''
             *** Get PriceLine Override
             READV PRCOVRD FROM PLNEFILE,PRD(9), 25 ELSE PRCOVRD = ''
             *** Get Product Override
             PRDOVRD = PRD(151)
             *** Get Entity override.
             IF LED(1)<1,GEN> # '' THEN
                READV ENTDOVRD FROM CUSFILE,LED(1)<1,GEN>,185 ELSE ENTDOVRD = ''
             END ELSE
                ENTDOVRD = ''
             END
             *** Get Entity override.
             IF LED(5)<1,GEN> # '' THEN
                READV ENTPOVRD FROM CUSFILE,LED(5)<1,GEN>,185 ELSE ENTPOVRD = ''
             END ELSE
                ENTPOVRD = ''
             END

             *** Get Reason Code
             RCODE = LD(46)<1,1>

             *** Now Call routine to creat the autopost
             GL.CREATE.AUTO.ACCT GL.NO,OID,GEN,GL.BR,SP.ID,BUYOVRD,PRCOVRD,PRDOVRD,ENTDOVRD,ENTPOVRD,RCODE

          END ELSE
             *** Old Method of using Sales Source and Product Type
             SP.EXT = SP.ID:'~':SOURCE:'~':PTYPE
             LOCATE SP.EXT IN GL.AUTO<1> SETTING POS THEN
                GL.NO = GL.AUTO<2,POS>
                IF GL.NO#'' THEN
                   SP.ID = SP.EXT
                   RETURN
                END ELSE
                   SP.EXT = SP.ID:'~~':PTYPE
                   LOCATE SP.EXT IN GL.AUTO<1> SETTING POS THEN
                     GL.NO = GL.AUTO<2,POS>
                     IF GL.NO#'' THEN
                        SP.ID = SP.EXT
                        RETURN
                     END
                   END
                END
             END
             *** If we get to hear we failed to find anything so we will
             *** go get the default value.
             LOCATE SP.ID IN GL.AUTO<1> SETTING POS THEN
                GL.NO = GL.AUTO<2,POS>
             END
          END

          RETURN
*-------------------------------------------------------------------------*
DELETE.ALL.REFS: *
          LOOP
          LOCATE SP.ID IN GL.IDS<1,1> SETTING POS THEN
             GL.IDS  = DELETE(GL.IDS,1,1,POS)
             GL.NOS  = DELETE(GL.NOS,1,1,POS)
             GL.AMTS = DELETE(GL.AMTS,1,1,POS)
          END ELSE EXIT
          REPEAT
          RETURN
*-------------------------------------------------------------------------*
UPD.GL:   IF GL.NO = '' THEN GOTO ABORT.GL
          GL.NO    = GL.BR:'~':GL.NO

          IF DGL.AMT # '' THEN
             DGL.IDS  = INSERT(DGL.IDS,1,1,-1;SP.ID)
             DGL.NOS  = INSERT(DGL.NOS,1,1,-1;GL.NO)
             DGL.AMTS = INSERT(DGL.AMTS,1,1,-1;DGL.AMT)
          END

          XSP.ID = SP.ID
          IF INDEX(XSP.ID,'^',1) THEN
             XSP.ID = FIELD(XSP.ID,'^',2)
          END
          FOUND    = NO
          GL.CT    = DCOUNT(GL.IDS<1,1>,SVM)
          FOR POS  = 1 TO GL.CT
             TSP.ID = GL.IDS<1,1,POS>
             IF INDEX(TSP.ID,'^',1) THEN
                TSP.ID = FIELD(TSP.ID,'^',2)
             END
             IF XSP.ID = TSP.ID THEN
                IF GL.NO = GL.NOS<1,1,POS> THEN
                   FOUND = YES
                   EXIT
                END
             END
          NEXT POS

          IF NOT(FOUND) THEN
             GL.IDS  = INSERT(GL.IDS,1,1,POS;SP.ID)
             GL.NOS  = INSERT(GL.NOS,1,1,POS;GL.NO)
             GL.AMTS = INSERT(GL.AMTS,1,1,POS;'')
          END

          GL.AMTS<1,1,POS> = GL.AMTS<1,1,POS> + GL.AMT

          RETURN
*-------------------------------------------------------------------------*
ABORT.GL: LOG.ERROR 'Auto Posting : ':SP.ID:' not found, OID : ':OID:'  GEN : ':GEN:'  Amt : ':OCONV(GL.AMT,'MR2')

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~06/14/16~13:25
