;**  CEST **********************************************************************
;*  Tabela CEST                                                                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 16/11/2015 por Kamila Gomes
;Ultima Alteracao: 07/06/2018 por Bruno Carvalho
;Motivo Alteracao: Adicionado/ajustado Grava_LogEx() p/ gravar log de erros ao excluir registros                          

;*******************************************************************************
;* Rotina para Localizar Registro                                              *
;*******************************************************************************
Proc CHAVE()
Moveto [CEST]
End Down                                   
While true
  SetaCorRegistro("Editar")

  TECLA=""
  AguardaTeclasPadrao()
  
 Switch
    Case RetVal=-33 : SemFuncoes()
    Case RetVal="F3": SemFicha()
    Case RetVal=-94 : MsgTeclasPadrao()
    Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
         While True
           PAGINA("CEST","MR")
           If EDITAR="S"
              Then CHAVE=0
                   Controla_EdicaoC()
              Else If EXCLUIR="S"
                      Then If QuerExcluir("")
                              Then Grava_LogEx("CEST")
                                   DEL
                              Else UnLockRecord
                           EndIf
                      Else QuitLoop
                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         End Down
    Case Retval="ESC":
         CHAVE=9
         Del
         Quitloop
    Case RetVal="ENTER":
         If IsBlank([]) Then Loop EndIf
         CODIGO=[]
         Del
         Locate CODIGO
         If Retval
            Then CHAVE=0
            Else CHAVE=1
                 End Down
                 [CEST]=CODIGO
         EndIf
         If IsLockRecord()
              Then End Down
                   Moveto [CEST]
                   Loop
           EndIf
           QuitLoop
    EndSwitch
  ;EndWhile
EndWhile

EndProc  ;(Chave)

;*******************************************************************************
;* Rotina para Digitacao das Informacoes                                       *
;*******************************************************************************
Proc DIGITA()

Moveto [Descricao CEST]
While True
      
      TECLA=" Esc=Retornar  "

      If CHAVE=0
         Then TECLA=TECLA+"Del=Excluir  "
      EndIf

      Prompt TECLA
      SetaCorRegistro("Editando")
      Wait Field
           Until "ESC","ENTER","DEL","FIELDVIEW","F35"

      Switch
               
      Case Retval="ESC":
           If Field()="Descricao CEST"
              Then If QuerDesfazer("")
                      Then If CHAVE=1
                              Then DEL
                              Else Undo
                           Endif
                           CHAVE=9
                           QuitLoop
                   Endif
              Else Left
           EndIf
      Case Retval="ENTER":
           If Field() = "Descricao CEST"
              Then QuitLoop
              Else Right
           EndIf
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case Retval="DEL":
           If CHAVE=0
              Then If QuerExcluir("")
                      Then Grava_LogEx("CEST")
                           DEL
                           QuitLoop
                   EndIf
           EndIf
      EndSwitch
EndWhile
EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
DIGITA()
If RecordStatus("Locked") Then UnLockRecord EndIf         
EndProc

;*************************************************
;* Rotina Principal                              *
;*************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show"
CoEdit "CEST"
PickForm "F"
While True
  CHAVE()
  If CHAVE=9
     Then QuitLoop
  EndIf
  Controla_EdicaoC()
EndWhile
Do_It!
ClearAll
Release Vars All
Release Procs All
