;** PS_CPG *********************************************************************
;*  PlugSales - condi‡äes de pagamento                                         *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 10/07/2019 por Anivel Augusto Rodrigues               
;Ultima Alteracao:   /  /     por     
;Motivo Alteracao: 

;*******************************************************************************
;* Rotina para Localizar Registro                                              *
;*******************************************************************************
Proc CHAVE()

While True
  SetaCorRegistro("Editar")
  Moveto [CPG_ID]
  End
  SEQ = [CPG_ID]
  Down

  While True
    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("PS_CPG","MR")
           If EDITAR="S"
              Then CHAVE=0
                   CPG_ID=[CPG_ID]
                   Controla_EdicaoC()
              Else If EXCLUIR="S"
                      Then If QuerExcluir("")
                              Then Grava_LogEx("PS_CPG")
                                   Del
                              Else UnLockRecord
                                   Return False
                           EndIf
                      Else QuitLoop
                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         QuitLoop
    Case RetVal="ESC":
         CHAVE=9
         Del
         Return         
    Case RetVal="ENTER":
         If Not Seta_Sequencia(SEQ,True)
            Then Del
                 QuitLoop
         EndIf
         CPG_ID=[]
         Del
         Moveto [CPG_ID]
         Locate CPG_ID
         If RetVal
            Then CHAVE=0
            Else CHAVE=1
                 End Down
                 [CPG_ID]=CPG_ID
         EndIf
         If IsLockRecord()
            Then If CHAVE=1 Then Del EndIf
                 QuitLoop
         EndIf
         Return  
    EndSwitch
  EndWhile
EndWhile

EndProc ;CHAVE()

;*******************************************************************************
;* Rotina para digitacao das informacoes                                       *
;*******************************************************************************
Proc DIGITA()

if CHAVE=1
  then moveto [FPG_ID           ]
  else moveto [CPG_DESCRICAO    ]
endif
While True
      TECLA=" Esc=Retornar  "
      If CHAVE=0
         Then TECLA=TECLA+"Alt+F=Fun‡Æo  "
      EndIf
      If Field()="FPG_ID"            Or 
         Field()="Condicao de Pagamento"    
         Then TECLA=TECLA+"F1=Tabela  "
      EndIf
      
      MSGCAMPO=""
      Prompt TECLA

      MSGCAMPO=Field()
      Switch
        Case Field()="FPG_ID"                   : MSGCAMPO="Forma de Pagamento"
        Case Field()="CPG_DESCRICAO"            : MSGCAMPO="Descri‡Æo no APP"
      EndSwitch   

      Wait Field
           MESSAGE MSGCAMPO
           Until "ESC","ENTER","DEL","FIELDVIEW","F35","F3","F4","F1","PGDN",-33

      Switch    
      Case RetVal=-33 :   
           ShowPopup " Fun‡Æo " CENTERED
            "[~F3~] Ativa/Inativa" : " Ativa/Inativa a opera‡Æo"         : "F3",
            Separator,
            "~C~ancelar          " : " Cancelar"                         : "CANCELAR"
           EndMenu
           To MenuVar
           Echo Off
      
           If MenuVar="CANCELAR" or MenuVar="Esc"
               Then Retval=""    
           EndIf
           
           Switch
             Case MenuVar="F3" : Ativa_Inativa()
                                 If CHAVE=9 Then QuitLoop EnDIf                    
           EndSwitch
           
      Case RetVal="F1":
           If Field()="FPG_ID"            Or 
              Field()="Condicao de Pagamento"    
              Then ConsultaF1()
           EndIf           
      Case RetVal="F3":
           Ativa_Inativa()
           If CHAVE=9 Then QuitLoop EnDIf           
      Case RetVal="ESC":   
           Switch
               Case Field()="FPG_ID" :
                    If QuerDesfazer("")
                       Then If CHAVE=1
                              Then Del
                              Else Undo
                            EndIf
                            CHAVE=9
                            QuitLoop
                    EndIf  
               Case Field()="Condicao de Pagamento" : moveto [FPG_ID]  
               Case Field()="CPG_DESCRICAO" : 
                 if CHAVE=1
                    then moveto [Condicao de Pagamento]
                    else
                          If QuerDesfazer("")
                             Then Undo
                                  CHAVE=9
                                  QuitLoop
                          EndIf  
                 endif   
            OtherWise : Left
           EndSwitch
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case RetVal="ENTER":
           Switch  
              Case Field()="FPG_ID"                   :
                   If Not IsBlank([]) 
                      Then Moveto [Condicao de Pagamento]
                   EndIf
              Case Field()="Condicao de Pagamento"    :
                   If Not IsBlank([]) 
                      Then Moveto [CPG_DESCRICAO]
                   EndIf
              Case Field()="CPG_DESCRICAO"            :
                   If Not IsBlank([]) 
                      Then CHAVE=9
                           QuitLoop
                   EndIf      
           EndSwitch
      EndSwitch
EndWhile

EndProc ;DIGITA()

;*******************************************************************************
;* Rotina para Ativar/Inativar a Opera‡Æo                                     *
;*******************************************************************************
Proc Ativa_Inativa()
Private INATIVO,MSG

Switch
  Case IsBlank([CPG_Inativo]) Or [CPG_Inativo]="N" : INATIVO = "S"
  Case [CPG_Inativo]="S"                           : INATIVO = "N"
EndSwitch

If INATIVO="S"
   Then MSG = "Deseja INATIVAR a opera‡Æo referente a forma de pagamento "+[CPG_DESCRICAO]+"?"
   Else MSG = "Deseja ATIVAR a opera‡Æo referente a forma de pagamento "+[CPG_DESCRICAO]+"?"
EndIf
    
If ConfGeral(MSG)
   Then [CPG_Inativo] = INATIVO
EndIf

Switch
  Case IsBlank([FPG_ID           ])        : Moveto [FPG_ID                  ]
  Case IsBlank([Condicao de Pagamento])    : Moveto [Condicao de Pagamento   ]
  Case IsBlank([CPG_DESCRICAO         ])   : Moveto [CPG_DESCRICAO           ]
  OtherWise                                : CHAVE=9
EndSwitch                                         

EndProc ;Ativa_Inativa()

;*******************************************************************************
;* 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 "PS_CPG"
PickForm "F"
While True
  CHAVE()
  If CHAVE=9
     Then QuitLoop
  EndIf
  Controla_EdicaoC()
EndWhile
Do_It!
ClearAll

Release Vars All
Release Procs All
