Option Strict Off
Option Explicit On
Friend Class frmBaixaReavisos
	Inherits System.Windows.Forms.Form
	Private idteBase As Date
	
	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
		Dim lrstAux As ADODB.Recordset
		'
		On Error Resume Next
		lrstAux = New ADODB.Recordset
		'
		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') "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.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"'
						ldblNumNF = CDbl(IIf(IsDbNull(.Fields("nf_numnf").value), 0, .Fields("nf_numnf").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"'
						lstrMesAno = Trim(IIf(IsDbNull(.Fields("nf_mesano").value), "00/0000", .Fields("nf_mesano").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"'
						ldtePagto = CDate(IIf(IsDbNull(.Fields("nf_datapag").value), idteBase, .Fields("nf_datapag").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"'
						lstrFlg = Trim(IIf(IsDbNull(.Fields("nf_flgst").value), "", .Fields("nf_flgst").value))
						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 & "' "
							Call Executar(lstrSql)
						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 & "' "
							Call Executar(lstrSql)
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
					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
				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
		Me.cmdFechar.Enabled = True
		Me.cmdProcessa.Enabled = True
	End Sub
	
	Private Sub frmBaixaReavisos_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.Return Then
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		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
	End Sub
	
	Private Sub frmBaixaReavisos_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		idteBase = BuscaDataBD
	End Sub
End Class