<%
'=======================================================================================================================
' Kit Model
'=======================================================================================================================

Class KitModel_Class
	Public Validator
	Public Class_Get_Properties

	Public ID '90
	Public JobNumber '106
	Public Jcode '106
	Public CreatedOn '122
	Public LabelsPrinted '122
	Public ExportedToSnailWorks '122
	Public InkJetJob '90
	Public JobType '106
	Public Filename '106
	Public Cass '90
	Public Status '106
	Public OutboundSTID '106
	Public InboundSTID '106
	Public OfficeCopiesAmount

	Private Sub Class_Initialize
		'ValidateExitsts Me, "",""
		Class_Get_Properties = Array("ID, JobNumber, Jcode, CreatedOn, LabelsPrinted, ExportedToSnailWorks, InkJetJob, JobType, Filename, Cass, Status, OutboundSTID, InboundSTID, OfficeCopiesAmount")
	End Sub

End CLass

Class IndexKitModel_Class
	Public Validator
	Public Class_Get_Properties

	Public ID
	Public JobNumber '90
	Public Jcode
	Public Jurisdiction '106
	Public LabelCount '106
	Public CreatedOn
	Public LabelsPrinted
	Public ExportedToSnailWorks
	Public InkJetJob
	Public JobType
	Public Filename
	Public Cass
	Public Status
	Public OutboundSTID
	Public InboundSTID
	Public OfficeCopiesAmount

	Private Sub Class_Initialize
		'ValidateExitsts Me, "",""
		Class_Get_Properties = Array("ID, JobNumber,Jcode, Jurisdiction,LabelCount,CreatedOn,LabelsPrinted,ExportedToSnailWorks,InkJetJob,JobType,Filename,Cass,Status,OutboundSTID,InboundSTID,OfficeCopiesAmount")
	End Sub
End Class


'=======================================================================================================================
' Kit Repository
'=======================================================================================================================

Class KitRepository_Class

	Public Function PagedPurpleEnvelopsIndexView(per_page, page_num, ByRef page_count, ByRef record_count)
			dim sql : sql = "SELECT Kit.ID,Kit.JobNumber as [JobNumber],Kit.JCode, " &_
		"Jurisdiction.Name As Jurisdiction," &_
		"(SELECT COUNT(*) From InkjetRecords Where KitId = Kit.ID)" &_
		" As [LabelCount],[CreatedOn],[LabelsPrinted],[ExportedToSnailWorks],[InkJetJob],[JobType],[Filename],[Cass],[Status],[OutboundSTID],[InboundSTID],[OfficeCopiesAmount] FROM Kit INNER JOIN Jurisdiction ON Kit.Jcode = Jurisdiction.JCode Where Kit.JobType = 'Purple Envelopes' Order By [ID];"

		dim list : set list = new LinkedList_Class
		dim rs   : set rs   = DAL.PagedQuery(sql, empty, per_page, page_num)
				If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then
			rs.PageSize     = per_page
			rs.AbsolutePage = page_num
			page_count      = rs.PageCount
			record_count    = rs.RecordCount
		End If
		set PagedPurpleEnvelopsIndexView = PagedIndexViewKitList(rs, per_page)
		Destroy rs
	End Function




	Public Function PagedIndexView(per_page, page_num, ByRef page_count, ByRef record_count)
		dim sql : sql = "SELECT Kit.ID,Kit.JobNumber as [JobNumber], " &_
		"Jurisdiction.Name As Jurisdiction," &_
		"(SELECT COUNT(*) From KitLabels Where KitId = Kit.ID)" &_
		" As [LabelCount],[CreatedOn],[LabelsPrinted],[ExportedToSnailWorks],[JobType] FROM Kit INNER JOIN Jurisdiction ON Kit.Jcode = Jurisdiction.JCode Where Kit.JobType = 'Labels';"

		dim list : set list = new LinkedList_Class
		dim rs   : set rs   = DAL.PagedQuery(sql, empty, per_page, page_num)
				If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then
			rs.PageSize     = per_page
			rs.AbsolutePage = page_num
			page_count      = rs.PageCount
			record_count    = rs.RecordCount
		End If
		set PagedIndexView = PagedIndexViewKitList(rs, per_page)
		Destroy rs
	End Function

	Private Function PagedIndexViewKitList(rs, per_page)
    	dim list : set list = new LinkedList_Class

		dim x : x =0
		Do While x < per_page and Not rs.EOF
			list.Push Automapper.AutoMap(rs, new IndexKitModel_Class)
			x = x +1
			rs.MoveNext
		Loop
		set PagedIndexViewKitList = list
	End Function
	
	Public Function SwitchBoardEditFindById(ID)
		dim sql : sql = "SELECT Kit.ID,Kit.JobNumber as [JobNumber], " &_
		"Jurisdiction.Name As Jurisdiction," &_
		"(SELECT COUNT(*) From KitLabels Where KitId = Kit.ID)" &_
		" As [LabelCount],[CreatedOn],[LabelsPrinted],[ExportedToSnailWorks],[JobType],[Status] FROM Kit INNER JOIN Jurisdiction ON Kit.Jcode = Jurisdiction.JCode " &_
		"WHERE ID = ?   AND Kit.JobType = 'Labels'"

		dim rs : set rs = DAL.Query(sql,ID)
		If rs.EOF then
    		Err.Raise 1, "KitRepository_Class", KitNotFoundException("ID", ID)
		Else
			set SwitchBoardEditFindByID = Automapper.AutoMap(rs,"IndexKitModel_Class")
        End If


	End Function 

	Public Function SwitchBoardPurpleEnvelopeEditFindById(ID)
		dim sql : sql = "SELECT Kit.ID,Kit.JobNumber as [JobNumber],Kit.JCode, " &_
		"Jurisdiction.Name As Jurisdiction," &_
		"(SELECT COUNT(*) From InkjetRecords Where KitID = Kit.ID)" &_
		" As [LabelCount],[CreatedOn],[LabelsPrinted],[ExportedToSnailWorks],[JobType],[Status],[OutboundSTID],[InboundSTID],[OfficeCopiesAmount] FROM Kit INNER JOIN Jurisdiction ON Kit.Jcode = Jurisdiction.JCode " &_
		"WHERE ID = ? "

		dim rs : set rs = DAL.Query(sql,ID)
		If rs.EOF then
    		Err.Raise 1, "KitRepository_Class", KitNotFoundException("ID", ID)
		Else
		Set	SwitchBoardPurpleEnvelopeEditFindById = Automapper.AutoMap(rs,"IndexKitModel_Class")
        End If


	End Function


	Public Function FindByID(ID)
		dim sql : sql = "Select [ID], [JobNumber], [Jcode], [CreatedOn], [LabelsPrinted], [ExportedToSnailWorks], [InkJetJob], [JobType], [Filename], [Cass], [Status], [OutboundSTID], [InboundSTID], [OfficeCopiesAmount] FROM [Kit] WHERE ID = ?"
		dim rs : set rs = DAL.Query(sql,ID)
    	If rs.EOF then
    		Err.Raise 1, "KitRepository_Class", KitNotFoundException("ID", ID)
		Else
			set FindByID = Automapper.AutoMap(rs,"KitModel_Class")
        End If
	End Function

  	Public Function GetAll(orderBy)
		set GetAll = Find(empty,orderBy)
   	End Function

    Public Function Find(where_kvarray, order_string_or_array)
		dim sql : sql = "Select [ID], [JobNumber], [Jcode], [CreatedOn], [LabelsPrinted], [ExportedToSnailWorks], [InkJetJob], [JobType], [Filename], [Cass], [Status], [OutboundSTID], [InboundSTID], [OfficeCopiesAmount] FROM [Kit]"

 		If Not IsEmpty(where_kvarray) then
			sql = sql & " WHERE "
				dim where_keys, where_values
				KVUnzip where_kvarray, where_keys, where_values

			dim i
			For i = 0 to UBound(where_keys)
				If i > 0 then sql = sql & " AND "
				sql = sql & " " & where_keys(i) & " "
			Next
		End If

		If Not IsEmpty(order_string_or_array) then
			sql = sql & "ORDER BY "
			If IsArray(order_string_or_array) then
				dim order_array : order_array = order_string_or_array
				For i = 0 to UBound(order_array)
					If i > 0 then sql = sql & ", "
					sql = sql & " " & order_array(i)
				Next
			Else
				sql = sql & order_string_or_array & " "
			End If
		End If

		dim rs : set rs = DAL.Query(sql, where_values)
		set Find = KitList(rs)
		Destroy rs
	End Function

	Public Function FindPaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)
		dim sql : sql = "Select [ID], [JobNumber], [Jcode], [CreatedOn], [LabelsPrinted], [ExportedToSnailWorks], [InkJetJob], [JobType], [Filename], [Cass], [Status], [OutboundSTID], [InboundSTID], [OfficeCopiesAmount] FROM [Kit]"

 		If Not IsEmpty(where_kvarray) then
			sql = sql & " WHERE "
				dim where_keys, where_values
				KVUnzip where_kvarray, where_keys, where_values

			dim i
			For i = 0 to UBound(where_keys)
				If i > 0 then sql = sql & " AND "
				sql = sql & " " & where_keys(i) & " "
			Next
		End If

		If Not IsEmpty(order_string_or_array) then
			sql = sql & "ORDER BY "
			If IsArray(order_string_or_array) then
				dim order_array : order_array = order_string_or_array
				For i = 0 to UBound(order_array)
					If i > 0 then sql = sql & ", "
					sql = sql & " " & order_array(i)
				Next
			Else
				sql = sql & order_string_or_array & " "
			End If
		End If

		dim list : set list = new LinkedList_Class
		dim rs   : set rs   = DAL.PagedQuery(sql, where_values, per_page, page_num)

		If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then
			rs.PageSize     = per_page
			rs.AbsolutePage = page_num
			page_count      = rs.PageCount
			record_count    = rs.RecordCount
		End If

		set FindPaged = PagedKitList(rs, per_page)
		Destroy rs
	End Function

	Public Function SearchTablePaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)
		dim sql : sql = "Select [ID], [JobNumber], [Jcode], [CreatedOn], [LabelsPrinted], [ExportedToSnailWorks], [InkJetJob], [JobType], [Filename], [Cass], [Status], [OutboundSTID], [InboundSTID], [OfficeCopiesAmount] FROM [Kit]"

 		If Not IsEmpty(where_kvarray) then
			sql = sql & " WHERE "
				dim where_keys, where_values
				KVUnzip where_kvarray, where_keys, where_values

			dim i
			For i = 0 to UBound(where_keys)
				If i > 0 then sql = sql & " OR"
				sql = sql & " " & where_keys(i) & " LIKE ?"
			Next
		End If

		If Not IsEmpty(order_string_or_array) then
			sql = sql & " ORDER BY "
			If IsArray(order_string_or_array) then
				dim order_array : order_array = order_string_or_array
				For i = 0 to UBound(order_array)
					If i > 0 then sql = sql & ", "
					sql = sql & " " & order_array(i)
				Next
			Else
				sql = sql & order_string_or_array & " "
			End If
		End If

		dim list : set list = new LinkedList_Class
		dim rs   : set rs   = DAL.PagedQuery(sql, where_values, per_page, page_num)

		If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then
			rs.PageSize     = per_page
			rs.AbsolutePage = page_num
			page_count      = rs.PageCount
			record_count    = rs.RecordCount
		End If

		set SearchTablePaged = PagedKitList(rs, per_page)
		Destroy rs
	End Function

	Private Function PagedKitList(rs, per_page)
    	dim list : set list = new LinkedList_Class

		dim x : x =0
		Do While x < per_page and Not rs.EOF
			list.Push Automapper.AutoMap(rs, new KitModel_Class)
			x = x +1
			rs.MoveNext
		Loop
		set PagedKitList = list
	End Function

	Private Function KitNotFoundException(ByVal field_name, ByVal field_val)
			 KitNotFoundException = "Kit was not found with " & field_name & " of '" & field_val & "'."
	End Function

	Private Function KitList(rs)
		dim list : set list = new LinkedList_Class
		dim model

		Do until rs.EOF
			set model = new KitModel_Class
			list.Push Automapper.AutoMap(rs, model)
			rs.MoveNext
		Loop
		set KitList = list
	End Function

	Public Sub AddNew(ByRef model)
		dim sql : sql = "INSERT INTO [Kit] (" &_
													"[JobNumber]," &_
													"[Jcode]," &_
													"[CreatedOn]," &_
													"[JobType])" &_ 
						"VALUES (?,?,?,?)"
		DAL.Execute sql, Array(model.JobNumber, _
							  model.Jcode,Now(),"Labels")
		sql = "SELECT TOP 1 ID FROM [Kit] ORDER BY ID DESC"
			dim rs : set rs = DAL.Query(sql, empty)
			model.ID = rs("ID")
			Destroy rs
	End Sub
Public Sub Update(model)
		dim sql : sql = "UPDATE [Kit] SET [JobNumber] = ?," &_
						"[Jcode] = ?," &_
						"[CreatedOn] = ?," &_
						"[LabelsPrinted] = ?," &_
						"[ExportedToSnailWorks] = ?," &_
						"[InkJetJob] = ?," &_
						"[JobType] = ?," &_
						"[Filename] = ?," &_
						"[Cass] = ?," &_
						"[Status] = ?," &_
						"[OutboundSTID] = ?," &_
						"[InboundSTID] = ?," &_
						"[OfficeCopiesAmount] = ?" &_
						" WHERE [ID] = ?"

		DAL.Execute sql, Array(model.JobNumber, _
							  model.Jcode, _
							  model.CreatedOn, _
							  model.LabelsPrinted, _
							  model.ExportedToSnailWorks, _
							  model.InkJetJob, _
							  model.JobType, _
							  model.Filename, _
							  model.Cass, _
							  model.Status, _
							  model.OutboundSTID, _
							  model.InboundSTID, _
							  model.OfficeCopiesAmount, _
							  model.ID)
	End Sub

	Public Sub Delete(id)
		dim sql : sql = "DELETE FROM [Kit] WHERE [ID] = ?"
		DAL.Execute sql, id
	End Sub
End Class

dim KitRepository__Singleton
Function KitRepository()
	If IsEmpty(KitRepository__Singleton) then
		set KitRepository__Singleton = new KitRepository_Class
	End If
	set KitRepository = KitRepository__Singleton
End Function
%>
