           SUBROUTINE (SELECTED.IDS,ACTIVE.INST)
** Version# 9.0001[1] - 07/21/2016 - 03:30pm - TSMITH - eclipse

*** Subroutine - QUEUE.SELECT.IPQ
*-------------------------------------------------------------------------*
*** This is the subroutine that select the invoice ids for invoice preview
*** queue based on some selection criterion
*-------------------------------------------------------------------------*
*** SELECTED.IDS - IDs selected                                       [OUT]
*** ACTIVE.INST - Instance of Queue/Widget                             [IN]
*-------------------------------------------------------------------------*
          CALL.SEL.I = NO
          CALL.SEL.T = NO
          CALL.SEL.Q = NO
          SELECTED.IDS = ''

          E.SDATE = 0
          L.EDATE = 0
          FOR CURRENT.INST = 1 TO DCOUNT(ACTIVE.INST,AM)
             PARAMS = ''
             PARAMS<1,1> = ACTIVE.INST<CURRENT.INST,2>
             PARAMS<1,2> = ACTIVE.INST<CURRENT.INST,3>

             SOLAR.WIDGET.GET.PARAM PARAMS,'start date',SEL.SDATE
             SOLAR.WIDGET.GET.PARAM PARAMS,'end date',SEL.EDATE
             SOLAR.WIDGET.GET.PARAM PARAMS,'print statuses',SEL.STATS

             * If date is in external format (i.e. variable dating) then
             * convert to internal date.
             IF SEL.SDATE # "" THEN
                IF INDEX(SEL.SDATE,"/",1) > 1 THEN
                   SDATE = REALDATE(SEL.SDATE)
                END ELSE
                   SDATE = SEL.SDATE
                END
                IF SDATE < E.SDATE OR E.SDATE = 0 THEN E.SDATE = SDATE
             END
             IF SEL.EDATE # "" THEN
                IF INDEX(SEL.EDATE,"/",1) > 1 THEN
                   EDATE = REALDATE(SEL.EDATE)
                END ELSE
                   EDATE = SEL.EDATE
                END
                IF EDATE > L.EDATE OR L.EDATE = 0 THEN L.EDATE = EDATE
             END

             SORT.IDS = ""
             CONVERT VM TO AM IN SEL.STATS
             IF SEL.SDATE # "" AND SEL.EDATE # "" THEN
                CALL.SEL.I = YES
             END ELSE
                IF SEL.STATS # "" THEN
                   LOCATE "T" IN SEL.STATS SETTING XX THEN CALL.SEL.T = YES
                END
                CALL.SEL.Q = YES
             END
          NEXT CURRENT.INST

          TEMP.FILE.ID = ""
          TEMP.FILE.HNDL = ""
          UT.TEMPFILE.CREATE TEMP.FILE.ID,TEMP.FILE.HNDL

          IF CALL.SEL.T THEN GOSUB SEL.T
          IF CALL.SEL.Q THEN GOSUB SEL.Q
          IF CALL.SEL.I THEN GOSUB SEL.I

          UT.TEMPFILE.DELETE TEMP.FILE.ID,TEMP.FILE.HNDL
          RETURN
*-------------------------------------------------------------------------*
SEL.T:    *** Select Invoices from the ORDER.QUEUE file.
          CMD  = 'SELECT ORDER.QUEUE WITH &INDEX&.STATUS = "T~S]"'
          EXECUTE CMD CAPTURING MSG
          LOOP
             READNEXT ID ELSE EXIT
             OID = FIELD(ID,".",1)
             GID = FIELD(ID,".",3)+0
             FORMATTED.ID = OID:".":GID

             READV NOTHING FROM TEMP.FILE.HNDL,FORMATTED.ID,0 ELSE
                WRITE "" ON TEMP.FILE.HNDL,FORMATTED.ID
                SELECTED.IDS<-1> = 'ORDER.QUEUE~':ID
             END
          REPEAT
          RETURN
*-------------------------------------------------------------------------*
SEL.Q:    *** Select Invoices from PRINT.QUEUE (will not get N print stat)
          EXECUTE 'SELECT PRINT.QUEUE = "S]" R]' CAPTURING MSG
          LOOP
             READNEXT ID ELSE EXIT
             OID = FIELD(ID,".",1)
             GID = FIELD(ID,".",2)+0
             FORMATTED.ID = OID:".":GID

             READV NOTHING FROM TEMP.FILE.HNDL,FORMATTED.ID,0 ELSE
                WRITE "" ON TEMP.FILE.HNDL,FORMATTED.ID
                * Need to store formatted.id here, otherwise in the case
                * of having a 'Sxxxxxxx.gid.XWARNING' and 'Sxxxxxxx.gid'
                * record, if the .XWARNING record is found first, we will
                * not save the actual invoiced record.  By saving
                * FORMATTED.ID, we will store the invoiced record, and if
                * it's not invoiced yet, it will get thrown out in
                * queue.filter.id.ipq.
                SELECTED.IDS<-1> = 'PRINT.QUEUE~':FORMATTED.ID
             END
          REPEAT
          RETURN
*-------------------------------------------------------------------------*
SEL.I:    *** Select Invoices from AR file
          IDS  = JLI.SELECT(E.SDATE,L.EDATE,,1)
          LOOP
             READNEXT ID ELSE EXIT
             OID  = FIELD(ID,".",1)
             INVN = FIELD(ID,".",2)+0
             GID  = ''

             READV INVNS FROM LEDFILE,OID,8 THEN
                LOCATE INVN IN INVNS<1> SETTING POS THEN
                   READV GIDS FROM LEDFILE,OID,12 THEN
                      GID = GIDS<1,POS>
                   END
                END
             END

             IF GID THEN
                FORMATTED.ID = OID:".":GID
                READV NOTHING FROM TEMP.FILE.HNDL,FORMATTED.ID,0 ELSE
                   WRITE "" ON TEMP.FILE.HNDL,FORMATTED.ID
                   SELECTED.IDS<-1> = 'JLI.QUEUE~':ID
                END
             END
          REPEAT
          RETURN

!TSMITH~07/21/16~15:30
