Jump to content
Fivewin Brasil

INTEGRAÇÃO COM MAQUINAS DE CARTÃO DE CREDITO / DEBITO


marcioe

Recommended Posts

Olá a todos os amigos, tudo bem 

Estou precisando fazer uma rotina para integrar com cartão de credito / debito (maquina)

Exemplo a maquina estará Plugada no PC, dai ao finalizar a venda seja enviado para a maquina o valor, etc... dai a pessoa passa o cartão digita a senha etc... 
Se aprovado preciso pegar o retorno e dar andamento no sistema pdv.

Agradeço desde já a todos

Link to comment
Share on other sites

Bom dia,


Segue um prg, que usado com o SITEF  da SOFTWARE EXPRESS. (Terá que fazer o contrato com eles, para usar o TEF dedicado - Transferência Eletrônica de Fundos, consumindo a dll deles ou a comunicação via "TXT"), ou o Gerenciador padrão, neste caso tem que ser homologado.

PDV -> PINPAD -> IMPRESSORA NÃO FISCAL ( para NFC-e ou SAT-ECF)

OU DESENVOLVER consumindo as dll ou api's das operadoras de cartão, que neste caso precisam ser homologados.

TEF.prg

#include "FiveWin.ch"
#include "Directry.ch"

*****************************************************************************
* PROGRAMA..: FAZTEF.PRG                                                    *
* FUNCAO....: Ativa gerenciador padrao de TEF.                              *
*                                                                           *
* PARÂMETROS: V_CTIPO -> Tipo da operação a ser executada;                  *
*             V_CTEF  -> Número de controle de operação TEF;                *
*             V_CPAGO -> Valor da operação passado como texto.              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                   EM: 14/05/02 *
*****************************************************************************
FUNCTION fazTef(v_cTipo,v_cTef,v_cPago, nCtTEF, nPgTEF, CintPos, lhiper, nMosMSG, lconf_pre)

LOCAL v_cBuf,v_cTxt,v_cCupom:=Space(6),v_n,v_nLin,v_nRet := 0, aRet,v_cRede,;
      v_cFinal,v_cNSU,oDlgTef,oSay[3],v_cValor, teste_val, osayTxtVal, osayTxtDoc, ;
      nValorSaque, nOrdCli

default lhiper := .f., nMosMSG := .t., v_cPago := '1,00', nCtTEF := 1, nPgTEF := 1, lconf_pre := .f.

//private ltravatef := .f.

ltravatef := .f.

  BEGIN  SEQUENC

     CursorWait()

     //FAZ VENDA EM CARTÃO
     IF v_cTipo = "CRT"

        nVezes := 1

        do while !ver_ativo(cdirtef)

           if !_tefdedic
              if ltravatef
                 liberatc()
              endif
           endif

           if !v_superM
              obtn_finaliza:disable()
              obtn_finaliza:refresh(.t.)
           endif
           if _tefdedic
              erro("Sem conexão SiTef","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           else
              erro("Gerenciador TEF não ativo!","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           endif
           if !v_superM
              obtn_finaliza:enable()
              obtn_finaliza:refresh(.t.)
           endif

           if !_tefdedic
              if ltravatef
                 travatc()
              endif
           endif

           sysrefresh()
        enddo

        DelMsgTef( cDirTef )
        v_cCupom = vdocum
        if val(StrTran(v_cPago,",")) > 999
           teste_val = StrTran(v_cPago,",")
        else
           teste_val = strzero(val(StrTran(v_cPago,",")),3)
        endif

        if !_tefdedic
           v_cTxt := "000-000 = CRT"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "004-000 = 0"+CRLF+;
                     "701-000 = SALC"+_versao+CRLF+;
                     "706-000 = 3"+CRLF+;
                     "716-000 = "+_soft+CRLF+;
                     "999-999 = 0"+CRLF
        else
           v_cTxt := "000-000 = CRT"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "210-084 = SALC"+_versao+"10"+CRLF+;
                     "701-034 = 4"+CRLF+;
                     "999-999 = 0"+CRLF
        endif

//                  "001-000 = "+v_cTef+CRLF+;
                  // "777-777 = teste Redecard"+CRLF+;    // Homologação
                  // "777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

        IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

           //SE TRANSAÇÃO APROVADA
           IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
              v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
              v_cBuf := ""
              FOR v_n = 1 TO v_nLin
                  v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                  sysrefresh()
              NEXT

              c_cnpj := ExtMsgTef(v_cTxt,"600-000")
              c_codban := ExtMsgTef(v_cTxt,"601-000")
              c_codaut := ExtMsgTef(v_cTxt,"602-000")

              if empty(c_cnpj) .and. empty(c_codban) .and. empty(c_codaut)

                 c_cnpj := ExtMsgTef(v_cTxt,"007-000")
                 c_codban := ExtMsgTef(v_cTxt,"010-000")
                 c_codaut := ExtMsgTef(v_cTxt,"013-000")

                 if "VISA"#upper(c_codban)
                    c_codban := "01"
                 elseif "MASTER"#upper(c_codban)
                    c_codban := "02"
                 elseif "AMEX"#upper(c_codban)
                    c_codban := "03"
                 elseif "SORO"#upper(c_codban)
                    c_codban := "04"
                 else
                    c_codban := "99"
                 endif

              endif

              if empty(c_cnpj)

                 nOrdCli := acliente->(indexord())
                 acliente->(dbsetorder(1))

                 if acliente->(dbseek(alltrim(albx_formas[iformas,19])))

                    c_cnpj := alltrim(acliente->CNPJ)

                 else

                    if v_nfce
                       erro("CNPJ da CREDENCIADA TEF não cadastrado!", "ERRO")
                       erro("NFC-E não poderá ser enviada sem essa informação!", "ERRO")
                    endif

                 endif

                 acliente->(dbsetorder(nOrdCli))

              endif

              if empty(c_codban)

                 c_codban := ExtMsgTef(v_cTxt,"010-003")

              endif

              if empty(c_codaut)
                 c_codaut := ExtMsgTef(v_cTxt,"013-000")
              endif

              if v_nfce .and. !v_nfce_ecf

                 cValorTEF := ExtMsgTef(v_cTxt,"003-000")

                 cValorTEF := val(cValorTEF)

                 v_cTpTran := ExtMsgTef(v_cTxt,"011-000")
                 v_cRedeTr  := ExtMsgTef(v_cTxt,"010-000")

                 aadd(aCOOCDC, { strzero(val(vdocum), 9), ;
                      albx_formas[iformas, 4], cValorTEF, albx_formas[iformas, 16], ;
                      v_cTpTran, v_cRedeTr, vtot_ven })

              endif

              if nPgTEF > 1 .and. nCtTEF < nPgTEF // verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                 nFileHandle := FCreate( cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                 if !_tefdedic
                    if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                       DelMsgTef(cdirtef)
                    endif
                 endif

                 albx_formas[iformas, 14] := cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt"
                 albx_formas[iformas, 15] := cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

              elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                 /*
                 if _tefdedic
                    if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                       DelMsgTef(cdirtef)
                    endif
                 endif
                 */

                 albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                 if !_tefdedic
                    albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                 else
                    albx_formas[iformas, 15] := if(file(cDirTef+"\IntPos.tmp"), cDirTef+"\IntPos.tmp", cDirTef+"\IntPos.001")
                 endif

              else
                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)
                 albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                 albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
              endif

              v_nRet := 1

              if _tefdedic

                 nValorSaque := round(Val( strtran(ExtMsgTef(v_cTxt,"200-000"), ',', '.') ), 2)

                 if nValorSaque > 0 // round((val(StrTran(v_cPago,",")) / 100), 2)
                    vtot_tef := vtot_tef + nValorSaque
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] + nValorSaque
                 endif

                 nDescCielo := round(Val( strtran(ExtMsgTef(v_cTxt,"210-081"), ',', '.') ), 2)

                 if nDescCielo > 0
                    vdesconto := vdesconto + (albx_formas[iformas, 2] - (nDescCielo + nValorSaque))
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] - ((albx_formas[iformas, 2] - nDescCielo)) + nValorSaque
                 endif

              else

                 nValorSaque := round(Val( ExtMsgTef(v_cTxt,"708-000") ) / 100, 2)

                 if (round((val(StrTran(v_cPago,",")) / 100), 2) + nValorSaque) > round((val(StrTran(v_cPago,",")) / 100), 2)
                    vtot_tef := vtot_tef + nValorSaque
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] + nValorSaque
                 endif

                 nDescCielo := round(Val( strtran(ExtMsgTef(v_cTxt,"709-000"), ',', '.') ), 2) / 100

                 if nDescCielo > 0
                    vdesconto := vdesconto + nDescCielo
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] - vdesconto
                    vtot_tef := vtot_tef - vdesconto
                 endif

              endif

           ELSE
              lTEFneg := .t.
              DelMsgTef( cDirTef )
              Ferase(cDirTef+"\IntPos.res")
              v_nRet := -21
           ENDIF
        ENDIF

     //FAZ VENDA COM CHEQUE - INCLUSO PARA REDECARD EM 22/03/06
     ELSEIF v_cTipo = "CHQ"

            nVezes := 1

            do while !ver_ativo(cdirtef)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if !v_superM
                  obtn_finaliza:disable()
                  obtn_finaliza:refresh(.t.)
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !v_superM
                  obtn_finaliza:enable()
                  obtn_finaliza:refresh(.t.)
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            DelMsgTef( cDirTef )
            v_cCupom = vdocum

            if val(StrTran(v_cPago,",")) > 999
               teste_val = StrTran(v_cPago,",")
            else
               teste_val = strzero(val(StrTran(v_cPago,",")),3)
            endif

            if !_tefdedic
               v_cTxt := "000-000 = CHQ"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "002-000 = "+v_cCupom+CRLF+;
                         "003-000 = "+AllTrim(teste_val)+CRLF+;
                         "004-000 = 0"+CRLF+;
                         "701-000 = SALC"+_versao+CRLF+;
                         "706-000 = 3"+CRLF+;
                         "716-000 = "+_soft+CRLF+;
                         "999-999 = 0"+CRLF
            else
               v_cTxt := "000-000 = CHQ"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "002-000 = "+v_cCupom+CRLF+;
                         "003-000 = "+AllTrim(teste_val)+CRLF+;
                         "999-999 = 0"+CRLF
            endif
//                         "001-000 = "+v_cTef+CRLF+;

*                         "032-927 = teste"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA
               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0" .OR. Substr(v_cTxt,At("009-000",v_cTxt)+10,2) = 'P1'
                  if Substr(v_cTxt,At("028-000",v_cTxt)+10,1) # "0"
                     v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
                     v_cBuf := ""
                     FOR v_n = 1 TO v_nLin
                         v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                         sysrefresh()
                     NEXT

                     if nPgTEF > 1 .and. nCtTEF < nPgTEF // verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                        nFileHandle := FCreate( cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                        if !_tefdedic
                           if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                              DelMsgTef(cdirtef)
                           endif
                        endif

                        albx_formas[iformas, 14] := cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt"
                        albx_formas[iformas, 15] := cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

                     elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                        nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                        if _tefdedic
                           if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                              DelMsgTef(cdirtef)
                           endif
                        endif

                        if !_tefdedic
                           albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                        else
                           albx_formas[iformas, 15] := if(file(cDirTef+"\IntPos.tmp"), cDirTef+"\IntPos.tmp", cDirTef+"\IntPos.001")
                        endif

                        albx_formas[iformas, 14] := cDirTef+"\imprime.txt"

                        //albx_formas[iformas, 15] := cDirTef+"\IntPos.001"

                     else
                        nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)
                        albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                        albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                     endif

                  endif

                  v_nRet := 1

               ELSE
                  DelMsgTef( cDirTef )
                  Ferase(cDirTef+"\IntPos.res")
                  v_nRet := -21
               ENDIF

            ENDIF

            if File(cDirTef+"\Intpos.res")
               Ferase(cDirTef+"\IntPos.res")
            endif

     //FAZ CONFIRMAÇÃO DE VENDA EM CARTÃO
     ELSEIF v_cTipo = "CNF"

            nVezes := 1

            do while !ver_ativo(cdirtef)
               //if ltravatef
               if !_tefdedic
                  liberatc()
               endif
               //endif
               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes > 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes > 3
                     exit
                  endif
               endif
               //if ltravatef
                 if !_tefdedic
                    travatc()
                 endif
               //endif
               sysrefresh()
            enddo

            v_cTxt   := MemoRead( if(!_tefdedic, cDirTef+"\RESP\IntPos.001",  CintPos))

            v_cCupom := ExtMsgTef(v_cTxt,"002-000")
            v_cNSU   := ExtMsgTef(v_cTxt,"012-000")
            v_cRede  := ExtMsgTef(v_cTxt,"010-000")
            v_cFinal := ExtMsgTef(v_cTxt,"027-000")

            if len(alltrim(v_cCupom)) == 0
               v_cTxt   := "000-000 = CNF"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            else
               v_cTxt   := "000-000 = CNF"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_cCupom+CRLF+;
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            endif

            if _tefdedic .and. nVezes > 3
               Ferase(cDirTef+"\RESP\IntPos.001")
            elseif !_tefdedic .and. nVezes > 3
               Ferase(cDirTef+"\RESP\IntPos.001")
            endif

            IF .NOT. Empty(EnvCmdTef( v_cTxt ))
               v_nRet := 1
               DelMsgTef(cDirTef)
            ENDIF

            if File(cDirTef+"\Intpos.res")
               Ferase(cDirTef+"\IntPos.res")
            endif

            if !_tefdedic
               liberatc()
            endif

     //CANCELA CUPOM TEF NÃO IMPRESSO
     ELSEIF v_cTipo = "NCN"

            if !_tefdedic
               travatc()
               ltravatef := .t.
            endif

            nVezes := 1

            do while !ver_ativo(cdirtef)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  if !lLoopNCN
                     nVezes++
                     if nVezes >= 3
                        exit
                     endif
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  if !lLoopNCN
                     nVezes++
                     if nVezes >= 3
                        exit
                     endif
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()
            enddo

            Ferase(cDirTef+"\Imprime.txt")

            v_cTxt   := MemoRead( if(_tefdedic, CintPos, cDirTef+"\RESP\IntPos.001") )

            v_cNSU   := ExtMsgTef(v_cTxt,"012-000")
            v_cRede  := ExtMsgTef(v_cTxt,"010-000")
            v_cFinal := ExtMsgTef(v_cTxt,"027-000")
            v_cValor := ExtMsgTef(v_cTxt,"003-000")

            IF v_cTef = NIL
               v_cTef := ExtMsgTef(v_cTxt,"001-000")
            ENDIF

            if !_tefdedic
               v_cTxt   := "000-000 = NCN"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            else
               v_CupVinc := ExtMsgTef(v_cTxt,"002-000")
               v_cTxt   := "000-000 = NCN"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_CupVinc+CRLF+;           // Cupom fiscal vinculado
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            endif

            IF .NOT. Empty(EnvCmdTef( v_cTxt ))
               DelMsgTef(cDirTef)
               nResp := .t.
            else
               if _tefdedic
                  nResp := .F.
               endif
            ENDIF

            if !_tefdedic
               liberatc()
               ltravatef := .f.
            endif

            if !_tefdedic

               DEFINE DIALOG odlg_ncn RESOURCE "TEFNCN" FONT fnt_ecf COLOR RGB(182,221,199), _CORPADRAO

               REDEFINE SAY ID 30 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
               REDEFINE SAY osayTxtDoc ID 31 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
               REDEFINE SAY ID 32 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
               REDEFINE SAY osayTxtVal ID 33 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE

               REDEFINE SAY oSay[1] ID 301 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
               REDEFINE SAY oSay[2] ID 302 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
               REDEFINE SAY oSay[3] ID 303 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE

               if upper(left(alltrim(v_cNsu),3)) <> 'ADM'
                  oSay[1]:SetText(v_cNSU)
               endif

	       oSay[2]:SetText(v_cRede)

	       IF val(v_cNsu) <> 0
                  oSay[2]:SetText(v_cRede)
	       else
	          osayTxtDoc:SetText("")
	          osayTxtDoc:refresh(.t.)
	          odlg_ncn:refresh(.t.)
	       ENDIF

	       IF val(v_cvalor) <> 0
                  if _tefdedic
                     oSay[3]:SetText(Trans(Val(v_cValor),"@E 999,999.99"))
                  else
                     oSay[3]:SetText(Trans(Val(v_cValor)/100,"@E 999,999.99"))
                  endif
	       else
	          osayTxtVal:SetText("")
	          osayTxtVal:refresh(.t.)
	          odlg_ncn:refresh(.t.)
	       ENDIF

               REDEFINE BUTTON ID 101 of odlg_ncn action odlg_ncn:end() COLOR cor_btn CANCEL

               odlg_ncn:lhelpicon:=.f.

               ACTIVATE DIALOG odlg_ncn CENTERED

            else

               if nResp .and. nMosMSG
                  erro("Transação não foi Efetuada."+CRLF+"Favor reter o Cupom.","Atenção")
               endif

            endif

            if File(cDirTef+"\Intpos.res")
               Ferase(cDirTef+"\IntPos.res")
            endif

     //FAZ ADM
     ELSEIF v_cTipo = "ADM"

            nVezes := 1

            do while !ver_ativo(cdirtef)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()
            enddo

            if !_tefdedic
               v_cTxt := "000-000 = ADM"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "701-000 = SALC"+_versao+CRLF+;
                         "706-000 = 3"+CRLF+;
                         "716-000 = "+_soft+CRLF+;
                         "999-999 = 0"+CRLF
            else
               v_cTxt := "000-000 = ADM"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "999-999 = 0"+CRLF
            endif

            IF .NOT. Empty( v_cTxt:=EnvCmdTef( v_cTxt ) )

               v_nLin := Val( ExtMsgTef(v_cTxt,"028-000") )
               v_Canc := Val( ExtMsgTef(v_cTxt,"025-000") )

               IF v_nLin > 0
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

                  if !_tefdedic
                     ltravatef := .t.
                     travatc()
                  endif

                  if _tefdedic

                     if !v_nfce

                        IF PrintCupTef( .T. ) = 1
                           fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                        ELSE
                           if v_Canc > 0
                              fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                              erro("Transação TEF confirmada."+CRLF+"Favor reimprimir último comprovante.","Atenção")
                           else
                              fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                           endif
                        ENDIF

                     else

                        cRet := MemoRead( cDirTef+"\imprime.txt")

                        nViasTEF := 2

                        for xTef := 1 to nViasTEF

                           oFrPrn := frReportManager():New()
                           oFrPrn:LoadFromFile(".\tef.fr3")

                           oFrPrn:AddVariable("Comprovante", "tef", "'"+cRet+"'")

                           oFrPrn:PrintOptions:SetShowDialog(.f.)
                           oFrPrn:PrepareReport()

                           oFrPrn:Print(.T.)

                           oFrPrn:ClearDataSets()
                           oFrPrn:DestroyFR()

                        next

                        fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")

                     endif

                  else

                     if !v_nfce

                        IF PrintCupTef( .T. ) = 1
                           liberatc()
                           fazTef("CNF",v_cTef,v_cPago)
                        ELSE
                           fazTef("NCN",v_cTef )
                        ENDIF

                     else

                        cRet := MemoRead( cDirTef+"\imprime.txt")

                        nViasTEF := 2

                        for xTef := 1 to nViasTEF

                           oFrPrn := frReportManager():New()
                           oFrPrn:LoadFromFile(".\tef.fr3")

                           oFrPrn:AddVariable("Comprovante", "tef", "'"+cRet+"'")

                           oFrPrn:PrintOptions:SetShowDialog(.f.)
                           oFrPrn:PrepareReport()

                           oFrPrn:Print(.T.)

                           oFrPrn:ClearDataSets()
                           oFrPrn:DestroyFR()

                        next

                        fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")

                     endif

                  endif

                  if !_tefdedic
                     liberatc()
                     ltravatef := .f.
                  endif

               ENDIF
               v_nRet := 1
               DelMsgTef( cDirTef )
               if File(cDirTef+"\Intpos.res")
                  Ferase(cDirTef+"\IntPos.res")
               endif
               Ferase(cDirTef+"\Imprime.txt")

            ENDIF

     ELSEIF v_cTipo = "CNC"

            if lhiper
               cdirant := cdirtef
               cdirtef := cdirhiper
            endif

            nVezes := 1

            do while !ver_ativo(cdirtef)
               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()
            enddo

            DelMsgTef( cdirtef )

            v_cTXT := MemoRead( CintPos )
            v_cCupom   := ExtMsgTef(v_cTxt,"002-000")
            v_valort   := ExtMsgTef(v_cTxt,"003-000")
            v_cRede  := ExtMsgTef(v_cTxt,"010-000")
            v_cNSU   := ExtMsgTef(v_cTxt,"012-000")
            v_dtcmv  := ExtMsgTef(v_cTxt,"022-000")
            v_hrcmv  := ExtMsgTef(v_cTxt,"023-000")
//            v_cFinal := ExtMsgTef(v_cTxt,"027-000")

            if !_tefdedic
               v_cTxt   := "000-000 = CNC"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_cCupom+CRLF+;
                           "003-000 = "+AllTrim(v_valort)+CRLF+;
                           "004-000 = 0"+CRLF+;
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "022-000 = "+v_dtcmv+CRLF+;
                           "023-000 = "+v_hrcmv+CRLF+;
                           "701-000 = SALC"+_versao+CRLF+;
                           "706-000 = 3"+CRLF+;
                           "716-000 = "+_soft+CRLF+;
                           "999-999 = 0"+CRLF
            else
               v_cTxt   := "000-000 = CNC"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_cCupom+CRLF+;
                           "003-000 = "+AllTrim(v_valort)+CRLF+;
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "022-000 = "+v_dtcmv+CRLF+;
                           "023-000 = "+v_hrcmv+CRLF+;
                           "999-999 = 0"+CRLF
            endif
//                        "027-000 = "+v_cFinal+CRLF+;

                     //"777-777 = teste Redecard"+CRLF+;    // Homologação   // se nao for necessario pra CNC retirar essas 2 linhas
                     //"777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA

               nResp := .T.

               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
                  v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

               ELSE
                  DelMsgTef( cdirtef )
                  Ferase(cdirtef+"\IntPos.res")
                  v_nRet := -21
                  nResp := .f.
               ENDIF

            ELSE
               if !_tefdedic
                  liberatc()
               endif

               DelMsgTef( cdirtef )
               Ferase(cdirtef+"\IntPos.res")
               v_nRet := -21
               nResp := .F.
            ENDIF

            if lhiper
               cdirtef := cdirant
               cdirant := cdirtef
            endif

     //FAZ PRE-AUTORIZACAO
     ELSEIF v_cTipo = "PRE"

        nVezes := 1

        do while !ver_ativo(cdirtef)

           if !_tefdedic
              if ltravatef
                 liberatc()
              endif
           endif

           if !v_superM
              obtn_finaliza:disable()
              obtn_finaliza:refresh(.t.)
           endif
           if _tefdedic
              erro("Sem conexão SiTef","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           else
              erro("Gerenciador TEF não ativo!","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           endif
           if !v_superM
              obtn_finaliza:enable()
              obtn_finaliza:refresh(.t.)
           endif

           if !_tefdedic
              if ltravatef
                 travatc()
              endif
           endif

           sysrefresh()
        enddo

        DelMsgTef( cDirTef )
        v_cCupom = vdocum
        if val(StrTran(v_cPago,",")) > 999
           teste_val = StrTran(v_cPago,",")
        else
           teste_val = strzero(val(StrTran(v_cPago,",")),3)
        endif

        if lconf_pre
           cConf501 := "0010000000000000000000000000000000000000000000000000000000000000000000000000000"
        else
           cConf501 := "1101000000000000000000000000000000000000000000000000000000000000000000000000000"
        endif

        if !_tefdedic
           v_cTxt := "000-000 = PRE"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "004-000 = 0"+CRLF+;
                     "501-001 = "+cConf501+CRLF+;
                     "701-000 = SALC"+_versao+CRLF+;
                     "716-000 = "+_soft+CRLF+;
                     "999-999 = 0"+CRLF
        else
           v_cTxt := "000-000 = PRE"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "210-084 = SALC"+_versao+"10"+CRLF+;
                     "501-001 = "+cConf501+CRLF+;
                     "701-034 = 4"+CRLF+;
                     "999-999 = 0"+CRLF
        endif

//                  "001-000 = "+v_cTef+CRLF+;
                  // "777-777 = teste Redecard"+CRLF+;    // Homologação
                  // "777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

        IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

           //SE TRANSAÇÃO APROVADA
           IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
              v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
              v_cBuf := ""
              FOR v_n = 1 TO v_nLin
                  v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                  sysrefresh()
              NEXT

              if nPgTEF > 1 .and. nCtTEF < nPgTEF // verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                 nFileHandle := FCreate( cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 if !lconf_pre

                    if !_tefdedic
                       ltravatef := .t.
                       travatc()
                    endif

                    if _tefdedic

                       IF PrintCupTef( .T. ) = 1
                          if !_tefdedic
                             liberatc()
                          endif
                          fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                       ELSE
                          fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                       ENDIF

                    else

                       IF PrintCupTef( .T. ) = 1
                          liberatc()
                          fazTef("CNF",v_cTef,v_cPago)
                       ELSE
                          fazTef("NCN",v_cTef )
                       ENDIF

                    endif

                    if !_tefdedic
                       liberatc()
                       ltravatef := .f.
                    endif

                 endif

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                 if lconf_pre
                    if !_tefdedic
                       if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                          DelMsgTef(cdirtef)
                       endif
                    endif
                 endif

                 if lconf_pre
                    albx_formas[iformas, 14] := cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt"
                    albx_formas[iformas, 15] := cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP"
                 endif

              elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 if !lconf_pre

                    if !_tefdedic
                       ltravatef := .t.
                       travatc()
                    endif

                    if _tefdedic

                       IF PrintCupTef( .T. ) = 1
                          if !_tefdedic
                             liberatc()
                          endif
                          fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                       ELSE
                          fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                       ENDIF

                    else

                       IF PrintCupTef( .T. ) = 1
                          liberatc()
                          fazTef("CNF",v_cTef,v_cPago)
                       ELSE
                          fazTef("NCN",v_cTef )
                       ENDIF

                    endif

                    if !_tefdedic
                       liberatc()
                       ltravatef := .f.
                    endif

                 endif

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                 if lconf_pre
                    albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                    if !_tefdedic
                       albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                    else
                       albx_formas[iformas, 15] := if(file(cDirTef+"\IntPos.tmp"), cDirTef+"\IntPos.tmp", cDirTef+"\IntPos.001")
                    endif
                 endif

              else

                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 if !lconf_pre

                    if !_tefdedic
                       ltravatef := .t.
                       travatc()
                    endif

                    if _tefdedic

                       IF PrintCupTef( .T. ) = 1
                          if !_tefdedic
                             liberatc()
                          endif
                          fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                       ELSE
                          fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                       ENDIF

                    else

                       IF PrintCupTef( .T. ) = 1
                          liberatc()
                          fazTef("CNF",v_cTef,v_cPago)
                       ELSE
                          fazTef("NCN",v_cTef )
                       ENDIF

                    endif

                    if !_tefdedic
                       liberatc()
                       ltravatef := .f.
                    endif

                 endif

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                 if lconf_pre
                    albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                    albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                 endif

              endif

              v_nRet := 1

           ELSE
              lTEFneg := .t.
              DelMsgTef( cDirTef )
              Ferase(cDirTef+"\IntPos.res")
              v_nRet := -21
           ENDIF
        ENDIF

     ENDIF

  END SEQUENC

RETURN v_nRet


FUNCTION ExtMsgTef( cTxt,cCod )
LOCAL cLin

   if At(cCod,cTxt) == 0
      cLin := ""
   else
      cLin := Substr(cTxt,At(cCod,cTxt)+10)
      cLin := Left(cLin,At(CHR(13)+CHR(10),cLin)-1)
   endif

RETURN cLin


FUNCTION EnvCmdTef( cTxt )

LOCAL nQte:= 0,cRet,nLin, c002Req, c001sts, c002sts, c003sts, c005Sts, nFileHandle

   Ferase(cDirTef+"\RESP\IntPos.Sts")
   nFileHandle := FCreate( cDirTef+"\REQ\IntPos.tmp", 0 )
   FWrite( nFileHandle, cTxt, Len(cTxt) )
   FClose( nFileHandle )

   copyfile(cDirTef+"\REQ\IntPos.tmp",cDirTef+"\REQ\IntPos.001")
   ferase(cDirTef+"\REQ\IntPos.tmp")

//   Frename(cDirTef+"\REQ\IntPos.tmp",cDirTef+"\REQ\IntPos.001")

   c001sts := ExtMsgTef(cTxt, "000-000")

   DO WHILE nQte < 4
      SysWait(2)
      IF File(cDirTef+"\RESP\IntPos.Sts")
         EXIT
      ENDIF
      nQte++
      sysrefresh()
   ENDDO

   IF nQte >= 4
      if !_tefdedic
         if ltravatef
            liberatc()
         endif
      endif

      if _tefdedic
         erro("Sem conexão SiTef","Atenção")
      else
         erro("Gerenciador TEF não ativo!","Atenção")
      endif

      if !_tefdedic
         if ltravatef
            travatc()
         endif
      endif

      if _tefdedic
         if c001sts # 'CNF'
            DelMsgTef( cDirTef )
         else
            ferase(cDirTef+"\IntPos.CNF")
            copyfile(cDirTef+"\REQ\IntPos.001",cDirTef+"\IntPos.CNF")
            ferase(cDirTef+"\REQ\IntPos.001")
            aImprime  := Directory(cDirTef+"\imprime*.txt" )
            if len(aImprime) > 0
               aeval(aImprime, {|a| ferase(cDirTef+"\"+alltrim(a[1]))})
            endif
         endif
      else
         DelMsgTef( cDirTef )
      endif

      RETURN ""
   ELSE
      if _tefdedic
         if file(cDirTef+"\IntPos.CNF")
            ferase(cDirTef+"\IntPos.001")
            copyfile(cDirTef+"\IntPos.CNF", cDirTef+"\REQ\IntPos.001")
            ferase(cDirTef+"\IntPos.CNF")
         endif
         c001stsD := MemoRead( cDirTef+"\RESP\IntPos.sts" )
         c001stsD := ExtMsgTef(c001stsD, "000-000")
         if c001stsD == 'NCN' .or. c001stsD == 'CNF'
            DelMsgTef( cDirTef )
            RETURN cTxt
         endif
      endif
   ENDIF

   nQte := 0

   DO WHILE nQte < if(_tefdedic, 300, 300) .AND. .NOT. File(cDirTef+"\RESP\IntPos.001")

      nQte++
      SysWait(2)

      if File(cDirTef+"\RESP\IntPos.001")

         c002Req := MemoRead( cDirTef+"\RESP\IntPos.001" )

         if c001sts == "CRT"

            // inicio teste numero 47 e 48 roteiro visa
            c002sts := ExtMsgTef(c002Req, "001-000")
            c003sts := ExtMsgTef(cTxt, "001-000")

            c002sts := val(c002sts)
            c003sts := val(c003sts)

            c009sts := ExtMsgTef(c002Req, "009-000")

            if c009sts == "0" .and. c002sts # c003sts
               Ferase(cDirTef+"\RESP\IntPos.001")
               loop
            endif  // fim teste numero 47 e 48 roteiro visa

         endif

         nFileHandle := FCreate( cDirTef+"\IntPos.tmp", 0 )
         FWrite( nFileHandle, c002Req, Len(c002Req) )
         FClose( nFileHandle )

         Frename(cDirTef+"\IntPos.tmp",cDirTef+"\IntPos.res")

      endif

      sysrefresh()

   ENDDO

   IF nQte < if(_tefdedic, 300, 300)
*      ok('Aguardando teste RedeCard...')  // Homologação
      cTxt := MemoRead( cDirTef+"\RESP\IntPos.001" )
      cRet := ExtMsgTef(cTxt,"030-000")
      nLin := Val( ExtMsgTef(cTxt,"028-000") )

      IF nLin = 0 .AND. .NOT. Empty(cRet)

         if type("Wvar") == "U"
            if valtype(ODLG_PROA) == 'O'
               SetForegroundWindow( ODLG_PROA:hWnd )
               sysrefresh()
            endif
            if valtype(ownd_tef) == 'O'
               ownd_tef:refresh(.t.)
               ownd_tef:setfocus()
            endif
            ok(cRet,"Atenção")
            Wvar := .t.
         endif

      ENDIF
   ELSE
      cTxt := ""
      if !_tefdedic
         if ltravatef
            liberatc()
         endif
      endif

      if _tefdedic
         erro("Sem conexão SiTef","Atenção")
      else
         erro("Gerenciador TEF não Responde !","Atenção")
      endif
      if !_tefdedic
         if ltravatef
            travatc()
         endif
      endif

      DelMsgTef( cDirTef )
   ENDIF

RETURN cTxt


FUNCTION DelMsgTef( cDir )

   Ferase(cDir+"\RESP\IntPos.001")
   Ferase(cDir+"\RESP\IntPos.sts")
   //Ferase(cDir+"\Imprime.txt")

RETURN NIL



/*function ApagTEF(cdir, nArqSeq, cArq001)

   if empty(cArq001) .and. empty(nArqSeq)
      return(NIL)
   endif

   Ferase(cArq001)
   Ferase(cDir+"\RESP\IntPos.sts")
   Ferase(nArqSeq)

return NIL */


function ApagTEFG(nArqSeq, cArq001)

   if empty(cArq001) .and. empty(nArqSeq)
      return(NIL)
   endif

   Ferase(cArq001)
   Ferase(nArqSeq)

return NIL



FUNCTION DelMsgTefM( cDir, ntefi, nPgTEF )

//   if ntefi # nPgTEF
      Ferase(cDir+"\IntPos"+strzero(ntefi, 2)+".TMP")
//   endif
   Ferase(cDir+"\RESP\IntPos.sts")
   Ferase(cDir+"\imprime"+if(ntefi == nPgTEF, "", strzero(ntefi, 2))+".txt")

RETURN NIL




*****************************************************************************
* PROGRAMA: PrintCupTef.PRG                                                 *
* FUNCAO..: Imprime cupom TEF.                                              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
*****************************************************************************
FUNCTION PrintCupTef( lErro, cForma, cValor )

LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg := "",cRet,nSecs:=Seconds(), var_imprimir

   IF File(cDirTef+"\imprime.txt")

      CursorWait()

      ownd_tef:SetFocus()
      ownd_tef:Show()

      aRet := tef_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cDirTef+"\RESP\IntPos.001" )
      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cDirTef+"\imprime.txt")

      //SE FOR IMPRESSÃO APÓS ERRO
      IF lErro

         lImpCCD := .f.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64
                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrintCupTef( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrintCupTef( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrintCupTef( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrintCupTef( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _daruma .or. _sweda .or. _elgin .or. _bematech // incluido em 05/02/2011

               lAlgoAberto := .f.

               if _daruma
                  cEstado := space(2)
                  if GrvCmdFis(41,,,@cEstado,,.f.)
                     cEstado := substr(cEstado, 1,1)
                     if cEstado # '0'
                        lAlgoAberto := .t.
                     endif
                  elseif !VFimPapel()
                      IF ErroPrinter(@aRet)
                         RETURN (PrintCupTef( .T., cForma, cValor ))
                      ELSE
                         RETURN -22
                      ENDIF
                  endif
               elseif _elgin

                  nStGerCDC := 0
                  E_StatExtMFD(@nStGerCDC)

                  if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                     lAlgoAberto := .t.
                  elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                     lAlgoAberto := .t.
                  endif

               elseif _bematech

                  if GrvCmdFis(80,,,,,.f.)
                     lAlgoAberto := .t.
                  endif

               else
                  if GrvCmdFis(41,,,,,.f.)
                     lAlgoAberto := .t.
                  endif
               endif

               if lAlgoAberto

                  if v_framework

                     cGerAbre := space(2)
                     nRet := DaMFDInf('56', @cGerAbre)
                     cGerAbre := alltrim(cGerAbre)

                     if cGerAbre == '4'

                        nQualComando := 25

                     else

                        nQualComando := 23

                     endif

                  else

                     nQualComando := 25

                  endif

                  IF GrvCmdFis(nQualComando,,,,,.F.)
                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrintCupTef( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrintCupTef( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            aRet[3]:SetText(cMsg)

            if _tefdedic
               c028001 := ExtMsgTef(cRet, "028-001")
               if val(c028001) == 0
                  nLinCorte := 0
                  nviastef := 1
               else
                  nLinCorte := val(c028001) + 1
               endif
            else
               nLinCorte := 0
            endif

            FOR n = 1 TO nviastef

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64

                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrintCupTef( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.f.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'

                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrintCupTef( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _elgin

                   lAlgoAberto := .f.
                   nStGerCDC := 0
                   E_StatExtMFD(@nStGerCDC)

                   if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                      lAlgoAberto := .t.
                   elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                      lAlgoAberto := .t.
                   endif

                   if !lAlgoAberto

                      cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                      nRetElgin := E_AbreGerMFD(cIndGer)

                      if nRetElgin == 0
                         IF ErroPrinter(@aRet)
                            RETURN (PrintCupTef( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _daruma

                   if v_framework

                      nRet := daProgFormas("RG", "TEF1", ";")

                      nRet := DaAbreGerMFD('TEF1')

                   endif


                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.F.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10) // reincluir
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)

                    nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor,,,,, cMsg1 := ExtMsgTef(cRet,"030-000"))

                    if nRetC == -22
                       RETURN -22
                    elseif nRetC == -30
                       RETURN 1
                    endif

                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF

                    if _urano .or. _epson
                       IF !GrvCmdFis(22,var_imprimir)
                          IF ErroPrinter(@aRet)
                             RETURN (PrintCupTef( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(@aRet)
                              RETURN (PrintCupTef( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else

                       if .NOT. GrvCmdFis(24,var_imprimir,,,,.f.)

                           IF ErroPrinter(@aRet)
                              RETURN (PrintCupTef( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       elseif !VFimPapel()

                          IF ErroPrinter(@aRet)
                             RETURN (PrintCupTef( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       ENDIF

                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. nviastef > 1

                   FOR k = 1 TO 10

                       if _urano .or. _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(@aRet)
                                RETURN (PrintCupTef( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrintCupTef( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else

                          if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(@aRet)
                                RETURN (PrintCupTef( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrintCupTef( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()
                      SysWait(3)
                      aRet[1]:SetFocus()   // Alteracao Alex

                      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                         aRet[3]:SetText(cMsg)
                      ENDIF

                   endif

                ENDIF

                sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma / Elgin / Sweda / Urano / Epson

      ELSE

         IF .NOT. GrvCmdFis(21,{cForma,cValor,NIL},,,,.F.)
            IF ErroPrinter(@aRet)
               RETURN (PrintCupTef( .T., cForma, cValor ))
            ELSE
               RETURN -22
            ENDIF

         elseif !VFimPapel()

             IF ErroPrinter(@aRet)
                RETURN (PrintCupTef( .T., cForma, cValor ))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         if _tefdedic
            c028001 := ExtMsgTef(cRet, "028-001")
//            nLinCorte := val(c028001) + 1
            if val(c028001) == 0
               nLinCorte := 0
               nviastef := 1
            else
               nLinCorte := val(c028001) + 1
            endif
         else
            nLinCorte := 0
         endif

         FOR n = 1 TO nviastef
             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()
             FOR k = 1 TO MlCount(cTxt)

                 nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor,,,,, cMsg1 := ExtMsgTef(cRet,"030-000"))

                 if nRetC == -22
                    RETURN -22
                 elseif nRetC == -30
                    RETURN 1
                 endif

                 IF .NOT. GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k), CRLF),48)+CRLF,,,,.F. )
                    IF ErroPrinter(@aRet)
                       RETURN (PrintCupTef( .T., cForma, cValor ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 elseif !VFimPapel()
                    IF ErroPrinter(@aRet)
                       RETURN (PrintCupTef( .T., cForma, cValor ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 ENDIF

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviastef > 1

                FOR k = 1 TO 10

                    if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                       IF ErroPrinter(@aRet)
                          RETURN (PrintCupTef( .T., cForma, cValor ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                       IF ErroPrinter(@aRet)
                          RETURN (PrintCupTef( .T., cForma, cValor ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[1]:SetFocus()   // Alteracao Alex

                IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                   aRet[3]:SetText(cMsg)
                ENDIF

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         else
            if !GrvCmdFis(88)
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif
      ENDIF

      aRet[1]:End() ; aRet[1] := NIL

   ELSE
      if !_tefdedic
         liberatc()
      endif

      ok("Não há cupons para imprimir!","Atenção")
      lRet := .f.
   ENDIF

RETURN If(lRet,1,0)


STATIC FUNCTION ErroPrinter(aRet)

  local retorno := .f.

  if valtype(aRet[1]) == 'O'
     aRet[1]:End() ; aRet[1] := NIL
     ownd_tef:SetFocus()
     ownd_tef:Show()

     if !_tefdedic
         liberatc()
     endif

     retorno := Continua("Impressora não Responde. Tentar imprimir novamente?")

     if !_tefdedic
        travatc()
     endif

     if retorno
        syswait(5)
     endif
  else
     retorno := .t.
  endif

RETURN retorno



STATIC FUNCTION tef_aguarde( cIcone,cMens )
LOCAL oDlgWait,oMtr,nPerc,oSay,oIco

   CursorWait()
   cIcone := If(cIcone=NIL,"I_AGUARDE",cIcone)

   DEFINE DIALOG oDlgWait RESOURCE "DLG_AGUARDE" FONT fnt_ecf COLOR cor_dlg

	REDEFINE ICON oIco NAME (cIcone)        ID 101 OF oDlgWait UPDATE
        REDEFINE SAY oSay                       ID 301 OF oDlgWait UPDATE
        REDEFINE METER oMtr VAR nPerc TOTAL 100 ID 901 OF oDlgWait UPDATE

        IF cMens # NIL
           oSay:SetText(cMens)
        ENDIF

    odlgWait:lhelpicon:=.f.
    ACTIVATE DIALOG oDlgWait CENTERED NOWAIT

RETURN {oDlgWait,oMtr,oSay}



*****************************************************************************
* PROGRAMA: GrvCmdTef.PRG                                                   *
* FUNCAO..: Envia comando para TEF.                                         *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
*****************************************************************************
FUNCTION GrvCmdTef(nCmd,uPar,lMens,lInic, nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

LOCAL nRet:=0, oIni, nTef:=0, cTef, kl, xki, aSoTEF, nQtVinc  // aImprime,

default cCodForma := ''

  lMens := If(lMens=NIL,.T.,lMens)
  lInic := If(lInic=NIL,.T.,lInic)

  if vl_seqtef == NIL
     vl_seqtef := vl_venda
  endif

  cTef := StrZero(val(vl_seqtef),10)

  //VER TEF ATIVO
  IF nCmd = 1
         nRet := BmTfStatus(cTef)

  //ACIONA TEF ADM
  ELSEIF nCmd = 2
         nRet := fazTef("ADM",cTef,uPar )

  //VENDA CARTÃO
  ELSEIF nCmd = 3
         nRet := fazTef("CRT",cTef,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA DE CARTÃO
  ELSEIF nCmd = 4

         if !_tefdedic
            travatc()
            ltravatef := .t.
         endif

         nRet := PrintCupTef( .F.,uPar[1], uPar[2] )

         if !_tefdedic
            liberatc()
            ltravatef := .f.
         endif

  //CONFIRMA IMPRESSÃO DE TEF
  ELSEIF nCmd = 5

         nRet := fazTef("CNF",cTef,uPar[1],,, cArq001) // original

  ELSEIF nCmd = 6

         nRet := fazTef("CHQ",cTef,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA COM MULTIPLOS CARTOES
  ELSEIF nCmd = 7

         if 'INTPOS.001'$upper(cArq001)
            COPYFILE(cDirTef+"\IntPos.001",cDirTef+"\RESP\IntPos.001", .f.)
            ferase(cDirTef+"\IntPos.001")
            cArq001 := cDirTef+"\RESP\IntPos.001"
         endif

         nRet := PrtCupTefM( .F., albx_formas[nCtTEF, 4], albx_formas[nCtTEF, 2], nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

         if nRet == -44
            nRet := 1
         endif

  // PRE-AUTORIZACAO
  ELSEIF nCmd = 8

         nRet := fazTef("PRE",cTef,uPar, nCtTEF, nPgTEF, , , , .t.)

  ENDIF

  ownd_tef:Show()

   IF lMens
      IF nRet = 0
             erro("Gerenciador TEF Inativo !","Atenção")

      ELSEIF nRet = -2
             erro("Parâmetro inválido para comando "+Str(nCmd,2)+" !","Atenção")

      ELSEIF nRet = -18
             erro("Não foi possível abrir/criar arquivo .001 ou .sts !","Atenção")

      ELSEIF nRet = -19
             erro("Numero de Identificação não bate com o do arquivo .001 !","Atenção")

      ELSEIF nRet = -20
             erro("Transação finalizada pelo operador !","Atenção")

      ELSEIF nRet = -21
             erro("A transação não foi aprovada !","Atenção")

      ELSEIF nRet = -22
             erro("Não foi possível terminar a impressão !","Atenção")

      ELSEIF nRet = -23
             erro("Não foi possível terminar a operação !","Atenção")

      ELSE
             RETURN .T.
      ENDIF

  ELSEIF nRet = 1
      RETURN .T.
  ENDIF

RETURN .F.



FUNCTION ver_ativo( cdir )

LOCAL nQte:= 0, retorno := .t., ctxt

cTxt := "000-000 = ATV"+CRLF+;   // colocar ATV antes de toda requisicao // para visa
        "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
        "999-999 = 0"+CRLF

if !_tefdedic
   if !ltravado
      travatc()
   endif
endif

Ferase(cDir+"\RESP\IntPos.sts")

Ferase(cDir+"\REQ\IntPos.tmp")

nFileHandle := FCreate( cDir+"\REQ\IntPos.tmp", 0 )
FWrite( nFileHandle, cTxt, Len(cTxt) )
FClose( nFileHandle )

Frename(cDir+"\REQ\IntPos.tmp",cDir+"\REQ\IntPos.001")
Ferase(cDir+"\REQ\IntPos.tmp")

DO WHILE nQte < 4
   SysWait(2)
   IF File(cDir+"\RESP\IntPos.Sts")
      Ferase(cDir+"\RESP\IntPos.sts") // ver se não vai afetar nada, se afetar retirar, em 03/07/2012.
      EXIT
   ENDIF
   nQte++
   sysrefresh()
ENDDO

IF nQte >= 4
   retorno = .f.
ENDIF

if !_tefdedic
   if !ltravado
      liberatc()
   endif
endif

return retorno



function VFimBobina()

local Wret := .t., cErroExt

private vSemPapel := .f.

if v_NFCE .and. !v_NFCE_ECF
   return(.t.)
endif

if _bematech

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0

  nRet := Bmretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _daruma

  uBuf1_status := space(5)
  ubuf2_status := space(150)
  ubuf3_status := space(5)

  nComando := 8

  if v_framework

     nRet := DaECF(@nComando, @ubuf2_status)

     ubuf2_status := alltrim(ubuf2_status)
     ubuf2_status := strtran(ubuf2_status, chr(0), '')

     if nRet == -12
        nRet := 1
     endif

     if ubuf2_status == 'Bobina de papel ausente'
        Wret := .f.
     endif

  endif

elseif _Sweda

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := Ecfretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _elgin

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := E_retorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _urano

  vSemPapel := .f.
  Estado_Urano()
  if vSemPapel
     Wret := .f.
  endif

elseif _epson

/*
   if EPSON_AbrePorta() # 0
      erro('Não foi possível abrir a porta de comunicação com a Impressora!')
      retorno := .f.
   endif
*/

   cEstadoImpressora := space(17)
   cEstadoFisc := space(17)
   cRetCmd := space(5)
   cMsgCmd := space(101)

   nRet := EPSON_RetornoEx( @cEstadoImpressora, @cEstadoFisc, @cRetCmd, @cMsgCmd)

//   msginfo(cEstadoImpressora)

/*
   msginfo(cEstadoImpressora)
   msginfo(cEstadoFisc)
   msginfo(cRetCmd)
   msginfo(cMsgCmd)
*/

   if substr(cEstadoImpressora, 15, 1) == '1' .or. ;
      substr(cEstadoImpressora, 3, 1) == '1' .or. ;
      substr(cEstadoImpressora, 9, 1) == '1' .or. ;
      substr(cEstadoImpressora, 8, 1) == '1'
      Wret := .f.
   endif

/*
  vflag_aberto := space(21)
  GrvCmdFis(04,NIL,21,@vflag_aberto,,.f.)

  vflag_aberto := substr(vflag_aberto, 9, 4)

  vflag_aberto := HexToDec(vflag_aberto)

  *** Analisando o flag

  if vflag_aberto >= 32768
     vflag_aberto  = vflag_aberto - 32768
     Wret := .f.
  endif

  if vflag_aberto >= 16384
     vflag_aberto  = vflag_aberto - 16384
     Wret := .f.
  endif

  if vflag_aberto >= 4096
     vflag_aberto  = vflag_aberto - 4096
//     Wret := .f.
  endif

  if vflag_aberto >= 2048  && Memória Fiscal sem espaco
     vflag_aberto  = vflag_aberto - 2048
     Wret := .f.
  endif

  if vflag_aberto >= 1024  && Memória Fiscal sem espaco
     vflag_aberto  = vflag_aberto - 1024
     Wret := .f.
  endif

  if vflag_aberto >= 512
     vflag_aberto  = vflag_aberto - 512
     Wret := .f.
  endif

  if vflag_aberto >= 256
     vflag_aberto  = vflag_aberto - 256
     Wret := .f.
  endif

  if vflag_aberto >= 128
     vflag_aberto  = vflag_aberto - 128
     Wret := .f.
  endif

  if vflag_aberto >= 64
     vflag_aberto  = vflag_aberto - 64
     Wret := .f.
  endif

  if vflag_aberto >= 32
     vflag_aberto  = vflag_aberto - 32
     Wret := .f.
  endif

  if vflag_aberto >= 16
     vflag_aberto  = vflag_aberto - 16
     Wret := .f.
  endif

  if vflag_aberto >= 8
     vflag_aberto  = vflag_aberto - 8
     Wret := .f.
  endif

  if vflag_aberto >= 4
     vflag_aberto  = vflag_aberto - 4
     Wret := .f.
  endif

  if vflag_aberto >= 2
     vflag_aberto  = vflag_aberto - 2
     Wret := .f.
  endif

//  if vflag_aberto >= 1  && Cupom Fiscal Aberto
//     vflag_aberto  = vflag_aberto - 1
//  endif

*/

endif

return(Wret)




function VFimPapel()

local Wret := .t., cErroExt

private vSemPapel := .f.

if v_NFCE .and. !v_NFCE_ECF
   return(.t.)
endif

if _bematech

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0

  nRet := Bmretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _daruma

  uBuf1_status := space(5)
  ubuf2_status := space(5)
  ubuf3_status := space(5)

  if v_framework

     nRet := Daretorno(uBuf1_status, ubuf2_status)

     if nRet == -12
        nRet := 1
     endif

     uBuf1_status := val(uBuf1_status)
     ubuf2_status := val(uBuf2_status)

     if uBuf1_status == 72 .or. ubuf2_status == 2
        Wret := .f.
     endif

  endif

elseif _Sweda

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := Ecfretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _elgin

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := E_retorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _urano

  vSemPapel := .f.
  Estado_Urano()
  if vSemPapel
     Wret := .f.
  endif

elseif _epson

   cEstadoImpressora := space(17)
   cEstadoFisc := space(17)
   cRetCmd := space(5)
   cMsgCmd := space(101)

   nRet := EPSON_RetornoEx( @cEstadoImpressora, @cEstadoFisc, @cRetCmd, @cMsgCmd)

   if substr(cEstadoImpressora, 15, 1) == '1' .or. ;
      substr(cEstadoImpressora, 3, 1) == '1' .or. ;
      substr(cEstadoImpressora, 9, 1) == '1' .or. ;
      substr(cEstadoImpressora, 8, 1) == '1'
      Wret := .f.
   endif

endif

return(Wret)






*****************************************************************************
* PROGRAMA: PrtCupTefM.PRG                                                 *
* FUNCAO..: Imprime cupom TEF.                                              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
* ALTERADO: Sebastiao de Oliveira Pessoa                     EM: 09-20-2010 *
*****************************************************************************
FUNCTION PrtCupTefM( lErro, cForma, cValor, nki, nPgTEF, cNumArq, cArq001, cCodForma)

LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg := "",cRet,nSecs:=Seconds(), var_imprimir, aImprime, aImprime1, aImprime2

private nViasMC, nTefGP, nTefHp, nTefTc

   IF File(cNumArq)

      CursorWait()

      ownd_tef:SetFocus()
      ownd_tef:Show()

      aRet := tef_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cArq001)  //               "\RESP\IntPos.001" )
      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cNumArq)

      //SE FOR IMPRESSÃO APÓS ERRO

      IF lErro

         lGerImp := .t.

         cTxt := ""
         nViasMC := 0

         if _tefdedic
            nTefGP  := Directory(_cdirtef+"\imprime*.txt" )
            nViasMC := len(nTefGP)
         else
            nTefGP  := Directory(_cdirtef+"\imprime*.txt" )
            nTefHp  := Directory(cdirhiper+"\imprime*.txt" )
            nTefTc  := Directory(cdirtecban+"\imprime*.txt" )
            nViasMC := len(nTefGP) + len(nTefHp) + len(nTefTc)
         endif

         LeAposerro(@aImprime, @aImprime1, @aImprime2, @cTxt)

         lImpCCD := .f.
         lerroImp := .t.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64

                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )) // (PrtCupTefM( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _daruma .or. _sweda .or. _elgin .or. _bematech // incluido em 05/02/2011

               lAlgoAberto := .f.

               if _daruma

                  cEstado := space(2)
                  if GrvCmdFis(41,,,@cEstado,,.f.)
                     cEstado := substr(cEstado, 1,1)
                     if cEstado # '0'
                        lAlgoAberto := .t.
                     endif
                  elseif !VFimPapel()
                     IF ErroPrinter(@aRet)
                        RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                     ELSE
                        RETURN -22
                     ENDIF
                  endif

               elseif _elgin

                  nStGerCDC := 0
                  E_StatExtMFD(@nStGerCDC)

                  if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                     lAlgoAberto := .t.
                  elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                     lAlgoAberto := .t.
                  endif

               elseif _bematech

                  if GrvCmdFis(80,,,,,.f.)
                     lAlgoAberto := .t.
                  endif

               else
                  if GrvCmdFis(41,,,,,.F.)
                     lAlgoAberto := .t.
                  endif
               endif

               if lAlgoAberto

                  if v_framework

                     cGerAbre := space(2)
                     nRet := DaMFDInf('56', @cGerAbre)
                     cGerAbre := alltrim(cGerAbre)

                     if cGerAbre == '4'

                        nQualComando := 25

                     else

                        nQualComando := 23

                     endif

                  else

                     nQualComando := 25

                  endif

                  IF GrvCmdFis(nQualComando,,,,,.F.)
                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            aRet[3]:SetText(cMsg)

            if _tefdedic
               if nViasMC > 1
                  nLinCorte := 0
                  nviastef := 1
               else
                  c028001 := ExtMsgTef(cRet, "028-001")
                  if val(c028001) == 0
                     nLinCorte := 0
                     nviastef := 1
                  else
                     nLinCorte := val(c028001) + 1
                  endif
               endif
            else
               nLinCorte := 0
            endif

            FOR n := 1 TO if(nViasMC == 1, nviastef, 1)

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64

                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'
                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF
                   endif

                elseif _elgin

                     lAlgoAberto := .f.
                     nStGerCDC := 0
                     E_StatExtMFD(@nStGerCDC)

                     if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                        lAlgoAberto := .t.
                     elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                        lAlgoAberto := .t.
                     endif

                     if !lAlgoAberto

                        cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                        cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                        nRetElgin := E_AbreGerMFD(cIndGer)

                        if nRetElgin == 0
                           IF ErroPrinter(@aRet)
                              RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF
                        ENDIF

                     endif

                elseif _daruma

                   if v_framework

                      nRet := daProgFormas("RG", "TEF1", ";")

                      nRet := DaAbreGerMFD('TEF1')

                   endif

                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.F.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10)
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)

                    nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor, @nki, @nPgTEF, @cNumArq, @cArq001, cMsg1 := ExtMsgTef(cRet,"030-000"))

                    if nRetC == -22
                       RETURN -22
                    elseif nRetC == -30
                       RETURN 1
                    endif

                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF

                    if _urano .or. _epson

                       IF !GrvCmdFis(22,var_imprimir)

                          if _urano

                             nEstado := 0
                             GrvCmdFis(78,,,@nEstado,,.F.)

                             if nEstado == 32 .or. nEstado == 64

                                IF ErroPrinter(@aRet)
                                   RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             else

                                IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                   IF ErroPrinter(@aRet)
                                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF
                                ENDIF

                             endif

                          else

                             IF ErroPrinter(@aRet)
                                RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF

                          endif

                       elseif !VFimPapel()

                           IF ErroPrinter(@aRet)
                              RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else

                       IF .NOT. GrvCmdFis(24,var_imprimir,,,,.f.)
                          IF ErroPrinter(@aRet)
                             RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(@aRet)
                              RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. if(nViasMC == 1, nviastef, 1) > 1

                   FOR k = 1 TO 10

                       if _urano .or. _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )

                             if _urano

                                nEstado := 0
                                GrvCmdFis(78,,,@nEstado,,.F.)

                                if nEstado == 32 .or. nEstado == 64

                                   IF ErroPrinter(@aRet)
                                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF

                                else

                                   IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                      IF ErroPrinter(@aRet)
                                         RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                      ELSE
                                         GrvCmdFis(25,,,,,.F.)
                                         RETURN -22
                                      ENDIF
                                   ENDIF

                                endif

                             else

                                IF ErroPrinter(@aRet)
                                   RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             endif

                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else
                          if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(@aRet)
                                RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano .and. !_tefdedic
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()
                      SysWait(3)
                      aRet[1]:SetFocus()   // Alteracao Alex

                      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                         aRet[3]:SetText(cMsg)
                      ENDIF

                   endif

                ENDIF

                sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma / Elgin / Sweda / Urano

         aRet[1]:End() ; aRet[1] := NIL

         RETURN -44

      ELSE

         IF !GrvCmdFis(if(_daruma, 78, 21),{cForma,cValor,NIL},,,,.F.)

            IF ErroPrinter(@aRet)
               RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
            ELSE
               RETURN -22
            ENDIF

         elseif !VFimPapel()

             IF ErroPrinter(@aRet)
                RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         v_cTpTran := ExtMsgTef(cRet,"011-000")
         v_cRedeTr  := ExtMsgTef(cRet,"010-000")

         if _bematech
            if W_MFD == 1 .or. cModeloBm == 'MP25FI' .or. cModeloBm == 'MP50FI'
               cCooCDC := space(6)
               GrvCmdFis(14,, 6, @cCooCDC)
               aadd(aCOOCDC, { strzero(val(cCooCDC), 9), cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr, vtot_ven })
            endif
         else
            cCooCDC := space(6)
            GrvCmdFis(14,, 6, @cCooCDC)
            aadd(aCOOCDC, { strzero(val(cCooCDC), 9), cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr, vtot_ven })
         endif

         if _tefdedic
            c028001 := ExtMsgTef(cRet, "028-001")
//            nLinCorte := val(c028001) + 1
            if val(c028001) == 0
               nLinCorte := 0
               nviastef := 1
            else
               nLinCorte := val(c028001) + 1
            endif
         else
            nLinCorte := 0
         endif

         FOR n = 1 TO nviastef

             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()
             FOR k = 1 TO MlCount(cTxt)

                 nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor, @nki, @nPgTEF, @cNumArq, @cArq001, cMsg1 := ExtMsgTef(cRet,"030-000"))

                 if nRetC == -22
                    RETURN -22
                 elseif nRetC # -22 .and. k == MlCount(cTxt)
                    exit
                 elseif nRetC == -30
                    RETURN 1
                 endif

                 IF .NOT. GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k), CRLF),48)+CRLF,,,,.F. )
                    IF ErroPrinter(@aRet)
                       RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 elseif !VFimPapel()
                    IF ErroPrinter(@aRet)
                       RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 ENDIF

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviastef > 1
                FOR k = 1 TO 10

                    if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                       IF ErroPrinter(@aRet)
                          RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                       IF ErroPrinter(@aRet)
                          RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[1]:SetFocus()   // Alteracao Alex

                IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                   aRet[3]:SetText(cMsg)
                ENDIF

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         else

            if !GrvCmdFis(88)
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif
      ENDIF

      if valtype(aRet[1]) == 'O'
         aRet[1]:End() ; aRet[1] := NIL
      endif

   ELSE
      if !_tefdedic
         liberatc()
      endif
      ok("Não há cupoms para imprimir !","Atenção")
   ENDIF

RETURN(If(lRet,1,0))



function Leaposerro(aImprime, aImprime1, aImprime2, cTxt)

private nQueTEF := 0

cTxt := ""

if _tefdedic
   aImprime  := Directory(_cdirtef+"\imprime*.txt" )
   aImprime1 := {}
   aImprime2 := {}
else
   aImprime  := Directory(_cdirtef+"\imprime*.txt" )
   aImprime1 := Directory(cdirtecban+"\imprime*.txt" )
   aImprime2 := Directory(cdirhiper+"\imprime*.txt" )
endif

if (len(aImprime) >= 1 .or. len(aImprime1) >= 1 .or. len(aImprime2) >= 1) .and. nViasMC == 1

   if _tefdedic
      if len(nTefGP) > 0
         cTxt := Memoread(_cdirtef+"\imprime.txt" )
      endif
   else
      if len(nTefGP) > 0
         cTxt := Memoread(_cdirtef+"\imprime.txt" )
      elseif len(nTefHp) > 0
         cTxt := Memoread(cdirhiper+"\imprime.txt" )
      elseif len(nTefTc) > 0
         cTxt := Memoread(cdirtecban+"\imprime.txt" )
      endif
  endif

elseif len(aImprime) >= 1 .or. len(aImprime1) >= 1 .or. len(aImprime2) >= 1

   CriaTxt(@cTxt)

endif

return(.t.)



function CriaTxt(cTxt)

local kl, ncont, nTefDial, nTefTecB, nTefHiper, nTotTef, nSequen := 1, cTxt1 := ''

cTxt := ""

if _tefdedic
   nTefDial := len(Directory(_cdirtef+"\imprime*.txt" ))
   nTefTecB := 0
   nTefHiper := 0
else
   nTefDial := len(Directory(_cdirtef+"\imprime*.txt" ))
   nTefTecB := len(Directory(cdirtecban+"\imprime*.txt" ))
   nTefHiper := len(Directory(cdirhiper+"\imprime*.txt" ))
endif

if _tefdedic

   nTotTef := nTefDial
   cTxt := ""

   for kl := 1 to len(albx_formas)

       if albx_formas[kl, 2] == 0 .or. albx_formas[kl, 3] # 'A'
          loop
       endif

       if file(albx_formas[kl, 14])

          cTxt1 := MemoRead(albx_formas[kl, 15])

          c028001 := ExtMsgTef(cTxt1, "028-001")
          c028000 := ExtMsgTef(cTxt1, "028-000")

          nFimCupTef := val(c028000)

          if val(c028001) == 0
             nLinCorte := 0
             nviastef := 1
          else
             nLinCorte := val(c028001)
          endif

          if nLinCorte > 0

             cTxtV1 := MemoRead(albx_formas[kl, 14])

             for k := 1 to nLinCorte

                 cTxt := cTxt + Left(StrTran( Memoline(cTxtV1,50,k),CRLF),48)+CRLF

                 sysrefresh()

             next

             for ncont := 1 to 10
                 cTxt := cTxt + (Space(10)+CRLF)
                 sysrefresh()
             next

             for k := (nLinCorte + 1) to nFimCupTef

                 cTxt := cTxt + Left(StrTran( Memoline(cTxtV1,50,k),CRLF),48)+CRLF
                 sysrefresh()

             next

             if nSequen < nTotTef
                for ncont := 1 to 10
                    cTxt := cTxt + (Space(10)+CRLF)
                    sysrefresh()
                next
             endif

          else

             cTxtV1 := MemoRead(albx_formas[kl, 14])

             for k := 1 to nFimCupTef

                 cTxt := cTxt + Left(StrTran( Memoline(cTxtV1,50,k),CRLF),48)+CRLF
                 sysrefresh()

             next

          endif

          nSequen++

       endif

       sysrefresh()

   next

else

   nTotTef := nTefDial + nTefTecB + nTefHiper

   for kl := 1 to len(albx_formas)

       if albx_formas[kl, 2] == 0 .or. albx_formas[kl, 3] # 'A'
          loop
       endif

       if file(albx_formas[kl, 14])

          cTxt := cTxt + MemoRead(albx_formas[kl, 14])

          for ncont := 1 to 10
              cTxt := cTxt + (Space(10)+CRLF)
              sysrefresh()
          next

          cTxt := cTxt + MemoRead(albx_formas[kl, 14])

          if nSequen < nTotTef
             for ncont := 1 to 10
                 cTxt := cTxt + (Space(10)+CRLF)
                 sysrefresh()
             next
          endif

          nSequen++

       endif

       sysrefresh()

   next

endif

return(.t.)



function FTEF_Corte(k, nLinCorte, aRet, lerro, n, cForma, cValor, nki, nPgTEF, cNumArq, cArq001, cMsgTela)

local nRet := 1

begin sequence

if _tefdedic

    if k == nLinCorte .and. nLinCorte > 1

       nret := TEF_Espaco(@lerro, @n, @aRet, @cForma, @cValor, @nki, @nPgTEF, @cNumArq, @cArq001)

       if nRet == -22 .or. nRet == -30
          break
       endif

       if _epson
          EPSON_CortaPapel()
       elseif _urano
          GRVCMDFIS(80,,,,,.f.)
       elseif _elgin
          Elgin_Corta(1)
       elseif _bematech
          BmCortePapel(0)
       elseif _sweda
          ecfCorte(1)
       elseif _daruma
          DaGuilhotina("1")
       endif

       if valtype(aRet) == 'A' .and. len(aRet) > 0
          SysWait(1)
          aRet[3]:SetText("Destaque o comprovante.")
          SysWait(5)

          aRet[3]:SetText(cMsgTela)

          if aRet[1] # NIL
             aRet[1]:SetFocus()   // Alteracao Alex
          endif

       endif

   endif

endif

end sequence

return(nRet)



function TEF_Espaco(lerro, n, aRet, cForma, cValor, nki, nPgTEF, cNumArq, cArq001)

local nLinhasTEF := 10

if lerro

   if n = 1

      FOR k = 1 TO nLinhasTEF

          if _urano .or. _epson

             if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                IF ErroPrinter(@aRet)
                   nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                   RETURN(if(nRetPrin == -44, -30, -22))
                ELSE
                   GrvCmdFis(25,,,,,.F.)
                   RETURN -22
                ENDIF
             elseif !VFimPapel()
                 IF ErroPrinter(@aRet)
                    nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                    RETURN(if(nRetPrin == -44, -30, -22))
 //                   RETURN(-30)
                 ELSE
                    GrvCmdFis(25,,,,,.F.)
                    RETURN -22
                 ENDIF
             endif

          else

             if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                IF ErroPrinter(@aRet)
                   nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                   RETURN(if(nRetPrin == -44, -30, -22))
//                   RETURN(-30)
                ELSE
                   GrvCmdFis(25,,,,,.F.)
                   RETURN -22
                ENDIF
             elseif !VFimPapel()
                 IF ErroPrinter(@aRet)
                    nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                    RETURN(if(nRetPrin == -44, -30, -22))
//                    RETURN(-30)
                 ELSE
                    GrvCmdFis(25,,,,,.F.)
                    RETURN -22
                 ENDIF
             endif
          endif

          sysrefresh()

      NEXT

   endif

else
   IF n = 1

      FOR k = 1 TO nLinhasTEF

          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
             IF ErroPrinter(@aRet)
                nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                RETURN(if(nRetPrin == -44, -30, -22))
//                RETURN(-30)
             ELSE
                GrvCmdFis(23)
                RETURN -22
             ENDIF

          elseif !VFimPapel()

             IF ErroPrinter(@aRet)
                nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                RETURN(if(nRetPrin == -44, -30, -22))
//                RETURN(-30)
             ELSE
                GrvCmdFis(23)
                RETURN -22
             ENDIF

          endif

          sysrefresh()

      NEXT

   endif
endif

return(1)

TECBAN.prg

#include "FiveWin.ch"

*****************************************************************************
* PROGRAMA..: FAZTECBAN.PRGF                                                *
* FUNCAO....: Ativa gerenciador padrao de TEC-BAN                           *
*                                                                           *
* PARÂMETROS: V_CTIPO -> Tipo da operação a ser executada;                  *
*             V_CTecBan  -> Número de controle de operação TecBan;          *
*             V_CPAGO -> Valor da operação passado como texto.              *
*                                                                           *
* AUTOR...: Alexandre Pereira                                  EM: 19/07/04 *
*****************************************************************************
FUNCTION fazTecBan(v_cTipo,v_cTecBan,v_cPago, nCtTEF, nPgTEF, CintPos)

LOCAL v_cBuf,v_cTxt,v_cCupom:=Space(6),v_n,v_nLin,v_nRet := 0,aRet,v_cRede,;
      v_cFinal,v_cNSU,oDlgTecBan,oSay[3],v_cValor, teste_val, osayTxtVal

  ltravatef := .f.

  BEGIN  SEQUENC

     CursorWait()

     //FAZ VENDA EM CARTÃO

     IF v_cTipo = "CRT"

        if !v_superM
           ODLG_pag:disable()
           ODLG_pag:refresh(.t.)
        endif

        nVezes := 1

        do while !ver_ativo(cdirtecban)

           if !_tefdedic
              if ltravatef
                 liberatc()
              endif
           endif

           if _tefdedic
              erro("Sem conexão SiTef","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           else
              erro("Gerenciador TECBAN não ativo!","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           endif

           if !_tefdedic
              if ltravatef
                 travatc()
              endif
           endif

           sysrefresh()

        enddo

        if !v_superM
           ODLG_pag:enable()
           ODLG_pag:refresh(.t.)
        endif

        DelMsgTecBan( cdirtecban )
        v_cCupom = vdocum
        if val(StrTran(v_cPago,",")) > 999
           teste_val = StrTran(v_cPago,",")
        else
           teste_val = strzero(val(StrTran(v_cPago,",")),3)
        endif

        v_cTxt := "000-000 = CRT"+CRLF+;
                  "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                  "002-000 = "+v_cCupom+CRLF+;
                  "003-000 = "+AllTrim(teste_val)+CRLF+;
                  "999-999 = 0"+CRLF

//                   "777-777 = TESTE TECBAN"+CRLF+; // teste 11 item 25 roteiro tecban

*                   "032-927 = teste"+CRLF+;    // Homologação

        IF .NOT. Empty( v_cTxt := EnvCmdTecBan(v_cTxt)  )

           //SE TRANSAÇÃO APROVADA
           IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
              v_nLin   := Val( ExtMsgTecBan(v_cTxt,"028-000") )
              v_cBuf := ""
              FOR v_n = 1 TO v_nLin
                  v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                  sysrefresh()
              NEXT

              if nPgTEF > 1 .and. nCtTEF < nPgTEF// verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                 nFileHandle := FCreate( cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cdirtecban+"\RESP\IntPos.001",cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                 if GrvCmdTecBan(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f., nCtTEF, nPgTEF)
                    DelMsgTef(cdirtecban)
                 endif

                 albx_formas[iformas, 14] := cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt"
                 albx_formas[iformas, 15] := cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

              elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                 nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                 albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"

              else
                 nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                 albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"
              endif

              v_nRet := 1
           ELSE
              DelMsgTecBan( cdirtecban )
              v_nRet := -21
           ENDIF
        ENDIF

         DelReqTecBan( cdirtecban )

     //FAZ VENDA COM CHEQUE
     ELSEIF v_cTipo = "CHQ"

           nVezes := 1

           do while !ver_ativo(cdirtecban)

              if !_tefdedic
                 if ltravatef
                    liberatc()
                 endif
              endif

              if _tefdedic
                 erro("Sem conexão SiTef","Atenção")
                 nVezes++
                 if nVezes >= 3
                    exit
                 endif
              else
                 erro("Gerenciador TECBAN não ativo!","Atenção")
                 nVezes++
                 if nVezes >= 3
                    exit
                 endif
              endif

              if !_tefdedic
                 if ltravatef
                    liberatc()
                 endif
              endif

              sysrefresh()

           enddo

           DelMsgTecBan( cdirtecban )
           v_cCupom = vdocum
           if val(StrTran(v_cPago,",")) > 999
              teste_val = StrTran(v_cPago,",")
           else
              teste_val = strzero(val(StrTran(v_cPago,",")),3)
           endif

            v_cTxt := "000-000 = CHQ"+CRLF+;
                      "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                      "002-000 = "+v_cCupom+CRLF+;
                      "003-000 = "+AllTrim(teste_val)+CRLF+;
                      "999-999 = 0"+CRLF

*                      "032-927 = teste"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTecBan(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA
               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0" .OR. Substr(v_cTxt,At("009-000",v_cTxt)+10,2) = 'P1'

                  if Substr(v_cTxt,At("028-000",v_cTxt)+10,1) # "0"
                     v_nLin   := Val( ExtMsgTecBan(v_cTxt,"028-000") )
                     v_cBuf := ""
                     FOR v_n = 1 TO v_nLin
                         v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                         sysrefresh()
                     NEXT

                     if nPgTEF > 1 .and. nCtTEF < nPgTEF// verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                        nFileHandle := FCreate( cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cdirtecban+"\RESP\IntPos.001",cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                        if fazTecBan("CNF",left(albx_formas[nCtTEF, 1],16),v_cPago)
                           DelMsgTef(cdirtecban)
                        endif

                        albx_formas[iformas, 14] := cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt"
                        albx_formas[iformas, 15] := cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

                     elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                        nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                        albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"

                     else
                        nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                        albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"
                     endif

                  endif

                  v_nRet := 1
               ELSE
                  DelMsgTecBan( cdirtecban )
                  v_nRet := -21
               ENDIF
            ENDIF
            DelReqTecBan( cdirtecban )

     //FAZ CONFIRMAÇÃO DE VENDA EM CARTÃO
     ELSEIF v_cTipo = "CNF"

            nVezes := 1

            do while !ver_ativo(cdirtecban)
               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif
               sysrefresh()
            enddo

//            v_cCupom = vdocum
            v_cTxt   := MemoRead( cdirtecban+"\RESP\IntPos.001" )
            v_cCupom := ExtMsgTecBan(v_cTxt,"002-000")
            v_cNSU   := ExtMsgTecBan(v_cTxt,"012-000")
            v_cRede  := ExtMsgTecBan(v_cTxt,"010-000")
            v_cFinal := ExtMsgTecBan(v_cTxt,"027-000")
            v_cTxt   := "000-000 = CNF"+CRLF+;
                        "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                        "002-000 = "+v_cCupom+CRLF+;
                        "010-000 = "+v_cRede+ CRLF+;
                        "012-000 = "+v_cNSU +CRLF +;
                        "027-000 = "+v_cFinal+CRLF+;
                        "999-999 = 0"+CRLF

            IF .NOT. Empty(EnvCmdTecBan( v_cTxt ))
               v_nRet := 1
               DelMsgTecBan(cdirtecban)
            ENDIF
            DelReqTecBan( cdirtecban )

            if !_tefdedic
               liberatc()
            endif

     //CANCELA CUPOM TecBan NÃO IMPRESSO
     ELSEIF v_cTipo = "NCN"

            if !_tefdedic
               travatc()
               ltravatef := .t.
            endif

            nVezes := 1

            do while !ver_ativo(cdirtecban)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            v_cTxt   := MemoRead( cdirtecban+"\RESP\IntPos.001" )
            v_cNSU   := ExtMsgTecBan(v_cTxt,"012-000")
            v_cRede  := ExtMsgTecBan(v_cTxt,"010-000")
            v_cFinal := ExtMsgTecBan(v_cTxt,"027-000")
            v_cValor := ExtMsgTecBan(v_cTxt,"003-000")
            IF v_cTecBan = NIL
               v_cTecBan := ExtMsgTecBan(v_cTxt,"001-000")
            ENDIF
            v_cTxt   := "000-000 = NCN"+CRLF+;
                        "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                        "010-000 = "+v_cRede+ CRLF+;
                        "012-000 = "+v_cNSU +CRLF +;
                        "027-000 = "+v_cFinal+CRLF+;
                        "999-999 = 0"+CRLF

            if !_tefdedic
               liberatc()
            endif

            DEFINE DIALOG odlg_ncn RESOURCE "TefNCN" FONT fnt_ecf COLOR RGB(182,221,199), _CORPADRAO

                  REDEFINE SAY ID 30 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
                  REDEFINE SAY ID 31 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
                  REDEFINE SAY ID 32 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
                  REDEFINE SAY osayTxtVal ID 33 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE

                  REDEFINE SAY oSay[1] ID 301 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
                  REDEFINE SAY oSay[2] ID 302 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
                  REDEFINE SAY oSay[3] ID 303 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE

                  if upper(left(alltrim(v_cNsu),3)) <> 'ADM'
		     oSay[1]:SetText(v_cNSU)
		  endif

		  oSay[2]:SetText(v_cRede)
		  IF val(v_cvalor) <> 0
		     oSay[3]:SetText(Trans(Val(v_cValor)/100,"@E 999,999.99"))
                  else
                     osayTxtVal:SetText("")
                     osayTxtVal:refresh(.t.)
                     odlg_ncn:refresh(.t.)
		  ENDIF

                  REDEFINE BUTTON ID 101 of odlg_ncn action odlg_ncn:end() COLOR cor_btn CANCEL

                  odlg_ncn:lhelpicon:=.f.

            ACTIVATE DIALOG odlg_ncn CENTERED

            IF .NOT. Empty(EnvCmdTecBan( v_cTxt ))
               DelMsgTecBan(cdirtecban)
            ENDIF
            DelReqTecBan( cdirtecban )

     //FAZ ADM
     ELSEIF v_cTipo = "ADM"

            nVezes := 1

            do while !ver_ativo(cdirtecban)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            v_cTxt := "000-000 = ADM"+CRLF+;
                      "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                      "999-999 = 0"+CRLF

            IF .NOT. Empty( v_cTxt:=EnvCmdTecBan( v_cTxt ) )
               v_nLin := Val( ExtMsgTecBan(v_cTxt,"028-000") )
               IF v_nLin > 0
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

                  if !_tefdedic
                     travatc()
                  endif

                  IF PrintTecBan( .T. ) = 1
                     if !_tefdedic
                        liberatc()
                     endif
                     fazTecBan("CNF",v_cTecBan,v_cPago)
                  ELSE
                     fazTecBan("NCN",v_cTecBan )
                  ENDIF
                  if !_tefdedic
                     liberatc()
                  endif
               ENDIF
               v_nRet := 1
               DelMsgTecBan( cdirtecban )
            ENDIF
            DelReqTecBan( cdirtecban )

     ELSEIF v_cTipo = "CNC"

            nVezes := 1

            do while !ver_ativo(cdirtecban)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            DelMsgTecBan( cdirtecban )

            v_cTXT := MemoRead( CintPos )
            v_cCupom   := ExtMsgTecBan(v_cTxt,"002-000")
            v_valort   := ExtMsgTecBan(v_cTxt,"003-000")
            v_cRede  := ExtMsgTecBan(v_cTxt,"010-000")
            v_cNSU   := ExtMsgTecBan(v_cTxt,"012-000")
            v_dtcmv  := ExtMsgTecBan(v_cTxt,"022-000")
            v_hrcmv  := ExtMsgTecBan(v_cTxt,"023-000")
//            v_cFinal := ExtMsgTef(v_cTxt,"027-000")

            v_cTxt   := "000-000 = CNC"+CRLF+;
                        "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                        "002-000 = "+v_cCupom+CRLF+;
                        "003-000 = "+AllTrim(v_valort)+CRLF+;
                        "010-000 = "+v_cRede+ CRLF+;
                        "012-000 = "+v_cNSU +CRLF +;
                        "022-000 = "+v_dtcmv+CRLF+;
                        "023-000 = "+v_hrcmv+CRLF+;
                        "999-999 = 0"+CRLF

//                        "027-000 = "+v_cFinal+CRLF+;

                     //"777-777 = teste Redecard"+CRLF+;    // Homologação   // se nao for necessario pra CNC retirar essas 2 linhas
                     //"777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTecBan(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA

               nResp := .T.

               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
                  v_nLin   := Val( ExtMsgTecBan(v_cTxt,"028-000") )
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

               ELSE
                  DelMsgTecBan( cdirtecban )
                  Ferase(cdirtecban+"\IntPos.res")
                  v_nRet := -21
                  nResp := .f.
               ENDIF

            ELSE
               if !_tefdedic
                  liberatc()
               endif
               DelMsgTecBan( cdirtecban )
               Ferase(cdirtecban+"\IntPos.res")
               v_nRet := -21
               nResp := .F.
            ENDIF

     ENDIF

  END SEQUENC

RETURN v_nRet

FUNCTION ExtMsgTecBan( cTxt,cCod )
LOCAL cLin

   if At(cCod,cTxt) == 0
      cLin := ""
   else
      cLin := Substr(cTxt,At(cCod,cTxt)+10)
      cLin := Left(cLin,At(CHR(13)+CHR(10),cLin)-1)
   endif

RETURN cLin


FUNCTION EnvCmdTecBan( cTxt )

LOCAL nQte:= 0,cRet,nLin, nFileHandle

   Ferase(cdirtecban+"\RESP\IntPos.Sts")
   nFileHandle := FCreate( cdirtecban+"\REQ\IntPos.tmp", 0 )
   FWrite( nFileHandle, cTxt, Len(cTxt) )
   FClose( nFileHandle )
   Frename(cdirtecban+"\REQ\IntPos.tmp",cdirtecban+"\REQ\IntPos.001")

   DO WHILE nQte < 4
      SysWait(2)
      IF File(cdirtecban+"\RESP\IntPos.Sts")
         EXIT
      ENDIF
      nQte++
      sysrefresh()
   ENDDO

   IF nQte >= 4

      if !_tefdedic
         if ltravatef
            liberatc()
         endif
      endif

      if _tefdedic
         erro("Sem conexão SiTef","Atenção")
      else
         erro("Gerenciador TECBAN não ativo!","Atenção")
      endif

      if !_tefdedic
         if ltravatef
            travatc()
         endif
      endif

      DelMsgTecBan( cdirtecban )

      RETURN ""
   ENDIF

   nQte := 0
   DO WHILE nQte < 300 .AND. .NOT. File(cdirtecban+"\RESP\IntPos.001")
      nQte++
      SysWait(2)
      sysrefresh()
   ENDDO

   IF nQte < 300
*      ok('Aguardando teste RedeCard...')  // Homologação
      cTxt := MemoRead( cdirtecban+"\RESP\IntPos.001" )
      cRet := ExtMsgTecBan(cTxt,"030-000")
      nLin := Val( ExtMsgTecBan(cTxt,"028-000") )

      IF nLin = 0 .AND. .NOT. Empty(cRet)

         if type("Wvar") == "U"

            if valtype(ownd_tef) == 'O'
               ownd_tef:refresh(.t.)
               ownd_tef:setfocus()
            endif
            ok(cRet,"Atenção")

            DelMsgTecBan( cdirtecban )

            Wvar := .t.
         endif

      ENDIF
   ELSE
      cTxt := ""
      erro("Gerenciador TECBAN não Responde !","Atenção")
      DelMsgTecBan( cdirtecban )
   ENDIF

RETURN cTxt


FUNCTION DelMsgTecBan( cDir )

   Ferase(cDir+"\RESP\IntPos.001")
   Ferase(cDir+"\RESP\IntPos.sts")
   Ferase(cDir+"\Imprime.txt")

RETURN NIL

FUNCTION DelReqTecBan( cDir )

   Ferase(cDir+"\REQ\IntPos.001")
   Ferase(cDir+"\REQ\IntPos.sts")

RETURN NIL



*****************************************************************************
* PROGRAMA: PrintTecBan.PRG                                                 *
* FUNCAO..: Imprime cupom TecBan.                                              *
*****************************************************************************
FUNCTION PrintTecBan( lErro, cForma, cValor)
LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg,cRet,nSecs:=Seconds(), var_imprimir

   IF File(cdirtecban+"\imprime.txt")

      CursorWait()

      oWnd_tef:SetFocus()
      oWnd_tef:Show()
      aRet := TecBan_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cdirtecban+"\RESP\IntPos.001" )
      IF .NOT. Empty( cMsg := ExtMsgTecBan(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cdirtecban+"\imprime.txt")

      //SE FOR IMPRESSÃO APÓS ERRO
      IF lErro

         lImpCCD := .f.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64

                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                     RETURN (PrintTecBan( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrintTecBan( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            else

               if _daruma .or. _sweda .or. _elgin // incluido em 05/02/2011

                  lAlgoAberto := .f.

                  if _daruma
                     cEstado := space(2)
                     if GrvCmdFis(41,,,@cEstado,,.f.)
                        cEstado := substr(cEstado, 1,1)
                        if cEstado # '0'
                           lAlgoAberto := .t.
                        endif
                     endif
                  else
                     if GrvCmdFis(41,,,,,.f.)
                        lAlgoAberto := .t.
                     endif
                  endif

                  if lAlgoAberto

                     if v_framework

                        cGerAbre := space(2)
                        nRet := DaMFDInf('56', @cGerAbre)
                        cGerAbre := alltrim(cGerAbre)

                        if cGerAbre == '4'

                           nQualComando := 25

                        else

                           nQualComando := 23

                        endif

                     else

                        nQualComando := 25

                     endif

                     IF GrvCmdFis(nQualComando,,,,,.F.)

                        sysrefresh()
                        syswait(1)

                        if !VFimPapel()
                           IF ErroPrinter(aRet)
                            RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              RETURN -22
                           ENDIF
                        endif

                        IF Empty(cMsg)
                           aRet[3]:SetText("Imprimindo Leitura X...")
                        ELSE
                           aRet[3]:SetText(cMsg)
                        ENDIF

                     ELSEIF ErroPrinter(aRet)
                        RETURN (PrintTecBan( .T., cForma, cValor ))
                     ELSE
                         RETURN -22
                     ENDIF
                  endif

               else

                  IF GrvCmdFis(25,,,,,.t.)

                     sysrefresh()
                     syswait(1)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                         RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                     RETURN (PrintTecBan( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            FOR n = 1 TO nviastef

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64
                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrintTecBan( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF
                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'

                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrintTecBan( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _daruma

                   if v_framework
                      daProgFormas("RG", 'TEF1', ';')
                      DaAbreGerMFD('TEF1')
                   endif

                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.f.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _elgin

                     lAlgoAberto := .f.
                     nStGerCDC := 0
                     E_StatExtMFD(@nStGerCDC)

                     if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                        lAlgoAberto := .t.
                     elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                        lAlgoAberto := .t.
                     endif

                     if !lAlgoAberto

                        cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                        cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                        E_AbreGerMFD(cIndGer)

                     endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10)
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)
                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF
                    if _urano
                       IF .NOT. GrvCmdFis(22, var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF
                    elseif _epson

                       IF !GrvCmdFis(22,var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else
                       if !GrvCmdFis(24, var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             RETURN -22
                          ENDIF
                       elseif !VFimPapel()
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             RETURN -22
                          ENDIF
                       ENDIF
                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. nviastef > 1
                   FOR k = 1 TO 10
                       if _urano
                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrintTecBan( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       elseif _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrintTecBan( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else
                          if !GrvCmdFis(24,Space(10),,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                RETURN -22
                             ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()   // Alteracao Alex
                      SysWait(3)
                      aRet[3]:SetText(cMsg)
                      aRet[1]:SetFocus()   // Alteracao Alex
                   endif

                   sysrefresh()

               ENDIF

               sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma

      ELSE

         IF .NOT. GrvCmdFis(if(_daruma, 78, 21),{cForma,cValor,NIL},,,,.F.)
            IF ErroPrinter(aRet)
               RETURN (PrintTecBan( .T., cForma, cValor ))
            ELSE
               RETURN -22
            ENDIF
         elseif !VFimPapel()

             IF ErroPrinter(aRet)
               RETURN (PrintTecBan( .T., cForma, cValor ))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         FOR n = 1 TO nviasTef

             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()

             FOR k = 1 TO MlCount(cTxt)

                 if _urano .or. _epson
                    IF !GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF,,,,.F. )
                       IF ErroPrinter(aRet)
                          RETURN (PrintTecBan( .T., cForma, cValor ))
                       ELSE
                          GrvCmdFis(25,,,,,.F.)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                        IF ErroPrinter(aRet)
                           RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           GrvCmdFis(25,,,,,.F.)
                           RETURN -22
                        ENDIF

                    ENDIF
                 else
                    IF !GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF,,,,.F. )
                       IF ErroPrinter(aRet)
                          RETURN (PrintTecBan( .T., cForma, cValor ))
                       ELSE
                          RETURN -22
                       ENDIF
                    elseif !VFimPapel()
                        IF ErroPrinter(aRet)
                          RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                    ENDIF
                 endif

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviasTef > 1
                FOR k = 1 TO 10
                    if _urano .or. _epson
                       if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF
                       elseif !VFimPapel()
                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF
                       endif
                    else
                       if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF
                       elseif !VFimPapel()
                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF
                       endif

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[3]:SetText(cMsg)
                aRet[1]:SetFocus()   // Alteracao Alex

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
                IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
                ELSE
                   RETURN -22
                ENDIF
            endif

         else
            if !GrvCmdFis(88)
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif

      ENDIF

      aRet[1]:End() ; aRet[1] := NIL

   ELSE
      if !_tefdedic
         liberatc()
      endif
      ok("Não há cupons para imprimir !","Atenção")
   ENDIF

RETURN If(lRet,1,0)



STATIC FUNCTION ErroPrinter(aRet)
  local retorno

  aRet[1]:End() ; aRet[1] := NIL
  oWnd_tef:SetFocus()
  oWnd_tef:Show()

  if !_tefdedic
     liberatc()
  endif

  retorno = Continua("Impressora não Responde. Tentar imprimir novamente?","Confirmação")

  if !_tefdedic
     travatc()
  endif

  if retorno
     syswait(5)
  endif

RETURN retorno



STATIC FUNCTION TecBan_aguarde( cIcone,cMens )
LOCAL oDlgWait,oMtr,nPerc,oSay,oIco

   CursorWait()
   cIcone := If(cIcone=NIL,"I_AGUARDE",cIcone)

   DEFINE DIALOG oDlgWait RESOURCE "DLG_AGUARDE" FONT fnt_ecf COLOR cor_dlg

	REDEFINE ICON oIco NAME (cIcone)        ID 101 OF oDlgWait UPDATE
    REDEFINE SAY oSay                       ID 301 OF oDlgWait UPDATE
    REDEFINE METER oMtr VAR nPerc TOTAL 100 ID 901 OF oDlgWait UPDATE

        IF cMens # NIL
           oSay:SetText(cMens)
        ENDIF

    odlgWait:lhelpicon:=.f.
    ACTIVATE DIALOG oDlgWait CENTERED NOWAIT

RETURN {oDlgWait,oMtr,oSay}



*****************************************************************************
* PROGRAMA: GrvCmdTecBan.PRG                                                   *
* FUNCAO..: Envia comando para TecBan.                                         *
*                                                                           *
*****************************************************************************
FUNCTION GrvCmdTecBan(nCmd,uPar,lMens,lInic, nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

LOCAL nRet:=0,oIni,nTecBan:=0,cTecBan, kl, xki, aSoTEF, nQtVinc // aImprime,

default cCodForma := ''

  lMens := If(lMens=NIL,.T.,lMens)
  lInic := If(lInic=NIL,.T.,lInic)

  cTecBan := StrZero(val(vl_venda),10)

  //VER TEF ATIVO
  IF nCmd = 1
         nRet := BmTfStatus(cTecBan)

  //ACIONA TecBan ADM
  ELSEIF nCmd = 2
         nRet := fazTecBan("ADM",cTecBan,uPar )

  //VENDA CARTÃO
//  ELSEIF nCmd = 3
//         nRet := fazTecBan("CRT",cTecBan,uPar )

  ELSEIF nCmd = 3
         nRet := fazTecBan("CRT",cTecBan,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA DE CARTÃO
  ELSEIF nCmd = 4

         if !_tefdedic
            travatc()
            ltravatef := .t.
         endif

         nRet := PrintTecBan( .F.,uPar[1], uPar[2] )

         if !_tefdedic
            liberatc()
            ltravatef := .f.
         endif

  //CONFIRMA IMPRESSÃO DE TecBan
  ELSEIF nCmd = 5
         nRet := fazTecBan("CNF",cTecBan,uPar[1], nCtTEF, nPgTEF )

  //VENDA CHEQUE
  ELSEIF nCmd = 6
         nRet := fazTecBan("CHQ",cTecBan,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA COM MULTIPLOS CARTOES
  ELSEIF nCmd = 7

         if 'INTPOS.001'$upper(cArq001)
            COPYFILE(cdirtecban+"\IntPos.001",cdirtecban+"\RESP\IntPos.001", .f.)
            ferase(cdirtecban+"\IntPos.001")
            cArq001 := cdirtecban+"\RESP\IntPos.001"
         endif

         nRet := PrtCupTefB( .F., albx_formas[nCtTEF, 4], albx_formas[nCtTEF, 2], nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

         if nRet == -44
            nRet := 1
         endif

  ENDIF

  oWnd_tef:Show()

  IF lMens

     IF nRet = 0
            erro("Gerenciador TECBAN Inativo !","Atenção")

     ELSEIF nRet = -2
            erro("Parâmetro inválido para comando "+Str(nCmd,2)+" !","Atenção")

     ELSEIF nRet = -18
            erro("Não foi possível abrir/criar arquivo .001 ou .sts !","Atenção")

     ELSEIF nRet = -19
            erro("Numero de Identificação não bate com o do arquivo .001 !","Atenção")

     ELSEIF nRet = -20
            erro("Transação finalizada pelo operador !","Atenção")

     ELSEIF nRet = -21
            erro("A transação não foi aprovada !","Atenção")

     ELSEIF nRet = -22
            erro("Não foi possível terminar a impressão !","Atenção")

     ELSEIF nRet = -23
            erro("Não foi possível terminar a operação !","Atenção")

     ELSE
            RETURN .T.
     ENDIF

  ELSEIF nRet = 1
     RETURN .T.
  ENDIF

RETURN .F.



*****************************************************************************
* PROGRAMA: PrtCupTefB.PRG                                                 *
* FUNCAO..: Imprime cupom TEF.                                              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
* ALTERADO: Sebastiao de Oliveira Pessoa                     EM: 09-20-2010 *
*****************************************************************************
FUNCTION PrtCupTefB( lErro, cForma, cValor, nki, nPgTEF, cNumArq, cArq001, cCodForma)

LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg := "",cRet,nSecs:=Seconds(), var_imprimir, aImprime, aImprime1, aImprime2

private nViasMC, nTefGP, nTefHp, nTefTc

   IF File(cNumArq)

      CursorWait()

      ownd_tef:SetFocus()
      ownd_tef:Show()

      aRet := TecBan_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cArq001)

      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cNumArq)

      //SE FOR IMPRESSÃO APÓS ERRO
      IF lErro

         lGerImp := .t.
         lerroImp := .t.

         cTxt := ""

         nTefGP  := Directory(_cdirtef+"\imprime*.txt" )
         nTefHp  := Directory(cdirhiper+"\imprime*.txt" )
         nTefTc  := Directory(cdirtecban+"\imprime*.txt" )

         nViasMC := 0
         nViasMC := len(nTefGP) + len(nTefHp) + len(nTefTc)

         LeAposerro(@aImprime, @aImprime1, @aImprime2, @cTxt)

         lImpCCD := .f.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64
                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF )) // (PrtCupTefB( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            else

               if _daruma .or. _sweda .or. _elgin // incluido em 05/02/2011

                  lAlgoAberto := .f.

                  if _daruma
                     cEstado := space(2)
                     if GrvCmdFis(41,,,@cEstado,,.f.)
                        cEstado := substr(cEstado, 1,1)
                        if cEstado # '0'
                           lAlgoAberto := .t.
                        endif
                     endif
                  else
                     if GrvCmdFis(41,,,,,.f.)
                        lAlgoAberto := .t.
                     endif
                  endif

                  if lAlgoAberto
                     if v_framework

                        cGerAbre := space(2)
                        nRet := DaMFDInf('56', @cGerAbre)
                        cGerAbre := alltrim(cGerAbre)

                        if cGerAbre == '4'

                           nQualComando := 25

                        else

                           nQualComando := 23

                        endif

                     else

                        nQualComando := 25

                     endif

                     IF GrvCmdFis(nQualComando,,,,,.F.)

                        if !VFimPapel()
                           IF ErroPrinter(aRet)
                              RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                           ELSE
                              RETURN -22
                           ENDIF
                        endif

                        IF Empty(cMsg)
                           aRet[3]:SetText("Imprimindo Leitura X...")
                        ELSE
                           aRet[3]:SetText(cMsg)
                        ENDIF

                     ELSEIF ErroPrinter(aRet)
                         RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                     ELSE
                         RETURN -22
                     ENDIF
                  endif

               else

                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            aRet[3]:SetText(cMsg)

            FOR n := 1 TO if(nViasMC == 1, nviastef, 1)

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64

                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'

                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _daruma

                   if v_framework
                      daProgFormas("RG", 'TEF1', ';')
                      DaAbreGerMFD('TEF')
                   endif

                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.f.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _elgin

                   lAlgoAberto := .f.
                   nStGerCDC := 0
                   E_StatExtMFD(@nStGerCDC)

                   if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                      lAlgoAberto := .t.
                   elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                      lAlgoAberto := .t.
                   endif

                   if !lAlgoAberto

                      cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                      E_AbreGerMFD(cIndGer)

                   endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10) // reincluir
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)

                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF

                    if _urano .or. _epson
                       IF !GrvCmdFis(22,var_imprimir)

                          if _urano

                             nEstado := 0
                             GrvCmdFis(78,,,@nEstado,,.F.)

                             if nEstado == 32 .or. nEstado == 64

                                IF ErroPrinter(aRet)
                                   RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             else

                                IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                   IF ErroPrinter(aRet)
                                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF
                                ENDIF

                             endif

                          else

                             IF ErroPrinter(aRet)
                                RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF

                          endif

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else

                       IF .NOT. GrvCmdFis(24,var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. if(nViasMC == 1, nviastef, 1) > 1
//                IF n = 1 .and. if(len(aImprime) == 1, nviastef, 1) > 1

                   FOR k = 1 TO 10

                       if _urano .or. _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )

                             if _urano

                                nEstado := 0
                                GrvCmdFis(78,,,@nEstado,,.F.)

                                if nEstado == 32 .or. nEstado == 64

                                   IF ErroPrinter(aRet)
                                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF

                                else

                                   IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                      IF ErroPrinter(aRet)
                                         RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                      ELSE
                                         GrvCmdFis(25,,,,,.F.)
                                         RETURN -22
                                      ENDIF
                                   ENDIF

                                endif

                             else

                                IF ErroPrinter(aRet)
                                   RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             endif

                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else
                          if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()
                      SysWait(3)
                      aRet[3]:SetText(cMsg)
                      aRet[1]:SetFocus()   // Alteracao Alex
                   endif

                ENDIF

                sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma / Elgin / Sweda / Urano

         aRet[1]:End() ; aRet[1] := NIL

         RETURN -44

      ELSE

         IF .NOT. GrvCmdFis(if(_daruma, 78, 21),{cForma,cValor,NIL},,,,.F.)

            IF ErroPrinter(aRet)
               RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
            ELSE
               RETURN -22
            ENDIF

         elseif !VFimPapel()

             IF ErroPrinter(aRet)
                RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         v_cTpTran := ExtMsgTef(cRet,"011-000")
         v_cRedeTr  := ExtMsgTef(cRet,"010-000")

         if _bematech
            if W_MFD == 1 .or. cModeloBm == 'MP25FI' .or. cModeloBm == 'MP50FI'
               cCooCDC := space(6)
               GrvCmdFis(14,, 6, @cCooCDC)
               aadd(aCOOCDC, { cCooCDC, cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr })
            endif
         else
            cCooCDC := space(6)
            GrvCmdFis(14,, 6, @cCooCDC)
            aadd(aCOOCDC, { cCooCDC, cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr })
         endif

         FOR n = 1 TO nviastef
             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()
             FOR k = 1 TO MlCount(cTxt)

                 IF .NOT. GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k), CRLF),48)+CRLF,,,,.F. )
                    IF ErroPrinter(aRet)
                       RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 elseif !VFimPapel()
                    IF ErroPrinter(aRet)
                       RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 ENDIF

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviastef > 1
                FOR k = 1 TO 10

                    if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                       IF ErroPrinter(aRet)
                          RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                       IF ErroPrinter(aRet)
                          RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[3]:SetText(cMsg)
                aRet[1]:SetFocus()   // Alteracao Alex

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         else
            if !GrvCmdFis(88)
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif
      ENDIF

      aRet[1]:End() ; aRet[1] := NIL

   ELSE
      if !_tefdedic
         liberatc()
      endif
      ok("Não há cupoms para imprimir !","Atenção")
   ENDIF

RETURN(If(lRet,1,0))

 

Link to comment
Share on other sites

Em 30/04/2022 at 13:38, Wellington Vieira disse:

Bom dia,


Segue um prg, que usado com o SITEF  da SOFTWARE EXPRESS. (Terá que fazer o contrato com eles, para usar o TEF dedicado - Transferência Eletrônica de Fundos, consumindo a dll deles ou a comunicação via "TXT"), ou o Gerenciador padrão, neste caso tem que ser homologado.

PDV -> PINPAD -> IMPRESSORA NÃO FISCAL ( para NFC-e ou SAT-ECF)

OU DESENVOLVER consumindo as dll ou api's das operadoras de cartão, que neste caso precisam ser homologados.

TEF.prg


#include "FiveWin.ch"
#include "Directry.ch"

*****************************************************************************
* PROGRAMA..: FAZTEF.PRG                                                    *
* FUNCAO....: Ativa gerenciador padrao de TEF.                              *
*                                                                           *
* PARÂMETROS: V_CTIPO -> Tipo da operação a ser executada;                  *
*             V_CTEF  -> Número de controle de operação TEF;                *
*             V_CPAGO -> Valor da operação passado como texto.              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                   EM: 14/05/02 *
*****************************************************************************
FUNCTION fazTef(v_cTipo,v_cTef,v_cPago, nCtTEF, nPgTEF, CintPos, lhiper, nMosMSG, lconf_pre)

LOCAL v_cBuf,v_cTxt,v_cCupom:=Space(6),v_n,v_nLin,v_nRet := 0, aRet,v_cRede,;
      v_cFinal,v_cNSU,oDlgTef,oSay[3],v_cValor, teste_val, osayTxtVal, osayTxtDoc, ;
      nValorSaque, nOrdCli

default lhiper := .f., nMosMSG := .t., v_cPago := '1,00', nCtTEF := 1, nPgTEF := 1, lconf_pre := .f.

//private ltravatef := .f.

ltravatef := .f.

  BEGIN  SEQUENC

     CursorWait()

     //FAZ VENDA EM CARTÃO
     IF v_cTipo = "CRT"

        nVezes := 1

        do while !ver_ativo(cdirtef)

           if !_tefdedic
              if ltravatef
                 liberatc()
              endif
           endif

           if !v_superM
              obtn_finaliza:disable()
              obtn_finaliza:refresh(.t.)
           endif
           if _tefdedic
              erro("Sem conexão SiTef","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           else
              erro("Gerenciador TEF não ativo!","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           endif
           if !v_superM
              obtn_finaliza:enable()
              obtn_finaliza:refresh(.t.)
           endif

           if !_tefdedic
              if ltravatef
                 travatc()
              endif
           endif

           sysrefresh()
        enddo

        DelMsgTef( cDirTef )
        v_cCupom = vdocum
        if val(StrTran(v_cPago,",")) > 999
           teste_val = StrTran(v_cPago,",")
        else
           teste_val = strzero(val(StrTran(v_cPago,",")),3)
        endif

        if !_tefdedic
           v_cTxt := "000-000 = CRT"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "004-000 = 0"+CRLF+;
                     "701-000 = SALC"+_versao+CRLF+;
                     "706-000 = 3"+CRLF+;
                     "716-000 = "+_soft+CRLF+;
                     "999-999 = 0"+CRLF
        else
           v_cTxt := "000-000 = CRT"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "210-084 = SALC"+_versao+"10"+CRLF+;
                     "701-034 = 4"+CRLF+;
                     "999-999 = 0"+CRLF
        endif

//                  "001-000 = "+v_cTef+CRLF+;
                  // "777-777 = teste Redecard"+CRLF+;    // Homologação
                  // "777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

        IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

           //SE TRANSAÇÃO APROVADA
           IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
              v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
              v_cBuf := ""
              FOR v_n = 1 TO v_nLin
                  v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                  sysrefresh()
              NEXT

              c_cnpj := ExtMsgTef(v_cTxt,"600-000")
              c_codban := ExtMsgTef(v_cTxt,"601-000")
              c_codaut := ExtMsgTef(v_cTxt,"602-000")

              if empty(c_cnpj) .and. empty(c_codban) .and. empty(c_codaut)

                 c_cnpj := ExtMsgTef(v_cTxt,"007-000")
                 c_codban := ExtMsgTef(v_cTxt,"010-000")
                 c_codaut := ExtMsgTef(v_cTxt,"013-000")

                 if "VISA"#upper(c_codban)
                    c_codban := "01"
                 elseif "MASTER"#upper(c_codban)
                    c_codban := "02"
                 elseif "AMEX"#upper(c_codban)
                    c_codban := "03"
                 elseif "SORO"#upper(c_codban)
                    c_codban := "04"
                 else
                    c_codban := "99"
                 endif

              endif

              if empty(c_cnpj)

                 nOrdCli := acliente->(indexord())
                 acliente->(dbsetorder(1))

                 if acliente->(dbseek(alltrim(albx_formas[iformas,19])))

                    c_cnpj := alltrim(acliente->CNPJ)

                 else

                    if v_nfce
                       erro("CNPJ da CREDENCIADA TEF não cadastrado!", "ERRO")
                       erro("NFC-E não poderá ser enviada sem essa informação!", "ERRO")
                    endif

                 endif

                 acliente->(dbsetorder(nOrdCli))

              endif

              if empty(c_codban)

                 c_codban := ExtMsgTef(v_cTxt,"010-003")

              endif

              if empty(c_codaut)
                 c_codaut := ExtMsgTef(v_cTxt,"013-000")
              endif

              if v_nfce .and. !v_nfce_ecf

                 cValorTEF := ExtMsgTef(v_cTxt,"003-000")

                 cValorTEF := val(cValorTEF)

                 v_cTpTran := ExtMsgTef(v_cTxt,"011-000")
                 v_cRedeTr  := ExtMsgTef(v_cTxt,"010-000")

                 aadd(aCOOCDC, { strzero(val(vdocum), 9), ;
                      albx_formas[iformas, 4], cValorTEF, albx_formas[iformas, 16], ;
                      v_cTpTran, v_cRedeTr, vtot_ven })

              endif

              if nPgTEF > 1 .and. nCtTEF < nPgTEF // verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                 nFileHandle := FCreate( cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                 if !_tefdedic
                    if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                       DelMsgTef(cdirtef)
                    endif
                 endif

                 albx_formas[iformas, 14] := cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt"
                 albx_formas[iformas, 15] := cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

              elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                 /*
                 if _tefdedic
                    if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                       DelMsgTef(cdirtef)
                    endif
                 endif
                 */

                 albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                 if !_tefdedic
                    albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                 else
                    albx_formas[iformas, 15] := if(file(cDirTef+"\IntPos.tmp"), cDirTef+"\IntPos.tmp", cDirTef+"\IntPos.001")
                 endif

              else
                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)
                 albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                 albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
              endif

              v_nRet := 1

              if _tefdedic

                 nValorSaque := round(Val( strtran(ExtMsgTef(v_cTxt,"200-000"), ',', '.') ), 2)

                 if nValorSaque > 0 // round((val(StrTran(v_cPago,",")) / 100), 2)
                    vtot_tef := vtot_tef + nValorSaque
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] + nValorSaque
                 endif

                 nDescCielo := round(Val( strtran(ExtMsgTef(v_cTxt,"210-081"), ',', '.') ), 2)

                 if nDescCielo > 0
                    vdesconto := vdesconto + (albx_formas[iformas, 2] - (nDescCielo + nValorSaque))
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] - ((albx_formas[iformas, 2] - nDescCielo)) + nValorSaque
                 endif

              else

                 nValorSaque := round(Val( ExtMsgTef(v_cTxt,"708-000") ) / 100, 2)

                 if (round((val(StrTran(v_cPago,",")) / 100), 2) + nValorSaque) > round((val(StrTran(v_cPago,",")) / 100), 2)
                    vtot_tef := vtot_tef + nValorSaque
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] + nValorSaque
                 endif

                 nDescCielo := round(Val( strtran(ExtMsgTef(v_cTxt,"709-000"), ',', '.') ), 2) / 100

                 if nDescCielo > 0
                    vdesconto := vdesconto + nDescCielo
                    albx_formas[iformas, 2] := albx_formas[iformas, 2] - vdesconto
                    vtot_tef := vtot_tef - vdesconto
                 endif

              endif

           ELSE
              lTEFneg := .t.
              DelMsgTef( cDirTef )
              Ferase(cDirTef+"\IntPos.res")
              v_nRet := -21
           ENDIF
        ENDIF

     //FAZ VENDA COM CHEQUE - INCLUSO PARA REDECARD EM 22/03/06
     ELSEIF v_cTipo = "CHQ"

            nVezes := 1

            do while !ver_ativo(cdirtef)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if !v_superM
                  obtn_finaliza:disable()
                  obtn_finaliza:refresh(.t.)
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !v_superM
                  obtn_finaliza:enable()
                  obtn_finaliza:refresh(.t.)
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            DelMsgTef( cDirTef )
            v_cCupom = vdocum

            if val(StrTran(v_cPago,",")) > 999
               teste_val = StrTran(v_cPago,",")
            else
               teste_val = strzero(val(StrTran(v_cPago,",")),3)
            endif

            if !_tefdedic
               v_cTxt := "000-000 = CHQ"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "002-000 = "+v_cCupom+CRLF+;
                         "003-000 = "+AllTrim(teste_val)+CRLF+;
                         "004-000 = 0"+CRLF+;
                         "701-000 = SALC"+_versao+CRLF+;
                         "706-000 = 3"+CRLF+;
                         "716-000 = "+_soft+CRLF+;
                         "999-999 = 0"+CRLF
            else
               v_cTxt := "000-000 = CHQ"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "002-000 = "+v_cCupom+CRLF+;
                         "003-000 = "+AllTrim(teste_val)+CRLF+;
                         "999-999 = 0"+CRLF
            endif
//                         "001-000 = "+v_cTef+CRLF+;

*                         "032-927 = teste"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA
               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0" .OR. Substr(v_cTxt,At("009-000",v_cTxt)+10,2) = 'P1'
                  if Substr(v_cTxt,At("028-000",v_cTxt)+10,1) # "0"
                     v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
                     v_cBuf := ""
                     FOR v_n = 1 TO v_nLin
                         v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                         sysrefresh()
                     NEXT

                     if nPgTEF > 1 .and. nCtTEF < nPgTEF // verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                        nFileHandle := FCreate( cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                        if !_tefdedic
                           if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                              DelMsgTef(cdirtef)
                           endif
                        endif

                        albx_formas[iformas, 14] := cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt"
                        albx_formas[iformas, 15] := cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

                     elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                        nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                        if _tefdedic
                           if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                              DelMsgTef(cdirtef)
                           endif
                        endif

                        if !_tefdedic
                           albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                        else
                           albx_formas[iformas, 15] := if(file(cDirTef+"\IntPos.tmp"), cDirTef+"\IntPos.tmp", cDirTef+"\IntPos.001")
                        endif

                        albx_formas[iformas, 14] := cDirTef+"\imprime.txt"

                        //albx_formas[iformas, 15] := cDirTef+"\IntPos.001"

                     else
                        nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)
                        albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                        albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                     endif

                  endif

                  v_nRet := 1

               ELSE
                  DelMsgTef( cDirTef )
                  Ferase(cDirTef+"\IntPos.res")
                  v_nRet := -21
               ENDIF

            ENDIF

            if File(cDirTef+"\Intpos.res")
               Ferase(cDirTef+"\IntPos.res")
            endif

     //FAZ CONFIRMAÇÃO DE VENDA EM CARTÃO
     ELSEIF v_cTipo = "CNF"

            nVezes := 1

            do while !ver_ativo(cdirtef)
               //if ltravatef
               if !_tefdedic
                  liberatc()
               endif
               //endif
               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes > 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes > 3
                     exit
                  endif
               endif
               //if ltravatef
                 if !_tefdedic
                    travatc()
                 endif
               //endif
               sysrefresh()
            enddo

            v_cTxt   := MemoRead( if(!_tefdedic, cDirTef+"\RESP\IntPos.001",  CintPos))

            v_cCupom := ExtMsgTef(v_cTxt,"002-000")
            v_cNSU   := ExtMsgTef(v_cTxt,"012-000")
            v_cRede  := ExtMsgTef(v_cTxt,"010-000")
            v_cFinal := ExtMsgTef(v_cTxt,"027-000")

            if len(alltrim(v_cCupom)) == 0
               v_cTxt   := "000-000 = CNF"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            else
               v_cTxt   := "000-000 = CNF"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_cCupom+CRLF+;
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            endif

            if _tefdedic .and. nVezes > 3
               Ferase(cDirTef+"\RESP\IntPos.001")
            elseif !_tefdedic .and. nVezes > 3
               Ferase(cDirTef+"\RESP\IntPos.001")
            endif

            IF .NOT. Empty(EnvCmdTef( v_cTxt ))
               v_nRet := 1
               DelMsgTef(cDirTef)
            ENDIF

            if File(cDirTef+"\Intpos.res")
               Ferase(cDirTef+"\IntPos.res")
            endif

            if !_tefdedic
               liberatc()
            endif

     //CANCELA CUPOM TEF NÃO IMPRESSO
     ELSEIF v_cTipo = "NCN"

            if !_tefdedic
               travatc()
               ltravatef := .t.
            endif

            nVezes := 1

            do while !ver_ativo(cdirtef)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  if !lLoopNCN
                     nVezes++
                     if nVezes >= 3
                        exit
                     endif
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  if !lLoopNCN
                     nVezes++
                     if nVezes >= 3
                        exit
                     endif
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()
            enddo

            Ferase(cDirTef+"\Imprime.txt")

            v_cTxt   := MemoRead( if(_tefdedic, CintPos, cDirTef+"\RESP\IntPos.001") )

            v_cNSU   := ExtMsgTef(v_cTxt,"012-000")
            v_cRede  := ExtMsgTef(v_cTxt,"010-000")
            v_cFinal := ExtMsgTef(v_cTxt,"027-000")
            v_cValor := ExtMsgTef(v_cTxt,"003-000")

            IF v_cTef = NIL
               v_cTef := ExtMsgTef(v_cTxt,"001-000")
            ENDIF

            if !_tefdedic
               v_cTxt   := "000-000 = NCN"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            else
               v_CupVinc := ExtMsgTef(v_cTxt,"002-000")
               v_cTxt   := "000-000 = NCN"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_CupVinc+CRLF+;           // Cupom fiscal vinculado
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "027-000 = "+v_cFinal+CRLF+;
                           "999-999 = 0"+CRLF
            endif

            IF .NOT. Empty(EnvCmdTef( v_cTxt ))
               DelMsgTef(cDirTef)
               nResp := .t.
            else
               if _tefdedic
                  nResp := .F.
               endif
            ENDIF

            if !_tefdedic
               liberatc()
               ltravatef := .f.
            endif

            if !_tefdedic

               DEFINE DIALOG odlg_ncn RESOURCE "TEFNCN" FONT fnt_ecf COLOR RGB(182,221,199), _CORPADRAO

               REDEFINE SAY ID 30 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
               REDEFINE SAY osayTxtDoc ID 31 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
               REDEFINE SAY ID 32 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
               REDEFINE SAY osayTxtVal ID 33 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE

               REDEFINE SAY oSay[1] ID 301 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
               REDEFINE SAY oSay[2] ID 302 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
               REDEFINE SAY oSay[3] ID 303 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE

               if upper(left(alltrim(v_cNsu),3)) <> 'ADM'
                  oSay[1]:SetText(v_cNSU)
               endif

	       oSay[2]:SetText(v_cRede)

	       IF val(v_cNsu) <> 0
                  oSay[2]:SetText(v_cRede)
	       else
	          osayTxtDoc:SetText("")
	          osayTxtDoc:refresh(.t.)
	          odlg_ncn:refresh(.t.)
	       ENDIF

	       IF val(v_cvalor) <> 0
                  if _tefdedic
                     oSay[3]:SetText(Trans(Val(v_cValor),"@E 999,999.99"))
                  else
                     oSay[3]:SetText(Trans(Val(v_cValor)/100,"@E 999,999.99"))
                  endif
	       else
	          osayTxtVal:SetText("")
	          osayTxtVal:refresh(.t.)
	          odlg_ncn:refresh(.t.)
	       ENDIF

               REDEFINE BUTTON ID 101 of odlg_ncn action odlg_ncn:end() COLOR cor_btn CANCEL

               odlg_ncn:lhelpicon:=.f.

               ACTIVATE DIALOG odlg_ncn CENTERED

            else

               if nResp .and. nMosMSG
                  erro("Transação não foi Efetuada."+CRLF+"Favor reter o Cupom.","Atenção")
               endif

            endif

            if File(cDirTef+"\Intpos.res")
               Ferase(cDirTef+"\IntPos.res")
            endif

     //FAZ ADM
     ELSEIF v_cTipo = "ADM"

            nVezes := 1

            do while !ver_ativo(cdirtef)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()
            enddo

            if !_tefdedic
               v_cTxt := "000-000 = ADM"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "701-000 = SALC"+_versao+CRLF+;
                         "706-000 = 3"+CRLF+;
                         "716-000 = "+_soft+CRLF+;
                         "999-999 = 0"+CRLF
            else
               v_cTxt := "000-000 = ADM"+CRLF+;
                         "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                         "999-999 = 0"+CRLF
            endif

            IF .NOT. Empty( v_cTxt:=EnvCmdTef( v_cTxt ) )

               v_nLin := Val( ExtMsgTef(v_cTxt,"028-000") )
               v_Canc := Val( ExtMsgTef(v_cTxt,"025-000") )

               IF v_nLin > 0
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

                  if !_tefdedic
                     ltravatef := .t.
                     travatc()
                  endif

                  if _tefdedic

                     if !v_nfce

                        IF PrintCupTef( .T. ) = 1
                           fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                        ELSE
                           if v_Canc > 0
                              fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                              erro("Transação TEF confirmada."+CRLF+"Favor reimprimir último comprovante.","Atenção")
                           else
                              fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                           endif
                        ENDIF

                     else

                        cRet := MemoRead( cDirTef+"\imprime.txt")

                        nViasTEF := 2

                        for xTef := 1 to nViasTEF

                           oFrPrn := frReportManager():New()
                           oFrPrn:LoadFromFile(".\tef.fr3")

                           oFrPrn:AddVariable("Comprovante", "tef", "'"+cRet+"'")

                           oFrPrn:PrintOptions:SetShowDialog(.f.)
                           oFrPrn:PrepareReport()

                           oFrPrn:Print(.T.)

                           oFrPrn:ClearDataSets()
                           oFrPrn:DestroyFR()

                        next

                        fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")

                     endif

                  else

                     if !v_nfce

                        IF PrintCupTef( .T. ) = 1
                           liberatc()
                           fazTef("CNF",v_cTef,v_cPago)
                        ELSE
                           fazTef("NCN",v_cTef )
                        ENDIF

                     else

                        cRet := MemoRead( cDirTef+"\imprime.txt")

                        nViasTEF := 2

                        for xTef := 1 to nViasTEF

                           oFrPrn := frReportManager():New()
                           oFrPrn:LoadFromFile(".\tef.fr3")

                           oFrPrn:AddVariable("Comprovante", "tef", "'"+cRet+"'")

                           oFrPrn:PrintOptions:SetShowDialog(.f.)
                           oFrPrn:PrepareReport()

                           oFrPrn:Print(.T.)

                           oFrPrn:ClearDataSets()
                           oFrPrn:DestroyFR()

                        next

                        fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")

                     endif

                  endif

                  if !_tefdedic
                     liberatc()
                     ltravatef := .f.
                  endif

               ENDIF
               v_nRet := 1
               DelMsgTef( cDirTef )
               if File(cDirTef+"\Intpos.res")
                  Ferase(cDirTef+"\IntPos.res")
               endif
               Ferase(cDirTef+"\Imprime.txt")

            ENDIF

     ELSEIF v_cTipo = "CNC"

            if lhiper
               cdirant := cdirtef
               cdirtef := cdirhiper
            endif

            nVezes := 1

            do while !ver_ativo(cdirtef)
               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TEF não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()
            enddo

            DelMsgTef( cdirtef )

            v_cTXT := MemoRead( CintPos )
            v_cCupom   := ExtMsgTef(v_cTxt,"002-000")
            v_valort   := ExtMsgTef(v_cTxt,"003-000")
            v_cRede  := ExtMsgTef(v_cTxt,"010-000")
            v_cNSU   := ExtMsgTef(v_cTxt,"012-000")
            v_dtcmv  := ExtMsgTef(v_cTxt,"022-000")
            v_hrcmv  := ExtMsgTef(v_cTxt,"023-000")
//            v_cFinal := ExtMsgTef(v_cTxt,"027-000")

            if !_tefdedic
               v_cTxt   := "000-000 = CNC"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_cCupom+CRLF+;
                           "003-000 = "+AllTrim(v_valort)+CRLF+;
                           "004-000 = 0"+CRLF+;
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "022-000 = "+v_dtcmv+CRLF+;
                           "023-000 = "+v_hrcmv+CRLF+;
                           "701-000 = SALC"+_versao+CRLF+;
                           "706-000 = 3"+CRLF+;
                           "716-000 = "+_soft+CRLF+;
                           "999-999 = 0"+CRLF
            else
               v_cTxt   := "000-000 = CNC"+CRLF+;
                           "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                           "002-000 = "+v_cCupom+CRLF+;
                           "003-000 = "+AllTrim(v_valort)+CRLF+;
                           "010-000 = "+v_cRede+ CRLF+;
                           "012-000 = "+v_cNSU +CRLF +;
                           "022-000 = "+v_dtcmv+CRLF+;
                           "023-000 = "+v_hrcmv+CRLF+;
                           "999-999 = 0"+CRLF
            endif
//                        "027-000 = "+v_cFinal+CRLF+;

                     //"777-777 = teste Redecard"+CRLF+;    // Homologação   // se nao for necessario pra CNC retirar essas 2 linhas
                     //"777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA

               nResp := .T.

               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
                  v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

               ELSE
                  DelMsgTef( cdirtef )
                  Ferase(cdirtef+"\IntPos.res")
                  v_nRet := -21
                  nResp := .f.
               ENDIF

            ELSE
               if !_tefdedic
                  liberatc()
               endif

               DelMsgTef( cdirtef )
               Ferase(cdirtef+"\IntPos.res")
               v_nRet := -21
               nResp := .F.
            ENDIF

            if lhiper
               cdirtef := cdirant
               cdirant := cdirtef
            endif

     //FAZ PRE-AUTORIZACAO
     ELSEIF v_cTipo = "PRE"

        nVezes := 1

        do while !ver_ativo(cdirtef)

           if !_tefdedic
              if ltravatef
                 liberatc()
              endif
           endif

           if !v_superM
              obtn_finaliza:disable()
              obtn_finaliza:refresh(.t.)
           endif
           if _tefdedic
              erro("Sem conexão SiTef","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           else
              erro("Gerenciador TEF não ativo!","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           endif
           if !v_superM
              obtn_finaliza:enable()
              obtn_finaliza:refresh(.t.)
           endif

           if !_tefdedic
              if ltravatef
                 travatc()
              endif
           endif

           sysrefresh()
        enddo

        DelMsgTef( cDirTef )
        v_cCupom = vdocum
        if val(StrTran(v_cPago,",")) > 999
           teste_val = StrTran(v_cPago,",")
        else
           teste_val = strzero(val(StrTran(v_cPago,",")),3)
        endif

        if lconf_pre
           cConf501 := "0010000000000000000000000000000000000000000000000000000000000000000000000000000"
        else
           cConf501 := "1101000000000000000000000000000000000000000000000000000000000000000000000000000"
        endif

        if !_tefdedic
           v_cTxt := "000-000 = PRE"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "004-000 = 0"+CRLF+;
                     "501-001 = "+cConf501+CRLF+;
                     "701-000 = SALC"+_versao+CRLF+;
                     "716-000 = "+_soft+CRLF+;
                     "999-999 = 0"+CRLF
        else
           v_cTxt := "000-000 = PRE"+CRLF+;
                     "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                     "002-000 = "+v_cCupom+CRLF+;
                     "003-000 = "+AllTrim(teste_val)+CRLF+;
                     "210-084 = SALC"+_versao+"10"+CRLF+;
                     "501-001 = "+cConf501+CRLF+;
                     "701-034 = 4"+CRLF+;
                     "999-999 = 0"+CRLF
        endif

//                  "001-000 = "+v_cTef+CRLF+;
                  // "777-777 = teste Redecard"+CRLF+;    // Homologação
                  // "777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

        IF .NOT. Empty( v_cTxt := EnvCmdTef(v_cTxt)  )

           //SE TRANSAÇÃO APROVADA
           IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
              v_nLin   := Val( ExtMsgTef(v_cTxt,"028-000") )
              v_cBuf := ""
              FOR v_n = 1 TO v_nLin
                  v_cBuf += StrTran(ExtMsgTef( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                  sysrefresh()
              NEXT

              if nPgTEF > 1 .and. nCtTEF < nPgTEF // verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                 nFileHandle := FCreate( cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 if !lconf_pre

                    if !_tefdedic
                       ltravatef := .t.
                       travatc()
                    endif

                    if _tefdedic

                       IF PrintCupTef( .T. ) = 1
                          if !_tefdedic
                             liberatc()
                          endif
                          fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                       ELSE
                          fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                       ENDIF

                    else

                       IF PrintCupTef( .T. ) = 1
                          liberatc()
                          fazTef("CNF",v_cTef,v_cPago)
                       ELSE
                          fazTef("NCN",v_cTef )
                       ENDIF

                    endif

                    if !_tefdedic
                       liberatc()
                       ltravatef := .f.
                    endif

                 endif

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                 if lconf_pre
                    if !_tefdedic
                       if GrvCmdTef(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f.)
                          DelMsgTef(cdirtef)
                       endif
                    endif
                 endif

                 if lconf_pre
                    albx_formas[iformas, 14] := cDirTef+"\imprime"+strzero(nCtTEF, 2)+".txt"
                    albx_formas[iformas, 15] := cDirTef+"\IntPos"+strzero(nCtTEF, 2)+".TMP"
                 endif

              elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 if !lconf_pre

                    if !_tefdedic
                       ltravatef := .t.
                       travatc()
                    endif

                    if _tefdedic

                       IF PrintCupTef( .T. ) = 1
                          if !_tefdedic
                             liberatc()
                          endif
                          fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                       ELSE
                          fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                       ENDIF

                    else

                       IF PrintCupTef( .T. ) = 1
                          liberatc()
                          fazTef("CNF",v_cTef,v_cPago)
                       ELSE
                          fazTef("NCN",v_cTef )
                       ENDIF

                    endif

                    if !_tefdedic
                       liberatc()
                       ltravatef := .f.
                    endif

                 endif

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                 if lconf_pre
                    albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                    if !_tefdedic
                       albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                    else
                       albx_formas[iformas, 15] := if(file(cDirTef+"\IntPos.tmp"), cDirTef+"\IntPos.tmp", cDirTef+"\IntPos.001")
                    endif
                 endif

              else

                 nFileHandle := FCreate( cDirTef+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 if !lconf_pre

                    if !_tefdedic
                       ltravatef := .t.
                       travatc()
                    endif

                    if _tefdedic

                       IF PrintCupTef( .T. ) = 1
                          if !_tefdedic
                             liberatc()
                          endif
                          fazTef("CNF",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001")
                       ELSE
                          fazTef("NCN",v_cTef,v_cPago,,, cDirTef+"\RESP\intpos.001" )
                       ENDIF

                    else

                       IF PrintCupTef( .T. ) = 1
                          liberatc()
                          fazTef("CNF",v_cTef,v_cPago)
                       ELSE
                          fazTef("NCN",v_cTef )
                       ENDIF

                    endif

                    if !_tefdedic
                       liberatc()
                       ltravatef := .f.
                    endif

                 endif

                 COPYFILE(cDirTef+"\RESP\IntPos.001",cDirTef+"\IntPos.001", .f.)

                 if lconf_pre
                    albx_formas[iformas, 14] := cDirTef+"\imprime.txt"
                    albx_formas[iformas, 15] := cDirTef+"\IntPos.001"
                 endif

              endif

              v_nRet := 1

           ELSE
              lTEFneg := .t.
              DelMsgTef( cDirTef )
              Ferase(cDirTef+"\IntPos.res")
              v_nRet := -21
           ENDIF
        ENDIF

     ENDIF

  END SEQUENC

RETURN v_nRet


FUNCTION ExtMsgTef( cTxt,cCod )
LOCAL cLin

   if At(cCod,cTxt) == 0
      cLin := ""
   else
      cLin := Substr(cTxt,At(cCod,cTxt)+10)
      cLin := Left(cLin,At(CHR(13)+CHR(10),cLin)-1)
   endif

RETURN cLin


FUNCTION EnvCmdTef( cTxt )

LOCAL nQte:= 0,cRet,nLin, c002Req, c001sts, c002sts, c003sts, c005Sts, nFileHandle

   Ferase(cDirTef+"\RESP\IntPos.Sts")
   nFileHandle := FCreate( cDirTef+"\REQ\IntPos.tmp", 0 )
   FWrite( nFileHandle, cTxt, Len(cTxt) )
   FClose( nFileHandle )

   copyfile(cDirTef+"\REQ\IntPos.tmp",cDirTef+"\REQ\IntPos.001")
   ferase(cDirTef+"\REQ\IntPos.tmp")

//   Frename(cDirTef+"\REQ\IntPos.tmp",cDirTef+"\REQ\IntPos.001")

   c001sts := ExtMsgTef(cTxt, "000-000")

   DO WHILE nQte < 4
      SysWait(2)
      IF File(cDirTef+"\RESP\IntPos.Sts")
         EXIT
      ENDIF
      nQte++
      sysrefresh()
   ENDDO

   IF nQte >= 4
      if !_tefdedic
         if ltravatef
            liberatc()
         endif
      endif

      if _tefdedic
         erro("Sem conexão SiTef","Atenção")
      else
         erro("Gerenciador TEF não ativo!","Atenção")
      endif

      if !_tefdedic
         if ltravatef
            travatc()
         endif
      endif

      if _tefdedic
         if c001sts # 'CNF'
            DelMsgTef( cDirTef )
         else
            ferase(cDirTef+"\IntPos.CNF")
            copyfile(cDirTef+"\REQ\IntPos.001",cDirTef+"\IntPos.CNF")
            ferase(cDirTef+"\REQ\IntPos.001")
            aImprime  := Directory(cDirTef+"\imprime*.txt" )
            if len(aImprime) > 0
               aeval(aImprime, {|a| ferase(cDirTef+"\"+alltrim(a[1]))})
            endif
         endif
      else
         DelMsgTef( cDirTef )
      endif

      RETURN ""
   ELSE
      if _tefdedic
         if file(cDirTef+"\IntPos.CNF")
            ferase(cDirTef+"\IntPos.001")
            copyfile(cDirTef+"\IntPos.CNF", cDirTef+"\REQ\IntPos.001")
            ferase(cDirTef+"\IntPos.CNF")
         endif
         c001stsD := MemoRead( cDirTef+"\RESP\IntPos.sts" )
         c001stsD := ExtMsgTef(c001stsD, "000-000")
         if c001stsD == 'NCN' .or. c001stsD == 'CNF'
            DelMsgTef( cDirTef )
            RETURN cTxt
         endif
      endif
   ENDIF

   nQte := 0

   DO WHILE nQte < if(_tefdedic, 300, 300) .AND. .NOT. File(cDirTef+"\RESP\IntPos.001")

      nQte++
      SysWait(2)

      if File(cDirTef+"\RESP\IntPos.001")

         c002Req := MemoRead( cDirTef+"\RESP\IntPos.001" )

         if c001sts == "CRT"

            // inicio teste numero 47 e 48 roteiro visa
            c002sts := ExtMsgTef(c002Req, "001-000")
            c003sts := ExtMsgTef(cTxt, "001-000")

            c002sts := val(c002sts)
            c003sts := val(c003sts)

            c009sts := ExtMsgTef(c002Req, "009-000")

            if c009sts == "0" .and. c002sts # c003sts
               Ferase(cDirTef+"\RESP\IntPos.001")
               loop
            endif  // fim teste numero 47 e 48 roteiro visa

         endif

         nFileHandle := FCreate( cDirTef+"\IntPos.tmp", 0 )
         FWrite( nFileHandle, c002Req, Len(c002Req) )
         FClose( nFileHandle )

         Frename(cDirTef+"\IntPos.tmp",cDirTef+"\IntPos.res")

      endif

      sysrefresh()

   ENDDO

   IF nQte < if(_tefdedic, 300, 300)
*      ok('Aguardando teste RedeCard...')  // Homologação
      cTxt := MemoRead( cDirTef+"\RESP\IntPos.001" )
      cRet := ExtMsgTef(cTxt,"030-000")
      nLin := Val( ExtMsgTef(cTxt,"028-000") )

      IF nLin = 0 .AND. .NOT. Empty(cRet)

         if type("Wvar") == "U"
            if valtype(ODLG_PROA) == 'O'
               SetForegroundWindow( ODLG_PROA:hWnd )
               sysrefresh()
            endif
            if valtype(ownd_tef) == 'O'
               ownd_tef:refresh(.t.)
               ownd_tef:setfocus()
            endif
            ok(cRet,"Atenção")
            Wvar := .t.
         endif

      ENDIF
   ELSE
      cTxt := ""
      if !_tefdedic
         if ltravatef
            liberatc()
         endif
      endif

      if _tefdedic
         erro("Sem conexão SiTef","Atenção")
      else
         erro("Gerenciador TEF não Responde !","Atenção")
      endif
      if !_tefdedic
         if ltravatef
            travatc()
         endif
      endif

      DelMsgTef( cDirTef )
   ENDIF

RETURN cTxt


FUNCTION DelMsgTef( cDir )

   Ferase(cDir+"\RESP\IntPos.001")
   Ferase(cDir+"\RESP\IntPos.sts")
   //Ferase(cDir+"\Imprime.txt")

RETURN NIL



/*function ApagTEF(cdir, nArqSeq, cArq001)

   if empty(cArq001) .and. empty(nArqSeq)
      return(NIL)
   endif

   Ferase(cArq001)
   Ferase(cDir+"\RESP\IntPos.sts")
   Ferase(nArqSeq)

return NIL */


function ApagTEFG(nArqSeq, cArq001)

   if empty(cArq001) .and. empty(nArqSeq)
      return(NIL)
   endif

   Ferase(cArq001)
   Ferase(nArqSeq)

return NIL



FUNCTION DelMsgTefM( cDir, ntefi, nPgTEF )

//   if ntefi # nPgTEF
      Ferase(cDir+"\IntPos"+strzero(ntefi, 2)+".TMP")
//   endif
   Ferase(cDir+"\RESP\IntPos.sts")
   Ferase(cDir+"\imprime"+if(ntefi == nPgTEF, "", strzero(ntefi, 2))+".txt")

RETURN NIL




*****************************************************************************
* PROGRAMA: PrintCupTef.PRG                                                 *
* FUNCAO..: Imprime cupom TEF.                                              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
*****************************************************************************
FUNCTION PrintCupTef( lErro, cForma, cValor )

LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg := "",cRet,nSecs:=Seconds(), var_imprimir

   IF File(cDirTef+"\imprime.txt")

      CursorWait()

      ownd_tef:SetFocus()
      ownd_tef:Show()

      aRet := tef_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cDirTef+"\RESP\IntPos.001" )
      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cDirTef+"\imprime.txt")

      //SE FOR IMPRESSÃO APÓS ERRO
      IF lErro

         lImpCCD := .f.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64
                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrintCupTef( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrintCupTef( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrintCupTef( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrintCupTef( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _daruma .or. _sweda .or. _elgin .or. _bematech // incluido em 05/02/2011

               lAlgoAberto := .f.

               if _daruma
                  cEstado := space(2)
                  if GrvCmdFis(41,,,@cEstado,,.f.)
                     cEstado := substr(cEstado, 1,1)
                     if cEstado # '0'
                        lAlgoAberto := .t.
                     endif
                  elseif !VFimPapel()
                      IF ErroPrinter(@aRet)
                         RETURN (PrintCupTef( .T., cForma, cValor ))
                      ELSE
                         RETURN -22
                      ENDIF
                  endif
               elseif _elgin

                  nStGerCDC := 0
                  E_StatExtMFD(@nStGerCDC)

                  if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                     lAlgoAberto := .t.
                  elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                     lAlgoAberto := .t.
                  endif

               elseif _bematech

                  if GrvCmdFis(80,,,,,.f.)
                     lAlgoAberto := .t.
                  endif

               else
                  if GrvCmdFis(41,,,,,.f.)
                     lAlgoAberto := .t.
                  endif
               endif

               if lAlgoAberto

                  if v_framework

                     cGerAbre := space(2)
                     nRet := DaMFDInf('56', @cGerAbre)
                     cGerAbre := alltrim(cGerAbre)

                     if cGerAbre == '4'

                        nQualComando := 25

                     else

                        nQualComando := 23

                     endif

                  else

                     nQualComando := 25

                  endif

                  IF GrvCmdFis(nQualComando,,,,,.F.)
                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrintCupTef( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrintCupTef( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            aRet[3]:SetText(cMsg)

            if _tefdedic
               c028001 := ExtMsgTef(cRet, "028-001")
               if val(c028001) == 0
                  nLinCorte := 0
                  nviastef := 1
               else
                  nLinCorte := val(c028001) + 1
               endif
            else
               nLinCorte := 0
            endif

            FOR n = 1 TO nviastef

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64

                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrintCupTef( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.f.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'

                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrintCupTef( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _elgin

                   lAlgoAberto := .f.
                   nStGerCDC := 0
                   E_StatExtMFD(@nStGerCDC)

                   if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                      lAlgoAberto := .t.
                   elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                      lAlgoAberto := .t.
                   endif

                   if !lAlgoAberto

                      cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                      nRetElgin := E_AbreGerMFD(cIndGer)

                      if nRetElgin == 0
                         IF ErroPrinter(@aRet)
                            RETURN (PrintCupTef( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _daruma

                   if v_framework

                      nRet := daProgFormas("RG", "TEF1", ";")

                      nRet := DaAbreGerMFD('TEF1')

                   endif


                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.F.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10) // reincluir
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)

                    nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor,,,,, cMsg1 := ExtMsgTef(cRet,"030-000"))

                    if nRetC == -22
                       RETURN -22
                    elseif nRetC == -30
                       RETURN 1
                    endif

                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF

                    if _urano .or. _epson
                       IF !GrvCmdFis(22,var_imprimir)
                          IF ErroPrinter(@aRet)
                             RETURN (PrintCupTef( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(@aRet)
                              RETURN (PrintCupTef( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else

                       if .NOT. GrvCmdFis(24,var_imprimir,,,,.f.)

                           IF ErroPrinter(@aRet)
                              RETURN (PrintCupTef( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       elseif !VFimPapel()

                          IF ErroPrinter(@aRet)
                             RETURN (PrintCupTef( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       ENDIF

                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. nviastef > 1

                   FOR k = 1 TO 10

                       if _urano .or. _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(@aRet)
                                RETURN (PrintCupTef( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrintCupTef( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else

                          if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(@aRet)
                                RETURN (PrintCupTef( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrintCupTef( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()
                      SysWait(3)
                      aRet[1]:SetFocus()   // Alteracao Alex

                      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                         aRet[3]:SetText(cMsg)
                      ENDIF

                   endif

                ENDIF

                sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma / Elgin / Sweda / Urano / Epson

      ELSE

         IF .NOT. GrvCmdFis(21,{cForma,cValor,NIL},,,,.F.)
            IF ErroPrinter(@aRet)
               RETURN (PrintCupTef( .T., cForma, cValor ))
            ELSE
               RETURN -22
            ENDIF

         elseif !VFimPapel()

             IF ErroPrinter(@aRet)
                RETURN (PrintCupTef( .T., cForma, cValor ))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         if _tefdedic
            c028001 := ExtMsgTef(cRet, "028-001")
//            nLinCorte := val(c028001) + 1
            if val(c028001) == 0
               nLinCorte := 0
               nviastef := 1
            else
               nLinCorte := val(c028001) + 1
            endif
         else
            nLinCorte := 0
         endif

         FOR n = 1 TO nviastef
             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()
             FOR k = 1 TO MlCount(cTxt)

                 nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor,,,,, cMsg1 := ExtMsgTef(cRet,"030-000"))

                 if nRetC == -22
                    RETURN -22
                 elseif nRetC == -30
                    RETURN 1
                 endif

                 IF .NOT. GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k), CRLF),48)+CRLF,,,,.F. )
                    IF ErroPrinter(@aRet)
                       RETURN (PrintCupTef( .T., cForma, cValor ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 elseif !VFimPapel()
                    IF ErroPrinter(@aRet)
                       RETURN (PrintCupTef( .T., cForma, cValor ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 ENDIF

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviastef > 1

                FOR k = 1 TO 10

                    if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                       IF ErroPrinter(@aRet)
                          RETURN (PrintCupTef( .T., cForma, cValor ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                       IF ErroPrinter(@aRet)
                          RETURN (PrintCupTef( .T., cForma, cValor ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[1]:SetFocus()   // Alteracao Alex

                IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                   aRet[3]:SetText(cMsg)
                ENDIF

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         else
            if !GrvCmdFis(88)
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrintCupTef( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif
      ENDIF

      aRet[1]:End() ; aRet[1] := NIL

   ELSE
      if !_tefdedic
         liberatc()
      endif

      ok("Não há cupons para imprimir!","Atenção")
      lRet := .f.
   ENDIF

RETURN If(lRet,1,0)


STATIC FUNCTION ErroPrinter(aRet)

  local retorno := .f.

  if valtype(aRet[1]) == 'O'
     aRet[1]:End() ; aRet[1] := NIL
     ownd_tef:SetFocus()
     ownd_tef:Show()

     if !_tefdedic
         liberatc()
     endif

     retorno := Continua("Impressora não Responde. Tentar imprimir novamente?")

     if !_tefdedic
        travatc()
     endif

     if retorno
        syswait(5)
     endif
  else
     retorno := .t.
  endif

RETURN retorno



STATIC FUNCTION tef_aguarde( cIcone,cMens )
LOCAL oDlgWait,oMtr,nPerc,oSay,oIco

   CursorWait()
   cIcone := If(cIcone=NIL,"I_AGUARDE",cIcone)

   DEFINE DIALOG oDlgWait RESOURCE "DLG_AGUARDE" FONT fnt_ecf COLOR cor_dlg

	REDEFINE ICON oIco NAME (cIcone)        ID 101 OF oDlgWait UPDATE
        REDEFINE SAY oSay                       ID 301 OF oDlgWait UPDATE
        REDEFINE METER oMtr VAR nPerc TOTAL 100 ID 901 OF oDlgWait UPDATE

        IF cMens # NIL
           oSay:SetText(cMens)
        ENDIF

    odlgWait:lhelpicon:=.f.
    ACTIVATE DIALOG oDlgWait CENTERED NOWAIT

RETURN {oDlgWait,oMtr,oSay}



*****************************************************************************
* PROGRAMA: GrvCmdTef.PRG                                                   *
* FUNCAO..: Envia comando para TEF.                                         *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
*****************************************************************************
FUNCTION GrvCmdTef(nCmd,uPar,lMens,lInic, nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

LOCAL nRet:=0, oIni, nTef:=0, cTef, kl, xki, aSoTEF, nQtVinc  // aImprime,

default cCodForma := ''

  lMens := If(lMens=NIL,.T.,lMens)
  lInic := If(lInic=NIL,.T.,lInic)

  if vl_seqtef == NIL
     vl_seqtef := vl_venda
  endif

  cTef := StrZero(val(vl_seqtef),10)

  //VER TEF ATIVO
  IF nCmd = 1
         nRet := BmTfStatus(cTef)

  //ACIONA TEF ADM
  ELSEIF nCmd = 2
         nRet := fazTef("ADM",cTef,uPar )

  //VENDA CARTÃO
  ELSEIF nCmd = 3
         nRet := fazTef("CRT",cTef,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA DE CARTÃO
  ELSEIF nCmd = 4

         if !_tefdedic
            travatc()
            ltravatef := .t.
         endif

         nRet := PrintCupTef( .F.,uPar[1], uPar[2] )

         if !_tefdedic
            liberatc()
            ltravatef := .f.
         endif

  //CONFIRMA IMPRESSÃO DE TEF
  ELSEIF nCmd = 5

         nRet := fazTef("CNF",cTef,uPar[1],,, cArq001) // original

  ELSEIF nCmd = 6

         nRet := fazTef("CHQ",cTef,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA COM MULTIPLOS CARTOES
  ELSEIF nCmd = 7

         if 'INTPOS.001'$upper(cArq001)
            COPYFILE(cDirTef+"\IntPos.001",cDirTef+"\RESP\IntPos.001", .f.)
            ferase(cDirTef+"\IntPos.001")
            cArq001 := cDirTef+"\RESP\IntPos.001"
         endif

         nRet := PrtCupTefM( .F., albx_formas[nCtTEF, 4], albx_formas[nCtTEF, 2], nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

         if nRet == -44
            nRet := 1
         endif

  // PRE-AUTORIZACAO
  ELSEIF nCmd = 8

         nRet := fazTef("PRE",cTef,uPar, nCtTEF, nPgTEF, , , , .t.)

  ENDIF

  ownd_tef:Show()

   IF lMens
      IF nRet = 0
             erro("Gerenciador TEF Inativo !","Atenção")

      ELSEIF nRet = -2
             erro("Parâmetro inválido para comando "+Str(nCmd,2)+" !","Atenção")

      ELSEIF nRet = -18
             erro("Não foi possível abrir/criar arquivo .001 ou .sts !","Atenção")

      ELSEIF nRet = -19
             erro("Numero de Identificação não bate com o do arquivo .001 !","Atenção")

      ELSEIF nRet = -20
             erro("Transação finalizada pelo operador !","Atenção")

      ELSEIF nRet = -21
             erro("A transação não foi aprovada !","Atenção")

      ELSEIF nRet = -22
             erro("Não foi possível terminar a impressão !","Atenção")

      ELSEIF nRet = -23
             erro("Não foi possível terminar a operação !","Atenção")

      ELSE
             RETURN .T.
      ENDIF

  ELSEIF nRet = 1
      RETURN .T.
  ENDIF

RETURN .F.



FUNCTION ver_ativo( cdir )

LOCAL nQte:= 0, retorno := .t., ctxt

cTxt := "000-000 = ATV"+CRLF+;   // colocar ATV antes de toda requisicao // para visa
        "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
        "999-999 = 0"+CRLF

if !_tefdedic
   if !ltravado
      travatc()
   endif
endif

Ferase(cDir+"\RESP\IntPos.sts")

Ferase(cDir+"\REQ\IntPos.tmp")

nFileHandle := FCreate( cDir+"\REQ\IntPos.tmp", 0 )
FWrite( nFileHandle, cTxt, Len(cTxt) )
FClose( nFileHandle )

Frename(cDir+"\REQ\IntPos.tmp",cDir+"\REQ\IntPos.001")
Ferase(cDir+"\REQ\IntPos.tmp")

DO WHILE nQte < 4
   SysWait(2)
   IF File(cDir+"\RESP\IntPos.Sts")
      Ferase(cDir+"\RESP\IntPos.sts") // ver se não vai afetar nada, se afetar retirar, em 03/07/2012.
      EXIT
   ENDIF
   nQte++
   sysrefresh()
ENDDO

IF nQte >= 4
   retorno = .f.
ENDIF

if !_tefdedic
   if !ltravado
      liberatc()
   endif
endif

return retorno



function VFimBobina()

local Wret := .t., cErroExt

private vSemPapel := .f.

if v_NFCE .and. !v_NFCE_ECF
   return(.t.)
endif

if _bematech

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0

  nRet := Bmretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _daruma

  uBuf1_status := space(5)
  ubuf2_status := space(150)
  ubuf3_status := space(5)

  nComando := 8

  if v_framework

     nRet := DaECF(@nComando, @ubuf2_status)

     ubuf2_status := alltrim(ubuf2_status)
     ubuf2_status := strtran(ubuf2_status, chr(0), '')

     if nRet == -12
        nRet := 1
     endif

     if ubuf2_status == 'Bobina de papel ausente'
        Wret := .f.
     endif

  endif

elseif _Sweda

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := Ecfretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _elgin

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := E_retorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _urano

  vSemPapel := .f.
  Estado_Urano()
  if vSemPapel
     Wret := .f.
  endif

elseif _epson

/*
   if EPSON_AbrePorta() # 0
      erro('Não foi possível abrir a porta de comunicação com a Impressora!')
      retorno := .f.
   endif
*/

   cEstadoImpressora := space(17)
   cEstadoFisc := space(17)
   cRetCmd := space(5)
   cMsgCmd := space(101)

   nRet := EPSON_RetornoEx( @cEstadoImpressora, @cEstadoFisc, @cRetCmd, @cMsgCmd)

//   msginfo(cEstadoImpressora)

/*
   msginfo(cEstadoImpressora)
   msginfo(cEstadoFisc)
   msginfo(cRetCmd)
   msginfo(cMsgCmd)
*/

   if substr(cEstadoImpressora, 15, 1) == '1' .or. ;
      substr(cEstadoImpressora, 3, 1) == '1' .or. ;
      substr(cEstadoImpressora, 9, 1) == '1' .or. ;
      substr(cEstadoImpressora, 8, 1) == '1'
      Wret := .f.
   endif

/*
  vflag_aberto := space(21)
  GrvCmdFis(04,NIL,21,@vflag_aberto,,.f.)

  vflag_aberto := substr(vflag_aberto, 9, 4)

  vflag_aberto := HexToDec(vflag_aberto)

  *** Analisando o flag

  if vflag_aberto >= 32768
     vflag_aberto  = vflag_aberto - 32768
     Wret := .f.
  endif

  if vflag_aberto >= 16384
     vflag_aberto  = vflag_aberto - 16384
     Wret := .f.
  endif

  if vflag_aberto >= 4096
     vflag_aberto  = vflag_aberto - 4096
//     Wret := .f.
  endif

  if vflag_aberto >= 2048  && Memória Fiscal sem espaco
     vflag_aberto  = vflag_aberto - 2048
     Wret := .f.
  endif

  if vflag_aberto >= 1024  && Memória Fiscal sem espaco
     vflag_aberto  = vflag_aberto - 1024
     Wret := .f.
  endif

  if vflag_aberto >= 512
     vflag_aberto  = vflag_aberto - 512
     Wret := .f.
  endif

  if vflag_aberto >= 256
     vflag_aberto  = vflag_aberto - 256
     Wret := .f.
  endif

  if vflag_aberto >= 128
     vflag_aberto  = vflag_aberto - 128
     Wret := .f.
  endif

  if vflag_aberto >= 64
     vflag_aberto  = vflag_aberto - 64
     Wret := .f.
  endif

  if vflag_aberto >= 32
     vflag_aberto  = vflag_aberto - 32
     Wret := .f.
  endif

  if vflag_aberto >= 16
     vflag_aberto  = vflag_aberto - 16
     Wret := .f.
  endif

  if vflag_aberto >= 8
     vflag_aberto  = vflag_aberto - 8
     Wret := .f.
  endif

  if vflag_aberto >= 4
     vflag_aberto  = vflag_aberto - 4
     Wret := .f.
  endif

  if vflag_aberto >= 2
     vflag_aberto  = vflag_aberto - 2
     Wret := .f.
  endif

//  if vflag_aberto >= 1  && Cupom Fiscal Aberto
//     vflag_aberto  = vflag_aberto - 1
//  endif

*/

endif

return(Wret)




function VFimPapel()

local Wret := .t., cErroExt

private vSemPapel := .f.

if v_NFCE .and. !v_NFCE_ECF
   return(.t.)
endif

if _bematech

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0

  nRet := Bmretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _daruma

  uBuf1_status := space(5)
  ubuf2_status := space(5)
  ubuf3_status := space(5)

  if v_framework

     nRet := Daretorno(uBuf1_status, ubuf2_status)

     if nRet == -12
        nRet := 1
     endif

     uBuf1_status := val(uBuf1_status)
     ubuf2_status := val(uBuf2_status)

     if uBuf1_status == 72 .or. ubuf2_status == 2
        Wret := .f.
     endif

  endif

elseif _Sweda

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := Ecfretorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _elgin

  uBuf1_status := 0
  ubuf2_status := 0
  ubuf3_status := 0
  nRet := E_retorno(@uBuf1_status, @ubuf2_status, @ubuf3_status)

  if ubuf2_status >= 128
     Wret := .f.
  endif

elseif _urano

  vSemPapel := .f.
  Estado_Urano()
  if vSemPapel
     Wret := .f.
  endif

elseif _epson

   cEstadoImpressora := space(17)
   cEstadoFisc := space(17)
   cRetCmd := space(5)
   cMsgCmd := space(101)

   nRet := EPSON_RetornoEx( @cEstadoImpressora, @cEstadoFisc, @cRetCmd, @cMsgCmd)

   if substr(cEstadoImpressora, 15, 1) == '1' .or. ;
      substr(cEstadoImpressora, 3, 1) == '1' .or. ;
      substr(cEstadoImpressora, 9, 1) == '1' .or. ;
      substr(cEstadoImpressora, 8, 1) == '1'
      Wret := .f.
   endif

endif

return(Wret)






*****************************************************************************
* PROGRAMA: PrtCupTefM.PRG                                                 *
* FUNCAO..: Imprime cupom TEF.                                              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
* ALTERADO: Sebastiao de Oliveira Pessoa                     EM: 09-20-2010 *
*****************************************************************************
FUNCTION PrtCupTefM( lErro, cForma, cValor, nki, nPgTEF, cNumArq, cArq001, cCodForma)

LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg := "",cRet,nSecs:=Seconds(), var_imprimir, aImprime, aImprime1, aImprime2

private nViasMC, nTefGP, nTefHp, nTefTc

   IF File(cNumArq)

      CursorWait()

      ownd_tef:SetFocus()
      ownd_tef:Show()

      aRet := tef_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cArq001)  //               "\RESP\IntPos.001" )
      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cNumArq)

      //SE FOR IMPRESSÃO APÓS ERRO

      IF lErro

         lGerImp := .t.

         cTxt := ""
         nViasMC := 0

         if _tefdedic
            nTefGP  := Directory(_cdirtef+"\imprime*.txt" )
            nViasMC := len(nTefGP)
         else
            nTefGP  := Directory(_cdirtef+"\imprime*.txt" )
            nTefHp  := Directory(cdirhiper+"\imprime*.txt" )
            nTefTc  := Directory(cdirtecban+"\imprime*.txt" )
            nViasMC := len(nTefGP) + len(nTefHp) + len(nTefTc)
         endif

         LeAposerro(@aImprime, @aImprime1, @aImprime2, @cTxt)

         lImpCCD := .f.
         lerroImp := .t.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64

                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )) // (PrtCupTefM( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _daruma .or. _sweda .or. _elgin .or. _bematech // incluido em 05/02/2011

               lAlgoAberto := .f.

               if _daruma

                  cEstado := space(2)
                  if GrvCmdFis(41,,,@cEstado,,.f.)
                     cEstado := substr(cEstado, 1,1)
                     if cEstado # '0'
                        lAlgoAberto := .t.
                     endif
                  elseif !VFimPapel()
                     IF ErroPrinter(@aRet)
                        RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                     ELSE
                        RETURN -22
                     ENDIF
                  endif

               elseif _elgin

                  nStGerCDC := 0
                  E_StatExtMFD(@nStGerCDC)

                  if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                     lAlgoAberto := .t.
                  elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                     lAlgoAberto := .t.
                  endif

               elseif _bematech

                  if GrvCmdFis(80,,,,,.f.)
                     lAlgoAberto := .t.
                  endif

               else
                  if GrvCmdFis(41,,,,,.F.)
                     lAlgoAberto := .t.
                  endif
               endif

               if lAlgoAberto

                  if v_framework

                     cGerAbre := space(2)
                     nRet := DaMFDInf('56', @cGerAbre)
                     cGerAbre := alltrim(cGerAbre)

                     if cGerAbre == '4'

                        nQualComando := 25

                     else

                        nQualComando := 23

                     endif

                  else

                     nQualComando := 25

                  endif

                  IF GrvCmdFis(nQualComando,,,,,.F.)
                     if !VFimPapel()
                        IF ErroPrinter(@aRet)
                           RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(@aRet)
                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            aRet[3]:SetText(cMsg)

            if _tefdedic
               if nViasMC > 1
                  nLinCorte := 0
                  nviastef := 1
               else
                  c028001 := ExtMsgTef(cRet, "028-001")
                  if val(c028001) == 0
                     nLinCorte := 0
                     nviastef := 1
                  else
                     nLinCorte := val(c028001) + 1
                  endif
               endif
            else
               nLinCorte := 0
            endif

            FOR n := 1 TO if(nViasMC == 1, nviastef, 1)

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64

                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'
                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(@aRet)
                            RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF
                   endif

                elseif _elgin

                     lAlgoAberto := .f.
                     nStGerCDC := 0
                     E_StatExtMFD(@nStGerCDC)

                     if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                        lAlgoAberto := .t.
                     elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                        lAlgoAberto := .t.
                     endif

                     if !lAlgoAberto

                        cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                        cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                        nRetElgin := E_AbreGerMFD(cIndGer)

                        if nRetElgin == 0
                           IF ErroPrinter(@aRet)
                              RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF
                        ENDIF

                     endif

                elseif _daruma

                   if v_framework

                      nRet := daProgFormas("RG", "TEF1", ";")

                      nRet := DaAbreGerMFD('TEF1')

                   endif

                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.F.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10)
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)

                    nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor, @nki, @nPgTEF, @cNumArq, @cArq001, cMsg1 := ExtMsgTef(cRet,"030-000"))

                    if nRetC == -22
                       RETURN -22
                    elseif nRetC == -30
                       RETURN 1
                    endif

                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF

                    if _urano .or. _epson

                       IF !GrvCmdFis(22,var_imprimir)

                          if _urano

                             nEstado := 0
                             GrvCmdFis(78,,,@nEstado,,.F.)

                             if nEstado == 32 .or. nEstado == 64

                                IF ErroPrinter(@aRet)
                                   RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             else

                                IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                   IF ErroPrinter(@aRet)
                                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF
                                ENDIF

                             endif

                          else

                             IF ErroPrinter(@aRet)
                                RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF

                          endif

                       elseif !VFimPapel()

                           IF ErroPrinter(@aRet)
                              RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else

                       IF .NOT. GrvCmdFis(24,var_imprimir,,,,.f.)
                          IF ErroPrinter(@aRet)
                             RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(@aRet)
                              RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. if(nViasMC == 1, nviastef, 1) > 1

                   FOR k = 1 TO 10

                       if _urano .or. _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )

                             if _urano

                                nEstado := 0
                                GrvCmdFis(78,,,@nEstado,,.F.)

                                if nEstado == 32 .or. nEstado == 64

                                   IF ErroPrinter(@aRet)
                                      RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF

                                else

                                   IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                      IF ErroPrinter(@aRet)
                                         RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                      ELSE
                                         GrvCmdFis(25,,,,,.F.)
                                         RETURN -22
                                      ENDIF
                                   ENDIF

                                endif

                             else

                                IF ErroPrinter(@aRet)
                                   RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             endif

                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else
                          if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(@aRet)
                                RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(@aRet)
                                 RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano .and. !_tefdedic
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()
                      SysWait(3)
                      aRet[1]:SetFocus()   // Alteracao Alex

                      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                         aRet[3]:SetText(cMsg)
                      ENDIF

                   endif

                ENDIF

                sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma / Elgin / Sweda / Urano

         aRet[1]:End() ; aRet[1] := NIL

         RETURN -44

      ELSE

         IF !GrvCmdFis(if(_daruma, 78, 21),{cForma,cValor,NIL},,,,.F.)

            IF ErroPrinter(@aRet)
               RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
            ELSE
               RETURN -22
            ENDIF

         elseif !VFimPapel()

             IF ErroPrinter(@aRet)
                RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         v_cTpTran := ExtMsgTef(cRet,"011-000")
         v_cRedeTr  := ExtMsgTef(cRet,"010-000")

         if _bematech
            if W_MFD == 1 .or. cModeloBm == 'MP25FI' .or. cModeloBm == 'MP50FI'
               cCooCDC := space(6)
               GrvCmdFis(14,, 6, @cCooCDC)
               aadd(aCOOCDC, { strzero(val(cCooCDC), 9), cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr, vtot_ven })
            endif
         else
            cCooCDC := space(6)
            GrvCmdFis(14,, 6, @cCooCDC)
            aadd(aCOOCDC, { strzero(val(cCooCDC), 9), cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr, vtot_ven })
         endif

         if _tefdedic
            c028001 := ExtMsgTef(cRet, "028-001")
//            nLinCorte := val(c028001) + 1
            if val(c028001) == 0
               nLinCorte := 0
               nviastef := 1
            else
               nLinCorte := val(c028001) + 1
            endif
         else
            nLinCorte := 0
         endif

         FOR n = 1 TO nviastef

             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()
             FOR k = 1 TO MlCount(cTxt)

                 nRetC := FTEF_Corte(@k, @nLinCorte, @aRet, @lerro, @n, @cForma, @cValor, @nki, @nPgTEF, @cNumArq, @cArq001, cMsg1 := ExtMsgTef(cRet,"030-000"))

                 if nRetC == -22
                    RETURN -22
                 elseif nRetC # -22 .and. k == MlCount(cTxt)
                    exit
                 elseif nRetC == -30
                    RETURN 1
                 endif

                 IF .NOT. GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k), CRLF),48)+CRLF,,,,.F. )
                    IF ErroPrinter(@aRet)
                       RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 elseif !VFimPapel()
                    IF ErroPrinter(@aRet)
                       RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 ENDIF

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviastef > 1
                FOR k = 1 TO 10

                    if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                       IF ErroPrinter(@aRet)
                          RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                       IF ErroPrinter(@aRet)
                          RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[1]:SetFocus()   // Alteracao Alex

                IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
                   aRet[3]:SetText(cMsg)
                ENDIF

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         else

            if !GrvCmdFis(88)
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(@aRet)
                  RETURN (PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif
      ENDIF

      if valtype(aRet[1]) == 'O'
         aRet[1]:End() ; aRet[1] := NIL
      endif

   ELSE
      if !_tefdedic
         liberatc()
      endif
      ok("Não há cupoms para imprimir !","Atenção")
   ENDIF

RETURN(If(lRet,1,0))



function Leaposerro(aImprime, aImprime1, aImprime2, cTxt)

private nQueTEF := 0

cTxt := ""

if _tefdedic
   aImprime  := Directory(_cdirtef+"\imprime*.txt" )
   aImprime1 := {}
   aImprime2 := {}
else
   aImprime  := Directory(_cdirtef+"\imprime*.txt" )
   aImprime1 := Directory(cdirtecban+"\imprime*.txt" )
   aImprime2 := Directory(cdirhiper+"\imprime*.txt" )
endif

if (len(aImprime) >= 1 .or. len(aImprime1) >= 1 .or. len(aImprime2) >= 1) .and. nViasMC == 1

   if _tefdedic
      if len(nTefGP) > 0
         cTxt := Memoread(_cdirtef+"\imprime.txt" )
      endif
   else
      if len(nTefGP) > 0
         cTxt := Memoread(_cdirtef+"\imprime.txt" )
      elseif len(nTefHp) > 0
         cTxt := Memoread(cdirhiper+"\imprime.txt" )
      elseif len(nTefTc) > 0
         cTxt := Memoread(cdirtecban+"\imprime.txt" )
      endif
  endif

elseif len(aImprime) >= 1 .or. len(aImprime1) >= 1 .or. len(aImprime2) >= 1

   CriaTxt(@cTxt)

endif

return(.t.)



function CriaTxt(cTxt)

local kl, ncont, nTefDial, nTefTecB, nTefHiper, nTotTef, nSequen := 1, cTxt1 := ''

cTxt := ""

if _tefdedic
   nTefDial := len(Directory(_cdirtef+"\imprime*.txt" ))
   nTefTecB := 0
   nTefHiper := 0
else
   nTefDial := len(Directory(_cdirtef+"\imprime*.txt" ))
   nTefTecB := len(Directory(cdirtecban+"\imprime*.txt" ))
   nTefHiper := len(Directory(cdirhiper+"\imprime*.txt" ))
endif

if _tefdedic

   nTotTef := nTefDial
   cTxt := ""

   for kl := 1 to len(albx_formas)

       if albx_formas[kl, 2] == 0 .or. albx_formas[kl, 3] # 'A'
          loop
       endif

       if file(albx_formas[kl, 14])

          cTxt1 := MemoRead(albx_formas[kl, 15])

          c028001 := ExtMsgTef(cTxt1, "028-001")
          c028000 := ExtMsgTef(cTxt1, "028-000")

          nFimCupTef := val(c028000)

          if val(c028001) == 0
             nLinCorte := 0
             nviastef := 1
          else
             nLinCorte := val(c028001)
          endif

          if nLinCorte > 0

             cTxtV1 := MemoRead(albx_formas[kl, 14])

             for k := 1 to nLinCorte

                 cTxt := cTxt + Left(StrTran( Memoline(cTxtV1,50,k),CRLF),48)+CRLF

                 sysrefresh()

             next

             for ncont := 1 to 10
                 cTxt := cTxt + (Space(10)+CRLF)
                 sysrefresh()
             next

             for k := (nLinCorte + 1) to nFimCupTef

                 cTxt := cTxt + Left(StrTran( Memoline(cTxtV1,50,k),CRLF),48)+CRLF
                 sysrefresh()

             next

             if nSequen < nTotTef
                for ncont := 1 to 10
                    cTxt := cTxt + (Space(10)+CRLF)
                    sysrefresh()
                next
             endif

          else

             cTxtV1 := MemoRead(albx_formas[kl, 14])

             for k := 1 to nFimCupTef

                 cTxt := cTxt + Left(StrTran( Memoline(cTxtV1,50,k),CRLF),48)+CRLF
                 sysrefresh()

             next

          endif

          nSequen++

       endif

       sysrefresh()

   next

else

   nTotTef := nTefDial + nTefTecB + nTefHiper

   for kl := 1 to len(albx_formas)

       if albx_formas[kl, 2] == 0 .or. albx_formas[kl, 3] # 'A'
          loop
       endif

       if file(albx_formas[kl, 14])

          cTxt := cTxt + MemoRead(albx_formas[kl, 14])

          for ncont := 1 to 10
              cTxt := cTxt + (Space(10)+CRLF)
              sysrefresh()
          next

          cTxt := cTxt + MemoRead(albx_formas[kl, 14])

          if nSequen < nTotTef
             for ncont := 1 to 10
                 cTxt := cTxt + (Space(10)+CRLF)
                 sysrefresh()
             next
          endif

          nSequen++

       endif

       sysrefresh()

   next

endif

return(.t.)



function FTEF_Corte(k, nLinCorte, aRet, lerro, n, cForma, cValor, nki, nPgTEF, cNumArq, cArq001, cMsgTela)

local nRet := 1

begin sequence

if _tefdedic

    if k == nLinCorte .and. nLinCorte > 1

       nret := TEF_Espaco(@lerro, @n, @aRet, @cForma, @cValor, @nki, @nPgTEF, @cNumArq, @cArq001)

       if nRet == -22 .or. nRet == -30
          break
       endif

       if _epson
          EPSON_CortaPapel()
       elseif _urano
          GRVCMDFIS(80,,,,,.f.)
       elseif _elgin
          Elgin_Corta(1)
       elseif _bematech
          BmCortePapel(0)
       elseif _sweda
          ecfCorte(1)
       elseif _daruma
          DaGuilhotina("1")
       endif

       if valtype(aRet) == 'A' .and. len(aRet) > 0
          SysWait(1)
          aRet[3]:SetText("Destaque o comprovante.")
          SysWait(5)

          aRet[3]:SetText(cMsgTela)

          if aRet[1] # NIL
             aRet[1]:SetFocus()   // Alteracao Alex
          endif

       endif

   endif

endif

end sequence

return(nRet)



function TEF_Espaco(lerro, n, aRet, cForma, cValor, nki, nPgTEF, cNumArq, cArq001)

local nLinhasTEF := 10

if lerro

   if n = 1

      FOR k = 1 TO nLinhasTEF

          if _urano .or. _epson

             if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                IF ErroPrinter(@aRet)
                   nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                   RETURN(if(nRetPrin == -44, -30, -22))
                ELSE
                   GrvCmdFis(25,,,,,.F.)
                   RETURN -22
                ENDIF
             elseif !VFimPapel()
                 IF ErroPrinter(@aRet)
                    nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                    RETURN(if(nRetPrin == -44, -30, -22))
 //                   RETURN(-30)
                 ELSE
                    GrvCmdFis(25,,,,,.F.)
                    RETURN -22
                 ENDIF
             endif

          else

             if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                IF ErroPrinter(@aRet)
                   nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                   RETURN(if(nRetPrin == -44, -30, -22))
//                   RETURN(-30)
                ELSE
                   GrvCmdFis(25,,,,,.F.)
                   RETURN -22
                ENDIF
             elseif !VFimPapel()
                 IF ErroPrinter(@aRet)
                    nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                    RETURN(if(nRetPrin == -44, -30, -22))
//                    RETURN(-30)
                 ELSE
                    GrvCmdFis(25,,,,,.F.)
                    RETURN -22
                 ENDIF
             endif
          endif

          sysrefresh()

      NEXT

   endif

else
   IF n = 1

      FOR k = 1 TO nLinhasTEF

          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
             IF ErroPrinter(@aRet)
                nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                RETURN(if(nRetPrin == -44, -30, -22))
//                RETURN(-30)
             ELSE
                GrvCmdFis(23)
                RETURN -22
             ENDIF

          elseif !VFimPapel()

             IF ErroPrinter(@aRet)
                nRetPrin := PrtCupTefM( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 )
                RETURN(if(nRetPrin == -44, -30, -22))
//                RETURN(-30)
             ELSE
                GrvCmdFis(23)
                RETURN -22
             ENDIF

          endif

          sysrefresh()

      NEXT

   endif
endif

return(1)

TECBAN.prg


#include "FiveWin.ch"

*****************************************************************************
* PROGRAMA..: FAZTECBAN.PRGF                                                *
* FUNCAO....: Ativa gerenciador padrao de TEC-BAN                           *
*                                                                           *
* PARÂMETROS: V_CTIPO -> Tipo da operação a ser executada;                  *
*             V_CTecBan  -> Número de controle de operação TecBan;          *
*             V_CPAGO -> Valor da operação passado como texto.              *
*                                                                           *
* AUTOR...: Alexandre Pereira                                  EM: 19/07/04 *
*****************************************************************************
FUNCTION fazTecBan(v_cTipo,v_cTecBan,v_cPago, nCtTEF, nPgTEF, CintPos)

LOCAL v_cBuf,v_cTxt,v_cCupom:=Space(6),v_n,v_nLin,v_nRet := 0,aRet,v_cRede,;
      v_cFinal,v_cNSU,oDlgTecBan,oSay[3],v_cValor, teste_val, osayTxtVal

  ltravatef := .f.

  BEGIN  SEQUENC

     CursorWait()

     //FAZ VENDA EM CARTÃO

     IF v_cTipo = "CRT"

        if !v_superM
           ODLG_pag:disable()
           ODLG_pag:refresh(.t.)
        endif

        nVezes := 1

        do while !ver_ativo(cdirtecban)

           if !_tefdedic
              if ltravatef
                 liberatc()
              endif
           endif

           if _tefdedic
              erro("Sem conexão SiTef","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           else
              erro("Gerenciador TECBAN não ativo!","Atenção")
              nVezes++
              if nVezes >= 3
                 exit
              endif
           endif

           if !_tefdedic
              if ltravatef
                 travatc()
              endif
           endif

           sysrefresh()

        enddo

        if !v_superM
           ODLG_pag:enable()
           ODLG_pag:refresh(.t.)
        endif

        DelMsgTecBan( cdirtecban )
        v_cCupom = vdocum
        if val(StrTran(v_cPago,",")) > 999
           teste_val = StrTran(v_cPago,",")
        else
           teste_val = strzero(val(StrTran(v_cPago,",")),3)
        endif

        v_cTxt := "000-000 = CRT"+CRLF+;
                  "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                  "002-000 = "+v_cCupom+CRLF+;
                  "003-000 = "+AllTrim(teste_val)+CRLF+;
                  "999-999 = 0"+CRLF

//                   "777-777 = TESTE TECBAN"+CRLF+; // teste 11 item 25 roteiro tecban

*                   "032-927 = teste"+CRLF+;    // Homologação

        IF .NOT. Empty( v_cTxt := EnvCmdTecBan(v_cTxt)  )

           //SE TRANSAÇÃO APROVADA
           IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
              v_nLin   := Val( ExtMsgTecBan(v_cTxt,"028-000") )
              v_cBuf := ""
              FOR v_n = 1 TO v_nLin
                  v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                  sysrefresh()
              NEXT

              if nPgTEF > 1 .and. nCtTEF < nPgTEF// verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                 nFileHandle := FCreate( cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 COPYFILE(cdirtecban+"\RESP\IntPos.001",cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                 if GrvCmdTecBan(5,{left(albx_formas[nCtTEF, 1],16),"CNF"},.f.,.f., nCtTEF, nPgTEF)
                    DelMsgTef(cdirtecban)
                 endif

                 albx_formas[iformas, 14] := cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt"
                 albx_formas[iformas, 15] := cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

              elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                 nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                 albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"

              else
                 nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                 FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                 FClose( nFileHandle )

                 albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                 albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"
              endif

              v_nRet := 1
           ELSE
              DelMsgTecBan( cdirtecban )
              v_nRet := -21
           ENDIF
        ENDIF

         DelReqTecBan( cdirtecban )

     //FAZ VENDA COM CHEQUE
     ELSEIF v_cTipo = "CHQ"

           nVezes := 1

           do while !ver_ativo(cdirtecban)

              if !_tefdedic
                 if ltravatef
                    liberatc()
                 endif
              endif

              if _tefdedic
                 erro("Sem conexão SiTef","Atenção")
                 nVezes++
                 if nVezes >= 3
                    exit
                 endif
              else
                 erro("Gerenciador TECBAN não ativo!","Atenção")
                 nVezes++
                 if nVezes >= 3
                    exit
                 endif
              endif

              if !_tefdedic
                 if ltravatef
                    liberatc()
                 endif
              endif

              sysrefresh()

           enddo

           DelMsgTecBan( cdirtecban )
           v_cCupom = vdocum
           if val(StrTran(v_cPago,",")) > 999
              teste_val = StrTran(v_cPago,",")
           else
              teste_val = strzero(val(StrTran(v_cPago,",")),3)
           endif

            v_cTxt := "000-000 = CHQ"+CRLF+;
                      "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                      "002-000 = "+v_cCupom+CRLF+;
                      "003-000 = "+AllTrim(teste_val)+CRLF+;
                      "999-999 = 0"+CRLF

*                      "032-927 = teste"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTecBan(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA
               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0" .OR. Substr(v_cTxt,At("009-000",v_cTxt)+10,2) = 'P1'

                  if Substr(v_cTxt,At("028-000",v_cTxt)+10,1) # "0"
                     v_nLin   := Val( ExtMsgTecBan(v_cTxt,"028-000") )
                     v_cBuf := ""
                     FOR v_n = 1 TO v_nLin
                         v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                         sysrefresh()
                     NEXT

                     if nPgTEF > 1 .and. nCtTEF < nPgTEF// verifica se existe mais um cartao tef na mesma venda e faz backup do INTPOS.001 e Imprime.txt

                        nFileHandle := FCreate( cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        COPYFILE(cdirtecban+"\RESP\IntPos.001",cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP", .f.)

                        if fazTecBan("CNF",left(albx_formas[nCtTEF, 1],16),v_cPago)
                           DelMsgTef(cdirtecban)
                        endif

                        albx_formas[iformas, 14] := cdirtecban+"\imprime"+strzero(nCtTEF, 2)+".txt"
                        albx_formas[iformas, 15] := cdirtecban+"\IntPos"+strzero(nCtTEF, 2)+".TMP"

                     elseif nPgTEF > 1 .and. nCtTEF == nPgTEF // testa se é o ultimo cartao tef, se for não faz CNF e imprime cdc´s na ordem

                        nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                        albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"

                     else
                        nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                        FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                        FClose( nFileHandle )

                        albx_formas[iformas, 14] := cdirtecban+"\imprime.txt"
                        albx_formas[iformas, 15] := cdirtecban+"\IntPos.001"
                     endif

                  endif

                  v_nRet := 1
               ELSE
                  DelMsgTecBan( cdirtecban )
                  v_nRet := -21
               ENDIF
            ENDIF
            DelReqTecBan( cdirtecban )

     //FAZ CONFIRMAÇÃO DE VENDA EM CARTÃO
     ELSEIF v_cTipo = "CNF"

            nVezes := 1

            do while !ver_ativo(cdirtecban)
               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif
               sysrefresh()
            enddo

//            v_cCupom = vdocum
            v_cTxt   := MemoRead( cdirtecban+"\RESP\IntPos.001" )
            v_cCupom := ExtMsgTecBan(v_cTxt,"002-000")
            v_cNSU   := ExtMsgTecBan(v_cTxt,"012-000")
            v_cRede  := ExtMsgTecBan(v_cTxt,"010-000")
            v_cFinal := ExtMsgTecBan(v_cTxt,"027-000")
            v_cTxt   := "000-000 = CNF"+CRLF+;
                        "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                        "002-000 = "+v_cCupom+CRLF+;
                        "010-000 = "+v_cRede+ CRLF+;
                        "012-000 = "+v_cNSU +CRLF +;
                        "027-000 = "+v_cFinal+CRLF+;
                        "999-999 = 0"+CRLF

            IF .NOT. Empty(EnvCmdTecBan( v_cTxt ))
               v_nRet := 1
               DelMsgTecBan(cdirtecban)
            ENDIF
            DelReqTecBan( cdirtecban )

            if !_tefdedic
               liberatc()
            endif

     //CANCELA CUPOM TecBan NÃO IMPRESSO
     ELSEIF v_cTipo = "NCN"

            if !_tefdedic
               travatc()
               ltravatef := .t.
            endif

            nVezes := 1

            do while !ver_ativo(cdirtecban)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            v_cTxt   := MemoRead( cdirtecban+"\RESP\IntPos.001" )
            v_cNSU   := ExtMsgTecBan(v_cTxt,"012-000")
            v_cRede  := ExtMsgTecBan(v_cTxt,"010-000")
            v_cFinal := ExtMsgTecBan(v_cTxt,"027-000")
            v_cValor := ExtMsgTecBan(v_cTxt,"003-000")
            IF v_cTecBan = NIL
               v_cTecBan := ExtMsgTecBan(v_cTxt,"001-000")
            ENDIF
            v_cTxt   := "000-000 = NCN"+CRLF+;
                        "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                        "010-000 = "+v_cRede+ CRLF+;
                        "012-000 = "+v_cNSU +CRLF +;
                        "027-000 = "+v_cFinal+CRLF+;
                        "999-999 = 0"+CRLF

            if !_tefdedic
               liberatc()
            endif

            DEFINE DIALOG odlg_ncn RESOURCE "TefNCN" FONT fnt_ecf COLOR RGB(182,221,199), _CORPADRAO

                  REDEFINE SAY ID 30 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
                  REDEFINE SAY ID 31 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
                  REDEFINE SAY ID 32 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE
                  REDEFINE SAY osayTxtVal ID 33 color _CORPADTXT, _CORPADRAO of odlg_ncn UPDATE

                  REDEFINE SAY oSay[1] ID 301 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
                  REDEFINE SAY oSay[2] ID 302 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE
                  REDEFINE SAY oSay[3] ID 303 OF odlg_ncn COLOR COR_SAY,RGB(182,221,199) UPDATE

                  if upper(left(alltrim(v_cNsu),3)) <> 'ADM'
		     oSay[1]:SetText(v_cNSU)
		  endif

		  oSay[2]:SetText(v_cRede)
		  IF val(v_cvalor) <> 0
		     oSay[3]:SetText(Trans(Val(v_cValor)/100,"@E 999,999.99"))
                  else
                     osayTxtVal:SetText("")
                     osayTxtVal:refresh(.t.)
                     odlg_ncn:refresh(.t.)
		  ENDIF

                  REDEFINE BUTTON ID 101 of odlg_ncn action odlg_ncn:end() COLOR cor_btn CANCEL

                  odlg_ncn:lhelpicon:=.f.

            ACTIVATE DIALOG odlg_ncn CENTERED

            IF .NOT. Empty(EnvCmdTecBan( v_cTxt ))
               DelMsgTecBan(cdirtecban)
            ENDIF
            DelReqTecBan( cdirtecban )

     //FAZ ADM
     ELSEIF v_cTipo = "ADM"

            nVezes := 1

            do while !ver_ativo(cdirtecban)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            v_cTxt := "000-000 = ADM"+CRLF+;
                      "001-000 = "+horaStr(time())+CRLF+;   // "001-000 = "+v_cTecBan + CRLF+;
                      "999-999 = 0"+CRLF

            IF .NOT. Empty( v_cTxt:=EnvCmdTecBan( v_cTxt ) )
               v_nLin := Val( ExtMsgTecBan(v_cTxt,"028-000") )
               IF v_nLin > 0
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

                  if !_tefdedic
                     travatc()
                  endif

                  IF PrintTecBan( .T. ) = 1
                     if !_tefdedic
                        liberatc()
                     endif
                     fazTecBan("CNF",v_cTecBan,v_cPago)
                  ELSE
                     fazTecBan("NCN",v_cTecBan )
                  ENDIF
                  if !_tefdedic
                     liberatc()
                  endif
               ENDIF
               v_nRet := 1
               DelMsgTecBan( cdirtecban )
            ENDIF
            DelReqTecBan( cdirtecban )

     ELSEIF v_cTipo = "CNC"

            nVezes := 1

            do while !ver_ativo(cdirtecban)

               if !_tefdedic
                  if ltravatef
                     liberatc()
                  endif
               endif

               if _tefdedic
                  erro("Sem conexão SiTef","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               else
                  erro("Gerenciador TECBAN não ativo!","Atenção")
                  nVezes++
                  if nVezes >= 3
                     exit
                  endif
               endif

               if !_tefdedic
                  if ltravatef
                     travatc()
                  endif
               endif

               sysrefresh()

            enddo

            DelMsgTecBan( cdirtecban )

            v_cTXT := MemoRead( CintPos )
            v_cCupom   := ExtMsgTecBan(v_cTxt,"002-000")
            v_valort   := ExtMsgTecBan(v_cTxt,"003-000")
            v_cRede  := ExtMsgTecBan(v_cTxt,"010-000")
            v_cNSU   := ExtMsgTecBan(v_cTxt,"012-000")
            v_dtcmv  := ExtMsgTecBan(v_cTxt,"022-000")
            v_hrcmv  := ExtMsgTecBan(v_cTxt,"023-000")
//            v_cFinal := ExtMsgTef(v_cTxt,"027-000")

            v_cTxt   := "000-000 = CNC"+CRLF+;
                        "001-000 = "+horaStr(time())+CRLF+;     // colocar hora aqui nesse campo para nao gerar igual
                        "002-000 = "+v_cCupom+CRLF+;
                        "003-000 = "+AllTrim(v_valort)+CRLF+;
                        "010-000 = "+v_cRede+ CRLF+;
                        "012-000 = "+v_cNSU +CRLF +;
                        "022-000 = "+v_dtcmv+CRLF+;
                        "023-000 = "+v_hrcmv+CRLF+;
                        "999-999 = 0"+CRLF

//                        "027-000 = "+v_cFinal+CRLF+;

                     //"777-777 = teste Redecard"+CRLF+;    // Homologação   // se nao for necessario pra CNC retirar essas 2 linhas
                     //"777-777 = TESTE HIPERCARD"+CRLF+;    // Homologação

            IF .NOT. Empty( v_cTxt := EnvCmdTecBan(v_cTxt)  )

               //SE TRANSAÇÃO APROVADA

               nResp := .T.

               IF Substr(v_cTxt,At("009-000",v_cTxt)+10,1) = "0"
                  v_nLin   := Val( ExtMsgTecBan(v_cTxt,"028-000") )
                  v_cBuf := ""
                  FOR v_n = 1 TO v_nLin
                      v_cBuf += StrTran(ExtMsgTecBan( v_cTxt,"029-"+StrZero(v_n,3) )+CRLF,["])
                      sysrefresh()
                  NEXT

                  nFileHandle := FCreate( cdirtecban+"\imprime.txt", 0 )
                  FWrite( nFileHandle, v_cBuf, Len(v_cBuf) )
                  FClose( nFileHandle )

               ELSE
                  DelMsgTecBan( cdirtecban )
                  Ferase(cdirtecban+"\IntPos.res")
                  v_nRet := -21
                  nResp := .f.
               ENDIF

            ELSE
               if !_tefdedic
                  liberatc()
               endif
               DelMsgTecBan( cdirtecban )
               Ferase(cdirtecban+"\IntPos.res")
               v_nRet := -21
               nResp := .F.
            ENDIF

     ENDIF

  END SEQUENC

RETURN v_nRet

FUNCTION ExtMsgTecBan( cTxt,cCod )
LOCAL cLin

   if At(cCod,cTxt) == 0
      cLin := ""
   else
      cLin := Substr(cTxt,At(cCod,cTxt)+10)
      cLin := Left(cLin,At(CHR(13)+CHR(10),cLin)-1)
   endif

RETURN cLin


FUNCTION EnvCmdTecBan( cTxt )

LOCAL nQte:= 0,cRet,nLin, nFileHandle

   Ferase(cdirtecban+"\RESP\IntPos.Sts")
   nFileHandle := FCreate( cdirtecban+"\REQ\IntPos.tmp", 0 )
   FWrite( nFileHandle, cTxt, Len(cTxt) )
   FClose( nFileHandle )
   Frename(cdirtecban+"\REQ\IntPos.tmp",cdirtecban+"\REQ\IntPos.001")

   DO WHILE nQte < 4
      SysWait(2)
      IF File(cdirtecban+"\RESP\IntPos.Sts")
         EXIT
      ENDIF
      nQte++
      sysrefresh()
   ENDDO

   IF nQte >= 4

      if !_tefdedic
         if ltravatef
            liberatc()
         endif
      endif

      if _tefdedic
         erro("Sem conexão SiTef","Atenção")
      else
         erro("Gerenciador TECBAN não ativo!","Atenção")
      endif

      if !_tefdedic
         if ltravatef
            travatc()
         endif
      endif

      DelMsgTecBan( cdirtecban )

      RETURN ""
   ENDIF

   nQte := 0
   DO WHILE nQte < 300 .AND. .NOT. File(cdirtecban+"\RESP\IntPos.001")
      nQte++
      SysWait(2)
      sysrefresh()
   ENDDO

   IF nQte < 300
*      ok('Aguardando teste RedeCard...')  // Homologação
      cTxt := MemoRead( cdirtecban+"\RESP\IntPos.001" )
      cRet := ExtMsgTecBan(cTxt,"030-000")
      nLin := Val( ExtMsgTecBan(cTxt,"028-000") )

      IF nLin = 0 .AND. .NOT. Empty(cRet)

         if type("Wvar") == "U"

            if valtype(ownd_tef) == 'O'
               ownd_tef:refresh(.t.)
               ownd_tef:setfocus()
            endif
            ok(cRet,"Atenção")

            DelMsgTecBan( cdirtecban )

            Wvar := .t.
         endif

      ENDIF
   ELSE
      cTxt := ""
      erro("Gerenciador TECBAN não Responde !","Atenção")
      DelMsgTecBan( cdirtecban )
   ENDIF

RETURN cTxt


FUNCTION DelMsgTecBan( cDir )

   Ferase(cDir+"\RESP\IntPos.001")
   Ferase(cDir+"\RESP\IntPos.sts")
   Ferase(cDir+"\Imprime.txt")

RETURN NIL

FUNCTION DelReqTecBan( cDir )

   Ferase(cDir+"\REQ\IntPos.001")
   Ferase(cDir+"\REQ\IntPos.sts")

RETURN NIL



*****************************************************************************
* PROGRAMA: PrintTecBan.PRG                                                 *
* FUNCAO..: Imprime cupom TecBan.                                              *
*****************************************************************************
FUNCTION PrintTecBan( lErro, cForma, cValor)
LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg,cRet,nSecs:=Seconds(), var_imprimir

   IF File(cdirtecban+"\imprime.txt")

      CursorWait()

      oWnd_tef:SetFocus()
      oWnd_tef:Show()
      aRet := TecBan_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cdirtecban+"\RESP\IntPos.001" )
      IF .NOT. Empty( cMsg := ExtMsgTecBan(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cdirtecban+"\imprime.txt")

      //SE FOR IMPRESSÃO APÓS ERRO
      IF lErro

         lImpCCD := .f.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64

                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                     RETURN (PrintTecBan( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrintTecBan( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            else

               if _daruma .or. _sweda .or. _elgin // incluido em 05/02/2011

                  lAlgoAberto := .f.

                  if _daruma
                     cEstado := space(2)
                     if GrvCmdFis(41,,,@cEstado,,.f.)
                        cEstado := substr(cEstado, 1,1)
                        if cEstado # '0'
                           lAlgoAberto := .t.
                        endif
                     endif
                  else
                     if GrvCmdFis(41,,,,,.f.)
                        lAlgoAberto := .t.
                     endif
                  endif

                  if lAlgoAberto

                     if v_framework

                        cGerAbre := space(2)
                        nRet := DaMFDInf('56', @cGerAbre)
                        cGerAbre := alltrim(cGerAbre)

                        if cGerAbre == '4'

                           nQualComando := 25

                        else

                           nQualComando := 23

                        endif

                     else

                        nQualComando := 25

                     endif

                     IF GrvCmdFis(nQualComando,,,,,.F.)

                        sysrefresh()
                        syswait(1)

                        if !VFimPapel()
                           IF ErroPrinter(aRet)
                            RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              RETURN -22
                           ENDIF
                        endif

                        IF Empty(cMsg)
                           aRet[3]:SetText("Imprimindo Leitura X...")
                        ELSE
                           aRet[3]:SetText(cMsg)
                        ENDIF

                     ELSEIF ErroPrinter(aRet)
                        RETURN (PrintTecBan( .T., cForma, cValor ))
                     ELSE
                         RETURN -22
                     ENDIF
                  endif

               else

                  IF GrvCmdFis(25,,,,,.t.)

                     sysrefresh()
                     syswait(1)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                         RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                     RETURN (PrintTecBan( .T., cForma, cValor ))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            FOR n = 1 TO nviastef

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64
                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrintTecBan( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF
                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'

                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrintTecBan( .T., cForma, cValor ))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _daruma

                   if v_framework
                      daProgFormas("RG", 'TEF1', ';')
                      DaAbreGerMFD('TEF1')
                   endif

                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.f.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _elgin

                     lAlgoAberto := .f.
                     nStGerCDC := 0
                     E_StatExtMFD(@nStGerCDC)

                     if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                        lAlgoAberto := .t.
                     elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                        lAlgoAberto := .t.
                     endif

                     if !lAlgoAberto

                        cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                        cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                        E_AbreGerMFD(cIndGer)

                     endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10)
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)
                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF
                    if _urano
                       IF .NOT. GrvCmdFis(22, var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF
                    elseif _epson

                       IF !GrvCmdFis(22,var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else
                       if !GrvCmdFis(24, var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             RETURN -22
                          ENDIF
                       elseif !VFimPapel()
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             RETURN -22
                          ENDIF
                       ENDIF
                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. nviastef > 1
                   FOR k = 1 TO 10
                       if _urano
                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrintTecBan( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       elseif _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrintTecBan( .T., cForma, cValor ))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else
                          if !GrvCmdFis(24,Space(10),,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                             IF ErroPrinter(aRet)
                                RETURN (PrintTecBan( .T., cForma, cValor ))
                             ELSE
                                RETURN -22
                             ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()   // Alteracao Alex
                      SysWait(3)
                      aRet[3]:SetText(cMsg)
                      aRet[1]:SetFocus()   // Alteracao Alex
                   endif

                   sysrefresh()

               ENDIF

               sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma

      ELSE

         IF .NOT. GrvCmdFis(if(_daruma, 78, 21),{cForma,cValor,NIL},,,,.F.)
            IF ErroPrinter(aRet)
               RETURN (PrintTecBan( .T., cForma, cValor ))
            ELSE
               RETURN -22
            ENDIF
         elseif !VFimPapel()

             IF ErroPrinter(aRet)
               RETURN (PrintTecBan( .T., cForma, cValor ))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         FOR n = 1 TO nviasTef

             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()

             FOR k = 1 TO MlCount(cTxt)

                 if _urano .or. _epson
                    IF !GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF,,,,.F. )
                       IF ErroPrinter(aRet)
                          RETURN (PrintTecBan( .T., cForma, cValor ))
                       ELSE
                          GrvCmdFis(25,,,,,.F.)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                        IF ErroPrinter(aRet)
                           RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           GrvCmdFis(25,,,,,.F.)
                           RETURN -22
                        ENDIF

                    ENDIF
                 else
                    IF !GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF,,,,.F. )
                       IF ErroPrinter(aRet)
                          RETURN (PrintTecBan( .T., cForma, cValor ))
                       ELSE
                          RETURN -22
                       ENDIF
                    elseif !VFimPapel()
                        IF ErroPrinter(aRet)
                          RETURN (PrintTecBan( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                    ENDIF
                 endif

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviasTef > 1
                FOR k = 1 TO 10
                    if _urano .or. _epson
                       if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF
                       elseif !VFimPapel()
                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF
                       endif
                    else
                       if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                          IF ErroPrinter(aRet)
                             RETURN (PrintTecBan( .T., cForma, cValor ))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF
                       elseif !VFimPapel()
                           IF ErroPrinter(aRet)
                              RETURN (PrintTecBan( .T., cForma, cValor ))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF
                       endif

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[3]:SetText(cMsg)
                aRet[1]:SetFocus()   // Alteracao Alex

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
                IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
                ELSE
                   RETURN -22
                ENDIF
            endif

         else
            if !GrvCmdFis(88)
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrintTecBan( .T., cForma, cValor ))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif

      ENDIF

      aRet[1]:End() ; aRet[1] := NIL

   ELSE
      if !_tefdedic
         liberatc()
      endif
      ok("Não há cupons para imprimir !","Atenção")
   ENDIF

RETURN If(lRet,1,0)



STATIC FUNCTION ErroPrinter(aRet)
  local retorno

  aRet[1]:End() ; aRet[1] := NIL
  oWnd_tef:SetFocus()
  oWnd_tef:Show()

  if !_tefdedic
     liberatc()
  endif

  retorno = Continua("Impressora não Responde. Tentar imprimir novamente?","Confirmação")

  if !_tefdedic
     travatc()
  endif

  if retorno
     syswait(5)
  endif

RETURN retorno



STATIC FUNCTION TecBan_aguarde( cIcone,cMens )
LOCAL oDlgWait,oMtr,nPerc,oSay,oIco

   CursorWait()
   cIcone := If(cIcone=NIL,"I_AGUARDE",cIcone)

   DEFINE DIALOG oDlgWait RESOURCE "DLG_AGUARDE" FONT fnt_ecf COLOR cor_dlg

	REDEFINE ICON oIco NAME (cIcone)        ID 101 OF oDlgWait UPDATE
    REDEFINE SAY oSay                       ID 301 OF oDlgWait UPDATE
    REDEFINE METER oMtr VAR nPerc TOTAL 100 ID 901 OF oDlgWait UPDATE

        IF cMens # NIL
           oSay:SetText(cMens)
        ENDIF

    odlgWait:lhelpicon:=.f.
    ACTIVATE DIALOG oDlgWait CENTERED NOWAIT

RETURN {oDlgWait,oMtr,oSay}



*****************************************************************************
* PROGRAMA: GrvCmdTecBan.PRG                                                   *
* FUNCAO..: Envia comando para TecBan.                                         *
*                                                                           *
*****************************************************************************
FUNCTION GrvCmdTecBan(nCmd,uPar,lMens,lInic, nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

LOCAL nRet:=0,oIni,nTecBan:=0,cTecBan, kl, xki, aSoTEF, nQtVinc // aImprime,

default cCodForma := ''

  lMens := If(lMens=NIL,.T.,lMens)
  lInic := If(lInic=NIL,.T.,lInic)

  cTecBan := StrZero(val(vl_venda),10)

  //VER TEF ATIVO
  IF nCmd = 1
         nRet := BmTfStatus(cTecBan)

  //ACIONA TecBan ADM
  ELSEIF nCmd = 2
         nRet := fazTecBan("ADM",cTecBan,uPar )

  //VENDA CARTÃO
//  ELSEIF nCmd = 3
//         nRet := fazTecBan("CRT",cTecBan,uPar )

  ELSEIF nCmd = 3
         nRet := fazTecBan("CRT",cTecBan,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA DE CARTÃO
  ELSEIF nCmd = 4

         if !_tefdedic
            travatc()
            ltravatef := .t.
         endif

         nRet := PrintTecBan( .F.,uPar[1], uPar[2] )

         if !_tefdedic
            liberatc()
            ltravatef := .f.
         endif

  //CONFIRMA IMPRESSÃO DE TecBan
  ELSEIF nCmd = 5
         nRet := fazTecBan("CNF",cTecBan,uPar[1], nCtTEF, nPgTEF )

  //VENDA CHEQUE
  ELSEIF nCmd = 6
         nRet := fazTecBan("CHQ",cTecBan,uPar, nCtTEF, nPgTEF )

  //IMPRIME VENDA COM MULTIPLOS CARTOES
  ELSEIF nCmd = 7

         if 'INTPOS.001'$upper(cArq001)
            COPYFILE(cdirtecban+"\IntPos.001",cdirtecban+"\RESP\IntPos.001", .f.)
            ferase(cdirtecban+"\IntPos.001")
            cArq001 := cdirtecban+"\RESP\IntPos.001"
         endif

         nRet := PrtCupTefB( .F., albx_formas[nCtTEF, 4], albx_formas[nCtTEF, 2], nCtTEF, nPgTEF, nArqSeq, cArq001, cCodForma)

         if nRet == -44
            nRet := 1
         endif

  ENDIF

  oWnd_tef:Show()

  IF lMens

     IF nRet = 0
            erro("Gerenciador TECBAN Inativo !","Atenção")

     ELSEIF nRet = -2
            erro("Parâmetro inválido para comando "+Str(nCmd,2)+" !","Atenção")

     ELSEIF nRet = -18
            erro("Não foi possível abrir/criar arquivo .001 ou .sts !","Atenção")

     ELSEIF nRet = -19
            erro("Numero de Identificação não bate com o do arquivo .001 !","Atenção")

     ELSEIF nRet = -20
            erro("Transação finalizada pelo operador !","Atenção")

     ELSEIF nRet = -21
            erro("A transação não foi aprovada !","Atenção")

     ELSEIF nRet = -22
            erro("Não foi possível terminar a impressão !","Atenção")

     ELSEIF nRet = -23
            erro("Não foi possível terminar a operação !","Atenção")

     ELSE
            RETURN .T.
     ENDIF

  ELSEIF nRet = 1
     RETURN .T.
  ENDIF

RETURN .F.



*****************************************************************************
* PROGRAMA: PrtCupTefB.PRG                                                 *
* FUNCAO..: Imprime cupom TEF.                                              *
*                                                                           *
* AUTOR...: Vilian F. Arraes                                 EM: 01/05/2002 *
* ALTERADO: Sebastiao de Oliveira Pessoa                     EM: 09-20-2010 *
*****************************************************************************
FUNCTION PrtCupTefB( lErro, cForma, cValor, nki, nPgTEF, cNumArq, cArq001, cCodForma)

LOCAL n,k,cTxt,lRet:=.T.,aRet,cMsg := "",cRet,nSecs:=Seconds(), var_imprimir, aImprime, aImprime1, aImprime2

private nViasMC, nTefGP, nTefHp, nTefTc

   IF File(cNumArq)

      CursorWait()

      ownd_tef:SetFocus()
      ownd_tef:Show()

      aRet := TecBan_aguarde("I_IMPRESSORA")
      aRet[1]:show()
      aRet[1]:SetFocus()   // Alteracao Alex

      cRet := MemoRead( cArq001)

      IF .NOT. Empty( cMsg := ExtMsgTef(cRet,"030-000") )
         aRet[3]:SetText(cMsg)
         aRet[1]:Update()
      ENDIF

      cTxt := MemoRead(cNumArq)

      //SE FOR IMPRESSÃO APÓS ERRO
      IF lErro

         lGerImp := .t.
         lerroImp := .t.

         cTxt := ""

         nTefGP  := Directory(_cdirtef+"\imprime*.txt" )
         nTefHp  := Directory(cdirhiper+"\imprime*.txt" )
         nTefTc  := Directory(cdirtecban+"\imprime*.txt" )

         nViasMC := 0
         nViasMC := len(nTefGP) + len(nTefHp) + len(nTefTc)

         LeAposerro(@aImprime, @aImprime1, @aImprime2, @cTxt)

         lImpCCD := .f.

         if _bematech .or. _daruma .or. _sweda .or. _elgin .or. _urano .or. _epson

            CancCupTEF()

            aRet[3]:SetText("Verificando impressora...")

            if _urano

               nEstado := 0
               GrvCmdFis(78,,,@nEstado,,.F.)

               if nEstado == 32 .or. nEstado == 64
                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF )) // (PrtCupTefB( .T., cForma, cValor ))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            elseif _epson

               cInfDoc := space(56)
               GrvCmdFis(41,, 56, @cInfDoc,,.f.)

               cInfDoc := substr(cInfDoc, 1, 2)

               if cInfDoc == '02' .or. cInfDoc == '04'

                  IF GrvCmdFis(if(cInfDoc == '02', 88, 25),,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            else

               if _daruma .or. _sweda .or. _elgin // incluido em 05/02/2011

                  lAlgoAberto := .f.

                  if _daruma
                     cEstado := space(2)
                     if GrvCmdFis(41,,,@cEstado,,.f.)
                        cEstado := substr(cEstado, 1,1)
                        if cEstado # '0'
                           lAlgoAberto := .t.
                        endif
                     endif
                  else
                     if GrvCmdFis(41,,,,,.f.)
                        lAlgoAberto := .t.
                     endif
                  endif

                  if lAlgoAberto
                     if v_framework

                        cGerAbre := space(2)
                        nRet := DaMFDInf('56', @cGerAbre)
                        cGerAbre := alltrim(cGerAbre)

                        if cGerAbre == '4'

                           nQualComando := 25

                        else

                           nQualComando := 23

                        endif

                     else

                        nQualComando := 25

                     endif

                     IF GrvCmdFis(nQualComando,,,,,.F.)

                        if !VFimPapel()
                           IF ErroPrinter(aRet)
                              RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                           ELSE
                              RETURN -22
                           ENDIF
                        endif

                        IF Empty(cMsg)
                           aRet[3]:SetText("Imprimindo Leitura X...")
                        ELSE
                           aRet[3]:SetText(cMsg)
                        ENDIF

                     ELSEIF ErroPrinter(aRet)
                         RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                     ELSE
                         RETURN -22
                     ENDIF
                  endif

               else

                  IF GrvCmdFis(25,,,,,.F.)

                     if !VFimPapel()
                        IF ErroPrinter(aRet)
                           RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                        ELSE
                           RETURN -22
                        ENDIF
                     endif

                     IF Empty(cMsg)
                        aRet[3]:SetText("Imprimindo Leitura X...")
                     ELSE
                        aRet[3]:SetText(cMsg)
                     ENDIF

                  ELSEIF ErroPrinter(aRet)
                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                  ELSE
                      RETURN -22
                  ENDIF

               endif

            endif

            aRet[3]:SetText(cMsg)

            FOR n := 1 TO if(nViasMC == 1, nviastef, 1)

                if _urano

                   nEstado := 0
                   GrvCmdFis(78,,,@nEstado,,.F.)

                   if nEstado # 32 .and. nEstado # 64

                      IF !GrvCmdFis(24,,,'TEF1',,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _epson

                   cInfDoc := space(56)
                   GrvCmdFis(41,, 56, @cInfDoc,,.f.)

                   cInfDoc := substr(cInfDoc, 1, 2)

                   if cInfDoc # '02' .and. cInfDoc # '04'

                      cIndGer := ascan(aGerEpson, {|a| 'TEF1'$a[2] })
                      cIndGer := if(cIndGer == 0, '01', aGerEpson[cIndGer, 1])

                      IF !GrvCmdFis(24,,2,cIndGer,,.F.)
                         IF ErroPrinter(aRet)
                            RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                         ELSE
                            GrvCmdFis(25,,,,,.F.)
                            RETURN -22
                         ENDIF
                      ENDIF

                   endif

                elseif _daruma

                   if v_framework
                      daProgFormas("RG", 'TEF1', ';')
                      DaAbreGerMFD('TEF')
                   endif

                elseif _bematech .and. W_MFD == 1

                   if !GrvCmdFis(80,,,,,.f.)

                      cIndGer := ascan(aGerBema, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerBema[cIndGer, 1])

                      BmAbrGerNo(cIndGer)

                   endif

                elseif _elgin

                   lAlgoAberto := .f.
                   nStGerCDC := 0
                   E_StatExtMFD(@nStGerCDC)

                   if nStGerCDC = 4 // "Relatório Gerencial Aberto"
                      lAlgoAberto := .t.
                   elseif nStGerCDC = 2 // "Comprovante de Débito ou Crédito Aberto"
                      lAlgoAberto := .t.
                   endif

                   if !lAlgoAberto

                      cIndGer := ascan(aGerElgin, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerElgin[cIndGer, 1])

                      E_AbreGerMFD(cIndGer)

                   endif

                elseif _sweda

                   if !GrvCmdFis(83,,,,,.f.)

                      cIndGer := ascan(aGerSweda, {|a| 'TEF1'$a[2] })

                      cIndGer := if(cIndGer == 0, '01', aGerSweda[cIndGer, 1])

                      ecfAbreGer(cIndGer)

                   endif

                endif

                aRet[2]:SetTotal(MlCount(cTxt)*1.10) // reincluir
                aRet[1]:Update()

                FOR k = 1 TO MlCount(cTxt)

                    var_imprimir := Left(StrTran( Memoline(cTxt,50,k),CRLF),48)+CRLF

                    if _urano .or. _epson
                       IF !GrvCmdFis(22,var_imprimir)

                          if _urano

                             nEstado := 0
                             GrvCmdFis(78,,,@nEstado,,.F.)

                             if nEstado == 32 .or. nEstado == 64

                                IF ErroPrinter(aRet)
                                   RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             else

                                IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                   IF ErroPrinter(aRet)
                                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF
                                ENDIF

                             endif

                          else

                             IF ErroPrinter(aRet)
                                RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF

                          endif

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    else

                       IF .NOT. GrvCmdFis(24,var_imprimir)
                          IF ErroPrinter(aRet)
                             RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                          ELSE
                             GrvCmdFis(25,,,,,.F.)
                             RETURN -22
                          ENDIF

                       elseif !VFimPapel()

                           IF ErroPrinter(aRet)
                              RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                           ELSE
                              GrvCmdFis(25,,,,,.F.)
                              RETURN -22
                           ENDIF

                       ENDIF

                    endif

                    if valtype(aRet[1]) == 'O'
                       aRet[2]:Set(k)
                       aRet[1]:show()
                       aRet[1]:SetFocus()   // Alteracao Alex
                    endif

                    sysrefresh()

                NEXT

                IF n = 1 .and. if(nViasMC == 1, nviastef, 1) > 1
//                IF n = 1 .and. if(len(aImprime) == 1, nviastef, 1) > 1

                   FOR k = 1 TO 10

                       if _urano .or. _epson

                          if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )

                             if _urano

                                nEstado := 0
                                GrvCmdFis(78,,,@nEstado,,.F.)

                                if nEstado == 32 .or. nEstado == 64

                                   IF ErroPrinter(aRet)
                                      RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                   ELSE
                                      GrvCmdFis(25,,,,,.F.)
                                      RETURN -22
                                   ENDIF

                                else

                                   IF !GrvCmdFis(24,,,'TEF1',,.F.)
                                      IF ErroPrinter(aRet)
                                         RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                      ELSE
                                         GrvCmdFis(25,,,,,.F.)
                                         RETURN -22
                                      ENDIF
                                   ENDIF

                                endif

                             else

                                IF ErroPrinter(aRet)
                                   RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001 ))
                                ELSE
                                   GrvCmdFis(25,,,,,.F.)
                                   RETURN -22
                                ENDIF

                             endif

                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif

                       else
                          if .NOT. GrvCmdFis(24,Space(10)+CRLF,,,,.F. )
                             IF ErroPrinter(aRet)
                                RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                             ELSE
                                GrvCmdFis(25,,,,,.F.)
                                RETURN -22
                             ENDIF
                          elseif !VFimPapel()
                              IF ErroPrinter(aRet)
                                 RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                              ELSE
                                 GrvCmdFis(25,,,,,.F.)
                                 RETURN -22
                              ENDIF
                          endif
                       endif

                       sysrefresh()

                   NEXT

                   if _epson
                      EPSON_CortaPapel()
                   elseif _urano
                      GRVCMDFIS(80,,,,,.f.)
                   elseif _elgin
                      Elgin_Corta(1)
                   elseif _bematech
                      BmCortePapel(0)
                   elseif _sweda
                      ecfCorte(1)
                   elseif _daruma
                      DaGuilhotina("1")
                   endif

                   if valtype(aRet[1]) == 'O'
                      aRet[2]:Set(aRet[2]:nTotal)
                      SysWait(1)
                      aRet[3]:SetText("Destaque o comprovante.")
                      aRet[1]:show()
                      aRet[1]:SetFocus()
                      SysWait(3)
                      aRet[3]:SetText(cMsg)
                      aRet[1]:SetFocus()   // Alteracao Alex
                   endif

                ENDIF

                sysrefresh()

            NEXT

            if !GrvCmdFis(25,,,,,.F.)
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(25,,,,,.F.)
                  RETURN -22
               ENDIF
            endif

         endif  // Fim da Bematech / Daruma / Elgin / Sweda / Urano

         aRet[1]:End() ; aRet[1] := NIL

         RETURN -44

      ELSE

         IF .NOT. GrvCmdFis(if(_daruma, 78, 21),{cForma,cValor,NIL},,,,.F.)

            IF ErroPrinter(aRet)
               RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
            ELSE
               RETURN -22
            ENDIF

         elseif !VFimPapel()

             IF ErroPrinter(aRet)
                RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
             ELSE
                RETURN -22
             ENDIF

         ENDIF

         v_cTpTran := ExtMsgTef(cRet,"011-000")
         v_cRedeTr  := ExtMsgTef(cRet,"010-000")

         if _bematech
            if W_MFD == 1 .or. cModeloBm == 'MP25FI' .or. cModeloBm == 'MP50FI'
               cCooCDC := space(6)
               GrvCmdFis(14,, 6, @cCooCDC)
               aadd(aCOOCDC, { cCooCDC, cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr })
            endif
         else
            cCooCDC := space(6)
            GrvCmdFis(14,, 6, @cCooCDC)
            aadd(aCOOCDC, { cCooCDC, cForma, cValor, cCodForma, v_cTpTran, v_cRedeTr })
         endif

         FOR n = 1 TO nviastef
             aRet[2]:SetTotal(MlCount(cTxt)*1.1)
             aRet[1]:Update()
             FOR k = 1 TO MlCount(cTxt)

                 IF .NOT. GrvCmdFis(22,Left(StrTran( Memoline(cTxt,50,k), CRLF),48)+CRLF,,,,.F. )
                    IF ErroPrinter(aRet)
                       RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 elseif !VFimPapel()
                    IF ErroPrinter(aRet)
                       RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                    ELSE
                       GrvCmdFis(23)
                       RETURN -22
                    ENDIF
                 ENDIF

                 if valtype(aRet[1]) == 'O'
                    aRet[2]:Set(k)
                    aRet[1]:show()
                    aRet[1]:SetFocus()   // Alteracao Alex
                 endif

                 sysrefresh()

             NEXT

             IF n = 1 .and. nviastef > 1
                FOR k = 1 TO 10

                    if .NOT. GrvCmdFis(22,Space(10)+CRLF,,,,.F. )
                       IF ErroPrinter(aRet)
                          RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    elseif !VFimPapel()

                       IF ErroPrinter(aRet)
                          RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
                       ELSE
                          GrvCmdFis(23)
                          RETURN -22
                       ENDIF

                    endif

                    sysrefresh()

                NEXT

                if _epson
                   EPSON_CortaPapel()
                elseif _urano
                   GRVCMDFIS(80,,,,,.f.)
                elseif _elgin
                   Elgin_Corta(1)
                elseif _bematech
                   BmCortePapel(0)
                elseif _sweda
                   ecfCorte(1)
                elseif _daruma
                   DaGuilhotina("1")
                endif

                aRet[2]:Set(aRet[2]:nTotal)
                SysWait(1)
                aRet[3]:SetText("Destaque o comprovante.")
                SysWait(5)
                aRet[3]:SetText(cMsg)
                aRet[1]:SetFocus()   // Alteracao Alex

             ENDIF

             sysrefresh()

         NEXT

         if !_epson
            if !GrvCmdFis(23)
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         else
            if !GrvCmdFis(88)
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            elseif !VFimPapel()
               IF ErroPrinter(aRet)
                  RETURN (PrtCupTefB( .T., cForma, cValor, nki, nPgTEF, cNumArq, cArq001))
               ELSE
                  GrvCmdFis(23)
                  RETURN -22
               ENDIF
            endif
         endif
      ENDIF

      aRet[1]:End() ; aRet[1] := NIL

   ELSE
      if !_tefdedic
         liberatc()
      endif
      ok("Não há cupoms para imprimir !","Atenção")
   ENDIF

RETURN(If(lRet,1,0))

 

Obrigado amigo, 
Se puder me Conversar no Zap   (32) 98892 5094

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...