;** Rm_0080 ********************************************************************
;*  Emissao dos Titulos a Receber por Cliente                                  *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 05/12/1995 por Anivel Augusto Rodrigues
;Ultima Alteracao: 17/02/2004 por Anivel Augusto Rodrigues
;Motivo Alteracao: Mudan‡a Tabela Temporaria PrivDir()
;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
;Ultima Alteracao: 28/11/2011 por Karina Andrade
;Motivo Alteracao: Add op‡Æo ordem de impressÆo
;Ultima Alteracao: 18/12/2013 por Andrey Fillipe
;Motivo Alteracao: Inclusao de data media
;Ultima Alteracao: 21/01/2015 por Andrey Fillipe
;Motivo Alteracao: Adicionado Modelo Com Saldo anterior Report RM_1000a "2"
;Ultima Alteracao: 06/09/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(PARAM)
Private PARAM
ClearAll
If IsTable("\\Sistemas\\Ctarec\\"+Dir_Trab+"Duplic")
  Then {Ask} TypeIn "\\Sistemas\\Ctarec\\"+Dir_Trab+"DupliC" Enter Check
       Moveto [Codigo da Empresa ] TypeIn "~VSDGEMPRESA2.."
       If VSDGOPSIT = "2" 
          Then Moveto [Codigo do Destinatario] Example "cod" TypeIn " ,~VSDGDESTINAT.."
          Else Moveto [Codigo do Destinatario] TypeIn "~VSDGDESTINAT.."
       EndIf
       If PARAM = 2
          Then If Not IsBlank(VSDGDTINI)
                  Then Moveto [Data de Vencimento    ] TypeIn "<~VSDGDTINI"
               EndIf
          Else Moveto [Data de Vencimento    ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
       EndIf
       Moveto [Saldo do Documento    ] TypeIn "> 0.01"
       Moveto [Tipo do Lancamento    ] TypeIn "~VSDGTPCART.."
       If VSDGOPSIT="2"
          Then If IsTable("\\Sistemas\\Cadastro\\"+Dir_Trab+"Destinat")
                  Then {Ask} {\\Sistemas\\Cadastro\\Destinat}
                       Moveto [Codigo             ] Example "cod"
                       Moveto [Situacao do Cliente] TypeIn "PESSIMO"
               EndIf
       EndIf
       Do_It!
       ClearAll
EndIf

If VSDGOPSIT="3"
   Then If IsTable("\\Sistemas\\Cadastro\\"+Dir_Trab+"Destinat")
          Then {Ask} {\\Sistemas\\Cadastro\\Destinat}
               Moveto [Codigo             ] Example "cod"
               Moveto [Situacao do Cliente] TypeIn "PESSIMO"
               {Ask} {Answer} TypeIn "Fast Delete"
               Moveto [Codigo do Destinatario] Example "cod"
               Do_It!
               ClearAll
        EndIf
EndIf

If PARAM = 2 And Not IsEmpty("Answer") 
   Then {Ask} TypeIn Tabela Enter
        Moveto [Valor da Comissao] TypeIn "Changeto 0.00"
        Do_It!
        ClearAll
   
        If Not IsBlank(VSDGDTINI)
           Then Processa("Answer")
   
                {Ask} {Answer}
                Moveto [Codigo do Destinatario] Check
                Moveto [Valor do Documento    ] TypeIn "Calc Sum as Valor do Documento"
                Do_It!
                ClearAll
                
                {Ask} {Answer}
                Moveto [Codigo do Destinatario] Check Example "a"
                Moveto [Valor do Documento    ] Check
                {Ask} TypeIn Tabela Enter
                Moveto [Codigo do Destinatario] Example "a"
                Do_It!
                ClearAll
                
                
                View Tabela
                Coedit "Answer"
                Scan
                  COD = [Codigo do Destinatario]
                  
                  Moveto Tabela
                  Moveto [Codigo do Destinatario]
                  Locate COD
                  If RetVal
                     Then [Valor da Comissao] = [Answer->Valor do Documento]
                  EndIf    
                
                  Moveto "Answer"
                EndScan
                Do_It!
                ClearAll
        EndIf             
EndIf   

Release Vars PARAM
EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina p/ Selecionar os Vencimentos                                         *
;*******************************************************************************
Proc Selecionav()

{Ask} TypeIn Tabela Enter
Moveto [Codigo da Empresa     ] Check
Moveto [Codigo do Destinatario] Check
Moveto [Data de Vencimento    ] TypeIn "Calc Average as Data de Vencimento"

Do_It!
ClearAll

View "Answer"
Coedit Tabela
Scan
  CODEMP = [Codigo da Empresa]
  CODDES = [Codigo do Destinatario]
  
  Moveto "Answer"
  Moveto [Codigo da Empresa]
  Locate CODEMP,CODDES
  If RetVal
     Then  Moveto Tabela 
           Moveto [Data Limite Desconto]
           []=[Answer->Data de Vencimento]
           UnLockRecord
  EndIf
EndScan
Do_It!
ClearAll

EndProc   ; (Selecionav)

;*******************************************************************************
;* Rotina p/ processar Db Temporaria                                           *
;*******************************************************************************
Proc Processa(TabelaRef)
Private TabelaRef

;***** Ajusta Dados ************************************
If VSDGEMPRESA="0000"
   Then {Ask} TypeIn TabelaRef Enter TypeIn "Fast"
        Moveto [Codigo da Empresa      ] TypeIn "changeto 0000"
        Moveto [Nome Fantasia Empresa  ] TypeIn "changeto TOTALIZA EMPRESAS"
        Do_It!
        ClearAll
EndIf

Coedit TabelaRef
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=Round((([Saldo do Documento]*TXJ)/100)+([Saldo do Documento]*TXM/100),2)
      Else VRJUROS=Round((([Valor de Juros]/[NDias Taxa de Juros])*NDA),2)
           VRJUROS=VRJUROS+Round((([Saldo do Documento]*TXM)/100),2)
   EndIf

   [Valor do Documento]=([Saldo do Documento]+VRJUROS)-VRDESCT
   [Valor do Desconto ]=VRJUROS+(VRDESCT*-1)

EndScan
Do_It!
ClearAll

Release Vars TabelaRef
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_DESTINAT"      : Campo="TG_EMPRESA"
            Case Campo="TG_OPSIT"         : Campo="TG_DESTINAT"
            Case Campo="TG_DTINI"         : Campo="TG_OPSIT"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_DDPROTESTO"    : Campo="TG_DTFIM"
            Case Campo="TG_TPCART"        : Campo="TG_DDPROTESTO"
            Case Campo="TG_MODELOREL"     : Campo="TG_TPCART"
            Case Campo="TG_OPRELANALSINT" : Campo="TG_MODELOREL"
            Case Campo="TG_OPRELCODALF"   : Campo="TG_OPRELANALSINT"
            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_DESTINAT"
                    Case Campo="TG_DESTINAT"   :
                         VSDGOPSIT="1"
                         VSDGNOMOPSIT="Todas"
                         Campo="TG_OPSIT"
                    Case Campo="TG_OPSIT"      : Campo="TG_DTINI"
                         VSDGOPSIT=ControlValue("TG_OPSIT")
                         Switch
                           Case VSDGOPSIT="1": VSDGNOMOPSIT="TODAS"
                           Case VSDGOPSIT="2": VSDGNOMOPSIT="PESSIMA"
                           Case VSDGOPSIT="3": VSDGNOMOPSIT="BOA/REGULAR"
                         EndSwitch
                    Case Campo="TG_DTINI"         : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"         : Campo="TG_DDPROTESTO"
                    Case Campo="TG_DDPROTESTO"    : Campo="TG_TPCART"
                    Case Campo="TG_TPCART"        : Campo="TG_MODELOREL"
                    Case Campo="TG_MODELOREL"     : Campo="YES" 
                         VSDGMODELOREL=ControlValue("TG_MODELOREL")
                         Switch
                           Case VSDGMODELOREL="2": VSDGNOMMODELOREL="COM SALDO ANTERIOR"
                                VSDGOPRELANALSINT    = "1"
                                VSDGNOMOPRELANALSINT = "Anal¡tico"
                                VSDGOPRELCODALF      = "1"
                                VSDGNOMOPRELCODALF   = "C¢digo"
                                Campo="YES"
                           OtherWise:
                                VSDGMODELOREL="1"  VSDGNOMMODELOREL="PADRAO"
                                Campo="TG_OPRELANALSINT"
                         EndSwitch
                    Case Campo="TG_OPRELANALSINT" : Campo="TG_OPRELCODALF"
                    Case Campo="TG_OPRELCODALF"   : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DDPROTESTO":
     MSG_CAMPO="Informe o Numero de Dias"
  Case Campo="TG_OPSIT":
       MSG_CAMPO="1- Todas  2- Pessima  3- Boa/Regular"
  Case Campo="TG_MODELOREL" :
       MSG_CAMPO="Modelo: 1= PadrÆo 2= Com Saldo Anterior "     
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA      = ""
VSDGDESTINAT     = ""
VSDGTPCART       = ""
VSDGDDPROTESTO   = ""
VSDGDTINI        = BlankDate()
VSDGDTFIM        = BlankDate()
VSDGOPSIT        = ""
VSDGNOMOPSIT     = ""
VSDGMODELOREL    = ""
VSDGNOMMODELOREL = ""

While True
  ShowDialog "Titulos a Receber por Cliente" 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 68
   
    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 "Pessoa"  For "TG_DESTINAT"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 03,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMCLI" To VSDGNOMDEST

    Label  @ 04,00 "Op‡„o Cliente" For "TG_OPSIT"
    Accept @ 05,04 Width 04 "A1"  Picture "1,2,3" Tag "TG_OPSIT" To VSDGOPSIT
    Accept @ 05,09 Width 23 "A11"  Picture "!*!" Tag "TG_NOMOPSIT" To VSDGNOMOPSIT

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

    Label  @ 00,33 "Protestar ap¢s"  For "TG_DDPROTESTO"
    Accept @ 01,34 Width 07 "A4"  Picture "#*#" Tag "TG_DDPROTESTO" To VSDGDDPROTESTO
    Label  @ 01,41 "Dias em atraso"  For "TG_DDPROTESTO"

    Label  @ 02,33 "Tipo de Carteira"  For "TG_TPCART"
    Accept @ 03,37 Width 04 "A1"  Picture "!" Tag "TG_TPCART"  To VSDGTPCART
    Accept @ 03,42 Width 23 "A20" Picture "!*!" Tag "TG_NOMCART" To VSDGNOMCART

    Label  @ 04,33 "Modelo Relat¢rio" For "TG_MODELOREL"
    Accept @ 05,37 Width 04 "A1"   Picture "1,2" Tag "TG_MODELOREL" To VSDGMODELOREL
    Accept @ 05,42 Width 23 "A20"  Picture "!*!" Tag "TG_NOMMODELOREL" To VSDGNOMMODELOREL

    Label  @ 06,33 "Op‡„o do Relat¢rio" For "TG_OPRELANALSINT"
    Accept @ 07,37 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELANALSINT" To VSDGOPRELANALSINT
    Accept @ 07,42 Width 23 "A25"  Picture "!*!" Tag "TG_NOMOPRELANASINT" To VSDGNOMOPRELANALSINT
    
    Label  @ 08,33 "Ordem" For "TG_OPRELCODALF"
    Accept @ 09,37 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELCODALF" To VSDGOPRELCODALF
    Accept @ 09,42 Width 23 "A12" Picture "!*!" Tag "TG_NOMOPRELCODALF" To VSDGNOMOPRELCODALF

    Pushbutton @ 11,20 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 11,34 width 11 "~C~ancela" 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 Dados")
  Tabela=Cria_Temp(PrivDir())

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    If IsTable("Answer")     Then Delete "Answer" EndIf
    Seleciona(1)
    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(Tabela)
  Selecionav()
  Seleciona(2)
  
  If VSDGMODELOREL="2"
     Then CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1000a" "2" Tabela "R"
     Else If VSDGOPRELANALSINT="1"
             Then If VSDGOPRELCODALF="1"
                    Then CopyReport "a" "12" Tabela "R"
                    Else CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1001" "13" Tabela "R"
                  EndIf  
             Else If VSDGOPRELCODALF="1"
                    Then CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1001" "5"  Tabela "R"
                    Else CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_1001" "14" Tabela "R"
                  EndIf  
          EndIf
  EndIf        
  
  IMPRIREL(Tabela,"R")

  Delete Tabela
  
EndWhile
ClearAll
Release Vars All
Release Procs All
