Option Strict Off
Option Explicit On
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE

Friend Class frmBaixaReavisos
	Inherits System.Windows.Forms.Form
	Private idteBase As Date
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private dt As DataTable
	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdProcessa_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessa.Click
		Dim ldblNumNF As Double
		Dim lstrMesAno As String
		Dim ldtePagto As Date
		Dim lstrFlg As String
		Dim lstrSql As String
		Try
			Me.cmdFechar.Enabled = False
			Me.cmdProcessa.Enabled = False
			Me.lblInfo.Text = " Aguarde buscando reavisos... "
			Call Me.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & "       `concorte`.`numnf`  AS concorte_numnf, " & vbCrLf
			lstrSql = lstrSql & "       `concorte`.`mesano` AS concorte_mesano, " & vbCrLf
			lstrSql = lstrSql & "       `concorte`.`sit`    AS concorte_sit, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano_ref`   AS nf_mesano_ref, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`mesano`       AS nf_mesano, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`numnf`        AS nf_numnf, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`flgst`        AS nf_flgst, " & vbCrLf
			lstrSql = lstrSql & "       `nf`.`datapag`      AS nf_datapag " & vbCrLf
			lstrSql = lstrSql & "  FROM `concorte`, `nf` " & vbCrLf
			lstrSql = lstrSql & " WHERE `concorte`.`numnf`  = nf.numnf " & vbCrLf
			lstrSql = lstrSql & "   AND `concorte`.`mesano` = nf.mesano " & vbCrLf & vbCrLf
			lstrSql = lstrSql & "   AND `concorte`.`sit` NOT IN('P','X') " & vbCrLf
			lstrSql = lstrSql & "   AND `nf`.`flgst` IN('P','C') "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
				For Each row As DataRow In dt.Rows
					ldblNumNF = CDbl(IIf(IsDBNull(row.Item("nf_numnf")), 0, row.Item("nf_numnf")))
					lstrMesAno = Trim(IIf(IsDBNull(row.Item("nf_mesano")), "00/0000", row.Item("nf_mesano")))
					ldtePagto = CDate(IIf(IsDBNull(row.Item("nf_datapag")), idteBase, row.Item("nf_datapag")))
					lstrFlg = Trim(IIf(IsDBNull(row.Item("nf_flgst")), "", row.Item("nf_flgst")))
					If lstrFlg = "P" Then
						lstrSql = ""
						lstrSql = lstrSql & "UPDATE `concorte` " & vbCrLf
						lstrSql = lstrSql & "   SET `sit` = 'P', " & vbCrLf
						lstrSql = lstrSql & "       `datapag` = '" & VB6.Format(ldtePagto, "yyyy-MM-dd") & "' " & vbCrLf
						lstrSql = lstrSql & " WHERE `numnf` =" & ldblNumNF & " " & vbCrLf
						lstrSql = lstrSql & "   AND `mesano` ='" & lstrMesAno & "' "
						objExecAtualizaBancoDados.QUERY_NAME = lstrSql
						repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
					ElseIf lstrFlg = "C" Then
						lstrSql = ""
						lstrSql = lstrSql & "UPDATE `concorte` " & vbCrLf
						lstrSql = lstrSql & "   SET `sit` = 'X', " & vbCrLf
						lstrSql = lstrSql & "       `datapag` = '0000-00-00' " & vbCrLf
						lstrSql = lstrSql & " WHERE `numnf` =" & ldblNumNF & " " & vbCrLf
						lstrSql = lstrSql & "   AND `mesano` ='" & lstrMesAno & "' "
						objExecAtualizaBancoDados.QUERY_NAME = lstrSql
						repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
					End If
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				Next
				Call MsgBox("Processo finalizado.", MsgBoxStyle.Information, gstrTituloMensagem)
			Else
				Call MsgBox("Não foram encontrados reavisos com problema.", MsgBoxStyle.Exclamation, gstrTituloMensagem)
				Me.lblInfo.Text = ""
				Call Me.lblInfo.Refresh()
			End If
			Me.cmdFechar.Enabled = True
			Me.cmdProcessa.Enabled = True

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

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

	Private Sub frmBaixaReavisos_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		idteBase = Date.Now
	End Sub
End Class