Dim strConn,Conn,RecObj,strSQL,currentStatus,strStatus,arrSubject(1),blnApproved
Dim intCount,strDate : intCount = 0
Const Appending=8
strConn = "Provider=SQLOLEDB;Data Source=sql2014;Initial Catalog=SheddPark;User Id=sports;password=letmein@cliff64;"
strSQL = "EXECUTE dbo.USP_ROSTER_NOTICES"
Set Conn = CreateObject("adodb.Connection")
With Conn
.ConnectionString  = strConn 
.Open 
End With
Set RecObj = CreateObject("adodb.recordset")
With RecObj
.Open strSQL,Conn
Do While .EOF = False
If .Fields("PlayerCount") <= .Fields("RosterCountP") And .Fields("CoachCount") <= .Fields("RosterCountC") Then
	currentStatus = 1
Else
	currentStatus = 2
End If 
If .Fields("RosterStatus") <> currentStatus Or IsNull(.Fields("NoticeDate")) = True Then
If currentStatus = 1 Then 'Team Approved
blnApproved = "T"
strDate = .Fields("LastAppUpdate")
arrSubject(0) = "Congrats!: "&.Fields("SMOrg") & "-" & .Fields("TeamName") & " has been approved by "&.Fields("NameAbbr")&" for "&.Fields("Season")
arrSubject(1) = .Fields("SMOrg")&"-"&.Fields("Season")&"-"&.Fields("League")&"-"&.Fields("TeamName")&" is approved by "&.Fields("NameAbbr")
Call SendEmail(.Fields("TeamID"),blnApproved,.Fields("FormAbbr"),.Fields("SMOrgID"),.Fields("Approved"),.Fields("Disapproved"))
Else
blnApproved = "F"
arrSubject(0) = "Note: "&.Fields("SMOrg") & "-" & .Fields("TeamName") & " is no longer approved by "&.Fields("NameAbbr")&" for "&.Fields("Season")
arrSubject(1) = .Fields("SMOrg")&"-"&.Fields("Season")&"-"&.Fields("League")&"-"&.Fields("TeamName")&" is no longer approved by "& .Fields("NameAbbr")
Call SendEmail(.Fields("TeamID"),blnApproved,.Fields("FormAbbr"),.Fields("SMOrgID"),.Fields("Approved"),.Fields("Disapproved"))
End If
strSQL= "UPDATE TeamNames SET RosterStatus="&currentStatus&",NoticeDate=GETDATE() WHERE TeamID="&.Fields("TeamID")
Conn.Execute(strSQL)
Else
strSQL= "UPDATE TeamNames SET RosterStatus="&currentStatus&" WHERE TeamID="&.Fields("TeamID")
Conn.Execute(strSQL)
End If 
.MoveNext
Loop
End With
RecObj.Close() : Set RecObj = Nothing : Conn.Close() : Set Conn = Nothing

Set fs=CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile("C:\ScheduleTask\rosterLog.txt", Appending) 
f.Write VBCRLF&intCount&" Emails Sent Sucessfully At "&Now&VBCRLF&"------------------"
f.close()
Set fs=Nothing


Sub SendEmail(ByVal Team,ByVal bln,ByVal FormAbbr,ByVal OrgID,ByVal blnNoticeApproved,ByVal blnNoticeDisApproved)
Dim objMail,strSubject,RecMail,strCoachPlayer,strBody
If bln="F" Then  
strCoachPlayer = PlayerCaoch(Team)
Else
strAttachment=PdfHtml(Team,FormAbbr)
End If
Set RecMail =CreateObject("adodb.recordset")
With RecMail
.Open "EXECUTE USP_COACH_APPROVER "&Team & "," & OrgID,Conn
Do While .EOF =False
If .Fields("RosterNotice") = 3 Then '***If Approver has both approved and disapproved
intCount= intCount + 1
Set objMail=CreateObject("CDO.Message")
With objMail
.Subject=arrSubject(RecMail.Fields(2))
.From="SportsManager<donotreply@sportsmanager.us>"
.To=RecMail.Fields(1)
.BCC = "Copy<copy@sportsmanager.us>"
If bln = "T" Then
	objMail.AddAttachment strAttachment
End If
.HTMLBody = strCoachPlayer
.Send
End With
Set objMail=Nothing
ElseIf .Fields("Type") = 1 And ((bln="T" And .Fields("RosterNotice")=1) Or (bln="F" And .Fields("RosterNotice")=2)) Then '**IF APPROVER HAS EITHER APPROVED OR DISAPPROVED
intCount= intCount + 1
Set objMail=CreateObject("CDO.Message")
With objMail
.Subject=arrSubject(RecMail.Fields(2))
.From="SportsManager<donotreply@sportsmanager.us>"
.To=RecMail.Fields(1)
.BCC = "Copy<copy@sportsmanager.us>"
If bln = "T" Then
	objMail.AddAttachment strAttachment
End If
.HTMLBody = strCoachPlayer
.Send
End With
Set objMail=Nothing
ElseIf ((blnNoticeApproved = True And bln="T") Or (blnNoticeDisApproved = True And bln="F")) And .Fields("Type") <> 1 Then '***CHECK APPROVED AND DISAPPROVED FOR COACHES ORG
intCount= intCount + 1
Set objMail=CreateObject("CDO.Message")
With objMail
.Subject=arrSubject(RecMail.Fields(2))
.From="SportsManager<donotreply@sportsmanager.us>"
.To=RecMail.Fields(1)
.BCC = "Copy<copy@sportsmanager.us>"
If bln = "T" Then
	objMail.AddAttachment strAttachment
End If
.HTMLBody = strCoachPlayer
.Send
End With
Set objMail=Nothing
End If 
.MoveNext
Loop
.Close()
End With
Set RecMail = Nothing
If bln = "T" Then
Set fs=CreateObject("Scripting.FileSystemObject")
If fs.FileExists(strAttachment) Then   
  fs.DeleteFile(strAttachment)
End If 
Set fs=Nothing
End If
End Sub

Function PlayerCaoch(ByVal Team)
Dim RecList,SqlQuery,strTable
SqlQuery = "EXECUTE dbo.USP_ROSTER_PLAYER_COACH "&Team
Set RecList = CreateObject("adodb.recordset")
With RecList
.Open SqlQuery,Conn
If .EOF = False Then
strTable = "<table cellpadding=""5"">"
Do While .EOF = False
strTable = strTable &"<tr><td>"&.Fields("Type")&"</td><td>"&.Fields("First")&" "&.Fields("Last")&"</td><td>"&.Fields("Status")&"</td></tr>"
.MoveNext
Loop
strTable = strTable & "</table>"
End If 
.Close()
End With
Set RecList = Nothing
PlayerCaoch = strTable
End Function


Function PdfHtml(ByVal intTeam,ByVal Mode)
Dim RecTeam,strTable,strFileName 
Dim SqlQuery
If Mode="MAYSL" Then
	strHeader="MAYSL SOCCER APPROVED ROSTER"
	strOMORG="MAYSL"
ElseIf Mode="MD" Then
	strOMORG="MYSL"
	strHeader="MIDDLESEX SOCCER APPROVED ROSTER"
ElseIf Mode="NVYSL" Then
	strOMORG="NVYSL"
	strHeader="NVYSL SOCCER APPROVED ROSTER"
End If 
Set RecTeam = CreateObject("adodb.recordset")
SqlQuery = "EXECUTE dbo.USP_ROSTER_TEAM_INFO "&intTeam
With RecTeam
.Open SqlQuery,Conn
If .EOF = False Then
		strTeam = .Fields("TeamName")
		strOrg = .Fields("OrgName")
		strLeague =.Fields("League")
		If .Fields("TeamNumber") <> "" Then 
			strTeamID= .Fields("TeamNumber")
		Else
			strTeamID= .Fields("TeamID")
		End If 
		strDiv = .Fields("Div")
		strColor = .Fields("TeamColor")
		If .Fields("LeagueGenders") = 1 Then
			strBoys = "X"
			strGirls = "X"
			strBG = "C"
		ElseIf .Fields("LeagueGenders") = 2 Then
			strBoys = "X"
			strGirls = ""
			strBG = "B"
		Else
			strBoys = ""
			strGirls = "X"
			strBG = "G"
		End If
		strAgeGroup = .Fields("MaxAge") + 1
		If .Fields("TeamNumber") <> "" Then
			strTeamNumber = Replace(.Fields("TeamNumber"),"/","_")
			strTeamNumber = Replace(strTeamNumber,"\","_")
		Else
			strTeamNumber = Replace(.Fields("TeamName"),"/","_")
			strTeamNumber = Replace(strTeamNumber,"\","_")
		End If
		strPosition = .Fields("Pos")
		
End If 
.Close()
End With
Set RecTeam = Nothing
If Month(Date) < 10 Then
strMonth = "0" & Month(Date)
Else
strMonth = Month(Date)
End If
If Day(Date) < 10 Then
strDay = "0" & Day(Date)
Else
strDay = Day(Date)
End If
strFileName = strBG & strAgeGroup & "-" & strDiv & "-" & strTeamNumber & "-" & strMonth & strDay&"-"&intTeam&".pdf"
strFileName = Replace(strFileName,":","")

strTable = "<html><body><table cellpadding=""0"" width=""100%"" ><tr><td align=""center""><b>"&strHeader&"</b></td></tr>"
strTable = strTable & "<tr><td><table cellpadding=""0"" cellspacing=""0"" border=1 width=""100%"" style=""font-size:0.8em;font-weight:bold;""><tbody><tr><td colspan=""2"" width=""50%"">Town/Club: "&strOrg&"</td><td width=""17%"">Date: "& strDate & "</td><td width=""13%"">Original: </td><td width=""20%"">Change: </td></tr><tr><td width=""33%"">Team Name: "&strTeam&"</td><td width=""17%"" nowrap>Age: "&strLeague&"</td><td width=""17%"">Girls: "&strGirls&"</td><td width=""13%"" nowrap>Boys: "&strBoys&"</td><td width=""20%"">Shirt Color: "&strColor&"</td></tr><tr><td colspan=""2"">League:</td><td nowrap>Team ID#: "&strTeamID&"</td><td colspan=""2"">Div/Section: "&strDiv&"</td></tr></tbody></table></td></tr>"
strTable = strTable & "<tr><td align=""center""><table cellpadding=""0"" cellspacing=""0"" width=""100%"" border=1 style=""font-size:0.8em;font-weight:bold;""><thead><tr><td width=""5%"">&nbsp;</td><td width=""35%"" align=""center"">Coach</td><td width=""20%"" align=""center"">Phone</td><td width=""40%"" align=""center"">Email</td></tr></thead><tbody>"
intNum = intNum+1
Set RecCoach = CreateObject("adodb.recordset")
With RecCoach
.Open "EXECUTE dbo.USP_TEAM_COACHES "&intTeam, Conn
Do While .EOF = False 
strTable = strTable & "<tr><td >"&intNum&"</td><td>"&.Fields(0)&"</td><td>"&.Fields(2)&"</td><td>"&.Fields(1)&"</td></tr>"
intNum = intNum+1
.MoveNext
Loop
.Close()
End With
Set RecCoach = Nothing
Do While intNum <=3 
strTable = strTable & "<tr><td>"&intNum&"</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>"
intNum = intNum+1
Loop

intNum = 0
strTable = strTable & "</tbody></table></td></tr>"
strTable = strTable & "<tr><td align=""center""><table cellpadding=""0"" cellspacing=""0"" width=""100%"" border=1 style=""font-size:0.8em;font-weight:bold;"" ><thead><tr><td width=""5%"">&nbsp;</td><td width=""10%""><b>Shirt#</b></td><td width=""15%"">Last Name</td><td width=""15%"">First Name</td><td width=""10%"">DOB</td><td width=""15%"">Town</td><td width=""15%"">Assigned</td></tr></thead><tbody>"
Set RecPlayer = CreateObject("adodb.recordset")
With RecPlayer
.Open "EXECUTE dbo.USP_TEAM_PLAYERS "&intTeam, Conn
Do While .EOF = False 
intNum = intNum+1
strTable = strTable & "<tr><td width=""30px"">"&intNum&"</td><td width=""80px"">"&.Fields(0)&"&nbsp;</td><td>"&.Fields(1)&"</td><td>"&.Fields(2)&"</td><td>"&.Fields(3)&"</td><td>"&.Fields(4)&"</td><td>"&.Fields(5)&"</td></tr>"
.MoveNext
Loop
.Close()
End With
Set RecPlayer = Nothing
strTable = strTable & "</tbody></table></td></tr>"
strTable = strTable & "<tr><td align=""left""><strong>Transfers:</td></tr><tr><td><table cellpadding=""3"" width=""100%"" cellspacing=""0"" border=1 style=""font-size:12px;font-weight:bold;"">"
For I=1 To 3
strTable = strTable & "<tr><td width=""10%"">"&I&"</td><td width=""10%"">&nbsp;</td><td width=""10%"">&nbsp;</td><td width=""10%"">&nbsp;</td><td width=""10%"">&nbsp;</td><td width=""10%"">&nbsp;</td><td width=""10%"">&nbsp;</td></tr>"
Next
strTable = strTable & "</table></td></tr></table>"
strTable = strTable & "<tr><td><table cellpadding=""3"" width=""100%"" cellspacing=""0"" style=""font-size:12px;font-weight:bold;"" >"
If strOMORG <> "NVYSL" Then 
strTable = strTable & "<tr><td nowrap width=""40%"">"&strOMOrg&" Registrar Verification</td><td> ______________________________________________</td><td>Date</td><td> ___________________</td></tr>"
strTable = strTable & "<tr><td nowrap width=""40%"">"&strOMOrg&" Boys/Com Verification</td><td> ______________________________________________</td><td>Date</td><td> ___________________</td></tr>"
strTable = strTable & "<tr><td nowrap width=""40%"">"&strOMOrg&" Girls/Com Verification</td><td> ______________________________________________</td><td>Date</td><td>___________________</td></tr>"
Else
strTable = strTable & ApproverSignature(intTeam)
End If
strTable = strTable & "</table></td></tr><tr><td><table BORDER=1 CELLPADDING=""1"" width=""100%"" style=""font-size:0.8em;font-weight:bold""><tr><td width=""50%"" align=""center""><b>"&strPosition&"'s Signature</b></td><td align=""center""><b>Club/Town Certification</b></td></tr><tr><td><SPAN style=""font-size:0.8em;font-weight:bold"">I Certify that I will comply with Soccer and bylaws, playing rules, & " & strPosition & "'s Code of Conduct, and know the penalties for non-compliance.<BR><BR>" & strPosition & "'s Signature ______________________</SPAN></td><td><SPAN style='font-size:0.8em;'>All players and all coaches/managers meet all Soccer requirements for affilation and playing age, and the town/club is properly affiliated.<BR><BR>Signature _______________________________</SPAN></td></tr></table></td></tr>"
strTable = strTable & "<tr><td>&nbsp;</td></tr><tr><td><table cellpadding=""1"" width=""100%"" BORDER=1 CELLSPACING=""0"" style=""font-size:0.8em;font-weight:bold""><tr><td>Referee:</td><td>Sched. Date:</td><td>Actual Date:</td></tr><tr><td>Opposing Team:</td><td>Winner:</td><td>Score:</td></tr></table></td></tr>"
strTable = strTable & "</table></body></html>"
strFileName="C:\ScheduleTask\Attachment\"&strFileName
Set Pdf = CreateObject("Persits.Pdf")
Set doc = Pdf.CreateDocument
Set Page = Doc.Pages.Add
Set Image = Doc.OpenImage("C:\ScheduleTask\Approved.jpg")
Set Param = Pdf.CreateParam
Param("x") = 100
Param("y") = 200
Param("ScaleX") =2
Param("ScaleY") =2
Page.Background.DrawImage Image, Param
Doc.ImportFromUrl strTable
Doc.Save strFileName,True
Set Pdf = Nothing
PdfHtml = strFileName
End Function



Function ApproverSignature(ByVal TeamID)
Dim strSignature,strSQL
Set ObjFS=CreateObject("Scripting.FileSystemObject")
strSQL ="EXECUTE dbo.USP_ROSTER_FORM_APPROVER_SIGNATURE " & TeamID & ",1"

Set RecSignature = CreateObject("adodb.recordset")
With RecSignature
.Open strSQL,Conn
If .EOF = False Then
strSignature  = "<tr><td colspan=""4"">"
Do While .EOF = False
strLogo = "C:\inetpub\sportsmanager\"&Replace(.Fields(0),"/","\")
If ObjFS.FileExists(strLogo) then
	strSignature =  strSignature & "<img src="""&strLogo&""" border=""0""/>&nbsp;&nbsp;&nbsp;&nbsp;"
End If
.MoveNext
Loop
strSignature = strSignature & "</td></tr>"
End If 
End With 
Set RecSignature = Nothing : Set ObjFS = Nothing
ApproverSignature = strSignature
End Function