Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmCalculoGrupoBAvulso
	Inherits System.Windows.Forms.Form
	Private istrMesAno As String
	
	Private Sub AdicionaGridItem(ByVal astrCodigo As String, ByVal adblQtde As Double, ByVal adblValor As Double, ByVal astrReferecia As String)
		With Me.mfgCalculoMedia
			.Rows = .Rows + 1
			.set_TextMatrix(.Rows - 1, 0, astrCodigo)
			.set_TextMatrix(.Rows - 1, 1, codTodescri(astrCodigo))
			.set_TextMatrix(.Rows - 1, 2, astrReferecia)
			.set_TextMatrix(.Rows - 1, 3, Arredondar(CDbl(VB6.Format(adblQtde, "Standard")), 2))
			.set_TextMatrix(.Rows - 1, 4, VB6.Format(adblValor, "Standard"))
		End With
	End Sub
	
	Private Sub cmdCalcFatMedia_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCalcFatMedia.Click
		CalculoFaturaPelaMedia(CDbl(Me.txtMedia.Text))
	End Sub
	
	Private Sub CalculoFaturaPelaMedia(ByVal adblConsumo As Double)
		Dim lstrCodTar As String
		Dim lstrGrTar As String
		Dim lstrClasse As String
		Dim ldteUltLeit As Date
		Dim ldteAtuLeit As Date
		Dim ldblLimiteKWh As Double
		Dim ldblLimiteFimKWh As Double
		Dim ldblLimiteIniKWh As Double
		Dim ldblTarifa As Double
		Dim ldblAuxiliar As Double
		Dim ldblQtdeKWh As Double
		Dim ldblValorKwh As Double
		Dim ldblQtdeKWhAcumul As Double
		Dim ldblQtdeKWhA As Double
		'Icms
		Dim ldblAliqIcmsA As Double
		Dim ldblCoefIcmsA As Double
		Dim ldblIcmsTariA As Double
		Dim ldblBaseIcmsA As Double
		Dim ldblQtdeKWhB As Double
		Dim ldblAliqIcmsB As Double
		Dim ldblCoefIcmsB As Double
		Dim ldblIcmsTariB As Double
		Dim ldblBaseIcmsB As Double
		Dim ldblTBasIcmsA As Double
		Dim ldblTBasIcmsB As Double
		
		Dim ldblAliqPis As Double
		Dim ldblAliqCof As Double
		
		Dim lintCtrlCodigo As Short
		Dim ldblLimiteResid As Double
		Dim ldblLimiteRural As Short
		Dim lstrSql As String
		Dim lrstTarifa As ADODB.Recordset
		lrstTarifa = New ADODB.Recordset
		ldblAuxiliar = 180 'adblConsumo
		ldblLimiteResid = CDbl(LeConfig("LIMITE_RESIDENCIAL"))
		If ldblLimiteResid <= 0 Then ldblLimiteResid = 200
		ldblLimiteRural = CDbl(LeConfig("LIMITE_RURAL"))
		If ldblLimiteRural <= 0 Then ldblLimiteRural = 500
		lstrClasse = Trim(Me.txtCodClasse.Text)
		'  If Me.gridConsumos.Rows > 0 Then
		'    ldteUltLeit = Me.gridConsumos.TextMatrix(1, 2)
		'  Else
		'    ldteUltLeit = gDtBcoDados
		'  End If
		ldteAtuLeit = gDtBcoDados
		ldblAliqPis = MdCalculosGrupoB.Aliquota_PIS(ldteAtuLeit)
		ldblAliqCof = MdCalculosGrupoB.Aliquota_COFINS(ldteAtuLeit)
		Select Case CShort(VB.Left(lstrClasse, 1))
			'Industrial
			Case Is < 4
				lstrCodTar = "01B31011"
				'Comercial
			Case Is = 4
				lstrCodTar = "01B33011"
				'Rural
			Case Is = 5
				lstrCodTar = "01B20111"
				'Poder Publico
			Case Is = CShort("6")
				lstrCodTar = "01B35011"
				If VB.Left(lstrClasse, 2) = "64" Then
					lstrCodTar = "01B40111"
				End If
				'Serviço Publico
			Case Is = 7
				lstrCodTar = "01B32011"
				'Consumo Proprio
			Case Is = 8
				lstrCodTar = "01B34011"
				'Residencial
			Case Is = 9
				lstrCodTar = "01B10111"
				If VB.Right(lstrClasse, 1) = "4" Or VB.Right(lstrClasse, 1) = "5" Or VB.Right(lstrClasse, 1) = "6" Then
					lstrCodTar = "01B10211"
				End If
				lstrCodTar = "01B10211"
		End Select
		lstrSql = ""
		lstrSql = lstrSql & "SELECT num, descri, un, limitkwh, valor, datanova, flgcasc "
		lstrSql = lstrSql & "  FROM `tarifa` "
		lstrSql = lstrSql & " WHERE LEFT(num,8) = """ & lstrCodTar & """ "
		lstrSql = lstrSql & "   AND STR_TO_DATE(datanova,""%d/%m/%Y%"") <> ""1951-01-01"" "
		lstrSql = lstrSql & "   AND STR_TO_DATE(datanova,""%d/%m/%Y%"") <= """ & VB6.Format(ldteAtuLeit, "yyyy-MM-dd") & """ "
		lstrSql = lstrSql & " ORDER BY STR_TO_DATE(datanova,""%d/%m/%Y"") DESC, num ASC "
		Call Conectar(lstrSql, lrstTarifa)
		With lrstTarifa
			ldblLimiteIniKWh = 0
			If .RecordCount > 0 Then
				While Not .EOF And ldblAuxiliar <> 0
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrGrTar = IIf(IsDbNull(.Fields("num").value), "01B1011101", .Fields("num").value)
					ldblTarifa = TarifaProporcional(ldteUltLeit, ldteAtuLeit, lstrGrTar, 3) / 1000
					lintCtrlCodigo = CShort(VB.Right(lstrGrTar, 1)) - 1
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblLimiteFimKWh = IIf(IsDbNull(.Fields("limitkwh").value), 0, .Fields("limitkwh").value)
					ldblLimiteKWh = ldblLimiteFimKWh - ldblLimiteIniKWh
					If ldblAuxiliar <= ldblLimiteKWh Then
						ldblQtdeKWh = ldblAuxiliar
						ldblAuxiliar = 0
					Else
						ldblQtdeKWh = ldblLimiteKWh
						ldblAuxiliar = ldblAuxiliar - ldblQtdeKWh
					End If
					ldblValorKwh = Arredondar(ldblQtdeKWh * ldblTarifa, 2)
					ldblQtdeKWhAcumul = ldblQtdeKWhAcumul + ldblQtdeKWh
					'calculo base ICMS
					If ldblLimiteFimKWh >= ldblLimiteResid Then
						'12%
						ldblQtdeKWhB = ldblLimiteResid - ldblLimiteIniKWh
						If ldblQtdeKWhB > 0 Then
							ldblAliqIcmsB = MdCalculosGrupoB.Aliquota_ICMS(gstrEmpresa_UF, ldblLimiteResid, lstrClasse, ldteAtuLeit)
							ldblCoefIcmsB = Truncate(1 - (((ldblAliqPis / 100) + (ldblAliqCof / 100)) / (1 - (ldblAliqIcmsB / 100))), 11)
							ldblIcmsTariB = Arredondar(ldblTarifa / ldblCoefIcmsB, 5)
							ldblBaseIcmsB = Arredondar((ldblQtdeKWhB * ldblIcmsTariB) / (1 - (ldblAliqIcmsB / 100)), 2)
						Else
							ldblQtdeKWhB = 0
							'ldblAliqIcmsB = 0
							'ldblCoefIcmsB = 0
							'ldblIcmsTariB = 0
							ldblBaseIcmsB = 0
						End If
						'25%
						ldblQtdeKWhA = ldblQtdeKWh - ldblQtdeKWhB
						ldblAliqIcmsA = MdCalculosGrupoB.Aliquota_ICMS(gstrEmpresa_UF, ldblQtdeKWhAcumul, lstrClasse, ldteAtuLeit)
						ldblCoefIcmsA = Truncate(1 - (((ldblAliqPis / 100) + (ldblAliqCof / 100)) / (1 - (ldblAliqIcmsA / 100))), 11)
						ldblIcmsTariA = Arredondar(ldblTarifa / ldblCoefIcmsA, 5)
						ldblBaseIcmsA = Arredondar((ldblQtdeKWhA * ldblIcmsTariA) / (1 - (ldblAliqIcmsA / 100)), 2)
					Else '12%
						ldblAliqIcmsB = MdCalculosGrupoB.Aliquota_ICMS(gstrEmpresa_UF, ldblQtdeKWhAcumul, lstrClasse, ldteAtuLeit)
						ldblCoefIcmsB = Truncate(1 - (((ldblAliqPis / 100) + (ldblAliqCof / 100)) / (1 - (ldblAliqIcmsB / 100))), 11)
						ldblIcmsTariB = Arredondar(ldblTarifa / ldblCoefIcmsB, 5)
						ldblBaseIcmsB = Arredondar((ldblQtdeKWh * ldblIcmsTariB) / (1 - (ldblAliqIcmsB / 100)), 2)
					End If
					ldblTBasIcmsA = ldblTBasIcmsA + ldblBaseIcmsA
					ldblTBasIcmsB = ldblTBasIcmsB + ldblBaseIcmsB
					Call AdicionaGridItem("030201009" & lintCtrlCodigo, ldblQtdeKWh, ldblValorKwh, CStr(ldblTarifa))
					Call .MoveNext()
					ldblLimiteIniKWh = ldblLimiteFimKWh
				End While
				Call AdicionaGridItem("0302030010", ldblTBasIcmsB, Arredondar(ldblTBasIcmsB * (ldblAliqIcmsB / 100), 2), ldblAliqIcmsB & "%")
				Call AdicionaGridItem("0302030011", ldblTBasIcmsA, Arredondar(ldblTBasIcmsA * (ldblAliqIcmsA / 100), 2), ldblAliqIcmsA & "%")
				Call AdicionaGridItem("0302020029", (ldblTBasIcmsB + ldblTBasIcmsA), Arredondar((ldblTBasIcmsB + ldblTBasIcmsA) * (ldblAliqPis / 100), 2), ldblAliqPis & "%")
				Call AdicionaGridItem("0302020030", (ldblTBasIcmsB + ldblTBasIcmsA), Arredondar((ldblTBasIcmsB + ldblTBasIcmsA) * (ldblAliqCof / 100), 2), ldblAliqCof & "%")
			End If
			If .State = 1 Then Call .Close()
		End With
		'UPGRADE_NOTE: Object lrstTarifa 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"'
		lrstTarifa = Nothing
	End Sub
	
	Private Sub MontaGrid()
		With Me.mfgCalculoMedia
			Call .Clear()
			.Rows = 1
			.set_ColWidth(0, 1200)
			.set_ColWidth(1, 4740)
			.set_ColWidth(2, 1480)
			.set_ColWidth(3, 1480)
			.set_ColWidth(4, 1480)
			.set_TextMatrix(0, 0, "Código")
			.set_TextMatrix(0, 1, "Descrição")
			.set_TextMatrix(0, 2, "Referência")
			.set_TextMatrix(0, 3, "Quantidade")
			.set_TextMatrix(0, 4, "Valor")
		End With
	End Sub
	
	Public Sub SetaCalculoAvulsoB(ByVal alngConsId As Integer, ByVal alngUnidId As Integer, ByVal adteUltLeitura As Date, ByVal adblUltKwh As Double, ByVal adblQtdKwh As Double)
		'Funçao para receber os valores iniciais para o calculo
		Dim lstrSql As String
		Dim lrstConsulta As ADODB.Recordset
		lrstConsulta = New ADODB.Recordset
		lstrSql = "SELECT * FROM unidb WHERE cons_id = " & alngConsId & " AND unid_id = " & alngUnidId
		Call Conectar(lstrSql, lrstConsulta)
		With lrstConsulta
			If .RecordCount > 0 Then
				Me.txtZLC.Text = ""
			End If
		End With
		
	End Sub
	
	Private Sub frmCalculoGrupoBAvulso_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		istrMesAno = Trim(LeConfig("MESANOFATURAMENTO"))
		If Len(istrMesAno) = 0 Or istrMesAno = "-1" Then
			istrMesAno = VB6.Format(gDtBcoDados, "mm/yyyy")
		End If
		Call MontaGridIcms("9101", Today)
		Call MontaGridTributos((Me.mfgPis), Today, "Pis")
		Call MontaGridTributos((Me.mfgCofins), Today, "Cof")
		
	End Sub
	
	Private Sub MontaGridIcms(ByVal astrClasse As String, ByVal adteReferencia As Date)
		Dim lstrCodigo As String
		Dim lstrClasse As Short
		Dim lintClasse As Short
		Dim ldblLimiteKWh As Double
		Dim ldblValor As Double
		Dim ldteRefer As Date
		Dim lstrSql As String
		Dim lrstICMS As ADODB.Recordset
		lrstICMS = New ADODB.Recordset
		With Me.mfgIcms
			.Cols = 4
			.Clear()
			.Rows = 1
			.set_ColWidth(0, 1000)
			.set_ColWidth(1, 1000)
			.set_ColWidth(2, 1000)
			.set_ColWidth(3, 1000)
			.set_TextMatrix(0, 0, "Código")
			.set_TextMatrix(0, 1, "Lim.Kwh até")
			.set_TextMatrix(0, 2, "% Icms")
			.set_TextMatrix(0, 3, "Referência")
		End With
		
		lstrClasse = CShort(VB6.Format(astrClasse, "0000"))
		lintClasse = CShort(VB.Left(CStr(lstrClasse), 1))
		If lintClasse = 6 Then lintClasse = CShort(VB.Left(CStr(lstrClasse), 2))
		If lintClasse < 4 Then lintClasse = 1
		lstrCodigo = "40" & gstrEmpresa_UF & "B00" & VB.Left(lintClasse & "00", 2)
		lstrSql = ""
		lstrSql = lstrSql & "SELECT num, limitkwh, valor, datanova "
		lstrSql = lstrSql & "  FROM " & gstrDataBase & ".codigos "
		lstrSql = lstrSql & " WHERE codigos.datanova <= """ & VB6.Format(adteReferencia, "yyyy-MM-dd") & """ "
		lstrSql = lstrSql & "   AND LEFT(codigos.num,9) = """ & lstrCodigo & """ "
		lstrSql = lstrSql & " ORDER BY datanova DESC, limitkwh ASC, num ASC "
		Call Conectar(lstrSql, lrstICMS)
		With lrstICMS
			If .RecordCount > 0 Then
				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"'
					lstrCodigo = IIf(IsDbNull(.Fields("num").value), "40" & gstrEmpresa_UF & "B00901", .Fields("num").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"'
					ldblLimiteKWh = IIf(IsDbNull(.Fields("limitkwh").value), 0, .Fields("limitkwh").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"'
					ldblValor = IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").value)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteRefer = IIf(IsDbNull(.Fields("datanova").value), "1900-01-01", .Fields("datanova").value)
					With Me.mfgIcms
						.Rows = .Rows + 1
						.set_TextMatrix(.Rows - 1, 0, Mid(lstrCodigo, 3, 2) & " - " & VB.Right(lstrCodigo, 1))
						.set_TextMatrix(.Rows - 1, 1, ldblLimiteKWh)
						.set_TextMatrix(.Rows - 1, 2, VB6.Format(ldblValor, "Standard"))
						.set_TextMatrix(.Rows - 1, 3, VB6.Format(ldteRefer, "dd/mm/yyyy"))
					End With
					Call .MoveNext()
				End While
			End If
			If .State = 1 Then Call .Close()
		End With
		'UPGRADE_NOTE: Object lrstICMS 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"'
		lrstICMS = Nothing
	End Sub
	
	Private Sub MontaGridTributos(ByRef amfgAuxiliar As Object, ByVal adteReferencia As Date, ByVal astrTipo As String)
		Dim lstrCodigo As String
		Dim lstrClasse As Short
		Dim lintClasse As Short
		Dim ldblLimiteKWh As Double
		Dim ldblValor As Double
		Dim ldteRefer As Date
		Dim lstrSql As String
		Dim lrstConsulta As ADODB.Recordset
		lrstConsulta = New ADODB.Recordset
		With amfgAuxiliar
			'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.Cols. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.Cols = 2
			'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.Clear. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.Clear()
			'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.Rows. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.Rows = 1
			'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.ColWidth. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.ColWidth(0) = 1000
			'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.ColWidth. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.ColWidth(1) = 1000
			'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.TextMatrix. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.TextMatrix(0, 0) = "% " & astrTipo
			'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.TextMatrix. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			.TextMatrix(0, 1) = "Referência"
		End With
		lstrCodigo = "40" & UCase(astrTipo)
		lstrSql = ""
		lstrSql = lstrSql & "SELECT valor, datanova "
		lstrSql = lstrSql & "  FROM " & gstrDataBase & ".codigos "
		lstrSql = lstrSql & " WHERE codigos.datanova <= """ & VB6.Format(adteReferencia, "yyyy-MM-dd") & """ "
		lstrSql = lstrSql & "   AND LEFT(codigos.num,5) = """ & lstrCodigo & """ "
		lstrSql = lstrSql & " ORDER BY datanova DESC LIMIT 0, 12 "
		Call Conectar(lstrSql, lrstConsulta)
		With lrstConsulta
			If .RecordCount > 0 Then
				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"'
					ldblValor = CDbl(Replace(IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").value), ".", ","))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteRefer = IIf(IsDbNull(.Fields("datanova").value), "1900-01-01", .Fields("datanova").value)
					With amfgAuxiliar
						'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.Rows. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						.Rows = .Rows + 1
						'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.Rows. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.TextMatrix. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						.TextMatrix(.Rows - 1, 0) = VB6.Format(ldblValor, "#,##0.00000")
						'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.Rows. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						'UPGRADE_WARNING: Couldn't resolve default property of object amfgAuxiliar.TextMatrix. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						.TextMatrix(.Rows - 1, 1) = VB6.Format(ldteRefer, "dd/mm/yyyy")
					End With
					Call .MoveNext()
				End While
			End If
			If .State = 1 Then Call .Close()
		End With
		'UPGRADE_NOTE: Object lrstConsulta 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"'
		lrstConsulta = Nothing
	End Sub
End Class