Option Strict Off
Option Explicit On
Module mdlRelatorios
	
	Public Sub ImprimeDetalhamentosPaisagem(ByRef afrmRelatorio As FrmRelatorio, ByVal astrTabelaItens As String, ByVal aintTpRel As Short, Optional ByVal aintTipo As Short = 0, Optional ByVal adteFxIni As Date = #12:00:00 AM#, Optional ByVal adteFxFim As Date = #12:00:00 AM#)
		'aintTipo = 1  Normal
		'         = 2  Voltou da Reserva
		'         = 3  Todos
		Dim lstrMrgOri As String
		Dim llngPosicaoY As Integer
		Dim lstrTab As String
		Dim lstrTamCol As String
		Dim lstrCabDet As String
		
		Dim lintAux As Integer
		'UPGRADE_WARNING: Lower bound of array lstrTabela 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 lstrTabela(12) As String
		'UPGRADE_WARNING: Lower bound of array lstrTabDes 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 lstrTabDes(12) As String
		'UPGRADE_WARNING: Lower bound of array lstrMargem 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 lstrMargem(12) As String
		
		Dim lstrCod As String
		Dim lstrDes As String
		Dim ldblVlr As Double
		Dim ldblTotal As Double
		
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		lstrTamCol = "<13mm|<40mm|>15mm;"
		lstrCabDet = "Código|Descrição|Valor"
		
		With afrmRelatorio
			'UPGRADE_ISSUE: VBControlExtender method 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 + 300
			'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			'UPGRADE_WARNING: Couldn't resolve default property of object afrmRelatorio.VS.CurrentY. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			llngPosicaoY = .VS.CurrentY
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
			'UPGRADE_WARNING: Couldn't resolve default property of object afrmRelatorio.VS.MarginLeft. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			lstrMrgOri = .VS.MarginLeft
		End With
		'Parametrização
		'Coluna 1
		lstrTabela(1) = " WHERE ((MID(codigo, 1, 4)='9080' AND codigo NOT IN('9080000092','9080000098')) OR codigo IN('9070000061','9070000066','9070000067')) "
		lstrTabela(2) = " WHERE codigo = '9070000082' "
		lstrTabela(3) = " WHERE codigo = '9070000079' "
		lstrTabela(4) = " WHERE LEFT(codigo,9) = '907000003' "
		lstrTabela(5) = " WHERE MID(codigo,1,4)='9070' AND LEFT(codigo,9) <> '907000003' AND codigo NOT IN('9070000004','9070000051','9070000082','9070000087','9070000088','9070000090','9070000091','9070000092','9070000093','9070000079','9070000061','9070000066','9070000067') "
		'Coluna 2
		lstrTabela(6) = " WHERE (MID(codigo,1,4) IN('9040','9050') OR codigo IN('9070000051'))"
		'Coluna 3
		lstrTabela(7) = " WHERE MID(codigo,1,4)='9030' "
		lstrTabela(8) = " WHERE (codigo='9070000088' OR MID(codigo,1,4)='9090') "
		'Coluna 4
		lstrTabela(9) = " WHERE codigo IN('0302020032','9080000098','9070000087','9070000091', '9080000092','9070000004','9070000090','9070000092','9070000093') "
		lstrTabela(10) = " WHERE codigo IN('0302020051','0302020052','0302020053','0302020054','0302020055','0302020056','0302020057','0302020058') "
		lstrTabela(11) = " WHERE codigo IN('0602020001','0602020002','0602020003','0604020001','0604020002','0604020003') "
		lstrTabela(12) = " WHERE codigo IN('0302020008','0302020015','0302020011','0302020010','0302020023','0302020072','0302020031','0302020029','0302020030','0302020021','0302020022','0302020013','0302020014') "
		
		'Coluna 1
		lstrTabDes(1) = "Diversos"
		lstrTabDes(2) = "Dev.Dup.Pgto"
		lstrTabDes(3) = "Bonus Itaipu"
		lstrTabDes(4) = "Ressarc.IDQUAL"
		lstrTabDes(5) = "Outras Devoluções"
		'Coluna 2
		lstrTabDes(6) = "Taxados"
		'Coluna 3
		lstrTabDes(7) = "Convênios"
		lstrTabDes(8) = "Retenções"
		'Coluna 4
		lstrTabDes(9) = "Outros"
		lstrTabDes(10) = "Livres"
		lstrTabDes(11) = "Livres - Substituição Tributária"
		lstrTabDes(12) = "COOPERATIVAS"
		'Margens
		'Coluna 1
		lstrMargem(1) = lstrMrgOri
		lstrMargem(2) = lstrMrgOri
		lstrMargem(3) = lstrMrgOri
		lstrMargem(4) = lstrMrgOri
		lstrMargem(5) = lstrMrgOri
		'Coluna 2
		lstrMargem(6) = "76mm"
		'Coluna 3
		lstrMargem(7) = "145mm"
		lstrMargem(8) = "145mm"
		'Coluna 4
		lstrMargem(9) = "214mm"
		lstrMargem(10) = "214mm"
		lstrMargem(11) = "214mm"
		lstrMargem(12) = "214mm"
		'
		lstrSql = "SELECT `codigo`, SUM(`valor`) AS `valor` FROM " & astrTabelaItens
		For lintAux = 1 To UBound(lstrTabela)
			'Muda a margem conforme tabela
			afrmRelatorio.VS.MarginLeft = lstrMargem(lintAux)
			If lintAux = 1 Or lintAux = 6 Or lintAux = 7 Or lintAux = 9 Then
				'Posição Y de impressão
				'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
				afrmRelatorio.VS.CurrentY = llngPosicaoY
			End If
			ldblTotal = 0
			If aintTpRel = 0 Then 'Tota geral
				Call Conectar(lstrSql & lstrTabela(lintAux) & IIf(aintTipo < 3, " AND `tipo` = " & aintTipo & " ", "") & " GROUP BY `codigo` ASC ", lrstAux)
			ElseIf aintTpRel = 1 Then  'Diário
				Call Conectar(lstrSql & lstrTabela(lintAux) & " AND `datapag` = '" & VB6.Format(adteFxIni, "yyyy-MM-dd") & "'" & IIf(aintTipo < 3, " AND `tipo` = " & aintTipo & " ", "") & " GROUP BY `datapag` ASC, `codigo` ASC ", lrstAux)
			End If
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						With afrmRelatorio
							.VS.Font = VB6.FontChangeBold(.VS.Font, True)
							.VS.Table = "^68mm;Detalhamento dos " & lstrTabDes(lintAux)
							.VS.Font = VB6.FontChangeBold(.VS.Font, False)
							.VS.Table = lstrTamCol & lstrCabDet
						End With
						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"'
							lstrCod = IIf(IsDbNull(.Fields("codigo").value), "", .Fields("codigo").value)
							lstrDes = Replace(Replace(codTodescri(lstrCod), "DESCONTO", "DESC."), "CONFORME", "CONF.")
							If CDbl(Left(lstrCod, 4)) = 9040 Then
								lstrDes = lstrDes & IIf(CDbl(Right(lstrCod, 1)) = 1, " (B - Monofásico)", IIf(CDbl(Right(lstrCod, 1)) = 2, " (B - Bifásico)", IIf(CDbl(Right(lstrCod, 1)) = 3, " (B - Trifásico)", IIf(CDbl(Right(lstrCod, 1)) = 4, " (A)", ""))))
							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"'
							ldblVlr = CDbl(IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").value))
							If ldblVlr < 0 And (Left(lstrCod, 4) = "9070" Or Left(lstrCod, 4) = "9090") Then ldblVlr = ldblVlr * -1
							If Mid(lstrCod, 1, 4) = "9070" Or Mid(lstrCod, 1, 4) = "9090" Then ldblVlr = ldblVlr * -1
							lstrTab = ""
							lstrTab = lstrTab & lstrCod & "|"
							lstrTab = lstrTab & lstrDes & "|"
							lstrTab = lstrTab & VB6.Format(ldblVlr, "#,##0.00")
							ldblTotal = ldblTotal + ldblVlr
							afrmRelatorio.VS.Table = lstrTamCol & lstrTab
							Call .MoveNext()
						End While
						With afrmRelatorio
							.VS.Font = VB6.FontChangeBold(.VS.Font, True)
							.VS.Table = lstrTamCol & "|Total|" & VB6.Format(ldblTotal, "#,##0.00")
							.VS.Font = VB6.FontChangeBold(.VS.Font, False)
							.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
							.VS.Table = lstrTamCol & "||"
							.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
						End With
					End If
					Call .Close()
				End If
			End With
		Next lintAux
		afrmRelatorio.VS.MarginLeft = lstrMrgOri
		'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 Sub ImprimeDetalhamentosRetrato(ByRef afrmRelatorio As FrmRelatorio, ByVal astrTabelaItens As String, ByVal astrGrupo As String, Optional ByVal astrZona As String = "")
		Dim lstrMrgOri As String
		Dim lstrMrgNew As String
		Dim llngPosicaoY As Integer
		Dim lstrTab As String
		Dim lstrTamCol As String
		Dim lstrCabDet As String
		
		Dim lintAux As Integer
		'UPGRADE_WARNING: Lower bound of array lstrTabela 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 lstrTabela(11) As String
		'UPGRADE_WARNING: Lower bound of array lstrTabDes 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 lstrTabDes(11) As String
		
		Dim lstrCod As String
		Dim lstrDes As String
		Dim ldblVlr As Double
		Dim ldblTotal As Double
		
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		lstrTamCol = "<20mm|<60mm|>30mm;"
		lstrCabDet = "Código|Descrição|Valor"
		
		
		With afrmRelatorio
			'UPGRADE_WARNING: Couldn't resolve default property of object afrmRelatorio.VS.MarginLeft. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			lstrMrgOri = .VS.MarginLeft
			lstrMrgNew = "40mm"
			.VS.MarginLeft = lstrMrgNew
			'UPGRADE_ISSUE: VBControlExtender method 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 + 300
			'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			'UPGRADE_WARNING: Couldn't resolve default property of object afrmRelatorio.VS.CurrentY. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			llngPosicaoY = .VS.CurrentY
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
		End With
		'Parametrização
		'Coluna 1
		lstrTabela(1) = " AND ((MID(codigo, 1, 4)='9080' AND codigo NOT IN('9080000092','9080000098')) OR codigo IN('9070000061','9070000066','9070000067')) "
		lstrTabela(2) = " AND codigo = '9070000082' "
		lstrTabela(3) = " AND codigo = '9070000079' "
		lstrTabela(4) = " AND LEFT(codigo,9) = '907000003' "
		lstrTabela(5) = " AND MID(codigo,1,4)='9070' AND LEFT(codigo,9) <> '907000003' AND codigo NOT IN('9070000051','9070000082','9070000087','9070000088','9070000090','9070000091','9070000092','9070000093','9070000079','9070000061','9070000066','9070000067') "
		'Coluna 2
		lstrTabela(6) = " AND (MID(codigo,1,4) IN('9040','9050') OR codigo IN('9070000051'))"
		'Coluna 3
		lstrTabela(7) = " AND MID(codigo,1,4)='9030' "
		'Coluna 4
		lstrTabela(8) = " AND (codigo='9070000088' OR MID(codigo,1,4)='9090') "
		lstrTabela(9) = " AND codigo IN('0302020032','9080000098','9070000087','9070000091', '9080000092','9070000090','9070000092','9070000093') "
		lstrTabela(10) = " AND codigo IN('0302020051','0302020052','0302020053','0302020054','0302020055','0302020056','0302020057','0302020058') "
		lstrTabela(11) = " AND codigo IN('0602020001','0602020002','0602020003','0604020001','0604020002','0604020003') "
		'Coluna 1
		lstrTabDes(1) = "Diversos"
		lstrTabDes(2) = "Dev.Dup.Pgto"
		lstrTabDes(3) = "Bonus Itaipu"
		lstrTabDes(4) = "Ressarc.IDQUAL"
		lstrTabDes(5) = "Outras Devoluções"
		'Coluna 2
		lstrTabDes(6) = "Taxados"
		'Coluna 3
		lstrTabDes(7) = "Convênios"
		'Coluna 4
		lstrTabDes(8) = "Retenções"
		lstrTabDes(9) = "Outros"
		lstrTabDes(10) = "Livres"
		lstrTabDes(11) = "Livres - Substituição Tributária"
		'
		lstrSql = "SELECT `codigo`, SUM(`valor`) AS `valor` "
		lstrSql = lstrSql & " FROM " & astrTabelaItens & " "
		lstrSql = lstrSql & "WHERE 1 = 1 " & IIf(astrZona <> "", " AND `zona` = '" & astrZona & "' ", "") & IIf(astrGrupo <> "AB", " AND LEFT(`grupo`, 1) = '" & astrGrupo & "' ", "")
		For lintAux = 1 To UBound(lstrTabela)
			ldblTotal = 0
			Call Conectar(lstrSql & lstrTabela(lintAux) & " GROUP BY `codigo` ASC ", lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						With afrmRelatorio
							.VS.Font = VB6.FontChangeBold(.VS.Font, True)
							.VS.Table = "^110mm;Detalhamento dos " & lstrTabDes(lintAux)
							.VS.Font = VB6.FontChangeBold(.VS.Font, False)
							.VS.Table = lstrTamCol & lstrCabDet
						End With
						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"'
							lstrCod = IIf(IsDbNull(.Fields("codigo").value), "", .Fields("codigo").value)
							lstrDes = Replace(Replace(codTodescri(lstrCod), "DESCONTO", "DESC."), "CONFORME", "CONF.")
							If CDbl(Left(lstrCod, 4)) = 9040 Then
								lstrDes = lstrDes & IIf(CDbl(Right(lstrCod, 1)) = 1, " (B - Monofásico)", IIf(CDbl(Right(lstrCod, 1)) = 2, " (B - Bifásico)", IIf(CDbl(Right(lstrCod, 1)) = 3, " (B - Trifásico)", IIf(CDbl(Right(lstrCod, 1)) = 4, " (A)", ""))))
							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"'
							ldblVlr = CDbl(IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").value))
							If ldblVlr < 0 And (Left(lstrCod, 4) = "9070" Or Left(lstrCod, 4) = "9090") Then ldblVlr = ldblVlr * -1
							If Mid(lstrCod, 1, 4) = "9070" Or Mid(lstrCod, 1, 4) = "9090" Then ldblVlr = ldblVlr * -1
							lstrTab = ""
							lstrTab = lstrTab & lstrCod & "|"
							lstrTab = lstrTab & lstrDes & "|"
							lstrTab = lstrTab & VB6.Format(ldblVlr, "#,##0.00")
							ldblTotal = ldblTotal + ldblVlr
							afrmRelatorio.VS.Table = lstrTamCol & lstrTab
							Call .MoveNext()
						End While
						With afrmRelatorio
							.VS.Font = VB6.FontChangeBold(.VS.Font, True)
							.VS.Table = lstrTamCol & "|Total|" & VB6.Format(ldblTotal, "#,##0.00")
							.VS.Font = VB6.FontChangeBold(.VS.Font, False)
							'UPGRADE_ISSUE: VBControlExtender method 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 + 300
						End With
					End If
					Call .Close()
				End If
			End With
		Next lintAux
		afrmRelatorio.VS.MarginLeft = lstrMrgOri
		'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 Sub RelatorioPaisagemPadraoColunas(ByRef astrTamcol As String, ByRef astrCabec As String, ByRef astrTemp As String, ByRef astrBranco As String, ByRef astrNomClass As Object, ByRef astrCondicao As Object, Optional ByVal aintSomenteMicroGeradoras As Short = 0)
		'Parametros de quebra
		'UPGRADE_WARNING: Lower bound of array astrNomClass 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"'
		'UPGRADE_ISSUE: As Variant was removed from ReDim astrNomClass(1 To 12) statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="19AFCB41-AA8E-4E6B-A441-A3E802E5FD64"'
		ReDim astrNomClass(12)
		'UPGRADE_WARNING: Lower bound of array astrCondicao 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"'
		'UPGRADE_ISSUE: As Variant was removed from ReDim astrCondicao(1 To 12) statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="19AFCB41-AA8E-4E6B-A441-A3E802E5FD64"'
		ReDim astrCondicao(12)
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(1) = "Residencial"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(2) = "Industrial"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(3) = "Comercial"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(4) = "Rural"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(5) = "Rural Sup."
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(6) = "P.Público"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(7) = "I.Pública"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(8) = "S.Público"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(9) = "Cons.P.Liv"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(10) = "Cons.Livre"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(11) = "Sub-Total"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(12) = "C.Próprio"
		'
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(1) = " LEFT(`classe`, 1) = '9' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(2) = " (LEFT(`classe`, 1)+0) BETWEEN 0 AND 3 AND LEFT(`ta`, 2) <> 'AL' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(3) = " LEFT(`classe`, 1) = '4' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(4) = " LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) <> 'AX' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(5) = " LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) = 'AX' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(6) = " (LEFT(`classe`, 2)+0) BETWEEN 61 AND 63 "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(7) = " LEFT(`classe`, 2) = '64' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(8) = " LEFT(`classe`, 1) = '7' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(9) = " `ta` = 'ALS' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(10) = " TRIM(`ta`) = 'AL' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(11) = " LEFT(`classe`, 1) <> 8"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(12) = " LEFT(`classe`, 1) = 8"
		'
		If aintSomenteMicroGeradoras = 1 Then
			astrTamcol = "<15mm|>10mm|>17mm|>17mm|>14mm|>14mm|>14mm|>12mm|>13mm|>13mm|>17mm|>12mm|>8mm|>12mm|>12mm|>13mm|>13mm|>13mm|>15mm|>17mm;"
			astrCabec = ""
			astrCabec = astrCabec & "Classe|" '1
			astrCabec = astrCabec & "NºCons|" '2
			astrCabec = astrCabec & "KWh Fat|" '3
			astrCabec = astrCabec & "Forn.Liquido|" '4
			astrCabec = astrCabec & "Bandeira|" '5
			astrCabec = astrCabec & "ICMS|" '6
			astrCabec = astrCabec & "Pis|" '7
			astrCabec = astrCabec & "Cofins|" '8
			astrCabec = astrCabec & "Reativo|" '9
			astrCabec = astrCabec & "Reverso|" '9
			astrCabec = astrCabec & "Importe|" '10
			astrCabec = astrCabec & "Ilum.Pub|" '11
			astrCabec = astrCabec & "J.Eletr.|" '12
			astrCabec = astrCabec & "Taxados|" '13
			astrCabec = astrCabec & "Diversos|" '14
			astrCabec = astrCabec & "Convenios|" '15
			'astrCabec = astrCabec & "ECE/Mult/icms|" '16
			'astrCabec = astrCabec & "ICMS ECE|" '17
			astrCabec = astrCabec & "Reverso|" '18
			astrCabec = astrCabec & "Retenção|" '19
			astrCabec = astrCabec & "Sub.Trib.|" '20
			astrCabec = astrCabec & "Total" '21
			'Para preencher com zeros
			astrTemp = "|0|0|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00"
			'Para preencher com brancos
			astrBranco = "|||||||||||||||||||"
		Else
			astrTamcol = "<15mm|>10mm|>17mm|>17mm|>14mm|>14mm|>14mm|>12mm|>13mm|>17mm|>12mm|>12mm|>12mm|>12mm|>11mm|>13mm|>10mm|>13mm|>15mm|>17mm;"
			astrCabec = ""
			astrCabec = astrCabec & "Classe|" '1
			astrCabec = astrCabec & "NºCons|" '2
			astrCabec = astrCabec & "KWh Fat|" '3
			astrCabec = astrCabec & "Forn.Liquido|" '4
			astrCabec = astrCabec & "Bandeira|" '5
			astrCabec = astrCabec & "ICMS|" '6
			astrCabec = astrCabec & "Pis|" '7
			astrCabec = astrCabec & "Cofins|" '8
			astrCabec = astrCabec & "Reativo|" '9
			astrCabec = astrCabec & "Importe|" '10
			astrCabec = astrCabec & "Ilum.Pub|" '11
			astrCabec = astrCabec & "J.Eletr.|" '12
			astrCabec = astrCabec & "Taxados|" '13
			astrCabec = astrCabec & "Diversos|" '14
			astrCabec = astrCabec & "Convenios|" '15
			astrCabec = astrCabec & "ECE/Mult/icms|" '16
			'astrCabec = astrCabec & "ICMS ECE|" '17
			astrCabec = astrCabec & "Reverso|" '18
			astrCabec = astrCabec & "Retenção|" '19
			astrCabec = astrCabec & "Sub.Trib.|" '20
			astrCabec = astrCabec & "Total" '21
			'Para preencher com zeros
			astrTemp = "|0|0|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00"
			'Para preencher com brancos
			astrBranco = "|||||||||||||||||||"
		End If
	End Sub
	
	Public Sub RelatorioPaisagemMontaTabela(ByVal aintTipoRel As Short, ByRef astrTabela As String, ByRef arstFaturas As ADODB.Recordset, ByRef aProgressBar As System.Windows.Forms.ProgressBar, ByRef alblInfo As System.Windows.Forms.Label, ByRef alblPerc As System.Windows.Forms.Label, Optional ByVal adteBase As Date = #12:00:00 AM#, Optional ByVal adteFim As Date = #12:00:00 AM#, Optional ByVal ablnPdd As Boolean = False, Optional ByVal adblDiasRe As Double = 0, Optional ByVal adblDiasBx As Double = 0, Optional ByVal adblDiasRu As Double = 0, Optional ByVal adblDiasIn As Double = 0, Optional ByVal adblDiasCo As Double = 0, Optional ByVal adblDiasPP As Double = 0, Optional ByVal adblDiasIP As Double = 0, Optional ByVal adblDiasSP As Double = 0, Optional ByVal adblDiasCP As Double = 0, Optional ByVal astrSituacao As String = "LDCI", Optional ByVal aintSomenteMicroGeradoras As Short = 0)
		Dim lstrSql As String
		Dim lstrSqlIte As String
		Dim lstrSqlRes As String
		Dim llngSql As Integer
		'
		Dim lblnContinua As Boolean
		Dim lblnInclui As Boolean
		Dim lintTpRec As Short
		Dim ldblResNumNf As Double
		' Valores
		Dim lstrItens As String
		Dim ldblVlrAux As Double
		Dim ldblVkWhTU As Double
		Dim ldblVkWhTE As Double
		Dim ldblVkWhTEBan As Double
		Dim ldblVkWhNormal As Double
		Dim ldblVkWhNormalBan As Double
		Dim ldblVReverso As Double
		Dim ldblVReversoImporte As Double
		Dim ldblVlrTot As Double
		Dim ldblQkWhFatPo As Double
		Dim ldblQKwhFatfp As Double
		Dim ldblVkWhFatPo As Double
		Dim ldblVKwhFatfp As Double
		'
		Dim ldblQkWhFatPoBan As Double
		Dim ldblQKwhFatfpBan As Double
		Dim ldblVkWhFatPoBan As Double
		Dim ldblVKwhFatfpBan As Double
		'
		Dim ldblVlrDiv As Double
		Dim ldblVlrTax As Double
		Dim ldblVlrCov As Double
		Dim ldblVlrDes As Double
		Dim ldblVlrRet As Double
		Dim ldblVlrOut As Double
		Dim ldlbCodSrv As Double
		'PLD - antigo
		Dim ldblVkWhDevPo As Double
		Dim ldblVKwhDevfp As Double
		Dim ldblVkWhUtrPo As Double
		Dim ldblVKwhUtrfp As Double
		'Dados de identificação
		Dim ldblNumNf As Double
		Dim lstrMesAno As String
		Dim llngAnoMes As Integer
		Dim lstrSituacao As String
		Dim lstrNome As String
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim llngUnidId As Integer
		Dim llngConsId As String
		Dim lstrClas As String
		Dim lstrGrupo As String
		Dim ldtePgto As Date
		Dim ldteVcto As Date
		Dim llngDias As Integer
		Dim lstrLivEsp As String
		Dim ldteFlgSt As Date
		Dim lstrFlgst As String
		Dim lstrIdBanco As String
		Dim lstrNmBanco As String
		'Substituição Tributária
		Dim ldblIcmsSt As Double
		Dim ldblPisSt As Double
		Dim ldblCofinsSt As Double
		'cooperativas
		Dim ldblVConsCoperativaP As Double
		Dim ldblVConsCoperativaFP As Double
		Dim ldblVDemP As Double
		Dim ldblVDemFP As Double
		Dim ldblVPis As Double
		Dim ldblVCofins As Double
		'
		Dim lintMicroGeracaoConvenioSefaz As Short
		'
		Dim ldteAux As Double
		Dim lrstAuxRe As ADODB.Recordset
		lrstAuxRe = New ADODB.Recordset
		'
		alblInfo.Text = " Preparando as tabelas auxiliares..."
		Call alblInfo.Refresh()
		ldteAux = BuscaDataBD.ToOADate
		llngSql = 0
		lblnContinua = True
		'Elimina as tabelas temporarias caso existir
		Call Executar("DROP TEMPORARY TABLE IF EXISTS " & astrTabela)
		Call Executar("DROP TEMPORARY TABLE IF EXISTS " & astrTabela & "_itens")
		Call Executar("DROP TABLE IF EXISTS " & astrTabela)
		Call Executar("DROP TABLE IF EXISTS " & astrTabela & "_itens")
		
		'Criando as tabelas temporarias
		'Tabela Mãe
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TEMPORARY TABLE IF NOT EXISTS " & astrTabela & "(`tipo`   INTEGER  DEFAULT 0,"
		'lstrSql = lstrSql & "CREATE  TABLE IF NOT EXISTS " & astrTabela & "(`tipo`   INTEGER  DEFAULT 0,"
		'
		lstrSql = lstrSql & " `datapag`     DATE          DEFAULT '0000-00-00',"
		lstrSql = lstrSql & " `classe`      VARCHAR(4)    DEFAULT NULL,"
		lstrSql = lstrSql & " `ta`          VARCHAR(3)    DEFAULT NULL,"
		lstrSql = lstrSql & " `banco`       INTEGER       DEFAULT '0',"
		lstrSql = lstrSql & " `nmbanco`     VARCHAR(40)   DEFAULT NULL,"
		'
		lstrSql = lstrSql & " `kwh_fat`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `consumo`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `bandeira`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icms`        DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `reativo`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `tip`         DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `dif_eltrbr`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `ece`         DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icms_ece`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `multa_ece`   DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `pis`         DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `cofins`      DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icmspiscof`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `taxados`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `diversos`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `convenios`   DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `retencao`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_dev_p`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_dev_fp` DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_utr_p`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_utr_fp` DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icms_st`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `pis_st`      DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `cofins_st`   DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `reverso`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `reversoimporte`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `total`       DECIMAL(14,2) DEFAULT '0.00',"
		'
		lstrSql = lstrSql & "INDEX `idx_classe` (`classe`),"
		lstrSql = lstrSql & "INDEX `idx_banco` (`banco`)"
		'
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"
		Call Executar(lstrSql)
		'Tabela Filho
		lstrSql = "CREATE TEMPORARY TABLE IF NOT EXISTS " & astrTabela & "_itens" & "(`tipo`   INTEGER  DEFAULT 0,"
		'lstrSql = "CREATE  TABLE IF NOT EXISTS " & astrTabela & "_itens" & "(`tipo`   INTEGER  DEFAULT 0,"
		lstrSql = lstrSql & " `datapag` DATE DEFAULT '0000-00-00',"
		lstrSql = lstrSql & " `codigo` VARCHAR(10)  DEFAULT '0000000000',"
		lstrSql = lstrSql & " `valor` DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " INDEX `idxcod` (`codigo`)"
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"
		Call Executar(lstrSql)
		With arstFaturas
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso(aProgressBar, alblPerc, alblInfo, 0, .RecordCount)
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldblNumNf = CDbl(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 = Trim(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 = CInt(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"'
						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"'
						llngUnidId = CInt(IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						llngConsId = CStr(CInt(IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value)))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldtePgto = CDate(IIf(IsDbNull(.Fields("datapag").value), ldteAux, .Fields("datapag").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"'
						ldteVcto = CDate(IIf(IsDbNull(.Fields("datavenc").value), ldteAux, .Fields("datavenc").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"'
						lstrClas = Trim(IIf(IsDbNull(.Fields("codclas").value), "0000", .Fields("codclas").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrGrupo = UCase(Mid(Trim(IIf(IsDbNull(.Fields("ta").value), "00I0", .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"'
						lstrFlgst = Trim(IIf(IsDbNull(.Fields("flgst").value), "", .Fields("flgst").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldteFlgSt = CDate(IIf(IsDbNull(.Fields("datast").value), "1900-01-01", .Fields("datast").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"'
						lintMicroGeracaoConvenioSefaz = CShort(IIf(IsDbNull(.Fields("micro_geracao_convenio_sefaz").value), 0, .Fields("micro_geracao_convenio_sefaz").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"'
						lstrIdBanco = Trim(IIf(IsDbNull(.Fields("banarrec").value), "000", .Fields("banarrec").value))
						lstrNmBanco = BuscaNMBanco(lstrIdBanco)
						'
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrItens = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
						'Verifica se este recebimento estava na reserva
						'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"'
						ldblResNumNf = CDbl(IIf(IsDbNull(.Fields("reserva_numnf").value), 0, .Fields("reserva_numnf").value))
						lintTpRec = IIf((ldblResNumNf > 0), 2, 1)
						'lintTpRec = IIf(VerificaReserva(ldblNumNF, lstrMesAno, lstrZona, lstrLivro, lstrConta) = True, 2, 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"'
						ldblVlrTot = CDbl(IIf(IsDbNull(.Fields("valorliq").value), 0, .Fields("valorliq").value))
						If ldblVlrTot = 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"'
							ldblVlrTot = CDbl(IIf(IsDbNull(.Fields("valortot").value), 0, .Fields("valortot").value))
						End If
						'
						'Aqui começa o controle se for PDD
						If ablnPdd Then
							lstrNome = RetornaNomeConsumidor(lstrZona, lstrLivro, lstrConta, llngUnidId, CInt(llngConsId), lstrGrupo, lstrSituacao)
							'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
							llngDias = DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteVcto, adteBase)
							lstrSqlRes = ""
							lstrSqlRes = lstrSqlRes & "SELECT `numnf`,`mesano`,`zona`,`livro`,`conta`,`datares` " & vbCrLf
							lstrSqlRes = lstrSqlRes & "  FROM `reserva` " & vbCrLf
							lstrSqlRes = lstrSqlRes & " WHERE `numnf`    =  " & ldblNumNf & " " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `mesano`   = '" & lstrMesAno & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `zona`     = '" & lstrZona & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `livro`    = '" & lstrLivro & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `conta`    = '" & lstrConta & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `datares` <= '" & VB6.Format(adteBase, "yyyy-MM-dd") & "' "
							Call Conectar(lstrSqlRes, lrstAuxRe)
							With lrstAuxRe
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									lblnContinua = (.RecordCount = 0)
									Call .Close()
								End If
							End With
							If lblnContinua Then
								If CShort(Left(lstrClas, 1)) = 9 Then
									If lstrClas = "9101" Then
										lblnContinua = (adblDiasRe >= 0 And llngDias > adblDiasRe)
									Else
										lblnContinua = (adblDiasBx >= 0 And llngDias > adblDiasBx)
									End If
								ElseIf CShort(Left(lstrClas, 1)) <= 3 Then 
									lblnContinua = (adblDiasIn >= 0 And llngDias > adblDiasIn)
								ElseIf CShort(Left(lstrClas, 1)) = 4 Then 
									lblnContinua = (adblDiasCo >= 0 And llngDias > adblDiasCo)
								ElseIf CShort(Left(lstrClas, 1)) = 5 Then 
									lblnContinua = (adblDiasRu >= 0 And llngDias > adblDiasRu)
								ElseIf CShort(Left(lstrClas, 1)) = 6 Then 
									If CShort(Left(lstrClas, 2)) = 64 Then
										lblnContinua = (adblDiasIP >= 0 And llngDias > adblDiasIP)
									Else
										lblnContinua = (adblDiasPP >= 0 And llngDias > adblDiasPP)
									End If
								ElseIf CShort(Left(lstrClas, 1)) = 7 Then 
									lblnContinua = (adblDiasSP >= 0 And llngDias > adblDiasSP)
								ElseIf CShort(Left(lstrClas, 1)) = 8 Then 
									lblnContinua = (adblDiasCP >= 0 And llngDias > adblDiasCP)
								End If
								If lblnContinua Then
									lblnContinua = (InStr(1, astrSituacao, lstrSituacao) > 0)
								End If
							End If
						End If
						If lblnContinua Then
							'
							ldblVkWhDevPo = BuscaVlrItem(lstrItens, "0302020041") 'Valor Consumo devolvido na p
							ldblVKwhDevfp = BuscaVlrItem(lstrItens, "0302020042") 'Valor Consumo devolvido na fp
							ldblVkWhUtrPo = BuscaVlrItem(lstrItens, "0302020043") 'Valor Consumo ultrapassado na p
							ldblVKwhUtrfp = BuscaVlrItem(lstrItens, "0302020044") 'Valor Consumo ultrapassado na fp
							'
							'Montando o script até 300 Inserções por vez no banco de dados
							If llngSql >= 300 Then
								Call Executar(lstrSql)
								llngSql = 0
								lstrSql = ""
							End If
							If llngSql = 0 Then
								lstrSql = "INSERT INTO " & astrTabela & " VALUES ("
							Else
								lstrSql = lstrSql & ",("
							End If
							lstrSql = lstrSql & lintTpRec & ","
							'Resumo Diário
							If aintTipoRel = 1 Then
								lstrSql = lstrSql & "'" & VB6.Format(ldtePgto, "yyyy-MM-dd") & "'," 'datapag
							Else
								lstrSql = lstrSql & "'0000-00-00'," 'datapag
							End If
							lstrSql = lstrSql & "'" & lstrClas & "'," ' classe
							If Left(lstrGrupo, 2) = "AL" Then
								lstrLivEsp = BuscaLivreEspecial(llngUnidId)
							Else
								lstrLivEsp = ""
							End If
							lstrSql = lstrSql & "'" & lstrGrupo & lstrLivEsp & "'," 'ta
							'Banco
							lstrSql = lstrSql & "'" & CShort(IIf(IsNumeric(lstrIdBanco), lstrIdBanco, 0)) & "'," ' banco
							lstrSql = lstrSql & "'" & lstrNmBanco & "'," ' nome banco
							'
							
							'Quantidade de Consumo Faturado - kwh fat
							ldblVlrAux = 0
							If Left(lstrGrupo, 1) = "A" Then
								ldblVlrAux = BuscaVlrItem(lstrItens, "0302010009")
								ldblQkWhFatPo = BuscaVlrItem(lstrItens, "0302010010") + BuscaVlrItem(lstrItens, "0302010060") 'Consumo Lido na Ponta
								ldblQKwhFatfp = BuscaVlrItem(lstrItens, "0302010011") + BuscaVlrItem(lstrItens, "0302010061") 'Consumo Lido na Fora Ponta
								ldblVlrAux = ldblVlrAux + ldblQkWhFatPo + ldblQKwhFatfp
							Else
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "030201009") + BuscaVlrItem(lstrItens, "030201019") + BuscaVlrItem(lstrItens, "030201029") + BuscaVlrItem(lstrItens, "030201039") 'por Faixa de consumo Grupo B
							End If
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Valor do Consumo Faturado - consumo
							ldblVlrAux = 0
							ldblVkWhNormal = 0
							ldblVReverso = 0
							ldblVReversoImporte = 0
							ldblVkWhNormalBan = 0
							ldblVkWhFatPoBan = 0
							ldblVKwhFatfpBan = 0
							If Left(lstrGrupo, 1) = "A" Then
								ldblVkWhFatPo = 0
								ldblVKwhFatfp = 0
								ldblVkWhFatPo = BuscaVlrItem(lstrItens, "0302020010") + BuscaVlrItem(lstrItens, "0302020060") 'Consumo faturado p
								ldblVKwhFatfp = BuscaVlrItem(lstrItens, "0302020011") + BuscaVlrItem(lstrItens, "0302020061") 'Consumo faturado fp
								ldblVkWhFatPoBan = BuscaVlrItem(lstrItens, "0302120010") 'bandeira
								ldblVKwhFatfpBan = BuscaVlrItem(lstrItens, "0302120011") 'bandeira
								'consumo
								ldblVlrAux = BuscaVlrItem(lstrItens, "0302020009") 'convecional
								ldblVkWhNormalBan = BuscaVlrItem(lstrItens, "0302120009")
								ldblVlrAux = ldblVlrAux + ldblVkWhFatPo 'na Ponta
								ldblVlrAux = ldblVlrAux + ldblVKwhFatfp 'Fora da Ponta
								'demanda
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020015") 'convecional
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020021") 'na Ponta
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020023") '+ BuscaVlrItem(lstrItens, "0302020072") 'Fora da Ponta
								'demanda Ultrapassagem
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020018") 'convencioal
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020022") 'ponta
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020026") 'fora ponta
								If lintMicroGeracaoConvenioSefaz = 0 Then
									ldblVReversoImporte = BuscaVlrItem(lstrItens, "0302020994")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302020993")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022010")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022110")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022011")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022111")
								End If
							Else
								ldblVkWhTU = BuscaVlrItem(lstrItens, "030302009")
								ldblVkWhTE = BuscaVlrItem(lstrItens, "030402009")
								ldblVkWhTEBan = BuscaVlrItem(lstrItens, "030412009")
								ldblVkWhNormal = BuscaVlrItem(lstrItens, "030202009") + BuscaVlrItem(lstrItens, "030202019") + BuscaVlrItem(lstrItens, "030202029") + BuscaVlrItem(lstrItens, "030202039")
								ldblVkWhNormalBan = BuscaVlrItem(lstrItens, "030212009") + BuscaVlrItem(lstrItens, "0302120190") + BuscaVlrItem(lstrItens, "0302120290") + BuscaVlrItem(lstrItens, "0302120390")
								If lintMicroGeracaoConvenioSefaz = 0 Then
									ldblVReversoImporte = BuscaVlrItem(lstrItens, "0302020994")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302020993")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022010")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022110")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022011")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022111")
								Else
									If aintSomenteMicroGeradoras = 1 Then
										ldblVReverso = BuscaVlrItem(lstrItens, "0302020994")
										ldblVReverso = ldblVReverso + BuscaVlrItem(lstrItens, "0302020993")
									End If
								End If
								If ldblVReverso = 0 Then
									If (ldblVkWhTU + ldblVkWhTE) > 0 Then
										ldblVReverso = ldblVkWhNormal - (ldblVkWhTU + ldblVkWhTE)
										If ldblVkWhNormal - (ldblVkWhTU + ldblVkWhTE) > 0 Then
											ldblVReverso = ldblVReverso + (ldblVkWhNormalBan - ldblVkWhTEBan)
										End If
									End If
								End If
								If aintSomenteMicroGeradoras = 0 Then If llngAnoMes >= CDbl("201601") Then ldblVReverso = 0
								ldblVlrAux = ldblVlrAux + ldblVkWhNormal 'consumo
							End If
							If Left(lstrGrupo, 2) = "AL" Then
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302060021") 'dif.Demanda p
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302060025") 'dif.Demanda fp
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020051") 'Desconto Provisorio
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020052") 'Desconto real
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020053") 'diferença a devolver
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020054") 'diferença a cobrar
								
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020055") 'Desconto Provisorio
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020056") 'Desconto real
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020057") 'diferença a devolver
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020058") 'diferença a cobrar
							End If
							If Left(lstrGrupo, 2) = "AV" Then
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302060023") 'diferença consumo fora ponta
							End If
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr(ldblVkWhFatPoBan + ldblVKwhFatfpBan + ldblVkWhNormalBan), ",", ".") & "," 'Bandeira
							'
							'Icms 1 e 2
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302030009") + BuscaVlrItem(lstrItens, "0302062009")
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302030010")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							ldblVlrAux = 0
							'
							'Reativo
							If Left(lstrGrupo, 1) = "A" Then
								ldblVlrAux = BuscaVlrItem(lstrItens, "0302020012") 'Consumo
							End If
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020031") 'Demanda
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020013") 'Consumo ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020014") 'Consumo fora ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020027") 'Demanda ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020028") 'Demanda fora ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302990044") 'Consumo Reservado
							If Left(lstrGrupo, 1) = "B" Then
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020012") 'Consumo Reativo
							End If
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Contribuição de Iluminação Pública
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0301000003")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Eletrobras (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302990041")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'ECE (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302070002") '0302070005
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'ICMS ECE (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302030006")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Multa ECE (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302050004")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Pis
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302020029") + BuscaVlrItem(lstrItens, "0302062029")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Cofins
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302020030") + BuscaVlrItem(lstrItens, "0302062030")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Icms Sob Pis e Cofins
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302030012")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Convenios, Diversos e Taxados
							ldblVlrDiv = 0
							ldblVlrTax = 0
							ldblVlrCov = 0
							ldblVlrDes = 0
							ldblVlrRet = 0
							ldblVlrOut = 0
							ldblIcmsSt = 0
							ldblPisSt = 0
							ldblCofinsSt = 0
							
							'coperativas
							ldblVConsCoperativaP = 0
							ldblVConsCoperativaFP = 0
							ldblVDemP = 0
							ldblVDemFP = 0
							ldblVPis = 0
							ldblVCofins = 0
							'
							For IndCodServ = 1 To CInt(Len(Trim(lstrItens)))
								lblnInclui = False
								Call BuscaCodItem(IndCodServ, lstrItens)
								ldlbCodSrv = CDbl(Replace(IIf(IsNumeric(VlrCodServ), VlrCodServ, 0), ".", ","))
								If (ldlbCodSrv < 0) And (Mid(codserv, 1, 4) <> "9080") And lstrGrupo <> "AX" Then
									ldlbCodSrv = ldlbCodSrv * -1
								End If
								'
								'Taxas
								If (Mid(codserv, 1, 4) = "9040") Or (Mid(codserv, 1, 4) = "9050") Then
									ldblVlrTax = ldblVlrTax + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Outros (EUSD - MUST
								If codserv = "0302020032" Then
									ldblVlrOut = ldblVlrOut + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Diversos
								If Mid(codserv, 1, 4) = "9080" Then
									ldblVlrDiv = ldblVlrDiv + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Convenios
								If Mid(codserv, 1, 4) = "9030" Then
									ldblVlrCov = ldblVlrCov + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Descontos
								If (Mid(codserv, 1, 4) = "9070") And (codserv <> "9070000088") Then
									ldblVlrDes = ldblVlrDes + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Retencoes
								If (codserv = "9070000088") Or (Mid(codserv, 1, 4) = "9090") Then
									ldblVlrRet = ldblVlrRet + ldlbCodSrv
									lblnInclui = True
								End If
								If (codserv = "0302020051") Or (codserv = "0302020052") Or (codserv = "0302020053") Or (codserv = "0302020054") Or (codserv = "0302020055") Or (codserv = "0302020056") Or (codserv = "0302020057") Or (codserv = "0302020058") Then
									If CDbl(Right(codserv, 1)) <> 4 And CDbl(Right(codserv, 1)) <> 8 Then
										If ldlbCodSrv > 0 Then
											ldlbCodSrv = ldlbCodSrv * (-1)
										End If
									End If
									lblnInclui = True
								End If
								'
								'Substituição Tributária
								If (codserv = "0602020001" Or codserv = "0602020002" Or codserv = "0602020003") Then
									If codserv = "0602020001" Then
										ldblIcmsSt = ldblIcmsSt + BuscaVlrItem(lstrItens, "0602020001") + BuscaVlrItem(lstrItens, "0604020001")
									ElseIf codserv = "0602020002" Then 
										ldblPisSt = ldblPisSt + BuscaVlrItem(lstrItens, "0602020002") + BuscaVlrItem(lstrItens, "0604020002")
									ElseIf codserv = "0602020003" Then 
										ldblCofinsSt = ldblCofinsSt + BuscaVlrItem(lstrItens, "0602020003") + BuscaVlrItem(lstrItens, "0604020003")
									End If
									lblnInclui = True
								End If
								'COOPERATIVA
								If CDbl(lstrClas) = 5201 Then
									If codserv = "0302020010" Then
										ldblVConsCoperativaP = ldblVkWhFatPo + BuscaVlrItem(lstrItens, "0302020010") 'Consumo faturado p
									ElseIf codserv = "0302020011" Then 
										ldblVConsCoperativaFP = ldblVKwhFatfp + BuscaVlrItem(lstrItens, "0302020011") + BuscaVlrItem(lstrItens, "0302020008") 'Consumo Faturado Fp
									ElseIf codserv = "0302020021" Then 
										ldblVDemP = ldblVDemP + BuscaVlrItem(lstrItens, "0302020021") ' demanda na ponta
									ElseIf codserv = "0302020023" Then 
										ldblVDemFP = ldblVDemFP + BuscaVlrItem(lstrItens, "0302020023") + BuscaVlrItem(lstrItens, "0302020015") 'Demanda Fora da Ponta
									ElseIf codserv = "0302020029" Then 
										ldblVPis = ldblVPis + BuscaVlrItem(lstrItens, "0302020029") ' pis
									ElseIf codserv = "0302020030" Then 
										ldblVCofins = ldblVCofins + BuscaVlrItem(lstrItens, "0302020030") ' cofins
									End If
									lblnInclui = True
								End If
								'Inclusão dos itens
								If lblnInclui Then
									lstrSqlIte = "INSERT INTO " & astrTabela & "_itens" & " VALUES ( "
									lstrSqlIte = lstrSqlIte & lintTpRec & ","
									If aintTipoRel = 1 Then 'Resumo Diário
										lstrSqlIte = lstrSqlIte & "'" & VB6.Format(ldtePgto, "yyyy-MM-dd") & "',"
									Else
										lstrSqlIte = lstrSqlIte & "'0000-00-00',"
									End If
									lstrSqlIte = lstrSqlIte & "'" & codserv & "'," & Replace(CStr(ldlbCodSrv), ",", ".") & ")"
									Call Executar(lstrSqlIte)
								End If
							Next IndCodServ
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrTax), ",", ".") & "," 'taxados
							lstrSql = lstrSql & " " & Replace(CStr((ldblVlrDiv + ldblVlrOut) - (ldblVlrDes)), ",", ".") & "," 'diversos
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrCov), ",", ".") & "," 'convenios
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrRet), ",", ".") & "," 'rentecao
							lstrSql = lstrSql & " " & Replace(CStr(ldblVkWhDevPo), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr(ldblVKwhDevfp), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr(ldblVkWhUtrPo), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr(ldblVKwhUtrfp), ",", ".") & ","
							'
							'Substituição Tributária
							lstrSql = lstrSql & " " & Replace(CStr(ldblIcmsSt), ",", ".") & "," 'Icms St
							lstrSql = lstrSql & " " & Replace(CStr(ldblPisSt), ",", ".") & "," 'Pis St
							lstrSql = lstrSql & " " & Replace(CStr(ldblCofinsSt), ",", ".") & "," 'Cofins St
							'
							lstrSql = lstrSql & " " & Replace(CStr(ldblVReverso), ",", ".") & "," 'Reverso
							lstrSql = lstrSql & " " & Replace(CStr(ldblVReversoImporte), ",", ".") & "," 'ReversoImporte
							'
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrTot), ",", ".") & ")"
							'
							llngSql = llngSql + 1
						End If
						Call .MoveNext()
						Call BarraProgresso(aProgressBar, alblPerc, alblInfo, 1)
					End While
					If Trim(lstrSql) <> "" Then Call Executar(lstrSql)
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstAuxRe 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"'
		lrstAuxRe = Nothing
	End Sub
	
	Public Function RelatorioPaisagemMontaTemp(ByVal arstQuery As ADODB.Recordset, Optional ByVal aintSomenteMicroGeradora As Short = 0) As String
		Dim lstrTab As String
		With arstQuery
			lstrTab = ""
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("ncons").value), 0, .Fields("ncons").value), "#,##0") & "|"
			'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("kwhf").value), 0, .Fields("kwhf").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("consumo").value), 0, .Fields("consumo").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("bandeira").value), 0, .Fields("bandeira").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("icms").value), 0, .Fields("icms").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("pis").value), 0, .Fields("pis").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("cofins").value), 0, .Fields("cofins").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("reativo").value), 0, .Fields("reativo").value), "#,##0.00") & "|"
			If aintSomenteMicroGeradora = 1 Then
				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("reversoimporte").value), 0, .Fields("reversoimporte").value), "#,##0.00") & "|"
			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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("importe").value), 0, .Fields("importe").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("tip").value), 0, .Fields("tip").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("eletrobras").value), 0, .Fields("eletrobras").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("taxados").value), 0, .Fields("taxados").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("diversos").value), 0, .Fields("diversos").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("convenios").value), 0, .Fields("convenios").value), "#,##0.00") & "|"
			If aintSomenteMicroGeradora = 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"'
				lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("ece").value), 0, .Fields("ece").value), "#,##0.00") & "|"
			End If
			'lstrTab = lstrTab & Strings.Format(IIf(IsNull(.Fields("icms_ece").Value), 0, .Fields("icms_ece").Value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("reverso").value), 0, .Fields("reverso").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("retencao").value), 0, .Fields("retencao").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("icms_st").value), 0, .Fields("icms_st").value) + IIf(IsDbNull(.Fields("pis_st").value), 0, .Fields("pis_st").value) + IIf(IsDbNull(.Fields("cofins_st").value), 0, .Fields("cofins_st").value), "#,##0.00") & "|"
			'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"'
			lstrTab = lstrTab & VB6.Format(IIf(IsDbNull(.Fields("total").value), 0, .Fields("total").value), "#,##0.00")
		End With
		RelatorioPaisagemMontaTemp = lstrTab
	End Function
	
	Public Function RelatorioPaisagemSelecionaDados(ByVal aintTipoRel As Short, ByVal astrSql As String, ByVal astrTabela As String, Optional ByVal aintSomenteMicroGeradoras As Short = 0) As String
		astrSql = "SELECT "
		If aintTipoRel = 5 Then
			astrSql = astrSql & "`datapag`, `banco`, `nmbanco`, " '0,1,2
			astrSql = astrSql & "IF(LEFT(`classe`, 1) = '9', '01Residencial', "
			astrSql = astrSql & " IF((LEFT(`classe`, 1)+0) BETWEEN 0 AND 3 AND LEFT(`ta`, 2) <> 'AL', '02Industrial', "
			astrSql = astrSql & "  IF(LEFT(`classe`, 1) = '4','03Comercial', "
			astrSql = astrSql & "   IF(LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) <> 'AX', '04Rural', "
			astrSql = astrSql & "    IF(LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) = 'AX', '05Rural Sup.', "
			astrSql = astrSql & "     IF((LEFT(`classe`, 2)+0) BETWEEN 61 AND 63,'06P.Público', "
			astrSql = astrSql & "      IF(LEFT(`classe`, 2) = '64','07I.Pública', "
			astrSql = astrSql & "       IF(LEFT(`classe`, 1) = '7','08S.Público', "
			astrSql = astrSql & "        IF(LEFT(`classe`, 1) = 8,'11C.Próprio',"
			astrSql = astrSql & "         IF(`ta` = 'ALS','09Cons.P.Liv', "
			astrSql = astrSql & "          IF(TRIM(`ta`) = 'AL','10Cons.Livre', 'Indefinido'"
			astrSql = astrSql & "          )"
			astrSql = astrSql & "         )"
			astrSql = astrSql & "        )"
			astrSql = astrSql & "       )"
			astrSql = astrSql & "      )"
			astrSql = astrSql & "     )"
			astrSql = astrSql & "    )"
			astrSql = astrSql & "   )"
			astrSql = astrSql & "  )"
			astrSql = astrSql & " )"
			astrSql = astrSql & ") classes, " '3
		End If
		If aintTipoRel = 1 Then
			astrSql = astrSql & "       datapag, "
		End If
		astrSql = astrSql & "       COUNT(*) ncons, " '1
		astrSql = astrSql & "       SUM(kwh_fat) kwhf, " '2
		If aintSomenteMicroGeradoras = 0 Then
			astrSql = astrSql & "       SUM(COALESCE(consumo, 0) + COALESCE(reversoimporte, 0)) consumo, " '3
		Else
			astrSql = astrSql & "       SUM(COALESCE(consumo, 0)) consumo, " '3
			astrSql = astrSql & "       SUM(COALESCE(reversoimporte, 0)) reversoimporte, " '3
		End If
		astrSql = astrSql & "       SUM(bandeira) bandeira, " '3
		astrSql = astrSql & "       SUM(icms) icms, " '4
		astrSql = astrSql & "       SUM(pis) pis, " '5
		astrSql = astrSql & "       SUM(cofins) cofins, " '6
		astrSql = astrSql & "       SUM(reativo) reativo, " '7
		astrSql = astrSql & "       SUM(COALESCE(consumo, 0) + "
		astrSql = astrSql & "           COALESCE(bandeira, 0) + "
		astrSql = astrSql & "           COALESCE(icms, 0) + "
		astrSql = astrSql & "           COALESCE(pis, 0) + "
		astrSql = astrSql & "           COALESCE(cofins, 0) + "
		astrSql = astrSql & "           COALESCE(reativo, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_dev_p, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_dev_fp, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_utr_p, 0) + "
		astrSql = astrSql & "           COALESCE(reversoimporte, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_utr_fp, 0)) importe, " '8
		astrSql = astrSql & "       SUM(tip) tip, " '9
		astrSql = astrSql & "       SUM(dif_eltrbr) eletrobras, " '10
		astrSql = astrSql & "       SUM(taxados) taxados, " '11
		astrSql = astrSql & "       SUM(diversos) diversos, " '12
		astrSql = astrSql & "       SUM(convenios) convenios, " '13
		astrSql = astrSql & "       SUM(ece)+SUM(multa_ece)+SUM(icms_ece) ece, " '14
		'astrSql = astrSql & "       SUM(icms_ece) icms_ece, " '15
		'astrSql = astrSql & "       SUM(multa_ece) multa_ece, " '14
		If aintSomenteMicroGeradoras = 0 Then
			astrSql = astrSql & "       SUM(COALESCE(reverso,0) - COALESCE(reversoimporte,0)) reverso , " '16
		Else
			astrSql = astrSql & "       SUM(COALESCE(reverso,0)) reverso , " '16
		End If
		astrSql = astrSql & "       SUM(retencao) retencao, " '17
		astrSql = astrSql & "       SUM(icms_st) icms_st, " '18
		astrSql = astrSql & "       SUM(pis_st) pis_st, " '19
		astrSql = astrSql & "       SUM(cofins_st) cofins_st, " '20
		astrSql = astrSql & "       SUM(total) total" '21
		astrSql = astrSql & "  FROM " & astrTabela
		RelatorioPaisagemSelecionaDados = astrSql
	End Function
	
	Private Function BuscaLivreEspecial(ByVal alngUnidId As Integer) As String
		Dim lintLiv As Short
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrSql = ""
		lstrSql = lstrSql & "SELECT `unid_id`, `livreespecial` "
		lstrSql = lstrSql & "  FROM `unida` "
		lstrSql = lstrSql & " WHERE `unida`.`unid_id` = " & alngUnidId
		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"'
					lintLiv = IIf(IsDbNull(.Fields("livreespecial").value), 0, .Fields("livreespecial").value)
				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
		BuscaLivreEspecial = IIf(lintLiv = 1, "S", "")
	End Function
	
	Private Function BuscaNMBanco(ByVal astrBanco As String) As String
		Dim lstrSql As String
		Dim llngBco As Integer
		Dim lstrBco As String
		Dim lstrTip As String
		Dim lstrRet As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		llngBco = CInt(IIf(IsNumeric(astrBanco), astrBanco, 0))
		lstrSql = ""
		lstrSql = lstrSql & "SELECT `ban_id`, `nmbco`, `tpmovto`, `grupo`"
		lstrSql = lstrSql & "  FROM `bancos` "
		lstrSql = lstrSql & " WHERE `ban_id` =" & llngBco
		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"'
					lstrBco = IIf(IsDbNull(.Fields("nmbco").value), "", .Fields("nmbco").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"'
					lstrTip = IIf(IsDbNull(.Fields("tpmovto").value), "N", .Fields("tpmovto").value)
					lstrRet = lstrBco & IIf(lstrTip = "D", "(Débito)", "")
				Else
					lstrRet = ""
				End If
				Call .Close()
			End If
		End With
		BuscaNMBanco = lstrRet
		'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 Function
	
	Public Function RelatorioPaisagemSelecionaNFs(ByRef astrAux As ADODB.Recordset, ByVal adteIni As Date, ByVal adteFim As Date, Optional ByVal ablnReserva As Boolean = False, Optional ByVal ablnSomenteMicroGeradoras As Boolean = False, Optional ByVal ablnSomenteMicroGeradorasSefaz As Boolean = False, Optional ByVal ablnSomenteMicroGeradorasNaoSefaz As Boolean = False) As Boolean
		Dim lstrSql As String
		Dim lblnRet As Boolean
		With astrAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then Call .Close()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`numnf`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano_ref`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`rota`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`zona`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`livro`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`conta`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`cons_id`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`unid_id`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`flgst`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`datapag`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`datavenc`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`datast`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`codclas`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`ta`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`grupo`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`itensserv`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`valorliq`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`valortot`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`banarrec`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`micro_geracao_convenio_sefaz`, " & vbCrLf
			lstrSql = lstrSql & "       `reserva`.`numnf`   `reserva_numnf`, " & vbCrLf
			lstrSql = lstrSql & "       `reserva`.`mesano`  `reserva_mesano`, " & vbCrLf
			lstrSql = lstrSql & "       `reserva`.`datares` `reserva_datares`" & vbCrLf
			lstrSql = lstrSql & "  FROM (`nf` " & vbCrLf
			lstrSql = lstrSql & "        LEFT JOIN `reserva` " & vbCrLf
			lstrSql = lstrSql & "               ON((`reserva`.`numnf` = `nf`.`numnf` AND `reserva`.`mesano` = `nf`.`mesano` AND `reserva`.`datares` <= '" & VB6.Format(adteFim, "yyyy-MM-dd") & "')))" & vbCrLf
			If ablnReserva = False Then
				lstrSql = lstrSql & "   WHERE (`nf`.`flgst` = 'P' AND `datapag` BETWEEN '" & VB6.Format(adteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(adteFim, "yyyy-MM-dd") & "') " & vbCrLf
			Else
				lstrSql = lstrSql & "   WHERE ((`nf`.`flgst` = 'R' AND `datapag` BETWEEN '" & VB6.Format(adteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(adteFim, "yyyy-MM-dd") & "') OR (`nf`.`flgst` <> 'R' AND `reserva`.`datares` BETWEEN '" & VB6.Format(adteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(adteFim, "yyyy-MM-dd") & "')) "
			End If
			If ablnSomenteMicroGeradoras Then
				lstrSql = lstrSql & "   AND (`nf`.`micro_geracao` = 1 OR `nf`.`micro_geracao_distribuida` = 1) "
				If ablnSomenteMicroGeradorasSefaz Then
					lstrSql = lstrSql & "  AND `nf`.`micro_geracao_convenio_sefaz`  = 1"
				ElseIf ablnSomenteMicroGeradorasNaoSefaz Then 
					lstrSql = lstrSql & "  AND `nf`.`micro_geracao_convenio_sefaz`  = 0"
				End If
			End If
			'lstrSql = lstrSql & " and nf.codclas < 4000"
			'lstrSql = lstrSql & " and (nf.micro_geracao = 0 and nf.micro_geracao_distribuida = 0) "
			'lstrSql = lstrSql & " and nf.grupo = 'A' and nf.ta <> '01AL00' and nf.micro_geracao = 0  and nf.micro_geracao_distribuida = 0"
			'lstrSql = lstrSql & " and nf.grupo = 'B' and nf.micro_geracao = 0  and nf.micro_geracao_distribuida = 0"
			'lstrSql = lstrSql & " and nf.ta = '01AL00' "
			Call Conectar(lstrSql, astrAux)
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				lblnRet = (.RecordCount > 0)
			End If
		End With
		RelatorioPaisagemSelecionaNFs = lblnRet
	End Function
	
	Public Function RelatorioEmAbertoSelecionaNFs(ByRef astrAux As ADODB.Recordset, ByVal adteIni As Date, ByVal adteFim As Date, ByVal aintTipoRel As Short) As Boolean
		Dim lstrSql As String
		Dim lblnRet As Boolean
		With astrAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then Call .Close()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nf`.*, " & vbCrLf
			lstrSql = lstrSql & "       0   `reserva_numnf`, " & vbCrLf
			lstrSql = lstrSql & "       ''  `reserva_mesano`, " & vbCrLf
			lstrSql = lstrSql & "       '0000-00-00' `reserva_datares` " & vbCrLf
			lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
			lstrSql = lstrSql & " WHERE (`flgst` <> 'C' OR " & vbCrLf
			lstrSql = lstrSql & "        ( (`flgst`='C' AND `datast` > '" & VB6.Format(adteFim, "yyyy-MM-dd") & "') " & vbCrLf
			'lstrSql = lstrSql & "        OR (`flgst`='C' AND `mesano_ref` <> REPLACE(LEFT(`datast`, 7),'-','') AND `datast` BETWEEN '" & Strings.Format(adteIni, "yyyy-MM-dd") & "' AND '" & Strings.Format(adteFim, "yyyy-MM-dd") & "')" & vbCrLf
			lstrSql = lstrSql & "         )" & vbCrLf
			lstrSql = lstrSql & "        )" & vbCrLf
			If aintTipoRel = 1 Then
				lstrSql = lstrSql & "   AND (`mesano_ref` BETWEEN " & CInt(VB6.Format(adteIni, "yyyyMM")) & " AND " & CInt(VB6.Format(adteFim, "yyyyMM")) & ") " & vbCrLf
			Else
				lstrSql = lstrSql & "   AND `mesano_ref` <= " & CInt(VB6.Format(adteFim, "yyyyMM")) & " " & vbCrLf
			End If
			lstrSql = lstrSql & "   AND (`datapag` > '" & VB6.Format(adteFim, "yyyy-MM-dd") & "' OR `datapag` = '0000-00-00') " & vbCrLf
			lstrSql = lstrSql & "   AND NOT EXISTS(SELECT * " & vbCrLf
			lstrSql = lstrSql & "                    FROM `reserva` " & vbCrLf
			lstrSql = lstrSql & "                   WHERE `reserva`.`numnf`    = `nf`.`numnf`  " & vbCrLf
			lstrSql = lstrSql & "                     AND `reserva`.`mesano`   = `nf`.`mesano` " & vbCrLf
			lstrSql = lstrSql & "                     AND `reserva`.`zona`     = `nf`.`zona`   " & vbCrLf
			lstrSql = lstrSql & "                     AND `reserva`.`livro`    = `nf`.`livro`  " & vbCrLf
			lstrSql = lstrSql & "                     AND `reserva`.`conta`    = `nf`.`conta`  " & vbCrLf
			lstrSql = lstrSql & "                     AND `reserva`.`datares` <= '" & VB6.Format(adteFim, "yyyy-MM-dd") & "')"
			
			'lstrSql = lstrSql & " AND MID(codclas, 1, 1) BETWEEN 0 AND 3 AND codclas <> 100"
			
			Call Conectar(lstrSql, astrAux)
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				lblnRet = (.RecordCount > 0)
			End If
		End With
		RelatorioEmAbertoSelecionaNFs = lblnRet
	End Function
	
	Public Sub RelatorioPaisagemMontaTabelaCergal(ByVal aintTipoRel As Short, ByVal aintRelatorio As Short, ByRef astrTabela As String, ByVal adteBase As Date, ByRef arstFaturas As ADODB.Recordset, ByRef aProgressBar As System.Windows.Forms.ProgressBar, ByRef alblInfo As System.Windows.Forms.Label, ByRef alblPerc As System.Windows.Forms.Label, ByVal adteFim As Date, Optional ByVal ablnPdd As Boolean = False, Optional ByVal adblDiasRe As Double = 0, Optional ByVal adblDiasBx As Double = 0, Optional ByVal adblDiasRu As Double = 0, Optional ByVal adblDiasIn As Double = 0, Optional ByVal adblDiasCo As Double = 0, Optional ByVal adblDiasPP As Double = 0, Optional ByVal adblDiasIP As Double = 0, Optional ByVal adblDiasSP As Double = 0, Optional ByVal adblDiasCP As Double = 0, Optional ByVal astrSituacao As String = "LDCI")
		
		'aintTipoRel
		'0 = Total Geral
		'1 = Resumo diário
		'2 = Zona e nome da cidade
		'3 = Banco e nome do banco
		'5 = Recebimentos por dia/banc/classe
		'aintRelatorio
		'0 = Arrecadação
		'1 = Em abertos
		Dim lstrSql As String
		Dim lstrSqlIte As String
		Dim lstrSqlRes As String
		Dim llngSql As Integer
		'
		Dim lblnContinua As Boolean
		Dim lblnInclui As Boolean
		Dim lintTpRec As Short
		Dim ldblResNumNf As Double
		' Valores
		Dim lstrItens As String
		Dim ldblVlrAux As Double
		Dim ldblVkWhTU As Double
		Dim ldblVkWhTE As Double
		Dim ldblVkWhTEBan As Double
		Dim ldblVkWhNormal As Double
		Dim ldblVkWhNormalBan As Double
		Dim ldblVReverso As Double
		Dim ldblVReversoImporte As Double
		Dim ldblVlrTot As Double
		Dim ldblQkWhFatPo As Double
		Dim ldblQKwhFatfp As Double
		Dim ldblVkWhFatPo As Double
		Dim ldblVKwhFatfp As Double
		'
		Dim ldblQkWhFatPoBan As Double
		Dim ldblQKwhFatfpBan As Double
		Dim ldblVkWhFatPoBan As Double
		Dim ldblVKwhFatfpBan As Double
		'
		Dim ldblVlrDiv As Double
		Dim ldblVlrTax As Double
		Dim ldblVlrCov As Double
		Dim ldblVlrDes As Double
		Dim ldblVlrRet As Double
		Dim ldblVlrOut As Double
		Dim ldlbCodSrv As Double
		'PLD - antigo
		Dim ldblVkWhDevPo As Double
		Dim ldblVKwhDevfp As Double
		Dim ldblVkWhUtrPo As Double
		Dim ldblVKwhUtrfp As Double
		'Dados de identificação
		Dim ldblNumNf As Double
		Dim lstrMesAno As String
		Dim lstrSituacao As String
		Dim lstrNome As String
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim llngUnidId As Integer
		Dim llngConsId As Integer
		Dim lstrClas As String
		Dim lstrGrupo As String
		Dim ldtePgto As Date
		Dim ldteVcto As Date
		Dim llngDias As Integer
		Dim lstrLivEsp As String
		Dim ldteFlgSt As Date
		Dim lstrFlgst As String
		Dim lintMpr As Short 'Usado para caso tiver alguma fatura cancelada no mês de faturamento de outro mês
		Dim lstrIdBanco As String
		Dim lstrNmBanco As String
		Dim lstrNmCidade As String
		'Substituição Tributária
		Dim ldblIcmsSt As Double
		Dim ldblPisSt As Double
		Dim ldblCofinsSt As Double
		'cooperativas
		Dim ldblVConsCoperativaP As Double
		Dim ldblVConsCoperativaFP As Double
		Dim ldblVDemP As Double
		Dim ldblVDemFP As Double
		Dim ldblVPis As Double
		Dim ldblVCofins As Double
		'
		Dim lintParcialmenteCativo As Short
		Dim lintMicroGeracaoConvenioSefaz As Short
		'
		Dim ldteAux As Double
		Dim lrstAuxRe As ADODB.Recordset
		lrstAuxRe = New ADODB.Recordset
		'
		alblInfo.Text = " Preparando as tabelas auxiliares..."
		Call alblInfo.Refresh()
		ldteAux = BuscaDataBD.ToOADate
		lblnContinua = True
		llngSql = 0
		'Elimina as tabelas temporarias caso existir
		Call Executar("DROP TEMPORARY TABLE IF EXISTS " & astrTabela)
		Call Executar("DROP TEMPORARY TABLE IF EXISTS " & astrTabela & "_itens")
		Call Executar("DROP TABLE IF EXISTS " & astrTabela)
		Call Executar("DROP TABLE IF EXISTS " & astrTabela & "_itens")
		
		'Criando as tabelas temporarias
		'Tabela Mãe
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TEMPORARY TABLE IF NOT EXISTS " & astrTabela & "(`tipo`   INTEGER  DEFAULT 0,"
		'lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS " & astrTabela & "(`tipo`   INTEGER  DEFAULT 0,"
		'
		lstrSql = lstrSql & " `dataaux`     DATE          DEFAULT '0000-00-00',"
		lstrSql = lstrSql & " `diasaux`     INT(11)       DEFAULT '0',"
		lstrSql = lstrSql & " `classe`      VARCHAR(4)    DEFAULT NULL,"
		lstrSql = lstrSql & " `ta`          VARCHAR(3)    DEFAULT NULL,"
		lstrSql = lstrSql & " `banco`       INT(11)       DEFAULT '0',"
		lstrSql = lstrSql & " `nmbanco`     VARCHAR(100)   DEFAULT NULL,"
		lstrSql = lstrSql & " `zona`        VARCHAR(3)    DEFAULT '001',"
		lstrSql = lstrSql & " `nmcidade`    VARCHAR(60)   DEFAULT NULL,"
		'
		lstrSql = lstrSql & " `kwh_fat`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `consumo`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `bandeira`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icms`        DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `reativo`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `tip`         DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `dif_eltrbr`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `ece`         DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icms_ece`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `multa_ece`   DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `pis`         DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `cofins`      DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icmspiscof`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `taxados`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `diversos`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `convenios`   DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `retencao`    DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_dev_p`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_dev_fp` DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_utr_p`  DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `vkwh_utr_fp` DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `icms_st`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `pis_st`      DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `cofins_st`   DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `reverso`     DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " `total`       DECIMAL(14,2) DEFAULT '0.00',"
		'
		lstrSql = lstrSql & " INDEX `idx_classe` (`classe`),"
		lstrSql = lstrSql & " INDEX `idx_banco` (`banco`),"
		lstrSql = lstrSql & " INDEX `idx_zona` (`zona`)"
		'
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"
		Call Executar(lstrSql)
		'Tabela Filho
		lstrSql = "CREATE TEMPORARY TABLE " & astrTabela & "_itens" & "(`tipo`   INTEGER  DEFAULT 0,"
		'lstrSql = "CREATE TABLE " & astrTabela & "_itens" & "(`tipo`   INTEGER  DEFAULT 0,"
		lstrSql = lstrSql & " `dataaux` DATE DEFAULT '0000-00-00',"
		lstrSql = lstrSql & " `diasaux` INT(11)       DEFAULT '0',"
		lstrSql = lstrSql & " `banco`   INT(11)       DEFAULT '0',"
		lstrSql = lstrSql & " `zona`    VARCHAR(3)    DEFAULT '001',"
		lstrSql = lstrSql & " `codigo`  VARCHAR(10)  DEFAULT '0000000000',"
		lstrSql = lstrSql & " `valor`   DECIMAL(14,2) DEFAULT '0.00',"
		lstrSql = lstrSql & " INDEX `idxcod` (`codigo`),"
		lstrSql = lstrSql & " INDEX `idx_banco` (`banco`),"
		lstrSql = lstrSql & " INDEX `idx_zona` (`zona`)"
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1;"
		Call Executar(lstrSql)
		With arstFaturas
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso(aProgressBar, alblPerc, alblInfo, 0, .RecordCount)
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldblNumNf = CDbl(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 = Trim(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"'
						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"'
						llngUnidId = CInt(IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						llngConsId = CInt(IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldtePgto = CDate(IIf(IsDbNull(.Fields("datapag").value), ldteAux, .Fields("datapag").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"'
						ldteVcto = CDate(IIf(IsDbNull(.Fields("datavenc").value), ldteAux, .Fields("datavenc").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"'
						lstrClas = Trim(IIf(IsDbNull(.Fields("codclas").value), "0000", .Fields("codclas").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrGrupo = UCase(Mid(Trim(IIf(IsDbNull(.Fields("ta").value), "00I0", .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"'
						lstrFlgst = Trim(IIf(IsDbNull(.Fields("flgst").value), "", .Fields("flgst").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						ldteFlgSt = CDate(IIf(IsDbNull(.Fields("datast").value), "1900-01-01", .Fields("datast").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lintParcialmenteCativo = IIf(IsDbNull(.Fields("parcialmente_cativo").value), 0, .Fields("parcialmente_cativo").value)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lintMicroGeracaoConvenioSefaz = IIf(IsDbNull(.Fields("micro_geracao_convenio_sefaz").value), 0, .Fields("micro_geracao_convenio_sefaz").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"'
						lstrIdBanco = Trim(IIf(IsDbNull(.Fields("banarrec").value), "000", .Fields("banarrec").value))
						lstrNmBanco = BuscaNMBanco(lstrIdBanco)
						lstrNmCidade = BuscaNmCidade(lstrZona, "", 0)
						'
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrItens = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
						'Verifica se este recebimento estava na reserva
						'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"'
						ldblResNumNf = CDbl(IIf(IsDbNull(.Fields("reserva_numnf").value), 0, .Fields("reserva_numnf").value))
						lintTpRec = IIf((ldblResNumNf > 0), 2, 1)
						'lintTpRec = IIf(VerificaReserva(ldblNumNF, lstrMesAno, lstrZona, lstrLivro, lstrConta) = True, 2, 1)
						'
						If lstrFlgst = "C" Then
							If ldteFlgSt <= adteFim Then
								lintMpr = -1
							Else
								lintMpr = 1
							End If
						Else
							lintMpr = 1
						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"'
						ldblVlrTot = CDbl(IIf(IsDbNull(.Fields("valorliq").value), 0, .Fields("valorliq").value))
						If ldblVlrTot = 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"'
							ldblVlrTot = CDbl(IIf(IsDbNull(.Fields("valortot").value), 0, .Fields("valortot").value))
						End If
						ldblVlrTot = ldblVlrTot * lintMpr
						'
						'Aqui começa o controle se for PDD
						If ablnPdd Then
							lstrNome = RetornaNomeConsumidor(lstrZona, lstrLivro, lstrConta, llngUnidId, llngConsId, lstrGrupo, lstrSituacao)
							'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
							llngDias = DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteVcto, adteBase)
							lstrSqlRes = ""
							lstrSqlRes = lstrSqlRes & "SELECT `numnf`,`mesano`,`zona`,`livro`,`conta`,`datares` " & vbCrLf
							lstrSqlRes = lstrSqlRes & "  FROM `reserva` " & vbCrLf
							lstrSqlRes = lstrSqlRes & " WHERE `numnf`    =  " & ldblNumNf & " " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `mesano`   = '" & lstrMesAno & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `zona`     = '" & lstrZona & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `livro`    = '" & lstrLivro & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `conta`    = '" & lstrConta & "' " & vbCrLf
							lstrSqlRes = lstrSqlRes & "   AND `datares` <= '" & VB6.Format(adteBase, "yyyy-MM-dd") & "' "
							Call Conectar(lstrSqlRes, lrstAuxRe)
							With lrstAuxRe
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									lblnContinua = (.RecordCount = 0)
									Call .Close()
								End If
							End With
							If lblnContinua Then
								If CShort(Left(lstrClas, 1)) = 9 Then
									If lstrClas = "9101" Then
										lblnContinua = (adblDiasRe >= 0 And llngDias > adblDiasRe)
									Else
										lblnContinua = (adblDiasBx >= 0 And llngDias > adblDiasBx)
									End If
								ElseIf CShort(Left(lstrClas, 1)) <= 3 Then 
									lblnContinua = (adblDiasIn >= 0 And llngDias > adblDiasIn)
								ElseIf CShort(Left(lstrClas, 1)) = 4 Then 
									lblnContinua = (adblDiasCo >= 0 And llngDias > adblDiasCo)
								ElseIf CShort(Left(lstrClas, 1)) = 5 Then 
									lblnContinua = (adblDiasRu >= 0 And llngDias > adblDiasRu)
								ElseIf CShort(Left(lstrClas, 1)) = 6 Then 
									If CShort(Left(lstrClas, 2)) = 64 Then
										lblnContinua = (adblDiasIP >= 0 And llngDias > adblDiasIP)
									Else
										lblnContinua = (adblDiasPP >= 0 And llngDias > adblDiasPP)
									End If
								ElseIf CShort(Left(lstrClas, 1)) = 7 Then 
									lblnContinua = (adblDiasSP >= 0 And llngDias > adblDiasSP)
								ElseIf CShort(Left(lstrClas, 1)) = 8 Then 
									lblnContinua = (adblDiasCP >= 0 And llngDias > adblDiasCP)
								End If
								If lblnContinua Then
									lblnContinua = (InStr(1, astrSituacao, lstrSituacao) > 0)
								End If
							End If
						End If
						If lblnContinua Then
							ldblVkWhDevPo = BuscaVlrItem(lstrItens, "0302020041") * lintMpr 'Valor Consumo devolvido na p
							ldblVKwhDevfp = BuscaVlrItem(lstrItens, "0302020042") * lintMpr 'Valor Consumo devolvido na fp
							ldblVkWhUtrPo = BuscaVlrItem(lstrItens, "0302020043") * lintMpr 'Valor Consumo ultrapassado na p
							ldblVKwhUtrfp = BuscaVlrItem(lstrItens, "0302020044") * lintMpr 'Valor Consumo ultrapassado na fp
							'
							'Montando o script até 300 Inserções por vez no banco de dados
							If llngSql >= 300 Then
								Call Executar(lstrSql)
								llngSql = 0
								lstrSql = ""
							End If
							If llngSql = 0 Then
								lstrSql = "INSERT INTO " & astrTabela & " VALUES ("
							Else
								lstrSql = lstrSql & ",("
							End If
							lstrSql = lstrSql & lintTpRec & ","
							'Resumo Diário
							If aintTipoRel = 1 Then
								If aintRelatorio = 0 Then
									lstrSql = lstrSql & "'" & VB6.Format(ldtePgto, "yyyy-MM-dd") & "'," 'dataaux
									'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
									llngDias = DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteVcto, ldtePgto)
								ElseIf aintRelatorio = 1 Then 
									lstrSql = lstrSql & "'" & VB6.Format(ldteVcto, "yyyy-MM-dd") & "'," 'dataaux
									'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
									llngDias = DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteVcto, adteBase)
								End If
							Else
								If aintRelatorio = 0 Then
									lstrSql = lstrSql & "'0000-00-00'," 'dataaux
								Else
									lstrSql = lstrSql & "'" & VB6.Format(ldteVcto, "yyyy-MM-dd") & "'," 'dataaux
									'UPGRADE_WARNING: DateDiff behavior may be different. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6B38EC3F-686D-4B2E-B5A5-9E8E7A762E32"'
									llngDias = DateDiff(Microsoft.VisualBasic.DateInterval.Day, ldteVcto, adteBase)
								End If
							End If
							'If llngDias < 0 Then llngDias = 0
							lstrSql = lstrSql & "'" & llngDias & "'," 'diasaux
							lstrSql = lstrSql & "'" & lstrClas & "'," ' classe
							If Left(lstrGrupo, 2) = "AL" Then
								lstrLivEsp = BuscaLivreEspecial(llngUnidId)
							Else
								lstrLivEsp = ""
							End If
							lstrSql = lstrSql & "'" & lstrGrupo & lstrLivEsp & "'," 'ta
							'Banco
							lstrSql = lstrSql & "'" & CShort(IIf(IsNumeric(lstrIdBanco), lstrIdBanco, 0)) & "'," ' banco
							lstrSql = lstrSql & "'" & lstrNmBanco & "'," ' nome banco
							'
							'Cidade por Zona
							lstrSql = lstrSql & "'" & lstrZona & "'," ' zona
							lstrSql = lstrSql & "'" & lstrNmCidade & "'," ' nome cidade
							'
							'Quantidade de Consumo Faturado - kwh fat
							ldblVlrAux = 0
							If Left(lstrGrupo, 1) = "A" Then
								
								ldblVlrAux = BuscaVlrItem(lstrItens, "0302010009")
								ldblQkWhFatPo = BuscaVlrItem(lstrItens, "0302010010") + BuscaVlrItem(lstrItens, "0302010060") 'Consumo Lido na Ponta
								ldblQKwhFatfp = BuscaVlrItem(lstrItens, "0302010011") + BuscaVlrItem(lstrItens, "0302010061") 'Consumo Lido na Fora Ponta
								ldblVlrAux = ldblVlrAux + ldblQkWhFatPo + ldblQKwhFatfp
							Else
								''ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "030201009") 'por Faixa de consumo Grupo B
								'Arlindo
								ldblVlrAux = ldblVlrAux + CDbl(Replace(CStr(BuscaVlrItem(lstrItens, "030201009") + BuscaVlrItem(lstrItens, "030201019") + BuscaVlrItem(lstrItens, "030201029") + BuscaVlrItem(lstrItens, "030201039")), ".", ",")) 'por Faixa de consumo Grupo B
								
							End If
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'Valor do Consumo Faturado - consumo
							ldblVlrAux = 0
							ldblVkWhNormal = 0
							ldblVReverso = 0
							ldblVkWhNormalBan = 0
							ldblVkWhFatPoBan = 0
							ldblVKwhFatfpBan = 0
							
							If Left(lstrGrupo, 1) = "A" Then
								ldblVkWhFatPo = 0
								ldblVKwhFatfp = 0
								ldblVkWhFatPo = BuscaVlrItem(lstrItens, "0302020010") 'Arlindo + BuscaVlrItem(lstrItens, "0302020060") 'Consumo faturado p
								If lintParcialmenteCativo = 1 Then
									ldblVkWhFatPo = ldblVkWhFatPo + BuscaValorItem(lstrItens, "0302020060") ' Replace(ChecaItensServ("0302020060", lstrItens), ".", ",") 'convecional
								End If
								
								ldblVKwhFatfp = BuscaVlrItem(lstrItens, "0302020011") 'Arlindo + BuscaVlrItem(lstrItens, "0302020061") 'Consumo faturado fp
								If lintParcialmenteCativo = 1 Then
									ldblVKwhFatfp = ldblVKwhFatfp + BuscaValorItem(lstrItens, "0302020061") 'Replace(ChecaItensServ("0302020061", lstrItens), ".", ",") 'convecional
								End If
								
								ldblVkWhFatPoBan = BuscaVlrItem(lstrItens, "0302120010") 'bandeira
								ldblVKwhFatfpBan = BuscaVlrItem(lstrItens, "0302120011") 'bandeira
								'consumo
								ldblVlrAux = BuscaVlrItem(lstrItens, "0302020009") 'convecional
								ldblVkWhNormalBan = BuscaVlrItem(lstrItens, "0302120009")
								ldblVlrAux = ldblVlrAux + ldblVkWhFatPo 'na Ponta
								ldblVlrAux = ldblVlrAux + ldblVKwhFatfp 'Fora da Ponta
								'demanda
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020015") 'convecional
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020021") 'na Ponta
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020023") 'Arlindo + BuscaVlrItem(lstrItens, "0302020072") 'Fora da Ponta
								'demanda Ultrapassagem
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020018") 'convencioal
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020022") 'ponta
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020026") 'fora ponta
								'Arlindo
								If lintMicroGeracaoConvenioSefaz = 0 Then
									ldblVReversoImporte = BuscaVlrItem(lstrItens, "0302022010")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022110")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022011")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302022111")
								End If
							Else
								ldblVkWhTU = BuscaVlrItem(lstrItens, "030302009")
								ldblVkWhTE = BuscaVlrItem(lstrItens, "030402009")
								ldblVkWhTEBan = BuscaVlrItem(lstrItens, "030412009")
								'ldblVkWhNormal = BuscaVlrItem(lstrItens, "030202009")
								'Arlindo
								ldblVkWhNormal = BuscaVlrItem(lstrItens, "030202009") + BuscaVlrItem(lstrItens, "030202019") + BuscaVlrItem(lstrItens, "030202029") + BuscaVlrItem(lstrItens, "030202039")
								'ldblVkWhNormalBan = BuscaVlrItem(lstrItens, "030212009")
								'Arlindo
								ldblVkWhNormalBan = BuscaVlrItem(lstrItens, "030212009") + BuscaVlrItem(lstrItens, "0302120190") + BuscaVlrItem(lstrItens, "0302120290") + BuscaVlrItem(lstrItens, "0302120390")
								'ldblVReverso = BuscaVlrItem(lstrItens, "0302020994")
								'Arlindo
								If lintMicroGeracaoConvenioSefaz = 0 Then
									ldblVReversoImporte = BuscaVlrItem(lstrItens, "0302020994")
									ldblVReversoImporte = ldblVReversoImporte + BuscaVlrItem(lstrItens, "0302020993")
								Else
									ldblVReverso = BuscaVlrItem(lstrItens, "0302020994")
									ldblVReverso = ldblVReverso + BuscaVlrItem(lstrItens, "0302020993")
								End If
								
								If ldblVReverso = 0 Then
									If (ldblVkWhTU + ldblVkWhTE) > 0 Then
										ldblVReverso = ldblVkWhNormal - (ldblVkWhTU + ldblVkWhTE)
										If ldblVkWhNormal - (ldblVkWhTU + ldblVkWhTE) > 0 Then
											ldblVReverso = ldblVReverso + (ldblVkWhNormalBan - ldblVkWhTEBan)
										End If
									End If
								End If
								ldblVlrAux = ldblVlrAux + ldblVkWhNormal 'consumo
							End If
							If Left(lstrGrupo, 2) = "AL" Then
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302060021") 'dif.Demanda p
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302060025") 'dif.Demanda fp
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020051") 'Desconto Provisorio
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020052") 'Desconto real
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020053") 'diferença a devolver
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020054") 'diferença a cobrar
								
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020055") 'Desconto Provisorio
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020056") 'Desconto real
								ldblVlrAux = ldblVlrAux - BuscaVlrItem(lstrItens, "0302020057") 'diferença a devolver
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020058") 'diferença a cobrar
							End If
							If Left(lstrGrupo, 2) = "AV" Then
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302060023") 'diferença consumo fora ponta
							End If
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr((ldblVkWhFatPoBan + ldblVKwhFatfpBan + ldblVkWhNormalBan) * lintMpr), ",", ".") & "," 'Bandeira
							'
							'Icms 1 e 2
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302030009")
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302030010")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							ldblVlrAux = 0
							'
							'Reativo
							If Left(lstrGrupo, 1) = "A" Then
								ldblVlrAux = BuscaVlrItem(lstrItens, "0302020012") 'Consumo
							End If
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020031") 'Demanda
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020013") 'Consumo ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020014") 'Consumo fora ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020027") 'Demanda ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020028") 'Demanda fora ponta
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302990044") 'Consumo Reservado
							If Left(lstrGrupo, 1) = "B" Then
								ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302020012") 'Consumo Reativo
							End If
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'Contribuição de Iluminação Pública
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0301000003") * lintMpr
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux), ",", ".") & ","
							'
							'Eletrobras (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302990041")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'ECE (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302070002")
							ldblVlrAux = ldblVlrAux + BuscaVlrItem(lstrItens, "0302070005")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'ICMS ECE (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302030006")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'Multa ECE (sem uso)
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302050004")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'Pis
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302020029")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'Cofins
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302020030")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'Icms Sob Pis e Cofins
							ldblVlrAux = 0
							ldblVlrAux = BuscaVlrItem(lstrItens, "0302030012")
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrAux * lintMpr), ",", ".") & ","
							'
							'Convenios, Diversos e Taxados
							ldblVlrDiv = 0
							ldblVlrTax = 0
							ldblVlrCov = 0
							ldblVlrDes = 0
							ldblVlrRet = 0
							ldblVlrOut = 0
							ldblIcmsSt = 0
							ldblPisSt = 0
							ldblCofinsSt = 0
							'
							'coperativas
							ldblVConsCoperativaP = 0
							ldblVConsCoperativaFP = 0
							ldblVDemP = 0
							ldblVDemFP = 0
							ldblVPis = 0
							ldblVCofins = 0
							'
							For IndCodServ = 1 To CInt(Len(Trim(lstrItens)))
								lblnInclui = False
								Call BuscaCodItem(IndCodServ, lstrItens)
								ldlbCodSrv = CDbl(Replace(IIf(IsNumeric(VlrCodServ), VlrCodServ, 0), ".", ","))
								If (ldlbCodSrv < 0) And (Mid(codserv, 1, 4) <> "9080") Then
									ldlbCodSrv = ldlbCodSrv * -1
								End If
								'
								'Taxas
								If (Mid(codserv, 1, 4) = "9040") Or (Mid(codserv, 1, 4) = "9050") Then
									ldblVlrTax = ldblVlrTax + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Outros (EUSD - MUST
								If codserv = "0302020032" Then
									ldblVlrOut = ldblVlrOut + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Diversos
								If Mid(codserv, 1, 4) = "9080" Then
									ldblVlrDiv = ldblVlrDiv + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Convenios
								If Mid(codserv, 1, 4) = "9030" Then
									ldblVlrCov = ldblVlrCov + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Descontos
								If (Mid(codserv, 1, 4) = "9070") And (codserv <> "9070000088") Then
									ldblVlrDes = ldblVlrDes + ldlbCodSrv
									lblnInclui = True
								End If
								'
								'Retencoes
								If (codserv = "9070000088") Or (Mid(codserv, 1, 4) = "9090") Then
									ldblVlrRet = ldblVlrRet + ldlbCodSrv
									lblnInclui = True
								End If
								If (codserv = "0302020051") Or (codserv = "0302020052") Or (codserv = "0302020053") Or (codserv = "0302020054") Or (codserv = "0302020055") Or (codserv = "0302020056") Or (codserv = "0302020057") Or (codserv = "0302020058") Then
									If CDbl(Right(codserv, 1)) < 4 Then
										If ldlbCodSrv > 0 Then
											ldlbCodSrv = ldlbCodSrv * (-1)
										End If
									End If
									lblnInclui = True
								End If
								'
								'Substituição Tributária
								If (codserv = "0602020001" Or codserv = "0602020002" Or codserv = "0602020003") Then
									If codserv = "0602020001" Then
										ldblIcmsSt = ldblIcmsSt + BuscaVlrItem(lstrItens, "0602020001")
									ElseIf codserv = "0602020002" Then 
										ldblPisSt = ldblPisSt + BuscaVlrItem(lstrItens, "0602020002")
									ElseIf codserv = "0602020003" Then 
										ldblCofinsSt = ldblCofinsSt + BuscaVlrItem(lstrItens, "0602020003")
									End If
									lblnInclui = True
								End If
								'COOPERATIVA
								If CDbl(lstrClas) = 5201 Then
									If codserv = "0302020010" Then
										ldblVConsCoperativaP = ldblVkWhFatPo + BuscaVlrItem(lstrItens, "0302020010") 'Consumo faturado p
									ElseIf codserv = "0302020011" Then 
										ldblVConsCoperativaFP = ldblVKwhFatfp + BuscaVlrItem(lstrItens, "0302020011") + BuscaVlrItem(lstrItens, "0302020008") 'Consumo Faturado Fp
									ElseIf codserv = "0302020021" Then 
										ldblVDemP = ldblVDemP + BuscaVlrItem(lstrItens, "0302020021") ' demanda na ponta
									ElseIf codserv = "0302020023" Then 
										ldblVDemFP = ldblVDemFP + BuscaVlrItem(lstrItens, "0302020023") + BuscaVlrItem(lstrItens, "0302020015") 'Demanda Fora da Ponta
									ElseIf codserv = "0302020029" Then 
										ldblVPis = ldblVPis + BuscaVlrItem(lstrItens, "0302020029") ' pis
									ElseIf codserv = "0302020030" Then 
										ldblVCofins = ldblVCofins + BuscaVlrItem(lstrItens, "0302020030") ' cofins
									End If
									lblnInclui = True
								End If
								If lstrFlgst <> "C" Then
									'Inclusão dos itens
									If lblnInclui Then
										lstrSqlIte = "INSERT INTO " & astrTabela & "_itens" & " VALUES ( "
										lstrSqlIte = lstrSqlIte & lintTpRec & ","
										If aintTipoRel = 1 Then 'Resumo Diário
											If aintRelatorio = 0 Then
												lstrSqlIte = lstrSqlIte & "'" & VB6.Format(ldtePgto, "yyyy-MM-dd") & "',"
											Else
												lstrSqlIte = lstrSqlIte & "'" & VB6.Format(ldteVcto, "yyyy-MM-dd") & "',"
											End If
										Else
											If aintRelatorio = 0 Then
												lstrSqlIte = lstrSqlIte & "'0000-00-00',"
											Else
												lstrSqlIte = lstrSqlIte & "'" & VB6.Format(ldteVcto, "yyyy-MM-dd") & "',"
											End If
										End If
										lstrSqlIte = lstrSqlIte & "'" & llngDias & "'," 'diasaux
										lstrSqlIte = lstrSqlIte & "'" & CShort(IIf(IsNumeric(lstrIdBanco), lstrIdBanco, 0)) & "'," ' banco
										lstrSqlIte = lstrSqlIte & "'" & lstrZona & "',"
										lstrSqlIte = lstrSqlIte & "'" & codserv & "'," & Replace(CStr(ldlbCodSrv), ",", ".") & ")"
										Call Executar(lstrSqlIte)
									End If
								End If
							Next IndCodServ
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrTax * lintMpr), ",", ".") & "," 'taxados
							lstrSql = lstrSql & " " & Replace(CStr(((ldblVlrDiv + ldblVlrOut) - (ldblVlrDes)) * lintMpr), ",", ".") & "," 'diversos
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrCov * lintMpr), ",", ".") & "," 'convenios
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrRet * lintMpr), ",", ".") & "," 'rentecao
							lstrSql = lstrSql & " " & Replace(CStr(ldblVkWhDevPo), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr(ldblVKwhDevfp), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr(ldblVkWhUtrPo), ",", ".") & ","
							lstrSql = lstrSql & " " & Replace(CStr(ldblVKwhUtrfp), ",", ".") & ","
							'
							'Substituição Tributária
							lstrSql = lstrSql & " " & Replace(CStr(ldblIcmsSt * lintMpr), ",", ".") & "," 'Icms St
							lstrSql = lstrSql & " " & Replace(CStr(ldblPisSt * lintMpr), ",", ".") & "," 'Pis St
							lstrSql = lstrSql & " " & Replace(CStr(ldblCofinsSt * lintMpr), ",", ".") & "," 'Cofins St
							'
							lstrSql = lstrSql & " " & Replace(CStr(ldblVReverso * lintMpr), ",", ".") & "," 'Reverso
							'
							lstrSql = lstrSql & " " & Replace(CStr(ldblVlrTot), ",", ".") & ")"
							'
							llngSql = llngSql + 1
						End If
						Call .MoveNext()
						Call BarraProgresso(aProgressBar, alblPerc, alblInfo, 1)
					End While
					If Trim(lstrSql) <> "" Then Call Executar(lstrSql)
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstAuxRe 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"'
		lrstAuxRe = Nothing
	End Sub
	
	Public Sub RelatorioPaisagemPadraoColunas2(ByRef astrTamcol As String, ByRef astrCabec As String, ByRef astrTemp As String, ByRef astrBranco As String, ByRef astrNomClass As Object, ByRef astrCondicao As Object)
		'Parametros de quebra
		'UPGRADE_WARNING: Lower bound of array astrNomClass 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"'
		'UPGRADE_ISSUE: As Variant was removed from ReDim astrNomClass(1 To 12) statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="19AFCB41-AA8E-4E6B-A441-A3E802E5FD64"'
		ReDim astrNomClass(12)
		'UPGRADE_WARNING: Lower bound of array astrCondicao 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"'
		'UPGRADE_ISSUE: As Variant was removed from ReDim astrCondicao(1 To 12) statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="19AFCB41-AA8E-4E6B-A441-A3E802E5FD64"'
		ReDim astrCondicao(12)
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(1) = "Residencial"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(2) = "Industrial"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(3) = "Comercial"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(4) = "Rural"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(5) = "Rural Sup."
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(6) = "P.Público"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(7) = "I.Pública"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(8) = "S.Público"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(9) = "Cons.P.Liv"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(10) = "Cons.Livre"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(11) = "Sub-Total"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrNomClass(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrNomClass(12) = "C.Próprio"
		'
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(1) = " LEFT(`classe`, 1) = '9' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(2) = " (LEFT(`classe`, 1)+0) BETWEEN 0 AND 3 AND LEFT(`ta`, 2) <> 'AL' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(3) = " LEFT(`classe`, 1) = '4' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(4) = " LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) <> 'AX' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(5) = " LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) = 'AX' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(6) = " (LEFT(`classe`, 2)+0) BETWEEN 61 AND 63 "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(7) = " LEFT(`classe`, 2) = '64' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(8) = " LEFT(`classe`, 1) = '7' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(9) = " `ta` = 'ALS' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(10) = " TRIM(`ta`) = 'AL' "
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(11) = " LEFT(`classe`, 1) <> 8"
		'UPGRADE_WARNING: Couldn't resolve default property of object astrCondicao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		astrCondicao(12) = " LEFT(`classe`, 1) = 8"
		'
		astrTamcol = "<15mm|>10mm|>17mm|>17mm|>14mm|>14mm|>14mm|>12mm|>13mm|>17mm|>12mm|>12mm|>12mm|>12mm|>14mm|>13mm|>10mm|>13mm|>15mm|>17mm;"
		astrCabec = ""
		astrCabec = astrCabec & "Classe|" '1
		astrCabec = astrCabec & "NºCons|" '2
		astrCabec = astrCabec & "KWh Fat|" '3
		astrCabec = astrCabec & "Forn.Liquido|" '4
		astrCabec = astrCabec & "Bandeira|" '5
		astrCabec = astrCabec & "ICMS|" '6
		astrCabec = astrCabec & "Pis|" '7
		astrCabec = astrCabec & "Cofins|" '8
		astrCabec = astrCabec & "Reativo|" '9
		astrCabec = astrCabec & "Importe|" '10
		astrCabec = astrCabec & "Ilum.Pub|" '11
		astrCabec = astrCabec & "J.Eletr.|" '12
		astrCabec = astrCabec & "Taxados|" '13
		astrCabec = astrCabec & "Diversos|" '14
		astrCabec = astrCabec & "Convenios|" '15
		astrCabec = astrCabec & "Ece/Mt./Ic.|" '16
		'astrCabec = astrCabec & "ICMS ECE|" '17
		astrCabec = astrCabec & "Reverso|" '17
		astrCabec = astrCabec & "Retenção|" '18
		astrCabec = astrCabec & "Sub.Trib.|" '19
		astrCabec = astrCabec & "Total" '20
		'Para preencher com zeros
		astrTemp = "|0|0|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00|0,00"
		'Para preencher com brancos
		astrBranco = "|||||||||||||||||||"
	End Sub
	
	Public Function RelatorioPaisagemSelecionaDados2(ByVal aintTipoRel As Short, ByVal aintRelatorio As Short, ByVal astrSql As String, ByVal astrTabela As String) As String
		'0 = Total Geral
		'1 = Resumo diário
		'2 = Zona e nome da cidade
		'3 = Banco e nome do banco
		'5 = Recebimentos por dia/banc/classe
		'aintRelatorio
		'0 = Arrecadacao
		'1 = Em Abertos
		astrSql = "SELECT "
		If aintTipoRel = 5 Then 'Recebimentos por dia/banco/classe
			astrSql = astrSql & "`dataaux`, `banco`, `nmbanco`, " '0,1,2
			astrSql = astrSql & "IF(LEFT(`classe`, 1) = '9', '01Residencial', "
			astrSql = astrSql & " IF((LEFT(`classe`, 1)+0) BETWEEN 0 AND 3 AND LEFT(`ta`, 2) <> 'AL', '02Industrial', "
			astrSql = astrSql & "  IF(LEFT(`classe`, 1) = '4','03Comercial', "
			astrSql = astrSql & "   IF(LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) <> 'AX', '04Rural', "
			astrSql = astrSql & "    IF(LEFT(`classe`, 1) = '5' AND LEFT(`ta`, 2) = 'AX', '05Rural Sup.', "
			astrSql = astrSql & "     IF((LEFT(`classe`, 2)+0) BETWEEN 61 AND 63,'06P.Público', "
			astrSql = astrSql & "      IF(LEFT(`classe`, 2) = '64','07I.Pública', "
			astrSql = astrSql & "       IF(LEFT(`classe`, 1) = '7','08S.Público', "
			astrSql = astrSql & "        IF(LEFT(`classe`, 1) = 8,'11C.Próprio',"
			astrSql = astrSql & "         IF(`ta` = 'ALS','09Cons.P.Liv', "
			astrSql = astrSql & "          IF(TRIM(`ta`) = 'AL','10Cons.Livre', 'Indefinido'"
			astrSql = astrSql & "          )"
			astrSql = astrSql & "         )"
			astrSql = astrSql & "        )"
			astrSql = astrSql & "       )"
			astrSql = astrSql & "      )"
			astrSql = astrSql & "     )"
			astrSql = astrSql & "    )"
			astrSql = astrSql & "   )"
			astrSql = astrSql & "  )"
			astrSql = astrSql & " )"
			astrSql = astrSql & ") classes, " '3
		End If
		If aintTipoRel = 1 Then 'Resumo diário
			astrSql = astrSql & "       dataaux, "
		ElseIf aintTipoRel = 2 Then  'Por Zona
			astrSql = astrSql & "       `zona`, `nmcidade`, "
		ElseIf aintTipoRel = 3 Then  'Por Banco
			astrSql = astrSql & "       `banco`, `nmbanco`, "
		End If
		astrSql = astrSql & "       SUM(IF(total<0,0,1)) ncons, " '1
		astrSql = astrSql & "       SUM(kwh_fat) kwhf, " '2
		astrSql = astrSql & "       SUM(consumo) consumo, " '3
		astrSql = astrSql & "       SUM(bandeira) bandeira, " '3
		astrSql = astrSql & "       SUM(icms) icms, " '4
		astrSql = astrSql & "       SUM(pis) pis, " '5
		astrSql = astrSql & "       SUM(cofins) cofins, " '6
		astrSql = astrSql & "       SUM(reativo) reativo, " '7
		astrSql = astrSql & "       SUM(COALESCE(consumo, 0) + "
		astrSql = astrSql & "           COALESCE(bandeira, 0) + "
		astrSql = astrSql & "           COALESCE(icms, 0) + "
		astrSql = astrSql & "           COALESCE(pis, 0) + "
		astrSql = astrSql & "           COALESCE(cofins, 0) + "
		astrSql = astrSql & "           COALESCE(reativo, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_dev_p, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_dev_fp, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_utr_p, 0) + "
		astrSql = astrSql & "           COALESCE(vkwh_utr_fp, 0)) importe, " '8
		astrSql = astrSql & "       SUM(tip) tip, " '9
		astrSql = astrSql & "       SUM(dif_eltrbr) eletrobras, " '10
		astrSql = astrSql & "       SUM(taxados) taxados, " '11
		astrSql = astrSql & "       SUM(diversos) diversos, " '12
		astrSql = astrSql & "       SUM(convenios) convenios, " '13
		astrSql = astrSql & "       SUM(ece)+SUM(multa_ece)+SUM(icms_ece) ece, " '14
		'astrSql = astrSql & "       SUM(icms_ece) icms_ece, " '15
		'astrSql = astrSql & "       SUM(multa_ece) multa_ece, " '14
		astrSql = astrSql & "       SUM(reverso) reverso, " '16
		astrSql = astrSql & "       SUM(retencao) retencao, " '17
		astrSql = astrSql & "       SUM(icms_st) icms_st, " '18
		astrSql = astrSql & "       SUM(pis_st) pis_st, " '19
		astrSql = astrSql & "       SUM(cofins_st) cofins_st, " '20
		astrSql = astrSql & "       SUM(total) total" '21
		astrSql = astrSql & "  FROM " & astrTabela
		RelatorioPaisagemSelecionaDados2 = astrSql
	End Function
	
	Public Sub ImprimeDetalhamentosPaisagem2(ByRef afrmRelatorio As FrmRelatorio, ByVal astrTabelaItens As String, ByVal aintTpRel As Short, Optional ByVal aintTipo As Short = 3, Optional ByVal aintRelatorio As Short = 0, Optional ByVal adteFxIni As Date = "01/01/1900", Optional ByVal adteFxFim As Date = "01/01/1900", Optional ByVal alngBanco As Integer = 0, Optional ByVal astrZona As String = "", Optional ByVal alngDiasIni As Integer = 0, Optional ByVal alngDiasFim As Integer = 999999999)
		'aintTipoRel
		'0 = Total Geral
		'1 = Resumo diário
		'2 = Zona e nome da cidade
		'3 = Banco e nome do banco
		'5 = Recebimentos por dia/banc/classe
		'aintRelatorio
		'0 = Arrecadacao
		'1 = Em Abertos
		'2 = Vencidos e Vencer
		'aintTipo
		'1 = Normal
		'2 = Voltou da Reserva
		'3 = Todos
		Dim lstrMrgOri As String
		Dim llngPosicaoY As Integer
		Dim lstrTab As String
		Dim lstrTamCol As String
		Dim lstrCabDet As String
		
		Dim lintAux As Integer
		'UPGRADE_WARNING: Lower bound of array lstrTabela 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 lstrTabela(12) As String
		'UPGRADE_WARNING: Lower bound of array lstrTabDes 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 lstrTabDes(12) As String
		'UPGRADE_WARNING: Lower bound of array lstrMargem 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 lstrMargem(12) As String
		
		Dim lstrCod As String
		Dim lstrDes As String
		Dim ldblVlr As Double
		Dim ldblTotal As Double
		
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		lstrTamCol = "<13mm|<40mm|>15mm;"
		lstrCabDet = "Código|Descrição|Valor"
		
		With afrmRelatorio
			'UPGRADE_ISSUE: VBControlExtender method 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 + 300
			'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			'UPGRADE_WARNING: Couldn't resolve default property of object afrmRelatorio.VS.CurrentY. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			llngPosicaoY = .VS.CurrentY
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
			'UPGRADE_WARNING: Couldn't resolve default property of object afrmRelatorio.VS.MarginLeft. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			lstrMrgOri = .VS.MarginLeft
		End With
		'Parametrização
		'Coluna 1
		lstrTabela(1) = " WHERE ((MID(codigo, 1, 4)='9080' AND codigo NOT IN('9080000092','9080000098')) OR codigo IN('9070000061','9070000066','9070000067')) "
		lstrTabela(2) = " WHERE codigo = '9070000082' "
		lstrTabela(3) = " WHERE codigo = '9070000079' "
		lstrTabela(4) = " WHERE LEFT(codigo,9) = '907000003' "
		lstrTabela(5) = " WHERE MID(codigo,1,4)='9070' AND LEFT(codigo,9) <> '907000003' AND codigo NOT IN('9070000004','9070000051','9070000082','9070000087','9070000088','9070000090','9070000091','9070000092','9070000093','9070000079','9070000061','9070000066','9070000067') "
		'Coluna 2
		lstrTabela(6) = " WHERE (MID(codigo,1,4) IN('9040','9050') OR codigo IN('9070000051'))"
		'Coluna 3
		lstrTabela(7) = " WHERE MID(codigo,1,4)='9030' "
		lstrTabela(8) = " WHERE (codigo='9070000088' OR MID(codigo,1,4)='9090') "
		'Coluna 4
		lstrTabela(9) = " WHERE codigo IN('0302020032','9080000098','9070000087','9070000091', '9080000092','9070000004','9070000090','9070000092','9070000093') "
		lstrTabela(10) = " WHERE codigo IN('0302020051','0302020052','0302020053','0302020054') "
		lstrTabela(11) = " WHERE codigo IN('0602020001','0602020002','0602020003') "
		lstrTabela(12) = " WHERE codigo IN('0302020008','0302020015','0302020011','0302020010','0302020023','0302020072','0302020031','0302020029','0302020030','0302020021') "
		
		'Coluna 1
		lstrTabDes(1) = "Diversos"
		lstrTabDes(2) = "Dev.Dup.Pgto"
		lstrTabDes(3) = "Bonus Itaipu"
		lstrTabDes(4) = "Ressarc.IDQUAL"
		lstrTabDes(5) = "Outras Devoluções"
		'Coluna 2
		lstrTabDes(6) = "Taxados"
		'Coluna 3
		lstrTabDes(7) = "Convênios"
		lstrTabDes(8) = "Retenções"
		'Coluna 4
		lstrTabDes(9) = "Outros"
		lstrTabDes(10) = "Livres"
		lstrTabDes(11) = "Livres - Substituição Tributária"
		lstrTabDes(12) = "COOPERATIVAS"
		'Margens
		'Coluna 1
		lstrMargem(1) = lstrMrgOri
		lstrMargem(2) = lstrMrgOri
		lstrMargem(3) = lstrMrgOri
		lstrMargem(4) = lstrMrgOri
		lstrMargem(5) = lstrMrgOri
		'Coluna 2
		lstrMargem(6) = "76mm"
		'Coluna 3
		lstrMargem(7) = "145mm"
		lstrMargem(8) = "145mm"
		'Coluna 4
		lstrMargem(9) = "214mm"
		lstrMargem(10) = "214mm"
		lstrMargem(11) = "214mm"
		lstrMargem(12) = "214mm"
		'
		lstrSql = "SELECT `codigo`, SUM(`valor`) AS `valor` FROM " & astrTabelaItens
		For lintAux = 1 To UBound(lstrTabela)
			'Muda a margem conforme tabela
			afrmRelatorio.VS.MarginLeft = lstrMargem(lintAux)
			If lintAux = 1 Or lintAux = 6 Or lintAux = 7 Or lintAux = 9 Then
				'Posição Y de impressão
				'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
				afrmRelatorio.VS.CurrentY = llngPosicaoY
			End If
			ldblTotal = 0
			If aintTpRel = 0 Then 'Tota geral
				If aintRelatorio = 0 Then
					Call Conectar(lstrSql & lstrTabela(lintAux) & IIf(aintTipo < 3, " AND `tipo` = " & aintTipo & " ", "") & " GROUP BY `codigo` ASC ", lrstAux)
				ElseIf aintRelatorio = 1 Then 
					Call Conectar(lstrSql & lstrTabela(lintAux) & IIf(aintTipo < 3, " AND `tipo` = " & aintTipo & " ", "") & " GROUP BY `codigo` ASC ", lrstAux)
				ElseIf aintRelatorio = 2 Then 
					Call Conectar(lstrSql & lstrTabela(lintAux) & " AND (`diasaux` BETWEEN " & alngDiasIni & " AND " & alngDiasFim & ") GROUP BY `codigo` ASC ", lrstAux)
				End If
			ElseIf aintTpRel = 1 Then  'Diário
				If aintRelatorio = 0 Then
					Call Conectar(lstrSql & lstrTabela(lintAux) & " AND `dataaux` = '" & VB6.Format(adteFxIni, "yyyy-MM-dd") & "'" & IIf(aintTipo < 3, " AND `tipo` = " & aintTipo & " ", "") & " GROUP BY `dataaux` ASC, `codigo` ASC ", lrstAux)
				ElseIf aintRelatorio = 1 Then 
					Call Conectar(lstrSql & lstrTabela(lintAux) & " AND `dataaux` = '" & VB6.Format(adteFxIni, "yyyy-MM-dd") & "' GROUP BY `dataaux` ASC, `codigo` ASC ", lrstAux)
				ElseIf aintRelatorio = 2 Then 
					Call Conectar(lstrSql & lstrTabela(lintAux) & " AND (`diasaux` BETWEEN " & alngDiasIni & " AND " & alngDiasFim & ") GROUP BY `codigo` ASC ", lrstAux)
				End If
			ElseIf aintTpRel = 2 Then  'Municipio
				If aintRelatorio = 2 Then
					Call Conectar(lstrSql & lstrTabela(lintAux) & " AND `zona` = '" & astrZona & "' AND (`diasaux` BETWEEN " & alngDiasIni & " AND " & alngDiasFim & ") GROUP BY `zona` ASC, `codigo` ASC ", lrstAux)
				Else
					Call Conectar(lstrSql & lstrTabela(lintAux) & " AND `zona` = '" & astrZona & "'" & IIf(aintTipo < 3, " AND `tipo` = " & aintTipo & " ", "") & " GROUP BY `zona` ASC, `codigo` ASC ", lrstAux)
				End If
			ElseIf aintTpRel = 3 Then  'Banco
				Call Conectar(lstrSql & lstrTabela(lintAux) & " AND `banco` = " & alngBanco & " " & IIf(aintTipo < 3, " AND `tipo` = " & aintTipo & " ", "") & " GROUP BY `banco` ASC, `codigo` ASC ", lrstAux)
			End If
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						With afrmRelatorio
							.VS.Font = VB6.FontChangeBold(.VS.Font, True)
							.VS.Table = "^68mm;Detalhamento dos " & lstrTabDes(lintAux)
							.VS.Font = VB6.FontChangeBold(.VS.Font, False)
							.VS.Table = lstrTamCol & lstrCabDet
						End With
						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"'
							lstrCod = IIf(IsDbNull(.Fields("codigo").value), "", .Fields("codigo").value)
							lstrDes = Replace(Replace(codTodescri(lstrCod), "DESCONTO", "DESC."), "CONFORME", "CONF.")
							If CDbl(Left(lstrCod, 4)) = 9040 Then
								lstrDes = lstrDes & IIf(CDbl(Right(lstrCod, 1)) = 1, " (B - Monofásico)", IIf(CDbl(Right(lstrCod, 1)) = 2, " (B - Bifásico)", IIf(CDbl(Right(lstrCod, 1)) = 3, " (B - Trifásico)", IIf(CDbl(Right(lstrCod, 1)) = 4, " (A)", ""))))
							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"'
							ldblVlr = CDbl(IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").value))
							If ldblVlr < 0 And (Left(lstrCod, 4) = "9070" Or Left(lstrCod, 4) = "9090") Then ldblVlr = ldblVlr * -1
							If Mid(lstrCod, 1, 4) = "9070" Or Mid(lstrCod, 1, 4) = "9090" Then ldblVlr = ldblVlr * -1
							lstrTab = ""
							lstrTab = lstrTab & lstrCod & "|"
							lstrTab = lstrTab & lstrDes & "|"
							lstrTab = lstrTab & VB6.Format(ldblVlr, "#,##0.00")
							ldblTotal = ldblTotal + ldblVlr
							afrmRelatorio.VS.Table = lstrTamCol & lstrTab
							Call .MoveNext()
						End While
						With afrmRelatorio
							.VS.Font = VB6.FontChangeBold(.VS.Font, True)
							.VS.Table = lstrTamCol & "|Total|" & VB6.Format(ldblTotal, "#,##0.00")
							.VS.Font = VB6.FontChangeBold(.VS.Font, False)
							.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
							.VS.Table = lstrTamCol & "||"
							.VS.TableBorder = VSVIEW6.TableBorderSettings.tbAll
						End With
					End If
					Call .Close()
				End If
			End With
		Next lintAux
		afrmRelatorio.VS.MarginLeft = lstrMrgOri
		'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 Module