Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmEmissaoBaixaRenda
	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 frmEmissaoBaixaRenda_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrSql As String
		istrTabela = "planilha_baixa_renda"
		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 & "_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 & "`t1_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`t2_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`t3_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`t4_kwhc` int(11) default NULL, "
		lstrSql = lstrSql & "`t1_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t1_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t2_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t3_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t2_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t3_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t4_bxvalor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t4_valor` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`zona` varchar(3) default '', "
		lstrSql = lstrSql & "`livro` varchar(2) default '', "
		lstrSql = lstrSql & "`conta` varchar(6) default '', "
		lstrSql = lstrSql & "`codclas` varchar(4) default '', "
		lstrSql = lstrSql & "`bx0402` char(1) default '' "
		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 & "`t1qtde` int(11) default NULL, "
		lstrSql = lstrSql & "`totbxqtde` int(11) default NULL, "
		lstrSql = lstrSql & "`t2qtde` int(11) default NULL, "
		lstrSql = lstrSql & "`t3qtde` int(11) default NULL, "
		lstrSql = lstrSql & "`t4qtde` int(11) default NULL, "
		lstrSql = lstrSql & "`t1kwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`totbxkhwc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t2kwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t3kwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t4kwhc` double(12,3) default '0.000', "
		lstrSql = lstrSql & "`t1valr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t1bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`totbxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t2bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t3bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`totvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t2valr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t3valr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t4bxvalr` double(12,2) default '0.00', "
		lstrSql = lstrSql & "`t4valr` 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 frmEmissaoBaixaRenda_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
	
	Private Sub frmEmissaoBaixaRenda_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If CInt(Len(Trim(istrTabela))) > 0 Then
			Call Executar("DROP TABLE IF EXISTS " & istrTabela)
			Call Executar("DROP TABLE IF EXISTS " & istrTabela & "_total ")
		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
		'Quantidade consumidores
		'Quantidade Geral
		Dim ldblQtde As Double
		'Quantidade Residencial Normal
		Dim ldblT1Qtd As Double
		'Quantidade Bx 246 até 80kwh
		Dim ldblT2Qtd As Double
		'Quantidade Bx 485 NIS
		Dim ldblT3Qtd As Double
		'Quantidade de T2 + T3 que foram baixa renda em 2002
		Dim ldblT4Qtd As Double
		'
		'Somatorio das quantidade de kwhc
		'Resindencial normal
		Dim ldblT1kWh As Double
		'246
		Dim ldblT2kWh As Double
		'485
		Dim ldblT3kWh As Double
		'T2+T3 em 2002
		Dim ldblT4kWh As Double
		'
		'Valor do consumo
		'Resindencial normal
		Dim ldblT1Vlr As Double
		'Resindencial normal com valor de baixa renda
		Dim ldblT1BxVlr As Double
		'bx renda 246
		Dim ldblT2BxVlr As Double
		'bx renda 246 com valor residencial normal sem pro infa e sem ccc
		Dim ldblT2Vlr As Double
		'bx renda 485
		Dim ldblT3BxVlr As Double
		'bx renda 485 com valor residencial normal sem pro infa e sem ccc
		Dim ldblT3Vlr As Double
		't2+t3  em 2002
		Dim ldblT4BxVlr As Double
		't2+t3  em 2002 com valor residencial normal sem pro infa e sem ccc
		Dim ldblT4Vlr As Double
		'
		'Dados das faturas e unidade consumidoras
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrClasse As String
		Dim lstrJul10 As String
		Dim lstrAbr02 As String
		Dim lstrFase As String
		Dim ldteleitAtual As Date
		Dim ldteLeitAnter As Date
		Dim lstrItens As String
		Dim ldblQtdkWh As Double
		Dim ldblVlrkWh As Double
		Dim ldblVlrRes As Double
		Dim ldblVlrBxR As Double
		'Controle das consultas, faixas e loop
		Dim llngLimRes As Integer
		'UPGRADE_WARNING: Lower bound of array lstrTpFases 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 lstrTpFases(3) As String
		'UPGRADE_WARNING: Lower bound of array lstrTpFaixa 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 lstrTpFaixa(6) As String
		'UPGRADE_WARNING: Lower bound of array lstrFxDescr 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 lstrFxDescr(6) As String
		'UPGRADE_WARNING: Lower bound of array lstrFxDesAx 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 lstrFxDesAx(6) As String
		Dim llngProc As Double
		Dim llngFase As Double
		'Controle de query
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Me.lblInfo.Text = " Buscando os dados..."
		Call Me.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 & "       IFNULL(`unidb`.`jul2010`,'N') AS `jul2010`, " & vbCrLf
		lstrSql = lstrSql & "       IFNULL(`unidb`.`bx0402`,'N') AS `bx0402` " & 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) "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					lblnRet = (.RecordCount > 0)
					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"'
						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"'
						lstrJul10 = Trim(IIf(IsDbNull(.Fields("jul2010").value), "N", .Fields("jul2010").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"'
						lstrAbr02 = Trim(IIf(IsDbNull(.Fields("bx0402").value), "N", .Fields("bx0402").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 = 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"'
						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"'
						lstrLivro = Trim(IIf(IsDbNull(.Fields("livro").value), "000", .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"'
						lstrConta = Trim(IIf(IsDbNull(.Fields("conta").value), "000", .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"'
						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"'
						ldteleitAtual = 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"'
						ldteLeitAnter = IIf(IsDbNull(.Fields("dataleitant").value), "1900-01-01", .Fields("dataleitant").value)
						If lstrClasse = "9104" Or lstrClasse = "9105" Or lstrClasse = "9106" Then
							If Me.chkZeraValores.CheckState = 1 Then
								ldblQtdkWh = 0
								ldblVlrkWh = 0
							End If
						End If
						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO " & istrTabela & " SET "
						lstrSql = lstrSql & " `fase` = '" & lstrFase & "', "
						lstrSql = lstrSql & " `kwhc` = " & Replace(CStr(Arredondar(ldblQtdkWh, 0)), ",", ".") & ", "
						lstrSql = lstrSql & " `zona` = '" & lstrZona & "', "
						lstrSql = lstrSql & " `livro` = '" & lstrLivro & "', "
						lstrSql = lstrSql & " `conta` = '" & lstrConta & "', "
						lstrSql = lstrSql & " `codclas` = '" & lstrClasse & "', "
						lstrSql = lstrSql & " `bx0402` = '" & lstrAbr02 & "', "
						'If lstrClasse = "9101" Then  't1
						If lstrClasse = "9101" Or (lstrJul10 = "N" And (lstrClasse = "9104" Or lstrClasse = "9105" Or lstrClasse = "9106")) Then 't1
							ldblVlrRes = IIf(lstrAbr02 = "S", Residencial(ldblQtdkWh, ldteLeitAnter, ldteleitAtual), 0)
							ldblVlrBxR = IIf(lstrAbr02 = "S", BaixaRenda(ldblQtdkWh, IIf(VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "02/2010", CDate("01/02/2010"), ldteLeitAnter), IIf(VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "02/2010", CDate("01/03/2010"), ldteleitAtual)), 0)
							'Consumos (t?_kwhc)
							lstrSql = lstrSql & " `t1_kwhc` = " & Replace(CStr(Arredondar(IIf(lstrAbr02 = "S", ldblQtdkWh, 0), 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_kwhc` = " & Replace(CStr(Arredondar(0, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_kwhc` = " & Replace(CStr(Arredondar(0, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_kwhc` = " & Replace(CStr(Arredondar(0, 0)), ",", ".") & ", "
							'Valores
							lstrSql = lstrSql & " `t1_valor` = " & Replace(CStr(ldblVlrRes), ",", ".") & ", "
							lstrSql = lstrSql & " `t1_bxvalor` = " & Replace(CStr(ldblVlrBxR), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_bxvalor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_bxvalor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_valor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_valor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_bxvalor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_valor` = " & Replace(CStr(0), ",", ".") & "; "
						End If
						'If lstrClasse = "9104" Then ' t2 e ou t4
						If lstrClasse = "9104" And lstrJul10 = "S" Then ' t2 e ou t4
							If Me.chkZeraValores.CheckState = 1 Then
								ldblVlrRes = 0
							Else
								ldblVlrRes = Residencial(ldblQtdkWh, ldteLeitAnter, ldteleitAtual)
							End If
							'Consumos (t?_kwhc)
							lstrSql = lstrSql & " `t1_kwhc` = " & Replace(CStr(Arredondar(0, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_kwhc` = " & Replace(CStr(Arredondar(ldblQtdkWh, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_kwhc` = " & Replace(CStr(Arredondar(0, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_kwhc` = " & Replace(CStr(Arredondar(IIf(lstrAbr02 = "S", ldblQtdkWh, 0), 0)), ",", ".") & ", "
							'Valores
							lstrSql = lstrSql & " `t1_valor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t1_bxvalor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_bxvalor` = " & Replace(CStr(ldblVlrkWh), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_bxvalor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_valor` = " & Replace(CStr(ldblVlrRes), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_valor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_bxvalor` = " & Replace(IIf(lstrAbr02 = "S", ldblVlrkWh, 0), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_valor` = " & Replace(IIf(lstrAbr02 = "S", ldblVlrRes, 0), ",", ".") & "; "
						End If
						'If (lstrClasse = "9105" Or lstrClasse = "9106") Then 't3 e ou t4
						If (lstrClasse = "9105" Or lstrClasse = "9106") And lstrJul10 = "S" Then 't3 e ou t4
							If Me.chkZeraValores.CheckState = 1 Then
								ldblVlrRes = 0
							Else
								ldblVlrRes = Residencial(ldblQtdkWh, ldteLeitAnter, ldteleitAtual)
							End If
							'Consumos (t?_kwhc)
							lstrSql = lstrSql & " `t1_kwhc` = " & Replace(CStr(Arredondar(0, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_kwhc` = " & Replace(CStr(Arredondar(0, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_kwhc` = " & Replace(CStr(Arredondar(ldblQtdkWh, 0)), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_kwhc` = " & Replace(CStr(Arredondar(IIf(lstrAbr02 = "S", ldblQtdkWh, 0), 0)), ",", ".") & ", "
							'Valores
							lstrSql = lstrSql & " `t1_valor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t1_bxvalor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_bxvalor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_bxvalor` = " & Replace(CStr(ldblVlrkWh), ",", ".") & ", "
							lstrSql = lstrSql & " `t2_valor` = " & Replace(CStr(0), ",", ".") & ", "
							lstrSql = lstrSql & " `t3_valor` = " & Replace(CStr(ldblVlrRes), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_bxvalor` = " & Replace(IIf(lstrAbr02 = "S", ldblVlrkWh, 0), ",", ".") & ", "
							lstrSql = lstrSql & " `t4_valor` = " & Replace(IIf(lstrAbr02 = "S", ldblVlrRes, 0), ",", ".") & "; "
						End If
						Call Executar(lstrSql)
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
				End If
				Call .Close()
			End If
		End With
		If lblnRet Then
			'Segunda Parte colocar em uma tabela todos os dados mastigados para facilitar no crystal
			llngLimRes = CInt(LeConfig("LIMITE_RESIDENCIAL_XML"))
			lstrTpFases(1) = " AND fase='M' "
			lstrTpFases(2) = " AND fase='B' "
			lstrTpFases(3) = " AND fase='T' "
			lstrTpFaixa(1) = " WHERE kwhc <=30 "
			lstrTpFaixa(2) = " WHERE kwhc >=31 AND kwhc <=79 "
			lstrTpFaixa(3) = " WHERE kwhc >=80 AND kwhc <=100 "
			lstrTpFaixa(4) = " WHERE kwhc >=101 AND kwhc <=" & llngLimRes & " "
			lstrTpFaixa(5) = " WHERE kwhc >=" & llngLimRes + 1 & " AND kwhc<=220 "
			lstrTpFaixa(6) = " WHERE kwhc >220 "
			lstrFxDescr(1) = " Até 30       "
			lstrFxDescr(2) = " De  31 até  79 "
			lstrFxDescr(3) = " De  80 até 100 "
			lstrFxDescr(4) = " De 101 até " & llngLimRes & " "
			lstrFxDescr(5) = " De " & llngLimRes + 1 & " até 220 "
			lstrFxDescr(6) = " Acima 220    "
			lstrFxDesAx(1) = "de01ate30"
			lstrFxDesAx(2) = "de31ate79"
			lstrFxDesAx(3) = "de80ate100"
			lstrFxDesAx(4) = "de101ate_limite_regional"
			lstrFxDesAx(5) = "limite_regional_ate220"
			lstrFxDesAx(6) = "Acima220"
			For llngFase = 1 To 3
				For llngProc = 1 To 6
					lstrSql = ""
					lstrSql = lstrSql & "SELECT `t1_kwhc`, "
					lstrSql = lstrSql & "       `t2_kwhc`, "
					lstrSql = lstrSql & "       `t3_kwhc`, "
					lstrSql = lstrSql & "       `t4_kwhc`, "
					lstrSql = lstrSql & "       `t1_valor`, "
					lstrSql = lstrSql & "       `t1_bxvalor`, "
					lstrSql = lstrSql & "       `t2_bxvalor`, "
					lstrSql = lstrSql & "       `t2_valor`, "
					lstrSql = lstrSql & "       `t3_bxvalor`, "
					lstrSql = lstrSql & "       `t3_valor`, "
					lstrSql = lstrSql & "       `t4_bxvalor`, "
					lstrSql = lstrSql & "       `t4_valor` "
					lstrSql = lstrSql & "  FROM planilha_baixa_renda "
					lstrSql = lstrSql & lstrTpFaixa(llngProc) & lstrTpFases(llngFase)
					Call Conectar(lstrSql, lrstAux)
					With lrstAux
						If .State = ADODB.ObjectStateEnum.adStateOpen Then
							ldblQtde = 0
							ldblT1Qtd = 0
							ldblT2Qtd = 0
							ldblT3Qtd = 0
							ldblT4Qtd = 0
							ldblT1kWh = 0
							ldblT2kWh = 0
							ldblT3kWh = 0
							ldblT4kWh = 0
							ldblT1Vlr = 0
							ldblT1BxVlr = 0
							ldblT2BxVlr = 0
							ldblT2Vlr = 0
							ldblT3BxVlr = 0
							ldblT3Vlr = 0
							ldblT4BxVlr = 0
							ldblT4Vlr = 0
							If .RecordCount > 0 Then
								Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
								While Not .EOF
									'Quantidade Geral
									ldblQtde = ldblQtde + 1
									'Quantidade de consumidores por tipos dos 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"'
									If IIf(IsDbNull(.Fields("t1_kwhc").value), 0, .Fields("t1_kwhc").value) > 0 Then ldblT1Qtd = ldblT1Qtd + 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"'
									If IIf(IsDbNull(.Fields("t2_kwhc").value), 0, .Fields("t2_kwhc").value) > 0 Then ldblT2Qtd = ldblT2Qtd + 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"'
									If IIf(IsDbNull(.Fields("t3_kwhc").value), 0, .Fields("t3_kwhc").value) > 0 Then ldblT3Qtd = ldblT3Qtd + 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"'
									If IIf(IsDbNull(.Fields("t4_kwhc").value), 0, .Fields("t4_kwhc").value) > 0 Then ldblT4Qtd = ldblT4Qtd + 1
									'Somatorio dos valores por tipos dos 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"'
									ldblT1kWh = ldblT1kWh + IIf(IsDbNull(.Fields("t1_kwhc").value), 0, .Fields("t1_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"'
									ldblT2kWh = ldblT2kWh + IIf(IsDbNull(.Fields("t2_kwhc").value), 0, .Fields("t2_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"'
									ldblT3kWh = ldblT3kWh + IIf(IsDbNull(.Fields("t3_kwhc").value), 0, .Fields("t3_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"'
									ldblT4kWh = ldblT4kWh + IIf(IsDbNull(.Fields("t4_kwhc").value), 0, .Fields("t4_kwhc").value)
									'Residencial 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"'
									ldblT1Vlr = ldblT1Vlr + IIf(IsDbNull(.Fields("t1_valor").value), 0, .Fields("t1_valor").value)
									'Residencial Normal com valor de 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"'
									ldblT1BxVlr = ldblT1BxVlr + IIf(IsDbNull(.Fields("t1_bxvalor").value), 0, .Fields("t1_bxvalor").value)
									'Baixa renda 246
									'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"'
									ldblT2BxVlr = ldblT2BxVlr + IIf(IsDbNull(.Fields("t2_bxvalor").value), 0, .Fields("t2_bxvalor").value)
									'Baixa renda 246 com valor de residencial
									'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"'
									ldblT2Vlr = ldblT2Vlr + IIf(IsDbNull(.Fields("t2_valor").value), 0, .Fields("t2_valor").value)
									'Baixa renda 485
									'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"'
									ldblT3BxVlr = ldblT3BxVlr + IIf(IsDbNull(.Fields("t3_bxvalor").value), 0, .Fields("t3_bxvalor").value)
									'Baixa renda 485 com valor residencial
									'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"'
									ldblT3Vlr = ldblT3Vlr + IIf(IsDbNull(.Fields("t3_valor").value), 0, .Fields("t3_valor").value)
									'Baixa renda 246 e 485 que foram baixa renda em 2002
									'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"'
									ldblT4BxVlr = ldblT4BxVlr + IIf(IsDbNull(.Fields("t4_bxvalor").value), 0, .Fields("t4_bxvalor").value)
									'Baixa renda 246 e 485 que foram baixa renda em 2002 com valor de residencial
									'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"'
									ldblT4Vlr = ldblT4Vlr + IIf(IsDbNull(.Fields("t4_valor").value), 0, .Fields("t4_valor").value)
									Call .MoveNext()
									Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
								End While
							End If
							Call .Close()
						End If
					End With
					lstrSql = ""
					lstrSql = lstrSql & "INSERT INTO planilha_baixa_renda_total SET"
					lstrSql = lstrSql & " `ordimp1`   = " & llngFase & ", "
					lstrSql = lstrSql & " `ordimp2`   = " & llngProc & ", "
					lstrSql = lstrSql & " `faixa`     = '" & lstrFxDescr(llngProc) & "', "
					lstrSql = lstrSql & " `fase`      = '" & IIf(llngFase = 1, "M", IIf(llngFase = 2, "B", "T")) & "', "
					lstrSql = lstrSql & " `restotal`  = " & ldblQtde & ", "
					lstrSql = lstrSql & " `t1qtde`    = " & ldblT1Qtd & ", "
					lstrSql = lstrSql & " `totbxqtde` = " & (ldblT2Qtd + ldblT3Qtd) & ", "
					lstrSql = lstrSql & " `t2qtde`    = " & ldblT2Qtd & ", "
					lstrSql = lstrSql & " `t3qtde`    = " & ldblT3Qtd & ", "
					lstrSql = lstrSql & " `t4qtde`    = " & ldblT4Qtd & ", "
					lstrSql = lstrSql & " `t1kwhc`    = " & Replace(CStr(ldblT1kWh / 1000), ",", ".") & ", "
					lstrSql = lstrSql & " `totbxkhwc` = " & Replace(CStr((ldblT2kWh + ldblT3kWh) / 1000), ",", ".") & ", "
					lstrSql = lstrSql & " `t2kwhc`    = " & Replace(CStr(ldblT2kWh / 1000), ",", ".") & ", "
					lstrSql = lstrSql & " `t3kwhc`    = " & Replace(CStr(ldblT3kWh / 1000), ",", ".") & ", "
					lstrSql = lstrSql & " `t4kwhc`    = " & Replace(CStr(ldblT4kWh / 1000), ",", ".") & ", "
					lstrSql = lstrSql & " `t1valr`    = " & Replace(CStr(ldblT1Vlr), ",", ".") & ", "
					lstrSql = lstrSql & " `t1bxvalr`  = " & Replace(CStr(ldblT1BxVlr), ",", ".") & ", "
					lstrSql = lstrSql & " `totbxvalr` = " & Replace(CStr(ldblT2BxVlr + ldblT3BxVlr), ",", ".") & ", "
					lstrSql = lstrSql & " `t2bxvalr`  = " & Replace(CStr(ldblT2BxVlr), ",", ".") & ", "
					lstrSql = lstrSql & " `t3bxvalr`  = " & Replace(CStr(ldblT3BxVlr), ",", ".") & ", "
					lstrSql = lstrSql & " `totvalr`   = " & Replace(CStr(ldblT2Vlr + ldblT3Vlr), ",", ".") & ", "
					lstrSql = lstrSql & " `t2valr`    = " & Replace(CStr(ldblT2Vlr), ",", ".") & ", "
					lstrSql = lstrSql & " `t3valr`    = " & Replace(CStr(ldblT3Vlr), ",", ".") & ", "
					lstrSql = lstrSql & " `t4bxvalr`  = " & Replace(CStr(ldblT4BxVlr), ",", ".") & ", "
					lstrSql = lstrSql & " `t4valr`    = " & Replace(CStr(ldblT4Vlr), ",", ".") & ", "
					lstrSql = lstrSql & " `mesano`    = '" & VB6.Format(dtpMesAno.Month, "00") & "/" & dtpMesAno.Year & "', "
					lstrSql = lstrSql & " `faixaxml`  = '" & lstrFxDesAx(llngProc) & "' "
					Call Executar(lstrSql)
				Next llngProc
			Next llngFase
			'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 If
		MontaTabela = lblnRet
	End Function
	
	Private Sub GerarXML()
		'Controle do Arquivo externo
		Dim strNomeArquivo As String
		Dim intARQ As Short
		Dim strLinha As String
		Dim strSql As String
		Dim rsConsulta As ADODB.Recordset
		Dim intParte1 As Short
		Dim intParte2 As Short
		Dim strCabec1(2) As String
		Dim strCabec2(2) As String
		Dim strCabec3(5) As String
		Dim strCabec4(5) As String
		Dim strCampos(2, 5) As String
		Dim dblReducao As Double
		Dim dblAumento As Double
		Dim dblDiferenca As Double
		Dim strTab As String
		
		rsConsulta = New ADODB.Recordset
		On Error GoTo ErroArquivo
		lblInfo.Text = "Gerando o arquivo XML..."
		lblInfo.Refresh()
		
		strTab = "   "
		
		strCabec1(0) = "<!--Numero de Unidades Consumidoras-->"
		strCabec1(1) = "<!--Energia Faturada-->"
		strCabec1(2) = "<!--Faturamento Real-->"
		
		strCabec2(0) = "CONSUMIDORES"
		strCabec2(1) = "ENERGIA"
		strCabec2(2) = "FATURAMENTO"
		
		strCabec3(0) = "<!--Residencial Total-->"
		strCabec3(1) = "<!--Residencial T1-->"
		strCabec3(2) = "<!--Baixa Renda Total-->"
		strCabec3(3) = "<!--Baixa Renda T2-->"
		strCabec3(4) = "<!--Baixa Renda T3-->"
		strCabec3(5) = "<!--Baixa Renda T4-->"
		
		strCabec4(0) = "total"
		strCabec4(1) = "t1"
		strCabec4(2) = "brtotal"
		strCabec4(3) = "brt2"
		strCabec4(4) = "brt3"
		strCabec4(5) = "brt4"
		
		strCampos(0, 0) = "restotal"
		strCampos(0, 1) = "t1qtde"
		strCampos(0, 2) = "totbxqtde"
		strCampos(0, 3) = "t2qtde"
		strCampos(0, 4) = "t3qtde"
		strCampos(0, 5) = "t4qtde"
		
		strCampos(1, 1) = "t1kwhc"
		strCampos(1, 2) = "totbxkhwc"
		strCampos(1, 3) = "t2kwhc"
		strCampos(1, 4) = "t3kwhc"
		strCampos(1, 5) = "t4kwhc"
		
		strCampos(2, 1) = "t1valr"
		strCampos(2, 2) = "totbxvalr"
		strCampos(2, 3) = "t2bxvalr"
		strCampos(2, 4) = "t3bxvalr"
		strCampos(2, 5) = "t4bxvalr"
		
		strSql = "SELECT ordimp1, ordimp2, faixa, fase, restotal, " & "       t1qtde, totbxqtde, t2qtde, t3qtde, t4qtde, t1kwhc, totbxkhwc, t2kwhc, t3kwhc, t4kwhc, " & "       t1valr, t1bxvalr, totbxvalr, t2bxvalr, t3bxvalr, totvalr, t2valr, t3valr, t4bxvalr, t4valr,mesano,faixaxml" & "  FROM planilha_baixa_renda_total " & " WHERE mesano = '" & VB6.Format(dtpMesAno.Month, "00") & "/" & dtpMesAno.Year & "' " & "ORDER BY ordimp2 ASC, ordimp1 ASC "
		
		Call Conectar(strSql, rsConsulta)
		If rsConsulta.RecordCount > 0 Then
			intARQ = FreeFile
			strNomeArquivo = txtDir.Text & IIf(VB.Right(Trim(txtDir.Text), 1) <> "\", "\", "") & txtArquivo.Text
			FileOpen(intARQ, strNomeArquivo, OpenMode.Output)
			
			'145, 146 ou 180 para o caracter crase
			PrintLine(intARQ, Chr(239) & Chr(187) & Chr(191) & "<DMR versao=""V" & VB6.Format(txtVersao.Text, "00") & """ mes=""" & VB6.Format(dtpMesAno.Month, "00") & """ ano=""" & dtpMesAno.Year & """ codigo=""" & gstrEmpresa_CodRes89 & """ empresa=""" & gstrEmpresa_Sigla & """>")
			PrintLine(intARQ, strTab & "<!--Inicio dos valores do DMR-->")
			
			For intParte1 = 0 To 2
				PrintLine(intARQ, strTab & strCabec1(intParte1))
				PrintLine(intARQ, strTab & "<" & strCabec2(intParte1) & ">")
				
				For intParte2 = IIf(intParte1 = 0, 0, 1) To 5
					PrintLine(intARQ, strTab & strTab & strCabec3(intParte2))
					
					PrintLine(intARQ, strTab & strTab & "<" & strCabec4(intParte2) & ">")
					Call MontaDetalheArquivoXML(intARQ, rsConsulta, strCampos(intParte1, intParte2), intParte1, dblReducao, dblAumento)
					PrintLine(intARQ, strTab & strTab & "</" & strCabec4(intParte2) & ">")
				Next intParte2
				
				PrintLine(intARQ, strTab & "</" & strCabec2(intParte1) & ">")
			Next intParte1
			If VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "03/2010" Then
				dblReducao = 4404.91
			End If
			PrintLine(intARQ, strTab & "<!--Valor Mensal da Redu" & Chr(195) & Chr(167) & Chr(195) & Chr(163) & "o de Receita-->")
			PrintLine(intARQ, strTab & "<VMRR>" & FormatNumber(dblReducao, 2) & "</VMRR>")
			PrintLine(intARQ, strTab & "<!--Valor Mensal do Aumento de Receita-->")
			PrintLine(intARQ, strTab & "<VMAR>" & FormatNumber(dblAumento, 2) & "</VMAR>")
			PrintLine(intARQ, strTab & "<!--Valor Mensal da Diferen" & Chr(195) & Chr(167) & "a de Receita -->")
			PrintLine(intARQ, strTab & "<VMDR>" & FormatNumber(dblReducao - dblAumento, 2) & "</VMDR>")
			PrintLine(intARQ, "</DMR>")
			FileClose(intARQ)
			lblInfo.Text = " Arquivo gerado com sucesso!"
			lblInfo.Refresh()
			
		Else
			Call MsgBox("Não existe dados para gerar o XML do mês selecionando!", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
		If rsConsulta.State = 1 Then Call rsConsulta.Close()
		'UPGRADE_NOTE: Object rsConsulta 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"'
		rsConsulta = Nothing
		Exit Sub
ErroArquivo: 
		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
		If rsConsulta.State = 1 Then Call rsConsulta.Close()
		'UPGRADE_NOTE: Object rsConsulta 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"'
		rsConsulta = Nothing
	End Sub
	
	Private Sub MontaDetalheArquivoXML(ByRef aARQ As Short, ByVal arsConsulta As ADODB.Recordset, ByVal aCampo As String, ByVal aTipo As Short, ByRef aReducao As Double, ByRef aAumento As Double)
		Dim strFaixa As String
		Dim strFase As String
		Dim dblAux As Double
		Dim dblAux1 As Double
		Dim dblTotal As Double
		Dim intCtrl As Short
		
		Dim dblTotValr As Double
		Dim dblTotBXValr As Double
		Dim dblT4Valr As Double
		Dim dblT4BXValr As Double
		Dim dblT1Valr As Double
		Dim dblT1BXValr As Double
		Dim strTab As String
		
		strTab = "   "
		
		dblTotal = 0
		dblTotValr = 0
		dblTotBXValr = 0
		dblT4Valr = 0
		dblT4BXValr = 0
		
		arsConsulta.MoveFirst()
		While Not arsConsulta.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"'
			intCtrl = IIf(IsDbNull(arsConsulta.Fields("ordimp1").Value), 0, arsConsulta.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"'
			strFaixa = IIf(IsDbNull(arsConsulta.Fields("faixaxml").Value), "", arsConsulta.Fields("faixaxml").Value)
			If intCtrl = 1 Then
				PrintLine(aARQ, strTab & strTab & strTab & "<" & strFaixa & ">")
			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"'
			strFase = IIf(IsDbNull(arsConsulta.Fields("fase").Value), "M", arsConsulta.Fields("fase").Value)
			strFase = IIf(strFase = "M", "monofasico", IIf(strFase = "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"'
			dblAux = IIf(IsDbNull(arsConsulta.Fields(aCampo).value), 0, arsConsulta.Fields(aCampo).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"'
			dblTotValr = dblTotValr + IIf(IsDbNull(arsConsulta.Fields("totvalr").value), 0, arsConsulta.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"'
			dblTotBXValr = dblTotBXValr + IIf(IsDbNull(arsConsulta.Fields("totbxvalr").value), 0, arsConsulta.Fields("totbxvalr").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"'
			dblT4Valr = dblT4Valr + IIf(IsDbNull(arsConsulta.Fields("t4valr").value), 0, arsConsulta.Fields("t4valr").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"'
			dblT4BXValr = dblT4BXValr + IIf(IsDbNull(arsConsulta.Fields("t4bxvalr").value), 0, arsConsulta.Fields("t4bxvalr").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"'
			dblT1Valr = dblT1Valr + IIf(IsDbNull(arsConsulta.Fields("t1valr").value), 0, arsConsulta.Fields("t1valr").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"'
			dblT1BXValr = dblT1BXValr + IIf(IsDbNull(arsConsulta.Fields("t1bxvalr").value), 0, arsConsulta.Fields("t1bxvalr").value)
			
			PrintLine(aARQ, strTab & strTab & strTab & strTab & "<" & strFase & ">" & IIf(aTipo = 0, FormatNumber(dblAux, 0), IIf(aTipo = 1, FormatNumber(dblAux, 3), FormatNumber(dblAux, 2))) & "</" & strFase & ">")
			
			If intCtrl = 3 Then
				PrintLine(aARQ, strTab & strTab & strTab & "</" & strFaixa & ">")
			End If
			dblTotal = dblTotal + dblAux
			arsConsulta.MoveNext()
		End While
		aReducao = (dblTotValr - dblTotBXValr) - (dblT4Valr - dblT4BXValr)
		aAumento = (dblT1Valr - dblT1BXValr)
		PrintLine(aARQ, strTab & strTab & strTab & "<TOTAL>" & IIf(aTipo = 0, FormatNumber(dblTotal, 0), IIf(aTipo = 1, FormatNumber(dblTotal, 3), FormatNumber(dblTotal, 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 llngRes_Qtd_T1 As Integer
		Dim llngBxR_Qtd_To As Integer
		Dim llngBxR_Qtd_T2 As Integer
		Dim llngBxR_Qtd_T3 As Integer
		Dim llngBxR_Qtd_T4 As Integer
		'Totalizador da qtde de consumidores
		Dim llngRes_Tot_Qtd_Nr As Integer
		Dim llngRes_Tot_Qtd_T1 As Integer
		Dim llngBxR_Tot_Qtd_To As Integer
		Dim llngBxR_Tot_Qtd_T2 As Integer
		Dim llngBxR_Tot_Qtd_T3 As Integer
		Dim llngBxR_Tot_Qtd_T4 As Integer
		'Quantidade de MWh
		Dim ldblRes_Qtd_T1 As Double
		Dim ldblBxR_Qtd_To As Double
		Dim ldblBxR_Qtd_T2 As Double
		Dim ldblBxR_Qtd_T3 As Double
		Dim ldblBxR_Qtd_T4 As Double
		'Totalizador de MWh
		Dim ldblRes_Tot_Qtd_T1 As Double
		Dim ldblBxR_Tot_Qtd_To As Double
		Dim ldblBxR_Tot_Qtd_T2 As Double
		Dim ldblBxR_Tot_Qtd_T3 As Double
		Dim ldblBxR_Tot_Qtd_T4 As Double
		'Valor do MWh
		Dim ldblRes_Vlr_T1 As Double
		Dim ldblBxR_Vlr_T1 As Double
		Dim ldblBxR_Vlr_To As Double
		Dim ldblRes_Vlr_To As Double
		Dim ldblBxR_Vlr_T2 As Double
		Dim ldblRes_Vlr_T2 As Double
		Dim ldblBxR_Vlr_T3 As Double
		Dim ldblRes_Vlr_T3 As Double
		Dim ldblBxR_Vlr_T4 As Double
		Dim ldblRes_Vlr_T4 As Double
		'Totalizador MWh
		Dim ldblRes_Tot_Vlr_T1 As Double
		Dim ldblBxR_Tot_Vlr_T1 As Double
		Dim ldblBxR_Tot_Vlr_To As Double
		Dim ldblRes_Tot_Vlr_To As Double
		Dim ldblBxR_Tot_Vlr_T2 As Double
		Dim ldblRes_Tot_Vlr_T2 As Double
		Dim ldblBxR_Tot_Vlr_T3 As Double
		Dim ldblRes_Tot_Vlr_T3 As Double
		Dim ldblBxR_Tot_Vlr_T4 As Double
		Dim ldblRes_Tot_Vlr_T4 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, "
		lstrSql = lstrSql & "       restotal, t1qtde, totbxqtde, t2qtde,   t3qtde,   t4qtde, "
		lstrSql = lstrSql & "                 t1kwhc, totbxkhwc, t2kwhc,   t3kwhc,   t4kwhc, "
		lstrSql = lstrSql & "                 t1valr, totbxvalr, t2bxvalr, t3bxvalr, t4bxvalr, "
		lstrSql = lstrSql & "       t1bxvalr, totvalr, t2valr, t3valr,  t4valr,  mesano, faixaxml "
		lstrSql = lstrSql & "  FROM planilha_baixa_renda_total "
		lstrSql = lstrSql & " WHERE `mesano` = '" & VB6.Format(Me.dtpMesAno.Month, "00") & "/" & Me.dtpMesAno.Year & "' "
		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) & ";")
					PrintLine(lintARQ, ";;" & "NÚMERO DE UNIDADES CONSUMIDORAS" & ";;;;;;ENERGIA FATURADA (MWh);J;K;L;M;FATURAMENTO REAL (R$);")
					lstrLin = ""
					lstrLin = lstrLin & "Faixa de Consumo (KWh);Tipo de Ligação;Residencial Total;"
					lstrLin = lstrLin & "Residencial T1;Baixa Renda Total;Baixa Renda T2;Baixa Renda T3;Baixa Renda T4;"
					lstrLin = lstrLin & "Residencial T1;Baixa Renda Total;Baixa Renda T2;Baixa Renda T3;Baixa Renda T4;"
					lstrLin = lstrLin & "Residencial T1;Baixa Renda Total;Baixa Renda T2;Baixa Renda T3;Baixa Renda T4;"
					PrintLine(lintARQ, lstrLin)
					'Zerando os totalizadores
					'Quantide de consumidores
					llngRes_Tot_Qtd_Nr = 0
					llngRes_Tot_Qtd_T1 = 0
					llngBxR_Tot_Qtd_To = 0
					llngBxR_Tot_Qtd_T2 = 0
					llngBxR_Tot_Qtd_T3 = 0
					llngBxR_Tot_Qtd_T4 = 0
					'Quantidade de MWh
					ldblRes_Tot_Qtd_T1 = 0
					ldblBxR_Tot_Qtd_To = 0
					ldblBxR_Tot_Qtd_T2 = 0
					ldblBxR_Tot_Qtd_T3 = 0
					ldblBxR_Tot_Qtd_T4 = 0
					'Valor MWH
					ldblRes_Tot_Vlr_T1 = 0
					ldblBxR_Tot_Vlr_T1 = 0
					ldblBxR_Tot_Vlr_To = 0
					ldblRes_Tot_Vlr_To = 0
					ldblBxR_Tot_Vlr_T2 = 0
					ldblRes_Tot_Vlr_T2 = 0
					ldblBxR_Tot_Vlr_T3 = 0
					ldblRes_Tot_Vlr_T3 = 0
					ldblBxR_Tot_Vlr_T4 = 0
					ldblRes_Tot_Vlr_T4 = 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"'
						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"'
						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"'
						llngRes_Qtd_T1 = CInt(IIf(IsDbNull(.Fields("t1qtde").value), 0, .Fields("t1qtde").value)) 'Residencial 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"'
						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_T2 = CInt(IIf(IsDbNull(.Fields("t2qtde").value), 0, .Fields("t2qtde").value)) '246
						'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_T3 = CInt(IIf(IsDbNull(.Fields("t3qtde").value), 0, .Fields("t3qtde").value)) '485
						'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_T4 = CInt(IIf(IsDbNull(.Fields("t4qtde").value), 0, .Fields("t4qtde").value)) 'antigo bx renda
						'Totalizador da qtde de consumidores
						llngRes_Tot_Qtd_Nr = llngRes_Tot_Qtd_Nr + llngRes_Qtd_Nr
						llngRes_Tot_Qtd_T1 = llngRes_Tot_Qtd_T1 + llngRes_Qtd_T1
						llngBxR_Tot_Qtd_To = llngBxR_Tot_Qtd_To + llngBxR_Qtd_To
						llngBxR_Tot_Qtd_T2 = llngBxR_Tot_Qtd_T2 + llngBxR_Qtd_T2
						llngBxR_Tot_Qtd_T3 = llngBxR_Tot_Qtd_T3 + llngBxR_Qtd_T3
						llngBxR_Tot_Qtd_T4 = llngBxR_Tot_Qtd_T4 + llngBxR_Qtd_T4
						'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"'
						ldblRes_Qtd_T1 = CDbl(IIf(IsDbNull(.Fields("t1kwhc").value), 0, .Fields("t1kwhc").value)) 'Residencial 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"'
						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_T2 = CDbl(IIf(IsDbNull(.Fields("t2kwhc").value), 0, .Fields("t2kwhc").value)) '246
						'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_T3 = CDbl(IIf(IsDbNull(.Fields("t3kwhc").value), 0, .Fields("t3kwhc").value)) '485
						'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_T4 = CDbl(IIf(IsDbNull(.Fields("t4kwhc").value), 0, .Fields("t4kwhc").value)) 'antigo bx renda
						'Totalizador de MWh
						ldblRes_Tot_Qtd_T1 = ldblRes_Tot_Qtd_T1 + ldblRes_Qtd_T1
						ldblBxR_Tot_Qtd_To = ldblBxR_Tot_Qtd_To + ldblBxR_Qtd_To
						ldblBxR_Tot_Qtd_T2 = ldblBxR_Tot_Qtd_T2 + ldblBxR_Qtd_T2
						ldblBxR_Tot_Qtd_T3 = ldblBxR_Tot_Qtd_T3 + ldblBxR_Qtd_T3
						ldblBxR_Tot_Qtd_T4 = ldblBxR_Tot_Qtd_T4 + ldblBxR_Qtd_T4
						'Valor do 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"'
						ldblRes_Vlr_T1 = CDbl(IIf(IsDbNull(.Fields("t1valr").value), 0, .Fields("t1valr").value)) 'Residencial 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"'
						ldblBxR_Vlr_T1 = CDbl(IIf(IsDbNull(.Fields("t1bxvalr").value), 0, .Fields("t1bxvalr").value)) 'Residencial Normal mas com tarifa 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_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"'
						ldblRes_Vlr_To = CDbl(IIf(IsDbNull(.Fields("totvalr").value), 0, .Fields("totvalr").value)) 'Total do t2 e t3 mas com tarifa 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"'
						ldblBxR_Vlr_T2 = CDbl(IIf(IsDbNull(.Fields("t2bxvalr").value), 0, .Fields("t2bxvalr").value)) '246
						'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_T2 = CDbl(IIf(IsDbNull(.Fields("t2valr").value), 0, .Fields("t2valr").value)) '246 com tarifa 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"'
						ldblBxR_Vlr_T3 = CDbl(IIf(IsDbNull(.Fields("t3bxvalr").value), 0, .Fields("t3bxvalr").value)) '485
						'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_T3 = CDbl(IIf(IsDbNull(.Fields("t3valr").value), 0, .Fields("t3valr").value)) '485 com tarifa 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"'
						ldblBxR_Vlr_T4 = CDbl(IIf(IsDbNull(.Fields("t4bxvalr").value), 0, .Fields("t4bxvalr").value)) 'antigo bx 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"'
						ldblRes_Vlr_T4 = CDbl(IIf(IsDbNull(.Fields("t4valr").value), 0, .Fields("t4valr").value)) 'antigo bx renda coom tarifa resindencial normal
						'Totalizador MWh
						ldblRes_Tot_Vlr_T1 = ldblRes_Tot_Vlr_T1 + ldblRes_Vlr_T1
						ldblBxR_Tot_Vlr_T1 = ldblBxR_Tot_Vlr_T1 + ldblBxR_Vlr_T1
						ldblBxR_Tot_Vlr_To = ldblBxR_Tot_Vlr_To + ldblBxR_Vlr_To
						ldblRes_Tot_Vlr_To = ldblRes_Tot_Vlr_To + ldblRes_Vlr_To
						ldblBxR_Tot_Vlr_T2 = ldblBxR_Tot_Vlr_T2 + ldblBxR_Vlr_T2
						ldblRes_Tot_Vlr_T2 = ldblRes_Tot_Vlr_T2 + ldblRes_Vlr_T2
						ldblBxR_Tot_Vlr_T3 = ldblBxR_Tot_Vlr_T3 + ldblBxR_Vlr_T3
						ldblRes_Tot_Vlr_T3 = ldblRes_Tot_Vlr_T3 + ldblRes_Vlr_T3
						ldblBxR_Tot_Vlr_T4 = ldblBxR_Tot_Vlr_T4 + ldblBxR_Vlr_T4
						ldblRes_Tot_Vlr_T4 = ldblRes_Tot_Vlr_T4 + ldblRes_Vlr_T4
						'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") & ";" & llngRes_Qtd_Nr & ";"
						lstrLin = lstrLin & FormatNumber(llngRes_Qtd_T1, 0) & ";" & FormatNumber(llngBxR_Qtd_To, 0) & ";" & FormatNumber(llngBxR_Qtd_T2, 0) & ";" & FormatNumber(llngBxR_Qtd_T3, 0) & ";" & FormatNumber(llngBxR_Qtd_T4, 0) & ";"
						lstrLin = lstrLin & FormatNumber(ldblRes_Qtd_T1, 3) & ";" & FormatNumber(ldblBxR_Qtd_To, 3) & ";" & FormatNumber(ldblBxR_Qtd_T2, 3) & ";" & FormatNumber(ldblBxR_Qtd_T3, 3) & ";" & FormatNumber(ldblBxR_Qtd_T4, 3) & ";"
						lstrLin = lstrLin & FormatNumber(ldblRes_Vlr_T1, 2) & ";" & FormatNumber(ldblBxR_Vlr_To, 2) & ";" & FormatNumber(ldblBxR_Vlr_T2, 2) & ";" & FormatNumber(ldblBxR_Vlr_T3, 2) & ";" & FormatNumber(ldblBxR_Vlr_T4, 2) & ";"
						PrintLine(lintARQ, lstrLin)
						If lstrFase = "T" Then
							PrintLine(lintARQ, ";")
						End If
						Call .MoveNext()
					End While
					PrintLine(lintARQ, ";")
					lstrLin = ""
					lstrLin = lstrLin & "Total;;" & FormatNumber(llngRes_Tot_Qtd_Nr, 0) & ";"
					lstrLin = lstrLin & FormatNumber(llngRes_Tot_Qtd_T1, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_To, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_T2, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_T3, 0) & ";" & FormatNumber(llngBxR_Tot_Qtd_T4, 0) & ";"
					lstrLin = lstrLin & FormatNumber(ldblRes_Tot_Qtd_T1, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_To, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T2, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T3, 3) & ";" & FormatNumber(ldblBxR_Tot_Qtd_T4, 3) & ";"
					lstrLin = lstrLin & FormatNumber(ldblRes_Tot_Vlr_T1, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_To, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T2, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T3, 2) & ";" & FormatNumber(ldblBxR_Tot_Vlr_T4, 2) & ";"
					PrintLine(lintARQ, lstrLin)
					PrintLine(lintARQ, ";")
					'Calculo do Aumento e Redução
					ldblReducao = (ldblRes_Tot_Vlr_To - ldblBxR_Tot_Vlr_To) - (ldblRes_Tot_Vlr_T4 - ldblBxR_Tot_Vlr_T4)
					ldblAumento = (ldblRes_Tot_Vlr_T1 - ldblBxR_Tot_Vlr_T1)
					If VB6.Format(Me.dtpMesAno.value, "mm/yyyy") = "03/2010" Then
						ldblReducao = 4404.91
					End If
					PrintLine(lintARQ, "Valor Mensal da Redução da Receita (R$);;;;" & FormatNumber(ldblReducao, 2))
					PrintLine(lintARQ, ";")
					PrintLine(lintARQ, ";Valor Mensal da Diferença da Receita (R$);;;;;" & FormatNumber(ldblReducao - ldblAumento, 2))
					PrintLine(lintARQ, ";")
					PrintLine(lintARQ, "Valor Mensal do Aumento da Receita (R$);;;;" & FormatNumber(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\PlanilhaBaixaRenda2.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 = ""
			.Action = 0
		End With
	End Sub
	
	Private Function BaixaRenda(ByVal adblkWh As Double, ByVal adteLeituraAnterior As Date, ByVal adteLeituraAtual As Date) As Double
		'Contrle de tarifas
		Dim lstrCod As String
		Dim ldblTar As Double
		'Controle das faixas
		Dim ldblLimite As Double
		Dim ldblLimIni As Double
		Dim ldblLimFim As Double
		'Controle do Consumo
		Dim ldblAux As Double
		Dim ldblkWh As Double
		Dim ldblVlr As Double
		Dim ldblTot As Double
		'Query
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		ldblAux = adblkWh
		ldblLimIni = 0
		ldblkWh = 0
		ldblTot = 0
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT `tarifa`.`num`, `tarifa`.`datanova`, `tarifa`.`limitkwh` "
		lstrSql = lstrSql & "    FROM `tarifa` "
		lstrSql = lstrSql & "   WHERE STR_TO_DATE(`tarifa`.`datanova`,'%d/%m/%Y%') <= '" & VB6.Format(CDate("01/" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy")), "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "     AND LEFT(`tarifa`.`num`, 8) = '01B10211' "
		'lstrSql = lstrSql & "     AND LEFT(TRIM(`tarifa`.`descri`), 7) = 'Energia' "
		lstrSql = lstrSql & "     AND `tarifa`.`valor` <> 0 "
		lstrSql = lstrSql & "ORDER BY STR_TO_DATE(`tarifa`.`datanova`,'%d/%m/%Y%') DESC, `tarifa`.`num` ASC, `tarifa`.`limitkwh` ASC"
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					While Not .EOF And ldblAux > 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"'
						lstrCod = Trim(IIf(IsDbNull(.Fields("num").value), "", .Fields("num").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"'
						ldblLimFim = CDbl(IIf(IsDbNull(.Fields("limitkwh").value), 0, .Fields("limitkwh").value))
						ldblTar = Arredondar(TarifaProporcional(adteLeituraAnterior, adteLeituraAtual, lstrCod, 3) / 1000, 5)
						ldblLimite = ldblLimFim - ldblLimIni
						If ldblAux <= ldblLimite Then
							ldblkWh = ldblAux
							ldblVlr = Arredondar(ldblAux * ldblTar, 2)
							ldblAux = 0
						Else
							ldblkWh = ldblLimite
							ldblVlr = Arredondar(ldblLimite * ldblTar, 2)
							ldblAux = ldblAux - ldblLimite
						End If
						ldblTot = ldblTot + ldblVlr
						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 = ldblTot
	End Function
	
	Private Function Residencial(ByVal adblkWh As Double, ByVal adteLeituraAnterior As Date, ByVal adteLeituraAtual As Date) As Double
		Dim lstrCod As String
		Dim ldblTar As Double
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		'Pega a tarifa do Residencial Normal
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT `tarifa`.`num`, `tarifa`.`datanova` "
		lstrSql = lstrSql & "    FROM `tarifa` "
		lstrSql = lstrSql & "   WHERE STR_TO_DATE(`tarifa`.`datanova`,'%d/%m/%Y%') <= '" & VB6.Format(CDate("01/" & VB6.Format(Me.dtpMesAno.value, "mm/yyyy")), "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "     AND LEFT(`tarifa`.`num`, 8) = '01B10114' "
		lstrSql = lstrSql & "     AND `tarifa`.`valor` <> 0 "
		lstrSql = lstrSql & "ORDER BY STR_TO_DATE(`tarifa`.`datanova`,'%d/%m/%Y%') DESC, `tarifa`.`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"'
					lstrCod = Trim(IIf(IsDbNull(.Fields("num").value), "", .Fields("num").value))
					ldblTar = Arredondar(TarifaProporcional(adteLeituraAnterior, adteLeituraAtual, lstrCod, 3) / 1000, 5)
					Residencial = adblkWh * 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
	End Function
	
	Private Sub NomeArquivoXML()
		Me.txtDir.Text = istrCaminho
		Me.txtArquivo.Text = istrArquivo & VB6.Format(dtpMesAno.Month, "00") & dtpMesAno.Year & "_V" & VB6.Format(txtVersao.Text, "00") & ".XML"
	End Sub
	
	Private Sub LigDes(ByRef aModo As Boolean)
		lblMesAno.Enabled = aModo
		dtpMesAno.Enabled = aModo
		lblVersao.Enabled = aModo
		txtVersao.Enabled = aModo
		lblArquivo.Enabled = aModo
		txtDir.Enabled = aModo
		cmdSalvarArq.Enabled = aModo
		cmdProcessar.Enabled = aModo
		cmdImprimir.Enabled = aModo
		cmdGerar.Enabled = aModo
		cmdFechar.Enabled = aModo
	End Sub
End Class