*     ******************  FINDBILLS COMMAND FILE  *****************
* This procedure finds specific bills that we are looking for, then allows
* us to edit them.
*      The bill can be specified by bill number and/or amount.  If you decide
* not to pay a bill that was found specifying more than one item, you will be
* presented the rest of the entries for the supplier based on name only.
**************************************************************************

SELECT PRIMARY
USÅ B:CostBaså INDEØ B:$Supp

STORE 'N' TO Finished
DO WHILE !(Finished) <> 'F'

   * "Entering" controls a closed loop that allows the operator to change
   * the entry if he or shee spots an error.
   STORE "C" TO Entering
   DO WHILE !(Entering) = 'C'
      ERASE
      @ 4,0 
      ACCEPT ' NAME OF SUPPLIER ' TO MName
      ACCEPT 'THEIR BILL NUMBER ' TO MBill:Nmbr
      ACCEPT '     ENTER AMOUNT ' TO Temp
      STORE !(MName) TO MName
      STORE !(MBill:Nmbr) TO MBill:Nmbr
      STORE VAL(Temp) TO MAmount
      STORE MAmount*1.00 TO MAmount
      @  6,19 SAY MName
      @  7,19 SAY MBill:Nmbr
      @  8,19 SAY MAmount
      @ 11, 0 SAY '        C to CHANGE,'
      ? '     <Return> to continue.'

      * OneByOne is used so that we look at the entire listing for a name once.
      * If we could have started in the middle of the list and the bill is not
      * the one we want, we go up to the first listing then go through all the
      * entries for the name, one by one.  Used in the last loop in this file.
      IF Bill:Nmbr > '     ' .OR Amount <> 0
         STORE 0 TO OneByOne
      ELSE
         STORE 1 TO OneByOne
      ENDIF

      WAIT TO Entering
   ENDDO Entering

   STORE T TO Looking
   @ 11, 0 SAY "I'M LOOKING, I'M LOOKING!!"
   @ 12, 0
   @ 13, 0

   * Now look for a match on the first 10 characters of the name.  This finds
   * the first entry for that supplier, then looks for bill number or amount
   * if we specified them.  If not specified, it skips through all entries
   * for the name.

   IF LEN(MName) > 10
      STORE $(MName,1,10) TO Key
   ELSE
      STORE MName TO Key
   ENDIF

   FIND &Key

   @ 11, 0
   IF # = 0
      ?
      ? "   GEE, I CAN'T FIND THE NAME.  Please check the spelling."
      ? "   Or maybe it hasn't been posted to the COSTBASE yet."
      ? '<Return> to continue.'
      WAIT
      ERASE
   ELSE
      * Found at least one entry with a matching name.
      STORE T TO Looking
      IF MBill:Nmbr = ' ' .AND. MAmount = 0
         STORE F TO Looking
      ELSE
         * If we have more than the name, we first check for the bill number.
         IF MBill:Nmbr > ' '
            DO WHILE Name=Key .AND. .NOT. EOF .AND. Looking
               IF Bill:Nmbr <> MBill:Nmbr
                  SKIP
               ELSE
                  STORE F TO Looking
               ENDIF
            ENDDO

            * If we're on a new name or the end of the file, Looking is TRUE
            * because we have not found the supplier we were looking for.
            * Otherwise, we have a matching bill number to confirm.
            IF Looking
               ? '   This BILL NUMBER is not in the costbase.'
               ? '<Return> to continue.'
               WAIT
            ENDIF
         ELSE
            * If no bill number, look for the amount.
            DO WHILE Name=Key .AND. .NOT. EOF .AND. Looking
               IF Amount <> MAmount
                  SKIP
               ELSE
                  STORE F TO Looking
               ENDIF
            ENDDO

            * If we're on a new name or the end of the file, Looking is TRUE
            * Otherwise, we have an unpaid bill to confirm.
            IF Looking
               ? '   No bill for this amount and this supplier.'
               ? '<Return> to continue.'
               WAIT
            ENDIF
         ENDIF we have the bill number
      ENDIF we have "only" the name
   ENDIF there is an unpaid bill for the supplier
   
   STORE 'N' TO Changing
   DO WHILE !(Changing) <> 'Y' .AND. .NOT. Looking
      @ 12,0
      DISPLAY
      ? CHR(7)
      ? '        E to EDIT this record,'
      ? '        Q to QUIT this supplier,'
      ACCEPT '     <Return> to continue.' TO Changing
      ?
      IF !(Changing) = 'Q'
         STORE T TO Looking
      ELSE
         IF !(Changing) = 'E'
            STORE STR(#,5) TO Found
            EDIT &Number
            ERASE
         ELSE
            * If the first record is not the one we want, we skip through the
            * rest of the entries for the name.  We first go on from where we
            * were in the listing (if we had more than one name), then go back
            * to the first entry and look at those we had skipped.  If we had
            * only the name, OneByOne = 1 and we go through the list only once.
            SKIP
            IF EOF .OR. Name <> Key
               IF OneByOne = 0
                  FIND &Key
                  STORE 1 TO OneByOne
               ELSE
                  @ 11, 0 SAY CHR(27) + CHR(74)
                  ? "We've gone through all the entries for " +MName+'.'
                  ? '<Return> to continue.'
                  STORE T TO Looking
                  WAIT
               ENDIF
            ENDIF we've gone through the list
         ENDIF is it the right record
      ENDIF
   ENDDO Changing the record
   ?
   ? '        F if FINISHED finding bills,'
   ? '     <Return> to cnotinue.'
   ? CHR(7)
   WAIT TO Finished
ENDDO Finished

