Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class FrmRelEstatisticas
	Inherits System.Windows.Forms.Form
	
	Dim itens As System.Windows.Forms.ListViewItem
	Dim strgrupoG As String
	Dim strTabela As String 'Tabela com os dados de consumo, demanda,etc..
	Dim strTabItens As String 'Tabela dos Itens (Multa,Juro,Retencoes,etc..)
	Dim lngControleLinha As Integer
	Dim strCabec As String
	Dim strTabNf As String 'Tabela NF ou NFTRANSICAO
	Dim strBusca As String 'Função de Banco para buscar os itens
	Dim fr As New FrmRelatorio
	'UPGRADE_WARNING: Lower bound of array strCol was changed from 1,1 to 0,0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
	Dim strCol(4, 7) As String
	
	
	'UPGRADE_WARNING: Event cbRota1.SelectedIndexChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub cbRota1_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cbRota1.SelectedIndexChanged
		cbRota2.Text = cbRota1.Text
		cbRota2.Refresh()
	End Sub
	
	Private Sub FrmRelEstatisticas_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			Me.Close()
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub FrmRelEstatisticas_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		txtMesAno.MaxLength = 7
		txtMesAno.Text = LeConfig("MESANOFATURAMENTO")
		
		If Len(Trim(txtMesAno.Text)) < 1 Then
			txtMesAno.Text = VB6.Format(Today, "mm/yyyy")
		End If
		
		cbxOption.SelectedIndex = 0
		cbRota1.Enabled = False
		cbRota1.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
		cbRota2.Enabled = False
		cbRota2.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
		
		strTabNf = "nf "
		strBusca = "BuscaVlrItemFatura "
	End Sub
	
	'UPGRADE_WARNING: Event chkTrans.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkTrans_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTrans.CheckStateChanged
		If Me.chkTrans.CheckState Then
			strTabNf = "nftransicao "
			strBusca = "BuscaVlrItemFaturaTrans"
		Else
			strTabNf = "nf "
			strBusca = "BuscaVlrItemFatura"
		End If
	End Sub
	
	'UPGRADE_WARNING: Event chkRota.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkRota_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkRota.CheckStateChanged
		If chkRota.CheckState Then
			cbRota1.Enabled = True
			cbRota1.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
			cbRota2.Enabled = True
			cbRota2.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Call LigDesObj(False)
			Call encheRota()
			Call LigDesObj(True)
			Me.Cursor = System.Windows.Forms.Cursors.Default
		Else
			cbRota1.Enabled = False
			cbRota1.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
			cbRota2.Enabled = False
			cbRota2.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
			cbRota1.Items.Clear()
			cbRota2.Items.Clear()
		End If
	End Sub
	
	'UPGRADE_WARNING: Event cbxOption.SelectedIndexChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub cbxOption_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cbxOption.SelectedIndexChanged
		If cbxOption.SelectedIndex = 1 Then
			chkRota.Enabled = False
			chkRota.CheckState = System.Windows.Forms.CheckState.Unchecked
			cbRota1.Enabled = False
			cbRota1.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
			cbRota2.Enabled = False
			cbRota2.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
		Else
			chkRota.Enabled = True
		End If
	End Sub
	
	Private Sub FrmRelEstatisticas_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If CInt(Len(Trim(strTabela))) > 0 Then
			Call Executar("DROP TABLE IF EXISTS " & strTabela)
			Call Executar("DROP TABLE IF EXISTS " & strTabItens)
		End If
	End Sub
	
	Private Sub txtMesAno_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAno.Enter
		txtMesAno.SelectionStart = 0
		txtMesAno.SelectionLength = Len(txtMesAno.Text)
		
		If Len(Trim(txtMesAno.Text)) > 0 Then
			cmdEmitir.Enabled = True
		End If
	End Sub
	
	Private Sub txtMesAno_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAno.Leave
		txtMesAno.Text = Replace(txtMesAno.Text, "_", "0")
		
		If CDbl(VB.Left(txtMesAno.Text, 2)) < 1 Or CDbl(VB.Left(txtMesAno.Text, 2)) > 12 Then
			MsgBox("Mês inválido!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			txtMesAno.Focus()
			txtMesAno.SelectionStart = 0
			txtMesAno.SelectionLength = Len(txtMesAno.Text)
			System.Windows.Forms.Application.DoEvents()
			cmdEmitir.Enabled = False
		ElseIf VB.Right(txtMesAno.Text, 4) < "1951" Then 
			MsgBox("Ano inválido!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			txtMesAno.Focus()
			txtMesAno.SelectionStart = 0
			txtMesAno.SelectionLength = Len(txtMesAno.Text)
			System.Windows.Forms.Application.DoEvents()
			cmdEmitir.Enabled = False
		Else
			cmdEmitir.Enabled = True
		End If
	End Sub
	
	Private Sub txtMesAno_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtMesAno.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		ElseIf KeyAscii <> 8 And (KeyAscii <> 44 And (KeyAscii < 48 Or KeyAscii > 57)) Then 
			KeyAscii = 0
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Me.Close()
	End Sub
	
	Private Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
		Dim strsql As String
		
		If Val(cbRota1.Text) > Val(cbRota2.Text) Then
			MsgBox("A rota inicial é maior qua a rota final!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			cbRota1.Focus()
			Exit Sub
		End If
		lngControleLinha = 769
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call LigDesObj(False)
		lblInfo.Text = " Montando dados para exibição..."
		lblInfo.Refresh()
		
		'Define o nome das tabelas para o relatorio
		strTabela = "temp_estatis_" & LCase(gstrNmEstacao)
		strTabItens = strTabela & "_itens"
		
		'Dropa a tabela temporaria caso exista
		'strSql = "DROP TEMPORARY TABLE IF EXISTS " & strTabela
		'Call executar(strSql)
		strsql = "DROP TABLE IF EXISTS " & strTabela
		Call Executar(strsql)
		
		'Dropa a tabela temporaria de itens caso exista
		'strSql = "DROP TEMPORARY TABLE IF EXISTS " & strTabItens
		'Call executar(strSql)
		strsql = "DROP TABLE IF EXISTS " & strTabItens
		Call Executar(strsql)
		
		If MontaTabela = True Then
			lblInfo.Text = " Montando Relatório..."
			lblInfo.Refresh()
			
			fr.quem_chama = Me
			
			fr.VS.Font = VB6.FontChangeName(fr.VS.Font, "Arial")
			fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 5)
			fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			fr.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
			fr.VS.Orientation = VSVIEW6.OrientationSettings.orPortrait
			fr.VS.PhysicalPage = True
			fr.VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
			fr.VS.MarginTop = "7mm"
			fr.VS.MarginRight = "7mm"
			fr.VS.MarginLeft = "7mm"
			fr.VS.MarginBottom = "5mm"
			fr.VS.TextColor = System.Drawing.Color.Black
			fr.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
			fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
			fr.VS.StartDoc()
			
			strCabec = " "
			If cbxOption.SelectedIndex = 0 Then
				If chkGrupoA.CheckState Then
					Call CabecaRel("A")
					Call RelEstat_Operacional("A")
					
					If fr.VS.CurrentLine > 769 Then
						fr.VS.NewPage()
						fr.VS.CurrentLine = 1
						Call CabecaRel("A")
					End If
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					Call CabecaRel("(A)")
					Call ImprimeDetalhes("A")
				End If
				
				If chkGrupoA.CheckState And chkGrupoB.CheckState Then
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
				End If
				
				If chkGrupoB.CheckState Then
					Call CabecaRel("B")
					Call RelEstat_Operacional("B")
					
					If fr.VS.CurrentLine > 769 Then
						fr.VS.NewPage()
						fr.VS.CurrentLine = 1
						Call CabecaRel("B")
					End If
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					Call CabecaRel("(B)")
					Call ImprimeDetalhes("B")
				End If
				
				If chkGrupoA.CheckState And chkGrupoB.CheckState Then
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					
					Call CabecaRel("A+B")
					Call RelEstat_Operacional("AB")
					
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					
					Call CabecaRel("A+B")
					Call ImprimeDetalhes("AB")
				End If
			Else
				'Por fases
				If chkGrupoA.CheckState Then
					Call CabecaRel("A")
					Call RelEstat_Classe("A")
					
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					
					Call CabecaRel("A")
					Call ImprimeDetalhes("A")
				End If
				
				If chkGrupoA.CheckState And chkGrupoB.CheckState Then
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
				End If
				
				If chkGrupoB.CheckState Then
					Call CabecaRel("B")
					Call RelEstat_Classe("B")
					
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					
					Call CabecaRel("B")
					Call ImprimeDetalhes("B")
				End If
				
				If chkGrupoA.CheckState Or chkGrupoB.CheckState Then
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					
					Call CabecaRel("A+B")
					Call RelEstat_Classe("AB")
					
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					
					Call CabecaRel("A+B")
					Call ImprimeDetalhes("AB")
				End If
			End If
			
			fr.VS.EndDoc()
			fr.Show()
			
			'Dropa a tabela temporaria caso exista
			'strSql = "DROP TEMPORARY TABLE IF EXISTS " & strTabela
			'Call executar(strSql)
			strsql = "DROP TABLE IF EXISTS " & strTabela
			Call Executar(strsql)
			
			'Dropa a tabela temporaria de itens caso exista
			'strSql = "DROP TEMPORARY TABLE IF EXISTS " & strTabItens
			'Call executar(strSql)
			strsql = "DROP TABLE IF EXISTS " & strTabItens
			Call Executar(strsql)
			
			lblInfo.Text = " Pronto!"
			lblInfo.Refresh()
		Else
			lblInfo.Text = ""
			lblInfo.Refresh()
		End If
		Call LigDesObj(True)
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Function MontaTabela() As Boolean
		Dim strsql As String
		Dim strSql2 As String
		Dim strClasse As String
		Dim strBusca As String
		Dim strGrupo As String
		Dim intConta As Integer
		Dim bInclui As Boolean
		Dim strZona As String
		Dim dblQKwhCons As Double 'Qtde Consumo lido
		Dim dblQKwhFat As Double 'Qtde Consumo faturado
		Dim dblQKwFat As Double 'Qtde Demanda
		Dim dblVKwhCons As Double 'Valor Consumo lido
		Dim dblVKwhFat As Double 'Valor Consumo faturado
		Dim dblVKwFat As Double 'Valor Demanda
		Dim dblQKwhFatp As Double 'Qtde Consumo Faturado p
		Dim dblQKwhFatfp As Double 'Qtde Consumo Faturado fp
		Dim dblQKwFatp As Double 'Qtde Demanda p
		Dim dblQKwFatfp As Double 'Qtde Demanda fp
		Dim dblQKwFatUp As Double 'Qtde Demanda Ultrapassagem p
		Dim dblQKwFatUfp As Double 'Qtde Demanda Ultrapassagem fp
		Dim dblVKwhFatp As Double 'Valor Consumo faturado p
		Dim dblVKwhFatfp As Double 'Valor Consumo faturado fp
		Dim dblVKwFatp As Double 'Valor Demanda p
		Dim dblVKwFatfp As Double 'Valor Demanda fp
		Dim dblVKwFatUp As Double 'Valor Demanda Ultrapassagem p
		Dim dblVKwFatUfp As Double 'Valor Demanda Ultrapassagem fp
		Dim dblTip As Double 'valor de iluminação publica.
		Dim dblIcms As Double 'valor de icms.
		Dim dblPis As Double 'valor de PIS.
		Dim dblCofins As Double 'valor de COFINS.
		Dim dblIcmsPisCof As Double 'valor de icms/pis/cofins.
		Dim dblsaldoarre As Double
		Dim dblDifEltrbr As Double
		Dim dblJuros As Double 'juros verificar ainda com o calculo.
		Dim dblEncargo As Double 'encargos confirma ainda com o calculo
		Dim dblFtPot As Double 'fator de potencia confirma ainda com o calculo
		Dim dblCR As Double 'Consumo Reativo (Convencional)
		Dim dblDR As Double 'Demanda Reativo (Convencional)
		Dim dblCRp As Double 'Consumo Reativo na ponta (Horosazonal)
		Dim dblCRfp As Double 'Consumo Reativo fora da ponta (Horosazonal)
		Dim dblDRp As Double 'Demanda Reativo na ponta (Horosazonal)
		Dim dblDRfp As Double 'Demanda Reativo fora da ponta (Horosazonal)
		Dim dblDiversos As Double
		Dim dblTaxados As Double
		Dim dblConvenios As Double
		Dim dblDescontos As Double
		Dim dblQCIPC As Double 'Quantidade de consumidores COM Iluminacao Publica
		Dim dblQCIPS As Double 'Quantidade de consumidores SEM Iluminacao Publica
		Dim dblRetencao As Double
		Dim rsFatura As New ADODB.Recordset
		
		strsql = "SELECT * FROM " & strTabNf
		
		strsql = strsql & "WHERE mesano = '" & Trim(txtMesAno.Text) & "' "
		strsql = strsql & "AND itensserv IS NOT NULL "
		
		If chkRota.CheckState = 1 Then
			strsql = strsql & " AND LEFT(rota,4) BETWEEN '" & cbRota1.Text & "' " & "AND '" & cbRota2.Text & "'"
		End If
		
		If chkGrupoA.CheckState And Not chkGrupoB.CheckState Then
			strsql = strsql & "AND MID(ta,3,1) = 'A'"
		ElseIf chkGrupoB.CheckState And Not chkGrupoA.CheckState Then 
			strsql = strsql & "AND MID(ta,3,1) = 'B'"
		End If
		
		Call Conectar(strsql, rsFatura)
		
		If rsFatura.RecordCount > 0 Then
			rsFatura.MoveLast()
			rsFatura.MoveFirst()
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, rsFatura.RecordCount)
			intConta = 0
			
			'Cria Tabela temporaria das faturas
			'strSql = "CREATE TEMPORARY TABLE " & strTabela & "("
			strsql = "CREATE TABLE " & strTabela & "("
			strsql = strsql & "classe       VARCHAR(6), " 'Classe
			
			'Convencional
			strsql = strsql & "qkwh_cons    DOUBLE, " 'Kwh CONSUMIDO (ENERGIA CONSUMIDA)
			strsql = strsql & "qkwh_fat     DOUBLE, " 'Kwh FATURADO
			strsql = strsql & "qkw_fat      DOUBLE, " 'Kw FATURADO
			strsql = strsql & "vkwh_cons    DOUBLE, " 'Kwh CONSUMIDO (ENERGIA CONSUMIDA)
			strsql = strsql & "vkwh_fat     DOUBLE, " 'Kwh FATURADO
			strsql = strsql & "vkw_fat      DOUBLE, " 'Kw FATURADO
			strsql = strsql & "vconreat     DOUBLE, " 'REATIVO
			strsql = strsql & "vdemreat     DOUBLE, " 'REATIVO
			
			'Horosazonal
			strsql = strsql & "qkwh_fat_p   DOUBLE, " 'Kwh FATURADO (ENERGIA FAT PONTA)
			strsql = strsql & "qkwh_fat_fp  DOUBLE, " 'Kwh FATURADO (ENERGIA FAT FORA PONTA)
			strsql = strsql & "qkw_fat_p    DOUBLE, " 'Kw FATURADO (DEMANDA FAT PONTA)
			strsql = strsql & "qkw_fat_fp   DOUBLE, " 'Kw FATURADO (DEMANDA FAT FORA PONTA)
			strsql = strsql & "qkw_fatu_p   DOUBLE, " 'Kw FATURADO ULTRAPASSAGEM P
			strsql = strsql & "qkw_fatu_fp  DOUBLE, " 'Kw FATURADO ULTRAPASSAGEM FP
			strsql = strsql & "vkwh_fat_p   DOUBLE, " 'Kwh FATURADO (ENERGIA FAT PONTA)
			strsql = strsql & "vkwh_fat_fp  DOUBLE, " 'Kwh FATURADO (ENERGIA FAT FORA PONTA)
			strsql = strsql & "vkw_fat_p    DOUBLE, " 'Kw FATURADO (DEMANDA FAT PONTA)
			strsql = strsql & "vkw_fat_fp   DOUBLE, " 'Kw FATURADO (DEMANDA FAT FORA PONTA)
			strsql = strsql & "vkw_fatu_p   DOUBLE, " 'Kw FATURADO ULTRAPASSAGEM P
			strsql = strsql & "vkw_fatu_fp  DOUBLE, " 'Kw FATURADO ULTRAPASSAGEM FP
			
			strsql = strsql & "vconreat_p   DOUBLE, " 'REATIVO
			strsql = strsql & "vconreat_fp  DOUBLE, " 'REATIVO
			strsql = strsql & "vdemreat_p   DOUBLE, " 'REATIVO
			strsql = strsql & "vdemreat_fp  DOUBLE, " 'REATIVO
			'------
			strsql = strsql & "tip          DOUBLE, " 'CIP R$
			strsql = strsql & "sld_arred    DOUBLE, " 'SALDO ARRED. R$
			strsql = strsql & "icms         DOUBLE, " 'ICMS R$
			strsql = strsql & "pis          DOUBLE, " 'PIS R$
			strsql = strsql & "cofins       DOUBLE, " 'COFINS R$
			strsql = strsql & "icmspiscof   DOUBLE, " 'ICMS/PIS/COFINS R$
			strsql = strsql & "dif_eltrbr   DOUBLE, " 'DIF.ELETR. R$
			strsql = strsql & "juros        DOUBLE, " 'JUROS R$
			strsql = strsql & "encargo      DOUBLE, " 'ENCARGO CAP EMERG R$
			strsql = strsql & "total        DOUBLE, " 'TOTAL R$
			strsql = strsql & "taxados      DOUBLE, "
			strsql = strsql & "diversos     DOUBLE, " 'DIVERSOS R$
			strsql = strsql & "convenios    DOUBLE, " 'CONVENIOS R$
			strsql = strsql & "descontos    DOUBLE, " 'DESCONTOS R$
			strsql = strsql & "fase         CHAR(1), "
			strsql = strsql & "grupo        CHAR(2), "
			strsql = strsql & "zona         VARCHAR(6), "
			strsql = strsql & "KEY `keyclasse`  (`classe`), "
			strsql = strsql & "KEY `keykhwcons` (`qkwh_cons`))"
			Call Executar(strsql)
			
			'Cria tabela temporaria dos itens da fatura
			strsql = "CREATE TABLE " & strTabItens & "("
			'strSql = "CREATE TEMPORARY TABLE " & strTabItens & "("
			strsql = strsql & "codigo VARCHAR(10), " & "nome   VARCHAR(100), " & "valor  DOUBLE, " & "grupo  CHAR(2), " & " KEY `keycod` (`codigo`))"
			Call Executar(strsql)
			
			While Not rsFatura.EOF
				intConta = intConta + 1
				lblInfo.Text = " Processando: " & intConta & " de " & rsFatura.RecordCount
				lblInfo.Refresh()
				strGrupo = Mid(rsFatura.Fields("ta").Value, 3, 2)
				
				'***Limpa as parada
				dblQKwhCons = 0 'Consumo lido
				dblQKwhFat = 0 'Consumo faturado
				dblQKwFat = 0 'Demanda
				'Valor
				dblVKwhCons = 0 'Consumo lido
				dblVKwhFat = 0 'Consumo faturado
				dblVKwFat = 0 'Demanda
				dblCR = 0 'Consumo Reativo
				dblDR = 0 'Demanda Reativo
				dblQKwhFatp = 0 'Consumo faturado p
				dblQKwhFatfp = 0 'Consumo faturado fp
				dblQKwFatp = 0 'Demanda p
				dblQKwFatfp = 0 'Demanda fp
				dblQKwhCons = 0
				dblVKwhCons = 0
				dblQKwFatUp = 0 'Demanda de ultrapassagem p
				dblQKwFatUfp = 0 'Demanda de ultrapassagem fp
				'Valor
				dblVKwhFatp = 0 'Consumo faturado p
				dblVKwhFatfp = 0 'Consumo faturado fp
				dblVKwFatp = 0 'Demanda p
				dblVKwFatfp = 0 'Demanda fp
				dblVKwFatUp = 0 'Demanda de ultrapassagem p
				dblVKwFatUfp = 0 'Demanda de ultrapassagem fp
				dblCRp = 0 'Consumo Reativo p
				dblCRfp = 0 'Consumo Reativo fp
				dblDRp = 0 'Demanda Reativo p
				dblDRfp = 0 'Demanda Reativo fp
				dblTip = 0 'Vlr Iluminação Pública.
				dblIcms = 0 'Vlr Icms.
				dblPis = 0 'Vlr PIS.
				dblCofins = 0 'Vlr COFINS.
				dblIcmsPisCof = 0 'Vlr Icms/Pis/Cofins.
				dblsaldoarre = 0
				dblDifEltrbr = 0
				dblJuros = 0 'Juros verificar ainda com o calculo.
				dblEncargo = 0 'Encargos confirma ainda com o calculo
				
				dblQCIPC = 0
				dblQCIPS = 0
				'************
				If strGrupo = "AS" Or strGrupo = "A4" Or VB.Left(strGrupo, 1) = "B" Then
					'Quantidade
					If VB.Left(strGrupo, 1) <> "B" Then
						dblQKwhCons = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010008") 'Consumo lido
						dblQKwhFat = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010009") 'Consumo faturado
						dblVKwhCons = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020008") 'Consumo lido
						dblVKwhFat = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020009") 'Consumo faturado
					Else
						dblQKwhCons = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "030201008") 'Consumo lido
						dblQKwhFat = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "030201009") 'Consumo faturado
						dblVKwhCons = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "030202008") 'Consumo lido
						dblVKwhFat = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "030202009") 'Consumo faturado
					End If
					dblQKwFatUfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010018") 'Demanda de ultrapassagem fp
					dblQKwFat = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010017") + dblQKwFatUfp 'Demanda
					'Valor
					dblVKwFatUfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020018") 'Demanda de ultrapassagem Convencional
					dblVKwFat = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020015") + dblVKwFatUfp 'Demanda
					If VB.Left(strGrupo, 1) = "B" Then
						dblCR = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020012") 'Consumo Reativo
					Else
						dblCR = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020012") 'Consumo Reativo
					End If
					dblDR = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020031") 'Demanda Reativo
				ElseIf strGrupo = "AA" Or strGrupo = "AV" Or strGrupo = "AL" Or strGrupo = "AP" Then 
					'Quantidade
					dblQKwhFatp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010010") 'Consumo faturado p
					dblQKwhFatfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010011") 'Consumo faturado fp
					
					'dblQKwFatp = BuscaVlrItem(rsFatura!itensserv, "0302010021")   'Demanda p
					'dblQKwFatfp = BuscaVlrItem(rsFatura!itensserv, "0302010025")  'Demanda fp
					
					dblQKwFatUp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010022") 'Demanda de ultrapassagem p
					dblQKwFatUfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010026") 'Demanda de ultrapassagem fp
					
					dblQKwFatp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010021") + dblQKwFatUp 'Demanda p
					dblQKwFatfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302010025") + dblQKwFatUfp 'Demanda fp
					
					dblQKwhCons = dblQKwhFatp + dblQKwhFatfp '+ dblQKwFatp + dblQKwFatfp
					dblVKwhCons = dblVKwhFatp + dblVKwhFatfp '+ dblVKwFatp + dblVKwFatfp
					
					'Valor
					dblVKwhFatp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020010") 'Consumo faturado p
					dblVKwhFatfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020011") 'Consumo faturado fp
					
					dblVKwFatUp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020022") 'Demanda de ultrapassagem p
					dblVKwFatUfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020026") 'Demanda de ultrapassagem fp
					
					dblVKwFatp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020021") + dblVKwFatUp 'Demanda p
					dblVKwFatfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020023") + dblVKwFatUfp '+ BuscaVlrItem(rsFatura!itensserv, "0302020072") 'Demanda fp
					
					dblCRp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020013") 'Consumo Reativo p
					dblCRfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020014") 'Consumo Reativo fp
					
					dblDRp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020027") 'Demanda Reativo p
					dblDRfp = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020028") 'Demanda Reativo fp
				End If
				
				dblTip = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0301000003") 'Vlr Iluminação Pública.
				dblIcms = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302030009") 'Vlr Icms.
				dblIcms = dblIcms + BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302030010") 'Vlr Icms.
				dblPis = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020029") 'Vlr PIS.
				dblCofins = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302020030") 'Vlr COFINS.
				dblIcmsPisCof = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302030012") 'Vlr Icms/Pis/Cofins.
				'dblDifEltrbr = BuscaVlrItem(rsFatura!itensserv, "0302990041")
				dblsaldoarre = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302990041")
				dblJuros = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302050002") 'Juros verificar ainda com o calculo.
				dblEncargo = BuscaVlrItem(rsFatura.Fields("itensserv").Value, "0302070002") 'Encargos confirma ainda com o calculo
				
				'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"'
				strClasse = IIf(IsDbNull(rsFatura.Fields("codclas").Value), "0000", rsFatura.Fields("codclas").Value)
				strZona = rsFatura.Fields("Zona").Value
				dblDiversos = 0
				dblTaxados = 0
				dblConvenios = 0
				dblRetencao = 0
				dblDescontos = 0
				If CDbl(VB.Left(strClasse, 1)) <> 8 Then
					'Busca valor de Taxas, Diversos e Convenios
					For IndCodServ = 1 To Len(Trim(rsFatura.Fields("itensserv").Value))
						Call BuscaCodItem(IndCodServ, rsFatura.Fields("itensserv").Value)
						
						bInclui = False
						
						If Mid(codserv, 1, 4) = "9040" Or Mid(codserv, 1, 4) = "9050" Then
							dblTaxados = dblTaxados + Val(VlrCodServ)
							bInclui = True
						End If
						
						If Mid(codserv, 1, 4) = "9080" Then
							dblDiversos = dblDiversos + Val(VlrCodServ)
							bInclui = True
						End If
						
						If Mid(codserv, 1, 4) = "9030" Then
							dblConvenios = dblConvenios + Val(VlrCodServ)
							bInclui = True
						End If
						
						If Mid(codserv, 1, 10) = "0302020032" Then
							bInclui = True
						End If
						
						If Mid(codserv, 1, 10) = "9070000088" Then
							dblRetencao = dblRetencao + Val(VlrCodServ)
							bInclui = True
						End If
						
						'Retencoes
						If Mid(codserv, 1, 4) = "9090" Then
							dblRetencao = dblRetencao + Val(VlrCodServ)
							bInclui = True
						End If
						
						If Mid(codserv, 1, 4) = "9070" And Mid(codserv, 1, 10) <> "9070000088" Then
							dblDescontos = dblDescontos + Val(VlrCodServ)
							bInclui = True
						End If
						
						If bInclui = True Then
							strsql = "INSERT INTO " & strTabItens & " (codigo, nome, valor, grupo) VALUES " & "('" & codserv & "','" & codTodescri(codserv) & "'" & "," & VlrCodServ & ",'" & Mid(rsFatura.Fields("ta").Value, 3, 1) & "')"
							Call Executar(strsql)
						End If
					Next IndCodServ
				End If
				strsql = "INSERT INTO " & strTabela & " (classe, " & "qkwh_cons, qkwh_fat, qkw_fat, " & "vkwh_cons, vkwh_fat, vkw_fat, " & "vconreat, vdemreat, " & "qkwh_fat_p, qkwh_fat_fp, qkw_fat_p, qkw_fat_fp, qkw_fatu_p, qkw_fatu_fp, " & "vkwh_fat_p, vkwh_fat_fp, vkw_fat_p, vkw_fat_fp, vkw_fatu_p, vkw_fatu_fp, " & "vconreat_p, vconreat_fp, vdemreat_p, vdemreat_fp, " & "tip, sld_arred, icms, pis, cofins, icmspiscof, " & "dif_eltrbr, juros, encargo, total, taxados, diversos, convenios, descontos, fase, " & "grupo, zona ) VALUES("
				
				strsql = strsql & "'" & strClasse & "', " 'classe
				strsql = strsql & "'" & Replace(CStr(dblQKwhCons), ",", ".") & "', " 'qkwh_cons
				strsql = strsql & "'" & Replace(CStr(dblQKwhFat), ",", ".") & "', " 'qkwh_fat
				strsql = strsql & "'" & Replace(CStr(dblQKwFat), ",", ".") & "', " 'qkw_fat
				strsql = strsql & "'" & Replace(CStr(dblVKwhCons), ",", ".") & "', " 'vkwh_cons
				strsql = strsql & "'" & Replace(CStr(dblVKwhFat), ",", ".") & "', " 'vkwh_fat
				strsql = strsql & "'" & Replace(CStr(dblVKwFat), ",", ".") & "', " 'vkw_fat
				strsql = strsql & "'" & Replace(CStr(dblCR), ",", ".") & "', " 'vconreat
				strsql = strsql & "'" & Replace(CStr(dblDR), ",", ".") & "', " 'vdemreat
				
				strsql = strsql & "'" & Replace(CStr(dblQKwhFatp), ",", ".") & "', " 'qkwh_fat_p
				strsql = strsql & "'" & Replace(CStr(dblQKwhFatfp), ",", ".") & "', " 'qkwh_fat_fp
				strsql = strsql & "'" & Replace(CStr(dblQKwFatp), ",", ".") & "', " 'qkw_fat_p
				strsql = strsql & "'" & Replace(CStr(dblQKwFatfp), ",", ".") & "', " 'qkw_fat_fp
				strsql = strsql & "'" & Replace(CStr(dblQKwFatUp), ",", ".") & "', " 'qkw_fatu_p
				strsql = strsql & "'" & Replace(CStr(dblQKwFatUfp), ",", ".") & "', " 'qkw_fatu_fp
				
				strsql = strsql & "'" & Replace(CStr(dblVKwhFatp), ",", ".") & "', " 'vkwh_fat_p
				strsql = strsql & "'" & Replace(CStr(dblVKwhFatfp), ",", ".") & "', " 'vkwh_fat_fp
				strsql = strsql & "'" & Replace(CStr(dblVKwFatp), ",", ".") & "', " 'vkw_fat_p
				strsql = strsql & "'" & Replace(CStr(dblVKwFatfp), ",", ".") & "', " 'vkw_fat_fp
				strsql = strsql & "'" & Replace(CStr(dblVKwFatUp), ",", ".") & "', " 'vkw_fatu_p
				strsql = strsql & "'" & Replace(CStr(dblVKwFatUfp), ",", ".") & "', " 'vkw_fatu_fp
				
				strsql = strsql & "'" & Replace(CStr(dblCRp), ",", ".") & "', " 'Consumo Reativo ponta
				strsql = strsql & "'" & Replace(CStr(dblCRfp), ",", ".") & "', " 'Consumo Reativo fora ponta
				strsql = strsql & "'" & Replace(CStr(dblDRp), ",", ".") & "', " 'Demanda Reativo ponta
				strsql = strsql & "'" & Replace(CStr(dblDRfp), ",", ".") & "', " 'Demanda Reativo fora ponta
				strsql = strsql & "'" & Replace(CStr(dblTip), ",", ".") & "', " 'tip
				strsql = strsql & "'" & Replace(CStr(dblsaldoarre), ",", ".") & "', " 'arrendodamento
				strsql = strsql & "'" & Replace(CStr(dblIcms), ",", ".") & "', " 'icms
				strsql = strsql & "'" & Replace(CStr(dblPis), ",", ".") & "', " 'pis
				strsql = strsql & "'" & Replace(CStr(dblCofins), ",", ".") & "', " 'cofins
				strsql = strsql & "'" & Replace(CStr(dblIcmsPisCof), ",", ".") & "', " 'icms/pis/cofins
				strsql = strsql & "'" & Replace(CStr(dblDifEltrbr), ",", ".") & "', " 'dif_eltrbr
				strsql = strsql & "'" & Replace(CStr(dblJuros), ",", ".") & "', " 'juros
				strsql = strsql & "'" & Replace(CStr(dblEncargo), ",", ".") & "', " 'encardo
				strsql = strsql & "'" & Replace(Trim(Str(rsFatura.Fields("ValorTot").Value)), ",", ".") & "', " 'total
				strsql = strsql & "'" & Replace(Trim(Str(dblTaxados)), ",", ".") & "', " 'taxados
				strsql = strsql & "'" & Replace(Trim(Str(dblDiversos)), ",", ".") & "', " 'diversos
				strsql = strsql & "'" & Replace(Trim(Str(dblConvenios)), ",", ".") & "', " 'convenios
				strsql = strsql & "'" & Replace(Trim(Str(dblDescontos)), ",", ".") & "', " 'decontos
				strsql = strsql & "'" & rsFatura.Fields("fase").Value & "', " 'fase
				strsql = strsql & "'" & IIf(Mid(rsFatura.Fields("ta").Value, 3, 1) = "A", Mid(rsFatura.Fields("ta").Value, 3, 2), Mid(rsFatura.Fields("ta").Value, 3, 1)) & "'," 'grupo
				strsql = strsql & "'" & rsFatura.Fields("Zona").Value & "'" 'fase
				strsql = strsql & ")"
				Call Executar(strsql)
				rsFatura.MoveNext()
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				System.Windows.Forms.Application.DoEvents()
			End While
			MontaTabela = True
			Call PegaFaixaBxRenda()
		Else
			MsgBox("Não existem faturas no período selecionado!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
			MontaTabela = False
		End If
		If rsFatura.State = 1 Then rsFatura.Close()
	End Function
	
	Private Sub PegaFaixaBxRenda()
		Dim strsql As String
		Dim rsTarifa As New ADODB.Recordset
		Dim strDataNova As String
		Dim lngIni As Integer
		Dim lngFim As Integer
		
		strsql = "SELECT num, descri, un, limitkwh, valor, datanova, flgcasc "
		strsql = strsql & "FROM `tarifa` "
		strsql = strsql & "WHERE LEFT(num,8) = '01B10211' "
		strsql = strsql & "AND LEFT(STR_TO_DATE(datanova,'%d/%m/%Y%'),7) <= '" & VB.Right(txtMesAno.Text, 4) & "-" & VB.Left(txtMesAno.Text, 2) & "' "
		strsql = strsql & "ORDER BY STR_TO_DATE(datanova,'%d/%m/%Y') DESC, num ASC "
		Call Conectar(strsql, rsTarifa)
		If rsTarifa.RecordCount > 0 Then
			lngIni = 0
			lngFim = 0
			While Not rsTarifa.EOF
				If Len(Trim(strDataNova)) < 1 Then
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					strDataNova = IIf(IsDbNull(rsTarifa.Fields("datanova").Value), "", rsTarifa.Fields("datanova").Value)
				End If
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				If strDataNova = IIf(IsDbNull(rsTarifa.Fields("datanova").Value), "", rsTarifa.Fields("datanova").Value) Then
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lngFim = IIf(IsDbNull(rsTarifa.Fields("limitkwh").Value), 0, rsTarifa.Fields("limitkwh").Value)
					MSFGridTarifa.Rows = MSFGridTarifa.Rows + 1
					MSFGridTarifa.set_TextMatrix(MSFGridTarifa.Rows - 1, 0, lngIni)
					MSFGridTarifa.set_TextMatrix(MSFGridTarifa.Rows - 1, 1, lngFim)
				End If
				If lngIni < lngFim Then
					lngIni = lngFim + 1
				End If
				rsTarifa.MoveNext()
			End While
		End If
		If rsTarifa.State = 1 Then rsTarifa.Close()
	End Sub
	
	Private Sub RelEstat_Operacional(ByRef astrGrupo As String) 'FATB73
		Dim strsql As String
		Dim strSql0 As String
		Dim strSql1 As String
		Dim strSql2 As String
		Dim strSql3 As String
		Dim strTexto As String
		Dim lngIni As Integer
		Dim lngFim As Integer
		Dim intLinha As Short
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		strgrupoG = astrGrupo
		strsql = "SELECT COUNT(*) ncons, " & "SUM(qkwh_cons) qkwh_cons, SUM(qkwh_fat) qkwh_fat, SUM(qkw_fat) qkw_fat, " & "SUM(vkwh_cons) vkwh_cons, SUM(vkwh_fat) vkwh_fat, SUM(vkw_fat) vkw_fat, " & "SUM(vconreat) vconreat, SUM(vdemreat) vdemreat, " & "SUM(qkwh_fat_p) qkwh_fat_p, SUM(qkwh_fat_fp) qkwh_fat_fp, " & "SUM(qkw_fat_p) qkw_fat_p, SUM(qkw_fat_fp) qkw_fat_fp, " & "SUM(qkw_fatu_p) qkw_fatu_p, SUM(qkw_fatu_fp) qkw_fatu_fp, " & "SUM(vkwh_fat_p) vkwh_fat_p, SUM(vkwh_fat_fp) vkwh_fat_fp, " & "SUM(vkw_fat_p) vkw_fat_p, SUM(vkw_fat_fp) vkw_fat_fp, " & "SUM(vkw_fatu_p) vkw_fatu_p, SUM(vkw_fatu_fp) vkw_fatu_fp, " & "SUM(vconreat_p) vconreat_p, SUM(vconreat_fp) vconreat_fp, " & "SUM(vdemreat_p) vdemreat_p, SUM(vdemreat_fp) vdemreat_fp, " & "SUM(tip) tip, SUM(sld_arred) saldo, SUM(icms) icms, " & "SUM(pis) pis, SUM(cofins) cofins, SUM(icmspiscof) icmspiscof, " & "SUM(dif_eltrbr) dif_ele, SUM(juros) juros, SUM(encargo) encargo, " & "SUM(convenios) convenios, sum(descontos) descontos, SUM(diversos) diversos, SUM(taxados) taxados, " & "SUM(total) total "
		strSql0 = ", IF(LEFT(grupo,1)='B','B',grupo) grupo "
		strSql1 = "FROM " & strTabela & " WHERE "
		If astrGrupo = "AB" Then
			'------------------------------------
			strSql3 = "GROUP BY grupo ASC "
			'--------------------------------------
			strTexto = "UPDATE " & strTabela & " SET grupo ='B' WHERE grupo = 'A4'"
			Call Executar(strTexto)
			strTexto = ""
		End If
		If astrGrupo <> "AB" Then
			strSql1 = strSql1 & "LEFT(grupo,1) = '" & VB.Left(astrGrupo, 1) & "' AND "
		End If
		If astrGrupo = "A" Then
			strSql3 = "GROUP BY grupo ASC "
		ElseIf astrGrupo = "B" Then 
			strSql3 = "GROUP BY LEFT(grupo,1) ASC "
		End If
		If astrGrupo = "B" Or astrGrupo = "AB" Then
			For intLinha = 0 To MSFGridTarifa.Rows - 1
				lngIni = CInt(MSFGridTarifa.get_TextMatrix(intLinha, 0))
				lngFim = CInt(MSFGridTarifa.get_TextMatrix(intLinha, 1))
				'RESIDENCIAL BX. RENDA 9104 = 246
				strSql2 = "LEFT(classe,1) = '9' AND classe = '9104' " & "AND qkwh_fat BETWEEN " & lngIni & " AND " & lngFim & " "
				Call Conectar(strsql & ", classe " & strSql1 & strSql2 & strSql3 & ", classe ASC ", lrstAux)
				If lrstAux.RecordCount > 0 Then
					If lrstAux.Fields("ncons").Value > 0 Then
						If lngIni > 0 And lngFim > 999 Then
							strTexto = "(" & lngIni & "- ACIMA) (246)"
						Else
							strTexto = "(" & lngIni & "-" & lngFim & ") (246)"
						End If
						Call Imprime(lrstAux, "RESIDENCIAL BX. RENDA " & strTexto, astrGrupo,  , "D")
					End If
				End If
				
				lrstAux.Close()
			Next intLinha
			
			'TOTAL RESIDENCIAL BX. RENDA 9104
			strSql2 = "LEFT(classe,1) = '9' AND classe = '9104'  " & "AND qkwh_fat BETWEEN 0 AND " & lngFim & " "
			Call Conectar(strsql & strSql1 & strSql2 & strSql3, lrstAux)
			If lrstAux.RecordCount > 0 Then
				If lrstAux.Fields("ncons").Value > 0 Then
					Call Imprime(lrstAux, "TOTAL RESIDENCIAL BX. RENDA (246)", astrGrupo,  , "T")
				End If
			End If
			lrstAux.Close()
			
			For intLinha = 0 To MSFGridTarifa.Rows - 1
				lngIni = CInt(MSFGridTarifa.get_TextMatrix(intLinha, 0))
				lngFim = CInt(MSFGridTarifa.get_TextMatrix(intLinha, 1))
				'RESIDENCIAL BX. RENDA 9105 = 485
				strSql2 = "LEFT(classe,1) = '9' AND classe = '9105' " & "AND qkwh_fat BETWEEN " & lngIni & " AND " & lngFim & " "
				
				Call Conectar(strsql & ", classe " & strSql1 & strSql2 & strSql3 & ", classe ASC ", lrstAux)
				
				If lrstAux.RecordCount > 0 Then
					If lrstAux.Fields("ncons").Value > 0 Then
						If lngIni > 0 And lngFim = 999 Then
							strTexto = "(" & lngIni & "- ACIMA) (485)"
						Else
							strTexto = "(" & lngIni & "-" & lngFim & ") (485)"
						End If
						Call Imprime(lrstAux, "RESIDENCIAL BX. RENDA " & strTexto, astrGrupo,  , "D")
					End If
				End If
				
				lrstAux.Close()
			Next intLinha
			
			'TOTAL RESIDENCIAL BX. RENDA 9105
			strSql2 = "LEFT(classe,1) = '9' AND classe = '9105'  " & "AND qkwh_fat BETWEEN 0 AND " & lngFim & " "
			Call Conectar(strsql & strSql1 & strSql2 & strSql3, lrstAux)
			If lrstAux.RecordCount > 0 Then
				If lrstAux.Fields("ncons").Value > 0 Then
					Call Imprime(lrstAux, "TOTAL RESIDENCIAL BX. RENDA (485)", astrGrupo,  , "T")
				End If
			End If
			lrstAux.Close()
			
			For intLinha = 0 To MSFGridTarifa.Rows - 1
				lngIni = CInt(MSFGridTarifa.get_TextMatrix(intLinha, 0))
				lngFim = CInt(MSFGridTarifa.get_TextMatrix(intLinha, 1))
				'RESIDENCIAL BX. RENDA 9106 = 694
				strSql2 = "LEFT(classe,1) = '9' AND classe = '9106' " & "AND qkwh_fat BETWEEN " & lngIni & " AND " & lngFim & " "
				
				Call Conectar(strsql & ", classe " & strSql1 & strSql2 & strSql3 & ", classe ASC ", lrstAux)
				
				If lrstAux.RecordCount > 0 Then
					If lrstAux.Fields("ncons").Value > 0 Then
						If lngIni > 0 And lngFim = 999 Then
							strTexto = "(" & lngIni & "- ACIMA) (694)"
						Else
							strTexto = "(" & lngIni & "-" & lngFim & ") (694)"
						End If
						Call Imprime(lrstAux, "RESIDENCIAL BX. RENDA " & strTexto, astrGrupo,  , "D")
					End If
				End If
				
				lrstAux.Close()
			Next intLinha
			
			'TOTAL RESIDENCIAL BX. RENDA 9106
			strSql2 = "LEFT(classe,1) = '9' AND classe = '9106'  " & "AND qkwh_fat BETWEEN 0 AND " & lngFim & " "
			Call Conectar(strsql & strSql1 & strSql2 & strSql3, lrstAux)
			If lrstAux.RecordCount > 0 Then
				If lrstAux.Fields("ncons").Value > 0 Then
					Call Imprime(lrstAux, "TOTAL RESIDENCIAL BX. RENDA (694)", astrGrupo,  , "T")
				End If
			End If
			lrstAux.Close()
			
			'TOTAL RESIDENCIAL BX. RENDA
			strSql2 = "LEFT(classe,1) = '9' AND classe <> '9101'  " & "AND qkwh_fat BETWEEN 0 AND " & lngFim & " "
			Call Conectar(strsql & strSql1 & strSql2 & strSql3, lrstAux)
			If lrstAux.RecordCount > 0 Then
				If lrstAux.Fields("ncons").Value > 0 Then
					Call Imprime(lrstAux, "TOTAL RESIDENCIAL BX. RENDA ", astrGrupo,  , "T")
				End If
			End If
			lrstAux.Close()
		End If
		
		'RESIDENCIAL NORMAL (9101)
		
		strSql2 = "classe = 9101 "
		Call Conectar(strsql & strSql1 & strSql2 & strSql3, lrstAux)
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "RESIDENCIAL NORMAL (9101) ", astrGrupo,  , "D")
			End If
		End If
		lrstAux.Close()
		
		'TOTAL RESIDENCIAIS
		strSql2 = "LEFT(classe,1) = '9' "
		
		Call Conectar(strsql & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL RESIDENCIAL", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'INDUSTRIAL (1,2,3)
		strSql2 = "classe BETWEEN 0 AND 3999 AND classe <> '0100' "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				While Not lrstAux.EOF
					Call Imprime(lrstAux, "INDUSTRIAL (0,1,2,3) ", lrstAux.Fields("grupo").Value,  , "D")
					lrstAux.MoveNext()
				End While
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL INDUSTRIAL (1,2,3)
		strSql2 = "classe BETWEEN 0 AND 3999 AND classe <> '0100' "
		
		Call Conectar(strsql & strSql1 & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL INDUSTRIAL (0,1,2,3) ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'CONSUMIDOR LIVRE (100)
		strSql2 = "classe = '0100' "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL CONSUMIDOR LIVRE (100) ", lrstAux.Fields("grupo").Value,  , "D")
			End If
		End If
		
		lrstAux.Close()
		
		'COMERCIAL (4)
		strSql2 = "classe BETWEEN 4000 AND 4999 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				While Not lrstAux.EOF
					Call Imprime(lrstAux, "COMERCIAL (4) ", lrstAux.Fields("grupo").Value,  , "D")
					lrstAux.MoveNext()
				End While
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL COMERCIAL (4)
		strSql2 = "classe BETWEEN 4000 AND 4999 "
		
		Call Conectar(strsql & strSql1 & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL COMERCIAL (4) ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'RURAL (5)
		strSql2 = "classe BETWEEN 5000 AND 5999 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				While Not lrstAux.EOF
					Call Imprime(lrstAux, "RURAL (5) ", lrstAux.Fields("grupo").Value,  , "D")
					lrstAux.MoveNext()
				End While
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL RURAL (5)
		strSql2 = "classe BETWEEN 5000 AND 5999 "
		
		Call Conectar(strsql & strSql1 & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL RURAL (5) ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'PODER PUBLICO MUNICIPAL (61)
		strSql2 = "classe BETWEEN 6100 AND 6199 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				While Not lrstAux.EOF
					Call Imprime(lrstAux, "PODER PÚBLICO MUNICIPAL (61) ", lrstAux.Fields("grupo").Value,  , "D")
					lrstAux.MoveNext()
				End While
			End If
		End If
		
		lrstAux.Close()
		
		'PODER PUBLICO ESTADUAL (62)
		strSql2 = "classe BETWEEN 6200 AND 6299 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				While Not lrstAux.EOF
					Call Imprime(lrstAux, "PODER PÚBLICO ESTADUAL (62) ", lrstAux.Fields("grupo").Value,  , "D")
					lrstAux.MoveNext()
				End While
			End If
		End If
		
		lrstAux.Close()
		
		'PODER PUBLICO FEDERAL (63)
		strSql2 = "classe BETWEEN 6300 AND 6399 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				While Not lrstAux.EOF
					Call Imprime(lrstAux, "PODER PÚBLICO FEDERAL (63) ", lrstAux.Fields("grupo").Value,  , "D")
					lrstAux.MoveNext()
				End While
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL PODER PUBLICO (61,62,63)
		strSql2 = "classe BETWEEN 6100 AND 6399 "
		
		Call Conectar(strsql & strSql1 & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL PODER PÚBLICO (61,62,63) ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL ILUMINAÇÃO PUBLICA (64)
		strSql2 = "classe BETWEEN 6400 AND 6499"
		
		Call Conectar(strsql & strSql1 & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL ILUMINAÇÃO PÚBLICA (64) ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'SERVIÇO PUBLICO (7)
		strSql2 = "classe BETWEEN 7000 AND 7999 AND LEFT(classe,2) <> '72' "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "SERVIÇO PÚBLICO (7) ", lrstAux.Fields("grupo").Value,  , "D")
			End If
		End If
		
		lrstAux.Close()
		
		'AGUA, ESGOTO E SANEAMENTO(72)
		strSql2 = "classe BETWEEN 7200 AND 7299 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "ÁGUA, ESGOTO E SANEAMENTO (7) ", lrstAux.Fields("grupo").Value,  , "D")
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL SERVIÇO PUBLICO (7)
		strSql2 = "classe BETWEEN 7000 AND 7999 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL SERVIÇO PÚBLICO (7) ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL CONSUMO PROPRIO (8)
		strSql2 = "classe BETWEEN 8000 AND 8999 "
		
		Call Conectar(strsql & strSql0 & strSql1 & strSql2 & strSql3, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL CONSUMO PRÓPRIO (8) ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
		
		'TOTAL GERAL
		strSql2 = "1 = 1 "
		If astrGrupo = "AB" Then
			strSql1 = "FROM " & strTabela & ""
			strSql2 = ""
		End If
		Call Conectar(strsql & strSql1 & strSql2, lrstAux)
		If lrstAux.RecordCount > 0 Then
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL GERAL ", astrGrupo,  , "T")
			End If
		End If
		
		lrstAux.Close()
	End Sub
	
	Private Sub ImprimeDetalhes(ByRef astrGrupo As String)
		'############## DIVERSOS, TAXADOS, CONVENIOS ####################
		Dim dblTot As Double
		Dim strsql As String
		Dim strSql2 As String
		Dim strMargem As String
		Dim strNovaMar As String
		Dim lngMargem As Integer
		Dim strTemp As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		'UPGRADE_WARNING: Couldn't resolve default property of object fr.VS.MarginLeft. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		strMargem = fr.VS.MarginLeft
		strNovaMar = "40mm"
		fr.VS.MarginLeft = strNovaMar
		fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
		
		strsql = "SELECT codigo, nome, SUM(valor) valor FROM " & strTabItens & " WHERE "
		
		If astrGrupo <> "AB" Then
			strsql = strsql & "LEFT(grupo,1) = '" & VB.Left(astrGrupo, 1) & "' AND "
		End If
		
		'DIVERSOS
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 300
		
		dblTot = 0
		strSql2 = "MID(codigo,1,4) = 9080 AND codigo<>9080000098 GROUP BY codigo ORDER BY codigo ASC"
		
		Call Conectar(strsql & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			fr.VS.Table = "^110mm;DETALHAMENTO DOS DIVERSOS"
			
			While Not lrstAux.EOF
				fr.VS.Table = "<20mm|<60mm|>30mm;" & lrstAux.Fields("codigo").Value & "|" & lrstAux.Fields("nome").Value & "|" & FormatNumber(lrstAux.Fields("valor").Value, 2)
				dblTot = dblTot + lrstAux.Fields("valor").Value
				lrstAux.MoveNext()
				System.Windows.Forms.Application.DoEvents()
				
				If fr.VS.CurrentLine > lngControleLinha Then
					fr.VS.MarginLeft = strMargem
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					Call CabecaRel("(" & astrGrupo & ")" & strCabec)
					fr.VS.MarginLeft = strNovaMar
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
					fr.VS.Table = "^110mm;DETALHAMENTO DOS DIVERSOS CONTINUAÇÃO"
				End If
			End While
		End If
		
		fr.VS.Table = "<80mm|>30mm;" & "TOTAL DIVERSOS:|" & FormatNumber(dblTot, 2)
		dblTot = 0
		lrstAux.Close()
		
		'TAXADOS
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 300
		dblTot = 0
		strSql2 = "MID(codigo,1,4) IN ('9040','9050') GROUP BY codigo ORDER BY codigo ASC"
		
		Call Conectar(strsql & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			fr.VS.Table = "^110mm;DETALHAMENTO DOS TAXADOS"
			
			While Not lrstAux.EOF
				fr.VS.Table = "<20mm|<60mm|>30mm;" & lrstAux.Fields("codigo").Value & "|" & lrstAux.Fields("nome").Value & "|" & FormatNumber(lrstAux.Fields("valor").Value, 2)
				dblTot = dblTot + lrstAux.Fields("valor").Value
				lrstAux.MoveNext()
				System.Windows.Forms.Application.DoEvents()
				If fr.VS.CurrentLine > lngControleLinha Then
					fr.VS.MarginLeft = strMargem
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					Call CabecaRel("(" & astrGrupo & ")" & strCabec)
					fr.VS.MarginLeft = strNovaMar
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
					fr.VS.Table = "^110mm;DETALHAMENTO DOS TAXADOS CONTINUAÇÃO"
				End If
			End While
		End If
		
		fr.VS.Table = "<80mm|>30mm;" & "TOTAL TAXADOS:|" & FormatNumber(dblTot, 2)
		dblTot = 0
		lrstAux.Close()
		
		'CONVENIOS
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 300
		dblTot = 0
		strSql2 = "MID(codigo,1,4) = 9030 GROUP BY codigo ORDER BY codigo ASC"
		
		Call Conectar(strsql & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			fr.VS.Table = "^110mm;DETALHAMENTO DOS CONVÊNIOS"
			
			While Not lrstAux.EOF
				fr.VS.Table = "<20mm|<60mm|>30mm;" & lrstAux.Fields("codigo").Value & "|" & lrstAux.Fields("nome").Value & "|" & FormatNumber(lrstAux.Fields("valor").Value, 2)
				dblTot = dblTot + lrstAux.Fields("valor").Value
				lrstAux.MoveNext()
				System.Windows.Forms.Application.DoEvents()
				If fr.VS.CurrentLine > lngControleLinha Then
					fr.VS.MarginLeft = strMargem
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					Call CabecaRel("(" & astrGrupo & ")" & strCabec)
					fr.VS.MarginLeft = strNovaMar
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
					fr.VS.Table = "^110mm;DETALHAMENTO DOS CONVÊNIOS CONTINUAÇÃO"
				End If
			End While
		End If
		
		fr.VS.Table = "<80mm|>30mm;" & "TOTAL CONVENIOS:|" & FormatNumber(dblTot, 2)
		dblTot = 0
		lrstAux.Close()
		
		'RETENCOES
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 300
		
		dblTot = 0
		strSql2 = "(MID(codigo,1,4) = 9090 OR MID(codigo, 1, 10) = '9070000088') GROUP BY codigo ORDER BY codigo ASC"
		
		Call Conectar(strsql & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			fr.VS.Table = "^110mm;DETALHAMENTO DAS RETENÇÕES"
			
			While Not lrstAux.EOF
				strTemp = "<20mm|<60mm|>30mm;" & lrstAux.Fields("codigo").Value & "|" & lrstAux.Fields("nome").Value & "|"
				If CDbl(VB.Left(lrstAux.Fields("codigo").Value, 4)) = 9070 Or CDbl(VB.Left(lrstAux.Fields("codigo").Value, 4)) = 9090 Then
					strTemp = strTemp & FormatNumber(lrstAux.Fields("valor").Value * -1, 2)
					dblTot = dblTot - lrstAux.Fields("valor").Value
				Else
					strTemp = strTemp & FormatNumber(lrstAux.Fields("valor").Value * 1, 2)
					dblTot = dblTot + lrstAux.Fields("valor").Value
				End If
				fr.VS.Table = strTemp
				lrstAux.MoveNext()
				System.Windows.Forms.Application.DoEvents()
				If fr.VS.CurrentLine > lngControleLinha Then
					fr.VS.MarginLeft = strMargem
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					Call CabecaRel("(" & astrGrupo & ")" & strCabec)
					fr.VS.MarginLeft = strNovaMar
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
					fr.VS.Table = "^110mm;DETALHAMENTO DAS RETENÇÕES CONTINUAÇÃO"
				End If
			End While
		End If
		
		fr.VS.Table = "<80mm|>30mm;" & "TOTAL RETENÇÕES:|" & FormatNumber(dblTot, 2)
		dblTot = 0
		lrstAux.Close()
		
		'OUTROS
		fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 300
		
		dblTot = 0
		strSql2 = "(MID(codigo,1,4) = 9070 AND MID(codigo, 1, 10) <> '9070000088'  OR codigo=9080000098 OR codigo=0302020032) GROUP BY codigo ORDER BY codigo ASC"
		
		Call Conectar(strsql & strSql2, lrstAux)
		
		If lrstAux.RecordCount > 0 Then
			fr.VS.Table = "^110mm;DETALHAMENTO OUTROS(DIVERSOS)"
			
			While Not lrstAux.EOF
				strTemp = "<20mm|<60mm|>30mm;" & lrstAux.Fields("codigo").Value & "|" & lrstAux.Fields("nome").Value & "|"
				If CDbl(VB.Left(lrstAux.Fields("codigo").Value, 4)) = 9070 Or CDbl(VB.Left(lrstAux.Fields("codigo").Value, 4)) = 9090 Then
					strTemp = strTemp & FormatNumber(lrstAux.Fields("valor").Value * -1, 2)
					dblTot = dblTot - lrstAux.Fields("valor").Value
				Else
					strTemp = strTemp & FormatNumber(lrstAux.Fields("valor").Value * 1, 2)
					dblTot = dblTot + lrstAux.Fields("valor").Value
				End If
				fr.VS.Table = strTemp
				lrstAux.MoveNext()
				System.Windows.Forms.Application.DoEvents()
				If fr.VS.CurrentLine > lngControleLinha Then
					fr.VS.MarginLeft = strMargem
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
					Call CabecaRel("(" & astrGrupo & ")" & strCabec)
					fr.VS.MarginLeft = strNovaMar
					fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
					fr.VS.Table = "^110mm;DETALHAMENTO OUTROS(DIVERSOS) CONTINUAÇÃO"
				End If
			End While
		End If
		
		fr.VS.Table = "<80mm|>30mm;" & "TOTAL OUTROS:|" & FormatNumber(dblTot, 2)
		dblTot = 0
		lrstAux.Close()
		
		'ILUMINACAO PUBLICA
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 300
		dblTot = 0
		strSql2 = ""
		strsql = "SELECT COUNT(*) qtde FROM " & strTabela & " WHERE tip <> 0 "
		
		If astrGrupo <> "AB" Then
			strSql2 = strSql2 & " AND LEFT(grupo,1) = '" & VB.Left(astrGrupo, 1) & "' "
		End If
		
		Call Conectar(strsql & strSql2, lrstAux)
		If lrstAux.RecordCount > 0 Then
			fr.VS.Table = "^110mm;" & fr.VS.CurrentLine & "DETALHAMENTO DA ILUMINAÇÃO PÚBLICA"
			fr.VS.Table = "<80mm|>30mm;NORMAL|" & FormatNumber(lrstAux.Fields("qtde").Value, 0)
			dblTot = dblTot + lrstAux.Fields("qtde").Value
			System.Windows.Forms.Application.DoEvents()
		End If
		lrstAux.Close()
		If fr.VS.CurrentLine > lngControleLinha Then
			fr.VS.MarginLeft = strMargem
			fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			fr.VS.NewPage()
			fr.VS.CurrentLine = 1
			Call CabecaRel("(" & astrGrupo & ")" & strCabec)
			fr.VS.MarginLeft = strNovaMar
			fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbBoxColumns
			fr.VS.Table = "^110mm;DETALHAMENTO DA ILUMINAÇÃO PÚBLICA CONTINUAÇÃO"
		End If
		
		strsql = "SELECT COUNT(*) qtde FROM " & strTabela & " WHERE tip = 0 "
		
		Call Conectar(strsql & strSql2, lrstAux)
		If lrstAux.RecordCount > 0 Then
			fr.VS.Table = "<80mm|>30mm;ISENTO|" & FormatNumber(lrstAux.Fields("qtde").Value, 0)
			dblTot = dblTot + lrstAux.Fields("qtde").Value
			System.Windows.Forms.Application.DoEvents()
		End If
		lrstAux.Close()
		fr.VS.Table = "<80mm|>30mm;TOTAL:|" & FormatNumber(dblTot, 0)
		dblTot = 0
		fr.VS.MarginLeft = strMargem
		fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
	End Sub
	
	Private Sub Imprime(ByRef arstAux As ADODB.Recordset, ByRef tit As String, ByRef astrGrupo As String, Optional ByRef astrFase As String = "", Optional ByRef astrTipo As String = "")
		Dim strTemp As String
		
		Call LimpaCols()
		
		If astrTipo = "D" Then
			'Grupo B e Grupo A Convencional
			If VB.Left(astrGrupo, 1) = "B" Or astrGrupo = "A4" Or astrGrupo = "AB" Then
				strCol(1, 1) = " Nº CONSUMIDORES:||" & FormatNumber(arstAux.Fields("ncons").Value, 0)
				strCol(1, 2) = "||"
				strCol(1, 3) = "Kwh CONSUMIDO:||" & FormatNumber(arstAux.Fields("qkwh_cons").Value, 0)
				strCol(1, 4) = "||"
				strCol(1, 5) = "Kwh FATURADO:||" & FormatNumber(arstAux.Fields("qkwh_fat").Value, 0)
				strCol(1, 6) = "||"
				strCol(1, 7) = "Kw FATURADO:||" & FormatNumber(arstAux.Fields("qkw_fat").Value, 0)
				
				strCol(2, 1) = "|CONS.REATIVO|R$:|" & FormatNumber(arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value)
				strCol(2, 2) = "|||"
				strCol(2, 3) = "|||" '"||R$:|" & FormatNumber(arstAux!vkwh_cons)
				strCol(2, 4) = "|||"
				strCol(2, 5) = "||R$:|" & FormatNumber(arstAux.Fields("vkwh_fat").Value)
				strCol(2, 6) = "|||"
				strCol(2, 7) = "||R$:|" & FormatNumber(arstAux.Fields("vkw_fat").Value)
				
				strCol(3, 1) = "|CONVENIOS|R$:|" & FormatNumber(arstAux.Fields("Convenios").Value)
				strCol(3, 2) = "|DIVERSOS|R$:|" & FormatNumber(arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(3, 3) = "|CIP|R$:|" & FormatNumber(arstAux.Fields("tip").Value)
				strCol(3, 4) = "|SALDO ARRED.|R$:|" & FormatNumber(arstAux.Fields("saldo").Value)
				strCol(3, 5) = "|JUROS|R$:|" & FormatNumber(arstAux.Fields("juros").Value)
				strCol(3, 6) = "|DIF.ELETR.|R$:|" & FormatNumber(arstAux.Fields("dif_ele").Value)
				strCol(3, 7) = "|ENCARGO|R$:|" & FormatNumber(arstAux.Fields("encargo").Value)
				
				strCol(4, 1) = "|ICMS|R$:|" & FormatNumber(arstAux.Fields("ICMS").Value)
				strCol(4, 2) = "|PIS|R$:|" & FormatNumber(arstAux.Fields("Pis").Value)
				strCol(4, 3) = "|COFINS|R$:|" & FormatNumber(arstAux.Fields("cofins").Value)
				strCol(4, 4) = "|ICMS/PIS/COF|R$:|" & FormatNumber(arstAux.Fields("icmspiscof").Value)
				strCol(4, 5) = "|REC.COMPANH.|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat").Value + arstAux.Fields("vkw_fat").Value + (arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value) + arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(4, 6) = "|SUB-TOTAL|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat").Value + arstAux.Fields("vkw_fat").Value + arstAux.Fields("cofins").Value + arstAux.Fields("ICMS").Value + arstAux.Fields("Pis").Value + (arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value))
				strCol(4, 7) = "|TOTAL|R$:|" & FormatNumber(arstAux.Fields("total").Value)
			ElseIf astrGrupo = "AA" Or astrGrupo = "AV" Or astrGrupo = "AL" Then 
				strCol(1, 1) = "Nº CONSUMIDORES:||" & FormatNumber(arstAux.Fields("ncons").Value, 0)
				strCol(1, 2) = "||"
				strCol(1, 3) = "Kwh CONSUMIDO:||" & FormatNumber(arstAux.Fields("qkwh_cons").Value, 0)
				strCol(1, 4) = "Kwh FAT. PONTA:||" & FormatNumber(arstAux.Fields("qkwh_fat_p").Value, 0)
				strCol(1, 5) = "Kwh FAT. F.PONTA:||" & FormatNumber(arstAux.Fields("qkwh_fat_fp").Value, 0)
				strCol(1, 6) = "Kw FAT. PONTA:||" & FormatNumber(arstAux.Fields("qkw_fat_p").Value, 0)
				strCol(1, 7) = "Kw FAT. F.PONTA:||" & FormatNumber(arstAux.Fields("qkw_fat_fp").Value, 0)
				
				strCol(2, 1) = "|CONS.REATIVO|R$:|" & FormatNumber(arstAux.Fields("vconreat_p").Value + arstAux.Fields("vconreat_fp").Value + arstAux.Fields("vdemreat_p").Value + arstAux.Fields("vdemreat_fp").Value)
				strCol(2, 2) = "|||"
				strCol(2, 3) = "|||" '"||R$:|" & FormatNumber(arstAux!vkwh_fat_p + arstAux!vkwh_fat_fp) ' FormatNumber(arstAux!vkwh_cons)
				strCol(2, 4) = "||R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_p").Value)
				strCol(2, 5) = "||R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_fp").Value)
				strCol(2, 6) = "||R$:|" & FormatNumber(arstAux.Fields("vkw_fat_p").Value)
				strCol(2, 7) = "||R$:|" & FormatNumber(arstAux.Fields("vkw_fat_fp").Value)
				
				strCol(3, 1) = "|CONVENIOS|R$:|" & FormatNumber(arstAux.Fields("Convenios").Value)
				strCol(3, 2) = "|DIVERSOS|R$:|" & FormatNumber(arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(3, 3) = "|CIP|R$:|" & FormatNumber(arstAux.Fields("tip").Value)
				strCol(3, 4) = "|||"
				strCol(3, 5) = "|JUROS|R$:|" & FormatNumber(arstAux.Fields("juros").Value)
				strCol(3, 6) = "|DIF.ELETR.|R$:|" & FormatNumber(arstAux.Fields("dif_ele").Value)
				strCol(3, 7) = "|ENCARGO|R$:|" & FormatNumber(arstAux.Fields("encargo").Value)
				
				strCol(4, 1) = "|ICMS|R$:|" & FormatNumber(arstAux.Fields("ICMS").Value)
				strCol(4, 2) = "|PIS|R$:|" & FormatNumber(arstAux.Fields("Pis").Value)
				strCol(4, 3) = "|COFINS|R$:|" & FormatNumber(arstAux.Fields("cofins").Value)
				strCol(4, 4) = "|ICMS/PIS/COF|R$:|" & FormatNumber(arstAux.Fields("icmspiscof").Value)
				strCol(4, 5) = "|REC.COMPANH.|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_p").Value + arstAux.Fields("vkwh_fat_fp").Value + arstAux.Fields("vkw_fat_p").Value + arstAux.Fields("vkw_fat_fp").Value + (arstAux.Fields("vconreat_p").Value + arstAux.Fields("vconreat_fp").Value + arstAux.Fields("vdemreat_p").Value + arstAux.Fields("vdemreat_fp").Value) + arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(4, 6) = "|SUB-TOTAL|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_p").Value + arstAux.Fields("vkwh_fat_fp").Value + arstAux.Fields("vkw_fat_p").Value + arstAux.Fields("vkw_fat_fp").Value + arstAux.Fields("cofins").Value + arstAux.Fields("ICMS").Value + arstAux.Fields("Pis").Value + (arstAux.Fields("vconreat_p").Value + arstAux.Fields("vconreat_fp").Value + arstAux.Fields("vdemreat_p").Value + arstAux.Fields("vdemreat_fp").Value))
				strCol(4, 7) = "|TOTAL|R$:|" & FormatNumber(arstAux.Fields("total").Value)
			End If
		ElseIf astrTipo = "T" Then 
			If astrGrupo = "A" Or astrGrupo = "AB" Then
				strCol(1, 1) = "Nº CONSUMIDORES:||" & FormatNumber(arstAux.Fields("ncons").Value, 0)
				strCol(1, 2) = "||"
				strCol(1, 3) = "Kwh CONSUMIDO:||" & FormatNumber(arstAux.Fields("qkwh_cons").Value, 0)
				strCol(1, 4) = "Kwh FAT. PONTA:||" & FormatNumber(arstAux.Fields("qkwh_fat_p").Value, 0)
				strCol(1, 5) = "Kwh FAT. F.PONTA:||" & FormatNumber(arstAux.Fields("qkwh_fat_fp").Value + arstAux.Fields("qkwh_fat").Value, 0)
				strCol(1, 6) = "Kw FAT. PONTA:||" & FormatNumber(arstAux.Fields("qkw_fat_p").Value, 0)
				strCol(1, 7) = "Kw FAT. F.PONTA:||" & FormatNumber(arstAux.Fields("qkw_fat_fp").Value + arstAux.Fields("qkw_fat").Value, 0)
				
				strCol(2, 1) = "|CONS.REATIVO|R$:|" & FormatNumber(arstAux.Fields("vconreat_p").Value + arstAux.Fields("vconreat_fp").Value + arstAux.Fields("vdemreat_p").Value + arstAux.Fields("vdemreat_fp").Value + arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value)
				strCol(2, 2) = "|||"
				strCol(2, 3) = "|||" '"||R$:|" & FormatNumber(arstAux!vkwh_fat_p + arstAux!vkwh_fat_fp + arstAux!vkwh_fat) 'FormatNumber(arstAux!vkwh_cons)
				strCol(2, 4) = "||R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_p").Value)
				strCol(2, 5) = "||R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_fp").Value + arstAux.Fields("vkwh_fat").Value)
				strCol(2, 6) = "||R$:|" & FormatNumber(arstAux.Fields("vkw_fat_p").Value)
				strCol(2, 7) = "||R$:|" & FormatNumber(arstAux.Fields("vkw_fat_fp").Value + arstAux.Fields("vkw_fat").Value)
				
				strCol(3, 1) = "|CONVENIOS|R$:|" & FormatNumber(arstAux.Fields("Convenios").Value)
				strCol(3, 2) = "|DIVERSOS|R$:|" & FormatNumber(arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(3, 3) = "|CIP|R$:|" & FormatNumber(arstAux.Fields("tip").Value)
				strCol(3, 4) = "|SALDO ARRED.|R$:|" & FormatNumber(arstAux.Fields("saldo").Value)
				strCol(3, 5) = "|JUROS|R$:|" & FormatNumber(arstAux.Fields("juros").Value)
				strCol(3, 6) = "|DIF.ELETR.|R$:|" & FormatNumber(arstAux.Fields("dif_ele").Value)
				strCol(3, 7) = "|ENCARGO|R$:|" & FormatNumber(arstAux.Fields("encargo").Value)
				
				strCol(4, 1) = "|ICMS|R$:|" & FormatNumber(arstAux.Fields("ICMS").Value)
				strCol(4, 2) = "|PIS|R$:|" & FormatNumber(arstAux.Fields("Pis").Value)
				strCol(4, 3) = "|COFINS|R$:|" & FormatNumber(arstAux.Fields("cofins").Value)
				strCol(4, 4) = "|ICMS/PIS/COF|R$:|" & FormatNumber(arstAux.Fields("icmspiscof").Value)
				strCol(4, 5) = "|REC.COMPANH.|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_p").Value + arstAux.Fields("vkwh_fat_fp").Value + arstAux.Fields("vkwh_fat").Value + arstAux.Fields("vkw_fat_p").Value + arstAux.Fields("vkw_fat_fp").Value + arstAux.Fields("vkw_fat").Value + (arstAux.Fields("vconreat_p").Value + arstAux.Fields("vconreat_fp").Value + arstAux.Fields("vdemreat_p").Value + arstAux.Fields("vdemreat_fp").Value + arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value) + arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(4, 6) = "|SUB-TOTAL|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat_p").Value + arstAux.Fields("vkwh_fat_fp").Value + arstAux.Fields("vkwh_fat").Value + arstAux.Fields("vkw_fat_p").Value + arstAux.Fields("vkw_fat_fp").Value + arstAux.Fields("vkw_fat").Value + arstAux.Fields("cofins").Value + arstAux.Fields("ICMS").Value + arstAux.Fields("Pis").Value + (arstAux.Fields("vconreat_p").Value + arstAux.Fields("vconreat_fp").Value + arstAux.Fields("vdemreat_p").Value + arstAux.Fields("vdemreat_fp").Value + arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value))
				strCol(4, 7) = "|TOTAL|R$:|" & FormatNumber(arstAux.Fields("total").Value)
			ElseIf astrGrupo = "B" Then 
				strCol(1, 1) = "Nº CONSUMIDORES:||" & FormatNumber(arstAux.Fields("ncons").Value, 0)
				strCol(1, 2) = "||"
				strCol(1, 3) = "Kwh CONSUMIDO:||" & FormatNumber(arstAux.Fields("qkwh_cons").Value, 0)
				strCol(1, 4) = "||"
				strCol(1, 5) = "Kwh FATURADO:||" & FormatNumber(arstAux.Fields("qkwh_fat").Value, 0)
				strCol(1, 6) = "||"
				strCol(1, 7) = "Kw FATURADO:||" & FormatNumber(arstAux.Fields("qkw_fat").Value, 0)
				
				strCol(2, 1) = "|CONS.REATIVO|R$:|" & FormatNumber(arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value)
				strCol(2, 2) = "|||"
				strCol(2, 3) = "|||" '"||R$:|" & FormatNumber(arstAux!vkwh_cons)
				strCol(2, 4) = "|||"
				strCol(2, 5) = "||R$:|" & FormatNumber(arstAux.Fields("vkwh_fat").Value)
				strCol(2, 6) = "|||"
				strCol(2, 7) = "||R$:|" & FormatNumber(arstAux.Fields("vkw_fat").Value)
				
				strCol(3, 1) = "|CONVENIOS|R$:|" & FormatNumber(arstAux.Fields("Convenios").Value)
				strCol(3, 2) = "|DIVERSOS|R$:|" & FormatNumber(arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(3, 3) = "|CIP|R$:|" & FormatNumber(arstAux.Fields("tip").Value)
				strCol(3, 4) = "|SALDO ARRED.|R$:|" & FormatNumber(arstAux.Fields("saldo").Value)
				strCol(3, 5) = "|JUROS|R$:|" & FormatNumber(arstAux.Fields("juros").Value)
				strCol(3, 6) = "|DIF.ELETR.|R$:|" & FormatNumber(arstAux.Fields("dif_ele").Value)
				strCol(3, 7) = "|ENCARGO|R$:|" & FormatNumber(arstAux.Fields("encargo").Value)
				
				strCol(4, 1) = "|ICMS|R$:|" & FormatNumber(arstAux.Fields("ICMS").Value)
				strCol(4, 2) = "|PIS|R$:|" & FormatNumber(arstAux.Fields("Pis").Value)
				strCol(4, 3) = "|COFINS|R$:|" & FormatNumber(arstAux.Fields("cofins").Value)
				strCol(4, 4) = "|ICMS/PIS/COF|R$:|" & FormatNumber(arstAux.Fields("icmspiscof").Value)
				strCol(4, 5) = "|REC.COMPANH.|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat").Value + arstAux.Fields("vkw_fat").Value + (arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value) + arstAux.Fields("Diversos").Value - arstAux.Fields("descontos").Value)
				strCol(4, 6) = "|SUB-TOTAL|R$:|" & FormatNumber(arstAux.Fields("vkwh_fat").Value + arstAux.Fields("vkw_fat").Value + arstAux.Fields("cofins").Value + arstAux.Fields("ICMS").Value + arstAux.Fields("Pis").Value + (arstAux.Fields("vconreat").Value + arstAux.Fields("vdemreat").Value))
				strCol(4, 7) = "|TOTAL|R$:|" & FormatNumber(arstAux.Fields("total").Value)
			End If
		End If
		
		fr.VS.Font = VB6.FontChangeName(fr.VS.Font, "Arial")
		fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 10)
		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
		fr.VS.Table = "<200mm;" & tit & IIf(VB.Left(astrGrupo, 2) = "A4", "Convencional", IIf(VB.Left(astrGrupo, 2) = "AA", "Horo Sazonal Azul", IIf(VB.Left(astrGrupo, 2) = "AV", "Horo Sazonal Verde", IIf(VB.Left(astrGrupo, 2) = "AL", "Consumidor Livre", ""))))
		fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 6)
		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
		
		strTemp = "<25mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|>7mm|>20mm;"
		strTemp = strTemp & strCol(1, 1) & strCol(2, 1) & strCol(3, 1) & strCol(4, 1)
		fr.VS.Table = strTemp
		strTemp = "<25mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|>7mm|>20mm;"
		strTemp = strTemp & strCol(1, 2) & strCol(2, 2) & strCol(3, 2) & strCol(4, 2)
		fr.VS.Table = strTemp
		strTemp = "<25mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|>7mm|>20mm;"
		strTemp = strTemp & strCol(1, 3) & strCol(2, 3) & strCol(3, 3) & strCol(4, 3)
		fr.VS.Table = strTemp
		strTemp = "<25mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|>7mm|>20mm;"
		strTemp = strTemp & strCol(1, 4) & strCol(2, 4) & strCol(3, 4) & strCol(4, 4)
		fr.VS.Table = strTemp
		strTemp = "<25mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|>7mm|>20mm;"
		strTemp = strTemp & strCol(1, 5) & strCol(2, 5) & strCol(3, 5) & strCol(4, 5)
		fr.VS.Table = strTemp
		strTemp = "<25mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|>7mm|>20mm;"
		strTemp = strTemp & strCol(1, 6) & strCol(2, 6) & strCol(3, 6) & strCol(4, 6)
		fr.VS.Table = strTemp
		strTemp = "<25mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|<7mm|>20mm|<20mm|>7mm|>20mm;"
		strTemp = strTemp & strCol(1, 7) & strCol(2, 7) & strCol(3, 7) & strCol(4, 7)
		fr.VS.Table = strTemp
		
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.DrawLine("5mm", fr.VS.CurrentY + 100, "200mm", fr.VS.CurrentY + 100)
		'UPGRADE_ISSUE: VBControlExtender method fr.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 200
		
		If fr.VS.CurrentLine > 769 Then
			fr.VS.NewPage()
			fr.VS.CurrentLine = 1
			If strgrupoG = "AB" Then
				Call CabecaRel("A+B", astrFase)
			Else
				Call CabecaRel(IIf(astrGrupo <> "AB", VB.Left(astrGrupo, 1), astrGrupo), astrFase)
			End If
		End If
		
	End Sub
	
	Private Function CabecaRel(ByRef astrGrupo As String, Optional ByRef astrFase As String = "") As Object
		Dim strAux As String
		Dim strAux2 As String
		
		If VB6.GetItemString(cbxOption, cbxOption.SelectedIndex) = "Operacional" Then
			If chkRota.CheckState = 1 Then
				If cbRota1.Text = cbRota2.Text Then
					strAux = "Resumo do Faturamento - " & Trim(txtMesAno.Text) '& " - " & cbxOption.list(cbxOption.ListIndex)
					strAux2 = "Grupo(" & astrGrupo & ") - da Rota " & VB6.Format(cbRota1.Text, "0000")
				Else
					strAux = "Resumo do Faturamento - " & Trim(txtMesAno.Text) '& " - " & cbxOption.list(cbxOption.ListIndex)
					strAux2 = "Grupo(" & astrGrupo & ") - da Rota " & VB6.Format(cbRota1.Text, "0000") & " até a Rota " & VB6.Format(cbRota2.Text, "0000")
				End If
			Else
				strAux = "Resumo do Faturamento - " & Trim(txtMesAno.Text) '& " - " & cbxOption.list(cbxOption.ListIndex)
				strAux2 = "Grupo(" & astrGrupo & ") - Todas as Rotas "
			End If
		Else
			strAux = "Resumo do Faturamento - " & Trim(txtMesAno.Text) '& " - " & cbxOption.list(cbxOption.ListIndex)
			strAux2 = "Grupo(" & astrGrupo & ") - Consumidores " & astrFase
		End If
		Call Cabecalho_Relatorio((fr.VS), strAux, strAux2, "2", False, "")
		Call LineFeed((fr.VS), 1)
	End Function
	
	Private Sub RelEstat_Classe(ByRef astrGrupo As String)
		Dim strFase As String
		Dim intTipo As Short
		Dim strNomeFase As String
		Dim strsql As String
		Dim strSql2 As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		For intTipo = 1 To 3
			Select Case intTipo
				Case 1
					strFase = "M"
					strNomeFase = "Monofásico"
				Case 2
					strFase = "B"
					strNomeFase = "Bifásico"
				Case 3
					strFase = "T"
					strNomeFase = "Trifásico"
			End Select
			
			Call CabecaRel(astrGrupo, strNomeFase)
			
			strsql = "SELECT COUNT(*) ncons, " & "SUM(qkwh_cons) qkwh_cons, SUM(qkwh_fat) qkwh_fat, SUM(qkw_fat) qkw_fat, " & "SUM(vkwh_cons) vkwh_cons, SUM(vkwh_fat) vkwh_fat, SUM(vkw_fat) vkw_fat, " & "SUM(vconreat) vconreat, SUM(vdemreat) vdemreat, " & "SUM(qkwh_fat_p) qkwh_fat_p, SUM(qkwh_fat_fp) qkwh_fat_fp, " & "SUM(qkw_fat_p) qkw_fat_p, SUM(qkw_fat_fp) qkw_fat_fp, " & "SUM(qkw_fatu_p) qkw_fatu_p, SUM(qkw_fatu_fp) qkw_fatu_fp, " & "SUM(vkwh_fat_p) vkwh_fat_p, SUM(vkwh_fat_fp) vkwh_fat_fp, " & "SUM(vkw_fat_p) vkw_fat_p, SUM(vkw_fat_fp) vkw_fat_fp, " & "SUM(vkw_fatu_p) vkw_fatu_p, SUM(vkw_fatu_fp) vkw_fatu_fp, " & "SUM(vconreat_p) vconreat_p, SUM(vconreat_fp) vconreat_fp, " & "SUM(vdemreat_p) vdemreat_p, SUM(vdemreat_fp) vdemreat_fp, " & "SUM(tip) tip, SUM(sld_arred) saldo, SUM(icms) icms, " & "SUM(pis) pis, SUM(cofins) cofins, SUM(icmspiscof) icmspiscof, " & "SUM(dif_eltrbr) dif_ele, SUM(juros) juros, SUM(encargo) encargo, " & "SUM(convenios) convenios, SUM(diversos) diversos, SUM(taxados) taxados, " & "SUM(total) total " & "FROM " & strTabela & " " & "WHERE "
			If astrGrupo <> "AB" Then
				strsql = strsql & "grupo = '" & astrGrupo & "' AND "
			End If
			'RESIDENCIAL BX. RENDA ( 0-30) (9102)
			strSql2 = "classe IN (9102,9101) " & "AND fase = '" & strFase & "' " & "AND qkwh_fat BETWEEN 0 AND 30 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "RESIDENCIAL BX. RENDA (0-30) (9101,9102)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'RESIDENCIAL BX. RENDA ( 31-50) (9102)
			strSql2 = "classe IN (9102,9101) " & "AND fase = '" & strFase & "' " & "AND qkwh_fat BETWEEN 31 AND 50 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "RESIDENCIAL BX. RENDA (31-50) (9101,9102)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'RESIDENCIAL BX. RENDA ( 51-100) (9102)
			strSql2 = "classe IN (9102,9101) " & "AND fase = '" & strFase & "' " & "AND qkwh_fat BETWEEN 51 AND 100 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "RESIDENCIAL BX. RENDA (51-100) (9101,9102)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'RESIDENCIAL BX. RENDA ( 101-160) (9102)
			strSql2 = "classe IN (9102,9101) " & "AND fase = '" & strFase & "' " & "AND qkwh_fat BETWEEN 101 AND 160 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "RESIDENCIAL BX. RENDA (101-160) (9101,9102)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'RESIDENCIAL NORMAL (9101)
			strSql2 = "classe = 9101 " & "AND fase = '" & strFase & "' "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "RESIDENCIAL NORMAL (9101)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL RESIDENCIAL (9101 E 9102)
			strSql2 = "classe IN (9102,9101) " & "AND fase = '" & strFase & "' "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL RESIDENCIAL (9101 E 9102)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL INDUSTRIAL (1,2,3)
			strSql2 = "fase = '" & strFase & "' " & "AND classe BETWEEN 0 AND 3999 AND classe <> '0100' "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL INDUSTRIAL (0,1,2,3)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL COMERCIAL (4)
			strSql2 = "fase = '" & strFase & "' " & "AND classe BETWEEN 4000 AND 4999 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL COMERCIAL (4)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL RURAL (5)
			strSql2 = "fase = '" & strFase & "' " & "AND classe BETWEEN 5000 AND 5999 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL RURAL (5)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL PODER PUBLICO (61,62,63)
			strSql2 = "fase = '" & strFase & "' " & "AND classe BETWEEN 6100 AND 6399 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL PODER PUBLICO (61,62,63)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL ILUMINAÇÃO PUBLICA (64)
			strSql2 = "fase = '" & strFase & "' " & "AND classe = 6400 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL ILUMINAÇÃO PÚBLICA (64)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL SERVIÇO PUBLICO (7)
			strSql2 = "fase = '" & strFase & "' " & "AND classe BETWEEN 7000 AND 7999 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL SERVIÇO PÚBLICO (7)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL CONSUMO PROPRIO (8)
			strSql2 = "fase = '" & strFase & "' " & "AND classe BETWEEN 8000 AND 8999 "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL CONSUMO PRÓPRIO (8)", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			'TOTAL GERAL
			strSql2 = "fase = '" & strFase & "' "
			Call Conectar(strsql & strSql2, lrstAux)
			If lrstAux.Fields("ncons").Value > 0 Then
				Call Imprime(lrstAux, "TOTAL GERAL", astrGrupo, strNomeFase)
			End If
			lrstAux.Close()
			If Not intTipo = 3 Then
				fr.VS.NewPage()
				fr.VS.CurrentLine = 1
				Call CabecaRel(astrGrupo, strNomeFase)
			End If
		Next intTipo
	End Sub
	
	Private Function ValidaMesAno(ByRef astrMesAno As String) As String
		Dim strMes As String
		Dim strAno As String
		
		Select Case Len(astrMesAno)
			Case 1
				If Val(astrMesAno) >= 1 Or Val(astrMesAno) <= 12 Then
					astrMesAno = VB6.Format(astrMesAno, "00") & "/" & VB6.Format(Today, "yyyy")
				Else
					astrMesAno = ""
				End If
			Case 2
				If Val(astrMesAno) >= 1 Or Val(astrMesAno) <= 12 Then
					astrMesAno = VB6.Format(astrMesAno, "00") & "/" & VB6.Format(Today, "yyyy")
				Else
					astrMesAno = ""
				End If
			Case 3
				If Val(VB.Left(astrMesAno, 1)) >= 1 Or Val(VB.Left(astrMesAno, 1)) <= 9 Then
					astrMesAno = VB6.Format(VB.Left(astrMesAno, 1), "00") & "/" & IIf(CDbl(VB.Right(astrMesAno, 2)) >= 51, "19" & VB6.Format(VB.Right(astrMesAno, 2), "00"), "20" & VB6.Format(VB.Right(astrMesAno, 2), "00"))
				Else
					astrMesAno = ""
				End If
			Case 4
				If Val(VB.Left(astrMesAno, 2)) >= 1 Or Val(VB.Left(astrMesAno, 2)) <= 12 Then
					txtMesAno.Text = VB6.Format(VB.Left(astrMesAno, 2), "00") & "/" & IIf(CDbl(VB.Right(astrMesAno, 2)) >= 51, "19" & VB6.Format(VB.Right(astrMesAno, 2), "00"), "20" & VB6.Format(VB.Right(astrMesAno, 2), "00"))
				Else
					astrMesAno = ""
				End If
			Case 5
				If Val(VB.Left(astrMesAno, 1)) >= 1 Or Val(VB.Left(astrMesAno, 1)) <= 9 Then
					astrMesAno = VB6.Format(VB.Left(astrMesAno, 1), "00") & "/" & VB6.Format(VB.Right(astrMesAno, 4), "0000")
				Else
					astrMesAno = ""
				End If
			Case 6
				If Val(VB.Left(astrMesAno, 2)) >= 1 Or Val(VB.Left(astrMesAno, 2)) <= 9 Then
					astrMesAno = VB6.Format(VB.Left(astrMesAno, 2), "00") & "/" & VB6.Format(VB.Right(astrMesAno, 4), "0000")
				Else
					astrMesAno = ""
				End If
			Case Else
				astrMesAno = ""
		End Select
		ValidaMesAno = astrMesAno
	End Function
	
	Private Function LimpaCols() As Object
		Dim intlin As Short
		Dim intCol As Short
		
		For intCol = 1 To 4
			For intlin = 1 To 7
				strCol(intCol, intlin) = ""
			Next intlin
		Next intCol
	End Function
	
	Private Sub LigDesObj(ByRef abMode As Boolean)
		txtMesAno.Enabled = abMode
		lblMesAno.Enabled = abMode
		lblTipo.Enabled = abMode
		cbxOption.Enabled = abMode
		chkRota.Enabled = abMode
		lblInicial.Enabled = abMode
		cbRota1.Enabled = abMode
		lblFinal.Enabled = abMode
		cbRota2.Enabled = abMode
		chkGrupoA.Enabled = abMode
		chkGrupoB.Enabled = abMode
		chkTrans.Enabled = abMode
		cmdEmitir.Enabled = abMode
		cmdFechar.Enabled = abMode
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub encheRota()
		Dim strsql As String
		Dim rsConsulta As New ADODB.Recordset
		
		lblInfo.Text = "Buscando as rotas das faturas "
		lblInfo.Refresh()
		strsql = "SELECT LEFT(rota, 4) rota " & "  FROM " & strTabNf & " USE INDEX(keyrota) " & " WHERE mesano = '" & txtMesAno.Text & "' " & " GROUP BY LEFT(rota, 4) " & " ORDER BY LEFT(rota, 4) "
		Call Conectar(strsql, rsConsulta)
		If rsConsulta.RecordCount > 0 Then
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, rsConsulta.RecordCount)
			Call cbRota1.Items.Clear()
			Call cbRota2.Items.Clear()
			While Not rsConsulta.EOF
				If rsConsulta.Fields("ROTA").Value <> "" Then
					Call cbRota1.Items.Add(rsConsulta.Fields("ROTA").Value)
					Call cbRota2.Items.Add(rsConsulta.Fields("ROTA").Value)
				End If
				Call rsConsulta.MoveNext()
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				System.Windows.Forms.Application.DoEvents()
			End While
			cbRota1.SelectedIndex = 0
			cbRota2.SelectedIndex = 0
		End If
		lblInfo.Text = ""
		lblInfo.Refresh()
	End Sub
End Class