Option Strict Off
Option Explicit On
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmLocalizaFaturaValor
	Inherits System.Windows.Forms.Form
	Private ifrmQuemChama As System.Windows.Forms.Form
	Dim sform

	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 dt As DataTable
		Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
		Dim sNumero As String
		Dim sMesAno As String
		Dim sZona As String
		Dim sLivro As String
		Dim sConta As String
		Dim sValorTotal As String
		Dim sValorPagar As String
		Dim sVencto As String
		Dim sPagto As String
		Dim sSituacao As String
		Dim sNome As String
		Dim sClasse As String
		Try

			'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` as 'Numero', "
			lstrSql = lstrSql & " `nf`.`mesano` as 'MesAno', "
			lstrSql = lstrSql & " `nf`.`zona` as 'Zona', "
			lstrSql = lstrSql & " `nf`.`livro` as 'Livro', "
			lstrSql = lstrSql & " `nf`.`conta` as 'Conta', "
			lstrSql = lstrSql & " FORMAT(CONVERT(ROUND(`nf`.`valortot`,2) USING latin1),2,'de_DE') as 'Vlr.Total', "
			lstrSql = lstrSql & " FORMAT(CONVERT(ROUND(`nf`.`valorliq`,2) USING latin1),2,'de_DE') as 'Vlr.Pagar', "
			lstrSql = lstrSql & " DATE_FORMAT(`nf`.`datavenc`,'%d/%m/%Y') as 'Vecto', "
			lstrSql = lstrSql & " DATE_FORMAT(`nf`.`datapag`,'%d/%m/%Y') as '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) as 'Situacao', "
			lstrSql = lstrSql & " IFNULL(`uc_faturamento`.`nome`,`uc_faturamento`.`nome`) as 'Nome', "
			lstrSql = lstrSql & " IFNULL(`classe`.`descricao`,'') as 'Classe' "
			lstrSql = lstrSql & "FROM `nf` "
			lstrSql = lstrSql & " LEFT JOIN `uc_faturamento` ON(`uc_faturamento`.`id` = `nf`.`unid_id`) "
			'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 "

			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			Call MontaGrade()

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					sNumero = Trim(IIf(IsDBNull(row.Item("Numero")), "", row.Item("Numero")))
					sMesAno = Trim(IIf(IsDBNull(row.Item("MesAno")), "", row.Item("MesAno")))
					sZona = Trim(IIf(IsDBNull(row.Item("Zona")), "", row.Item("Zona")))
					sLivro = Trim(IIf(IsDBNull(row.Item("Livro")), "", row.Item("Livro")))
					sConta = Trim(IIf(IsDBNull(row.Item("Conta")), "", row.Item("Conta")))
					sValorTotal = Trim(IIf(IsDBNull(row.Item("Vlr.Total")), "", row.Item("Vlr.Total")))
					sValorPagar = Trim(IIf(IsDBNull(row.Item("Vlr.Pagar")), "", row.Item("Vlr.Pagar")))
					sVencto = Trim(IIf(IsDBNull(row.Item("Vecto")), "", row.Item("Vecto")))
					sPagto = Trim(IIf(IsDBNull(row.Item("Pgto")), "", row.Item("Pgto")))
					sSituacao = Trim(IIf(IsDBNull(row.Item("Situacao")), "", row.Item("Situacao")))
					sNome = Trim(IIf(IsDBNull(row.Item("Nome")), "", row.Item("Nome")))
					sClasse = Trim(IIf(IsDBNull(row.Item("Classe")), "", row.Item("Classe")))

					With Me.mshDados
						.Rows = Me.mshDados.Rows + 1
						.set_TextMatrix(.Rows - 1, 0, "")
						.set_TextMatrix(.Rows - 1, 1, sNumero)
						.set_TextMatrix(.Rows - 1, 2, sMesAno)
						.set_TextMatrix(.Rows - 1, 3, sZona)
						.set_TextMatrix(.Rows - 1, 4, sLivro)
						.set_TextMatrix(.Rows - 1, 5, sConta)
						.set_TextMatrix(.Rows - 1, 6, VB6.Format(CDbl(sValorTotal), "Standard"))
						.set_TextMatrix(.Rows - 1, 7, VB6.Format(CDbl(sValorPagar), "Standard"))
						.set_TextMatrix(.Rows - 1, 8, VB6.Format(sVencto, "dd/mm/yyyy"))
						.set_TextMatrix(.Rows - 1, 9, VB6.Format(sPagto, "dd/mm/yyyy"))
						.set_TextMatrix(.Rows - 1, 10, sSituacao)
						.set_TextMatrix(.Rows - 1, 11, sNome)
						.set_TextMatrix(.Rows - 1, 12, sClasse)
					End With
				Next
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdLocalizar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	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
		Try
			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
				TabEnter(KeyAscii, 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 & " - Rotina: frmLocalizaFaturaValor_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmLocalizaFaturaValor_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			'Call CenterForm(Me)
			If Not ifrmQuemChama Is Nothing Then
				ifrmQuemChama.Enabled = False
				sform = ifrmQuemChama.Name
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmLocalizaFaturaValor_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmLocalizaFaturaValor_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		Try
			If Not ifrmQuemChama Is Nothing Then
				ifrmQuemChama.Enabled = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmLocalizaFaturaValor_FormClosed" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub mshDados_DblClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mshDados.DblClick
		Try
			If Not ifrmQuemChama Is Nothing Then
				If Me.mshDados.RowSel > 0 Then
					If ifrmQuemChama.Name = "frmRecMan" Then
						'Call ifrmQuemChama.SetaValor(Me.mshDados.get_TextMatrix(Me.mshDados.RowSel, 0), Me.mshDados.get_TextMatrix(Me.mshDados.RowSel, 1))
						Call frmRecMan.SetaValor(Me.mshDados.get_TextMatrix(Me.mshDados.RowSel, 1), Me.mshDados.get_TextMatrix(Me.mshDados.RowSel, 2))
					End If
					If Not ifrmQuemChama Is Nothing Then
						ifrmQuemChama.Enabled = True
					End If
					Call Me.Close()
				End If
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: mshDados_DblClick" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

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

	Private Sub txtValor_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValor.KeyPress
		Try
			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
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: txtValor_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

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

	Private Sub MontaGrade()
		Try

			With Me.mshDados
				Call .Clear()
				.Redraw = False
				.Rows = 1
				.Cols = 13
				.set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignRightCenter)
				.set_ColAlignment(6, MSFlexGridLib.AlignmentSettings.flexAlignRightCenter)
				.set_ColAlignment(7, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(8, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(9, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(10, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(11, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(12, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				'.set_ColAlignment(13, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColWidth(1, 1500)
				.set_ColWidth(2, 1500)
				.set_ColWidth(3, 1500)
				.set_ColWidth(4, 1500)
				.set_ColWidth(5, 1500)
				.set_ColWidth(6, 1500)
				.set_ColWidth(7, 1500)
				.set_ColWidth(8, 2500)
				.set_ColWidth(9, 1500)
				.set_ColWidth(10, 1500)
				.set_ColWidth(11, 1500)
				.set_ColWidth(12, 3000)
				'.set_ColWidth(.get_Cols() - 2, 3000)
				'.set_ColWidth(.get_Cols() - 1, 2000)
				.set_TextMatrix(0, 0, "")
				.set_TextMatrix(0, 1, "Número")
				.set_TextMatrix(0, 2, "Mês/Ano")
				.set_TextMatrix(0, 3, "Zona")
				.set_TextMatrix(0, 4, "Livro")
				.set_TextMatrix(0, 5, "Conta")
				.set_TextMatrix(0, 6, "Valor Total")
				.set_TextMatrix(0, 7, "Valor Pagar")
				.set_TextMatrix(0, 8, "Vencimento")
				.set_TextMatrix(0, 9, "Pagamento")
				.set_TextMatrix(0, 10, "Situação")
				.set_TextMatrix(0, 11, "Nome")
				.set_TextMatrix(0, 12, "Classe")
				.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeColumns
				.AllowUserResizing = MSFlexGridLib.AllowUserResizeSettings.flexResizeBoth
				.AllowBigSelection = True
				.Redraw = True
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaGrade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

End Class