Option Strict Off
Option Explicit On
Friend Class FrmRelPDD
	Inherits System.Windows.Forms.Form
	
	Private Structure totClas
		Dim intClas As Short
		Dim dblValor As Double
	End Structure
	
	Private fr As New FrmRelatorio
	Private dblTotal As Double
	Private lngContador As Integer
	Private lngClasse As Integer
	Private strClasseCabec As String
	Private vtClasse(10) As totClas
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdOK_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOK.Click
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		cmdOK.Enabled = False
		cmdFechar.Enabled = False
		Call RelPDD()
		cmdOK.Enabled = True
		cmdFechar.Enabled = True
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Sub FrmRelPDD_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		dtpBase._Value = Now
	End Sub
	
	Private Sub RelPDD()
		Dim strSql As String
		Dim strSql1 As String
		Dim strSql2 As String
		Dim strSql3 As String
		Dim strClasse As String
		Dim strZona As String
		Dim strLivro As String
		Dim strConta As String
		Dim strNome As String
		Dim dtVencto As Date
		Dim dblValor As Double
		Dim lngDias As Integer
		Dim Ilum As Boolean
		Dim intAux As Short
		Dim rsConsulta As New ADODB.Recordset
		
		strClasseCabec = ""
		
		strSql1 = "SELECT nf.codclas classe, nf.numnf, nf.mesano, nf.zona, nf.livro,  nf.conta, " & "       u.nome nome, nf.datavenc, DATEDIFF('" & VB6.Format(dtpBase.value, "yyyy-MM-dd") & "',nf.datavenc) dias, nf.valortot " & "  FROM nf nf, unida u"
		strSql3 = "SELECT nf.codclas classe, nf.numnf, nf.mesano, nf.zona, nf.livro,  nf.conta, " & "       u.nome nome, nf.datavenc, DATEDIFF('" & VB6.Format(dtpBase.value, "yyyy-MM-dd") & "',nf.datavenc) dias, nf.valortot " & "  FROM nf nf, unidb u"
		
		strSql2 = " WHERE nf.zona=u.zona " & "   AND nf.livro = u.livro " & "   AND nf.conta = u.conta " & "   AND (nf.datapag = '0000-00-00' OR nf.datapag >= '" & VB6.Format(dtpBase.value, "yyyy-MM-dd") & "') " & "   AND NOT EXISTS(SELECT * FROM reserva " & "                   WHERE reserva.numnf = nf.numnf " & "                     AND reserva.mesano = nf.mesano " & "                     AND reserva.zona = nf.zona " & "                     AND reserva.livro = nf.livro " & "                     AND reserva.conta = nf.conta " & "                     AND reserva.datares <= '" & VB6.Format(dtpBase.value, "yyyy-MM-dd") & "') " & "   AND to_days(nf.datavenc) <= to_days('" & VB6.Format(dtpBase.value, "yyyy-MM-dd") & "') "
		
		If chkJudice.CheckState Then
			strSql2 = strSql2 & " AND trim(nf.flgst) in('R','J') "
		Else
			strSql2 = strSql2 & " AND trim(nf.flgst) in('R') "
		End If
		
		strSql = strSql1 & strSql2 & " UNION " & strSql3 & strSql2 & " ORDER BY MID(classe,1,1) ASC, datavenc ASC"
		Call Conectar(strSql, rsConsulta)
		
		If rsConsulta.RecordCount > 0 Then
			fr.quem_chama = Me
			fr.VS.Font = VB6.FontChangeName(fr.VS.Font, "Arial")
			fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 9)
			fr.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			fr.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
			fr.VS.ShowGuides = VSVIEW6.ShowGuideSettings.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()
			fr.LineFeed((1))
			fr.VS.Table = "<25mm|<90mm|^30mm|>20mm|>30mm;" & "Identificação|Nome|Vencimento|Dias|Valor(R$)"
			fr.LineFeed((1))
			
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, rsConsulta.RecordCount)
			
			'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"'
			strClasse = IIf(IsDbNull(rsConsulta.Fields("classe").Value), "", rsConsulta.Fields("classe").Value)
			Call QuebraClasse(CShort(Mid(strClasse, 1, 1)), True)
			
			For intAux = 1 To 10
				vtClasse(intAux).intClas = 0
				vtClasse(intAux).dblValor = 0
			Next intAux
			
			lngClasse = 0
			lngContador = 0
			dblTotal = 0
			Ilum = False
			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"'
				strClasse = IIf(IsDbNull(rsConsulta.Fields("classe").Value), "", rsConsulta.Fields("classe").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"'
				strZona = IIf(IsDbNull(rsConsulta.Fields("Zona").Value), "", rsConsulta.Fields("Zona").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"'
				strLivro = IIf(IsDbNull(rsConsulta.Fields("Livro").Value), "", rsConsulta.Fields("Livro").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"'
				strConta = IIf(IsDbNull(rsConsulta.Fields("Conta").Value), "", rsConsulta.Fields("Conta").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"'
				strNome = IIf(IsDbNull(rsConsulta.Fields("nome").Value), "", rsConsulta.Fields("nome").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"'
				dtVencto = IIf(IsDbNull(rsConsulta.Fields("datavenc").Value), "", rsConsulta.Fields("datavenc").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("ValorTot").Value), "", rsConsulta.Fields("ValorTot").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"'
				lngDias = IIf(IsDbNull(rsConsulta.Fields("dias").Value), "", rsConsulta.Fields("dias").Value)
				If Not lngClasse = CDbl(Mid(strClasse, 1, 1)) Then
					Call QuebraClasse(CShort(Mid(strClasse, 1, 1)))
					lngClasse = CInt(Mid(strClasse, 1, 1))
				End If
				
				If Mid(strClasse, 1, 2) = "64" And Ilum = False Then
					Call QuebraClasse(64)
					Ilum = True
				End If
				
				fr.VS.Table = "<25mm|<90mm|^30mm|>20mm|>30mm;" & strZona & " " & strLivro & " " & strConta & "|" & strNome & "|" & VB6.Format(dtVencto, "dd/mm/yyyy") & "|" & lngDias & "|" & VB6.Format(dblValor, "Standard")
				
				lngContador = lngContador + 1
				dblTotal = dblTotal + dblValor
				
				rsConsulta.MoveNext()
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				System.Windows.Forms.Application.DoEvents()
			End While
			Call QuebraClasse(100)
			
			fr.VS.EndDoc()
			fr.Show()
		Else
			Call MsgBox("Não há consumidores para esta data base!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
		End If
		If rsConsulta.State = 1 Then Call rsConsulta.Close()
	End Sub
	
	Private Sub QuebraClasse(ByRef aintClas As Short, Optional ByRef bPrimeiraPagina As Boolean = False)
		
		If lngClasse > 0 Then
			If bPrimeiraPagina = True Then
				GoTo TitCabec
			End If
			If lngClasse = 1 Or lngClasse = 2 And (aintClas = 2 Or aintClas = 3) Then
				Exit Sub
			End If
			'UPGRADE_ISSUE: VBControlExtender method fr.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("7mm", fr.VS.CurrentY + 30, "202mm", fr.VS.CurrentY + 30)
			'UPGRADE_ISSUE: VBControlExtender method fr.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 + 50
			fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, True)
			fr.VS.Table = "<35mm|<50mm|>10mm|<35mm|<40mm|>25mm;" & "|Total de Faturas em Reserva: |" & lngContador & "||Valor Total das Faturas: |" & VB6.Format(dblTotal, "Standard")
			fr.VS.Font = VB6.FontChangeBold(fr.VS.Font, False)
			If lngClasse = 64 Then
				vtClasse(8).intClas = 64
				vtClasse(8).dblValor = dblTotal
			Else
				vtClasse(lngClasse).intClas = lngClasse
				vtClasse(lngClasse).dblValor = dblTotal
			End If
			lngContador = 0
			dblTotal = 0
			If aintClas = 100 Then
				strClasseCabec = "RESUMO DE TOTAIS APURADOS"
				fr.VS.NewPage()
				fr.LineFeed((1))
				fr.VS.LineSpacing = 3
				fr.VS.Font = VB6.FontChangeSize(fr.VS.Font, 11)
				fr.VS.Table = "<50mm|<50mm|>30mm;|Classe|Valor(R$)"
				'UPGRADE_ISSUE: VBControlExtender method fr.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("57mm", fr.VS.CurrentY + 30, "137mm", fr.VS.CurrentY + 30)
				'UPGRADE_ISSUE: VBControlExtender method fr.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 + 50
				If vtClasse(9).dblValor > 0 Then
					fr.VS.Table = "<50mm|<50mm|>30mm;|Residencial|" & VB6.Format(vtClasse(9).dblValor, "Standard")
					dblTotal = dblTotal + vtClasse(9).dblValor
				End If
				If vtClasse(4).dblValor > 0 Then
					fr.VS.Table = "<50mm|<50mm|>30mm;|Comercial|" & VB6.Format(vtClasse(4).dblValor, "Standard")
					dblTotal = dblTotal + vtClasse(4).dblValor
				End If
				If (vtClasse(1).dblValor + vtClasse(2).dblValor + vtClasse(3).dblValor) > 0 Then
					fr.VS.Table = "<50mm|<50mm|>30mm;|Industrial|" & VB6.Format(vtClasse(1).dblValor + vtClasse(2).dblValor + vtClasse(3).dblValor, "Standard")
					dblTotal = dblTotal + vtClasse(1).dblValor + vtClasse(2).dblValor + vtClasse(3).dblValor
				End If
				If vtClasse(5).dblValor > 0 Then
					fr.VS.Table = "<50mm|<50mm|>30mm;|Rural|" & VB6.Format(vtClasse(5).dblValor, "Standard")
					dblTotal = dblTotal + vtClasse(5).dblValor
				End If
				If vtClasse(6).dblValor > 0 Then
					fr.VS.Table = "<50mm|<50mm|>30mm;|P. Publico|" & VB6.Format(vtClasse(6).dblValor, "Standard")
					dblTotal = dblTotal + vtClasse(6).dblValor
				End If
				If vtClasse(7).dblValor > 0 Then
					fr.VS.Table = "<50mm|<50mm|>30mm;|S. Publico|" & VB6.Format(vtClasse(7).dblValor, "Standard")
					dblTotal = dblTotal + vtClasse(7).dblValor
				End If
				If vtClasse(8).dblValor > 0 Then 'ILUM PUBLICA
					fr.VS.Table = "<50mm|<50mm|>30mm;|I. Publica|" & VB6.Format(vtClasse(8).dblValor, "Standard")
					dblTotal = dblTotal + vtClasse(8).dblValor
				End If
				'UPGRADE_ISSUE: VBControlExtender method fr.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("57mm", fr.VS.CurrentY + 30, "137mm", fr.VS.CurrentY + 30)
				'UPGRADE_ISSUE: VBControlExtender method fr.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 + 50
				fr.VS.Table = "<50mm|<50mm|>30mm;|TOTAL| " & VB6.Format(dblTotal, "Standard")
				Exit Sub
			End If
TitCabec: 
			
			Select Case aintClas
				Case 1, 2, 3
					strClasseCabec = "INDUSTRIAIS VENCIDOS A MAIS DE 360 DIAS"
				Case 4
					strClasseCabec = "COMERCIAIS VENCIDOS A MAIS DE 180 DIAS"
				Case 5
					strClasseCabec = "RURAIS VENCIDOS A MAIS DE 90 DIAS"
				Case 6
					strClasseCabec = "PODERES PUBLICOS VENCIDOS A MAIS DE 360 DIAS"
				Case 64
					strClasseCabec = "ILUMINAÇÃO PUBLICA VENCIDOS A MAIS DE 360 DIAS"
				Case 7
					strClasseCabec = "SERVIÇOS PUBLICOS VENCIDOS A MAIS DE 360 DIAS"
				Case 9
					strClasseCabec = "RESIDENCIAIS VENCIDOS A MAIS DE 90 DIAS"
			End Select
			If bPrimeiraPagina = True Then
				Exit Sub
			End If
			fr.VS.NewPage()
			
			fr.LineFeed((1))
			fr.VS.Table = "<25mm|<90mm|^30mm|>20mm|>30mm;" & "Identificação|Nome|Dt Vencto|Dias|Valor"
			fr.LineFeed((1))
		End If
	End Sub
	
	Public Function CabecaRel() As Object
		Call Cabecalho_Relatorio((fr.VS), "Relatório Provisão de Devedores Duvidosos - PDD (Reserva)", "Data Base: " & dtpBase.value, "4", True, strClasseCabec)
		Call LineFeed((fr.VS), 1)
	End Function
End Class