;** Rm_0012A  ******************************************************************
;*  Emissao dos Titulos a Programados por Plano de Contas/Centro de Custo      *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 02/06/2010 por Bruno Carvalho
;Ultima Alteracao: 08/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: 27/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog
;Ultima Alteracao: 10/05/2017 por Rafael Matos
;Motivo Alteracao: Incluso TG_OPCCUSTO                   
;Ultima Alteracao: 02/08/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Sequencia da Baixa TitRat        

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

If IsTable("\\Sistemas\\Ctapag\\"+Dir_Trab+"Titulos")
  Then {Ask} TypeIn "\\Sistemas\\Ctapag\\"+Dir_Trab+"Titulos" Enter Check
       Moveto [Codigo da Empresa ] TypeIn "~VSDGEMPRESA2.."
       Moveto [Data Competencia  ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
       Moveto [Codigo Plano_Conta] TypeIn PLCONTAS
       If CheckTG_OPCCUSTO[1]
          Then Moveto [Codigo CCusto          ] TypeIn "Not Blank, ~VSDGCCUSTO.."
          Else Moveto [Codigo CCusto          ] TypeIn "~VSDGCCUSTO.."
       Endif
       Do_It!
       ClearAll
       {Ask} "TitRat" Enter
       Moveto [Numero do Lancamento] example "NumLan"        
       {Ask} "Answer" Enter TypeIn "Delete"
       Moveto [Numero do Lancamento] example "NumLan" 
       Moveto [Sequencia da Baixa  ] TypeIn "0"
       Do_It!
       ClearAll
       
       Primeirav="******"
       Coedit "Answer","TitRat","Deleted"
       Scan
         If [Numero do Lancamento]<>Primeirav 
            then Primeirav = [Numero do Lancamento] 
                 CopyToArray Array_Del      
                 Moveto "TitRat"
                 Moveto [Numero do Lancamento]
                 Locate [deleted->Numero do Lancamento],"0"
                 If RetVal             
                    Then While [Numero do Lancamento]=[Deleted->Numero do Lancamento] and
                               [Sequencia da Baixa  ]="0"

                           If Not IsBlank([Codigo Plano_Conta]) Then
                           Moveto "Answer"
                           End Down
                           CopyFromArray Array_del
                           [Codigo Plano_Conta   ]=[TitRat->Codigo Plano_Conta   ]
                           [Descricao Plano_Conta]=[TitRat->Descricao Plano_Conta] 
                           [Codigo CCusto        ]=[TitRat->Codigo CCusto        ]
                           [Descricao CCusto     ]=[TitRat->Descricao CCusto     ]
                           [Valor do Documento   ]=[TitRat->Valor                ]                            
                           UnLockRecord
                           EndIf
                           Moveto "TitRat"      
                           Skip
                           If EOT() Then QuitLoop EndIf
                         EndWhile
                 EndIf 
                 Moveto "Deleted"  
         EndIf       
       EndScan
       Do_It!
       ClearAll     
   
;CoEdit "Answer"
;Scan For Not IsBlank([Codigo CCusto])
;  [Codigo Plano_Conta   ]=Completa([Codigo Plano_Conta],14," ")+[Codigo CCusto]
;  [Descricao Plano_Conta]="          "+LTrim([Descricao CCusto])
;EndScan
;Do_It!
;ClearAll       
       
EndIf
EndProc  ; (Seleciona)

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

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

{Ask} TypeIn Tabela Enter TypeIn "Fast"
Moveto [Pag_1             ] 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 [Pag_1             ] TypeIn "Changeto _tc"
Do_It!
ClearAll
EndProc  ; (Seleciona)

;*******************************************************************************
;* 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_CCUSTO"        : Campo="TG_PLCONTAS"
            Case Campo="TG_DTINI"         : Campo="TG_CCUSTO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_OPCCUSTO"      : Campo="TG_DTFIM"
            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_CCUSTO"
                    Case Campo="TG_CCUSTO"       : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"        : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"        : Campo="TG_OPCCUSTO"
                    Case Campo="TG_OPCCUSTO"     : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA=""
VSDGPLCONTAS=""
VSDGCCUSTO=""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()

Array TG_OPCCUSTO[1]
TG_OPCCUSTO[1]="Ignorar em Branco"

Array CheckTG_OPCCUSTO[ArraySize(TG_OPCCUSTO)]
CheckTG_OPCCUSTO[1]=False

While True
  ShowDialog "Anal¡tico/Programado por Plano de Conta/Centro de Custo" 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 "Centro de Custo" For "TG_CCUSTO"
    Accept @ 06,01 Width 33 "A7"  Picture "###.###" Tag "TG_CCUSTO" To VSDGCCUSTO
    Accept @ 07,01 Width 33 "A20" Picture "!*!" Tag "TG_NOMCUST"  To VSDGNOMCUST
    
    Label  @ 08,00 "Per¡odo Competˆncia de" For "TG_DTINI"
    Accept @ 09,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 09,16 "at‚" For "TG_DTFIM"
    Accept @ 09,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM
    
    Label  @ 00,35 "Centro de Custo"  For "TG_OPCCUSTO"
    CheckBoxes @ 01,35 Height 1 Width 35 Tag "TG_OPCCUSTO"
    TG_OPCCUSTO[1] to CheckTG_OPCCUSTO[1]

    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 IsBlank(VSDGDTFIM)
     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()
    If IsTable("Answer")     Then Delete "Answer" EndIf
    Seleciona()
    If Not IsTable("Answer") Then Loop EndIf
    If IsTable(Tabela)
       Then Add "Answer" Tabela
       Else Rename "Answer" Tabela
    EndIf
  EndWhile

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

  CopyReport "\\Base\\Sistemas\\CtaPag\\Rm_1001" "7" Tabela "R"

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
