          PH.ARGS DRPT,SD,ED,USR,ENT,ETYPES,SORTBY,INCL.COMP
** Version# 12.0002[8] - 05/08/2014 - 01:31pm - TSMITH - eclipse
*** V12.0002 Change - Custom Coding . - 05/08/2014 - TSMITH - eclipse
** Copied from BP EVENT.PHR.TRACKING Version# 12 - 01/30/2008 - 03:38pm - BABS - main

*** Program - EVENTS.PHR.TRACKING
*-------------------------------------------------------------------------*
*** This is the Phantom routine for the Events Tracking Report, which
*** displays all Events set up in the Schedular for a specified time
*** period and for specified entities. The report can be run for specific
*** Users, Entities (Customers, Vendors or Prospects), and Event Types. It
*** can also be Sorted by either User, Entity, Event Type or Start
*** Date/Time.
*-------------------------------------------------------------------------*
*** DRPT      - Standard report parameter.                         [IN]
*** SD        - Start Date to include data for                     (IN)
*** ED        - End Date to include data for                       [IN]
*** USR       - User(s) to report data for.                        (IN)
*** ENT       - Entities (Custmrs or Vendors) to report data for.  (IN)
*** ETYPES    - Event Types to report data for.                    (IN)
*** SORTBY    - Report Data to sort by and report totals for.      [IN]
***           -  Options Include: User, Entity, Event Type &
***           -                   Date/Time
*** INCL.COMP - Flag to 'Include, Exclude or Only' show Completed  [IN]
***          -  Events.
*-------------------------------------------------------------------------*
          GOSUB INIT

          UT.OPEN.FILE 'V2.SCHED.DET',SCHFILE,ERR.MSG,YES
          IF ERR.MSG THEN GOTO FINISH

          UT.OPEN.FILE 'CONTACT',CFILE,ERR.MSG,YES
          IF ERR.MSG THEN GOTO FINISH

          UT.TEMPFILE.CREATE FLNM.ID,TEMPFILE

          WRITE 'Selecting...' ON PHSTFILE,PID$
          GOSUB SEL.IDS

          WRITE 'Spooling...' ON PHSTFILE,PID$
          GOSUB PRT.RPT

          UT.TEMPFILE.DELETE FLNM.ID

FINISH:   *** Cleanup phantom and exit routine
          UT.PH.CLEANUP

          IF ITEM.CT   = 0 THEN
             COUNT.MSG = ' - No Items Found'
          END ELSE
             COUNT.MSG = ' - is Complete.'
          END

          SEND.MESSAGE 'PHANTOM',USER.ID,TITLE:COUNT.MSG

          STOP
*-------------------------------------------------------------------------*
INIT:     *** Initialize all necessary variables
          ITEM.CT        = 0     ; *Total # of items printed on report.
          LAST.EVNT.TYPE = '@@@' ; *Last EventType var used for totalling.
          LAST.SRTBY     = '@@@' ; *Last Sortby var used for totalling.

          SUBTOTAL.EVENTS = 0
          TOTAL.EVENTS    = 0
          GTOTAL.EVENTS   = 0

          * Set up parameters for printing in our Header
          SDT = OCONV(SD,'D4/')
          EDT = OCONV(ED,'D4/')

          * All events will be shown and selected upon based on time sone
          * of the user that ran the report
          GET.USER.TZ USER.ID,EVAL.TZ

          IF DCOUNT(USR,VM) > 1 THEN USR.SEL = '*Multi*' ELSE USR.SEL = USR
          IF DCOUNT(ENT,VM) > 1 THEN ENT.SEL = '*Multi*' ELSE ENT.SEL = ENT
          IF DCOUNT(ETYPES,VM) > 1 THEN
             ETYPES.SEL = '*Multi*'
          END ELSE
             ETYPES.SEL = ETYPES
          END

          * Set up parameters for our call to 'PRINTER.ON'
          HDG  = ''
          HDG  = 'Events Tracking Report for ':SDT:' to ':EDT

          IF USR OR ENT OR ETYPES THEN
             HDG<1,2> = 'Selected on'

             IF USR THEN
                HDG<1,2> := ' - User(s): ':USR.SEL
             END
             IF ENT THEN
                HDG<1,2> := ' - Entity(s): ':ENT.SEL
             END
             IF ETYPES THEN
                HDG<1,2> := ' - Event Type(s): ':ETYPES.SEL
             END
          END

          IF HDG<1,2> THEN HDG<1,2> := '  '
          HDG<1,2>  := 'Sorted by ':SORTBY

          HDG<1,2>  := '  ':INCL.COMP:' Completed Events'

          HDG<1,3>   = SPACE(12):'Start':SPACE(5):'Start':SPACE(4):'End'

          HDG<1,4>   = 'User Id...  Date....  Time...  Time...  '
          HDG<1,4>  := 'Completed  Event Id....  Event Type.....  '
          HDG<1,4>  := 'Entity Id  Entity Name.........................  '
          HDG<1,4>  := 'Contact Name........  Tracking #  '
          HDG<1,4>  := 'Comments........................... Notes'

          WDTH       = LEN(HDG<1,4>)
          HDG1.LNGTH = LEN(HDG<1,1>)

          HDG<1,1>  := SPACE(WDTH - HDG1.LNGTH - 11):'Page: ^####'

          TITLE      = 'Events Tracking Report by ':SORTBY

          RETURN
*-------------------------------------------------------------------------*
SEL.IDS:  *** Select all our data to report on
          USER.LIST = RAISE(USR)
          IF NOT(USER.LIST) THEN
             SELECT INIFILE
             READLIST USER.LIST ELSE USER.LIST = ''
          END

          USER.CT = DCOUNT(USER.LIST,AM)
          FOR YY = 1 TO USER.CT
             ST.IDS  = ''
             END.IDS = ''
             REC.IDS = ''
             USR.ID = USER.LIST<YY>
             SCHED.EVENTS.GET USR.ID, SD, ED, EVAL.TZ, ST.IDS, END.IDS, REC.IDS
             REC.CT = DCOUNT(REC.IDS,AM)
             FOR RR = 1 TO REC.CT
                ID = REC.IDS<RR>
                READ EVNT FROM SCHFILE,ID ELSE EVNT = ''
                GOSUB FILTER.REC
             NEXT RR
          NEXT YY
          RETURN
*-------------------------------------------------------------------------*
FILTER.REC:  *** apply filter criteria to the scheduled event.  If
             *** the criteria matches
          NEXT.ENTITY = ''
          ESORTBY     = ''

          * Filter against the Entity Select
          ENTS.USD = ''
          IF ENT THEN
             ENT.CNT = DCOUNT(EVNT<10,1>,SVM)
             FOR ENT.CTR = 1 TO ENT.CNT
                LOCATE EVNT<10,1,ENT.CTR> IN ENT<1> SETTING EPOS THEN
                   ENTS.USD<-1> = ENT<1,EPOS>
                END
             NEXT ENT.CTR
             IF NOT(ENTS.USD) THEN RETURN
          END ELSE
             ENTS.USD = RAISE(RAISE(EVNT<10,1>))
          END


          * Apply completed event filter
          BEGIN CASE
          CASE INCL.COMP = 'Exclude'
             IF EVNT<7> # '' THEN RETURN
          CASE INCL.COMP = 'Only'
             IF EVNT<7> = '' THEN RETURN
          END CASE

          * Filter against the Event Type Select
          ETYPE = EVNT<6>
          IF ETYPES THEN
             LOCATE ETYPE IN ETYPES<1> SETTING EVPOS ELSE RETURN
          END

          * Set up the Start Date/Time for our Sort Id
          GET.PICK.DATE.TIME ST.DT,ST.TM,ST.IDS<RR>
          DT.TM = ST.DT:'~':ST.TM

          IF SORTBY = 'Entity' AND ENTS.USD THEN
             ENT.CT = DCOUNT(ENTS.USD,AM)
             FOR LL = 1 TO ENT.CT
                NEXT.ENTITY = ENTS.USD<LL>
                READV ESORTBY FROM ENTFILE,NEXT.ENTITY,8 ELSE
                   ESORTBY = NEXT.ENTITY
                END
                GOSUB ADD.REC
             NEXT LL
          END ELSE
             GOSUB ADD.REC
          END
          RETURN
*-------------------------------------------------------------------------*
ADD.REC:  *** Build Temp Id(s) and store the record(s) in our Tempfile
          TEMP.ID  = ''

          BEGIN CASE
          CASE SORTBY = 'User'
             TEMP.ID  = USR.ID:'!':ETYPE:'!':DT.TM:'!'
          CASE SORTBY = 'Entity'
             TEMP.ID  = ESORTBY:'!':NEXT.ENTITY:'!':ETYPE:'!':DT.TM:'!'
          CASE SORTBY = 'Event Type'
             TEMP.ID  = ETYPE:'!':DT.TM:'!'
          CASE SORTBY = 'Date/Time'
             TEMP.ID  = DT.TM:'!':ETYPE:'!'
          END CASE

          TEMP.ID    := ID:'!':USR.ID

          WRITE EVNT ON TEMPFILE,TEMP.ID

          RETURN
*-------------------------------------------------------------------------*
PRT.RPT:  *** Loop through data and generate report
          WDTH = 300
          PRINTER.ON WDTH,TITLE,DOC.ID,HDG,RPT.DFLT=DRPT

          SSELECT TEMPFILE

          LOOP
             READNEXT TID ELSE EXIT
             READ EVNT FROM TEMPFILE,TID ELSE EVNT = ''

             GOSUB PRINT.REC
          REPEAT

          * We only want to print Totals if we've actually printed
          * event data on the report
          IF ITEM.CT # 0 THEN
             * Go print the last of the Event totals
             GOSUB PRINT.EVENT.TOTALS

             * Go print our Sortby Entity totals
             IF SORTBY # 'Event Type' THEN
                GOSUB PRINT.TOTALS
             END

             * Go print the Grand Totals for the report
             GOSUB PRINT.GTOTALS
          END

          * Turn our Printer off
          PRINTER.OFF DOC.ID
          RETURN
*-------------------------------------------------------------------------*
PRINT.REC: *** Coming in here with one record and printing record data

          * Break out our record data
          BEGIN CASE
          CASE SORTBY = 'User'
             SRTBY        = FIELD(TID,'!',1)
             EVNT.TYPE    = FIELD(TID,'!',2)
             START.DT     = FIELD(FIELD(TID,'!',3),'~',1)
             START.TM     = FIELD(FIELD(TID,'!',3),'~',2)
             REC.EVENT.ID = FIELD(TID,'!',4)
             USRID        = FIELD(TID,'!',5)
          CASE SORTBY = 'Entity'
             SRTBY        = FIELD(TID,'!',1)
             ENTTY        = FIELD(TID,'!',2)
             EVNT.TYPE    = FIELD(TID,'!',3)
             START.DT     = FIELD(FIELD(TID,'!',4),'~',1)
             START.TM     = FIELD(FIELD(TID,'!',4),'~',2)
             REC.EVENT.ID = FIELD(TID,'!',5)
             USRID        = FIELD(TID,'!',6)

             IF ENTTY THEN
                READV ENT.NAME FROM ENTFILE,ENTTY,1 ELSE ENT.NAME = ENTTY
             END ELSE
                ENT.NAME = "No Entity Attached."
             END
          CASE SORTBY = 'Event Type'
             SRTBY        = FIELD(TID,'!',1)
             EVNT.TYPE    = SRTBY
             START.DT     = FIELD(FIELD(TID,'!',2),'~',1)
             START.TM     = FIELD(FIELD(TID,'!',2),'~',2)
             REC.EVENT.ID = FIELD(TID,'!',3)
             USRID        = FIELD(TID,'!',4)
          CASE SORTBY = 'Date/Time'
             SRTBY        = FIELD(FIELD(TID,'!',1),'~',1)
             START.DT     = SRTBY
             START.TM     = FIELD(FIELD(TID,'!',1),'~',2)
             EVNT.TYPE    = FIELD(TID,'!',2)
             REC.EVENT.ID = FIELD(TID,'!',3)
             USRID        = FIELD(TID,'!',4)
          END CASE

          IF NOT(EVNT) THEN
             READ EVNT FROM SCHFILE,REC.EVENT.ID ELSE EVNT = ''
          END

          * Tack our Repeating Event flag onto our Event Id
          REC.EVENT.ID = "SH#":REC.EVENT.ID

          NEW.EVNT = (EVNT.TYPE#LAST.EVNT.TYPE) AND (LAST.EVNT.TYPE#'@@@')
          IF NEW.EVNT OR (SRTBY # LAST.SRTBY AND LAST.SRTBY # '@@@') THEN
             GOSUB PRINT.EVENT.TOTALS
          END

          * Print totals for the last Sortby entity if necessary
          IF SORTBY # 'Event Type' THEN
             IF SRTBY # LAST.SRTBY AND LAST.SRTBY # '@@@' THEN
                GOSUB PRINT.TOTALS
             END
          END

          * We'll use this for our totals layout later
          LINE.NUM = 1

          * Check whether this is a 'Private' event
          PRIVATE  = (EVNT<5> = 'Private')

          REAL.START.TM = START.TM
          REAL.START.DT = START.DT

          * Scheduled End Time
          IF EVNT<9> THEN
             * If this is a timeless date
             REAL.END.TM = ''
             REAL.END.DT = EVNT<9>
          END ELSE
             NEW.DATETIME = ''
             CONVERT.TZ EVNT<4>,'GMT',NEW.DATETIME,EVAL.TZ
             GET.PICK.DATE.TIME REAL.END.DT,REAL.END.TM,NEW.DATETIME
          END

          IF SORTBY # 'Entity' THEN
             * First Entity Info
             ENTTY = EVNT<10,1,1>
          END

          GOSUB GET.ENTITY.TYPE

          IF SORTBY # 'Entity' THEN
             READV ENT.NAME FROM ENTFILE,ENTTY,1 ELSE ENT.NAME = ENTTY
          END

          * First Contact Info
          CNTCT.ID = EVNT<10,2,1>
          READV CFIRST.NM FROM CFILE,CNTCT.ID,1 ELSE CFIRST.NM = ''
          READV CLAST.NM  FROM CFILE,CNTCT.ID,3 ELSE CLAST.NM  = ''
          CNTCT.NM = CFIRST.NM:' ':CLAST.NM

          * First Tracking No Info
          TRCK.NUM = EVNT<10,3,1>

          IF EVNT<7> THEN
             CMPLTED  = 'Yes'
          END ELSE
             CMPLTED  = 'No'
          END

          * Build the comments
          CMNT = ''

          * Build the Event Description
          EVENT.DESC = ''
          V2.SCHED.GET.DESC EVNT,USER.ID,EVENT.DESC
          IF EVENT.DESC THEN
             CONVERT SVM TO ' ' IN EVENT.DESC
             EVENT.DESC   = 'Event Description: ':EVENT.DESC

             FOLD.STRING EVENT.DESC,35,EV.DESC,DESC.VCT
             CMNT    = EV.DESC
          END ELSE
             EV.DESC = ''
          END

          * Build the Schedule Notes
          SCHED.NOTES = ''
          LOCATE USRID IN EVNT<10> SETTING UPOS THEN
             LOCATE 'P' IN EVNT<14,UPOS> SETTING NOTE.POS THEN
                SCHED.NOTES = EVNT<13,UPOS,NOTE.POS>
             END
          END
          IF SCHED.NOTES THEN
             IF PRIVATE AND USRID # USER.ID THEN
                SCHED.NOTES = '*** Private ***'
             END ELSE
                CONVERT SVM TO ' ' IN SCHED.NOTES
             END

             SNOTES = 'Schedule Notes: ':SCHED.NOTES

             FOLD.STRING SNOTES,35,SCH.NOTES,SCHED.VCT

             * If we had an Event Description, we'll need to
             * separate it and our Schedule Notes with a Value Mark
             * so that the Sched. Notes will start on another line
             IF EV.DESC THEN CMNT := VM

             CMNT := SCH.NOTES
          END ELSE
             SCH.NOTES = ''
          END

          IF CMNT THEN
             * Take out the first line of our Comments
             CMNT1 = CMNT<1,1>
             CMNT  = DELETE(CMNT,1,1)
          END ELSE
             CMNT1 = ''
          END
          NOTES = TRANS('V2.SCHED.DET',FIELD(REC.EVENT.ID,'#',2),13,'X')
          * Print the first line of our record
          PRINT USRID                                "L#10":'  ':
          PRINT OCONV(REAL.START.DT,'D2/')           "R#8" :'  ':

          IF EVNT<9> THEN
             PRINT '*** Timeless ***'                "L#16":'  ':
          END ELSE
             PRINT OCONV(REAL.START.TM,'MTH')        "R#7" :'  ':
             PRINT OCONV(REAL.END.TM,'MTH')          "R#7" :'  ':
          END

          PRINT CMPLTED                              "L#9" :'  ':
          PRINT REC.EVENT.ID                         "L#12":'  ':
          PRINT EVNT.TYPE                            "L#15":'  ':
          IF ENTTY # '' THEN
             PRINT '&':ENTTY                         "L#8" :'  ':
          END ELSE
             PRINT ''                                "L#9" :'  ':
          END
          PRINT ENT.NAME "L#25":' ':ENT.TYPE         "R#10":'  ':
          PRINT CNTCT.NM                             "L#20":'  ':
          PRINT TRCK.NUM                             "L#10":'  ':
          PRINT CMNT1                                "L#34":'  ':
          PRINT NOTES                                "L#34"

          * Additional Entities
          MORE.ENTS.CT = 0
          IF SORTBY # 'Entity' AND ENTTY THEN
             MORE.ENTS    = RAISE(EVNT<10,1>)
             LOCATE ENTTY IN MORE.ENTS<1> SETTING TPOS THEN
                MORE.ENTS = DELETE(MORE.ENTS,1,TPOS)
             END
             MORE.ENTS.CT = DCOUNT(MORE.ENTS,VM)
          END ELSE
             MORE.ENTS    = ''
          END

          * Additional Contacts
          MORE.CNTCTS   = EVNT<10,2>
          MORE.CNTCTS   = DELETE(MORE.CNTCTS,1,1,1)
          MORE.CNTCTS   = RAISE(MORE.CNTCTS)
          MORE.CNTCT.CT = DCOUNT(MORE.CNTCTS,VM)

          * Additional Tracking numbers
          MORE.TRKS    = EVNT<10,3>
          MORE.TRKS    = DELETE(MORE.TRKS,1,1,1)
          MORE.TRKS    = RAISE(MORE.TRKS)
          MORE.TRKS.CT = DCOUNT(MORE.TRKS,VM)

          * Additional Comment lines
          MORE.CMNT.CT = DCOUNT(CMNT,VM)

          * Find the greatest number of lines that we need to print
          LN.CT = MORE.ENTS.CT
          IF MORE.CNTCT.CT > LN.CT THEN LN.CT = MORE.CNTCT.CT
          IF MORE.CNTCT.CT > LN.CT THEN LN.CT = MORE.CNTCT.CT
          IF MORE.TRKS.CT  > LN.CT THEN LN.CT = MORE.TRKS.CT
          LINE.NUM += LN.CT

          * Print our additional lines of data
          FOR LN = 1 TO LN.CT
             PRINT SPACE(82):

             ENTTY = MORE.ENTS<1,LN>
             IF ENTTY # '' THEN
                PRINT '&':ENTTY                         "L#8" :'  ':
                READV ENAME FROM ENTFILE,MORE.ENTS<1,LN>,1 ELSE ENAME = ''

                GOSUB GET.ENTITY.TYPE

                PRINT ENAME "L#25":' ':ENT.TYPE         "R#10":'  ':
             END ELSE
                PRINT SPACE(49):
             END

             CNTCT = MORE.CNTCTS<1,LN>
             READV CFIRST.NM FROM CFILE,CNTCT,1 ELSE CFIRST.NM = ''
             READV CLAST.NM  FROM CFILE,CNTCT,3 ELSE CLAST.NM  = ''
             CNTCT.NM = CFIRST.NM:' ':CLAST.NM

             PRINT CNTCT.NM                             "L#20":'  ':

             TRK = MORE.TRKS<1,LN>
             IF TRK THEN
               PRINT TRK                                "L#10":'  ':
             END ELSE
               PRINT SPACE(12):
             END

             PRINT CMNT<1,LN>                           "L#35"
          NEXT LN

          SUBTOTAL.EVENTS += 1
          TOTAL.EVENTS    += 1
          GTOTAL.EVENTS   += 1

          ITEM.CT         += 1

          LAST.EVNT.TYPE   = EVNT.TYPE
          LAST.SRTBY       = SRTBY
          LAST.ENT.NAME    = ENT.NAME

          RETURN
*-------------------------------------------------------------------------*
GET.ENTITY.TYPE: *** Set up the Entity Type for printing
          READV ENTITY.TYPE FROM ENTFILE,ENTTY,7 ELSE ENTITY.TYPE = ''

          BEGIN CASE
          CASE ENTITY.TYPE<1,1> AND ENTITY.TYPE<1,2>
             ENT.TYPE = '(BT/ST)'
          CASE ENTITY.TYPE<1,1>
             ENT.TYPE = '(BillTo)'
          CASE ENTITY.TYPE<1,2>
             ENT.TYPE = '(ShipTo)'
          CASE ENTITY.TYPE<1,5> AND ENTITY.TYPE<1,6>
             ENT.TYPE = '(PT/SF)'
          CASE ENTITY.TYPE<1,5>
             ENT.TYPE = '(PayTo)'
          CASE ENTITY.TYPE<1,6>
             ENT.TYPE = '(ShipFrom)'
          CASE ENTITY.TYPE<1,9>
             ENT.TYPE = '(Prospect)'
          CASE OTHERWISE
             ENT.TYPE = ''
          END CASE

          RETURN
*-------------------------------------------------------------------------*
PRINT.EVENT.TOTALS: *** Print Subtotals by Event
          IF LAST.EVNT.TYPE = '' THEN
             ST.MSG = 'Total Events with no Type '
          END ELSE
             ST.MSG = 'Total ':LAST.EVNT.TYPE:' Events '
          END

          BEGIN CASE
          CASE SORTBY = 'User'
             ST.MSG  := 'for ':SORTBY:' ':LAST.SRTBY:' : '
          CASE SORTBY = 'Entity'
             ST.MSG  := 'for ':SORTBY:' ':LAST.ENT.NAME:' : '
          CASE SORTBY = 'Event Type'
             ST.MSG  := ' : '
          CASE SORTBY = 'Date/Time'
             ST.MSG  := 'on ':OCONV(LAST.SRTBY,'D2/'):' : '
          END CASE

          ST.MSG := SUBTOTAL.EVENTS

          * Just to make the report look nicer, we'll only print
          * a blank line, if the previous event took up ONLY one line
          IF LINE.NUM = 1 THEN PRINT

          PRINT '   **** ':ST.MSG:' ****'
          PRINT

          SUBTOTAL.EVENTS = 0

          RETURN
*-------------------------------------------------------------------------*
PRINT.TOTALS: *** Print totals for the Sortby Entity
          TMSG = 'Total Events '

          BEGIN CASE
          CASE SORTBY = 'User'
             TMSG := 'for ':SORTBY:' ':LAST.SRTBY:' : '
          CASE SORTBY = 'Entity'
             TMSG := 'for ':SORTBY:' ':LAST.ENT.NAME:' : '
          CASE SORTBY = 'Event Type'
             TMSG := ' : '
          CASE SORTBY = 'Date/Time'
             TMSG := 'on ':OCONV(LAST.SRTBY,'D2/'):' : '
          END CASE

          TMSG      := TOTAL.EVENTS

          TMSG.LNGTH = LEN(TMSG)

          PRINT '   ':STR('-',TMSG.LNGTH)
          PRINT '   ':TMSG
          PRINT '   ':STR('-',TMSG.LNGTH)
          PRINT

          TOTAL.EVENTS = 0

          RETURN
*-------------------------------------------------------------------------*
PRINT.GTOTALS: *** Print Grand Totals
          GT.MSG       = 'GRAND TOTAL of Events : ':GTOTAL.EVENTS
          GT.MSG.LNGTH = LEN(GT.MSG)

          PRINT '   ':STR('=',GT.MSG.LNGTH)
          PRINT '   ':GT.MSG
          PRINT '   ':STR('=',GT.MSG.LNGTH)

          RETURN
*-------------------------------------------------------------------------*
!TSMITH~05/08/14~13:31
