Option Strict Off
Option Explicit On
Friend Class frmLocalizaFaturaValor
	Inherits System.Windows.Forms.Form
	Private ifrmQuemChama As System.Windows.Forms.Form
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdLocalizar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLocalizar.Click
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		With Me.mshDados
			Call .Clear()
			.Rows = 2
			.set_Cols( , 2)
			.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeBoth
			.AllowBigSelection = True
		End With
		lstrSql = ""
		lstrSql = lstrSql & "SELECT "
		lstrSql = lstrSql & " `nf`.`numnf` 'Número', "
		lstrSql = lstrSql & " `nf`.`mesano` 'Mês Ano', "
		lstrSql = lstrSql & " `nf`.`zona` 'Zona', "
		lstrSql = lstrSql & " `nf`.`livro` 'Livro', "
		lstrSql = lstrSql & " `nf`.`conta` 'Conta', "
		lstrSql = lstrSql & " FORMAT(CONVERT(ROUND(`nf`.`valortot`,2) USING latin1),2,'de_DE')  'Vlr.Total', "
		lstrSql = lstrSql & " FORMAT(CONVERT(ROUND(`nf`.`valorliq`,2) USING latin1),2,'de_DE')  'Vlr.Pagar', "
		lstrSql = lstrSql & " DATE_FORMAT(`nf`.`datavenc`,'%d/%m/%Y') 'Vecto.', "
		lstrSql = lstrSql & " DATE_FORMAT(`nf`.`datapag`,'%d/%m/%Y') 'Pgto.', "
		lstrSql = lstrSql & " (CASE `nf`.`flgst` WHEN 'P' THEN 'Pago' WHEN 'C' THEN 'Cancelado' WHEN 'R' THEN 'Reserva' WHEN 'J' THEN 'Júdice' ELSE 'Aberta' END) 'Situação', "
		lstrSql = lstrSql & " IFNULL(`unida`.`nome`,`unidb`.`nome`) 'Nome', "
		lstrSql = lstrSql & " IFNULL(`classe`.`descricao`,'') 'Classe' "
		lstrSql = lstrSql & "FROM `nf` "
		lstrSql = lstrSql & " LEFT JOIN `unida` ON(`unida`.`unid_id` = `nf`.`unid_id` AND `nf`.`grupo` = 'A') "
		lstrSql = lstrSql & " LEFT JOIN `unidb` ON(`unidb`.`unid_id` = `nf`.`unid_id` AND `nf`.`grupo` = 'B') "
		lstrSql = lstrSql & " LEFT JOIN `classe` ON(`classe`.`codigo` = `nf`.`codclas`) "
		lstrSql = lstrSql & "WHERE "
		If Me.opbEmAberta.Checked Then
			lstrSql = lstrSql & "`flgst` ='' "
		Else
			lstrSql = lstrSql & "1=1 "
		End If
		lstrSql = lstrSql & "  AND (`nf`.`valortot` = " & Replace(Replace(Me.txtValor.Text, ".", ""), ",", ".") & " OR "
		lstrSql = lstrSql & "       `nf`.`valorliq` = " & Replace(Replace(Me.txtValor.Text, ".", ""), ",", ".") & ") "
		lstrSql = lstrSql & " ORDER BY `mesano_ref` DESC, `numnf` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					Me.mshDados.DataSource = .DataSource
					With Me.mshDados
						.set_ColAlignment(1, MSHierarchicalFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
						.set_ColAlignment(2, MSHierarchicalFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
						.set_ColAlignment(3, MSHierarchicalFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
						.set_ColAlignment(4, MSHierarchicalFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
						.set_ColAlignment(5, MSHierarchicalFlexGridLib.AlignmentSettings.flexAlignRightCenter)
						.set_ColAlignment(6, MSHierarchicalFlexGridLib.AlignmentSettings.flexAlignRightCenter)
						.set_ColAlignment(7, MSHierarchicalFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
						.set_ColWidth(1,  , 800)
						.set_ColWidth(2,  , 500)
						.set_ColWidth(3,  , 500)
						.set_ColWidth(4,  , 700)
						.set_ColWidth(5,  , 1500)
						.set_ColWidth(6,  , 1500)
						.set_ColWidth(.get_Cols() - 2,  , 3000)
						.set_ColWidth(.get_Cols() - 1,  , 2000)
						.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeColumns
					End With
				End If
				Call .Close()
			End If
		End With
	End Sub
	
	Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form)
		ifrmQuemChama = afrmQuemChama
	End Sub
	
	Private Sub frmLocalizaFaturaValor_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 Me.Close()
		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 frmLocalizaFaturaValor_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		If Not ifrmQuemChama Is Nothing Then
			ifrmQuemChama.Enabled = False
		End If
	End Sub
	
	Private Sub frmLocalizaFaturaValor_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If Not ifrmQuemChama Is Nothing Then
			ifrmQuemChama.Enabled = True
		End If
	End Sub
	
	Private Sub mshDados_DblClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mshDados.DblClick
		If Not ifrmQuemChama Is Nothing Then
			If Me.mshDados.RowSel > 0 Then
				'UPGRADE_ISSUE: Control SetaValor could not be resolved because it was within the generic namespace Form. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="084D22AD-ECB1-400F-B4C7-418ECEC5E36E"'
				Call ifrmQuemChama.SetaValor(Me.mshDados.get_TextMatrix(Me.mshDados.RowSel, 0), Me.mshDados.get_TextMatrix(Me.mshDados.RowSel, 1))
				Call Me.Close()
			End If
		End If
	End Sub
	
	Private Sub txtValor_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValor.Enter
		With Me.txtValor
			.SelectionStart = 0
			.SelectionLength = CInt(Len(.Text))
		End With
	End Sub
	
	Private Sub txtValor_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValor.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
			If KeyAscii = System.Windows.Forms.Keys.Delete Then
				KeyAscii = System.Windows.Forms.Keys.Snapshot
			End If
			If KeyAscii <> System.Windows.Forms.Keys.Snapshot Then
				KeyAscii = 0
			Else
				If InStr(1, Me.txtValor.Text, ",") > 0 Then
					KeyAscii = 0
				End If
			End If
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtValor_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValor.Leave
		With Me.txtValor
			.Text = VB6.Format(.Text, "#,##0.00")
		End With
	End Sub
End Class