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 frmEmissaoBaixaRenda
	Inherits System.Windows.Forms.Form
	
	Private istrTabela As String
	Private istrArquivo As String
	Private istrCaminho As String

	Private objatualizabancodados As New entidadeAtualizaBancoDados
	Private repositorioatualizabancodados As New repositorioAtualizaBancoDados

	Private objexecatualizabancodados As New entidadeAtualizaBancoDados
	Private repositorioexecatualizabancodados 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 frmEmissaoBaixaRenda_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			Dim lstrSql As String
			istrTabela = "aux_planilha_baixa_renda"
			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 & "_89_"
			Me.txtVersao.Text = "01"
			Call NomeArquivoXML()

			'Dropa a tabela
			lstrSql = "DROP TABLE IF EXISTS " & istrTabela
			objexecatualizabancodados.QUERY_NAME = lstrSql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)

			lstrSql = "DROP TABLE IF EXISTS " & istrTabela & "_total "
			objexecatualizabancodados.QUERY_NAME = lstrSql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)

			Call sCriaTabelaAuxiliar()

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

	Private Sub frmEmissaoBaixaRenda_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: frmEmissaoBaixaRenda_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
				fbdressarcimento.ShowDialog()
				txtDir.Text = fbdressarcimento.SelectedPath & "\"
				'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
			objexecatualizabancodados.QUERY_NAME = lstrsql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)

			lstrsql = "DELETE FROM " & istrTabela & "_total "
			objexecatualizabancodados.QUERY_NAME = lstrsql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)

			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
		'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
		Dim lstrFxDescr(6) As String
		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
		Try
			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(`uc_faturamento`.`jul2010`,'N') AS `jul2010`, " & vbCrLf
			lstrSql = lstrSql & "       IFNULL(`uc_faturamento`.`bx0402`,'N') AS `bx0402` " & vbCrLf
			lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
			lstrSql = lstrSql & " LEFT JOIN `uc_faturamento` ON(`uc_faturamento`.`zona` = `nf`.`zona` AND `uc_faturamento`.`livro` = `nf`.`livro` AND `uc_faturamento`.`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) "
			objatualizabancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizabancodados.Consultar(objatualizabancodados)

			If dt.Rows.Count > 0 Then
				lblnRet = (dt.Rows.Count > 0)
				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")))
					lstrJul10 = Trim(IIf(IsDBNull(row.Item("jul2010")), "N", row.Item("jul2010")))
					lstrAbr02 = Trim(IIf(IsDBNull(row.Item("bx0402")), "N", row.Item("bx0402")))
					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")), "000", row.Item("livro")))
					lstrConta = Trim(IIf(IsDBNull(row.Item("conta")), "000", row.Item("conta")))
					lstrClasse = Trim(IIf(IsDBNull(row.Item("codclas")), "0000", row.Item("codclas")))
					ldteleitAtual = IIf(IsDBNull(row.Item("dataleit")), "1900-01-01", row.Item("dataleit"))
					ldteLeitAnter = IIf(IsDBNull(row.Item("dataleitant")), "1900-01-01", row.Item("dataleitant"))
					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
					objexecatualizabancodados.QUERY_NAME = lstrSql
					repositorioexecatualizabancodados.Execute(objexecatualizabancodados)
					'Call .MoveNext()
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				Next
			End If


			If lblnRet Then
				'Segunda Parte colocar em uma tabela todos os dados mastigados para facilitar no crystal
				llngLimRes = CInt(GetConfig("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 " & istrTabela
						lstrSql = lstrSql & lstrTpFaixa(llngProc) & lstrTpFases(llngFase)
						objatualizabancodados.QUERY_NAME = lstrSql
						dt1 = repositorioatualizabancodados.Consultar(objatualizabancodados)


						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 dt1.Rows.Count > 0 Then
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
							For Each row1 As DataRow In dt1.Rows
								'Quantidade Geral
								ldblQtde = ldblQtde + 1
								'Quantidade de consumidores por tipos dos kwh

								If IIf(IsDBNull(row1.Item("t1_kwhc")), 0, row1.Item("t1_kwhc")) > 0 Then ldblT1Qtd = ldblT1Qtd + 1
								If IIf(IsDBNull(row1.Item("t2_kwhc")), 0, row1.Item("t2_kwhc")) > 0 Then ldblT2Qtd = ldblT2Qtd + 1
								If IIf(IsDBNull(row1.Item("t3_kwhc")), 0, row1.Item("t3_kwhc")) > 0 Then ldblT3Qtd = ldblT3Qtd + 1
								If IIf(IsDBNull(row1.Item("t4_kwhc")), 0, row1.Item("t4_kwhc")) > 0 Then ldblT4Qtd = ldblT4Qtd + 1
								'Somatorio dos valores por tipos dos kwh

								ldblT1kWh = ldblT1kWh + IIf(IsDBNull(row1.Item("t1_kwhc")), 0, row1.Item("t1_kwhc"))
								ldblT2kWh = ldblT2kWh + IIf(IsDBNull(row1.Item("t2_kwhc")), 0, row1.Item("t2_kwhc"))
								ldblT3kWh = ldblT3kWh + IIf(IsDBNull(row1.Item("t3_kwhc")), 0, row1.Item("t3_kwhc"))
								ldblT4kWh = ldblT4kWh + IIf(IsDBNull(row1.Item("t4_kwhc")), 0, row1.Item("t4_kwhc"))
								'Residencial Normal
								ldblT1Vlr = ldblT1Vlr + IIf(IsDBNull(row1.Item("t1_valor")), 0, row1.Item("t1_valor"))
								'Residencial Normal com valor de baixa renda
								ldblT1BxVlr = ldblT1BxVlr + IIf(IsDBNull(row1.Item("t1_bxvalor")), 0, row1.Item("t1_bxvalor"))
								'Baixa renda 246
								ldblT2BxVlr = ldblT2BxVlr + IIf(IsDBNull(row1.Item("t2_bxvalor")), 0, row1.Item("t2_bxvalor"))
								'Baixa renda 246 com valor de residencial
								ldblT2Vlr = ldblT2Vlr + IIf(IsDBNull(row1.Item("t2_valor")), 0, row1.Item("t2_valor"))
								'Baixa renda 485
								ldblT3BxVlr = ldblT3BxVlr + IIf(IsDBNull(row1.Item("t3_bxvalor")), 0, row1.Item("t3_bxvalor"))
								'Baixa renda 485 com valor residencial
								ldblT3Vlr = ldblT3Vlr + IIf(IsDBNull(row1.Item("t3_valor")), 0, row1.Item("t3_valor"))
								'Baixa renda 246 e 485 que foram baixa renda em 2002
								ldblT4BxVlr = ldblT4BxVlr + IIf(IsDBNull(row1.Item("t4_bxvalor")), 0, row1.Item("t4_bxvalor"))
								'Baixa renda 246 e 485 que foram baixa renda em 2002 com valor de residencial
								ldblT4Vlr = ldblT4Vlr + IIf(IsDBNull(row1.Item("t4_valor")), 0, row1.Item("t4_valor"))
								'Call .MoveNext()
								Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
							Next
						End If
						' End If


						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO " & istrTabela & "_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(Month(DTPMesAno.Value), "00") & "/" & Year(DTPMesAno.Value) & "', "
						lstrSql = lstrSql & " `faixaxml`  = '" & lstrFxDesAx(llngProc) & "' "
						objexecatualizabancodados.QUERY_NAME = lstrSql
						repositorioexecatualizabancodados.Execute(objexecatualizabancodados)
					Next llngProc
				Next llngFase

			End If
			MontaTabela = lblnRet
		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 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
		Try
			'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, "
			strSql = strSql & " t1qtde, totbxqtde, t2qtde, t3qtde, t4qtde, t1kwhc, totbxkhwc, t2kwhc, t3kwhc, t4kwhc, "
			strSql = strSql & " t1valr, t1bxvalr, totbxvalr, t2bxvalr, t3bxvalr, totvalr, t2valr, t3valr, t4bxvalr, t4valr,mesano,faixaxml"
			strSql = strSql & " FROM " & istrTabela & "_total"
			strSql = strSql & " WHERE mesano = '" & VB6.Format(Month(DTPMesAno.Value), "00") & "/" & Year(DTPMesAno.Value) & "' " & "ORDER BY ordimp2 ASC, ordimp1 ASC "
			objatualizabancodados.QUERY_NAME = strSql
			dt = repositorioatualizabancodados.Consultar(objatualizabancodados)

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

		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 aARQ As Short, ByVal arsConsulta As DataTable, 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
		Dim strsql As String

		strTab = "   "

		dblTotal = 0
		dblTotValr = 0
		dblTotBXValr = 0
		dblT4Valr = 0
		dblT4BXValr = 0

		strsql = "SELECT ordimp1, ordimp2, faixa, fase, restotal, "
		strsql = strsql & " t1qtde, totbxqtde, t2qtde, t3qtde, t4qtde, t1kwhc, totbxkhwc, t2kwhc, t3kwhc, t4kwhc, "
		strsql = strsql & " t1valr, t1bxvalr, totbxvalr, t2bxvalr, t3bxvalr, totvalr, t2valr, t3valr, t4bxvalr, t4valr,mesano,faixaxml"
		strsql = strsql & " FROM " & istrTabela & "_total"
		strsql = strsql & " WHERE mesano = '" & VB6.Format(Month(DTPMesAno.Value), "00") & "/" & Year(DTPMesAno.Value) & "' " & "ORDER BY ordimp2 ASC, ordimp1 ASC "
		objatualizabancodados.QUERY_NAME = strsql
		dt = repositorioatualizabancodados.Consultar(objatualizabancodados)

		If dt.Rows.Count > 0 Then
			For Each row As DataRow In dt.Rows
				intCtrl = IIf(IsDBNull(row.Item("ordimp1")), 0, row.Item("ordimp1"))
				strFaixa = IIf(IsDBNull(row.Item("faixaxml")), "", row.Item("faixaxml"))
				If intCtrl = 1 Then
					PrintLine(aARQ, strTab & strTab & strTab & "<" & strFaixa & ">")
				End If
				strFase = IIf(IsDBNull(row.Item("fase")), "M", row.Item("fase"))
				strFase = IIf(strFase = "M", "monofasico", IIf(strFase = "B", "bifasico", "trifasico"))
				dblAux = IIf(IsDBNull(row.Item(aCampo)), 0, row.Item(aCampo))
				dblTotValr = dblTotValr + IIf(IsDBNull(row.Item("totvalr")), 0, row.Item("totvalr"))
				dblTotBXValr = dblTotBXValr + IIf(IsDBNull(row.Item("totbxvalr")), 0, row.Item("totbxvalr"))
				dblT4Valr = dblT4Valr + IIf(IsDBNull(row.Item("t4valr")), 0, row.Item("t4valr"))
				dblT4BXValr = dblT4BXValr + IIf(IsDBNull(row.Item("t4bxvalr")), 0, row.Item("t4bxvalr"))
				dblT1Valr = dblT1Valr + IIf(IsDBNull(row.Item("t1valr")), 0, row.Item("t1valr"))
				dblT1BXValr = dblT1BXValr + IIf(IsDBNull(row.Item("t1bxvalr")), 0, row.Item("t1bxvalr"))

				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
			Next
		End If
		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
		Try
			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 " & istrTabela & "_total"
			lstrSql = lstrSql & " WHERE `mesano` = '" & VB6.Format(Month(Me.DTPMesAno.Value), "00") & "/" & Year(Me.DTPMesAno.Value) & "' "
			lstrSql = lstrSql & "ORDER BY ordimp2 ASC, ordimp1 ASC "
			objatualizabancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizabancodados.Consultar(objatualizabancodados)


			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 row_c As DataRow In dt.Rows
					PrintLine(lintARQ, "MÊS/ANO:;" & IIf(IsDBNull(row_c.Item("mesano")), "", row_c.Item("mesano")) & ";")
					Exit For
				Next
				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
				'
				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
					llngRes_Qtd_T1 = CInt(IIf(IsDBNull(row.Item("t1qtde")), 0, row.Item("t1qtde"))) 'Residencial Normal
					llngBxR_Qtd_To = CInt(IIf(IsDBNull(row.Item("totbxqtde")), 0, row.Item("totbxqtde"))) 'Total Baixa Renda
					llngBxR_Qtd_T2 = CInt(IIf(IsDBNull(row.Item("t2qtde")), 0, row.Item("t2qtde"))) '246
					llngBxR_Qtd_T3 = CInt(IIf(IsDBNull(row.Item("t3qtde")), 0, row.Item("t3qtde"))) '485
					llngBxR_Qtd_T4 = CInt(IIf(IsDBNull(row.Item("t4qtde")), 0, row.Item("t4qtde"))) '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
					ldblRes_Qtd_T1 = CDbl(IIf(IsDBNull(row.Item("t1kwhc")), 0, row.Item("t1kwhc"))) 'Residencial Normal
					ldblBxR_Qtd_To = CDbl(IIf(IsDBNull(row.Item("totbxkhwc")), 0, row.Item("totbxkhwc"))) 'Total Baixa Renda
					ldblBxR_Qtd_T2 = CDbl(IIf(IsDBNull(row.Item("t2kwhc")), 0, row.Item("t2kwhc"))) '246
					ldblBxR_Qtd_T3 = CDbl(IIf(IsDBNull(row.Item("t3kwhc")), 0, row.Item("t3kwhc"))) '485
					ldblBxR_Qtd_T4 = CDbl(IIf(IsDBNull(row.Item("t4kwhc")), 0, row.Item("t4kwhc"))) '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
					ldblRes_Vlr_T1 = CDbl(IIf(IsDBNull(row.Item("t1valr")), 0, row.Item("t1valr"))) 'Residencial Normal
					ldblBxR_Vlr_T1 = CDbl(IIf(IsDBNull(row.Item("t1bxvalr")), 0, row.Item("t1bxvalr"))) 'Residencial Normal mas com tarifa baixa renda
					ldblBxR_Vlr_To = CDbl(IIf(IsDBNull(row.Item("totbxvalr")), 0, row.Item("totbxvalr"))) 'Total Baixa Renda
					ldblRes_Vlr_To = CDbl(IIf(IsDBNull(row.Item("totvalr")), 0, row.Item("totvalr"))) 'Total do t2 e t3 mas com tarifa resindencial normal
					ldblBxR_Vlr_T2 = CDbl(IIf(IsDBNull(row.Item("t2bxvalr")), 0, row.Item("t2bxvalr"))) '246
					ldblRes_Vlr_T2 = CDbl(IIf(IsDBNull(row.Item("t2valr")), 0, row.Item("t2valr"))) '246 com tarifa resindencial normal
					ldblBxR_Vlr_T3 = CDbl(IIf(IsDBNull(row.Item("t3bxvalr")), 0, row.Item("t3bxvalr"))) '485
					ldblRes_Vlr_T3 = CDbl(IIf(IsDBNull(row.Item("t3valr")), 0, row.Item("t3valr"))) '485 com tarifa resindencial normal
					ldblBxR_Vlr_T4 = CDbl(IIf(IsDBNull(row.Item("t4bxvalr")), 0, row.Item("t4bxvalr"))) 'antigo bx renda
					ldblRes_Vlr_T4 = CDbl(IIf(IsDBNull(row.Item("t4valr")), 0, row.Item("t4valr"))) '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

					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
				Next


				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

		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("rptPlanilhaBaixaRenda2")

		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 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
		Try
			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"
			objatualizabancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizabancodados.Consultar(objatualizabancodados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					lstrCod = Trim(IIf(IsDBNull(row.Item("num")), "", row.Item("num")))
					ldblLimFim = CDbl(IIf(IsDBNull(row.Item("limitkwh")), 0, row.Item("limitkwh")))
					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
				Next
			End If
			BaixaRenda = ldblTot
		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 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
		Try
			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"
			objatualizabancodados.QUERY_NAME = lstrSql
			dt = repositorioatualizabancodados.Consultar(objatualizabancodados)

			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(adteLeituraAnterior, adteLeituraAtual, lstrCod, 3) / 1000, 5)
					Residencial = adblkWh * ldblTar
				Next
			End If
		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()
		Me.txtDir.Text = istrCaminho
		Me.txtArquivo.Text = istrArquivo & VB6.Format(Month(DTPMesAno.Value), "00") & Year(DTPMesAno.Value) & "_V" & VB6.Format(txtVersao.Text, "00") & ".XML"
	End Sub

	Private Sub frmEmissaoBaixaRenda_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
		Dim lstrSql As String

		Try

			'Dropa a tabela
			lstrSql = "DROP TABLE IF EXISTS " & istrTabela
			objexecatualizabancodados.QUERY_NAME = lstrSql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)

			lstrSql = "DROP TABLE IF EXISTS " & istrTabela & "_total "
			objexecatualizabancodados.QUERY_NAME = lstrSql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)

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

	Private Sub LigDes(ByRef aModo As Boolean)
		Try
			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

		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 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 = "rptPlanilhaBaixaRenda2" 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("t1qtde", GetType(String))
				dt_2.Columns.Add("totbxqtde", GetType(String))
				dt_2.Columns.Add("t2qtde", GetType(String))
				dt_2.Columns.Add("t3qtde", GetType(String))
				dt_2.Columns.Add("t4qtde", GetType(String))
				dt_2.Columns.Add("reskwhc", GetType(String))
				dt_2.Columns.Add("t1kwhc", GetType(String))
				dt_2.Columns.Add("totbxkhwc", GetType(String))
				dt_2.Columns.Add("t2kwhc", GetType(String))
				dt_2.Columns.Add("t3kwhc", GetType(String))
				dt_2.Columns.Add("t4kwhc", GetType(String))
				dt_2.Columns.Add("resvalr", GetType(String))
				dt_2.Columns.Add("t1valr", GetType(String))
				dt_2.Columns.Add("t1bxvalr", GetType(String))
				dt_2.Columns.Add("totbxvalr", GetType(String))
				dt_2.Columns.Add("t2bxvalr", GetType(String))
				dt_2.Columns.Add("t3bxvalr", GetType(String))
				dt_2.Columns.Add("totvalr", GetType(String))
				dt_2.Columns.Add("t2valr", GetType(String))
				dt_2.Columns.Add("t3valr", GetType(String))
				dt_2.Columns.Add("t4bxvalr", GetType(String))
				dt_2.Columns.Add("t4valr", 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_baixa_renda_total " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
				objatualizabancodados.QUERY_NAME = lstrsql
				dt = repositorioatualizabancodados.Consultar(objatualizabancodados)

				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("t1qtde"),
										  dr_1("totbxqtde"),
										  dr_1("t2qtde"),
										  dr_1("t3qtde"),
										  dr_1("t4qtde"),
										  dr_1("reskwhc"),
										  dr_1("t1kwhc"),
										  dr_1("totbxkhwc"),
										  dr_1("t2kwhc"),
										  dr_1("t3kwhc"),
										  dr_1("t4kwhc"),
										  dr_1("resvalr"),
										  dr_1("t1valr"),
										  dr_1("t1bxvalr"),
										  dr_1("totbxvalr"),
										  dr_1("t2bxvalr"),
										  dr_1("t3bxvalr"),
										  dr_1("totvalr"),
										  dr_1("t2valr"),
										  dr_1("t3valr"),
										  dr_1("t4bxvalr"),
										  dr_1("t4valr"),
										  dr_1("mesano"),
										  dr_1("faixaxml"),
										  LImagem)
					Next dr_1
				End If

				ds.Tables.Add(dt)
				ds.Tables(0).TableName = "REL_PLANILHA_BAIXA_RENDA_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 rptPlanilhaBaixaRenda2

					rpt.SetDataSource(ds)
					'rpt.SetParameterValue("nomeempresa", 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 = "Relatório 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
	Private Sub sCriaTabelaAuxiliar()
		Try

			Dim lstrsql As String

			lstrsql = ""
			lstrsql = lstrsql & "CREATE TABLE " & istrTabela & " ( "
			lstrsql = lstrsql & "`fase`      varchar(6) default NULL, "
			lstrsql = lstrsql & "`kwhc`      varchar(6) default '0', "
			lstrsql = lstrsql & "`t1_kwhc`   varchar(6) default NULL, "
			lstrsql = lstrsql & "`t2_kwhc`   varchar(6) default NULL, "
			lstrsql = lstrsql & "`t3_kwhc`   varchar(6) default NULL, "
			lstrsql = lstrsql & "`t4_kwhc`   varchar(6) default NULL, "
			lstrsql = lstrsql & "`t1_valor`  varchar(6) default '0.00', "
			lstrsql = lstrsql & "`t1_bxvalor` varchar(6) default '0.00', "
			lstrsql = lstrsql & "`t2_bxvalor` varchar(6) default '0.00', "
			lstrsql = lstrsql & "`t3_bxvalor` varchar(6) default '0.00', "
			lstrsql = lstrsql & "`t2_valor`  varchar(6) default '0.00', "
			lstrsql = lstrsql & "`t3_valor`  varchar(6) default '0.00', "
			lstrsql = lstrsql & "`t4_bxvalor` varchar(6) default '0.00', "
			lstrsql = lstrsql & "`t4_valor` varchar(6) default '0.00', "
			lstrsql = lstrsql & "`zona`     varchar(6) default '', "
			lstrsql = lstrsql & "`livro`    varchar(6) default '', "
			lstrsql = lstrsql & "`conta`    varchar(6) default '', "
			lstrsql = lstrsql & "`codclas`  varchar(4) default '', "
			lstrsql = lstrsql & "`bx0402`   varchar(6)default '' )"
			objexecatualizabancodados.QUERY_NAME = lstrsql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)


			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 & "`t1qtde`   varchar(20), "
			lstrsql = lstrsql & "`totbxqtde` varchar(20), "
			lstrsql = lstrsql & "`t2qtde`   varchar(20), "
			lstrsql = lstrsql & "`t3qtde`   varchar(20), "
			lstrsql = lstrsql & "`t4qtde`   varchar(20), "
			'
			lstrsql = lstrsql & "`reskwhc`  varchar(20), "
			lstrsql = lstrsql & "`t1kwhc`   varchar(20), "
			lstrsql = lstrsql & "`totbxkhwc` varchar(20), "
			lstrsql = lstrsql & "`t2kwhc`   varchar(20), "
			lstrsql = lstrsql & "`t3kwhc`   varchar(20), "
			lstrsql = lstrsql & "`t4kwhc`   varchar(20), "
			'
			lstrsql = lstrsql & "`resvalr`  varchar(20), "
			lstrsql = lstrsql & "`t1valr`   varchar(20), "
			lstrsql = lstrsql & "`t1bxvalr` varchar(20), "
			lstrsql = lstrsql & "`totbxvalr` varchar(20), "
			lstrsql = lstrsql & "`t2bxvalr` varchar(20), "
			lstrsql = lstrsql & "`t3bxvalr` varchar(20), "
			lstrsql = lstrsql & "`totvalr`  varchar(20), "
			lstrsql = lstrsql & "`t2valr`   varchar(20), "
			lstrsql = lstrsql & "`t3valr`   varchar(20), "
			lstrsql = lstrsql & "`t4bxvalr` varchar(20), "
			lstrsql = lstrsql & "`t4valr`   varchar(20), "
			'
			lstrsql = lstrsql & "`mesano`   varchar(20), "
			lstrsql = lstrsql & "`faixaxml` varchar(50))"
			objexecatualizabancodados.QUERY_NAME = lstrsql
			repositorioexecatualizabancodados.Execute(objexecatualizabancodados)

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

End Class