;** Rm_0010  *******************************************************************
;*  Emissao dos Titulos a Receber por Tipo de Pagamento/Vencimento             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 05/12/1995 por Anivel Augusto Rodrigues
;Ultima Alteracao: 09/07/2004 por Anivel Augusto Rodrigues
;Motivo Alteracao: Opcao Sintetico/Analitico
;Ultima Alteracao: 13/01/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: TOTALIZA EMPRESAS
;Ultima Alteracao: 25/08/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: Correcao Data de Vencimento em Branco
;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc SELECIONA()
ClearAll

If IsTable("\\Sistemas\\Ctarec\\"+Dir_Trab+"Duplic")
  Then {Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupliC" Enter Check
       Moveto [Codigo da Empresa ] TypeIn "~VSDGEMPRESA2.."
       Moveto [Data de Vencimento] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
       Moveto [Saldo do Documento] TypeIn "> 0.01"
       Moveto [Tipo do Pagamento ] TypeIn "~VSDGTPPAGTO.."
       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 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 (Today()-[Data de Vencimento])>=NumVal(VSDGDDPROTESTO)
       Then If [Descricao do Lancamento]="PROTESTADA"
               Then [Observacoes]="Cartorio"
               Else [Observacoes]="Protestar"
            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)

   If VSDGOPRELANALSINT="2"
      Then If [Data de Vencimento]<Today()
              Then [Valor do Documento]=[Saldo do Documento]
                   [Valor do Desconto ]=[Saldo do Documento]
                   [Saldo do Documento]=BlankNum()
              Else [Saldo do Documento]=[Saldo do Documento]
                   [Valor do Desconto ]=[Saldo do Documento]
                   [Valor do Documento]=BlankNum()
           EndIf
   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_DTINI"         : Campo="TG_EMPRESA"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_DDPROTESTO"    : Campo="TG_DTFIM"
            Case Campo="TG_TPPAGTO"       : Campo="TG_DDPROTESTO"
            Case Campo="TG_OPRELANALSINT" : Campo="TG_TPPAGTO"
            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="TG_DDPROTESTO"
                    Case Campo="TG_DDPROTESTO"    : Campo="TG_TPPAGTO"
                    Case Campo="TG_TPPAGTO"       : Campo="TG_OPRELANALSINT"
                    Case Campo="TG_OPRELANALSINT" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DDPROTESTO":
       MSG_CAMPO="Informe o Numero de Dias"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()
VSDGEMPRESA   =""
VSDGTPPAGTO   =""
VSDGDDPROTESTO=""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()

While True
  ShowDialog "Receber por Tipo de Pagamento/Vencimento" 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 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

    Label  @ 04,00 "Protestar apos N§ dias do vencimento"  For "TG_DDPROTESTO"
    Accept @ 5,01 Width 07 "A4"  Picture "#*#" Tag "TG_DDPROTESTO" To VSDGDDPROTESTO

    Label  @ 06,00 "Tipo de Pagamento"  For "TG_TPPAGTO"
    Accept @ 07,01 Width 05 "A2"  Picture "!*!" Tag "TG_TPPAGTO"    To VSDGTPPAGTO
    Accept @ 07,07 Width 23 "A20" Picture "!*!" Tag "TG_NOMTPPG"    To VSDGNOMTPPAGTO

    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

    Pushbutton @ 11,14 width 11 "~E~xecutar"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 11,28 width 11 "~F~echar" CANCEL                    Tag "NO"  To BottonValue
  EndDialog

  If Not RetVal Then QuitLoop EndIf

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

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

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, processando")
  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\\CtaRec\\Rm_1000" "2" Tabela "R"
     Else CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1001" "3" Tabela "R"
  EndIf

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
