;** UsRegras********************************************************************
;*  Manute‡Æo regras por usuariosa                                             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido em : 18/10/2019 por Anivel Augusto Rodrigues
;Ultima Alteracao: 07/11/2019 por Anivel Augusto Rodrigues 
;Motivo Alteracao: Ajustes Desabilitar()

;*******************************************************************************
;* Rotina para Localizar Registro Cabeca                                       *
;*******************************************************************************
Proc CHAVEC()
While True
  Moveto [CodigoDaRegra]
  End
  CODIGO=[CodigoDaRegra]
  If OPCAO_FORM="F"
   Then Down
   Else CtrlPgDn
  EndIf
  Moveto [CodigoDaRegra]
  
  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("Usregrac","MR")
                Else PAGINA("Usregrac","MT")
             EndIf
             CODIGO=[CodigoDaRegra]
             If EDITAR="S"
                Then PickForm "1"
                     CHAVEC=0
                     Controla_EdicaoC()
                Else if EXCLUIR="S"
                        then UnLockRecord
                             fastmsg("ExclusÆo nÆo permitida !")
                        else quitloop
                     endif        
             EndIf
             SetaPosForm()
           EndWhile
           QuitLoop
      Case Retval="ESC":
           CHAVEC=9
           Del
           Return
      Case Retval="ENTER":
           if isblank([]) then fastmsg("InclusÆo nÆo permitida !") quitloop endif

           CODIGO=[]
           Del
           Locate CODIGO
           If Retval
              Then CHAVEC=0
              Else CHAVEC=1
                   fastmsg("InclusÆo nÆo permitida !")
                   quitloop
           EndIf
           If not IsLockRecord()
              Then PickForm "1"
                   return
           EndIf
           quitloop
      EndSwitch
  EndWhile
EndWhile
EndProc  ; (ChaveC)

;*******************************************************************************
;* Rotina para Digitacao da Cabeca                                             *
;*******************************************************************************
Proc DIGITAC()
SetaCorRegistro("Editando")

Moveto [RegraAtiva]
While True

      Tecla = " Esc=Retornar  "
      MSGCAMPO=Field()

      Switch
        Case Field()="RegraAtiva"                 : MSGCAMPO="Regra Ativa ? (Sim/N„o)"
      EndSwitch

      Prompt TECLA
      Wait Field
           Until "F1","ESC","ENTER","FIELDVIEW","F35"

      Switch
      Case Retval="ESC":
           Switch
           Case Field()="RegraAtiva":
                If QuerDesfazer("")
                   Then Undo
                        CHAVEC=9
                        QuitLoop
                EndIf
           Otherwise:
                Left
           EndSwitch
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case Retval="ENTER":
           Switch
           Case Field()="RegraAtiva":
                If IsBlank([])
                   Then []="N"
                EndIf
                if []="N"
                   then CHAVEC=9
                endif   
                
                QuitLoop
           EndSwitch
      EndSwitch
EndWhile
EndProc   ; (DigitaC)

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

  SetaCorRegistro("Editar")
  While True

    TECLA= " Esc=Retornar  PgDn=Finalizar  "

    MSGCAMPO=Field()

    Switch
      Case Field()="Chapa"                 : MSGCAMPO="Usu rio, [F1]Consultar tabela"
      Case Field()="Codigo da Empresa"     : MSGCAMPO="Empresa, [F1]Consultar tabela, Em Branco = TODAS"
    EndSwitch

    Prompt TECLA
    Wait Field
         Until "ESC","ENTER","F7","HOME","PGDN","UP","F1"

    Switch
      Case RetVal="F1":
           If Field()="Chapa"                    Or
              Field()="Codigo da Empresa"
              Then CONSULTAF1()
           endif   

    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("Usregrai","MR")
           CHAPA  =[Chapa            ]
           EMPRESA=[Codigo da Empresa]
           If EDITAR="S"
              Then CHAVEI=0
                   Controla_EdicaoI()
              Else If EXCLUIR="S"
                      Then CONFIRMA()
                           If OK="S"
                              Then Del
                              Else UnLockRecord
                           EndIf
                      Else QuitLoop
                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         QuitLoop
    Case Retval="ENTER":
      switch   
        case field()="Chapa"            :           
          CHAPA=[]
          if isblank([])
             then loop
          endif
          if [Usregrac->RegraPorEmpresa]="S"
             then moveto [Codigo da Empresa]
                  loop
             else
                  EMPRESA=""
          endif         

        case field()="Codigo da Empresa":           
          EMPRESA=[]
       
      endswitch    

       Del
       Locate CODIGO,CHAPA,EMPRESA
       If Retval
          Then CHAVEI=0
          Else CHAVEI=1
               End
               Down
               [CodigoDaRegra    ]=CODIGO
               [Chapa            ]=CHAPA
               [Codigo da Empresa]=EMPRESA
               UnLockRecord
               Locate CODIGO,CHAPA,EMPRESA
       EndIf
       Return
    
    EndSwitch
  EndWhile
EndWhile
EndProc   ; (ChaveI)

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc Desabilitar()
switch
  case [Usregrac->CodigoDaRegra]="CD0031" : [Incluir]="-" [Deletar]="-"
  case [Usregrac->CodigoDaRegra]="CD0032" : [Incluir]="-" [Deletar]="-"
  case [Usregrac->CodigoDaRegra]="CD0033" : [Incluir]="-" [Deletar]="-"
    
endswitch

endproc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc Validar(Campo,vr)
private campo, vr

result=true
switch
  case [Usregrac->CodigoDaRegra]="CD0031" : 
    [Incluir]="-" [Deletar]="-"
    
endswitch

if [Usregrac->RegraComSupervisor]<>"S"
   then
        if vr="S" 
           then fastmsg("op‡Æo nÆo permitida !") 
                moveto field campo 
                []=""
                result=false
        endif
endif    

return result
endproc




;*******************************************************************************
;* Rotina para Digitacao das Informacoes                                       *
;*******************************************************************************
Proc DIGITAI()
Desabilitar()

Moveto [Incluir]
While True

      TECLA=" Esc=Retornar  "

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

      MSGCAMPO=Field()+" - X=Marcar"

      Prompt TECLA
      Wait Field
           Until "ESC","ENTER","DEL","FIELDVIEW","F35"

      Switch
      Case Retval="ESC":
           If Field()="Incluir"
              Then If QuerDesfazer("")
                      Then If CHAVEI=1
                              Then Del
                              Else If RecordStatus("Modified")
                                      Then Undo
                                   EndIf
                           EndIf
                           CHAVEI=9
                           QuitLoop
                   EndIf
              Else Left
           EndIf
      Case Retval="ENTER":
           if not Validar(Field(),[]) then loop endif

           switch
             case field()="Ver" : quitloop
             otherwise          : Right
           endswitch
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case Retval="DEL":
           If CHAVEI=0
              Then If QuerExcluir("")
                      Then DEL
                           QuitLoop
                   EndIf
           EndIf
      EndSwitch
EndWhile
SetaCorRegistro("Padrao")

EndProc  ; (DigitaI)

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

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

Moveto "Usregrai"
If RecordStatus("Locked") Then UnLockRecord EndIf         
EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
DIGITAC()
SetaCorRegistro("Padrao")
While CHAVEC<>9
  Moveto "Usregrai"
  CHAVEI()
  If CHAVEI=9 Or
     CHAVEI=8
     Then QuitLoop
  EndIf
  Controla_EdicaoI()
EndWhile
SetaCorRegistro("Padrao")

Moveto "Usregrac"
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"

If Checa_Supervisor()
   then 
        CoEdit "Usregrac"
        PickForm "1"
        SetaCorRegistro("Padrao")
        Moveto "Usregrai" SetaCorRegistro("Padrao")
        Moveto "Usregrap" SetaCorRegistro("Padrao")
        
        Moveto "Usregrac"
        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
endif
Release Vars All
Release Procs All
