;** Listtit ********************************************************************
;*  Manutencao de Cadastro de comunicados                                      *
;*********************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 03/04/2019 por Anivel Augusto Rodrigues
;Ultima Alteracao:   /  /     por 
;Motivo Alteracao:                      

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

while true
  End
  SEQ=[Sequencia]
  If OPCAO_FORM="F"
    Then Down
    Else CtrlPgDn
  EndIf
  Moveto [Sequencia]
  
  SetaCorRegistro("Editar")
  While true
  
    TECLA=""
    MSGCAMPO=Field()
    If Field()="CodigoTitulo"
       Then MSGCAMPO=MSGCAMPO+", F1=Tabelas  "
    EndIf
    
    AguardaTeclasPadrao()

    If RetVal=-33
       Then Echo Normal
            Echo Off
            ShowPopup " Fun‡”es " CENTERED
             "F5  ~L~impar             " : " Limpar lista                            " : "F5",
             "F6  ~I~mprimir           " : " Imprimir lista                          " : "F6",
             Separator,
             "~C~ancelar               " : " Cancelar a Opera‡„o                     " : "CANCELAR"
            EndMenu
            To MenuVar
            Echo Off
            If MenuVar="CANCELAR" or MenuVar="Esc"
                Then Loop
            EndIf
            RetVal=MenuVar
    EndIf

    Switch
        Case RetVal="F3":
             If OPCAO_FORM="F"
                Then OPCAO_FORM="1"
                Else OPCAO_FORM="F"
             EndIf
             Del
             CHAVEC=5
             Return
        Case RetVal="F5":
             Del
             ConfGeral("Confirma a exclus„o dos registro desta relac„o ?")
             If OK="S"
                 Then Limpar()
             EndIf
             QuitLoop
        Case RetVal="F6":
             Del
             SelecaoImprimir()
             QuitLoop
             
        Case RetVal=-94 : MsgTeclasPadrao()
        Case RetVal="F7" Or RetVal="UP" Or RetVal="PGUP" Or RetVal="HOME":
             While True
               If OPCAO_FORM="F"
                  Then PAGINA("ListNotC","MR")
                  Else PAGINA("ListNotC","MT")
               EndIf
               SEQ=[Sequencia]

               If EDITAR="S"
                  Then CHAVEC=0
                       COD=[Sequencia]
                       PickForm "1"
                       Controla_EdicaoC()
                 Else If EXCLUIR="S"
                          Then If QuerExcluir("")
                                  Then PickForm "1"
                                       EXCLUSAO()
                                  Else UnLockRecord
                               EndIf
                          else
                               quitloop     
                       EndIf
               EndIf
               If OPCAO_FORM="F"
                  Then PickForm "F"
                  Else PickForm "1"
               EndIf
               SetaPosForm()
             EndWhile
             quitloop
        Case Retval="ESC":
             CHAVEC=9
             Del
             return
        Case RetVal="ENTER":
           COD=[]   
           Del
           If IsBlank(COD)
              Then COD=SEQ+1
           EndIf                       

           PickForm "1"
           Locate COD
           If Retval
              Then CHAVEC=0
              Else CHAVEC=1
                   End CtrlPgDn
                   [Sequencia]=COD
                   [Data     ]=Today()
                   Controle_Dia()
           EndIf
           If not IsLockRecord()
              Then return
           EndIf
  
           If OPCAO_FORM="F"
              Then PickForm "F"
           EndIf
           QuitLoop
        EndSwitch
  EndWhile
endwhile
EndProc  ; (CHAVE)

;*******************************************************************************
;* Rotina Limpar Registros                                                     *
;*******************************************************************************
Proc Limpar()
Aguarde("Aguarde, Excluindo registros")
PickForm "1"

While RecordStatus("New")=False

  Moveto "ListNotI"
  While RecordStatus("New")=False
    Del
  EndWhile

  Moveto "ListNotC"
  Del

EndWhile

PickForm OPCAO_FORM
EndProc

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

Moveto [CodigoTitulo]
While True

      TECLA=" Esc=Retornar  "
      If CHAVEC=0
         Then TECLA=TECLA+"Del=Excluir  "
      EndIf

      MSGCAMPO=Field()
      If Field()="CodigoTitulo"
         Then MSGCAMPO="C¢digo do T¡tulo, F1=Tabelas  "
      EndIf

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

      Switch

      Case RetVal="F1":
           If Field()="CodigoTitulo"
              Then CONSULTAF1()
           EndIf
      Case Retval="ESC":
           Switch
             Case Field()="CodigoTitulo":
               If QuerDesfazer("")
                  Then If CHAVEC=1
                          Then EXCLUSAO()
                          Else Undo
                       EndIf
                       CHAVEC=9
                       QuitLoop
               EndIf
             Case Field()="Data": moveto [CodigoTitulo]
             Otherwise: left 
           EndSwitch
      Case Retval="ENTER":
           Switch
             Case Field()="CodigoTitulo":
                if isblank([]) then fastmsg("Valor inv lido !") loop endif
                moveto [Data] 
               
             Case Field()="Data":
                if isblank([]) then []=today() endif
                quitloop
                
             Otherwise: right
           EndSwitch
      Case Retval="FIELDVIEW" or Retval="F35":
           EDITFIELD()
      Case Retval="DEL":
           If CHAVEC=0
              Then If QuerExcluir("")
                      Then EXCLUSAO()
                           QuitLoop
                   EndIf
           EndIf
      EndSwitch
EndWhile
EndProc

;*******************************************************************************
;* 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  "

    MSGCAMPO=Field()

    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("ListNotI","MR")
           NITEM=[Ordem]
           If EDITAR="S"
              Then CHAVEI=0
                   Controla_EdicaoI()
              Else If EXCLUIR="S"
                      Then If QuerExcluir("")
                              Then Del
                              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 COD,NITEM
         If Retval
            Then CHAVEI=0
                 NITEM=[]
            Else CHAVEI=1
                 End
                 Down
                 [Sequencia      ]=COD    
                 [Ordem          ]=NITEM
                 UnLockRecord
                 Locate COD,NITEM
         EndIf
         Return
    EndSwitch
  EndWhile
EndWhile
EndProc   ; (ChaveI)

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

      TECLA=" Esc=Retornar  "

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

      MSGCAMPO=Field()
      switch
        case Field()="Codigo do Item" : MSGCAMPO=LOCALIZAR_POR +", [F1]Consultar tabela            [F9]Alterar modo localizar"
        case Field()="ObservacaoLista": MSGCAMPO="Observa‡Æo do Item"
        case Field()="ValorLista"     : MSGCAMPO="Valor do Item"
      endswitch

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

      Switch
      Case RetVal="F9"  :
           ModoLocalizarItem("SELECAO","","ListNotC")
      Case RetVal="F1":
           If Field()="Codigo do Item"
              Then moveto [Lookup_Item]
                   if CONSULTAF1()
                      then [Codigo do Item]=[Lookup_Item]
                   endif   
                   moveto [Codigo do Item]
           EndIf
      Case Retval="ESC":
           switch
             case Field()="Codigo do Item":
                If QuerDesfazer("")
                   Then If CHAVEI=1
                           Then Del
                           Else Undo
                        EndIf
                        CHAVEI=9
                        QuitLoop
                EndIf
             case Field()="ObservacaoLista": moveto [Codigo do Item]
             otherwise: Left
           endswitch
      Case Retval="ENTER":
           switch
             case Field()="Codigo do Item":
               if isblank([]) then fastmsg("Valor inv lido !") loop endif

               CODIGO = []
               Moveto [Lookup_Item]
               LOC_RETORNO_CODITEM = True
               Ativa_Lookup()
               ModoLocalizarItem("LOCALIZAR",CODIGO,"")
               moveto [Codigo do Item]
               CODIGO = LOC_CODIGO_ITEM
               If CODIGO=""
                  Then Fastmsg("Item/Referˆncia n„o localizado !")
                       Loop
               EndIf
               [Lookup_Item   ]=CODIGO
               [Codigo do Item]=CODIGO
             
               moveto [ObservacaoLista]
             case Field()="ObservacaoLista": 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
EndProc  ; (DigitaI)

;*******************************************************************************
;* Exclusao de Itens                                                           *
;*******************************************************************************
Proc EXCLUSAO()
Moveto "ListNotI"
While RecordStatus("New")=False
 Del
EndWhile
Moveto "ListNotC"
Del
EndProc  ; (Exclusao)

;*******************************************************************************
;* Seleciona Pedidos para impressao                                            *
;*******************************************************************************
Proc SELECIONA()

{Ask} {Itens}
moveto [Codigo do Item        ] typeIn "_ite"
moveto [Referencia            ] check
{Ask} {ListNotC} Check
Moveto [Sequencia             ] typeIn "_seq"
{Ask} {ListNotI} Check
Moveto [Sequencia             ] typeIn "_seq"
moveto [Codigo do Item        ] typeIn "_ite"
Do_It!
ClearAll

EndProc ;SELECIONA()

;************************************
;* ROTINA PARA Ler/Gravacao Opcao   *
;************************************
Proc ModeloRelatoriosCarregaLista()

VSDGCODMODELO     = ""
VSDGMODELO        = ""

;ModeloRelatoriosAdd("XXX XX maximo 70 XX")

ModeloRelatoriosAdd("Criar") ; linha obrigatoria inicial
ModeloRelatoriosAdd("01  Lista I")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista

;*******************************************************************************
;* Rotina Impressao                                                            *
;*******************************************************************************
Proc SelecaoImprimir()
private dmsg

campo="TG_CODMODELO"
if not Dialog_F1() 
   then return
endif

Do_It!
ClearAll

while true

  switch
  
    ;--- modelos report --------------------------------------------------------
    case VSDGCODMODELO="01":        
  
      DISPOSITIVO()
      If OK=""
         Then quitLoop
      endif

      AGUARDE("Aguarde, selecionado dados")
      Switch
;       Case VSDGCODMODELO="XX"  : SELECIONA1()
        OTherWise                : SELECIONA()
      EndSwitch

      If IsEmpty("Answer")
         Then MENSAGEM("N„o existem registros a processar")
              QuitLoop
      endif
         
      AGUARDE("Aguarde, gerando o relat¢rio")
      Switch
        Case VSDGCODMODELO="01" : CopyReport  "\\Base\\Sistemas\\Estoque\\RM_5000"  "R" "Answer" "R"
      EndSwitch
      IMPRIREL("Answer","R")
      
  Endswitch
  QuitLoop

Endwhile

if IsTable("Answer") then Delete "Answer" endif

CarregaTabelas()

EndProc

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

SetaCorRegistro("Padrao")
EndProc

;*******************************************************************************
;* Rotina para controlar Edicao do registro                                    *
;*******************************************************************************
Proc Controla_EdicaoC()

Moveto "ListNotI"
SetaCorRegistro("Padrao")

Moveto "ListNotC"
DIGITAC()

SetaCorRegistro("Padrao")
While CHAVEC<>9
  Moveto "ListNotI"
  CHAVEI()
  If CHAVEI=9 Or
     CHAVEI=8
     Then QuitLoop
  EndIf
  Controla_EdicaoI()
EndWhile
Moveto "ListNotC"
If RecordStatus("Locked") Then UnLockRecord EndIf         
If OPCAO_FORM="F"
   Then PickForm "F"
   Else PickForm "1"
EndIf
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc CarregaTabelas()

CoEdit "ListNotC"
PickForm "1"

Moveto "ListNotI"
SetaCorRegistro("Padrao")

Moveto "ListNotC"
PickForm OPCAO_FORM

SetaCorRegistro("Padrao")
EndProc

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

sys_Valor()

;---- M‚todo p/ Localizar o Item -----------------------------------------------
If IsFile(PrivDir()+"ListNotC.ini")
   Then LeiaINI(PrivDir()+"ListNotC.ini","LOCALIZA_CODIGO.S.null")
EndIf
If Not IsAssigned(LOCALIZA_CODIGO) Then LOCALIZA_CODIGO=""  EndIf
ModoLocalizarItem("","","")

ModeloRelatoriosCarregaLista()

OPCAO_FORM="F"

CarregaTabelas()

While True
  CHAVE()
  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
