;** Rm_0125 ********************************************************************
;*  Emissao dos Titulos a Recebidos por Conta / Representantes                 *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 20/05/2002 por Anivel Augusto Rodrigues
;Ultima Alteracao: 02/06/2004 por Davi Pereira Carrano
;Motivo Alteracao: Media de vencimentos nos relatorios
;Ultima Alteracao: 06/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 26/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 08/06/2020 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   

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

If IsTable("\\Sistemas\\Ctarec\\"+Dir_Trab+"Duplic") And
   IsTable("\\Sistemas\\Ctarec\\"+Dir_Trab+"DupBaixa")

   Then {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.."

        {Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupBaixa" Enter Check
        Moveto [Numero do Lancamento] Example "lanc"
        Moveto [Data do Pagamento   ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [Codigo da Conta     ] TypeIn "~VSDGCONTA.."
        Moveto [Tipo da Baixa       ] TypeIn "Blank or NORMAL"
        Do_It!
        ClearAll
EndIf
EndProc  ; (Seleciona)

;*******************************************************************************
;* Funcao que verifica a data de referencia quando o iten e excluido           *
;*******************************************************************************
Proc ATUA_REF()
M_VALORM   =0
VALOR_TOTAL=0
CoEdit Tabela
Scan
  MDIA=[Data de Vencimento]-VSDGDTREF
  If MDIA=0
     Then [Valor da Comissao]= [Valor do Recebimento]        ;;Temporario
     Else [Valor da Comissao]=([Valor do Recebimento]*MDIA)  ;;Temporario
  EndIf
  [NDias Taxa de Juros]=MDIA

  M_VALORM   =M_VALORM   +[Valor da Comissao ]
  VALOR_TOTAL=VALOR_TOTAL+[Valor do Recebimento]
EndScan
Do_It!
ClearAll

M_DIAREF     = Round( ( M_VALORM / VALOR_TOTAL ), 3 )
M_DIAREFI    = Int( M_DIAREF )
M_DIAREFD    = M_DIAREF - M_DIAREFI
If M_DIAREFD > 0.51
   Then M_DIAREFI = M_DIAREFI + 1
EndIf

DTBASE =VSDGDTREF
DTMEDIA=VSDGDTREF+M_DIAREFI
NDIAS  =M_DIAREFI
EndProc  ; (Calcula)


;*******************************************************************************
;* 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_CONTA"         : Campo="TG_EMPRESA"
            Case Campo="TG_REPRESEN"      : Campo="TG_CONTA"
            Case Campo="TG_DTINI"         : Campo="TG_REPRESEN"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_DTREF"         : Campo="TG_DTFIM"
            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_REPRESEN"
                    Case Campo="TG_REPRESEN": Campo="TG_DTINI"
                    Case Campo="TG_DTINI"   : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"   : Campo="TG_DTREF"
                    Case Campo="TG_DTREF"   : 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 =""
VSDGNOMEMP =""
VSDGNOMCONT   =""
VSDGCONTA   =""
VSDGNOMREPR=""
VSDGREPRESEN=""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGDTREF   =Today()

While True

  ShowDialog "T¡tulos Recebidos por Conta/Representante" 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_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 "Conta"  For "TG_CONTA"
    Accept @ 03,01 Width 07 "A4"  Picture "#*#" Tag "TG_CONTA" To VSDGCONTA
    Accept @ 03,09 Width 25 "A30" Picture "!*!" Tag "TG_NOMCONT" To VSDGNOMCONTA

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

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

    Label  @ 08,00 "Data Base" For "TG_DTREF"
    Accept @ 09,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTREF" To VSDGDTREF
    @ 09,17 ?? "<-Calular Data M‚dia"

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

  If IsBlank(VSDGDTFIM)
     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

  ATUA_REF()
  Processa()

  CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1010" "3" Tabela "R"

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
