Pessoal fonte de TEf falta Verificar Retorno ?.

20/12/2005

0

Pessoal peguei alguns exemplos, da bematech e outro postado
neste forum , estou tentando criar um componente.
ele já imprime os comprovantes fecha a venda entre outros, agora
o que está dificil é saber quando o usuário chehou a entrar no programa
da bandeira e depois cancelou, ou cancelou no inicio da escolha da bandeira. Pessoal eis o codigo se alguem puder me ajudar a terminar, ou colaborar em algo fico agradecido.

unit UTef;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls,UMensagem,UCancelaVenda,Inifiles;

type
TTef = class(TCOmponent)
private
F_Ativo : Boolean ;
F_Identifica : String ;
F_Rede : String ;
F_Valor : String ;
F_Doc : String ;
F_Data : String ;
F_Hora : String ;
F_Cancel : Boolean ;
{ Private declarations }
protected
procedure ImprimeGerencial;
procedure Msg_Ncn ;
function GP_Ativar(CarregarTEF_DIAL: Boolean; cTefDial: String): Boolean;
procedure CancelaTransacao ;
{ Protected declarations }
public
constructor Create(AOwner : TComponent); override;
procedure SetaPath;
procedure VerificaGerenciador ;
procedure Administrativo ;
procedure ImprimeRelatorio ;
procedure VendaCartao( NrCupom : String; Total : String ) ;
procedure RetornoTEF ;
procedure NaoConfirmaTef ;
procedure VerificaTransacao ;
procedure ConfirmaTef ;
procedure CancelaVenda ;
procedure ImprimirTXT ;
{ Public declarations }
published
property Gerenciador : Boolean read F_Ativo Write F_Ativo ;
property NrIdentifica : String read F_Identifica write F_Identifica ;
property Rede : String read F_Rede Write F_Rede ;
property Cancelado : Boolean read F_Cancel Write F_Cancel ;

{ Published declarations }
end;
function Bematech_FI_VerificaImpressoraLigada: Integer; StdCall; External ´BEMAFI32.DLL´ Name ´Bematech_FI_VerificaImpressoraLigada´;
function Bematech_FI_FechaRelatorioGerencial: Integer; StdCall; External ´BEMAFI32.DLL´;
function Bematech_FI_RelatorioGerencial( Texto: String ): Integer; StdCall; External ´BEMAFI32.DLL´;
function Bematech_FI_SubTotal( SubTotal: String ): Integer; StdCall; External ´BEMAFI32.DLL´;
function Bematech_FI_AbreComprovanteNaoFiscalVinculado( FormaPagamento: String; Valor: String; NumeroCupom: String ): Integer; StdCall; External ´BEMAFI32.DLL´;
function Bematech_FI_UsaComprovanteNaoFiscalVinculado( Texto: String ): Integer; StdCall; External ´BEMAFI32.DLL´
function Bematech_FI_FechaComprovanteNaoFiscalVinculado: Integer; StdCall; External ´BEMAFI32.DLL´;
function Bematech_FI_IniciaModoTEF: Integer; StdCall; External ´BEMAFI32.DLL´ Name ´Bematech_FI_IniciaModoTEF´;
function Bematech_FI_FinalizaModoTEF: Integer; StdCall; External ´BEMAFI32.DLL´ Name ´Bematech_FI_FinalizaModoTEF´;
function Bematech_FI_TerminaFechamentoCupom( Mensagem: String): Integer; StdCall; External ´BEMAFI32.DLL´;

var sBufArq : string;
sConteudo: string;
sPathRESP: string;
sPathREQ : string;
sPath : string;
sArqRede : String;
sBuffer : String;
sLinha : String;
ArqCMD : String;
F : TextFile;
sLinhas : string;
iRet : longbool;
bExisteTexto: boolean;
iRetorno, iRetornoStatus, iACK, iST1, iST2: integer;
procedure Register;
implementation
////////////////////////////////////////////////////////////////////////////////
// Esta função verifica qual a rede está selecionada.
////////////////////////////////////////////////////////////////////////////////
procedure TTef.SetaPath;
begin
F_Rede:=F_Rede ;
If (F_Rede =´VISA´) or (F_Rede =´REDECARDA´) or(F_Rede =´AMEX´) Then // Caso as redes sejam VISA/REDECARD/AMEX
begin
sPathREQ := ´C:\TEF_DIAL\REQ\´; sPathRESP := ´C:\TEF_DIAL\RESP\´;
end;
If F_Rede =´TECBAN´ Then // Caso a rede seja TECBAN
begin
sPathREQ := ´C:\TEF_DISC\REQ\´; sPathRESP := ´C:\TEF_DISC\RESP\´;
end;
If F_Rede=(´HIPERCARD´) Then // Caso a rede seja HIPERCARD
begin
sPathREQ := ´C:\DISKTEF\DIR_SOLI\´; sPathRESP := ´C:\DISKTEF\DIR_RESP\´;
end;
end;

function TransacaoTEF: Boolean;
var ArquivoINI: TIniFile;
Status: String;
begin
try
ArquivoINI := TIniFile.Create(´C:\PDV\ESPDV.INI´);
Status := ArquivoINI.ReadString( ´STATUS´, ´Transacao´, ´Aberta´);
finally
ArquivoINI.Free;
end;
Result := Status = ´Aberta´;
end;

procedure AbreTransacaoTEF;
var ArquivoINI: TIniFile;
begin
try
ArquivoINI := TIniFile.Create(´C:\PDV\ESPDV.INI´);
ArquivoINI.WriteString(´STATUS´, ´Transacao´, ´Aberta´);
finally
ArquivoINI.Free;
end;
end;

procedure FechaTransacaoTEF;
var ArquivoINI: TIniFile;
begin
try
ArquivoINI := TIniFile.Create(´C:\PDV\ESPDV.INI´);
ArquivoINI.WriteString(´STATUS´, ´Transacao´, ´Fechada´);
finally
ArquivoINI.Free;
end;
end;

function GetAveCharSize(Canvas: TCanvas): TPoint;
var
I: Integer;
Buffer: array[0..51] of Char;
begin
for I := 0 to 25 do Buffer[I] := Chr(I + Ord(´A´));
for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord(´a´));
GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result));
Result.X := Result.X div 52;
end;
procedure AtualizarINI(Mensagem: String; var Transacao: Integer; Incrementar: Boolean);
var ArquivoINI: TIniFile;
begin
try
ArquivoINI := TIniFile.Create(´C:\Pdv\ESPDV.INI´);
Transacao := StrToInt(ArquivoINI.ReadString( ´TEF´,´NumeroTransacao´,´0´));
if Incrementar then Inc(Transacao);
ArquivoINI.WriteString(´TEF´,´NumeroTransacao´,IntToStr(Transacao));
ArquivoINI.WriteString(´TEF´,´Mensagem´, Mensagem);
finally
ArquivoINI.Free;
end;
end;

procedure LerINI(var Mensagem: String; var Transacao: Integer);
var ArquivoINI: TIniFile;
begin
try
ArquivoINI := TIniFile.Create(´C:\PDV\EsPDV.INI´);
Transacao := StrToInt(ArquivoINI.ReadString(´TEF´,´NumeroTransacao´,´0´));
Mensagem := ArquivoINI.ReadString(´TEF´,´Mensagem´, ´ERR´);
finally
ArquivoINI.Free;
end;
end;

procedure MostraMensagem(const ACaption, APrompt: string; DuracaoEmSegundos: Integer);
var
Form: TForm;
Prompt: TLabel;
DialogUnits: TPoint;
nX, Lines: Integer;
begin
Form := TForm.Create(Application);
Lines := 0;
For nX := 1 to Length(APrompt) do
if APrompt[nX]=#13 then Inc(Lines);
with Form do
try
Canvas.Font := Font;
DialogUnits := GetAveCharSize(Canvas);
BorderStyle := bsDialog;
FormStyle := fsStayOnTop;
BorderIcons := [];
Caption := ACaption;
ClientWidth := MulDiv(Screen.Width div 4, DialogUnits.X, 4);
ClientHeight := MulDiv(23 + (Lines*10), DialogUnits.Y, 8);
Position := poScreenCenter;
Prompt := TLabel.Create(Form);
with Prompt do
begin
Parent := Form;
AutoSize := True;
Left := MulDiv(8, DialogUnits.X, 4);
Top := MulDiv(8, DialogUnits.Y, 8);
Caption := APrompt;
end;
Show;
Application.ProcessMessages;
finally
Sleep(DuracaoEmSegundos*1000);
Form.Free;
end;
end;

function GP_LeStatus(): Boolean;
var Resposta: TStringList;
Erros: Integer;
Numero: Integer;
Mensagem: String;
Aguardar: Integer;
begin
LerINI(Mensagem, Numero);
Erros := 3;
Resposta := TStringList.Create;
Aguardar := 1; // Aguardar até 7 segundos
while not FileExists(sPathRESP+´IntPos.sts´) do
begin
if Aguardar > 7 then break;
Sleep(1000);
Inc(Aguardar);
end;
try
Resposta.LoadFromFile(sPathRESP+´IntPos.sts´);
// Verificar se a transacao está consistente - Requisicao e Resposta.
Erros := Erros - Integer(Resposta.Strings[0] = ´000-000 = ´+Mensagem);
Erros := Erros - Integer(Resposta.Strings[1] = ´001-000 = ´+IntToSTr(Numero));
Erros := Erros - Integer(Resposta.Strings[2] = ´999-999 = 0´);
finally
Resposta.Free;
end;
Result := Erros=0;
end;

function TTef.GP_Ativar(CarregarTEF_DIAL: Boolean; cTefDial: String): Boolean;
//Const NomeArquivo = ´C:\tef_dial\resp\ativo.001´; // Caminho e nome do arquivo criado pelo GP ao ser carregado.
var Arquivo: TextFile; // Armazenar o arquivo INTPOS.001.
Numero: Integer; // Número da transação - Obtido do arquivo INI.
Esperar: Integer; // Esperar até 7 segundos.
Erros: Integer; // Quantidade de erros deve ser zero antes de result. Iniciando c/ 2.
begin
Application.ProcessMessages ;
Esperar := 1;
Erros := 2;
SetaPath ;
MostraMensagem(´Aviso´,´Testando a comunicação com o Gerenciador Padrão.´,1);
Application.ProcessMessages ;
if CarregarTEF_DIAL then
begin
WinExec(PChar(cTefDial), SW_HIDE); // Carregar o GP.
// Aguardar até 7 segundos pela criação do arquivo de resposta do GP.
while not FileExists(sPathRESP+´Ativo.001´) do
begin
if Esperar > 7 then
begin
Inc(Erros); // Incrementa a qtde de erros. Não carregou em tempo hábil.
break;
end;
Sleep(1000); // Espera de 1 segundo.
Inc(Esperar); // Incrementar o contador do tempo de espera.
end;
// Decrementar o contador de erros se o arquivo ATIVO.001 tiver sido criado
// Normalmente.
Erros := Erros - Integer(FileExists(SpathRESP+ ´ATIVO.001´));
// Remover o arquivo para.
try
DeleteFile(SpathRESP+ ´ATIVO.001´);
except
// Exceção silenciosa.
end;
end else
Dec(Erros); // Caso não seja para carregar o GP.
// Elimina arquivo anterior
try
DeleteFile(SpathRESP+´IntPos.sts´); // Evita erro de armazenamento indevido.
except
// Exceção silenciosa.
end;
AtualizarINI(´ATV´, Numero, True);
AssignFile(Arquivo,SpathREQ+´IntPos.Tmp´);
try
ReWrite(Arquivo);
WriteLn(Arquivo,´000-000 = ´+´ATV´);
WriteLn(Arquivo,´001-000 = ´+IntToSTr(Numero));
WriteLn(Arquivo,´999-999 = 0´);
finally
CloseFile(Arquivo);
RenameFile(sPathREQ+´IntPos.tmp´, sPathREQ+´IntPos.001´);
end;
Erros := Erros - Integer(GP_LeStatus()); // Número passado e igual ao retornado.
Result := Erros = 0;
end;

procedure Register;
begin
RegisterComponents(´EsComp´, [TTef]);
end;

constructor TTef.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
F_Identifica :=´0000000001´;
F_Rede :=´VISA´ ;
end;


procedure TTef.VerificaGerenciador ;
var cTef: String;
hTef: HWnd;
GP_Ativo : Boolean ;
begin
Application.ProcessMessages ;
cTef := ´c:\tef_dial\Tef_dial.exe´;
hTef := FindWindow(nil, PChar(´Tef_dial´));
GP_Ativo := false;
if not IsWindow(hTef) then
begin
MessageDlg(´Gerenciador Padrão não está ativo e será ativado automaticamente.´,mtInformation,[mbOk],0);
if FileExists(cTef) then
begin
GP_Ativo := GP_Ativar( True, cTef );
if not GP_Ativo then
begin
F_Ativo :=False ;
MessageDlg(´Não foi possível ativar gerenciador padrão.´+#13+
´Este terminal não efetuará vendas com cartão.´,mtWarning,[mbCancel],0);
end;
end else
begin
F_Ativo :=False ;
MessageDlg(´Não é possível ativar gerenciador padrão.´+13+
´Este terminal não efetuará vendas com cartão.´,mtWarning,[mbCancel],0);
end;
end else
begin
GP_Ativo := GP_Ativar( false, cTef );
if not GP_Ativo then
begin
F_Ativo :=False ;
MessageDlg(´Não foi possível ativar gerenciador padrão.´+13+
´Este terminal não efetuará vendas com cartão.´,mtWarning,[mbCancel],0);
end;
end;
// if TransacaoTEF then
// begin
// MostraMensagem(´Atenção´,´Uma trasação TEF está pendente e será cancelada.´, 2);
// Efetuar o cancelamento
// FechaTransacaoTEF;
//end;
if GP_Ativo then
F_Ativo :=True ;
end;

procedure TTef.Administrativo ;
begin
SetaPath;
// -----------------------------------------------
// Abre o arquivo INTPOS.001, para cria as linhas
// que vão abrir o Gerenciador Padrão das REDES,
// para as funções Administrativas.
// -----------------------------------------------
AssignFile( F, ´INTPOS.001´ );
ReWrite( F );
// Conteúdo do arquivo INTPOS.001
sLinhas := ´´;
sLinhas := ´000-000 = ADM´ + #13 + 10 +
´001-000 = ´ + F_Identifica + 13 + 10 +
´999-999 = 0´;
// Escreve no arquivo INTPOS.001
WriteLn( F, sLinhas );
// Fecha o arquivo INTPOS.001
CloseFile( F );
// Copia o arquivo para o diretório do Gerenciador Padrão
CopyFile( pchar( ´INTPOS.001´ ), pchar( sPathREQ + ´INTPOS.001´ ), iRet );
DeleteFile( ´INTPOS.001´ );
end;

procedure TTef.Msg_Ncn;
begin
SetaPath;
sBufArq := ´´;
sConteudo := ´´;
while True do
begin
if FileExists( sPathRESP + ´INTPOS.001´ ) then
begin
//-----------------------------------------------------------
// Abre o arquivo INTPOS.001 gerado pela REDE, para leitura
// das linhas.
//-----------------------------------------------------------
AssignFile( F, sPathRESP + ´INTPOS.001´ ); // Abre o arquivo
Reset( F );
while not EOF( F ) do
begin
ReadLn( F, sBufArq );
if copy( sBufArq, 1, 3 ) = ´010´ then
sConteudo := sConteudo + ´Rede: ´ + copy( sBufArq, 11, Length( sBufArq ) - 10 ) + #13 + 10;
If copy( sBufArq, 1, 3 ) = ´012´ Then
sConteudo := sConteudo + ´NSU: ´ + copy( sBufArq, 11, Length( sBufArq ) - 10 ) + #13 + 10;
If copy( sBufArq, 1, 3 ) = ´003´ Then
sConteudo := sConteudo + ´Valor: ´ + copy( sBufArq, 11, Length( sBufArq ) - 10 ) + 13 + 10;
end;
CloseFile(F);
break;
end;
end;
NaoConfirmaTef ;
Application.MessageBox( pchar( sConteudo ), ´Última transação TEF foi cancelada´, MB_IconInformation + MB_OK );
end;

procedure TTef.ImprimeGerencial;
var iVias: integer;
begin
for iVias := 1 to 2 do
if bExisteTexto = True then
begin
AssignFile( F, ´IMPRIME.TXT´ );
Reset( F );
while not EOF( F ) do
begin
ReadLn( F, sBufArq );
iRetorno := Bematech_FI_VerificaImpressoraLigada();
if iRetorno = 1 then
iRetorno := Bematech_FI_RelatorioGerencial( pchar( sBufArq ) )
else
If Application.MessageBox( ´Impressora não responde.´ + #13 +
´Tentar imprimir novamente ?´, ´Atenção´, MB_IconQuestion + MB_YesNo ) = ID_Yes then
begin
//---------------------------------------------
// Fecha o Comprovante do TEF.
//---------------------------------------------
iRetorno := Bematech_FI_FechaRelatorioGerencial();
CloseFile( F );
ImprimeGerencial;
break;
end
else
begin
CloseFile( F );
MSG_NCN;
NaoConfirmaTEF ;
bExisteTexto := False;
break;
end;
end;
end;
iRetorno := Bematech_FI_FechaRelatorioGerencial();
end;

procedure TTef.ImprimeRelatorio ;
begin
SetaPath;
//-----------------------------------------------------------
// Verifica se existe algum arquivo IMPRIME.TXT antigo. Se
// existe apaga o arquivo.
//-----------------------------------------------------------
if FileExists( ´IMPRIME.TXT´) then
DeleteFile( ´IMPRIME.TXT´ );
bExisteTexto := False;
sBufArq := ´´; sConteudo := ´´;
while True do
begin
if FileExists( sPathRESP + ´INTPOS.001´ ) then
begin
//-----------------------------------------------------------
// Abre o arquivo INTPOS.001 gerado pela REDE, para leitura
// das linhas.
//-----------------------------------------------------------
AssignFile( F, sPathRESP + ´INTPOS.001´ );
Reset( F );
while not EOF( F ) do
begin
ReadLn( F, sBufArq ); //Lê uma linha do arquivo INTPOS.001 e grava em sConteudo
if copy( sBufArq, 1, 3 ) = ´030´ then
begin
FrMensagemTef.lblMensagem.Caption := copy( sBufArq, 11, Length( sBufArq ) - 10 );
FrMensagemTef.ShowModal ;
F_Cancel :=True ;
break;
end;
if copy( sBufArq, 1, 3 ) = ´029´ then
begin
sConteudo := sConteudo + copy( sBufArq, 12, Length( sBufArq ) - 12 ) + #13 + 10;
bExisteTexto := True;
end;
end;
CloseFile( F );
end;
break;
end;
//-----------------------------------------------------------
// Prepara o arquivo IMPRIME.TXT. Este arquivo é uma cópia de
// segurança do texto da transação.
//-----------------------------------------------------------

AssignFile( F, ´IMPRIME.TXT´ );
ReWrite( F );
WriteLn( F, sConteudo ); // Escreve no arquivo IMPRIME.TXT
CloseFile( F );

//-----------------------------------------------------------
// Abre o arquivo IMPRIME.TXT para começar a impressão das
// linhas.
//-----------------------------------------------------------

AssignFile( F, ´IMPRIME.TXT´ );
Reset( F );

while not EOF( F ) do
begin
ReadLn( F, sBufArq );
//---------------------------------------------
// Verifica se a impressora fiscal está ligada
//---------------------------------------------
iRetorno := Bematech_FI_VerificaImpressoraLigada();
if iRetorno = 1 then
begin
if bExisteTexto = True then
begin
//---------------------------------------------
// Imprime a linhas lida do arquivo IMPRIME.TXT,
// no comprovante.
//---------------------------------------------
iRetorno := Bematech_FI_RelatorioGerencial( pchar( sBufArq ) );
FrMensagemTef.Close;
end
else
begin
FrMensagemTef.Close; break;
end;
end
else
If Application.MessageBox( ´Impressora não responde.´ + #13 +
´Tentar imprimir novamente ?´, ´Atencão´, MB_IconQuestion + MB_YesNo ) = ID_Yes then
begin
//---------------------------------------------
// Fecha o Comprovante do TEF.
//---------------------------------------------
iRetorno := Bematech_FI_FechaRelatorioGerencial();
CloseFile( F );
bExisteTexto := True;
ImprimeGerencial;
bExisteTexto := False;
break;
end
else
begin
CloseFile( F );
NaoConfirmaTef ;
break;
end;

end;
CloseFile( F );
iRetorno := Bematech_FI_FechaRelatorioGerencial();
end;

procedure TTef.VendaCartao(NrCupom : String; Total : String) ;
begin
SetaPath;
if FileExists(sPathRESP+´IntPos.001´) then
DeleteFile(sPathRESP +´IntPos.001´);
AssignFile( F, ´INTPOS.001´ );
// Conteúdo do arquivo INTPOS.001
sLinhas := ´´;
sLinhas := ´000-000 = CRT´ + #13 + 10 +
´001-000 = ´ + F_Identifica + 13 + 10 +
´002-000 = ´ + NrCupom + 13 + 10 +
´003-000 = ´ + Total + 13 + 10 +
´999-999 = 0´;
ReWrite( F );
WriteLn( F, SLinhas );
CloseFile( F );
CopyFile( pchar( ´INTPOS.001´ ), pchar( sPathREQ + ´INTPOS.001´ ), iRet );
DeleteFile( ´INTPOS.001´);
AbreTransacaoTEF ;
RetornoTef ;
end;


procedure TTef.RetornoTEF ;
var
sForma, cSaltaLinha: string;
bRetDelete: boolean;
RetIntPos : TextFile ;
begin
while not FileExists(sPathRESP+´IntPos.001´) do
begin
Application.ProcessMessages ;
end;
SetaPath;
sForma := ´Cartao´;
//-----------------------------------------------------------
// Verifica se existe algum arquivo IMPRIME.TXT antigo. Se
// existe apaga o arquivo.
//-----------------------------------------------------------
if FileExists(´C:\Pdv\IMPRIME.TXT´) then
DeleteFile(´C:\IMPRIME.TXT´ );
sBufArq := ´´; sConteudo := ´´;
bExisteTexto := False;
while True do
begin
if FileExists( sPathRESP + ´INTPOS.001´ ) then
begin
//-----------------------------------------------------------
// Abre o arquivo INTPOS.001 gerado pela REDE, para leitura
// das linhas.
//-----------------------------------------------------------
AssignFile( RetIntPos, sPathRESP + ´INTPOS.001´ );
Reset( RetIntPos);
while not EOF( RetIntPos ) do
begin
ReadLn( RetIntPos, sBufArq );
f Acretido que seja por esta parte que eu deveria verificar se ele cancelou a operaçao se sim mandar o comando de nao confirmar a venda é isto ?
if copy( sBufArq, 1, 3 ) = ´030´ then
begin
if not Assigned( FrMensagemTEF ) Then
FrMensagemTef:=TFrMensagemTef.Create(Application);
FrMensagemTef.lblMensagem.Caption := copy( sBufArq, 11, Length( sBufArq ) - 10 );
FrMensagemTef.Show;
FrMensagemTef.Refresh;
sleep( 3000 );
FrMensagemTef.Close;
Application.ProcessMessages ;
break;
end;
if copy( sBufArq, 1, 3 ) = ´029´ then
begin
sConteudo := sConteudo + copy( sBufArq, 12, Length( sBufArq ) - 12 ) + #13 + 10;
bExisteTexto := True;
end;
end;
CloseFile( RetIntPos );
//frmPrincipal.Refresh;
break;
end;
end;

//-----------------------------------------------------------
// Prepara o arquivo IMPRIME.TXT. Este arquivo é uma cópia de
// segurança do texto da transação.
//-----------------------------------------------------------

AssignFile( RetIntPos, ´C:\Pdv\IMPRIME.TXT´ );
ReWrite( RetIntPos );
WriteLn( RetIntPos, sConteudo );
CloseFile( RetIntPos );
//-----------------------------------------------------------
// Abre o arquivo IMPRIME.TXT para começar a impressão das
// linhas.
//-----------------------------------------------------------
iRetorno := Bematech_FI_TerminaFechamentoCupom(´Obrigado Volte Sempre´);
// iRetorno := Bematech_FI_IniciaModoTEF();

if FileExists(´C:\Pdv\IMPRIME.TXT´) then
iRetorno := Bematech_FI_AbreComprovanteNaoFiscalVinculado( pchar( sForma ), ´´, ´´ );
AssignFile( RetIntPos, ´IMPRIME.TXT´ );
begin
Reset( RetIntPos );
sConteudo := ´´;
while not EOF( RetIntPos ) do
begin
Application.ProcessMessages ;
ReadLn( RetIntPos, sBufArq );
sConteudo := sConteudo + sBufArq + #13 + 10;
if bExisteTexto = True then
begin
if ( sBufArq = ´´ ) then sBufArq := ´ ´;
iRetorno := Bematech_FI_UsaComprovanteNaoFiscalVinculado( pchar( sBufArq ) + 13 );
if ( iRetorno <> 1 ) then
begin
iRetorno := Bematech_FI_FinalizaModoTEF();
If Application.MessageBox( ´Impressora não responde.´ + 13 +
´Tentar imprimir novamente ?´, ´Atenção´,
MB_IconQuestion + MB_YesNo ) = ID_Yes then
begin
//---------------------------------------------
// Fecha o Comprovante do TEF.
//---------------------------------------------
iRetorno := Bematech_FI_FechaComprovanteNaoFiscalVinculado();
CloseFile( RetIntPos );
bExisteTexto := True;
ImprimeGerencial;
bExisteTexto := False;
break;
end
else
begin
iRetorno := Bematech_FI_FinalizaModoTEF();
CloseFile( RetIntPos );
MSG_NCN;
NaoConfirmaTef ;
break;
end;
end;
if EOF( RetIntPos ) then
begin
cSaltaLinha := #13 + 10 + 13 + 10 + 13 + 10 + 13 + 10 + 13 + 10;
iRetorno := Bematech_FI_UsaComprovanteNaoFiscalVinculado( pchar( cSaltaLinha) );
FrMensagemTef:=TFrMensagemTef.Create(Application);
FrMensagemTef.lblMensagem.Caption := ´Por favor, destaque a 1ª Via´;
FrMensagemTef.Show;
FrMensagemTef.Refresh;
sleep( 5000 );
FrMensagemTef.Close;
iRetorno := Bematech_FI_UsaComprovanteNaoFiscalVinculado( pchar( sConteudo ) );
iRetorno := Bematech_FI_FinalizaModoTEF();
end;
end;
end;
end;
iRetorno := Bematech_FI_FinalizaModoTEF();
if ( bExisteTexto <> false ) then CloseFile( RetIntPos );
DeleteFile(´C:\Pdv\IMPRIME.TXT´ );
iRetorno := Bematech_FI_FechaComprovanteNaoFiscalVinculado();
ConfirmaTef ;
end;

procedure TTef.NaoConfirmaTef ;
var Arquivo: TStringList;
ArquivoINI: TIniFile;
Numero: Integer;
begin
SetaPath;
Arquivo := TStringList.Create;
ArquivoINI := TIniFile.Create(´C:\PDV\ESPDV.INI´);
// Atualiza INI e obtem o numero da última transacao
AtualizarINI(´NCN´, Numero, False);
sBufArq := ´´; sConteudo := ´´;
AssignFile( F, sPathRESP + ´INTPOS.001´ );
Reset( F );
while not EOF( F ) do
begin
ReadLn( F, sBufArq );
If ( copy( sBufArq, 1, 3 ) = ´001´ ) or ( copy( sBufArq, 1, 3 ) = ´010´ ) or
( copy( sBufArq, 1, 3 ) = ´012´ ) or
( copy( sBufArq, 1, 3 ) = ´027´ ) then
sConteudo := sConteudo + sBufArq + #13 + 10;
if ( copy( sBufArq, 1, 3 ) = ´999´ ) then
sConteudo := sConteudo + sBufArq;
end;
CloseFile( F );
sConteudo := ´000-000 = NCN´ + 13 + 10 + sConteudo;
AssignFile( F, ´INTPOS.001´ );
ReWrite( F );
WriteLn( F, sConteudo );
CloseFile( F );
CopyFile( pchar( ´INTPOS.001´ ), pchar( sPathREQ + ´INTPOS.001´ ), iRet );
DeleteFile( ´INTPOS.001´ );
DeleteFile( sPathRESP + ´INTPOS.001´ );
FechaTransacaoTef ;
end;

procedure Ttef.VerificaTransacao ;
begin
if TransacaoTEF Then
begin
Application.MessageBox( ´Exite uma transação pendente e a mesma será cancelada !´,
´Atenção´, MB_IconInformation + MB_OK );
MSG_NCN;
NaoConfirmaTef ;
end;
end;

procedure TTef.ConfirmaTef ;
begin
SetaPath;
sBufArq := ´´; sConteudo := ´´;
AssignFile( F, sPathRESP + ´INTPOS.001´);
Reset( F );
while not EOF( F ) do
begin
ReadLn( F, sBufArq );
If ( copy( sBufArq, 1, 3 ) = ´001´ ) or
( copy( sBufArq, 1, 3 ) = ´010´ ) or
( copy( sBufArq, 1, 3 ) = ´012´ ) or
( copy( sBufArq, 1, 3 ) = ´027´ ) then
sConteudo := sConteudo + sBufArq + #13 + 10;
if ( copy( sBufArq, 1, 3 ) = ´999´ ) then
sConteudo := sConteudo + sBufArq;
end;
CloseFile( F );
sConteudo := ´000-000 = CNF´ + 13 + 10 + sConteudo;
AssignFile( F, ´INTPOS.001´ );
ReWrite( F );
WriteLn( F, sConteudo );
CloseFile( F );
CopyFile( pchar( ´INTPOS.001´ ), pchar( sPathREQ + ´INTPOS.001´ ), iRet );
DeleteFile( ´INTPOS.001´ );
DeleteFile( sPathRESP + ´INTPOS.001´ );
end;

procedure TTef.CancelaTransacao ;
begin
SetaPath;
// Prepara o arquivo para o Cancelamento da Transação informada
sConteudo := ´´;
sConteudo := ´000-000 = CNC´ + #13 + 10 +
´001-000 = ´ + F_Identifica + 13 + 10 +
´003-000 = ´ + F_Valor + 13 + 10 +
´010-000 = ´ + F_Rede + 13 + 10 +
´012-000 = ´ + F_Doc + 13 + 10 +
´022-000 = ´ + F_Data + 13 + 10 +
´023-000 = ´ + F_Hora + 13 + 10 +
´999-999 = 0´;
AssignFile( F, ´INTPOS.001´ );
ReWrite( F );
WriteLn( F, sConteudo );
CloseFile( F );
// Copia o arquivo para o diretório do Gerenciador Padrão
CopyFile( pchar( ´INTPOS.001´ ), pchar( sPathREQ + ´INTPOS.001´ ), iRet );
DeleteFile( ´INTPOS.001´ );
ImprimirTXT;
iRetorno := Bematech_FI_FechaRelatorioGerencial();
end;

procedure TTef.CancelaVenda ;
begin
if not assigned(FrmCancelaVenda) Then
FrmCancelaVenda:=TFrmCancelaVenda.Create(Application);
FrmCancelaVenda.txtNIdent.Text :=F_Identifica ;
FrmCancelaVenda.txtREDE.Text :=F_Rede ;
FrmCancelaVenda.ShowModal ;
if (FrmCancelaVenda.ModalResult =mrOk) Then
begin
F_Valor :=FrmCancelaVenda.txtValor.Text ;
F_Doc :=FrmCancelaVenda.txtDOC.Text ;
F_Data :=FrmCancelaVenda.txtData.Text ;
F_Hora :=FrmCancelaVenda.txtHora.Text ;
CancelaTransacao ;
end;
FrmCancelaVenda.Release ;
FrmCancelaVenda:=nil ;
end;

////////////////////////////////////////////////////////////////////////////////
// Esta funções efetua a impressão do arquivo IMPRIME.TXT
// que contém o texto da transação.
////////////////////////////////////////////////////////////////////////////////
procedure TTef.ImprimirTXT;
var iVias: integer;
bContinua: boolean;
begin
sBufArq := ´´; sConteudo := ´´;
while True do
begin
if FileExists( ´IMPRIME.TXT´ ) then
begin
DeleteFile( ´IMPRIME.TXT´ );
break;
end
else
break;
end;
bContinua := True; bExisteTexto := False;
while bContinua = True do
begin
if FileExists( sPathRESP + ´INTPOS.001´ ) then
begin
try
AssignFile( F, sPathRESP + ´INTPOS.001´ );
Reset( F );
while not EOF( F ) do
begin
ReadLn( F, sBufArq );
if copy( sBufArq, 1, 3 ) = ´029´ then
begin
sConteudo := sConteudo + copy( sBufArq, 12, Length( sBufArq ) - 12) + #13 + 10;
bExisteTexto := True;
end;
end;
except
//Excessão Silenciosa
end;
end;
end;
CloseFile( F );
//-----------------------------------------------------------
// Prepara o arquivo IMPRIME.TXT. Este arquivo é uma cópia de
// segurança do texto da transação.
//-----------------------------------------------------------
if sConteudo <> ´´ then
begin
AssignFile( F, ´IMPRIME.TXT´ );
ReWrite( F );
WriteLn( F, sConteudo );
CloseFile( F );

//-----------------------------------------------------------
// Abre o arquivo IMPRIME.TXT para começar a impressão das
// linhas.
//-----------------------------------------------------------

AssignFile( F, ´IMPRIME.TXT´ );
for iVias := 1 to 2 do
begin
Reset( F );
sConteudo := ´´;
while not EOF( F ) do
begin
ReadLn( F, sBufArq );
//---------------------------------------------
// Verifica se a impressora fiscal está ligada
//---------------------------------------------
iRetorno := Bematech_FI_VerificaImpressoraLigada();
if iRetorno = 1 then
begin
if bExisteTexto = True then
begin
//---------------------------------------------
// Imprime a linhas lida do arquivo IMPRIME.TXT,
// no comprovante.
//---------------------------------------------
iRetorno := Bematech_FI_RelatorioGerencial( pchar( sBufArq ) );
FrMensagemTef.Close;
end
else
begin
FrMensagemTef.Close; break;
end;
end
else
If Application.MessageBox( ´Impressora não responde.´ + #13 +
´Tentar imprimir novamente ?´, ´Atenção´, MB_IconQuestion + MB_YesNo ) = ID_Yes then
begin
//---------------------------------------------
// Fecha o Comprovante do TEF.
//---------------------------------------------
iRetorno := Bematech_FI_FechaRelatorioGerencial();
CloseFile( F );
ImprimeGerencial;
break;
end
else
begin
CloseFile( F );
break;
end;
end;
end;
end;
end;
end.


Essistemas

Essistemas

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar