;** DRC ************************************************************************
;*  Demonstrativo de Resultado                                                 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 07/11/2017 por Bruno Carvalho              
;Ultima Alteracao: 13/10/2017 por Anivel Augusto Rodrigues 
;Motivo Alteracao: ajustes  
;Ultima Alteracao: 21/10/2017 por Anivel Augusto Rodrigues 
;Motivo Alteracao: ajustes  
;Ultima Alteracao: 15/12/2017 por Anivel Augusto Rodrigues 
;Motivo Alteracao: ajustes  
;Ultima Alteracao: 26/12/2017 por Anivel Augusto Rodrigues 
;Motivo Alteracao: ajustes  

;******************************************************************************
;* Rotina para Localizar Registro Carteira                                     *
;*******************************************************************************
Proc CHAVEC()
FACA_DIGITAC=True

While True
  Moveto [DRConta_Codigo]
  End
  DRCODIGO=[]
  If OPCAO_FORM="F"
     Then Down
     Else PgDn
  EndIf

  SetaCorRegistro("Editar")
  While True

  MSGCAMPO=""
  TECLA=""                     
  AguardaTeclasPadrao()

  Switch
      Case RetVal=-33 : SemFuncoes()
      Case RetVal=-94 : MsgTeclasPadrao()
      Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
           Del
           While True
             FACA_DIGITAC=True
             
             If OPCAO_FORM="F"
                Then PAGINA("DRC","MR")
                Else PAGINA("DRC","MT")
             EndIf
             DRCODIGO=[DRConta_Codigo]
             Switch
               Case EDITAR="S":
                    PickForm "1"
                    Echo Normal
                    Echo Off
                    CHAVE=0
                    Controla_EdicaoC()
               Case EXCLUIR="S":
                    PickForm "1"
                    Echo Normal
                    Echo Off
                    If QuerExcluir("")
                       Then 
                            if [DRConta_Operacao]="SPC"
                               then EXCLUSAO()
                               else FastMsg("ExclusÆo tipo de conta nÆo permitida")
                                    UnLockRecord
                            endif   
                       Else UnLockRecord
                    EndIf
               OtherWise: QuitLoop
             EndSwitch
             If OPCAO_FORM="F"
                Then PickForm "F"
                Else PickForm "1"
             EndIf
             SetaPosForm()
           EndWhile
           QuitLoop
      Case RetVal="F3":
           If OPCAO_FORM="F"
              Then OPCAO_FORM="1"
              Else OPCAO_FORM="F"
           EndIf
           Del
           CHAVE=5
           Return
      Case Retval="ESC":
           CHAVE=9
           Del
           Return
      Case Retval="ENTER":
           If IsBlank([])
              Then If IsBlank(DRCODIGO)
                      Then []=StrVal(1)
                      Else []=StrVal((NumVal(DRCODIGO)+1))
                   EndIf
           EndIf
           DRCODIGO=[]
           Del
           PickForm "1"
           
           Moveto "DRC"
           Locate DRCODIGO
           If Retval
              Then CHAVE=0
              Else CHAVE=1
                   End Pgdn
                   [DRConta_Codigo]=DRCODIGO
           EndIf
           If Not IsLockRecord()
              Then Echo Normal
                   Echo Off
                   Return
           EndIf
           If CHAVE=1
              Then Del
           EndIf
           If OPCAO_FORM="F"
              Then PickForm "F"
           EndIf
           QuitLoop
      EndSwitch
  EndWhile
EndWhile

EndProc ; (CHAVEC)

;*******************************************************************************
;* Rotina para Digitacao da Cabeca                                             *
;*******************************************************************************
Proc DIGITAC()
FACA_CHAVEI=True

Moveto "DRC"
Moveto "DRI" SetaCorRegistro("Padrao")
Moveto "DRC"

Moveto [DRConta_Descricao]

SetaCorRegistro("Editar")


if IsFixo()
   then
        return 
endif   


While True

  TECLA=" Esc=Retornar                                                    PgDn=Finalizar"
  MSGCAMPO=Field()
  Switch
    Case Field()="DRConta_Codigo"           : MSGCAMPO="C¢digo "
    Case Field()="DRConta_Descricao"        : MSGCAMPO="Descri‡„o"
    Case Field()="DRConta_Tipo"             : MSGCAMPO="Tipo: Anal¡tica, Sint‚tica"
    Case Field()="DRConta_Operacao"         : MSGCAMPO="Opera‡„o"
    Case Field()="DRConta_TemFormula"       : MSGCAMPO="Tem F¢rmula?"
    Case Field()="DRConta_Formula"          : MSGCAMPO="F¢rmula"
    Case Field()="DRConta_SomarPlanoConta"  : MSGCAMPO="Somar Plano de Conta?"
  EndSwitch
  
  Prompt TECLA
  Wait Field
     Until "F1","ESC","ENTER","DEL","FIELDVIEW","F35","DOWN","PGDN"

  Switch
  Case Retval="FIELDVIEW" or Retval="F35":
       EDITFIELD()
  Case RetVal="F1":
       ;If Field()=""
       ;   Then CONSULTAF1()
       ;EndIf
   Case RetVal="DOWN":
        If CHAVE = 0
           Then QuitLoop
           Else Loop
        EndIf
  Case Retval="DEL" And CHAVE=0:
        if [DRConta_Operacao]="SPC"
           then
                 If QuerExcluir("este demonstrativo de resultado?")
                    Then EXCLUSAO()
                         CHAVE=9
                         QuitLoop
                 EndIf
           else FastMsg("ExclusÆo tipo de conta nÆo permitida")
        endif   
  
  Case Retval="ESC":
       Switch
       Case Field()="DRConta_Descricao"       :
            If QuerDesfazer("")
               Then If CHAVE=1
                       Then Exclusao()
                    EndIf
                    FACA_CHAVEI =False
                    FACA_DIGITAC=False
                    CHAVE=9
                    QuitLoop
            EndIf
       Case Field()="DRConta_Tipo"            : Moveto [DRConta_Descricao] 
       Case Field()="DRConta_Operacao"        : Moveto [DRConta_Tipo]
       Case Field()="DRConta_TemFormula"      : Moveto [DRConta_Descricao]
       Case Field()="DRConta_EmNegrito"       : Moveto [DRConta_TemFormula]
       Case Field()="DRConta_SomarPlanoConta" : Moveto [DRConta_Formula]       
       OtherWise:
            Left
       EndSwitch
  Case Retval="ENTER":
       Switch
       Case Field()="DRConta_Descricao"       : 
            If isblank([DRConta_Descricao])
               Then fastmsg("Valor invalido !")
                    loop
            EndIf    
            if CHAVE=1
               Then [DRConta_Tipo           ]="A"
                    [DRConta_Operacao       ]="SPC"
                    [DRConta_IncluiSubContas]="S"
            endif   
            Moveto [DRConta_TemFormula]
       Case Field()="DRConta_Tipo"            :
            If Isblank([DRConta_Tipo])
               Then fastmsg("Valor invalido !")
                    loop
            EndIf
            
            Moveto [DRConta_TemFormula]
       Case Field()="DRConta_Operacao"        :
            If IsBlank([DRConta_Operacao])
               then [DRConta_Operacao]="SPC"
            EndIf   
            Moveto [DRConta_TemFormula]
            
       Case Field()="DRConta_TemFormula"      :
            If IsBlank([DRConta_TemFormula])
               then [DRConta_TemFormula]="N"
            EndIf   
            Moveto [DRConta_EmNegrito]
       Case Field()="DRConta_EmNegrito"      :
            If IsBlank([DRConta_EmNegrito])
               then [DRConta_EmNegrito]="N"
            EndIf
            QuitLoop
             
       OtherWise:
            Right
       EndSwitch
  EndSwitch
EndWhile

EndProc ; (DIGITAC)

;*******************************************************************************
;* Rotina para Localizar Item                                                  *
;*******************************************************************************
Proc CHAVEI()

While True
  Moveto "DRI"
  Moveto [DRConta_Sequencia]
  End
  NITEM=[DRConta_Sequencia]
  Down
  Moveto [DRConta_Sequencia]

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

  SetaCorRegistro("Editar")
  While True
    TECLA=" F5=Formula                                                      PgDn=Finalizar"
    MSGCAMPO=""
    
    Prompt TECLA
    Wait Field
         Until "ESC","ENTER","F3","F7","HOME","UP","PGUP","PGDN","F9","F6",-33,"F5"          
     
    Switch    
    Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
         Del
         While True
           PAGINA("DRI","MR")
           If EDITAR="S"
              Then CHAVEI=0
                   NITEM   =[DRConta_Sequencia]
                   Controla_EdicaoI()               
              Else If EXCLUIR="S"
                      Then If QuerExcluir("")
                              Then Del
                              Else UnLockRecord
                           EndIf
                      Else QuitLoop
                   EndIf
           EndIf
           SetaPosForm()
         EndWhile
         QuitLoop
    Case Retval="F5":
         Del
         if [DRC->DRConta_TemFormula]="S" or [DRC->DRConta_Operacao]="CF"
            then EditaFormula()
         endif   
         quitloop
    Case Retval="PGDN":
         Del
         FACA_CHAVEI =False
         FACA_DIGITAC=False
         CHAVEI=9
         Return  
    Case Retval="ENTER":
    
         If [DRC->DRConta_IncluiSubContas]<>"S" 
            Then fastmsg("InclusÆo nÆo permitida !")
                 Del
                 QuitLoop
         EndIf
    
         If Not Seta_Sequencia(NITEM,True)   
            Then Del
                 QuitLoop
         EndIf
         NITEM=[]
         Del
         Locate DRCODIGO,NITEM
         If Retval
            Then CHAVEI=0
            Else CHAVEI=1
                 End Down
                 [DRConta_Codigo   ]=DRCODIGO
                 [DRConta_Sequencia]=NITEM
                 UnLockRecord
                 Locate DRCODIGO,NITEM
         EndIf
         Return
    EndSwitch
  EndWhile
EndWhile

EndProc  ;ChaveI()

;*******************************************************************************
;* Rotina para Digitacao dos Itens                                             *
;*******************************************************************************
Proc DIGITAI()
Private vrcampo, titok, regdestinat, msg

Moveto [Codigo Plano_Conta]

While True
  TECLA=" Esc=Retornar  "
  MSGCAMPO=""
  Switch
    Case Field()="Codigo Plano_Conta"     : MSGCAMPO="Plano de Conta"
    Case Field()="IncluirSubContas"       : MSGCAMPO="Incluir Sub-Contas? Sim/N„o"
    Case Field()="VisualizarConta"        : MSGCAMPO="Visualizar Conta? Sim/N„o"  
  EndSwitch
  
  If Field()="Codigo Plano_Conta"
     Then MSGCAMPO=MSGCAMPO+", [F1]Consultar Plano de Contas"
  EndIf   
  

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

  Switch
    Case RetVal="F1":  
         If Field()="Codigo Plano_Conta"
            Then ConsultaF1()
         EndIf         
    Case Retval="FIELDVIEW" or Retval="F35":
         EDITFIELD()
    Case Retval="ESC":                        
         Switch
           Case Field()="Codigo Plano_Conta" :
                If CHAVEI=1
                   Then Del
                EndIf
                QuitLoop
           Case Field()="IncluirSubContas" : Moveto [Codigo Plano_Conta]     
           OtherWise: Left
         EndSwitch
    Case Retval="ENTER":
         If IsBlank([]) Then Loop EndIf
         Switch
           Case Field() = "Codigo Plano_Conta" : Moveto [IncluirSubContas]
           Case Field() = "IncluirSubContas"   : Moveto [VisualizarConta]
           Case Field() = "VisualizarConta"    : UnLockRecord
                                                 QuitLoop 
           OtherWise: Right
         EndSwitch      
  EndSwitch
EndWhile
                 
EndProc ;DIGITAI()

;*******************************************************************************
;* Exclusao de Lote                                                            *
;*******************************************************************************
Proc EXCLUSAO()

Aguarde("Processando exclusÆo")

CHAVE=8
Moveto "DRI"
While RecordStatus("New")=False
  Del
EndWhile

Moveto "DRC"
UnLockRecord
Del

LimpAguarde()

EndProc; (Exclusao)


;*******************************************************************************
;*                                                                             *
;*******************************************************************************
Proc IsFixo()
private retorno

retorno=false

switch
  case [DRC->DRConta_Operacao]="CF"   : retorno=true
  case [DRC->DRConta_Operacao]="CMV"  : retorno=true
  case [DRC->DRConta_Operacao]="CNO"  : retorno=true
  case [DRC->DRConta_Operacao]="CTF"  : retorno=true
  case [DRC->DRConta_Operacao]="CTV"  : retorno=true
  case [DRC->DRConta_Operacao]="DV"   : retorno=true
  case [DRC->DRConta_Operacao]="MC"   : retorno=true
  case [DRC->DRConta_Operacao]="PEG"  : retorno=true
  case [DRC->DRConta_Operacao]="PEO"  : retorno=true
  case [DRC->DRConta_Operacao]="RL"   : retorno=true
  case [DRC->DRConta_Operacao]="RO"   : retorno=true
  case [DRC->DRConta_Operacao]="VB"   : retorno=true
  case [DRC->DRConta_Operacao]="VL"   : retorno=true
  case [DRC->DRConta_Operacao]="CCT"  : retorno=true

  case [DRC->DRConta_Operacao]="RC"   : retorno=true
  case [DRC->DRConta_Operacao]="ROP"  : retorno=true
  case [DRC->DRConta_Operacao]="RNO"  : retorno=true
endswitch

return retorno
EndProc; (IsFixo)



;*******************************************************************************
;*                                                                             *
;*******************************************************************************
Proc EditaFormula()
private ntab,campo

ntab  =table()
ncampo=field()

Moveto "DRC"
Edita_Memo("DRConta_Formula",09,01,13,76,"Formula")

moveto ntab
moveto field ncampo
EndProc; (IsFixo)

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoI()
DIGITAI()
Moveto "DRI"
If RecordStatus("Locked") Then UnLockRecord EndIf         
EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()
While FACA_DIGITAC
  DIGITAC()
  SetaCorRegistro("Padrao")

  If CHAVE=9 Then QuitLoop EndIf
  
  While FACA_CHAVEI
    Moveto "DRI"
    CHAVEI()
    If CHAVEI=9 Then QuitLoop EndIf
    Controla_EdicaoI()
  EndWhile
  Moveto "DRC"
  
EndWhile
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"
SYS_VALOR()

OPCAO_FORM = "F"

CoEdit "DRC"
PickForm OPCAO_FORM

While True
  CHAVEC()
  If CHAVE=9 Then QuitLoop EndIf
  If CHAVE=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
