<%
'=======================================================================================================================
' KitLabels Model
'=======================================================================================================================

Class KitLabelsModel_Class
	Public Validator
	Public Class_Get_Properties

	Public ID '90
	Public KitId '122
	Public OutboundSerial '106
	Public InBoundSerial '106
	Public OutboundIMB '106
	Public InBoundIMB '106
	Public OutboundIMBDigits '106
	Public InBoundIMBDigits '106
	Public OutboundIMBPNG
	Public INBOUNDIMBPNG
	Public SetNumber
	Private Sub Class_Initialize
		'ValidateExitsts Me, "",""
		Class_Get_Properties = Array("ID, KitId, OutboundSerial, InBoundSerial, OutboundIMB, InBoundIMB, OutboundIMBDigits, InBoundIMBDigits,OutboundIMBPNG,INBOUNDIMBPNG,SetNumber")
	End Sub

End CLass

'=======================================================================================================================
' KitLabels Repository
'=======================================================================================================================

Class KitLabelsRepository_Class

	Public Function FindByID(ID)
		dim sql : sql = "Select [ID], [KitId], [OutboundSerial], [InBoundSerial], [OutboundIMB], [InBoundIMB], [OutboundIMBDigits], [InBoundIMBDigits],OutboundIMBPNG,INBOUNDIMBPNG,[SetNumber] FROM [KitLabels] WHERE ID = ?"
		dim rs : set rs = DAL.Query(sql,ID)
    	If rs.EOF then
    		Err.Raise 1, "KitLabelsRepository_Class", KitLabelsNotFoundException("ID", ID)
		Else
			set FindByID = Automapper.AutoMap(rs,"KitLabelsModel_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], [KitId], [OutboundSerial], [InBoundSerial], [OutboundIMB], [InBoundIMB], [OutboundIMBDigits], [InBoundIMBDigits],OutboundIMBPNG,INBOUNDIMBPNG, [SetNumber] FROM [KitLabels]"

 		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 = KitLabelsList(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], [KitId], [OutboundSerial], [InBoundSerial], [OutboundIMB], [InBoundIMB], [OutboundIMBDigits], [InBoundIMBDigits],OutboundIMBPNG,INBOUNDIMBPNG, [SetNumber] FROM [KitLabels]"

 		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 = PagedKitLabelsList(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], [KitId], [OutboundSerial], [InBoundSerial], [OutboundIMB], [InBoundIMB], [OutboundIMBDigits], [InBoundIMBDigits],OutboundIMBPNG,INBOUNDIMBPNG, [SetNumber] FROM [KitLabels]"

 		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 = PagedKitLabelsList(rs, per_page)
		Destroy rs
	End Function

	Private Function PagedKitLabelsList(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 KitLabelsModel_Class)
			x = x +1
			rs.MoveNext
		Loop
		set PagedKitLabelsList = list
	End Function

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

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

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

	Public Sub BulkAdd(KitId,Amount)
		Dim rest : Set rest = Server.CreateObject("Chilkat_9_5_0.Rest")
		Dim responseJson : responseJson = rest.Connect("https://postalpro.usps.com",443,1,1)
		Dim Kit : Set Kit = KitRepository.Find(Array("ID = ?",KitId),empty).pop()
		Dim Jurisdiction:Set Jurisdiction = JurisdictionRepository.Find(Array("JCode =?",Kit.Jcode),empty).pop
		Dim MailingID:MailingID = SettingsRepository.Find(Array("Name =?","MailingID"),empty).pop().Value
		Dim SerialNumberStart:SerialNumberStart = SettingsRepository.Find(Array("Name =?","SerialNumberStart"),empty).pop().Value
		Dim serialOffset:serialOffset = SettingsRepository.Find(Array("Name =?","SerialOffset"),empty).pop().Value
		Dim SerialStart:SerialStart  = CLng(SerialNumberStart) + CLng(serialOffset)
		dim i,s
		s = 1
		for i = 0 to (Amount * 2) - 2 Step 2 
		dim NewKitLabel : set NewKitLabel = new KitLabelsModel_Class
			NewKitLabel.KitId = KitId
			NewKitLabel.SetNumber = s
			s = s + 1
			NewKitLabel.OutboundSerial = PadLeft(SerialStart + i,9,"0")
			NewKitLabel.InBoundSerial = PadLeft(SerialStart + i + 1,9,"0")
			NewKitLabel.OutboundIMBDigits ="00716" & MailingID & NewKitLabel.OutboundSerial & "000000000" 
			NewKitLabel.InBoundIMBDigits = "00778" & MailingID & NewKitLabel.InBoundSerial & right(Jurisdiction.IMB_Digits,9)
			dim imbJson : imbJson = rest.FullRequestNoBody("get","/ppro-tools-api/imb/encode?imb=" & NewKitLabel.OutboundIMBDigits)
			
			json.loadJSON(imbJson)

			NewKitLabel.OutboundIMB = json.Data("imb")  
			NewKitLabel.OutboundIMBPNG = json.Data("imbImage")
			imbJson = rest.FullRequestNoBody("get","/ppro-tools-api/imb/encode?imb=" & NewKitLabel.InBoundIMBDigits )
			json.loadJSON(imbJson)
			NewKitLabel.InBoundIMB = json.Data("imb") 
			NewKitLabel.INBOUNDIMBPNG = json.Data("imbImage")
			me.AddNew NewKitLabel
		Next

		DAL.Execute "UPDATE Settings SET [Value] ='"  &  (Amount * 2) + serialOffset  & "' Where [Name] ='SerialOffset'",empty

	

	
	
	End Sub

	Public Sub AddNew(ByRef model)
		dim sql : sql = "INSERT INTO [KitLabels] (" &_
													"[KitId]," &_
													"[OutboundSerial]," &_
													"[InBoundSerial]," &_
													"[OutboundIMB]," &_
													"[InBoundIMB]," &_
													"[OutboundIMBDigits]," &_
													"[InBoundIMBDigits]," &_
													"[OutboundIMBPNG]," &_
													"[INBOUNDIMBPNG]," &_
													"[SetNumber])" &_
						"VALUES (?,?,?,?,?,?,?,?,?,?)"
		DAL.Execute sql, Array(model.KitId, _
							  model.OutboundSerial, _
							  model.InBoundSerial, _
							  model.OutboundIMB, _
							  model.InBoundIMB, _
							  model.OutboundIMBDigits, _
							  model.InBoundIMBDigits, _
							  model.OutboundIMBPNG, _
							  model.INBOUNDIMBPNG, _
							  model.SetNumber)
			sql = "SELECT TOP 1 ID FROM [KitLabels] 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 [KitLabels] SET [KitId] = ?," &_
						"[OutboundSerial] = ?," &_
						"[InBoundSerial] = ?," &_
						"[OutboundIMB] = ?," &_
						"[InBoundIMB] = ?," &_
						"[OutboundIMBDigits] = ?," &_
						"[InBoundIMBDigits] = ?," &_
						"OutboundIMBPNG,INBOUNDIMBPNG,[SetNumber])" &_
						" WHERE [ID] = ?"

		DAL.Execute sql, Array(model.KitId, _
							  model.OutboundSerial, _
							  model.InBoundSerial, _
							  model.OutboundIMB, _
							  model.InBoundIMB, _
							  model.OutboundIMBDigits, _
							  model.InBoundIMBDigits, _
							  model.OutboundIMBPNG, _
							  model.INBOUNDIMBPNG, _
							  model.SetNumber, _
							  model.ID)
	End Sub

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

dim KitLabelsRepository__Singleton
Function KitLabelsRepository()
	If IsEmpty(KitLabelsRepository__Singleton) then
		set KitLabelsRepository__Singleton = new KitLabelsRepository_Class
	End If
	set KitLabelsRepository = KitLabelsRepository__Singleton
End Function
%>
