          SUBROUTINE (IDLIST,CONLIST,ERR.MSGS,CON.BY,SHOW.COST,SIGLIST)
** Version# 19 - 03/04/2009 - 10:36am - BABS - main

*** Subroutine - CREATE.CONSOLIDATED.INVS
*-------------------------------------------------------------------------*
*** Program will take a list of invoices for and consolidate them into
*** one invoice
*-------------------------------------------------------------------------*
*** IDLIST  -  list of invoices to be consolidated attriubute marked [IN]
***         -  by ship-to customer.  Value marked by branch.
*** CONLIST -  list of consolidate invoices that were created        (OUT)
***            Will also contain single invoices if we are not
***            consolidating single invoices (control record)
*** ERR.MSG -  list of error message if any                          (OUT)
*** CON.BY  -  Consolidate by option. This will be used to determine (IN)
***            the ship-to.  If this is blank then we will use the
***            ship to of the first invoice to be consolidated.
*** SHOW.COST- Option to show costs on converted invoices,           (IN)
***         -  (None, COGS, Comm Cost)
*** SIGLIST - NO LONGER USED                                         (OUT)
*** SIGLIST - list of invoices that need to be printed but where not (OUT)
***           consolidated invoice due to only one invoice in the
***           list and the only consolidate multiple invoice control
***           being set to 'Y'
*-------------------------------------------------------------------------*
*** Common Variables - LED
*-------------------------------------------------------------------------*

          IF CON.BY = '' THEN CON.BY = 'Ship-To'
          ERR.MSGS = ''
          CONLIST  = ''
          C.CNT    = 0
          SIGLIST  = ''
          S.CNT    = ''
          FET.AMT  = 0
          FET.OVRD = 0
          READ CON.PN FROM CTRLFILE,'CONSOLIDATE.PRODUCT' ELSE CON.PN = ''
          READV TST FROM PRDFILE,CON.PN,1 ELSE CON.PN = ''

          READ CON.MULT FROM CTRLFILE,'CONS.MULT.INVS' ELSE CON.MULT = ''

          IF CON.PN = '' THEN
             UT.GET.PROMPT '%400',MSG
             ERR.MSGS<-1> = MSG
             RETURN
          END

          *** process all invoices in list by ship-to and branch
          GOSUB PROCESS.INVOICES

          READVU TST FROM ARFILE,INV.NO,0 THEN
             SOE.CALC.DISC OID,GEN,CASH.DISC
             WRITEV CASH.DISC ON ARFILE,INV.NO,9
          END ELSE
             RELEASE ARFILE,INV.NO
          END

          * Sort CONLIST
          SRTLIST = ''
          CLCT = DCOUNT(CONLIST,AM)
          FOR CC = 1 TO CLCT
             CINV = CONLIST<CC>
             LOCATE CINV IN SRTLIST BY 'AL' SETTING POS ELSE
                SRTLIST = INSERT(SRTLIST,POS;CINV)
             END
          NEXT CC
          CONLIST = LOWER(SRTLIST)

          RETURN
*-------------------------------------------------------------------------*
PROCESS.INVOICES: *** loop through each ship to and then branch and
                  *** create a consolidated invoice.

          ST.CNT = DCOUNT(IDLIST,AM)
          FOR ST = 1 TO ST.CNT
             INV.CNT = DCOUNT(IDLIST<ST>,VM)
             IF INV.CNT > 1 OR (NOT(CON.MULT)) THEN
                FOR INV = 1 TO INV.CNT
                   ID = IDLIST<ST,INV>
                   IF INV = 1 THEN GOSUB INIT.VARS
                   *** set up the data so that the consolidated invoice
                   *** can be created by calling OE.CREATE.LEDGER
                   GOSUB SET.DATA
                NEXT INV
                GOSUB CREATE.CONS.INV
                GOSUB INVOICE.ORDER
                GOSUB APPLY.INV
             END ELSE
                *** just need to add this invoice to the list
                GOSUB ADD.SINGLE.INVOICE
             END
          NEXT ST

          RETURN
*-------------------------------------------------------------------------*
INIT.VARS: *** Intialize Variables


          MODE      = 'S'
          ERR.MSG   = ''
          WRITER    = ''
          PO.NO     = ''
          T.OID     = FIELD(ID,'.',1)
          T.GID     = FIELD(ID,'.',2)+0

          READ LED.REC FROM LEDFILE,T.OID ELSE LED.REC = ''

          LOCATE T.GID IN LED.REC<12> SETTING GEN ELSE GEN = 1

          * Options for consolidation are what determines the ship-to,
          * PO#, and writer.  Consolidation can happen by Ship-to, Bill-to,
          * Purchase Order, or Master Job Bid.
          BEGIN CASE
          CASE CON.BY = 'Bill-To'
             READV IS.ST FROM CUSFILE,LED.REC<1,GEN>,7 ELSE IS.ST = ''
             * If bill-to is also a ship-to, make both entities the bill-to
             IF IS.ST<1,2> THEN
                BT.CN = LED.REC<1,GEN>
                ST.CN = BT.CN
             END ELSE
                BT.CN = LED.REC<1,GEN>
                ST.CN = LED.REC<5,GEN>
             END
             PO.NO    = LED.REC<13,GEN>
             WRITER   = LED.REC<73,GEN>
          CASE CON.BY = 'Master Job Bid' AND LED.REC<129,1>
             * Make sure there is a master job bid set in the ledger
             * record else our values will not be set
             READ M.REC FROM LEDFILE,LED.REC<129,1> ELSE M.REC = ''
             ST.CN  = M.REC<5,1>
             BT.CN  = M.REC<1,1>
             PO.NO  = M.REC<13,1>
             WRITER = M.REC<73,1>
          CASE OTHERWISE
             BT.CN     = LED.REC<1,GEN>
             ST.CN     = LED.REC<5,GEN>
             PO.NO     = LED.REC<13,GEN>
             WRITER    = LED.REC<73,GEN>
          END CASE

          PRC.BR    = LED.REC<2,GEN,1>
          OID       = ''
          GEN       = ''
          STATUS    = 'C'
          PNS       = CON.PN
          QTYS      = 1
          INV.IDS   = ''
          NETS        = 0
          NETS<1,1,2> = YES  ;* No GP Check Flag

          *** used to build the comment that will list every invoice
          *** that has been consolidated
          CMTS         = ''
          CMT.CNT      = 0
          LED.DATA     = ''
          LED.DATA<13> = PO.NO
          LED.DATA<73> = WRITER
          LED.DATA<30> = 'CINV'

          RETURN
*-------------------------------------------------------------------------*
SET.DATA: *** set up comments which is just a list of invoices that were
          *** consolidated
          C.OID                 = FIELD(ID,'.',1)
          C.GID                 = FIELD(ID,'.',2)+0

          READ TLED FROM LEDFILE,C.OID ELSE
             UT.GET.PROMPT '%401':AM:C.OID,MSG
             ERR.MSGS<-1> = MSG
             RETURN
          END
          GIDS    = TLED<12>
          INV.NOS = TLED<8>

          LOCATE C.GID IN GIDS<1> SETTING TGEN ELSE
             UT.GET.PROMPT '%402':AM:C.GID,MSG
             ERR.MSGS<-1> = MSG
             RETURN
          END

          INVN = INV.NOS<1,TGEN>
          CMT.CNT              += 1
          INV.ID                = C.OID :'.':INVN "R%3"
          READV OPEN.BAL FROM ARFILE,INV.ID,8 ELSE OPEN.BAL = ''
          CMTS<CMT.CNT>         = INV.ID
          CMTS<CMT.CNT>        := SVM:'Price: '
          CMTS<CMT.CNT>        := OCONV(OPEN.BAL,'MR2$') "R#12"

          IF SHOW.COST # 'None' THEN
             IF SHOW.COST = 'COGS' THEN OPEN.COST = TLED<16,TGEN> ELSE
                OPEN.COST = TLED<17,TGEN>
             END
             CMTS<CMT.CNT>        := SVM:'Cost : '
             CMTS<CMT.CNT>        := OCONV(-1*OPEN.COST,'MR2$') "R#12"
          END

          INV.IDS<CMT.CNT>      = INV.ID
          *** get any FET amounts from invoice so they can be transferred
          *** to the consolidated invoice when it gets created
          MATBUILD SAVELED FROM LED
          MATBUILD SAVELD FROM LD

          MATREAD LED FROM LEDFILE,C.OID ELSE MAT LED = ''
          TLDIDS = LED(48)<1,TGEN>
          LDCNT = DCOUNT(TLDIDS,SVM)
          FOR LN = 1 TO LDCNT
             TLDID = TLDIDS<1,TGEN,LN>
             LD.GET TLDID
             QS      = (SUM(LD(5)<1,TGEN>) + SUM(LD(6)<1,TGEN>)) * -1
             FET.AMT += LD(55)<1,TGEN>*QS
             FET.OVRD += LD(56)<1,TGEN>*QS
          NEXT LN

          MATPARSE LD FROM SAVELD
          MATPARSE LED FROM SAVELED

          RETURN
*-------------------------------------------------------------------------*
CREATE.CONS.INV: *** create consolidated invoice with zero amount.

          INV.MSG = CMTS
          CMTS    = LOWER(LOWER(CMTS))
          LD.DATA = ''
          LD.DATA<1,55> = FET.AMT
          LD.DATA<1,56> = FET.OVRD

          OE.CREATE.LEDGER MODE,0,OID,GEN,PRC.BR,BT.CN,ST.CN,STATUS,DATE(),DATE(),PNS,QTYS,CMTS,,,,,,,,,ERR.MSG,NETS,,,,,,,,,,,,,,,,,LED.DATA,,LD.DATA
          IF ERR.MSG THEN
             UT.GET.PROMPT '%403':AM:ERR.MSG:AM:ST,MSG
             ERR.MSGS<-1> = MSG
          END

          FET.AMT = 0
          FET.OVRD = 0

          RETURN
*-------------------------------------------------------------------------*
INVOICE.ORDER: *** Take the order that has just been created and invoice
               *** it.  Then set the print status to 'B'

          OE.PROCESS.ORDER OID,GEN

          ORD.ID = OID:'.':LED(12)<1,GEN>
          RF.INPROCESS.CLOSE ORD.ID

          OE.UPD.PRINT.STAT OID,GEN,'B'
          OE.UNLOCK.LED OID

          INV.NO         = OID :'.':LED(8)<1,GEN> "R%3"
          C.CNT         += 1

          CONLIST<C.CNT> = ORD.ID

          RETURN
*-------------------------------------------------------------------------*
APPLY.INV:  *** Apply the newly create invoice against the invoices that
            *** are being consolidated so that their balances

          CON.INV.CNT = DCOUNT(INV.IDS,AM)

          FOR CNID = 1 TO CON.INV.CNT
             C.ID     = INV.IDS<CNID>
             C.OID    = FIELD(C.ID,'.',1)
             C.INV    = FIELD(C.ID,'.',2)+0
             READV INV.NOS FROM LEDFILE,C.OID,8 ELSE
                UT.GET.PROMPT '%404':AM:C.OID,MSG
                ERR.MSGS<-1> = MSG
                CONTINUE
             END
             READV C.GIDS FROM LEDFILE,C.OID,12 ELSE C.GIDS = ''
             LOCATE C.INV IN INV.NOS<1> SETTING CGEN ELSE
                UT.GET.PROMPT '%407':AM:C.ID,MSG
                ERR.MSGS<-1> = MSG
                CONTINUE
             END

             READV OPEN.BAL FROM ARFILE,C.ID,8 ELSE OPEN.BAL = ''
             *** Invoice we are moving the amount to
             CID      = OID:'.':LED(8)<1,GEN>"R%3"
             *** invoice we are paying off
             DID      = C.ID

             AR.APPLY.DIRECT CID,DID,-OPEN.BAL,DATE()

             READV PRT.STATS FROM LEDLFILE,C.OID,9 ELSE PRT.STATS = ''
             PRT.STATS<1,CGEN> = 'N'
             WRITEV PRT.STATS ON LEDLFILE,C.OID,9
             DELETE PQFILE,C.OID:'.':C.GIDS<1,CGEN>

          NEXT CNID

          RETURN
*-------------------------------------------------------------------------*
ADD.SINGLE.INVOICE: *** this is just a single invoice and the consolidate
          *** only multiple invoices control record is set... so we just
          *** want to add the invoice to the list so that is can later
          *** be printed.

          ID    =  IDLIST<ST,1>
          C.OID = FIELD(ID,'.',1)
          C.GID = FIELD(ID,'.',2)+0

          READ TLED FROM LEDFILE,C.OID ELSE
             UT.GET.PROMPT '%401':AM:C.OID,MSG
             ERR.MSGS<-1> = MSG
             RETURN
          END

          GIDS    = TLED<12>
          INV.NOS = TLED<8>

          LOCATE C.GID IN GIDS<1> SETTING TGEN ELSE
             UT.GET.PROMPT '%402':AM:C.GID,MSG
             ERR.MSGS<-1> = MSG
             RETURN
          END

          INV.NO         = C.OID :'.':TLED<8,TGEN> "R%3"
          C.CNT         += 1
          CONLIST<C.CNT> = C.OID :'.':C.GID

          RETURN
*-------------------------------------------------------------------------*
!BABS~03/04/09~10:36
