<!-- #INCLUDE FILE="../library/rtflib_20050408.asp" -->
<!-- #INCLUDE FILE="../library/functions.asp" -->

<%

'==============================================================================================
'START FUNCTION AREA
'==============================================================================================

sub Append(strContent)
	'THIS FUNCTION SIMPLIFIES APPENDING CONTENT TO THE MAIN RTF_CONTENT STRING
	RTF_OUTPUT = RTF_OUTPUT & strContent
end sub

Function GroupOneHeader(sText)
	sText = DrawChars(sText,RTF_FONT_ARIAL, "18", RTF_BOLD, RTF_BLUE)	
	Call RegisterTableCell(sText, TableWidth, "l", "c", RTF_LIGHT_GRAY, 0, NULL, NULL, NULL, NULL,null)
	GroupOneHeader = DrawTableRow(false, false, NULL, NULL, NULL, NULL)
end function

Function GroupTwoHeader(sText)
	
	dim RowWidth
	RowWidth = TableWidth-(Indent*1)

	sText = DrawChars(sText,RTF_FONT_ARIAL, "14", RTF_ITALIC, RTF_DARK_GRAY)	
	Call RegisterTableCell("", Indent, "l", "c", NULL, NULL, NULL, NULL, NULL, NULL,NULL)
	Call RegisterTableCell(sText,RowWidth, "l", "c", NULL, NULL, NULL, NULL, NULL, NULL,NULL)
	GroupTwoHeader = DrawTableRow(false, false, NULL, NULL, NULL, NULL)

end function

Function GroupThreeHeader(Header1,Header2,Header3,Header4,Header5)

	dim RowWidth
	RowWidth = TableWidth-(Indent*2)

	Header1 = DrawChars(Header1,RTF_FONT_ARIAL, "12", RTF_BOLD, RTF_BLACK)	
	Header2 = DrawChars(Header2,RTF_FONT_ARIAL, "12", RTF_BOLD, RTF_BLACK)	
	Header3 = DrawChars(Header3,RTF_FONT_ARIAL, "12", RTF_BOLD, RTF_BLACK)	
	Header4 = DrawChars(Header4,RTF_FONT_ARIAL, "12", RTF_BOLD, RTF_BLACK)	
	Header5 = DrawChars(Header5,RTF_FONT_ARIAL, "12", RTF_BOLD, RTF_BLACK)	
	
	Call RegisterTableCell("", Indent, "l", "c",0,0,false,false,false,false,null)
	Call RegisterTableCell("", Indent, "l", "c",0,0,false,false,false,false,null)

	Call RegisterTableCell(Header1,RowWidth * 0.15, "l", "t", 0,0,false,false,true,true,null)
	Call RegisterTableCell(Header2,RowWidth * 0.15, "l", "t", 0,0,false,false,true,true,null)
	Call RegisterTableCell(Header3,RowWidth * 0.15, "l", "t", 0,0,false,false,true,true,null)
	Call RegisterTableCell(Header4,RowWidth * 0.15, "l", "t", 0,0,false,false,true,true,null)
	Call RegisterTableCell(Header5,RowWidth * 0.40, "l", "t", 0,0,false,false,true,true,null)
			
	GroupThreeHeader = DrawTableRow(false, true, NULL, NULL, NULL, NULL)
end function

Function GroupThreeDetails(Value1,Value2,Value3,Value4,Value5)

	dim RowWidth
	RowWidth = TableWidth-(Indent*2)
	
	Call RegisterTableCell("", Indent, "l", "t",0,0,false,false,false,false,null)
	Call RegisterTableCell("", Indent, "l", "t",0,0,false,false,false,false,null)

	Call RegisterTableCell(Value1,RowWidth * 0.15, "l", "t", 0,0,false,false,false,true,null)
	Call RegisterTableCell(Value2,RowWidth * 0.15, "l", "t", 0,0,false,false,false,true,null)
	Call RegisterTableCell(Value3,RowWidth * 0.15, "l", "t", 0,0,false,false,false,true,null)
	Call RegisterTableCell(Value4,RowWidth * 0.15, "l", "t", 0,0,false,false,false,true,null)
	Call RegisterTableCell(Value5,RowWidth * 0.40, "l", "t", 0,0,false,false,false,true,null)
			
	GroupThreeDetails = DrawTableRow(false, true, NULL, NULL, NULL, NULL)
end function

function MaintenanceHistory(AssetID)

	dim RTF_LOCAL, strSQL, rsHistory

	strSQL = "SELECT Maintenance.MaintenanceDate, Maintenance.MaintenanceDescription, Maintenance.MaintenancePerformedBy, Maintenance.MaintenanceCost " & _
		" FROM Maintenance " & _
		" WHERE AssetID = " & AssetID & " " & _
		" ORDER BY	Maintenance.MaintenanceDate DESC	"

	set rsHistory = OpenRS(strSQL,strConn,null,null,null,null)
	
	'call dump_rs(rsHistory,true)

	do while NOT rsHistory.EOF
		RTF_LOCAL = RTF_LOCAL & rsHistory("MaintenanceDate")
		RTF_LOCAL = RTF_LOCAL & " - "
		RTF_LOCAL = RTF_LOCAL & rsHistory("MaintenanceDescription") & RTF_LINE_BREAK
		RTF_LOCAL = RTF_LOCAL & "By: " & rsHistory("MaintenancePerformedBy") & RTF_LINE_BREAK
		RTF_LOCAL = RTF_LOCAL & "Cost: $" & rsHistory("MaintenanceCost") & RTF_LINE_BREAK
		rsHistory.movenext
	loop
	
	MaintenanceHistory = RTF_LOCAL

end function

function MainReport()

	dim MaintenanceHistoryContents

	strSQL = "SELECT * FROM vwSampleReport ORDER BY DepartmentName, AssetCategory, AssetDescription "
	set rsReportData = OpenRS(strSQL,strConn,null,null,null,null)
	
	rsReportData.movefirst
	
	do while NOT rsReportData.eof
	
		if group_DepartmentName <> rsReportData("DepartmentName") then Call Append(GroupOneHeader(rsReportData("DepartmentName")))
		if group_AssetCategory <> rsReportData("AssetCategory") then 
			Call Append(GroupTwoHeader(rsReportData("AssetCategory")))
			Call Append(GroupThreeHeader("Asset","Status","Employee","Vendor","Maintenance History"))
		end if

		MaintenanceHistoryContents = MaintenanceHistory(rsReportData("AssetID"))

		Call Append(GroupThreeDetails(rsReportData("AssetDescription"),rsReportData("Status"),rsReportData("EmpFullName"),rsReportData("VendorName"),MaintenanceHistoryContents))
		
		group_DepartmentName = rsReportData("DepartmentName")
		group_AssetCategory = rsReportData("AssetCategory")
	
		rsReportData.movenext
	loop

end function

'==============================================================================================
'END FUNCTIONS
'==============================================================================================


'==============================================================================================
'START MAIN REPORT CODE
'==============================================================================================

dim RTF_OUTPUT, strSQL, strConn, strDBPath, rsReportData, group_DepartmentName, group_AssetCategory, Today, PageXofY, TableWidth, Indent

strDBPath = Server.MapPath("rtf_sample.mdb")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";Persist Security Info=False"

RTF_OUTPUT = ""

SetVerticalMeasure("inches")
SetHorizontalMeasure("inches")

'==================================================
Append(DrawRTFHeader("Doc Title","Document Subject","Doc Author","Company","Document Comments",RTF_FONT_ARIAL,11,RTF_LANDSCAPE,11,8.5,"1","1","0.35","0.35",RTF_PROTECTED))
'==================================================

call RegisterFont(RTF_FONT_ARIAL, "9", 0, RTF_DARK_GRAY)

Today = WeekdayName(Weekday(Now())) & " " & monthname(month(now())) & " " & day(now()) & ", " & year(now())
Today = DrawChars(Today, null, null, null, null)
PageXofY = DrawChars("Page " & RTF_PAGE_NUMBER & " of " & RTF_PAGE_TOTAL, null, null, null, null)

Append(DrawPageHeader_formatted(Today,DrawChars("Assets By Department", null, null, null, null), PageXofY))
Append(DrawPageFooter_formatted(DrawChars("Assets By Department", null, null, null, null),"",PageXofY))

call RegisterFont(RTF_FONT_ARIAL, "24", RTF_BOLD, RTF_BLUE)
Append(DrawParagraph("SAMPLE REPORT - ASSETS BY DEPARTMENT" & RTF_LINE_BREAK))

TableWidth = 9
Indent = 0.5

MainReport()

'==================================================
Call Append(DrawRTFFooter())
'==================================================

Response.ContentType="application/rtf"
Response.AddHeader "content-disposition", "filename=sample.rtf"
Response.Write RTF_OUTPUT

%>