Option Strict Off
Option Explicit On
Friend Class FrmConsAbertoClasse
	Inherits System.Windows.Forms.Form
	Dim strTabela As String
	
	'UPGRADE_WARNING: Event chkTrans.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkTrans_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkTrans.CheckStateChanged
		If chkTrans.CheckState = 1 Then
			strTabela = "nftransicao"
		Else
			strTabela = "nf"
		End If
	End Sub
	
	Private Sub FrmConsAbertoClasse_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call MontaGrid()
		tDataBase.value = VB6.Format(Today, "dd/mm/yyyy")
		strTabela = "nf"
	End Sub
	
	Private Sub MontaGrid()
		'caption
		If OptDia.Checked Then
			grid.set_TextMatrix(0, 0, "Dia")
		End If
		If OptClasse.Checked Then
			grid.set_TextMatrix(0, 0, "Classe")
		End If
		grid.set_TextMatrix(0, 1, "Qtdade")
		grid.set_TextMatrix(0, 2, "Valor")
		grid.set_TextMatrix(0, 3, "%")
		grid.set_ColWidth(0, 1600)
		grid.set_ColWidth(1, 950)
		grid.set_ColWidth(2, 1200)
		grid.set_ColWidth(3, 950)
	End Sub
	
	Private Sub cmdAnalisar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAnalisar.Click
		Call LigDes(False)
		
		If OptClasse.Checked Then
			Call carregaDados()
		End If
		
		If OptDia.Checked Then
			Call carregaDados1()
		End If
		
		Call LigDes(True)
	End Sub
	
	Private Sub carregaDados()
		Dim lngQtd As Integer
		Dim dblVlrTot As Double
		Dim strsql As String
		Dim strSql2 As String
		Dim rsConsulta As New ADODB.Recordset
		Dim strTipo As Object
		Dim strCodi As Object
		Dim lngIni As Integer
		Dim lngFim As Integer
		Dim intLinha As Short
		
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		
		'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		'UPGRADE_WARNING: Couldn't resolve default property of object strTipo. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		strTipo = New Object(){"Residencial Normal", "Res.Baixa Renda", "Rural", "Comercial", "Industrial", "Consumidor Livre", "P.P Municipal", "P.P Estadual", "P.P Federal", "Iluminação Publica", "Serviço Publico", "Consumo Proprio"}
		'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		'UPGRADE_WARNING: Couldn't resolve default property of object strCodi. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		strCodi = New Object(){"codclas = 9101", "codclas in(9102,9103,9104,9105,9106)", "LEFT(codclas,1) = 5", "LEFT(codclas,1) = 4", "(LEFT(codclas,1) in (0,1,2,3) AND codclas <> '0100')", "codclas = '0100'", "LEFT(codclas,2) = 61", "LEFT(codclas,2) = 62", "LEFT(codclas,2) = 63", "LEFT(codclas,2) = 64", "LEFT(codclas,1) = 7", "LEFT(codclas,1) = 8"}
		
		lngIni = LBound(strTipo)
		lngFim = UBound(strTipo)
		
		grid.Rows = lngFim + 3
		
		grid.Clear()
		Call MontaGrid()
		
		lblInfo.Text = "Preparando os dados..."
		lblInfo.Refresh()
		
		strsql = "SELECT COUNT(*) qtd, SUM(valortot) valor FROM " & strTabela & " USE INDEX(KeyVenc) " & " WHERE trim(flgst)='' AND datavenc <= '" & VB6.Format(tDataBase.value, "yyyy-MM-dd") & "' "
		
		Call Conectar(strsql, rsConsulta)
		If rsConsulta.RecordCount > 0 Then
			'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"'
			lngQtd = IIf(IsDbNull(rsConsulta.Fields("qtd").Value), 0, rsConsulta.Fields("qtd").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"'
			dblVlrTot = IIf(IsDbNull(rsConsulta.Fields("valor").Value), 0, rsConsulta.Fields("valor").Value)
			If rsConsulta.State = 1 Then rsConsulta.Close()
			
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, lngFim + 1)
			For intLinha = lngIni To lngFim
				'UPGRADE_WARNING: Couldn't resolve default property of object strTipo(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lblInfo.Text = "Somando " & strTipo(intLinha)
				lblInfo.Refresh()
				
				'UPGRADE_WARNING: Couldn't resolve default property of object strCodi(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				Call Conectar(strsql & " AND " & strCodi(intLinha), rsConsulta)
				If rsConsulta.RecordCount > 0 Then
					'UPGRADE_WARNING: Couldn't resolve default property of object strTipo(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					grid.set_TextMatrix(intLinha + 1, 0, strTipo(intLinha))
					'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"'
					grid.set_TextMatrix(intLinha + 1, 1, IIf(IsDbNull(rsConsulta.Fields("qtd").Value), 0, rsConsulta.Fields("qtd").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"'
					grid.set_TextMatrix(intLinha + 1, 2, VB6.Format(IIf(IsDbNull(rsConsulta.Fields("valor").Value), 0, rsConsulta.Fields("valor").Value), "Standard"))
					'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"'
					grid.set_TextMatrix(intLinha + 1, 3, VB6.Format(System.Math.Round((IIf(IsDbNull(rsConsulta.Fields("valor").Value), 0, rsConsulta.Fields("valor").Value) / dblVlrTot) * 100, 2), "Standard"))
				End If
				If rsConsulta.State = 1 Then rsConsulta.Close()
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
			Next intLinha
			'Total
			grid.set_TextMatrix(intLinha + 1, 0, "Total")
			grid.set_TextMatrix(intLinha + 1, 1, lngQtd)
			grid.set_TextMatrix(intLinha + 1, 2, VB6.Format(dblVlrTot, "Standard"))
			grid.set_TextMatrix(intLinha + 1, 3, "100%")
			
			lblInfo.Text = "Montando Gráfico..."
			System.Windows.Forms.Application.DoEvents()
			Call MontaGrafico()
			lblInfo.Text = "Pronto!"
			System.Windows.Forms.Application.DoEvents()
		Else
			MsgBox("Não existe faturas em aberto menor ou igual a data base " & tDataBase.value & "!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
		End If
		If rsConsulta.State = 1 Then rsConsulta.Close()
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Sub carregaDados1()
		Dim intDia As Short
		Dim lngQtd As Integer
		Dim dblValor As Double
		Dim dblTotVlr As Double
		Dim lngTotQtd As Integer
		Dim strsql As String
		Dim rsConsulta As New ADODB.Recordset
		Dim lngConta As Integer
		
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		
		'Grid
		grid.Rows = 1
		grid.Clear()
		Call MontaGrid()
		
		lblInfo.Text = "Preparando os dados..."
		lblInfo.Refresh()
		
		strsql = "SELECT DAY(datavenc) diavenc, COUNT(*) qtd, SUM(valortot) valor " & "  FROM " & strTabela & " USE INDEX(KeyVenc) " & " WHERE trim(flgst)='' " & "   AND datavenc <= '" & VB6.Format(tDataBase.value, "yyyy-MM-dd") & "' " & " GROUP BY diavenc "
		Call Conectar(strsql, rsConsulta)
		
		If rsConsulta.RecordCount > 0 Then
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, rsConsulta.RecordCount)
			
			dblTotVlr = 0
			lngTotQtd = 0
			
			While Not rsConsulta.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"'
				intDia = IIf(IsDbNull(rsConsulta.Fields("DiaVenc").Value), 0, rsConsulta.Fields("DiaVenc").Value)
				lblInfo.Text = "Somando o dia " & intDia
				lblInfo.Refresh()
				
				'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"'
				lngQtd = IIf(IsDbNull(rsConsulta.Fields("qtd").Value), 0, rsConsulta.Fields("qtd").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"'
				dblValor = IIf(IsDbNull(rsConsulta.Fields("valor").Value), 0, rsConsulta.Fields("valor").Value)
				
				dblTotVlr = dblTotVlr + dblValor
				lngTotQtd = lngTotQtd + lngQtd
				
				grid.Rows = grid.Rows + 1
				grid.set_TextMatrix(grid.Rows - 1, 0, intDia)
				grid.set_TextMatrix(grid.Rows - 1, 1, lngQtd)
				grid.set_TextMatrix(grid.Rows - 1, 2, VB6.Format(dblValor, "Standard"))
				
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				rsConsulta.MoveNext()
			End While
			If rsConsulta.State = 1 Then rsConsulta.Close()
			
			For lngConta = 1 To grid.Rows - 1
				grid.set_TextMatrix(lngConta, 3, VB6.Format(System.Math.Round((CDbl(grid.get_TextMatrix(lngConta, 2)) / dblTotVlr) * 100, 2), "Standard"))
			Next lngConta
			'Total
			grid.Rows = grid.Rows + 1
			grid.set_TextMatrix(grid.Rows - 1, 0, "Total")
			grid.set_TextMatrix(grid.Rows - 1, 1, lngTotQtd)
			grid.set_TextMatrix(grid.Rows - 1, 2, VB6.Format(dblTotVlr, "Standard"))
			grid.set_TextMatrix(grid.Rows - 1, 3, "100%")
			
			lblInfo.Text = "Montando Gráfico..."
			System.Windows.Forms.Application.DoEvents()
			Call MontaGrafico()
			lblInfo.Text = "Pronto!"
			System.Windows.Forms.Application.DoEvents()
		Else
			MsgBox("Não existe faturas em aberto menor ou igual a data base " & tDataBase.value & "!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
		End If
		If rsConsulta.State = 1 Then rsConsulta.Close()
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	Private Sub cmdRelatorio_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelatorio.Click
		Call LigDes(False)
		Call EmitirRelatorio()
		Call LigDes(True)
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Me.Close()
	End Sub
	
	Private Sub MontaGrafico()
		Dim intConta As Integer
		
		chart.Visible = True
		chart.ColumnCount = 10
		chart.RowCount = 1
		If OptDia.Checked = True Then
			chart.Title._Text = "Débitos Vencidos por Dia"
		End If
		If OptClasse.Checked = True Then
			chart.Title._Text = "Débitos Vencidos por Classe"
		End If
		
		If OptClasse.Checked Then
			For intConta = 1 To 10
				chart.Row = 1
				chart.Column = intConta
				chart.ColumnLabel = grid.get_TextMatrix(intConta, 0)
				chart.Data = grid.get_TextMatrix(intConta, 3)
			Next intConta
		ElseIf OptDia.Checked Then 
			For intConta = 1 To grid.Rows - 2
				chart.Row = 1
				chart.Column = intConta
				chart.ColumnLabel = grid.get_TextMatrix(intConta, 0)
				chart.Data = grid.get_TextMatrix(intConta, 3)
			Next intConta
		End If
		chart.ShowLegend = True
	End Sub
	
	
	Private Sub EmitirRelatorio()
		Dim intLinha As Short
		Dim strLinha As String
		Dim strTam As String
		Dim intMarg As Short
		Dim fr As New FrmRelatorio
		
		fr.quem_chama = Me
		
		lblInfo.Text = "Montando Relatório..."
		lblInfo.Refresh()
		
		intMarg = 20
		fr.VS.Font = VB6.FontChangeName(fr.VS.Font, "Arial")
		fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 8)
		
		fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
		fr.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
		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.TextColor = System.Drawing.Color.Black
		fr.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
		fr.VS.StartDoc()
		
		If OptClasse.Checked Then
			
			Call Cabecalho_Relatorio((fr.VS), "Débitos Vencidos por Classe", "Data Base: " & Trim(tDataBase.value), "2", False, "")
			Call LineFeed((fr.VS), 1)
			
			fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
			strTam = "<" & intMarg & "mm|<50mm|>30mm|>30mm|>20mm;"
			fr.VS.Table = strTam & "|Classe|Qtdade|Valor|%"
			'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			fr.VS.DrawLine(10 + (intMarg - 2) & "mm", fr.VS.CurrentY + 20, 140 + (intMarg - 2) & "mm", fr.VS.CurrentY + 20)
			fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
		End If
		
		If OptDia.Checked Then
			Call Cabecalho_Relatorio((fr.VS), "Débitos Vencidos por Dia", "Data Base: " & Trim(tDataBase.value), "2", False, "")
			Call LineFeed((fr.VS), 1)
			
			fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
			strTam = "<" & intMarg & "mm|<50mm|>30mm|>30mm|>20mm;"
			fr.VS.Table = strTam & "|Dia|Qtdade|Valor|%"
			'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			fr.VS.DrawLine(10 + (intMarg - 2) & "mm", fr.VS.CurrentY + 20, 140 + (intMarg - 2) & "mm", fr.VS.CurrentY + 20)
			fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
		End If
		
		'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 100
		
		For intLinha = 1 To grid.Rows - 2
			strLinha = "|" & grid.get_TextMatrix(intLinha, 0) & "|" & grid.get_TextMatrix(intLinha, 1) & "|" & VB6.Format(grid.get_TextMatrix(intLinha, 2), "Standard") & "|" & VB6.Format(grid.get_TextMatrix(intLinha, 3), "Standard") & "%"
			fr.VS.Table = strTam & strLinha
		Next intLinha
		
		'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.DrawLine(10 + (intMarg - 2) & "mm", fr.VS.CurrentY + 5, 140 + (intMarg - 2) & "mm", fr.VS.CurrentY + 5)
		
		'UPGRADE_ISSUE: VBControlExtender method VS.CurrentY was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		fr.VS.CurrentY = fr.VS.CurrentY + 10
		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
		strLinha = "|" & grid.get_TextMatrix(grid.Rows - 1, 0) & "|" & grid.get_TextMatrix(grid.Rows - 1, 1) & "|" & VB6.Format(grid.get_TextMatrix(grid.Rows - 1, 2), "Standard") & "|100%"
		fr.VS.Table = strTam & strLinha
		fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
		
		chart.EditCopy()
		Picture1.Image = My.Computer.Clipboard.GetImage()
		fr.VS.DrawPicture(Picture1.Image, "40mm", "100mm")
		
		fr.VS.EndDoc()
		fr.Show()
	End Sub
	
	'Public Function CabecaRel()
	' Call Cabecalho_Relatorio(fr.VS, "Débitos Vencidos por Classe", "Data Base: " & Trim(tDataBase.Value), "2", False, "")
	' Call LineFeed(fr.VS, 1)
	'End Function
	
	Private Sub LigDes(ByRef abModo As Boolean)
		chkTrans.Enabled = abModo
		lblDatabase.Enabled = abModo
		tDataBase.Enabled = abModo
		cmdAnalisar.Enabled = abModo
		grid.Enabled = abModo
		cmdRelatorio.Enabled = abModo
		cmdFechar.Enabled = abModo
		System.Windows.Forms.Application.DoEvents()
	End Sub
End Class