Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelIcmsNovo
	Inherits System.Windows.Forms.Form
	Private ifrmIcms As FrmRelatorio
	Private istrTamCol As String
	Private istrCabCol As String
	Private istrGruCla As String
	Private ilngLimRes As Integer
	Private ilngLimRur As Integer
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		Me.dialArqOpen.DefaultExt = "TXT"
		'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		Me.dialArqOpen.Filter = "*." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.FileName = My.Application.Info.DirectoryPath & "\ICMS_" & VB6.Format(BuscaHoraBD, "yyyymmddHHMMSS") & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação do Icms SpedFiscal"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivo.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim llngRow As Integer
		Dim llngCol As Integer
		lintARQ = FreeFile
		If Trim(Me.txtArquivo.Text) <> "" Then
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Call LigDes(False)
			FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
			With Me.mfgDados
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1, " Gerando Arquivo ")
				For llngRow = 0 To .Rows - 1
					lstrLin = ""
					For llngCol = 0 To .Cols - 1
						lstrLin = lstrLin & .get_TextMatrix(llngRow, llngCol) & ";"
					Next llngCol
					PrintLine(lintARQ, lstrLin)
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, 1, " Gerando Arquivo ")
				Next llngRow
			End With
			Me.Cursor = System.Windows.Forms.Cursors.Default
			Call LigDes(True)
			FileClose(lintARQ)
			Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
		Else
			Call MsgBox("Você precisa selecionar o caminho e o arquivo para exportação!", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
	End Sub
	
	Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call LigDes(False)
		Call MontaICMS()
		Call ImpIcms()
		Call LigDes(True)
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Sub frmRelIcmsNovo_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Select Case KeyAscii
			Case System.Windows.Forms.Keys.Return
				KeyAscii = 0
				Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			Case System.Windows.Forms.Keys.Escape
				Call cmdFechar_Click(cmdFechar, New System.EventArgs())
		End Select
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub frmRelIcmsNovo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Me.dtpPeriodo.value = CDate("01/" & VB6.Format(BuscaDataBD, "mm/yyyy"))
		ifrmIcms = New FrmRelatorio
		Call MontaGrade()
		ilngLimRes = CInt(LeConfig("LIMITE_RESIDENCIAL"))
		ilngLimRur = CInt(LeConfig("LIMITE_RURAL"))
		'             C     G    F    CF   Q     A     k     B     I     IS    D
		istrTamCol = "<45mm|^6mm|^6mm|^12mm|>14mm|>20mm|>10mm|>20mm|>20mm|>20mm|>20mm;"
		istrCabCol = "CLASSE|G.|F.|Cfop |N.CONS.|kWh / kW|ALIQ.|BASE|ICMS|ISENTOS|DIFERIDO"
	End Sub
	
	Private Sub MontaGrade()
		With Me.mfgDados
			Call .Clear()
			.Rows = 1
			.set_ColWidth(0, 1800)
			.set_ColWidth(1, 1800)
			.set_ColWidth(2, 550)
			.set_ColWidth(3, 550)
			.set_ColWidth(4, 600)
			.set_ColWidth(5, 1200)
			.set_ColWidth(6, 1200)
			.set_ColWidth(7, 1200)
			.set_ColWidth(8, 1200)
			.set_ColWidth(9, 1200)
			.set_ColWidth(10, 1200)
			.set_ColWidth(11, 1200)
			.set_ColWidth(12, 1200)
			.set_ColWidth(13, 1200)
			.set_ColWidth(14, 550)
			.set_ColWidth(15, 1200)
			.set_ColWidth(16, 1200)
			.set_ColWidth(17, 1200)
			.set_ColWidth(18, 1200)
			.set_ColWidth(19, 1200)
			.set_ColWidth(20, 1200)
			.set_ColWidth(21, 1200)
			.set_ColWidth(22, 1200)
			.set_ColWidth(23, 1200)
			.set_TextMatrix(0, 0, "Gr.Cla.")
			.set_TextMatrix(0, 1, "Classe")
			.set_TextMatrix(0, 2, "Grupo")
			.set_TextMatrix(0, 3, "Fase")
			.set_TextMatrix(0, 4, "CFOP")
			.set_TextMatrix(0, 5, "Qtde")
			.set_TextMatrix(0, 6, "Isentos")
			.set_TextMatrix(0, 7, "Diferido")
			.set_TextMatrix(0, 8, "Aliq A")
			.set_TextMatrix(0, 9, "Base A")
			.set_TextMatrix(0, 10, "Icms A")
			.set_TextMatrix(0, 11, "Aliq B")
			.set_TextMatrix(0, 12, "Base B")
			.set_TextMatrix(0, 13, "Icms B")
			.set_TextMatrix(0, 14, "Ordem")
			.set_TextMatrix(0, 15, "Aliq S.T.")
			.set_TextMatrix(0, 16, "Base S.T.")
			.set_TextMatrix(0, 17, "Icms S.T.")
			.set_TextMatrix(0, 18, "kWh Fat.A")
			.set_TextMatrix(0, 19, "kWh Fat.B")
			.set_TextMatrix(0, 20, "kWh Fat.D")
			.set_TextMatrix(0, 21, "kW Fat.")
			.set_TextMatrix(0, 22, "kW Fat.D")
			.set_TextMatrix(0, 23, "kWh Fat.S")
		End With
		Call MontaGrupos("CLASSE", "01", "")
		Call MontaGrupos("SERVIÇOS TAXADOS", "02", "5.949-1")
		Call MontaGrupos("REC.FINANCEIRAS", "03", "5.949-2")
		Call MontaGrupos("MUST", "04", "5.949-3")
		Call MontaGrupos("ENCARGO USO SIS.TRANS.", "05", "5.949-4")
	End Sub
	
	Private Sub MontaICMS()
		Dim lstrFlg As String
		Dim ldteFlgSt As Date
		Dim lintMpr As Short
		Dim lstrCod As String
		Dim lstrCla As String
		Dim lstrTpC As String
		Dim lstrItm As String
		Dim lstrTar As String
		Dim lstrTarA As String
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim lstrFas As String
		Dim lstrCfp As String
		Dim lstrOrdCl As String
		Dim lstrOrdFs As String
		Dim lstrOrdGr As String
		'
		Dim ldblAliqS As Double
		Dim ldblBaseS As Double
		Dim ldblIcmsS As Double
		'
		Dim ldblAliqA As Double
		Dim ldblAliqB As Double
		Dim ldblAlPis As Double
		Dim ldblAlCof As Double
		Dim ldblBasPC As Double
		Dim ldblIcmsA As Double
		Dim ldblIcmsB As Double
		Dim ldblBaseA As Double
		Dim ldblBaseB As Double
		Dim ldblkWTarp As Double
		Dim ldblkWTarf As Double
		Dim ldblkWLid As Double
		Dim ldblkWFat As Double
		Dim ldblkWFatD As Double
		Dim ldblkWhFat As Double
		Dim ldblkWhFatA As Double
		Dim ldblkWhFatB As Double
		Dim ldblkWhFatD As Double
		Dim ldblkWhFatS As Double
		'
		Dim ldblEnCnx As Double
		Dim ldblUMust As Double
		Dim ldblDescA As Double
		Dim ldblDescB As Double
		Dim ldblDescP As Double
		'
		Dim ldblRessa As Double 'Ressacirmento de indicadores
		'
		Dim ldblSevTx As Double
		Dim ldblRecFn As Double
		'
		Dim ldblIsent As Double
		Dim ldblDifer As Double
		Dim ldblValor As Double
		'
		Dim ldteIni As Date
		Dim ldteFim As Date
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Me.lblInfo.Text = " Carregado as faturas"
		Me.lblInfo.Refresh()
		Call MontaGrade()
		Me.mfgDados.Redraw = False
		ldblRessa = 0
		ldteIni = CDate("01/" & VB6.Format(Me.dtpPeriodo.value, "MM/YYYY"))
		ldteFim = CDate(UltimoDiaMes(Month(ldteIni), Year(ldteIni)) & "/" & Month(ldteIni) & "/" & Year(ldteIni))
		lstrSql = ""
		lstrSql = lstrSql & "SELECT `nf`.`zona`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`livro`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`conta`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`ta`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`fase`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`cfop`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`codclas`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`flgst`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`datast`, " & vbCrLf
		lstrSql = lstrSql & "       `nf`.`itensserv` " & vbCrLf
		lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
		lstrSql = lstrSql & " WHERE (`nf`.`mesano_ref` = " & CInt(VB6.Format(ldteFim, "yyyymm")) & " OR " & vbCrLf
		lstrSql = lstrSql & "      (`nf`.`flgst` = 'C' AND `nf`.`mesano_ref` <> REPLACE(LEFT(`nf`.`datast`, 7),'-','') AND `nf`.`datast` BETWEEN '" & VB6.Format(ldteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(ldteFim, "yyyy-MM-dd") & "'))" & vbCrLf
		'lstrSql = lstrSql & " and nf.unid_id = 51 and grupo = 'A' "
		'lstrSql = lstrSql & " AND nf.irrigacao_aquicultura = 1 "
		'lstrSql = lstrSql & " and grupo = 'A' and ta = '01AL00' AND parcialmente_cativo = 0 and livre_verde = 1 and unid_id = 85"
		'lstrSql = lstrSql & " and left(nf.codclas,1) = 4 and left(nf.codclas,2) not in(42,43) and nf.grupo = 'B' "
		lstrSql = lstrSql & " and nf.codclas between 5000 and 5999 and nf.fase = 'T'"
		lstrSql = lstrSql & " ORDER BY `nf`.`ta` ASC, `nf`.`codclas` ASC, `nf`.`fase` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State > 0 Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrZon = VB6.Format(IIf(IsDbNull(.Fields("zona").value), "0", .Fields("zona").value), "000")
						'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"'
						lstrLiv = VB6.Format(IIf(IsDbNull(.Fields("livro").value), "0", .Fields("livro").value), "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"'
						lstrCon = VB6.Format(IIf(IsDbNull(.Fields("conta").value), "0", .Fields("conta").value), "000000")
						'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"'
						lstrFas = Trim(IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").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"'
						lstrCla = VB6.Format(IIf(IsDbNull(.Fields("codclas").value), "0", .Fields("codclas").value), "0000")
						'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"'
						lstrTar = Mid(IIf(IsDbNull(.Fields("ta").value), "01B1", .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"'
						lstrTarA = Mid(IIf(IsDbNull(.Fields("ta").value), "01B1", .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"'
						lstrItm = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrFlg = 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))
						lintMpr = 1
						If lstrFlg = "C" Then
							If ldteFlgSt <= ldteFim Then
								lintMpr = -1
							End If
						End If
						ldblkWTarp = 0
						ldblkWTarf = 0
						If lstrZon = "001" And lstrLiv = "99" And lstrCon = "000040" Then
							ldblkWTarf = CDbl(BuscaVlrItem(lstrItm, "01" & lstrTar & "012102"))
							ldblkWTarp = CDbl(BuscaVlrItem(lstrItm, "01" & lstrTar & "012101"))
						End If
						'lstrCfp = Trim(IIf(IsNull(.Fields("cfop").Value), "", .Fields("cfop").Value))
						If VB.Left(lstrTar, 1) = "B" Then
							If CDbl(VB.Left(lstrCla, 1)) < 4 Then
								lstrTpC = "INDUSTRIAL"
								lstrTar = "B3"
								lstrOrdCl = "11"
								lstrOrdGr = "1"
								lstrCfp = "5.252"
							ElseIf CDbl(VB.Left(lstrCla, 1)) = 4 Then 
								lstrTpC = "COMERCIAL"
								lstrTar = "B3"
								lstrOrdCl = "14"
								lstrOrdGr = "1"
								lstrCfp = "5.253"
								If CDbl(VB.Left(lstrCla, 2)) = 42 Then
									lstrTpC = "SERVIÇO TRANSPORTE"
									lstrTar = "B3"
									lstrOrdCl = "16"
									lstrOrdGr = "1"
									lstrCfp = "5.254"
								ElseIf CDbl(VB.Left(lstrCla, 2)) = 43 Then 
									lstrTpC = "SERVIÇO COMUNICAÇÃO"
									lstrTar = "B3"
									lstrOrdCl = "15"
									lstrOrdGr = "1"
									lstrCfp = "5.255"
								End If
							ElseIf CDbl(VB.Left(lstrCla, 1)) = 5 Then 
								lstrTpC = "RURAL"
								lstrTar = "B2"
								lstrOrdCl = "22"
								lstrOrdGr = "1"
								lstrCfp = "5.256"
							ElseIf CDbl(VB.Left(lstrCla, 2)) = 61 Then 
								lstrTpC = "PODER PÚBLICO MUNICIPAL"
								lstrTar = "B3"
								lstrOrdCl = "23"
								lstrOrdGr = "1"
								lstrCfp = "5.258"
							ElseIf CDbl(VB.Left(lstrCla, 2)) = 62 Then 
								lstrTpC = "PODER PÚBLICO ESTADUAL"
								lstrTar = "B3"
								lstrOrdCl = "24"
								lstrOrdGr = "1"
								lstrCfp = "5.258"
							ElseIf CDbl(VB.Left(lstrCla, 2)) = 63 Then 
								lstrTpC = "PODER PÚBLICO FEDERAL"
								lstrTar = "B3"
								lstrOrdCl = "25"
								lstrOrdGr = "1"
								lstrCfp = "5.258"
							ElseIf CDbl(VB.Left(lstrCla, 2)) = 64 Then 
								lstrTpC = "ILUMINAÇÃO PÚBLICA"
								lstrTar = "B4"
								lstrOrdCl = "17"
								lstrOrdGr = "1"
								lstrCfp = "5.258"
							ElseIf CDbl(VB.Left(lstrCla, 1)) = 7 Then 
								lstrTpC = "SERVIÇO PÚBLICO"
								lstrTar = "B3"
								lstrOrdCl = "18"
								lstrOrdGr = "1"
								lstrCfp = "5.258"
							ElseIf CDbl(VB.Left(lstrCla, 1)) = 8 Then 
								lstrTpC = "CONSUMO PRÓPRIO"
								lstrTar = "B3"
								lstrOrdCl = "19"
								lstrOrdGr = "1"
								lstrCfp = "5.253"
							ElseIf CDbl(VB.Left(lstrCla, 1)) = 9 Then 
								If VB.Right(lstrCla, 2) = "04" Or VB.Right(lstrCla, 2) = "05" Or VB.Right(lstrCla, 2) = "06" Then
									lstrTpC = "RESIDENCIAL BX.RENDA"
									lstrOrdCl = "21"
								Else
									lstrTpC = "RESIDENCIAL NORMAL"
									lstrOrdCl = "20"
								End If
								lstrTar = "B1"
								lstrOrdGr = "1"
								lstrCfp = "5.258"
							End If
						Else
							If VB.Left(lstrTar, 2) = "AS" Or VB.Left(lstrTar, 2) = "AX" Then
								lstrTpC = "DEMANDA CONTRATADA (COOPER)"
								lstrOrdCl = "13"
							Else
								lstrTpC = "DEMANDA CONTRATADA"
								lstrOrdCl = "12"
							End If
							lstrTar = "A4"
							lstrCfp = "5.257"
							lstrOrdGr = "1"
						End If
						
						If lstrFas = "M" Then
							lstrOrdFs = "1"
						ElseIf lstrFas = "B" Then 
							lstrOrdFs = "2"
						ElseIf lstrFas = "T" Then 
							lstrOrdFs = "3"
						End If
						
						ldblAliqA = Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302030002")), 2)
						ldblAliqB = Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302030013")), 2)
						ldblIcmsA = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302030009")), 2) * lintMpr)
						ldblIcmsB = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302030010")), 2) * lintMpr)
						ldblBaseA = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302030001")), 2) * lintMpr)
						ldblBaseB = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302030003")), 2) * lintMpr)
						'          If CLng(Format(ldteFim, "yyyymm")) = 201401 Then
						'            If lstrZon = "001" Then
						'              If lstrLiv = "01" And lstrCon = "000810" Then
						'                ldblBaseA = 8908.43
						'                ldblIcmsA = 2227.11
						'              ElseIf lstrLiv = "02" And lstrCon = "001262" Then
						'                ldblBaseA = 9123.47
						'                ldblIcmsA = 2280.87
						'              End If
						'            End If
						'          End If
						ldblAliqS = Arredondar(CDbl(BuscaVlrItem(lstrItm, "0601020001")), 2)
						ldblIcmsS = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0602020001")), 2) * lintMpr) + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0604020001")), 2) * lintMpr)
						ldblBaseS = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0602010001")), 2) * lintMpr) + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0604010001")), 2) * lintMpr)
						If lstrFlg <> "C" And ldblBaseB < 0 Then ldblBaseB = 0
						ldblDifer = 0
						ldblkWLid = 0
						ldblkWFat = 0
						If CInt(VB6.Format(Me.dtpPeriodo._Value, "YYYYMM")) <= 201108 Then
							If lstrZon = "001" And lstrLiv = "99" And lstrCon = "000040" Then
								'Fora de Ponta
								ldblkWLid = Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010023")), 0)
								ldblkWFat = Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010025")), 0)
								ldblAlPis = CDbl(BuscaVlrItem(lstrItm, "40PIS00001"))
								ldblAlCof = CDbl(BuscaVlrItem(lstrItm, "40COF00001"))
								'ldblDifer = CDbl(BuscaVlrItem(lstrItm, "0302030021")) 'Demanda lida para desconto provisorio
								ldblBasPC = CDbl(BuscaVlrItem(lstrItm, "0302030020"))
								ldblBasPC = Arredondar((ldblBasPC * (ldblAlPis / 100)) + (ldblBasPC * (ldblAlCof / 100)), 2)
								If ldblkWFat > ldblkWLid Then
									ldblDifer = Arredondar((ldblkWFat - ldblkWLid) * ldblkWTarf, 2) + IIf(VB6.Format(Me.dtpPeriodo._Value, "MM/YYYY") = "06/2010", 44.39, 0) + IIf(CBool(VB6.Format(Me.dtpPeriodo._Value, "MM/YYYY")), -8646.18, 0) + IIf(VB6.Format(Me.dtpPeriodo._Value, "MM/YYYY") = "06/2011", -4026.24, 0) + IIf(VB6.Format(Me.dtpPeriodo._Value, "MM/YYYY") = "07/2011", -5086.09, 0) + IIf(VB6.Format(Me.dtpPeriodo._Value, "MM/YYYY") = "08/2011", -3247.43, 0)
									ldblDifer = ldblDifer + ldblBasPC
								End If
								'Ponta
								ldblkWLid = Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010019")), 0)
								ldblkWFat = Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010021")), 0)
								ldblAlPis = CDbl(BuscaVlrItem(lstrItm, "40PIS00001"))
								ldblAlCof = CDbl(BuscaVlrItem(lstrItm, "40COF00001"))
								ldblBasPC = CDbl(BuscaVlrItem(lstrItm, "0302030019"))
								ldblBasPC = Arredondar((ldblBasPC * (ldblAlPis / 100)) + (ldblBasPC * (ldblAlCof / 100)), 2)
								If ldblkWFat > ldblkWLid Then
									ldblDifer = ldblDifer + (Arredondar((ldblkWFat - ldblkWLid) * ldblkWTarp, 2) + ldblBasPC)
								End If
							End If
						Else
							ldblDifer = ldblDifer + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302030030")), 2) * lintMpr)
						End If
						If (ldblAliqA = 0 And ldblIcmsA = 0) Then
							If lstrOrdCl = "13" Then
								If ldblBaseA = 0 Then
									ldblDifer = ldblDifer + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302020034")), 2) * lintMpr) 'base Pis
								End If
							End If
							If ldblBaseA <> 0 Then
								ldblDifer = ldblDifer + ldblBaseA
							End If
							ldblBaseA = 0
						End If
						If (ldblAliqB = 0 And ldblIcmsB = 0) Then
							If ldblBaseB <> 0 Then
								ldblDifer = ldblDifer + ldblBaseB
							End If
							ldblBaseB = 0
						End If
						'Usinas
						ldblEnCnx = (CDbl(BuscaVlrItem(lstrItm, "9080000098")) * lintMpr) 'Encargo de Conexão
						ldblUMust = (CDbl(BuscaVlrItem(lstrItm, "0302020032")) * lintMpr) 'EUSD - MUST
						ldblDescA = (CDbl(BuscaVlrItem(lstrItm, "9070000087")) * lintMpr) 'Desconto Res  22 22.05.2005
						ldblDescB = (CDbl(BuscaVlrItem(lstrItm, "9070000091")) * lintMpr) 'Desconto Res 466 20.02.2006
						ldblDescP = (CDbl(BuscaVlrItem(lstrItm, "9070000090")) * lintMpr) 'Desconto Provisorio para o Livre especial
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000030")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000031")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000032")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000033")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000034")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000035")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000036")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000037")) * lintMpr)
						ldblRessa = ldblRessa + (CDbl(BuscaVlrItem(lstrItm, "9070000004")) * lintMpr) 'devolução cobrança a maior
						If CInt(VB6.Format(ldteFim, "yyyymm")) >= 201204 Then
							ldblRessa = 0
						End If
						
						ldblkWhFat = 0
						ldblkWhFatA = 0
						ldblkWhFatB = 0
						ldblkWhFatD = 0
						ldblkWhFatS = 0
						ldblkWFat = 0
						ldblkWFatD = 0
						If VB.Left(lstrTar, 1) = "B" Then
							'consumo e demanda
							ldblkWhFat = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "030201009")), 2) * lintMpr)
							If ldblDifer = 0 Then
								If ldblAliqA <> ldblAliqB Then
									If ldblAliqA > 0 Then
										If CDbl(VB.Left(lstrCla, 1)) = 9 Then
											ldblkWhFatA = (IIf(ldblkWhFat > ilngLimRes, ilngLimRes, ldblkWhFat)) '* lintMpr)
										ElseIf CDbl(VB.Left(lstrCla, 1)) = 5 Then 
											ldblkWhFatA = (IIf(ldblkWhFat > ilngLimRur, ilngLimRur, ldblkWhFat)) '* lintMpr)
										Else
											ldblkWhFatA = (ldblkWhFat) '* lintMpr)
										End If
										ldblkWhFatB = ((ldblkWhFat - ldblkWhFatA)) '* lintMpr)
									End If
									If ldblAliqA = 0 And ldblAliqB > 0 Then
										ldblkWhFatB = (ldblkWhFat) ' * lintMpr)
									End If
								End If
							Else
								ldblkWhFatD = ldblkWhFat
							End If
						ElseIf VB.Left(lstrTar, 1) = "A" Then 
							'consumo
							ldblkWhFatS = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0601010006")), 2) * lintMpr) + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0603010006")), 2) * lintMpr)
							If ldblDifer = 0 Then
								If VB.Left(lstrTarA, 2) = "A4" Or VB.Left(lstrTarA, 2) = "AS" Then
									ldblkWhFatA = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010009")), 2) * lintMpr)
								ElseIf VB.Left(lstrTarA, 2) = "AA" Or VB.Left(lstrTarA, 2) = "AV" Or VB.Left(lstrTarA, 2) = "AL" Or VB.Left(lstrTarA, 2) = "AP" Or VB.Left(lstrTarA, 2) = "AX" Then 
									ldblkWhFatA = ldblkWhFatA + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010010")) + CDbl(BuscaVlrItem(lstrItm, "0302010060")), 2) * lintMpr)
									ldblkWhFatA = ldblkWhFatA + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010011")) + CDbl(BuscaVlrItem(lstrItm, "0302010061")), 2) * lintMpr)
								End If
							Else
								If VB.Left(lstrTarA, 2) = "A4" Or VB.Left(lstrTarA, 2) = "AS" Then
									If VB.Left(lstrTarA, 2) = "AS" Then
										ldblkWhFatD = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010009")), 2) * lintMpr)
									Else
										ldblkWhFatA = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010009")), 2) * lintMpr)
									End If
								ElseIf VB.Left(lstrTarA, 2) = "AA" Or VB.Left(lstrTarA, 2) = "AV" Or VB.Left(lstrTarA, 2) = "AL" Or VB.Left(lstrTarA, 2) = "AP" Or VB.Left(lstrTarA, 2) = "AX" Then 
									If VB.Left(lstrTarA, 2) = "AX" Then
										ldblkWhFatD = ldblkWhFatD + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010010")) + CDbl(BuscaVlrItem(lstrItm, "0302010060")), 2) * lintMpr)
										ldblkWhFatD = ldblkWhFatD + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010011")) + CDbl(BuscaVlrItem(lstrItm, "0302010061")), 2) * lintMpr)
									Else
										ldblkWhFatA = ldblkWhFatA + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010010")) + CDbl(BuscaVlrItem(lstrItm, "0302010060")), 2) * lintMpr)
										ldblkWhFatA = ldblkWhFatA + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010011")) + CDbl(BuscaVlrItem(lstrItm, "0302010061")), 2) * lintMpr)
									End If
								End If
							End If
							'demanda
							If ldblDifer = 0 Then
								If VB.Left(lstrTarA, 2) = "A4" Or VB.Left(lstrTarA, 2) = "AS" Then
									ldblkWFat = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010017")), 2) * lintMpr)
								ElseIf VB.Left(lstrTarA, 2) = "AA" Or VB.Left(lstrTarA, 2) = "AV" Or VB.Left(lstrTarA, 2) = "AL" Or VB.Left(lstrTarA, 2) = "AP" Or VB.Left(lstrTarA, 2) = "AX" Then 
									ldblkWFat = ldblkWFat + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010021")), 2) * lintMpr)
									ldblkWFat = ldblkWFat + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010025")), 2) * lintMpr)
								End If
							Else
								If VB.Left(lstrTarA, 2) = "A4" Or VB.Left(lstrTarA, 2) = "AS" Then
									ldblkWFatD = (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010017")), 2) * lintMpr)
								ElseIf VB.Left(lstrTarA, 2) = "AA" Or VB.Left(lstrTarA, 2) = "AV" Or VB.Left(lstrTarA, 2) = "AL" Or VB.Left(lstrTarA, 2) = "AP" Or VB.Left(lstrTarA, 2) = "AX" Then 
									ldblkWFatD = ldblkWFatD + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010021")), 2) * lintMpr)
									ldblkWFatD = ldblkWFatD + (Arredondar(CDbl(BuscaVlrItem(lstrItm, "0302010025")), 2) * lintMpr)
								End If
							End If
						End If
						lstrCod = "01." & lstrOrdCl & "." & lstrOrdGr & "." & lstrOrdFs
						Call AdicionaGrade("CLASSE", lstrTpC, lstrTar, lstrFas, lstrCfp, ldblAliqA, ldblAliqB, ldblIsent, ldblDifer, ldblBaseA, ldblIcmsA, ldblBaseB, ldblIcmsB, lstrCod, ldblAliqS, ldblBaseS, ldblIcmsS, ldblkWhFatA, ldblkWhFatB, ldblkWhFatD, ldblkWFat, ldblkWFatD, ldblkWhFatS)
						ldblSevTx = 0
						ldblRecFn = 0
						ldblValor = 0
						If VB.Left(lstrTpC, 11) = "RESIDENCIAL" Then
							lstrTpC = "RESIDENCIAL"
							lstrOrdCl = "20"
						End If
						'If lstrFlg <> "C" Then
						For IndCodServ = 1 To Len(Trim(lstrItm))
							Call BuscaCodItem(IndCodServ, lstrItm)
							If Mid(codserv, 1, 4) = "9040" Or Mid(codserv, 1, 4) = "9050" Or codserv = "9070000051" Then
								If CDbl(VB.Left(codserv, 4)) = 9070 Then
									ldblSevTx = ldblSevTx - (CDbl(Replace(VlrCodServ, ".", ",")) * lintMpr)
								Else
									ldblSevTx = ldblSevTx + (CDbl(Replace(VlrCodServ, ".", ",")) * lintMpr)
								End If
							End If
							'diversos
							If Mid(codserv, 1, 4) = "9080" Then
								If codserv <> "9080000098" And codserv <> "9080000092" Then
									ldblRecFn = ldblRecFn + (CDbl(Replace(VlrCodServ, ".", ",")) * lintMpr)
								End If
							End If
							'Devoluções
							If Mid(codserv, 1, 4) = "9070" Then
								If VB.Left(codserv, 9) <> "907000003" And codserv <> "9070000051" And codserv <> "9070000062" And codserv <> "9070000079" And codserv <> "9070000081" And codserv <> "9070000082" And codserv <> "9070000087" And codserv <> "9070000088" And codserv <> "9070000090" And codserv <> "9070000091" And codserv <> "9070000092" And codserv <> "9070000096" And codserv <> "9070000097" And codserv <> "9070000040" And codserv <> "9070000041" And codserv <> "9070000084" And codserv <> "9070000004" Then
									
									ldblRecFn = ldblRecFn - (CDbl(Replace(VlrCodServ, ".", ",")) * lintMpr)
								End If
							End If
						Next IndCodServ
						If ldblSevTx <> 0 Then
							lstrCod = "02." & lstrOrdCl & "." & lstrOrdGr & "." & lstrOrdFs
							Call AdicionaGrade("SERVIÇOS TAXADOS", lstrTpC, lstrTar, lstrFas, "5.949-1", 0, 0, ldblSevTx, 0, 0, 0, 0, 0, lstrCod, 0, 0, 0, 0, 0, 0, 0, 0, 0)
						End If
						If ldblRecFn <> 0 Then
							lstrCod = "03." & lstrOrdCl & "." & lstrOrdGr & "." & lstrOrdFs
							Call AdicionaGrade("REC.FINANCEIRAS", lstrTpC, lstrTar, lstrFas, "5.949-2", 0, 0, ldblRecFn, 0, 0, 0, 0, 0, lstrCod, 0, 0, 0, 0, 0, 0, 0, 0, 0)
						End If
						If ldblUMust <> 0 Then
							lstrCod = "04." & lstrOrdCl & "." & lstrOrdGr & "." & lstrOrdFs
							'Call AdicionaGrade("MUST", lstrTpC, lstrTar, lstrFas, "5.949-3", 0, 0, (ldblUMust - (ldblDescA + ldblDescB)), 0, 0, 0, 0, 0, lstrCod, 0, 0, 0,0,0, 0, 0,0)
							Call AdicionaGrade("MUST", lstrTpC, lstrTar, lstrFas, "5.949-3", 0, 0, ((ldblDescA + ldblDescB)), 0, 0, 0, 0, 0, lstrCod, 0, 0, 0, 0, 0, 0, 0, 0, 0)
						End If
						If ldblEnCnx <> 0 Then
							lstrCod = "05." & lstrOrdCl & "." & lstrOrdGr & "." & lstrOrdFs
							Call AdicionaGrade("ENCARGO USO SIS.TRANS.", lstrTpC, lstrTar, lstrFas, "5.949-4", 0, 0, ldblEnCnx, 0, 0, 0, 0, 0, lstrCod, 0, 0, 0, 0, 0, 0, 0, 0, 0)
						End If
						'End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
					If ldblRessa <> 0 Then
						ldblRessa = ldblRessa * -1
						lstrCod = "03.20.1.1"
						Call AdicionaGrade("REC.FINANCEIRAS", "RESIDENCIAL NORMAL", "B1", "M", "5.949-2", 0, 0, ldblRessa, 0, 0, 0, 0, 0, lstrCod, 0, 0, 0, 0, 0, 0, 0, 0, 0)
						ldblRessa = 0
					End If
				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
		With Me.mfgDados
			.Col = 14
			.Sort = MSFlexGridLib.SortSettings.flexSortGenericAscending
			.Redraw = True
		End With
		Call LimpaDesnecessarios()
	End Sub
	
	Private Sub AdicionaGrade(ByVal astrGrC As String, ByVal astrCla As String, ByVal astrGru As String, ByVal astrFas As String, ByVal astrCfp As String, ByVal adblAqA As Double, ByVal adblAqB As Double, ByVal adblIse As Double, ByVal adblDif As Double, ByVal adblBsA As Double, ByVal adblIcA As Double, ByVal adblBsB As Double, ByVal adblIcB As Double, ByVal astrCod As String, ByVal adblAqS As Double, ByVal adblBsS As Double, ByVal adblIcS As Double, ByVal adblkWhFatA As Double, ByVal adblkWhFatB As Double, ByVal adblkWhFatD As Double, ByVal adblkWFat As Double, ByVal adblkWFatD As Double, ByVal adblkWhFatS As Double)
		Dim llngRow As Integer
		Dim llngPos As Integer
		llngPos = 0
		With Me.mfgDados
			'Busca a linha da grid que tem a informacao
			For llngRow = 1 To .Rows - 1
				If .get_TextMatrix(llngRow, 14) = astrCod Then
					llngPos = llngRow
					llngRow = .Rows - 1
				End If
			Next llngRow
			If llngPos = 0 Then
				.Rows = .Rows + 1
				llngPos = (.Rows - 1)
				.set_TextMatrix(llngPos, 0, astrGrC) 'Grupo Classe
				.set_TextMatrix(llngPos, 1, astrCla) 'Classe
				.set_TextMatrix(llngPos, 2, astrGru) 'Grupo
				.set_TextMatrix(llngPos, 3, astrFas) 'Fase
				.set_TextMatrix(llngPos, 14, astrCod)
			End If
			.set_TextMatrix(llngPos, 4, astrCfp) 'Cfop
			If (adblIse + adblDif + adblBsA + adblIcA + adblBsB + adblIcB) <> 0 Then
				If (adblIse + adblDif + adblBsA + adblIcA + adblBsB + adblIcB) > 0 Then
					.set_TextMatrix(llngPos, 5, FormatNumber(CInt(IIf(.get_TextMatrix(llngPos, 5) = "", 0, .get_TextMatrix(llngPos, 5))) + 1, 0)) 'Qtde
				End If
				.set_TextMatrix(llngPos, 6, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 6) = "", 0, .get_TextMatrix(llngPos, 6))) + adblIse, 2)) 'Isentos
				.set_TextMatrix(llngPos, 7, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 7) = "", 0, .get_TextMatrix(llngPos, 7))) + adblDif, 2)) 'Diferido
				If .get_TextMatrix(llngPos, 8) = "" Or .get_TextMatrix(llngPos, 8) = "0,00" Then
					.set_TextMatrix(llngPos, 8, FormatNumber(adblAqA, 2)) 'Aliquota
				End If
				.set_TextMatrix(llngPos, 9, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 9) = "", 0, .get_TextMatrix(llngPos, 9))) + adblBsA, 2)) 'Base A
				.set_TextMatrix(llngPos, 10, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 10) = "", 0, .get_TextMatrix(llngPos, 10))) + adblIcA, 2)) 'Icms A
				If .get_TextMatrix(llngPos, 11) = "" Or .get_TextMatrix(llngPos, 11) = "0,00" Then
					.set_TextMatrix(llngPos, 11, FormatNumber(adblAqB, 2)) 'Aliquota
				End If
				.set_TextMatrix(llngPos, 12, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 12) = "", 0, .get_TextMatrix(llngPos, 12))) + adblBsB, 2)) 'Base B
				.set_TextMatrix(llngPos, 13, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 13) = "", 0, .get_TextMatrix(llngPos, 13))) + adblIcB, 2)) 'Icms B
				
				If .get_TextMatrix(llngPos, 15) = "" Or .get_TextMatrix(llngPos, 15) = "0,00" Then
					.set_TextMatrix(llngPos, 15, FormatNumber(adblAqS, 2)) 'Aliquota ST
				End If
				.set_TextMatrix(llngPos, 16, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 16) = "", 0, .get_TextMatrix(llngPos, 16))) + adblBsS, 2)) 'Base ST
				.set_TextMatrix(llngPos, 17, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 17) = "", 0, .get_TextMatrix(llngPos, 17))) + adblIcS, 2)) 'Icms ST
				.set_TextMatrix(llngPos, 18, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 18) = "", 0, .get_TextMatrix(llngPos, 18))) + adblkWhFatA, 2)) 'kWh
				.set_TextMatrix(llngPos, 19, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 19) = "", 0, .get_TextMatrix(llngPos, 19))) + adblkWhFatB, 2)) 'kWh
				.set_TextMatrix(llngPos, 20, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 20) = "", 0, .get_TextMatrix(llngPos, 20))) + adblkWhFatD, 2)) 'kWh
				.set_TextMatrix(llngPos, 21, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 21) = "", 0, .get_TextMatrix(llngPos, 21))) + adblkWFat, 2)) 'kWh
				.set_TextMatrix(llngPos, 22, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 22) = "", 0, .get_TextMatrix(llngPos, 22))) + adblkWFatD, 2)) 'kWh
				.set_TextMatrix(llngPos, 23, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 23) = "", 0, .get_TextMatrix(llngPos, 23))) + adblkWhFatS, 2)) 'kWh
			End If
		End With
	End Sub
	
	Private Sub MontaGrupos(ByVal astrGrCl As String, ByVal astrSeq As String, ByVal astrCfop As String)
		Call AdicionaGrade(astrGrCl, "INDUSTRIAL", "B3", "M", IIf(astrCfop = "", "5.252", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".11.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "INDUSTRIAL", "B3", "B", IIf(astrCfop = "", "5.252", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".11.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "INDUSTRIAL", "B3", "T", IIf(astrCfop = "", "5.252", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".11.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "DEMANDA CONTRATADA", "A4", "M", IIf(astrCfop = "", "5.257", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".12.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "DEMANDA CONTRATADA", "A4", "B", IIf(astrCfop = "", "5.257", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".12.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "DEMANDA CONTRATADA", "A4", "T", IIf(astrCfop = "", "5.257", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".12.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		
		Call AdicionaGrade(astrGrCl, "DEMANDA CONTRATADA (COOPER)", "A4", "M", IIf(astrCfop = "", "5.257", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".13.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "DEMANDA CONTRATADA (COOPER)", "A4", "B", IIf(astrCfop = "", "5.257", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".13.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "DEMANDA CONTRATADA (COOPER)", "A4", "T", IIf(astrCfop = "", "5.257", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".13.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		
		
		Call AdicionaGrade(astrGrCl, "COMERCIAL", "B3", "M", IIf(astrCfop = "", "5.253", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".14.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "COMERCIAL", "B3", "B", IIf(astrCfop = "", "5.253", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".14.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "COMERCIAL", "B3", "T", IIf(astrCfop = "", "5.253", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".14.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO COMUNICAÇÃO", "B3", "M", IIf(astrCfop = "", "5.255", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".15.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO COMUNICAÇÃO", "B3", "B", IIf(astrCfop = "", "5.255", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".15.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO COMUNICAÇÃO", "B3", "T", IIf(astrCfop = "", "5.255", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".15.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO TRANSPORTE", "B3", "M", IIf(astrCfop = "", "5.254", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".16.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO TRANSPORTE", "B3", "B", IIf(astrCfop = "", "5.254", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".16.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO TRANSPORTE", "B3", "T", IIf(astrCfop = "", "5.254", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".16.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "ILUMINAÇÃO PÚBLICA", "B4", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".17.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "ILUMINAÇÃO PÚBLICA", "B4", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".17.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "ILUMINAÇÃO PÚBLICA", "B4", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".17.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO PÚBLICO", "B3", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".18.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO PÚBLICO", "B3", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".18.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "SERVIÇO PÚBLICO", "B3", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".18.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "CONSUMO PRÓPRIO", "B3", "M", IIf(astrCfop = "", "5.253", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".19.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "CONSUMO PRÓPRIO", "B3", "B", IIf(astrCfop = "", "5.253", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".19.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "CONSUMO PRÓPRIO", "B3", "T", IIf(astrCfop = "", "5.253", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".19.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		If astrGrCl = "CLASSE" Then
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL NORMAL", "B1", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".20.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL NORMAL", "B1", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".20.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL NORMAL", "B1", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".20.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL BX.RENDA", "B1", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".21.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL BX.RENDA", "B1", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".21.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL BX.RENDA", "B1", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".21.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Else
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL", "B1", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".20.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL", "B1", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".20.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
			Call AdicionaGrade(astrGrCl, "RESIDENCIAL", "B1", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".20.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		End If
		Call AdicionaGrade(astrGrCl, "RURAL", "B2", "M", IIf(astrCfop = "", "5.256", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".22.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "RURAL", "B2", "B", IIf(astrCfop = "", "5.256", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".22.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "RURAL", "B2", "T", IIf(astrCfop = "", "5.256", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".22.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO MUNICIPAL", "B3", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".23.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO MUNICIPAL", "B3", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".23.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO MUNICIPAL", "B3", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".23.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO ESTADUAL", "B3", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".24.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO ESTADUAL", "B3", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".24.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO ESTADUAL", "B3", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".24.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO FEDERAL", "B3", "M", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".25.1.1", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO FEDERAL", "B3", "B", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".25.1.2", 0, 0, 0, 0, 0, 0, 0, 0, 0)
		Call AdicionaGrade(astrGrCl, "PODER PÚBLICO FEDERAL", "B3", "T", IIf(astrCfop = "", "5.258", astrCfop), 0, 0, 0, 0, 0, 0, 0, 0, astrSeq & ".25.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0)
	End Sub
	
	Public Sub CabecaRel()
		With ifrmIcms
			Call Cabecalho_Relatorio(.VS, gstrEmpresa_Nome, "Relatório de ICMS - Faturamento do Mês " & VB6.Format(Me.dtpPeriodo._Value, "MM/YYYY"), "4", True, "GERAL - (GRUPO A+B)")
			.VS.CurrentLine = 1
			.VS.Font = VB6.FontChangeSize(.VS.Font, 7)
			Call LineFeed(.VS, 1)
			.VS.Table = istrTamCol & istrCabCol
			'.VS.CurrentY = .VS.CurrentY + 100
		End With
		'Call CabecTipo
	End Sub
	
	Private Sub CabecTipo()
		Dim lstrTab As String
		If istrGruCla <> "" Then
			lstrTab = ""
			lstrTab = lstrTab & istrGruCla & "|" 'Grupo Classe
			lstrTab = lstrTab & "|" 'Grupo
			lstrTab = lstrTab & "|" 'Fase
			lstrTab = lstrTab & "|" 'Cfop
			lstrTab = lstrTab & "|" 'Qtde Consumidores
			lstrTab = lstrTab & "|" 'kWh
			lstrTab = lstrTab & "|" 'Aliquota
			lstrTab = lstrTab & "|" 'Base Icms
			lstrTab = lstrTab & "|" 'Valor Icms
			lstrTab = lstrTab & "|" 'Isentos
			lstrTab = lstrTab & "" 'Diferido
			With ifrmIcms
				.VS.Font = VB6.FontChangeBold(.VS.Font, True)
				.VS.Table = istrTamCol & lstrTab
				.VS.Font = VB6.FontChangeBold(.VS.Font, False)
			End With
		End If
	End Sub
	
	Private Sub ImpIcms()
		Dim llngRow As Integer
		Dim lstrTab As String
		Dim lstrCla As String
		Dim ldblAlqS As Double
		Dim ldblBasS As Double
		Dim ldblIcms As Double
		Dim ldblKWhA As Double
		Dim ldblKWhB As Double
		Dim ldblkWhD As Double
		Dim ldblkWhS As Double
		Dim ldblAlqA As Double
		Dim ldblBasA As Double
		Dim ldblIcmA As Double
		Dim ldblAlqB As Double
		Dim ldblBasB As Double
		Dim ldblIcmB As Double
		Dim ldblIsen As Double
		Dim ldblDife As Double
		Dim ldblQtde As Integer
		'
		Dim llngTcQtd As Integer
		Dim ldblTcIcm As Double
		Dim ldblTckWh As Double
		Dim ldblTcBas As Double
		Dim ldblTcIse As Double
		Dim ldblTcDif As Double
		'
		Dim llngTgQtd As Integer
		Dim ldblTgIcm As Double
		Dim ldblTgkWh As Double
		Dim ldblTgBas As Double
		Dim ldblTgIse As Double
		Dim ldblTgDif As Double
		'
		Dim llngTtQtd As Integer
		Dim ldblTtIcm As Double
		Dim ldblTtkWh As Double
		Dim ldblTtBas As Double
		Dim ldblTtIse As Double
		Dim ldblTtDif As Double
		'
		Dim ldblAlqSTotal As Double
		Dim ldblBasSTotal As Double
		Dim ldblIcmsTotal As Double
		'
		With ifrmIcms
			.quem_chama = Me
			.VS.Font = VB6.FontChangeName(.VS.Font, "Arial")
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.ShowGuides = VSVIEW6.ShowGuideSettings.gdShow
			.VS.Orientation = VSVIEW6.OrientationSettings.orPortrait
			.VS.PhysicalPage = True
			.VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
			.VS.MarginTop = "7mm"
			.VS.MarginRight = "7mm"
			.VS.MarginLeft = "7mm"
			.VS.MarginBottom = "5mm"
			.VS.MarginFooter = "0mm"
			.VS.MarginHeader = "0mm"
			.VS.TextColor = System.Drawing.Color.Black
			.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
			.VS.StartDoc()
		End With
		With Me.mfgDados
			istrGruCla = .get_TextMatrix(1, 0)
			lstrCla = ""
			For llngRow = 1 To .Rows - 1
				ldblQtde = CInt(IIf(.get_TextMatrix(llngRow, 5) = "", 0, .get_TextMatrix(llngRow, 5)))
				ldblAlqA = CDbl(IIf(.get_TextMatrix(llngRow, 8) = "", 0, .get_TextMatrix(llngRow, 8)))
				ldblBasA = CDbl(IIf(.get_TextMatrix(llngRow, 9) = "", 0, .get_TextMatrix(llngRow, 9)))
				ldblIcmA = CDbl(IIf(.get_TextMatrix(llngRow, 10) = "", 0, .get_TextMatrix(llngRow, 10)))
				ldblAlqB = CDbl(IIf(.get_TextMatrix(llngRow, 11) = "", 0, .get_TextMatrix(llngRow, 11)))
				ldblBasB = CDbl(IIf(.get_TextMatrix(llngRow, 12) = "", 0, .get_TextMatrix(llngRow, 12)))
				ldblIcmB = CDbl(IIf(.get_TextMatrix(llngRow, 13) = "", 0, .get_TextMatrix(llngRow, 13)))
				ldblIsen = CDbl(IIf(.get_TextMatrix(llngRow, 6) = "", 0, .get_TextMatrix(llngRow, 6)))
				ldblDife = CDbl(IIf(.get_TextMatrix(llngRow, 7) = "", 0, .get_TextMatrix(llngRow, 7)))
				'
				ldblAlqS = CDbl(IIf(.get_TextMatrix(llngRow, 15) = "", 0, .get_TextMatrix(llngRow, 15)))
				ldblBasS = CDbl(IIf(.get_TextMatrix(llngRow, 16) = "", 0, .get_TextMatrix(llngRow, 16)))
				ldblIcms = CDbl(IIf(.get_TextMatrix(llngRow, 17) = "", 0, .get_TextMatrix(llngRow, 17)))
				'
				ldblKWhA = CDbl(IIf(.get_TextMatrix(llngRow, 18) = "", 0, .get_TextMatrix(llngRow, 18))) + CDbl(IIf(.get_TextMatrix(llngRow, 21) = "", 0, .get_TextMatrix(llngRow, 21)))
				ldblKWhB = CDbl(IIf(.get_TextMatrix(llngRow, 19) = "", 0, .get_TextMatrix(llngRow, 19)))
				ldblkWhD = CDbl(IIf(.get_TextMatrix(llngRow, 20) = "", 0, .get_TextMatrix(llngRow, 20))) + CDbl(IIf(.get_TextMatrix(llngRow, 22) = "", 0, .get_TextMatrix(llngRow, 22)))
				ldblkWhS = CDbl(IIf(.get_TextMatrix(llngRow, 23) = "", 0, .get_TextMatrix(llngRow, 23)))
				'
				If istrGruCla <> .get_TextMatrix(llngRow, 0) Then
					Call ImpTotSubTot(llngTcQtd, ldblTcBas, ldblTcIcm, ldblTcIse, ldblTcDif, ldblTckWh, "T")
					Call ImpTotSubTot(llngTgQtd, ldblTgBas, ldblTgIcm, ldblTgIse, ldblTgDif, ldblTgkWh, "S")
					istrGruCla = .get_TextMatrix(llngRow, 0)
					Call ifrmIcms.VS.NewPage()
					Call CabecTipo()
					lstrCla = ""
				End If
				lstrTab = ""
				'If .TextMatrix(llngRow, 1) = "PODER PÚBLICO ESTADUAL" Or .TextMatrix(llngRow, 1) = "DEMANDA CONTRATADA" Then
				If lstrCla <> .get_TextMatrix(llngRow, 1) Then
					If lstrCla <> "" Then
						If Mid(.get_TextMatrix(llngRow, 14), 4, 2) <> "11" Then
							Call ImpTotSubTot(llngTcQtd, ldblTcBas, ldblTcIcm, ldblTcIse, ldblTcDif, ldblTckWh, "T")
						End If
					End If
					Call LineFeed((ifrmIcms.VS), 1)
					lstrTab = ""
					lstrTab = lstrTab & .get_TextMatrix(llngRow, 1) & "|" 'Classe
					lstrCla = .get_TextMatrix(llngRow, 1)
				Else
					lstrTab = lstrTab & "|" 'Classe
				End If
				lstrTab = lstrTab & .get_TextMatrix(llngRow, 2) & "|" 'Grupo
				lstrTab = lstrTab & .get_TextMatrix(llngRow, 3) & "|" 'Fase
				lstrTab = lstrTab & .get_TextMatrix(llngRow, 4) & "|" 'CFOP
				lstrTab = lstrTab & FormatNumber(ldblQtde, 0) & "|" 'Qtde Consumidores
				If ldblAlqA > 0 Then
					lstrTab = lstrTab & FormatNumber(ldblKWhA, 2) & "|" 'Qtd kWh
					lstrTab = lstrTab & FormatNumber(ldblAlqA, 2) & "|" 'Aliquota
					lstrTab = lstrTab & FormatNumber(ldblBasA, 2) & "|" 'Base Icms
					lstrTab = lstrTab & FormatNumber(ldblIcmA, 2) & "|" 'Valor Icms
					lstrTab = lstrTab & "-|" 'Isento
					lstrTab = lstrTab & "-" 'Diferito
				ElseIf ldblAlqB > 0 Then 
					lstrTab = lstrTab & FormatNumber(ldblKWhB, 2) & "|" 'Qtd kWh
					lstrTab = lstrTab & FormatNumber(ldblAlqB, 2) & "|" 'Aliquota
					lstrTab = lstrTab & FormatNumber(ldblBasB, 2) & "|" 'Base Icms
					lstrTab = lstrTab & FormatNumber(ldblIcmB, 2) & "|" 'Valor Icms
					lstrTab = lstrTab & "-|" 'Isento
					lstrTab = lstrTab & "-" 'Diferito
				End If
				If ldblAlqB > 0 And ldblAlqA > 0 Then
					ifrmIcms.VS.Table = istrTamCol & lstrTab
					lstrTab = ""
					lstrTab = lstrTab & "|" 'Classe
					lstrTab = lstrTab & "|" 'Grupo
					lstrTab = lstrTab & "|" 'Fase
					lstrTab = lstrTab & "|" 'Cfop
					lstrTab = lstrTab & "|" 'Qtde Consumidores
					lstrTab = lstrTab & FormatNumber(ldblKWhB, 2) & "|" 'Qtd kWh
					lstrTab = lstrTab & FormatNumber(ldblAlqB, 2) & "|" 'Aliquota
					lstrTab = lstrTab & FormatNumber(ldblBasB, 2) & "|" 'Base Icms
					lstrTab = lstrTab & FormatNumber(ldblIcmB, 2) & "|" 'Valor Icms
					lstrTab = lstrTab & "-|" 'Isento
					lstrTab = lstrTab & "-" 'Diferito
				End If
				If ldblIsen <> 0 Then
					If VB.Left(.get_TextMatrix(llngRow, 14), 2) = "01" Then
						ifrmIcms.VS.Table = istrTamCol & lstrTab
						If ldblAlqB > 0 Or ldblAlqA > 0 Then
							lstrTab = ""
							lstrTab = lstrTab & "|" 'Classe
							lstrTab = lstrTab & "|" 'Grupo
							lstrTab = lstrTab & "|" 'Fase
							lstrTab = lstrTab & "|" 'Cfop
							lstrTab = lstrTab & "|" 'Qtde Consumidores
							lstrTab = lstrTab & "-|" 'Qtd kWh
							lstrTab = lstrTab & "ISE." & "|" 'Aliquota
							lstrTab = lstrTab & "-|" 'Base Icms
							lstrTab = lstrTab & "-|" 'Valor Icms
							lstrTab = lstrTab & IIf(ldblIsen <> 0, FormatNumber(ldblIsen, 2), "-") & "|" 'Isentos
							lstrTab = lstrTab & "-" 'Diferido
						Else
							lstrTab = lstrTab & "-|" 'Qtd kWh
							lstrTab = lstrTab & "ISE." & "|" 'aliquota
							lstrTab = lstrTab & "-|" 'base
							lstrTab = lstrTab & "-|" 'icms
							lstrTab = lstrTab & IIf(ldblIsen <> 0, FormatNumber(ldblIsen, 2), "-") & "|" 'Isentos
							lstrTab = lstrTab & "-" 'Diferido
						End If
					Else
						lstrTab = lstrTab & "-|" 'Qtd kWh
						lstrTab = lstrTab & IIf(VB.Left(.get_TextMatrix(llngRow, 14), 2) <> "01", "ISE.", "-") & "|" 'aliquota
						lstrTab = lstrTab & "-|" 'base
						lstrTab = lstrTab & "-|" 'icms
						lstrTab = lstrTab & IIf(ldblIsen <> 0, FormatNumber(ldblIsen, 2), "-") & "|" 'Isentos
					End If
				Else
					If ldblAlqB = 0 And ldblAlqA = 0 Then
						lstrTab = lstrTab & IIf(ldblDife <> 0, FormatNumber(ldblkWhD, 2), "-") & "|" 'Qtd kWh
						lstrTab = lstrTab & IIf(VB.Left(.get_TextMatrix(llngRow, 14), 2) <> "01", "ISE.", IIf(ldblDife <> 0, "DIF.", "-")) & "|" 'aliquota
						lstrTab = lstrTab & "-|" 'Base Icms
						lstrTab = lstrTab & "-|" 'Valor Icms
						lstrTab = lstrTab & "-|" 'Isentos
					End If
				End If
				If ldblDife <> 0 Then
					If VB.Left(.get_TextMatrix(llngRow, 14), 2) = "01" Then
						If ldblIsen = 0 Then
							If ldblAlqB > 0 Or ldblAlqA > 0 Then
								ifrmIcms.VS.Table = istrTamCol & lstrTab
								lstrTab = ""
								lstrTab = lstrTab & "|" 'Classe
								lstrTab = lstrTab & "|" 'Grupo
								lstrTab = lstrTab & "|" 'Fase
								lstrTab = lstrTab & "|" 'Cfop
								lstrTab = lstrTab & "|" 'Qtde Consumidores
								lstrTab = lstrTab & IIf(ldblDife <> 0, FormatNumber(ldblkWhD, 2), "-") & "|" 'Qtd kWh
								lstrTab = lstrTab & "DIF." & "|" 'Aliquota
								lstrTab = lstrTab & "-|" 'Base Icms
								lstrTab = lstrTab & "-|" 'Valor Icms
								lstrTab = lstrTab & "-|" 'Isentos
								lstrTab = lstrTab & IIf(ldblDife <> 0, FormatNumber(ldblDife, 2), "-") 'Isentos
							Else
								'              lstrTab = lstrTab & "DIFERIDO" & "|" 'aliquota
								'              lstrTab = lstrTab & "-|" 'base
								'              lstrTab = lstrTab & "-|" 'icms
								'              lstrTab = lstrTab & "-|"  'Isentos
								lstrTab = lstrTab & IIf(ldblDife <> 0, FormatNumber(ldblDife, 2), "-") 'Diferido
							End If
						Else
							lstrTab = lstrTab & IIf(ldblDife <> 0, FormatNumber(ldblDife, 2), "-") 'Diferido
						End If
					Else
						'          lstrTab = lstrTab & IIf(Left(.TextMatrix(llngRow, 14), 2) <> "01", "DIFERIDO", "-") & "|" 'aliquota
						'          lstrTab = lstrTab & "-|" 'base
						'          lstrTab = lstrTab & "-|" 'icms
						'          lstrTab = lstrTab & "-|" 'Isentos
						lstrTab = lstrTab & IIf(ldblDife <> 0, FormatNumber(ldblDife, 2), "-") 'Diferido
					End If
				Else
					If ldblAlqB = 0 And ldblAlqA = 0 Then
						lstrTab = lstrTab & "-" 'Diferido
					End If
				End If
				If ldblAlqS > 0 Then
					ifrmIcms.VS.Table = istrTamCol & lstrTab
					lstrTab = ""
					lstrTab = lstrTab & "SUBSTITUIÇÃO TRIBUTÁRIA|" 'Classe
					lstrTab = lstrTab & "|" 'Grupo
					lstrTab = lstrTab & "|" 'Fase
					lstrTab = lstrTab & "|" 'Cfop
					lstrTab = lstrTab & "|" 'Qtde Consumidores
					lstrTab = lstrTab & FormatNumber(ldblkWhS, 2) & "|" 'Qtd kWh
					lstrTab = lstrTab & FormatNumber(ldblAlqS, 2) & "|" 'Aliquota
					lstrTab = lstrTab & FormatNumber(ldblBasS, 2) & "|" 'Base Icms
					lstrTab = lstrTab & FormatNumber(ldblIcms, 2) & "|" 'Valor Icms
					lstrTab = lstrTab & "-|" 'Isentos
					lstrTab = lstrTab & "-" 'Diferido
					ldblAlqSTotal = ldblAlqSTotal + ldblAlqS
					ldblBasSTotal = ldblBasSTotal + ldblBasS
					ldblIcmsTotal = ldblIcmsTotal + ldblIcms
				End If
				ifrmIcms.VS.Table = istrTamCol & lstrTab
				If VB.Left(.get_TextMatrix(llngRow, 14), 2) = "01" Or VB.Left(.get_TextMatrix(llngRow, 14), 2) = "04" Then
					llngTcQtd = llngTcQtd + ldblQtde
					llngTgQtd = llngTgQtd + ldblQtde
					llngTtQtd = llngTtQtd + ldblQtde
				End If
				ldblTcIcm = ldblTcIcm + (ldblIcmA + ldblIcmB)
				ldblTcBas = ldblTcBas + (ldblBasA + ldblBasB)
				ldblTckWh = ldblTckWh + ldblKWhA + ldblKWhB + ldblkWhD
				ldblTcIse = ldblTcIse + ldblIsen
				ldblTcDif = ldblTcDif + ldblDife
				'
				ldblTgIcm = ldblTgIcm + (ldblIcmA + ldblIcmB)
				ldblTgBas = ldblTgBas + (ldblBasA + ldblBasB)
				'ldblTgkWh = ldblTgkWh + ldblkWh
				ldblTgIse = ldblTgIse + ldblIsen
				ldblTgDif = ldblTgDif + ldblDife
				'
				ldblTtIcm = ldblTtIcm + (ldblIcmA + ldblIcmB)
				ldblTtBas = ldblTtBas + (ldblBasA + ldblBasB)
				'ldblTtkWh = ldblTtkWh + ldblkWh
				ldblTtIse = ldblTtIse + ldblIsen
				ldblTtDif = ldblTtDif + ldblDife
			Next llngRow
		End With
		Call ImpTotSubTot(llngTcQtd, ldblTcBas, ldblTcIcm, ldblTcIse, ldblTcDif, ldblTckWh, "T")
		Call ImpTotSubTot(llngTgQtd, ldblTgBas, ldblTgIcm, ldblTgIse, ldblTgDif, ldblTgkWh, "S")
		Call impTotSubTributaria(ldblAlqSTotal, ldblBasSTotal, ldblIcmsTotal)
		Call ImpTotSubTot(llngTtQtd, ldblTtBas, ldblTtIcm, ldblTtIse, ldblTtDif, ldblTtkWh, "G")
		With ifrmIcms
			Call .VS.EndDoc()
			Call .Show()
		End With
	End Sub
	
	Private Sub impTotSubTributaria(ByRef ldblAlqSTot As Double, ByRef ldblBasSTot As Double, ByRef ldblIcmsTot As Double)
		Dim lstrTab As String
		Dim lstrCor As String
		
		lstrTab = ""
		lstrTab = lstrTab & "TOTAL SUBSTITUIÇÃO TRIBUTÁRIA|" 'Classe
		lstrTab = lstrTab & "|" 'Grupo
		lstrTab = lstrTab & "|" 'Fase
		lstrTab = lstrTab & "|" 'Cfop
		lstrTab = lstrTab & "|" 'Qtde Consumidores
		lstrTab = lstrTab & "-|" 'Qtde kWh
		lstrTab = lstrTab & FormatNumber(ldblAlqSTot, 2) & "|" 'Aliquota
		lstrTab = lstrTab & FormatNumber(ldblBasSTot, 2) & "|" 'Base Icms
		lstrTab = lstrTab & FormatNumber(ldblIcmsTot, 2) & "|" 'Valor Icms
		lstrTab = lstrTab & "-|" 'Isentos
		lstrTab = lstrTab & "-" 'Diferido
		With ifrmIcms
			Call LineFeed(.VS, 1)
			.VS.Font = VB6.FontChangeBold(.VS.Font, True)
			.VS.PenStyle = VSVIEW6.PenStyleSettings.psTransparent
			.VS.BrushColor = System.Drawing.ColorTranslator.FromOle(&HE0E0E0)
			'UPGRADE_ISSUE: VBControlExtender method ifrmIcms.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			Call .VS.DrawRectangle("8mm", .VS.CurrentY, "200mm", .VS.CurrentY + 165)
			.VS.BrushColor = System.Drawing.Color.White
			.VS.PenStyle = VSVIEW6.PenStyleSettings.psSolid
			.VS.Table = istrTamCol & lstrTab
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
		End With
	End Sub
	
	Private Sub ImpTotSubTot(ByRef alngQtd As Integer, ByRef adblBas As Double, ByRef adblIcm As Double, ByRef adblIse As Double, ByRef adblDif As Double, ByRef adblkWh As Double, ByVal astrTipo As String)
		Dim lstrTab As String
		Dim lstrCor As String
		lstrTab = ""
		lstrTab = lstrTab & IIf(astrTipo = "T", "TOTAL", IIf(astrTipo = "S", "SUB-TOTAL", "TOTAL GERAL")) & "|" 'Classe
		lstrTab = lstrTab & "|" 'Grupo
		lstrTab = lstrTab & "|" 'Fase
		lstrTab = lstrTab & "|" 'Cfop
		lstrTab = lstrTab & FormatNumber(alngQtd, 0) & "|" 'Qtde
		lstrTab = lstrTab & FormatNumber(adblkWh, 2) & "|" 'Qtd kWh
		lstrTab = lstrTab & "|" 'Aliquota
		lstrTab = lstrTab & FormatNumber(adblBas, 2) & "|" 'Base Icms
		lstrTab = lstrTab & FormatNumber(adblIcm, 2) & "|" 'Valor Icms
		lstrTab = lstrTab & FormatNumber(adblIse, 2) & "|" 'Isentos
		lstrTab = lstrTab & FormatNumber(adblDif, 2) & "|" 'Diferido
		With ifrmIcms
			If astrTipo = "S" Then
				Call LineFeed(.VS, 1)
			ElseIf astrTipo = "G" Then 
				Call LineFeed(.VS, 2)
			End If
			.VS.Font = VB6.FontChangeBold(.VS.Font, True)
			.VS.PenStyle = VSVIEW6.PenStyleSettings.psTransparent
			.VS.BrushColor = System.Drawing.ColorTranslator.FromOle(&HE0E0E0)
			'UPGRADE_ISSUE: VBControlExtender method ifrmIcms.VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			Call .VS.DrawRectangle("8mm", .VS.CurrentY, "200mm", .VS.CurrentY + 165)
			.VS.BrushColor = System.Drawing.Color.White
			.VS.PenStyle = VSVIEW6.PenStyleSettings.psSolid
			.VS.Table = istrTamCol & lstrTab
			If astrTipo = "G" Then
				'istrTamCol = "<45mm |^6mm|^6mm|^13mm|>15mm|^18mm|>20mm|>20mm|>20mm|>20mm;"
				Call LineFeed(.VS, 1)
				.VS.Table = "<103mm|>20mm;TOTAL DO FATURAMENTO.....|" & FormatNumber(adblBas + adblIse + adblDif, 2)
			End If
			.VS.Font = VB6.FontChangeBold(.VS.Font, False)
		End With
		If astrTipo <> "G" Then
			alngQtd = 0
			adblBas = 0
			adblIcm = 0
			adblIse = 0
			adblDif = 0
			adblkWh = 0
		End If
	End Sub
	
	Private Sub LimpaDesnecessarios()
		Dim lstrGru As String
		Dim lstrCla As String
		Dim llngQtd As Integer
		Dim ldblIse As Double
		Dim ldblDif As Double
		Dim ldblBas As Double
		Dim ldblIcm As Double
		Dim llngRow As Integer
		Dim lstrOld As String
		
		With Me.mfgDados
			lstrCla = .get_TextMatrix(.Rows - 1, 1)
			For llngRow = .Rows - 1 To 1 Step -1
				lstrGru = .get_TextMatrix(llngRow, 0)
				If VB.Left(.get_TextMatrix(llngRow, 14), 2) <> "01" Then
					llngQtd = llngQtd + CInt(IIf(.get_TextMatrix(llngRow, 5) = "", 0, .get_TextMatrix(llngRow, 5)))
					ldblIse = ldblIse + CDbl(IIf(.get_TextMatrix(llngRow, 6) = "", 0, .get_TextMatrix(llngRow, 6)))
					ldblDif = ldblDif + CDbl(IIf(.get_TextMatrix(llngRow, 7) = "", 0, .get_TextMatrix(llngRow, 7)))
					ldblBas = ldblBas + CDbl(IIf(.get_TextMatrix(llngRow, 9) = "", 0, .get_TextMatrix(llngRow, 9)))
					ldblIcm = ldblIcm + CDbl(IIf(.get_TextMatrix(llngRow, 10) = "", 0, .get_TextMatrix(llngRow, 10)))
					ldblBas = ldblBas + CDbl(IIf(.get_TextMatrix(llngRow, 12) = "", 0, .get_TextMatrix(llngRow, 12)))
					ldblIcm = ldblIcm + CDbl(IIf(.get_TextMatrix(llngRow, 13) = "", 0, .get_TextMatrix(llngRow, 13)))
					lstrOld = .get_TextMatrix(llngRow - 1, 1)
					If lstrCla <> lstrOld Then
						lstrCla = .get_TextMatrix(llngRow - 1, 1)
						If (llngQtd + ldblIse + ldblDif + ldblBas + ldblIcm) = 0 Then
							.RemoveItem((llngRow + 2))
							.RemoveItem((llngRow + 1))
							.RemoveItem((llngRow))
						End If
						llngQtd = 0
						ldblIse = 0
						ldblDif = 0
						ldblBas = 0
						ldblIcm = 0
					End If
				End If
			Next llngRow
		End With
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.cmdArquivo.Enabled = ablnModo
		Me.cmdExporta.Enabled = ablnModo
		Me.cmdImprimir.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.dtpPeriodo.Enabled = ablnModo
	End Sub
End Class