Option Strict Off
Option Explicit On
Friend Class frmDevolucao
	Inherits System.Windows.Forms.Form
	
	Private Sub frmDevolucao_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Me.txtEntrada.Visible = False
		Call MontaGrid()
	End Sub
	
	Private Sub MontaGrid()
		With Me.mfgDevolucao
			Call .Clear()
			.Rows = 1
			.set_ColWidth(0, 800)
			.set_TextMatrix(0, 0, "Registro")
			.set_ColWidth(1, 1000)
			.set_TextMatrix(0, 1, "Fatura")
			.set_ColWidth(2, 800)
			.set_TextMatrix(0, 2, "Mês/Ano")
			.set_ColWidth(3, 1000)
			.set_TextMatrix(0, 3, "Emissão")
			.set_ColWidth(4, 1000)
			.set_TextMatrix(0, 4, "Devolução")
			.set_ColWidth(5, 1740)
			.set_TextMatrix(0, 5, "Valor")
			.set_ColWidth(6, 640)
			.set_TextMatrix(0, 6, "Estado")
			.set_ColWidth(7, 640)
			.set_TextMatrix(0, 7, "Imprimir")
			.set_ColWidth(8, 0)
			.set_TextMatrix(0, 8, "Banco")
			.set_ColWidth(9, 500)
			.set_TextMatrix(0, 9, "Zona")
			.set_ColWidth(10, 500)
			.set_TextMatrix(0, 10, "Livro")
			.set_ColWidth(11, 800)
			.set_TextMatrix(0, 11, "Conta")
			.set_ColWidth(12, 0)
			.set_TextMatrix(0, 12, "Vecto")
			.set_ColWidth(13, 0)
			.set_TextMatrix(0, 13, "Pagto")
			.set_ColWidth(14, 0)
			.set_TextMatrix(0, 14, "grupo")
		End With
	End Sub
	
	Private Sub cmdGeraLista_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGeraLista.Click
		Dim lstrSql As String
		Dim lrstDevolucao As ADODB.Recordset
		
		lrstDevolucao = New ADODB.Recordset
		
		Me.txtEntrada.Visible = False
		
		Call MontaGrid()
		lstrSql = ""
		lstrSql = lstrSql & "SELECT "
		lstrSql = lstrSql & "       `devolucao`.`regnum`, "
		lstrSql = lstrSql & "       `devolucao`.`nf`, "
		lstrSql = lstrSql & "       `devolucao`.`mesano`, "
		lstrSql = lstrSql & "       `devolucao`.`dataemis`, "
		lstrSql = lstrSql & "       `devolucao`.`datadevol`, "
		lstrSql = lstrSql & "       `devolucao`.`valor`, "
		lstrSql = lstrSql & "       `devolucao`.`estado`, "
		lstrSql = lstrSql & "       `devolucao`.`impressao`, "
		lstrSql = lstrSql & "       `devolucao`.`banarrec`, "
		lstrSql = lstrSql & "       `devolucao`.`zona`, "
		lstrSql = lstrSql & "       `devolucao`.`livro`, "
		lstrSql = lstrSql & "       `devolucao`.`conta`, "
		lstrSql = lstrSql & "       `devolucao`.`vecto`, "
		lstrSql = lstrSql & "       `devolucao`.`pagto`, "
		lstrSql = lstrSql & "       `devolucao`.`ta` `de_ta`, "
		lstrSql = lstrSql & "       `nf`.`datavenc`, "
		lstrSql = lstrSql & "       `nf`.`ta` `nf_ta`, "
		lstrSql = lstrSql & "       `nf`.`mesano`, "
		lstrSql = lstrSql & "       `nf`.`numnf` "
		lstrSql = lstrSql & "  FROM `devolucao`, `nf` "
		lstrSql = lstrSql & " WHERE `devolucao`.`nf` = `nf`.`numnf` "
		lstrSql = lstrSql & "   AND `devolucao`.`mesano` = `nf`.`mesano` "
		If Me.OptDevolvidos.Checked = True Then 'Devolvidos
			lstrSql = lstrSql & " AND `devolucao`.`estado` = ""D"" "
		ElseIf OptDevolver.Checked = True Then  'À devolver
			lstrSql = lstrSql & " AND (`devolucao`.`estado` <> 'D' OR `devolucao`.`estado` IS NULL) "
		End If
		lstrSql = lstrSql & " ORDER BY `estado` ASC, `impressao` DESC, `regnum` DESC"
		Call Conectar(lstrSql, lrstDevolucao)
		
		If lrstDevolucao.RecordCount > 0 Then
			With Me.mfgDevolucao
				While Not lrstDevolucao.EOF
					.Rows = .Rows + 1
					'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"'
					.set_TextMatrix(.Rows - 1, 0, IIf(IsDbNull(lrstDevolucao.Fields("regnum").value), 0, lrstDevolucao.Fields("regnum").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"'
					.set_TextMatrix(.Rows - 1, 1, VB6.Format(IIf(IsDbNull(lrstDevolucao.Fields("nf").value), 0, lrstDevolucao.Fields("nf").value), "000000000"))
					'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"'
					.set_TextMatrix(.Rows - 1, 2, IIf(IsDbNull(lrstDevolucao.Fields("mesano").value), "00/0000", lrstDevolucao.Fields("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"'
					.set_TextMatrix(.Rows - 1, 3, VB6.Format(IIf(IsDbNull(lrstDevolucao.Fields("dataemis").value), "00/00/0000", lrstDevolucao.Fields("dataemis").value), "dd/mm/yyyy"))
					'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"'
					.set_TextMatrix(.Rows - 1, 4, VB6.Format(IIf(IsDbNull(lrstDevolucao.Fields("datadevol").value), "00/00/0000", lrstDevolucao.Fields("datadevol").value), "dd/mm/yyyy"))
					'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"'
					.set_TextMatrix(.Rows - 1, 5, VB6.Format(IIf(IsDbNull(lrstDevolucao.Fields("valor").value), 0, lrstDevolucao.Fields("valor").value), "standard"))
					'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"'
					.set_TextMatrix(.Rows - 1, 6, IIf(IsDbNull(lrstDevolucao.Fields("estado").value), "", lrstDevolucao.Fields("estado").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"'
					.set_TextMatrix(.Rows - 1, 7, IIf(IsDbNull(lrstDevolucao.Fields("impressao").value), "", lrstDevolucao.Fields("impressao").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"'
					.set_TextMatrix(.Rows - 1, 8, IIf(IsDbNull(lrstDevolucao.Fields("banarrec").value), "", lrstDevolucao.Fields("banarrec").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"'
					.set_TextMatrix(.Rows - 1, 9, IIf(IsDbNull(lrstDevolucao.Fields("zona").value), "", lrstDevolucao.Fields("zona").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"'
					.set_TextMatrix(.Rows - 1, 10, IIf(IsDbNull(lrstDevolucao.Fields("livro").value), "", lrstDevolucao.Fields("livro").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"'
					.set_TextMatrix(.Rows - 1, 11, IIf(IsDbNull(lrstDevolucao.Fields("conta").value), "", lrstDevolucao.Fields("conta").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"'
					.set_TextMatrix(.Rows - 1, 12, IIf(IsDbNull(lrstDevolucao.Fields("vecto").value), IIf(IsDbNull(lrstDevolucao.Fields("datavenc").value), "", lrstDevolucao.Fields("datavenc").value), lrstDevolucao.Fields("vecto").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"'
					.set_TextMatrix(.Rows - 1, 13, IIf(IsDbNull(lrstDevolucao.Fields("pagto").value), "", lrstDevolucao.Fields("pagto").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"'
					.set_TextMatrix(.Rows - 1, 14, IIf(IsDbNull(lrstDevolucao.Fields("de_ta").value), IIf(IsDbNull(lrstDevolucao.Fields("nf_ta").value), "", lrstDevolucao.Fields("nf_ta").value), lrstDevolucao.Fields("de_ta").value))
					Call lrstDevolucao.MoveNext()
				End While
				Me.txtValTot.Text = CStr(.Rows - 1)
			End With
		End If
		If lrstDevolucao.State = 1 Then Call lrstDevolucao.Close()
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub mfgDevolucao_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgDevolucao.ClickEvent
		With Me.mfgDevolucao
			If (.Col = 4 Or .Col = 6 Or .Col = 7) Then
				If .Rows >= 2 Then
					Me.txtEntrada.Text = .get_TextMatrix(.Row, .Col)
					Call Me.txtEntrada.SetBounds(VB6.TwipsToPixelsX(.CellLeft + VB6.PixelsToTwipsX(.Left)), VB6.TwipsToPixelsY(.CellTop + VB6.PixelsToTwipsY(.Top)), VB6.TwipsToPixelsX(.CellWidth), VB6.TwipsToPixelsY(.CellHeight))
					Me.txtEntrada.Visible = True
					Me.txtEntrada.Focus()
				End If
			Else
				Me.txtEntrada.Visible = False
			End If
		End With
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub txtEntrada_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEntrada.Enter
		With Me.txtEntrada
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtEntrada_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEntrada.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then
			Call txtEntrada_Leave(txtEntrada, New System.EventArgs())
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtEntrada_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEntrada.Leave
		With Me.txtEntrada
			.Text = UCase(.Text)
			.Visible = False
			If Me.mfgDevolucao.Col = 4 Then 'Data Devolução
				.Text = FormataData(Me.txtEntrada, 2)
			ElseIf Me.mfgDevolucao.Col = 6 Then  'Estado do Registro
				If .Text <> "D" And .Text <> "N" Then
					.Text = "N"
				End If
			ElseIf Me.mfgDevolucao.Col = 7 Then  'Imprimir
				If .Text <> "S" And .Text <> "N" Then
					.Text = "N"
				End If
			End If
		End With
		With Me.mfgDevolucao
			If .Col = 4 Or .Col = 6 Or .Col = 7 Then
				.set_TextMatrix(.Row, .Col, Me.txtEntrada.Text)
			End If
		End With
	End Sub
	
	Private Sub cmdDel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDel.Click
		With Me.mfgDevolucao
			If .RowSel > 0 Then
				If .Rows > 2 Then
					.RemoveItem((.RowSel))
				Else
					.Rows = 1
				End If
			End If
			Me.txtValTot.Text = CStr(.Rows - 1)
		End With
	End Sub
	
	Private Sub cmdImp_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImp.Click
		Dim lstrSql As String
		Dim llngConta As Integer
		Dim ldblNumNF As Double
		Dim lstrMesAno As String
		Dim ldblValor As Double
		Dim lstrImprimir As String
		
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrRota As String
		Dim ldtVcto As Date
		Dim ldtPgto As Date
		Dim ldtDupl As Date
		
		Dim lstrBanco As String
		Dim lstrGrTar As String
		Dim lstrAgenc As String
		Dim lstrCc As String
		Dim lstrNome As String
		Dim lstrEnd As String
		Dim lstrComplement As String
		Dim lstrCep As String
		Dim lstrBairro As String
		Dim lstrCidade As String
		Dim lstrUf As String
		Dim lstrCpfCgc As String
		Dim lstrRg As String
		Dim lstrInsc As String
		Dim lstrComplEnt As String
		Dim llngUnidId As Integer
		Dim llngConsId As Integer
		Dim lrstConsulta As ADODB.Recordset
		
		lrstConsulta = New ADODB.Recordset
		
		lstrSql = "DELETE FROM `temp_devolucao` "
		Call Executar(lstrSql)
		
		
		For llngConta = 1 To Me.mfgDevolucao.Rows - 1
			With Me.mfgDevolucao
				ldblNumNF = CDbl(.get_TextMatrix(llngConta, 1))
				lstrMesAno = .get_TextMatrix(llngConta, 2)
				ldtDupl = IIf(IsDate(.get_TextMatrix(llngConta, 3)), .get_TextMatrix(llngConta, 3), "01/01/1900")
				ldblValor = CDbl(.get_TextMatrix(llngConta, 5))
				lstrImprimir = .get_TextMatrix(llngConta, 7)
				lstrBanco = .get_TextMatrix(llngConta, 8)
				lstrZona = .get_TextMatrix(llngConta, 9)
				lstrLivro = .get_TextMatrix(llngConta, 10)
				lstrConta = .get_TextMatrix(llngConta, 11)
				ldtVcto = IIf(IsDate(.get_TextMatrix(llngConta, 12)), .get_TextMatrix(llngConta, 12), "01/01/1900")
				ldtPgto = IIf(IsDate(.get_TextMatrix(llngConta, 13)), .get_TextMatrix(llngConta, 13), "01/01/1900")
				lstrGrTar = .get_TextMatrix(llngConta, 14)
			End With
			If lstrImprimir = "S" Then
				lstrSql = "SELECT f.zona, f.livro, f.conta, f.rota, f.datavenc, f.datapag, f.ta, f.cons_id, f.unid_id " & "  FROM nf f" & " WHERE f.numnf = " & ldblNumNF & "   AND f.mesano = """ & lstrMesAno & """ "
				If Len(lstrZona & lstrLivro & lstrConta) > 0 Then
					lstrSql = lstrSql & " AND f.zona = '" & lstrZona & "' " & "   AND f.livro = '" & lstrLivro & "' " & "   AND f.conta = '" & lstrConta & "' "
				End If
				Call Conectar(lstrSql, lrstConsulta)
				With lrstConsulta
					If .RecordCount > 0 Then
						'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"'
						lstrZona = VB6.Format(IIf(IsDbNull(.Fields("zona").value), "0", .Fields("zona").value), "000")
						'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"'
						lstrLivro = VB6.Format(IIf(IsDbNull(.Fields("livro").value), "0", .Fields("livro").value), "00")
						'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"'
						lstrConta = VB6.Format(IIf(IsDbNull(.Fields("conta").value), "0", .Fields("conta").value), "000000")
						'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"'
						lstrRota = VB6.Format(IIf(IsDbNull(.Fields("rota").value), "0", .Fields("rota").value), "00000000")
						'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"'
						ldtVcto = IIf(IsDbNull(.Fields("datavenc").value), gDtBcoDados, .Fields("datavenc").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"'
						ldtPgto = IIf(IsDbNull(.Fields("datapag").value), gDtBcoDados, .Fields("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"'
						lstrGrTar = IIf(IsDbNull(.Fields("ta").value), "", .Fields("ta").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"'
						llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").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"'
						llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value)
					Else
						lstrRota = ""
						llngConsId = 0
						llngUnidId = 0
					End If
					If .State = 1 Then Call .Close()
				End With
				
				lstrSql = "SELECT u.agenc, u.cc, u.nome, u.end, u.complement, u.cep, u.bairro, " & "       u.cidade, u.uf, u.cpf_cnpj, u.rg, u.insc, u.rota, u.ta, u.cons_id, u.unid_id " & "  FROM unid" & LCase(Mid(lstrGrTar, 3, 1)) & " u " & " WHERE u.zona = '" & lstrZona & "' " & "   AND u.livro = '" & lstrLivro & "' " & "   AND u.conta = '" & lstrConta & "' "
				Call Conectar(lstrSql, lrstConsulta)
				With lrstConsulta
					If .RecordCount > 0 Then
						'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"'
						lstrAgenc = IIf(IsDbNull(.Fields("agenc").value), "", .Fields("agenc").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"'
						lstrCc = IIf(IsDbNull(.Fields("cc").value), "", .Fields("cc").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"'
						lstrNome = IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").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"'
						lstrEnd = IIf(IsDbNull(.Fields("end").value), "", .Fields("end").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"'
						lstrComplement = IIf(IsDbNull(.Fields("complement").value), "", .Fields("complement").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"'
						lstrCep = IIf(IsDbNull(.Fields("cep").value), "", .Fields("cep").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"'
						lstrBairro = IIf(IsDbNull(.Fields("bairro").value), "", .Fields("bairro").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"'
						lstrCidade = IIf(IsDbNull(.Fields("cidade").value), "", .Fields("cidade").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"'
						lstrUf = IIf(IsDbNull(.Fields("uf").value), "", .Fields("uf").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"'
						lstrCpfCgc = IIf(IsDbNull(.Fields("cpf_cnpj").value), "", .Fields("cpf_cnpj").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"'
						lstrRg = IIf(IsDbNull(.Fields("rg").value), "", .Fields("rg").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"'
						lstrInsc = IIf(IsDbNull(.Fields("insc").value), "", .Fields("insc").value)
						If llngConsId = 0 And llngUnidId = 0 Then
							'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"'
							lstrRota = VB6.Format(IIf(IsDbNull(.Fields("rota").value), "0", .Fields("rota").value), "00000000")
							'lstrGrTar = IIf(IsNull(.Fields("ta").Value), "", .Fields("ta").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"'
							llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").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"'
							llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value)
						End If
					End If
					If .State = 1 Then Call .Close()
				End With
				
				lstrSql = "INSERT INTO temp_devolucao SET " & "numnf       = " & ldblNumNF & "," & "mesano      = """ & lstrMesAno & """," & "zona        = """ & lstrZona & """," & "livro       = """ & lstrLivro & """," & "conta       = """ & lstrConta & """," & "rota        = """ & lstrRota & """," & "agenc       = """ & lstrAgenc & """," & "cc          = """ & lstrCc & """," & "valortot    = """ & Replace(CStr(ldblValor), ",", ".") & """," & "datavenc    = """ & VB6.Format(ldtVcto, "yyyy-MM-dd") & """," & "datapag     = """ & VB6.Format(ldtPgto, "yyyy-MM-dd") & """," & "dataduplic  = """ & VB6.Format(ldtDupl, "yyyy-MM-dd") & """," & "banco_fa    = """ & BuscaNMBanco(lstrBanco) & """," & "nome        = """ & lstrNome & """," & "rua         = """ & lstrEnd & """," & "comple      = """ & lstrComplEnt & """," & "cep         = """ & lstrCep & """," & "bairro      = """ & lstrBairro & """," & "cidade      = """ & lstrCidade & """," & "uf          = """ & lstrUf & """," & "cpf         = """ & lstrCpfCgc & """," & "rg          = """ & lstrRg & """," & "insc        = """ & lstrInsc & """," & "valorextenso= """ & Extenso(ldblValor, "Reais", "Real") & """ "
				Call Executar(lstrSql)
			End If
		Next llngConta
		
		'Abre o Recibo no Crystal View
		With Me.crpAux
			.ReportFileName = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "ReciboPagtoDuplicidade.rpt"
			.DiscardSavedData = True
			.WindowState = Crystal.WindowStateConstants.crptMaximized
			.WindowControlBox = True
			.WindowControls = True
			.WindowShowPrintSetupBtn = True
			.WindowShowCloseBtn = True
			.WindowShowSearchBtn = True
			.WindowTitle = "Recibo de Pagto em Duplicidade"
			.Destination = Crystal.DestinationConstants.crptToWindow
			.SelectionFormula = ""
			.Action = 0
		End With
		
	End Sub
	
	Private Sub cmdExec_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExec.Click
		Dim strSql As String
		Dim lngConta As Integer
		With Me.mfgDevolucao
			For lngConta = 1 To .Rows - 1
				strSql = "UPDATE devolucao SET " & "                     datadevol = '" & VB6.Format(.get_TextMatrix(lngConta, 4), "yyyy-MM-dd") & "'," & "                     estado='D'" & " WHERE regnum = '" & .get_TextMatrix(lngConta, 0) & "'"
				Call Executar(strSql)
			Next lngConta
		End With
		MsgBox("Gravação concluida!", MsgBoxStyle.Information, gstrTituloMensagem)
		mfgDevolucao.Clear()
		mfgDevolucao.Rows = 1
	End Sub
	
	Private Sub cmdListagem_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdListagem.Click
		Dim llngConta As Integer
		Dim llngUnidId As Integer
		Dim llngConsId As Integer
		Dim lstrGrTar As String
		Dim lstrTabela As String
		Dim ldblNumNF As Double
		Dim lstrMesAno As String
		Dim lstrEmissao As String
		Dim lstrDevolucao As String
		Dim ldblValor As Double
		Dim lstrSql As String
		Dim lstrNome As String
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrTamCol As String
		Dim lstrTemp As String
		Dim lfrmImpressao As New FrmRelatorio
		Dim lrstConsulta As ADODB.Recordset
		
		'Set lfrmImpressao = New FrmRelatorio
		lrstConsulta = New ADODB.Recordset
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		
		lstrTamCol = "<2mm|<22mm|<17mm|^15mm|<70mm|^18mm|^18mm|>25mm;|"
		With lfrmImpressao
			.quem_chama = Me
			.VS.Font = VB6.FontChangeName(.VS.Font, "Arial")
			.VS.TableBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.PageBorder = VSVIEW6.TableBorderSettings.tbNone
			.VS.Orientation = VSVIEW6.OrientationSettings.orPortrait
			.VS.PhysicalPage = True
			.VS.PaperSize = VSVIEW6.PaperSizeSettings.pprA4
			.VS.MarginTop = "7mm"
			.VS.MarginRight = "7mm"
			.VS.MarginLeft = "7mm"
			.VS.MarginBottom = "5mm"
			.VS.TextColor = System.Drawing.Color.Black
			.VS.BrushStyle = VSVIEW6.BrushStyleSettings.bsSolid
			Call .VS.StartDoc()
		End With
		Call CabecaRel((lfrmImpressao.VS), lstrTamCol)
		
		For llngConta = 1 To Me.mfgDevolucao.Rows - 1
			ldblNumNF = CDbl(mfgDevolucao.get_TextMatrix(llngConta, 1))
			lstrMesAno = mfgDevolucao.get_TextMatrix(llngConta, 2)
			lstrEmissao = mfgDevolucao.get_TextMatrix(llngConta, 3)
			lstrDevolucao = mfgDevolucao.get_TextMatrix(llngConta, 4)
			ldblValor = CDbl(mfgDevolucao.get_TextMatrix(llngConta, 5))
			lstrSql = "SELECT nf.unid_id, nf.cons_id, nf.ta " & "  FROM nf " & " WHERE nf.numnf = " & ldblNumNF & "   AND nf.mesano = """ & lstrMesAno & """ " & " LIMIT 1 "
			Call Conectar(lstrSql, lrstConsulta)
			With lrstConsulta
				If .RecordCount > 0 Then
					'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"'
					llngUnidId = IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").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"'
					llngConsId = IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").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"'
					lstrZona = IIf(IsDbNull(.Fields("zona").value), "000", .Fields("zona").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"'
					lstrLivro = IIf(IsDbNull(.Fields("livro").value), "00", .Fields("livro").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"'
					lstrConta = IIf(IsDbNull(.Fields("conta").value), "000000", .Fields("conta").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"'
					lstrGrTar = IIf(IsDbNull(.Fields("ta").value), "01B1", .Fields("ta").value)
					lstrTabela = "unid" & LCase(Mid(lstrGrTar, 3, 1))
				Else
					llngUnidId = 0
					llngConsId = 0
					lstrZona = ""
					lstrLivro = ""
					lstrConta = ""
					lstrGrTar = "01B1"
					lstrTabela = "unid" & LCase(Mid(lstrGrTar, 3, 1))
				End If
				If .State = 1 Then Call .Close()
			End With
			lstrSql = "SELECT " & lstrTabela & ".nome " & "  FROM " & lstrTabela & " WHERE " & lstrTabela & ".unid_id = " & llngUnidId & "   AND " & lstrTabela & ".cons_id = " & llngConsId & " LIMIT 1 "
			Call Conectar(lstrSql, lrstConsulta)
			With lrstConsulta
				If .RecordCount > 0 Then
					'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"'
					lstrNome = IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)
				Else
					If .State = 1 Then Call .Close()
					lstrSql = "SELECT consum.nome " & "  FROM consum " & " WHERE consreg = " & llngConsId & " LIMIT 1 "
					Call Conectar(lstrSql, lrstConsulta)
					With lrstConsulta
						If .RecordCount > 0 Then
							'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"'
							lstrNome = IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)
						Else
							lstrNome = ""
						End If
					End With
				End If
				If .State = 1 Then Call .Close()
			End With
			lstrTemp = lstrZona & "." & lstrLivro & "." & lstrConta & "|" & VB6.Format(ldblNumNF, "000000000") & "|" & lstrMesAno & "|" & lstrNome & "|" & lstrEmissao & "|" & lstrDevolucao & "|" & ldblValor
			lfrmImpressao.VS.Table = lstrTamCol & lstrTemp
			If lfrmImpressao.VS.CurrentLine > 402 Then
				Call CabecaRel((lfrmImpressao.VS), lstrTamCol)
			End If
		Next llngConta
		lfrmImpressao.VS.EndDoc()
		lfrmImpressao.Show()
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Function CabecaRel(ByRef aVs As AxVSVIEW6.AxVSPrinter, ByRef astrTamcol As String) As Object
		Call Cabecalho_Relatorio(aVs, "Listagem das Devoluções", "", "2",  , "")
		Call LineFeed(aVs, 2)
		aVs.Font = VB6.FontChangeBold(aVs.Font, True)
		aVs.BrushColor = System.Drawing.Color.White
		aVs.Font = VB6.FontChangeSize(aVs.Font, 8)
		aVs.Table = astrTamcol & "Identificação|Fatura|Mês/Ano|Nome|Emissão|Devolução|Valor"
		aVs.Font = VB6.FontChangeBold(aVs.Font, False)
	End Function
	
	Private Function BuscaNMBanco(ByRef astrBanco As String) As String
		Dim strSql As String
		Dim lngBanco As Integer
		Dim rsBanco As New ADODB.Recordset
		
		lngBanco = CInt(astrBanco)
		
		strSql = "SELECT nmbco FROM bancos WHERE ban_id =" & lngBanco
		Call Conectar(strSql, rsBanco)
		If rsBanco.RecordCount > 0 Then
			'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"'
			BuscaNMBanco = IIf(IsDbNull(rsBanco.Fields("nmbco").Value), "", rsBanco.Fields("nmbco").Value)
		Else
			BuscaNMBanco = ""
		End If
		If rsBanco.State = 1 Then rsBanco.Close()
	End Function
End Class