Option Strict Off
Option Explicit On
Friend Class frmIsento
	Inherits System.Windows.Forms.Form
	
	Private strSql As String
	
	'UPGRADE_WARNING: Event chkReCobrar.CheckStateChanged 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 chkReCobrar_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkReCobrar.CheckStateChanged
		cmdSalvar.Enabled = True
		If chkReCobrar.CheckState = 1 Then
			txtUsuario.Text = gstrUsuario_Login
		Else
			txtUsuario.Text = ""
		End If
		
	End Sub
	
	Private Sub frmIsento_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		'Seta Mês e Ano atual no filtro
		txtMesAno.Text = Mid(CStr(Now), 4, 7)
		
		Call formaGrid()
		cmdSalvar.Enabled = False
	End Sub
	
	'UPGRADE_WARNING: Event chkIsento.CheckStateChanged 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 chkIsento_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkIsento.CheckStateChanged
		'Habilita botão Salvar
		cmdSalvar.Enabled = True
		
		'Seta o usuário
		If chkIsento.CheckState = 1 Then
			txtUsuario.Text = gstrUsuario_Login
		Else
			txtUsuario.Text = ""
		End If
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		If cmdSalvar.Enabled = True Then
			If MsgBox("Existem alterações que não foram salvas." & vbCrLf & vbCrLf & "Deseja salvá-las agora?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				Call cmdSalvar_Click(cmdSalvar, New System.EventArgs())
				cmdSalvar.Enabled = False
			End If
		End If
		
		Me.Close()
	End Sub
	
	Private Sub cmdPesquisar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPesquisar.Click
		
		If txtMesAno.Text = "" Then
			txtMesAno.Focus()
			Exit Sub
		End If
		
		Call formaGrid()
		Call EncheGrid()
		Call BuscaDados()
	End Sub
	
	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		If chkIsento.CheckState = 1 Then
			strSql = "UPDATE multajuros SET isento = 1,  usuario = """ & txtUsuario.Text & """ WHERE codigo = '" & grid.get_TextMatrix(grid.Row, 0) & "'  "
			Call Executar(strSql)
		End If
		
		If Me.chkReCobrar.CheckState = 1 Then
			strSql = "UPDATE multajuros SET cobrado = 'N',  usuario = """ & txtUsuario.Text & """ WHERE codigo = '" & grid.get_TextMatrix(grid.Row, 0) & "'  "
			Call Executar(strSql)
		End If
		cmdSalvar.Enabled = False
	End Sub
	
	Private Sub formaGrid()
		grid.Rows = 1
		grid.Clear()
		
		grid.set_ColWidth(0, 0)
		grid.set_TextMatrix(0, 0, "")
		grid.set_ColWidth(1, 16659)
		grid.set_TextMatrix(0, 1, "Nome")
	End Sub
	
	Private Sub EncheGrid()
		Dim rsConsulta As New ADODB.Recordset
		Dim Filtro As String
		
		Filtro = ""
		If Len(Trim(txtFiltroZona.Text)) > 0 Then
			Filtro = " AND m.zona = " & txtFiltroZona.Text & ""
		End If
		If Len(Trim(txtFiltroLivro.Text)) > 0 Then
			Filtro = Filtro & " AND m.livro = " & txtFiltroLivro.Text & ""
		End If
		If Len(Trim(txtFiltroConta.Text)) > 0 Then
			Filtro = Filtro & " AND m.conta = " & txtFiltroConta.Text & ""
		End If
		
		strSql = " SELECT m.*, u.nome FROM multajuros m, unidb u " & " WHERE m.zona = u.zona " & " AND m.livro = u.livro " & " AND m.conta = u.conta " & " AND mesano = """ & txtMesAno.Text & """" & Filtro & " UNION ALL " & " SELECT m.*, u.nome FROM multajuros m, unida u " & " WHERE m.zona = u.zona " & " AND m.livro = u.livro " & " AND m.conta = u.conta " & " AND mesano = """ & txtMesAno.Text & """" & Filtro
		
		Call Conectar(strSql, rsConsulta)
		
		If rsConsulta.RecordCount > 0 Then
			Do While Not rsConsulta.EOF
				grid.Rows = grid.Rows + 1
				
				'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(grid.Rows - 1, 0, IIf(IsDbNull(rsConsulta.Fields("codigo").Value), 0, rsConsulta.Fields("codigo").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"'
				grid.set_TextMatrix(grid.Rows - 1, 1, IIf(IsDbNull(rsConsulta.Fields("nome").Value), "", rsConsulta.Fields("nome").Value))
				
				rsConsulta.MoveNext()
			Loop 
		Else
			grid.Clear()
			Call formaGrid()
		End If
		If rsConsulta.State = 1 Then rsConsulta.Close()
	End Sub
	
	Private Sub grid_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grid.ClickEvent
		Call BuscaDados()
	End Sub
	
	'Responsável por buscar os dados quando o usuário
	'navegar com as setas para baixo e para cima no grid
	Private Sub grid_EnterCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grid.EnterCell
		Call BuscaDados()
	End Sub
	
	Private Sub grid_LeaveCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grid.LeaveCell
		If cmdSalvar.Enabled = True Then
			If MsgBox("Existem alterações que não foram salvas." & vbCrLf & vbCrLf & "Deseja salvá-las agora?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				Call cmdSalvar_Click(cmdSalvar, New System.EventArgs())
				cmdSalvar.Enabled = False
			End If
		End If
	End Sub
	
	Private Sub txtFiltroConta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroConta.Enter
		With txtFiltroConta
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtFiltroConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroConta.Leave
		txtFiltroConta.Text = VB6.Format(txtFiltroConta.Text, "000000")
	End Sub
	
	Private Sub txtFiltroLivro_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroLivro.Enter
		With txtFiltroLivro
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtFiltroLivro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroLivro.Leave
		txtFiltroLivro.Text = VB6.Format(txtFiltroLivro.Text, "00")
	End Sub
	
	Private Sub txtFiltroZona_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroZona.Enter
		With txtFiltroZona
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtFiltroZona_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroZona.Leave
		txtFiltroZona.Text = VB6.Format(txtFiltroZona.Text, "000")
	End Sub
	
	Private Sub txtMesAno_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAno.Enter
		With txtMesAno
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtMesAno_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAno.Leave
		txtMesAno.Text = FormataData(txtMesAno, 1)
		If Len(Trim(txtMesAno.Text)) = 0 Then
			txtMesAno.Text = LeConfig("MESANOFATURAMENTO")
		End If
	End Sub
	
	Private Sub frmIsento_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Or KeyAscii = System.Windows.Forms.Keys.Tab Then 'Identifica o Enter
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True) 'Grava no buffer do teclado um Tab
			KeyAscii = 0 'Zera KeyAscii para eliminar o Enter
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub BuscaDados()
		Dim rsConsulta As New ADODB.Recordset
		
		strSql = "SELECT m.*, u.nome FROM multajuros m, unidb u " & "WHERE m.zona = u.zona " & "AND m.livro = u.livro " & "AND m.conta = u.conta " & "AND m.codigo = '" & grid.get_TextMatrix(grid.Row, 0) & "' " & "UNION ALL " & "SELECT m.*, u.nome FROM multajuros m, unida u " & "WHERE m.zona = u.zona " & "AND m.livro = u.livro " & "AND m.conta = u.conta " & "AND m.codigo = '" & grid.get_TextMatrix(grid.Row, 0) & "' "
		
		Call Conectar(strSql, rsConsulta)
		
		If rsConsulta.RecordCount > 0 Then
			'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"'
			txtNome.Text = UCase(Trim(IIf(IsDbNull(rsConsulta.Fields("nome").Value), "", rsConsulta.Fields("nome").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"'
			txtZona.Text = UCase(Trim(IIf(IsDbNull(rsConsulta.Fields("Zona").Value), "", rsConsulta.Fields("Zona").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"'
			txtLivro.Text = UCase(Trim(IIf(IsDbNull(rsConsulta.Fields("Livro").Value), "", rsConsulta.Fields("Livro").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"'
			txtConta.Text = UCase(Trim(IIf(IsDbNull(rsConsulta.Fields("Conta").Value), "", rsConsulta.Fields("Conta").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"'
			txtNf.Text = VB6.Format(IIf(IsDbNull(rsConsulta.Fields("numNf").Value), "", rsConsulta.Fields("numNf").Value), "000000")
			'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"'
			txtMulta.Text = VB6.Format(IIf(IsDbNull(rsConsulta.Fields("vlrMulta").Value), "", rsConsulta.Fields("vlrMulta").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"'
			txtJuros.Text = VB6.Format(IIf(IsDbNull(rsConsulta.Fields("vlrJuros").Value), "", rsConsulta.Fields("vlrJuros").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"'
			txtCM.Text = VB6.Format(IIf(IsDbNull(rsConsulta.Fields("vlrCM").Value), "", rsConsulta.Fields("vlrCM").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"'
			txtDias.Text = UCase(Trim(IIf(IsDbNull(rsConsulta.Fields("diasAtraso").Value), "", rsConsulta.Fields("diasAtraso").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"'
			chkIsento.CheckState = CShort(UCase(Trim(IIf(IsDbNull(rsConsulta.Fields("isento").Value), "", rsConsulta.Fields("isento").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"'
			txtUsuario.Text = Trim(IIf(IsDbNull(rsConsulta.Fields("usuario").Value), "", rsConsulta.Fields("usuario").Value))
		End If
		If rsConsulta.State = 1 Then rsConsulta.Close()
		cmdSalvar.Enabled = False
	End Sub
End Class