Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmHistEventos
	Inherits System.Windows.Forms.Form
	
	Private Sub frmHistEventos_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call setGridCons()
		Call setGridUnid()
	End Sub
	
	Private Sub listaHistoricoCons()
		Dim sql As String
		Dim rs As New ADODB.Recordset
		
		tHistCons.Text = ""
		
		sql = "SELECT evento FROM hist_cons WHERE cons_id = '" & gridCons.get_TextMatrix(gridCons.Row, 0) & "' "
		
		Call Conectar(sql, rs)
		
		If rs.RecordCount > 0 Then
			While Not rs.EOF
				tHistCons.Text = tHistCons.Text & rs.Fields("evento").Value & vbCrLf
				rs.MoveNext()
			End While
		End If
	End Sub
	
	Private Sub listaHistoricoUnidCons()
		Dim sql As String
		Dim rs As New ADODB.Recordset
		
		tHistUnidCons.Text = ""
		
		sql = "SELECT evento FROM hist_unid "
		sql = sql & "WHERE zona = '" & gridUnid.get_TextMatrix(gridUnid.Row, 1) & "' "
		sql = sql & "AND livro  = '" & gridUnid.get_TextMatrix(gridUnid.Row, 2) & "' "
		sql = sql & "AND conta  = '" & gridUnid.get_TextMatrix(gridUnid.Row, 3) & "' "
		
		Call Conectar(sql, rs)
		
		If rs.RecordCount > 0 Then
			While Not rs.EOF
				tHistUnidCons.Text = tHistUnidCons.Text & rs.Fields("evento").Value & vbCrLf
				rs.MoveNext()
			End While
		End If
	End Sub
	
	Private Sub gridCons_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles gridCons.ClickEvent
		If gridCons.Rows > 1 Then
			gridCons.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightAlways
			Call listaHistoricoCons()
		End If
	End Sub
	
	Private Sub gridUnid_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles gridUnid.ClickEvent
		If gridUnid.Rows > 1 Then
			gridUnid.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightAlways
			Call listaHistoricoUnidCons()
		End If
	End Sub
	
	Private Sub setGridCons()
		With gridCons
			.Rows = 1
			.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightNever
			.set_ColWidth(0, 1000)
			.set_TextMatrix(0, 0, "Código")
			.set_ColWidth(1, 4000)
			.set_TextMatrix(0, 1, "Nome")
			.set_ColWidth(2, 1870)
			.set_TextMatrix(0, 2, "CPF/CGC")
			.set_ColWidth(3, 1865)
			.set_TextMatrix(0, 3, "RG")
		End With
	End Sub
	
	Private Sub setGridUnid()
		With gridUnid
			.Rows = 1
			.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightNever
			.set_ColWidth(0, 1000)
			.set_TextMatrix(0, 0, "Código")
			.set_ColWidth(1, 1000)
			.set_TextMatrix(0, 1, "Zona")
			.set_ColWidth(2, 1000)
			.set_TextMatrix(0, 2, "Livro")
			.set_ColWidth(3, 1000)
			.set_TextMatrix(0, 3, "Conta")
			.set_ColWidth(4, 1000)
			.set_TextMatrix(0, 4, "Rota")
			.set_ColWidth(5, 1000)
			.set_TextMatrix(0, 5, "Grupo")
			.set_ColWidth(6, 1000)
			.set_TextMatrix(0, 6, "Id Cons")
		End With
	End Sub
	
	Private Sub buscaConsumidor()
		Dim sql As String
		Dim rs As New ADODB.Recordset
		
		sql = "SELECT consreg, nome, cgc, rg "
		sql = sql & "FROM consum "
		sql = sql & "WHERE nome LIKE('%" & tNome.Text & "%') "
		sql = sql & "ORDER BY nome "
		
		Call Conectar(sql, rs)
		
		If rs.RecordCount > 0 Then
			gridCons.Redraw = False
			
			While Not rs.EOF
				With gridCons
					.Rows = .Rows + 1
					
					.set_TextMatrix(.Rows - 1, 0, rs.Fields("ConsReg").Value)
					.set_TextMatrix(.Rows - 1, 1, rs.Fields("nome").Value)
					If Len(rs.Fields("cgc").Value) = 11 Then
						.set_TextMatrix(.Rows - 1, 2, Mid(rs.Fields("cgc").Value, 1, 3) & "." & Mid(rs.Fields("cgc").Value, 4, 3) & "." & Mid(rs.Fields("cgc").Value, 7, 3) & "-" & Mid(rs.Fields("cgc").Value, 10, 2))
					ElseIf Len(rs.Fields("cgc").Value) = 14 Then 
						.set_TextMatrix(.Rows - 1, 2, Mid(rs.Fields("cgc").Value, 1, 2) & "." & Mid(rs.Fields("cgc").Value, 3, 3) & "." & Mid(rs.Fields("cgc").Value, 6, 3) & "/" & Mid(rs.Fields("cgc").Value, 9, 4) & "-" & Mid(rs.Fields("cgc").Value, 13, 2))
					Else
						.set_TextMatrix(.Rows - 1, 2, rs.Fields("cgc").Value)
					End If
					.set_TextMatrix(.Rows - 1, 3, rs.Fields("rg").Value)
					
					rs.MoveNext()
				End With
			End While
			
			gridCons.Redraw = True
		Else
			MsgBox("O consumidor especificado não pode ser localizado. " & vbCrLf & vbCrLf & "Tente outro critério de busca.", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
	End Sub
	
	Private Sub buscaUnid()
		Dim sql As String
		Dim rs As New ADODB.Recordset
		
		sql = "SELECT unid_id, zona, livro, conta, rota, grupo, cons_id "
		sql = sql & "FROM unida "
		
		If Trim(tZona.Text) <> "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) <> "" Then
			sql = sql & "WHERE zona = '" & tZona.Text & "' AND livro = '" & tLivro.Text & "' AND conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) = "" Then 
			
		ElseIf Trim(tZona.Text) <> "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) = "" Then 
			sql = sql & "WHERE zona = '" & tZona.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) <> "" Then 
			sql = sql & "WHERE livro = '" & tLivro.Text & "' AND conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) <> "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) = "" Then 
			sql = sql & "WHERE zona = '" & tZona.Text & "' AND livro = '" & tLivro.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) <> "" Then 
			sql = sql & "WHERE conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) <> "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) <> "" Then 
			sql = sql & "WHERE zona = '" & tZona.Text & "' AND conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) = "" Then 
			sql = sql & "WHERE livro = '" & tLivro.Text & "' "
		End If
		
		sql = sql & "UNION "
		sql = sql & "SELECT unid_id, zona, livro, conta, rota, grupo, cons_id "
		sql = sql & "FROM unidb "
		
		If Trim(tZona.Text) <> "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) <> "" Then
			sql = sql & "WHERE zona = '" & tZona.Text & "' AND livro = '" & tLivro.Text & "' AND conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) = "" Then 
			
		ElseIf Trim(tZona.Text) <> "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) = "" Then 
			sql = sql & "WHERE zona = '" & tZona.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) <> "" Then 
			sql = sql & "WHERE livro = '" & tLivro.Text & "' AND conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) <> "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) = "" Then 
			sql = sql & "WHERE zona = '" & tZona.Text & "' AND livro = '" & tLivro.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) <> "" Then 
			sql = sql & "WHERE conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) <> "" And Trim(tLivro.Text) = "" And Trim(tConta.Text) <> "" Then 
			sql = sql & "WHERE zona = '" & tZona.Text & "' AND conta = '" & tConta.Text & "' "
		ElseIf Trim(tZona.Text) = "" And Trim(tLivro.Text) <> "" And Trim(tConta.Text) = "" Then 
			sql = sql & "WHERE livro = '" & tLivro.Text & "' "
		End If
		
		sql = sql & "ORDER BY 2, 3, 4 "
		
		Call Conectar(sql, rs)
		
		If rs.RecordCount > 0 Then
			gridUnid.Redraw = False
			
			While Not rs.EOF
				With gridUnid
					.Rows = .Rows + 1
					
					.set_TextMatrix(.Rows - 1, 0, rs.Fields("unid_id").Value)
					.set_TextMatrix(.Rows - 1, 1, rs.Fields("Zona").Value)
					.set_TextMatrix(.Rows - 1, 2, rs.Fields("Livro").Value)
					.set_TextMatrix(.Rows - 1, 3, rs.Fields("Conta").Value)
					'UPGRADE_WARNING: Couldn't resolve default property of object ChecaNulo(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					.set_TextMatrix(.Rows - 1, 4, ChecaNulo(rs.Fields("ROTA")))
					'UPGRADE_WARNING: Couldn't resolve default property of object ChecaNulo(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					.set_TextMatrix(.Rows - 1, 5, ChecaNulo(VB.Left(rs.Fields("grupo").Value, 1)))
					.set_TextMatrix(.Rows - 1, 6, rs.Fields("cons_id").Value)
					
					rs.MoveNext()
				End With
			End While
			
			gridUnid.Redraw = True
		Else
			MsgBox("O unidade consumidora especificada não pode ser localizada. " & vbCrLf & vbCrLf & "Tente outro critério de busca.", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
	End Sub
	
	Private Sub btBuscarCons_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btBuscarCons.Click
		Call setGridCons()
		Call buscaConsumidor()
	End Sub
	
	Private Sub btBuscarUnid_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btBuscarUnid.Click
		Call setGridUnid()
		Call buscaUnid()
	End Sub
	
	Private Sub btFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btFechar.Click
		Me.Close()
	End Sub
	
	Private Sub tZona_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tZona.Enter
		tZona.SelectionStart = 0
		tZona.SelectionLength = Len(tZona.Text)
	End Sub
	
	Private Sub tZona_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tZona.Leave
		tZona.Text = VB6.Format(tZona.Text, "000")
	End Sub
	
	Private Sub tLivro_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tLivro.Enter
		tLivro.SelectionStart = 0
		tLivro.SelectionLength = Len(tLivro.Text)
	End Sub
	
	Private Sub tLivro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tLivro.Leave
		tLivro.Text = VB6.Format(tLivro.Text, "00")
	End Sub
	
	Private Sub tConta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tConta.Enter
		tConta.SelectionStart = 0
		tConta.SelectionLength = Len(tConta.Text)
	End Sub
	
	Private Sub tConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tConta.Leave
		tConta.Text = VB6.Format(tConta.Text, "000000")
	End Sub
	
	Private Sub tZona_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles tZona.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then Call btBuscarUnid_Click(btBuscarUnid, New System.EventArgs())
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub tLivro_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles tLivro.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then Call btBuscarUnid_Click(btBuscarUnid, New System.EventArgs())
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub tConta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles tConta.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then Call btBuscarUnid_Click(btBuscarUnid, New System.EventArgs())
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub tNome_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles tNome.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then Call btBuscarCons_Click(btBuscarCons, New System.EventArgs())
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
End Class