;** Rm_0170 ********************************************************************
;*  Emissao dos Titulos nao Recebidos                                          *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 01/09/1998 por Anivel Augusto Rodrigues
;Ultima Alteracao: 17/02/2004 por Anivel Augusto Rodrigues
;Motivo Alteracao: Mudan‡a Tabela Temporaria PrivDir()
;Ultima Alteracao: 25/08/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: Correcao Data de Vencimento em Branco
;Ultima Alteracao: 20/05/2020 por Bruno Carvalho
;Motivo Alteracao: Ajustes para o Base no CopyReport do "RM_1000" para "\\Base\\Sistemas\\CtaRec\\RM_1000"

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

If IsTable("\\Sistemas\\Ctarec\\"+Dir_Trab+"Duplic")
  Then {Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupliC" Enter Check
        Moveto [Codigo da Empresa ] TypeIn "~VSDGEMPRESA.."
        Moveto [Data de Emissao   ] TypeIn ">=~VSDGDTINI, <=~VSDGDTFIM"
        Do_It!
        ClearAll
EndIf
EndProc ; (Seleciona_01)

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

{Ask} TypeIn TabelaC Enter
Moveto [Numero do Lancamento] Example "numero"

{Ask} {DupBaixa} Check
Moveto [Numero do Lancamento] Example "numero"
Do_It!
ClearAll

EndProc  ; (Seleciona_02)

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

{Ask} TypeIn TabelaC Enter  TypeIn "Fast"
Moveto [Valor do Documento  ] Example "vr"
Moveto [Saldo do Documento  ] TypeIn "Changeto "  Example "vr"
Do_It!
ClearAll

Coedit TabelaI,TabelaC
Moveto [Numero do Lancamento]
Scan
   Lancto=[Numero do Lancamento]
   VRPG=0
   Moveto TabelaI
   Moveto [Numero do Lancamento]
   Locate Lancto
   If RetVal
      Then While [Numero do Lancamento]=Lancto And Not RecordStatus("New")
             If [Data do Pagamento]<=VSDGDTFIM
                Then VRPG=VRPG+([Valor do Recebimento]+[Valor do Desconto])
             EndIf
             Down
           EndWhile
   EndIf
   Moveto TabelaC
   [Saldo do Documento]=[Saldo do Documento]-VRPG
EndScan
Do_It!
ClearAll

{Ask} TypeIn TabelaC Enter TypeIn "Fast Delete"
Moveto [Saldo do Documento  ] TypeIn "<=0.01"
Do_It!
ClearAll
EndProc ; (Seleciona_02)

;*******************************************************************************
;* Rotina p/ processar Db temporaria                                             *
;*******************************************************************************
Proc Processa()

Coedit TabelaC
Scan For Not IsBlank([Data de Vencimento])
   If [Data de Vencimento]<Today()
      Then NDA=Today()-[Data de Vencimento]
           TXM=[Multa]
           [Observacoes]="Vencida"
      Else NDA=0
           TXM=0
           [Observacoes]=""
   EndIf

   If [Data Limite Desconto]>=Today()
      Then VRDESCT=[Valor do Desconto]
      Else VRDESCT=0
   EndIf

   If [Taxa de Juros]>0
      Then TXJ=([Taxa de Juros]/[NDias Taxa de Juros])*NDA
           VRJUROS=(([Saldo do Documento]*TXJ)/100)+([Saldo do Documento]*TXM/100)
      Else VRJUROS=(([Valor de Juros]/[NDias Taxa de Juros])*NDA)
           VRJUROS=VRJUROS+(([Saldo do Documento]*TXM)/100)
   EndIf

   [Valor do Documento]=([Saldo do Documento]+VRJUROS)-VRDESCT
   [Valor do Desconto ]=VRJUROS+(VRDESCT*-1)
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_DTINI"         : Campo="TG_EMPRESA"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            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_DTINI"
                    Case Campo="TG_DTINI"  : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"  : 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=""
VSDGCONTA  =""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()

While True
  ShowDialog "Tˇtulos Emitidos e n„o Recebidos" 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 09 Width 56
    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 1,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA" To VSDGEMPRESA
    Accept @ 1,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP

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

    Pushbutton @ 05,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 05,28 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(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
          Loop
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Gerando o Relat˘rio")
  TabelaC=Cria_Temp(PrivDir())

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    Seleciona_01()
    If IsTable(TabelaC)
       Then Add "Answer" TabelaC
            Delete "Answer"
       Else Rename "Answer" TabelaC
            TabelaI=Cria_Temp(PrivDir())
    EndIf

    Seleciona_02()
    If IsTable(TabelaI)
       Then Add "Answer" TabelaI
            Delete "Answer"
       Else Rename "Answer" TabelaI
    EndIf
  EndWhile

  Seleciona_03()

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

  CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1000" "5" TabelaC "R"

  IMPRIREL(TabelaC,"R")

  Delete TabelaC
  Delete TabelaI

EndWhile
ClearAll
Release Vars All
Release Procs All