Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmEnvioGraficaNovo
	Inherits System.Windows.Forms.Form
	Private ifrmMsgFaturas As frmMsgFaturas
	Private istrTabela As String
	Private istrHistorico(6, 12) As String
	Private istrTempo As String
	Private ilngTempo As Integer
	
	Private Sub frmEnvioGraficaNovo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrMesAno As String
		Dim lstrCaminho As String
		ifrmMsgFaturas = New frmMsgFaturas
		Me.tmrDecorrido.Enabled = False
		istrTabela = "nftransicao"
		lstrMesAno = LeConfig("MESANOFATURAMENTO")
		If lstrMesAno = "-1" Or lstrMesAno = "" Then
			lstrMesAno = VB6.Format(BuscaDataBD, "mm/yyyy")
		End If
		Me.dtpMesAno.value = CDate(VB6.Format("01/" & lstrMesAno, "dd/mm/yyyy"))
		lstrCaminho = Trim(LeConfig("DIR_GRAFICA"))
		If lstrCaminho = "-1" Then
			lstrCaminho = My.Application.Info.DirectoryPath
			lstrCaminho = lstrCaminho & IIf(VB.Right(lstrCaminho, 1) = "\", "", "\")
			lstrCaminho = Replace(lstrCaminho, "\", "\\")
			Call CriaParConfig("DIR_GRAFICA", lstrCaminho, True, "Diretório para guardar os arquivos para envio para grafica.")
			lstrCaminho = Trim(LeConfig("DIR_GRAFICA"))
		ElseIf Len(lstrCaminho) = 0 Then 
			lstrCaminho = My.Application.Info.DirectoryPath
			lstrCaminho = lstrCaminho & IIf(VB.Right(lstrCaminho, 1) = "\", "", "\")
			lstrCaminho = Replace(lstrCaminho, "\", "\\")
			Call GravaConfig("DIR_GRAFICA", lstrCaminho)
			lstrCaminho = Trim(LeConfig("DIR_GRAFICA"))
		End If
		Me.txtArquivo.Text = lstrCaminho & "FATTXT.TXT"
	End Sub
	
	Private Sub cmdMensagem_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdMensagem.Click
		Call CenterForm(ifrmMsgFaturas)
		VB6.ShowForm(ifrmMsgFaturas, False)
	End Sub
	
	Private Sub cmdGerar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerar.Click
		Dim lstrCam As String
		Dim lintPos As Short
		If Len(Trim(Me.txtArquivo.Text)) = 0 Then
			Call MsgBox("Faltou informar o nome do arquivo", MsgBoxStyle.Information, gstrTituloMensagem)
			Me.cmdArquivo.Focus()
			Call cmdArquivo_Click(cmdArquivo, New System.EventArgs())
		Else
			lintPos = 1
			lstrCam = Trim(Me.txtArquivo.Text)
			While InStr(lintPos, lstrCam, "\") > 0
				lintPos = InStr(lintPos, lstrCam, "\") + 1
			End While
			lstrCam = VB.Left(Trim(Me.txtArquivo.Text), lintPos - 1)
			Call LigDes(False)
			Call GerarArquivo()
			Call LigDes(True)
			Call GravaConfig("DIR_GRAFICA", Replace(lstrCam, "\", "\\"))
		End If
	End Sub
	
	Private Sub GerarArquivo()
		'Controle do Arquivo externo
		Dim lstrArquivo As String
		Dim lintARQ As Short
		Dim lstrLinha As String
		Dim lstrSql As String
		Dim lrstArquivo As ADODB.Recordset
		lrstArquivo = New ADODB.Recordset
		
		Me.tmrDecorrido.Enabled = True
		'checa e abre o arquivo
		If Len(Trim(Me.txtArquivo.Text)) = 0 Then
			Call MsgBox("Arquivo de destino não foi informado!", MsgBoxStyle.Information, gstrTituloMensagem)
			Me.txtArquivo.Focus()
			Exit Sub
		End If
		'Prepara os dados para exportar
		Call InsereDados()
		'Exporta para TXT os dados das faturas
		lstrArquivo = Trim(Me.txtArquivo.Text)
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		lintARQ = FreeFile
		FileOpen(lintARQ, lstrArquivo, OpenMode.Output)
		lstrSql = "SELECT * FROM grafica"
		Call Conectar(lstrSql, lrstArquivo)
		With lrstArquivo
			If .RecordCount > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				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"'
					lstrLinha = IIf(IsDbNull(.Fields("linha").value), "", .Fields("linha").value)
					'Grava Linha Arquivo externo
					PrintLine(lintARQ, lstrLinha)
					Call .MoveNext()
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				End While
			End If
			If .State = 1 Then Call .Close()
		End With
		FileClose(lintARQ)
		lstrSql = "DELETE FROM grafica"
		Call Executar(lstrSql)
		Call MsgBox("Processo Concluído! Arquivo gerado: " & Trim(Me.txtArquivo.Text), MsgBoxStyle.Information, gstrTituloMensagem)
		Me.lblInfo.Text = ""
		Call Me.lblInfo.Refresh()
		'UPGRADE_NOTE: Object lrstArquivo 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"'
		lrstArquivo = Nothing
		Me.Cursor = System.Windows.Forms.Cursors.Default
		Me.tmrDecorrido.Enabled = False
	End Sub
	
	Private Function InsereDados() As Object
		
		Dim lstrCtrlMensagem As String
		Dim lstrItemImp As String
		Dim llngNumControle As Integer
		Dim lstrConjunto As String
		Dim lstrDescrHistorico As String
		Dim lstrAuxiliar As String
		Dim lstrPrepara As String
		Dim llngConta As Integer
		Dim lstrMensagem(11) As String
		'UPGRADE_WARNING: Lower bound of array lstrMensaAux was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim lstrMensaAux(3) As String
		Dim lstrMensAux As String
		Dim llintDelimitador As Short
		
		Dim lstrMesAno As String
		Dim llngConsId As Integer
		Dim llngUnidId As Integer
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrRota As String
		Dim lstrSetor As String
		Dim lstrFase As String
		Dim lstrTensao As String
		Dim lstrLimMaximo As String
		Dim lstrLimMinimo As String
		Dim lstrMedidor As String
		Dim lstrConstMed As String
		Dim ldblNumNF As Double
		Dim lstrNumNf As String
		Dim lstrDomicilioBancario As String
		Dim lstrDtVencto As String
		Dim lstrDtAprest As String
		Dim lstrDtLeitAtual As String
		Dim lstrDtLeitAnt As String
		Dim lstrDtProxLeit As String
		Dim lstrCfop As String
		Dim lstrGrupoTarifa As String
		Dim lstrCdClasse As String
		Dim lstrNmClasse As String
		Dim lstrDiasConsumo As String
		Dim llngDiasConsumo As Integer
		Dim lstrValorTotal As String
		Dim ldblValorTotal As Double
		Dim lstrValorLiqui As String
		Dim ldblValorLiqui As Double
		Dim lstrNSU As String
		Dim lstrHashCod As String
		Dim lstrCodigoBarras As String
		Dim lstrCodigoBarDigito As String
		
		'Itens
		Dim lstrLeitAnter As String
		Dim lstrLeitAtual As String
		Dim lstrConsumoLido As String
		Dim ldblConsumoLido As Double
		Dim lstrConsumoFat As String
		Dim ldblConsumoFat As Double
		Dim ldblResiduo As Double
		Dim ldblVlrConsFat As Double
		Dim lstrConsumoDia As String
		Dim ldblBaseIcms1 As Double
		Dim ldblBaseIcms2 As Double
		Dim lstrBaseIcms As String
		Dim lstrAliqIcms As String
		Dim ldblAliqIcms1 As Double
		Dim ldblAliqIcms2 As Double
		Dim lstrValorIcms As String
		Dim ldblValorIcms1 As Double
		Dim ldblValorIcms2 As Double
		Dim lstrValorPis As String
		Dim ldblValorPis As Double
		Dim lstrAliqPis As String
		Dim ldblAliqPis As Double
		Dim lstrBasePis As String
		Dim ldblBasePis As Double
		Dim lstrValorCof As String
		Dim ldblValorCof As Double
		Dim lstrAliqCof As String
		Dim ldblAliqCof As Double
		Dim lstrBaseCof As String
		Dim ldblBaseCof As Double
		Dim lstrRefDecFec As String
		Dim lstrPadraoDec As String
		Dim lstrPadraoFec As String
		Dim lstrPadraoDic As String
		Dim lstrPadraoFic As String
		Dim lstrRealizDec As String
		Dim lstrRealizFec As String
		Dim lstrRealizDic As String
		Dim lstrRealizFic As String
		Dim lstrRealiDmic As String
		Dim lstrCompGerEnerg As String
		Dim lstrCompTransmis As String
		Dim lstrCompDistribu As String
		Dim lstrCompEncSetor As String
		Dim lstrCompTributos As String
		Dim lstrCompTotal As String
		Dim lstrOcorrencia As String
		
		'Outras informacoes
		Dim lstrMaiorConsumokwh As String
		Dim ldblMaiorConsumokwh As Double
		Dim lstrMenorConsumokwh As String
		Dim ldblMenorConsumokwh As Double
		Dim lstrMediaUltTresMeskwh As String
		Dim ldblMediaUltTresMeskwh As Double
		Dim lstrValorDiarioReal As String
		Dim lstrTotalPendente As String
		Dim lstrQtdeContasAberto As String
		Dim ldblValorPendente As Double
		Dim llngQtdePendente As Integer
		
		'Dados da Unidade Consumidora
		Dim lstrNmUc As String
		Dim lstrEndUc As String
		Dim lstrNumUc As String
		Dim lstrComUc As String
		Dim lstrBaiUc As String
		Dim lstrCidUc As String
		Dim lstrCepUc As String
		Dim lstrQuaUc As String
		Dim lstrLotUc As String
		Dim lstrEndEnt As String
		Dim lstrNumEnt As String
		Dim lstrComEnt As String
		Dim lstrBaiEnt As String
		Dim lstrCidEnt As String
		Dim lstrCepEnt As String
		Dim lstrQuaEnt As String
		Dim lstrLotEnt As String
		
		Dim lstrCpfCnpj As String
		Dim lstrInscricao As String
		Dim lstrRegGeral As String
		Dim lstrAgencia As String
		Dim lstrContaCorrente As String
		
		Dim lstrSql As String
		Dim lrstFat As ADODB.Recordset
		Dim lrstUnC As ADODB.Recordset
		Dim lrstAux As ADODB.Recordset
		Dim lrstConsumo As ADODB.Recordset
		
		lrstFat = New ADODB.Recordset
		lrstUnC = New ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lrstConsumo = New ADODB.Recordset
		
		Me.lblInfo.Text = "Buscando os dados de faturas, aguarde...."
		Call Me.lblInfo.Refresh()
		Call Executar("DELETE FROM grafica")
		
		'Conjunto de continuidade
		lstrConjunto = gstrEmpresa_Conjunto
		If Len(lstrConjunto) = 0 Or lstrConjunto = "-1" Then
			lstrConjunto = "               "
		Else
			lstrConjunto = lstrConjunto & "-" & gstrEmpresa_Cidade
		End If
		
		'Preparando a mensagem das faturas
		lstrAuxiliar = ""
		lstrMensAux = ""
		lstrMensagem(0) = Replace(LeConfig("FATURA_MENSAGEM"), vbCrLf, "|")
		llngConta = 1
		llintDelimitador = 1
		While llintDelimitador <> 0
			llintDelimitador = InStr(llngConta, lstrMensagem(0), "|")
			If llintDelimitador > 0 Then
				lstrAuxiliar = Mid(lstrMensagem(0), llngConta, llintDelimitador - llngConta)
			Else
				lstrAuxiliar = Mid(lstrMensagem(0), llngConta)
			End If
			llngConta = llintDelimitador + 1
			lstrMensAux = lstrMensAux & VB.Left(lstrAuxiliar & New String(" ", 80), 80)
		End While
		lstrMensagem(0) = lstrMensAux
		lstrMensagem(0) = PreparaCampoGrafica(lstrMensagem(0), ALFANUMERICO, 800, " ")
		lstrMensagem(2) = PreparaCampoGrafica(Mid(lstrMensagem(0), 1, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(3) = PreparaCampoGrafica(Mid(lstrMensagem(0), 81, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(4) = PreparaCampoGrafica(Mid(lstrMensagem(0), 161, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(5) = PreparaCampoGrafica(Mid(lstrMensagem(0), 241, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(6) = PreparaCampoGrafica(Mid(lstrMensagem(0), 321, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(7) = PreparaCampoGrafica(Mid(lstrMensagem(0), 401, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(8) = PreparaCampoGrafica(Mid(lstrMensagem(0), 481, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(9) = PreparaCampoGrafica(Mid(lstrMensagem(0), 561, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(10) = PreparaCampoGrafica(Mid(lstrMensagem(0), 641, 80), ALFANUMERICO, 80, " ")
		lstrMensagem(11) = PreparaCampoGrafica(Mid(lstrMensagem(0), 721, 80), ALFANUMERICO, 80, " ")
		'********************************
		'Montando consulta das faturas
		
		lstrMesAno = VB6.Format(Me.dtpMesAno.value, "mm/yyyy")
		lstrSql = MontaSqlFaturas
		Call Conectar(lstrSql, lrstFat)
		With lrstFat
			If .RecordCount > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				llngNumControle = 1
				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"'
					lstrZona = VB6.Format(IIf(IsDbNull(.Fields("zona").value), "0", .Fields("zona").value), "000")
					'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"'
					lstrLivro = VB6.Format(IIf(IsDbNull(.Fields("livro").value), "0", .Fields("livro").value), "00")
					'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"'
					lstrConta = VB6.Format(IIf(IsDbNull(.Fields("conta").value), "0", .Fields("conta").value), "000000")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrRota = VB.Right(VB6.Format(IIf(IsDbNull(.Fields("rota").value), "0", .Fields("rota").value), "00000000"), 4)
					'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"'
					lstrSetor = Mid(VB6.Format(IIf(IsDbNull(.Fields("rota").value), "0", .Fields("rota").value), "00000000"), 3, 2)
					'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"'
					ldblNumNF = IIf(IsDbNull(.Fields("numnf").value), 0, .Fields("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"'
					llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("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"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)
					lstrNumNf = Str(Val(CStr(ldblNumNF)))
					'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"'
					lstrDomicilioBancario = IIf(IsDbNull(.Fields("banident").value), "", .Fields("banident").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"'
					lstrFase = IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").value)
					Select Case Trim(lstrFase)
						Case "B" : lstrFase = "BIFASICO"
						Case "T" : lstrFase = "TRIFASICO"
						Case Else : lstrFase = "MONOFASICO"
					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"'
					lstrTensao = Trim(Replace(IIf(IsDbNull(.Fields("tensao").value), "220", .Fields("tensao").value), "V", ""))
					Select Case lstrTensao
						Case "220"
							lstrLimMaximo = "231"
							lstrLimMinimo = "201"
						Case "380"
							lstrLimMaximo = "396"
							lstrLimMinimo = "348"
						Case "220/380"
							lstrLimMaximo = "231/396"
							lstrLimMinimo = "201/348"
					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"'
					lstrMedidor = IIf(IsDbNull(.Fields("num_med").value), "", .Fields("num_med").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"'
					lstrConstMed = IIf(IsDbNull(.Fields("const_med").value), 0, .Fields("const_med").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"'
					lstrDtVencto = VB6.Format(IIf(IsDbNull(.Fields("datavenc").value), "1951-01-01", .Fields("datavenc").value), "dd/mm/yy")
					'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"'
					lstrDtAprest = VB6.Format(IIf(IsDbNull(.Fields("dataapr").value), "1951-01-01", .Fields("dataapr").value), "dd/mm/yy")
					'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"'
					lstrDtLeitAtual = VB6.Format(IIf(IsDbNull(.Fields("dataleit").value), "1951-01-01", .Fields("dataleit").value), "dd/mm/yy")
					'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"'
					lstrDtLeitAnt = VB6.Format(IIf(IsDbNull(.Fields("dataleitant").value), "1951-01-01", .Fields("dataleitant").value), "dd/mm/yy")
					'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"'
					lstrDtProxLeit = VB6.Format(IIf(IsDbNull(.Fields("dataproxleit").value), "1951-01-01", .Fields("dataproxleit").value), "dd/mm/yy")
					'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"'
					lstrCfop = IIf(IsDbNull(.Fields("cfop").value), " ", .Fields("cfop").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"'
					lstrGrupoTarifa = Mid(IIf(IsDbNull(.Fields("ta").value), "", .Fields("ta").value), 3, 2)
					'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"'
					lstrCdClasse = IIf(IsDbNull(.Fields("codclas").value), "0", .Fields("codclas").value)
					lstrNmClasse = RetiraAcentuacao(BuscaNmClasse(lstrCdClasse))
					lstrGrupoTarifa = lstrGrupoTarifa & " " & lstrCdClasse & " - " & lstrNmClasse
					'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"'
					llngDiasConsumo = IIf(IsDbNull(.Fields("dias").value), "0", .Fields("dias").value)
					lstrDiasConsumo = CStr(llngDiasConsumo)
					'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"'
					ldblValorTotal = IIf(IsDbNull(.Fields("valortot").value), 0, .Fields("valortot").value)
					lstrValorTotal = VB6.Format(ldblValorTotal, "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblValorLiqui = IIf(IsDbNull(.Fields("valorliq").value), 0, .Fields("valorliq").value)
					If ldblValorLiqui = 0 Then
						lstrValorLiqui = lstrValorTotal
						ldblValorLiqui = ldblValorTotal
					Else
						lstrValorLiqui = VB6.Format(ldblValorLiqui, "Standard")
					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"'
					lstrNSU = "NSU: " & VB6.Format(IIf(IsDbNull(.Fields("nsu").value), 0, .Fields("nsu").value), "0000000000")
					'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"'
					lstrHashCod = IIf(IsDbNull(.Fields("codhash").value), "", .Fields("codhash").value)
					lstrHashCod = Mid(lstrHashCod, 1, 4) & "." & Mid(lstrHashCod, 5, 4) & "." & Mid(lstrHashCod, 9, 4) & "." & Mid(lstrHashCod, 13, 4) & "." & Mid(lstrHashCod, 17, 4) & "." & Mid(lstrHashCod, 21, 4) & "." & Mid(lstrHashCod, 25, 4) & "." & Mid(lstrHashCod, 29, 4)
					'Código de Barras
					If gstrEmpresa_Sigla = "COORSEL" 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"'
						lstrCodigoBarras = IIf(IsDbNull(.Fields("codbarra").value), 0, .Fields("codbarra").value)
					Else
						lstrSql = "SELECT calculacodbar" & IIf(istrTabela = "nf", "", "trans") & "(" & ldblNumNF & ",""" & lstrMesAno & """) barras"
						Call Conectar(lstrSql, lrstAux)
						With lrstAux
							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"'
								lstrCodigoBarras = IIf(IsDbNull(.Fields("barras").value), "", .Fields("barras").value)
							Else
								lstrCodigoBarras = ""
							End If
							If .State = 1 Then Call .Close()
						End With
					End If
					If gstrEmpresa_Sigla = "COORSEL" 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"'
						lstrCodigoBarDigito = IIf(IsDbNull(.Fields("dvcodbarra").value), "", .Fields("dvcodbarra").value)
					Else
						lstrSql = "SELECT calculadvcodbarra(" & lstrCodigoBarras & ") barras"
						Call Conectar(lstrSql, lrstAux)
						With lrstAux
							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"'
								lstrCodigoBarDigito = IIf(IsDbNull(.Fields("barras").value), "", .Fields("barras").value)
							Else
								lstrCodigoBarDigito = ""
							End If
							If .State = 1 Then Call .Close()
						End With
					End If
					lstrCodigoBarDigito = Mid(lstrCodigoBarras, 1, 11) & " - " & Mid(lstrCodigoBarDigito, 1, 1) & " - " & Mid(lstrCodigoBarras, 12, 11) & " - " & Mid(lstrCodigoBarDigito, 2, 1) & " - " & Mid(lstrCodigoBarras, 23, 11) & " - " & Mid(lstrCodigoBarDigito, 3, 1) & " - " & Mid(lstrCodigoBarras, 34, 11) & " - " & Mid(lstrCodigoBarDigito, 4, 1)
					'Itens da fatura
					'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"'
					lstrOcorrencia = IIf(IsDbNull(.Fields("ocorrencia").value), "", .Fields("ocorrencia").value)
					If Len(Trim(lstrOcorrencia)) > 0 Then
						If CDbl(Mid(lstrOcorrencia, 3, 1)) = 1 Then
							lstrOcorrencia = "MEDIA"
						Else
							lstrOcorrencia = "LIDO "
						End If
					Else
						lstrOcorrencia = "LIDO "
					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"'
					lstrLeitAtual = IIf(IsDbNull(.Fields("leitatual").value), "", .Fields("leitatual").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"'
					lstrLeitAnter = IIf(IsDbNull(.Fields("leitanter").value), "", .Fields("leitanter").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"'
					ldblConsumoLido = IIf(IsDbNull(.Fields("consumolido").value), 0, .Fields("consumolido").value)
					lstrConsumoLido = CStr(ldblConsumoLido)
					'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"'
					ldblConsumoFat = IIf(IsDbNull(.Fields("consumofat").value), "0", .Fields("consumofat").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"'
					ldblResiduo = IIf(IsDbNull(.Fields("residuo").value), "0", .Fields("residuo").value)
					lstrConsumoFat = CStr(ldblConsumoFat)
					'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"'
					ldblVlrConsFat = IIf(IsDbNull(.Fields("vlrconsfat").value), "0", .Fields("vlrconsfat").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"'
					ldblBaseIcms1 = IIf(IsDbNull(.Fields("baseicms1").value), 0, .Fields("baseicms1").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"'
					ldblBaseIcms2 = IIf(IsDbNull(.Fields("baseicms2").value), 0, .Fields("baseicms2").value)
					lstrBaseIcms = VB6.Format(ldblBaseIcms1 + ldblBaseIcms2, "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblAliqIcms1 = IIf(IsDbNull(.Fields("aliqicms1").value), 0, .Fields("aliqicms1").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"'
					ldblAliqIcms2 = IIf(IsDbNull(.Fields("aliqicms2").value), 0, .Fields("aliqicms2").value)
					lstrAliqIcms = CStr(Arredondar(ldblAliqIcms1, 0))
					'lstrAliqIcms = Format(ldblAliqIcms1, "Standard")
					If ldblAliqIcms2 > 0 Then
						lstrAliqIcms = Trim(lstrAliqIcms) & "/" & Arredondar(ldblAliqIcms2, 0) 'Format(ldblAliqIcms2, "Standard")
					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"'
					ldblValorIcms1 = IIf(IsDbNull(.Fields("valoricms1").value), 0, .Fields("valoricms1").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"'
					ldblValorIcms2 = IIf(IsDbNull(.Fields("valoricms2").value), 0, .Fields("valoricms2").value)
					lstrValorIcms = VB6.Format(ldblValorIcms1 + ldblValorIcms2, "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblValorPis = IIf(IsDbNull(.Fields("valorpis").value), 0, .Fields("valorpis").value)
					lstrValorPis = VB6.Format(ldblValorPis, "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblAliqPis = IIf(IsDbNull(.Fields("aliqpis").value), 0, .Fields("aliqpis").value)
					lstrAliqPis = VB6.Format(ldblAliqPis, "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblBasePis = Arredondar(CDbl(IIf(IsDbNull(.Fields("basepis").value), 0, .Fields("basepis").value)), 2)
					lstrBasePis = VB6.Format(ldblBasePis, "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblValorCof = IIf(IsDbNull(.Fields("valorcof").value), 0, .Fields("valorcof").value)
					lstrValorCof = VB6.Format(ldblValorCof, "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblAliqCof = IIf(IsDbNull(.Fields("aliqcof").value), 0, .Fields("aliqcof").value)
					lstrAliqCof = VB6.Format(ldblAliqCof, "standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblBaseCof = IIf(IsDbNull(.Fields("basecof").value), 0, .Fields("basecof").value)
					lstrBaseCof = VB6.Format(ldblBaseCof, "Standard")
					'Consumo Diário
					If llngDiasConsumo > 0 Then
						lstrConsumoDia = VB6.Format(ldblConsumoLido / llngDiasConsumo, "Standard")
					Else
						lstrConsumoDia = CStr(0)
					End If
					'Valor Diário
					If llngDiasConsumo > 0 Then
						lstrValorDiarioReal = VB6.Format(ldblVlrConsFat / llngDiasConsumo, "Standard")
					Else
						lstrValorDiarioReal = CStr(0)
					End If
					'Indicadores de continuidade
					'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"'
					lstrRefDecFec = IIf(IsDbNull(.Fields("refdecfec").value), "00/0000", .Fields("refdecfec").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"'
					lstrPadraoDec = VB6.Format(IIf(IsDbNull(.Fields("decpadrao").value), 0, .Fields("decpadrao").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrPadraoFec = VB6.Format(IIf(IsDbNull(.Fields("fecpadrao").value), 0, .Fields("fecpadrao").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrPadraoDic = VB6.Format(IIf(IsDbNull(.Fields("dicpadrao").value), 0, .Fields("dicpadrao").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrPadraoFic = VB6.Format(IIf(IsDbNull(.Fields("ficpadrao").value), 0, .Fields("ficpadrao").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrRealizDec = VB6.Format(IIf(IsDbNull(.Fields("decrealiz").value), 0, .Fields("decrealiz").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrRealizFec = VB6.Format(IIf(IsDbNull(.Fields("fecrealiz").value), 0, .Fields("fecrealiz").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrRealizDic = VB6.Format(IIf(IsDbNull(.Fields("dicrealiz").value), 0, .Fields("dicrealiz").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrRealizFic = VB6.Format(IIf(IsDbNull(.Fields("ficrealiz").value), 0, .Fields("ficrealiz").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrRealiDmic = VB6.Format(IIf(IsDbNull(.Fields("dmicreali").value), 0, .Fields("dmicreali").value), "Standard")
					'Composição da Fatura
					'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"'
					lstrCompGerEnerg = VB6.Format(IIf(IsDbNull(.Fields("gerenergia").value), 0, .Fields("gerenergia").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCompTransmis = VB6.Format(IIf(IsDbNull(.Fields("transmissao").value), 0, .Fields("transmissao").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCompDistribu = VB6.Format(IIf(IsDbNull(.Fields("distribuicao").value), 0, .Fields("distribuicao").value), "Standard")
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCompEncSetor = VB6.Format(IIf(IsDbNull(.Fields("encsetoriais").value), 0, .Fields("encsetoriais").value), "Standard")
					lstrCompTributos = VB6.Format(ldblValorIcms1 + ldblValorIcms2 + ldblValorPis + ldblValorCof, "Standard")
					lstrCompTotal = CStr(CDbl(lstrCompGerEnerg) + CDbl(lstrCompTransmis) + CDbl(lstrCompDistribu) + CDbl(lstrCompEncSetor) + CDbl(lstrCompTributos))
					'Itens de impressão
					'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"'
					lstrItemImp = IIf(IsDbNull(.Fields("itensImp").value), "", .Fields("itensImp").value)
					'Busca dados da unidade consumidora
					lstrSql = "SELECT * " & "  FROM unidb USE INDEX (keyzona, keylivro, keyconta) " & " WHERE zona = """ & lstrZona & """ " & "   AND livro = """ & lstrLivro & """ " & "   AND conta = """ & lstrConta & """ " & " LIMIT 0, 1 "
					Call Conectar(lstrSql, lrstUnC)
					With lrstUnC
						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"'
							lstrNmUc = RetiraAcentuacao(Trim(IIf(IsDbNull(.Fields("nome").value), "NOME INDEFINIDO", .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"'
							lstrComUc = RetiraAcentuacao(Trim(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"'
							lstrQuaUc = RetiraAcentuacao(Trim(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"'
							lstrLotUc = RetiraAcentuacao(Trim(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"'
							lstrNumUc = Trim(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"'
							lstrEndUc = RetiraAcentuacao(IIf(IsDbNull(.Fields("end").value), " ", .Fields("end").value))
							lstrEndUc = lstrEndUc & IIf(Len(lstrNumUc) > 0, ", " & lstrNumUc, "") & IIf(Len(lstrComUc) > 0, " - " & lstrComUc, "") & IIf(Len(lstrQuaUc) > 0, " Q: " & lstrQuaUc, "") & IIf(Len(lstrLotUc) > 0, " L: " & lstrLotUc, "")
							'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"'
							lstrBaiUc = RetiraAcentuacao(Trim(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"'
							lstrCidUc = RetiraAcentuacao(Trim(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"'
							lstrCepUc = Trim(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"'
							lstrComEnt = RetiraAcentuacao(Trim(IIf(IsDbNull(.Fields("compl_entrega").value), "", .Fields("compl_entrega").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"'
							lstrQuaEnt = RetiraAcentuacao(Trim(IIf(IsDbNull(.Fields("qua_entrega").value), "", .Fields("qua_entrega").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"'
							lstrLotEnt = RetiraAcentuacao(Trim(IIf(IsDbNull(.Fields("lot_entrega").value), "", .Fields("lot_entrega").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"'
							lstrNumEnt = Trim(IIf(IsDbNull(.Fields("num_entrega").value), "", .Fields("num_entrega").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"'
							lstrEndEnt = RetiraAcentuacao(Trim(IIf(IsDbNull(.Fields("end_entrega").value), "", .Fields("end_entrega").value)))
							lstrEndEnt = lstrEndEnt & IIf(Len(lstrNumEnt) > 0, ", " & lstrNumEnt, "") & IIf(Len(lstrComEnt) > 0, " - " & lstrComEnt, "") & IIf(Len(lstrQuaEnt) > 0, " Q: " & lstrQuaEnt, "") & IIf(Len(lstrLotEnt) > 0, " L: " & lstrLotEnt, "")
							'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"'
							lstrBaiEnt = RetiraAcentuacao(Trim(IIf(IsDbNull(.Fields("bai_entrega").value), "", .Fields("bai_entrega").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"'
							lstrCidEnt = RetiraAcentuacao(Trim(IIf(IsDbNull(.Fields("cid_entrega").value), "", .Fields("cid_entrega").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"'
							lstrCepEnt = Trim(IIf(IsDbNull(.Fields("cep_entrega").value), "", .Fields("cep_entrega").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 = Trim(IIf(IsDbNull(.Fields("cpf_cnpj").value), "", .Fields("cpf_cnpj").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"'
							lstrInscricao = Trim(IIf(IsDbNull(.Fields("insc").value), "", .Fields("insc").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"'
							lstrRegGeral = Trim(IIf(IsDbNull(.Fields("rg").value), "", .Fields("rg").value))
							If Len(lstrRegGeral) > 0 Then lstrInscricao = lstrRegGeral
							'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"'
							lstrAgencia = IIf(IsDbNull(.Fields("agenc").value), "", .Fields("agenc").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"'
							lstrContaCorrente = IIf(IsDbNull(.Fields("cc").value), "", .Fields("cc").value)
						End If
						If .State = 1 Then Call .Close()
					End With
					If Len(Trim(lstrAgencia)) > 0 Then
						lstrCodigoBarras = "                                            "
						lstrCodigoBarDigito = "Sr. Caixa, favor não cobrar. Débito Automático em Conta Corrente.    "
					End If
					If ldblValorTotal = 0 Then
						lstrCodigoBarras = "                                            "
						lstrCodigoBarDigito = "Sr. Caixa, favor não cobrar. Fatura com valor igual a zero.          "
					End If
					'Maior Consumo e 'Menor Consumo e Media dos Ultimos 3 meses
					Call MaiorMenorMediaConsumos(llngUnidId, ldblConsumoLido, ldblMaiorConsumokwh, ldblMenorConsumokwh, ldblMediaUltTresMeskwh)
					lstrMaiorConsumokwh = CStr(ldblMaiorConsumokwh)
					lstrMenorConsumokwh = CStr(ldblMenorConsumokwh)
					lstrMediaUltTresMeskwh = CStr(ldblMediaUltTresMeskwh)
					'Total Pendente e qtde de consumidores em aberto
					ldblValorPendente = 0
					llngQtdePendente = 0
					lstrSql = "SELECT SUM(valortot) valor, COUNT(numnf) total " & "  FROM nf USE INDEX(keyzona,keylivro,keyconta) " & " WHERE (datapag IS NULL OR datapag = '0000-00-00') " & "   AND (flgst = '' OR flgst is null) " & "   AND zona = '" & lstrZona & "' " & "   AND livro = '" & lstrLivro & "' " & "   AND conta = '" & lstrConta & "' " & "   AND unid_id = " & llngUnidId & " " & "   AND cons_id = " & llngConsId & " LIMIT 0, 1 "
					Call Conectar(lstrSql, lrstConsumo)
					If lrstConsumo.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"'
						ldblValorPendente = IIf(IsDbNull(lrstConsumo.Fields("valor").Value), 0, lrstConsumo.Fields("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"'
						llngQtdePendente = IIf(IsDbNull(lrstConsumo.Fields("total").Value), 0, lrstConsumo.Fields("total").Value)
					End If
					If lrstConsumo.State = 1 Then lrstConsumo.Close()
					lstrTotalPendente = VB6.Format(Replace(Str(ldblValorPendente), ".", ","), "standard")
					lstrQtdeContasAberto = Str(llngQtdePendente)
					'Grava na tabela para gerar arquivo
					System.Windows.Forms.Application.DoEvents()
					'Registro F Dados da Fatura
					lstrSql = "INSERT INTO grafica VALUES ( """
					lstrSql = lstrSql & PreparaCampoGrafica("F", ALFANUMERICO, 1, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrZona, ALFANUMERICO, 3, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrLivro, ALFANUMERICO, 2, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(VB.Right(lstrConta, 4), ALFANUMERICO, 4, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRota, ALFANUMERICO, 4, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrNmUc, ALFANUMERICO, 44, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrEndUc, ALFANUMERICO, 44, " ") '44
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCepUc, ALFANUMERICO, 9, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCidUc, ALFANUMERICO, 9, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCpfCnpj, ALFANUMERICO, 18, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrInscricao, ALFANUMERICO, 13, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrDtVencto, ALFANUMERICO, 8, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCfop, ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrDtAprest, ALFANUMERICO, 8, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(VB.Left(lstrMesAno, 3) & VB.Right(lstrMesAno, 2), ALFANUMERICO, 5, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrDtLeitAtual, ALFANUMERICO, 8, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrLeitAtual, ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrDtLeitAnt, ALFANUMERICO, 8, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrLeitAnter, ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrConstMed, ALFANUMERICO, 5, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrConsumoLido, ALFANUMERICO, 9, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrGrupoTarifa, ALFANUMERICO, 60, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrFase, ALFANUMERICO, 10, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrMedidor, ALFANUMERICO, 8, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrBaseIcms, ALFANUMERICO, 9, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrAliqIcms, ALFANUMERICO, 5, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrValorTotal, ALFANUMERICO, 17, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrValorIcms, ALFANUMERICO, 9, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCodigoBarras, ALFANUMERICO, 44, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrNumNf, ALFANUMERICO, 5, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrDomicilioBancario, ALFANUMERICO, 20, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrAgencia, ALFANUMERICO, 4, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrDiasConsumo, ALFANUMERICO, 3, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrConsumoDia, ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCodigoBarDigito, ALFANUMERICO, 69, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrContaCorrente, ALFANUMERICO, 13, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRefDecFec, ALFANUMERICO, 7, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrPadraoDec, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrPadraoFec, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrPadraoDic, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrPadraoFic, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRealizDec, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRealizFec, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRealizDic, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRealizFic, ALFANUMERICO, 5, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrTensao, ALFANUMERICO, 7, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrLimMaximo, ALFANUMERICO, 11, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrLimMinimo, ALFANUMERICO, 11, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrDtProxLeit, ALFANUMERICO, 8, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrSetor, ALFANUMERICO, 2, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrMaiorConsumokwh, ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrMediaUltTresMeskwh, ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrValorDiarioReal, ALFANUMERICO, 10, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrOcorrencia, ALFANUMERICO, 5, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrConjunto, ALFANUMERICO, 15, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRealiDmic, ALFANUMERICO, 5, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrHashCod, ALFANUMERICO, 40, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrTotalPendente, ALFANUMERICO, 10, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrQtdeContasAberto, ALFANUMERICO, 2, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrBaiUc, ALFANUMERICO, 22, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrConsumoFat, ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrValorLiqui, ALFANUMERICO, 17, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 287, " ")
					lstrSql = lstrSql & """), ("""
					'Registro T Tarifas
					lstrSql = lstrSql & PreparaCampoGrafica("T", ALFANUMERICO, 1, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrValorPis, ALFANUMERICO, 10, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrAliqPis, ALFANUMERICO, 8, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrBasePis, ALFANUMERICO, 10, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrValorCof, ALFANUMERICO, 10, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrAliqCof, ALFANUMERICO, 8, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrBaseCof, ALFANUMERICO, 10, " ", "E")
					lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 962, " ")
					lstrSql = lstrSql & """), ("""
					'Registro M mensagens
					lstrMensagem(0) = lstrMensAux
					lstrMensagem(0) = PreparaCampoGrafica(lstrMensagem(0), ALFANUMERICO, 800, " ")
					lstrMensagem(2) = PreparaCampoGrafica(Mid(lstrMensagem(0), 1, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(3) = PreparaCampoGrafica(Mid(lstrMensagem(0), 81, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(4) = PreparaCampoGrafica(Mid(lstrMensagem(0), 161, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(5) = PreparaCampoGrafica(Mid(lstrMensagem(0), 241, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(6) = PreparaCampoGrafica(Mid(lstrMensagem(0), 321, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(7) = PreparaCampoGrafica(Mid(lstrMensagem(0), 401, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(8) = PreparaCampoGrafica(Mid(lstrMensagem(0), 481, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(9) = PreparaCampoGrafica(Mid(lstrMensagem(0), 561, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(10) = PreparaCampoGrafica(Mid(lstrMensagem(0), 641, 80), ALFANUMERICO, 80, " ")
					lstrMensagem(11) = PreparaCampoGrafica(Mid(lstrMensagem(0), 721, 80), ALFANUMERICO, 80, " ")
					
					lstrMensagem(1) = lstrNSU
					lstrMensagem(1) = PreparaCampoGrafica(lstrMensagem(1), ALFANUMERICO, 80, " ")
					For llngConta = 11 To 2 Step -1
						If Len(Trim(lstrMensagem(llngConta))) > 0 Then
							llngConta = 2
						Else
							lstrMensagem(llngConta) = ""
						End If
					Next llngConta
					'Mensagem FIXA conforme res. 407/2010
					If VB.Left(lstrCdClasse, 1) = "9" Then
						If VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "09/2010" Or VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "10/2010" Or VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "11/2010" Or VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "12/2010" Or VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "01/2011" Or VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "02/2011" Then
							lstrMensagem(2) = PreparaCampoGrafica("TARIFA SOCIAL DE ENERGIA ELETRICA PARA TODAS AS", ALFANUMERICO, 80, " ")
							lstrMensagem(3) = PreparaCampoGrafica("FAMILIAS QUE ESTEJAM INSCRITAS NO CADASTRO UNICO", ALFANUMERICO, 80, " ")
							lstrMensagem(4) = PreparaCampoGrafica("DE PROGRAMAS SOCIAIS, E SEUS MORADORES QUE", ALFANUMERICO, 80, " ")
							lstrMensagem(5) = PreparaCampoGrafica("RECEBAM BENEFICIOS DE PRESTACAO DA ASSISTENCIA", ALFANUMERICO, 80, " ")
							lstrMensagem(6) = PreparaCampoGrafica("SOCIAL CONFORME LEI Nº 12.212 DE 2010.", ALFANUMERICO, 80, " ")
							lstrMensagem(7) = PreparaCampoGrafica("QUALQUER DUVIDA, FAVOR DIRIGIR-SE A " & gstrEmpresa_Sigla & ".", ALFANUMERICO, 80, " ")
						End If
					End If
					If chkQuitacaoDebito.CheckState = 1 Then
						If VerificaQuitacaoDebito(llngConsId, CInt(VB.Right(lstrMesAno, 4)) - 1, lstrZona, lstrLivro, lstrConta) Then
							lstrMensagem(4) = PreparaCampoGrafica("Conforme Lei 12.007, de 29 de Julho de 2009 Art. 2º § 1º.Certifico, a vista de", ALFANUMERICO, 80, " ")
							lstrMensagem(5) = PreparaCampoGrafica("documentos comprobatórios e pesquisa de registros na nossa base de dados, que", ALFANUMERICO, 80, " ")
							lstrMensagem(6) = PreparaCampoGrafica("esta unidade consumidora, se encontra em situação regular, sem débitos", ALFANUMERICO, 80, " ")
							lstrMensagem(7) = PreparaCampoGrafica("ou serviços a faturar, no período de Janeiro a Dezembro de " & CInt(VB.Right(lstrMesAno, 4)) - 1, ALFANUMERICO, 80, " ")
						End If
					End If
					If ldblResiduo <> 0 Then
						lstrMensagem(8) = PreparaCampoGrafica("Acrescido residuo de " & FormatNumber(ldblResiduo, 0) & " kwh ", ALFANUMERICO, 80, " ")
					End If
					For llngConta = 1 To 11
						If Len(lstrMensagem(llngConta)) > 0 Then
							lstrSql = lstrSql & PreparaCampoGrafica("M", ALFANUMERICO, 1, " ")
							lstrSql = lstrSql & lstrMensagem(llngConta)
							lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 1018 - Len(lstrMensagem(llngConta)), " ")
							lstrSql = lstrSql & """),("""
						End If
					Next llngConta
					lstrCtrlMensagem = "0"
					'Registro H historicos
					For llngConta = 0 To 11
						istrHistorico(1, llngConta) = VB6.Format(istrHistorico(1, llngConta), "00") 'dias
						If CDbl(Mid(istrHistorico(2, llngConta), 3, 1)) = 1 Then
							istrHistorico(2, llngConta) = "MEDIA"
						Else
							If Trim(istrHistorico(0, llngConta)) <> "/" Then
								istrHistorico(2, llngConta) = "LIDO "
							Else
								istrHistorico(2, llngConta) = "     "
							End If
						End If
						istrHistorico(6, llngConta) = VB6.Format(istrHistorico(6, llngConta), "dd/mm/yy")
						'Pendente
						If Trim(istrHistorico(0, llngConta)) <> "/" Then
							istrHistorico(5, llngConta) = VB6.Format(istrHistorico(5, llngConta), "Standard")
							If Len(Trim(istrHistorico(6, llngConta))) = 0 Or istrHistorico(6, llngConta) = "00/00/00" Or istrHistorico(6, llngConta) = "0000-00-00" Or istrHistorico(6, llngConta) = "1951-01-01" Or istrHistorico(6, llngConta) = "01/01/51" Then
								istrHistorico(6, llngConta) = "Pendente"
								lstrCtrlMensagem = "1"
							End If
						Else
							istrHistorico(6, llngConta) = ""
						End If
					Next llngConta
					For llngConta = 0 To 11
						lstrDescrHistorico = Trim(VB.Left(istrHistorico(0, llngConta), 2))
						lstrDescrHistorico = IIf(Len(lstrDescrHistorico) = 0, 0, lstrDescrHistorico)
						lstrDescrHistorico = VB.Left(UCase(Retorna_Mes(CShort(lstrDescrHistorico))), 3)
						If Len(lstrDescrHistorico) = 0 Then lstrDescrHistorico = "   "
						
						lstrDescrHistorico = lstrDescrHistorico & "/" & VB.Right(istrHistorico(0, llngConta), 2) & VB.Right(istrHistorico(1, llngConta), 2) & istrHistorico(2, llngConta) & PreparaCampoGrafica(istrHistorico(3, llngConta), ALFANUMERICO, 6, " ", "E") & PreparaCampoGrafica(istrHistorico(4, llngConta), ALFANUMERICO, 6, " ", "E") & PreparaCampoGrafica(istrHistorico(5, llngConta), ALFANUMERICO, 10, " ", "E") & PreparaCampoGrafica(istrHistorico(6, llngConta), ALFANUMERICO, 8, " ", "E")
						lstrSql = lstrSql & PreparaCampoGrafica("H", ALFANUMERICO, 1, " ")
						lstrSql = lstrSql & lstrDescrHistorico
						lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 975, " ")
						lstrSql = lstrSql & """), ("""
					Next llngConta
					'Registro I descricao dos itens
					lstrAuxiliar = ""
					llngConta = 1
					lstrItemImp = Replace(Trim(lstrItemImp), vbCrLf, "")
					While llngConta <= Len(lstrItemImp)
						lstrPrepara = Mid(lstrItemImp, llngConta, 65)
						lstrPrepara = VB.Left(VB.Left(lstrPrepara, Len(lstrPrepara) - 15), 45) & " " & VB.Right("          " & VB6.Format(VB.Right(lstrPrepara, 15), "Standard"), 10)
						lstrAuxiliar = "I" & lstrPrepara
						llngConta = llngConta + 65
						lstrSql = lstrSql & lstrAuxiliar
						lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 962, " ")
						lstrSql = lstrSql & """), ("""
					End While
					'registro C para a composicao da tarifa
					lstrSql = lstrSql & PreparaCampoGrafica("C", ALFANUMERICO, 1, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCompGerEnerg, ALFANUMERICO, 12, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCompTransmis, ALFANUMERICO, 12, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCompDistribu, ALFANUMERICO, 12, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCompEncSetor, ALFANUMERICO, 12, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCompTributos, ALFANUMERICO, 12, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCompTotal, ALFANUMERICO, 12, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 946, " ")
					lstrSql = lstrSql & """), ("""
					'Registro P Mensagem de pagamento
					If lstrCtrlMensagem = "0" Then
						lstrMensaAux(1) = "               AGRADECEMOS SEU COMPROMISSO COM A PONTUALIDADE NO                            "
						If gstrEmpresa_Sigla = "EFLUL" Then
							lstrMensaAux(2) = "PAGAMENTO, O QUE PERMITE A EFLUL CONTINUAR INVESTINDO NA                                   "
						Else
							lstrMensaAux(2) = "PAGAMENTO, O QUE PERMITE A COORSEL CONTINUAR INVESTINDO NA                                 "
						End If
						lstrMensaAux(3) = "QUALIDADE DO SEU PRODUTO E SERVICO"
					Else
						lstrMensaAux(1) = "                         REGISTRAMOS FATURAS PENDENTES DE PAGAMENTO                        "
						lstrMensaAux(2) = "CONFORME HISTORICO DE CONSUMO ESTANDO SUJEITO A SUSPENCAO               "
						lstrMensaAux(3) = "DE FORNECIMENTO. DESCONSIDERAR ESTE AVISO CASO TENHA EFETUADO O PAGAMENTO"
					End If
					lstrSql = lstrSql & PreparaCampoGrafica("P", ALFANUMERICO, 1, " ")
					lstrSql = lstrSql & lstrMensaAux(1)
					lstrSql = lstrSql & lstrMensaAux(2)
					lstrSql = lstrSql & lstrMensaAux(3)
					If lstrCtrlMensagem = "0" Then
						lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 801, " ")
					Else
						lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 782, " ")
					End If
					lstrSql = lstrSql & """), ("""
					'Registro V verso da fatura
					lstrSql = lstrSql & PreparaCampoGrafica("V", ALFANUMERICO, 1, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrZona, ALFANUMERICO, 3, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrLivro, ALFANUMERICO, 2, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(VB.Right(lstrConta, 4), ALFANUMERICO, 4, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrRota, ALFANUMERICO, 4, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrNmUc, ALFANUMERICO, 44, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrEndEnt, ALFANUMERICO, 70, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCepEnt, ALFANUMERICO, 9, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrCidEnt, ALFANUMERICO, 9, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrMedidor, ALFANUMERICO, 8, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(VB6.Format(Str(llngNumControle), "0000"), ALFANUMERICO, 4, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrSetor, ALFANUMERICO, 2, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 6, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(lstrBaiEnt, ALFANUMERICO, 40, " ")
					lstrSql = lstrSql & PreparaCampoGrafica(" ", ALFANUMERICO, 813, " ")
					lstrSql = lstrSql & """)"
					Call Executar(lstrSql)
					llngNumControle = llngNumControle + 1
					Call .MoveNext()
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					System.Windows.Forms.Application.DoEvents()
				End While
			End If
			If .State = 1 Then Call .Close()
		End With
		
		'UPGRADE_NOTE: Object lrstFat 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"'
		lrstFat = Nothing
		'UPGRADE_NOTE: Object lrstUnC 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"'
		lrstUnC = Nothing
		'UPGRADE_NOTE: Object lrstConsumo 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"'
		lrstConsumo = Nothing
		'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 Function
	
	Private Sub cmdBuscar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscar.Click
		Dim lstrMesAno As String
		Dim lstrAuxiliar As String
		Dim lstrSql As String
		Dim lrstFatura As ADODB.Recordset
		
		Me.cmdBuscar.Enabled = False
		lstrMesAno = VB6.Format(Me.dtpMesAno.value, "mm/yyyy")
		lrstFatura = New ADODB.Recordset
		Call lvsRota.Items.Clear()
		Call lvsVenc.Items.Clear()
		System.Windows.Forms.Application.DoEvents()
		'*** Lista de rotas das faturas geradas
		Me.lblInfo.Text = " Buscando as faturas por rota de leitura, aguarde... "
		Call Me.lblInfo.Refresh()
		lstrSql = "SELECT DISTINCT MID(rota,1,4) rotaaux " & "  FROM " & istrTabela & " USE INDEX(keyrota) " & " WHERE mesano = """ & lstrMesAno & """ " & "   AND MID(ta,3,1) = ""B"" " & " GROUP BY MID(rota,1,4) "
		Call Conectar(lstrSql, lrstFatura)
		With lrstFatura
			If .RecordCount > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
				Call Me.lvsRota.Items.Add("Todas as rotas")
				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"'
					lstrAuxiliar = VB6.Format(IIf(IsDbNull(.Fields("rotaaux").value), "0", .Fields("rotaaux").value), "0000")
					Call Me.lvsRota.Items.Add(lstrAuxiliar)
					Call .MoveNext()
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				End While
				If .State = 1 Then Call .Close()
				System.Windows.Forms.Application.DoEvents()
				Me.lblInfo.Text = " Buscando as faturas por vencimento, aguarde... "
				Call Me.lblInfo.Refresh()
				'*** Lista de vencimentos
				lstrSql = "SELECT DISTINCT datavenc " & "  FROM " & istrTabela & " USE INDEX(keyvenc) " & " WHERE mesano = """ & lstrMesAno & """ " & "   AND MID(ta,3,1) = ""B"" " & " GROUP BY datavenc"
				Call Conectar(lstrSql, lrstFatura)
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
					Call Me.lvsVenc.Items.Add("Todos os vencimentos")
					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"'
						lstrAuxiliar = VB6.Format(IIf(IsDbNull(.Fields("datavenc").value), "1951-01-01", .Fields("datavenc").value), "dd/mm/yyyy")
						If lstrAuxiliar <> "01/01/1951" Then
							Call lvsVenc.Items.Add(lstrAuxiliar)
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
				End If
				If .State = 1 Then Call .Close()
			Else
				Call MsgBox("Não existe faturas para o mês informado!", MsgBoxStyle.Information, gstrTituloMensagem)
				If .State = 1 Then Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstFatura 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"'
		lrstFatura = Nothing
		Me.lblInfo.Text = ""
		Call Me.lblInfo.Refresh()
		Me.cmdBuscar.Enabled = True
	End Sub
	
	Private Function PreparaCampoGrafica(ByVal astrCampo As String, ByVal aintTipoCampo As Short, ByVal aintComp As Short, ByVal astrCaracter As String, Optional ByVal astrAlinha As String = "D") As String
		astrCampo = Trim(astrCampo)
		Select Case aintTipoCampo
			'TIPO NUMERICO
			Case 0
				astrCampo = Replace(astrCampo, ",", "")
				astrCampo = Replace(astrCampo, ".", "")
				If Len(astrCampo) > aintComp Then
					PreparaCampoGrafica = VB.Left(astrCampo, aintComp)
				Else
					PreparaCampoGrafica = New String(astrCaracter, aintComp - Len(astrCampo)) & astrCampo
				End If
				'TIPO ALFANUMÉRICO
			Case 1
				If Len(astrCampo) > aintComp Then
					PreparaCampoGrafica = VB.Left(astrCampo, aintComp)
				Else
					If astrAlinha = "D" Then
						PreparaCampoGrafica = astrCampo & New String(astrCaracter, aintComp - Len(astrCampo))
					Else
						PreparaCampoGrafica = New String(astrCaracter, aintComp - Len(astrCampo)) & astrCampo
					End If
				End If
		End Select
	End Function
	
	Private Sub frmEnvioGraficaNovo_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'UPGRADE_NOTE: Object ifrmMsgFaturas 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"'
		ifrmMsgFaturas = Nothing
	End Sub
	
	Private Sub lvsRota_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lvsRota.ItemCheck
		Dim Item As System.Windows.Forms.ListViewItem = lvsRota.Items(eventArgs.Index)
		Dim lngConta As Integer
		With Me.lvsRota
			If Item.Text = "Todas as rotas" Then
				'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				If .Items.Item(1).Checked = True Then
					For lngConta = 1 To .Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Items.Item(lngConta).Checked = True
					Next lngConta
					'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				ElseIf .Items.Item(1).Checked = False Then 
					For lngConta = 1 To lvsRota.Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Items.Item(lngConta).Checked = False
					Next lngConta
				End If
			Else
				If Item.Checked = False Then
					'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(1).Checked = False
				End If
			End If
		End With
	End Sub
	
	Private Sub lvsVenc_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lvsVenc.ItemCheck
		Dim Item As System.Windows.Forms.ListViewItem = lvsVenc.Items(eventArgs.Index)
		Dim lngConta As Integer
		With Me.lvsVenc
			If Item.Text = "Todos os vencimentos" Then
				'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				If .Items.Item(1).Checked = True Then
					For lngConta = 1 To .Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Items.Item(lngConta).Checked = True
					Next lngConta
					'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				ElseIf .Items.Item(1).Checked = False Then 
					For lngConta = 1 To .Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Items.Item(lngConta).Checked = False
					Next lngConta
				End If
			Else
				If Item.Checked = False Then
					'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(1).Checked = False
				End If
			End If
		End With
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	'UPGRADE_WARNING: Event chkNfTrans.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 chkNfTrans_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkNfTrans.CheckStateChanged
		istrTabela = IIf(Me.chkNfTrans.CheckState = 1, "nftransicao", "nf")
	End Sub
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		'UPGRADE_WARNING: CommonDialog variable was not upgraded Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="671167DC-EA81-475D-B690-7A40C7BF4A23"'
		With Me.dialArq
			.InitialDirectory = Replace(Me.txtArquivo.Text, "FATTXT.TXT", "")
			.DefaultExt = "TXT"
			'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			.Filter = "*." & .DefaultExt
			.FileName = "FATTXT.TXT"
			.Title = "Arquivo de remessa para a gráfica"
			Call .ShowDialog()
			Me.txtArquivo.Text = IIf(InStr(1, .FileName, "\") > 0, .FileName, Me.txtArquivo.Text)
			Me.cmdGerar.Enabled = Len(Trim(Me.txtArquivo.Text)) > 0
		End With
	End Sub
	
	Private Function MontaSqlFaturas() As String
		Dim lstrMesAno As String
		Dim llngConta As Integer
		Dim llngFiltro As Integer
		Dim lstrFilRota As String
		Dim lstrFilVenc As String
		Dim lstrOrdena As String
		Dim lstrSql As String
		
		lstrMesAno = VB6.Format(Me.dtpMesAno.value, "mm/yyyy")
		'Montando consulta das faturas
		lstrSql = "SELECT "
		lstrSql = lstrSql & "f.zona, "
		lstrSql = lstrSql & "f.livro, "
		lstrSql = lstrSql & "f.conta, "
		lstrSql = lstrSql & "f.rota, "
		lstrSql = lstrSql & "f.numnf, "
		lstrSql = lstrSql & "f.cons_id, "
		lstrSql = lstrSql & "f.unid_id, "
		lstrSql = lstrSql & "f.banident, "
		lstrSql = lstrSql & "f.fase, "
		lstrSql = lstrSql & "f.tensao, "
		lstrSql = lstrSql & "f.num_med, "
		lstrSql = lstrSql & "f.const_med, "
		lstrSql = lstrSql & "f.datavenc, "
		lstrSql = lstrSql & "f.dataapr, "
		lstrSql = lstrSql & "f.dataleit, "
		lstrSql = lstrSql & "f.dataleitant, "
		lstrSql = lstrSql & "f.dataproxleit, "
		lstrSql = lstrSql & "f.cfop, "
		lstrSql = lstrSql & "f.ta, "
		lstrSql = lstrSql & "f.codclas, "
		lstrSql = lstrSql & "f.dias, "
		lstrSql = lstrSql & "f.valortot, "
		lstrSql = lstrSql & "f.valorliq, "
		lstrSql = lstrSql & "f.nsu, "
		lstrSql = lstrSql & "f.codhash, "
		lstrSql = lstrSql & "f.codbarra, "
		lstrSql = lstrSql & "f.dvcodbarra, "
		lstrSql = lstrSql & "f.itensImp, "
		lstrSql = lstrSql & "f.itensserv, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""90410000"") ocorrencia, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302990039"") leitatual, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302990040"") leitanter, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""030201008"")+0 consumolido, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""030201009"")+0 consumofat, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""030202009"")+0 vlrconsfat, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302020998"")+0 residuo, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302030001"")+0 baseicms1, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302030003"")+0 baseicms2, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302030002"")+0 aliqicms1, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302030013"")+0 aliqicms2, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302030009"")+0 valoricms1, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302030010"")+0 valoricms2, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302020029"")+0 valorpis, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302990303"")+0 aliqpis, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302020033"")+0 basepis, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302020030"")+0 valorcof, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302990304"")+0 aliqcof, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302020034"")+0 basecof, "
		lstrSql = lstrSql & "buscarefitem(f.itensserv,""0302080099"") refdecfec, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080101"")+0 decpadrao, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080102"")+0 fecpadrao, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080103"")+0 dicpadrao, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080104"")+0 ficpadrao, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080001"")+0 decrealiz, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080002"")+0 fecrealiz, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080003"")+0 dicrealiz, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080004"")+0 ficrealiz, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302080105"")+0 dmicreali, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302090001"")+0 gerenergia, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302090002"")+0 transmissao, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302090003"")+0 distribuicao, "
		lstrSql = lstrSql & "buscavlritem(f.itensserv,""0302090004"")+0 encsetoriais "
		lstrSql = lstrSql & "  FROM " & istrTabela & " f USE INDEX (keyvenc,keyrota) "
		lstrSql = lstrSql & " WHERE f.mesano = """ & lstrMesAno & """ "
		lstrSql = lstrSql & "   AND MID(f.ta,3,1) = ""B"" "
		'Monta filtro da consulta por rota
		lstrFilRota = ""
		llngFiltro = 0
		With Me.lvsRota
			If .Items.Count > 0 Then
				'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				If .Items.Item(1).Checked = False Then
					lstrFilRota = lstrFilRota & " AND MID(f.rota,1,4) IN ( "
					For llngConta = 2 To .Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						If .Items.Item(llngConta).Checked = True Then
							'UPGRADE_WARNING: Lower bound of collection Me.lvsRota.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
							lstrFilRota = lstrFilRota & """" & .Items.Item(llngConta).Text & ""","
							llngFiltro = llngFiltro + 1
						End If
					Next llngConta
					If llngFiltro > 0 Then
						lstrFilRota = VB.Left(lstrFilRota, Len(lstrFilRota) - 1) & ")"
					Else
						lstrFilRota = ""
					End If
				Else
					lstrFilRota = ""
				End If
			Else
				lstrFilRota = ""
			End If
		End With
		'Monta filtro da consulta por vencimento
		lstrFilVenc = ""
		llngFiltro = 0
		With Me.lvsVenc
			If .Items.Count > 0 Then
				'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				If .Items.Item(1).Checked = False Then
					lstrFilVenc = lstrFilVenc & " AND f.datavenc IN ( "
					For llngConta = 2 To .Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						If .Items.Item(llngConta).Checked = True Then
							'UPGRADE_WARNING: Lower bound of collection Me.lvsVenc.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
							lstrFilVenc = lstrFilVenc & """" & VB6.Format(.Items.Item(llngConta).Text, "yyyy-MM-dd") & ""","
							llngFiltro = llngFiltro + 1
						End If
					Next llngConta
					If llngFiltro > 0 Then
						lstrFilVenc = VB.Left(lstrFilVenc, Len(lstrFilVenc) - 1) & ")"
					Else
						lstrFilVenc = ""
					End If
				Else
					lstrFilVenc = ""
				End If
			Else
				lstrFilVenc = ""
			End If
		End With
		lstrOrdena = " ORDER BY f.agrupa ASC, MID(f.agrupa_rota, 3, 7) ASC, MID(f.rota, 3, 6) ASC"
		MontaSqlFaturas = lstrSql & lstrFilRota & lstrFilVenc & lstrOrdena
	End Function
	
	Private Function BuscaNmClasse(ByVal astrCdClasse As String) As String
		'Busca o nome da classe
		Dim lstrSql As String
		Dim lrstConsulta As ADODB.Recordset
		
		lrstConsulta = New ADODB.Recordset
		lstrSql = "SELECT descricao " & "  FROM classe " & " WHERE codigo = """ & astrCdClasse & """ " & " LIMIT 0, 1 "
		Call Conectar(lstrSql, lrstConsulta)
		With lrstConsulta
			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"'
				BuscaNmClasse = IIf(IsDbNull(.Fields("descricao").value), "", .Fields("descricao").value)
			Else
				BuscaNmClasse = ""
			End If
			If .State = 1 Then Call .Close()
		End With
		'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 Function
	
	Private Sub MaiorMenorMediaConsumos(ByVal alngUnidId As Integer, ByVal adblConsumoAtual As Double, ByRef adblMaior As Double, ByRef adblMenor As Double, ByRef adblMedia As Double)
		Dim ldblAcumulado As Double
		Dim lintConta As Short
		Dim ldblConsumo As Double
		Dim lstrSql As String
		Dim lrstConsulta As ADODB.Recordset
		
		lrstConsulta = New ADODB.Recordset
		adblMaior = adblConsumoAtual
		adblMenor = adblConsumoAtual
		
		For lintConta = 0 To 11
			istrHistorico(0, lintConta) = "   /  "
			istrHistorico(1, lintConta) = "  "
			istrHistorico(2, lintConta) = ""
			istrHistorico(3, lintConta) = ""
			istrHistorico(4, lintConta) = ""
			istrHistorico(5, lintConta) = ""
			istrHistorico(6, lintConta) = ""
		Next lintConta
		
		lintConta = 0
		ldblAcumulado = 0
		
		lstrSql = "SELECT buscavlritem(itensserv,""030201008"")+0 consumolido, " & "       mesano, " & "       dias, " & "       buscavlritem(itensserv,""90410000"") ocorrencia, " & "       buscavlritem(itensserv,""0302990039"") leitura, " & "       valortot, " & "       datapag " & "  FROM nf " & " WHERE unid_id = " & alngUnidId & "   AND MID(ta,3,1) = ""B"" " & " ORDER BY CONCAT(RIGHT(mesano,4),LEFT(mesano,2))+0 DESC " & " LIMIT 0, 12 "
		Call Conectar(lstrSql, lrstConsulta)
		With lrstConsulta
			
			If .RecordCount > 0 Then
				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"'
					ldblConsumo = IIf(IsDbNull(.Fields("consumolido").value), 0, .Fields("consumolido").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"'
					istrHistorico(0, lintConta) = IIf(IsDbNull(.Fields("mesano").value), "   /  ", .Fields("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"'
					istrHistorico(1, lintConta) = IIf(IsDbNull(.Fields("dias").value), "0", .Fields("dias").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"'
					istrHistorico(2, lintConta) = IIf(IsDbNull(.Fields("ocorrencia").value), "", .Fields("ocorrencia").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"'
					istrHistorico(3, lintConta) = IIf(IsDbNull(.Fields("leitura").value), "0", .Fields("leitura").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"'
					istrHistorico(4, lintConta) = IIf(IsDbNull(.Fields("consumolido").value), "0", .Fields("consumolido").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"'
					istrHistorico(5, lintConta) = IIf(IsDbNull(.Fields("valortot").value), "0", .Fields("valortot").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"'
					istrHistorico(6, lintConta) = IIf(IsDbNull(.Fields("datapag").value), "1951-01-01", .Fields("datapag").value)
					If ldblConsumo > adblMaior Then adblMaior = ldblConsumo
					If ldblConsumo < adblMenor Then adblMenor = ldblConsumo
					
					lintConta = lintConta + 1
					If lintConta <= 3 Then
						ldblAcumulado = ldblAcumulado + ldblConsumo
					End If
					Call .MoveNext()
				End While
				adblMedia = Arredondar(ldblAcumulado / 3, 0)
			End If
			If .State = 1 Then Call .Close()
		End With
		'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 tmrDecorrido_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmrDecorrido.Tick
		ilngTempo = ilngTempo + 1
		istrTempo = CStr(TimeSerial(0, 0, ilngTempo))
		Me.lblTempo.Text = istrTempo
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.dtpMesAno.Enabled = ablnModo
		Me.chkNfTrans.Enabled = ablnModo
		Me.cmdBuscar.Enabled = ablnModo
		Me.cmdArquivo.Enabled = ablnModo
		Me.cmdGerar.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.lvsRota.Enabled = ablnModo
		Me.lvsVenc.Enabled = ablnModo
	End Sub
	
	Private Function VerificaQuitacaoDebito(ByVal alngConsId As Integer, ByVal alngAno As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String) As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		lstrSql = ""
		lstrSql = lstrSql & "SELECT ano,cons_id,zona,livro,conta "
		lstrSql = lstrSql & "  FROM declaracao_debito "
		lstrSql = lstrSql & " WHERE ano     = " & alngAno & " "
		lstrSql = lstrSql & "   AND cons_id = " & alngConsId & " "
		lstrSql = lstrSql & "   AND zona    = '" & astrZona & "' "
		lstrSql = lstrSql & "   AND livro   = '" & astrLivro & "' "
		lstrSql = lstrSql & "   AND conta   = '" & astrConta & "' "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				VerificaQuitacaoDebito = (.RecordCount > 0)
				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 Function
End Class