Option Strict Off
Option Explicit On
Imports fatd_faturamento.ENTIDADE
Imports fatd_faturamento.BLL.Repositorio
Friend Class frmIsento
	Inherits System.Windows.Forms.Form

	Private strSql As String
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private dt, dt1, dt2, dt3 As DataTable

	'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
		Try
			cmdSalvar.Enabled = True
			If chkReCobrar.CheckState = 1 Then
				txtUsuario.Text = vUsuario.LOGIN
			Else
				txtUsuario.Text = ""
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkReCobrar_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmIsento_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			'Seta Mês e Ano atual no filtro
			txtMesAno.Text = Mid(CStr(Now), 4, 7)

			Call formaGrid()
			cmdSalvar.Enabled = False
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmIsento_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	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
		Try
			'Habilita botão Salvar
			cmdSalvar.Enabled = True

			'Seta o usuário
			If chkIsento.CheckState = 1 Then
				txtUsuario.Text = vUsuario.LOGIN
			Else
				txtUsuario.Text = ""
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkIsento_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Try
			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()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdFechar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdPesquisar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPesquisar.Click
		Try
			If txtMesAno.Text = "" Then
				txtMesAno.Focus()
				Exit Sub
			End If

			Call formaGrid()
			Call EncheGrid()
			Call BuscaDados()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdPesquisar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		Try
			If chkIsento.CheckState = 1 Then
				strSql = "UPDATE multajuros SET isento = 1,  usuario = """ & txtUsuario.Text & """ WHERE codigo = '" & grid.get_TextMatrix(grid.Row, 0) & "'  "
				objExecAtualizaBancoDados.QUERY_NAME = strSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			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) & "'  "
				objExecAtualizaBancoDados.QUERY_NAME = strSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If
			cmdSalvar.Enabled = False
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdSalvar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub formaGrid()
		Try
			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")
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: formaGrid" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub EncheGrid()
		Try
			'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, uc_faturamento 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
			objAtualizaBancoDados.QUERY_NAME = strSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			'Call Conectar(strSql, rsConsulta)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					grid.Rows = grid.Rows + 1
					grid.set_TextMatrix(grid.Rows - 1, 0, IIf(IsDBNull(row.Item("codigo")), 0, row.Item("codigo")))
					grid.set_TextMatrix(grid.Rows - 1, 1, IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")))
					'rsConsulta.MoveNext()
				Next
			Else
				grid.Clear()
				Call formaGrid()
			End If
			'If rsConsulta.State = 1 Then rsConsulta.Close()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: EncheGrid" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub grid_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grid.ClickEvent
		Try
			Call BuscaDados()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: grid_ClickEvent" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	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
		Try
			Call BuscaDados()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: grid_EnterCell" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub grid_LeaveCell(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grid.LeaveCell
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: grid_LeaveCell" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtFiltroConta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroConta.Enter
		Try
			With txtFiltroConta
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtFiltroConta_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtFiltroConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroConta.Leave
		Try
			txtFiltroConta.Text = VB6.Format(txtFiltroConta.Text, "000000")
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtFiltroConta_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtFiltroLivro_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroLivro.Enter
		Try
			With txtFiltroLivro
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtFiltroLivro_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtFiltroLivro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroLivro.Leave
		Try
			txtFiltroLivro.Text = VB6.Format(txtFiltroLivro.Text, "00")
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtFiltroLivro_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtFiltroZona_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroZona.Enter
		Try
			With txtFiltroZona
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtFiltroZona_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtFiltroZona_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtFiltroZona.Leave
		Try
			txtFiltroZona.Text = VB6.Format(txtFiltroZona.Text, "000")
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtFiltroZona_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtMesAno_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAno.Enter
		Try
			With txtMesAno
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtMesAno_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtMesAno_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAno.Leave
		Try
			txtMesAno.Text = FormataData(txtMesAno, 1)
			If Len(Trim(txtMesAno.Text)) = 0 Then
				txtMesAno.Text = GetConfig("mes.ano.faturamento")
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtMesAno_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmIsento_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmIsento_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub BuscaDados()
		'Dim rsConsulta As New ADODB.Recordset
		Try
			strSql = "SELECT m.*, u.nome FROM multajuros m, uc_faturamento 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) & "' "
			objAtualizaBancoDados.QUERY_NAME = strSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			'Call Conectar(strSql, rsConsulta)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					txtNome.Text = UCase(Trim(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome"))))
					txtZona.Text = UCase(Trim(IIf(IsDBNull(row.Item("Zona")), "", row.Item("Zona"))))
					txtLivro.Text = UCase(Trim(IIf(IsDBNull(row.Item("Livro")), "", row.Item("Livro"))))
					txtConta.Text = UCase(Trim(IIf(IsDBNull(row.Item("Conta")), "", row.Item("Conta"))))
					txtNf.Text = VB6.Format(IIf(IsDBNull(row.Item("numNf")), "", row.Item("numNf")), "000000")
					txtMulta.Text = VB6.Format(IIf(IsDBNull(row.Item("vlrMulta")), "", row.Item("vlrMulta")), "standard")
					txtJuros.Text = VB6.Format(IIf(IsDBNull(row.Item("vlrJuros")), "", row.Item("vlrJuros")), "standard")
					txtCM.Text = VB6.Format(IIf(IsDBNull(row.Item("vlrCM")), "", row.Item("vlrCM")), "standard")
					txtDias.Text = UCase(Trim(IIf(IsDBNull(row.Item("diasAtraso")), "", row.Item("diasAtraso"))))
					chkIsento.CheckState = CShort(UCase(Trim(IIf(IsDBNull(row.Item("isento")), "", row.Item("isento")))))
					txtUsuario.Text = Trim(IIf(IsDBNull(row.Item("usuario")), "", row.Item("usuario")))
				Next
			End If

			cmdSalvar.Enabled = False
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaDados" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

End Class