Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelPisCofins
	Inherits System.Windows.Forms.Form
	Private ifrmPisCofins As FrmRelatorio
	Private istrTamCol As String
	Private istrCabCol As String
	Private istrGruCla As String
	Private iintPagina As Short
	
	Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Dim lintAux As Short
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call LigDes(False)
		Call MontaPisCofins()
		iintPagina = 1
		Call InicializaImpressao()
		For lintAux = 1 To 6
			iintPagina = lintAux
			If lintAux > 1 Then Call ifrmPisCofins.VS.NewPage()
			Call ImpPisCofins(lintAux)
		Next lintAux
		Call FinalizaImpressao()
		Call LigDes(True)
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	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 & "\PIS_COFINS_" & VB6.Format(BuscaHoraBD, "yyyymmddHHMMSS") & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação do Pis e Cofins 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 frmRelPisCofins_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 Me.Close()
		End Select
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub frmRelPisCofins_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		Me.dtpPeriodo.value = CDate("01/" & VB6.Format(BuscaDataBD, "mm/yyyy"))
		ifrmPisCofins = New FrmRelatorio
		Call MontaGrade()
		'             C     G    F    CF   Q     A.PIS A.COF BASF  V.PIS V.COF
		istrTamCol = "<45mm|^6mm|^6mm|^12mm|>14mm|>12mm|>12mm|>20mm|>20mm|>20mm;"
		istrCabCol = "CLASSE|G.|F.|Cfop |N.CONS.|A.PIS|A.COF|BASE|PIS|COFINS"
	End Sub
	
	Private Sub MontaPisCofins()
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim lstrCla As String
		Dim lstrGru As String
		Dim lstrFas As String
		Dim lstrTar As String
		Dim lstrTarA As String
		Dim lstrItm As String
		Dim lstrFlg As String
		Dim ldteFlgSt As Date
		Dim lintMpr As Short
		'
		Dim lstrDesClas As String
		Dim lstrGrTar As String
		Dim lstrOrdClas As String
		Dim lstrOrdGru As String
		Dim lstrOrdFas As String
		Dim lstrCfop As String
		'
		Dim ldblAPis As Double
		Dim ldblACof As Double
		Dim ldblBIse As Double
		Dim ldblBaseA As Double
		Dim ldblVPisA As Double
		Dim ldblVCofA As Double
		Dim ldblVIcmA As Double
		Dim ldblVIcmB As Double
		'
		Dim ldblBaseB As Double
		Dim ldblVPisB As Double
		Dim ldblVCofB As Double
		'
		Dim ldblAIc1 As Double
		Dim ldblAIc2 As Double
		Dim ldblBaseE As Double
		Dim ldblVPisE As Double
		Dim ldblVCofE As Double
		Dim ldblAPisE As Double
		Dim ldblACofE As Double
		'
		Dim ldblBaseF As Double
		Dim ldblVPisF As Double
		Dim ldblVCofF As Double
		'
		Dim ldblKWhFatp As Double
		Dim ldblKWhFatf As Double
		Dim ldblKWhBanp As Double
		Dim ldblKWhBanf As Double
		Dim ldblKWFatp As Double
		Dim ldblKWFatf As Double
		Dim ldblKWUltp As Double
		Dim ldblKWUltf As Double
		Dim ldblUferp As Double
		Dim ldblUferf As Double
		Dim ldblDmcrp As Double
		Dim ldblDmcrf As Double
		
		Dim ldblDemPrv As Double
		Dim ldblDemCob As Double
		Dim ldblDemDev As Double
		Dim ldblEnePrv As Double
		Dim ldblEneCob As Double
		Dim ldblEneDev As Double
		'
		Dim ldblCoefSemIcms As Double
		Dim ldblCoefComIcm1 As Double
		Dim ldblCoefComIcm2 As Double
		Dim lstrCod As String
		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
		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`.`grupo`, " & 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.numnf = 691 "
		lstrSql = lstrSql & " ORDER BY `nf`.`ta` ASC, `nf`.`codclas` ASC, `nf`.`fase` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount)
					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"'
						lstrGru = IIf(IsDbNull(.Fields("grupo").value), "B", .Fields("grupo").value)
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						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
						lstrGrTar = lstrTarA
						Call SetaDados(lstrCla, lstrGru, lstrFas, lstrGrTar, lstrDesClas, lstrOrdClas, lstrOrdGru, lstrOrdFas, lstrCfop)
						'Pegar os valores tributados, base de pis e cofins, valores de pis e cofins, aliqutoas de pis e cofins
						ldblBaseA = BuscaValorItem(lstrItm, "0302020033")
						ldblVPisA = BuscaValorItem(lstrItm, "0302020029")
						ldblVCofA = BuscaValorItem(lstrItm, "0302020030")
						ldblVIcmA = BuscaValorItem(lstrItm, "0302030009")
						ldblVIcmB = BuscaValorItem(lstrItm, "0302030010")
						'
						ldblAPisE = IIf(IsNumeric(Me.txtPis.Text), Me.txtPis.Text, 0)
						ldblACofE = IIf(IsNumeric(Me.txtCofins.Text), Me.txtCofins.Text, 0)
						'
						ldblBaseE = (ldblBaseA - ldblVPisA - ldblVCofA) 'Tira pis e cofins
						ldblBaseE = Arredondar(ldblBaseE / ((100 - (ldblAPisE + ldblACofE)) / 100), 2)
						ldblVPisE = Arredondar(ldblBaseE * (ldblAPisE / 100), 2)
						ldblVCofE = Arredondar(ldblBaseE * (ldblACofE / 100), 2)
						'
						ldblBaseF = (ldblBaseA - ldblVPisA - ldblVCofA - ldblVIcmA - ldblVIcmB) 'Tira pis e cofins e icms
						ldblBaseF = Arredondar(ldblBaseF / ((100 - (ldblAPisE + ldblACofE)) / 100), 2)
						ldblVPisF = Arredondar(ldblBaseF * (ldblAPisE / 100), 2)
						ldblVCofF = Arredondar(ldblBaseF * (ldblACofE / 100), 2)
						'
						If lstrGru = "B" Then
							ldblAPis = BuscaValorItem(lstrItm, "0302990303")
							ldblACof = BuscaValorItem(lstrItm, "0302990304")
							ldblAIc1 = BuscaValorItem(lstrItm, "0302030002")
							ldblAIc2 = BuscaValorItem(lstrItm, "0302030013")
						Else
							ldblAPis = BuscaValorItem(lstrItm, "40PIS00001")
							ldblACof = BuscaValorItem(lstrItm, "40COF00001")
							ldblAIc1 = BuscaValorItem(lstrItm, "0302030002")
						End If
						ldblBaseB = (ldblBaseA - ldblVPisA - ldblVCofA - ldblVIcmA - ldblVIcmB) 'Tira icms pis e cofins e recalcula
						ldblBaseB = Arredondar(ldblBaseB / ((100 - (ldblAPis + ldblACof)) / 100), 2)
						ldblVPisB = Arredondar(ldblBaseB * (ldblAPis / 100), 2)
						ldblVCofB = Arredondar(ldblBaseB * (ldblACof / 100), 2)
						
						'Recalcular os valores sem icms AFFFF
						ldblCoefSemIcms = Truncate(1 - ((ldblAPis / 100) + (ldblACof / 100)), 11)
						If ldblAIc1 > 0 Then
							ldblCoefComIcm1 = Truncate(1 - (((ldblAPis / 100) + (ldblACof / 100)) / (1 / (ldblAIc1 / 100))), 11)
						Else
							ldblCoefComIcm1 = 1
						End If
						If ldblAIc2 > 0 Then
							ldblCoefComIcm2 = Truncate(1 - (((ldblAPis / 100) + (ldblACof / 100)) / (1 / (ldblAIc2 / 100))), 11)
						Else
							ldblCoefComIcm2 = 1
						End If
						'          If lstrGru = "B" Then
						'            ldblKWhFatf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "030202009")), 2) * lintMpr)
						'            ldblKWhBanf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "030212009")), 2) * lintMpr)
						'            If ldblKWhBanf = 0 Then
						'              ldblKWhBanf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "030222009")), 2) * lintMpr)
						'            End If
						'            If ldblKWhBanf = 0 Then
						'              ldblKWhBanf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "030232009")), 2) * lintMpr)
						'            End If
						'            ldblBaseB = Arredondar(ldblKWhFatf / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWhBanf / ldblCoefSemIcms, 2)
						'            ldblVPisB = Arredondar(ldblBaseB * (ldblAPis / 100), 2)
						'            ldblVCofB = Arredondar(ldblBaseB * (ldblACof / 100), 2)
						'          Else
						'            ldblKWhFatp = 0
						'            ldblKWhFatf = 0
						'            ldblKWhBanp = 0
						'            ldblKWhBanf = 0
						'            ldblKWFatp = 0
						'            ldblKWFatf = 0
						'            ldblKWUltp = 0
						'            ldblKWUltf = 0
						'            ldblUferp = 0
						'            ldblUferf = 0
						'            ldblDmcrp = 0
						'            ldblDmcrf = 0
						'            ldblDemPrv = 0
						'            ldblDemCob = 0
						'            ldblDemDev = 0
						'            ldblEnePrv = 0
						'            ldblEneCob = 0
						'            ldblEneDev = 0
						'            If Left(lstrTarA, 2) = "A4" Or Left(lstrTarA, 2) = "AS" Then
						'              ldblKWhFatf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020009")), 2) * lintMpr)
						'              ldblKWhBanf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "030212009")), 2) * lintMpr)
						'              ldblKWFatf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020015")), 2) * lintMpr)
						'              ldblKWUltf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020018")), 2) * lintMpr)
						'              ldblUferf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020012")), 2) * lintMpr)
						'              ldblDmcrf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020031")), 2) * lintMpr)
						'            ElseIf Left(lstrTarA, 2) = "AA" Or Left(lstrTarA, 2) = "AV" Or Left(lstrTarA, 2) = "AL" Or Left(lstrTarA, 2) = "AP" Or Left(lstrTarA, 2) = "AX" Then
						'              ldblKWhFatp = ldblKWhFatp + (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020010")) + CDbl(BuscaValorItem(lstrItm, "0302020060")), 2) * lintMpr)
						'              ldblKWhFatf = ldblKWhFatf + (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020011")) + CDbl(BuscaValorItem(lstrItm, "0302020061")), 2) * lintMpr)
						'              ldblKWhBanp = (Arredondar(CDbl(BuscaValorItem(lstrItm, "030212010")), 2) * lintMpr)
						'              ldblKWhBanf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "030212011")), 2) * lintMpr)
						'              ldblKWFatp = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020021")), 2) * lintMpr)
						'              ldblKWFatf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020023")), 2) * lintMpr)
						'              ldblKWUltp = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020022")), 2) * lintMpr)
						'              ldblKWUltf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020026")), 2) * lintMpr)
						'              ldblUferp = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020013")), 2) * lintMpr)
						'              ldblUferf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020014")), 2) * lintMpr)
						'              ldblDmcrp = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020027")), 2) * lintMpr)
						'              ldblDmcrf = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020028")), 2) * lintMpr)
						'
						'              If Left(lstrTarA, 2) = "AL" Then
						'                ldblDemPrv = (Arredondar((CDbl(BuscaValorItem(lstrItm, "0302020051")) * -1), 2) * lintMpr)
						'                ldblDemDev = (Arredondar((CDbl(BuscaValorItem(lstrItm, "0302020053")) * -1), 2) * lintMpr)
						'                ldblDemCob = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020054")), 2) * lintMpr)
						'                ldblEnePrv = (Arredondar((CDbl(BuscaValorItem(lstrItm, "0302020055")) * -1), 2) * lintMpr)
						'                ldblEneDev = (Arredondar((CDbl(BuscaValorItem(lstrItm, "0302020057")) * -1), 2) * lintMpr)
						'                ldblEneCob = (Arredondar(CDbl(BuscaValorItem(lstrItm, "0302020058")), 2) * lintMpr)
						'              End If
						'            End If
						'            ldblBaseB = Arredondar(ldblKWhFatp / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWhFatf / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWhBanp / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWhBanf / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWFatp / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWFatf / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWUltp / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblKWUltf / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblUferp / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblUferf / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblDmcrp / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblDmcrf / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblDemPrv / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblDemDev / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblDemCob / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblEnePrv / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblEneDev / ldblCoefSemIcms, 2)
						'            ldblBaseB = ldblBaseB + Arredondar(ldblEneCob / ldblCoefSemIcms, 2)
						'            '
						'            ldblVPisB = Arredondar(ldblBaseB * (ldblAPis / 100), 2)
						'            ldblVCofB = Arredondar(ldblBaseB * (ldblACof / 100), 2)
						'          End If
						If ((ldblBaseA - ldblBaseB) + (ldblVPisA - ldblVPisB) + (ldblVCofA - ldblVCofB)) < 0 Then
							ldblBaseB = ldblBaseA
							ldblVPisB = ldblVPisA
							ldblVCofB = ldblVCofA
						End If
						lstrCod = "01." & lstrOrdClas & "." & lstrOrdGru & "." & lstrOrdFas
						Call AdicionaGrade("CLASSE", lstrDesClas, lstrGrTar, lstrFas, lstrCfop, ldblAPis, ldblACof, ldblBIse, ldblBaseA, ldblVPisA, ldblVCofA, ldblBaseB, ldblVPisB, ldblVCofB, lstrCod, (ldblBaseA - ldblBaseB), (ldblVPisA - ldblVPisB), (ldblVCofA - ldblVCofB), ldblBaseE, ldblVPisE, ldblVCofE, ldblBaseF, ldblVPisF, ldblVCofF, (ldblBaseE - ldblBaseF), (ldblVPisE - ldblVPisF), (ldblVCofE - ldblVCofF))
						
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
				End If
				Call .Close()
			End If
		End With
		With Me.mfgDados
			'    .Col = 17
			'    .Sort = flexSortGenericAscending
			.Redraw = True
		End With
		
	End Sub
	
	Private Sub SetaDados(ByVal astrClasse As String, ByVal astrGrupo As String, ByVal astrFase As String, ByRef pstrGrTar As String, ByRef pstrDesClas As String, ByRef pstrOrdClas As String, ByRef pstrOrdGru As String, ByRef pstrOrdFas As String, ByRef pstrCfop As String)
		If VB.Left(astrGrupo, 1) = "B" Then
			If CDbl(VB.Left(astrClasse, 1)) < 4 Then
				pstrDesClas = "INDUSTRIAL"
				pstrGrTar = "B3"
				pstrOrdClas = "11"
				pstrOrdGru = "1"
				pstrCfop = "5.252"
			ElseIf CDbl(VB.Left(astrClasse, 1)) = 4 Then 
				pstrDesClas = "COMERCIAL"
				pstrGrTar = "B3"
				pstrOrdClas = "14"
				pstrOrdGru = "1"
				pstrCfop = "5.253"
				If CDbl(VB.Left(astrClasse, 2)) = 42 Then
					pstrDesClas = "SERVIÇO TRANSPORTE"
					pstrGrTar = "B3"
					pstrOrdClas = "16"
					pstrOrdGru = "1"
					pstrCfop = "5.254"
				ElseIf CDbl(VB.Left(astrClasse, 2)) = 43 Then 
					pstrDesClas = "SERVIÇO COMUNICAÇÃO"
					pstrGrTar = "B3"
					pstrOrdClas = "15"
					pstrOrdGru = "1"
					pstrCfop = "5.255"
				End If
			ElseIf CDbl(VB.Left(astrClasse, 1)) = 5 Then 
				pstrDesClas = "RURAL"
				pstrGrTar = "B2"
				pstrOrdClas = "22"
				pstrOrdGru = "1"
				pstrCfop = "5.256"
			ElseIf CDbl(VB.Left(astrClasse, 2)) = 61 Then 
				pstrDesClas = "PODER PÚBLICO MUNICIPAL"
				pstrGrTar = "B3"
				pstrOrdClas = "23"
				pstrOrdGru = "1"
				pstrCfop = "5.258"
			ElseIf CDbl(VB.Left(astrClasse, 2)) = 62 Then 
				pstrDesClas = "PODER PÚBLICO ESTADUAL"
				pstrGrTar = "B3"
				pstrOrdClas = "24"
				pstrOrdGru = "1"
				pstrCfop = "5.258"
			ElseIf CDbl(VB.Left(astrClasse, 2)) = 63 Then 
				pstrDesClas = "PODER PÚBLICO FEDERAL"
				pstrGrTar = "B3"
				pstrOrdClas = "25"
				pstrOrdGru = "1"
				pstrCfop = "5.258"
			ElseIf CDbl(VB.Left(astrClasse, 2)) = 64 Then 
				pstrDesClas = "ILUMINAÇÃO PÚBLICA"
				pstrGrTar = "B4"
				pstrOrdClas = "17"
				pstrOrdGru = "1"
				pstrCfop = "5.258"
			ElseIf CDbl(VB.Left(astrClasse, 1)) = 7 Then 
				pstrDesClas = "SERVIÇO PÚBLICO"
				pstrGrTar = "B3"
				pstrOrdClas = "18"
				pstrOrdGru = "1"
				pstrCfop = "5.258"
			ElseIf CDbl(VB.Left(astrClasse, 1)) = 8 Then 
				pstrDesClas = "CONSUMO PRÓPRIO"
				pstrGrTar = "B3"
				pstrOrdClas = "19"
				pstrOrdGru = "1"
				pstrCfop = "5.253"
			ElseIf CDbl(VB.Left(astrClasse, 1)) = 9 Then 
				If VB.Right(astrClasse, 2) = "04" Or VB.Right(astrClasse, 2) = "05" Or VB.Right(astrClasse, 2) = "06" Then
					pstrDesClas = "RESIDENCIAL BX.RENDA"
					pstrOrdClas = "21"
				Else
					pstrDesClas = "RESIDENCIAL NORMAL"
					pstrOrdClas = "20"
				End If
				pstrGrTar = "B1"
				pstrOrdGru = "1"
				pstrCfop = "5.258"
			End If
		Else
			If VB.Left(pstrGrTar, 2) = "AS" Or VB.Left(pstrGrTar, 2) = "AX" Then
				pstrDesClas = "DEMANDA CONTRATADA (COOPER)"
				pstrOrdClas = "13"
			Else
				pstrDesClas = "DEMANDA CONTRATADA"
				pstrOrdClas = "12"
			End If
			pstrGrTar = "A4"
			pstrCfop = "5.257"
			pstrOrdGru = "1"
		End If
		If astrFase = "M" Then
			pstrOrdFas = "1"
		ElseIf astrFase = "B" Then 
			pstrOrdFas = "2"
		ElseIf astrFase = "T" Then 
			pstrOrdFas = "3"
		End If
	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 adblAPis As Double, ByVal adblACof As Double, ByVal adblIse As Double, ByVal adblBaseA As Double, ByVal adblVPisA As Double, ByVal adblVCofA As Double, ByVal adblBaseB As Double, ByVal adblVPisB As Double, ByVal adblVCofB As Double, ByVal astrCod As String, ByVal adblBaseD As Double, ByVal adblVPisD As Double, ByVal adblVCofD As Double, ByVal adblBaseE As Double, ByVal adblVPisE As Double, ByVal adblVCofE As Double, ByVal adblBaseF As Double, ByVal adblVPisF As Double, ByVal adblVCofF As Double, ByVal adblBaseN As Double, ByVal adblVPisN As Double, ByVal adblVCofN 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, 27) = 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, 27, astrCod)
			End If
			.set_TextMatrix(llngPos, 4, astrCfp) 'Cfop
			If (adblIse + adblBaseA + adblVPisA + adblVCofA + adblBaseB + adblVPisB + adblVCofB) <> 0 Then
				If (adblIse + adblBaseA + adblVPisA + adblVCofA + adblBaseB + adblVPisB + adblVCofB) > 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
				If .get_TextMatrix(llngPos, 7) = "" Or .get_TextMatrix(llngPos, 7) = "0,00" Then
					.set_TextMatrix(llngPos, 7, FormatNumber(adblAPis, 5)) 'Aliquota Pis
				End If
				If .get_TextMatrix(llngPos, 8) = "" Or .get_TextMatrix(llngPos, 8) = "0,00" Then
					.set_TextMatrix(llngPos, 8, FormatNumber(adblACof, 5)) 'Aliquota Cofins
				End If
				'
				.set_TextMatrix(llngPos, 9, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 9) = "", 0, .get_TextMatrix(llngPos, 9))) + adblBaseA, 2)) 'Base Pis e Cofins
				.set_TextMatrix(llngPos, 10, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 10) = "", 0, .get_TextMatrix(llngPos, 10))) + adblVPisA, 2)) 'Valor Pis
				.set_TextMatrix(llngPos, 11, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 11) = "", 0, .get_TextMatrix(llngPos, 11))) + adblVCofA, 2)) 'Valor Cofins
				'
				.set_TextMatrix(llngPos, 12, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 12) = "", 0, .get_TextMatrix(llngPos, 12))) + adblBaseB, 2)) 'Base Pis e Cofins sem Icms
				.set_TextMatrix(llngPos, 13, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 13) = "", 0, .get_TextMatrix(llngPos, 13))) + adblVPisB, 2)) 'Valor Pis sem Icms
				.set_TextMatrix(llngPos, 14, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 14) = "", 0, .get_TextMatrix(llngPos, 14))) + adblVCofB, 2)) 'Valor Cofins sem Icms
				'
				.set_TextMatrix(llngPos, 15, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 15) = "", 0, .get_TextMatrix(llngPos, 15))) + adblBaseD, 2)) 'kWh
				.set_TextMatrix(llngPos, 16, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 16) = "", 0, .get_TextMatrix(llngPos, 16))) + adblVPisD, 2)) 'kWh
				.set_TextMatrix(llngPos, 17, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 17) = "", 0, .get_TextMatrix(llngPos, 17))) + adblVCofD, 2)) 'kWh
				
				'Pis e Cofins aliquota Cheia
				.set_TextMatrix(llngPos, 18, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 18) = "", 0, .get_TextMatrix(llngPos, 18))) + adblBaseE, 2)) 'kWh
				.set_TextMatrix(llngPos, 19, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 19) = "", 0, .get_TextMatrix(llngPos, 19))) + adblVPisE, 2)) 'kWh
				.set_TextMatrix(llngPos, 20, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 20) = "", 0, .get_TextMatrix(llngPos, 20))) + adblVCofE, 2)) 'kWh
				
				.set_TextMatrix(llngPos, 21, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 21) = "", 0, .get_TextMatrix(llngPos, 21))) + adblBaseF, 2)) 'kWh
				.set_TextMatrix(llngPos, 22, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 22) = "", 0, .get_TextMatrix(llngPos, 22))) + adblVPisF, 2)) 'kWh
				.set_TextMatrix(llngPos, 23, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 23) = "", 0, .get_TextMatrix(llngPos, 23))) + adblVCofF, 2)) 'kWh
				
				.set_TextMatrix(llngPos, 24, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 24) = "", 0, .get_TextMatrix(llngPos, 24))) + adblBaseN, 2)) 'kWh
				.set_TextMatrix(llngPos, 25, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 25) = "", 0, .get_TextMatrix(llngPos, 25))) + adblVPisN, 2)) 'kWh
				.set_TextMatrix(llngPos, 26, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 26) = "", 0, .get_TextMatrix(llngPos, 26))) + adblVCofN, 2)) 'kWh
				
			End If
		End With
	End Sub
	
	Private Sub MontaGrade()
		With Me.mfgDados
			Call .Clear()
			.Rows = 1
			.Cols = 28
			.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, 1200)
			.set_ColWidth(15, 1200)
			.set_ColWidth(16, 1200)
			.set_ColWidth(17, 1200)
			.set_ColWidth(18, 550)
			.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, "Aliq Pis")
			.set_TextMatrix(0, 8, "Aliq Cofins")
			.set_TextMatrix(0, 9, "Base A")
			.set_TextMatrix(0, 10, "Pis A")
			.set_TextMatrix(0, 11, "Cofins A")
			.set_TextMatrix(0, 12, "Base B")
			.set_TextMatrix(0, 13, "Pis B")
			.set_TextMatrix(0, 14, "Cofins B")
			.set_TextMatrix(0, 15, "Base D")
			.set_TextMatrix(0, 16, "Pis D")
			.set_TextMatrix(0, 17, "Cofins D")
			
			.set_TextMatrix(0, 18, "Base E")
			.set_TextMatrix(0, 19, "Pis E")
			.set_TextMatrix(0, 20, "Cofins E")
			
			.set_TextMatrix(0, 21, "Base F")
			.set_TextMatrix(0, 22, "Pis F")
			.set_TextMatrix(0, 23, "Cofins F")
			
			.set_TextMatrix(0, 24, "Base N")
			.set_TextMatrix(0, 25, "Pis N")
			.set_TextMatrix(0, 26, "Cofins N")
			
			.set_TextMatrix(0, 27, "Ordem")
		End With
		Call MontaGrupos("CLASSE", "01", "")
	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, 0, astrSeq & ".11.1.1", 0, 0, 0, 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, 0, astrSeq & ".11.1.2", 0, 0, 0, 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, 0, astrSeq & ".11.1.3", 0, 0, 0, 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, 0, astrSeq & ".12.1.1", 0, 0, 0, 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, 0, astrSeq & ".12.1.2", 0, 0, 0, 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, 0, astrSeq & ".12.1.3", 0, 0, 0, 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, 0, astrSeq & ".13.1.1", 0, 0, 0, 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, 0, astrSeq & ".13.1.2", 0, 0, 0, 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, 0, astrSeq & ".13.1.3", 0, 0, 0, 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, 0, astrSeq & ".14.1.1", 0, 0, 0, 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, 0, astrSeq & ".14.1.2", 0, 0, 0, 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, 0, astrSeq & ".14.1.3", 0, 0, 0, 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, 0, astrSeq & ".15.1.1", 0, 0, 0, 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, 0, astrSeq & ".15.1.2", 0, 0, 0, 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, 0, astrSeq & ".15.1.3", 0, 0, 0, 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, 0, astrSeq & ".16.1.1", 0, 0, 0, 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, 0, astrSeq & ".16.1.2", 0, 0, 0, 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, 0, astrSeq & ".16.1.3", 0, 0, 0, 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, 0, astrSeq & ".17.1.1", 0, 0, 0, 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, 0, astrSeq & ".17.1.2", 0, 0, 0, 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, 0, astrSeq & ".17.1.3", 0, 0, 0, 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, 0, astrSeq & ".18.1.1", 0, 0, 0, 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, 0, astrSeq & ".18.1.2", 0, 0, 0, 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, 0, astrSeq & ".18.1.3", 0, 0, 0, 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, 0, astrSeq & ".19.1.1", 0, 0, 0, 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, 0, astrSeq & ".19.1.2", 0, 0, 0, 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, 0, astrSeq & ".19.1.3", 0, 0, 0, 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, 0, astrSeq & ".20.1.1", 0, 0, 0, 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, 0, astrSeq & ".20.1.2", 0, 0, 0, 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, 0, astrSeq & ".20.1.3", 0, 0, 0, 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, 0, astrSeq & ".21.1.1", 0, 0, 0, 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, 0, astrSeq & ".21.1.2", 0, 0, 0, 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, 0, astrSeq & ".21.1.3", 0, 0, 0, 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, 0, astrSeq & ".20.1.1", 0, 0, 0, 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, 0, astrSeq & ".20.1.2", 0, 0, 0, 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, 0, astrSeq & ".20.1.3", 0, 0, 0, 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, 0, astrSeq & ".22.1.1", 0, 0, 0, 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, 0, astrSeq & ".22.1.2", 0, 0, 0, 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, 0, astrSeq & ".22.1.3", 0, 0, 0, 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, 0, astrSeq & ".23.1.1", 0, 0, 0, 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, 0, astrSeq & ".23.1.2", 0, 0, 0, 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, 0, astrSeq & ".23.1.3", 0, 0, 0, 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, 0, astrSeq & ".24.1.1", 0, 0, 0, 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, 0, astrSeq & ".24.1.2", 0, 0, 0, 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, 0, astrSeq & ".24.1.3", 0, 0, 0, 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, 0, astrSeq & ".25.1.1", 0, 0, 0, 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, 0, astrSeq & ".25.1.2", 0, 0, 0, 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, 0, astrSeq & ".25.1.3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
	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
	
	Public Sub CabecaRel()
		With ifrmPisCofins
			Call Cabecalho_Relatorio(.VS, gstrEmpresa_Nome, "Relatório de Pis/Cofins - Faturamento do Mês " & VB6.Format(Me.dtpPeriodo._Value, "MM/YYYY"), "4", True, IIf(iintPagina = 1, "FATURADO", IIf(iintPagina = 2, "SEM ICMS", IIf(iintPagina = 3, "DIFERENÇA", IIf(iintPagina = 4, "PIS/COFINS ALIQUOTA CHEIA", IIf(iintPagina = 5, "PIS/COFINS ALIQUOTA CHEIA SEM ICMS", "PIS/COFINS ALIQUOTA CHEIA DIFERENÇA"))))))
			.VS.CurrentLine = 1
			.VS.Font = VB6.FontChangeSize(.VS.Font, 7)
			Call LineFeed(.VS, 2)
			.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 & "|" 'Aliquota Pis
			lstrTab = lstrTab & "|" 'Aliquota Cofins
			lstrTab = lstrTab & "|" 'Base
			lstrTab = lstrTab & "|" 'Pis
			lstrTab = lstrTab & "" 'Cofins
			With ifrmPisCofins
				.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 InicializaImpressao()
		With ifrmPisCofins
			.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
	End Sub
	
	Private Sub FinalizaImpressao()
		With ifrmPisCofins
			Call .VS.EndDoc()
			Call .Show()
		End With
	End Sub
	
	Private Sub ImpPisCofins(ByVal aintPagina As Short)
		Dim llngCol As Integer
		Dim llngRow As Integer
		Dim lstrTab As String
		Dim lstrCla As String
		'
		Dim ldblQtde As Double
		Dim ldblAPis As Double
		Dim ldblACof As Double
		Dim ldblBase As Double
		Dim ldblVPis As Double
		Dim ldblVCof As Double
		'
		Dim llngTcQtd As Integer
		Dim ldblTcBase As Double
		Dim ldblTcVPis As Double
		Dim ldblTcVCof As Double
		'
		Dim llngTgQtd As Integer
		Dim ldblTgBase As Double
		Dim ldblTgVPis As Double
		Dim ldblTgVCof As Double
		'
		Dim llngTtQtd As Integer
		Dim ldblTtBase As Double
		Dim ldblTtVPis As Double
		Dim ldblTtVCof As Double
		If aintPagina = 1 Then
			llngCol = 9
		ElseIf aintPagina = 2 Then 
			llngCol = 12
		ElseIf aintPagina = 3 Then 
			llngCol = 15
		ElseIf aintPagina = 4 Then 
			llngCol = 18
		ElseIf aintPagina = 5 Then 
			llngCol = 21
		ElseIf aintPagina = 6 Then 
			llngCol = 24
		End If
		'
		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)))
				If aintPagina < 4 Then
					ldblAPis = CDbl(IIf(.get_TextMatrix(llngRow, 7) = "", 0, .get_TextMatrix(llngRow, 7)))
					ldblACof = CDbl(IIf(.get_TextMatrix(llngRow, 8) = "", 0, .get_TextMatrix(llngRow, 8)))
				Else
					ldblAPis = IIf(IsNumeric(Me.txtPis.Text), Me.txtPis.Text, 0)
					ldblACof = IIf(IsNumeric(Me.txtCofins.Text), Me.txtCofins.Text, 0)
				End If
				ldblBase = CDbl(IIf(.get_TextMatrix(llngRow, llngCol) = "", 0, .get_TextMatrix(llngRow, llngCol)))
				ldblVPis = CDbl(IIf(.get_TextMatrix(llngRow, llngCol + 1) = "", 0, .get_TextMatrix(llngRow, llngCol + 1)))
				ldblVCof = CDbl(IIf(.get_TextMatrix(llngRow, llngCol + 2) = "", 0, .get_TextMatrix(llngRow, llngCol + 2)))
				
				If istrGruCla <> .get_TextMatrix(llngRow, 0) Then
					Call ImpTotSubTot(llngTcQtd, ldblTcBase, ldblTcVPis, ldblTcVCof, "T")
					Call ImpTotSubTot(llngTgQtd, ldblTgBase, ldblTgVPis, ldblTgVCof, "S")
					istrGruCla = .get_TextMatrix(llngRow, 0)
					Call ifrmPisCofins.VS.NewPage()
					Call CabecTipo()
					lstrCla = ""
				End If
				lstrTab = ""
				If lstrCla <> .get_TextMatrix(llngRow, 1) Then
					If lstrCla <> "" Then
						If Mid(.get_TextMatrix(llngRow, 14), 4, 2) <> "11" Then
							Call ImpTotSubTot(llngTcQtd, ldblTcBase, ldblTcVPis, ldblTcVCof, "T")
						End If
					End If
					Call LineFeed((ifrmPisCofins.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
				
				lstrTab = lstrTab & FormatNumber(ldblAPis, 5) & "|" 'Aliquota
				lstrTab = lstrTab & FormatNumber(ldblACof, 5) & "|" 'Aliquota
				
				lstrTab = lstrTab & FormatNumber(ldblBase, 2) & "|" 'Base Faturada
				lstrTab = lstrTab & FormatNumber(ldblVPis, 2) & "|" 'Valor Pis
				lstrTab = lstrTab & FormatNumber(ldblVCof, 2) & "|" 'Valor Cofins
				
				ifrmPisCofins.VS.Table = istrTamCol & lstrTab
				If VB.Left(.get_TextMatrix(llngRow, 17), 2) = "01" Or VB.Left(.get_TextMatrix(llngRow, 17), 2) = "04" Then
					llngTcQtd = llngTcQtd + ldblQtde
					llngTgQtd = llngTgQtd + ldblQtde
					llngTtQtd = llngTtQtd + ldblQtde
				End If
				ldblTcBase = ldblTcBase + ldblBase
				ldblTcVPis = ldblTcVPis + ldblVPis
				ldblTcVCof = ldblTcVCof + ldblVCof
				'
				ldblTgBase = ldblTgBase + ldblBase
				ldblTgVPis = ldblTgVPis + ldblVPis
				ldblTgVCof = ldblTgVCof + ldblVCof
				'
				ldblTtBase = ldblTtBase + ldblBase
				ldblTtVPis = ldblTtVPis + ldblVPis
				ldblTtVCof = ldblTtVCof + ldblVCof
			Next llngRow
		End With
		Call ImpTotSubTot(llngTcQtd, ldblTcBase, ldblTcVPis, ldblTcVCof, "T")
		Call ImpTotSubTot(llngTgQtd, ldblTgBase, ldblTgVPis, ldblTgVCof, "S")
		Call ImpTotSubTot(llngTtQtd, ldblTtBase, ldblTtVPis, ldblTtVCof, "G")
	End Sub
	
	Private Sub ImpTotSubTot(ByRef alngQtd As Integer, ByRef adblBase As Double, ByRef adblVPis As Double, ByRef adblVCof 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 & "|" 'Aliquota PIS
		lstrTab = lstrTab & "|" 'Aliquota COFINS
		lstrTab = lstrTab & FormatNumber(adblBase, 2) & "|" 'Base Faturada
		lstrTab = lstrTab & FormatNumber(adblVPis, 2) & "|" 'Valor Pis
		lstrTab = lstrTab & FormatNumber(adblVCof, 2) & "|" 'Valor Cofins
		
		With ifrmPisCofins
			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 ifrmPisCofins.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
				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
			adblBase = 0
			adblVPis = 0
			adblVCof = 0
		End If
	End Sub
End Class