Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelMicroGeracao
	Inherits System.Windows.Forms.Form
	Dim llngkwhReverso_Sobra As Integer
	Dim lblnContinua As Boolean
	
	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 lstrCriterio As String
		lstrCriterio = "{temp_micro_geracao_saldos.anomes} = " & VB6.Format(Me.dtpMesAno.value, "yyyyMM") & " "
		With Me.crpAux
			.ReportFileName = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "RelMicroGeracao_saldo.rpt"
			.DiscardSavedData = True
			.WindowState = Crystal.WindowStateConstants.crptMaximized
			.WindowControlBox = True
			.WindowControls = True
			.WindowShowPrintSetupBtn = True
			.WindowShowCloseBtn = True
			.WindowShowSearchBtn = True
			.SelectionFormula = lstrCriterio
			.WindowTitle = "Relatório de Micro Geração - Saldos"
			.Destination = Crystal.DestinationConstants.crptToWindow
			.Action = 0
		End With
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean, ByVal ablnAux As Boolean)
		'
		Me.dtpMesAno.Enabled = ablnAux
		'
		Me.cmdProcessar.Enabled = IIf(ablnAux, ablnAux, ablnModo)
		Me.cmdImprimir.Enabled = IIf(ablnAux, ablnAux, ablnModo)
		Me.cmdFechar.Enabled = IIf(ablnAux, ablnAux, ablnModo)
		
	End Sub
	
	Private Sub ProcessarSaldos()
		Dim llngAnoMes As Integer
		Dim llngAnoMes_Base As Integer
		Dim lstrSql As String
		
		Dim lstrItensserv As String
		
		Dim llngTarifa As Integer
		Dim lstrClasse As String
		
		Dim llngQResiduo As Integer
		Dim ldubVResiduo As Double
		
		Dim llngQResiduo_temp As Integer
		Dim ldubVResiduo_temp As Double
		
		Dim llngTotQResiduo As Integer
		Dim ldubTotVResiduo As Double
		
		Dim ldubValorTarifa As Double
		
		
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Dim lrstAux1 As ADODB.Recordset
		lrstAux1 = New ADODB.Recordset
		
		On Error GoTo ErroProcessa
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call LigDes(False, False)
		
		lstrSql = "DELETE FROM temp_micro_geracao_saldos"
		Call Executar(lstrSql)
		
		llngAnoMes = CInt(VB6.Format(Me.dtpMesAno.value, "yyyyMM"))
		llngAnoMes = llngAnoMes + 1
		
		llngAnoMes_Base = CInt(VB6.Format(Me.dtpMesAno.value, "yyyyMM"))
		
		Me.lblInfo.Text = "Buscando os lançamentos, aguarde... "
		Call Me.lblInfo.Refresh()
		lstrSql = ""
		lstrSql = lstrSql & "SELECT nf.codclas, nf.itensserv"
		lstrSql = lstrSql & " FROM " & gstrDataBase & ".`nf` "
		lstrSql = lstrSql & " WHERE `mesano_ref`  = " & llngAnoMes & ""
		lstrSql = lstrSql & " AND `micro_geracao` = 1"
		lstrSql = lstrSql & " ORDER BY codclas"
		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, " lançamentos ")
					While Not .EOF
						
						'Pegando Valores
						llngQResiduo = 0
						llngQResiduo_temp = 0
						llngTotQResiduo = 0
						ldubVResiduo = 0
						ldubVResiduo_temp = 0
						ldubTotVResiduo = 0
						ldubValorTarifa = 0
						lstrItensserv = ""
						
						lstrClasse = Trim(IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) < 4, "Industrial", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 4, "Comercial", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 5, "Rural", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 7, "Serviço Publico", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 8, "Consumo Proprio", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 9, "Residencial", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 61, "Poder Público", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 62, "Poder Público", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 63, "Poder Público", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 64, "Iluminação Publica", "")))))))))))
						'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"'
						lstrItensserv = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
						
						llngQResiduo = BuscaVlrItem(lstrItensserv, "0302020997")
						
						If llngQResiduo > 0 Then
							
							If lstrClasse = "Residencial" Then
								ldubValorTarifa = BuscaVlrItem(lstrItensserv, "01B1011101")
							ElseIf lstrClasse = "Industrial" Then 
								ldubValorTarifa = BuscaVlrItem(lstrItensserv, "01B3101101")
							ElseIf lstrClasse = "Comercial" Then 
								ldubValorTarifa = BuscaVlrItem(lstrItensserv, "01B3301101")
							ElseIf lstrClasse = "Rural" Then 
								ldubValorTarifa = BuscaVlrItem(lstrItensserv, "01B2011101")
							ElseIf lstrClasse = "Poder Público" Then 
								ldubValorTarifa = BuscaVlrItem(lstrItensserv, "01B3501101")
							End If
							
							lstrSql = ""
							lstrSql = lstrSql & "SELECT * FROM temp_micro_geracao_saldos " & vbCrLf
							lstrSql = lstrSql & " WHERE anomes = " & llngAnoMes_Base & ""
							lstrSql = lstrSql & " AND classe = '" & lstrClasse & "'"
							Call Conectar(lstrSql, lrstAux1)
							With lrstAux1
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									If .RecordCount > 0 Then
										
										'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
										llngQResiduo_temp = CInt(Trim(IIf(IsDbNull(.Fields("Qtde_kWh").value), 0, .Fields("Qtde_kWh").value)))
										
										llngTotQResiduo = llngQResiduo + llngQResiduo_temp
										ldubTotVResiduo = llngTotQResiduo * ldubValorTarifa
										
										lstrSql = ""
										lstrSql = lstrSql & "UPDATE " & gstrDataBase & ".`temp_micro_geracao_saldos`"
										lstrSql = lstrSql & "  SET `classe`      =  '" & lstrClasse & "', "
										lstrSql = lstrSql & "  `anomes`          =  " & llngAnoMes_Base & ", "
										lstrSql = lstrSql & "  `qtde_kWh`   =  " & llngTotQResiduo & ", "
										lstrSql = lstrSql & "  `valor_kWh`   = " & Replace(CStr(ldubTotVResiduo), ",", ".") & "" & ", "
										lstrSql = lstrSql & "  `tarifa` = '" & ldubValorTarifa & "'"
										lstrSql = lstrSql & "  WHERE `classe`    = '" & lstrClasse & "'"
										lstrSql = lstrSql & "  AND `anomes`      = " & llngAnoMes_Base & ""
										Call Executar(lstrSql)
										
									Else
										
										llngTotQResiduo = llngQResiduo
										ldubTotVResiduo = llngTotQResiduo * ldubValorTarifa
										
										lstrSql = ""
										lstrSql = lstrSql & "INSERT INTO " & gstrDataBase & ".`temp_micro_geracao_saldos` SET "
										lstrSql = lstrSql & "  `classe`          =  '" & lstrClasse & "', "
										lstrSql = lstrSql & "  `anomes`          =  " & llngAnoMes_Base & ", "
										lstrSql = lstrSql & "  `qtde_kWh`   =  " & llngTotQResiduo & ", "
										lstrSql = lstrSql & "  `valor_kWh`   = " & Replace(CStr(ldubTotVResiduo), ",", ".") & "" & ", "
										lstrSql = lstrSql & "  `tarifa` = '" & ldubValorTarifa & "'"
										Call Executar(lstrSql)
										
									End If
									Call .Close()
								End If
							End With
							
						End If
						
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " lançamentos ")
						
					End While
					
				End If
				Call .Close()
				
			End If
		End With
		
ErroProcessa: 
		Me.Cursor = System.Windows.Forms.Cursors.Default
		If Err.Number > 0 Then
			Call MsgBox(Err.Number & " - " & GetErrosVB(Err.Number, Err.Description), MsgBoxStyle.Exclamation, gstrTituloMensagem)
			If lrstAux.State = ADODB.ObjectStateEnum.adStateOpen Then lrstAux.Close()
			If lrstAux1.State = ADODB.ObjectStateEnum.adStateOpen Then lrstAux1.Close()
		Else
			Me.lblInfo.Text = " Finalizando. "
			Call Me.lblInfo.Refresh()
			'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 lrstAux1 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"'
			lrstAux1 = Nothing
		End If
		Call LigDes(True, True)
	End Sub
	
	Private Sub Processar()
		Dim llngAnoMes As Integer
		Dim llngAnoMes_Anter As Integer
		Dim lstrSql As String
		
		Dim lstrItensserv As String
		Dim llngReverso_Anter As Integer
		Dim llngReverso_Atual As Integer
		Dim llngReverso_SalAnter As Integer
		Dim llngReverso_SalAtual As Integer
		Dim llngKwh_Reverso_Lid As Integer
		Dim llngKwh_Reverso_Fat As Integer
		Dim llngKwh_Reverso_Res As Integer
		Dim llngConsLido As Integer
		Dim lstrClasse As String
		Dim lstrData_Leit_Anter As String
		Dim lstrData_Leit_Atual As String
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim alngConsId As Integer
		Dim alngUnidId As Integer
		
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Dim lrstAux1 As ADODB.Recordset
		lrstAux1 = New ADODB.Recordset
		Dim lrstAux2 As ADODB.Recordset
		lrstAux2 = New ADODB.Recordset
		On Error GoTo ErroProcessa
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call LigDes(False, False)
		
		lstrSql = "DELETE FROM temp_micro_geracao"
		Call Executar(lstrSql)
		
		llngAnoMes = CInt(VB6.Format(Me.dtpMesAno.value, "yyyyMM"))
		Me.lblInfo.Text = "Buscando os lançamentos, aguarde... "
		Call Me.lblInfo.Refresh()
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & " FROM " & gstrDataBase & ".`nf` "
		lstrSql = lstrSql & " WHERE `mesano_ref`  = " & llngAnoMes & ""
		lstrSql = lstrSql & " AND `micro_geracao` = 1"
		lstrSql = lstrSql & " ORDER BY numnf"
		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, " lançamentos ")
					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"'
						lstrZona = Trim(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 = Trim(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 = Trim(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"'
						alngConsId = CInt(Trim(IIf(IsDbNull(.Fields("cons_id").value), "", .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"'
						alngUnidId = CInt(Trim(IIf(IsDbNull(.Fields("unid_id").value), "", .Fields("unid_id").value)))
						
						'---Pegando Saldo Anterior --------------------------------------------------------------------------------
						
						llngAnoMes_Anter = llngAnoMes - 1
						
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & " FROM " & gstrDataBase & ".`nf` "
						lstrSql = lstrSql & " WHERE `mesano_ref`  = " & llngAnoMes_Anter & ""
						lstrSql = lstrSql & " AND `micro_geracao` = 1"
						lstrSql = lstrSql & " AND `zona`          = '" & lstrZona & "'"
						lstrSql = lstrSql & " AND `livro`         = '" & lstrLivro & "'"
						lstrSql = lstrSql & " AND `conta`         = '" & lstrConta & "'"
						lstrSql = lstrSql & " AND `cons_id`       = " & alngConsId & ""
						lstrSql = lstrSql & " AND `unid_id`       = " & alngUnidId & ""
						Call Conectar(lstrSql, lrstAux1)
						With lrstAux1
							If .State = ADODB.ObjectStateEnum.adStateOpen Then
								If .RecordCount > 0 Then
									
									'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
									lstrItensserv = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
									
									'Reverso
									llngKwh_Reverso_Lid = BuscaVlrItem(lstrItensserv, "302020996")
									llngKwh_Reverso_Fat = BuscaVlrItem(lstrItensserv, "302020995")
									llngKwh_Reverso_Res = BuscaVlrItem(lstrItensserv, "302020997")
									
									'Consumo
									llngConsLido = BuscaVlrItem(lstrItensserv, "30201008")
									
									If llngConsLido > (llngKwh_Reverso_Lid + llngKwh_Reverso_Res) Then
										llngReverso_SalAnter = 0
									Else
										llngReverso_SalAnter = llngKwh_Reverso_Lid + (llngKwh_Reverso_Res - llngConsLido)
									End If
									
								End If
								Call .Close()
							End If
						End With
						'---------------------------------------------------------------------------------------------------------
						
						'Pegando Valores e Saldo Atual
						
						llngReverso_Anter = 0
						llngReverso_Atual = 0
						llngKwh_Reverso_Lid = 0
						llngKwh_Reverso_Fat = 0
						llngKwh_Reverso_Res = 0
						llngConsLido = 0
						lstrItensserv = ""
						
						lstrClasse = Trim(IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) < 4, "Industrial", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 4, "Comercial", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 5, "Rural", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 7, "Serviço Publico", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 8, "Consumo Proprio", IIf(CDbl(VB.Left(.Fields("codclas").value, 1)) = 9, "Residencial", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 61, "Poder Pub. Municipal", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 62, "Poder Pub. Estadual", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 63, "Poder Pub. Federal", IIf(CDbl(VB.Left(.Fields("codclas").value, 2)) = 64, "Iluminação Publica", "")))))))))))
						'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"'
						lstrItensserv = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
						
						'Reverso
						llngReverso_Anter = BuscaVlrItem(lstrItensserv, "302990050")
						llngReverso_Atual = BuscaVlrItem(lstrItensserv, "302990049")
						llngKwh_Reverso_Lid = BuscaVlrItem(lstrItensserv, "302020996")
						llngKwh_Reverso_Fat = BuscaVlrItem(lstrItensserv, "302020995")
						llngKwh_Reverso_Res = BuscaVlrItem(lstrItensserv, "302020997")
						
						'Consumo
						llngConsLido = BuscaVlrItem(lstrItensserv, "30201008")
						'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"'
						lstrData_Leit_Anter = VB6.Format(IIf(IsDbNull(.Fields("dataleitant").value), "", .Fields("dataleitant").value), "yyyy-mm-dd")
						'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"'
						lstrData_Leit_Atual = VB6.Format(IIf(IsDbNull(.Fields("dataleit").value), "", .Fields("dataleit").value), "yyyy-mm-dd")
						
						If llngConsLido > (llngKwh_Reverso_Lid + llngKwh_Reverso_Res) Then
							llngReverso_SalAtual = 0
						Else
							llngReverso_SalAtual = llngKwh_Reverso_Lid + (llngKwh_Reverso_Res - llngConsLido)
						End If
						
						Call ValidadeReverso(lstrZona, lstrLivro, lstrConta, alngConsId, alngUnidId, llngAnoMes, llngReverso_Anter, llngReverso_Atual, llngConsLido, llngKwh_Reverso_Lid, llngKwh_Reverso_Res, lstrData_Leit_Anter, lstrData_Leit_Atual)
						
						If lblnContinua = False Then GoTo pula
						
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & " FROM " & gstrDataBase & ".`temp_micro_geracao` "
						lstrSql = lstrSql & " WHERE `classe` = '" & lstrClasse & "'"
						lstrSql = lstrSql & " AND `anomes`   = " & llngAnoMes & ""
						Call Conectar(lstrSql, lrstAux2)
						With lrstAux2
							If .State = ADODB.ObjectStateEnum.adStateOpen Then
								If .RecordCount > 0 Then
									llngReverso_SalAnter = llngReverso_SalAnter + .Fields("Reverso_Anter").value
									llngReverso_SalAtual = llngReverso_SalAtual + .Fields("Reverso_Atual").value
									llngKwh_Reverso_Lid = llngKwh_Reverso_Lid + .Fields("Kwh_Reverso_Lid").value
									llngKwh_Reverso_Fat = llngKwh_Reverso_Fat + .Fields("Kwh_Reverso_Fat").value
									
									lstrSql = ""
									lstrSql = lstrSql & "UPDATE " & gstrDataBase & ".`temp_micro_geracao`"
									lstrSql = lstrSql & "  SET `classe`      =  '" & lstrClasse & "', "
									lstrSql = lstrSql & "  `dataleit`        =  '" & lstrData_Leit_Atual & "', "
									lstrSql = lstrSql & "  `anomes`          =  " & llngAnoMes & ", "
									lstrSql = lstrSql & "  `Reverso_Anter`   =  " & llngReverso_SalAnter & ", "
									lstrSql = lstrSql & "  `Reverso_Atual`   = " & llngReverso_SalAtual & ", "
									lstrSql = lstrSql & "  `Kwh_Reverso_Lid` = " & llngKwh_Reverso_Lid & ", "
									lstrSql = lstrSql & "  `Kwh_Reverso_Fat` = " & llngKwh_Reverso_Fat & ", "
									lstrSql = lstrSql & "  `Kwh_Reverso_Res` = " & llngKwh_Reverso_Res & ", "
									lstrSql = lstrSql & "  `ConsLido`        = " & llngConsLido & ""
									lstrSql = lstrSql & "  WHERE `classe`    = '" & lstrClasse & "'"
									lstrSql = lstrSql & "  AND `anomes`      = " & llngAnoMes & ""
									Call Executar(lstrSql)
								Else
									lstrSql = ""
									lstrSql = lstrSql & "INSERT INTO " & gstrDataBase & ".`temp_micro_geracao` SET "
									lstrSql = lstrSql & "  `classe`          =  '" & lstrClasse & "', "
									lstrSql = lstrSql & "  `dataleit`        =  '" & lstrData_Leit_Atual & "', "
									lstrSql = lstrSql & "  `anomes`          =  " & llngAnoMes & ", "
									lstrSql = lstrSql & "  `Reverso_Anter`   =  " & llngReverso_SalAnter & ", "
									lstrSql = lstrSql & "  `Reverso_Atual`   = " & llngReverso_SalAtual & ", "
									lstrSql = lstrSql & "  `Kwh_Reverso_Lid` = " & llngKwh_Reverso_Lid & ", "
									lstrSql = lstrSql & "  `Kwh_Reverso_Fat` = " & llngKwh_Reverso_Fat & ", "
									lstrSql = lstrSql & "  `Kwh_Reverso_Res` = " & llngKwh_Reverso_Res & ", "
									lstrSql = lstrSql & "  `ConsLido`        = " & llngConsLido & ""
									Call Executar(lstrSql)
								End If
								Call .Close()
							End If
						End With
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " lançamentos ")
					End While
				End If
pula: 
				Call .Close()
			End If
		End With
		
ErroProcessa: 
		Me.Cursor = System.Windows.Forms.Cursors.Default
		If Err.Number > 0 Then
			Call MsgBox(Err.Number & " - " & GetErrosVB(Err.Number, Err.Description), MsgBoxStyle.Exclamation, gstrTituloMensagem)
			If lrstAux.State = ADODB.ObjectStateEnum.adStateOpen Then lrstAux.Close()
			If lrstAux1.State = ADODB.ObjectStateEnum.adStateOpen Then lrstAux1.Close()
			If lrstAux2.State = ADODB.ObjectStateEnum.adStateOpen Then lrstAux2.Close()
		Else
			Me.lblInfo.Text = " Finalizando. "
			Call Me.lblInfo.Refresh()
			'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 lrstAux1 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"'
			lrstAux1 = Nothing
			'UPGRADE_NOTE: Object lrstAux2 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"'
			lrstAux2 = Nothing
		End If
		Call LigDes(True, True)
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		
		''Call Processar
		Call ProcessarSaldos()
		
	End Sub
	
	Private Sub frmRelMicroGeracao_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		Me.dtpMesAno.value = BuscaDataBD
		Call LigDes(False, True)
		dtpMesAno.value = Today
		
	End Sub
	Private Sub ValidadeReverso(ByVal lstrZona As String, ByVal lstrLivro As String, ByVal lstrConta As String, ByVal alngConsId As Integer, ByVal alngUnidId As Integer, ByVal llngAnoMes As Integer, ByVal llngReverso_Anter As Integer, ByVal llngReverso_Atual As Integer, ByVal llngConsLido As Integer, ByVal llngKwh_Reverso_Lid As Integer, ByVal llngKwh_Reverso_Res As Byte, ByRef lstrData_Leit_Anter As String, ByVal lstrData_Leit_Atual As String)
		
		Dim lstrSql As String
		Dim lstrDataValidade As String
		
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		llngkwhReverso_Sobra = 0
		lblnContinua = True
		
		llngkwhReverso_Sobra = llngConsLido - (llngKwh_Reverso_Lid + llngKwh_Reverso_Res)
		lstrDataValidade = IIf(llngkwhReverso_Sobra = 0, " ", VB6.Format(DateAdd(Microsoft.VisualBasic.DateInterval.Day, 60, CDate(lstrData_Leit_Atual)), "yyyy-mm-dd"))
		
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM " & gstrDataBase & ".`temp_valida_reverso` "
		lstrSql = lstrSql & " WHERE `anomes` >= " & llngAnoMes & " "
		lstrSql = lstrSql & "   AND `cons_id` = " & alngConsId & " "
		lstrSql = lstrSql & "   AND `unid_id` = " & alngUnidId & " "
		lstrSql = lstrSql & "   AND `zona`    = '" & lstrZona & "' "
		lstrSql = lstrSql & "   AND `livro`   = '" & lstrLivro & "' "
		lstrSql = lstrSql & "   AND `conta`   = '" & lstrConta & "' "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .RecordCount = 0 Then
				
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO " & gstrDataBase & ".`temp_valida_reverso` SET "
				lstrSql = lstrSql & "  `zona`           =  '" & lstrZona & "', "
				lstrSql = lstrSql & "  `livro`          =  '" & lstrLivro & "', "
				lstrSql = lstrSql & "  `conta`          = '" & lstrConta & "', "
				lstrSql = lstrSql & "  `cons_id`        =  " & alngConsId & ", "
				lstrSql = lstrSql & "  `unid_id`        =  " & alngUnidId & ", "
				lstrSql = lstrSql & "  `anomes`         = " & llngAnoMes & ","
				lstrSql = lstrSql & "  `reverso_anter`  = " & llngReverso_Anter & ","
				lstrSql = lstrSql & "  `reverso_atual`  = " & llngReverso_Atual & ","
				lstrSql = lstrSql & "  `reverso_lido`   = " & llngKwh_Reverso_Lid & ", "
				lstrSql = lstrSql & "  `data_leitanter` = '" & lstrData_Leit_Anter & "',"
				lstrSql = lstrSql & "  `data_leitatual` = '" & lstrData_Leit_Atual & "',"
				lstrSql = lstrSql & "  `data_validade`  = '" & lstrDataValidade & "',"
				lstrSql = lstrSql & "  `reverso_sobra`  = " & llngkwhReverso_Sobra & ""
				Call Executar(lstrSql)
				
			Else
				
				lblnContinua = MsgBox("Existe dados de meses superiores, se continuar, serão apagados e será necessario reprocessar mês a mês novamente !" & vbCrLf & "Deseja continuar mesmo assim?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton1, gstrTituloMensagem) = MsgBoxResult.Yes
				
				If lblnContinua Then
					lstrSql = ""
					lstrSql = lstrSql & "DELETE FROM temp_valida_reverso "
					lstrSql = lstrSql & " WHERE `anomes`  >= " & llngAnoMes & ""
					Call Executar(lstrSql)
					lblnContinua = False
				End If
			End If
			If .State = 1 Then Call .Close()
		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
End Class