Option Strict Off
Option Explicit On
Friend Class frmRelMercado
	Inherits System.Windows.Forms.Form
	Dim sql As String
	Dim fr As New FrmRelatorio
	
	Dim kwhf(10, 100) As Integer
	Dim valor(10, 100) As Double
	Dim qtd(10, 100) As Integer
	Dim tt(10) As Double
	Dim SubTit As String
	
	
	Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		
		Command1.Enabled = False
		Command2.Enabled = False
		
		If cTipo.SelectedIndex = 0 Then
			Call Municipio()
		Else
			Call carregaDados()
			Call Zona()
		End If
		
		Command1.Enabled = True
		Command2.Enabled = True
		
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
		Me.Close()
	End Sub
	
	Private Sub frmRelMercado_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		tMesAno.Text = Month(Today) & "/" & Mid(CStr(Year(Today)), 3)
		cTipo.SelectedIndex = 0
	End Sub
	
	Private Sub Municipio()
		
	End Sub
	
	Private Sub Zona()
		
		fr.quem_chama = Me
		SubTit = "Numero de Consumidores"
		
		fr.VS.Font = VB6.FontChangeName(fr.VS.Font, "Arial")
		fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 5)
		
		fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
		
		fr.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
		'fr.VS.ShowGuides = gdShow
		
		fr.VS.Orientation = VSVIEW6.OrientationSettings.orPortrait
		fr.VS.PhysicalPage = True
		fr.VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
		fr.VS.MarginTop = "7mm"
		fr.VS.MarginRight = "7mm"
		fr.VS.MarginLeft = "7mm"
		fr.VS.MarginBottom = "5mm"
		'    fr.VS.MarginFooter = "0mm"
		'    fr.VS.MarginHeader = "0mm"
		
		fr.VS.TextColor = System.Drawing.Color.Black
		fr.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
		fr.VS.StartDoc()
		
		Dim tot As Integer
		Dim I As Integer
		
		'----- QTDADE ---------
		
		fr.VS.Table = "<20mm|>17mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & "ZONA|RESIDENCIAL|INDUSTRIAL|COMERCIAL|RURAL|P. PUBLICO|IL. PUBLICA|SERV. PUBL.| PROPRIO|TOTAL"
		
		info.Text = "Imprimindo Qtdade.."
		pgb.Minimum = 0
		pgb.Maximum = 100
		pgb.Value = 0
		For I = 1 To 100
			tot = qtd(1, I) + qtd(3, I) + qtd(4, I) + qtd(5, I) + qtd(6, I) + qtd(7, I) + qtd(8, I) + qtd(9, I)
			If tot > 0 Then
				fr.VS.Table = "<20mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & VB6.Format(I, "000") & "|" & qtd(9, I) & "|" & qtd(1, I) & "|" & qtd(4, I) & "|" & qtd(5, I) & "|" & qtd(6, I) & "|" & qtd(3, I) & "|" & qtd(7, I) & "|" & qtd(8, I) & "|" & tot
				
				tt(1) = tt(1) + qtd(1, I)
				tt(3) = tt(3) + qtd(3, I)
				tt(4) = tt(4) + qtd(4, I)
				tt(5) = tt(5) + qtd(5, I)
				tt(6) = tt(6) + qtd(6, I)
				tt(7) = tt(7) + qtd(7, I)
				tt(8) = tt(8) + qtd(8, I)
				tt(9) = tt(9) + qtd(9, I)
				tt(10) = tt(10) + tot
			End If
			pgb.Value = pgb.Value + 1
			System.Windows.Forms.Application.DoEvents()
		Next 
		
		fr.LineFeed((1))
		fr.VS.Table = "^20mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & "TOTAL GERAL|" & VB6.Format(tt(9), "Standard") & "|" & VB6.Format(tt(1), "Standard") & "|" & VB6.Format(tt(4), "Standard") & "|" & VB6.Format(tt(5), "Standard") & "|" & VB6.Format(tt(6), "Standard") & "|" & VB6.Format(tt(3), "Standard") & "|" & VB6.Format(tt(7), "Standard") & "|" & VB6.Format(tt(8), "Standard") & "|" & VB6.Format(tt(10), "Standard")
		
		tt(1) = 0
		tt(3) = 0
		tt(4) = 0
		tt(5) = 0
		tt(6) = 0
		tt(7) = 0
		tt(8) = 0
		tt(9) = 0
		tt(10) = 0
		
		'----- CONSUMO ---------
		info.Text = "Imprimindo Consumo.."
		SubTit = "kWh Faturados"
		fr.VS.NewPage()
		fr.VS.Table = "<20mm|>17mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & "ZONA|RESIDENCIAL|INDUSTRIAL|COMERCIAL|RURAL|P. PUBLICO|IL. PUBLICA|SERV. PUBL.| PROPRIO|TOTAL"
		pgb.Minimum = 0
		pgb.Maximum = 100
		pgb.Value = 0
		For I = 1 To 100
			tot = kwhf(1, I) + kwhf(3, I) + kwhf(4, I) + kwhf(5, I) + kwhf(6, I) + kwhf(7, I) + kwhf(8, I) + kwhf(9, I)
			If tot > 0 Then
				fr.VS.Table = "<20mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & VB6.Format(I, "000") & "|" & kwhf(9, I) & "|" & kwhf(1, I) & "|" & kwhf(4, I) & "|" & kwhf(5, I) & "|" & kwhf(6, I) & "|" & kwhf(3, I) & "|" & kwhf(7, I) & "|" & kwhf(8, I) & "|" & tot
				
				tt(1) = tt(1) + kwhf(1, I)
				tt(3) = tt(3) + kwhf(3, I)
				tt(4) = tt(4) + kwhf(4, I)
				tt(5) = tt(5) + kwhf(5, I)
				tt(6) = tt(6) + kwhf(6, I)
				tt(7) = tt(7) + kwhf(7, I)
				tt(8) = tt(8) + kwhf(8, I)
				tt(9) = tt(9) + kwhf(9, I)
				tt(10) = tt(10) + tot
				
			End If
			pgb.Value = pgb.Value + 1
			System.Windows.Forms.Application.DoEvents()
		Next 
		
		fr.LineFeed((1))
		fr.VS.Table = "^20mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & "TOTAL GERAL|" & VB6.Format(tt(9), "Standard") & "|" & VB6.Format(tt(1), "Standard") & "|" & VB6.Format(tt(4), "Standard") & "|" & VB6.Format(tt(5), "Standard") & "|" & VB6.Format(tt(6), "Standard") & "|" & VB6.Format(tt(3), "Standard") & "|" & VB6.Format(tt(7), "Standard") & "|" & VB6.Format(tt(8), "Standard") & "|" & VB6.Format(tt(10), "Standard")
		tt(1) = 0
		tt(3) = 0
		tt(4) = 0
		tt(5) = 0
		tt(6) = 0
		tt(7) = 0
		tt(8) = 0
		tt(9) = 0
		tt(10) = 0
		
		'----- VALOR ---------
		info.Text = "Imprimindo Valores.."
		SubTit = "Receita (Consumo+Demanda+FP)"
		fr.VS.NewPage()
		fr.VS.Table = "<18mm|>17mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & "ZONA|RESIDENCIAL|INDUSTRIAL|COMERCIAL|RURAL|P. PUBLICO|IL. PUBLICA|SERV. PUBL.| PROPRIO|TOTAL"
		pgb.Minimum = 0
		pgb.Maximum = 100
		pgb.Value = 0
		For I = 1 To 100
			tot = valor(1, I) + valor(3, I) + valor(4, I) + valor(5, I) + valor(6, I) + valor(7, I) + valor(8, I) + valor(9, I)
			If tot > 0 Then
				fr.VS.Table = "<20mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & VB6.Format(I, "000") & "|" & VB6.Format(valor(9, I), "Standard") & "|" & VB6.Format(valor(1, I), "Standard") & "|" & VB6.Format(valor(4, I), "Standard") & "|" & VB6.Format(valor(5, I), "Standard") & "|" & VB6.Format(valor(6, I), "Standard") & "|" & VB6.Format(valor(3, I), "Standard") & "|" & VB6.Format(valor(7, I), "Standard") & "|" & VB6.Format(valor(8, I), "Standard") & "|" & VB6.Format(tot, "Standard")
				
				tt(1) = tt(1) + valor(1, I)
				tt(3) = tt(3) + valor(3, I)
				tt(4) = tt(4) + valor(4, I)
				tt(5) = tt(5) + valor(5, I)
				tt(6) = tt(6) + valor(6, I)
				tt(7) = tt(7) + valor(7, I)
				tt(8) = tt(8) + valor(8, I)
				tt(9) = tt(9) + valor(9, I)
				tt(10) = tt(10) + tot
			End If
			pgb.Value = pgb.Value + 1
			System.Windows.Forms.Application.DoEvents()
		Next 
		fr.LineFeed((1))
		fr.VS.Table = "^20mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm|>15mm;" & "TOTAL GERAL|" & VB6.Format(tt(9), "Standard") & "|" & VB6.Format(tt(1), "Standard") & "|" & VB6.Format(tt(4), "Standard") & "|" & VB6.Format(tt(5), "Standard") & "|" & VB6.Format(tt(6), "Standard") & "|" & VB6.Format(tt(3), "Standard") & "|" & VB6.Format(tt(7), "Standard") & "|" & VB6.Format(tt(8), "Standard") & "|" & VB6.Format(tt(10), "Standard")
		
		
		tt(1) = 0
		tt(3) = 0
		tt(4) = 0
		tt(5) = 0
		tt(6) = 0
		tt(7) = 0
		tt(8) = 0
		tt(9) = 0
		tt(10) = 0
		
		fr.VS.EndDoc()
		fr.Show()
		
	End Sub
	
	
	Private Sub carregaDados()
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		info.Text = "Carregando dados..."
		info.Refresh()
		sql = "select * from nf where mesano='" & Trim(tMesAno.Text) & "'"
		Call Conectar(sql, lrstAux)
		
		If lrstAux.BOF = False And lrstAux.EOF = False Then
			lrstAux.MoveLast()
			lrstAux.MoveFirst()
			pgb.Minimum = 0
			'   pgb.Max = lrstAux.RecordCount
			pgb.Value = 0
		Else
			MsgBox("Não existe faturas no mês selecionado!!", MsgBoxStyle.Information, "Atenção")
			lrstAux.Close()
			Exit Sub
		End If
		
		Dim I As Integer
		
		While Not lrstAux.EOF
			I = I + 1
			info.Text = "Processando: " & I
			info.Refresh()
			Select Case Mid(lrstAux.Fields("codclas").Value, 1, 1)
				Case CStr(1), CStr(2), CStr(3)
					kwhf(1, lrstAux.Fields("Zona").Value) = kwhf(1, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
					valor(1, lrstAux.Fields("Zona").Value) = valor(1, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
					qtd(1, lrstAux.Fields("Zona").Value) = qtd(1, lrstAux.Fields("Zona").Value) + 1
				Case CStr(4)
					kwhf(4, lrstAux.Fields("Zona").Value) = kwhf(4, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
					valor(4, lrstAux.Fields("Zona").Value) = valor(4, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
					qtd(4, lrstAux.Fields("Zona").Value) = qtd(4, lrstAux.Fields("Zona").Value) + 1
				Case CStr(5)
					kwhf(5, lrstAux.Fields("Zona").Value) = kwhf(5, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
					valor(5, lrstAux.Fields("Zona").Value) = valor(5, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
					qtd(5, lrstAux.Fields("Zona").Value) = qtd(5, lrstAux.Fields("Zona").Value) + 1
				Case CStr(6)
					If CDbl(Mid(lrstAux.Fields("codclas").Value, 1, 2)) = 64 Then
						kwhf(3, lrstAux.Fields("Zona").Value) = kwhf(3, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
						valor(3, lrstAux.Fields("Zona").Value) = valor(3, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
						qtd(3, lrstAux.Fields("Zona").Value) = qtd(3, lrstAux.Fields("Zona").Value) + 1
					Else
						kwhf(6, lrstAux.Fields("Zona").Value) = kwhf(6, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
						valor(6, lrstAux.Fields("Zona").Value) = valor(6, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
						qtd(6, lrstAux.Fields("Zona").Value) = qtd(6, lrstAux.Fields("Zona").Value) + 1
					End If
				Case CStr(7)
					kwhf(7, lrstAux.Fields("Zona").Value) = kwhf(7, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
					valor(7, lrstAux.Fields("Zona").Value) = valor(7, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
					qtd(7, lrstAux.Fields("Zona").Value) = qtd(7, lrstAux.Fields("Zona").Value) + 1
				Case CStr(8)
					kwhf(8, lrstAux.Fields("Zona").Value) = kwhf(8, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
					valor(8, lrstAux.Fields("Zona").Value) = valor(8, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
					qtd(8, lrstAux.Fields("Zona").Value) = qtd(8, lrstAux.Fields("Zona").Value) + 1
				Case CStr(9)
					kwhf(9, lrstAux.Fields("Zona").Value) = kwhf(9, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302010009", lrstAux.Fields("itensserv").Value))
					valor(9, lrstAux.Fields("Zona").Value) = valor(9, lrstAux.Fields("Zona").Value) + CDbl(ChecaItensServ("0302020009", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020015", lrstAux.Fields("itensserv").Value)) + CDbl(ChecaItensServ("0302020012", lrstAux.Fields("itensserv").Value))
					qtd(9, lrstAux.Fields("Zona").Value) = qtd(9, lrstAux.Fields("Zona").Value) + 1
			End Select
			
			lrstAux.MoveNext()
			'    pgb.Value = pgb.Value + 1
			System.Windows.Forms.Application.DoEvents()
		End While
		lrstAux.Close()
		
	End Sub
	
	Public Function CabecaRel() As Object
		Call Cabecalho_Relatorio((fr.VS), "RESUMO DE MERCADO - " & Trim(tMesAno.Text), SubTit, "2", False, "")
		Call LineFeed((fr.VS), 1)
	End Function
End Class