Missing Time Agent

Author: Tripp W Black

Created: 11/19/2001 at 04:46 PM


Notes Developer Tips

Code is sample used for verifying timesheets for missing days or times entered by employees.

Option Public
Option Declare

' initialize agent dims
Dim DomDb As NotesDatabase
Dim AddressDb As NotesDatabase
Dim GroupView As NotesView
Dim GroupDoc As NotesDocument
Dim CurrentDoc As NotesDocument
Dim GroupItem As NotesItem
Dim DC As NotesDocumentCollection
Dim View As NotesView
Dim urlstring As String
Dim h As Integer
Dim Doc As NotesDocument
Dim Employee As String

' variables in both initialize and in DoReport function
Dim lastmonth As Integer
Dim lastmonthyear As Integer
Dim daysinmonth As Integer
Dim SendTo As String

Sub Initialize
'Send out reminder if no time tracking documents found
'Run 1st day of the month

'SH 11/2/01
'New Requirement: Allow this agent to be called from a form event as well, similar to the Monthly Client History Newsletter

Dim Session As New NotesSession
Dim DateTime As New NotesDateTime(Today)

' Setup url to view of timesheets
urlstring = "http://www.adteco.com/timekeeping.nsf/TimeCardsByDate/"

' Set up environment
Set DomDb = Session.CurrentDatabase
Set CurrentDoc = Session.DocumentContext

Set View = DomDB.GetView("TimeCardsByStaffByDate")

' Test to see if this was run on schedule/manual or from the kick-off form.
If CurrentDoc Is Nothing Then
' Use the last month as the data and send a report to all employees with timecards
Set View = DomDB.GetView("TimeCardsByStaffByDate")
' Setup date environment
' Get yesterday
Call DateTime.AdjustDay(-7)
' Get last month
lastmonth = Month(DateTime.DateOnly) ' 1 = January, 12= December
lastmonthyear = Year(DateTime.DateOnly) ' Year
' We have a document context so it was kicked off the form and we need the selections on the form
' Use the values off the kick-off doc to set the employee and the month
lastmonth = CurrentDoc.LastMonth(0) ' 1 = Jan, 12 = Dec
lastmonthyear = CurrentDoc.LastYear(0) ' Year
End If

' get the number of days for the month selected
Select Case lastmonth
Case 1, 3, 5, 7, 8, 10, 12
Case 2, 4, 6, 9, 11
Case 2
daysinmonth=28 ' NOTE: This code does not take into account leap year
End Select

If CurrentDoc Is Nothing Then
' Then we need to cycle through the address book

' get staff list from employees group in names.nsf
' cycle through each staff
' look at each day of the month
' if day is a work day, see if staff has a timesheet
' if staff doesn't have a timesheet or staff
Set AddressDb = Session.GetDatabase( "pluto/Audiorama", "names.nsf" )
Set GroupView = AddressDb.GetView("Groups")
Set GroupDoc = GroupView.GetDocumentByKey("Employees")
Set GroupItem = GroupDoc.GetFirstItem( "Members" )

' begin cycle through employees
Forall g In GroupItem.Values
SendTo = g
Call DoReport(SendTo, DomDb, daysinmonth, lastmonth, lastmonthyear )
End Forall
' We need to use the name on the current doc's form
SendTo = CurrentDoc.TIMEACT_EMP(0)
Call DoReport(SendTo, DomDb, daysinmonth, lastmonth, lastmonthyear )
End If

End Sub

Function DoReport(SendTo As String, DomDb As NotesDatabase, daysinmonth As Integer, lastmonth As Integer, lastmonthyear As Integer)

Dim MailDoc As NotesDocument
Dim Body As NotesRichTextItem
Dim i As Integer
Dim j As Integer
Dim aday As Integer
Dim datestring As String
Dim dateweekday As String
Dim SearchString(1) As String
Dim SearchPerson As String
Dim Hours As Integer

' setup mailer document for employee
i=0 ' This is a toggle. If a timecard is missing then i=1 and the url will be appended.
Set MailDoc = New NotesDocument(DomDb)
Set Body = New NotesRichTextItem ( MailDoc, "Body" )
MailDoc.Subject = "Time Tracking Status Report - Missing Time Notice"

' begin cycle through days
For aday = 1 To daysinmonth
datestring = Right$("0" & lastmonth, 2) & "/" & Right$("0" & aday, 2) & "/" & lastmonthyear

' check to see if datestring is a work day
dateweekday = Weekday ( datestring )
Select Case dateweekday
Case 1, 7
' do nothing it's a weekend day
Case Else
' perform check for timecards ...
' search the view for this date and employee and total hours for the day
SearchString(0) = datestring
If Instr(1, SendTo, "CN=") Then
SearchPerson = Strleft(SendTo, "/O")
SearchPerson = Strright(SearchPerson, "CN=")
SearchPerson = SendTo
End If

SearchString(1) = SearchPerson
Set DC = View.GetAllDocumentsByKey(SearchString, True)
Hours = 0
If ( DC.Count > 0 ) Then
' Loop through each of the documents & add the hours
For j = 1 To DC.Count
Set Doc = DC.GetNthDocument(j)
Hours = Hours + Doc.Q1(0)
Next j
If Hours < 8 Then
' flag this day as short time on timecard(s)
Call Body.AppendText ("You are missing " & Str(8-Hours) & " hours from " & datestring)
Call Body.AddNewLine(1)
For j = 1 To DC.Count
' add a url link for each document in collection where hours didn't add up
Set Doc = DC.GetNthDocument(j)
Call Body.AppendText ("Timecard URL: (" & Str(Doc.Q1(0)) & " hours)")
Print "You are missing " & Str(8-Hours) & " hours from " & datestring & "<BR>"
Call Body.AddNewLine(1)
Call Body.AppendText (urlstring & Doc.UniversalID)
Print "<a href=" & urlstring & Doc.UniversalID & ">View Doc</a><BR>"
Call Body.AddNewLine(1)
Next j
Call Body.AddNewLine(1)
End If
' flag this day as a missing timecard
Call Body.AppendText ("You are missing a timecard for " & datestring & " .")
Print "You are missing a timecard for " & datestring & " .<BR><BR>"
Call Body.AddNewLine(2)
End If
End Select
Next aday

' If i=1 then send the mailer document before we cycle to next employee
If i=1 Then
Call Body.AppendText ("URL to review timecards: " & urlstring)
Call MailDoc.Send( False, SendTo)
Print "Completed history compilation<BR>"
Print "<BR><B>Sent message successfully.</B><BR>"
Print "Check your inbox.<BR>"
Call Body.AppendText ("There is no missing time on your time card.")
Call MailDoc.Send( False, SendTo)
Print "Completed history compilation<BR>"
Print "<BR><B>NO missing time found.</B><BR>"
End If
End Function

previous page