Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmDesFaturamento
	Inherits System.Windows.Forms.Form
	Private ifrmBarraProgresso As frmBarraProgresso
	
	Private Sub cmdBuscaIndi_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscaIndi.Click
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim llngUid As Integer
		Dim lstrTar As String
		Dim lstrGru As String
		Dim lstrNom As String
		Dim ldtePag As Date
		Dim ldblNNf As Double
		Dim llngMAn As Integer
		Dim lblnCor As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		Me.txtZona.Text = ""
		Me.txtLivro.Text = ""
		Me.txtConta.Text = ""
		Me.txtNome.Text = ""
		Me.txtTA.Text = ""
		If IsNumeric(Me.txtNumNf.Text) Then
			ldblNNf = CDbl(Me.txtNumNf.Text)
			llngMAn = CInt(VB.Right(Me.txtMesAno.Text, 4) & VB.Left(Me.txtMesAno.Text, 2))
			llngUid = 0
			lstrTar = ""
			lstrGru = ""
			lstrZon = ""
			lstrLiv = ""
			lstrCon = ""
			ldtePag = CDate("01/01/1951")
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nf`.`unid_id`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`ta`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`grupo`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`zona`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`livro`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`conta`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`datapag`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`flgst`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano_ref`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano`, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`numnf` " & vbCrLf
			lstrSql = lstrSql & "  FROM `nf` " & vbCrLf
			lstrSql = lstrSql & " WHERE `nf`.`mesano_ref` = " & llngMAn & vbCrLf
			lstrSql = lstrSql & "   AND `nf`.`numnf`      = " & ldblNNf
			lstrSql = lstrSql & " LIMIT 0, 1"
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .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"'
						llngUid = CInt(IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_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"'
						lstrTar = Mid(Trim(IIf(IsDbNull(.Fields("ta").value), "01B1", .Fields("ta").value)), 3, 2)
						'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"'
						lstrGru = Trim(IIf(IsDbNull(.Fields("grupo").value), "B", .Fields("grupo").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"'
						lstrZon = VB6.Format(Trim(IIf(IsDbNull(.Fields("zona").value), "0", .Fields("zona").value)), "000")
						'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"'
						lstrLiv = VB6.Format(Trim(IIf(IsDbNull(.Fields("livro").value), "0", .Fields("livro").value)), "00")
						'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"'
						lstrCon = VB6.Format(Trim(IIf(IsDbNull(.Fields("conta").value), "0", .Fields("conta").value)), "000000")
						'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"'
						ldtePag = CDate(IIf(IsDbNull(.Fields("datapag").value) Or .Fields("datapag").value = "00/00/0000", "01/01/1951", .Fields("datapag").value))
					End If
					Call .Close()
				End If
				If Len(Trim(lstrGru)) > 0 Then
					lstrSql = ""
					lstrSql = lstrSql & "SELECT `nome` "
					lstrSql = lstrSql & "  FROM `unid" & LCase(lstrGru) & "` "
					lstrSql = lstrSql & " WHERE `unid_id` = " & llngUid
					Call Conectar(lstrSql, lrstAux)
					If .State = ADODB.ObjectStateEnum.adStateOpen Then
						If .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"'
							lstrNom = IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)
						Else
							lstrNom = ""
						End If
						Call .Close()
					End If
				Else
					lstrNom = ""
				End If
				If llngUid = 0 Then
					Call MsgBox("Esta fatura não existe na arrecadação!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
				Me.txtZona.Text = lstrZon
				Me.txtLivro.Text = lstrLiv
				Me.txtConta.Text = lstrCon
				Me.txtNome.Text = lstrNom
				Me.txtTA.Text = lstrTar
				If ldtePag <> CDate("01/01/1951") Then
					Call MsgBox("Esta Fatura já foi paga, não pode ser faturada!", MsgBoxStyle.Information, gstrTituloMensagem)
					Me.cmdProcessar.Enabled = False
				Else
					lblnCor = VerExisteCorte
					If Not lblnCor Then
						Call MsgBox("Esta fatura já foi Cortada/Paga e não pode ser processada!", MsgBoxStyle.Information, gstrTituloMensagem)
						Me.cmdProcessar.Enabled = False
					Else
						Me.cmdProcessar.Enabled = True
					End If
				End If
			End With
		End If
		'UPGRADE_NOTE: Object lrstAux 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"'
		lrstAux = Nothing
	End Sub
	
	Private Sub cmdBuscaRota_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscaRota.Click
		Call CarregaRotas()
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim lstrSql As String
		Dim lstrAux As String
		Dim lintIdx As Short
		Dim ldblNNf As Double
		Dim llngMAn As Integer
		Dim lblnAux As Boolean
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lblnAux = False
		If Me.opbIndividual.Checked Then
			If IsNumeric(Me.txtNumNf.Text) Then
				ldblNNf = CDbl(Me.txtNumNf.Text)
				llngMAn = CInt(VB.Right(Me.txtMesAno.Text, 4) & VB.Left(Me.txtMesAno.Text, 2))
				If TransfereFatura(ldblNNf, llngMAn) Then
					Call MsgBox("Fatura " & Me.txtNumNf.Text & " do mês e ano " & Me.txtMesAno.Text & " transferida para faturamento com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
			Else
				Call MsgBox("Por favor, informar um númeração de fatura válida!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call Me.txtNumNf.Focus()
			End If
		ElseIf Me.opbRota.Checked Then 
			llngMAn = CInt(VB6.Format(Me.dtpMesAno.value, "yyyymm"))
			lstrAux = ""
			With Me.lsvRotas
				If .Items.Count > 0 Then
					'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					If .Items.Item(1).Checked = False Then
						For lintIdx = 2 To .Items.Count
							'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
							If .Items.Item(lintIdx).Checked = True Then
								'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
								lstrAux = lstrAux & "'" & .Items.Item(lintIdx).Text & "',"
							End If
						Next lintIdx
					End If
				End If
			End With
			If Len(Trim(lstrAux)) > 0 Then
				lstrAux = " AND MID(`nf`.`rota`, 1, 4) IN ( " & VB.Left(lstrAux, Len(lstrAux) - 1) & ") "
			End If
			Call ifrmBarraProgresso.SetaBarraProgresso(Me)
			ifrmBarraProgresso.Show()
			ifrmBarraProgresso.lblInfo.Text = " Aguarde buscando as faturas!"
			Call ifrmBarraProgresso.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nf`.`numnf`,`nf`.`mesano_ref`,`nf`.`mesano` "
			lstrSql = lstrSql & "  FROM `nf` "
			lstrSql = lstrSql & " WHERE `nf`.`mesano_ref` = " & VB6.Format(Me.dtpMesAno.value, "yyyymm")
			lstrSql = lstrSql & "   AND `nf`.`flgst` = '' "
			lstrSql = lstrSql & lstrAux
			If Me.chkSomenteMicroGeracao.CheckState = System.Windows.Forms.CheckState.Checked Then
				lstrSql = lstrSql & "   AND (`nf`.`micro_geracao` = 1 OR `nf`.`micro_geracao_distribuida` = 1) "
			End If
			If Me.chkSomenteBaixaRenda.CheckState = System.Windows.Forms.CheckState.Checked Then
				lstrSql = lstrSql & "   AND (`nf`.`codclas` BETWEEN '9102' AND '9106') "
			End If
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						Call BarraProgresso((ifrmBarraProgresso.ProgressBar), (ifrmBarraProgresso.lblPerc), (ifrmBarraProgresso.lblInfo), 0, .RecordCount)
						While Not .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"'
							ldblNNf = CDbl(IIf(IsDbNull(.Fields("numnf").value), 0, .Fields("numnf").value))
							lblnAux = TransfereFatura(ldblNNf, llngMAn)
							If lblnAux = False Then
								Call .MoveLast()
							End If
							Call .MoveNext()
							Call BarraProgresso((ifrmBarraProgresso.ProgressBar), (ifrmBarraProgresso.lblPerc), (ifrmBarraProgresso.lblInfo), 1)
						End While
						Call ifrmBarraProgresso.Close()
					Else
						Call MsgBox("Não foi encontrado nenhuma fatura para transferência, favor mude os filtros!", MsgBoxStyle.Information, gstrTituloMensagem)
					End If
					Call .Close()
				End If
			End With
			If lblnAux Then
				Call MsgBox("Transferência para em faturamento realizada com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		ElseIf Me.opbAgrupa.Checked Then 
			llngMAn = CInt(VB6.Format(Me.dtpMesAno.value, "yyyymm"))
			lstrAux = ""
			lstrAux = MontaFiltroAgrupa
			Call ifrmBarraProgresso.SetaBarraProgresso(Me)
			ifrmBarraProgresso.Show()
			ifrmBarraProgresso.lblInfo.Text = " Aguarde buscando as faturas!"
			Call ifrmBarraProgresso.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `nf`.`numnf`,`nf`.`mesano_ref`,`nf`.`mesano` "
			lstrSql = lstrSql & "  FROM `nf` "
			lstrSql = lstrSql & " WHERE `nf`.`mesano_ref` = " & VB6.Format(Me.dtpMesAnoAgru.value, "yyyymm")
			lstrSql = lstrSql & "   AND `nf`.`flgst` = '' "
			lstrSql = lstrSql & lstrAux
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						Call BarraProgresso((ifrmBarraProgresso.ProgressBar), (ifrmBarraProgresso.lblPerc), (ifrmBarraProgresso.lblInfo), 0, .RecordCount)
						While Not .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"'
							ldblNNf = CDbl(IIf(IsDbNull(.Fields("numnf").value), 0, .Fields("numnf").value))
							lblnAux = TransfereFatura(ldblNNf, llngMAn)
							If lblnAux = False Then
								Call .MoveLast()
							End If
							Call .MoveNext()
							Call BarraProgresso((ifrmBarraProgresso.ProgressBar), (ifrmBarraProgresso.lblPerc), (ifrmBarraProgresso.lblInfo), 1)
						End While
						Call ifrmBarraProgresso.Close()
					Else
						Call MsgBox("Não foi encontrado nenhuma fatura para transferência, favor mude os filtros!", MsgBoxStyle.Information, gstrTituloMensagem)
					End If
					Call .Close()
				End If
			End With
			If lblnAux Then
				Call MsgBox("Transferência para em faturamento realizada com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		End If
		'UPGRADE_NOTE: Object lrstAux 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"'
		lrstAux = Nothing
	End Sub
	
	Private Function TransfereFatura(ByVal adblNumNf As Double, ByVal alngMesAno As Integer) As Boolean
		Dim lblnAux As Boolean
		Dim lstrSql As String
		On Error GoTo ErroTransfereFatura
		lblnAux = False
		If VerExisteFatura("nftransicao", adblNumNf, alngMesAno) Then
			If VerExisteFatura("nf", adblNumNf, alngMesAno) Then
				lstrSql = "DELETE FROM nftransicao WHERE mesano_ref = " & alngMesAno & " AND numnf = " & adblNumNf
				Call Executar(lstrSql)
				lstrSql = "INSERT INTO nftransicao (SELECT * FROM nf WHERE mesano_ref = " & alngMesAno & " AND numnf = " & adblNumNf & ")"
				Call Executar(lstrSql)
				lstrSql = "DELETE FROM nf WHERE mesano_ref = " & alngMesAno & " AND numnf = " & adblNumNf
				Call Executar(lstrSql)
				lblnAux = True
			End If
		Else
			If VerExisteFatura("nf", adblNumNf, alngMesAno) Then
				lstrSql = "INSERT INTO nftransicao (SELECT * FROM nf WHERE mesano_ref = " & alngMesAno & " AND numnf = " & adblNumNf & ")"
				Call Executar(lstrSql)
				lstrSql = "DELETE FROM nf WHERE mesano_ref = " & alngMesAno & " AND numnf = " & adblNumNf
				Call Executar(lstrSql)
				lblnAux = True
			End If
		End If
ErroTransfereFatura: 
		If Err.Number > 0 Then
			lblnAux = False
		End If
		TransfereFatura = lblnAux
	End Function
	
	Private Function VerExisteFatura(ByVal astrTabela As String, ByVal adblNumNf As Double, ByVal alngMesAno As Integer) As Boolean
		Dim lblnAux As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lblnAux = False
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM `" & astrTabela & "` "
		lstrSql = lstrSql & " WHERE `numnf`      = " & adblNumNf
		lstrSql = lstrSql & "   AND `mesano_ref` = " & alngMesAno
		lstrSql = lstrSql & "   AND `flgst` = '' "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				lblnAux = .RecordCount > 0
				Call .Close()
			End If
		End With
		VerExisteFatura = lblnAux
		'UPGRADE_NOTE: Object lrstAux 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"'
		lrstAux = Nothing
	End Function
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub frmDesFaturamento_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			Call cmdFechar_Click(cmdFechar, New System.EventArgs())
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub frmDesFaturamento_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrMesAno As String
		Call CenterForm(Me)
		lstrMesAno = Trim(LeConfig("MESANOFATURAMENTO"))
		If lstrMesAno = "" Or lstrMesAno = "-1" Then
			Call MsgBox("Não foi definido o mês e ano de faturamento!", MsgBoxStyle.Information, gstrTituloMensagem)
			Call Me.Close()
		End If
		Me.txtMesAno.Text = lstrMesAno
		Me.dtpMesAno.value = BuscaDataBD
		'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpMesAno.value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		Me.dtpMesAnoAgru.value = Me.dtpMesAno.value
		Call LigDesCampos(Me.opbIndividual.Checked, Me.opbRota.Checked, Me.opbAgrupa.Checked)
		Me.cmdProcessar.Enabled = False
		ifrmBarraProgresso = New frmBarraProgresso
		Call CarregaAgrupamento()
	End Sub
	
	Private Sub lsvRotas_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lsvRotas.ItemCheck
		Dim aListItem As System.Windows.Forms.ListViewItem = lsvRotas.Items(eventArgs.Index)
		Dim lintIdx As Short
		With Me.lsvRotas
			If aListItem.Text = "Todas as rotas" Then
				'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				If .Items.Item(1).Checked = True Then
					For lintIdx = 1 To .Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Items.Item(lintIdx).Checked = True
					Next lintIdx
					'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				ElseIf .Items.Item(1).Checked = False Then 
					For lintIdx = 1 To .Items.Count
						'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						.Items.Item(lintIdx).Checked = False
					Next lintIdx
				End If
			Else
				If aListItem.Checked = False Then
					'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(1).Checked = False
				End If
			End If
		End With
	End Sub
	
	'UPGRADE_WARNING: Event opbAgrupa.CheckedChanged 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 opbAgrupa_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbAgrupa.CheckedChanged
		If eventSender.Checked Then
			Call LigDesCampos(Me.opbIndividual.Checked, Me.opbRota.Checked, Me.opbAgrupa.Checked)
		End If
	End Sub
	
	'UPGRADE_WARNING: Event opbIndividual.CheckedChanged 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 opbIndividual_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbIndividual.CheckedChanged
		If eventSender.Checked Then
			Call LigDesCampos(Me.opbIndividual.Checked, Me.opbRota.Checked, Me.opbAgrupa.Checked)
		End If
	End Sub
	
	'UPGRADE_WARNING: Event opbRota.CheckedChanged 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 opbRota_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbRota.CheckedChanged
		If eventSender.Checked Then
			Call LigDesCampos(Me.opbIndividual.Checked, Me.opbRota.Checked, Me.opbAgrupa.Checked)
		End If
	End Sub
	
	Private Sub CarregaRotas()
		Dim lstrRota As String
		Dim lstrGrupo As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		' ----- Monta as colunas na ListView de rotas
		With Me.lsvRotas
			Call .Items.Clear()
			Call .Columns.Clear()
			If .Columns.Count <= 1 Then
				Call .Columns.Add("", "Rota", CInt(VB6.TwipsToPixelsX(1800)))
				Call .Columns.Add("Grupo", CInt(VB6.TwipsToPixelsX(650)), System.Windows.Forms.HorizontalAlignment.Center)
			End If
			Call .Items.Add("Todas as rotas")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add("T")
		End With
		Call ifrmBarraProgresso.SetaBarraProgresso(Me)
		ifrmBarraProgresso.Show()
		ifrmBarraProgresso.lblInfo.Text = " Aguarde buscando as rotas!"
		Call ifrmBarraProgresso.lblInfo.Refresh()
		' ----- Busca as rotas
		lstrSql = ""
		lstrSql = lstrSql & "SELECT DISTINCT LEFT(`nf`.`rota`,4) AS `rotaaux`, `grupo` "
		lstrSql = lstrSql & "  FROM `" & gstrDataBase & "`.`nf` USE INDEX(`keyrota`) "
		lstrSql = lstrSql & " WHERE `nf`.`mesano_ref` = " & VB6.Format(Me.dtpMesAno.value, "yyyymm")
		lstrSql = lstrSql & "   AND `nf`.`flgst` = '' "
		lstrSql = lstrSql & " ORDER BY 2 ASC, 1 ASC"
		Call Conectar(lstrSql, lrstAux)
		' ----- Fim do Busca as rotas
		With lrstAux
			' ----- Se Retornou alguma coisa, monta as rotas!
			If .RecordCount > 0 Then
				Call BarraProgresso((ifrmBarraProgresso.ProgressBar), (ifrmBarraProgresso.lblPerc), (ifrmBarraProgresso.lblInfo), 0, .RecordCount)
				While Not .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"'
					lstrRota = VB6.Format(IIf(IsDbNull(.Fields("rotaaux").value), "0", .Fields("rotaaux").value), "0000")
					'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"'
					lstrGrupo = IIf(IsDbNull(.Fields("grupo").value), "B", .Fields("grupo").value)
					With Me.lsvRotas
						Call .Items.Add(lstrRota)
						'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						Call .Items.Item(.Items.Count).SubItems.Add(lstrGrupo)
					End With
					Call .MoveNext()
					Call BarraProgresso((ifrmBarraProgresso.ProgressBar), (ifrmBarraProgresso.lblPerc), (ifrmBarraProgresso.lblInfo), 1)
				End While
				Call ifrmBarraProgresso.Close()
				Me.cmdProcessar.Enabled = True
			Else
				Call MsgBox("Não existem faturas na arrecadação para a(s) rota(s) selecionada(s)!", MsgBoxStyle.Information, gstrTituloMensagem)
				Me.cmdProcessar.Enabled = False
			End If
			If .State = 1 Then Call .Close()
		End With
		'UPGRADE_NOTE: Object lrstAux 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"'
		lrstAux = Nothing
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub LigDesCampos(ByVal ablnModoIndi As Boolean, ByVal ablnModoRota As Boolean, ByVal ablnModoAgru As Boolean)
		Me.fmeAux(0).Enabled = ablnModoIndi
		Me.txtNumNf.Enabled = ablnModoIndi
		Me.cmdBuscaIndi.Enabled = ablnModoIndi
		Me.fmeAux(1).Enabled = ablnModoRota
		Me.dtpMesAno.Enabled = ablnModoRota
		Me.lsvRotas.Enabled = ablnModoRota
		Me.cmdBuscaRota.Enabled = ablnModoRota
		Me.txtNumNf.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(ablnModoIndi, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
		Me.lsvRotas.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(ablnModoRota, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
		Me.chkSomenteMicroGeracao.Enabled = ablnModoRota
		Me.chkSomenteBaixaRenda.Enabled = ablnModoRota
		Me.fmeAux(2).Enabled = ablnModoAgru
		Me.lsvAgrupa.Enabled = ablnModoAgru
		Me.dtpMesAnoAgru.Enabled = ablnModoAgru
	End Sub
	
	Private Sub txtNumNf_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtNumNf.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then
			KeyAscii = 0
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Function VerExisteCorte() As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Dim lstrSit As String
		lblnRet = True
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM concorte "
		lstrSql = lstrSql & " WHERE numnf = " & CDbl(IIf(IsNumeric(Me.txtNumNf.Text), Me.txtNumNf.Text, 0)) & " "
		lstrSql = lstrSql & "   AND mesano = """ & Me.txtMesAno.Text & """ "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .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"'
					lstrSit = IIf(IsDbNull(.Fields("sit").value), "R", .Fields("sit").value)
					If lstrSit = "R" Or lstrSit = "E" Then
						Call Executar("DELETE FROM concorte WHERE mesano = """ & Me.txtMesAno.Text & """ AND numnf = " & CDbl(IIf(IsNumeric(Me.txtNumNf.Text), Me.txtNumNf.Text, 0)) & " ")
						lblnRet = True
					Else
						lblnRet = False
					End If
				Else
					lblnRet = True
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstAux 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"'
		lrstAux = Nothing
		VerExisteCorte = lblnRet
	End Function
	
	Private Sub lsvAgrupa_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lsvAgrupa.ItemCheck
		Dim aItem As System.Windows.Forms.ListViewItem = lsvAgrupa.Items(eventArgs.Index)
		Dim lintIdx As Short
		With Me.lsvAgrupa
			For lintIdx = 1 To .Items.Count
				'UPGRADE_WARNING: Lower bound of collection Me.lsvAgrupa.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				If .Items.Item(lintIdx).Checked Then
					Me.cmdProcessar.Enabled = True
					lintIdx = .Items.Count
				Else
					Me.cmdProcessar.Enabled = False
				End If
			Next lintIdx
		End With
	End Sub
	
	Private Sub CarregaAgrupamento()
		Dim lstrCod As String
		Dim lstrDes As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		' ----- Monta as colunas na ListView de rotas
		With Me.lsvAgrupa
			Call .Items.Clear()
			Call .Columns.Clear()
			If .Columns.Count <= 1 Then
				Call .Columns.Add("", "Agrupa", CInt(VB6.TwipsToPixelsX(4790)))
				Call .Columns.Add("Código", CInt(VB6.TwipsToPixelsX(700)), System.Windows.Forms.HorizontalAlignment.Right)
			End If
		End With
		' ----- Busca as rotas
		lstrSql = ""
		lstrSql = lstrSql & "SELECT `codigo`, " & vbCrLf
		lstrSql = lstrSql & "       `descricao`"
		lstrSql = lstrSql & "  FROM `agrupa` " & vbCrLf
		lstrSql = lstrSql & " ORDER BY 1 ASC"
		Call Conectar(lstrSql, lrstAux)
		' ----- Insere os dados na ListView de rotas
		With lrstAux
			If .State > 0 Then
				If .RecordCount > 0 Then
					While Not .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"'
						lstrCod = VB6.Format(IIf(IsDbNull(.Fields("codigo").value), "0", .Fields("codigo").value), "000")
						'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"'
						lstrDes = Trim(IIf(IsDbNull(.Fields("descricao").value), "", .Fields("descricao").value))
						With Me.lsvAgrupa
							Call .Items.Add(lstrDes)
							'UPGRADE_WARNING: Lower bound of collection Me.lsvAgrupa.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
							Call .Items.Item(.Items.Count).SubItems.Add(lstrCod)
						End With
						Call .MoveNext()
					End While
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_NOTE: Object lrstAux 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"'
		lrstAux = Nothing
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Function MontaFiltroAgrupa() As String
		Dim lstrAux As String
		Dim lintIdx As Short
		lstrAux = ""
		With Me.lsvAgrupa
			For lintIdx = 1 To .Items.Count
				'UPGRADE_WARNING: Lower bound of collection Me.lsvAgrupa.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
				If .Items.Item(lintIdx).Checked = True Then
					'UPGRADE_WARNING: Lower bound of collection Me.lsvAgrupa.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					'UPGRADE_WARNING: Lower bound of collection Me.lsvAgrupa.ListItems() has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					lstrAux = lstrAux & """" & .Items.Item(lintIdx).SubItems(1).Text & ""","
				End If
			Next lintIdx
			If Len(Trim(lstrAux)) > 0 Then
				lstrAux = " (" & VB.Left(lstrAux, Len(lstrAux) - 1) & ") "
			End If
			If Len(Trim(lstrAux)) > 0 Then
				lstrAux = " AND agrupa IN " & lstrAux & " "
			End If
		End With
		MontaFiltroAgrupa = lstrAux
	End Function
End Class