*     ***********  JOBCOSTS COMMAND FILE  *****************
* Provides summaries of costs by client and job number.  This can
* also be used to summarize all office categories, since they fall
* into these fields.
*      REPORTS ARE BY JOB NUMBER. Client code is used only in the heading.
* The report is actually prepared based on the job number, so accuracy is
* critical.
*      This file works with a partially indexed costbase, so "Unindexed" is
* used to keep track of how many records are not in the index.  If this gets
* beyond a specific number, the operator is prompted to reindex the Costbase.
**************************************************************************

SET TALK OFF

RESTORE FROM B:Constant
DO GetDate

STORE 0 TO Unindexed
STORE ' ' TO Again
DO WHILE !(Again) <> 'F'
   STORE '   ' TO MClient
   STORE '   ' TO MJob:Nmbr
   STORE ' ' TO Hardcopy
   STORE 'N' TO Number
   ERASE
   @  2,11 SAY ' JOB COST SUMMARY :'
   @  4,11 SAY 'ENTER CLIENT CODE ' GET MClient
   @  5,11 SAY ' ENTER JOB NUMBER ' GET MJob:Nmbr
   @  6,11 SAY '       P to PRINT ' GET Hardcopy
   @  7,11 SAY 'SHOW BILL NUMBERS ' GET Number
   READ
   ?
   IF MClient = ' ' .OR. MJob:Nmbr = '  '
      @  9, 0
      ? '     CLIENT or JOB NUMBER missing.'
      ? '     F if Finished,'
      ? '  <Return> to continue.'
      WAIT TO Again
   ELSE
      @ 8,0 SAY CHR(27)+CHR(74)
      ACCEPT '    OPTIONAL JOB DESCRIPTION ' TO Message
      STORE TRIM(!(Message)) TO Message
      STORE !(MClient) TO MClient
      STORE !(Hardcopy) TO Hardcopy
      STORE !(Number) TO Number
      @ 4,30 SAY MClient
      @ 6,30 SAY Hardcopy
      @ 7,30 SAY Number
      @ 9,30 SAY Message
      ?
      ?
      ACCEPT 'Type C to CHANGE any entries' TO Changes
      IF !(Changes) = 'C'
         STORE ' ' TO Again
         ERASE
      ELSE
         ERASE
         IF !(Hardcopy) = 'P'
            STORE "TO PRINT" TO Hardcopy
            SET PRINT ON
         ENDIF Hardcopy

         IF Number = 'Y'
            STORE 'Bill #' TO Other
         ELSE
            STORE CHR(0) TO Other
         ENDIF

         ? $(Date,3,2)+'/'+$(Date,5,2)+'/'+$(Date,1,2)+':   COST SUMMARY FOR ';
                      +'&MClient-&MJob:Nmbr'
         ? '           ' + Message
         ?
         ? 'DATE    NAME                  DESCRIPTION                 AMOUNT';
            +'  &Other'
         ?
         USE B:CostBase INDEX B:$Jobs
         IF Number = 'Y'
            STORE ',Bill:Nmbr' TO Other
         ELSE
            STORE CHR(0) TO Other
         ENDIF

         STORE 0 TO Sum
         STORE 0 TO HowMany
         STORE 0 TO LineCnt
         STORE 0 TO Spacer
         FIND &MJob:Nmbr
         IF # <> 0
            DO WHILE Job:Nmbr = VAL(MJob:Nmbr) .AND. .NOT. EOF
               DISPLAY Next 1 Bill:Date,Name,Descrip+'   ',Amount &Other OFF
               STORE Sum + Amount TO Sum
               STORE LineCnt + 1 TO LineCnt
               STORE Spacer + 1 TO Spacer

               IF Spacer = 10
                  ?
                  STORE 0 TO Spacer
               ENDIF

               IF LineCnt = 50
                  ? CHR(12)
                  STORE 0 TO LineCnt
                  STORE 0 TO Spacer
                  ? 'DATE    NAME                   DESCRIPTION';
                    +'                 AMOUNT'
                  ?
               ENDIF
               SKIP
            ENDDO
         ENDIF

         GO TOP
         IF Job:Nmbr <> 0
            ? 'TIME OUT... MUST reindex COSTBASE'
            ? '  am switching you back to the menu'
            ? '  RUN item 5>... then come back and run this.'
            RELEASE ALL
            RETURN
         ENDIF
         STORE VAL(Name) TO LastReco
         USE B:CostBase
         STORE 0 TO Unindexed
         GOTO LastReco
         SKIP
         DO WHILE .NOT. EOF
            DISPLAY NEXT 1 Bill:Date, Name, Descrip+'   ', Amount;
                    FOR Job:Nmbr = VAL(MJob:Nmbr) OFF
            IF Job:Nmbr = &MJob:Nmbr
               STORE Sum + Amount TO Sum
               STORE LineCnt + 1 TO LineCnt
               STORE Spacer + 1 TO Spacer

               IF Spacer = 10
                  ?
                  STORE 0 TO Spacer
               ENDIF
      
               IF LineCnt = 50
                  ? CHR(12)
                  STORE 0 TO LineCnt
                  STORE 0 TO Spacer
                  ? 'DATE    NAME                   DESCRIPTION';
                    +'                 AMOUNT'         
                  ?
               ENDIF
            ENDIF
            STORE Unindexed + 1 TO Unindexed
            SKIP
         ENDDO
         ?
         ? '                                  TOTAL COSTS TO DATE: ' -;
                                                          STR(Sum,9,2)

         STORE LineCnt + 2 TO LineCnt
         STORE 0 TO Spacer
         IF LineCnt = 40
            ? CHR(12)
            STORE 0 TO LineCnt
         ELSE
            ?
            ?
            ?
         ENDIF

         USE B:Billings
         ? 'BILLED TO DATE FOR &MClient-&MJob:Nmbr'
         ?
         ? 'DATE   INV#   DESCRIPTION                   TAXABLE'+;
            '        TAX    TAX FREE'
         ?
         STORE LineCnt + 4 TO LineCnt
         STORE 0 TO Sum
         STORE 0 TO T
         STORE 0 TO S
         STORE 0 TO F
         DO WHILE .NOT. EOF
            IF Job:Nmbr = &MJob:Nmbr
               DISPLAY Next 1 Inv:Date, Inv:Nmbr, Descrip,STR(Taxable,9,2)+' ';
                   STR(Sales:Tax,9,2)+' ',TaxFree FOR Job:Nmbr = &MJob:Nmbr OFF
               STORE T + Taxable TO T
               STORE S + Sales:Tax TO S
               STORE F + TaxFree TO F
               STORE Sum + Taxable + Sales:Tax + TaxFree TO Sum
               STORE LineCnt + 1 TO LineCnt
               STORE Spacer + 1 TO Spacer

               IF Spacer = 10
                  ?
                  STORE 0 TO Spacer
               ENDIF
 
               IF LineCnt = 50
                  ? CHR(12)
                  STORE 0 TO LineCnt
                  STORE 0 TO Spacer
                  ? 'DATE   INV#  DESCRIPTION   TAXABLE      TAX      TAX FREE'
                  ?
               ENDIF
            ENDIF
            SKIP
         ENDDO
         ?
         ? '                             SUB-TOTALS : '+STR(T,9,2) + '  ';
            + STR(S,9,2)+'   ' + STR(F,9,2)
         ?
         ? '                                 TOTAL BILLED TO DATE: ' -;
                                                          STR(Sum,9,2)

         ? CHR(12)
         SET PRINT OFF

         ? '     F if FINISHED,'
         ? '  <Return> to continue.'
         WAIT TO Again
      ENDIF ok to do the report
   ENDIF 
ENDDO Again

IF Unindexed > 50
   ERASE
   @ 5,0
   ? '   There are ' - STR(Unindexed,9) + 'unindexed records'
   ? '   in the Costbase.  To speed up the Job Costs procedure,'
   ? '   please reindex from the next menu.'
   ? '<Return> to continue.'
   WAIT 
ENDIF

RELEASE All
RETURN
