;* TpContra*********************************************************************
;*                Cadastro de Tipos de Contrato                                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido  em: 01/11/2016 por Rafael Matos 
;Ultima Alteracao: 16/04/2021 por Bruno Carvalho
;Motivo Alteracao: Adicionado biblioteca "Bib_Bloq" 

;*******************************************************************************
;* Rotina para Localizar Registro                                              *
;*******************************************************************************
Proc CHAVE()
While True
  Moveto [Codigo_TpContrato]
  End
  COD=[Codigo_TpContrato]
  If OPCAO_FORM="F"
     Then Down
     Else PgDn
  EndIf
  Moveto [Codigo_TpContrato]

  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
           CHAVE=5
           Return
      Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
           While True
             If OPCAO_FORM="F"
                Then PAGINA("TpContra","MR")
                Else PAGINA("TpContra","MT")
             EndIf
             If EDITAR="S"
                Then COD=[Codigo_TpContrato]
                     CHAVE=0
                     PickForm "1"
                     Controla_EdicaoC()
                Else If EXCLUIR="S"
                        Then If QuerExcluir("")
                                Then PickForm "1"
                                     del
                                Else UnLockRecord
                             EndIf
                             If OPCAO_FORM="F"
                                Then PickForm "F"
                                Else PickForm "1"
                             EndIf
                        Else QuitLoop
                     EndIf
             EndIf
             SetaPosForm()
           EndWhile
           QuitLoop
      Case Retval="ESC":
           CHAVE=9
           Del
           Return
      Case RetVal="ENTER":
           If Numero_Lancamento("","F")
              Then COD = PROXLANCTO_NLANC
                   Release Vars PROXLANCTO_NLANC 
              Else QuitLoop
           EndIf

           PickForm "1"
           Moveto [Codigo_TpContrato]
           If PROXIMO_LANCTO_REG_ACHOU
              Then CHAVE=0
              Else CHAVE=1
           EndIf
           Return 
      EndSwitch
  EndWhile
EndWhile

EndProc  ; (CHAVE)

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

Moveto [Descricao_TpContrato]
While True

      TECLA=" Esc=Retornar  "
      If Field()="Forma de Pagamento"  
         Then TECLA=TECLA+"F1=Tabelas  "
      EndIf

      If CHAVE=0
         Then TECLA=TECLA+"Del=Excluir  PgDn=Finalizar  "
         Else TECLA=TECLA+"PgDn=Finalizar  "
      EndIf

      Prompt TECLA

      MSGCAMPO=""
      Switch
        Case Field()="Descricao_TpContrato"        : MSGCAMPO="Descri‡Æo do Tipo de Contrato"      
        Case Field()="Valor_TpContrato"            : MSGCAMPO="Valor do Tipo de Contrato"
        Case Field()="Forma de Pagamento"          : MSGCAMPO="Forma de Pagamento do Tipo de Contrato"
        Case Field()="TpContrato_Inativo"          : MSGCAMPO="Tipo de Contrato Inativo? Sim/NÆo"
      EndSwitch                                     

      Wait Field
           Until "ESC","ENTER","DEL","FIELDVIEW","F35","F1","PGDN"

      Switch
      Case Retval="F1":   
           If Field()="Forma de Pagamento" 
              Then ConsultaF1()
           EndIf
           
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case RetVal="DEL" :
           If CHAVE=0
              Then If QuerExcluir("")
                      Then del
                           QuitLoop
                   EndIf
           EndIf
      Case Retval="PGDN":
           If IsBlank([Descricao_TpContrato]) Or
              IsBlank    ([Valor_TpContrato]) Or
              IsBlank  ([Forma de Pagamento]) Or
              IsBlank    ([TpContrato_Inativo])
              Then Loop
              Else CHAVE=9
                   QuitLoop
           EndIf        
      Case Retval="ESC":
           Switch
              Case Field()="Descricao_TpContrato"         :   
                   If QuerDesfazer("")
                      Then If CHAVE=1
                              Then Del
                              Else If RecordStatus("Modified")
                                      Then Undo
                                   EndIf
                           EndIf
                           CHAVE=9
                           QuitLoop
                   EndIf
              Case Field()="Valor_TpContrato"         : Moveto [Descricao_TpContrato] 
              Case Field()="Forma de Pagamento"       : Moveto [Valor_TpContrato]
              Case Field()="TpContrato_Inativo"       : Moveto [Forma de Pagamento]
              OtherWise                               : Left
           EndSwitch
      Case Retval="ENTER":
           Switch
              Case Field()="Descricao_TpContrato"         :  
                   If IsBlank([Descricao_TpContrato])
                      Then Loop
                      Else Moveto [Valor_TpContrato]
                   EndIf
              Case Field()="Valor_TpContrato"               : 
                   If IsBlank([Valor_TpContrato])
                      Then Loop
                      Else Moveto [Forma de Pagamento]
                   EndIf 
              Case Field()="Forma de Pagamento"             : 
                   If IsBlank([Forma de Pagamento])
                      Then Loop
                      Else Moveto [TpContrato_Inativo]
                   EndIf
              Case Field()="TpContrato_Inativo"             : 
                   If IsBlank([TpContrato_Inativo])
                      Then [TpContrato_Inativo] = "N" 
                   EndIf
                   Moveto [Descricao_TpContrato]                       
              OtherWise                               : Right
           EndSwitch
      EndSwitch
EndWhile
EndProc

;*******************************************************************************
;* Rotina para Checagem do C¢digo                                              *
;*******************************************************************************
Proc Checa_Codigo(COD)
Private COD

If Not IsBlank(Cod)
   Then Cod=AlinhO(Cod)
   Else COD="0" 
EndIf  

If []="" Or COD="0"
   Then COD=Novo_Codigo(AlinhM(COD,3))
EndIf   

Return COD

EndProc ;Checa_Codigo 

;******************************************************************************
;** Rotina Alinhamento Especial Codigo Destinat Letras e Numeros            ***
;******************************************************************************
proc AlinhO(Vr)
Private Vr

If Len(vr)<3 And SubStr(vr,1,1)>"9"
   Then vr=SubStr(vr,1,1)+AlinhM(SubStr(vr,2,2),2)     
   Else vr=AlinhM(vr,3)			   
EndIf	  
Return Vr

EndProc ;AlinhO()

;*******************************************************************************
;* Rotina para Criar Novo C¢digo                                               *
;*******************************************************************************
Proc Novo_Codigo(COD)
Private COD

Vr=SubStr(COD,1,1)
If vr>"9" or COD="999"
   Then If SubStr(COD,2,2)="99"
	         Then Switch
					        Case vr="9" : COD="A00"
					        Case vr="Z" : Mensagem("Fim de codigo disponivel !")
                                COD=""
					        OtherWise   : COD=Chr(asc(vr)+1)+"00"
					      EndSwitch
					 Else cod=vr+AlinhM(StrVal(NumVal(SubStr(cod,2,2))+1),2)     
				EndIf	      
   Else cod=AlinhM(StrVal(NumVal(cod)+1),3)
EndIf

Return COD 

EndProc;Checa_Codigo()

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
DIGITA()
Moveto "TpContra"
If RecordStatus("Locked") Then UnLockRecord EndIf         
If OPCAO_FORM="F"
   Then PickForm "F"
EndIf
EndProc

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

SYS_VALOR()
CoEdit "TpContra"
PickForm "F"
OPCAO_FORM="F"
While True
  CHAVE()
  If CHAVE=9
     Then QuitLoop
  EndIf
  If CHAVE=5
     Then If OPCAO_FORM="1"
             Then PickForm "1"
             Else PickForm "F"
          EndIf
          Loop
  EndIf
  Controla_EdicaoC()
EndWhile
Do_It!
ClearAll
Release Vars All
Release Procs All
