Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmEmissaoBaixaRendaVI
	Inherits System.Windows.Forms.Form
	
	Private istrTabela As String
	Private istrArquivo As String
	Private istrCaminho As String
	
	Private Sub DTPMesAno_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles DTPMesAno.Leave
		Call NomeArquivoXML()
	End Sub
	
	Private Sub frmEmissaoBaixaRendaVI_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrSql As String
		Call CenterForm(Me)
		Me.chkNovoNis.Text = Me.chkNovoNis.Text & " > " & CDate(LeConfig("DATANOVOBXRENDA"))
		istrTabela = "planilha_bxrenda"
		Me.dtpMesAno.value = BuscaDataBD
		istrCaminho = LeConfig("CAMINHO_XML")
		If Len(Trim(istrCaminho)) = 0 Or Trim(istrCaminho) = "-1" Then
			istrCaminho = My.Application.Info.DirectoryPath
		End If
		If VB.Right(Trim(istrCaminho), 1) <> "\" Then
			istrCaminho = istrCaminho & "\"
		End If
		'istrArquivo = "APLDMR" & gstrEmpresa_CodRes89 & "_" & gstrEmpresa_Sigla & "_AnexoVI_"
		istrArquivo = "APLDMR" & gstrEmpresa_CodRes89 & "_" & gstrEmpresa_Sigla & "_89_"
		Me.txtVersao.Text = "01"
		Call NomeArquivoXML()
		'Dropa a tabela
		lstrSql = "DROP TABLE IF EXISTS " & istrTabela
		Call Executar(lstrSql)
		lstrSql = "DROP TABLE IF EXISTS " & istrTabela & "_total "
		Call Executar(lstrSql)
		'Cria a tabela
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TABLE " & istrTabela & " ( "
		lstrSql = lstrSql & "`fase` char(1) default NULL, "
		lstrSql = lstrSql & "`kwhc` int(11) default '0', "
		lstrSql = lstrSql & "`t5_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`t6_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`t7_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`t8_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`res_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t5_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t5_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t6_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t7_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t6_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t7_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t8_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t8_valor` double(12,2) default '0.00' "
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1; "
		Call Executar(lstrSql)
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TABLE " & istrTabela & "_total ("
		lstrSql = lstrSql & "`ordimp1` int(1) default NULL, "
		lstrSql = lstrSql & "`ordimp2` int(1) default NULL, "
		lstrSql = lstrSql & "`faixa` char(40) default NULL, "
		lstrSql = lstrSql & "`fase` char(1) default NULL, "
		lstrSql = lstrSql & "`restotal` int(11) default NULL, "
		lstrSql = lstrSql & "`t5qtde` int(11) default NULL, "
		lstrSql = lstrSql & "`totbxqtde` int(11) default NULL, "
		lstrSql = lstrSql & "`t6qtde` int(11) default NULL, "
		lstrSql = lstrSql & "`t7qtde` int(11) default NULL, "
		lstrSql = lstrSql & "`t8qtde` int(11) default NULL, "
		'
		lstrSql = lstrSql & "`reskwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t5kwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`totbxkhwc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t6kwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t7kwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t8kwhc` double(12,3) default '0.000', "
		'
		lstrSql = lstrSql & "`resvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t5valr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t5bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`totbxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t6bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t7bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`totvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t6valr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t7valr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t8bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t8valr` double(12,2) default '0.00', "
		'
		lstrSql = lstrSql & "`mesano` varchar(7) NOT NULL default '', "
		lstrSql = lstrSql & "`faixaxml` char(40) default NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; "
		Call Executar(lstrSql)
	End Sub
	
	Private Sub frmEmissaoBaixaRendaVI_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			Call cmdFechar_Click(cmdFechar, New System.EventArgs())
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	'UPGRADE_WARNING: Event txtDir.TextChanged 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 txtDir_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDir.TextChanged
		With Me.txtDir
			istrCaminho = .Text
		End With
	End Sub
	
	Private Sub txtVersao_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtVersao.Enter
		With Me.txtVersao
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtVersao_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtVersao.Leave
		With Me.txtVersao
			.Text = VB6.Format(.Text, "00")
			Call NomeArquivoXML()
		End With
	End Sub
	
	Private Sub txtDir_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDir.Enter
		With Me.txtDir
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtDir_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDir.Leave
		With Me.txtDir
			If Len(Trim(.Text)) = 0 Then
				Call MsgBox("Este campo é de preenchimento obrigatório!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call NomeArquivoXML()
				.Focus()
			Else
				istrCaminho = .Text
			End If
		End With
	End Sub
	
	Private Sub cmdSalvarArq_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvarArq.Click
		With Me.cmdSalvarArq
			Me.Enabled = False
			frmDir.frmQuemChama = Me
			frmDir.intTipo = 0
			Call frmDir.SetBounds(VB6.TwipsToPixelsX(VB6.PixelsToTwipsX(Me.Left) + VB6.PixelsToTwipsX(.Left) + 400), VB6.TwipsToPixelsY(VB6.PixelsToTwipsY(Me.Top) + VB6.PixelsToTwipsY(.Top) + 400), 0, 0, Windows.Forms.BoundsSpecified.X Or Windows.Forms.BoundsSpecified.Y)
			Call frmDir.Show()
		End With
	End Sub
	
	Private Sub cmdGerar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerar.Click
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call LigDes(False)
		System.Windows.Forms.Application.DoEvents()
		If Me.chkCSV.CheckState = 1 Then
			Call GerarCSV()
		Else
			Call GerarXML()
		End If
		Call LigDes(True)
		System.Windows.Forms.Application.DoEvents()
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call Executar("DELETE FROM " & istrTabela)
		Call Executar("DELETE FROM " & istrTabela & "_total ")
		If Len(Trim(txtDir.Text)) = 0 Then
			Call MsgBox("Arquivo de destino não foi informado!", MsgBoxStyle.Information, gstrTituloMensagem)
			Call Me.txtDir.Focus()
		Else
			Call LigDes(False)
			System.Windows.Forms.Application.DoEvents()
			If MontaTabela = True Then
				Me.lblInfo.Text = "Pronto!"
				Call Me.lblInfo.Refresh()
				If MsgBox("Deseja Imprimir a Planilha agora?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton1, gstrTituloMensagem) = MsgBoxResult.Yes Then
					Call EmitirPlanilha()
				End If
			Else
				Me.lblInfo.Text = ""
				Call Me.lblInfo.Refresh()
				Call MsgBox("Não existem faturas de consumidores residenciais no mês selecionado!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			End If
			Call LigDes(True)
		End If
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call LigDes(False)
		System.Windows.Forms.Application.DoEvents()
		Call EmitirPlanilha()
		Call LigDes(True)
		System.Windows.Forms.Application.DoEvents()
		Me.Cursor = System.Windows.Forms.Cursors.Default
	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 Function MontaTabela() As Boolean
		'Dados da U.C.
		Dim lstrItens As String
		Dim lstrFase As String
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrClasse As String
		Dim ldteLAtual As Date
		Dim ldteLAnter As Date
		Dim llngUnidId As Integer
		'Dados de Qtde e Vlr de kWh da fatura
		Dim ldblQtdkWh As Double
		Dim ldblVlrkWh As Double
		'Controle se é novo NIS.
		Dim lblnNovoNis As Boolean
		Dim lblnContinua As Boolean
		Dim ldteNovoBxRenda As Date
		'Controle da planilha e do xml
		Dim llngLiRes As Integer
		Dim lstrFases As Object
		Dim lstrFxPla As Object
		Dim lstrFxXml As Object
		Dim lstrFaixa As Object
		Dim llngFaixa As Double
		Dim llngFases As Double
		'Quantidade de consumidores em cada classe
		Dim llngReqtde As Integer
		Dim llngT5qtde As Integer
		Dim llngT6qtde As Integer
		Dim llngT7qtde As Integer
		Dim llngT8qtde As Integer
		'Quantidade de kwh em cada classe
		Dim ldblReskwhc As Double
		Dim ldblT5kwhc As Double
		Dim ldblT6kwhc As Double
		Dim ldblT7kwhc As Double
		Dim ldblT8kwhc As Double
		'Valores de kwh com tarifa residencial normal de cada classe
		Dim ldblReskvlr As Double
		Dim ldblT5kvlr As Double
		Dim ldblT6kvlr As Double
		Dim ldblT7kvlr As Double
		Dim ldblT8kvlr As Double
		'Valores de kwh com tarifa residencial baixa renda de cada classe
		Dim ldblT5kbxv As Double
		Dim ldblT6kbxv As Double
		Dim ldblT7kbxv As Double
		Dim ldblT8kbxv As Double
		'Outros controles
		Dim ldteBase As Date
		Dim lstrSql As String
		Dim lrstNis As ADODB.Recordset
		Dim lrstAux As ADODB.Recordset
		lrstNis = New ADODB.Recordset
		lrstAux = New ADODB.Recordset
		'
		ldteNovoBxRenda = CDate(LeConfig("DATANOVOBXRENDA"))
		ldteBase = CDate(UltimoDiaMes((Me.dtpMesAno.Month), (Me.dtpMesAno.Year)) & "/" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy"))
		lblInfo.Text = " Buscando os dados..."
		lblInfo.Refresh()
		lstrSql = ""
		lstrSql = lstrSql & "SELECT " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`mesano_ref`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`zona`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`livro`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`conta`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`codclas`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`fase`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`dataleit`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`dataleitant`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`itensserv`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`grupo`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`unid_id` " & vbCrLf
		lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
		'lstrSql = lstrSql & " LEFT JOIN `unidb` ON(`unidb`.`zona` = `nf`.`zona` AND `unidb`.`livro` = `nf`.`livro` AND `unidb`.`conta` = `nf`.`conta`) " & vbCrLf
		lstrSql = lstrSql & " WHERE `nf`.`mesano_ref` = " & CInt(VB6.Format(Me.dtpMesAno.value, "yyyymm")) & " " & vbCrLf
		lstrSql = lstrSql & "   AND `nf`.`grupo` = 'B' " & vbCrLf
		lstrSql = lstrSql & "   AND (`nf`.`codclas` >= 9000 AND `nf`.`codclas` <= 9999) " & vbCrLf
		lstrSql = lstrSql & "   AND (`nf`.`flgst` <> 'C' OR (`nf`.`flgst` = 'C' AND `nf`.`datast` > '" & VB6.Format(ldteBase, "yyyy-MM-dd") & "')) "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
					While Not lrstAux.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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrItens = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrFase = Trim(IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").value))
						ldblQtdkWh = BuscaVlrItem(lstrItens, "030201009")
						ldblVlrkWh = BuscaVlrItem(lstrItens, "030202009")
						'Para Buscar as informções para o T1 e para o T4
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrZona = Trim(IIf(IsDbNull(.Fields("zona").value), "000", .Fields("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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrLivro = Trim(IIf(IsDbNull(.Fields("livro").value), "00", .Fields("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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrConta = Trim(IIf(IsDbNull(.Fields("conta").value), "000000", .Fields("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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrClasse = Trim(IIf(IsDbNull(.Fields("codclas").value), "0000", .Fields("codclas").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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldteLAtual = CDate(IIf(IsDbNull(.Fields("dataleit").value), "1900-01-01", .Fields("dataleit").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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldteLAnter = CDate(IIf(IsDbNull(.Fields("dataleitant").value), "1900-01-01", .Fields("dataleitant").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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						llngUnidId = CInt(IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value))
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & "  FROM nis "
						lstrSql = lstrSql & " WHERE nis.unid_id = " & llngUnidId & " "
						If Me.chkNovoNis.CheckState = 1 Then
							'lstrSql = lstrSql & "   AND nis.data_alteracao >= '" & Strings.Format(ldteNovoBxRenda, "yyyy-MM-dd") & "'"
							lstrSql = lstrSql & "   AND nis.data_cadastro >= '" & VB6.Format(ldteNovoBxRenda, "yyyy-MM-dd") & "'"
						End If
						Call Conectar(lstrSql, lrstNis)
						With lrstNis
							If .State = ADODB.ObjectStateEnum.adStateOpen Then
								lblnNovoNis = (.RecordCount > 0)
								Call .Close()
							End If
						End With
						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO `" & istrTabela & "` SET "
						If lstrClasse = "9105" And lblnNovoNis Then
							lstrSql = lstrSql & "fase     = '" & lstrFase & "', "
							lstrSql = lstrSql & "kwhc     = " & ldblQtdkWh & ", "
							lstrSql = lstrSql & "t5_kwhc    = " & ldblQtdkWh & ", "
							lstrSql = lstrSql & "t6_kwhc    = 0, "
							lstrSql = lstrSql & "t7_kwhc    = 0, "
							lstrSql = lstrSql & "t8_kwhc    = 0, "
							lstrSql = lstrSql & "res_valor  = " & Replace(CStr(ldblVlrkWh), ",", ".") & ", "
							lstrSql = lstrSql & "t5_valor   = " & Replace(CStr(Residencial(ldblQtdkWh, ldteLAnter, ldteLAtual)), ",", ".") & ", "
							lstrSql = lstrSql & "t5_bxvalor = " & Replace(CStr(BaixaRenda(ldblQtdkWh, ldteLAnter, ldteLAtual)), ",", ".") & ", "
							lstrSql = lstrSql & "t6_bxvalor = 0, "
							lstrSql = lstrSql & "t7_bxvalor = 0, "
							lstrSql = lstrSql & "t6_valor   = 0, "
							lstrSql = lstrSql & "t7_valor   = 0, "
							lstrSql = lstrSql & "t8_bxvalor = 0, "
							lstrSql = lstrSql & "t8_valor   = 0; "
						ElseIf lstrClasse = "9101" Or lstrClasse = "9104" Or lstrClasse = "9106" Or (lstrClasse = "9105" And Not lblnNovoNis) Then 
							lstrSql = lstrSql & "fase     = '" & lstrFase & "', "
							lstrSql = lstrSql & "kwhc     = " & ldblQtdkWh & ", "
							lstrSql = lstrSql & "t5_kwhc    = 0, "
							lstrSql = lstrSql & "t6_kwhc    = 0, "
							lstrSql = lstrSql & "t7_kwhc    = 0, "
							lstrSql = lstrSql & "t8_kwhc    = 0, "
							lstrSql = lstrSql & "res_valor  = " & Replace(CStr(ldblVlrkWh), ",", ".") & ", "
							lstrSql = lstrSql & "t5_valor   = 0, "
							lstrSql = lstrSql & "t5_bxvalor = 0, "
							lstrSql = lstrSql & "t6_bxvalor = 0, "
							lstrSql = lstrSql & "t7_bxvalor = 0, "
							lstrSql = lstrSql & "t6_valor   = 0, "
							lstrSql = lstrSql & "t7_valor   = 0, "
							lstrSql = lstrSql & "t8_bxvalor = 0, "
							lstrSql = lstrSql & "t8_valor   = 0; "
						End If
						Call Executar(lstrSql)
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						System.Windows.Forms.Application.DoEvents()
					End While
					lblnContinua = True
				Else
					Call MsgBox("Não existem faturas de consumidores residenciais no mês selecionado!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
					MontaTabela = False
					lblnContinua = False
				End If
				Call .Close()
			End If
		End With
		If lblnContinua Then
			'Segunda Parte colocar em uma tabela todos os dados mastigados para facilitar no crystal
			llngLiRes = CInt(LeConfig("LIMITE_RESIDENCIAL_XML"))
			'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			'UPGRADE_WARNING: Couldn't resolve default property of object lstrFases. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			lstrFases = New Object(){" AND fase='M' ", " AND fase='B' ", " AND fase = 'T' "}
			If Me.chkOficioCircular120.CheckState = 1 Then
				'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				'UPGRADE_WARNING: Couldn't resolve default property of object lstrFxPla. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFxPla = New Object(){" Até 30       ", " De  31 até  50 ", " De  51 até 100 ", " De 101 até 220 ", " Acima 220    "}
				'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				'UPGRADE_WARNING: Couldn't resolve default property of object lstrFxXml. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFxXml = New Object(){"de01ate30", "de31ate50", "de51ate100", "de101ate220", "Acima220"}
				'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				'UPGRADE_WARNING: Couldn't resolve default property of object lstrFaixa. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFaixa = New Object(){" WHERE kwhc <=30 ", " WHERE kwhc >=31 AND kwhc <=50 ", " WHERE kwhc >=51 AND kwhc <=100 ", " WHERE kwhc >=101 AND kwhc <=220 ", " WHERE kwhc >220 "}
			Else
				'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				'UPGRADE_WARNING: Couldn't resolve default property of object lstrFxPla. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFxPla = New Object(){" Até 30       ", " De  31 até  79 ", " De  80 até 100 ", " De 101 até " & llngLiRes & " ", " De " & llngLiRes + 1 & " até 220 ", " Acima 220    "}
				'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				'UPGRADE_WARNING: Couldn't resolve default property of object lstrFxXml. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFxXml = New Object(){"de01ate30", "de31ate79", "de80ate100", "de101ate_limite_regional", "limite_regional_ate220", "Acima220"}
				'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				'UPGRADE_WARNING: Couldn't resolve default property of object lstrFaixa. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFaixa = New Object(){" WHERE kwhc <=30 ", " WHERE kwhc >=31 AND kwhc <=79 ", " WHERE kwhc >=80 AND kwhc <=100 ", " WHERE kwhc >=101 AND kwhc <=" & llngLiRes & " ", " WHERE kwhc >=" & llngLiRes + 1 & " AND kwhc<=220 ", " WHERE kwhc >220 "}
			End If
			For llngFases = 0 To 2
				For llngFaixa = LBound(lstrFaixa) To UBound(lstrFaixa)
					lstrSql = ""
					lstrSql = lstrSql & "SELECT t5_kwhc   , t6_kwhc   , t7_kwhc   , t8_kwhc, kwhc, "
					lstrSql = lstrSql & "       t5_valor  , t6_valor  , t7_valor  , t8_valor, res_valor, "
					lstrSql = lstrSql & "       t5_bxvalor, t6_bxvalor, t7_bxvalor, t8_bxvalor "
					lstrSql = lstrSql & "  FROM planilha_bxrenda "
					'UPGRADE_WARNING: Couldn't resolve default property of object lstrFases(llngFases). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					'UPGRADE_WARNING: Couldn't resolve default property of object lstrFaixa(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrSql = lstrSql & lstrFaixa(llngFaixa) & lstrFases(llngFases)
					Call Conectar(lstrSql, lrstAux)
					With lrstAux
						If .State = ADODB.ObjectStateEnum.adStateOpen Then
							If .RecordCount > 0 Then
								Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
								'
								llngReqtde = 0
								llngT5qtde = 0
								llngT6qtde = 0
								llngT7qtde = 0
								llngT8qtde = 0
								'
								ldblReskwhc = 0
								ldblT5kwhc = 0
								ldblT6kwhc = 0
								ldblT7kwhc = 0
								ldblT8kwhc = 0
								'
								ldblReskvlr = 0
								ldblT5kvlr = 0
								ldblT6kvlr = 0
								ldblT7kvlr = 0
								ldblT8kvlr = 0
								'
								ldblT5kbxv = 0
								ldblT6kbxv = 0
								ldblT7kbxv = 0
								ldblT8kbxv = 0
								While Not .EOF
									'quantidade de unidades consumidoras
									llngReqtde = llngReqtde + 1
									'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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									If CInt(IIf(IsDbNull(.Fields("t5_kwhc").value), 0, .Fields("t5_kwhc").value)) > 0 Then llngT5qtde = llngT5qtde + 1
									'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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									If CInt(IIf(IsDbNull(.Fields("t6_kwhc").value), 0, .Fields("t6_kwhc").value)) > 0 Then llngT6qtde = llngT6qtde + 1
									'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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									If CInt(IIf(IsDbNull(.Fields("t7_kwhc").value), 0, .Fields("t7_kwhc").value)) > 0 Then llngT7qtde = llngT7qtde + 1
									'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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									If CInt(IIf(IsDbNull(.Fields("t8_kwhc").value), 0, .Fields("t8_kwhc").value)) > 0 Then llngT8qtde = llngT8qtde + 1
									'kwh
									'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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblReskwhc = ldblReskwhc + CInt(IIf(IsDbNull(.Fields("kwhc").value), 0, .Fields("kwhc").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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT5kwhc = ldblT5kwhc + CInt(IIf(IsDbNull(.Fields("t5_kwhc").value), 0, .Fields("t5_kwhc").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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT6kwhc = ldblT6kwhc + CInt(IIf(IsDbNull(.Fields("t6_kwhc").value), 0, .Fields("t6_kwhc").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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT7kwhc = ldblT7kwhc + CInt(IIf(IsDbNull(.Fields("t7_kwhc").value), 0, .Fields("t7_kwhc").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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT8kwhc = ldblT8kwhc + CInt(IIf(IsDbNull(.Fields("t8_kwhc").value), 0, .Fields("t8_kwhc").value))
									'valor normal
									'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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblReskvlr = ldblReskvlr + CDbl(IIf(IsDbNull(.Fields("res_valor").value), 0, .Fields("res_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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT5kvlr = ldblT5kvlr + CDbl(IIf(IsDbNull(.Fields("t5_valor").value), 0, .Fields("t5_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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT6kvlr = ldblT6kvlr + CDbl(IIf(IsDbNull(.Fields("t6_valor").value), 0, .Fields("t6_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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT7kvlr = ldblT7kvlr + CDbl(IIf(IsDbNull(.Fields("t7_valor").value), 0, .Fields("t7_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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT8kvlr = ldblT8kvlr + CDbl(IIf(IsDbNull(.Fields("t8_valor").value), 0, .Fields("t8_valor").value))
									'valor baixa renda
									'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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT5kbxv = ldblT5kbxv + CDbl(IIf(IsDbNull(.Fields("t5_bxvalor").value), 0, .Fields("t5_bxvalor").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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT6kbxv = ldblT6kbxv + CDbl(IIf(IsDbNull(.Fields("t6_bxvalor").value), 0, .Fields("t6_bxvalor").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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT7kbxv = ldblT7kbxv + CDbl(IIf(IsDbNull(.Fields("t7_bxvalor").value), 0, .Fields("t7_bxvalor").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"'
									'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
									ldblT8kbxv = ldblT8kbxv + CDbl(IIf(IsDbNull(.Fields("t8_bxvalor").value), 0, .Fields("t8_bxvalor").value))
									Call .MoveNext()
									Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
								End While
							Else
								'
								llngReqtde = 0
								llngT5qtde = 0
								llngT6qtde = 0
								llngT7qtde = 0
								llngT8qtde = 0
								'
								ldblReskwhc = 0
								ldblT5kwhc = 0
								ldblT6kwhc = 0
								ldblT7kwhc = 0
								ldblT8kwhc = 0
								'
								ldblReskvlr = 0
								ldblT5kvlr = 0
								ldblT6kvlr = 0
								ldblT7kvlr = 0
								ldblT8kvlr = 0
								'
								ldblT5kbxv = 0
								ldblT6kbxv = 0
								ldblT7kbxv = 0
								ldblT8kbxv = 0
							End If
							Call .Close()
						End If
					End With
					lstrSql = ""
					lstrSql = lstrSql & "INSERT INTO " & istrTabela & "_total SET "
					lstrSql = lstrSql & "ordimp1  = " & llngFases & ", "
					lstrSql = lstrSql & "ordimp2  = " & llngFaixa & ", "
					'UPGRADE_WARNING: Couldn't resolve default property of object lstrFxPla(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrSql = lstrSql & "faixa    = '" & lstrFxPla(llngFaixa) & "', "
					'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrSql = lstrSql & "fase     = '" & IIf(llngFases = 0, "M", IIf(llngFases = 1, "B", "T")) & "', "
					lstrSql = lstrSql & "mesano   = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "', "
					'UPGRADE_WARNING: Couldn't resolve default property of object lstrFxXml(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrSql = lstrSql & "faixaxml = '" & lstrFxXml(llngFaixa) & "', "
					'Quantidades de U.C.
					lstrSql = lstrSql & "restotal  = " & llngReqtde & ", "
					lstrSql = lstrSql & "totbxqtde = " & (llngT5qtde + llngT6qtde + llngT7qtde + llngT8qtde) & ", "
					lstrSql = lstrSql & "t5qtde    = " & llngT5qtde & ", "
					lstrSql = lstrSql & "t6qtde    = " & llngT6qtde & ", "
					lstrSql = lstrSql & "t7qtde    = " & llngT7qtde & ", "
					lstrSql = lstrSql & "t8qtde    = " & llngT8qtde & ", "
					'Quantide de kWh
					lstrSql = lstrSql & "reskwhc    = " & Replace(CStr(ldblReskwhc / 1000), ",", ".") & ", "
					lstrSql = lstrSql & "totbxkhwc = " & Replace(CStr((ldblT6kwhc + ldblT7kwhc + ldblT5kwhc + ldblT8kwhc) / 1000), ",", ".") & ", "
					lstrSql = lstrSql & "t5kwhc    = " & Replace(CStr(ldblT5kwhc / 1000), ",", ".") & ", "
					lstrSql = lstrSql & "t6kwhc    = " & Replace(CStr(ldblT6kwhc / 1000), ",", ".") & ", "
					lstrSql = lstrSql & "t7kwhc    = " & Replace(CStr(ldblT7kwhc / 1000), ",", ".") & ", "
					lstrSql = lstrSql & "t8kwhc    = " & Replace(CStr(ldblT8kwhc / 1000), ",", ".") & ", "
					'Valor residencial normal
					lstrSql = lstrSql & "resvalr  = " & Replace(CStr(ldblReskvlr), ",", ".") & ", "
					lstrSql = lstrSql & "totvalr = " & Replace(CStr(ldblT5kvlr + ldblT6kvlr + ldblT7kvlr + ldblT8kvlr), ",", ".") & ", "
					lstrSql = lstrSql & "t5valr  = " & Replace(CStr(ldblT5kvlr), ",", ".") & ", "
					lstrSql = lstrSql & "t6valr  = " & Replace(CStr(ldblT6kvlr), ",", ".") & ", "
					lstrSql = lstrSql & "t7valr  = " & Replace(CStr(ldblT7kvlr), ",", ".") & ", "
					lstrSql = lstrSql & "t8valr  = " & Replace(CStr(ldblT8kvlr), ",", ".") & ", "
					'Valor resindencial baixa renda
					lstrSql = lstrSql & "totbxvalr = " & Replace(CStr(ldblT5kbxv + ldblT6kbxv + ldblT7kbxv + ldblT8kbxv), ",", ".") & ", "
					lstrSql = lstrSql & "t5bxvalr  = " & Replace(CStr(ldblT5kbxv), ",", ".") & ", "
					lstrSql = lstrSql & "t6bxvalr  = " & Replace(CStr(ldblT6kbxv), ",", ".") & ", "
					lstrSql = lstrSql & "t7bxvalr  = " & Replace(CStr(ldblT7kbxv), ",", ".") & ", "
					lstrSql = lstrSql & "t8bxvalr  = " & Replace(CStr(ldblT8kbxv), ",", ".") & " "
					Call Executar(lstrSql)
				Next llngFaixa
			Next llngFases
		End If
		'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
		'UPGRADE_NOTE: Object lrstNis 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"'
		lrstNis = Nothing
		Me.lblInfo.Text = "Pronto!"
		Call Me.lblInfo.Refresh()
		MontaTabela = True
	End Function
	
	Private Sub GerarXML()
		'Controle do Arquivo externo
		Dim lstrArq As String
		Dim lintARQ As Short
		Dim lintPar1 As Short
		Dim lintPar2 As Short
		'UPGRADE_WARNING: Lower bound of array lstrCab was changed from 1,0 to 0,0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim lstrCab(4, 5) As String
		Dim lstrCam(2, 5) As String
		Dim ldblRed As Double
		Dim ldblAum As Double
		Dim lstrTab As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		On Error GoTo ErroArquivo
		Me.lblInfo.Text = "Gerando o arquivo XML..."
		Call Me.lblInfo.Refresh()
		lstrTab = "   "
		'
		lstrCab(1, 0) = "<!--Numero de Unidades Consumidoras-->"
		lstrCab(1, 1) = "<!--Energia Faturada-->"
		lstrCab(1, 2) = "<!--Faturamento Real-->"
		'
		lstrCab(2, 0) = "CONSUMIDORES"
		lstrCab(2, 1) = "ENERGIA"
		lstrCab(2, 2) = "FATURAMENTO"
		'
		If Me.chk472.CheckState = 0 Then
			lstrCab(3, 0) = "<!--Residencial Total-->"
			lstrCab(3, 1) = "<!--Residencial T1-->"
			lstrCab(3, 2) = "<!--Baixa Renda Total-->"
			lstrCab(3, 3) = "<!--Baixa Renda T2-->"
			lstrCab(3, 4) = "<!--Baixa Renda T3-->"
			lstrCab(3, 5) = "<!--Baixa Renda T4-->"
		Else
			lstrCab(3, 0) = "<!--Residencial Total-->"
			lstrCab(3, 1) = "<!--Baixa Renda Total-->"
			lstrCab(3, 2) = "<!--Residencial T5-->"
			lstrCab(3, 3) = "<!--Baixa Renda T6-->"
			lstrCab(3, 4) = "<!--Baixa Renda T7-->"
			lstrCab(3, 5) = "<!--Baixa Renda T8-->"
		End If
		If Me.chk472.CheckState = 0 Then
			lstrCab(4, 0) = "total"
			lstrCab(4, 1) = "t1"
			lstrCab(4, 2) = "brtotal"
			lstrCab(4, 3) = "brt2"
			lstrCab(4, 4) = "brt3"
			lstrCab(4, 5) = "brt4"
		Else
			lstrCab(4, 0) = "total"
			lstrCab(4, 1) = "brtotal"
			lstrCab(4, 2) = "t5"
			lstrCab(4, 3) = "brt6"
			lstrCab(4, 4) = "brt7"
			lstrCab(4, 5) = "brt8"
		End If
		lstrCam(0, 0) = "restotal"
		lstrCam(0, 1) = "totbxqtde"
		lstrCam(0, 2) = "t5qtde"
		lstrCam(0, 3) = "t6qtde"
		lstrCam(0, 4) = "t7qtde"
		lstrCam(0, 5) = "t8qtde"
		'
		lstrCam(1, 1) = "totbxkhwc"
		lstrCam(1, 2) = "t5kwhc"
		lstrCam(1, 3) = "t6kwhc"
		lstrCam(1, 4) = "t7kwhc"
		lstrCam(1, 5) = "t8kwhc"
		'
		lstrCam(2, 1) = "totbxvalr"
		lstrCam(2, 2) = "t5bxvalr"
		lstrCam(2, 3) = "t6bxvalr"
		lstrCam(2, 4) = "t7bxvalr"
		lstrCam(2, 5) = "t8bxvalr"
		'
		lstrSql = ""
		lstrSql = lstrSql & "SELECT ordimp1, ordimp2, faixa, fase, mesano, faixaxml, restotal, "
		lstrSql = lstrSql & "       totbxqtde, t5qtde  , t6qtde  , t7qtde  , t8qtde  , "
		lstrSql = lstrSql & "       totbxkhwc, t5kwhc  , t6kwhc  , t7kwhc  , t8kwhc  , "
		lstrSql = lstrSql & "       totbxvalr, t5bxvalr, t6bxvalr, t7bxvalr, t8bxvalr, "
		lstrSql = lstrSql & "       totvalr  , t5valr  , t6valr  , t7valr  , t8valr   "
		lstrSql = lstrSql & "  FROM " & istrTabela & "_total "
		lstrSql = lstrSql & " WHERE `mesano` = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "' "
		lstrSql = lstrSql & "ORDER BY ordimp2 ASC, ordimp1 ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					lintARQ = FreeFile
					lstrArq = Me.txtDir.Text & IIf(VB.Right(Trim(Me.txtDir.Text), 1) <> "\", "\", "") & Me.txtArquivo.Text
					FileOpen(lintARQ, lstrArq, OpenMode.Output)
					'145, 146 ou 180 para o caracter crase
					PrintLine(lintARQ, Chr(239) & Chr(187) & Chr(191) & "<DMR versao=""V" & VB6.Format(Me.txtVersao.Text, "00") & """ mes=""" & VB6.Format(Me.dtpMesAno.Month, "00") & """ ano=""" & Me.dtpMesAno.Year & """ codigo=""" & gstrEmpresa_CodRes89 & """ empresa=""" & gstrEmpresa_Sigla & """>")
					PrintLine(lintARQ, lstrTab & "<!--Inicio dos valores do DMR-->")
					For lintPar1 = 0 To 2
						PrintLine(lintARQ, lstrTab & lstrCab(1, lintPar1))
						PrintLine(lintARQ, lstrTab & "<" & lstrCab(2, lintPar1) & ">")
						For lintPar2 = IIf(lintPar1 = 0, 0, 1) To 5
							PrintLine(lintARQ, lstrTab & lstrTab & lstrCab(3, lintPar2))
							PrintLine(lintARQ, lstrTab & lstrTab & "<" & lstrCab(4, lintPar2) & ">")
							Call MontaDetalheArquivoXML(lintARQ, lrstAux, lstrCam(lintPar1, lintPar2), lintPar1, ldblRed, ldblAum)
							PrintLine(lintARQ, lstrTab & lstrTab & "</" & lstrCab(4, lintPar2) & ">")
						Next lintPar2
						PrintLine(lintARQ, lstrTab & "</" & lstrCab(2, lintPar1) & ">")
					Next lintPar1
					If Me.chk472.CheckState = 0 Then
						ldblRed = 0
						ldblAum = 0
					End If
					PrintLine(lintARQ, lstrTab & "<!--Valor Mensal da Redu" & Chr(195) & Chr(167) & Chr(195) & Chr(163) & "o de Receita-->")
					PrintLine(lintARQ, lstrTab & "<VMRR>" & FormatNumber(ldblRed, 2) & "</VMRR>")
					PrintLine(lintARQ, lstrTab & "<!--Valor Mensal do Aumento de Receita-->")
					PrintLine(lintARQ, lstrTab & "<VMAR>" & FormatNumber(ldblAum, 2) & "</VMAR>")
					PrintLine(lintARQ, lstrTab & "<!--Valor Mensal da Diferen" & Chr(195) & Chr(167) & "a de Receita -->")
					PrintLine(lintARQ, lstrTab & "<VMDR>" & FormatNumber(ldblRed - ldblAum, 2) & "</VMDR>")
					PrintLine(lintARQ, "</DMR>")
					FileClose(lintARQ)
					Me.lblInfo.Text = " Arquivo gerado com sucesso!"
					Call Me.lblInfo.Refresh()
				Else
					Call MsgBox("Não existe dados para gerar o XML do mês selecionando!", MsgBoxStyle.Information, "Mensagem do Sistema")
				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
ErroArquivo: 
		If Err.Number > 0 Then
			Select Case Err.Number
				Case 76
					Call MsgBox("Não foi possível encontrar o caminho para gravar o arquivo!", MsgBoxStyle.Exclamation, "Mensagem do Sistema")
				Case Else
					Call MsgBox(ErrorToString(), MsgBoxStyle.Exclamation, "Mensagem do Sistema")
			End Select
		End If
	End Sub
	
	Private Sub MontaDetalheArquivoXML(ByRef aintArq As Short, ByVal arstAux As ADODB.Recordset, ByVal astrCam As String, ByVal aintTip As Short, ByRef adblRed As Double, ByRef adblAum As Double)
		Dim lstrFai As String
		Dim lstrFas As String
		Dim ldblAux As Double
		Dim ldblTot As Double
		Dim lintCtr As Short
		Dim ldblToN As Double
		Dim ldblToB As Double
		Dim lstrTab As String
		lstrTab = "   "
		ldblTot = 0
		ldblToN = 0
		ldblToB = 0
		With arstAux
			Call .MoveFirst()
			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"'
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lintCtr = CShort(IIf(IsDbNull(.Fields("ordimp1").value), 0, .Fields("ordimp1").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"'
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFai = IIf(IsDbNull(.Fields("faixaxml").value), "", .Fields("faixaxml").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"'
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFas = Trim(IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").value))
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrFas = IIf(lstrFas = "M", "monofasico", IIf(lstrFas = "B", "bifasico", "trifasico"))
				'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"'
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				ldblAux = CDbl(IIf(IsDbNull(.Fields(astrCam).value), 0, .Fields(astrCam).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"'
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				ldblToN = ldblToN + CDbl(IIf(IsDbNull(.Fields("totvalr").value), 0, .Fields("totvalr").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"'
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				ldblToB = ldblToB + CDbl(IIf(IsDbNull(.Fields("totbxvalr").value), 0, .Fields("totbxvalr").value))
				If lintCtr = 0 Then
					PrintLine(aintArq, lstrTab & lstrTab & lstrTab & "<" & lstrFai & ">")
				End If
				'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				PrintLine(aintArq, lstrTab & lstrTab & lstrTab & lstrTab & "<" & lstrFas & ">" & FormatNumber(ldblAux, IIf(aintTip = 0, 0, IIf(aintTip = 1, 3, 2))) & "</" & lstrFas & ">")
				If lintCtr = 2 Then
					PrintLine(aintArq, lstrTab & lstrTab & lstrTab & "</" & lstrFai & ">")
				End If
				ldblTot = ldblTot + ldblAux
				Call .MoveNext()
			End While
		End With
		If Me.chk472.CheckState = 0 Then
			adblRed = 0
			adblAum = 0
		Else
			adblRed = ldblToN
			adblAum = ldblToB
		End If
		'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		PrintLine(aintArq, lstrTab & lstrTab & lstrTab & "<TOTAL>" & FormatNumber(ldblTot, IIf(aintTip = 0, 0, IIf(aintTip = 1, 3, 2))) & "</TOTAL>")
	End Sub
	
	Private Sub GerarCSV()
		Dim lstrFaixa As String
		Dim lstrFase As String
		Dim lstrFaixa_Ant As String
		Dim lstrFase_Ant As String
		
		'Quantide de consumidores
		Dim llngRes_Qtd_Nr As Integer
		Dim llngBxR_Qtd_To As Integer
		Dim llngBxR_Qtd_T5 As Integer
		Dim llngBxR_Qtd_T6 As Integer
		Dim llngBxR_Qtd_T7 As Integer
		Dim llngBxR_Qtd_T8 As Integer
		'Totalizador da qtde de consumidores
		Dim llngRes_Tot_Qtd_Nr As Integer
		Dim llngBxR_Tot_Qtd_To As Integer
		Dim llngBxR_Tot_Qtd_T5 As Integer
		Dim llngBxR_Tot_Qtd_T6 As Integer
		Dim llngBxR_Tot_Qtd_T7 As Integer
		Dim llngBxR_Tot_Qtd_T8 As Integer
		'Sub-Totalizador da qtde de consumidores
		Dim llngRes_SbT_Qtd_Nr As Integer
		Dim llngBxR_SbT_Qtd_To As Integer
		Dim llngBxR_SbT_Qtd_T5 As Integer
		Dim llngBxR_SbT_Qtd_T6 As Integer
		Dim llngBxR_SbT_Qtd_T7 As Integer
		Dim llngBxR_SbT_Qtd_T8 As Integer
		'Quantidade de MWh
		Dim ldblBxR_Qtd_Res As Double
		Dim ldblBxR_Qtd_To As Double
		Dim ldblBxR_Qtd_T5 As Double
		Dim ldblBxR_Qtd_T6 As Double
		Dim ldblBxR_Qtd_T7 As Double
		Dim ldblBxR_Qtd_T8 As Double
		'Totalizador de MWh
		Dim ldblBxR_Tot_Qtd_Res As Double
		Dim ldblBxR_Tot_Qtd_To As Double
		Dim ldblBxR_Tot_Qtd_T5 As Double
		Dim ldblBxR_Tot_Qtd_T6 As Double
		Dim ldblBxR_Tot_Qtd_T7 As Double
		Dim ldblBxR_Tot_Qtd_T8 As Double
		'Sub-Totalizador de MWh
		Dim ldblBxR_SbT_Qtd_Res As Double
		Dim ldblBxR_SbT_Qtd_To As Double
		Dim ldblBxR_SbT_Qtd_T5 As Double
		Dim ldblBxR_SbT_Qtd_T6 As Double
		Dim ldblBxR_SbT_Qtd_T7 As Double
		Dim ldblBxR_SbT_Qtd_T8 As Double
		'Valor do MWh do bx
		Dim ldblBxR_Vlr_To As Double
		Dim ldblBxR_Vlr_T5 As Double
		Dim ldblBxR_Vlr_T6 As Double
		Dim ldblBxR_Vlr_T7 As Double
		Dim ldblBxR_Vlr_T8 As Double
		'Valor do MWh do normal
		Dim ldblRes_Vlr_Res As Double
		Dim ldblRes_Vlr_To As Double
		Dim ldblRes_Vlr_T5 As Double
		Dim ldblRes_Vlr_T6 As Double
		Dim ldblRes_Vlr_T7 As Double
		Dim ldblRes_Vlr_T8 As Double
		'Totalizador MWh do bx
		Dim ldblRes_Tot_Vlr_Res As Double
		Dim ldblBxR_Tot_Vlr_To As Double
		Dim ldblBxR_Tot_Vlr_T5 As Double
		Dim ldblBxR_Tot_Vlr_T6 As Double
		Dim ldblBxR_Tot_Vlr_T7 As Double
		Dim ldblBxR_Tot_Vlr_T8 As Double
		'Sub-Totalizador MWh do bx
		Dim ldblRes_SbT_Vlr_Res As Double
		Dim ldblBxR_SbT_Vlr_To As Double
		Dim ldblBxR_SbT_Vlr_T5 As Double
		Dim ldblBxR_SbT_Vlr_T6 As Double
		Dim ldblBxR_SbT_Vlr_T7 As Double
		Dim ldblBxR_SbT_Vlr_T8 As Double
		'Totalizador MWh do normal
		Dim ldblRes_Tot_Vlr_To As Double
		Dim ldblRes_Tot_Vlr_T5 As Double
		Dim ldblRes_Tot_Vlr_T6 As Double
		Dim ldblRes_Tot_Vlr_T7 As Double
		Dim ldblRes_Tot_Vlr_T8 As Double
		'Receita
		Dim ldblReducao As Double
		Dim ldblAumento As Double
		'Controle do Arquivo externo
		Dim lstrArq As String
		Dim lintARQ As Short
		Dim lstrLin As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		On Error GoTo ErroArquivo
		Me.lblInfo.Text = "Gerando o arquivo CSV..."
		Call Me.lblInfo.Refresh()
		lstrSql = ""
		lstrSql = lstrSql & "SELECT ordimp1, ordimp2, faixa, fase, mesano, faixaxml, restotal, "
		lstrSql = lstrSql & "       totbxqtde, t5qtde  , t6qtde  , t7qtde  , t8qtde  , "
		lstrSql = lstrSql & "       totbxkhwc, t5kwhc  , t6kwhc  , t7kwhc  , t8kwhc  , reskwhc, "
		lstrSql = lstrSql & "       totbxvalr, t5bxvalr, t6bxvalr, t7bxvalr, t8bxvalr, "
		lstrSql = lstrSql & "       totvalr  , t5valr  , t6valr  , t7valr  , t8valr, resvalr   "
		lstrSql = lstrSql & "  FROM " & istrTabela & "_total "
		lstrSql = lstrSql & " WHERE `mesano` = '" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & "' "
		lstrSql = lstrSql & "ORDER BY ordimp2 ASC, ordimp1 ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					lintARQ = FreeFile
					lstrArq = Me.txtDir.Text & IIf(VB.Right(Trim(Me.txtDir.Text), 1) <> "\", "\", "") & Replace(Me.txtArquivo.Text, ".XML", ".CSV")
					FileOpen(lintARQ, lstrArq, OpenMode.Output)
					'Cabeçalho da planilha
					PrintLine(lintARQ, "CONCESSIONÁRIA: " & gstrEmpresa_Nome & ";")
					PrintLine(lintARQ, ";")
					'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"'
					PrintLine(lintARQ, "MÊS/ANO:;" & IIf(IsDbNull(.Fields("mesano").value), "", .Fields("mesano").value) & ";")
					If Me.chkOficioCircular120.CheckState = 1 Then
						PrintLine(lintARQ, ";;" & "NÚMERO DE UNIDADES CONSUMIDORAS" & ";;;;;;ENERGIA FATURADA (MWh);;;;;;FATURAMENTO REAL (R$);")
					Else
						PrintLine(lintARQ, ";;" & "NÚMERO DE UNIDADES CONSUMIDORAS" & ";;;;;;ENERGIA FATURADA (MWh);;;;;FATURAMENTO REAL (R$);")
					End If
					lstrLin = ""
					lstrLin = lstrLin & "Faixa de Consumo (KWh);Tipo de Ligação;"
					lstrLin = lstrLin & "Residencial Total;Baixa Renda Total (T5+T6+T7+T8);Novos (T5);Bx Renda Indígena (T6);Bx Renda Quilombola (T7);Multi-Familiares (T8);"
					If Me.chkOficioCircular120.CheckState = 1 Then
						lstrLin = lstrLin & "Residencial Total;Baixa Renda Total (T5+T6+T7+T8);Novos (T5);Bx Renda Indígena (T6);Bx Renda Quilombola (T7);Multi-Familiares (T8);"
						lstrLin = lstrLin & "Residencial Total;Baixa Renda Total (T5+T6+T7+T8);Novos (T5);Bx Renda Indígena (T6);Bx Renda Quilombola (T7);Multi-Familiares (T8);"
					Else
						lstrLin = lstrLin & "Baixa Renda Total (T5+T6+T7+T8);Novos (T5);Bx Renda Indígena (T6);Bx Renda Quilombola (T7);Multi-Familiares (T8);"
						lstrLin = lstrLin & "Baixa Renda Total (T5+T6+T7+T8);Novos (T5);Bx Renda Indígena (T6);Bx Renda Quilombola (T7);Multi-Familiares (T8);"
					End If
					PrintLine(lintARQ, lstrLin)
					'Zerando os totalizadores
					'Quantide de consumidores
					llngRes_Tot_Qtd_Nr = 0
					llngBxR_Tot_Qtd_T5 = 0
					llngBxR_Tot_Qtd_To = 0
					llngBxR_Tot_Qtd_T6 = 0
					llngBxR_Tot_Qtd_T7 = 0
					llngBxR_Tot_Qtd_T8 = 0
					'sub-Quantide de consumidores
					llngRes_SbT_Qtd_Nr = 0
					llngBxR_SbT_Qtd_T5 = 0
					llngBxR_SbT_Qtd_To = 0
					llngBxR_SbT_Qtd_T6 = 0
					llngBxR_SbT_Qtd_T7 = 0
					llngBxR_SbT_Qtd_T8 = 0
					'Quantidade de MWh
					ldblBxR_Tot_Qtd_Res = 0
					ldblBxR_Tot_Qtd_T5 = 0
					ldblBxR_Tot_Qtd_To = 0
					ldblBxR_Tot_Qtd_T6 = 0
					ldblBxR_Tot_Qtd_T7 = 0
					ldblBxR_Tot_Qtd_T8 = 0
					'sub qtde
					ldblBxR_SbT_Qtd_Res = 0
					ldblBxR_SbT_Qtd_T5 = 0
					ldblBxR_SbT_Qtd_To = 0
					ldblBxR_SbT_Qtd_T6 = 0
					ldblBxR_SbT_Qtd_T7 = 0
					ldblBxR_SbT_Qtd_T8 = 0
					'Valor MWH
					ldblRes_Tot_Vlr_Res = 0
					'
					ldblBxR_Tot_Vlr_To = 0
					ldblBxR_Tot_Vlr_T5 = 0
					ldblBxR_Tot_Vlr_T6 = 0
					ldblBxR_Tot_Vlr_T7 = 0
					ldblBxR_Tot_Vlr_T8 = 0
					'
					ldblRes_Tot_Vlr_To = 0
					ldblRes_Tot_Vlr_T5 = 0
					ldblRes_Tot_Vlr_T6 = 0
					ldblRes_Tot_Vlr_T7 = 0
					ldblRes_Tot_Vlr_T8 = 0
					'Sub-Valor MWH
					ldblRes_SbT_Vlr_Res = 0
					'
					ldblBxR_SbT_Vlr_To = 0
					ldblBxR_SbT_Vlr_T5 = 0
					ldblBxR_SbT_Vlr_T6 = 0
					ldblBxR_SbT_Vlr_T7 = 0
					ldblBxR_SbT_Vlr_T8 = 0
					'
					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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrFaixa = IIf(IsDbNull(.Fields("faixa").value), "", .Fields("faixa").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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrFase = IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").value)
						'Quantidade de Consumidores
						'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"'
						llngRes_Qtd_Nr = CInt(IIf(IsDbNull(.Fields("restotal").value), 0, .Fields("restotal").value)) 'Residenciais
						'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"'
						llngBxR_Qtd_To = CInt(IIf(IsDbNull(.Fields("totbxqtde").value), 0, .Fields("totbxqtde").value)) 'Total Baixa Renda
						'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"'
						llngBxR_Qtd_T5 = CInt(IIf(IsDbNull(.Fields("t5qtde").value), 0, .Fields("t5qtde").value)) 'novo nis
						'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"'
						llngBxR_Qtd_T6 = CInt(IIf(IsDbNull(.Fields("t6qtde").value), 0, .Fields("t6qtde").value)) 'Indígena
						'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"'
						llngBxR_Qtd_T7 = CInt(IIf(IsDbNull(.Fields("t7qtde").value), 0, .Fields("t7qtde").value)) 'Quilomola
						'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"'
						llngBxR_Qtd_T8 = CInt(IIf(IsDbNull(.Fields("t8qtde").value), 0, .Fields("t8qtde").value)) 'Multi-Familiares
						'Totalizador da qtde de consumidores
						llngRes_Tot_Qtd_Nr = llngRes_Tot_Qtd_Nr + llngRes_Qtd_Nr
						llngBxR_Tot_Qtd_To = llngBxR_Tot_Qtd_To + llngBxR_Qtd_To
						llngBxR_Tot_Qtd_T5 = llngBxR_Tot_Qtd_T5 + llngBxR_Qtd_T5
						llngBxR_Tot_Qtd_T6 = llngBxR_Tot_Qtd_T6 + llngBxR_Qtd_T6
						llngBxR_Tot_Qtd_T7 = llngBxR_Tot_Qtd_T7 + llngBxR_Qtd_T7
						llngBxR_Tot_Qtd_T8 = llngBxR_Tot_Qtd_T8 + llngBxR_Qtd_T8
						'subTotalizador da qtde de consumidores
						llngRes_SbT_Qtd_Nr = llngRes_SbT_Qtd_Nr + llngRes_Qtd_Nr
						llngBxR_SbT_Qtd_To = llngBxR_SbT_Qtd_To + llngBxR_Qtd_To
						llngBxR_SbT_Qtd_T5 = llngBxR_SbT_Qtd_T5 + llngBxR_Qtd_T5
						llngBxR_SbT_Qtd_T6 = llngBxR_SbT_Qtd_T6 + llngBxR_Qtd_T6
						llngBxR_SbT_Qtd_T7 = llngBxR_SbT_Qtd_T7 + llngBxR_Qtd_T7
						llngBxR_SbT_Qtd_T8 = llngBxR_SbT_Qtd_T8 + llngBxR_Qtd_T8
						'Quantidade de MWh
						'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"'
						ldblBxR_Qtd_To = CDbl(IIf(IsDbNull(.Fields("totbxkhwc").value), 0, .Fields("totbxkhwc").value)) 'Total Baixa Renda
						'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"'
						ldblBxR_Qtd_T5 = CDbl(IIf(IsDbNull(.Fields("t5kwhc").value), 0, .Fields("t5kwhc").value)) 'novo nis
						'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"'
						ldblBxR_Qtd_T6 = CDbl(IIf(IsDbNull(.Fields("t6kwhc").value), 0, .Fields("t6kwhc").value)) 'indígena
						'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"'
						ldblBxR_Qtd_T7 = CDbl(IIf(IsDbNull(.Fields("t7kwhc").value), 0, .Fields("t7kwhc").value)) 'quilombola
						'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"'
						ldblBxR_Qtd_T8 = CDbl(IIf(IsDbNull(.Fields("t8kwhc").value), 0, .Fields("t8kwhc").value)) 'multi-familiares
						'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"'
						ldblBxR_Qtd_Res = CDbl(IIf(IsDbNull(.Fields("reskwhc").value), 0, .Fields("reskwhc").value)) 'todos os residenciais
						'Totalizador de MWh
						ldblBxR_Tot_Qtd_To = ldblBxR_Tot_Qtd_To + ldblBxR_Qtd_To
						ldblBxR_Tot_Qtd_T5 = ldblBxR_Tot_Qtd_T5 + ldblBxR_Qtd_T5
						ldblBxR_Tot_Qtd_T6 = ldblBxR_Tot_Qtd_T6 + ldblBxR_Qtd_T6
						ldblBxR_Tot_Qtd_T7 = ldblBxR_Tot_Qtd_T7 + ldblBxR_Qtd_T7
						ldblBxR_Tot_Qtd_T8 = ldblBxR_Tot_Qtd_T8 + ldblBxR_Qtd_T8
						ldblBxR_Tot_Qtd_Res = ldblBxR_Tot_Qtd_Res + ldblBxR_Qtd_Res
						'sub-Totalizador de MWh
						ldblBxR_SbT_Qtd_To = ldblBxR_SbT_Qtd_To + ldblBxR_Qtd_To
						ldblBxR_SbT_Qtd_T5 = ldblBxR_SbT_Qtd_T5 + ldblBxR_Qtd_T5
						ldblBxR_SbT_Qtd_T6 = ldblBxR_SbT_Qtd_T6 + ldblBxR_Qtd_T6
						ldblBxR_SbT_Qtd_T7 = ldblBxR_SbT_Qtd_T7 + ldblBxR_Qtd_T7
						ldblBxR_SbT_Qtd_T8 = ldblBxR_SbT_Qtd_T8 + ldblBxR_Qtd_T8
						ldblBxR_SbT_Qtd_Res = ldblBxR_SbT_Qtd_Res + ldblBxR_Qtd_Res
						'Valor do MWh do bx
						'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"'
						ldblBxR_Vlr_To = CDbl(IIf(IsDbNull(.Fields("totbxvalr").value), 0, .Fields("totbxvalr").value)) 'Total Baixa Renda
						'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"'
						ldblBxR_Vlr_T5 = CDbl(IIf(IsDbNull(.Fields("t5bxvalr").value), 0, .Fields("t5bxvalr").value)) 'novo nis
						'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"'
						ldblBxR_Vlr_T6 = CDbl(IIf(IsDbNull(.Fields("t6bxvalr").value), 0, .Fields("t6bxvalr").value)) 'indígena
						'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"'
						ldblBxR_Vlr_T7 = CDbl(IIf(IsDbNull(.Fields("t7bxvalr").value), 0, .Fields("t7bxvalr").value)) 'quilombola
						'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"'
						ldblBxR_Vlr_T8 = CDbl(IIf(IsDbNull(.Fields("t8bxvalr").value), 0, .Fields("t8bxvalr").value)) 'multi-familiares
						'Valor do MWh do normal
						'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"'
						ldblRes_Vlr_To = CDbl(IIf(IsDbNull(.Fields("totvalr").value), 0, .Fields("totvalr").value)) 'Total resindencial normal
						'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"'
						ldblRes_Vlr_T5 = CDbl(IIf(IsDbNull(.Fields("t5valr").value), 0, .Fields("t5valr").value)) 'novo nis
						'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"'
						ldblRes_Vlr_T6 = CDbl(IIf(IsDbNull(.Fields("t6valr").value), 0, .Fields("t6valr").value)) 'indígena
						'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"'
						ldblRes_Vlr_T7 = CDbl(IIf(IsDbNull(.Fields("t7valr").value), 0, .Fields("t7valr").value)) 'quilombola
						'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"'
						ldblRes_Vlr_T8 = CDbl(IIf(IsDbNull(.Fields("t8valr").value), 0, .Fields("t8valr").value)) 'multi-familiares
						'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"'
						ldblRes_Vlr_Res = CDbl(IIf(IsDbNull(.Fields("resvalr").value), 0, .Fields("resvalr").value)) 'todos os residenciais
						'Totalizador MWh do bx
						ldblBxR_Tot_Vlr_To = ldblBxR_Tot_Vlr_To + ldblBxR_Vlr_To
						ldblBxR_Tot_Vlr_T5 = ldblBxR_Tot_Vlr_T5 + ldblBxR_Vlr_T5
						ldblBxR_Tot_Vlr_T6 = ldblBxR_Tot_Vlr_T6 + ldblBxR_Vlr_T6
						ldblBxR_Tot_Vlr_T7 = ldblBxR_Tot_Vlr_T7 + ldblBxR_Vlr_T7
						ldblBxR_Tot_Vlr_T8 = ldblBxR_Tot_Vlr_T8 + ldblBxR_Vlr_T8
						ldblRes_Tot_Vlr_Res = ldblRes_Tot_Vlr_Res + ldblRes_Vlr_Res
						'Totalizador MWh do normal
						ldblRes_Tot_Vlr_To = ldblRes_Tot_Vlr_To + ldblRes_Vlr_To
						ldblRes_Tot_Vlr_T5 = ldblRes_Tot_Vlr_T5 + ldblRes_Vlr_T5
						ldblRes_Tot_Vlr_T6 = ldblRes_Tot_Vlr_T6 + ldblRes_Vlr_T6
						ldblRes_Tot_Vlr_T7 = ldblRes_Tot_Vlr_T7 + ldblRes_Vlr_T7
						ldblRes_Tot_Vlr_T8 = ldblRes_Tot_Vlr_T8 + ldblRes_Vlr_T8
						'
						'SubTotalizador MWh do bx
						ldblRes_SbT_Vlr_Res = ldblRes_SbT_Vlr_Res + ldblRes_Vlr_Res
						ldblBxR_SbT_Vlr_To = ldblBxR_SbT_Vlr_To + ldblBxR_Vlr_To
						ldblBxR_SbT_Vlr_T5 = ldblBxR_SbT_Vlr_T5 + ldblBxR_Vlr_T5
						ldblBxR_SbT_Vlr_T6 = ldblBxR_SbT_Vlr_T6 + ldblBxR_Vlr_T6
						ldblBxR_SbT_Vlr_T7 = ldblBxR_SbT_Vlr_T7 + ldblBxR_Vlr_T7
						ldblBxR_SbT_Vlr_T8 = ldblBxR_SbT_Vlr_T8 + ldblBxR_Vlr_T8
						'Monta Linha da planilha
						lstrLin = ""
						If lstrFaixa_Ant <> lstrFaixa Then
							lstrLin = lstrLin & lstrFaixa & ";"
							lstrFaixa_Ant = lstrFaixa
						Else
							lstrLin = lstrLin & ";"
						End If
						'UPGRADE_WARNING: Couldn't resolve default property of object Switch(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrLin = lstrLin & VB.Switch(lstrFase = "M", "Monfásico", lstrFase = "B", "Bifásico", lstrFase = "T", "Trifásico") & ";"
						lstrLin = lstrLin & FormatNumber(llngRes_Qtd_Nr, 0) & ";"
						lstrLin = lstrLin & FormatNumber(llngBxR_Qtd_To, 0) & ";"
						lstrLin = lstrLin & FormatNumber(llngBxR_Qtd_T5, 0) & ";"
						lstrLin = lstrLin & FormatNumber(llngBxR_Qtd_T6, 0) & ";"
						lstrLin = lstrLin & FormatNumber(llngBxR_Qtd_T7, 0) & ";"
						lstrLin = lstrLin & FormatNumber(llngBxR_Qtd_T8, 0) & ";"
						If Me.chkOficioCircular120.CheckState = 1 Then
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_Res, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_To, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T5, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T6, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T7, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T8, 3) & ";"
							'
							lstrLin = lstrLin & FormatNumber(ldblRes_Vlr_Res, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_To, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T5, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T6, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T7, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T8, 2) & ";"
						Else
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_To, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T5, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T6, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T7, 3) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Qtd_T8, 3) & ";"
							'
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_To, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T5, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T6, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T7, 2) & ";"
							lstrLin = lstrLin & FormatNumber(ldblBxR_Vlr_T8, 2) & ";"
						End If
						PrintLine(lintARQ, lstrLin)
						If lstrFase = "T" Then
							lstrLin = ""
							lstrLin = lstrLin & ";Sub-Total;"
							lstrLin = lstrLin & FormatNumber(llngRes_SbT_Qtd_Nr, 0) & ";"
							lstrLin = lstrLin & FormatNumber(llngBxR_SbT_Qtd_To, 0) & ";"
							lstrLin = lstrLin & FormatNumber(llngBxR_SbT_Qtd_T5, 0) & ";"
							lstrLin = lstrLin & FormatNumber(llngBxR_SbT_Qtd_T6, 0) & ";"
							lstrLin = lstrLin & FormatNumber(llngBxR_SbT_Qtd_T7, 0) & ";"
							lstrLin = lstrLin & FormatNumber(llngBxR_SbT_Qtd_T8, 0) & ";"
							If Me.chkOficioCircular120.CheckState = 1 Then
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_Res, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_To, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T5, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T6, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T7, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T8, 3) & ";"
								'
								lstrLin = lstrLin & FormatNumber(ldblRes_SbT_Vlr_Res, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_To, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T5, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T6, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T7, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T8, 2) & ";"
							Else
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_To, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T5, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T6, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T7, 3) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Qtd_T8, 3) & ";"
								'
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_To, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T5, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T6, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T7, 2) & ";"
								lstrLin = lstrLin & FormatNumber(ldblBxR_SbT_Vlr_T8, 2) & ";"
							End If
							PrintLine(lintARQ, lstrLin)
							llngRes_SbT_Qtd_Nr = 0
							llngBxR_SbT_Qtd_To = 0
							llngBxR_SbT_Qtd_T5 = 0
							llngBxR_SbT_Qtd_T6 = 0
							llngBxR_SbT_Qtd_T7 = 0
							llngBxR_SbT_Qtd_T8 = 0
							ldblBxR_SbT_Qtd_Res = 0
							ldblBxR_SbT_Qtd_To = 0
							ldblBxR_SbT_Qtd_T5 = 0
							ldblBxR_SbT_Qtd_T6 = 0
							ldblBxR_SbT_Qtd_T7 = 0
							ldblBxR_SbT_Qtd_T8 = 0
							ldblRes_SbT_Vlr_Res = 0
							ldblBxR_SbT_Vlr_To = 0
							ldblBxR_SbT_Vlr_T5 = 0
							ldblBxR_SbT_Vlr_T6 = 0
							ldblBxR_SbT_Vlr_T7 = 0
							ldblBxR_SbT_Vlr_T8 = 0
							'Print #lintARQ, ";"
						End If
						Call .MoveNext()
					End While
					PrintLine(lintARQ, ";")
					lstrLin = ""
					lstrLin = lstrLin & "Total;;"
					lstrLin = lstrLin & FormatNumber(llngRes_Tot_Qtd_Nr, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_To, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_T5, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_T6, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_T7, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_T8, 0) & ";"
					If Me.chkOficioCircular120.CheckState = 1 Then
						lstrLin = lstrLin & FormatNumber(ldblBxR_Tot_Qtd_Res, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_To, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T5, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T6, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T7, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T8, 3) & ";"
						lstrLin = lstrLin & FormatNumber(ldblRes_Tot_Vlr_Res, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_To, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T5, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T6, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T7, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T8, 2) & ";"
					Else
						lstrLin = lstrLin & FormatNumber(ldblBxR_Tot_Qtd_To, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T5, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T6, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T7, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T8, 3) & ";"
						lstrLin = lstrLin & FormatNumber(ldblBxR_Tot_Vlr_To, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T5, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T6, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T7, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T8, 2) & ";"
					End If
					PrintLine(lintARQ, lstrLin)
					PrintLine(lintARQ, ";")
					'Calculo do Aumento e Redução
					If Me.chk472.CheckState = 0 Then
						ldblReducao = 0
						ldblAumento = 0
					Else
						ldblReducao = ldblRes_Tot_Vlr_To
						ldblAumento = ldblBxR_Tot_Vlr_To
						If VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "03/2010" Then
							ldblReducao = 4404.91
						End If
					End If
					PrintLine(lintARQ, "Valor Mensal da Redução da Receita (R$);;;;;" & FormatNumber(ldblReducao, 2))
					PrintLine(lintARQ, "Valor Mensal do Aumento da Receita (R$);;;;;" & FormatNumber(ldblAumento, 2))
					PrintLine(lintARQ, ";")
					PrintLine(lintARQ, "Valor Mensal da Diferença da Receita (R$);;;;;" & FormatNumber(ldblReducao - ldblAumento, 2))
					FileClose(lintARQ)
					Me.lblInfo.Text = " Arquivo gerado com sucesso!"
					Call Me.lblInfo.Refresh()
				Else
					Call MsgBox("Não existe dados para gerar o CSV do mês selecionando!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
				Call .Close()
			End If
		End With
ErroArquivo: 
		If Err.Number <> 0 Then
			Select Case Err.Number
				Case 76
					Call MsgBox("Não foi possível encontrar o caminho para gravar o arquivo!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
				Case Else
					Call MsgBox(ErrorToString(), MsgBoxStyle.Exclamation, gstrTituloMensagem)
			End Select
		End If
		'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 EmitirPlanilha()
		With Me.crpAux
			.ReportFileName = My.Application.Info.DirectoryPath & "\rel\PlanilhabxrendaVI.rpt"
			.DiscardSavedData = True
			.WindowState = Crystal.WindowStateConstants.crptMaximized
			.WindowControlBox = True
			.WindowControls = True
			.WindowShowPrintSetupBtn = True
			.WindowShowCloseBtn = True
			.WindowShowSearchBtn = True
			.WindowTitle = "Planilha Baixa Renda"
			.Destination = Crystal.DestinationConstants.crptToWindow
			.SelectionFormula = ""
			If Me.chk472.CheckState = 0 Then
				.set_Formulas(1, "Reducao=0")
				.set_Formulas(2, "Aumento=0")
			Else
				.set_Formulas(1, "")
				.set_Formulas(2, "")
			End If
			.Action = 0
		End With
	End Sub
	
	Private Function BaixaRenda(ByVal adblConsumo As Double, ByVal adteLeiAnter As Date, ByVal adteLeiAtual As Date) As Double
		Dim ldblRet As Double
		Dim lstrCod As String
		Dim ldteBas As Date
		Dim ldblTar As Double
		Dim ldblKWHaux As Double
		Dim ldblVlrAux As Double
		'Controle dos limites entre as faixas de tarifas
		Dim ldblLimAux As Double
		Dim ldblLimIni As Double
		Dim ldblLimFim As Double
		'Controle de query
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		'
		ldteBas = CDate(VB6.Format("01/" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy"), "yyyy-MM-dd"))
		ldblKWHaux = adblConsumo
		ldblLimIni = 0
		ldblRet = 0
		'Pega as tarifas para calculo do Baixa Renda
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT * FROM `tarifa` "
		lstrSql = lstrSql & "   WHERE STR_TO_DATE(`datanova`, '%d/%m/%Y') <= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "     AND LEFT(`num`, 8) = '01B10211' "
		lstrSql = lstrSql & "     AND LEFT(TRIM(`descri`), 7) = 'Energia' "
		lstrSql = lstrSql & "     AND `valor` <> 0 "
		lstrSql = lstrSql & "     AND (`datanova` IS NOT NULL OR `datanova` <> '') "
		lstrSql = lstrSql & "ORDER BY STR_TO_DATE(`datanova`, '%d/%m/%Y') DESC, `num` ASC, `limitkwh` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					While Not .EOF And ldblKWHaux > 0
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						ldblLimFim = CDbl(IIf(IsDbNull(.Fields("limitkwh").value), 0, .Fields("limitkwh").value))
						ldblLimAux = ldblLimFim - ldblLimIni
						'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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrCod = Trim(IIf(IsDbNull(.Fields("num").value), "", .Fields("num").value))
						ldblTar = Arredondar(TarifaProporcional(adteLeiAnter, adteLeiAtual, lstrCod, 3) / 1000, 5)
						If ldblKWHaux <= ldblLimAux Then
							ldblVlrAux = Arredondar(ldblKWHaux * ldblTar, 2)
							ldblKWHaux = 0
						Else
							ldblVlrAux = Arredondar(ldblLimAux * ldblTar, 2)
							ldblKWHaux = ldblKWHaux - ldblLimAux
						End If
						ldblRet = ldblRet + ldblVlrAux
						ldblLimIni = ldblLimFim
						Call .MoveNext()
					End While
				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
		BaixaRenda = ldblRet
	End Function
	
	Private Function Residencial(ByVal adblConsumo As Double, ByVal adteLeiAnter As Date, ByVal adteLeiAtual As Date) As Double
		Dim ldblRet As Double
		Dim lstrCod As String
		Dim ldteBas As Date
		Dim ldblTar As Double
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		ldteBas = CDate(VB6.Format("01/" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy"), "yyyy-MM-dd"))
		If Me.chk472.CheckState = System.Windows.Forms.CheckState.Unchecked Then
			lstrCod = "01B10114" 'Residencial Normal - CCC - PROINFA
		Else
			lstrCod = "01B10111" 'Residencial Normal
		End If
		ldblRet = 0
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT * FROM `tarifa` "
		lstrSql = lstrSql & "   WHERE STR_TO_DATE(`datanova`, '%d/%m/%Y') <= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "     AND LEFT(`num`, 8) = '" & lstrCod & "' "
		lstrSql = lstrSql & "     AND LEFT(TRIM(`descri`), 7) = 'Energia' "
		lstrSql = lstrSql & "     AND `valor` <> 0 "
		lstrSql = lstrSql & "     AND (`datanova` IS NOT NULL OR `datanova` <> '') "
		lstrSql = lstrSql & "ORDER BY STR_TO_DATE(datanova,'%d/%m/%Y') DESC, `num` ASC"
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			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"'
					'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrCod = Trim(IIf(IsDbNull(.Fields("num").value), "", .Fields("num").value))
					ldblTar = Arredondar(TarifaProporcional(adteLeiAnter, adteLeiAtual, lstrCod, 3) / 1000, 5)
					ldblRet = adblConsumo * ldblTar
				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
		Residencial = ldblRet
	End Function
	
	Private Sub NomeArquivoXML()
		istrArquivo = "APLDMR" & gstrEmpresa_CodRes89 & "_" & gstrEmpresa_Sigla & "_89_"
		Me.txtDir.Text = istrCaminho
		Me.txtArquivo.Text = istrArquivo & VB6.Format(Me.dtpMesAno.Month, "00") & Me.dtpMesAno.Year & "_V" & VB6.Format(Me.txtVersao.Text, "00") & ".XML"
	End Sub
	
	Private Sub LigDes(ByRef aModo As Boolean)
		Me.lblMesAno.Enabled = aModo
		Me.dtpMesAno.Enabled = aModo
		Me.lblVersao.Enabled = aModo
		Me.lblArquivo.Enabled = aModo
		Me.txtDir.Enabled = aModo
		Me.lblCaminho.Enabled = aModo
		Me.txtArquivo.Enabled = aModo
		Me.chk472.Enabled = aModo
		Me.chkCSV.Enabled = aModo
		Me.chkNovoNis.Enabled = aModo
		Me.chkOficioCircular120.Enabled = aModo
		Me.txtVersao.Enabled = aModo
		Me.lblVersao.Enabled = aModo
		Me.cmdSalvarArq.Enabled = aModo
		Me.cmdGerar.Enabled = aModo
		Me.cmdFechar.Enabled = aModo
		Me.cmdImprimir.Enabled = aModo
		Me.cmdProcessar.Enabled = aModo
	End Sub
End Class