;** Rm_0233B *******************************************************************
;*  Emissao dos Titulos a Receber por Rota/Cliente/Vencto                    *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 24/03/2003 por Anivel Augusto Rodrigues
;Ultima Alteracao: 03/04/2006 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes
;Ultima Alteracao: 25/08/2009 por Anivel Augusto Rodrigues
;Motivo Alteracao: Correcao Data de Vencimento em Branco
;Ultima Alteracao: 06/09/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 28/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 11/08/2017 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes c˘cigos modelo de relatorios
;Ultima Alteracao: 29/05/2018 por Anivel Augusto Rodrigues
;Motivo Alteracao: Adicionado ValidarModelosRelatorios()

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

{Ask} {\\Sistemas\\Cadastro\\Destinat}
Moveto [Codigo           ] Example "cli"
Moveto [UF               ] Check
Moveto [Cidade           ] Check
Moveto [Codigo da Rota   ] Check TypeIn "~VSDGROTA.."
Moveto [Descricao da Rota] Check
{Ask} {Duplic} Check
Moveto [Codigo do Destinatario ] Example "cli"
Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
Moveto [Data de Vencimento     ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
Moveto [Saldo do Documento     ] TypeIn "> 0.01"
Moveto [Tipo do Pagamento      ] TypeIn "Not \"BO\", Not \"CO\", Not \"DP\""
Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."

Do_It!
ClearAll

EndProc

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

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

If VSDGCODMODELO="02"
   Then
        {Ask} {\\Sistemas\\Cadastro\\RotaCid}
        Moveto [Codigo da Rota] Example "rot"
        Moveto [Ordem         ] Example "ord"
        Moveto [UF            ] Example "uf"
        Moveto [Cidade        ] Example "cid"
        {Ask} {Answer} TypeIn "Fast"
        Moveto [Codigo da Rota     ] Example "rot"
        Moveto [UF                 ] Example "uf"
        Moveto [Cidade             ] Example "cid"
        Moveto [NDias Taxa de Juros] TypeIn "Changeto " Example "ord"
        Do_It!
        ClearAll

EndIf

EndProc    ; (Processa)

;************************************
;* ROTINA PARA Ler/Gravacao Opcao   *
;************************************
Proc ModeloRelatoriosCarregaLista()

VSDGCODMODELO     = ""
VSDGMODELO        = ""

;ModeloRelatoriosAdd("XXX XX maximo 70 XX")

ModeloRelatoriosAdd("Criar") ; linha obrigatoria inicial
ModeloRelatoriosAdd("01  Pr‚-Venda")
ModeloRelatoriosAdd("02  Pronta Entrega")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista

;*******************************************************************************
;* 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_REPRESEN"      : Campo="TG_EMPRESA"
            Case Campo="TG_ROTA"          : Campo="TG_REPRESEN"
            Case Campo="TG_DTINI"         : Campo="TG_ROTA"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_OPRELANALSINT" : Campo="TG_DTFIM"
            Case Campo="TG_CODMODELO"     : 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_REPRESEN"
                    Case Campo="TG_REPRESEN"      : Campo="TG_ROTA"
                    Case Campo="TG_ROTA"          : Campo="TG_DTINI"
                    Case Campo="TG_DTINI"         : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"         : Campo="TG_OPRELANALSINT"
                    Case Campo="TG_OPRELANALSINT" : Campo="TG_CODMODELO"
                    Case Campo="TG_CODMODELO"     : Campo="YES"
                         If VSDGCODMODELO="01"
                            Then VSDGROTA    =""
                                 VSDGDESCROTA=""
                            Else VSDGREPRESEN=""
                                 VSDGNOMREPR =""
                         EndIf
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
       MSG_CAMPO=""
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()

ModeloRelatoriosCarregaLista()

While True
  ShowDialog "Tˇtulos a Receber por Rota/Cobran‡a IV" 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 16 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 "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 "Rota"  For "TG_ROTA"
    Accept @ 05,01 Width 07 "A4"  Picture "#*#" Tag "TG_ROTA" To VSDGROTA
    Accept @ 05,09 Width 25 "A20" Picture "!*!" Tag "TG_DESCROTA"  To VSDGDESCROTA

    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

    @ 03,36 ?? "FPagto BO, CO e DP n„o sai !"

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

    Label  @ 09,00  "Modelo de ~R~elat˘rio"  For "TG_CODMODELO"
    Accept @ 10,01  Width 06 "A3"  Picture "!*!" Tag "TG_CODMODELO" To VSDGCODMODELO
    Accept @ 10,06  Width 63 "A60"  Picture "!*!" Tag "TG_MODELO" To VSDGMODELO        

    Pushbutton @ 12,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 12,36 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  if not ValidarModelosRelatorios() then loop endif         
  If Not Usuario_Vendedor() Then Loop EndIf  

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

  If VSDGCODMODELO="01" And IsBlank(VSDGREPRESEN)
     Then Mensagem("Informe o Representante !")
          Loop
  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()                         ; mudar estrutura das tabs abaixo, acrescentado campos

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

  IMPRIREL("Answer","R")

  Delete "Answer"

EndWhile
ClearAll
Release Vars All
Release Procs All

