Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelTarifaAjuste
	Inherits System.Windows.Forms.Form
	Private istrExtensaoArq As String
	Private istrNmArq As String
	Private istrPath As String
	Private idteBas As Date
	Private iintMesIniPerSeco As Short
	Private iintMesFimPerSeco As Short
	Private ifrAuxiliar As FrmRelatorio
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Call LigDes(False)
		If VerificaExisteRegistro("fat_tar_ajuste", "1=1 LIMIT 0,1") Then
			If MsgBox("Deseja processar novamente?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, gstrTituloMensagem) = MsgBoxResult.Yes Then
				Call Processa()
			End If
		Else
			Call Processa()
		End If
		Call ExportaCSV()
		Call LigDes(True)
	End Sub
	
	Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Call LigDes(False)
		If VerificaExisteRegistro("fat_tar_ajuste", "1=1 LIMIT 0,1") Then
			If MsgBox("Deseja processar novamente?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, gstrTituloMensagem) = MsgBoxResult.Yes Then
				Call Processa()
			End If
		Else
			Call Processa()
		End If
		Call Imprimir()
		Call LigDes(True)
	End Sub
	
	Private Sub frmRelTarifaAjuste_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Call CenterForm(Me)
		idteBas = BuscaDataBD
		iintMesIniPerSeco = Val(LeConfig("MES_INI_PERIODO_SECO"))
		iintMesFimPerSeco = iintMesIniPerSeco + 6
		'
		lstrSql = "SELECT DISTINCT `referencia_ini` FROM `tarifa_ajustes` "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			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"'
					Me.dtpTarifaAjuste.value = IIf(IsDbNull(.Fields("referencia_ini").value), idteBas, .Fields("referencia_ini").value)
				Else
					Me.dtpTarifaAjuste.value = idteBas
				End If
				Call .Close()
			End If
		End With
		'
		Me.txtArquivo.Text = My.Application.Info.DirectoryPath & "\" & istrNmArq & "." & Me.dialArqOpen.DefaultExt
		Call NomeArquivo()
		'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
		ifrAuxiliar = New FrmRelatorio
	End Sub
	
	Private Sub frmRelTarifaAjuste_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 cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub Processa()
		Dim ldblNumNF As Double
		Dim lstrMesAno As String
		Dim llngAnoMes As Integer
		Dim lstrCodClaX As String
		Dim lstrCodClas As String
		Dim lstrDesClas As String
		Dim lstrItensNf As String
		Dim lstrGrupo As String
		Dim lstrGrTar As String
		Dim lstrGrTaI As String
		Dim ldteLeiAt As Date
		Dim ldteLeiAn As Date
		Dim ldteLeiAtX As Date
		Dim ldteLeiAnX As Date
		Dim lintLivEsp As Short
		Dim lintParCat As Short
		Dim lintLivVer As Short
		Dim lintIrrAqu As Short
		'
		Dim ldblQtdkWhp As Double
		Dim ldblQtdKwhF As Double
		Dim ldblQtLKwhP As Double
		Dim ldblQtLKwhF As Double
		Dim ldblQtdKwhI As Double
		Dim ldblQtdkWh1 As Double
		Dim ldblQtdkWh2 As Double
		Dim ldblQtdkWh3 As Double
		Dim ldblQtdkWh4 As Double
		
		Dim ldblVlrkWhp As Double
		Dim ldblVlrKwhF As Double
		Dim ldblVlLKwhP As Double
		Dim ldblVlLKwhF As Double
		Dim ldblVlrkWhI As Double
		Dim ldblVlrkWh1 As Double
		Dim ldblVlrkWh2 As Double
		Dim ldblVlrkWh3 As Double
		Dim ldblVlrkWh4 As Double
		
		Dim ldblTarKwhP As Double
		Dim ldblTarKwhF As Double
		Dim ldblTaLKwhP As Double
		Dim ldblTaLKwhF As Double
		Dim ldblTarKwhI As Double
		Dim ldblTarKwh1 As Double
		Dim ldblTarKwh2 As Double
		Dim ldblTarKwh3 As Double
		Dim ldblTarKwh4 As Double
		
		Dim ldblVlsKwhP As Double
		Dim ldblVlsKwhF As Double
		Dim ldblVsLKwhP As Double
		Dim ldblVsLKwhF As Double
		Dim ldblVlsKwhI As Double
		Dim ldblVlsKwh1 As Double
		Dim ldblVlsKwh2 As Double
		Dim ldblVlsKwh3 As Double
		Dim ldblVlsKwh4 As Double
		
		Dim ldblDifKwhP As Double
		Dim ldblDifKwhF As Double
		Dim ldblDiLKwhP As Double
		Dim ldblDiLKwhF As Double
		Dim ldblDifKwhI As Double
		Dim ldblDifKwh1 As Double
		Dim ldblDifKwh2 As Double
		Dim ldblDifKwh3 As Double
		Dim ldblDifKwh4 As Double
		
		Dim lstrClaAjus As String
		Dim lstrGruAjus As String
		Dim ldblTarAjus As Double
		Dim ldblVlrAjuP As Double
		Dim ldblVlrAjuF As Double
		Dim ldblVlrAju1 As Double
		Dim ldblVlrAju2 As Double
		Dim ldblVlrAju3 As Double
		Dim ldblVlrAju4 As Double
		
		Dim lstrClaAjuX As String
		Dim lstrGruAjuX As String
		
		Dim lblnPeriodo As Boolean
		Dim ldteRef As Date
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Me.lblInfo.Text = " Buscando os dados de faturamento, aguarde..."
		Call Me.lblInfo.Refresh()
		Call myConC.Execute("DELETE FROM fat_tar_ajuste")
		lstrSql = "SELECT "
		lstrSql = lstrSql & " `nf`.`numnf`, "
		lstrSql = lstrSql & " `nf`.`mesano`, "
		lstrSql = lstrSql & " `nf`.`mesano_ref`, "
		lstrSql = lstrSql & " `nf`.`codclas`, "
		lstrSql = lstrSql & " `classe`.`descricao` `classe_descricao`, "
		lstrSql = lstrSql & " `nf`.`grupo`, "
		lstrSql = lstrSql & " `nf`.`ta`, "
		lstrSql = lstrSql & " `nf`.`itensserv`, "
		lstrSql = lstrSql & " `nf`.`dataleit`, "
		lstrSql = lstrSql & " `nf`.`dataleitant`, "
		lstrSql = lstrSql & " `nf`.`livreespecial`, "
		lstrSql = lstrSql & " `nf`.`parcialmente_cativo`, "
		lstrSql = lstrSql & " `nf`.`livre_verde`, "
		lstrSql = lstrSql & " `nf`.`irrigacao_aquicultura` "
		lstrSql = lstrSql & "  FROM `nf` LEFT JOIN `classe` ON(`classe`.`codigo` = `nf`.`codclas`) "
		lstrSql = lstrSql & " WHERE `dataleitant` <= '" & VB6.Format(Me.dtpTarifaAjuste.value, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "   AND `dataleit` > '" & VB6.Format(Me.dtpTarifaAjuste.value, "yyyy-MM-dd") & "' "
		'lstrSql = lstrSql & "   AND `grupo` = 'A' " 'Temporario*******************************************
		'lstrSql = lstrSql & "   AND parcialmente_cativo = 1 "
		lstrSql = lstrSql & " ORDER BY `grupo` ASC, `codclas`+0 ASC, `mesano_ref` ASC, `numnf` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					lstrGruAjuX = ""
					lstrClaAjuX = ""
					lstrCodClaX = ""
					ldteLeiAtX = CDate("01/01/1900")
					ldteLeiAnX = CDate("01/01/1900")
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
					While Not .EOF
						'limpeza de variáveis evita sujeira nos dados
						ldblQtdkWhp = 0
						ldblQtdKwhF = 0
						ldblQtLKwhP = 0
						ldblQtLKwhF = 0
						ldblQtdKwhI = 0
						ldblQtdkWh1 = 0
						ldblQtdkWh2 = 0
						ldblQtdkWh3 = 0
						ldblQtdkWh4 = 0
						ldblVlrkWhp = 0
						ldblVlrKwhF = 0
						ldblVlLKwhP = 0
						ldblVlLKwhF = 0
						ldblVlrkWhI = 0
						ldblVlrkWh1 = 0
						ldblVlrkWh2 = 0
						ldblVlrkWh3 = 0
						ldblVlrkWh4 = 0
						ldblVlsKwhP = 0
						ldblVlsKwhF = 0
						ldblVsLKwhP = 0
						ldblVsLKwhF = 0
						ldblVlsKwhI = 0
						ldblVlsKwh1 = 0
						ldblVlsKwh2 = 0
						ldblVlsKwh3 = 0
						ldblVlsKwh4 = 0
						ldblDifKwhP = 0
						ldblDifKwhF = 0
						ldblDiLKwhP = 0
						ldblDiLKwhF = 0
						ldblDifKwhI = 0
						ldblDifKwh1 = 0
						ldblDifKwh2 = 0
						ldblDifKwh3 = 0
						ldblDifKwh4 = 0
						lstrClaAjus = CStr(0)
						lstrGruAjus = CStr(0)
						ldblVlrAjuP = 0
						ldblVlrAjuF = 0
						ldblVlrAju1 = 0
						ldblVlrAju2 = 0
						ldblVlrAju3 = 0
						ldblVlrAju4 = 0
						'
						'dados fatura
						'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"'
						lstrMesAno = IIf(IsDbNull(.Fields("mesano").value), "", .Fields("mesano").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"'
						llngAnoMes = IIf(IsDbNull(.Fields("mesano_ref").value), 0, .Fields("mesano_ref").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), "", .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"'
						lstrGrTar = 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"'
						lstrCodClas = IIf(IsDbNull(.Fields("codclas").value), "", .Fields("codclas").value)
						Select Case VB.Left(CStr(Int(CDbl(lstrCodClas))), 1)
							Case Is < CStr(4)
								lstrDesClas = "Industrial"
							Case Is = CStr(4)
								lstrDesClas = "Comercial"
							Case Is = CStr(5)
								lstrDesClas = "Rural"
							Case Is = CStr(6)
								'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("classe_descricao").value), "", .Fields("classe_descricao").value)
							Case Is = CStr(7)
								lstrDesClas = "Serviço Público"
							Case Is = CStr(8)
								lstrDesClas = "Consumo Próprio"
							Case Is = CStr(9)
								'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("classe_descricao").value), "", .Fields("classe_descricao").value)
						End Select
						'
						'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"'
						lstrItensNf = 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"'
						ldteLeiAt = 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"'
						ldteLeiAn = IIf(IsDbNull(.Fields("dataleitant").value), "1900-01-01", .Fields("dataleitant").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"'
						lintLivEsp = IIf(IsDbNull(.Fields("livreespecial").value), 0, .Fields("livreespecial").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"'
						lintParCat = CShort(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"'
						lintLivVer = IIf(IsDbNull(.Fields("livre_verde").value), 0, .Fields("livre_verde").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"'
						lintIrrAqu = IIf(IsDbNull(.Fields("irrigacao_aquicultura").value), 0, .Fields("irrigacao_aquicultura").value)
						If lstrGrupo = "A" Then
							If lstrGrTar = "AL" Then
								lstrGrTaI = "A4 - Fonte Incentivada"
							Else
								lstrGrTaI = "A4"
							End If
						Else
							If VB.Left(CStr(Int(CDbl(lstrCodClas))), 1) = "5" Then
								lstrGrTaI = "B2"
							ElseIf lstrCodClas = "9105" Then 
								lstrGrTaI = "B1 - Baixa Renda"
							ElseIf lstrCodClas = "9101" Then 
								lstrGrTaI = "B1"
							Else
								lstrGrTaI = "B3"
								If lstrCodClas = "6401" Then
									lstrGrTaI = "B4a"
								ElseIf lstrCodClas = "6402" Then 
									lstrGrTaI = "B4b"
								End If
							End If
						End If
						'
						'Buscar a tarifa proporcional sem o desconto da angra III
						If lstrGrupo = "B" Then
							If lstrCodClas <> lstrCodClaX Or (ldteLeiAt <> ldteLeiAtX And ldteLeiAn <> ldteLeiAnX) Then
								If VB.Left(lstrCodClas, 4) = "9101" Then
									ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B1011100' AND '01B1011199') ")
								ElseIf VB.Left(lstrCodClas, 1) = "9" And VB.Right(lstrCodClas, 1) <> "1" Then 
									ldblTarKwh1 = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01B1021101') ")
									ldblTarKwh2 = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01B1021102') ")
									ldblTarKwh3 = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01B1021103') ")
									ldblTarKwh4 = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01B1021104') ")
								ElseIf VB.Left(lstrCodClas, 1) = "5" Then 
									ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B2011100' AND '01B2011199') ")
									If lintIrrAqu = 1 Then
										ldblTarKwhI = Arredondar(ldblTarKwhF * (60 / 100), 5)
									End If
								ElseIf VB.Left(lstrCodClas, 2) = "64" Then 
									If CDbl(VB.Right(lstrCodClas, 1)) = 2 Then
										ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B4021100' AND '01B4021199') ")
									Else
										ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B4011100' AND '01B4011199') ")
									End If
								Else
									If CDbl(VB.Left(lstrCodClas, 1)) < 4 Then
										ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B3101100' AND '01B3101199') ")
									ElseIf CDbl(VB.Left(lstrCodClas, 1)) = 7 Then 
										ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B3201100' AND '01B3201199') ")
										ldblTarKwhF = Arredondar(ldblTarKwhF - (ldblTarKwhF * (15 / 100)), 5)
									ElseIf CDbl(VB.Left(lstrCodClas, 1)) = 4 Then 
										ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B3301100' AND '01B3301199') ")
									ElseIf CDbl(VB.Left(lstrCodClas, 1)) = 8 Then 
										ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B3401100' AND '01B3401199') ")
									ElseIf CDbl(VB.Left(lstrCodClas, 1)) = 6 Then 
										ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` BETWEEN '01B3501100' AND '01B3501199') ")
									End If
								End If
								lstrCodClaX = lstrCodClas
								ldteLeiAtX = ldteLeiAt
								ldteLeiAnX = ldteLeiAn
							End If
							ldblTarKwhP = 0
							ldblTaLKwhP = 0
							ldblTaLKwhF = 0
						Else
							'Periodo seco / úmido
							lblnPeriodo = (CShort(Month(ldteLeiAt)) >= iintMesIniPerSeco And CShort(Month(ldteLeiAt)) <= iintMesFimPerSeco)
							'
							'Tarifa Ponta
							If lintParCat = 1 Then
								ldblTarKwhP = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01AA" & IIf(lblnPeriodo, "011101", "011105") & "') ")
								'Parcialmente Livre
								ldblTaLKwhP = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01" & lstrGrTar & IIf(lblnPeriodo, "011101", "011105") & "') ")
							Else
								ldblTarKwhP = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01" & lstrGrTar & IIf(lblnPeriodo, "011101", "011105") & "') ")
								If lintLivVer = 1 And lstrGrTar = "AL" Then
									ldblTarKwhP = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01AV" & IIf(lblnPeriodo, "011201", "011205") & "') ")
								End If
								If CDbl(VB.Left(lstrCodClas, 1)) = 7 Then
									ldblTarKwhP = Arredondar(ldblTarKwhP - (ldblTarKwhP * (15 / 100)), 5)
								End If
							End If
							'
							'Tarifa Fora de Ponta
							If lintParCat = 1 Then
								ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01AA" & IIf(lblnPeriodo, "011102", "011106") & "') ")
								'Parcilamente Livre
								ldblTaLKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01" & lstrGrTar & IIf(lblnPeriodo, "011102", "011106") & "') ")
							Else
								ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01" & lstrGrTar & IIf(lblnPeriodo, "011102", "011106") & "') ")
								If lintLivVer = 1 And lstrGrTar = "AL" Then
									ldblTarKwhF = GetTarProp(ldteLeiAt, ldteLeiAt, ldteLeiAn, "(`num` = '01AV" & IIf(lblnPeriodo, "011202", "011206") & "') ")
								End If
								If CDbl(VB.Left(lstrCodClas, 1)) = 7 Then
									ldblTarKwhF = Arredondar(ldblTarKwhF - (ldblTarKwhF * (15 / 100)), 5)
								End If
							End If
							ldblTarKwhI = 0
							ldblTarKwh1 = 0
							ldblTarKwh2 = 0
							ldblTarKwh3 = 0
							ldblTarKwh4 = 0
						End If
						'
						'Consumos
						If lstrGrupo = "B" Then
							If lstrCodClas = "9105" Then
								ldblQtdkWh1 = Arredondar(BuscaValorItem(lstrItensNf, "0302010090"), 2)
								ldblQtdkWh2 = Arredondar(BuscaValorItem(lstrItensNf, "0302010091"), 2)
								ldblQtdkWh3 = Arredondar(BuscaValorItem(lstrItensNf, "0302010092"), 2)
								ldblQtdkWh4 = Arredondar(BuscaValorItem(lstrItensNf, "0302010093"), 2)
								'
								ldblVlrkWh1 = BuscaValorItem(lstrItensNf, "0302020090") 'valor aplicado
								ldblVlrkWh2 = BuscaValorItem(lstrItensNf, "0302020091") 'valor aplicado
								ldblVlrkWh3 = BuscaValorItem(lstrItensNf, "0302020092") 'valor aplicado
								ldblVlrkWh4 = BuscaValorItem(lstrItensNf, "0302020093") 'valor aplicado
								'
								ldblVlsKwh1 = Truncate(ldblQtdkWh1 * ldblTarKwh1, 2) 'sem tarifa ajuste
								ldblVlsKwh2 = Truncate(ldblQtdkWh2 * ldblTarKwh2, 2) 'sem tarifa ajuste
								ldblVlsKwh3 = Truncate(ldblQtdkWh3 * ldblTarKwh3, 2) 'sem tarifa ajuste
								ldblVlsKwh4 = Truncate(ldblQtdkWh4 * ldblTarKwh4, 2) 'sem tarifa ajuste
								'
								ldblDifKwh1 = ldblVlrkWh1 - ldblVlsKwh1 ' diferença tarifa ajuste - Item 2
								ldblDifKwh2 = ldblVlrkWh2 - ldblVlsKwh2 ' diferença tarifa ajuste - Item 2
								ldblDifKwh3 = ldblVlrkWh3 - ldblVlsKwh3 ' diferença tarifa ajuste - Item 2
								ldblDifKwh4 = ldblVlrkWh4 - ldblVlsKwh4 ' diferença tarifa ajuste - Item 2
							Else
								ldblQtdKwhF = Arredondar(BuscaValorItem(lstrItensNf, "030201009"), 2)
								ldblQtdKwhI = Arredondar(BuscaValorItem(lstrItensNf, "0302990053"), 2)
								ldblVlrKwhF = BuscaValorItem(lstrItensNf, "030202009") 'valor aplicado
								ldblVlrkWhI = BuscaValorItem(lstrItensNf, "9070000009") 'valor aplicado
								ldblVlsKwhF = Truncate(ldblQtdKwhF * ldblTarKwhF, 2) 'sem tarifa ajuste
								ldblVlsKwhI = Truncate(ldblQtdKwhI * ldblTarKwhI, 2) 'sem tarifa ajuste
								ldblDifKwhF = ldblVlrKwhF - ldblVlsKwhF ' diferença tarifa ajuste - Item 2
								ldblDifKwhI = ldblVlrkWhI - ldblVlsKwhI ' diferença tarifa ajuste - Item 2
							End If
						Else
							If lintParCat = 1 Then
								ldblQtLKwhP = Arredondar(BuscaValorItem(lstrItensNf, "0302010060"), 2)
								ldblQtLKwhF = Arredondar(BuscaValorItem(lstrItensNf, "0302010061"), 2)
								ldblQtdkWhp = Arredondar(BuscaValorItem(lstrItensNf, "0302010010") - ldblQtLKwhP, 2)
								ldblQtdKwhF = Arredondar(BuscaValorItem(lstrItensNf, "0302010011") - ldblQtLKwhF, 2)
							Else
								ldblQtdkWhp = Arredondar(BuscaValorItem(lstrItensNf, "0302010010"), 2)
								ldblQtdKwhF = Arredondar(BuscaValorItem(lstrItensNf, "0302010011"), 2)
							End If
							ldblVlrkWhp = BuscaValorItem(lstrItensNf, "0302020010") 'valor aplicado
							ldblVlrKwhF = BuscaValorItem(lstrItensNf, "0302020011") 'valor aplicado
							'Parcialmente Livre
							ldblVlLKwhP = BuscaValorItem(lstrItensNf, "0302020060") 'valor aplicado
							ldblVlLKwhF = BuscaValorItem(lstrItensNf, "0302020061") 'valor aplicado
							'
							ldblVlsKwhP = Truncate(ldblQtdkWhp * ldblTarKwhP, 2) 'sem tarifa ajuste
							ldblVlsKwhF = Truncate(ldblQtdKwhF * ldblTarKwhF, 2) 'sem tarifa ajuste
							'Parcialmente Livre
							ldblVsLKwhP = Truncate(ldblQtLKwhP * ldblTaLKwhP, 2) 'sem tarifa ajuste
							ldblVsLKwhF = Truncate(ldblQtLKwhF * ldblTaLKwhF, 2) 'sem tarifa ajuste
							'
							ldblDifKwhP = ldblVlrkWhp - ldblVlsKwhP ' diferença tarifa ajuste - Item 2
							ldblDifKwhF = ldblVlrKwhF - ldblVlsKwhF ' diferença tarifa ajuste - Item 2
							'Parcialmente Livre
							ldblDiLKwhP = ldblVlLKwhP - ldblVsLKwhP ' diferença tarifa ajuste - Item 2
							ldblDiLKwhF = ldblVlLKwhF - ldblVsLKwhF ' diferença tarifa ajuste - Item 2
						End If
						'
						'Item 1
						lstrClaAjus = lstrCodClas
						If lstrGrupo = "B" Then
							lstrGruAjus = "B"
							If VB.Left(lstrCodClas, 1) = "5" Then
								lstrClaAjus = "9101"
							End If
						Else
							lstrGruAjus = lstrGrTar & IIf(lintLivEsp = 1, "1", "")
						End If
						If lstrGruAjus <> lstrGruAjuX Or lstrClaAjus <> lstrClaAjuX Then
							ldblTarAjus = GetTarifaAjuste(lstrGruAjus, CShort(lstrClaAjus)) * -1
							lstrGruAjuX = lstrGruAjus
							lstrClaAjuX = lstrClaAjus
						End If
						ldblVlrAjuP = Truncate((ldblQtdkWhp + ldblQtLKwhP) * ldblTarAjus, 2)
						ldblVlrAjuF = Truncate((ldblQtdKwhF + ldblQtLKwhF) * ldblTarAjus, 2)
						ldblVlrAju1 = Truncate(ldblQtdkWh1 * ldblTarAjus, 2)
						ldblVlrAju2 = Truncate(ldblQtdkWh2 * ldblTarAjus, 2)
						ldblVlrAju3 = Truncate(ldblQtdkWh3 * ldblTarAjus, 2)
						ldblVlrAju4 = Truncate(ldblQtdkWh4 * ldblTarAjus, 2)
						'
						'Gravar os dados
						If (ldblQtdkWhp + ldblQtdKwhF + ldblQtdkWh1 + ldblQtdkWh2 + ldblQtdkWh3 + ldblQtdkWh4) > 0 Then
							lstrSql = "INSERT INTO fat_tar_ajuste SET "
							lstrSql = lstrSql & "numero_fiscal = " & ldblNumNF & ","
							lstrSql = lstrSql & "mesano_fiscal = '" & lstrMesAno & "',"
							lstrSql = lstrSql & "anomes_fiscal = " & llngAnoMes & ","
							lstrSql = lstrSql & "grupo = '" & lstrGrupo & "',"
							lstrSql = lstrSql & "grtar = '" & lstrGrTar & "',"
							lstrSql = lstrSql & "grtai = '" & lstrGrTaI & "',"
							lstrSql = lstrSql & "codigo_classe = '" & lstrCodClas & "',"
							lstrSql = lstrSql & "descricao_classe = '" & lstrDesClas & "',"
							lstrSql = lstrSql & "qtd_kwh_p = " & Replace(CStr(ldblQtdkWhp), ",", ".") & ","
							lstrSql = lstrSql & "qtd_kwh_f = " & Replace(CStr(ldblQtdKwhF), ",", ".") & ","
							lstrSql = lstrSql & "qtl_kwh_p = " & Replace(CStr(ldblQtLKwhP), ",", ".") & ","
							lstrSql = lstrSql & "qtl_kwh_f = " & Replace(CStr(ldblQtLKwhF), ",", ".") & ","
							lstrSql = lstrSql & "qtd_kwh_i = " & Replace(CStr(ldblQtdKwhI), ",", ".") & ","
							lstrSql = lstrSql & "qtd_kwh01 = " & Replace(CStr(ldblQtdkWh1), ",", ".") & ","
							lstrSql = lstrSql & "qtd_kwh02 = " & Replace(CStr(ldblQtdkWh2), ",", ".") & ","
							lstrSql = lstrSql & "qtd_kwh03 = " & Replace(CStr(ldblQtdkWh3), ",", ".") & ","
							lstrSql = lstrSql & "qtd_kwh04 = " & Replace(CStr(ldblQtdkWh4), ",", ".") & ","
							'Item 1
							lstrSql = lstrSql & "tarifa_tabela_ajuste = " & Replace(CStr(ldblTarAjus), ",", ".") & ","
							lstrSql = lstrSql & "valor_kwh_tar_ajus_p = " & Replace(CStr(ldblVlrAjuP), ",", ".") & ","
							lstrSql = lstrSql & "valor_kwh_tar_ajus_f = " & Replace(CStr(ldblVlrAjuF), ",", ".") & ","
							'baixa renda
							lstrSql = lstrSql & "valor_kwh_tar_ajus_1 = " & Replace(CStr(ldblVlrAju1), ",", ".") & ","
							lstrSql = lstrSql & "valor_kwh_tar_ajus_2 = " & Replace(CStr(ldblVlrAju2), ",", ".") & ","
							lstrSql = lstrSql & "valor_kwh_tar_ajus_3 = " & Replace(CStr(ldblVlrAju3), ",", ".") & ","
							lstrSql = lstrSql & "valor_kwh_tar_ajus_4 = " & Replace(CStr(ldblVlrAju4), ",", ".") & ","
							'Item 2
							lstrSql = lstrSql & "vlr_fat_kwh_p = " & Replace(CStr(ldblVlrkWhp), ",", ".") & ","
							lstrSql = lstrSql & "vlr_fat_kwh_f = " & Replace(CStr(ldblVlrKwhF), ",", ".") & ","
							lstrSql = lstrSql & "vll_fat_kwh_p = " & Replace(CStr(ldblVlLKwhP), ",", ".") & ","
							lstrSql = lstrSql & "vll_fat_kwh_f = " & Replace(CStr(ldblVlLKwhF), ",", ".") & ","
							lstrSql = lstrSql & "vlr_fat_kwh_i = " & Replace(CStr(ldblVlrkWhI), ",", ".") & ","
							lstrSql = lstrSql & "vlr_fat_kwh01 = " & Replace(CStr(ldblVlrkWh1), ",", ".") & ","
							lstrSql = lstrSql & "vlr_fat_kwh02 = " & Replace(CStr(ldblVlrkWh2), ",", ".") & ","
							lstrSql = lstrSql & "vlr_fat_kwh03 = " & Replace(CStr(ldblVlrkWh3), ",", ".") & ","
							lstrSql = lstrSql & "vlr_fat_kwh04 = " & Replace(CStr(ldblVlrkWh4), ",", ".") & ","
							
							lstrSql = lstrSql & "tar_sem_kwh_p = " & Replace(CStr(ldblTarKwhP), ",", ".") & ", "
							lstrSql = lstrSql & "tar_sem_kwh_f = " & Replace(CStr(ldblTarKwhF), ",", ".") & ", "
							lstrSql = lstrSql & "tal_sem_kwh_p = " & Replace(CStr(ldblTaLKwhP), ",", ".") & ", "
							lstrSql = lstrSql & "tal_sem_kwh_f = " & Replace(CStr(ldblTaLKwhF), ",", ".") & ", "
							lstrSql = lstrSql & "tar_sem_kwh_i = " & Replace(CStr(ldblTarKwhI), ",", ".") & ", "
							lstrSql = lstrSql & "tar_sem_kwh01 = " & Replace(CStr(ldblTarKwh1), ",", ".") & ", "
							lstrSql = lstrSql & "tar_sem_kwh02 = " & Replace(CStr(ldblTarKwh2), ",", ".") & ", "
							lstrSql = lstrSql & "tar_sem_kwh03 = " & Replace(CStr(ldblTarKwh3), ",", ".") & ", "
							lstrSql = lstrSql & "tar_sem_kwh04 = " & Replace(CStr(ldblTarKwh4), ",", ".") & ","
							
							lstrSql = lstrSql & "vlr_sem_kwh_p = " & Replace(CStr(ldblVlsKwhP), ",", ".") & ","
							lstrSql = lstrSql & "vlr_sem_kwh_f = " & Replace(CStr(ldblVlsKwhF), ",", ".") & ","
							lstrSql = lstrSql & "vll_sem_kwh_p = " & Replace(CStr(ldblVsLKwhP), ",", ".") & ","
							lstrSql = lstrSql & "vll_sem_kwh_f = " & Replace(CStr(ldblVsLKwhF), ",", ".") & ","
							lstrSql = lstrSql & "vlr_sem_kwh_i = " & Replace(CStr(ldblVlsKwhI), ",", ".") & ","
							lstrSql = lstrSql & "vlr_sem_kwh01 = " & Replace(CStr(ldblVlsKwh1), ",", ".") & ","
							lstrSql = lstrSql & "vlr_sem_kwh02 = " & Replace(CStr(ldblVlsKwh2), ",", ".") & ","
							lstrSql = lstrSql & "vlr_sem_kwh03 = " & Replace(CStr(ldblVlsKwh3), ",", ".") & ","
							lstrSql = lstrSql & "vlr_sem_kwh04 = " & Replace(CStr(ldblVlsKwh4), ",", ".") & ","
							
							lstrSql = lstrSql & "vlr_dif_kwh_p = " & Replace(CStr(Arredondar(ldblDifKwhP, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vlr_dif_kwh_f = " & Replace(CStr(Arredondar(ldblDifKwhF, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vll_dif_kwh_p = " & Replace(CStr(Arredondar(ldblDiLKwhP, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vll_dif_kwh_f = " & Replace(CStr(Arredondar(ldblDiLKwhF, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vlr_dif_kwh_i = " & Replace(CStr(Arredondar(ldblDifKwhI, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vlr_dif_kwh01 = " & Replace(CStr(Arredondar(ldblDifKwh1, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vlr_dif_kwh02 = " & Replace(CStr(Arredondar(ldblDifKwh2, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vlr_dif_kwh03 = " & Replace(CStr(Arredondar(ldblDifKwh3, 2)), ",", ".") & ","
							lstrSql = lstrSql & "vlr_dif_kwh04 = " & Replace(CStr(Arredondar(ldblDifKwh4, 2)), ",", ".") & ""
							Call myConC.Execute(lstrSql)
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
				End If
			End If
		End With
		Me.lblInfo.Text = Me.lblInfo.Text & " - Finalizado"
		Call Me.lblInfo.Refresh()
	End Sub
	
	Private Function GetTarProp(ByVal adteReferencia As Date, ByVal adteLeiAtu As Date, ByVal adteLeiAnt As Date, ByVal astrSqlAux As String) As Double
		Dim ldblRet As Double
		Dim ldblAtu As Double
		Dim ldblAnt As Double
		Dim ldteAtu As Date
		Dim ldteAnt As Date
		Dim llngDAt As Integer
		Dim llngDAn As Integer
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT * "
		lstrSql = lstrSql & "    FROM " & gstrDataBase & ".`tarifa` "
		lstrSql = lstrSql & "   WHERE " & astrSqlAux & " "
		lstrSql = lstrSql & "     AND `referencia` <> '1951-01-01' "
		lstrSql = lstrSql & "     AND `referencia` <= '" & VB6.Format(adteReferencia, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "     AND `referencia` <> '" & VB6.Format(Me.dtpTarifaAjuste.value, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "ORDER BY `referencia` DESC LIMIT 0, 2"
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			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"'
					ldblAtu = IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").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"'
					ldteAtu = IIf(IsDbNull(.Fields("datanova").value), "1951-01-01", .Fields("datanova").value)
					If .RecordCount = 2 Then
						Call .MoveNext()
					End If
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblAnt = IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").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"'
					ldteAnt = IIf(IsDbNull(.Fields("datanova").value), "1951-01-01", .Fields("datanova").value)
				End If
				Call .Close()
			End If
		End With
		'
		If adteLeiAtu > ldteAtu And adteLeiAnt < ldteAtu Then
			llngDAt = (adteLeiAtu.ToOADate - ldteAtu.ToOADate)
			llngDAn = ldteAtu.ToOADate - adteLeiAnt.ToOADate
		ElseIf adteLeiAtu <= ldteAtu And adteLeiAnt < ldteAtu Then 
			llngDAt = 0
			llngDAn = 1
		Else
			llngDAt = 1
			llngDAn = 0
		End If
		ldblRet = (((ldblAtu * llngDAt) + (ldblAnt * llngDAn)) / (llngDAt + llngDAn)) / 1000
		'
		'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
		GetTarProp = ldblRet
	End Function
	
	Private Function GetTarifaAjuste(ByVal astrGrupo As String, ByVal aintClas As Short) As Double
		Dim ldblRet As Double
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrSql = "SELECT * "
		lstrSql = lstrSql & " FROM tarifa_ajustes "
		lstrSql = lstrSql & "WHERE referencia_ini = '" & VB6.Format(Me.dtpTarifaAjuste.value, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "  AND grupo = '" & astrGrupo & "' "
		lstrSql = lstrSql & "  AND " & aintClas & " BETWEEN classe_ini AND classe_fim "
		lstrSql = lstrSql & " LIMIT 0, 1 "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			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"'
					ldblRet = IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").value)
				Else
					ldblRet = 0
				End If
			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
		GetTarifaAjuste = ldblRet
	End Function
	
	Private Sub frmRelTarifaAjuste_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'UPGRADE_NOTE: Object ifrAuxiliar 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"'
		ifrAuxiliar = Nothing
	End Sub
	
	Private Sub Imprimir()
		With ifrAuxiliar
			.quem_chama = Me
			.VS.Font = VB6.FontChangeName(.VS.Font, "Arial")
			.VS.Font = VB6.FontChangeSize(.VS.Font, 7)
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.PageBorder = VSVIEW6.PageBorderSettings.pbNone
			.VS.ShowGuides = VSVIEW6.ShowGuideSettings.gdShow
			.VS.Orientation = VSVIEW6.OrientationSettings.orPortrait
			.VS.PhysicalPage = True
			.VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
			.VS.MarginTop = "7mm"
			.VS.MarginRight = "7mm"
			.VS.MarginLeft = "15mm"
			.VS.MarginBottom = "5mm"
			.VS.MarginFooter = "0mm"
			.VS.MarginHeader = "0mm"
			.VS.TextColor = System.Drawing.Color.Black
			.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
			Call .VS.StartDoc()
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
			Call ImprimeTabelaTarifaAjuste()
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			Call .LineFeed(2)
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
			Call ImprimeTotalizacao()
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			Call .LineFeed(2)
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
			ImprimeQuatidadeFaturasPeriodo()
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			Call .VS.EndDoc()
			Call .Show()
		End With
	End Sub
	
	Private Sub ImprimeTotalizacao()
		Dim lstrGru As String
		Dim lstrGrI As String
		Dim ldblkWh As Double
		Dim ldblkW1 As Double
		Dim ldblkW2 As Double
		Dim ldblkW3 As Double
		Dim ldblkW4 As Double
		'
		Dim ldblI01 As Double
		Dim ldblI11 As Double
		Dim ldblI12 As Double
		Dim ldblI13 As Double
		Dim ldblI14 As Double
		'
		Dim ldblI02 As Double
		Dim ldblI21 As Double
		Dim ldblI22 As Double
		Dim ldblI23 As Double
		Dim ldblI24 As Double
		'
		'UPGRADE_WARNING: Lower bound of array ldblToQ was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblToQ(2) As Double
		'UPGRADE_WARNING: Lower bound of array ldblToV was changed from 1,1 to 0,0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblToV(2, 2) As Double
		
		Dim lstrCol As String
		Dim lstrTab As String
		'
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrSql = "SELECT "
		lstrSql = lstrSql & "`grupo`, `grtai`, "
		lstrSql = lstrSql & " (SUM(qtd_kwh_p)+SUM(qtd_kwh_f)+SUM(qtl_kwh_p)+SUM(qtl_kwh_f)) `tot_qtd_kwh`, "
		lstrSql = lstrSql & " SUM(qtd_kwh01) `tot_qtd_kwh01`, "
		lstrSql = lstrSql & " SUM(qtd_kwh02) `tot_qtd_kwh02`, "
		lstrSql = lstrSql & " SUM(qtd_kwh03) `tot_qtd_kwh03`, "
		lstrSql = lstrSql & " SUM(qtd_kwh04) `tot_qtd_kwh04`, "
		'Item 1
		lstrSql = lstrSql & " (SUM(valor_kwh_tar_ajus_p)+SUM(valor_kwh_tar_ajus_f)) `tot_item_1`, "
		lstrSql = lstrSql & " SUM(valor_kwh_tar_ajus_1) `tot_ite1_bx1`, "
		lstrSql = lstrSql & " SUM(valor_kwh_tar_ajus_2) `tot_ite1_bx2`, "
		lstrSql = lstrSql & " SUM(valor_kwh_tar_ajus_3) `tot_ite1_bx3`, "
		lstrSql = lstrSql & " SUM(valor_kwh_tar_ajus_4) `tot_ite1_bx4`, "
		'Item 2
		lstrSql = lstrSql & " (SUM(vlr_dif_kwh_p)+SUM(vlr_dif_kwh_f)+SUM(vll_dif_kwh_p)+SUM(vll_dif_kwh_f)-SUM(vlr_dif_kwh_i)) `tot_item_2`, "
		lstrSql = lstrSql & " SUM(vlr_dif_kwh01) `tot_ite2_01`, "
		lstrSql = lstrSql & " SUM(vlr_dif_kwh02) `tot_ite2_02`, "
		lstrSql = lstrSql & " SUM(vlr_dif_kwh03) `tot_ite2_03`, "
		lstrSql = lstrSql & " SUM(vlr_dif_kwh04) `tot_ite2_04` "
		lstrSql = lstrSql & " FROM `fat_tar_ajuste` "
		lstrSql = lstrSql & " GROUP BY `grupo`, `grtai` "
		lstrSql = lstrSql & " ORDER BY `grupo` ASC, `grtai` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					ldblToQ(1) = 0
					ldblToQ(2) = 0
					ldblToV(1, 1) = 0
					ldblToV(1, 2) = 0
					ldblToV(2, 1) = 0
					ldblToV(2, 2) = 0
					ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
					ifrAuxiliar.VS.Table = "^170mm;FATURAMENTO"
					lstrCol = "^10mm|^40mm|^40mm|^40mm|^40mm;"
					ifrAuxiliar.VS.Table = lstrCol & "Grupo|SubGrupo/Classe|Qtd." & IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "MWh", "kWh") & "|ITEM 1 / " & IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "MWh", "kWh") & "|ITEM 2 / " & IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "MWh", "kWh") & ""
					lstrCol = "^10mm|<40mm|>40mm|>40mm|>40mm;"
					ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
					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"'
						lstrGru = IIf(IsDbNull(.Fields("grupo").value), "", .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"'
						lstrGrI = IIf(IsDbNull(.Fields("grtai").value), "", .Fields("grtai").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"'
						ldblkWh = Arredondar(IIf(IsDbNull(.Fields("tot_qtd_kwh").value), 0, .Fields("tot_qtd_kwh").value), 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"'
						ldblkW1 = Arredondar(IIf(IsDbNull(.Fields("tot_qtd_kwh01").value), 0, .Fields("tot_qtd_kwh01").value), 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"'
						ldblkW2 = Arredondar(IIf(IsDbNull(.Fields("tot_qtd_kwh02").value), 0, .Fields("tot_qtd_kwh02").value), 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"'
						ldblkW3 = Arredondar(IIf(IsDbNull(.Fields("tot_qtd_kwh03").value), 0, .Fields("tot_qtd_kwh03").value), 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"'
						ldblkW4 = Arredondar(IIf(IsDbNull(.Fields("tot_qtd_kwh04").value), 0, .Fields("tot_qtd_kwh04").value), 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"'
						ldblI01 = Arredondar(IIf(IsDbNull(.Fields("tot_item_1").value), 0, .Fields("tot_item_1").value), 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"'
						ldblI11 = Arredondar(IIf(IsDbNull(.Fields("tot_ite1_bx1").value), 0, .Fields("tot_ite1_bx1").value), 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"'
						ldblI12 = Arredondar(IIf(IsDbNull(.Fields("tot_ite1_bx2").value), 0, .Fields("tot_ite1_bx2").value), 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"'
						ldblI13 = Arredondar(IIf(IsDbNull(.Fields("tot_ite1_bx3").value), 0, .Fields("tot_ite1_bx3").value), 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"'
						ldblI14 = Arredondar(IIf(IsDbNull(.Fields("tot_ite1_bx4").value), 0, .Fields("tot_ite1_bx4").value), 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"'
						ldblI02 = Arredondar(IIf(IsDbNull(.Fields("tot_item_2").value), 0, .Fields("tot_item_2").value), 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"'
						ldblI21 = Arredondar(IIf(IsDbNull(.Fields("tot_ite2_01").value), 0, .Fields("tot_ite2_01").value), 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"'
						ldblI22 = Arredondar(IIf(IsDbNull(.Fields("tot_ite2_02").value), 0, .Fields("tot_ite2_02").value), 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"'
						ldblI23 = Arredondar(IIf(IsDbNull(.Fields("tot_ite2_03").value), 0, .Fields("tot_ite2_03").value), 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"'
						ldblI24 = Arredondar(IIf(IsDbNull(.Fields("tot_ite2_04").value), 0, .Fields("tot_ite2_04").value), 2)
						'
						If lstrGru = "B" And lstrGrI = "B1 - Baixa Renda" Then
							lstrTab = ""
							lstrTab = lstrTab & lstrGru
							lstrTab = lstrTab & "|" & lstrGrI & " - faixa 01"
							lstrTab = lstrTab & "|" & VB6.Format(ldblkW1 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI11 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI21 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							lstrTab = ""
							lstrTab = lstrTab & lstrGru
							lstrTab = lstrTab & "|" & lstrGrI & " - faixa 02"
							lstrTab = lstrTab & "|" & VB6.Format(ldblkW2 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI12 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI22 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							lstrTab = ""
							lstrTab = lstrTab & lstrGru
							lstrTab = lstrTab & "|" & lstrGrI & " - faixa 03"
							lstrTab = lstrTab & "|" & VB6.Format(ldblkW3 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI13 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI23 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							lstrTab = ""
							lstrTab = lstrTab & lstrGru
							lstrTab = lstrTab & "|" & lstrGrI & " - faixa 04"
							lstrTab = lstrTab & "|" & VB6.Format(ldblkW4 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI14 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI24 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							lstrTab = ""
							lstrTab = lstrTab & lstrGru
							lstrTab = lstrTab & "|Total Bx.Renda"
							lstrTab = lstrTab & "|" & VB6.Format((ldblkW1 + ldblkW2 + ldblkW3 + ldblkW4) / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format((ldblI11 + ldblI12 + ldblI13 + ldblI14) / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format((ldblI21 + ldblI22 + ldblI23 + ldblI24) / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
							ldblToQ(2) = ldblToQ(2) + ((ldblkW1 + ldblkW2 + ldblkW3 + ldblkW4) / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
							ldblToV(2, 1) = ldblToV(2, 1) + ((ldblI11 + ldblI12 + ldblI13 + ldblI14) / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
							ldblToV(2, 2) = ldblToV(2, 2) + ((ldblI21 + ldblI22 + ldblI23 + ldblI24) / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
						Else
							lstrTab = ""
							lstrTab = lstrTab & lstrGru
							lstrTab = lstrTab & "|" & lstrGrI
							lstrTab = lstrTab & "|" & VB6.Format(ldblkWh / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI01 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblI02 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1), "#,##0.00")
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							If lstrGru = "B" Then
								ldblToQ(2) = ldblToQ(2) + (ldblkWh / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
								ldblToV(2, 1) = ldblToV(2, 1) + (ldblI01 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
								ldblToV(2, 2) = ldblToV(2, 2) + (ldblI02 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
							Else
								ldblToQ(1) = ldblToQ(1) + (ldblkWh / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
								ldblToV(1, 1) = ldblToV(1, 1) + (ldblI01 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
								ldblToV(1, 2) = ldblToV(1, 2) + (ldblI02 / IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, 1000, 1))
							End If
						End If
						Call .MoveNext()
						If Not .EOF 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"'
							If lstrGru = "A" And (lstrGru <> IIf(IsDbNull(.Fields("grupo").value), "", .Fields("grupo").value)) Then
								lstrTab = ""
								lstrTab = lstrTab & lstrGru
								lstrTab = lstrTab & "|Sub-Total"
								lstrTab = lstrTab & "|" & VB6.Format(ldblToQ(1), "#,##0.00")
								lstrTab = lstrTab & "|" & VB6.Format(ldblToV(1, 1), "#,##0.00")
								lstrTab = lstrTab & "|" & VB6.Format(ldblToV(1, 2), "#,##0.00")
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
								ifrAuxiliar.VS.Table = lstrCol & lstrTab
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
							End If
						Else
							If lstrGru = "B" Then
								lstrTab = ""
								lstrTab = lstrTab & lstrGru
								lstrTab = lstrTab & "|Sub-Total"
								lstrTab = lstrTab & "|" & VB6.Format(ldblToQ(2), "#,##0.00")
								lstrTab = lstrTab & "|" & VB6.Format(ldblToV(2, 1), "#,##0.00")
								lstrTab = lstrTab & "|" & VB6.Format(ldblToV(2, 2), "#,##0.00")
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
								ifrAuxiliar.VS.Table = lstrCol & lstrTab
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
							End If
							lstrTab = ""
							lstrTab = lstrTab & ""
							lstrTab = lstrTab & "|Total Geral"
							lstrTab = lstrTab & "|" & VB6.Format(ldblToQ(1) + ldblToQ(2), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblToV(1, 1) + ldblToV(2, 1), "#,##0.00")
							lstrTab = lstrTab & "|" & VB6.Format(ldblToV(1, 2) + ldblToV(2, 2), "#,##0.00")
							ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
						End If
					End While
				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 ImprimeQuatidadeFaturasPeriodo()
		Dim lstrMAn As String
		Dim lstrGru As String
		Dim lstrGrI As String
		Dim ldblQtd As Double
		'
		'UPGRADE_WARNING: Lower bound of array ldblTot was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblTot(2) As Double
		'
		Dim lstrCol As String
		Dim lstrTab As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrSql = "SELECT "
		lstrSql = lstrSql & " mesano_fiscal, grupo, grtai, "
		lstrSql = lstrSql & " COUNT(numero_fiscal) qtde "
		lstrSql = lstrSql & " FROM `fat_tar_ajuste` "
		lstrSql = lstrSql & " GROUP BY `grupo`, `mesano_fiscal`,  `grtai` "
		lstrSql = lstrSql & " ORDER BY `grupo` ASC, `anomes_fiscal` ASC,  `grtai` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					ldblTot(1) = 0
					ldblTot(2) = 0
					ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
					ifrAuxiliar.VS.Table = "^80mm;QUANTIDADE DE FATURAS"
					lstrCol = "^15mm|^10mm|^40mm|^15mm;"
					ifrAuxiliar.VS.Table = lstrCol & "Mês/Ano|Grupo|SubGrupo|Qtd."
					lstrCol = "^15mm|^10mm|<40mm|>15mm;"
					ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
					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"'
						lstrMAn = IIf(IsDbNull(.Fields("mesano_fiscal").value), "", .Fields("mesano_fiscal").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"'
						lstrGru = IIf(IsDbNull(.Fields("grupo").value), "", .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"'
						lstrGrI = IIf(IsDbNull(.Fields("grtai").value), "", .Fields("grtai").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"'
						ldblQtd = IIf(IsDbNull(.Fields("qtde").value), "", .Fields("qtde").value)
						If lstrGru = "A" Then
							ldblTot(1) = ldblTot(1) + ldblQtd
						Else
							ldblTot(2) = ldblTot(2) + ldblQtd
						End If
						lstrTab = ""
						lstrTab = lstrTab & lstrMAn & "|" & lstrGru & "|" & lstrGrI
						lstrTab = lstrTab & "|" & VB6.Format(ldblQtd, "#,###0")
						ifrAuxiliar.VS.Table = lstrCol & lstrTab
						Call .MoveNext()
						If Not .EOF 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"'
							If lstrGru = "A" And lstrGru <> IIf(IsDbNull(.Fields("grupo").value), "", .Fields("grupo").value) Then
								lstrTab = ""
								lstrTab = lstrTab & "|" & lstrGru
								lstrTab = lstrTab & "|Sub-Total"
								lstrTab = lstrTab & "|" & VB6.Format(ldblTot(1), "#,###0")
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
								ifrAuxiliar.VS.Table = lstrCol & lstrTab
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
							End If
						Else
							If lstrGru = "B" Then
								lstrTab = ""
								lstrTab = lstrTab & "|" & lstrGru
								lstrTab = lstrTab & "|Sub-Total"
								lstrTab = lstrTab & "|" & VB6.Format(ldblTot(2), "#,###0")
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
								ifrAuxiliar.VS.Table = lstrCol & lstrTab
								ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
							End If
							lstrTab = ""
							lstrTab = lstrTab & "|"
							lstrTab = lstrTab & "|Total Geral"
							lstrTab = lstrTab & "|" & VB6.Format(ldblTot(1) + ldblTot(2), "#,###0")
							ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
							ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
						End If
					End While
				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 ImprimeTabelaTarifaAjuste()
		Dim lstrGru As String
		Dim lintClI As Short
		Dim lintClF As Short
		Dim ldblVlr As Double
		'
		Dim lstrCol As String
		Dim lstrTab As String
		'
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		'GRUPO Subgrupo  Classe/Subclasse  TE (R$/MWh)
		
		lstrSql = "SELECT grupo, classe_ini, classe_fim, valor "
		lstrSql = lstrSql & " FROM tarifa_ajustes "
		lstrSql = lstrSql & "WHERE referencia_ini = '" & VB6.Format(Me.dtpTarifaAjuste.value, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & " ORDER BY `grupo` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, True)
					ifrAuxiliar.VS.Table = "^85mm;TARIFA DE AJUSTE - TABELA 3"
					lstrCol = "^10mm|^15mm|<30mm|>30mm;"
					
					ifrAuxiliar.VS.Table = lstrCol & "Grupo|SubGrupo|Classe|TE (R$/" & IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "MWh", "kWh") & ")"
					ifrAuxiliar.VS.Font = VB6.FontChangeBold(ifrAuxiliar.VS.Font, False)
					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"'
						lstrGru = IIf(IsDbNull(.Fields("grupo").value), "", .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"'
						lintClI = IIf(IsDbNull(.Fields("classe_ini").value), 0, .Fields("classe_ini").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"'
						lintClF = IIf(IsDbNull(.Fields("classe_fim").value), 0, .Fields("classe_fim").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"'
						ldblVlr = IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").value)
						If Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked Then
							ldblVlr = ldblVlr * 1000
						End If
						If VB.Left(lstrGru, 1) = "A" Then
							lstrTab = ""
							lstrTab = lstrTab & VB.Left(lstrGru, 1)
							lstrTab = lstrTab & "|NA"
							Select Case lstrGru
								Case Is = "A4"
									lstrTab = lstrTab & "|Convencional"
								Case Is = "AA"
									lstrTab = lstrTab & "|Horo Azul"
								Case Is = "AV"
									lstrTab = lstrTab & "|Horo Verde"
								Case Is = "AL"
									lstrTab = lstrTab & "|Livre"
								Case Is = "AL1"
									lstrTab = lstrTab & "|Livre P.Cat."
								Case Is = "AX"
									lstrTab = lstrTab & "|Suprimento"
							End Select
							lstrTab = lstrTab & "|" & VB6.Format(ldblVlr, IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "#,##0.00", "#,#####0.00000"))
							ifrAuxiliar.VS.Table = lstrCol & lstrTab
						Else
							lstrTab = ""
							If lintClI < 7000 Then
								lstrTab = VB.Left(lstrGru, 1)
								If lintClI >= 0 And lintClF <= 4999 Then
									lstrTab = lstrTab & "|B3|Ind./Com."
								ElseIf lintClI >= 5000 And lintClF <= 5999 Then 
									lstrTab = lstrTab & "|B2|Rural"
								ElseIf lintClI >= 6100 And lintClF <= 6399 Then 
									lstrTab = lstrTab & "|B3|P.Públicos"
								ElseIf lintClI = 6401 Then 
									lstrTab = lstrTab & "|B4a|Ilum.Pública"
								ElseIf lintClI = 6402 Then 
									lstrTab = lstrTab & "|B4b|Ilum.Pública"
								End If
								lstrTab = lstrTab & "|" & VB6.Format(ldblVlr, IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "#,##0.00", "#,#####0.00000"))
								ifrAuxiliar.VS.Table = lstrCol & lstrTab
							Else
								If lintClI >= 7000 And lintClF <= 9999 Then
									lstrTab = VB.Left(lstrGru, 1) & "|B1|Resid./Bx.Renda" & "|" & VB6.Format(ldblVlr, IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "#,##0.00", "#,#####0.00000"))
									ifrAuxiliar.VS.Table = lstrCol & lstrTab
									lstrTab = VB.Left(lstrGru, 1) & "|B3|Serv.Público" & "|" & VB6.Format(ldblVlr, IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "#,##0.00", "#,#####0.00000"))
									ifrAuxiliar.VS.Table = lstrCol & lstrTab
									lstrTab = VB.Left(lstrGru, 1) & "|B3|C.Próprio" & "|" & VB6.Format(ldblVlr, IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, "#,##0.00", "#,#####0.00000"))
									ifrAuxiliar.VS.Table = lstrCol & lstrTab
								End If
							End If
						End If
						Call .MoveNext()
					End While
				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
	
	Public Function CabecaRel() As Object
		With ifrAuxiliar
			Call Cabecalho_Relatorio(.VS, "DEVOLUÇÃO ANGRA 3 / REDUTOR ABRIL/2017", "APLICAÇÃO TABELAS 3 REH 2.214/2017", "4", "", IIf(Me.chkEmMWh.CheckState = System.Windows.Forms.CheckState.Checked, " em MWh ", " em kWh "))
			Call LineFeed(.VS, 1)
			'UPGRADE_ISSUE: VBControlExtender method ifrAuxiliar.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			.VS.CurrentY = .VS.CurrentY + 200
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
		End With
	End Function
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.cmdImprimir.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.dtpTarifaAjuste.Enabled = ablnModo
		Me.chkEmMWh.Enabled = ablnModo
		Me.cmdExporta.Enabled = ablnModo
	End Sub
	
	Private Sub ExportaCSV()
		Dim llngAux As Integer
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Trim(Me.txtArquivo.Text) <> "" Then
			lstrSql = "SELECT "
			lstrSql = lstrSql & " numero_fiscal 'Número Fatura', "
			lstrSql = lstrSql & " mesano_fiscal 'Mês / Ano', "
			lstrSql = lstrSql & " grupo 'Grupo', "
			lstrSql = lstrSql & " grtar 'Gr.Tarifa', "
			lstrSql = lstrSql & " grtai 'Grupo Impressão', "
			lstrSql = lstrSql & " codigo_classe 'Código Classe', "
			lstrSql = lstrSql & " descricao_classe 'Descrição Classe', "
			lstrSql = lstrSql & " qtd_kwh_p 'Qtd. kWh Ponta', "
			lstrSql = lstrSql & " qtd_kwh_f 'Qtd. kWh Fora de Ponta', "
			lstrSql = lstrSql & " qtl_kwh_p 'Livre - Qtd. kWh Ponta', "
			lstrSql = lstrSql & " qtl_kwh_f 'Livre - Qtd. kWh Fora de Ponta', "
			lstrSql = lstrSql & " qtd_kwh_i 'Qtd. kWh - Reservado', "
			lstrSql = lstrSql & " qtd_kwh01 'Qtd. kWh - Faixa 01', "
			lstrSql = lstrSql & " qtd_kwh02 'Qtd. kWh - Faixa 02', "
			lstrSql = lstrSql & " qtd_kwh03 'Qtd. kWh - Faixa 03', "
			lstrSql = lstrSql & " qtd_kwh04 'Qtd. kWh - Faixa 04', "
			lstrSql = lstrSql & " tarifa_tabela_ajuste 'Tarifa Tab.3', "
			lstrSql = lstrSql & " valor_kwh_tar_ajus_p 'Valor Item 1 Ponta', "
			lstrSql = lstrSql & " valor_kwh_tar_ajus_f 'Valor Item 1 Fora de Ponta', "
			lstrSql = lstrSql & " valor_kwh_tar_ajus_1 'Valor Item 1 - Faixa 01', "
			lstrSql = lstrSql & " valor_kwh_tar_ajus_2 'Valor Item 1 - Faixa 02', "
			lstrSql = lstrSql & " valor_kwh_tar_ajus_3 'Valor Item 1 - Faixa 03', "
			lstrSql = lstrSql & " valor_kwh_tar_ajus_4 'Valor Item 1 - Faixa 04', "
			lstrSql = lstrSql & " vlr_fat_kwh_p 'Valor Faturado kWh Ponta', "
			lstrSql = lstrSql & " vlr_fat_kwh_f 'Valor Faturado kWh Fora de Ponta', "
			lstrSql = lstrSql & " vll_fat_kwh_p 'Livre - Valor Faturado kWh Ponta', "
			lstrSql = lstrSql & " vll_fat_kwh_f 'Livre - Valor Faturado kWh Fora de Ponta', "
			lstrSql = lstrSql & " vlr_fat_kwh_i 'Valor Faturado - Rerservado', "
			lstrSql = lstrSql & " vlr_fat_kwh01 'Valor Fatura - Faixa 01', "
			lstrSql = lstrSql & " vlr_fat_kwh02 'Valor Fatura - Faixa 02', "
			lstrSql = lstrSql & " vlr_fat_kwh03 'Valor Fatura - Faixa 03', "
			lstrSql = lstrSql & " vlr_fat_kwh04 'Valor Fatura - Faixa 04', "
			lstrSql = lstrSql & " tar_sem_kwh_p 'Tarifa Sem Angra Ponta', "
			lstrSql = lstrSql & " tar_sem_kwh_f 'Tarifa Sem Angra Fora de Ponta', "
			lstrSql = lstrSql & " tal_sem_kwh_p 'Livre - Tarifa Sem Angra Ponta', "
			lstrSql = lstrSql & " tal_sem_kwh_f 'Livre - Tarifa Sem Angra Fora de Ponta', "
			lstrSql = lstrSql & " tar_sem_kwh_i 'Tarifa Sem Angra - Reservado', "
			lstrSql = lstrSql & " tar_sem_kwh01 'Tarifa Sem Angra - Faixa 01', "
			lstrSql = lstrSql & " tar_sem_kwh02 'Tarifa Sem Angra - Faixa 02', "
			lstrSql = lstrSql & " tar_sem_kwh03 'Tarifa Sem Angra - Faixa 03', "
			lstrSql = lstrSql & " tar_sem_kwh04 'Tarifa Sem Angra - Faixa 04', "
			lstrSql = lstrSql & " vlr_sem_kwh_p 'Valor kWh Sem Angra Ponta', "
			lstrSql = lstrSql & " vlr_sem_kwh_f 'Valor kWh Sem Angra Fora de Ponta', "
			lstrSql = lstrSql & " vll_sem_kwh_p 'Livre - Valor kWh Sem Angra Ponta', "
			lstrSql = lstrSql & " vll_sem_kwh_f 'Livre - Valor kWh Sem Angra Fora de Ponta', "
			lstrSql = lstrSql & " vlr_sem_kwh_i 'Valor kWh Sem Angra - Reservado', "
			lstrSql = lstrSql & " vlr_sem_kwh01 'Valor kWh Sem Angra - Faixa 01', "
			lstrSql = lstrSql & " vlr_sem_kwh02 'Valor kWh Sem Angra - Faixa 02', "
			lstrSql = lstrSql & " vlr_sem_kwh03 'Valor kWh Sem Angra - Faixa 03', "
			lstrSql = lstrSql & " vlr_sem_kwh04 'Valor kWh Sem Angra - Faixa 04', "
			lstrSql = lstrSql & " vlr_dif_kwh_p 'Valor Diferença kWh Ponta', "
			lstrSql = lstrSql & " vlr_dif_kwh_f 'Valor Diferença kWh Fora de Ponta', "
			lstrSql = lstrSql & " vll_dif_kwh_p 'Livre - Valor Diferença kWh Ponta', "
			lstrSql = lstrSql & " vll_dif_kwh_f 'Livre - Valor Diferença kWh Fora de Ponta', "
			lstrSql = lstrSql & " vlr_dif_kwh_i 'Valor Diferença kWh - Reservado', "
			lstrSql = lstrSql & " vlr_dif_kwh01 'Valor Diferença kWh - Faixa 01', "
			lstrSql = lstrSql & " vlr_dif_kwh02 'Valor Diferença kWh - Faixa 02', "
			lstrSql = lstrSql & " vlr_dif_kwh03 'Valor Diferença kWh - Faixa 03', "
			lstrSql = lstrSql & " vlr_dif_kwh04 'Valor Diferença kWh - Faixa 04' "
			lstrSql = lstrSql & " FROM fat_tar_ajuste "
			lstrSql = lstrSql & " ORDER BY `grupo` ASC, `anomes_fiscal` ASC, `grtai` ASC "
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						lintARQ = FreeFile
						FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " - Exportando")
						lstrLin = ""
						For llngAux = 0 To .Fields.Count - 1
							'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"'
							lstrLin = lstrLin & IIf(IsDbNull(.Fields(llngAux).Name), "", .Fields(llngAux).Name) & ";"
						Next llngAux
						PrintLine(lintARQ, lstrLin)
						While Not .EOF
							lstrLin = ""
							For llngAux = 0 To .Fields.Count - 1
								'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"'
								lstrLin = lstrLin & Replace(IIf(IsDbNull(.Fields(llngAux).value), "", .Fields(llngAux).value), ";", "") & ";"
							Next llngAux
							PrintLine(lintARQ, lstrLin)
							Call .MoveNext()
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " - Exportando")
						End While
						FileClose(lintARQ)
					End If
					Call .Close()
				End If
			End With
			Me.lblInfo.Text = Me.lblInfo.Text & " - Gerado!"
			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 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 de faturas"
		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 NomeArquivo()
		Dim lstrAux As String
		Dim lstrGrupo As String
		Dim lstrTipo As String
		Dim lstrPeriodo As String
		Me.dialArqOpen.DefaultExt = "CSV"
		istrNmArq = "Faturas_Angra3"
		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
End Class