;** LogSys *********************************************************************
;* Log de Usuarios                                                             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 22/11/2005 por Anivel Augusto Rodrigues
;Ultima Alteracao: 16/04/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Datas
;Ultima Alteracao: 14/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog     
;Ultima Alteracao: 03/01/2017 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog MensagemRadioButtons()              
;Ultima Alteracao: 11/07/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes logica e layout relatorio
;Ultima Alteracao: 18/12/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes extensÆo arquivo rois.r -> rois.log
;Ultima Alteracao: 30/06/2018 por Bruno Carvalho
;Motivo Alteracao: Adicionadao op‡Æo para mostrar logs de apenas processos exclu¡dos
;Ultima Alteracao: 19/07/2018 por Bruno Carvalho
;Motivo Alteracao: Faz a verifica‡Æo se arquivo "rois" existe
;Ultima Alteracao: 31/08/2018 por Bruno Carvalho
;Motivo Alteracao: Adicionado op‡Æo 6 p/ exibir "Processo com Problemas"

;*******************************************************************************
;* Pega Dados                                                                  *
;*******************************************************************************
Proc Pega_Dados()
Private ii

DATA    = ""
HORA    = ""
CHAPA   = "" 
NOME    = ""
ESTACAO = ""
SCRIPT  = ""
LERRO   = ""
LPOSI   = ""
LPROC   = ""
LCODE   = ""
LUSER   = ""
LMSG    = ""

DELIMITADOR = ";"

For ii From 1 To 12
  PosI = Search(DELIMITADOR,TEXTO)
  If PosI>0 And ii<>12
     Then LINHA = Ltrim(Rtrim(SubStr(TEXTO,1,PosI-1)))
          Switch
            Case ii=1  : DATA    = LINHA
                         If Len(DATA)=9 Then DATA="0"+DATA EndIf
                         If Len(DATA)=10
                            Then DATA = SubStr(DATA,1,2)+"."+SubStr(DATA,4,2)+"."+SubStr(DATA,7,4)
                         EndIf   
            Case ii=2  : HORA    = LINHA
            Case ii=3  : CHAPA   = LINHA 
            Case ii=4  : NOME    = LINHA
            Case ii=5  : ESTACAO = LINHA
            Case ii=6  : SCRIPT  = LINHA 
            Case ii=7  : LERRO   = LINHA
            Case ii=8  : LPOSI   = LINHA
            Case ii=9  : LPROC   = LINHA
            Case ii=10 : LCODE   = LINHA
            Case ii=11 : LUSER   = LINHA
          EndSwitch
          TEXTO = SubStr(TEXTO,PosI+1,Len(TEXTO))             
  EndIf
  
  If Search("8/01/2018",DATA)>0 Then Debug EndIf
  
  If ii=2 
     Then If Type(DateVal(DATA))="D"  
             Then dtlog=DateVal(DATA)
                  hrlog=SubStr(HORA,1,5)
                  If (dtlog<VSDGDTINI) Or
                     (dtlog=VSDGDTINI And hrlog<VSDGHRINI) Or
                     (dtlog>VSDGDTFIM) Or
                     (dtlog=VSDGDTFIM And hrlog>VSDGHRFIM)
                     Then Return False
                   EndIf  
           EnDIf
  EndIf 
  
  
  If ii=12
     Then LMSG = Ltrim(Rtrim(TEXTO))
  EndIf
Endfor     

TEXTO=      Completa(Format("D13",DATA),10," ")+" "
TEXTO=TEXTO+Format("W8",HORA)+" "
TEXTO=TEXTO+Completa(Format("W4",CHAPA)+" "+Format("W15",NOME),15," ")+" "
TEXTO=TEXTO+Completa(ESTACAO,12," ")+" "
TEXTO=TEXTO+Completa(SCRIPT,30," ")+" "
TEXTO=TEXTO+Format("W10",LERRO)+" "
TEXTO=TEXTO+Format("W10",LPOSI)+" "
TEXTO=TEXTO+Format("W10",LPROC)+" "
TEXTO=TEXTO+Format("W10",StrVal(LCODE))+" "
TEXTO=TEXTO+Format("W10",LUSER)+" "
TEXTO=TEXTO+LMSG

Return True
          
EndProc ;Pega_Dados   

;*******************************************************************************
;* Importa dados TXT -> DB                                                     *
;*******************************************************************************
Proc Importa()
Private ArqTxt

ARQ=PrivDir()+"tmplog"
If IsTable(ARQ)
   Then Delete ARQ
EndIf
Copy "\\Base\\Sistemas\\Cadastro\\Rm_txlog" ARQ

Aguarde("Aguarde, preparando dados")
Empty(ARQ)
If IsAssigned(varinner) Then release vars varinner  EndIf

If VSDGOPCAO=6
   Then ArqTxt = SubStr(Directory(),1,2)+"\\Sistemas\\Cadastro\\logerror.txt"
   Else ArqTxt = SubStr(Directory(),1,2)+"\\Sistemas\\Cadastro\\rois.log" 
EndIf

If Isfile(ArqTxt)
   Then FileRead ArqTxt to varinner
        Edit ARQ
        pos  = 1
        Cont = 0
        While True
          Cont = Cont + 1
          Aguarde("Aguarde, montando log de erro: "+StrVal(Cont))
          
          posf=searchFrom("\n",varinner,pos)
          if posf=0 then quitLoop endif
          
          TEXTO = substr(varinner,pos,(posf-pos))
          If VSDGOPCAO=6
              Then If Not Pega_Dados()  
                      Then TEXTO = ""
                   EndIf   
          EndIf    
          
          [Text]=TEXTO 
          Down
          pos=posf+1   
        EndWhile
        Do_It!
        ClearAll 
EndIf   

If IsAssigned(varinner) Then release vars varinner  EndIf

Endproc

;*******************************************************************************
;* Rotina Converte DB                                                          *
;*******************************************************************************
Proc Converte()
ClearAll

Importa()

Barra(2, 1)
Aguarde("Convertendo log's")

NR = NRecords(ARQ)

View ARQ
EditKey
SetHighLight Current Record 23 Field 31
Scan
  Barra(NR, RecNo())
  Aguarde("Aguarde, Processando "+BARRA_STRING)

  If IsBlank([Text]) Then Loop EndIf
  oplog=SubStr([Text],55,1)
  
  Switch
    Case VSDGOPCAO=1 : If oplog<>">" Then Limpa_Text() Loop EndIf
    Case VSDGOPCAO=2 : If oplog<>"!" Then Limpa_Text() Loop EndIf
    Case VSDGOPCAO=3 : If oplog<>"#" Then Limpa_Text() Loop EndIf
    Case VSDGOPCAO=4 : If oplog<>"#" Then Limpa_Text() Loop EndIf
    Case VSDGOPCAO=5 : If Upper(SubStr([Text],67,4))<>"EXCL" Then Limpa_Text() Loop EndIf
  EndSwitch
  
  Switch
    Case VSDGOPCAO=3: uslog=SubStr([Text],76,4)
    OtherWise       : uslog=SubStr([Text],21,4)
  EndSwitch

  If Not IsBlank(VSDGCHAPA)
     Then If uslog<>VSDGCHAPA Then Limpa_Text() Loop EndIf
  EndIf
  
  s=SubStr([Text],1,10)
  s=SubStr(s,4,2)+"/"+LTrim(SubStr(s,1,2))+"/"+SubStr(s,7,4)
  dtlog=DateVal(s)
;;dtlog=DateVal(LTrim(SubStr([Text],1,10)))
  hrlog=SubStr([Text],12,5)
  If dtlog<VSDGDTINI Then Limpa_Text() Loop EndIf
  If dtlog=VSDGDTINI And hrlog<VSDGHRINI Then Limpa_Text() Loop EndIf
  If dtlog>VSDGDTFIM Then Limpa_Text() Loop EndIf
  If dtlog=VSDGDTFIM And hrlog>VSDGHRFIM Then Limpa_Text() Loop EndIf

EndScan
Do_It!
ClearAll

{Ask} TypeIn ARQ Enter TypeIn "Fast Delete"
Moveto [Text] TypeIn "Blank"
Do_It!
ClearAll

LimpAguarde()
EndProc

Proc Limpa_Text()
[Text]=""
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = 9: Temp=False
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_CHAPA"         : Campo="NO"
            Case Campo="TG_DTINI"         : Campo="TG_CHAPA"
            Case Campo="TG_HRINI"         : Campo="TG_DTINI"
            Case Campo="TG_DTFIM"         : Campo="TG_HRINI"
            Case Campo="TG_HRFIM"         : Campo="TG_DTFIM"
            Case Campo="TG_OPCOES"        : Campo="TG_HRFIM"
            Case Campo="YES" Or Campo="NO": Campo="TG_CHAPA"
          EndSwitch
     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_CHAPA"    : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"    : Campo="TG_HRINI"
                    Case Campo="TG_HRINI"    : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"    : Campo="TG_HRFIM"
                    Case Campo="TG_HRFIM"    : Campo="TG_OPCOES"
                    Case Campo="TG_OPCOES"   : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo=""             : MSG_CAMPO=""
  Case Campo="TG_OPCOES"    : MensagemRadioButtons()
Endswitch

Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()

VSDGCHAPA=""
VSDGDTINI=BlankDate()
VSDGDTFIM=Today()
VSDGHRINI=""
VSDGHRFIM=""

While True

  ShowDialog "Log de Usu rios" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 19 Width 72

    Label  @ 00,00 "Usu rio" For "TG_CHAPA"
    Accept @ 01,01 Width 07 "A4"  Picture "#*#" Tag "TG_CHAPA" To VSDGCHAPA
    Accept @ 01,09 Width 25 "A30" Picture "!*!" Tag "TG_CHAPA" To VSDGNOMCHAPA

    Label  @ 02,00 "Per¡odo Referente" For "TG_DTINI"
    Label  @ 03,04 " de" For "TG_DTINI"
    Accept @ 03,09 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 03,22 "as" For "TG_DTINI"
    Accept @ 03,26 Width 08 "A5"  Picture "##:##" Tag "TG_HRINI" To VSDGHRINI
    Label  @ 04,04 "at‚" For "TG_DTFIM"
    Accept @ 04,09 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM
    Label  @ 04,22 "as" For "TG_DTFIM"
    Accept @ 04,26 Width 08 "A5"  Picture "##:##" Tag "TG_HRFIM" To VSDGHRFIM

    Label  @ 06,00 "Op‡”es de opera‡”es " For "TG_OPCOES"
    RadioButtons @ 07,00 Height 7 Width 46
      "~1~ Rotinas Menu    ",
      "~2~ Controladas     ",
      "~3~ Supervisionadas/Usu rio selecionado",
      "~4~ Supervisionadas/por outro Usu rio",
      "~5~ Processo de Exclusäes",
      "~6~ Processos interrompidos",
      "~7~ Todas"
        Tag "TG_OPCOES"
        To VSDGOPCAO
        
    Pushbutton @ 15,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 15,36 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  If IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  Switch
    Case VSDGOPCAO=1: VSDGNOMOPCOES="Rotinas Menu"
    Case VSDGOPCAO=2: VSDGNOMOPCOES="Controladas"
    Case VSDGOPCAO=3: VSDGNOMOPCOES="Supervisionadas/Usuario selecionado"
    Case VSDGOPCAO=4: VSDGNOMOPCOES="Supervisionadas/por outro Usuario"
    Case VSDGOPCAO=5: VSDGNOMOPCOES="Processo de Exclusäes"
    Case VSDGOPCAO=6: VSDGNOMOPCOES="Processo com Problemas"
    Case VSDGOPCAO=7: VSDGNOMOPCOES="Todas"
  EndSwitch

  DISPOSITIVO()
  If OK=""  Then Loop EndIf

  AGUARDE("Aguarde, gerando o relat¢rio")

  Converte()
  If IsEmpty(ARQ)
     Then MENSAGEM("N„o existem registros a processar")
          Loop
  EndIf

  Impressao_Orientacao="Paisagem"
  If VSDGOPCAO=6
     Then CopyReport "\\Base\\Sistemas\\Cadastro\\Rm_txlog" "2" ARQ "R" 
     Else CopyReport "\\Base\\Sistemas\\Cadastro\\Rm_txlog" "R" ARQ "R"
  EndIf   
  IMPRIREL(ARQ,"R")

  Delete ARQ

EndWhile
ClearAll
Release Vars All
Release Procs All
