Option Strict Off
Option Explicit On
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE

Friend Class frmConfirmaCorteEspecial
	Inherits System.Windows.Forms.Form

	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
	'Private Declare Function SHCreateThread Lib "shlwapi.dll" (ByVal pfnThreadProc As Integer, ByRef pData As Any, ByVal dwFlags As Integer, ByVal pfnCallback As Integer) As Integer

	Private idteBase As Date
	Private ilngConsId As Integer
	Private ilngUnidId As Integer
	Private idblRegNum As Double
	Private istrGrupo As String
	Private ifrmLocalizaUnidade As frmLocalizaUnidade
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private dt As DataTable

	Private Sub cmdBuscar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscar.Click
		'Busca os dados da unidade consumidora
		Dim lstrSql As String
		Dim lstrCpfCnpj As String
		Dim lstrRgIe As String

		Try
			If Me.optGrupoB.Checked Then
				istrGrupo = "B"
			Else
				istrGrupo = "A"
			End If
			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 `u`.`nome`, "
				lstrSql = lstrSql & "       `u`.`end`, "
				lstrSql = lstrSql & "       `u`.`numero`, "
				lstrSql = lstrSql & "       `u`.`complement`, "
				lstrSql = lstrSql & "       `u`.`bairro`, "
				lstrSql = lstrSql & "       `u`.`cidade`, "
				lstrSql = lstrSql & "       `u`.`cep`, "
				lstrSql = lstrSql & "       `u`.`uf`, "
				lstrSql = lstrSql & "       `u`.`distrito`, "
				lstrSql = lstrSql & "       `u`.`lote`, "
				lstrSql = lstrSql & "       `u`.`quadra`, "
				lstrSql = lstrSql & "       `u`.`cons_id` , "
				lstrSql = lstrSql & "       `u`.`id`, "
				lstrSql = lstrSql & "       `u`.`cpf_cnpj`, "
				lstrSql = lstrSql & "       `u`.`rg`, "
				lstrSql = lstrSql & "       `u`.`insc`, "
				lstrSql = lstrSql & "       `u`.`situacao` "
				lstrSql = lstrSql & "  FROM `uc_faturamento` AS `u` "
				lstrSql = lstrSql & " WHERE `u`.`zona`  = """ & Me.txtZona.Text & """ "
				lstrSql = lstrSql & "   AND `u`.`livro` = """ & Me.txtLivro.Text & """ "
				lstrSql = lstrSql & "   AND `u`.`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.lblCpf.Text = FormataCPF(lstrCpfCnpj)
							Me.lblRg.Text = IIf(IsDBNull(row.Item("rg")), "", row.Item("rg"))
						Else
							Me.lblCnj.Text = FormataCNPJ(lstrCpfCnpj)
							Me.lblIe.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 = "CORTADO"
							Case Is = "I"
								Me.lblSituacao.Text = "DESATIVADO"
							Case Else
								Me.lblSituacao.Text = "NÃO LIGADO"
						End Select
						ilngConsId = IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id"))
						ilngUnidId = IIf(IsDBNull(row.Item("id")), 0, row.Item("id"))
						Call BuscaDados()
					Next
				Else
					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
				Call MsgBox("Faltou informar o identificador!!!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call LimpaCamposConsumidor()
				Call Me.txtZona.Focus()
			End If

		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 cmdProc_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProc.Click
		Try
			Call frmLocalizaUnidade.Show()
			Call frmLocalizaUnidade.SetaLocalizaUnidade("U", Me, istrGrupo)
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProc_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 lstrSql As String
		Try
			If Me.dtpCorte.Value > CDate(Me.lblDtPagto.Text) Then
				Call MsgBox("A data do corte não pode ser superior a data de pagamento!!!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			Else
				lstrSql = ""
				lstrSql = lstrSql & "UPDATE `concorte` "
				lstrSql = lstrSql & "   SET `datacorte` = '" & VB6.Format(Me.dtpCorte.Value, "yyyy-MM-dd") & "' "
				lstrSql = lstrSql & " WHERE `regnum`  = " & idblRegNum & " "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

				Call AtualizaHistoricoUnidade(ilngUnidId, istrGrupo, "CORTADO PELA FATURA: " & Me.lblNumNf.Text & " " & Me.lblMesAno.Text & " EM: " & VB6.Format(idteBase, "dd/MM/yyyy") & " CORTE: " & VB6.Format(Me.dtpCorte.Value, "dd/MM/yyyy"))
				Call AtualizaSituacaoUnidade(ilngUnidId, istrGrupo, "C", Me.dtpCorte.Value)
				Call AtualizaSituacaoUnidadeLC(ilngUnidId, istrGrupo, "C", Me.dtpCorte.Value)
				Call MsgBox("Unidade consumidora cortada com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call cmdBuscar_Click(cmdBuscar, New System.EventArgs())
			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 cmdVoltar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdVoltar.Click
		Call LigDes(True)
	End Sub
	
	Private Sub frmConfirmaCorteEspecial_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: frmConfirmaCorteEspecial_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmConfirmaCorteEspecial_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			Me.Height = VB6.TwipsToPixelsY(6000)
			'Call CenterForm(Me)
			'ifrmLocalizaUnidade = New frmLocalizaUnidade
			'irstReaviso = New ADODB.Recordset
			idteBase = Date.Now
			Me.dtpCorte.Value = idteBase
			Call MontaGrade()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmConfirmaCorteEspecial_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub msfgDados_DblClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles msfgDados.DblClick
		Dim lblnContinua As Boolean
		Dim llngRow As Integer
		Dim lblnAux As Boolean

		Try
			lblnContinua = True

			If Me.lblSituacao.Text = "LIGADO" Then
				With Me.msfgDados
					If IsDate(.get_TextMatrix(.RowSel, 4)) Then
						Call MsgBox("Já foi registrado o corte por esta fatura!!!", MsgBoxStyle.Information, gstrTituloMensagem)
						lblnContinua = False
					End If
					'
					If lblnContinua And IsDate(.get_TextMatrix(.RowSel, 6)) Then
						If CDate(.get_TextMatrix(.RowSel, 6)) >= idteBase Then
							Call MsgBox("O prazo deste reaviso ainda não venceu!!!", MsgBoxStyle.Information, gstrTituloMensagem)
							lblnContinua = False
						End If
					End If
					'
					If lblnContinua And .RowSel > 1 Then
						lblnAux = False
						For llngRow = 1 To .RowSel - 1
							If IsDate(.get_TextMatrix(.RowSel, 4)) Then
								lblnAux = True
							End If
						Next llngRow
						If lblnAux Then
							Call MsgBox("Esta unidade foi cortada por faturas mais novas não podendo ser cortado novamente por faturas anteriores!!!", MsgBoxStyle.Information, gstrTituloMensagem)
							lblnContinua = False
						End If
					End If
					'
				End With
			Else
				lblnContinua = False
				If Me.lblSituacao.Text = "CORTADO" Then
					Call MsgBox("Esta unidade consumidora já esta com a situação cortada!!!", MsgBoxStyle.Information, gstrTituloMensagem)
				Else
					Call MsgBox("Esta unidade consumidora esta com desligada ou desativada!!!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
			End If
			If lblnContinua Then
				Me.fmeRegistra.Top = Me.fmeFaturas.Top
				idblRegNum = CDbl(Me.msfgDados.get_TextMatrix(Me.msfgDados.RowSel, 15))
				Me.lblNumNf.Text = Me.msfgDados.get_TextMatrix(Me.msfgDados.RowSel, 0)
				Me.lblMesAno.Text = Me.msfgDados.get_TextMatrix(Me.msfgDados.RowSel, 1)
				Me.lblDtPagto.Text = Me.msfgDados.get_TextMatrix(Me.msfgDados.RowSel, 3)
				Me.dtpCorte.Value = Me.msfgDados.get_TextMatrix(Me.msfgDados.RowSel, 3)
				Call LigDes(False)
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: msfgDados_DblClick" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub optGrupoA_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optGrupoA.CheckedChanged
		Try
			If eventSender.Checked Then
				istrGrupo = "A"
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: optGrupoA_CheckedChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub optGrupoB_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optGrupoB.CheckedChanged
		Try
			If eventSender.Checked Then
				istrGrupo = "B"
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: optGrupoB_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_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_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_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 LimpaCamposConsumidor()
		Try
			Me.lblNomeUC.Text = ""
			Me.lblEndereco.Text = ""
			Me.lblNumero.Text = ""
			Me.lblComplement.Text = ""
			Me.lblBairro.Text = ""
			Me.lblCidade.Text = ""
			Me.lblUf.Text = ""
			Me.lblLote.Text = ""
			Me.lblQuadra.Text = ""
			Me.lblCep.Text = ""
			Me.lblDistrito.Text = ""
			Me.lblCpf.Text = ""
			Me.lblCnj.Text = ""
			Me.lblRg.Text = ""
			Me.lblIe.Text = ""
			Me.lblSituacao.Text = ""
			ilngConsId = 0
			ilngUnidId = 0
			idblRegNum = 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 Sub BuscaDados()
		Dim ldblFatura_NumNf As Double
		Dim lstrFatura_MesAno As String
		Dim llngFatura_MesAnoRef As Integer
		Dim lstrFatura_Zona As String
		Dim lstrFatura_Livro As String
		Dim lstrFatura_Conta As String
		Dim llngFatura_ConsId As Integer
		Dim llngFatura_UnidId As Integer
		Dim lstrFatura_Grupo As String
		Dim ldteCorte_Reaviso As Date
		Dim ldteCorte_PrazoR As Date
		Dim ldteCorte_Entrega As Date
		Dim ldteCorte_Vencto As Date
		Dim ldteCorte_Pagto As Date
		Dim ldteCorte_Corte As Date
		Dim ldblCorte_Valor As Double
		Dim lstrCorte_Situacao As String
		Dim ldblCorte_RegNum As Double
		Dim lstrSql As String
		Try

			Call MontaGrade()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & "       `nf`.`numnf` `fatura_numnf`, "
			lstrSql = lstrSql & "       `nf`.`mesano` `fatura_mesano`, "
			lstrSql = lstrSql & "       `nf`.`mesano_ref` `fatura_mesanoref`, "
			lstrSql = lstrSql & "       `nf`.`zona` `fatura_zona`, "
			lstrSql = lstrSql & "       `nf`.`livro` `fatura_livro`, "
			lstrSql = lstrSql & "       `nf`.`conta` `fatura_conta`, "
			lstrSql = lstrSql & "       `nf`.`cons_id` `fatura_cons_id`, "
			lstrSql = lstrSql & "       `nf`.`unid_id` `fatura_unid_id`, "
			lstrSql = lstrSql & "       `nf`.`grupo` `fatura_grupo`, "
			lstrSql = lstrSql & "       `concorte`.`datareav` `corte_datareav`, "
			lstrSql = lstrSql & "       `concorte`.`prazoreav` `corte_prazoreav`, "
			lstrSql = lstrSql & "       `concorte`.`dataentrega` `corte_dataentrega`, "
			lstrSql = lstrSql & "       `concorte`.`datavenc` `corte_datavenc`, "
			lstrSql = lstrSql & "       `concorte`.`datapag` `corte_datapag`, "
			lstrSql = lstrSql & "       `concorte`.`datacorte` `corte_datacorte`, "
			lstrSql = lstrSql & "       `concorte`.`valor` `corte_valor`, "
			lstrSql = lstrSql & "       `concorte`.`sit` `corte_situacao`, "
			lstrSql = lstrSql & "       `concorte`.`regnum` `corte_regnum` "
			lstrSql = lstrSql & "  FROM `nf`, `concorte`  "
			lstrSql = lstrSql & " WHERE `nf`.`zona`    = '" & Me.txtZona.Text & "' "
			lstrSql = lstrSql & "   AND `nf`.`livro`   = '" & Me.txtLivro.Text & "' "
			lstrSql = lstrSql & "   AND `nf`.`conta`   = '" & Me.txtConta.Text & "' "
			lstrSql = lstrSql & "   AND `nf`.`cons_id` =  " & ilngConsId & " "
			lstrSql = lstrSql & "   AND `nf`.`unid_id` =  " & ilngUnidId & " "
			lstrSql = lstrSql & "   AND `nf`.`grupo`   = '" & istrGrupo & "' "
			lstrSql = lstrSql & "   AND (`nf`.`flgst` = 'P' OR (`nf`.`flgst`='' AND `concorte`.`sit` = 'C') ) "
			lstrSql = lstrSql & "   AND `concorte`.`numnf`   = `nf`.`numnf` "
			lstrSql = lstrSql & "   AND `concorte`.`mesano`  = `nf`.`mesano` "
			lstrSql = lstrSql & "   AND `concorte`.`zona`    = `nf`.`zona` "
			lstrSql = lstrSql & "   AND `concorte`.`livro`   = `nf`.`livro` "
			lstrSql = lstrSql & "   AND `concorte`.`conta`   = `nf`.`conta` "
			lstrSql = lstrSql & "   AND `concorte`.`unid_id` = `nf`.`unid_id` "
			lstrSql = lstrSql & "   AND `concorte`.`cons_id` = `nf`.`cons_id` "
			lstrSql = lstrSql & " ORDER BY `nf`.`mesano_ref` DESC LIMIT 0, 12"
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				Me.msfgDados.Redraw = False
				For Each row As DataRow In dt.Rows

					ldblFatura_NumNf = IIf(IsDBNull(row.Item("fatura_numnf")), 0, row.Item("fatura_numnf"))
					lstrFatura_MesAno = IIf(IsDBNull(row.Item("fatura_mesano")), "", row.Item("fatura_mesano"))
					llngFatura_MesAnoRef = IIf(IsDBNull(row.Item("fatura_mesanoref")), 0, row.Item("fatura_mesanoref"))
					lstrFatura_Zona = IIf(IsDBNull(row.Item("fatura_zona")), "", row.Item("fatura_zona"))
					lstrFatura_Livro = IIf(IsDBNull(row.Item("fatura_livro")), "", row.Item("fatura_livro"))
					lstrFatura_Conta = IIf(IsDBNull(row.Item("fatura_conta")), "", row.Item("fatura_conta"))
					llngFatura_ConsId = IIf(IsDBNull(row.Item("fatura_cons_id")), 0, row.Item("fatura_cons_id"))
					llngFatura_UnidId = IIf(IsDBNull(row.Item("fatura_unid_id")), 0, row.Item("fatura_unid_id"))
					lstrFatura_Grupo = IIf(IsDBNull(row.Item("fatura_grupo")), "B", row.Item("fatura_grupo"))
					ldteCorte_Reaviso = IIf(IsDBNull(row.Item("corte_datareav")), "1900-01-01", row.Item("corte_datareav"))
					ldteCorte_PrazoR = IIf(IsDBNull(row.Item("corte_prazoreav")), "1900-01-01", row.Item("corte_prazoreav"))
					ldteCorte_Entrega = IIf(IsDBNull(row.Item("corte_dataentrega")), "1900-01-01", row.Item("corte_dataentrega"))
					ldteCorte_Vencto = IIf(IsDBNull(row.Item("corte_datavenc")), "1900-01-01", row.Item("corte_datavenc"))
					ldteCorte_Pagto = IIf(IsDBNull(row.Item("corte_datapag")), "1900-01-01", row.Item("corte_datapag"))
					ldteCorte_Corte = IIf(IsDBNull(row.Item("corte_datacorte")), "1900-01-01", row.Item("corte_datacorte"))
					ldblCorte_Valor = IIf(IsDBNull(row.Item("corte_valor")), 0, row.Item("corte_valor"))
					lstrCorte_Situacao = IIf(IsDBNull(row.Item("corte_situacao")), "", row.Item("corte_situacao"))
					ldblCorte_RegNum = IIf(IsDBNull(row.Item("corte_regnum")), 0, row.Item("corte_regnum"))
					With Me.msfgDados
						.Rows = .Rows + 1
						.set_TextMatrix(.Rows - 1, 0, VB6.Format(ldblFatura_NumNf, "###,#0"))
						.set_TextMatrix(.Rows - 1, 1, lstrFatura_MesAno)
						.set_TextMatrix(.Rows - 1, 2, VB6.Format(ldteCorte_Vencto, "dd/MM/yyyy"))
						If lstrCorte_Situacao = "C" Then
							.set_TextMatrix(.Rows - 1, 3, VB6.Format(BuscaDataBD, "dd/MM/yyyy"))
						Else
							.set_TextMatrix(.Rows - 1, 3, VB6.Format(ldteCorte_Pagto, "dd/MM/yyyy"))
						End If
						If ldteCorte_Corte <> CDate("01/01/1900") Then
							If lstrCorte_Situacao = "C" Then
								.set_TextMatrix(.Rows - 1, 4, "")
							Else
								.set_TextMatrix(.Rows - 1, 4, VB6.Format(ldteCorte_Corte, "dd/MM/yyyy"))
							End If
						Else
							.set_TextMatrix(.Rows - 1, 4, "")
						End If
						.set_TextMatrix(.Rows - 1, 5, VB6.Format(ldteCorte_Reaviso, "dd/MM/yyyy"))
						.set_TextMatrix(.Rows - 1, 6, VB6.Format(ldteCorte_PrazoR, "dd/MM/yyyy"))
						.set_TextMatrix(.Rows - 1, 7, VB6.Format(ldteCorte_Entrega, "dd/MM/yyyy"))
						.set_TextMatrix(.Rows - 1, 8, VB6.Format(ldblCorte_Valor, "#,##0.00"))
						.set_TextMatrix(.Rows - 1, 9, lstrFatura_Zona)
						.set_TextMatrix(.Rows - 1, 10, lstrFatura_Livro)
						.set_TextMatrix(.Rows - 1, 11, lstrFatura_Conta)
						.set_TextMatrix(.Rows - 1, 12, llngFatura_ConsId)
						.set_TextMatrix(.Rows - 1, 13, llngFatura_UnidId)
						.set_TextMatrix(.Rows - 1, 14, lstrFatura_Grupo)
						.set_TextMatrix(.Rows - 1, 15, ldblCorte_RegNum)
					End With
				Next
				Me.msfgDados.Redraw = True
			End If

		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

	Private Sub MontaGrade()
		Try
			With Me.msfgDados
				.Redraw = False
				Call .Clear()
				.Rows = 1
				.set_TextMatrix(0, 0, "Fatura")
				.set_TextMatrix(0, 1, "Mês/Ano")
				.set_TextMatrix(0, 2, "Vencimento")
				.set_TextMatrix(0, 3, "Pagamento")
				.set_TextMatrix(0, 4, "Corte")
				.set_TextMatrix(0, 5, "Reaviso")
				.set_TextMatrix(0, 6, "Prazo")
				.set_TextMatrix(0, 7, "Entrega")
				.set_TextMatrix(0, 8, "Valor")
				.set_TextMatrix(0, 9, "zona")
				.set_TextMatrix(0, 10, "livro")
				.set_TextMatrix(0, 11, "conta")
				.set_TextMatrix(0, 12, "cons_id")
				.set_TextMatrix(0, 13, "unid_id")
				.set_TextMatrix(0, 14, "grupo")
				.set_TextMatrix(0, 15, "regnum")

				.set_ColWidth(1, 800)
				.set_ColWidth(2, 960)
				.set_ColWidth(3, 960)
				.set_ColWidth(4, 960)
				.set_ColWidth(5, 960)
				.set_ColWidth(6, 960)
				.set_ColWidth(7, 960)
				.set_ColWidth(8, 1200)
				.set_ColWidth(9, 0)
				.set_ColWidth(10, 0)
				.set_ColWidth(11, 0)
				.set_ColWidth(12, 0)
				.set_ColWidth(13, 0)
				.set_ColWidth(14, 0)
				.set_ColWidth(15, 0)
				.set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(6, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(8, MSFlexGridLib.AlignmentSettings.flexAlignRightCenter)
				.Redraw = True
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaGrade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub LigDes(ByVal ablnModo As Boolean)
		Try
			Me.fmeFiltros.Enabled = ablnModo
			Me.txtZona.Enabled = ablnModo
			Me.txtLivro.Enabled = ablnModo
			Me.txtConta.Enabled = ablnModo
			Me.cmdProc.Enabled = ablnModo
			Me.cmdBuscar.Enabled = ablnModo
			Me.cmdFechar.Enabled = ablnModo
			Me.fmeDados.Enabled = ablnModo
			Me.msfgDados.Enabled = ablnModo
			Me.fmeFaturas.Enabled = ablnModo
			Me.fmeFaturas.Visible = Me.fmeFaturas.Enabled
			Me.fmeRegistra.Visible = (Not ablnModo)
			Me.fmeRegistra.Enabled = Me.fmeRegistra.Visible
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LigDes" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	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.optGrupoA.Checked = True
			ElseIf astrGrupo = "B" Then
				Me.optGrupoB.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

End Class