Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmRelMediaConsumo
	Inherits System.Windows.Forms.Form

	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private dt, dt1, dt2 As DataTable

	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
		Try
			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

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub MontaGrade()
		Try
			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)
				.FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
				.Row = 1
				.Col = 1
				.Redraw = True
			End With

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Exit Sub
		End Try
	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

		Try
			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)
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			Me.mfgDados.Redraw = False

			If dt.Rows.Count > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count, " Segregando e totalizando")
				For Each row As DataRow In dt.Rows
					lstrGru = UCase(Trim(IIf(IsDBNull(row.Item("grupo")), "B", row.Item("grupo"))))
					lstrCla = Trim(IIf(IsDBNull(row.Item("codclas")), "6000", row.Item("codclas")))
					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
					lstrIte = Trim(IIf(IsDBNull(row.Item("itensserv")), "", row.Item("itensserv")))
					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 BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Segregando e totalizando")
				Next
				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


			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()
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			'Call Conectar(lstrSql, lrstAux)
			Me.mfgDados.Redraw = False

			If dt1.Rows.Count > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt1.Rows.Count, " Quantificando o último mês")
				For Each row1 As DataRow In dt1.Rows
					lstrGru = UCase(Trim(IIf(IsDBNull(row1.Item("grupo")), "B", row1.Item("grupo"))))
					lstrCla = Trim(IIf(IsDBNull(row1.Item("codclas")), "6000", row1.Item("codclas")))
					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 BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Quantificando o último mês")
				Next
			End If


			Me.lblInfo.Text = " Pronto!"
			Call Me.lblInfo.Refresh()
			Me.mfgDados.Redraw = True

			'lrstAux = Nothing

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Exit Sub
		End Try
	End Sub
	Private Sub frmRelMediaConsumo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		'Call CenterForm(Me)
		Try
			Call MontaGrade()
			Me.dtpInicial.Value = Date.Now()
			Me.dtpInicial.Value = CDate("01/01/" & Year(Me.dtpInicial.Value))
			Me.dtpFinal.Value = CDate("31/12/" & Year(Me.dtpInicial.Value))
			Me.txtQtdeMeses.Text = (Month(Me.dtpFinal.Value) - Month(Me.dtpInicial.Value)) + 1
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Exit Sub
		End Try
	End Sub

	Private Sub dtpInicial_ValueChanged(sender As Object, e As EventArgs) Handles dtpInicial.ValueChanged
		Try
			If Me.dtpInicial.Value > Me.dtpFinal.Value Then
				Me.dtpFinal.Value = Me.dtpInicial.Value
			End If
			Me.txtQtdeMeses.Text = (Month(Me.dtpFinal.Value) - Month(Me.dtpInicial.Value)) + 1
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AddLancto" & "" & lblInfo.Text, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Exit Sub
		End Try
	End Sub

	Private Sub dtpFinal_ValueChanged(sender As Object, e As EventArgs) Handles dtpFinal.ValueChanged
		Call dtpInicial_ValueChanged(dtpInicial, New System.EventArgs())
	End Sub
End Class