;** RE_0040 ********************************************************************
;*  Emissao de Envelopes de Representantes                                        *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 17/04/2003 por Karen Lauar Nery
;Ultima Alteracao: 03/12/2004 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes
;Alterado em     : 10/07/2006 por Antonio Marcelo Alves
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 13/08/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Deleta_Arquivo
;Ultima Alteracao: 23/05/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Inclusao Controla_Edicao
;Ultima Alteracao: 29/12/2010 por Bruno Carvalho
;Motivo Alteracao: Ajustes Novo Layout de Teclas
;Ultima Alteracao: 01/01/2013 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes SetaCorRegistro("Editando") 

;*******************************************************************************
;* Rotina para Localizar Registro                                              *
;*******************************************************************************
Proc CHAVE()
While True
  End
  Down Down
  Moveto [Codigo do Representante]

  SetaCorRegistro("Editando")
  While True

  TECLA="F1=Tabelas"
  AguardaTeclasPadrao()

    If RetVal=-33
    Then Echo Normal
         Echo Off
         ShowPopup " Fun‡”es " CENTERED
          "~I~mprimir [F6]  ": " Selecionar/Imprimir etiqueta     " :"F6",
          "~L~impar   [F5]  ": " Limpar a lista corrente          " :"F5",
          "~S~ele‡„o  [F4]  ": " Selecionar dados                 " :"F4",
          "~C~ancelar       ": " Cancelar a Opera‡„o              " : "CANCELAR"
         EndMenu
         To MenuVar
         Echo Off
         If MenuVar="CANCELAR" or MenuVar="Esc"
            Then Loop
        EndIf                                         
        RetVal=MenuVar
  EndIf

  Switch
    Case RetVal="F3": SemFicha()
    Case RetVal=-94 : MsgTeclasPadrao()
    Case RetVal="F5":
         ConfGeral("Confirma a exclus„o dos registro desta relac„o ?")
         If OK="S"
             Then Limpar()
         EndIf
         QuitLoop
    Case RetVal="F4":
         Del
         Selecao()
         QuitLoop
    Case RetVal="F6":
         Del
         Do_it!
         If Not IsEmpty("Re_0020")
            Then DISPOSITIVO()
                 If OK="" Then Quitloop EndIf
                 ASP1=""
                 ASP2=""
                 ARQIMP = DIREPORT+"Envelope.TXT"
                 Switch
                       Case OK="A": ARQIMP = DIREPORT+ARQUIVO+".TXT"
                       Case OK="V": ASP1="\""
                                    ASP2="\""
                EndSwitch
                Deleta_Arquivo(ARQIMP)
                AGUARDE("Imprimindo")
                Imprimir()
                If OK="V"
                   Then Tabela=Cria_Temp(PrivDir())
                        {Tools} {Copy} {Table} {Rm_tx250} TypeIn Tabela Enter ClearAll
                        {Tools} {ExportImport} {Import} {ASCII} {AppendDelimited} TypeIn ARQIMP Enter
                        TypeIn Tabela Enter ClearAll
                        OPCAOPAG="Todas"
                        IMPRIREL(Tabela,"R")
                        Deleta_Arquivo(ARQIMP)
                        Delete Tabela
                   Else Mensagem("Impress„o Concluida !")
                        If OK="I"
                           Then ;---- Imprime Arquivo Impressao ------------------------------------------------
                                ImpriArq(ARQIMP)
                        EndIf
                        ;Printer Off
                        ;Close Printer
                EndIf
               EndIf
         ClearAll
         CoEdit "RE_0020"
         PickForm "1"
         QuitLoop
    Case RetVal="F1":
         If Field()="Codigo do Representante"
            Then If Not CONSULTAF1()
                    Then Loop
                 EndIf   
         EndIf
    Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
         While True
           PAGINA("RE_0020","MR")
           If EDITAR="S"
              Then CHAVE=0
                   UnLockRecord
              Else If EXCLUIR="S"
                      Then If QuerExcluir("")
                              Then DEL
                              Else UnLockRecord
                           EndIf
                      Else QuitLoop
                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         QuitLoop
    Case Retval="ESC":
         CHAVE=9
         Del
         Return
    Case RetVal="ENTER":
         If IsBlank([])
            Then Loop
         EndIf
         []=Alinh()
         CODIGO=[]
         Echo Normal
         Del
         Locate CODIGO
         Echo Off
         If Retval
            Then Mensagem("Codigo j  relacionado !")
            Else CHAVE=1
                 End Down
                 [Codigo do Representante]=CODIGO
         EndIf
         Quitloop
    EndSwitch
  EndWhile
EndWhile
EndProc
;*******************************************************************************
;* Rotina Limpar Registros                                                     *
;*******************************************************************************
Proc Limpar()
Aguarde("Aguarde, Excluindo registros")
Do_It!
Empty("RE_0020")
CoEditKey
PickForm "1"
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = 9: Temp=False
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            ;** Selecao **
            Case Campo="TG_CIDADE"         : Campo="NO"
            ;** Imprimir **
            Case Campo="TG_LINHAS"     : Campo="NO"
            Case Campo="TG_COLUNAS"    : Campo="TG_LINHAS"
            Case Campo="YES" Or Campo="NO" :
                 If OP_SHOW="S"
                    Then Campo="TG_TIPO"
                    Else Campo="TG_LINHAS"
                 EndIf
          EndSwitch

     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or 
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    ;** Selecao **
                    Case Campo="TG_CIDADE"   : Campo="YES"
                         If Upper(VSDGCIDADE)="TODAS"
                            Then CIDADE=""
                            Else CIDADE=VSDGCIDADE
                         EndIf
                    ;** Imprimir **
                    Case Campo="TG_LINHAS"     :
                        VSDGLINHAS=ControlValue("TG_LINHAS")
                        If IsBlank(VSDGLINHAS)
                           Then VSDGLINHAS=0
                        EndIf
                        Campo="TG_COLUNAS"
                    Case Campo="TG_COLUNAS"    :
                        VSDGCOLUNAS=ControlValue("TG_COLUNAS")
                        If IsBlank(VSDGCOLUNAS)
                           Then VSDGCOLUNAS=0
                        EndIf
                        Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_LINHAS":
     MSG_CAMPO="Digite o n§ de linhas a saltar para que se inicie a impressÆo"
  Case Campo="TG_COLUNAS":
     MSG_CAMPO="Digite o n§ de colunas a saltar para que se inicie a impressÆo"
EndSwitch

Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* Rotina Filtrar Registros                                                    *
;*******************************************************************************
Proc Selecao()
LimpAguarde()
Do_It!
ClearAll
Echo Normal
Echo Off

VSDGCIDADE   =""

OP_SHOW="S"
ShowDialog "Sele‡„o de Registros a Incluir" Proc "SD_Dialog"
Trigger "OPEN","ARRIVE"
Key "F1","ENTER","ESC","TAB",
    -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140
 @ 01,01 Height 07 Width 45

  Label  @ 00,00 "Cidade"  For "TG_CIDADE"
  Accept @ 01,01 Width 23 "A20"  Picture "!*!" Tag "TG_CIDADE"  To VSDGCIDADE

  Pushbutton @ 03,10 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
  Pushbutton @ 03,24 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

EndDialog
If RetVal
   Then LimpaRodape(0)
        Aguarde("Aguarde, Selecionando registros")
        ClearAll

        {Ask} {Represen}
        Moveto [Codigo do Representante] Check
        Moveto [Cidade Represent       ] Check TypeIn CIDADE
        Do_It!
        ClearAll

        {Ask} {Re_0020}
        Moveto [Codigo do Representante] Example "a1"
        {Ask} {Answer} TypeIn "Fast Delete"
        Moveto [Codigo do Representante] Example "a1"
        Do_It!
        ClearAll

        If IsEmpty("Answer")
           Then Mensagem("N„o exite registro conforme sele‡„o !")
        EndIf

EndIf

ClearAll
CoEdit "RE_0020"
PickForm "1"
EndProc

;*******************************************************************************
;* Rotina Impressao                                                            *
;*******************************************************************************
Proc Imprimir()
LimpaRodape(0)
Do_It!
ClearAll

While True

  OP_SHOW="I"
  ShowDialog " Imprimir " Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
    -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140
  @ 01,01 Height 07 Width 27

   Label  @ 00,00 "Linhas"  For "TG_LINHAS"
   Accept @ 01,01 Width 7 "S"  Tag "TG_LINHAS" To VSDGLINHAS

   Label  @ 00,10 "Colunas"  For "TG_COLUNAS"
   Accept @ 01,11 Width 7 "S"  Tag "TG_COLUNAS" To VSDGCOLUNAS

   Pushbutton @ 03,00 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
   Pushbutton @ 03,12 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  View "Re_0020"
  scan
  i=1
  While True
   If (i>=VSDGLINHAS)
      Then QuitLoop
      Else Print File ARQIMP ASP1+ASP2+"\n"
           i=i+1
   EndIf
  EndWhile

  Print File ARQIMP ASP1+Spaces(VSDGCOLUNAS+1)+Format("W39,AL",[Razao Social Represent])+ASP2+"\n"
  Print File ARQIMP ASP1+Spaces(VSDGCOLUNAS+1)+"AT.:"+Format("W35,AL",[Contato Represent])+ASP2+"\n"
  Print File ARQIMP ASP1+Spaces(VSDGCOLUNAS+5)+Format("W35,AL",[Endereco Represent])+ASP2+"\n"
  Print File ARQIMP ASP1+Spaces(VSDGCOLUNAS+5)+Format("W35,AL",[CEP Represent]+"  "+[Bairro Represent])+ASP2+"\n"
  Print File ARQIMP ASP1+Spaces(VSDGCOLUNAS+5)+Format("W35,AL",[Cidade Represent]+"/"+[UF Represent])+ASP2+"\n"
  Print File ARQIMP ASP1+"\f"+ASP2+"\n"

 EndScan
 Quitloop

EndWhile

ClearAll

EndProc

;************************************
;* ROTINA PARA LIMPAR AGUARDE       *
;************************************
Proc LimpAguarde2L()
@ 23,0 ?? Fill(" ",80)
PaintCanvas Attribute 31 23,0,23,79
@ 24,0 ?? Fill(" ",80)
PaintCanvas Attribute 31 24,0,24,79
EndProc ; (LimpAguarde2L)

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show"

VSDGOPRELCODALFDIG    =""
VSDGNOMOPRELCODALFDIG =""
VSDGOPRELLISTCAD      =""
VSDGNOMOPRELLISTCAD   =""

CoEdit "RE_0020"
PickForm "1"
While True
  CHAVE()
  If CHAVE=9 Then QuitLoop EndIf
EndWhile
Do_It!
ClearAll
Release Vars All
Release Procs All
