;** Rm_0200 *******************************************************************
;*  Analitico  p/ Plano de Contas                                             *
;******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 08/11/2002 por Anivel Augusto Rodrigues
;Ultima Alteracao: 06/12/2005 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 27/06/2007 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Modelo Relatorio
;Ultima Alteracao: 05/05/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: TOTALIZA EMPRESAS
;Ultima Alteracao: 15/12/2010 por Bruno Carvalho
;Motivo Alteracao: Ajustes Report 5 (Tipo de Despesa)
;Ultima Alteracao: 02/12/2011 por Bruno Carvalho
;Motivo Alteracao: Ajustes CopyReport
;Ultima Alteracao: 03/09/2014 por Luis Gustavo
;Motivo Alteracao: Adicionado filtro Op‡ao de data
;Ultima Alteracao: 03/02/2015 por Luis Gustavo
;Motivo Alteracao: Implementado Filtro Despesas, filtra pelo Campo Tipo_Custo da tabela PLCONTAS
;Ultima Alteracao: 06/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 17/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes TG_EMPRESATE                 
;Ultima Alteracao: 28/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog.                   
;Ultima Alteracao: 11/08/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes c¢cigos modelo de relatorios
;Ultima Alteracao: 19/01/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Tipo de Operacao 03, 12           

;*******************************************************************************
;* Rotina p/ Selecionar e Order Registros                                      *
;*******************************************************************************
Proc Seleciona()
ClearAll

If IsTable("\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont")
  Then {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont" Enter Check
       Moveto [Codigo da Empresa  ] TypeIn "~VSDGEMPRESA2.."
       Moveto [Tipo de Operacao   ] TypeIn "05 or 06 or 07 or 13 or 14 or 03 or 12"
       If VSDGOPDTEMISCOMP="1"
          Then Moveto [Data Competencia] Check
               Moveto [Data            ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
          Else Moveto [Data            ] Check
               Moveto [Data Competencia] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM, as Data"
       EndIf
       Moveto [Codigo do EmitDest ] TypeIn "~VSDGDESTINAT.."
       Moveto [Codigo Plano_Conta ] TypeIn PLCONTAS
       Moveto [Cheque Pre-Datado  ] TypeIn "Not S"
       Do_It!
       ClearAll
       
       If Not IsBlank(VSDGDESP)
          Then {Ask} "Answer" Enter Check
               Moveto [Codigo Plano_Conta ] Example "plcont"
               {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"Plcontas" Enter
               Moveto [Codigo Plano_Conta ] Example "plcont"
               Moveto [Tipo_Custo         ] Check TypeIn "~VSDGDESP.."
               Do_It!
               ClearAll
       EndIf
EndIf
EndProc     ;  (Seleciona)

;*******************************************************************************
;* Rotina p/ Selecionar e Order Registros                                      *
;*******************************************************************************
Proc Seleciona2()
ClearAll

If IsTable("\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont")
  Then {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"MovCont" Enter Check
       Moveto [Codigo da Empresa     ] TypeIn "~VSDGEMPRESA2.."
       Moveto [Tipo de Operacao      ] TypeIn "05"
       Moveto [Codigo do EmitDest    ] TypeIn "~VSDGDESTINAT.."
       Moveto [Codigo Plano_Conta    ] TypeIn PLCONTAS
       Moveto [Cheque Pre-Datado     ] TypeIn "S"
       Moveto [Data Cheque Pre-Datado] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
       If VSDGOPDTEMISCOMP = "2"
          Then Moveto [Data                  ] Check
               Moveto [Data Competencia      ] TypeIn "as Data"
          Else Moveto [Data Competencia      ] Check
       EndIf
       Do_It!
       ClearAll
       
       If Not IsBlank(VSDGDESP)
          Then {Ask} "Answer" Enter Check
               Moveto [Codigo Plano_Conta ] Example "plcont"
               {Ask} TypeIn "\\Sistemas\\Ctacor\\"+Dir_Trab+"Plcontas" Enter
               Moveto [Codigo Plano_Conta ] Example "plcont"
               Moveto [Tipo_Custo         ] Check TypeIn "~VSDGDESP.."
               Do_It!
               ClearAll
       EndIf
EndIf
EndProc     ;  (Seleciona)

;*******************************************************************************
;* Rotina p/ Processar Registros                                               *
;*******************************************************************************
Proc Processa()
ClearAll

{Ask} TypeIn Tabela Enter TypeIn "Fast"
Moveto [Tipo de Operacao       ] TypeIn "05"
Moveto [Cheque Pre-Datado      ] TypeIn "S"
Moveto [Data Cheque Pre-Datado ] TypeIn "_dchp"
Moveto [Data                   ] TypeIn "Changeto _dchp"
Do_It!
ClearAll

{Ask} TypeIn Tabela Enter
TypeIn "Fast"
Moveto [Lancamento Gerador     ] TypeIn "_nlan, Not Blank"
Moveto [Numero do Lancamento   ] TypeIn "Changeto _nlan"
Do_It!
ClearAll

{Ask} TypeIn Tabela Enter
TypeIn "Fast"
Moveto [Sistema Gerador        ] TypeIn "Blank, Changeto CB"
Do_It!
ClearAll

EndProc     ;  (Seleciona)

;*******************************************************************************
;* ROTINA Ajusta Dados Empresa                                                 *
;*******************************************************************************
Proc Processa2()

;***** Ajusta Dados ************************************
If VSDGEMPRESA="0000"
   Then {Ask} TypeIn Tabela Enter TypeIn "Fast"
        Moveto [Codigo da Empresa      ] TypeIn "changeto 0000"
        Moveto [Nome Fantasia Empresa  ] TypeIn "changeto TOTALIZA EMPRESAS"
        Do_It!
        ClearAll
EndIf

EndProc

;*******************************************************************************
;* Rotina p/ Ajustar tipocusto                                                 *
;*******************************************************************************
Proc Ajusta()
ClearAll

{Ask} TypeIn Tabela Enter TypeIn "Fast"
Moveto [Tipo da Conta Trf ] TypeIn "Changeto X"
Do_It!
ClearAll

{Ask} {\\Sistemas\\Ctacor\\Plcontas}
Moveto [Codigo Plano_Conta] example "plc"
Moveto [Tipo_Custo        ] example "tc"
{Ask} TypeIn Tabela Enter TypeIn "Fast"
Moveto [Codigo Plano_Conta] example "plc"
Moveto [Tipo da Conta Trf ] TypeIn "Changeto _tc"
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  Simples")
ModeloRelatoriosAdd("03  Detalhado")
ModeloRelatoriosAdd("Separator")
ModeloRelatoriosAdd("02  Agrupado por Emitente/Destinat rio")
ModeloRelatoriosAdd("04  Agrupado por Emitente/Destinat rio e Mˆs/Ano")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista


;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
TECLA=""
MSG_CAMPO=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESATE"     : Campo="NO"
            Case Campo="TG_PLCONTAS"      : Campo="TG_EMPRESATE"
            Case Campo="TG_DESTINAT"      : Campo="TG_PLCONTAS"
            Case Campo="TG_OPDTEMISCOMP"  : Campo="TG_DESTINAT"
            Case Campo="TG_DTINI"         : Campo="TG_OPDTEMISCOMP"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_DESP"          : Campo="TG_DTFIM"
            Case Campo="TG_CODMODELO"     : Campo="TG_DESP"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESATE"
          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_EMPRESATE"    : Campo="TG_PLCONTAS"
                    Case Campo="TG_PLCONTAS"     : Campo="TG_DESTINAT"
                         If VSDGPLCONTAS=""
                            Then VSDGDESP    = ""
                                 VSDGNOMDESP = "TODAS"    
                         EndIf   
                    Case Campo="TG_DESTINAT"     : Campo="TG_OPDTEMISCOMP"
                    Case Campo="TG_OPDTEMISCOMP" : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"        : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"        : Campo="TG_DESP"
                    Case Campo="TG_DESP"         : Campo="TG_CODMODELO"
                         VSDGDESP = ControlValue("TG_DESP")
                         Switch
                            Case VSDGDESP = "F": VSDGNOMDESP = "FIXA"
                            Case VSDGDESP = "V": VSDGNOMDESP = "VARIAVEL"
                            OtherWise          : VSDGDESP    = ""
                                                 VSDGNOMDESP = "TODAS"    
                         EndSwitch                  
                    Case Campo="TG_CODMODELO"  : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DESP":
       MSG_CAMPO="Op‡äes: F=Fixa  V=Vari vel  Em Branco= Todas"
  Case Campo="TG_DTINI":
       MSG_CAMPO="Informe a Data Inicial"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA     =""
VSDGNOMPLCONTAS =""
VSDGDESTINAT    =""
VSDGDESP        =""
VSDGNOMDESP     =""
VSDGDTINI       =BlankDate()
VSDGDTFIM       =BlankDate()
VSDGOPDTEMISCOMP   =""
VSDGNOMOPDTEMISCOMP=""

ModeloRelatoriosCarregaLista()

While True
  ShowDialog "Anal¡tico p/ Plano de Contas" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 15 Width 72

    Label  @ 00,00 "Empresa"  For "TG_EMPRESATE"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESATE" To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP
    
    Label  @ 02,00 "Plano de Contas" For "TG_PLCONTAS"
    Accept @ 03,01 Width 33 "A13"  Picture "!*!" Tag "TG_PLCONTAS" To VSDGPLCONTAS
    Accept @ 04,01 Width 33 "A40" Picture "!*!" Tag "TG_NOMPLCONTAS"  To VSDGNOMPLCONTAS
    
    Label  @ 05,00 "Pessoa"  For "TG_DESTINAT"
    Accept @ 06,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 06,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMDEST"  To VSDGNOMDEST
                              
    Label  @ 00,35 "Op‡äes de Datas" For "TG_OPDTEMISCOMP"
    Accept @ 01,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPDTEMISCOMP" To VSDGOPDTEMISCOMP
    Accept @ 01,44 Width 25 "A20"  Picture "!*!" Tag "TG_NOMOPDTEMISCOMP" To VSDGNOMOPDTEMISCOMP

    Label  @ 02,35 "Per¡odo Referente de" For "TG_DTINI"
    Accept @ 03,38 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 03,51 "at‚" For "TG_DTFIM"
    Accept @ 03,56 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 04,35 "Tipo de Despesas"  For "TG_DESP"
    Accept @ 05,39 Width 04 "A1"  Picture "F,V" Tag "TG_DESP" To VSDGDESP
    Accept @ 05,44 Width 25 "A20" Picture "!*!" Tag "TG_NOMDESP"  To VSDGNOMDESP

    Label  @ 08,00  "Modelo de ~R~elat¢rio"  For "TG_CODMODELO"
    Accept @ 09,01  Width 06 "A3"  Picture "!*!" Tag "TG_CODMODELO" To VSDGCODMODELO
    Accept @ 09,06  Width 63 "A60"  Picture "!*!" Tag "TG_MODELO" To VSDGMODELO        

    Pushbutton @ 11,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 11,36 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

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

  If Len(VSDGPLCONTAS)<13
     Then PLCONTAS=VSDGPLCONTAS + ".."
     Else PLCONTAS=VSDGPLCONTAS
  EndIf

  If VSDGEMPRESA="0000"
     Then VSDGEMPRESA2=""
     Else VSDGEMPRESA2=VSDGEMPRESA
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Gerando o Relat¢rio")
  Tabela=Cria_Temp(PrivDir())

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    For ii From 1 to 2
      If IsTable("Answer")     Then Delete "Answer" EndIf
      Switch
        Case ii=1: Seleciona()
        Case ii=2: Seleciona2()
      EndSwitch
      If Not IsTable("Answer") Then Loop EndIf
      If IsTable(Tabela)
         Then Add "Answer" Tabela
         Else Rename "Answer" Tabela
      EndIf
    EndFor
  EndWhile

  If IsEmpty(Tabela)
     Then MENSAGEM("N„o existem registros a processar")
          Delete Tabela
          Loop
  EndIf
  
  Processa()
  Processa2()

  If VSDGCODMODELO="01" Then Ajusta()
                             CopyReport "\\Base\\Sistemas\\Ctacor\\RM_0120" "5" Tabela "R"   EndIf
  If VSDGCODMODELO="02" Then CopyReport "\\Base\\Sistemas\\Ctacor\\RM_0120" "6" Tabela "R"   EndIf
  If VSDGCODMODELO="03" Then CopyReport "\\Base\\Sistemas\\Ctacor\\RM_0120" "4" Tabela "R"   EndIf
  If VSDGCODMODELO="04" Then CopyReport "\\Base\\Sistemas\\Ctacor\\RM_0120" "8" Tabela "R"   EndIf
  
  
  IMPRIREL(Tabela,"R") 
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
