;** Rota ***********************************************************************
;*  Digitacao de Rotas p/ cobran‡a                                             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido em : 26/07/2004 por Jose Luis Ameno da Silva
;Ultima Alteracao: 23/05/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Inclusao Controla_Edicao
;Ultima Alteracao: 24/12/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Seta_Sequencia  
;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 Cabeca                                       *
;*******************************************************************************
Proc CHAVEC()
While True
   Moveto [Codigo da Rota]
   End
   CODIGO=[Codigo da Rota]
   If OPCAO_FORM="F"
     Then Down
     Else PgDn
   EndIf
   Moveto [Codigo da Rota]

   SetaCorRegistro("Editar")
   While True

  TECLA=""
  AguardaTeclasPadrao()

    Switch
      Case RetVal=-33 : SemFuncoes()
      Case RetVal=-94 : MsgTeclasPadrao()
      Case RetVal="F3":
           If OPCAO_FORM="F"
              Then OPCAO_FORM="1"
              Else OPCAO_FORM="F"
           EndIf
           Del
           CHAVEC=5
           Return
      Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
           While True
             If OPCAO_FORM="F"
                Then PAGINA("Rota","MR")
                Else PAGINA("Rota","MT")
             EndIf
             CODIGO=[Codigo da Rota]
             If EDITAR="S"
                Then PickForm "1"
                     CHAVEC=0
                     Controla_EdicaoC()
                Else If EXCLUIR="S"
                        Then If QuerExcluir("desta rota")
                                Then PickForm "1"
                                     EXCLUSAO("Cabeca")
                                Else UnLockRecord
                             EndIf
                             If OPCAO_FORM="1"
                                Then PickForm "1"
                                Else PickForm "F"
                             EndIf
                        Else QuitLoop
                     EndIf
             EndIf
             SetaPosForm()
           EndWhile
           QuitLoop
      Case Retval="ESC":
           CHAVEC=9
           Del
           Return
      Case Retval="ENTER":

           If IsBlank([])
              Then If IsBlank(CODIGO)
                      Then CODIGO="1"
                      Else CODIGO=StrVal(NumVal(CODIGO)+1)
                   EndIf
                   []=CODIGO
           EndIf
           []=ALINH()
           CODIGO=[]
           Del
           PickForm "1"
           Locate CODIGO
           If Retval
              Then CHAVEC=0
              Else CHAVEC=1
                   End Pgdn
                   Moveto [Codigo da Rota]
                   []=CODIGO
           EndIf
           If IsLockRecord()
              Then If OPCAO_FORM="F"
                      Then PickForm "F"
                           QuitLoop
                   EndIf
           EndIf
           Return
      EndSwitch
   EndWhile
EndWhile
EndProc  ; (ChaveC)

;*******************************************************************************
;* Rotina para Digitacao da Cabeca                                             *
;*******************************************************************************
Proc DIGITAC()
Moveto [Descricao da Rota]
While True

      TECLA=" Esc=Retornar  "

      If CHAVEC=0
         Then TECLA=TECLA+"F8/=Cidades  Del=Excluir  "
      EndIf

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

      Switch
      Case RetVal="F8" Or RetVal="DOWN":
           If CHAVEC=0
              Then QuitLoop
           EndIf
      Case Retval="ESC":
           Switch
           Case Field()="Descricao da Rota":
                If QuerDesfazer("")
                   Then If CHAVEC=1
                           Then Del
                           Else Undo
                        EndIf
                        CHAVEC=9
                        QuitLoop
                EndIf
           Otherwise:
                Left
           EndSwitch
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case Retval="DEL":
           If CHAVEC=0
              Then If QuerExcluir("desta rota")
                      Then EXCLUSAO("Cabeca")
                           CHAVEC=9
                           QuitLoop
                   EndIf
           EndIf
      Case Retval="ENTER":
           Switch
           Case Field()="Descricao da Rota":
                If IsBlank([])
                   Then Loop
                EndIf
                QuitLoop
           EndSwitch
      EndSwitch
EndWhile
EndProc   ; (DigitaC)

;*******************************************************************************
;* Rotina para Localizar Ordem                                                 *
;*******************************************************************************
Proc ChaveI()
While True
  Moveto [Ordem]
  End
  NITEM = [Ordem]
  Down
  Moveto [Ordem]

  Seta_Sequencia(NITEM,False)
  If IsLockRecord()
     Then Del
          Loop
  EndIf

  SetaCorRegistro("Editar")
  While True

    TECLA= " Esc=Retornar  PgDn=Finalizar  "

    Prompt TECLA

    Wait Field
         Until "ESC","ENTER","F7","HOME","PGDN","UP"

    Switch
    Case RetVal="ESC":
         Del
         CHAVEI=9
         Return
    Case RetVal="PGDN":
         Del
         CHAVEI=8
         Return
    Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
         Del
         While True
           PAGINA("RotaCid","MR")
           NITEM=[Ordem]
           If EDITAR="S"
              Then CHAVEI=0
                   Controla_EdicaoI()
              Else If EXCLUIR="S"
                      Then CONFIRMA()
                           If OK="S"
                              Then Exclusao("Item")
                              Else UnLockRecord
                           EndIf
                      Else QuitLoop

                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         QuitLoop
    Case Retval="ENTER":
         If Not Seta_Sequencia(NITEM,True)
            Then Del
                 QuitLoop
         EndIf
         NITEM=[]
         Del
         Locate CODIGO,NITEM
         If Retval
            Then CHAVEI=0
                 NITEM=[]
            Else CHAVEI=1
                 End
                 Down
                 [Codigo da Rota ]=CODIGO
                 [Ordem          ]=NITEM
                 UnLockRecord
                 Locate CODIGO,NITEM
         EndIf
         Return
    EndSwitch
  EndWhile
EndWhile
EndProc   ; (ChaveI)

;*******************************************************************************
;* Rotina para Digitacao das Informacoes                                       *
;*******************************************************************************
Proc DIGITAI()
Moveto [UF]
While True

      TECLA=" Esc=Retornar  "

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

      If Field()="Cidade"                   Or
         Field()="UF"
         Then Tecla=Tecla+"F1=Tabelas  "
      EndIf

      MSGCAMPO=Field()

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

      Switch
      Case RetVal="F1":
           If Field()="Cidade"                   Or
              Field()="UF"
              Then CONSULTAF1()
           EndIf
      Case Retval="ESC":
           If Field()="UF"
              Then If CHAVEI=1
                      Then Del
                      Else If RecordStatus("Modified")
                              Then Undo
                           EndIf
                   EndIf
                   CHAVEI=9
                   QuitLoop
              Else Left
           EndIf
      Case Retval="ENTER":
           If IsBlank([])
              Then Loop
           EndIf
           If Field() = "Cidade"
              Then QuitLoop
              Else Right
           EndIf
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case Retval="DEL":
           If CHAVEI=0
              Then If QuerExcluir("")
                      Then DEL
                           QuitLoop
                   EndIf
           EndIf
      EndSwitch
EndWhile
EndProc  ; (DigitaI)

;*******************************************************************************
;* Exclusao de Itens                                                           *
;*******************************************************************************
Proc EXCLUSAO(TIPO)
Private TIPO
Switch
Case TIPO="Cabeca":
     Moveto "Rotacid"
     While RecordStatus("New")=False
       Del
     EndWhile
     Moveto "Rota"
     Del
Case TIPO="Item":
     Moveto "Rotacid"
     Del
EndSwitch
EndProc  ; (Exclusao)

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoI()
DIGITAI()
SetaCorRegistro("Padrao")
EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
DIGITAC()
SetaCorRegistro("Padrao")
While CHAVEC<>9
  Moveto "RotaCid"
  CHAVEI()
  If CHAVEI=9 Or
     CHAVEI=8
     Then QuitLoop
  EndIf
  Controla_EdicaoI()
EndWhile
Moveto "Rota"
If RecordStatus("Locked") Then UnLockRecord EndIf         
If OPCAO_FORM="F"
   Then PickForm "F"
   Else PickForm "1"
EndIf
EndProc

;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bibliot,\\Sistemas\\Cadastro\\Bib_Show"
CoEdit "Rota"
PickForm "F"
OPCAO_FORM="F"
While True
  CHAVEC()
  If CHAVEC=9
     Then QuitLoop
  EndIf
  If CHAVEC=5
     Then If OPCAO_FORM="F"
             Then PickForm "F"
             Else PickForm "1"
          EndIf
          Loop
  EndIf
  Controla_EdicaoC()
EndWhile
Do_It!
ClearAll
Release Vars All
Release Procs All
