Option Strict Off
Option Explicit On
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 irstReaviso As ADODB.Recordset
	Private ifrmLocalizaUnidade As frmLocalizaUnidade
	
	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
		Dim lrstConsulta As ADODB.Recordset
		
		lrstConsulta = New ADODB.Recordset
		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`.`unid_id`, "
			lstrSql = lstrSql & "       `u`.`cpf_cnpj`, "
			lstrSql = lstrSql & "       `u`.`rg`, "
			lstrSql = lstrSql & "       `u`.`insc`, "
			lstrSql = lstrSql & "       `u`.`situacao` "
			lstrSql = lstrSql & "  FROM `" & gstrDataBase & "`.`unid" & LCase(istrGrupo) & "` 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 & """ "
			Call Conectar(lstrSql, lrstConsulta)
			With lrstConsulta
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .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"'
						Me.lblNomeUC.Text = " " & IIf(IsDbNull(.Fields("nome").value), "Nome em branco!!!", .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"'
						Me.lblEndereco.Text = " " & IIf(IsDbNull(.Fields("end").value), "", .Fields("end").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"'
						Me.lblNumero.Text = IIf(IsDbNull(.Fields("numero").value), "", .Fields("numero").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"'
						Me.lblComplement.Text = " " & IIf(IsDbNull(.Fields("complement").value), "", .Fields("complement").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"'
						Me.lblBairro.Text = " " & IIf(IsDbNull(.Fields("bairro").value), "", .Fields("bairro").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"'
						Me.lblCidade.Text = " " & IIf(IsDbNull(.Fields("cidade").value), "", .Fields("cidade").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"'
						Me.lblLote.Text = IIf(IsDbNull(.Fields("lote").value), "", .Fields("lote").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"'
						Me.lblQuadra.Text = IIf(IsDbNull(.Fields("quadra").value), "", .Fields("quadra").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"'
						Me.lblCep.Text = IIf(IsDbNull(.Fields("cep").value), "", .Fields("cep").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"'
						Me.lblUf.Text = IIf(IsDbNull(.Fields("uf").value), "", .Fields("uf").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"'
						Me.lblDistrito.Text = " " & IIf(IsDbNull(.Fields("distrito").value), "", .Fields("distrito").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"'
						lstrCpfCnpj = IIf(IsDbNull(.Fields("cpf_cnpj").value), "", .Fields("cpf_cnpj").value)
						If Len(lstrCpfCnpj) <= 11 Then
							Me.lblCpf.Text = FormataCPF(lstrCpfCnpj)
							'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"'
							Me.lblRg.Text = IIf(IsDbNull(.Fields("rg").value), "", .Fields("rg").value)
						Else
							Me.lblCnj.Text = FormataCNPJ(lstrCpfCnpj)
							'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"'
							Me.lblIe.Text = IIf(IsDbNull(.Fields("insc").value), "", .Fields("insc").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"'
						Me.lblSituacao.Text = IIf(IsDbNull(.Fields("situacao").value), "L", .Fields("situacao").value)
						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
						'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"'
						ilngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").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"'
						ilngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value)
						Call BuscaDados()
					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
					Call .Close()
				End If
			End With
		Else
			Call MsgBox("Faltou informar o identificador!!!", MsgBoxStyle.Information, gstrTituloMensagem)
			Call LimpaCamposConsumidor()
			Call Me.txtZona.Focus()
		End If
		'UPGRADE_NOTE: Object lrstConsulta may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstConsulta = Nothing
		
	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
		Call ifrmLocalizaUnidade.Show()
		Call ifrmLocalizaUnidade.SetaLocalizaUnidade("U", Me, istrGrupo)
	End Sub
	
	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		Dim lstrSql As String
		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 `" & gstrDataBase & "`.`concorte` "
			lstrSql = lstrSql & "   SET `datacorte` = '" & VB6.Format(Me.dtpCorte.value, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & " WHERE `regnum`  = " & idblRegNum & " "
			If Executar(lstrSql) Then
				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
		End If
	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)
		Select Case KeyAscii
			Case System.Windows.Forms.Keys.Return
				Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			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
	End Sub
	
	Private Sub frmConfirmaCorteEspecial_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Me.Height = VB6.TwipsToPixelsY(5535)
		Call CenterForm(Me)
		ifrmLocalizaUnidade = New frmLocalizaUnidade
		irstReaviso = New ADODB.Recordset
		idteBase = BuscaDataBD
		Me.dtpCorte.value = idteBase
		Call MontaGrade()
	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
		
		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
	End Sub
	
	'UPGRADE_WARNING: Event optGrupoA.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 optGrupoA_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optGrupoA.CheckedChanged
		If eventSender.Checked Then
			istrGrupo = "A"
		End If
	End Sub
	
	'UPGRADE_WARNING: Event optGrupoB.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 optGrupoB_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optGrupoB.CheckedChanged
		If eventSender.Checked Then
			istrGrupo = "B"
		End If
	End Sub
	
	Private Sub txtConta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.Enter
		With Me.txtConta
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.Leave
		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
	End Sub
	
	Private Sub txtLivro_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLivro.Enter
		With Me.txtLivro
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	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
		With Me.txtZona
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	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()
		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
	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
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		'
		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 " & gstrDataBase & ".`nf`, " & gstrDataBase & ".`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"
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Me.msfgDados.Redraw = False
					While Not .EOF
						'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"'
						ldblFatura_NumNf = IIf(IsDbNull(.Fields("fatura_numnf").value), 0, .Fields("fatura_numnf").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"'
						lstrFatura_MesAno = IIf(IsDbNull(.Fields("fatura_mesano").value), "", .Fields("fatura_mesano").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"'
						llngFatura_MesAnoRef = IIf(IsDbNull(.Fields("fatura_mesanoref").value), 0, .Fields("fatura_mesanoref").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"'
						lstrFatura_Zona = IIf(IsDbNull(.Fields("fatura_zona").value), "", .Fields("fatura_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"'
						lstrFatura_Livro = IIf(IsDbNull(.Fields("fatura_livro").value), "", .Fields("fatura_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"'
						lstrFatura_Conta = IIf(IsDbNull(.Fields("fatura_conta").value), "", .Fields("fatura_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"'
						llngFatura_ConsId = IIf(IsDbNull(.Fields("fatura_cons_id").value), 0, .Fields("fatura_cons_id").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"'
						llngFatura_UnidId = IIf(IsDbNull(.Fields("fatura_unid_id").value), 0, .Fields("fatura_unid_id").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"'
						lstrFatura_Grupo = IIf(IsDbNull(.Fields("fatura_grupo").value), "B", .Fields("fatura_grupo").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"'
						ldteCorte_Reaviso = IIf(IsDbNull(.Fields("corte_datareav").value), "1900-01-01", .Fields("corte_datareav").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"'
						ldteCorte_PrazoR = IIf(IsDbNull(.Fields("corte_prazoreav").value), "1900-01-01", .Fields("corte_prazoreav").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"'
						ldteCorte_Entrega = IIf(IsDbNull(.Fields("corte_dataentrega").value), "1900-01-01", .Fields("corte_dataentrega").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"'
						ldteCorte_Vencto = IIf(IsDbNull(.Fields("corte_datavenc").value), "1900-01-01", .Fields("corte_datavenc").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"'
						ldteCorte_Pagto = IIf(IsDbNull(.Fields("corte_datapag").value), "1900-01-01", .Fields("corte_datapag").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"'
						ldteCorte_Corte = IIf(IsDbNull(.Fields("corte_datacorte").value), "1900-01-01", .Fields("corte_datacorte").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"'
						ldblCorte_Valor = IIf(IsDbNull(.Fields("corte_valor").value), 0, .Fields("corte_valor").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"'
						lstrCorte_Situacao = IIf(IsDbNull(.Fields("corte_situacao").value), "", .Fields("corte_situacao").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"'
						ldblCorte_RegNum = IIf(IsDbNull(.Fields("corte_regnum").value), 0, .Fields("corte_regnum").value)
						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
						Call .MoveNext()
					End While
					Me.msfgDados.Redraw = True
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub MontaGrade()
		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
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		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
	End Sub
End Class