Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.ENTIDADE
Imports fatd_faturamento.BLL.Repositorio
Friend Class frmConfirmaEntrega
	Inherits System.Windows.Forms.Form
	
	Private ilngConsId As Integer
	Private ilngUnidId As Integer
	Private istrGrupo As String
	Private istrZona As String
	Private istrLivro As String
	Private istrConta As String
	Private idblNumNf As Double
	Private istrMesAno As String
	Private ilngDiasNr As Integer
	Private ilngDiasBx As Integer
	'Private ifrmLocalizaUnidade As frmLocalizaUnidade
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private dt As DataTable

	Private Sub cmdEntregaRota_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEntregaRota.Click
		Try
			Call frmEntregaReaviso.Show()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdEntregaRota_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdLocalizarUnidadeConsumidora_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizarUnidadeConsumidora.Click
		Try
			'With ifrmLocalizaUnidade
			Call frmLocalizaUnidade.Show()
			Call frmLocalizaUnidade.SetaLocalizaUnidade("U", Me)
			'End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdLocalizarUnidadeConsumidora_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		Dim lstrSql As String
		Dim llngDias As Integer
		Try
			If Me.dtpEntrega.Value < CDate(Me.lblDtReaviso.Text) Then
				Call MsgBox("A data da entrega do reaviso não pode ser menor que a data do reaviso!", MsgBoxStyle.Critical, gstrTituloMensagem)
			Else
				If Trim(Me.lblClasse.Text) = "9105" Then
					llngDias = ilngDiasBx
				Else
					llngDias = ilngDiasNr
				End If
				lstrSql = ""
				lstrSql = lstrSql & "UPDATE `concorte` "
				lstrSql = lstrSql & "   SET `concorte`.`dataentrega` = """ & VB6.Format(Me.dtpEntrega.Value, "yyyy-MM-dd hh:mm:ss") & """, "
				lstrSql = lstrSql & "       `concorte`.`sit`       = if(`concorte`.`sit`='P',`concorte`.`sit`,'E'), "
				lstrSql = lstrSql & "       `concorte`.`prazoreav` = """ & VB6.Format(DateAdd(DateInterval.Day, llngDias, Me.dtpEntrega.Value), "yyyy-MM-dd hh:mm:ss") & """ "
				lstrSql = lstrSql & " WHERE `concorte`.`numnf`  = """ & Replace(Me.lblNumNf.Text, ".", "") & """ "
				lstrSql = lstrSql & "   AND `concorte`.`mesano` = """ & Me.lblMesAno.Text & """ "
				lstrSql = lstrSql & "   AND `concorte`.`zona`   = """ & istrZona & """ "
				lstrSql = lstrSql & "   AND `concorte`.`livro`  = """ & istrLivro & """ "
				lstrSql = lstrSql & "   AND `concorte`.`conta`  = """ & istrConta & """ "
				lstrSql = lstrSql & "   AND `concorte`.`unid_id` = " & ilngUnidId
				'lstrSql = lstrSql & "   AND `concorte`.`cons_id` = " & ilngConsId
				lstrSql = lstrSql & "   AND MID(concorte.ta,3,1) = '" & istrGrupo & "' "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

				Call AtualizaHistoricoUnidade(ilngUnidId, istrGrupo, "ENTREGUE REAVISO DA FATURA: " & Me.lblNumNf.Text & " " & Me.lblMesAno.Text & " EM: " & VB6.Format(Me.dtpEntrega.Value, "dd/mm/yyyy"))
				Call BuscaUnidade()
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdSalvar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmConfirmaEntrega_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'ifrmLocalizaUnidade = Nothing
	End Sub
	Private Sub lblSitReaviso_Change()
		Try
			Select Case Me.lblSitReaviso.Text
				Case Is = "R"
					Me.lblEstadoReaviso.Text = "Emitido reaviso"
				Case Is = "E"
					Me.lblEstadoReaviso.Text = "Entregue reaviso"
				Case Is = "C"
					Me.lblEstadoReaviso.Text = "U.C. SUSPENDIDA"
				Case Is = "P"
					Me.lblEstadoReaviso.Text = "Efetivado pagamento"
				Case Else
					Me.lblEstadoReaviso.Text = "Status Indefinido"
			End Select
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: lblSitReaviso_Change" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub opbGrupoA_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbGrupoA.CheckedChanged
		Try
			If eventSender.Checked Then
				istrGrupo = "A"
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: opbGrupoA_CheckedChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub opbGrupoB_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbGrupoB.CheckedChanged
		Try
			If eventSender.Checked Then
				istrGrupo = "B"
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: opbGrupoB_CheckedChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub txtBarra_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtBarra.TextChanged
		Try
			With Me.txtBarra
				If Len(.Text) = 0 Then
					Me.txtZona.Enabled = True
					Me.txtLivro.Enabled = True
					Me.txtConta.Enabled = True
					Me.txtZona.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
					Me.txtLivro.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
					Me.txtConta.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
					Me.opbGrupoA.Enabled = True
					Me.opbGrupoB.Enabled = True
					Me.cmdBuscar.Enabled = True
					System.Windows.Forms.Application.DoEvents()
				Else
					Me.txtZona.Enabled = False
					Me.txtLivro.Enabled = False
					Me.txtConta.Enabled = False
					Me.txtZona.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
					Me.txtLivro.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
					Me.txtConta.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
					Me.opbGrupoA.Enabled = False
					Me.opbGrupoB.Enabled = False
					'Me.cmdBuscar.Enabled = False
					System.Windows.Forms.Application.DoEvents()
					If Len(Trim(.Text)) = 28 Then
						Call VerificaBarras()
					End If
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtBarra_TextChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtBarra_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtBarra.Enter
		Try
			With Me.txtBarra
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtBarra_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtBarra_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtBarra.Leave
		Call VerificaBarras()
	End Sub

	Private Sub VerificaBarras()
		Try
			With Me.txtBarra
				If Len(Trim(.Text)) = 28 Then
					.Text = Trim(.Text)
					istrZona = Mid(.Text, 1, 3)
					istrLivro = Mid(.Text, 4, 2)
					istrConta = Mid(.Text, 6, 6)
					idblNumNf = CDbl(Mid(.Text, 12, 9))
					istrMesAno = Mid(.Text, 21, 6)
					istrMesAno = VB.Right(istrMesAno, 2) & "/" & VB.Left(istrMesAno, 4)
					istrGrupo = IIf(CDbl(Mid(.Text, 27, 2)) = 65, "A", "B")
					Me.opbGrupoB.Checked = (istrGrupo = "B")
					Me.opbGrupoA.Checked = (istrGrupo = "A")
					Call BuscaUnidade()
					If Me.lblRegMax.Text <> "0" Then
						Call VerificaSalvar()
					End If
					If Me.cmdSalvar.Enabled And Me.cmdSalvar.Visible Then
						Call cmdSalvar.Focus()
					End If
					If Me.cmdSalvar.Enabled = False Then
						Call txtBarra_Enter(txtBarra, New System.EventArgs())
					End If
					'Call SendKeysA(vbKeyTab, False)
				ElseIf Len(Trim(.Text)) > 0 Then
					Call MsgBox("Código de barra inválido!", MsgBoxStyle.Information, gstrTituloMensagem)
					Call .Focus()
				Else
					istrZona = ""
					istrLivro = ""
					istrConta = ""
					idblNumNf = 0
					istrMesAno = ""
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaBarras" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub txtConta_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.TextChanged
		Try
			With Me.txtConta
				If Len(.Text) = 0 Then
					Me.txtBarra.Enabled = True
					Me.txtBarra.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
					System.Windows.Forms.Application.DoEvents()
				Else
					Me.txtBarra.Enabled = False
					Me.txtBarra.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtConta_TextChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub txtLivro_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLivro.TextChanged
		Try
			With Me.txtLivro
				If Len(.Text) = 0 Then
					Me.txtBarra.Enabled = True
					Me.txtBarra.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
					System.Windows.Forms.Application.DoEvents()
				Else
					Me.txtBarra.Enabled = False
					Me.txtBarra.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtLivro_TextChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub txtZona_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtZona.TextChanged
		Try
			With Me.txtZona
				If Len(.Text) = 0 Then
					Me.txtBarra.Enabled = True
					Me.txtBarra.BackColor = System.Drawing.ColorTranslator.FromOle(&H80000005)
					System.Windows.Forms.Application.DoEvents()
				Else
					Me.txtBarra.Enabled = False
					Me.txtBarra.BackColor = System.Drawing.ColorTranslator.FromOle(&H8000000F)
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtZona_TextChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtZona_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtZona.Enter
		Try
			With Me.txtZona
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtZona_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtZona_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtZona.Leave
		Try
			With Me.txtZona
				.Text = VB6.Format(.Text, "000")
				istrZona = .Text
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtZona_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtLivro_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLivro.Enter
		Try
			With Me.txtLivro
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtLivro_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtLivro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtLivro.Leave
		Try
			With Me.txtLivro
				.Text = VB6.Format(.Text, "00")
				istrLivro = .Text
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtLivro_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtConta_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.Enter
		Try
			With Me.txtConta
				.SelectionStart = 0
				.SelectionLength = Len(.Text)
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtConta_Enter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub txtConta_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConta.Leave
		Try
			With Me.txtConta
				.Text = VB6.Format(.Text, "000000")
				istrConta = .Text
				If Len(Trim(istrZona)) > 0 And Len(Trim(istrLivro)) > 0 And Len(Trim(istrConta)) > 0 Then
					Call BuscaUnidade()
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtConta_Leave" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdBuscar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscar.Click
		Try
			'Me.cmdBuscar.Enabled = False
			Call BuscaUnidade()
			'Me.cmdBuscar.Enabled = True
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdBuscar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub BuscaUnidade()
		Dim lblnReaviso As Boolean
		Dim lstrSql As String
		Dim lstrCpfCnpj As String
		Dim lstrRgIe As String
		Try
			Me.cmdSalvar.Enabled = False
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Call LimpaCamposConsumidor()
			If Len(Trim(istrZona)) > 0 And Len(Trim(istrLivro)) > 0 And Len(Trim(istrConta)) > 0 Then
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `nome`, "
				lstrSql = lstrSql & "       `end`, "
				lstrSql = lstrSql & "       `numero`, "
				lstrSql = lstrSql & "       `complement`, "
				lstrSql = lstrSql & "       `bairro`, "
				lstrSql = lstrSql & "       `cidade`, "
				lstrSql = lstrSql & "       `cep`, "
				lstrSql = lstrSql & "       `uf`, "
				lstrSql = lstrSql & "       `distrito`, "
				lstrSql = lstrSql & "       `lote`, "
				lstrSql = lstrSql & "       `quadra`, "
				lstrSql = lstrSql & "       `cons_id` , "
				lstrSql = lstrSql & "       `id`, "
				lstrSql = lstrSql & "       `cpf_cnpj`, "
				lstrSql = lstrSql & "       `rg`, "
				lstrSql = lstrSql & "       `insc`, "
				lstrSql = lstrSql & "       `situacao` "
				lstrSql = lstrSql & "  FROM `uc_faturamento` "
				lstrSql = lstrSql & "   WHERE `zona`  = '" & istrZona & "' "
				lstrSql = lstrSql & "   AND `livro` = '" & istrLivro & "' "
				lstrSql = lstrSql & "   AND `conta` = '" & istrConta & "' "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt.Rows.Count > 0 Then
					For Each row As DataRow In dt.Rows
						Me.lblNomeUC.Text = " " & IIf(IsDBNull(row.Item("nome")), "Nome em branco!!!", row.Item("nome"))
						Me.lblEndereco.Text = " " & IIf(IsDBNull(row.Item("end")), "", row.Item("end"))
						Me.lblNumero.Text = IIf(IsDBNull(row.Item("numero")), "", row.Item("numero"))
						Me.lblComplement.Text = " " & IIf(IsDBNull(row.Item("complement")), "", row.Item("complement"))
						Me.lblBairro.Text = " " & IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro"))
						Me.lblCidade.Text = " " & IIf(IsDBNull(row.Item("cidade")), "", row.Item("cidade"))
						Me.lblLote.Text = IIf(IsDBNull(row.Item("lote")), "", row.Item("lote"))
						Me.lblQuadra.Text = IIf(IsDBNull(row.Item("quadra")), "", row.Item("quadra"))
						Me.lblCep.Text = IIf(IsDBNull(row.Item("cep")), "", row.Item("cep"))
						Me.lblUf.Text = IIf(IsDBNull(row.Item("uf")), "", row.Item("uf"))
						Me.lblDistrito.Text = " " & IIf(IsDBNull(row.Item("distrito")), "", row.Item("distrito"))
						lstrCpfCnpj = IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj"))
						If Len(lstrCpfCnpj) <= 11 Then
							Me.lblCpfCnpj.Text = FormataCPF(lstrCpfCnpj)
							Me.lblRgIe.Text = IIf(IsDBNull(row.Item("rg")), "", row.Item("rg"))
						Else
							Me.lblCpfCnpj.Text = FormataCNPJ(lstrCpfCnpj)
							Me.lblRgIe.Text = IIf(IsDBNull(row.Item("insc")), "", row.Item("insc"))
						End If

						Me.lblSituacao.Text = IIf(IsDBNull(row.Item("situacao")), "L", row.Item("situacao"))
						Select Case Me.lblSituacao.Text
							Case Is = "L"
								Me.lblSituacao.Text = "LIGADO"
							Case Is = "D"
								Me.lblSituacao.Text = "DESLIGADO"
							Case Is = "C"
								Me.lblSituacao.Text = "SUSPENDIDO"
							Case Is = "I"
								Me.lblSituacao.Text = "DESATIVADO"
							Case Else
								Me.lblSituacao.Text = "NÃO LIGADO"
						End Select

						ilngConsId = CInt(IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id")))
						ilngUnidId = CInt(IIf(IsDBNull(row.Item("id")), 0, row.Item("id")))
						lblnReaviso = BuscaDadosReaviso()

						If Me.lblRegMax.Text <> "0" Then
							Call VerificaSalvar()
						End If
						If Me.cmdSalvar.Enabled Then
							Call cmdSalvar.Focus()
						End If
					Next
				Else
					Me.Cursor = System.Windows.Forms.Cursors.Default
					Call MsgBox("O identificar " & istrZona & "." & istrLivro & "." & istrConta & ", não foi encontrado", MsgBoxStyle.Information, gstrTituloMensagem)
					Call LimpaCamposConsumidor()
					If Me.txtZona.Enabled = True Then
						Call Me.txtZona.Focus()
					Else
						Call Me.txtBarra.Focus()
					End If
					Me.cmdSalvar.Enabled = False
				End If

			Else
				Me.Cursor = System.Windows.Forms.Cursors.Default
				Call MsgBox("Faltou informar o identificador!!!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call LimpaCamposConsumidor()
				If Me.txtZona.Enabled = True Then
					Call Me.txtZona.Focus()
				Else
					Call Me.txtBarra.Focus()
				End If
			End If
			If Not Me.cmdSalvar.Enabled Then
				If Me.txtZona.Enabled = True Then
					Call Me.txtZona.Focus()
				Else
					Call Me.txtBarra.Focus()
				End If
			End If
			Me.Cursor = System.Windows.Forms.Cursors.Default
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaUnidade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub LimpaCamposConsumidor()
		Try
			Me.lblNomeUC.Text = ""
			Me.lblEndereco.Text = ""
			Me.lblNumero.Text = ""
			Me.lblComplement.Text = ""
			Me.lblBairro.Text = ""
			Me.lblCidade.Text = ""
			Me.lblUf.Text = ""
			Me.lblLote.Text = ""
			Me.lblQuadra.Text = ""
			Me.lblCep.Text = ""
			Me.lblDistrito.Text = ""
			Me.lblCpfCnpj.Text = ""
			Me.lblRgIe.Text = ""
			Me.lblSituacao.Text = ""
			ilngConsId = 0
			ilngUnidId = 0
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LimpaCamposConsumidor" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Function BuscaDadosReaviso() As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim iNumNF As Integer
		Dim sMesAno As String
		Dim sdtReaviso As String
		Dim sdtPrazo As String
		Dim sdtEntrega As String
		Dim sitReaviso As String
		Dim sdtVencto As String
		Dim dvalor As Double
		Dim sdtPagto As String
		Dim sClasse As String
		Dim sPedidoPrazo As String
		Dim sNomeReaviso As String
		lblnRet = False
		Try
			If ilngConsId > 0 And ilngUnidId > 0 Then
				'If irstReaviso.State = ADODB.ObjectStateEnum.adStateOpen Then Call irstReaviso.Close()
				lstrSql = ""
				lstrSql = lstrSql & "SELECT `concorte`.`numnf`, "
				lstrSql = lstrSql & "       `concorte`.`mesano`, "
				lstrSql = lstrSql & "       `concorte`.`datareav`, "
				lstrSql = lstrSql & "       `concorte`.`prazoreav`, "
				lstrSql = lstrSql & "       `concorte`.`dataentrega`, "
				lstrSql = lstrSql & "       `concorte`.`sit`, "
				lstrSql = lstrSql & "       `concorte`.`datavenc`, "
				lstrSql = lstrSql & "       `concorte`.`valor`, "
				lstrSql = lstrSql & "       `concorte`.`datapag`, "
				lstrSql = lstrSql & "       `concorte`.`codclas`, "
				lstrSql = lstrSql & "       `concorte`.`dataprazo`, "
				lstrSql = lstrSql & "       `concorte`.`nome` "
				lstrSql = lstrSql & "  FROM `concorte` "
				lstrSql = lstrSql & " WHERE `concorte`.`zona`    = '" & istrZona & "' "
				lstrSql = lstrSql & "   AND `concorte`.`livro`   = '" & istrLivro & "' "
				lstrSql = lstrSql & "   AND `concorte`.`conta`   = '" & istrConta & "' "
				'lstrSql = lstrSql & "   AND `concorte`.`cons_id` = " & ilngConsId & " "
				lstrSql = lstrSql & "   AND `concorte`.`unid_id` = " & ilngUnidId & " "
				lstrSql = lstrSql & "   AND MID(concorte.ta,3,1) = '" & istrGrupo & "'"
				If idblNumNf > 0 Then
					lstrSql = lstrSql & "   AND `concorte`.`numnf` = """ & idblNumNf & """ "
					lstrSql = lstrSql & "   AND `concorte`.`mesano` = """ & istrMesAno & """ "
				End If
				lstrSql = lstrSql & " ORDER BY `concorte`.`datareav` DESC "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt.Rows.Count > 0 Then
					lblnRet = True
					Me.lblRegMin.Text = CStr(1)
					Me.lblRegMax.Text = CStr(dt.Rows.Count)
					Me.lblRegAtu.Text = CStr(1)


					For Each row As DataRow In dt.Rows
						iNumNF = IIf(IsDBNull(row.Item("numnf")), "", row.Item("numnf"))
						sMesAno = IIf(IsDBNull(row.Item("mesano")), "", row.Item("mesano"))
						sdtReaviso = IIf(IsDBNull(row.Item("datareav")), "", row.Item("datareav"))
						sdtPrazo = IIf(IsDBNull(row.Item("prazoreav")), "", row.Item("prazoreav"))
						sdtEntrega = IIf(IsDBNull(row.Item("dataentrega")), "", row.Item("dataentrega"))
						sitReaviso = IIf(IsDBNull(row.Item("sit")), "", row.Item("sit"))
						sdtVencto = IIf(IsDBNull(row.Item("datavenc")), "", row.Item("datavenc"))
						dvalor = IIf(IsDBNull(row.Item("valor")), "", row.Item("valor"))
						sdtPagto = IIf(IsDBNull(row.Item("datapag")), "", row.Item("datapag"))
						sClasse = IIf(IsDBNull(row.Item("codclas")), "", row.Item("codclas"))
						sPedidoPrazo = IIf(IsDBNull(row.Item("dataprazo")), "", row.Item("dataprazo"))
						sNomeReaviso = IIf(IsDBNull(row.Item("nome")), "", row.Item("nome"))


						With Me.msfReaviso
							'.Rows = .Rows + 1
							.FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
							.set_TextMatrix(.Rows - 1, 0, sNomeReaviso)
							.set_TextMatrix(.Rows - 1, 1, iNumNF)
							.set_TextMatrix(.Rows - 1, 2, sMesAno)
							.set_TextMatrix(.Rows - 1, 3, sitReaviso)
							.set_TextMatrix(.Rows - 1, 4, VB6.Format(sdtReaviso, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 5, VB6.Format(sdtPrazo, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 6, VB6.Format(sdtEntrega, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 7, VB6.Format(sPedidoPrazo, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 8, VB6.Format(sdtVencto, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 9, VB6.Format(sdtPagto, "dd/mm/yyyy"))
							.set_TextMatrix(.Rows - 1, 10, dvalor)
						End With
					Next
				Else
					lblnRet = False
					Me.lblRegMin.Text = CStr(0)
					Me.lblRegMax.Text = CStr(0)
					Me.lblRegAtu.Text = CStr(0)
				End If

			End If
			BuscaDadosReaviso = lblnRet
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaDadosReaviso" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	End Function

	'Private Sub cmdPrimeiro_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPrimeiro.Click
	'	Try
	'		If CInt(Me.lblRegMax.Text) > 0 Then
	'			Me.lblRegAtu.Text = CStr(1)
	'			'Call irstReaviso.MoveFirst()
	'		End If
	'		Call VerificaSalvar()
	'	Catch ex As Exception
	'		Exit Sub
	'	End Try
	'End Sub

	'Private Sub cmdAnterior_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAnterior.Click
	'	Try
	'		If CInt(Me.lblRegMax.Text) > 0 Then
	'			If CInt(Me.lblRegAtu.Text) > 1 Then
	'				Me.lblRegAtu.Text = CStr(CDbl(Me.lblRegAtu.Text) - 1)
	'				'Call irstReaviso.MovePrevious()
	'			End If
	'		End If
	'		Call VerificaSalvar()
	'	Catch ex As Exception
	'		Exit Sub
	'	End Try
	'End Sub

	'Private Sub cmdProximo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProximo.Click
	'	Try
	'		If CInt(Me.lblRegMax.Text) > 0 Then
	'			If CInt(Me.lblRegAtu.Text) < CInt(Me.lblRegMax.Text) Then
	'				Me.lblRegAtu.Text = CStr(CDbl(Me.lblRegAtu.Text) + 1)
	'				'Call irstReaviso.MoveNext()
	'			End If
	'		End If
	'		Call VerificaSalvar()
	'	Catch ex As Exception
	'		Exit Sub
	'	End Try
	'End Sub

	'Private Sub cmdUltimo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdUltimo.Click
	'	Try
	'		If CInt(Me.lblRegMax.Text) > 0 Then
	'			If CInt(Me.lblRegAtu.Text) < CInt(Me.lblRegMax.Text) Then
	'				'Me.lblRegAtu.Text = CStr(irstReaviso.RecordCount)
	'				'Call irstReaviso.MoveLast()
	'			End If
	'		End If
	'		Call VerificaSalvar()
	'	Catch ex As Exception
	'		Exit Sub
	'	End Try
	'End Sub

	Private Sub frmConfirmaEntrega_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			Me.dtpEntrega.Value = Date.Now()
			Me.dtpEntrega.Enabled = False
			ilngDiasNr = CInt(GetConfig("reaviso.prazo.normal.dias"))
			ilngDiasBx = CInt(GetConfig("reaviso.prazo.tsee.dias"))
			istrGrupo = IIf(Me.opbGrupoB.Checked, "B", "A")
			ilngConsId = 0
			ilngUnidId = 0
			MontaGrid()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmConfirmaEntrega_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub VerificaSalvar()
		Try
			If Trim(Me.lblSitReaviso.Text) = "R" Or (Trim(Me.lblSitReaviso.Text) = "P" And Trim(Me.lblDtEntrega.Text) = "") Then
				Me.cmdSalvar.Enabled = True
			Else
				Me.cmdSalvar.Enabled = False
			End If
			Me.dtpEntrega.Value = Date.Now
			Me.dtpEntrega.Enabled = Me.cmdSalvar.Enabled
			System.Windows.Forms.Application.DoEvents()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaSalvar" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmConfirmaEntrega_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			Select Case KeyAscii
				Case System.Windows.Forms.Keys.Return
					TabEnter(KeyAscii, eventArgs)
				Case System.Windows.Forms.Keys.Escape
					Call cmdFechar_Click(cmdFechar, New System.EventArgs())
			End Select
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmConfirmaEntrega_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub

	Public Sub SetaCodigo(ByVal alngUnidId As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal astrGrupo As String)
		Try
			Me.txtZona.Text = astrZona
			Me.txtLivro.Text = astrLivro
			Me.txtConta.Text = astrConta
			istrZona = astrZona
			istrLivro = astrLivro
			istrConta = astrConta
			'ilngConsId = alngConsId
			ilngUnidId = alngUnidId
			istrGrupo = astrGrupo
			Call BuscaUnidade()
			If astrGrupo = "A" Then
				Me.opbGrupoA.Checked = True
			ElseIf astrGrupo = "B" Then
				Me.opbGrupoB.Checked = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: SetaCodigo" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub MontaGrid()
		Try
			With Me.msfReaviso
				.Cols = 11
				'.Rows = 1
				'.FixedRows = 1
				.Redraw = False
				.set_ColWidth(0, 3000)
				.set_ColWidth(1, 800)
				.set_ColWidth(2, 700)
				.set_ColWidth(3, 700)
				.set_ColWidth(4, 1000)
				.set_ColWidth(5, 1000)
				.set_ColWidth(6, 1000)
				.set_ColWidth(7, 1000)
				.set_ColWidth(8, 1000)
				.set_ColWidth(9, 1000)
				.set_ColWidth(10, 1000)
				.set_TextMatrix(0, 0, "Nome")
				.set_TextMatrix(0, 1, "Fatura")
				.set_TextMatrix(0, 2, "Mês\Ano")
				.set_TextMatrix(0, 3, "Situação")
				.set_TextMatrix(0, 4, "Reaviso")
				.set_TextMatrix(0, 5, "Prazo")
				.set_TextMatrix(0, 6, "Entrega")
				.set_TextMatrix(0, 7, "Pedido Prazo")
				.set_TextMatrix(0, 8, "Vencimento")
				.set_TextMatrix(0, 9, "Pagamento")
				.set_TextMatrix(0, 10, "Valor")

				.Redraw = True
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaGrid" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub msfReaviso_ClickEvent(sender As Object, e As EventArgs) Handles msfReaviso.ClickEvent
		Try

			Dim numnf
			Dim mesano
			Dim datareav
			Dim prazoreav
			Dim dataentrega
			Dim sit
			Dim datavenc
			Dim valor
			Dim datapag
			Dim codclas
			Dim dataprazo
			Dim nome

			With msfReaviso

				nome = .get_TextMatrix(.RowSel, 0)
				numnf = .get_TextMatrix(.RowSel, 1)
				mesano = .get_TextMatrix(.RowSel, 2)
				sit = .get_TextMatrix(.RowSel, 3)
				datareav = .get_TextMatrix(.RowSel, 4)
				prazoreav = .get_TextMatrix(.RowSel, 5)
				dataentrega = .get_TextMatrix(.RowSel, 6)
				dataprazo = .get_TextMatrix(.RowSel, 7)
				datavenc = .get_TextMatrix(.RowSel, 8)
				datapag = .get_TextMatrix(.RowSel, 9)
				valor = .get_TextMatrix(.RowSel, 10)


				Me.lblNumNf.Text = numnf
				Me.lblMesAno.Text = mesano
				Me.lblDtReaviso.Text = datareav
				Me.lblDtPrazo.Text = prazoreav
				Me.lblDtEntrega.Text = dataentrega
				Me.lblSitReaviso.Text = sit
				Me.lblDtVecto.Text = datavenc
				Me.lblValor.Text = valor
				Me.lblDtPagto.Text = datapag
				Me.lblClasse.Text = codclas
				Me.lblPedidoPrazo.Text = dataprazo
				Me.lblNomeReaviso.Text = nome

				Call VerificaSalvar()

			End With

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: msfReaviso_ClickEvent" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
End Class