Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelRendaNaoFaturadaNovo
	Inherits System.Windows.Forms.Form
	Private istrTabela As String
	
	Private Function MontaTabela() As Boolean
		Dim lblnRetorno As Boolean
		Dim ldblNumNF As Double
		Dim llngConsId As Integer
		Dim llngUnidId As Integer
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrNome As String
		Dim lstrCodClas As String
		Dim lstrNomClas As String
		Dim lstrGrupo As String
		Dim lstrGruTar As String
		Dim ldteleitAtual As Date
		Dim ldteLeitAnter As Date
		Dim lintQtdDiasNao As Short
		Dim lintQtdDiasLeit As Short
		Dim lintTotDiasLeit As Short
		Dim lstrItens As String
		Dim ldblQtdkWh As Double
		Dim ldblVlrkWh As Double
		Dim ldblBankWh As Double
		Dim ldblQtdkW As Double
		Dim ldblVlrkW As Double
		Dim ldblQtdkWu As Double
		Dim ldblVlrkWu As Double
		Dim ldblQtdkWhNao As Double
		Dim ldblVlrkWhNao As Double
		Dim ldblBankWhNao As Double
		Dim ldblQtdkWNao As Double
		Dim ldblVlrkWNao As Double
		Dim ldblQtdkWuNao As Double
		Dim ldblVlrkWuNao As Double
		Dim lstrMesAno As String
		Dim ldteUltDia As Date
		Dim ldteMesAnt As Date
		Dim lintFaturaComplementar As Short
		Dim lintAux As Short
		Dim lstrSql As String
		Dim lintIns As Short
		Dim lstrIns As String
		Dim lstrDet As String
		Dim lrstAux As ADODB.Recordset
		Dim lrstFat As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lrstFat = New ADODB.Recordset
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		lblnRetorno = False
		On Error GoTo ErroMontaTabela
		ldteUltDia = CDate(VB6.Format(UltimoDiaMes((Me.dtpMesAno.Month), (Me.dtpMesAno.Year)), "00") & "/" & VB6.Format(Me.dtpMesAno.value, "MM/yyyy"))
		ldteMesAnt = System.Date.FromOADate(CDate(DateSerial(Me.dtpMesAno.Year, Me.dtpMesAno.Month, 1)).ToOADate - 1)
		lstrMesAno = VB6.Format(Me.dtpMesAno.value, "MM/yyyy")
		'
		Me.lblInfo.Text = " Buscando as faturas, aguarde..."
		Call Me.lblInfo.Refresh()
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * " & vbCrLf
		lstrSql = lstrSql & "  FROM " & gstrDataBase & ".`nf` " & vbCrLf
		lstrSql = lstrSql & " WHERE `mesano` = '" & lstrMesAno & "' " & vbCrLf
		If Me.chkConsProp.CheckState = System.Windows.Forms.CheckState.Unchecked Then
			lstrSql = lstrSql & "   AND (`codclas` NOT BETWEEN '8000' AND '8999') " & vbCrLf
		End If
		lstrSql = lstrSql & "   AND `grupo` IN("
		If Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrSql = lstrSql & "'A'"
		End If
		If Me.chkGrupoB.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrSql = lstrSql & IIf(Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Checked, ",", "") & "'B'"
		End If
		lstrSql = lstrSql & ")"
		lstrSql = lstrSql & "   AND (`flgst` <> 'C' OR ((`flgst`='C' AND `datast` > '" & VB6.Format(ldteUltDia, "yyyy-MM-dd") & "')))" & vbCrLf
		lstrSql = lstrSql & MontaFiltroClasse()
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call Executar("DELETE FROM " & gstrDataBase & ".`" & istrTabela & "` WHERE `anomes` = " & VB6.Format(Me.dtpMesAno.value, "yyyyMM") & " ", False,  , True, False)
					Call Executar("START TRANSACTION", False,  , False, False)
					lintIns = 0
					lstrIns = ""
					lstrIns = lstrIns & "INSERT INTO " & gstrDataBase & ".`" & istrTabela & "`("
					lstrIns = lstrIns & "`anomes`,`nome`, `numnf`, `zlc`, `classe`, `nmclasse`, `qtd_dias_leit`, `qtd_dias_leit_nao`, "
					lstrIns = lstrIns & "`qtd_kwh`, `qtd_kwh_nao`, `vlr_kwh`, `vlr_kwh_nao`, `ban_kwh`, `ban_kwh_nao`, "
					lstrIns = lstrIns & "`qtd_kw` , `qtd_kw_nao` , `vlrkw`  , `vlr_kw_nao` , "
					lstrIns = lstrIns & "`qtd_kwu`, `qtd_kwu_nao`, `vlrkwu` , `vlr_kwu_nao`) VALUES "
					lstrDet = ""
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
					While Not .EOF
						ldblQtdkWh = 0
						ldblVlrkWh = 0
						ldblBankWh = 0
						ldblQtdkW = 0
						ldblQtdkWu = 0
						ldblVlrkW = 0
						ldblVlrkWu = 0
						lintQtdDiasLeit = 0
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldblNumNF = IIf(IsDbNull(.Fields("numnf").value), 0, .Fields("numnf").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"'
						lintFaturaComplementar = IIf(IsDbNull(.Fields("fatura_complementar").value), 0, .Fields("fatura_complementar").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"'
						lstrCodClas = VB6.Format(IIf(IsDbNull(.Fields("codclas").value), "0", .Fields("codclas").value), "0000")
						lstrNomClas = GetNomeCLasse(lstrCodClas)
						'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"'
						llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").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"'
						llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").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"'
						lstrZona = IIf(IsDbNull(.Fields("zona").value), "", .Fields("zona").value)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrLivro = IIf(IsDbNull(.Fields("livro").value), "", .Fields("livro").value)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrConta = IIf(IsDbNull(.Fields("conta").value), "", .Fields("conta").value)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrGrupo = IIf(IsDbNull(.Fields("grupo").value), "B", .Fields("grupo").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"'
						lstrGruTar = Mid(IIf(IsDbNull(.Fields("ta").value), "01B100", .Fields("ta").value), 3, 2)
						lstrNome = RetornaNomeConsumidor(lstrZona, lstrLivro, lstrConta, llngUnidId, llngConsId, lstrGrupo)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldteleitAtual = IIf(IsDbNull(.Fields("dataleit").value), "1900-01-01", .Fields("dataleit").value)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldteLeitAnter = IIf(IsDbNull(.Fields("dataleitant").value), "1900-01-01", .Fields("dataleitant").value)
						lintQtdDiasNao = (UltimoDiaMes((Me.dtpMesAno.Month), (Me.dtpMesAno.Year)) - VB.Day(ldteleitAtual))
						'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
						lintQtdDiasLeit = DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteLeitAnter, ldteleitAtual, FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1)
						'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 = IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value)
						Call PegaItens(lstrGrupo, lstrItens, ldblQtdkWh, ldblVlrkWh, ldblBankWh, ldblQtdkW, ldblVlrkW, ldblQtdkWu, ldblVlrkWu)
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * " & vbCrLf
						lstrSql = lstrSql & "  FROM " & gstrDataBase & ".`nf` " & vbCrLf
						lstrSql = lstrSql & " WHERE `mesano_ref` <= " & VB6.Format(ldteMesAnt, "yyyyMM") & " " & vbCrLf
						lstrSql = lstrSql & "   AND (`flgst` <> 'C' OR ((`flgst`='C' AND `datast` > CONCAT(RIGHT(`mesano`, 4),'-',LEFT(`mesano`, 2),'-',DAY(LAST_DAY(CONCAT(RIGHT(`mesano`,4),'-',LEFT(`mesano`,2),'-01'))))))) " & vbCrLf
						lstrSql = lstrSql & "   AND `unid_id` = " & llngUnidId & " " & vbCrLf
						lstrSql = lstrSql & "   AND `cons_id` = " & llngConsId & " " & vbCrLf
						lstrSql = lstrSql & "   AND `fatura_final` = 0 " & vbCrLf
						lstrSql = lstrSql & "   AND `flgmedfat` <> 'S' " & vbCrLf
						lstrSql = lstrSql & "   AND `grupo` = '" & lstrGrupo & "' " & vbCrLf
						lstrSql = lstrSql & "   AND `zona` = '" & lstrZona & "' " & vbCrLf
						lstrSql = lstrSql & "   AND `livro` = '" & lstrLivro & "' " & vbCrLf
						lstrSql = lstrSql & "   AND `conta` = '" & lstrConta & "' " & vbCrLf
						lstrSql = lstrSql & "   AND `fatura_complementar` = " & lintFaturaComplementar
						lstrSql = lstrSql & "   ORDER BY `mesano_ref` DESC LIMIT 0, 2 "
						Call Conectar(lstrSql, lrstFat)
						With lrstFat
							If .State = ADODB.ObjectStateEnum.adStateOpen Then
								If .RecordCount > 0 Then
									While Not .EOF
										'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
										lstrItens = 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"'
										ldteleitAtual = IIf(IsDbNull(.Fields("dataleit").value), "1900-01-01", .Fields("dataleit").value)
										'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
										ldteLeitAnter = IIf(IsDbNull(.Fields("dataleitant").value), "1900-01-01", .Fields("dataleitant").value)
										'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
										lintQtdDiasLeit = lintQtdDiasLeit + DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteLeitAnter, ldteleitAtual, FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1)
										Call PegaItens(lstrGrupo, lstrItens, ldblQtdkWh, ldblVlrkWh, ldblBankWh, ldblQtdkW, ldblVlrkW, ldblQtdkWu, ldblVlrkWu)
										Call .MoveNext()
									End While
								End If
								Call .Close()
							End If
						End With
						'Calculo
						'Consumo
						ldblQtdkWhNao = Arredondar((ldblQtdkWh / lintQtdDiasLeit) * lintQtdDiasNao, 2)
						ldblVlrkWhNao = Arredondar((ldblVlrkWh / lintQtdDiasLeit) * lintQtdDiasNao, 2)
						ldblBankWhNao = Arredondar((ldblBankWh / lintQtdDiasLeit) * lintQtdDiasNao, 2)
						'Demanda
						ldblQtdkWNao = Arredondar((ldblQtdkW / lintQtdDiasLeit) * lintQtdDiasNao, 2)
						ldblVlrkWNao = Arredondar((ldblVlrkW / lintQtdDiasLeit) * lintQtdDiasNao, 2)
						'Ultrapassagem
						ldblQtdkWuNao = Arredondar((ldblQtdkWu / lintQtdDiasLeit) * lintQtdDiasNao, 2)
						ldblVlrkWuNao = Arredondar((ldblVlrkWu / lintQtdDiasLeit) * lintQtdDiasNao, 2)
						'lstrDet = ""
						lstrDet = lstrDet & "(" & VB6.Format(Me.dtpMesAno.value, "yyyyMM") & ","
						lstrDet = lstrDet & "'" & Replace(lstrNome, "'", "\'") & "',"
						lstrDet = lstrDet & ldblNumNF & ","
						lstrDet = lstrDet & "'" & lstrZona & "." & lstrLivro & "." & lstrConta & "',"
						lstrDet = lstrDet & "'" & lstrCodClas & "', "
						lstrDet = lstrDet & "'" & Replace(lstrNomClas, "'", "\'") & "',"
						lstrDet = lstrDet & lintQtdDiasLeit & ","
						lstrDet = lstrDet & lintQtdDiasNao & ","
						lstrDet = lstrDet & Replace(CStr(ldblQtdkWh), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblQtdkWhNao), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblVlrkWh), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblVlrkWhNao), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblBankWh), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblBankWhNao), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblQtdkW), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblQtdkWNao), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblVlrkW), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblVlrkWNao), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblQtdkWu), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblQtdkWuNao), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblVlrkWu), ",", ".") & ","
						lstrDet = lstrDet & Replace(CStr(ldblVlrkWuNao), ",", ".") & "),"
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						lintIns = lintIns + 1
						If lintIns > 600 Then
							lblnRetorno = Executar(lstrIns & VB.Left(lstrDet, Len(lstrDet) - 1), False,  , False, False)
							lintIns = 0
							lstrDet = ""
						End If
					End While
				End If
				If Len(lstrDet) > 0 Then
					lblnRetorno = Executar(lstrIns & VB.Left(lstrDet, Len(lstrDet) - 1), False,  , False, False)
				End If
				Call Executar("COMMIT", False,  , False, False)
				Call .Close()
			End If
		End With
ErroMontaTabela: 
		Me.Cursor = System.Windows.Forms.Cursors.Default
		If myConC.Errors.Count > 0 Then
			Call MsgBox("Código: " & myConC.Errors.Item(myConC.Errors.Count - 1).NativeError & vbCrLf & "Descrição:" & myConC.Errors.Item(myConC.Errors.Count - 1).Description, MsgBoxStyle.Critical, gstrTituloMensagem)
			Me.lblInfo.Text = "Erro no procedimento, entre em contato c/ Administrador"
			Call Executar("ROLLBACK", False)
		ElseIf Err.Number > 0 Then 
			Call MsgBox(Err.Number & " - " & GetErrosVB(Err.Number, Err.Description), MsgBoxStyle.Critical, gstrTituloMensagem)
			Me.lblInfo.Text = "Erro no procedimento, entre em contato c/ Administrador"
			Call Executar("ROLLBACK", False)
		Else
			Me.lblInfo.Text = Me.lblInfo.Text & " *** Finalizado **** "
			Call Me.lblInfo.Refresh()
		End If
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
		'UPGRADE_NOTE: Object lrstFat 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"'
		lrstFat = Nothing
		MontaTabela = lblnRetorno
	End Function
	
	Private Sub PegaItens(ByVal astrGrupo As String, ByVal astrItens As String, ByRef adblQtdKwh As Double, ByRef adblVlrkWh As Double, ByRef adblBankWh As Double, ByRef adblQtdkW As Double, ByRef adblVlrkW As Double, ByRef adblQtdkWu As Double, ByRef adblVlrkWu As Double)
		If astrGrupo = "B" Then
			'Consumo
			'Qtde
			adblQtdKwh = adblQtdKwh + BuscaValorItem(astrItens, "030201009")
			'Valor
			adblVlrkWh = adblVlrkWh + BuscaValorItem(astrItens, "030202009")
			'Bandeira
			adblBankWh = adblBankWh + BuscaValorItem(astrItens, "030212009")
		ElseIf astrGrupo = "A" Then 
			'Consumo
			'Qtde
			adblQtdKwh = adblQtdKwh + BuscaVlrItem(astrItens, "0302010009")
			adblQtdKwh = adblQtdKwh + BuscaVlrItem(astrItens, "0302010010") + BuscaVlrItem(astrItens, "0302010060") 'Consumo Lido na Ponta
			adblQtdKwh = adblQtdKwh + BuscaVlrItem(astrItens, "0302010011") + BuscaVlrItem(astrItens, "0302010061") 'Consumo Lido na Fora Ponta
			'Valores
			adblVlrkWh = adblVlrkWh + BuscaVlrItem(astrItens, "0302020009") 'convecional
			adblVlrkWh = adblVlrkWh + BuscaVlrItem(astrItens, "0302020010") + BuscaVlrItem(astrItens, "0302020060") 'Consumo faturado p
			adblVlrkWh = adblVlrkWh + BuscaVlrItem(astrItens, "0302020011") + BuscaVlrItem(astrItens, "0302020061") 'Consumo faturado fp
			'Bandeiras
			adblBankWh = adblBankWh + BuscaVlrItem(astrItens, "0302120009") 'convenional
			adblBankWh = adblBankWh + BuscaVlrItem(astrItens, "0302120010") + BuscaVlrItem(astrItens, "0302120011") 'ponta e fora ponta
			'Demandas
			'Qtde
			adblQtdkW = adblQtdkW + BuscaVlrItem(astrItens, "0302010017") 'Demanda
			adblQtdkW = adblQtdkW + BuscaVlrItem(astrItens, "0302010021") 'ponta
			adblQtdkW = adblQtdkW + BuscaVlrItem(astrItens, "0302010025") 'fora ponta
			adblQtdkWu = adblQtdkWu + BuscaVlrItem(astrItens, "0302010018") 'Demanda de ultrapassagem fp
			adblQtdkWu = adblQtdkWu + BuscaVlrItem(astrItens, "0302010022") 'ponta
			adblQtdkWu = adblQtdkWu + BuscaVlrItem(astrItens, "0302010026") 'fora ponta
			'Valores
			adblVlrkW = adblVlrkW + BuscaVlrItem(astrItens, "0302020015") 'convecional
			adblVlrkW = adblVlrkW + BuscaVlrItem(astrItens, "0302020021") 'na Ponta
			adblVlrkW = adblVlrkW + BuscaVlrItem(astrItens, "0302020023") '+ BuscaVlrItem(astrItens, "0302020072") 'Fora da Ponta
			adblVlrkWu = adblVlrkWu + BuscaVlrItem(astrItens, "0302020018") 'Demanda de ultrapassagem Convencional
			adblVlrkWu = adblVlrkWu + BuscaVlrItem(astrItens, "0302020022") 'Demanda de ultrapassagem p
			adblVlrkWu = adblVlrkWu + BuscaVlrItem(astrItens, "0302020026") 'Demanda de ultrapassagem fp
		End If
	End Sub
	
	Private Function CriaTabelaTemporaria() As Boolean
		Dim lblnRetorno As Boolean
		Dim lstrSql As String
		On Error GoTo ErroCriaTabelaTemporaria
		' Cria tabela temporária
		lstrSql = ""
		lstrSql = "CREATE TABLE IF NOT EXISTS " & gstrDataBase & ".`" & istrTabela & "` ("
		lstrSql = lstrSql & " `anomes`            int(6)  NOT NULL default '0',"
		lstrSql = lstrSql & " `nome`              varchar(255)  NOT NULL default '',"
		lstrSql = lstrSql & " `numnf`             double        NOT NULL default '0',"
		lstrSql = lstrSql & " `zlc`               varchar(13)   NOT NULL default '000.00.000000',"
		lstrSql = lstrSql & " `qtd_kwh`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kwh_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kwh`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kwh_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `ban_kwh`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `ban_kwh_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kw`            double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kw_nao`        double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlrkw`             double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kw_nao`        double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kwu`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kwu_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlrkwu`            double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kwu_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `classe`            varchar(4)    NOT NULL default '0000',"
		lstrSql = lstrSql & " `nmclasse`          varchar(255)  NOT NULL default '',"
		lstrSql = lstrSql & " `qtd_dias_leit`     int(3)        NOT NULL default '0',"
		lstrSql = lstrSql & " `qtd_dias_leit_nao` int(3)        NOT NULL default '0', "
		lstrSql = lstrSql & " KEY `idx_anomes` (`anomes`),"
		lstrSql = lstrSql & " KEY `idx_nmclasse` (`nmclasse`))"
		lblnRetorno = Executar(lstrSql, False,  , True, False)
		lstrSql = ""
		lstrSql = "CREATE TABLE IF NOT EXISTS " & gstrDataBase & ".`" & istrTabela & "_total` ("
		lstrSql = lstrSql & " `anomes`            int(6)  NOT NULL default '0',"
		lstrSql = lstrSql & " `qtd_kwh`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kwh_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kwh`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kwh_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `ban_kwh`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `ban_kwh_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kw`            double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kw_nao`        double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlrkw`             double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kw_nao`        double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kwu`           double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `qtd_kwu_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlrkwu`            double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `vlr_kwu_nao`       double(14,2)  NOT NULL default '0.00',"
		lstrSql = lstrSql & " `nmclasse`          varchar(255)  NOT NULL default '',"
		lstrSql = lstrSql & " KEY `idx_anomes` (`anomes`),"
		lstrSql = lstrSql & " KEY `idx_nmclasse` (`nmclasse`))"
		lblnRetorno = Executar(lstrSql, False,  , True, False)
ErroCriaTabelaTemporaria: 
		If myConC.Errors.Count > 0 Or Err.Number > 0 Then
			lblnRetorno = False
		End If
		CriaTabelaTemporaria = lblnRetorno
	End Function
	
	Private Function GetNomeCLasse(ByVal astrCodClasse As String) As String
		Dim lstrRetorno As String
		If (VB.Left(astrCodClasse, 1) = "9") Then
			lstrRetorno = "RESIDENCIAL"
		ElseIf (CDbl(VB.Left(astrCodClasse, 1)) >= 1) And (CDbl(VB.Left(astrCodClasse, 1)) <= 3) Then 
			lstrRetorno = "INDUSTRIAL"
		ElseIf (VB.Left(astrCodClasse, 1) = "4") Then 
			lstrRetorno = "COMERCIAL"
		ElseIf (VB.Left(astrCodClasse, 1) = "5") Then 
			lstrRetorno = "RURAL"
		ElseIf (VB.Left(astrCodClasse, 2) = "61") Then 
			lstrRetorno = "PODER PÚBLICO MUNICIPAL"
		ElseIf (VB.Left(astrCodClasse, 2) = "62") Then 
			lstrRetorno = "PODER PÚBLICO ESTADUAL"
		ElseIf (VB.Left(astrCodClasse, 2) = "63") Then 
			lstrRetorno = "PODER PÚBLICO FEDERAL"
		ElseIf (VB.Left(astrCodClasse, 2) = "64") Then 
			lstrRetorno = "ILUMINAÇÃO PÚBLICA"
		ElseIf (VB.Left(astrCodClasse, 1) = "7") Then 
			lstrRetorno = "SERVIÇO PÚBLICO"
		ElseIf (VB.Left(astrCodClasse, 4) = "0100") Then 
			lstrRetorno = "CONSUMIDOR LIVRE"
		ElseIf (VB.Left(astrCodClasse, 1) = "8") Then 
			lstrRetorno = "CONSUMO PRÓPRIO"
		Else
			lstrRetorno = "INDEFINIDO"
		End If
		GetNomeCLasse = lstrRetorno
	End Function
	
	'UPGRADE_WARNING: Event chkGrupoA.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 chkGrupoA_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkGrupoA.CheckStateChanged
		If Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Unchecked Then
			Me.chkGrupoB.CheckState = System.Windows.Forms.CheckState.Checked
		End If
	End Sub
	
	'UPGRADE_WARNING: Event chkGrupoB.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 chkGrupoB_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkGrupoB.CheckStateChanged
		If Me.chkGrupoB.CheckState = System.Windows.Forms.CheckState.Unchecked Then
			Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Checked
		End If
	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 lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * FROM " & gstrDataBase & ".`" & istrTabela & "` WHERE `anomes` = " & VB6.Format(Me.dtpMesAno.value, "yyyyMM") & " LIMIT 0, 1"
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call LigDes(False)
					With Me.crpRendaNaoFaturada
						.ReportFileName = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "RelRendaNaoFaturNovo.rpt"
						.DiscardSavedData = True
						.WindowState = Crystal.WindowStateConstants.crptMaximized
						.WindowControlBox = True
						.WindowControls = True
						.WindowShowPrintSetupBtn = True
						.WindowShowCloseBtn = True
						.WindowShowSearchBtn = True
						.SelectionFormula = "{temp_relrendnaofaturnovo.anomes} = " & VB6.Format(Me.dtpMesAno.value, "yyyyMM") & " "
						.set_Formulas(1, "mesano=""" & VB6.Format(Me.dtpMesAno.value, "MM/yyyy") & """")
						.set_Formulas(2, "TipoRelatorio=" & IIf(Me.optSintetico.Checked, 1, 0) & "")
						.WindowTitle = "Relatório de Renda Não Faturada"
						.Destination = Crystal.DestinationConstants.crptToWindow
						.Action = 0
					End With
					Call LigDes(True)
				Else
					Call MsgBox("O mês e o ano não foi processado ainda!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim lstrSql As String
		Call LigDes(False)
		If CriaTabelaTemporaria Then
			If MontaTabela Then
				Call TotalizaRendaNaoFatura()
				Call cmdImprimir_Click(cmdImprimir, New System.EventArgs())
			End If
		End If
		Call LigDes(True)
	End Sub
	
	Private Sub frmRelRendaNaoFaturadaNovo_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 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 frmRelRendaNaoFaturadaNovo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		Me.dtpMesAno.value = "01/" & LeConfig("MESANOFATURAMENTO")
		istrTabela = "temp_relrendnaofaturnovo"
		Call CarregaClasses((Me.lsvClasses))
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.dtpMesAno.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.cmdImprimir.Enabled = ablnModo
		Me.cmdProcessar.Enabled = ablnModo
		Me.chkGrupoA.Enabled = ablnModo
		Me.chkGrupoB.Enabled = ablnModo
		Me.chkConsProp.Enabled = ablnModo
		Me.chkAbrePoderesPublicos.Enabled = ablnModo
		If Me.optAnalitico.Checked Then
			Me.optSintetico.Enabled = ablnModo
			Me.optAnalitico.Checked = True
		ElseIf Me.optSintetico.Checked Then 
			Me.optAnalitico.Enabled = ablnModo
			Me.optSintetico.Checked = True
		End If
	End Sub
	
	Private Sub TotalizaRendaNaoFatura()
		Dim llngAnoMes As Integer
		Dim lstrCodClas As String
		Dim lstrDesClas As String
		Dim ldblQtdkWh As Double
		Dim ldblVlrkWh As Double
		Dim ldblBankWh As Double
		Dim ldblQtdkW As Double
		Dim ldblVlrkW As Double
		Dim ldblQtdkWu As Double
		Dim ldblVlrkWu As Double
		Dim ldblQtdkWhNao As Double
		Dim ldblVlrkWhNao As Double
		Dim ldblBankWhNao As Double
		Dim ldblQtdkWNao As Double
		Dim ldblVlrkWNao As Double
		Dim ldblQtdkWuNao As Double
		Dim ldblVlrkWuNao As Double
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		On Error GoTo ErroTotalizaRendaNaoFatura
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		lrstAux = New ADODB.Recordset
		Me.lblInfo.Text = " Totalizando a renda não faturada, aguarde... "
		Call Me.lblInfo.Refresh()
		Call Executar("DELETE FROM " & gstrDataBase & ".`" & istrTabela & "_total` WHERE `anomes` = " & VB6.Format(Me.dtpMesAno.value, "yyyyMM") & " ", False,  , True, False)
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT `anomes`, " & vbCrLf
		If Me.chkAbrePoderesPublicos.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrSql = lstrSql & "         `nmclasse`," & vbCrLf
		Else
			lstrSql = lstrSql & "        IF(INSTR(`nmclasse`,'PODER PÚBLICO')>0,'PODER PÚBLICO',`nmclasse`) `nmclasse`," & vbCrLf
		End If
		lstrSql = lstrSql & "          SUM(`qtd_kwh`)     `qtd_kwh`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`qtd_kwh_nao`) `qtd_kwh_nao`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`vlr_kwh`)     `vlr_kwh`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`vlr_kwh_nao`) `vlr_kwh_nao`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`ban_kwh`)     `ban_kwh`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`ban_kwh_nao`) `ban_kwh_nao`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`qtd_kw`)      `qtd_kw`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`qtd_kw_nao`)  `qtd_kw_nao`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`vlrkw`)       `vlrkw`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`vlr_kw_nao`)  `vlr_kw_nao`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`qtd_kwu`)     `qtd_kwu`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`qtd_kwu_nao`) `qtd_kwu_nao`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`vlrkwu`)      `vlrkwu`," & vbCrLf
		lstrSql = lstrSql & "          SUM(`vlr_kwu_nao`) `vlr_kwu_nao`" & vbCrLf
		lstrSql = lstrSql & "    FROM " & gstrDataBase & ".`" & istrTabela & "` " & vbCrLf
		lstrSql = lstrSql & "   WHERE `anomes` = " & VB6.Format(Me.dtpMesAno.value, "yyyyMM") & " " & vbCrLf
		lstrSql = lstrSql & "GROUP BY 1, 2 "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call Executar("START TRANSACTION", False,  , False, False)
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " Totalizando ")
					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"'
						llngAnoMes = IIf(IsDbNull(.Fields("anomes").value), 0, .Fields("anomes").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"'
						lstrDesClas = IIf(IsDbNull(.Fields("nmclasse").value), 0, .Fields("nmclasse").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"'
						ldblQtdkWh = IIf(IsDbNull(.Fields("qtd_kwh").value), 0, .Fields("qtd_kwh").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"'
						ldblVlrkWh = IIf(IsDbNull(.Fields("vlr_kwh").value), 0, .Fields("vlr_kwh").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"'
						ldblBankWh = IIf(IsDbNull(.Fields("ban_kwh").value), 0, .Fields("ban_kwh").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"'
						ldblQtdkW = IIf(IsDbNull(.Fields("qtd_kw").value), 0, .Fields("qtd_kw").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"'
						ldblVlrkW = IIf(IsDbNull(.Fields("vlrkw").value), 0, .Fields("vlrkw").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"'
						ldblQtdkWu = IIf(IsDbNull(.Fields("qtd_kwu").value), 0, .Fields("qtd_kwu").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"'
						ldblVlrkWu = IIf(IsDbNull(.Fields("vlrkwu").value), 0, .Fields("vlrkwu").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"'
						ldblQtdkWhNao = IIf(IsDbNull(.Fields("qtd_kwh_nao").value), 0, .Fields("qtd_kwh_nao").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"'
						ldblVlrkWhNao = IIf(IsDbNull(.Fields("vlr_kwh_nao").value), 0, .Fields("vlr_kwh_nao").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"'
						ldblBankWhNao = IIf(IsDbNull(.Fields("ban_kwh_nao").value), 0, .Fields("ban_kwh_nao").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"'
						ldblQtdkWNao = IIf(IsDbNull(.Fields("qtd_kw_nao").value), 0, .Fields("qtd_kw_nao").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"'
						ldblVlrkWNao = IIf(IsDbNull(.Fields("vlr_kw_nao").value), 0, .Fields("vlr_kw_nao").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"'
						ldblQtdkWuNao = IIf(IsDbNull(.Fields("qtd_kwu_nao").value), 0, .Fields("qtd_kwu_nao").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"'
						ldblVlrkWuNao = IIf(IsDbNull(.Fields("vlr_kwu_nao").value), 0, .Fields("vlr_kwu_nao").value)
						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO " & gstrDataBase & ".`" & istrTabela & "_total` ("
						lstrSql = lstrSql & "`anomes`,`nmclasse`,"
						lstrSql = lstrSql & "`qtd_kwh`,`qtd_kwh_nao`,`vlr_kwh`,`vlr_kwh_nao`,`ban_kwh`,`ban_kwh_nao`,"
						lstrSql = lstrSql & "`qtd_kw`,`qtd_kw_nao`,`vlrkw`,`vlr_kw_nao`,"
						lstrSql = lstrSql & "`qtd_kwu`,`qtd_kwu_nao`,`vlrkwu`,`vlr_kwu_nao`) VALUES ("
						lstrSql = lstrSql & llngAnoMes & ","
						lstrSql = lstrSql & "'" & lstrDesClas & "',"
						lstrSql = lstrSql & Replace(CStr(ldblQtdkWh), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblQtdkWhNao), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblVlrkWh), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblVlrkWhNao), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblBankWh), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblBankWhNao), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblQtdkW), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblQtdkWNao), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblVlrkW), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblVlrkWNao), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblQtdkWu), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblQtdkWuNao), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblVlrkWu), ",", ".") & ","
						lstrSql = lstrSql & Replace(CStr(ldblVlrkWuNao), ",", ".") & ")"
						Call Executar(lstrSql, False,  , False, False)
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Totalizando ")
					End While
				End If
				Call Executar("COMMIT", False,  , False, False)
				Call .Close()
			End If
		End With
ErroTotalizaRendaNaoFatura: 
		Me.Cursor = System.Windows.Forms.Cursors.Default
		If myConC.Errors.Count > 0 Then
			Call MsgBox("Código: " & myConC.Errors.Item(myConC.Errors.Count - 1).NativeError & vbCrLf & "Descrição:" & myConC.Errors.Item(myConC.Errors.Count - 1).Description, MsgBoxStyle.Critical, gstrTituloMensagem)
			Me.lblInfo.Text = "Erro no procedimento, entre em contato c/ Administrador"
			Call Executar("ROLLBACK", False)
		ElseIf Err.Number > 0 Then 
			Call MsgBox(Err.Number & " - " & GetErrosVB(Err.Number, Err.Description), MsgBoxStyle.Critical, gstrTituloMensagem)
			Me.lblInfo.Text = "Erro no procedimento, entre em contato c/ Administrador"
			Call Executar("ROLLBACK", False)
		Else
			Me.lblInfo.Text = Me.lblInfo.Text & " *** Finalizado **** "
			Call Me.lblInfo.Refresh()
		End If
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub CarregaClasses(ByRef alsvAuxiliar As System.Windows.Forms.ListView)
		Dim lstrNome As String
		Dim lstrZona As String
		With Me.lsvClasses
			Call .Items.Clear()
			If .Columns.Count <= 1 Then
				Call .Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(3100)))
				Call .Columns.Add("", "Condição1", CInt(VB6.TwipsToPixelsX(0)))
			End If
			Call .Items.Add("Marcar Todas")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '0000' AND '9999'")
			Call .Items.Add("Residencial")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = '9101'")
			Call .Items.Add("Baixa Renda")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '9102' AND '9106'")
			Call .Items.Add("Industrial")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '0000' AND '3999'")
			Call .Items.Add("Comercial")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '4000' AND '4999'")
			Call .Items.Add("Rural")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '5000' AND '5999'")
			Call .Items.Add("Poder Público Municipal")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '6100' AND '6199'")
			Call .Items.Add("Poder Público Estadual")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '6200' AND '6299'")
			Call .Items.Add("Poder Público Federal")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '6300' AND '6399'")
			Call .Items.Add("Iluminação Pública")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '6400' AND '6499'")
			Call .Items.Add("Serviço Púplico")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '7000' AND '7999'")
			Call .Items.Add("Consumo Próprio")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" BETWEEN '8000' AND '8999'")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			.Items.Item(1).Font = VB6.FontChangeBold(.Items.Item(1).Font, True)
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			.Items.Item(1).Checked = True
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call lsvClasses_ItemCheck(lsvClasses, New System.Windows.Forms.ItemCheckEventArgs(.Items.Item(1).Index, System.Windows.Forms.CheckState.Indeterminate, .Items.Item(1).Checked))
		End With
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub lsvClasses_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lsvClasses.ItemCheck
		Dim aItem As System.Windows.Forms.ListViewItem = lsvClasses.Items(eventArgs.Index)
		Dim lintIdx As Short
		With Me.lsvClasses
			If aItem.Text = "Marcar Todas" Then
				For lintIdx = 1 To .Items.Count
					'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(lintIdx).Checked = .Items.Item(1).Checked
				Next lintIdx
			Else
				If aItem.Checked = False Then
					'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(1).Checked = False
				End If
			End If
		End With
	End Sub
	
	Private Function MontaFiltroClasse() As String
		Dim lstrAux As String
		Dim lintIdx As Short
		lstrAux = ""
		With Me.lsvClasses
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			If .Items.Item(1).Checked = False Then
				For lintIdx = 2 To .Items.Count
					'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					If .Items.Item(lintIdx).Checked = True Then
						'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems.Item() has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						lstrAux = lstrAux & IIf(Len(Trim(lstrAux)) > 0, " OR ", "") & " `codclas` " & .Items.Item(lintIdx).SubItems(1).Text
					End If
				Next lintIdx
				lstrAux = " AND (" & lstrAux & ") "
			End If
		End With
		MontaFiltroClasse = lstrAux
	End Function
End Class