Option Strict Off
Option Explicit On
Friend Class frmHistCons
	Inherits System.Windows.Forms.Form
	
	Dim med3 As Double
	Dim med12 As Double
	Dim med60 As Double
	Dim som3 As Double
	Dim som12 As Double
	Dim som60 As Double
	Dim maior As Double
	Dim menor As Double
	Dim mesMaior As String
	Dim mesMenor As String
	Dim max As Integer
	Public unid As String
	
	Private Sub cmdLocalizar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizar.Click
		If optGrA.Checked = True Then
			unid = "unida"
		ElseIf optGrB.Checked = True Then 
			unid = "unidb"
		End If
		
		Call frmLocalizaUnidade.Show()
		Call frmLocalizaUnidade.SetaLocalizaUnidade("U", Me)
	End Sub
	
	Public Sub SetaCodigo(ByVal alngCodigo As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal astrGrupo As String)
		tZona.Text = astrZona
		tLivro.Text = astrLivro
		tConta.Text = astrConta
		
		Call tConta_Leave(tConta, New System.EventArgs())
		Call cmdListar_Click(cmdListar, New System.EventArgs())
	End Sub
	
	Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
		Me.Close()
	End Sub
	
	Private Sub cmdListar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdListar.Click
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		Dim sql As String
		Dim I As Short
		Dim leitura As Double
		
		
		maior = 0
		
		sql = "select nf.mesano, nf.dataleit, nf.datapag,nf.dataleitant, nf.dataproxleit, unidb.ultleit, buscavlritem(nf.itensserv,'030201008') consumo, nf.valortot "
		sql = sql & " from nf, unidb where nf.zona = unidb.zona and nf.livro = unidb.livro and nf.conta = unidb.conta"
		sql = sql & " and unidb.zona = '" & VB6.Format(tZona.Text, "000") & "'"
		sql = sql & " and unidb.livro = '" & VB6.Format(tLivro.Text, "00") & "'"
		sql = sql & " and unidb.conta = '" & VB6.Format(tConta.Text, "000000") & "'"
		sql = sql & " order by mid(mesano,4)desc, mid(mesano,1,2) desc"
		
		'MsgBox sql
		
		Call Conectar(sql, lrstAux)
		
		grid.Rows = 61
		som3 = 0
		som12 = 0
		som60 = 0
		
		I = 1
		
		If lrstAux.RecordCount <= 0 Then
			MsgBox("O consumidor indicado não possui histórico!")
		Else
			If lrstAux.RecordCount >= 60 Then
				max = 60
			Else
				max = lrstAux.RecordCount
			End If
			
			grid.Rows = max + 1
			
			leitura = lrstAux.Fields("ultleit").Value
			menor = lrstAux.Fields("consumo").Value
			
			While I <= max
				If maior < lrstAux.Fields("consumo").Value Then
					maior = lrstAux.Fields("consumo").Value
					mesMaior = lrstAux.Fields("mesano").Value
				End If
				If menor > lrstAux.Fields("consumo").Value Then
					menor = lrstAux.Fields("consumo").Value
					mesMenor = lrstAux.Fields("mesano").Value
				End If
				
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				If I < 3 Then som3 = som3 + IIf(IsDbNull(lrstAux.Fields("consumo").Value), 0, lrstAux.Fields("consumo").Value)
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				If I < 12 Then som12 = som12 + IIf(IsDbNull(lrstAux.Fields("consumo").Value), 0, lrstAux.Fields("consumo").Value)
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				som60 = som60 + IIf(IsDbNull(lrstAux.Fields("consumo").Value), 0, lrstAux.Fields("consumo").Value)
				grid.set_TextMatrix(I, 0, lrstAux.Fields("mesano").Value)
				grid.set_TextMatrix(I, 1, VB6.Format(lrstAux.Fields("dataleit").Value, "dd/mm/yyyy"))
				grid.set_TextMatrix(I, 2, VB6.Format(lrstAux.Fields("datapag").Value, "dd/mm/yyyy"))
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				grid.set_TextMatrix(I, 3, IIf(IsDbNull(lrstAux.Fields("dataproxleit").Value), 0, lrstAux.Fields("dataproxleit").Value) - IIf(IsDbNull(lrstAux.Fields("dataleitant").Value), 0, lrstAux.Fields("dataleitant").Value))
				grid.set_TextMatrix(I, 4, VB6.Format(leitura, "0"))
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				grid.set_TextMatrix(I, 5, VB6.Format(IIf(IsDbNull(lrstAux.Fields("consumo").Value), 0, lrstAux.Fields("consumo").Value), "0"))
				grid.set_TextMatrix(I, 6, VB6.Format(lrstAux.Fields("ValorTot").Value, "Standard"))
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				leitura = leitura - IIf(IsDbNull(lrstAux.Fields("consumo").Value), 0, lrstAux.Fields("consumo").Value)
				I = I + 1
				lrstAux.MoveNext()
			End While
		End If
		
		med3 = som3 / 3
		med12 = som12 / 12
		med60 = som60 / max
		
		maiorcons.Text = maior & " - " & mesMaior
		menorcons.Text = menor & " - " & mesMenor
		media3.Text = VB6.Format(med3, "0")
		media12.Text = VB6.Format(med12, "0")
		
		Option1.Text = max & " meses"
		
		Option1.Checked = True
		Call Option1_CheckedChanged(Option1, New System.EventArgs())
		
		Option1.Enabled = True
		Option2.Enabled = True
		Option3.Enabled = True
		lrstAux.Close()
		
	End Sub
	
	Private Sub frmHistCons_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		
		grid.set_ColWidth(0, 1300)
		grid.set_TextMatrix(0, 0, "Mês/Ano")
		grid.set_ColWidth(1, 1300)
		grid.set_TextMatrix(0, 1, "Data da Leitura")
		grid.set_ColWidth(2, 1700)
		grid.set_TextMatrix(0, 2, "Data do Pagamento")
		grid.set_ColWidth(3, 1300)
		grid.set_TextMatrix(0, 3, "Dias")
		grid.set_ColWidth(4, 1300)
		grid.set_TextMatrix(0, 4, "Leitura")
		grid.set_ColWidth(5, 1300)
		grid.set_TextMatrix(0, 5, "Consumo")
		grid.set_ColWidth(6, 1300)
		grid.set_TextMatrix(0, 6, "Valor (R$)")
		
	End Sub
	
	
	'UPGRADE_WARNING: Event Option1.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub Option1_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Option1.CheckedChanged
		If eventSender.Checked Then
			Dim I As Integer
			Dim J As Integer
			If Option1.Checked = True Then
				
				If grid.Rows < max + 1 Then Exit Sub
				
				MSChart1.ColumnCount = 2
				MSChart1.RowCount = max
				I = max
				J = 1
				
				While I >= 1
					MSChart1.Row = J
					MSChart1.Column = 1
					MSChart1.RowLabel = ""
					MSChart1.data = grid.get_TextMatrix(I, 5)
					MSChart1.Row = J
					MSChart1.Column = 2
					MSChart1.RowLabel = ""
					MSChart1.data = CStr(med60)
					I = I - 1
					J = J + 1
				End While
			End If
		End If
	End Sub
	
	'UPGRADE_WARNING: Event Option2.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub Option2_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Option2.CheckedChanged
		If eventSender.Checked Then
			Dim I As Integer
			Dim J As Integer
			If Option2.Checked = True Then
				
				If grid.Rows < 13 Then Exit Sub
				
				MSChart1.ColumnCount = 2
				MSChart1.RowCount = 12
				
				I = 12
				J = 1
				
				While I >= 1
					MSChart1.Row = J
					MSChart1.Column = 1
					MSChart1.RowLabel = grid.get_TextMatrix(I, 0)
					MSChart1.data = grid.get_TextMatrix(I, 5)
					MSChart1.Row = J
					MSChart1.Column = 2
					'MSChart1.RowLabel = ""
					MSChart1.data = CStr(med12)
					I = I - 1
					J = J + 1
				End While
				
			End If
		End If
	End Sub
	
	'UPGRADE_WARNING: Event Option3.CheckedChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub Option3_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Option3.CheckedChanged
		If eventSender.Checked Then
			Dim I As Integer
			Dim J As Integer
			If Option3.Checked = True Then
				
				If grid.Rows < 4 Then Exit Sub
				
				MSChart1.ColumnCount = 2
				MSChart1.RowCount = 3
				
				'    If Grid.Rows >= 4 Then
				I = 3
				'    ElseIf Grid.Rows < 4 Then
				'      i = Grid.Rows - 1
				'    End If
				
				J = 1
				
				While I >= 1
					MSChart1.Row = J
					MSChart1.Column = 1
					MSChart1.RowLabel = grid.get_TextMatrix(I, 0)
					MSChart1.data = grid.get_TextMatrix(I, 5)
					MSChart1.Row = J
					MSChart1.Column = 2
					'MSChart1.RowLabel = ""
					MSChart1.data = CStr(med3)
					I = I - 1
					J = J + 1
				End While
				
			End If
			
		End If
	End Sub
	
	Private Sub tConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tConta.Leave
		If Len(tConta.Text) > 0 Then tConta.Text = VB6.Format(tConta.Text, "000000")
		
		Dim sql As String
		Dim consulta As New ADODB.Recordset
		
		sql = "SELECT nome FROM unidb "
		sql = sql & "WHERE unidb.zona = '" & VB6.Format(tZona.Text, "000") & "' "
		sql = sql & "AND unidb.livro = '" & VB6.Format(tLivro.Text, "00") & "' "
		sql = sql & "AND unidb.conta = '" & VB6.Format(tConta.Text, "000000") & "' "
		
		Call Conectar(sql, consulta)
		If consulta.RecordCount > 0 Then
			nome.Text = consulta.Fields("nome").Value
			Call cmdListar_Click(cmdListar, New System.EventArgs())
			grid.Focus()
		Else
			nome.Text = "Consumidor não encontrado!"
		End If
	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 SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		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 SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		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 SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub tZona_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tZona.Leave
		If Len(tZona.Text) > 0 Then tZona.Text = VB6.Format(tZona.Text, "000")
	End Sub
	
	Private Sub tLivro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tLivro.Leave
		If Len(tLivro.Text) > 0 Then tLivro.Text = VB6.Format(tLivro.Text, "00")
	End Sub
End Class