Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelReceitaBandeira
	Inherits System.Windows.Forms.Form
	Private istrNmArq As String
	Private idteBase As Date
	Private ifrmRelReceitaBandeira As FrmRelatorio
	Private istrTamColC As String
	Private istrTamColD As String
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		Dim lstrAux As String
		Call NomeArquivo()
		'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		Me.dialArqOpen.Filter = "*." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.FileName = StrReverse(Mid(StrReverse(Me.txtArquivo.Text), CInt(InStr(StrReverse(Me.txtArquivo.Text), "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação do Baixa Renda Jul2010 OF.C. 31/2013"
		Me.dialArqOpen.ShowDialog()
		lstrAux = StrReverse(Me.dialArqOpen.FileName)
		Me.txtArquivo.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
	End Sub
	
	Private Sub cmdArquivoXML_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivoXML.Click
		Dim lstrAux As String
		Call NomeArquivoXML()
		'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		Me.dialArqOpen.Filter = "*." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.FileName = StrReverse(Mid(StrReverse(Me.txtArquivoXML.Text), CInt(InStr(StrReverse(Me.txtArquivoXML.Text), "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação de Bandeiras Tarifárias - CBT"
		Me.dialArqOpen.ShowDialog()
		lstrAux = StrReverse(Me.dialArqOpen.FileName)
		Me.txtArquivoXML.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim llngRow As Integer
		Dim llngCol As Integer
		If Trim(Me.txtArquivo.Text) <> "" Then
			lintARQ = FreeFile
			FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
			With Me.mfgDados
				If .Rows > 1 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows, " Gerando Arquivo CSV ")
					For llngRow = 0 To .Rows - 1
						lstrLin = ""
						For llngCol = 0 To .Cols - 1
							lstrLin = lstrLin & .get_TextMatrix(llngRow, llngCol) & ";"
						Next llngCol
						If CInt(Len(lstrLin)) > 0 Then
							PrintLine(lintARQ, lstrLin)
						End If
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Gerando Arquivo CSV ")
					Next llngRow
				End If
			End With
			PrintLine(lintARQ, ";")
			FileClose(lintARQ)
			Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
	End Sub
	
	Private Sub cmdExportaXML_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExportaXML.Click
		
		Dim lintARQ As Short
		Dim lstrTab As String
		
		With Me.mfgCbt
			
			If .get_TextMatrix(1, 4) = "" Then
				Call MsgBox("Favor clicar em processar antes de exportar!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
				cmdProcessar.Focus()
				Exit Sub
			End If
			
			lstrTab = "    "
			
			If Trim(Me.txtArquivoXML.Text) <> "" Then
				
				lintARQ = FreeFile
				
				FileOpen(lintARQ, Me.txtArquivoXML.Text, OpenMode.Output)
				
				PrintLine(lintARQ, "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>")
				
				PrintLine(lintARQ, "<contaCentralizadoraCBT>")
				PrintLine(lintARQ, lstrTab)
				PrintLine(lintARQ, lstrTab & "<referencia>")
				
				
				PrintLine(lintARQ, lstrTab & lstrTab & "<agente>" & VB6.Format(gstrEmpresa_CodRes89, "00000") & "</agente>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<mes_competencia>" & VB6.Format(dtpMesAno.value, "MM") & "</mes_competencia>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<ano_competencia>" & VB6.Format(dtpMesAno.value, "YYYY") & "</ano_competencia>")
				
				PrintLine(lintARQ, lstrTab & "</referencia>")
				PrintLine(lintARQ, lstrTab)
				PrintLine(lintARQ, lstrTab & "<recursosCBT>")
				
				PrintLine(lintARQ, lstrTab & lstrTab & "<A1>" & IIf(.get_TextMatrix(1, 0) = "", "0,00", VB6.Format(.get_TextMatrix(1, 0), "##0.00")) & "</A1>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<A2>" & IIf(.get_TextMatrix(1, 1) = "", "0,00", VB6.Format(.get_TextMatrix(1, 1), "##0.00")) & "</A2>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<A3>" & IIf(.get_TextMatrix(1, 2) = "", "0,00", VB6.Format(.get_TextMatrix(1, 2), "##0.00")) & "</A3>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<A3A>" & IIf(.get_TextMatrix(1, 3) = "", "0,00", VB6.Format(.get_TextMatrix(1, 3), "##0.00")) & "</A3A>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<A4>" & IIf(.get_TextMatrix(1, 4) = "", "0,00", VB6.Format(.get_TextMatrix(1, 4), "##0.00")) & "</A4>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<AS>" & "0,00" & "</AS>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<B1>" & IIf(.get_TextMatrix(1, 5) = "", "0,00", VB6.Format(.get_TextMatrix(1, 5), "##0.00")) & "</B1>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<B1BaixaRenda>" & IIf(.get_TextMatrix(1, 6) = "", "0,00", VB6.Format(.get_TextMatrix(1, 6), "##0.00")) & "</B1BaixaRenda>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<B2>" & IIf(.get_TextMatrix(1, 7) = "", "0,00", VB6.Format(.get_TextMatrix(1, 7), "##0.00")) & "</B2>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<B3>" & IIf(.get_TextMatrix(1, 8) = "", "0,00", VB6.Format(.get_TextMatrix(1, 8), "##0.00")) & "</B3>")
				PrintLine(lintARQ, lstrTab & lstrTab & "<B4>" & IIf(.get_TextMatrix(1, 9) = "", "0,00", VB6.Format(.get_TextMatrix(1, 9), "##0.00")) & "</B4>")
				
				PrintLine(lintARQ, lstrTab & "</recursosCBT>")
				PrintLine(lintARQ, lstrTab)
				PrintLine(lintARQ, "</contaCentralizadoraCBT>")
				
				FileClose(lintARQ)
				
				Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				
			End If
			
		End With
		
	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 Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Dim lstrTab As String
		Dim llngRow As Integer
		Dim llngCol As Integer
		
		Dim lstrAux1 As String
		Dim lstrAux2 As String
		istrTamColC = "<0mm|^15mm|^35mm|^35mm|^15mm|^35mm|^35mm|^35mm|^35mm|^35mm|>0mm;|"
		istrTamColD = "<0mm|^15mm|<35mm|<35mm|^15mm|>35mm|>35mm|>35mm|>35mm|>35mm|>0mm;|"
		With ifrmRelReceitaBandeira
			.quem_chama = Me
			.VS.Font = VB6.FontChangeName(.VS.Font, "Arial")
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
			.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.Orientation = VSVIEW6.OrientationSettings.orLandscape
			.VS.PhysicalPage = True
			.VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
			.VS.MarginTop = "7mm"
			.VS.MarginRight = "7mm"
			.VS.MarginLeft = "7mm"
			.VS.MarginBottom = "5mm"
			.VS.TextColor = System.Drawing.Color.Black
			.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
			.VS.StartDoc()
			.VS.CurrentLine = 0
			.VS.Font = VB6.FontChangeSize(.VS.Font, 9)
			Call Cabecalho_Relatorio(.VS, "Relatório de Receita da Bandeira Tarifária", "Base: " & VB6.Format(Me.dtpMesAno.value, "MM/yyyy"), "3")
			.VS.Font = VB6.FontChangeBold(.VS.Font, True)
			Call LineFeed(.VS, 2)
			
			'Cabeçalho
			lstrTab = ""
			With Me.mfgDados
				ifrmRelReceitaBandeira.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
				lstrTab = "|" & .get_TextMatrix(0, 4) & "|" & .get_TextMatrix(0, 6) & "|" & .get_TextMatrix(0, 8) & ""
				ifrmRelReceitaBandeira.VS.Table = "<0mm|^100mm|^69.86mm|^69.86mm|^35mm|>0mm;|" & lstrTab
				lstrTab = ""
				For llngCol = 0 To .Cols - 1
					lstrTab = lstrTab & .get_TextMatrix(1, llngCol) & IIf(llngCol < .Cols - 1, "|", "")
				Next llngCol
				ifrmRelReceitaBandeira.VS.Table = istrTamColC & lstrTab
				ifrmRelReceitaBandeira.VS.Font = VB6.FontChangeBold(ifrmRelReceitaBandeira.VS.Font, False)
				llngRow = 2
				lstrTab = .get_TextMatrix(llngRow, 0) & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				
				llngRow = 3
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				
				llngRow = 4
				lstrTab = "" & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				
				llngRow = 5
				lstrTab = "" & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 6
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 7
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 8
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 9
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 10
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				
				llngRow = 11
				lstrTab = "" & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				
				llngRow = 12
				lstrTab = .get_TextMatrix(llngRow, 0) & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 13
				lstrTab = "" & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 14
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 15
				lstrTab = "" & "|" & "" & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				
				llngRow = 16
				lstrTab = "" & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 17
				lstrTab = "" & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				llngRow = 18
				lstrTab = "" & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
				ifrmRelReceitaBandeira.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			End With
			Call LineFeed((ifrmRelReceitaBandeira.VS), 2)
			
			'EMPRESA_COD_RES89
			ifrmRelReceitaBandeira.VS.Font = VB6.FontChangeBold(ifrmRelReceitaBandeira.VS.Font, True)
			ifrmRelReceitaBandeira.VS.Table = "<0mm|<100mm;|Código do Agente: " & VB6.Format(IIf(IsNumeric(gstrEmpresa_CodRes89), gstrEmpresa_CodRes89, 0), "###,###0")
			ifrmRelReceitaBandeira.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
			istrTamColC = "<0mm|^15mm|^15mm|^15mm|^15mm|^30mm|^30mm|^30mm|^30mm|^30mm|^30mm|^35mm|>0mm;|"
			istrTamColD = "<0mm|^15mm|^15mm|^15mm|^15mm|>30mm|>30mm|>30mm|>30mm|>30mm|>30mm|>35mm|>0mm;|"
			With Me.mfgCbt
				llngRow = 0
				lstrTab = .get_TextMatrix(llngRow, 0) & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8) & "|" & .get_TextMatrix(llngRow, 9) & "|" & .get_TextMatrix(llngRow, 10) '& "|" & .TextMatrix(llngRow, 11)
				ifrmRelReceitaBandeira.VS.Table = istrTamColC & lstrTab
				ifrmRelReceitaBandeira.VS.Font = VB6.FontChangeBold(ifrmRelReceitaBandeira.VS.Font, False)
				llngRow = 1
				lstrTab = .get_TextMatrix(llngRow, 0) & "|" & .get_TextMatrix(llngRow, 1) & "|" & .get_TextMatrix(llngRow, 2) & "|" & .get_TextMatrix(llngRow, 3) & "|" & .get_TextMatrix(llngRow, 4) & "|" & .get_TextMatrix(llngRow, 5) & "|" & .get_TextMatrix(llngRow, 6) & "|" & .get_TextMatrix(llngRow, 7) & "|" & .get_TextMatrix(llngRow, 8) & "|" & .get_TextMatrix(llngRow, 9) & "|" & .get_TextMatrix(llngRow, 10) '& "|" & .TextMatrix(llngRow, 11)
				ifrmRelReceitaBandeira.VS.Table = istrTamColD & lstrTab
			End With
		End With
		
		With ifrmRelReceitaBandeira
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			Call .VS.EndDoc()
			Call VB6.ShowForm(ifrmRelReceitaBandeira, False)
		End With
		
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim ldblVKwhFat As Double
		Dim ldblVKwhFatBan As Double
		Dim ldblVkWhFatTE As Double
		Dim ldblVkWhFatTU As Double
		Dim ldblVkWhFatTEBan As Double
		Dim ldblVKwhFatp As Double
		Dim ldblVKwhFatfp As Double
		Dim ldblVKwhFatpLiv As Double
		Dim ldblVKwhFatfpLiv As Double
		Dim ldblVKwhFatpBan As Double
		Dim ldblVKwhFatfpBan As Double
		Dim lintMicroGeracao As Double
		Dim ldteIni As Date
		Dim ldteFim As Date
		Dim lintParcialmenteCativo As Short
		Dim lstrFlgst As String
		Dim lintMpr As Short
		Dim lstrItens As String
		Dim lstrGrupo As String
		Dim llngClasse As Integer
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Call MontaGrade()
		Call LigDes(False)
		Me.mfgDados.Redraw = False
		Me.mfgCbt.Redraw = False
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Me.lblInfo.Text = " Aguarde processando..."
		Call Me.lblInfo.Refresh()
		ldteFim = CDate(UltimoDiaMes((dtpMesAno.Month), (dtpMesAno.Year)) & "/" & dtpMesAno.Month & "/" & dtpMesAno.Year)
		ldteIni = CDate("01/" & VB6.Format(ldteFim, "mm/yyyy"))
		
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * " & vbCrLf
		lstrSql = lstrSql & "  FROM nf " & vbCrLf
		lstrSql = lstrSql & " WHERE (" & vbCrLf
		lstrSql = lstrSql & "        (`mesano` = '" & VB6.Format(Me.dtpMesAno.value, "MM/yyyy") & "' AND `flgst` <> 'C') OR " & vbCrLf
		lstrSql = lstrSql & "        (`flgst` ='C' AND datast > '" & VB6.Format(ldteFim, "yyyy-MM-dd") & "') OR " & vbCrLf
		lstrSql = lstrSql & "        (`flgst` ='C' AND `mesano_ref` <> REPLACE(LEFT(`datast`, 7),'-','') AND `datast` BETWEEN '" & VB6.Format(ldteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(ldteFim, "yyyy-MM-dd") & "')" & vbCrLf
		lstrSql = lstrSql & "       )" & vbCrLf
		
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrGrupo = Mid(IIf(IsDbNull(.Fields("ta").value), "01B100", .Fields("ta").value), 3, 2)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrItens = Trim(IIf(IsDbNull(.Fields("itensserv").Value), "", .Fields("itensserv").Value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrFlgst = Trim(IIf(IsDbNull(.Fields("flgst").value), "", .Fields("flgst").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						llngClasse = CInt(IIf(IsDbNull(.Fields("codclas").value), 0, .Fields("codclas").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lintParcialmenteCativo = IIf(IsDbNull(.Fields("parcialmente_cativo").value), 0, .Fields("parcialmente_cativo").value)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lintMicroGeracao = IIf(IsDbNull(.Fields("micro_geracao").value), 0, .Fields("micro_geracao").value)
						If lstrFlgst = "C" Then
							lintMpr = -1
						Else
							lintMpr = 1
						End If
						If lstrGrupo = "AS" Or lstrGrupo = "A4" Or VB.Left(lstrGrupo, 1) = "B" Then
							If VB.Left(lstrGrupo, 1) <> "B" Then
								ldblVKwhFat = BuscaVlrItem(lstrItens, "0302020009") * lintMpr 'Consumo faturado
								ldblVKwhFatBan = BuscaVlrItem(lstrItens, "0302120009") * lintMpr 'Consumo faturado bandeira
								'If lstrGrupo = "A4" Then
								With Me.mfgDados
									.set_TextMatrix(12, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(12, 3)), .get_TextMatrix(12, 3), 0) + 1, "###,##0"))
									.set_TextMatrix(12, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(12, 6)), .get_TextMatrix(12, 6), 0) + ldblVKwhFat, "#,##0.00"))
									.set_TextMatrix(12, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(12, 7)), .get_TextMatrix(12, 7), 0) + ldblVKwhFatBan, "#,##0.00"))
									.set_TextMatrix(12, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(12, 8)), .get_TextMatrix(12, 8), 0) + ldblVKwhFatBan, "#,##0.00"))
								End With
								Call TotalizaCbt(4, ldblVKwhFatBan) 'A4
								'End If
							Else 'B
								ldblVKwhFat = (BuscaVlrItem(lstrItens, "030202009") + BuscaVlrItem(lstrItens, "030202019") + BuscaVlrItem(lstrItens, "030202029") + BuscaVlrItem(lstrItens, "030202039")) * lintMpr 'Consumo faturado
								ldblVKwhFatBan = (BuscaVlrItem(lstrItens, "030212009") + BuscaVlrItem(lstrItens, "030212019") + BuscaVlrItem(lstrItens, "030212029") + BuscaVlrItem(lstrItens, "030212039")) * lintMpr 'Consumo faturado bandeira
								'
								ldblVkWhFatTE = BuscaVlrItem(lstrItens, "030402009") * lintMpr 'consumo faturado TE
								ldblVkWhFatTU = BuscaVlrItem(lstrItens, "030302009") * lintMpr 'consumo faturado TUSD
								ldblVkWhFatTEBan = BuscaVlrItem(lstrItens, "030412009") * lintMpr 'consumo faturado TE bandeira
								If ldblVkWhFatTEBan = 0 Then ldblVkWhFatTEBan = ldblVKwhFatBan
								With Me.mfgDados
									If llngClasse >= 0 And llngClasse <= 3999 Then
										.set_TextMatrix(5, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(5, 3)), .get_TextMatrix(5, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(5, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(5, 6)), .get_TextMatrix(5, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
										.set_TextMatrix(5, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(5, 7)), .get_TextMatrix(5, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										.set_TextMatrix(5, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(5, 8)), .get_TextMatrix(5, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										Call TotalizaCbt(8, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B3
									ElseIf llngClasse >= 4000 And llngClasse <= 4999 Then 
										.set_TextMatrix(6, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(6, 3)), .get_TextMatrix(6, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(6, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(6, 6)), .get_TextMatrix(6, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
										.set_TextMatrix(6, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(6, 7)), .get_TextMatrix(6, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										.set_TextMatrix(6, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(6, 8)), .get_TextMatrix(6, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										Call TotalizaCbt(8, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B3
									ElseIf llngClasse >= 5000 And llngClasse <= 5999 Then 
										.set_TextMatrix(4, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(4, 3)), .get_TextMatrix(4, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(4, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(4, 6)), .get_TextMatrix(4, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
										.set_TextMatrix(4, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(4, 7)), .get_TextMatrix(4, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										.set_TextMatrix(4, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(4, 8)), .get_TextMatrix(4, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										Call TotalizaCbt(7, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B2
									ElseIf llngClasse >= 6100 And llngClasse <= 6399 Then 
										.set_TextMatrix(7, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(7, 3)), .get_TextMatrix(7, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(7, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(7, 6)), .get_TextMatrix(7, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
										.set_TextMatrix(7, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(7, 7)), .get_TextMatrix(7, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										.set_TextMatrix(7, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(7, 8)), .get_TextMatrix(7, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										Call TotalizaCbt(8, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B3
									ElseIf llngClasse >= 6400 And llngClasse <= 6499 Then 
										.set_TextMatrix(11, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(11, 3)), .get_TextMatrix(11, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(11, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(11, 6)), .get_TextMatrix(11, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
										.set_TextMatrix(11, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(11, 7)), .get_TextMatrix(11, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										.set_TextMatrix(11, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(11, 8)), .get_TextMatrix(11, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										Call TotalizaCbt(9, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B4
									ElseIf llngClasse >= 7000 And llngClasse <= 7999 Then 
										.set_TextMatrix(8, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(8, 3)), .get_TextMatrix(8, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(8, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(8, 6)), .get_TextMatrix(8, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
										.set_TextMatrix(8, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(8, 7)), .get_TextMatrix(8, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										.set_TextMatrix(8, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(8, 8)), .get_TextMatrix(8, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										Call TotalizaCbt(8, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B3
									ElseIf llngClasse >= 8000 And llngClasse <= 8999 Then 
										.set_TextMatrix(9, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(9, 3)), .get_TextMatrix(9, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(9, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(9, 6)), .get_TextMatrix(9, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
										.set_TextMatrix(9, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(9, 7)), .get_TextMatrix(9, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										.set_TextMatrix(9, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(9, 8)), .get_TextMatrix(9, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
										Call TotalizaCbt(8, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B3
									ElseIf llngClasse >= 9100 And llngClasse <= 9199 Then 
										If llngClasse >= 9102 And llngClasse <= 9106 Then
											.set_TextMatrix(3, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(3, 3)), .get_TextMatrix(3, 3), 0) + 1, "###,##0"))
											.set_TextMatrix(3, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(3, 6)), .get_TextMatrix(3, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
											.set_TextMatrix(3, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(3, 7)), .get_TextMatrix(3, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
											.set_TextMatrix(3, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(3, 8)), .get_TextMatrix(3, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
											Call TotalizaCbt(6, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'Baixa Renda
										Else
											.set_TextMatrix(2, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(2, 3)), .get_TextMatrix(2, 3), 0) + 1, "###,##0"))
											.set_TextMatrix(2, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(2, 6)), .get_TextMatrix(2, 6), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFat, (ldblVkWhFatTU + ldblVkWhFatTE)), "#,##0.00"))
											.set_TextMatrix(2, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(2, 7)), .get_TextMatrix(2, 7), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
											.set_TextMatrix(2, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(2, 8)), .get_TextMatrix(2, 8), 0) + IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan), "#,##0.00"))
											Call TotalizaCbt(5, IIf(lintMicroGeracao = 0, ldblVKwhFatBan, ldblVkWhFatTEBan)) 'B1
										End If
									End If
								End With
							End If
						ElseIf lstrGrupo = "AA" Or lstrGrupo = "AV" Or lstrGrupo = "AL" Or lstrGrupo = "AP" Or lstrGrupo = "AX" Then 
							ldblVKwhFatp = 0
							ldblVKwhFatfp = 0
							ldblVKwhFatpLiv = 0
							ldblVKwhFatfpLiv = 0
							ldblVKwhFatp = BuscaVlrItem(lstrItens, "0302020010") * lintMpr 'Consumo faturado p
							ldblVKwhFatpBan = BuscaVlrItem(lstrItens, "0302120010") * lintMpr 'Consumo faturado p
							If lstrGrupo = "AL" Then
								If lintParcialmenteCativo = 1 Then
									ldblVKwhFatpLiv = BuscaVlrItem(lstrItens, "0302020060") * lintMpr 'Consumo faturado p
								Else
									ldblVKwhFatpLiv = BuscaVlrItem(lstrItens, "0302020010") * lintMpr 'Consumo faturado p
									ldblVKwhFatp = ldblVKwhFatp - ldblVKwhFatpLiv
									ldblVKwhFatpBan = 0
								End If
							End If
							ldblVKwhFatfp = BuscaVlrItem(lstrItens, "0302020011") * lintMpr 'Consumo faturado fp
							ldblVKwhFatfpBan = BuscaVlrItem(lstrItens, "0302120011") * lintMpr 'Consumo faturado fp
							If lstrGrupo = "AL" Then
								If lintParcialmenteCativo = 1 Then
									ldblVKwhFatfpLiv = BuscaVlrItem(lstrItens, "0302020061") * lintMpr 'Consumo faturado fp
								Else
									ldblVKwhFatfpLiv = BuscaVlrItem(lstrItens, "0302020011") * lintMpr 'Consumo faturado fp
									ldblVKwhFatfp = ldblVKwhFatfp - ldblVKwhFatfpLiv
									ldblVKwhFatfpBan = 0
								End If
							End If
							With Me.mfgDados
								If lstrGrupo = "AV" Or lstrGrupo = "AA" Then
									If llngClasse >= 0 And llngClasse <= 3999 Then
										.set_TextMatrix(13, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(13, 3)), .get_TextMatrix(13, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(13, 4, VB6.Format(IIf(IsNumeric(.get_TextMatrix(13, 4)), .get_TextMatrix(13, 4), 0) + ldblVKwhFatp, "#,##0.00"))
										.set_TextMatrix(13, 5, VB6.Format(IIf(IsNumeric(.get_TextMatrix(13, 5)), .get_TextMatrix(13, 5), 0) + ldblVKwhFatpBan, "#,##0.00"))
										.set_TextMatrix(13, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(13, 6)), .get_TextMatrix(13, 6), 0) + ldblVKwhFatfp, "#,##0.00"))
										.set_TextMatrix(13, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(13, 7)), .get_TextMatrix(13, 7), 0) + ldblVKwhFatfpBan, "#,##0.00"))
										.set_TextMatrix(13, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(13, 8)), .get_TextMatrix(13, 8), 0) + ldblVKwhFatpBan + ldblVKwhFatfpBan, "#,##0.00"))
									ElseIf llngClasse >= 4000 And llngClasse <= 4999 Then 
										.set_TextMatrix(14, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(14, 3)), .get_TextMatrix(14, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(14, 4, VB6.Format(IIf(IsNumeric(.get_TextMatrix(14, 4)), .get_TextMatrix(14, 4), 0) + ldblVKwhFatp, "#,##0.00"))
										.set_TextMatrix(14, 5, VB6.Format(IIf(IsNumeric(.get_TextMatrix(14, 5)), .get_TextMatrix(14, 5), 0) + ldblVKwhFatpBan, "#,##0.00"))
										.set_TextMatrix(14, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(14, 6)), .get_TextMatrix(14, 6), 0) + ldblVKwhFatfp, "#,##0.00"))
										.set_TextMatrix(14, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(14, 7)), .get_TextMatrix(14, 7), 0) + ldblVKwhFatfpBan, "#,##0.00"))
										.set_TextMatrix(14, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(14, 8)), .get_TextMatrix(14, 8), 0) + ldblVKwhFatpBan + ldblVKwhFatfpBan, "#,##0.00"))
									ElseIf llngClasse >= 6100 And llngClasse <= 6399 Then 
										.set_TextMatrix(15, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(15, 3)), .get_TextMatrix(15, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(15, 4, VB6.Format(IIf(IsNumeric(.get_TextMatrix(15, 4)), .get_TextMatrix(15, 4), 0) + ldblVKwhFatp, "#,##0.00"))
										.set_TextMatrix(15, 5, VB6.Format(IIf(IsNumeric(.get_TextMatrix(15, 5)), .get_TextMatrix(15, 5), 0) + ldblVKwhFatpBan, "#,##0.00"))
										.set_TextMatrix(15, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(15, 6)), .get_TextMatrix(15, 6), 0) + ldblVKwhFatfp, "#,##0.00"))
										.set_TextMatrix(15, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(15, 7)), .get_TextMatrix(15, 7), 0) + ldblVKwhFatfpBan, "#,##0.00"))
										.set_TextMatrix(15, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(15, 8)), .get_TextMatrix(15, 8), 0) + ldblVKwhFatpBan + ldblVKwhFatfpBan, "#,##0.00"))
									End If
								ElseIf lstrGrupo = "AL" Then 
									If lintParcialmenteCativo = 1 Then
										.set_TextMatrix(16, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(16, 3)), .get_TextMatrix(16, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(16, 4, VB6.Format(IIf(IsNumeric(.get_TextMatrix(16, 4)), .get_TextMatrix(16, 4), 0) + ldblVKwhFatp, "#,##0.00"))
										.set_TextMatrix(16, 5, VB6.Format(IIf(IsNumeric(.get_TextMatrix(16, 5)), .get_TextMatrix(16, 5), 0) + ldblVKwhFatpBan, "#,##0.00"))
										.set_TextMatrix(16, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(16, 6)), .get_TextMatrix(16, 6), 0) + ldblVKwhFatfp, "#,##0.00"))
										.set_TextMatrix(16, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(16, 7)), .get_TextMatrix(16, 7), 0) + ldblVKwhFatfpBan, "#,##0.00"))
										.set_TextMatrix(16, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(16, 8)), .get_TextMatrix(16, 8), 0) + ldblVKwhFatpBan + ldblVKwhFatfpBan, "#,##0.00"))
									Else
										.set_TextMatrix(17, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(17, 3)), .get_TextMatrix(17, 3), 0) + 1, "###,##0"))
										.set_TextMatrix(17, 4, VB6.Format(IIf(IsNumeric(.get_TextMatrix(17, 4)), .get_TextMatrix(17, 4), 0) + ldblVKwhFatp, "#,##0.00"))
										.set_TextMatrix(17, 5, VB6.Format(IIf(IsNumeric(.get_TextMatrix(17, 5)), .get_TextMatrix(17, 5), 0) + ldblVKwhFatpBan, "#,##0.00"))
										.set_TextMatrix(17, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(17, 6)), .get_TextMatrix(17, 6), 0) + ldblVKwhFatfp, "#,##0.00"))
										.set_TextMatrix(17, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(17, 7)), .get_TextMatrix(17, 7), 0) + ldblVKwhFatfpBan, "#,##0.00"))
										.set_TextMatrix(17, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(17, 8)), .get_TextMatrix(17, 8), 0) + ldblVKwhFatpBan + ldblVKwhFatfpBan, "#,##0.00"))
									End If
								ElseIf lstrGrupo = "AX" Then 
									.set_TextMatrix(18, 3, VB6.Format(IIf(IsNumeric(.get_TextMatrix(18, 3)), .get_TextMatrix(18, 3), 0) + 1, "###,##0"))
									.set_TextMatrix(18, 4, VB6.Format(IIf(IsNumeric(.get_TextMatrix(18, 4)), .get_TextMatrix(18, 4), 0) + ldblVKwhFatp, "#,##0.00"))
									.set_TextMatrix(18, 5, VB6.Format(IIf(IsNumeric(.get_TextMatrix(18, 5)), .get_TextMatrix(18, 5), 0) + ldblVKwhFatpBan, "#,##0.00"))
									.set_TextMatrix(18, 6, VB6.Format(IIf(IsNumeric(.get_TextMatrix(18, 6)), .get_TextMatrix(18, 6), 0) + ldblVKwhFatfp, "#,##0.00"))
									.set_TextMatrix(18, 7, VB6.Format(IIf(IsNumeric(.get_TextMatrix(18, 7)), .get_TextMatrix(18, 7), 0) + ldblVKwhFatfpBan, "#,##0.00"))
									.set_TextMatrix(18, 8, VB6.Format(IIf(IsNumeric(.get_TextMatrix(18, 8)), .get_TextMatrix(18, 8), 0) + ldblVKwhFatpBan + ldblVKwhFatfpBan, "#,##0.00"))
								End If
								Call TotalizaCbt(4, ldblVKwhFatpBan + ldblVKwhFatfpBan) 'A4
							End With
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
				End If
				Call .Close()
			End If
		End With
		Me.Cursor = System.Windows.Forms.Cursors.Default
		Me.mfgDados.Redraw = True
		Me.mfgCbt.Redraw = True
		Call LigDes(True)
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub TotalizaCbt(ByVal alngCol As Integer, ByVal adblValor As Double)
		With Me.mfgCbt
			.set_TextMatrix(1, alngCol, VB6.Format(IIf(IsNumeric(.get_TextMatrix(1, alngCol)), .get_TextMatrix(1, alngCol), 0) + adblValor, "#,##0.00"))
			.set_TextMatrix(1, 10, VB6.Format(IIf(IsNumeric(.get_TextMatrix(1, 10)), .get_TextMatrix(1, 10), 0) + adblValor, "#,##0.00"))
		End With
		
	End Sub
	
	Private Sub MontaGrade()
		With Me.mfgDados
			.Rows = 19
			.Cols = 9
			Call .Clear()
			.MergeCells = MSFlexGridLib.MergeCellsSettings.flexMergeRestrictAll
			.set_TextMatrix(0, 0, "Grupo")
			.set_TextMatrix(1, 0, .get_TextMatrix(0, 0))
			.set_TextMatrix(0, 1, "Classe")
			.set_TextMatrix(1, 1, .get_TextMatrix(0, 1))
			.set_TextMatrix(0, 2, "Sub-Classe")
			.set_TextMatrix(1, 2, .get_TextMatrix(0, 2))
			.set_TextMatrix(0, 3, "Qtde")
			.set_TextMatrix(1, 3, .get_TextMatrix(0, 3))
			
			.set_TextMatrix(0, 4, "Ponta")
			.set_TextMatrix(0, 5, .get_TextMatrix(0, 4))
			.set_TextMatrix(1, 4, "Verde")
			.set_TextMatrix(1, 5, "Receita")
			
			.set_TextMatrix(0, 6, "Fora Ponta")
			.set_TextMatrix(0, 7, .get_TextMatrix(0, 6))
			.set_TextMatrix(1, 6, "Verde")
			.set_TextMatrix(1, 7, "Receita")
			.set_TextMatrix(0, 8, "Receita")
			.set_TextMatrix(1, 8, "Total")
			
			
			.set_ColWidth(0, 600)
			.set_ColWidth(1, 1410)
			.set_ColWidth(2, 1400)
			.set_ColWidth(3, 1000)
			.set_ColWidth(4, 1000)
			.set_ColWidth(5, 1000)
			.set_ColWidth(6, 1000)
			.set_ColWidth(7, 1000)
			.set_ColWidth(8, 1000)
			
			'Grupos
			.set_TextMatrix(2, 0, "B")
			.set_TextMatrix(3, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(4, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(5, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(6, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(7, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(8, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(9, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(10, 0, .get_TextMatrix(2, 0))
			.set_TextMatrix(11, 0, .get_TextMatrix(2, 0))
			'
			.set_TextMatrix(12, 0, "A")
			.set_TextMatrix(13, 0, .get_TextMatrix(12, 0))
			.set_TextMatrix(14, 0, .get_TextMatrix(12, 0))
			.set_TextMatrix(15, 0, .get_TextMatrix(12, 0))
			.set_TextMatrix(16, 0, .get_TextMatrix(12, 0))
			.set_TextMatrix(17, 0, .get_TextMatrix(12, 0))
			.set_TextMatrix(18, 0, .get_TextMatrix(12, 0))
			
			'Classes
			.set_TextMatrix(2, 1, "Residencial")
			.set_TextMatrix(3, 1, .get_TextMatrix(2, 1))
			.set_TextMatrix(4, 1, "Rural")
			.set_TextMatrix(5, 1, "Demais Classes")
			.set_TextMatrix(6, 1, .get_TextMatrix(5, 1))
			.set_TextMatrix(7, 1, .get_TextMatrix(5, 1))
			.set_TextMatrix(8, 1, .get_TextMatrix(5, 1))
			.set_TextMatrix(9, 1, .get_TextMatrix(5, 1))
			.set_TextMatrix(10, 1, .get_TextMatrix(5, 1))
			.set_TextMatrix(11, 1, "Iluminação Pública")
			'
			.set_TextMatrix(12, 1, "Convencional")
			.set_TextMatrix(13, 1, "Horo-sazonal")
			.set_TextMatrix(14, 1, .get_TextMatrix(13, 1))
			.set_TextMatrix(15, 1, .get_TextMatrix(13, 1))
			
			.set_TextMatrix(16, 1, "Livre Parc.Cat.")
			.set_TextMatrix(17, 1, "Livre")
			.set_TextMatrix(18, 1, "Suprimento")
			
			'SubClasses
			.set_TextMatrix(2, 2, "Residencial")
			.set_TextMatrix(3, 2, "Baixa Renda")
			.set_TextMatrix(4, 2, "Rural")
			
			.set_TextMatrix(5, 2, "Industrial")
			.set_TextMatrix(6, 2, "Comercial")
			.set_TextMatrix(7, 2, "Poder Público")
			.set_TextMatrix(8, 2, "Serviço Público")
			.set_TextMatrix(9, 2, "Consumo Próprio")
			.set_TextMatrix(10, 2, "Total")
			
			.set_TextMatrix(11, 2, "Iluminação Pública")
			'
			.set_TextMatrix(12, 2, "C. Comercial")
			
			.set_TextMatrix(13, 2, "H. Industrial")
			.set_TextMatrix(14, 2, "H. Comercial")
			.set_TextMatrix(15, 2, "H. Poder Público")
			
			.set_TextMatrix(16, 2, "P. Industrial")
			.set_TextMatrix(17, 2, "L. Industrial")
			.set_TextMatrix(18, 2, "Cooperativa")
			'
			.set_MergeCol(0, True)
			.set_MergeCol(1, True)
			.set_MergeCol(2, True)
			.set_MergeCol(3, True)
			.set_MergeCol(4, True)
			.set_MergeCol(5, True)
			.set_MergeRow(0, True)
			.set_MergeRow(1, True)
			
			.set_MergeRow(2, False)
			.set_MergeRow(3, False)
			.set_MergeRow(4, False)
			.set_MergeRow(5, False)
			.set_MergeRow(6, False)
			.set_MergeRow(7, False)
			.set_MergeRow(8, False)
			.set_MergeRow(9, False)
			.set_MergeRow(10, False)
			.set_MergeRow(11, False)
			.set_MergeRow(12, False)
			.set_MergeRow(13, False)
			.set_MergeRow(14, False)
			.set_MergeRow(15, False)
			.set_MergeRow(16, False)
			.set_MergeRow(17, False)
		End With
		'
		With Me.mfgCbt
			.Rows = 2
			.Cols = 11
			Call .Clear()
			.set_TextMatrix(0, 0, "A1")
			.set_TextMatrix(0, 1, "A2")
			.set_TextMatrix(0, 2, "A3")
			.set_TextMatrix(0, 3, "A3a")
			.set_TextMatrix(0, 4, "A4")
			.set_TextMatrix(0, 5, "B1")
			.set_TextMatrix(0, 6, "B.Renda")
			.set_TextMatrix(0, 7, "B2")
			.set_TextMatrix(0, 8, "B3")
			.set_TextMatrix(0, 9, "B4")
			.set_TextMatrix(0, 10, "Total")
			'
			.set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(6, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(8, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(9, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(10, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			'
			.set_ColWidth(0, 520)
			.set_ColWidth(1, 520)
			.set_ColWidth(2, 520)
			.set_ColWidth(3, 520)
			.set_ColWidth(4, 1100)
			.set_ColWidth(5, 1000)
			.set_ColWidth(6, 1000)
			.set_ColWidth(7, 1000)
			.set_ColWidth(8, 1000)
			.set_ColWidth(9, 1000)
			.set_ColWidth(10, 1210)
		End With
	End Sub
	
	Private Sub frmRelReceitaBandeira_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		ifrmRelReceitaBandeira = New FrmRelatorio
		Me.dtpMesAno.value = BuscaDataBD
		Me.txtArquivo.Text = My.Application.Info.DirectoryPath & "\" & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		Me.txtArquivoXML.Text = My.Application.Info.DirectoryPath & "\" & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		Call NomeArquivo()
		Call NomeArquivoXML()
		Call MontaGrade()
	End Sub
	
	Private Sub NomeArquivo()
		Dim lstrAux As String
		Me.dtpMesAno.CustomFormat = "MM/yyyy"
		Me.dialArqOpen.DefaultExt = "CSV"
		istrNmArq = "receita_bandeira" & VB6.Format(Me.dtpMesAno.value, "MMYYYY")
		lstrAux = StrReverse(Me.txtArquivo.Text)
		Me.txtArquivo.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub NomeArquivoXML()
		
		Dim lstrAux As String
		Dim lstrRegRet As String
		
		If OptRegular.Checked = True Then
			lstrRegRet = "_S001"
		ElseIf OptRetificacao.Checked = True Then 
			lstrRegRet = "_S002"
		End If
		
		Me.dialArqOpen.DefaultExt = "XML"
		
		istrNmArq = "APLCBT" & VB6.Format(gstrEmpresa_CodRes89, "00000") & "_BANDEIRASTARIFARIAS_" & VB6.Format(Me.dtpMesAno.value, "MMYYYY") & lstrRegRet & ""
		
		lstrAux = StrReverse(Me.txtArquivoXML.Text)
		Me.txtArquivoXML.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		System.Windows.Forms.Application.DoEvents()
		
	End Sub
	
	Private Sub dtpMesAno_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpMesAno.Change
		Call NomeArquivo()
		Call NomeArquivoXML()
	End Sub
	
	Private Sub dtpMesAno_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpMesAno.ClickEvent
		Call dtpMesAno_Change(dtpMesAno, New System.EventArgs())
	End Sub
	
	Private Sub frmRelReceitaBandeira_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			Call cmdFechar_Click(cmdFechar, New System.EventArgs())
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.cmdProcessar.Enabled = ablnModo
		Me.cmdImprimir.Enabled = ablnModo
		Me.cmdExporta.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.dtpMesAno.Enabled = ablnModo
		Me.cmdArquivo.Enabled = ablnModo
		Me.cmdArquivoXML.Enabled = ablnModo
		Me.cmdExportaXML.Enabled = ablnModo
	End Sub
	
	Private Sub frmRelReceitaBandeira_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'UPGRADE_NOTE: Object ifrmRelReceitaBandeira may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		ifrmRelReceitaBandeira = Nothing
	End Sub
	
	
	'UPGRADE_WARNING: Event OptRegular.CheckedChanged 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 OptRegular_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles OptRegular.CheckedChanged
		If eventSender.Checked Then
			
			Call NomeArquivoXML()
			
		End If
	End Sub
	
	'UPGRADE_WARNING: Event OptRetificacao.CheckedChanged 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 OptRetificacao_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles OptRetificacao.CheckedChanged
		If eventSender.Checked Then
			
			Call NomeArquivoXML()
			
		End If
	End Sub
End Class