;** Rm_0330  *******************************************************************
;*  Emissao dos Titulos por Emitente/EmissÆo                                   *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido    : 08/10/2009 por Cleber
;Ultima Alteracao: 30/07/2012 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes titulo

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

If IsTable("\\Sistemas\\Ctapag\\"+Dir_Trab+"Titulos")
  Then {Ask} TypeIn "\\Sistemas\\Cadastro\\Destinat" Enter
       Moveto [Codigo            ] example "cemit"
       Moveto [Codigo Atividade  ] TypeIn "~VSDGATIVIDADE.."
       {Ask} TypeIn "\\Sistemas\\Ctapag\\"+Dir_Trab+"Titulos" Enter Check
       Moveto [Codigo da Empresa ] TypeIn "~VSDGEMPRESA2.."
       Moveto [Codigo do Emitente] example "cemit" TypeIn ", ~VSDGDESTINAT.."
       Moveto [Data de Emissao   ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM "       
       Moveto [Codigo da Conta   ] TypeIn "~VSDGCONTA.."
       Do_It!
       ClearAll
EndIf

EndProc  ; (Seleciona)

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

;***** 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
   
Coedit Tabela
Scan

   If IsBlank([Data a Pagar])
      Then [Data a Pagar]= Today()
   Else If [Data a Pagar]<Today()
      Then NDA=Today()-[Data a Pagar]
           TXM=[Multa]
      Else NDA=0
           TXM=0
   EndIf
   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)
   [Data de Vencimento]=[Data a Pagar]

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_CONTA"         : Campo="TG_EMPRESA"
            Case Campo="TG_ATIVIDADE"     : Campo="TG_CONTA"
            Case Campo="TG_DESTINAT"      : Campo="TG_ATIVIDADE"
            Case Campo="TG_OPRELANALSINT" : Campo="TG_DESTINAT"
            Case Campo="TG_DTINI"         : Campo="TG_OPRELANALSINT"
            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_CONTA"
                    Case Campo="TG_CONTA"         : Campo="TG_ATIVIDADE"
                    Case Campo="TG_ATIVIDADE"     : Campo="TG_DESTINAT"
                    Case Campo="TG_DESTINAT"      : Campo="TG_OPRELANALSINT"
                    Case Campo="TG_OPRELANALSINT" : 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   =""
VSDGDESTINAT=""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGATIVIDADE =""
VSDGNOMATIV  =""

While True
  ShowDialog "Rela‡Æo de titulos por emitente/emissÆo" 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 17 Width 56

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

    Label  @ 02,00 "Conta"  For "TG_CONTA"
    Accept @ 03,01 Width 07 "A4"  Picture "#*#" Tag "TG_CONTA"    To VSDGCONTA
    Accept @ 03,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMCONTA" To VSDGNOMCONTA

    Label  @ 04,00 "Atividade"  For "TG_ATIVIDADE"
    Accept @ 05,01 Width 07 "A4"  Picture "!*!" Tag "TG_ATIVIDADE" To VSDGATIVIDADE
    Accept @ 05,09 Width 33 "A30"  Picture "#*#" Tag "TG_NOMATIV" To VSDGNOMATIV

    Label  @ 06,00 "Emitente"  For "TG_DESTINAT"
    Accept @ 07,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 07,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMDEST"  To VSDGNOMDEST

    Label  @ 08,00 "Op‡„o do Relat¢rio" For "TG_OPRELANALSINT"
    Accept @ 09,01 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELANALSINT" To VSDGOPRELANALSINT
    Accept @ 09,06 Width 28 "A25"  Picture "!*!" Tag "TG_NOMOPRELANASINT" To VSDGNOMOPRELANALSINT

    Label  @ 10,00 "Periodo Referente" For "TG_DTINI"
    Label  @ 11,00 "de" For "TG_DTINI"
    Accept @ 11,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 11,17 "at‚" For "TG_DTFIM"
    Accept @ 11,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Pushbutton @ 13,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 13,28 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 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
  Processa()

 If VSDGOPRELANALSINT="1"
    Then CopyReport "\\Base\\Sistemas\\CtaPag\\RM_1001" "5" Tabela "R"
    Else CopyReport "\\Base\\Sistemas\\CtaPag\\RM_1010" "12"  Tabela "R"
 EndIf

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
