Option Strict Off
Option Explicit On
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmConfirmaCorte
	Inherits System.Windows.Forms.Form
	
	Private ilngConsId As Integer
	Private ilngUnidId As Integer
	Private istrGrupo As String
	'Private irstReaviso As ADODB.Recordset
	Private ifrmLocalizaUnidade As frmLocalizaUnidade
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private dt As DataTable
	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private Sub cmdAnterior_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAnterior.Click
		Try
			If CInt(Me.lblRegMax.Text) > 0 Then
				If CInt(Me.lblRegAtu.Text) > 1 Then
					Me.lblRegAtu.Text = CStr(CDbl(Me.lblRegAtu.Text) - 1)
					'Call irstReaviso.MovePrevious()
				End If
			End If
			Call VerificaSalvar()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdAnterior_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdBuscar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscar.Click
		Dim lblnReaviso As Boolean
		Dim lstrSql As String
		Dim lstrCpfCnpj As String
		Dim lstrRgIe As String
		Try
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Me.cmdSalvar.Enabled = False
			Call LimpaCamposConsumidor()
			If Len(Trim(Me.txtZona.Text)) > 0 And Len(Trim(Me.txtLivro.Text)) > 0 And Len(Trim(Me.txtConta.Text)) > 0 Then
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `nome`, "
				lstrSql = lstrSql & "       `end`, "
				lstrSql = lstrSql & "       `numero`, "
				lstrSql = lstrSql & "       `complement`, "
				lstrSql = lstrSql & "       `bairro`, "
				lstrSql = lstrSql & "       `cidade`, "
				lstrSql = lstrSql & "       `cep`, "
				lstrSql = lstrSql & "       `uf`, "
				lstrSql = lstrSql & "       `distrito`, "
				lstrSql = lstrSql & "       `lote`, "
				lstrSql = lstrSql & "       `quadra`, "
				lstrSql = lstrSql & "       `cons_id` , "
				lstrSql = lstrSql & "       `id`, "
				lstrSql = lstrSql & "       `cpf_cnpj`, "
				lstrSql = lstrSql & "       `rg`, "
				lstrSql = lstrSql & "       `insc`, "
				lstrSql = lstrSql & "       `situacao` "
				lstrSql = lstrSql & "  FROM `uc_faturamento` "
				lstrSql = lstrSql & " WHERE `zona`  = '" & Me.txtZona.Text & "' "
				lstrSql = lstrSql & "   AND `livro` = '" & Me.txtLivro.Text & "' "
				lstrSql = lstrSql & "   AND `conta` = '" & Me.txtConta.Text & "' "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt.Rows.Count > 0 Then
					For Each row As DataRow In dt.Rows
						Me.lblNomeUC.Text = " " & IIf(IsDBNull(row.Item("nome")), "Nome em branco!!!", row.Item("nome"))
						Me.lblEndereco.Text = " " & IIf(IsDBNull(row.Item("end")), "", row.Item("end"))
						Me.lblNumero.Text = IIf(IsDBNull(row.Item("numero")), "", row.Item("numero"))
						Me.lblComplement.Text = " " & IIf(IsDBNull(row.Item("complement")), "", row.Item("complement"))
						Me.lblBairro.Text = " " & IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro"))
						Me.lblCidade.Text = " " & IIf(IsDBNull(row.Item("cidade")), "", row.Item("cidade"))
						Me.lblLote.Text = IIf(IsDBNull(row.Item("lote")), "", row.Item("lote"))
						Me.lblQuadra.Text = IIf(IsDBNull(row.Item("quadra")), "", row.Item("quadra"))
						Me.lblCep.Text = IIf(IsDBNull(row.Item("cep")), "", row.Item("cep"))
						Me.lblUf.Text = IIf(IsDBNull(row.Item("uf")), "", row.Item("uf"))
						Me.lblDistrito.Text = " " & IIf(IsDBNull(row.Item("distrito")), "", row.Item("distrito"))
						lstrCpfCnpj = IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj"))
						If Len(lstrCpfCnpj) <= 11 Then
							Me.lblCpfCnpj.Text = FormataCPF(lstrCpfCnpj)
							Me.lblRgIe.Text = IIf(IsDBNull(row.Item("rg")), "", row.Item("rg"))
						Else
							Me.lblCpfCnpj.Text = FormataCNPJ(lstrCpfCnpj)
							Me.lblRgIe.Text = IIf(IsDBNull(row.Item("insc")), "", row.Item("insc"))
						End If
						Me.lblSituacao.Text = IIf(IsDBNull(row.Item("situacao")), "L", row.Item("situacao"))
						Select Case Me.lblSituacao.Text
							Case Is = "L"
								Me.lblSituacao.Text = "LIGADO"
							Case Is = "D"
								Me.lblSituacao.Text = "DESLIGADO"
							Case Is = "C"
								Me.lblSituacao.Text = "SUSPENDIDO"
							Case Is = "I"
								Me.lblSituacao.Text = "DESATIVADO"
							Case Else
								Me.lblSituacao.Text = "NÃO LIGADO"
						End Select
						ilngConsId = CInt(IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id")))
						ilngUnidId = CInt(IIf(IsDBNull(row.Item("id")), 0, row.Item("id")))
						lblnReaviso = BuscaDados()

						If Me.lblRegMax.Text <> "0" Then
							Call VerificaSalvar()
						End If
					Next
				Else
					Me.Cursor = System.Windows.Forms.Cursors.Default
					Call MsgBox("O identificar " & Me.txtZona.Text & "." & Me.txtLivro.Text & "." & Me.txtConta.Text & ", não foi encontrado", MsgBoxStyle.Information, gstrTituloMensagem)
					Call LimpaCamposConsumidor()
					Me.txtZona.Focus()
					Me.cmdSalvar.Enabled = False
				End If

			Else
				Me.Cursor = System.Windows.Forms.Cursors.Default
				Call MsgBox("Faltou informar o identificador!!!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call LimpaCamposConsumidor()
				Call Me.txtZona.Focus()
			End If
			Me.Cursor = System.Windows.Forms.Cursors.Default
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdBuscar_Click" & "", "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
		Call Me.Close()
	End Sub

	Private Sub cmdLocalizarUnidadeConsumidora_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizarUnidadeConsumidora.Click
		Try
			'With ifrmLocalizaUnidade
			Call frmLocalizaUnidade.Show()
			Call frmLocalizaUnidade.SetaLocalizaUnidade("U", Me)
			'	End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdLocalizarUnidadeConsumidora_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdPrimeiro_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPrimeiro.Click
		Try
			If CInt(Me.lblRegMax.Text) > 0 Then
				Me.lblRegAtu.Text = CStr(1)
				'Call irstReaviso.MoveFirst()
			End If
			Call VerificaSalvar()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdPrimeiro_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdProximo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProximo.Click
		Try
			If CInt(Me.lblRegMax.Text) > 0 Then
				If CInt(Me.lblRegAtu.Text) < CInt(Me.lblRegMax.Text) Then
					Me.lblRegAtu.Text = CStr(CDbl(Me.lblRegAtu.Text) + 1)
					'Call irstReaviso.MoveNext()
				End If
			End If
			Call VerificaSalvar()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProximo_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
		Dim lblnAux As Boolean
		Dim lstrSql As String
		Try
			lblnAux = True
			If Trim(Me.txtLeitura.Text) = "" Then
				lblnAux = MsgBox("Você esqueceu de informar a leitura!" & vbCrLf & "Deseja continuar mesmo assim?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, gstrTituloMensagem) = MsgBoxResult.Yes
			End If
			If lblnAux Then
				lstrSql = ""
				lstrSql = lstrSql & "UPDATE `concorte` "
				lstrSql = lstrSql & "   SET `concorte`.`datacorte` = """ & VB6.Format(Date.Now, "yyyy-MM-dd hh:mm:ss") & """, "
				If IsNumeric(Me.txtLeitura.Text) Then
					lstrSql = lstrSql & "       `concorte`.`leitura`   = " & Replace(Me.txtLeitura.Text, ",", ".") & ", "
				End If
				lstrSql = lstrSql & "       `concorte`.`sit`       = if(`concorte`.`sit`='P',`concorte`.`sit`,'C') "
				lstrSql = lstrSql & " WHERE `concorte`.`numnf`   = """ & Replace(Me.lblNumNf.Text, ".", "") & """ "
				lstrSql = lstrSql & "   AND `concorte`.`mesano`  = """ & Me.lblMesAno.Text & """ "
				lstrSql = lstrSql & "   AND `concorte`.`zona`    = """ & Me.txtZona.Text & """ "
				lstrSql = lstrSql & "   AND `concorte`.`livro`   = """ & Me.txtLivro.Text & """ "
				lstrSql = lstrSql & "   AND `concorte`.`conta`   = """ & Me.txtConta.Text & """ "
				lstrSql = lstrSql & "   AND `concorte`.`unid_id` = " & ilngUnidId
				lstrSql = lstrSql & "   AND `concorte`.`cons_id` = " & ilngConsId
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

				Call AtualizaHistoricoUnidade(ilngUnidId, istrGrupo, "CORTADO PELA FATURA: " & Me.lblNumNf.Text & " " & Me.lblMesAno.Text & " EM: " & VB6.Format(Me.dtpCorte.Value, "dd/mm/yyyy") & " LEITURA: " & Me.txtLeitura.Text & IIf(Me.chkDisjuntor.CheckState = 1, " DESLIGADO NO DISJUNTOR", ""))
				Call AtualizaSituacaoUnidade(ilngUnidId, istrGrupo, "C", Me.dtpCorte.Value)
				Call AtualizaSituacaoUnidadeLC(ilngUnidId, istrGrupo, "C", Me.dtpCorte.Value)
				If IsNumeric(Me.txtLeitura.Text) Then
					lstrSql = "UPDATE uc_faturamento SET "
					lstrSql = lstrSql & "       `ult_leit_corte`   = " & Replace(Me.txtLeitura.Text, ",", ".") & " "
					lstrSql = lstrSql & " WHERE `zona`    = """ & Me.txtZona.Text & """ "
					lstrSql = lstrSql & "   AND `livro`   = """ & Me.txtLivro.Text & """ "
					lstrSql = lstrSql & "   AND `conta`   = """ & Me.txtConta.Text & """ "
					lstrSql = lstrSql & "   AND `id` = " & ilngUnidId
					lstrSql = lstrSql & "   AND `cons_id` = " & ilngConsId
					objExecAtualizaBancoDados.QUERY_NAME = lstrSql
					repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
				End If
				Call MsgBox("Unidade consumidora cortada com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call cmdBuscar_Click(cmdBuscar, New System.EventArgs())
				Me.txtLeitura.Text = ""
				Me.chkDisjuntor.CheckState = System.Windows.Forms.CheckState.Unchecked
			End If
		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 cmdUltimo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdUltimo.Click
		Try
			If CInt(Me.lblRegMax.Text) > 0 Then
				If CInt(Me.lblRegAtu.Text) < CInt(Me.lblRegMax.Text) Then
					'Me.lblRegAtu.Text = CStr(irstReaviso.RecordCount)
					'Call irstReaviso.MoveLast()
				End If
			End If
			Call VerificaSalvar()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdUltimo_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmConfirmaCorte_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			Select Case KeyAscii
				Case System.Windows.Forms.Keys.Return
					TabEnter(KeyAscii, eventArgs)
				Case System.Windows.Forms.Keys.Escape
					Call cmdFechar_Click(cmdFechar, New System.EventArgs())
			End Select
			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: frmConfirmaCorte_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmConfirmaCorte_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			istrGrupo = IIf(Me.opbGrupoB.Checked, "B", "A")
			Me.dtpCorte.Value = Date.Now
			ilngConsId = 0
			ilngUnidId = 0
			Me.dtpCorte.Enabled = False
			MontaGrid()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmConfirmaCorte_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub lblSitReaviso_Change()
		Try
			Select Case Me.lblSitReaviso.Text
				Case Is = "R"
					Me.lblEstadoReaviso.Text = "Emitido reaviso"
				Case Is = "E"
					Me.lblEstadoReaviso.Text = "Entregue reaviso"
				Case Is = "C"
					Me.lblEstadoReaviso.Text = "U.C. SUSPENDIDA"
				Case Is = "P"
					Me.lblEstadoReaviso.Text = "Efetivado pagamento"
				Case Else
					Me.lblEstadoReaviso.Text = "Status Indefinido"
			End Select
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: lblSitReaviso_Change" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub opbGrupoA_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbGrupoA.CheckedChanged
		Try
			If eventSender.Checked Then
				istrGrupo = "A"
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: opbGrupoA_CheckedChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub


	Private Sub opbGrupoB_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbGrupoB.CheckedChanged
		Try
			If eventSender.Checked Then
				istrGrupo = "B"
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: opbGrupoB_CheckedChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

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

	Private Sub txtConta_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtConta.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			If KeyAscii = System.Windows.Forms.Keys.Return Then
				'Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
				KeyAscii = 0
			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: txtConta_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.Leave
		Try
			Me.txtConta.Text = VB6.Format(Me.txtConta.Text, "000000")
			If Len(Trim(Me.txtZona.Text)) > 0 And Len(Trim(Me.txtLivro.Text)) > 0 And Len(Trim(Me.txtConta.Text)) > 0 Then
				Call cmdBuscar_Click(cmdBuscar, New System.EventArgs())
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtConta_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

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

	Private Sub txtLivro_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtLivro.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			If KeyAscii = System.Windows.Forms.Keys.Return Then
				'Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
				KeyAscii = 0
			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: txtLivro_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub txtLivro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLivro.Leave
		Me.txtLivro.Text = VB6.Format(Me.txtLivro.Text, "00")
	End Sub

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

	Private Sub txtZona_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtZona.KeyPress
		Try
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = System.Windows.Forms.Keys.Return Then
				'Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
				KeyAscii = 0
			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: txtZona_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtZona_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtZona.Leave
		Me.txtZona.Text = VB6.Format(Me.txtZona.Text, "000")
	End Sub

	Private Sub VerificaSalvar()
		Try
			If IsDate(Me.lblDtPrazo.Text) Then
				If CDate(Me.lblDtPrazo.Text) < gDtBcoDados Then
					Me.lblDtPrazo.Font = VB6.FontChangeBold(Me.lblDtPrazo.Font, True)
					If Trim(Me.lblSituacao.Text) = "LIGADO" Then
						Me.lblSituacao.Font = VB6.FontChangeBold(Me.lblSituacao.Font, True)
						If Trim(Me.lblSitReaviso.Text) = "E" Or (Trim(Me.lblSitReaviso.Text) = "P" And Trim(Me.lblDtCorte.Text) = "" And Trim(Me.lblDtEntrega.Text) <> "") Then
							Me.cmdSalvar.Enabled = True
						Else
							Me.cmdSalvar.Enabled = False
						End If
					Else
						Me.cmdSalvar.Enabled = False
						Me.lblSituacao.Font = VB6.FontChangeBold(Me.lblSituacao.Font, False)
					End If
				Else
					Me.cmdSalvar.Enabled = False
					Me.lblDtPrazo.Font = VB6.FontChangeBold(Me.lblDtPrazo.Font, False)
				End If

				Me.dtpCorte.Enabled = Me.cmdSalvar.Enabled
				If Me.dtpCorte.Enabled Then
					Me.dtpCorte.Value = CDate(Me.lblDtPrazo.Text)
				Else
					Me.dtpCorte.Value = Date.Now()
				End If
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaSalvar" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub LimpaCamposConsumidor()
		Try
			Me.lblNomeUC.Text = ""
			Me.lblEndereco.Text = ""
			Me.lblNumero.Text = ""
			Me.lblComplement.Text = ""
			Me.lblBairro.Text = ""
			Me.lblCidade.Text = ""
			Me.lblCep.Text = ""
			Me.lblUf.Text = ""
			Me.lblDistrito.Text = ""
			Me.lblLote.Text = ""
			Me.lblQuadra.Text = ""
			Me.lblCpfCnpj.Text = ""
			Me.lblRgIe.Text = ""
			Me.lblSituacao.Text = ""
			Me.txtLeitura.Text = ""
			Me.chkDisjuntor.CheckState = System.Windows.Forms.CheckState.Unchecked
			ilngConsId = 0
			ilngUnidId = 0
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LimpaCamposConsumidor" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Function BuscaDados() As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim iNumNF As Integer
		Dim sMesAno As String
		Dim sdtReaviso As String
		Dim sdtPrazo As String
		Dim sdtEntrega As String
		Dim sitReaviso As String
		Dim sdtVencto As String
		Dim dvalor As Double
		Dim sdtPagto As String
		Dim sClasse As String
		Dim sPedidoPrazo As String
		Dim sNomeReaviso As String
		lblnRet = False
		Try

			If ilngConsId > 0 And ilngUnidId > 0 Then
				'If irstReaviso.State = ADODB.ObjectStateEnum.adStateOpen Then Call irstReaviso.Close()
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `concorte`.`numnf`, "
				lstrSql = lstrSql & "       `concorte`.`mesano`, "
				lstrSql = lstrSql & "       `concorte`.`datareav`, "
				lstrSql = lstrSql & "       `concorte`.`prazoreav`, "
				lstrSql = lstrSql & "       `concorte`.`dataentrega`, "
				lstrSql = lstrSql & "       `concorte`.`sit`, "
				lstrSql = lstrSql & "       `concorte`.`datavenc`, "
				lstrSql = lstrSql & "       `concorte`.`valor`, "
				lstrSql = lstrSql & "       `concorte`.`datapag`, "
				lstrSql = lstrSql & "       `concorte`.`datacorte`, "
				lstrSql = lstrSql & "       `concorte`.`dataprazo` "
				lstrSql = lstrSql & "  FROM `concorte` "
				lstrSql = lstrSql & " WHERE `concorte`.`zona`    = '" & Me.txtZona.Text & "' "
				lstrSql = lstrSql & "   AND `concorte`.`livro`   = '" & Me.txtLivro.Text & "' "
				lstrSql = lstrSql & "   AND `concorte`.`conta`   = '" & Me.txtConta.Text & "' "
				lstrSql = lstrSql & "   AND `concorte`.`cons_id` = " & ilngConsId & " "
				lstrSql = lstrSql & "   AND `concorte`.`unid_id` = " & ilngUnidId & " "
				lstrSql = lstrSql & " ORDER BY `concorte`.`prazoreav` DESC "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt.Rows.Count > 0 Then
					lblnRet = True
					Me.lblRegMin.Text = CStr(1)
					Me.lblRegMax.Text = CStr(dt.Rows.Count)
					Me.lblRegAtu.Text = CStr(1)

					For Each row As DataRow In dt.Rows
						iNumNF = IIf(IsDBNull(row.Item("numnf")), "", row.Item("numnf"))
						sMesAno = IIf(IsDBNull(row.Item("mesano")), "", row.Item("mesano"))
						sdtReaviso = IIf(IsDBNull(row.Item("datareav")), "", row.Item("datareav"))
						sdtPrazo = IIf(IsDBNull(row.Item("prazoreav")), "", row.Item("prazoreav"))
						sdtEntrega = IIf(IsDBNull(row.Item("dataentrega")), "", row.Item("dataentrega"))
						sitReaviso = IIf(IsDBNull(row.Item("sit")), "", row.Item("sit"))
						sdtVencto = IIf(IsDBNull(row.Item("datavenc")), "", row.Item("datavenc"))
						dvalor = IIf(IsDBNull(row.Item("valor")), "", row.Item("valor"))
						sdtPagto = IIf(IsDBNull(row.Item("datapag")), "", row.Item("datapag"))
						'sClasse = IIf(IsDBNull(row.Item("codclas")), "", row.Item("codclas"))
						sPedidoPrazo = IIf(IsDBNull(row.Item("dataprazo")), "", row.Item("dataprazo"))
						'sNomeReaviso = IIf(IsDBNull(row.Item("nome")), "", row.Item("nome"))


						With Me.msfReaviso
							'.Rows = .Rows + 1
							.FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
							.set_TextMatrix(.Rows - 1, 0, iNumNF)
							.set_TextMatrix(.Rows - 1, 1, sMesAno)
							.set_TextMatrix(.Rows - 1, 2, sitReaviso)
							'.set_TextMatrix(.Rows - 1, 3, sitReaviso)
							.set_TextMatrix(.Rows - 1, 3, VB6.Format(sdtReaviso, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 4, VB6.Format(sdtPrazo, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 5, VB6.Format(sdtEntrega, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 6, VB6.Format(sPedidoPrazo, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 7, VB6.Format(sdtVencto, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 8, VB6.Format(sdtPagto, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 9, dvalor)
						End With
					Next
				Else
					lblnRet = False
					Me.lblRegMin.Text = CStr(0)
					Me.lblRegMax.Text = CStr(0)
					Me.lblRegAtu.Text = CStr(0)
				End If

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

	Public Sub SetaCodigo(ByVal alngUnidId As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal astrGrupo As String)
		Try
			Me.txtZona.Text = astrZona
			Me.txtLivro.Text = astrLivro
			Me.txtConta.Text = astrConta
			'ilngConsId = alngConsId
			ilngUnidId = alngUnidId
			istrGrupo = astrGrupo
			Call cmdBuscar_Click(cmdBuscar, New System.EventArgs())
			If astrGrupo = "A" Then
				Me.opbGrupoA.Checked = True
			ElseIf astrGrupo = "B" Then
				Me.opbGrupoB.Checked = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: SetaCodigo" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub MontaGrid()
		Try
			With Me.msfReaviso
				.Cols = 10
				'.Rows = 1
				'.FixedRows = 1
				.Redraw = False
				.set_ColWidth(0, 800)
				.set_ColWidth(1, 900)
				.set_ColWidth(2, 1000)
				.set_ColWidth(3, 1200)
				.set_ColWidth(4, 1200)
				.set_ColWidth(5, 1200)
				.set_ColWidth(6, 1200)
				.set_ColWidth(7, 1200)
				.set_ColWidth(8, 1200)
				.set_ColWidth(9, 1200)
				.set_TextMatrix(0, 0, "Fatura")
				.set_TextMatrix(0, 1, "Mês\Ano")
				.set_TextMatrix(0, 2, "Situação")
				.set_TextMatrix(0, 3, "Reaviso")
				.set_TextMatrix(0, 4, "Prazo")
				.set_TextMatrix(0, 5, "Entrega")
				.set_TextMatrix(0, 6, "Pedido Prazo")
				.set_TextMatrix(0, 7, "Vencimento")
				.set_TextMatrix(0, 8, "Pagamento")
				.set_TextMatrix(0, 9, "Valor")


				.Redraw = True
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaGrid" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub msfReaviso_ClickEvent(sender As Object, e As EventArgs) Handles msfReaviso.ClickEvent
		Try

			Dim numnf
			Dim mesano
			Dim datareav
			Dim prazoreav
			Dim dataentrega
			Dim sit
			Dim datavenc
			Dim valor
			Dim datapag
			Dim codclas
			Dim dataprazo
			Dim nome

			With msfReaviso

				numnf = .get_TextMatrix(.RowSel, 0)
				mesano = .get_TextMatrix(.RowSel, 1)
				sit = .get_TextMatrix(.RowSel, 2)
				datareav = .get_TextMatrix(.RowSel, 3)
				prazoreav = .get_TextMatrix(.RowSel, 4)
				dataentrega = .get_TextMatrix(.RowSel, 5)
				dataprazo = .get_TextMatrix(.RowSel, 6)
				datavenc = .get_TextMatrix(.RowSel, 7)
				datapag = .get_TextMatrix(.RowSel, 8)
				valor = .get_TextMatrix(.RowSel, 9)
				'valor = .get_TextMatrix(.RowSel, 10)


				Me.lblNumNf.Text = numnf
				Me.lblMesAno.Text = mesano
				Me.lblDtReaviso.Text = datareav
				Me.lblDtPrazo.Text = prazoreav
				Me.lblDtEntrega.Text = dataentrega
				Me.lblSitReaviso.Text = sit
				Me.lblDtVecto.Text = datavenc
				Me.lblValor.Text = valor
				Me.lblDtPagto.Text = datapag
				'Me.lblClasse.Text = codclas
				Me.lblPedidoPrazo.Text = dataprazo
				'Me.lblNomeReaviso.Text = nome

				Call VerificaSalvar()

			End With

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


End Class