Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmCaixa
	Inherits System.Windows.Forms.Form
	
	Private AUTENTICA_ON As String
	Private AUTENTICA_OFF As String
	
	Private ilngCodCaixa As Integer
	Private gstrSitCaixa As String
	Private gstrZona As String
	Private gstrLivro As String
	Private gstrConta As String
	Private gdblNumNf As Double
	Private gstrMesAno As String
	Private gintSeq As Short
	Private gintNumAut As Short
	Private gdblTotAut As Double
	Private gdblVlrDoc As Double
	Private gintPosicao As Short
	Private gstrEstado As String
	Private gstrAtivar As String
	Private dtAtualBd As Date 'Pega a data do banco de dados
	Private glngBanId As Integer
	Private gstrNmBco As String
	Private gblnPermite As Boolean
	Private gblnCxAberto As Boolean
	Private Declare Sub Sleep Lib "kernel32"  Alias "sleep"(ByVal dwMilliseconds As Integer)
	
	
	Private Sub cmdAutCheque_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAutCheque.Click
		Dim strCheque As String
		Dim intResposta As Short
		
		If Me.opbCheque.Checked Then
			strCheque = gstrZona & " " & gstrLivro & " " & gstrConta
voltar: 
			If MsgBox("Autenticar o verso do cheque", MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				Call Autenticar(strCheque)
				GoTo voltar
			End If
		End If
	End Sub
	
	Private Sub frmCaixa_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Me.txtTotPagar.Text = "0,00"
		Me.txtTroco.Text = "0,00"
		Me.txtDinheiro.Text = "0,00"
		gdblTotAut = 0
		'Me.txtCodBarra.SetFocus
		AUTENTICA_ON = Chr(27) & Chr(126) & Chr(49)
		AUTENTICA_OFF = Chr(27) & Chr(126) & Chr(48)
		
		gblnCxAberto = False
		'Checa se o usuario tem permissao para executar a opcao de caixa
		gblnPermite = (glngNumCaixa > 0)
		If gblnPermite Then
			'Inicializa a Porta da Impressora Fiscal
			Call Init()
			
			'Busca a Quantidade de autenticacao que o caixa deve fazer por fatura recebida
			gintNumAut = CShort(LeConfig("NUM_AUTENTICACOES"))
			
			'Busca a data do banco
			dtAtualBd = BuscaDataBD
			Call checaCaixa()
			
			If gstrSitCaixa = "A" Then
				Call ListaFaturas()
			Else
				Me.cmdAutenticar.Enabled = False
				Me.cmdEstornar.Enabled = False
				'Me.cmdZera.Enabled = False
				Me.FrameCodBarra.Enabled = False
				Me.FrameTotais.Enabled = False
				Me.Frame1(0).Enabled = False
				Me.FramePgto.Enabled = False
				Me.grid.Enabled = False
				System.Windows.Forms.Application.DoEvents()
			End If
			glngBanId = CInt(LeConfig("BANCO_CAIXA"))
			gstrNmBco = GetNomeBanco(glngBanId)
			If gstrEmpresa_Sigla <> "HIDROPAN" Then
				Me.FrameTaxas.Enabled = False
				Me.FrameDeposito.Enabled = False
			End If
		Else
			Call MsgBox("Você não tem permissão para manipular o caixa!", MsgBoxStyle.Critical, gstrTituloMensagem)
			Me.FrameCodBarra.Enabled = False
			Me.cmdAutenticar.Enabled = False
			Me.cmdEstornar.Enabled = False
			'Me.cmdZera.Enabled = False
			Me.cmdAbreFechaCaixa.Enabled = False
			Me.FrameTotais.Enabled = False
			Me.Frame1(0).Enabled = False
			Me.FramePgto.Enabled = False
			Call Me.Close()
		End If
	End Sub
	
	Private Sub frmCaixa_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
		Dim KeyCode As Short = eventArgs.KeyCode
		Dim Shift As Short = eventArgs.KeyData \ &H10000
		If gblnCxAberto Then
			If gblnPermite Then
				Select Case KeyCode
					Case System.Windows.Forms.Keys.F2
						gintPosicao = 2
						Call habilitarFatura(False)
						Call habilitarCodBarra(True)
						Call habilitarTaxas(False)
						Call habilitarDeposito(False)
						Call LimpaCampos()
					Case System.Windows.Forms.Keys.F3
						gintPosicao = 3
						Call habilitarFatura(True)
						Call habilitarCodBarra(False)
						Call habilitarTaxas(False)
						Call habilitarDeposito(False)
						Call LimpaCampos()
					Case System.Windows.Forms.Keys.F4
						If gstrEmpresa_Sigla = "HIDROPAN" Then
							gintPosicao = 4
							Call habilitarFatura(False)
							Call habilitarCodBarra(False)
							Call habilitarTaxas(True)
							Call habilitarDeposito(False)
							Call LimpaCampos()
						End If
					Case System.Windows.Forms.Keys.F5
						gintPosicao = 5
						Call habilitarFatura(False)
						Call habilitarCodBarra(False)
						Call habilitarTaxas(False)
						Call habilitarDeposito(True)
						Call LimpaCampos()
					Case System.Windows.Forms.Keys.F6
						Call cmdAutenticar_Click(cmdAutenticar, New System.EventArgs())
					Case System.Windows.Forms.Keys.F8
						Call cmdZera_Click(cmdZera, New System.EventArgs())
					Case System.Windows.Forms.Keys.F9
						Call cmdAutCheque_Click(cmdAutCheque, New System.EventArgs())
					Case System.Windows.Forms.Keys.F7
						If gstrEmpresa_Sigla = "HIDROPAN" Then
							gstrAtivar = "ok"
							Me.FrameSenhaAcesso.Visible = True
							Me.cmdAutenticar.Enabled = False
							Me.txtSenha.Focus()
						Else
							Me.cmdAutenticar.Enabled = False
						End If
				End Select
			End If
		Else
			Call MsgBox("Você de abrir o caixa primeiro!!!", MsgBoxStyle.Information, gstrTituloMensagem)
			Me.cmdAbreFechaCaixa.Focus()
		End If
	End Sub
	
	Private Sub ListaFaturas()
		Dim lngLinha As Integer
		Dim dtCaixa As Date
		Dim strSql As String
		Dim dblEnergia As Double
		Dim dblVlrRecebido As Double
		Dim dblVlrTaxas As Double
		Dim dblVlrDeposito As Double
		Dim rsLista As ADODB.Recordset
		
		rsLista = New ADODB.Recordset
		
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Call Me.grid.Clear()
		Me.grid.Rows = 2
		
		Me.txtQtdRecbtos.Text = CStr(0)
		Me.txtValorRectos.Text = CStr(0)
		
		Me.txtQtdSaldo.Text = CStr(0)
		Me.txtValorSaldo.Text = CStr(CDbl(Me.txtSaldo.Text))
		
		Me.txtQtdExtorno.Text = CStr(0)
		Me.txtValorExtorno.Text = CStr(0)
		
		Me.txtVrTX.Text = CStr(0)
		Me.txtQtTx.Text = CStr(0)
		
		Me.txtVrDP.Text = CStr(0)
		Me.txtQtDP.Text = CStr(0)
		
		dblEnergia = 0
		dblVlrRecebido = 0
		strSql = "SELECT * " & "  FROM caixa_itens " & " WHERE codcaixa = " & glngNumCaixa & " " & "   AND dataproc = """ & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """ "
		Call Conectar(strSql, rsLista)
		If rsLista.RecordCount > 0 Then
			For lngLinha = 1 To rsLista.RecordCount
				'monta list
				With Me.grid
					.Rows = .Rows + 1
					.set_TextMatrix(lngLinha, 0, rsLista.Fields("numnf").Value)
					.set_TextMatrix(lngLinha, 1, rsLista.Fields("mesano").Value)
					.set_TextMatrix(lngLinha, 2, rsLista.Fields("zona").Value & "." & rsLista.Fields("livro").Value & "." & rsLista.Fields("Conta").Value)
					.set_TextMatrix(lngLinha, 3, VB6.Format(rsLista.Fields("valor").Value, "Standard"))
					.set_TextMatrix(lngLinha, 4, rsLista.Fields("tipo").Value)
				End With
				'Energia Recebida
				If VB.Left(rsLista.Fields("tipo").Value, 2) = "EN" Then
					Me.txtQtdRecbtos.Text = CStr(Val(Me.txtQtdRecbtos.Text) + 1)
					Me.txtValorRectos.Text = CStr(Val(Str(CDbl(Me.txtValorRectos.Text))) + Val(Str(rsLista.Fields("valor").Value)))
				End If
				'Taxas Recebidas
				If VB.Left(rsLista.Fields("tipo").Value, 2) = "TX" Then
					Me.txtQtTx.Text = CStr(Val(Me.txtQtTx.Text) + 1)
					Me.txtVrTX.Text = CStr(Val(Str(CDbl(Me.txtVrTX.Text))) + Val(Str(rsLista.Fields("valor").Value)))
				End If
				'Retirada
				If VB.Left(rsLista.Fields("tipo").Value, 2) = "RE" Then
					Me.txtQtDP.Text = CStr(Val(Me.txtQtDP.Text) + 1)
					Me.txtVrDP.Text = CStr(Val(Str(CDbl(Me.txtVrDP.Text))) + Val(Str(rsLista.Fields("valor").Value)))
				End If
				'Estornos
				If VB.Left(rsLista.Fields("tipo").Value, 2) = "ES" Then
					Me.txtQtdExtorno.Text = CStr(Val(Me.txtQtdExtorno.Text) + 1)
					Me.txtValorExtorno.Text = CStr(Val(Str(CDbl(Me.txtValorExtorno.Text))) + Val(Str(rsLista.Fields("valor").Value)))
					If VB.Left(rsLista.Fields("tipo").Value, 3) = "ESE" Then
						'dblEnergia = dblEnergia + Val(Str(rsLista.Fields("valor")))
						
					ElseIf VB.Left(rsLista.Fields("tipo").Value, 3) = "ESR" Then 
						'dblVlrDeposito = dblVlrDeposito + Val(Str(rsLista.Fields("valor")))
						
					ElseIf VB.Left(rsLista.Fields("tipo").Value, 3) = "EST" Then 
						'dblVlrTaxas = dblVlrTaxas + Val(Str(rsLista.Fields("valor")))
						
					End If
				End If
				'Saldo
				If VB.Left(rsLista.Fields("tipo").Value, 2) <> "ES" And VB.Left(rsLista.Fields("tipo").Value, 2) <> "RE" Then
					Me.txtQtdSaldo.Text = CStr(Val(Me.txtQtdSaldo.Text) + 1)
					Me.txtValorSaldo.Text = CStr(Val(Str(CDbl(Me.txtValorSaldo.Text))) + Val(Str(rsLista.Fields("valor").Value)))
				End If
				Call rsLista.MoveNext()
				System.Windows.Forms.Application.DoEvents()
			Next lngLinha
		End If
		Me.txtQtdSaldo.Text = CStr((Val(Me.txtQtdRecbtos.Text) + Val(Me.txtQtDP.Text) + Val(Me.txtQtTx.Text)) - Val(Me.txtQtdExtorno.Text))
		Me.txtValorSaldo.Text = CStr(Val(Str(CDbl(Me.txtValorSaldo.Text))) - (Val(Str(CDbl(Me.txtValorExtorno.Text))) + Val(Str(CDbl(Me.txtVrDP.Text)))))
		dblVlrRecebido = Val(Str(CDbl(Me.txtValorRectos.Text))) - dblEnergia
		Me.txtValorRectos.Text = VB6.Format(dblVlrRecebido, "Standard")
		Me.txtValorExtorno.Text = VB6.Format(Me.txtValorExtorno.Text, "Standard")
		Me.txtValorSaldo.Text = VB6.Format(Me.txtValorSaldo.Text, "Standard")
		Me.txtVrDP.Text = CStr(Val(Str(CDbl(Me.txtVrDP.Text))) - dblVlrDeposito)
		Me.txtVrDP.Text = VB6.Format(Me.txtVrDP.Text, "Standard")
		Me.txtVrTX.Text = CStr(Val(Str(CDbl(Me.txtVrTX.Text))) - dblVlrTaxas)
		Me.txtVrTX.Text = VB6.Format(Me.txtVrTX.Text, "Standard")
		With grid
			.Rows = .Rows - 1
			.set_TextMatrix(0, 0, "Nº Fatura")
			.set_TextMatrix(0, 1, "Mes/Ano")
			.set_TextMatrix(0, 2, "Zona Livro Conta")
			.set_TextMatrix(0, 3, "Valor")
			.set_TextMatrix(0, 4, "Sit*")
			.set_ColWidth(0, 900)
			.set_ColWidth(1, 900)
			.set_ColWidth(2, 1500)
			.set_ColWidth(4, 500)
		End With
		Me.Cursor = System.Windows.Forms.Cursors.Default
		If rsLista.State = 1 Then Call rsLista.Close()
		'UPGRADE_NOTE: Object rsLista 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"'
		rsLista = Nothing
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub cmdAbreFechaCaixa_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAbreFechaCaixa.Click
		Dim strSql As String
		Dim dblVrlSaldo As Double
		
		glngNumCaixa = ilngCodCaixa
		If gstrSitCaixa = "F" Then
			dblVrlSaldo = CDbl(Me.txtSaldo.Text)
			Call Imprimir("")
			Call Imprimir("================================================")
			Call Imprimir("    ABERTURA DO CAIXA No. " & glngNumCaixa)
			Call Imprimir("          DATA: " & VB6.Format(dtAtualBd, "dd-mm-yyyy"))
			Call Imprimir("     Saldo de Abertura: " & VB6.Format(dblVrlSaldo, "standard"))
			Call Imprimir("=================================================")
			Call Imprimir("")
			strSql = "INSERT INTO caixa (codigo, numcaixa, dtcaixa, estado, saldo) " & "           VALUES (0, " & glngNumCaixa & ", """ & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """, """ & "A "", " & Replace(CStr(dblVrlSaldo), ",", ".") & ")"
			Call Executar(strSql)
			
			Call checaCaixa()
			Me.cmdAutenticar.Enabled = True
			'Me.cmdEstornar.Enabled = True
			'Me.cmdZera.Enabled = True
			'Me.FrameCodBarra.Enabled = True
			Me.FrameTotais.Enabled = True
			Me.Frame1(0).Enabled = True
			Me.FramePgto.Enabled = True
			Me.grid.Enabled = True
			gblnCxAberto = True
			gintPosicao = 2
			Call habilitarFatura(False)
			Call habilitarCodBarra(True)
			Call habilitarTaxas(False)
			Call habilitarDeposito(False)
			Call LimpaCampos()
		Else
			If MsgBox("Já foi feito a retirada do caixa do dia " & Me.txtData.Text & "?", MsgBoxStyle.YesNoCancel, gstrTituloMensagem) = MsgBoxResult.Yes Then
				If MsgBox("Deseja realmente fechar o caixa do dia " & Me.txtData.Text & "?", MsgBoxStyle.YesNoCancel, gstrTituloMensagem) = MsgBoxResult.Yes Then
					Call Imprimir("")
					Call Imprimir("================================================")
					Call Imprimir("    FECHAMENTO DO CAIXA No. " & glngNumCaixa)
					Call Imprimir("          DATA: " & VB6.Format(dtAtualBd, "dd-mm-yyyy"))
					Call Imprimir("")
					Call Imprimir("No DE AUTENTICAÇÕES: " & VB6.Format(Me.txtQtdSaldo.Text, "00000"))
					dblVrlSaldo = CDbl(Me.txtSaldo.Text)
					Call Imprimir("SALDO ANTERIOR: " & PreencheCampo(VB6.Format(dblVrlSaldo, "standard"), 20, " ", "E") & "       ")
					Call Imprimir("VALOR ENERGIA : " & PreencheCampo(VB6.Format(Me.txtValorRectos.Text, "standard") & "(" & VB6.Format(Me.txtQtdRecbtos.Text, "00000") & ")", 20, " ", "E"))
					Call Imprimir("VALOR TAXAS   : " & PreencheCampo(VB6.Format(Me.txtVrTX.Text, "standard") & "(" & VB6.Format(Me.txtQtTx.Text, "00000") & ")", 20, " ", "E"))
					Call Imprimir("VALOR RETIRADA: " & PreencheCampo(VB6.Format(Me.txtVrDP.Text, "standard") & "(" & VB6.Format(Me.txtQtDP.Text, "00000") & ")", 20, " ", "E"))
					Call Imprimir("VALOR ESTORNO : " & PreencheCampo(VB6.Format(Me.txtValorExtorno.Text, "standard") & "(" & VB6.Format(Me.txtQtdExtorno.Text, "00000") & ")", 20, " ", "E"))
					Call Imprimir("")
					dblVrlSaldo = CDbl(Me.txtValorSaldo.Text)
					Call Imprimir("          SALDO FINAL : " & VB6.Format(dblVrlSaldo, "standard"))
					Call Imprimir("")
					Call Imprimir("================================================")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("CAIXA ..........................................")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("VISTO ...........................................")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("================================================")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					Call Imprimir("")
					strSql = "UPDATE caixa SET " & "                 estado = ""F"", " & "                 saldo = """ & Replace(CStr(dblVrlSaldo), ",", ".") & """ " & " WHERE numcaixa = " & glngNumCaixa & "   AND dtcaixa = """ & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """ "
					Call Executar(strSql)
					Call checaCaixa()
					Me.txtData.Text = ""
					Me.txtSaldo.Text = ""
					Me.txtCaixaUser.Text = ""
					Call checaCaixa()
					gblnCxAberto = False
				End If
			End If
		End If
	End Sub
	
	Private Sub processaCodBarra(ByVal strCodBarra As String)
		Dim intAuxCF As Short
		Dim lngAux As Integer
		Dim strZona As String
		Dim strLivro As String
		Dim strConta As String
		Dim strMesIni As String
		
		
		strMesIni = LeConfig("MES_INICIO_FATWIN")
		
		If Len(Trim(strCodBarra)) > 0 Then 'Verifica se foi digitado algo no codigo de barras
			If Len(strCodBarra) = 44 Then 'Verifica se foi digitado somente 44 digitos do codigo de barras
				If gstrEmpresa_Sigla = "HIDROPAN" Then
					If CDbl(Mid(strCodBarra, 31, 2)) < 20 Then
						gdblNumNf = CDbl(Val(Mid(strCodBarra, 20, 9)))
						gstrMesAno = Trim(Mid(strCodBarra, 29, 2) & "/" & IIf(Val(Mid(strCodBarra, 31, 2)) < 50, "20" & Mid(strCodBarra, 31, 2), "19" & Mid(strCodBarra, 31, 2)))
						strZona = Mid(Trim(strCodBarra), 33, 3)
						strLivro = Mid(Trim(strCodBarra), 36, 2)
						strConta = Mid(Trim(strCodBarra), 38, 7)
						strConta = VB6.Format(strConta, "000000")
					Else
						gdblNumNf = CDbl(Mid(strCodBarra, 20, 9))
						strZona = Mid(strCodBarra, 35, 3)
						strLivro = Mid(strCodBarra, 38, 2)
						strConta = Mid(strCodBarra, 40, 5)
						strConta = VB6.Format(strConta, "000000")
						gstrMesAno = Mid(strCodBarra, 29, 2) & "/" & Mid(strCodBarra, 31, 4)
					End If
				ElseIf gstrEmpresa_Sigla = "COORSEL" Then 
					If Val(VB.Left(VB.Right(Mid(strCodBarra, 20, 13), 4), 2)) < 20 Then 'é Windows
						gdblNumNf = CDbl(Val(Mid(strCodBarra, 20, 9)))
						gstrMesAno = Trim(Mid(strCodBarra, 29, 2) & "/" & IIf(Val(Mid(strCodBarra, 31, 2)) < 50, "20" & Mid(strCodBarra, 31, 2), "19" & Mid(strCodBarra, 31, 2)))
						strZona = Mid(Trim(strCodBarra), 33, 3)
						strLivro = Mid(Trim(strCodBarra), 36, 2)
						strConta = Mid(Trim(strCodBarra), 38, 7)
						strConta = VB6.Format(strConta, "000000")
					Else
						gdblNumNf = CDbl(Mid(strCodBarra, 20, 5))
						gstrMesAno = Mid(strCodBarra, 27, 2) & "/" & Mid(strCodBarra, 29, 4)
						strZona = Mid(strCodBarra, 33, 3)
						strLivro = Mid(strCodBarra, 36, 2)
						strConta = Mid(strCodBarra, 38, 6)
						strConta = VB6.Format(strConta, "000000")
					End If
				ElseIf gstrEmpresa_Sigla = "CHESP" Then 
					If Val(VB.Left(VB.Right(Mid(strCodBarra, 20, 13), 4), 2)) < 20 Then 'é Windows
						gdblNumNf = CDbl(Val(Mid(strCodBarra, 20, 9)))
						gstrMesAno = Trim(Mid(strCodBarra, 29, 2) & "/" & IIf(Val(Mid(strCodBarra, 31, 2)) < 50, "20" & Mid(strCodBarra, 31, 2), "19" & Mid(strCodBarra, 31, 2)))
						strZona = Mid(Trim(strCodBarra), 33, 3)
						strLivro = Mid(Trim(strCodBarra), 36, 2)
						strConta = Mid(Trim(strCodBarra), 38, 7)
						strConta = VB6.Format(strConta, "000000")
					Else
						gdblNumNf = CDbl(Mid(strCodBarra, 20, 7))
						gstrMesAno = Mid(strCodBarra, 28, 2) & "/" & Mid(strCodBarra, 30, 4)
						strZona = Mid(strCodBarra, 34, 3)
						strLivro = Mid(strCodBarra, 37, 2)
						strConta = Mid(strCodBarra, 39, 6)
						strConta = VB6.Format(strConta, "000000")
					End If
				ElseIf gstrEmpresa_Sigla = "EFLJCL" Then 
					gdblNumNf = CDbl(Mid(strCodBarra, 20, 5))
					gstrMesAno = Mid(strCodBarra, 27, 2) & "/" & Mid(strCodBarra, 29, 4)
					strZona = Mid(strCodBarra, 33, 3)
					strLivro = Mid(strCodBarra, 36, 2)
					If gstrMesAno < strMesIni Then Call trocaZonaLivro(strZona, strLivro)
					strConta = Mid(strCodBarra, 38, 6)
					strConta = VB6.Format(strConta, "000000")
				End If
				
				
				intAuxCF = consisteFatura(strZona, strLivro, strConta, gstrMesAno)
				
				If intAuxCF = 0 Or intAuxCF = 3 Then
					Call preencheCampos(gdblNumNf, gstrMesAno)
				ElseIf intAuxCF = 1 Then 
					Call MsgBox("Fatura não encontrada! Por favor, verifique o código de barras!", MsgBoxStyle.Information, gstrTituloMensagem)
				Else
					Call MsgBox("A fatura já foi paga!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
			Else
				Call MsgBox("Código de Barras inválido!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		End If
	End Sub
	
	Private Sub preencheCampos(ByVal adblNumNf As Double, ByVal astrMesAno As String)
		Dim strSql As String
		Dim lstrGrTar As String
		Dim strAux As String
		Dim lngUnidId As Integer
		Dim lngConsId As Integer
		Dim rsPC As ADODB.Recordset
		
		rsPC = New ADODB.Recordset
		
		gstrEstado = ""
		strSql = "SELECT * " & "  FROM nf " & " WHERE numnf = " & adblNumNf & " " & "   AND mesano = """ & astrMesAno & """ " & " LIMIT 1 "
		Call Conectar(strSql, rsPC)
		If rsPC.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"'
			strAux = IIf(IsDbNull(rsPC.Fields("flgst").Value), "", rsPC.Fields("flgst").Value)
			If Not (strAux = "P" Or strAux = "C") And Trim(gstrAtivar) = "" 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"'
				gstrEstado = IIf(IsDbNull(rsPC.Fields("flgst").Value), "", rsPC.Fields("flgst").Value)
				If rsPC.Fields("datast").Value <> "0000-00-00" Then
					gstrEstado = "R"
				End If
				Me.txtFatura.Text = CStr(adblNumNf)
				Me.txtValor.Text = VB6.Format(rsPC.Fields("valortot").Value, "standard")
				Me.txtTotPagar.Text = VB6.Format(CDbl(Me.txtTotPagar.Text) + CDbl(Me.txtValor.Text), "standard") 'Format(rsPC.Fields("valortot"), "standard")
				Me.txtDinheiro.Text = Me.txtTotPagar.Text
				Me.txtMesAno.Text = astrMesAno
				Me.txtDtVenc.Text = VB6.Format(rsPC.Fields("datavenc").Value, "dd/mm/yyyy")
				Me.txtZona.Text = rsPC.Fields("zona").Value
				Me.txtLivro.Text = rsPC.Fields("livro").Value
				Me.txtConta.Text = rsPC.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 = Mid(IIf(IsDbNull(rsPC.Fields("ta").Value), "01B", rsPC.Fields("ta").Value), 3, 1)
				gstrZona = Me.txtZona.Text
				gstrLivro = Me.txtLivro.Text
				gstrConta = Me.txtConta.Text
				lngUnidId = rsPC.Fields("unid_id").Value
				lngConsId = rsPC.Fields("cons_id").Value
				
				If rsPC.State = 1 Then Call rsPC.Close()
				'Pega o nome da unidade consumidora
				strSql = "SELECT nome " & "  FROM unid" & LCase(lstrGrTar) & " " & " WHERE unid_id = " & lngUnidId & " " & "   AND cons_id = " & lngConsId & " "
				Call Conectar(strSql, rsPC)
				If rsPC.RecordCount > 0 Then
					Me.txtNome.Text = rsPC.Fields("nome").Value
				Else
					If rsPC.State = 1 Then Call rsPC.Close()
					'Pega o nome do consumidor
					strSql = "SELECT nome " & "  FROM consum " & " WHERE cons_id = " & lngConsId & " "
					Call Conectar(strSql, rsPC)
					If rsPC.RecordCount > 0 Then
						Me.txtNome.Text = rsPC.Fields("nome").Value
					Else
						Me.txtNome.Text = ""
					End If
				End If
			Else
				Call MsgBox("A fatura já foi paga!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		Else
			Me.txtFatura.Text = ""
			Me.txtValor.Text = ""
			Me.txtMesAno.Text = ""
			Me.txtDtVenc.Text = ""
			Me.txtZona.Text = ""
			Me.txtLivro.Text = ""
			Me.txtConta.Text = ""
			Me.txtNome.Text = ""
			gstrZona = ""
			gstrLivro = ""
			gstrConta = ""
		End If
		If rsPC.State = 1 Then Call rsPC.Close()
		'UPGRADE_NOTE: Object rsPC 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"'
		rsPC = Nothing
		' cmdAutenticar.SetFocus
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub LimpaCampos()
		Me.txtCodBarra.Text = ""
		Me.txtFatura.Text = ""
		Me.txtValor.Text = ""
		Me.txtMesAno.Text = ""
		Me.txtDtVenc.Text = ""
		Me.txtZona.Text = ""
		Me.txtLivro.Text = ""
		Me.txtConta.Text = ""
		Me.txtNome.Text = ""
		Me.txtCodBarra.Text = ""
		Me.txtVlrTaxa.Text = ""
		Me.txtValorDP.Text = ""
		' me.txtTotPagar.Text = 0
		' me.txtTroco.Text = 0
		' me.txtDinheiro.Text = 0
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Function consisteFatura(ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal astrMesAno As String) As Short
		'****************************************
		'* valores de retorno:                  *
		'* 0 = OK, continua                     *
		'* 1 = não existe fatura                *
		'* 2 = a fatura existe, mas já foi paga *
		'****************************************
		Dim strSql As String
		Dim strAux As String
		Dim rsCF As ADODB.Recordset
		
		rsCF = New ADODB.Recordset
		
		gstrEstado = ""
		strSql = "SELECT * " & "  FROM nf USE INDEX(keyzona, keylivro, keyconta) " & " WHERE zona = """ & astrZona & """ " & "   AND livro = """ & astrLivro & """ " & "   AND conta = """ & astrConta & """ " & "   AND mesano = """ & astrMesAno & """ " & " LIMIT 1 "
		Call Conectar(strSql, rsCF)
		If rsCF.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"'
			strAux = IIf(IsDbNull(rsCF.Fields("flgst").Value), "", rsCF.Fields("flgst").Value)
			If Not (strAux = "P" Or strAux = "C") And Trim(gstrAtivar) = "" Then
				consisteFatura = 0
			Else
				consisteFatura = 2
			End If
		Else
			consisteFatura = 1
		End If
		If rsCF.State = 1 Then Call rsCF.Close()
		'UPGRADE_NOTE: Object rsCF 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"'
		rsCF = Nothing
	End Function
	
	Private Sub cmdAutenticar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAutenticar.Click
		Dim strResutado As String
		Dim strSql As String
		Dim strTipo As String
		
		If Not (Len(Trim(Me.txtVlrTaxa.Text)) = 0 And Len(Trim(Me.txtValorDP.Text)) = 0 And Len(Trim(Me.txtValor.Text)) = 0) Then
			Me.cmdAutenticar.Enabled = False
			Me.cmdAutenticar.Refresh()
			'Valor de Taxas
			If Len(Trim(Me.txtVlrTaxa.Text)) > 0 Then
				strTipo = "TX"
				strResutado = montaString(strTipo)
				gdblVlrDoc = CDbl(Me.txtVlrTaxa.Text)
				strSql = "INSERT INTO caixa_itens (codigo, codcaixa, seq, tipo, zona, livro, conta, numnf, mesano, valor, bobina, dataproc, sit) " & "                  VALUES(0, " & ilngCodCaixa & ", " & gintSeq & ", """ & strTipo & """, """ & "000"", """ & "00"", """ & "000000"", " & 0 & ", """ & "00/0000"", " & Replace(CStr(gdblVlrDoc), ",", ".") & ", """ & Replace(strResutado, ",", ".") & """,""" & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """,""" & "V"") "
				Call Executar(strSql)
				Call Imprime(strResutado)
			End If
			'Valor de Deposito
			If Len(Trim(Me.txtValorDP.Text)) > 0 Then
				strTipo = "RE"
				strResutado = montaString(strTipo)
				gdblVlrDoc = CDbl(Me.txtValorDP.Text)
				strSql = "INSERT INTO caixa_itens (codigo, codcaixa, seq, tipo, zona, livro, conta, numnf, mesano, valor, bobina, dataproc, sit) " & "                  VALUES(0, " & ilngCodCaixa & ", " & gintSeq & ", """ & strTipo & """, """ & "000"", """ & "00"", """ & "000000"", " & 0 & ", """ & "00/0000"", " & Replace(CStr(gdblVlrDoc), ",", ".") & ", """ & Replace(strResutado, ",", ".") & """,""" & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """,""" & "V"") "
				Call Executar(strSql)
				Call Imprimir("")
				Call Imprimir("=================================================")
				Call Imprimir(strResutado)
				Call Imprimir("=================================================")
				Call Imprimir("")
			End If
			'Valor Energia Recebida
			If Len(Trim(Me.txtValor.Text)) > 0 Then
				gdblVlrDoc = CDbl(Me.txtValor.Text)
				If gstrEstado = "R" Then
					strTipo = "TX"
				Else
					strTipo = "EN"
				End If
				strResutado = montaString(strTipo)
				strSql = "UPDATE nf SET " & "              flgst = ""P"", " & "              datapag = """ & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """, " & "              banarrec = " & glngBanId & ", " & "              banident = """ & gstrNmBco & """ " & " WHERE numnf = " & gdblNumNf & " " & "   AND mesano = """ & gstrMesAno & """ "
				Call Executar(strSql)
				strSql = "INSERT INTO caixa_itens (codigo, codcaixa, seq, tipo, zona, livro, conta, numnf, mesano, valor, bobina, dataproc, sit) " & "                  VALUES(0, " & ilngCodCaixa & ", " & gintSeq & ", """ & strTipo & """, """ & gstrZona & """, """ & gstrLivro & """, """ & gstrConta & """, " & gdblNumNf & ", """ & gstrMesAno & """, " & Replace(CStr(gdblVlrDoc), ",", ".") & ", """ & Replace(strResutado, ",", ".") & """,""" & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """,""" & "V"") "
				Call Executar(strSql)
				Call Imprime(strResutado)
			End If
			Call ListaFaturas()
			Call LimpaCampos()
			If gintPosicao = 2 Then
				Me.txtCodBarra.Focus()
			ElseIf gintPosicao = 3 Then 
				Me.txtNumNf.Focus()
			ElseIf gintPosicao = 4 Then 
				Me.txtVlrTaxa.Focus()
			ElseIf gintPosicao = 5 Then 
				Me.txtValorDP.Focus()
			End If
			'    gdblTotAut = 0
			'    Me.txtTotPagar.Text = "0,00"
			'    Me.txtDinheiro.Text = "0,00"
			'    Me.txtTroco.Text = "0,00"
		Else
			Me.cmdAutenticar.Enabled = False
			Call MsgBox("Não ha nenhum valor para autenticar ", MsgBoxStyle.Exclamation, gstrTituloMensagem)
		End If
	End Sub
	
	Private Sub cmdEstornar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEstornar.Click
		Dim strResutado As String
		Dim strSql As String
		Dim strTipo As String
		Dim rsEstorno As ADODB.Recordset
		Dim strSituacao As String
		Dim strZona As String
		Dim strLivro As String
		Dim strConta As String
		Dim dblNumNf As Double
		Dim strMesAno As String
		
		rsEstorno = New ADODB.Recordset
		With grid
			dblNumNf = CDbl(.get_TextMatrix(.RowSel, 0))
			strMesAno = .get_TextMatrix(.RowSel, 1)
			strZona = Trim(VB.Left(.get_TextMatrix(.RowSel, 2), 3))
			strLivro = Trim(Mid(.get_TextMatrix(.RowSel, 2), 5, 2))
			strConta = Trim(VB.Right(.get_TextMatrix(.RowSel, 2), 6))
			gdblVlrDoc = CDbl(.get_TextMatrix(.RowSel, 3))
			strSituacao = Trim(.get_TextMatrix(.RowSel, 4))
			
			Me.txtFatura.Text = CStr(dblNumNf)
			Me.txtMesAno.Text = strMesAno
			Me.txtZona.Text = strZona
			Me.txtLivro.Text = strLivro
			Me.txtConta.Text = strConta
			
			
		End With
		
		Select Case strSituacao
			Case "TX" 'Taxas
				strTipo = "EST"
				Me.txtVlrTaxa.Text = VB6.Format(gdblVlrDoc, "standard")
			Case "RE" 'Retirada
				strTipo = "ESR"
				Me.txtValorDP.Text = VB6.Format(gdblVlrDoc, "standard")
			Case "EN" 'Recebimento de Energia
				strTipo = "ESE"
				Me.txtValor.Text = VB6.Format(gdblVlrDoc, "standard")
		End Select
		strSql = "SELECT * " & "  FROM caixa_itens " & " WHERE zona = """ & strZona & """ " & "   AND livro = """ & strLivro & """ " & "   AND conta = """ & strConta & """ " & "   AND numnf = " & dblNumNf & " " & "   AND mesano = """ & strMesAno & """ " & "   AND valor = " & Replace(CStr(gdblVlrDoc), ",", ".") & " " & "   AND codcaixa = """ & ilngCodCaixa & """ "
		Call Conectar(strSql, rsEstorno)
		If rsEstorno.RecordCount > 0 Then
			If strTipo = "EST" Then
				strResutado = montaString(strTipo)
				strSql = "INSERT INTO caixa_itens (codigo, codcaixa, seq, tipo, zona, livro, conta, numnf, mesano, valor, bobina, dataproc, sit) " & "                  VALUES(0, " & ilngCodCaixa & ", " & gintSeq & ", """ & strTipo & """, """ & "000"", """ & "00"", """ & "000000"", " & 0 & ", """ & "00/0000"", " & Replace(CStr(gdblVlrDoc), ",", ".") & ", """ & Replace(strResutado, ",", ".") & """,""" & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """,""" & "V"") "
				Call Executar(strSql)
			End If
			If strTipo = "ESR" Then
				strResutado = montaString(strTipo)
				strSql = "INSERT INTO caixa_itens (codigo, codcaixa, seq, tipo, zona, livro, conta, numnf, mesano, valor, bobina, dataproc, sit) " & "                  VALUES(0, " & ilngCodCaixa & ", " & gintSeq & ", """ & strTipo & """, """ & "000"", """ & "00"", """ & "000000"", " & 0 & ", """ & "00/0000"", " & Replace(CStr(gdblVlrDoc), ",", ".") & ", """ & Replace(strResutado, ",", ".") & """,""" & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """,""" & "V"") "
				Call Executar(strSql)
			End If
			
			If strTipo = "ESE" Then
				strResutado = montaString(strTipo)
				strSql = "UPDATE nf SET " & "              flgst = """ & IIf(gstrEstado = "R", gstrEstado, "") & """, " & "              datapag = ""0000-00-00"", " & "              banarrec = banco_fatur, " & "              banident = """" " & " WHERE numnf = " & dblNumNf & " " & "   AND mesano = """ & strMesAno & """ "
				Call Executar(strSql)
				strSql = "INSERT INTO caixa_itens (codigo, codcaixa, seq, tipo, zona, livro, conta, numnf, mesano, valor, bobina, dataproc, sit) " & "                  VALUES(0, " & ilngCodCaixa & ", " & gintSeq & ", """ & strTipo & """, """ & strZona & """, """ & strLivro & """, """ & strConta & """, " & dblNumNf & ", """ & strMesAno & """, " & Replace(CStr(gdblVlrDoc), ",", ".") & ", """ & Replace(strResutado, ",", ".") & """,""" & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """,""" & "V"") "
				Call Executar(strSql)
			End If
			Call Imprime(strResutado)
			gdblTotAut = 0
			Me.txtTotPagar.Text = "0,00"
			Me.txtDinheiro.Text = "0,00"
			Me.txtTroco.Text = "0,00"
		Else
			Call MsgBox("Não existe referência deste valor para estornar " & Chr(10) & "Verifique na fita ", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
		If rsEstorno.State = 1 Then Call rsEstorno.Close()
		'UPGRADE_NOTE: Object rsEstorno 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"'
		rsEstorno = Nothing
		Call ListaFaturas()
		Call LimpaCampos()
		gstrAtivar = " "
		'If gstrEmpresa_Sigla = "HIDROPAN" Then
		Me.cmdEstornar.Enabled = False
		'End If
		Me.cmdAutenticar.Enabled = True
		Me.cmdAutenticar.Refresh()
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdZera_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdZera.Click
		gdblTotAut = 0
		Me.txtTotPagar.Text = "0,00"
		Me.txtDinheiro.Text = "0,00"
		Me.txtTroco.Text = "0,00"
	End Sub
	
	Private Sub habilitarTaxas(ByRef abModo As Boolean)
		Me.FrameTaxas.Enabled = abModo
		Me.txtVlrTaxa.Enabled = abModo
		Me.txtVlrTaxa.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(abModo, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
		Me.lblValorTaxas.Enabled = abModo
		If abModo Then
			Call Me.txtVlrTaxa.Focus()
		Else
			Me.txtVlrTaxa.Text = ""
		End If
	End Sub
	
	Private Sub habilitarFatura(ByRef abModo As Boolean)
		Me.FrameFatura.Enabled = abModo
		Me.txtNumNf.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(abModo, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
		Me.txtMesAnoNf.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(abModo, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
		Me.lblNumNfFat.Enabled = abModo
		Me.lblMesAnoFat.Enabled = abModo
		If abModo Then
			Call txtNumNf.Focus()
		Else
			Me.txtNumNf.Text = ""
			Me.txtMesAnoNf.Text = ""
		End If
	End Sub
	
	Private Sub habilitarCodBarra(ByRef abModo As Boolean)
		Me.FrameCodBarra.Enabled = abModo
		Me.txtCodBarra.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(abModo, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
		If abModo Then
			Call txtCodBarra.Focus()
		Else
			Me.txtCodBarra.Text = ""
		End If
	End Sub
	
	Private Sub frmCaixa_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Dim strCampo As String
		
		'UPGRADE_ISSUE: Control Name could not be resolved because it was within the generic namespace ActiveControl. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="084D22AD-ECB1-400F-B4C7-418ECEC5E36E"'
		strCampo = VB6.GetActiveControl().Name
		Select Case strCampo
			Case "txtCodBarra", "txtNumNf", "txtVlrTaxa", "txtValorDP", "txtDinheiro"
				Call ControleTecla(KeyAscii, "N")
			Case Else
				Call ControleTecla(KeyAscii, "A")
		End Select
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Function ControleTecla(ByRef aKeyAscii As Short, ByRef astrTipo As String) As Object
		If astrTipo = "N" Then
			If aKeyAscii = System.Windows.Forms.Keys.Return Then
				Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			ElseIf aKeyAscii <> 8 And (aKeyAscii <> 44 And (aKeyAscii < 48 Or aKeyAscii > 57)) Then 
				aKeyAscii = 0
			End If
		ElseIf astrTipo = "A" Then 
			If aKeyAscii = System.Windows.Forms.Keys.Return Then
				Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
			End If
		End If
	End Function
	
	Private Sub checaCaixa()
		Dim strSql As String
		Dim rsChecaCaixa As ADODB.Recordset
		
		rsChecaCaixa = New ADODB.Recordset
		
		ilngCodCaixa = glngNumCaixa
		strSql = "SELECT * " & "  FROM caixa " & " WHERE dtcaixa = """ & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """ " & "   AND numcaixa = " & glngNumCaixa & " "
		Call Conectar(strSql, rsChecaCaixa)
		If rsChecaCaixa.RecordCount > 0 Then
			If rsChecaCaixa.Fields("estado").Value = "F" Then
				Call MsgBox("O caixa do dia " & VB6.Format(rsChecaCaixa.Fields("dtcaixa").Value, "dd/mm/yyyy") & " foi fechado!", MsgBoxStyle.Information, gstrTituloMensagem)
				gstrSitCaixa = rsChecaCaixa.Fields("estado").Value
				'Me.FrameInfoCaixa.Enabled = False
				Call habilitarCodBarra(False)
				Call habilitarFatura(False)
				Call habilitarTaxas(False)
				Me.FramePgto.Enabled = False
				Me.cmdAutenticar.Enabled = False
				Me.cmdEstornar.Enabled = False
				Me.KeyPreview = False
				Me.cmdAbreFechaCaixa.Enabled = False
				gblnCxAberto = False
			Else
				Me.cmdAbreFechaCaixa.Text = "Fechar Caixa"
				gstrSitCaixa = "A"
				Me.txtData.Text = VB6.Format(rsChecaCaixa.Fields("dtcaixa").Value, "dd/mm/yyyy")
				Me.txtSaldo.Text = FormatNumber(rsChecaCaixa.Fields("saldo").Value, 2)
				Me.txtValorSaldo.Text = FormatNumber(rsChecaCaixa.Fields("saldo").Value, 2)
				Me.txtCaixaUser.Text = rsChecaCaixa.Fields("numCaixa").Value & " / " & gstrUsuario_Nome
				ilngCodCaixa = rsChecaCaixa.Fields("numCaixa").Value
				gblnCxAberto = True
			End If
		Else
			'Verifica se existe algum caixa aberto de acordo com o numero do caixa do usuario logado
			If rsChecaCaixa.State = 1 Then Call rsChecaCaixa.Close()
			strSql = "SELECT * " & "  FROM caixa " & " WHERE estado = ""A"" " & "   AND numcaixa = " & glngNumCaixa & " "
			Call Conectar(strSql, rsChecaCaixa)
			If rsChecaCaixa.RecordCount > 0 Then
				Me.cmdAbreFechaCaixa.Text = "Fechar Caixa"
				gstrSitCaixa = "A"
				Me.txtData.Text = VB6.Format(rsChecaCaixa.Fields("dtcaixa").Value, "dd/mm/yyyy")
				Me.txtSaldo.Text = FormatNumber(rsChecaCaixa.Fields("saldo").Value, 2)
				Me.txtValorSaldo.Text = FormatNumber(rsChecaCaixa.Fields("saldo").Value, 2)
				Me.txtCaixaUser.Text = rsChecaCaixa.Fields("numCaixa").Value & " / " & gstrUsuario_Nome
				
				If MsgBox("O caixa do dia " & VB6.Format(rsChecaCaixa.Fields("dtcaixa").Value, "dd/mm/yyyy") & " não foi fechado! Deseja fechá-lo?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
					Call cmdAbreFechaCaixa_Click(cmdAbreFechaCaixa, New System.EventArgs())
					Me.cmdAbreFechaCaixa.Text = "Abrir Caixa"
					gstrSitCaixa = "F"
					Me.txtData.Text = ""
					Me.txtSaldo.Text = "0,00"
					Me.txtCaixaUser.Text = "0,00"
				End If
				gblnCxAberto = False
			Else
				'Verifica se existe algum caixa fechado de acordo com o numero do caixa do usuario logado para buscar o saldo do dia anterior
				If rsChecaCaixa.State = 1 Then Call rsChecaCaixa.Close()
				strSql = "SELECT * " & "  FROM caixa " & " WHERE estado = ""F"" " & "   AND numcaixa = " & glngNumCaixa & " "
				Call Conectar(strSql, rsChecaCaixa)
				If rsChecaCaixa.RecordCount > 0 Then
					Call rsChecaCaixa.MoveLast()
					Me.txtData.Text = VB6.Format(dtAtualBd, "dd/mm/yyyy")
					Me.txtSaldo.Text = FormatNumber(rsChecaCaixa.Fields("saldo").Value, 2)
					Me.txtValorSaldo.Text = FormatNumber(rsChecaCaixa.Fields("saldo").Value, 2)
					Me.txtCaixaUser.Text = rsChecaCaixa.Fields("numCaixa").Value & " / " & gstrUsuario_Nome
					ilngCodCaixa = rsChecaCaixa.Fields("numCaixa").Value
					Me.cmdAbreFechaCaixa.Text = "Abrir Caixa"
				End If
				gstrSitCaixa = "F"
				gblnCxAberto = False
			End If
		End If
		If rsChecaCaixa.State = 1 Then Call rsChecaCaixa.Close()
		'UPGRADE_NOTE: Object rsChecaCaixa 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"'
		rsChecaCaixa = Nothing
	End Sub
	
	Private Sub frmCaixa_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		Call DeInit()
	End Sub
	
	
	Private Sub grid_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grid.ClickEvent
		Me.cmdEstornar.Enabled = True
	End Sub
	
	'Private Sub grid_DblClick()
	'  With Me.grid
	'    gdblNumNf = CDbl(.TextMatrix(.RowSel, 0))
	'    gstrMesAno = .TextMatrix(.RowSel, 1)
	'    Call preencheCampos(gdblNumNf, gstrMesAno)
	'  End With
	'  Me.cmdEstornar.Enabled = True
	'End Sub
	'
	'Private Sub opbCheque_Click()
	'  Me.cmdZera.Enabled = Me.opbCheque.Value
	'End Sub
	
	'Private Sub opbDinheiro_Click()
	'  Me.cmdZera.Enabled = Me.opbCheque.Value
	'End Sub
	'
	Private Sub txtCodBarra_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtCodBarra.Leave
		Call processaCodBarra(Me.txtCodBarra.Text)
	End Sub
	
	Private Sub txtCodBarra_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtCodBarra.Enter
		Me.cmdAutenticar.Enabled = True
		With Me.txtCodBarra
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtDinheiro_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDinheiro.Enter
		With Me.txtDinheiro
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtDinheiro_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDinheiro.Leave
		Dim dblValor As Double
		Dim dblDinheiro As Double
		Dim dblTroco As Double
		
		dblDinheiro = 0
		dblValor = CDbl(Replace(Me.txtTotPagar.Text, ".", ""))
		If Len(Me.txtDinheiro.Text) <> 0 Then
			dblDinheiro = CDbl(Replace(Me.txtDinheiro.Text, ".", ""))
		End If
		If dblDinheiro <> 0 Then
			dblTroco = (dblValor - dblDinheiro) * -1
		Else
			dblTroco = 0
		End If
		Me.txtDinheiro.Text = VB6.Format(Me.txtDinheiro.Text, "standard")
		Me.txtTroco.Text = VB6.Format(dblTroco, "standard")
	End Sub
	
	Private Sub txtMesAnoNf_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAnoNf.Enter
		With Me.txtMesAnoNf
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtMesAnoNf_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMesAnoNf.Leave
		Me.cmdAutenticar.Enabled = True
		Me.txtMesAnoNf.Text = FormataData(Me.txtMesAnoNf, 1)
		
		If Len(Trim(Me.txtNumNf.Text)) > 0 And Len(Trim(Me.txtMesAnoNf.Text)) > 0 Then
			gdblNumNf = CDbl(Me.txtNumNf.Text)
			gstrMesAno = Me.txtMesAnoNf.Text
			Call preencheCampos(gdblNumNf, gstrMesAno)
		End If
	End Sub
	
	Private Function montaString(ByRef astrTipo As String) As String
		Dim strSiglaFita As String
		Dim strSeqFita As String
		Dim strDataFita As String
		Dim strNumNfFita As String
		Dim strMesAnoFita As String
		Dim strValorFita As String
		Dim strSql As String
		Dim strResultado As String
		Dim rsMS As ADODB.Recordset
		
		rsMS = New ADODB.Recordset
		Select Case gstrEmpresa_Sigla
			Case "HIDROPAN"
				strSiglaFita = "HP"
			Case "EFLUL"
				strSiglaFita = "EF"
			Case "CHESP"
				strSiglaFita = "CH"
			Case "COORSEL"
				strSiglaFita = "COORS"
		End Select
		
		strSql = "SELECT MAX(seq) sequencia " & "  FROM caixa_itens " & " WHERE codcaixa = " & ilngCodCaixa & " " & "   AND dataproc = """ & VB6.Format(dtAtualBd, "yyyy-MM-dd") & """ "
		Call Conectar(strSql, rsMS)
		If rsMS.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"'
			strSeqFita = CStr(IIf(IsDbNull(rsMS.Fields("sequencia").Value), "0", rsMS.Fields("sequencia").Value) + 1)
		Else
			strSeqFita = CStr(1)
		End If
		If rsMS.State = 1 Then Call rsMS.Close()
		
		gintSeq = CShort(strSeqFita)
		strSeqFita = VB6.Format(strSeqFita, "0000")
		strDataFita = VB6.Format(dtAtualBd, "ddmmyyyy")
		
		If astrTipo = "EN" Then
			strNumNfFita = VB6.Format(Me.txtFatura.Text, "000000")
			strMesAnoFita = VB.Left(Me.txtMesAno.Text, 2) & VB.Right(Me.txtMesAno.Text, 4)
			strValorFita = VB6.Format(Me.txtValor.Text, "###,###.00")
		ElseIf astrTipo = "RE" Then 
			strNumNfFita = "      "
			strMesAnoFita = "      "
			strValorFita = VB6.Format(Me.txtValorDP.Text, "###,###.00")
		ElseIf astrTipo = "TX" Then 
			strNumNfFita = "      "
			strMesAnoFita = "      "
			strValorFita = VB6.Format(Me.txtVlrTaxa.Text, "###,###.00")
		ElseIf astrTipo = "ES" Then 
			strNumNfFita = VB6.Format(Me.txtFatura.Text, "000000")
			strMesAnoFita = VB.Left(Me.txtMesAno.Text, 2) & VB.Right(Me.txtMesAno.Text, 4)
			strValorFita = VB6.Format(Me.txtValor.Text, "###,###.00")
		ElseIf astrTipo = "ESR" Then 
			strNumNfFita = "      "
			strMesAnoFita = "      "
			strValorFita = VB6.Format(Me.txtValorDP.Text, "###,###.00")
		ElseIf astrTipo = "EST" Then 
			strNumNfFita = "      "
			strMesAnoFita = "      "
			strValorFita = VB6.Format(Me.txtVlrTaxa.Text, "###,###.00")
		ElseIf astrTipo = "ESE" Then 
			strNumNfFita = VB6.Format(Me.txtFatura.Text, "000000")
			strMesAnoFita = VB.Left(Me.txtMesAno.Text, 2) & VB.Right(Me.txtMesAno.Text, 4)
			strValorFita = VB6.Format(Me.txtValor.Text, "###,###.00")
		End If
		strValorFita = PreencheCampo(strValorFita, 10, " ", "E")
		strResultado = strSiglaFita & glngNumCaixa & strSeqFita & " " & strDataFita & " " & strNumNfFita & strMesAnoFita & " " & strValorFita & astrTipo
		montaString = strResultado
	End Function
	
	Private Sub Imprime(ByRef astrAux As String)
		Dim intConta As Short
		Dim intAut As Short
		
		'mudar numero de autenticações para deposito
		intAut = gintNumAut
		If Len(Me.txtValorDP.Text) > 0 Then
			intAut = intAut - 1
		End If
		'--------------------------------------------------------
		For intConta = 1 To intAut
			Call MsgBox("Posicione o documento e aperte enter...", MsgBoxStyle.Information, gstrTituloMensagem)
			Call Autenticar(astrAux)
		Next intConta
		Call MsgBox("Pressione enter para autenticar a bobina...", MsgBoxStyle.Information, gstrTituloMensagem)
		Call Imprimir(astrAux)
		gdblTotAut = gdblTotAut + gdblVlrDoc
		Me.txtTotPagar.Text = VB6.Format(gdblTotAut, "standard")
	End Sub
	
	Private Sub habilitarDeposito(ByRef abModo As Boolean)
		Me.FrameDeposito.Enabled = abModo
		Me.txtValorDP.Enabled = abModo
		Me.txtValorDP.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(abModo, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
		Me.lblValorDep.Enabled = abModo
		If abModo = True Then
			Call Me.txtValorDP.Focus()
		Else
			Me.txtValorDP.Text = ""
		End If
	End Sub
	
	Private Sub txtNumNf_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtNumNf.Enter
		With Me.txtNumNf
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtSenha_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSenha.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = 13 Then
			Me.FrameSenhaAcesso.Visible = False
			If UCase(Me.txtSenha.Text) <> UCase("irualdief") Then
				Call MsgBox("Você não tem autorização para " & Chr(10) & " fazer esta operação ", MsgBoxStyle.Information, "Mensagem do Sistema ")
			Else
				Me.cmdEstornar.Enabled = True
				gstrAtivar = "S"
				Call MsgBox("Escolha o campo para estornar, e em seguida clique, no botão estornar " & Chr(10) & "para concluir a função do estorno ", MsgBoxStyle.Information, "Mensagem do sistema ")
			End If
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtValorDP_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorDP.Enter
		With Me.txtValorDP
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtValorDP_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtValorDP.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Dim dblValor1 As Double
		Dim dblValor2 As Double
		
		If KeyAscii = 13 Then
			Me.cmdAutenticar.Enabled = True
			dblValor1 = CDbl(Me.txtValorSaldo.Text)
			dblValor2 = CDbl(Me.txtValorDP.Text)
			If dblValor1 >= dblValor2 Then
				Me.txtTotPagar.Text = VB6.Format(Me.txtValorDP.Text, "standard")
				Me.txtValorDP.Text = VB6.Format(Me.txtValorDP.Text, "standard")
				Me.txtDinheiro.Text = VB6.Format(Me.txtValorDP.Text, "standard")
				Me.cmdAutenticar.Focus()
			Else
				Call MsgBox("Valor da retirada maior que o saldo do caixa!!!", MsgBoxStyle.Information, "Mensagem do Sistema ")
			End If
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtValorDP_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtValorDP.Leave
		With Me.txtValorDP
			.Text = VB6.Format(.Text, "Standard")
		End With
	End Sub
	
	Private Sub txtVlrTaxa_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtVlrTaxa.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = 13 Then
			Me.cmdAutenticar.Enabled = True
			Me.txtTotPagar.Text = VB6.Format(Me.txtVlrTaxa.Text, "standard")
			Me.txtVlrTaxa.Text = VB6.Format(Me.txtVlrTaxa.Text, "standard")
			Me.txtDinheiro.Text = VB6.Format(Me.txtVlrTaxa.Text, "standard")
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	'Public Sub Acerto()
	'  Dim sql  As String
	'  Dim consulta As New ADODB.Recordset
	'  sql = "SELECT * FROM caixa_itens "
	'  Call Conectar(sql, consulta)
	'  If consulta.RecordCount > 0 Then
	'
	'   While Not consulta.EOF
	'     sql = "UPDATE nf SET  datapag = '" & Format(consulta!dataproc, "yyyy-MM-dd") & "'  WHERE numnf = '" & consulta!numNf & "' AND mesano = '" & consulta!mesano & "' "
	'     Call executar(sql)
	'     consulta.MoveNext
	'    Wend
	'   End If
	'   If consulta.State = 1 Then consulta.Close
	' Unload Me
	'End Sub
	'
	Private Function GetNomeBanco(ByRef alngBanId As Integer) As String
		Dim strSql As String
		Dim rsBanco As ADODB.Recordset
		
		rsBanco = New ADODB.Recordset
		strSql = "SELECT * FROM bancos WHERE ban_id = " & alngBanId & " LIMIT 1"
		Call Conectar(strSql, rsBanco)
		If rsBanco.RecordCount > 0 Then
			Call rsBanco.MoveFirst()
			Call rsBanco.MoveLast()
			'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"'
			GetNomeBanco = IIf(IsDbNull(rsBanco.Fields("nmbco").Value), "", rsBanco.Fields("nmbco").Value)
		Else
			GetNomeBanco = ""
		End If
		If rsBanco.State = 1 Then Call rsBanco.Close()
		'UPGRADE_NOTE: Object rsBanco 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"'
		rsBanco = Nothing
	End Function
End Class