Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelConvenios
	Inherits System.Windows.Forms.Form
	
	'UPGRADE_WARNING: Event chkReserva.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 chkReserva_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkReserva.CheckStateChanged
		If Me.chkReserva.CheckState = System.Windows.Forms.CheckState.Checked Then
			Me.chkArrecadado.CheckState = System.Windows.Forms.CheckState.Checked
			Me.chkArrecadado.Enabled = False
			Me.chkArrecadar.CheckState = System.Windows.Forms.CheckState.Unchecked
			Me.chkArrecadar.Enabled = False
		Else
			Me.chkArrecadado.Enabled = True
			Me.chkArrecadar.Enabled = True
		End If
	End Sub
	
	Private Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
		Dim fr As New FrmRelatorio
		Dim strsql As String
		Dim strCabec As String
		Dim strCabec2 As String
		Dim strCodConv As String
		Dim strNomConv As String
		Dim dtBase As Date
		
		dtBase = CDate(UltimoDiaMes((dtpFinal.Month), (dtpFinal.Year)) & "/" & dtpFinal.Month & "/" & dtpFinal.Year)
		
		
		If chkArrecadado.CheckState Or chkArrecadar.CheckState Then
			
			strCodConv = Mid(Me.cmbConv.Text, 1, 10)
			strNomConv = Mid(Me.cmbConv.Text, 14)
			
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Call LigDes(False)
			
			fr.quem_chama = Me
			fr.VS.Font = VB6.FontChangeName(fr.VS.Font, "Arial")
			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.StartDoc()
			
			If chkArrecadado.CheckState Then
				'Arrecadado no período Informado
				strsql = "   AND f.datapag >= '" & VB6.Format(dtpInicial.value, "yyyy-MM-dd") & "' AND f.datapag <= '" & VB6.Format(dtpFinal.value, "yyyy-MM-dd") & "' "
				strsql = strsql & "   AND f.datapag <> '0000-00-00' "
				If Me.chkReserva.CheckState = System.Windows.Forms.CheckState.Unchecked Then
					strCabec = "Bx.Reserva (" & strNomConv & ")"
					strsql = strsql & "   AND f.flgst = 'P'"
				Else
					strCabec = "Arrecadação (" & strNomConv & ")"
					strsql = strsql & "   AND f.flgst = 'R'"
				End If
				strCabec2 = "Período de " & VB6.Format(dtpInicial.value, "dd/mm/yyyy") & " a " & VB6.Format(dtpFinal.value, "dd/mm/yyyy")
				Call RelConvenios((fr.VS), strsql, strCabec, 0, strCodConv, strCabec2)
			End If
			
			If chkArrecadar.CheckState Then
				If chkArrecadado.CheckState Then
					fr.VS.NewPage()
					fr.VS.CurrentLine = 1
				End If
				'Em Aberto até a data final informada
				'      strsql = "   AND f.mesano_ref <= '" & Format(dtBase, "yyyymm") & "' " _
				''             & "   AND (f.datapag > '" & Format(dtBase, "yyyy-MM-dd") & "' OR f.datapag = '0000-00-00' )" _
				''             & "   AND f.flgst <> 'C' "
				
				strsql = "   AND f.datavenc >= """ & VB6.Format(dtpInicial.value, "yyyy-MM-dd") & """ AND f.datavenc <= """ & VB6.Format(dtpFinal.value, "yyyy-MM-dd") & """ " & "   AND (datapag = ""0000-00-00"" or datapag is null) AND flgst NOT IN('C','R','P') "
				
				strCabec = "Saldo a Arrecadar (" & strNomConv & ") "
				strCabec2 = "No dia " & VB6.Format(dtBase, "dd/mm/yyyy")
				Call RelConvenios((fr.VS), strsql, strCabec, 2, strCodConv, strCabec2)
			End If
			
			fr.VS.EndDoc()
			fr.Show()
			
			Call LigDes(True)
			
			Me.Cursor = System.Windows.Forms.Cursors.Default
		End If
	End Sub
	
	Private Sub frmRelConvenios_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		dtpInicial.value = Today
		dtpFinal.value = Today
		Call encheComboConvenios()
	End Sub
	
	Private Sub encheComboConvenios()
		Dim strsql As String
		Dim rsConsulta As New ADODB.Recordset
		
		strsql = "SELECT * " & "FROM codigos " & "WHERE num LIKE '9030%' " & "AND num <> '9030000000' " & "ORDER BY num "
		
		Call Conectar(strsql, rsConsulta)
		
		While Not rsConsulta.EOF
			Me.cmbConv.Items.Add(rsConsulta.Fields("Num").Value & " - " & rsConsulta.Fields("descri").Value)
			rsConsulta.MoveNext()
		End While
		
		If Me.cmbConv.Items.Count > 0 Then
			Me.cmbConv.Text = VB6.GetItemString(Me.cmbConv, 0)
		End If
	End Sub
	
	Private Sub RelConvenios(ByRef aVs As AxVSVIEW6.AxVSPrinter, ByRef astrSql As String, ByRef astrCabec As String, ByRef aintTipo As Short, ByRef astrCodConv As String, ByRef astrCabec2 As String)
		Dim strTemp As String
		Dim strTamCol As String
		Dim rsConsulta As ADODB.Recordset
		Dim rsUnidade As ADODB.Recordset
		Dim strZLC As String
		Dim strNome As String
		Dim strEnde As String
		Dim lstrGrTar As String
		Dim lngQtde As Integer
		Dim lngConsId As Integer
		Dim lngUnidId As Integer
		Dim dblValor As Double
		Dim lngTQtd As Integer
		Dim dblTVlr As Double
		Dim strsql As String
		Dim strSql3 As String
		rsConsulta = New ADODB.Recordset
		rsUnidade = New ADODB.Recordset
		
		Call Cabecalho_Relatorio(aVs, astrCabec, astrCabec2, "2")
		Call LineFeed(aVs, 1)
		
		aVs.Font = VB6.FontChangeBold(aVs.Font, True)
		aVs.BrushColor = System.Drawing.Color.White
		aVs.Font = VB6.FontChangeSize(aVs.Font, 8)
		strTamCol = "<5mm|<22mm|<80mm|<65mm|>20mm;"
		strTemp = "|IDENTIF.|NOME|ENDEREÇO|VALOR"
		aVs.Table = strTamCol & strTemp
		aVs.Font = VB6.FontChangeBold(aVs.Font, False)
		Call LineFeed(aVs, 1)
		
		
		strsql = "SELECT f.zona, f.livro, f.conta, f.numnf, f.mesano, f.datapag, f.itensserv, f.ta, f.cons_id, f.unid_id " & "  FROM nf f " & " WHERE itensserv like ""%" & astrCodConv & "%"""
		
		Call Conectar(strsql & astrSql, rsConsulta)
		If rsConsulta.RecordCount > 0 Then
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, rsConsulta.RecordCount, " " & VB.Left(astrCabec, InStr(1, astrCabec, "(") - 1))
			lngQtde = rsConsulta.RecordCount
			While Not rsConsulta.EOF
				dblValor = BuscaVlrItem(rsConsulta.Fields("itensserv").Value, astrCodConv)
				strZLC = rsConsulta.Fields("Zona").Value & "." & rsConsulta.Fields("Livro").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"'
				lstrGrTar = LCase(Mid(IIf(IsDbNull(rsConsulta.Fields("ta").Value), "01B", rsConsulta.Fields("ta").Value), 3, 1))
				'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"'
				lngConsId = IIf(IsDbNull(rsConsulta.Fields("cons_id").Value), 0, rsConsulta.Fields("cons_id").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"'
				lngUnidId = IIf(IsDbNull(rsConsulta.Fields("unid_id").Value), 0, rsConsulta.Fields("unid_id").Value)
				strsql = "SELECT nome, end " & "  FROM unid" & lstrGrTar & " " & " WHERE cons_id = " & lngConsId & "   AND unid_id = " & lngUnidId & "  LIMIT 1 "
				Call Conectar(strsql, rsUnidade)
				If rsUnidade.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"'
					strNome = VB.Left(IIf(IsDbNull(rsUnidade.Fields("nome").Value), "NOME INDEFINIDO", rsUnidade.Fields("nome").Value), 50)
					'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"'
					strEnde = VB.Left(IIf(IsDbNull(rsUnidade.Fields("End").Value), "ENDERECO INDEFINIDO", rsUnidade.Fields("End").Value), 30)
				Else
					If rsUnidade.State = 1 Then Call rsUnidade.Close()
					strsql = "SELECT nome, end " & "  FROM consum " & " WHERE consreg = " & lngConsId & "  LIMIT 1 "
					Call Conectar(strsql, rsUnidade)
					If rsUnidade.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"'
						strNome = VB.Left(IIf(IsDbNull(rsUnidade.Fields("nome").Value), "NOME INDEFINIDO", rsUnidade.Fields("nome").Value), 50)
						'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"'
						strEnde = VB.Left(IIf(IsDbNull(rsUnidade.Fields("End").Value), "ENDERECO INDEFINIDO", rsUnidade.Fields("End").Value), 30)
					Else
						strNome = "NOME INDEFINIDO"
						strEnde = "ENDERECO INDEFINIDO"
					End If
				End If
				If rsUnidade.State = 1 Then Call rsUnidade.Close()
				'dblValor = IIf(IsNull(rsConsulta!valor_item), 0, rsConsulta!valor_item)
				
				dblTVlr = dblTVlr + dblValor
				strTemp = "|" & strZLC & "|" & strNome & "|" & strEnde & "|" & VB6.Format(dblValor, "standard")
				aVs.Table = strTamCol & strTemp
				If aVs.CurrentLine > 350 Then
					aVs.NewPage()
					aVs.CurrentLine = 0
					Call Cabecalho_Relatorio(aVs, astrCabec, astrCabec2, "2")
					Call LineFeed(aVs, 1)
					aVs.Font = VB6.FontChangeBold(aVs.Font, True)
					aVs.BrushColor = System.Drawing.Color.White
					aVs.Font = VB6.FontChangeSize(aVs.Font, 8)
					strTemp = "|IDENTIF.|NOME|ENDEREÇO|VALOR"
					aVs.Table = strTamCol & strTemp
					aVs.Font = VB6.FontChangeBold(aVs.Font, False)
					Call LineFeed(aVs, 1)
				End If
				rsConsulta.MoveNext()
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " " & VB.Left(astrCabec, InStr(1, astrCabec, "(") - 1))
				System.Windows.Forms.Application.DoEvents()
			End While
		End If
		Call LineFeed(aVs, 1)
		aVs.Font = VB6.FontChangeBold(aVs.Font, True)
		strTemp = "|" & "TOTAL" & "|" & lngQtde & "||" & VB6.Format(dblTVlr, "standard")
		aVs.Table = strTamCol & strTemp
		aVs.Font = VB6.FontChangeBold(aVs.Font, False)
		
		
		If rsConsulta.State Then rsConsulta.Close()
		'UPGRADE_NOTE: Object rsConsulta 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"'
		rsConsulta = Nothing
	End Sub
	
	Private Sub LigDes(ByRef abModo As Boolean)
		Label1(0).Enabled = abModo
		Label1(1).Enabled = abModo
		Label1(2).Enabled = abModo
		dtpInicial.Enabled = abModo
		dtpFinal.Enabled = abModo
		FramePeriodo.Enabled = abModo
		cmdEmitir.Enabled = abModo
		cmdFechar.Enabled = abModo
		Me.cmbConv.Enabled = abModo
		chkReserva.Enabled = abModo
		If Me.chkReserva.CheckState = System.Windows.Forms.CheckState.Checked Then
			Me.chkArrecadado.Enabled = False
			Me.chkArrecadar.Enabled = False
		Else
			Me.chkArrecadado.Enabled = abModo
			Me.chkArrecadar.Enabled = abModo
		End If
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Me.Close()
	End Sub
End Class