Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports CrystalDecisions.CrystalReports.Engine
Friend Class frmEmissaoBaixaRendaVI
	Inherits System.Windows.Forms.Form
	
	Private istrTabela As String
	Private istrArquivo As String
	Private istrCaminho As String
	Private objatualizacaobancodados As New entidadeAtualizaBancoDados
	Private repositorioatualizacaobancodados As New repositorioAtualizaBancoDados

	Private objexecatualizacaobancodados As New entidadeAtualizaBancoDados
	Private repositorioexecatualizacaobancodados As New repositorioAtualizaBancoDados
	Private dt, dt1, dt2, dt3 As DataTable
	Private LImagem() As Byte
	Private sNomeEmpresa As String
	Private sEnderecoEmpresa As String
	Private sCepEmpresa As String
	Private sCidadeEmpresa As String
	Private sUFempresa As String
	Private sBairroEmpresa As String
	Private sFoneEmpresa As String
	Private sWebEmpresa 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
		Dim sData As String

		Try
			'Call CenterForm(Me)
			sData = GetConfig("data.nova.bxrenda")
			If Not IsDate(sData) Then
				sData = Date.Now
				Me.chkNovoNis.Text = Me.chkNovoNis.Text & " > " & CDate(sData)
			Else
				Me.chkNovoNis.Text = Me.chkNovoNis.Text & " > " & CDate(sData)
			End If
			istrTabela = "aux_planilha_bxrenda"
			Me.DTPMesAno.Value = Date.Now
			istrCaminho = GetConfig("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
			objexecatualizacaobancodados.QUERY_NAME = lstrSql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)

			lstrSql = "DROP TABLE IF EXISTS " & istrTabela & "_total "
			objexecatualizacaobancodados.QUERY_NAME = lstrSql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)

			Call sCriaTabelaAuxiliares()

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmEmissaoBaixaRendaVI_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub frmEmissaoBaixaRendaVI_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Try
			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 TabEnter(KeyAscii, eventArgs)
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmEmissaoBaixaRendaVI_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	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
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtDir_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdSalvarArq_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvarArq.Click
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdSalvarArq_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdGerar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerar.Click
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdGerar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim lstrsql As String

		Try
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

			lstrsql = "DELETE FROM " & istrTabela
			objexecatualizacaobancodados.QUERY_NAME = lstrsql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)

			lstrsql = "DELETE FROM " & istrTabela & "_total "
			objexecatualizacaobancodados.QUERY_NAME = lstrsql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)

			'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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProcessar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdImprimir_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	Private 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 sData As String
		'Dim lrstNis As ADODB.Recordset
		'Dim lrstAux As ADODB.Recordset
		'lrstNis = New ADODB.Recordset
		'lrstAux = New ADODB.Recordset
		Try
			sData = GetConfig("data.nova.bxrenda")
			If Not IsDate(sData) Then
				sData = Date.Now
				ldteNovoBxRenda = CDate(sData)
			Else
				ldteNovoBxRenda = CDate(GetConfig("data.nova.bxrenda"))
			End If

			'ldteNovoBxRenda = CDate(GetConfig("data.nova.bxrenda"))
			ldteBase = CDate(UltimoDiaMes(Month(Me.DTPMesAno.Value), Year(Me.DTPMesAno.Value)) & "/" & 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") & "')) "
			objatualizacaobancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

			If dt.Rows.Count > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
				For Each row As DataRow In dt.Rows

					lstrItens = Trim(IIf(IsDBNull(row.Item("itensserv")), "", row.Item("itensserv")))
					lstrFase = Trim(IIf(IsDBNull(row.Item("fase")), "M", row.Item("fase")))
					ldblQtdkWh = BuscaVlrItem(lstrItens, "030201009")
					ldblVlrkWh = BuscaVlrItem(lstrItens, "030202009")
					'Para Buscar as informções para o T1 e para o T4

					lstrZona = Trim(IIf(IsDBNull(row.Item("zona")), "000", row.Item("zona")))
					lstrLivro = Trim(IIf(IsDBNull(row.Item("livro")), "00", row.Item("livro")))
					lstrConta = Trim(IIf(IsDBNull(row.Item("conta")), "000000", row.Item("conta")))
					lstrClasse = Trim(IIf(IsDBNull(row.Item("codclas")), "0000", row.Item("codclas")))
					ldteLAtual = CDate(IIf(IsDBNull(row.Item("dataleit")), "1900-01-01", row.Item("dataleit")))
					ldteLAnter = CDate(IIf(IsDBNull(row.Item("dataleitant")), "1900-01-01", row.Item("dataleitant")))
					llngUnidId = CInt(IIf(IsDBNull(row.Item("unid_id")), 0, row.Item("unid_id")))

					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
					objatualizacaobancodados.QUERY_NAME = lstrSql
					dt1 = repositorioexecatualizacaobancodados.Consultar(objexecatualizacaobancodados)

					lblnNovoNis = (dt1.Rows.Count > 0)

					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
					objexecatualizacaobancodados.QUERY_NAME = lstrSql
					repositorioexecatualizacaobancodados.Execute(objexecatualizacaobancodados)

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					System.Windows.Forms.Application.DoEvents()
				Next
				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

			If lblnContinua Then
				'Segunda Parte colocar em uma tabela todos os dados mastigados para facilitar no crystal
				llngLiRes = CInt(GetConfig("limite.residencial.xml"))
				lstrFases = New Object() {" AND fase='M' ", " AND fase='B' ", " AND fase = 'T' "}
				If Me.chkOficioCircular120.CheckState = 1 Then
					lstrFxPla = New Object() {" Até 30       ", " De  31 até  50 ", " De  51 até 100 ", " De 101 até 220 ", " Acima 220    "}
					lstrFxXml = New Object() {"de01ate30", "de31ate50", "de51ate100", "de101ate220", "Acima220"}
					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
					lstrFxPla = New Object() {" Até 30       ", " De  31 até  79 ", " De  80 até 100 ", " De 101 até " & llngLiRes & " ", " De " & llngLiRes + 1 & " até 220 ", " Acima 220    "}
					lstrFxXml = New Object() {"de01ate30", "de31ate79", "de80ate100", "de101ate_limite_regional", "limite_regional_ate220", "Acima220"}
					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 "
						lstrSql = lstrSql & lstrFaixa(llngFaixa) & lstrFases(llngFases)
						objatualizacaobancodados.QUERY_NAME = lstrSql
						dt3 = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

						If dt3.Rows.Count > 0 Then
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt3.Rows.Count)
							'
							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

							For Each row3 As DataRow In dt3.Rows
								'quantidade de unidades consumidoras
								llngReqtde = llngReqtde + 1

								If CInt(IIf(IsDBNull(row3.Item("t5_kwhc")), 0, row3.Item("t5_kwhc"))) > 0 Then llngT5qtde = llngT5qtde + 1
								If CInt(IIf(IsDBNull(row3.Item("t6_kwhc")), 0, row3.Item("t6_kwhc"))) > 0 Then llngT6qtde = llngT6qtde + 1
								If CInt(IIf(IsDBNull(row3.Item("t7_kwhc")), 0, row3.Item("t7_kwhc"))) > 0 Then llngT7qtde = llngT7qtde + 1
								If CInt(IIf(IsDBNull(row3.Item("t8_kwhc")), 0, row3.Item("t8_kwhc"))) > 0 Then llngT8qtde = llngT8qtde + 1
								'kwh
								ldblReskwhc = ldblReskwhc + CInt(IIf(IsDBNull(row3.Item("kwhc")), 0, row3.Item("kwhc")))
								ldblT5kwhc = ldblT5kwhc + CInt(IIf(IsDBNull(row3.Item("t5_kwhc")), 0, row3.Item("t5_kwhc")))
								ldblT6kwhc = ldblT6kwhc + CInt(IIf(IsDBNull(row3.Item("t6_kwhc")), 0, row3.Item("t6_kwhc")))
								ldblT7kwhc = ldblT7kwhc + CInt(IIf(IsDBNull(row3.Item("t7_kwhc")), 0, row3.Item("t7_kwhc")))
								ldblT8kwhc = ldblT8kwhc + CInt(IIf(IsDBNull(row3.Item("t8_kwhc")), 0, row3.Item("t8_kwhc")))
								'valor normal
								ldblReskvlr = ldblReskvlr + CDbl(IIf(IsDBNull(row3.Item("res_valor")), 0, row3.Item("res_valor")))
								ldblT5kvlr = ldblT5kvlr + CDbl(IIf(IsDBNull(row3.Item("t5_valor")), 0, row3.Item("t5_valor")))
								ldblT6kvlr = ldblT6kvlr + CDbl(IIf(IsDBNull(row3.Item("t6_valor")), 0, row3.Item("t6_valor")))
								ldblT7kvlr = ldblT7kvlr + CDbl(IIf(IsDBNull(row3.Item("t7_valor")), 0, row3.Item("t7_valor")))
								ldblT8kvlr = ldblT8kvlr + CDbl(IIf(IsDBNull(row3.Item("t8_valor")), 0, row3.Item("t8_valor")))
								'valor baixa renda
								ldblT5kbxv = ldblT5kbxv + CDbl(IIf(IsDBNull(row3.Item("t5_bxvalor")), 0, row3.Item("t5_bxvalor")))
								ldblT6kbxv = ldblT6kbxv + CDbl(IIf(IsDBNull(row3.Item("t6_bxvalor")), 0, row3.Item("t6_bxvalor")))
								ldblT7kbxv = ldblT7kbxv + CDbl(IIf(IsDBNull(row3.Item("t7_bxvalor")), 0, row3.Item("t7_bxvalor")))
								ldblT8kbxv = ldblT8kbxv + CDbl(IIf(IsDBNull(row3.Item("t8_bxvalor")), 0, row3.Item("t8_bxvalor")))

								Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
							Next
						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


						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), ",", ".") & " "
						objexecatualizacaobancodados.QUERY_NAME = lstrSql
						repositorioexecatualizacaobancodados.Execute(objexecatualizacaobancodados)
					Next llngFaixa
				Next llngFases
			End If

			Me.lblInfo.Text = "Pronto!"
			Call Me.lblInfo.Refresh()
			MontaTabela = True

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaTabela" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	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
		Try
			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 "
			objatualizacaobancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

			If dt.Rows.Count > 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(Month(Me.DTPMesAno.Value), "00") & """ ano=""" & Year(Me.DTPMesAno.Value) & """ 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, dt, 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

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GerarXML" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub MontaDetalheArquivoXML(ByRef aintArq As Short, ByVal arstAux As DataTable, 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
		Dim lstrsql As String

		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 "
		objatualizacaobancodados.QUERY_NAME = lstrSql
		dt = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

		With dt
			If .Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					lintCtr = CShort(IIf(IsDBNull(row.Item("ordimp1")), 0, row.Item("ordimp1")))
					lstrFai = IIf(IsDBNull(row.Item("faixaxml")), "", row.Item("faixaxml"))
					lstrFas = Trim(IIf(IsDBNull(row.Item("fase")), "M", row.Item("fase")))
					lstrFas = IIf(lstrFas = "M", "monofasico", IIf(lstrFas = "B", "bifasico", "trifasico"))
					ldblAux = CDbl(IIf(IsDBNull(row.Item(astrCam)), 0, row.Item(astrCam)))
					ldblToN = ldblToN + CDbl(IIf(IsDBNull(row.Item("totvalr")), 0, row.Item("totvalr")))
					ldblToB = ldblToB + CDbl(IIf(IsDBNull(row.Item("totbxvalr")), 0, row.Item("totbxvalr")))
					If lintCtr = 0 Then
						PrintLine(aintArq, lstrTab & lstrTab & lstrTab & "<" & lstrFai & ">")
					End If
					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
				Next
			End If
		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
		Try
			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 "
			objatualizacaobancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

			If dt.Rows.Count > 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, ";")
				For Each rowMesAno As DataRow In dt.Rows
					PrintLine(lintARQ, "MÊS/ANO:;" & IIf(IsDBNull(rowMesAno.Item("mesano")), "", rowMesAno.Item("mesano")) & ";")
					Exit For
				Next
				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
				'
				For Each row As DataRow In dt.Rows

					lstrFaixa = IIf(IsDBNull(row.Item("faixa")), "", row.Item("faixa"))
					lstrFase = IIf(IsDBNull(row.Item("fase")), "M", row.Item("fase"))
					'Quantidade de Consumidores
					llngRes_Qtd_Nr = CInt(IIf(IsDBNull(row.Item("restotal")), 0, row.Item("restotal"))) 'Residenciais
					llngBxR_Qtd_To = CInt(IIf(IsDBNull(row.Item("totbxqtde")), 0, row.Item("totbxqtde"))) 'Total Baixa Renda
					llngBxR_Qtd_T5 = CInt(IIf(IsDBNull(row.Item("t5qtde")), 0, row.Item("t5qtde"))) 'novo nis
					llngBxR_Qtd_T6 = CInt(IIf(IsDBNull(row.Item("t6qtde")), 0, row.Item("t6qtde"))) 'Indígena
					llngBxR_Qtd_T7 = CInt(IIf(IsDBNull(row.Item("t7qtde")), 0, row.Item("t7qtde"))) 'Quilomola
					llngBxR_Qtd_T8 = CInt(IIf(IsDBNull(row.Item("t8qtde")), 0, row.Item("t8qtde"))) '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(row.Item("totbxkhwc")), 0, row.Item("totbxkhwc"))) '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(row.Item("t5kwhc")), 0, row.Item("t5kwhc"))) '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(row.Item("t6kwhc")), 0, row.Item("t6kwhc"))) '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(row.Item("t7kwhc")), 0, row.Item("t7kwhc"))) '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(row.Item("t8kwhc")), 0, row.Item("t8kwhc"))) '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(row.Item("reskwhc")), 0, row.Item("reskwhc"))) '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(row.Item("totbxvalr")), 0, row.Item("totbxvalr"))) '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(row.Item("t5bxvalr")), 0, row.Item("t5bxvalr"))) '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(row.Item("t6bxvalr")), 0, row.Item("t6bxvalr"))) '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(row.Item("t7bxvalr")), 0, row.Item("t7bxvalr"))) '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(row.Item("t8bxvalr")), 0, row.Item("t8bxvalr"))) '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(row.Item("totvalr")), 0, row.Item("totvalr"))) '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(row.Item("t5valr")), 0, row.Item("t5valr"))) '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(row.Item("t6valr")), 0, row.Item("t6valr"))) '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(row.Item("t7valr")), 0, row.Item("t7valr"))) '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(row.Item("t8valr")), 0, row.Item("t8valr"))) '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(row.Item("resvalr")), 0, row.Item("resvalr"))) '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
				Next
				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

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GerarCSV" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub EmitirPlanilha()
		Try
			Call sDadosEmpresa(LImagem, sNomeEmpresa, sEnderecoEmpresa, sCepEmpresa, sCidadeEmpresa, sUFempresa, sBairroEmpresa, sFoneEmpresa, sWebEmpresa)
			Call lPrint("rptPlanilhabxrendaVI")

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: EmitirPlanilha" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	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
		'
		Try
			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 "
			objatualizacaobancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					ldblLimFim = CDbl(IIf(IsDBNull(row.Item("limitkwh")), 0, row.Item("limitkwh")))
					ldblLimAux = ldblLimFim - ldblLimIni
					lstrCod = Trim(IIf(IsDBNull(row.Item("num")), "", row.Item("num")))
					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
				Next
			End If

			BaixaRenda = ldblRet
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BaixaRenda" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	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
		Try
			'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"
			objatualizacaobancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					lstrCod = Trim(IIf(IsDBNull(row.Item("num")), "", row.Item("num")))
					ldblTar = Arredondar(TarifaProporcional(adteLeiAnter, adteLeiAtual, lstrCod, 3) / 1000, 5)
					ldblRet = adblConsumo * ldblTar
				Next
			End If

			Residencial = ldblRet
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: Residencial" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	End Function

	Private Sub NomeArquivoXML()
		Try
			istrArquivo = "APLDMR" & gstrEmpresa_CodRes89 & "_" & gstrEmpresa_Sigla & "_89_"
			Me.txtDir.Text = istrCaminho
			Me.txtArquivo.Text = istrArquivo & VB6.Format(Month(Me.DTPMesAno.Value), "00") & Year(Me.DTPMesAno.Value) & "_V" & VB6.Format(Me.txtVersao.Text, "00") & ".XML"
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: NomeArquivoXML" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmEmissaoBaixaRendaVI_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

		Dim lstrSql As String

		Try
			lstrSql = "DROP TABLE IF EXISTS " & istrTabela
			objexecatualizacaobancodados.QUERY_NAME = lstrSql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)

			lstrSql = "DROP TABLE IF EXISTS " & istrTabela & "_total "
			objexecatualizacaobancodados.QUERY_NAME = lstrSql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmEmissaoBaixaRendaVI_FormClosed" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub LigDes(ByRef aModo As Boolean)
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LigDes" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub sCriaTabelaAuxiliares()

		Try

			Dim lstrsql As String

			lstrsql = ""
			lstrsql = lstrsql & "CREATE TABLE IF NOT EXISTS " & istrTabela & " ( "
			lstrsql = lstrsql & "`fase`       varchar(10), "
			lstrsql = lstrsql & "`kwhc`       varchar(20), "
			lstrsql = lstrsql & "`t5_kwhc`    varchar(20), "
			lstrsql = lstrsql & "`t6_kwhc`    varchar(20), "
			lstrsql = lstrsql & "`t7_kwhc`    varchar(20), "
			lstrsql = lstrsql & "`t8_kwhc`    varchar(20), "
			lstrsql = lstrsql & "`res_valor`  varchar(20), "
			lstrsql = lstrsql & "`t5_valor`   varchar(20), "
			lstrsql = lstrsql & "`t5_bxvalor` varchar(20), "
			lstrsql = lstrsql & "`t6_bxvalor` varchar(20), "
			lstrsql = lstrsql & "`t7_bxvalor` varchar(20), "
			lstrsql = lstrsql & "`t6_valor`   varchar(20), "
			lstrsql = lstrsql & "`t7_valor`   varchar(20), "
			lstrsql = lstrsql & "`t8_bxvalor` varchar(20), "
			lstrsql = lstrsql & "`t8_valor`   varchar(20)  "
			lstrsql = lstrsql & ") "
			objexecatualizacaobancodados.QUERY_NAME = lstrsql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)


			lstrsql = ""
			lstrsql = lstrsql & "CREATE TABLE IF NOT EXISTS " & istrTabela & "_total ("
			lstrsql = lstrsql & "`ordimp1`  varchar(20), "
			lstrsql = lstrsql & "`ordimp2`  varchar(20), "
			lstrsql = lstrsql & "`faixa`    varchar(20), "
			lstrsql = lstrsql & "`fase`     varchar(20), "
			lstrsql = lstrsql & "`restotal` varchar(20),"
			lstrsql = lstrsql & "`t5qtde`   varchar(20), "
			lstrsql = lstrsql & "`totbxqtde` varchar(20), "
			lstrsql = lstrsql & "`t6qtde`   varchar(20), "
			lstrsql = lstrsql & "`t7qtde`   varchar(20), "
			lstrsql = lstrsql & "`t8qtde`   varchar(20), "
			'
			lstrsql = lstrsql & "`reskwhc`  varchar(20), "
			lstrsql = lstrsql & "`t5kwhc`   varchar(20), "
			lstrsql = lstrsql & "`totbxkhwc` varchar(20), "
			lstrsql = lstrsql & "`t6kwhc`   varchar(20), "
			lstrsql = lstrsql & "`t7kwhc`   varchar(20), "
			lstrsql = lstrsql & "`t8kwhc`   varchar(20), "
			'
			lstrsql = lstrsql & "`resvalr`  varchar(20), "
			lstrsql = lstrsql & "`t5valr`   varchar(20), "
			lstrsql = lstrsql & "`t5bxvalr` varchar(20), "
			lstrsql = lstrsql & "`totbxvalr` varchar(20), "
			lstrsql = lstrsql & "`t6bxvalr` varchar(20), "
			lstrsql = lstrsql & "`t7bxvalr` varchar(20), "
			lstrsql = lstrsql & "`totvalr`  varchar(20), "
			lstrsql = lstrsql & "`t6valr`   varchar(20), "
			lstrsql = lstrsql & "`t7valr`   varchar(20), "
			lstrsql = lstrsql & "`t8bxvalr` varchar(20), "
			lstrsql = lstrsql & "`t8valr`   varchar(20), "
			'
			lstrsql = lstrsql & "`mesano`   varchar(20), "
			lstrsql = lstrsql & "`faixaxml` varchar(50))"
			objexecatualizacaobancodados.QUERY_NAME = lstrsql
			repositorioatualizacaobancodados.Execute(objexecatualizacaobancodados)

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: sCriaTabelaAuxiliares" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub
	Private Sub lPrint(ByVal vNomeRPT As String)

		Dim dt As DataTable
		Dim dt_1 As DataTable
		Dim dt_2 As DataTable
		Dim dt_3 As DataTable
		Dim dc As DataColumn
		Dim ds As DataSet
		Dim lstrsql As String

		'Dim bytBLOBData() As Byte

		'Dim objEmpresa As New entidadeEmpresa
		'Dim repositorioEmpresa As New repositorioEmpresa

		'Dim vlist

		Try
			Dim rpt As New ReportDocument()

			If vNomeRPT = "rptPlanilhabxrendaVI" Then

				'vlist = repositorioEmpresa.Select(objEmpresa)

				'If vlist.Count <> 0 Then
				'    For Each objEmpresaFor As entidadeEmpresa In vlist
				'        bytBLOBData = objEmpresaFor.LOGOTIPO
				'    Next
				'End If

				dt_2 = New DataTable()
				dt_2.Columns.Add("ordimp1", GetType(String))
				dt_2.Columns.Add("ordimp2", GetType(String))
				dt_2.Columns.Add("faixa", GetType(String))
				dt_2.Columns.Add("fase", GetType(String))
				dt_2.Columns.Add("restotal", GetType(String))
				dt_2.Columns.Add("t5qtde", GetType(String))
				dt_2.Columns.Add("totbxqtde", GetType(String))
				dt_2.Columns.Add("t6qtde", GetType(String))
				dt_2.Columns.Add("t7qtde", GetType(String))
				dt_2.Columns.Add("t8qtde", GetType(String))
				dt_2.Columns.Add("reskwhc", GetType(String))
				dt_2.Columns.Add("t5kwhc", GetType(String))
				dt_2.Columns.Add("totbxkhwc", GetType(String))
				dt_2.Columns.Add("t6kwhc", GetType(String))
				dt_2.Columns.Add("t7kwhc", GetType(String))
				dt_2.Columns.Add("t8kwhc", GetType(String))
				dt_2.Columns.Add("resvalr", GetType(String))
				dt_2.Columns.Add("t5valr", GetType(String))
				dt_2.Columns.Add("t5bxvalr", GetType(String))
				dt_2.Columns.Add("totbxvalr", GetType(String))
				dt_2.Columns.Add("t6bxvalr", GetType(String))
				dt_2.Columns.Add("t7bxvalr", GetType(String))
				dt_2.Columns.Add("totvalr", GetType(String))
				dt_2.Columns.Add("t6valr", GetType(String))
				dt_2.Columns.Add("t7valr", GetType(String))
				dt_2.Columns.Add("t8bxvalr", GetType(String))
				dt_2.Columns.Add("t8valr", GetType(String))
				dt_2.Columns.Add("mesano", GetType(String))
				dt_2.Columns.Add("faixaxml", GetType(String))
				dt_2.Columns.Add("IMAGEM", GetType(Byte()))

				ds = New DataSet

				lstrsql = ""
				lstrsql = "SELECT * FROM AUX_PLANILHA_BXRENDA_TOTAL " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
				objatualizacaobancodados.QUERY_NAME = lstrsql
				dt = repositorioatualizacaobancodados.Consultar(objatualizacaobancodados)

				If dt.Rows.Count > 0 Then
					For Each dr_1 As DataRow In dt.Rows

						dt_2.Rows.Add(dr_1("ordimp1"),
										  dr_1("ordimp2"),
										  dr_1("faixa"),
										  dr_1("fase"),
										  dr_1("restotal"),
										  dr_1("t5qtde"),
										  dr_1("totbxqtde"),
										  dr_1("t6qtde"),
										  dr_1("t7qtde"),
										  dr_1("t8qtde"),
										  dr_1("reskwhc"),
										  dr_1("t5kwhc"),
										  dr_1("totbxkhwc"),
										  dr_1("t6kwhc"),
										  dr_1("t7kwhc"),
										  dr_1("t8kwhc"),
										  dr_1("resvalr"),
										  dr_1("t5valr"),
										  dr_1("t5bxvalr"),
										  dr_1("totbxvalr"),
										  dr_1("t6bxvalr"),
										  dr_1("t7bxvalr"),
										  dr_1("totvalr"),
										  dr_1("t6valr"),
										  dr_1("t7valr"),
										  dr_1("t8bxvalr"),
										  dr_1("t8valr"),
										  dr_1("mesano"),
										  dr_1("faixaxml"),
										  LImagem)
					Next dr_1
				End If

				ds.Tables.Add(dt)
				ds.Tables(0).TableName = "REL_PLANILHA_BXRENDA_TOTAL"

				If ds.Tables(0).Rows.Count = 0 Then
					MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
					Exit Sub
				Else

					rpt = New rptPlanilhabxrendaVI

					rpt.SetDataSource(ds)
					'rpt.SetParameterValue("sNomeEmpresa", sNomeEmpresa)
					'rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
					'rpt.SetParameterValue("cepempresa", sCepEmpresa)
					'rpt.SetParameterValue("ufempresa", sUFempresa)
					'rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
					'rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
					'rpt.SetParameterValue("foneempresa", sFoneEmpresa)
					'rpt.SetParameterValue("websiteempresa", sWebEmpresa)

					frmViewRelatorios.rpt = rpt

					frmViewRelatorios.CRViewer.Zoom((100))
					frmViewRelatorios.Text = "Planilha Baixa Renda"
					frmViewRelatorios.ShowDialog()

				End If


			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: lprint" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub
End Class