;** Rm_0233  *******************************************************************
;*  Emissao dos Titulos a Receber por Repres/Cliente/Vencto                    *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 24/03/2003 por Anivel Augusto Rodrigues
;Ultima Alteracao: 05/04/2004 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes
;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: 05/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 17/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes TG_EMPRESATE                 
;Ultima Alteracao: 24/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 31/01/2020 por Bruno Carvalho             
;Motivo Alteracao: Adicionado processo para escolher mais de uma forma de pagamento

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

{Ask} {Duplic} Check
Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."
Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
Moveto [Data de Vencimento     ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
Moveto [Saldo do Documento     ] TypeIn "> 0.01"
Switch
  Case ListaRegra_VSDGTPPAGTO_Ativo  : Moveto [Tipo do Pagamento] TypeIn ListaRegra_VSDGTPPAGTO
  Otherwise                          : Moveto [Tipo do Pagamento] TypeIn "~VSDGTPPAGTO.."
EndSwitch  
Do_It!
ClearAll

EndProc

;*******************************************************************************
;* Rotina p/ Processar db Temporaria                                           *
;*******************************************************************************
Proc Processa()

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

Coedit "Answer"
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)

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"] = -60 Or 
          Evento["KeyCode"] = "F2" : If Campo="TG_TPPAGTO"                      
                                        Then ListaRegra_F2() 
                                     EndIf     
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESATE"       : Campo="NO"
            Case Campo="TG_REPRESEN"      : Campo="TG_EMPRESATE"
            Case Campo="TG_DTINI"         : Campo="TG_REPRESEN"
            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_EMPRESATE"
          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_EMPRESATE"       : Campo="TG_REPRESEN"
                    Case Campo="TG_REPRESEN"      : 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="":
       MSG_CAMPO=""
  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   =""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()
VSDGTPPAGTO   =""
VSDGDDPROTESTO=""
VSDGREPRESEN=""

ListaRegra_TGAtivado="[TG_TPPAGTO]"

While True
  ShowDialog "Tˇtulos a Receber por Representante/Cliente" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140, -60, "F2"  
   @ 05,12   Height 11 Width 72

    Label  @ 00,00 "Empresa"  For "TG_EMPRESATE"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESATE" To VSDGEMPRESA
    Accept @ 01,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMEMP" To VSDGNOMEMP   
    
    Label  @ 02,00 "Representante"  For "TG_REPRESEN"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_REPRESEN" To VSDGREPRESEN
    Accept @ 03,09 Width 25 "A20" Picture "!*!" Tag "TG_NOMREPR"  To VSDGNOMREPR

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

    Label  @ 00,35 "Protestar ap˘s"  For "TG_DDPROTESTO"
    Accept @ 01,36 Width 07 "A4"  Picture "#*#" Tag "TG_DDPROTESTO" To VSDGDDPROTESTO
    Label  @ 01,43 "Dias em atraso"  For "TG_DDPROTESTO"

    Label  @ 02,35 "Forma de Pagamento"  For "TG_TPPAGTO"
    Accept @ 03,38 Width 05 "A2"  Picture "!*!" Tag "TG_TPPAGTO"    To VSDGTPPAGTO
    Accept @ 03,44 Width 25 "A20" Picture "!*!" Tag "TG_NOMTPPG"    To VSDGNOMTPPAGTO

    Label  @ 04,35 "Op‡„o do Relat˘rio" For "TG_OPRELANALSINT"
    Accept @ 05,39 Width 04 "A1"  Picture "1,2" Tag "TG_OPRELANALSINT" To VSDGOPRELANALSINT
    Accept @ 05,44 Width 25 "A25"  Picture "!*!" Tag "TG_NOMOPRELANASINT" To VSDGNOMOPRELANALSINT

    Pushbutton @ 07,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 07,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 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")
  SELECIONA()

  If IsEmpty("Answer")
     Then MENSAGEM("N„o existem registros a processar")
          Loop
  EndIf
  PROCESSA()

  If VSDGOPRELANALSINT="1"
     Then CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_0230" "4" "Answer" "R"
     Else CopyReport "\\Base\\Sistemas\\CtaRec\\Rm_0230" "5" "Answer" "R"
  EndIf

  IMPRIREL("Answer","R")

  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All
