Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelMediaConsumo
	Inherits System.Windows.Forms.Form
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		
		Me.cmdProcessar.Enabled = False
		Me.cmdFechar.Enabled = False
		Me.dtpInicial.Enabled = False
		Me.dtpFinal.Enabled = False
		Me.txtQtdeMeses.Enabled = False
		Me.mfgDados.Enabled = False
		System.Windows.Forms.Application.DoEvents()
		Call GeraPorClasse()
		Me.cmdProcessar.Enabled = True
		Me.cmdFechar.Enabled = True
		Me.dtpInicial.Enabled = True
		Me.dtpFinal.Enabled = True
		Me.txtQtdeMeses.Enabled = True
		Me.mfgDados.Enabled = True
	End Sub
	
	Private Sub MontaGrade()
		With Me.mfgDados
			Call .Clear()
			.Redraw = False
			.Rows = 7
			.Width = VB6.TwipsToPixelsX(8850)
			.set_TextMatrix(0, 0, "Classe")
			.set_TextMatrix(0, 1, "Média KWh MT")
			.set_TextMatrix(0, 2, "Qtde MT")
			.set_TextMatrix(0, 3, "Média KWh BT")
			.set_TextMatrix(0, 4, "Qtde BT")
			.set_TextMatrix(0, 5, "Média KWh Total")
			.set_TextMatrix(0, 6, "Qtde Total")
			.set_ColWidth(0, 1000)
			.set_ColWidth(1, 1600)
			.set_ColWidth(2, 1000)
			.set_ColWidth(3, 1600)
			.set_ColWidth(4, 1000)
			.set_ColWidth(5, 1600)
			.set_ColWidth(6, 1000)
			.set_TextMatrix(1, 0, "Residencial")
			.set_TextMatrix(2, 0, "Industrial")
			.set_TextMatrix(3, 0, "Comercial")
			.set_TextMatrix(4, 0, "Rural")
			.set_TextMatrix(5, 0, "Outras")
			.set_TextMatrix(6, 0, "Total")
			.set_TextMatrix(1, 1, "0,00000")
			.set_TextMatrix(2, 1, "0,00000")
			.set_TextMatrix(3, 1, "0,00000")
			.set_TextMatrix(4, 1, "0,00000")
			.set_TextMatrix(5, 1, "0,00000")
			.set_TextMatrix(6, 1, "0,00000")
			.set_TextMatrix(1, 3, "0,00000")
			.set_TextMatrix(2, 3, "0,00000")
			.set_TextMatrix(3, 3, "0,00000")
			.set_TextMatrix(4, 3, "0,00000")
			.set_TextMatrix(5, 3, "0,00000")
			.set_TextMatrix(6, 3, "0,00000")
			.set_TextMatrix(1, 5, "0,00000")
			.set_TextMatrix(2, 5, "0,00000")
			.set_TextMatrix(3, 5, "0,00000")
			.set_TextMatrix(4, 5, "0,00000")
			.set_TextMatrix(5, 5, "0,00000")
			.set_TextMatrix(6, 5, "0,00000")
			.set_TextMatrix(1, 2, "0")
			.set_TextMatrix(2, 2, "0")
			.set_TextMatrix(3, 2, "0")
			.set_TextMatrix(4, 2, "0")
			.set_TextMatrix(5, 2, "0")
			.set_TextMatrix(6, 2, "0")
			.set_TextMatrix(1, 4, "0")
			.set_TextMatrix(2, 4, "0")
			.set_TextMatrix(3, 4, "0")
			.set_TextMatrix(4, 4, "0")
			.set_TextMatrix(5, 4, "0")
			.set_TextMatrix(6, 4, "0")
			.set_TextMatrix(1, 6, "0")
			.set_TextMatrix(2, 6, "0")
			.set_TextMatrix(3, 6, "0")
			.set_TextMatrix(4, 6, "0")
			.set_TextMatrix(5, 6, "0")
			.set_TextMatrix(6, 6, "0")
			'UPGRADE_ISSUE: VBControlExtender method mfgDados.FillStyle was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			.FillStyle = MSFlexGridLib.FillStyleSettings.flexFillRepeat
			.Col = 0
			.Row = 6
			.ColSel = .Cols - 1
			.CellFontBold = True
			.CellBackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
			.Col = 5
			.Row = 1
			.RowSel = .Rows - 2
			.CellFontBold = True
			.CellBackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
			.Col = 6
			.Row = 1
			.RowSel = .Rows - 2
			.CellFontBold = True
			.CellBackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
			'UPGRADE_ISSUE: VBControlExtender method mfgDados.FillStyle was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			.FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
			.Row = 1
			.Col = 1
			.Redraw = True
		End With
	End Sub
	
	Private Sub GeraPorClasse()
		Dim llngQtdeMes As Integer
		Dim lstrCla As String
		Dim lstrGru As String
		Dim lstrIte As String
		Dim lintLin As Short
		Dim lintCol As Short
		Dim ldblTot As Double
		Dim ldblKWhB As Double
		Dim ldblKWhA As Double
		Dim ldblKWhAP As Double
		Dim ldblKWhAFp As Double
		Dim ldblMediaG As Double
		Dim ldblMediaA As Double
		Dim ldblMediaB As Double
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		llngQtdeMes = CInt(Me.txtQtdeMeses.Text)
		Call MontaGrade()
		lstrSql = ""
		lstrSql = lstrSql & " SELECT "
		lstrSql = lstrSql & "        `codclas`, "
		lstrSql = lstrSql & "        `zona`, "
		lstrSql = lstrSql & "        `livro`, "
		lstrSql = lstrSql & "        `conta`, "
		lstrSql = lstrSql & "        `numnf`, "
		lstrSql = lstrSql & "        `flgst`, "
		lstrSql = lstrSql & "        `mesano_ref`, "
		lstrSql = lstrSql & "        `grupo`, "
		lstrSql = lstrSql & "        `itensserv` "
		lstrSql = lstrSql & "  FROM `nf` "
		lstrSql = lstrSql & " WHERE `flgst` <> 'C' "
		lstrSql = lstrSql & "    AND `mesano_ref` BETWEEN " & CInt(VB6.Format(Me.dtpInicial.value, "yyyymm")) & " AND " & CInt(VB6.Format(Me.dtpFinal.value, "yyyymm")) & " "
		If Me.txtLivro.Text <> "" Then
			lstrSql = lstrSql & "    AND livro IN('" & Me.txtLivro.Text & "') "
		End If
		Me.lblInfo.Text = " Segregando e totalizando..."
		Call Me.lblInfo.Refresh()
		Call Conectar(lstrSql, lrstAux)
		Me.mfgDados.Redraw = False
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " Segregando e totalizando")
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrGru = UCase(Trim(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"'
						lstrCla = Trim(IIf(IsDbNull(.Fields("codclas").value), "6000", .Fields("codclas").value))
						If CShort(VB.Left(lstrCla, 1)) = 9 Then
							lstrCla = "Residencial"
							lintLin = 1
						ElseIf CShort(VB.Left(lstrCla, 1)) < 4 Then 
							lstrCla = "Industrial"
							lintLin = 2
						ElseIf CShort(VB.Left(lstrCla, 1)) = 4 Then 
							lstrCla = "Comercial"
							lintLin = 3
						ElseIf CShort(VB.Left(lstrCla, 1)) = 5 Then 
							lstrCla = "Rural"
							lintLin = 4
						Else
							lstrCla = "Outras"
							lintLin = 5
						End If
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrIte = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
						If lstrGru = "B" Then
							ldblKWhB = BuscaVlrItem(lstrIte, "030201009")
							ldblKWhA = 0
							ldblKWhAP = 0
							ldblKWhAFp = 0
							lintCol = 3
						ElseIf lstrGru = "A" Then 
							ldblKWhB = 0
							ldblKWhA = BuscaVlrItem(lstrIte, "0302010009")
							ldblKWhAP = BuscaVlrItem(lstrIte, "0302010010")
							ldblKWhAFp = BuscaVlrItem(lstrIte, "0302010011")
							lintCol = 1
						End If
						With Me.mfgDados
							.set_TextMatrix(lintLin, lintCol, IIf(IsNumeric(.get_TextMatrix(lintLin, lintCol)), .get_TextMatrix(lintLin, lintCol), 0) + (ldblKWhA + ldblKWhAP + ldblKWhAFp + ldblKWhB))
							.set_TextMatrix(lintLin, 5, IIf(IsNumeric(.get_TextMatrix(lintLin, 5)), .get_TextMatrix(lintLin, 5), 0) + (ldblKWhA + ldblKWhAP + ldblKWhAFp + ldblKWhB))
							.set_TextMatrix(6, lintCol, IIf(IsNumeric(.get_TextMatrix(6, lintCol)), .get_TextMatrix(6, lintCol), 0) + (ldblKWhA + ldblKWhAP + ldblKWhAFp + ldblKWhB))
							.set_TextMatrix(6, 5, IIf(IsNumeric(.get_TextMatrix(6, 5)), .get_TextMatrix(6, 5), 0) + (ldblKWhA + ldblKWhAP + ldblKWhAFp + ldblKWhB))
						End With
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Segregando e totalizando")
					End While
					With Me.mfgDados
						For lintLin = 1 To 6
							.set_TextMatrix(lintLin, 1, FormatNumber(CDbl(IIf(IsNumeric(.get_TextMatrix(lintLin, 1)), .get_TextMatrix(lintLin, 1), 0)) / llngQtdeMes, 5))
							.set_TextMatrix(lintLin, 3, FormatNumber(CDbl(IIf(IsNumeric(.get_TextMatrix(lintLin, 3)), .get_TextMatrix(lintLin, 3), 0)) / llngQtdeMes, 5))
							.set_TextMatrix(lintLin, 5, FormatNumber(CDbl(IIf(IsNumeric(.get_TextMatrix(lintLin, 5)), .get_TextMatrix(lintLin, 5), 0)) / llngQtdeMes, 5))
						Next lintLin
					End With
				End If
				Call .Close()
			End If
		End With
		Me.mfgDados.Redraw = True
		Me.lblInfo.Text = " Pronto!"
		Call Me.lblInfo.Refresh()
		System.Windows.Forms.Application.DoEvents()
		lstrSql = ""
		lstrSql = lstrSql & " SELECT "
		lstrSql = lstrSql & "        `codclas`, "
		lstrSql = lstrSql & "        `zona`, "
		lstrSql = lstrSql & "        `livro`, "
		lstrSql = lstrSql & "        `conta`, "
		lstrSql = lstrSql & "        `numnf`, "
		lstrSql = lstrSql & "        `mesano_ref`, "
		lstrSql = lstrSql & "        `grupo`, "
		lstrSql = lstrSql & "        `flgst` "
		lstrSql = lstrSql & "  FROM `nf` "
		lstrSql = lstrSql & " WHERE `flgst` <> 'C' "
		lstrSql = lstrSql & "    AND `mesano_ref` = " & CInt(VB6.Format(Me.dtpFinal.value, "yyyymm")) & " "
		If Me.txtLivro.Text <> "" Then
			lstrSql = lstrSql & "    AND livro IN('" & Me.txtLivro.Text & "') "
		End If
		Me.lblInfo.Text = " Quantificando o último mês..."
		Call Me.lblInfo.Refresh()
		Call Conectar(lstrSql, lrstAux)
		Me.mfgDados.Redraw = False
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " Quantificando o último mês")
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrGru = UCase(Trim(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"'
						lstrCla = Trim(IIf(IsDbNull(.Fields("codclas").value), "6000", .Fields("codclas").value))
						If CShort(VB.Left(lstrCla, 1)) = 9 Then
							lstrCla = "Residencial"
							lintLin = 1
						ElseIf CShort(VB.Left(lstrCla, 1)) < 4 Then 
							lstrCla = "Industrial"
							lintLin = 2
						ElseIf CShort(VB.Left(lstrCla, 1)) = 4 Then 
							lstrCla = "Comercial"
							lintLin = 3
						ElseIf CShort(VB.Left(lstrCla, 1)) = 5 Then 
							lstrCla = "Rural"
							lintLin = 4
						Else
							lstrCla = "Outras"
							lintLin = 5
						End If
						If lstrGru = "B" Then
							lintCol = 4
						ElseIf lstrGru = "A" Then 
							lintCol = 2
						End If
						With Me.mfgDados
							.set_TextMatrix(lintLin, lintCol, FormatNumber(IIf(IsNumeric(.get_TextMatrix(lintLin, lintCol)), .get_TextMatrix(lintLin, lintCol), 0) + 1, 0))
							.set_TextMatrix(lintLin, 6, FormatNumber(IIf(IsNumeric(.get_TextMatrix(lintLin, 6)), .get_TextMatrix(lintLin, 6), 0) + 1, 0))
							.set_TextMatrix(6, lintCol, FormatNumber(IIf(IsNumeric(.get_TextMatrix(6, lintCol)), .get_TextMatrix(6, lintCol), 0) + 1, 0))
							.set_TextMatrix(6, 6, FormatNumber(IIf(IsNumeric(.get_TextMatrix(6, 6)), .get_TextMatrix(6, 6), 0) + 1, 0))
						End With
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Quantificando o último mês")
					End While
				End If
				Call .Close()
			End If
		End With
		Me.lblInfo.Text = " Pronto!"
		Call Me.lblInfo.Refresh()
		Me.mfgDados.Redraw = True
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
	End Sub
	
	Private Sub dtpFinal_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpFinal.Change
		Call dtpInicial_Change(dtpInicial, New System.EventArgs())
	End Sub
	
	Private Sub dtpFinal_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpFinal.ClickEvent
		Call dtpInicial_Change(dtpInicial, New System.EventArgs())
	End Sub
	
	Private Sub dtpInicial_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpInicial.Change
		'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpFinal.value. 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 Me.dtpInicial.value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		If Me.dtpInicial.value > Me.dtpFinal.value Then
			'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpInicial.value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			Me.dtpFinal.value = Me.dtpInicial.value
		End If
		Me.txtQtdeMeses.Text = (Me.dtpFinal.Month - Me.dtpInicial.Month) + 1
	End Sub
	
	Private Sub dtpInicial_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpInicial.ClickEvent
		Call dtpInicial_Change(dtpInicial, New System.EventArgs())
	End Sub
	
	Private Sub frmRelMediaConsumo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		Call MontaGrade()
		Me.dtpInicial.value = BuscaDataBD
		Me.dtpInicial.value = CDate("01/01/" & Me.dtpInicial.Year)
		Me.dtpFinal.value = CDate("31/12/" & Me.dtpInicial.Year)
		Me.txtQtdeMeses.Text = (Me.dtpFinal.Month - Me.dtpInicial.Month) + 1
	End Sub
End Class