;** Rm_0200 ********************************************************************
;*  Emissao dos Titulos Baixados por Data de Operacao                          *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 10/12/2001 por Anivel Augusto Rodrigues
;Ultima Alteracao: 31/05/2006 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes TG_NCARREG
;Ultima Alteracao: 21/05/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Tipo de Baixa
;Ultima Alteracao: 27/05/2015 por Andrey Fillipe
;Motivo Alteracao: Adicionado para pegar Mes Fechado
;Ultima Alteracao: 06/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 14/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 26/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog  
;Ultima Alteracao: 28/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes showdialog TG_DESTINAT                 

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

ClearAll
{Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupliC" Enter Check
Moveto [Numero do Lancamento   ] Example "lanc"
Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
Moveto [Codigo do Destinatario ] Example "dest" TypeIn ", ~VSDGDESTINAT.."
{Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupBaixa" Enter Check
Moveto [Numero do Lancamento   ] Example "lanc"
If Not IsBlank(VSDGNCARREG)
   Then Moveto [Numero_Carregamento] TypeIn "~VSDGNCARREG.."
   Else If VSDGOPCAO="1"
           Then Moveto [Data_Inclusao          ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
           Else Moveto [Data do Pagamento      ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        EndIf
EndIf
Moveto [Codigo da Conta        ] TypeIn "~VSDGCONTA.."
Moveto [Codigo_Usuario         ] TypeIn "~VSDGCHAPA.."
Moveto [Tipo da Baixa          ] TypeIn "~VSDGTPBAIXA.."

{Ask} {\\sistemas\\cadastro\\Destinat}
Moveto [Codigo                 ] Example "dest"
Moveto [Codigo da Area         ] Check
If Not IsBlank (VSDGAREA)
   Then TypeIn "~VSDGAREA.."
EndIf
Moveto [Descricao da Area      ] Check
Do_It!
ClearAll

EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina p/ processar Db temporaria                                           *
;*******************************************************************************
Proc Processa()
Coedit Tabela
Scan
  [Valor do Desconto-1]=([Valor do Desconto-1]*-1)+[Valor do Juros]
  If IsBlank([Valor do Desconto-1]) Then [Valor do Desconto-1]=0 EndIf
EndScan
Do_It!
ClearAll
EndProc   ; (Processa)

;*******************************************************************************
;* 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_EMPRESA"       : Campo="NO"
            Case Campo="TG_NCARREG"       : Campo="TG_EMPRESA"
            Case Campo="TG_AREA"          : Campo="TG_NCARREG"
            Case Campo="TG_CONTA"         : Campo="TG_AREA"
            Case Campo="TG_REPRESEN"      : Campo="TG_CONTA"
            Case Campo="TG_OPCAO"         : Campo="TG_REPRESEN"
            Case Campo="TG_DTINI"         : Campo="TG_OPCAO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_DESTINAT"      : Campo="TG_DTFIM"
            Case Campo="TG_CHAPA"         : Campo="TG_DESTINAT"
            Case Campo="TG_TPBAIXA"       : Campo="TG_NCARREG"
            Case Campo="YES" Or Campo="NO": Campo="TG_EMPRESA"
          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_EMPRESA"       : Campo="TG_NCARREG"
                    Case Campo="TG_NCARREG"       :
                         If IsBlank(VSDGNCARREG)
                            Then Campo="TG_AREA"
                            Else VSDGAREA    =""
                                 VSDGCONTA   =""
                                 VSDGREPRESEN=""
                                 VSDGDESTINAT=""
                                 VSDGCHAPA   =""
                                 VSDGDTINI   =BlankDate()
                                 VSDGDTFIM   =BlankDate()
                                 VSDGOPCAO   =""
                                 Campo="TG_TPBAIXA"
                         EndIf
                    Case Campo="TG_AREA"          : Campo="TG_CONTA"
                    Case Campo="TG_CONTA"         : Campo="TG_REPRESEN"
                    Case Campo="TG_REPRESEN"      : Campo="TG_OPCAO"
                    Case Campo="TG_OPCAO"         :
                        VSDGOPCAO=ControlValue("TG_OPCAO")
                        If IsBlank(VSDGOPCAO) or (VSDGOPCAO="1")
                           Then VSDGOPCAO="1"
                                VSDGNOMOPCAO="OPERA€ŽO"
                           Else VSDGOPCAO="2"
                                VSDGNOMOPCAO="PAGAMENTO"
                        EndIf
                       Campo="TG_DTINI"

                    Case Campo="TG_DTINI"         : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"         : Campo="TG_DESTINAT"

                    Case Campo="TG_DESTINAT"      : Campo="TG_CHAPA"
                    Case Campo="TG_CHAPA"         : Campo="TG_TPBAIXA"

                    Case Campo="TG_TPBAIXA"       : Campo="YES"
                        VSDGTPBAIXA=ControlValue("TG_TPBAIXA")
                        Switch
                          Case VSDGTPBAIXA="D": VSDGNOMTPBAIXA="DEVOLUCAO"
                          Case VSDGTPBAIXA="C": VSDGNOMTPBAIXA="CANCELADO"
                          Case VSDGTPBAIXA="N": VSDGNOMTPBAIXA="NORMAL"
                          OtherWise           : VSDGNOMTPBAIXA="TODAS"
                        EndSwitch
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_OPCAO":
       MSG_CAMPO=" 1=Data de Opera‡Ćo  2=Data de Pagamento"
  Case Campo="TG_TPBAIXA":
       MSG_CAMPO=" N=Normal  D=Devolu‡„o  C=Cancelado"
EndSwitch

Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGNCARREG =""
VSDGAREA    =""
VSDGCONTA   =""
VSDGDESTINAT=""
VSDGTPBAIXA =""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()

While True
  ShowDialog "Tˇtulos Baixados por Data de Operacao" 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 19 Width 72

    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA" To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP

    Label  @ 02,00 "Carregamento" For "TG_NCARREG"
    Accept @ 03,01 Width 9  "A6"  Picture "[&,#][#*#]" Tag "TG_NCARREG"    To VSDGNCARREG
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMNCARREG" To VSDGNOMNCARREG

    Label  @ 04,00 "Area"  For "TG_AREA"
    Accept @ 05,03 Width 05 "A2"  Picture "#*#" Tag "TG_AREA" To VSDGAREA
    Accept @ 05,09 Width 25 "A30"  Picture "!*!" Tag "TG_NOMAREA" To VSDGNOMAREA

    Label  @ 06,00 "Conta"  For "TG_CONTA"
    Accept @ 07,01 Width 07 "A4"  Picture "#*#" Tag "TG_CONTA" To VSDGCONTA
    Accept @ 07,09 Width 25 "A30" Picture "!*!" Tag "TG_NOMCONT" To VSDGNOMCONTA

    Label  @ 08,00 "Representante"  For "TG_REPRESEN"
    Accept @ 09,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_REPRESEN" To VSDGREPRESEN
    Accept @ 09,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMREPR"  To VSDGNOMREPR

    Label  @ 10,00 "Op‡äes de Datas"  For "TG_OPCAO"
    Accept @ 11,04 Width 04 "A1"  Picture "1,2" Tag "TG_OPCAO" To VSDGOPCAO
    Accept @ 11,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMOPCAO" To VSDGNOMOPCAO

    Label  @ 12,00 "Perˇodo Referente de" For "TG_DTINI"
    Accept @ 13,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 13,16 "at‚" For "TG_DTFIM"
    Accept @ 13,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Label  @ 00,35 "Pessoa"  For "TG_DESTINAT"
    Accept @ 01,36 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 01,44 Width 25 "A20" Picture "!*!" Tag "TG_NOMCLI" To VSDGNOMDEST

    Label  @ 02,35 "Usuario"  For "TG_CHAPA"
    Accept @ 03,36 Width 07 "A4"  Picture "#*#" Tag "TG_CHAPA"     To VSDGCHAPA
    Accept @ 03,44 Width 25 "A20" Picture "!*!" Tag "TG_NOMCHAPA"  To VSDGNOMCHAPA

    Label  @ 04,35 "Op‡Ćo de Baixa"  For "TG_TPBAIXA"
    Accept @ 05,39 Width 04 "A1"  Picture "N,D,C" Tag "TG_TPBAIXA" To VSDGTPBAIXA
    Accept @ 05,44 Width 25 "A20" Picture "!*!" Tag "TG_NOMTPBAIXA" To VSDGNOMTPBAIXA

    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 Not Valida_ShowDialog("") Then Loop EndIf

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

  DISPOSITIVO()
  If OK="" Then Loop EndIf
  
  AGUARDE("Aguarde, processando dados")
  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
  Processa()

  If Not IsBlank(VSDGNCARREG)
     Then CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1010" "1" Tabela "R"
     Else If VSDGOPCAO="1"
             Then CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1011" "2" Tabela "R"
             Else CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1011" "1" Tabela "R"
          EndIf
  EndIf

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
