;** Rm_0760 **********************************************************************
;* Representante/Tipo Pgto/Cond. Pagto                                         *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 14/07/2003 por Karen Lauar Nery
;Ultima Alteracao: 08/09/2004 por Jos‚ Lu¡s Ameno da Silva
;Motivo Alteracao: PrivDir
;Ultima Alteracao: 15/12/2008 por Anivel Augusto Rodrigues
;Motivo Alteracao: TOTALIZA EMPRESAS
;Ultima Alteracao: 03/10/2013 por Andrey Fillipe          
;Motivo Alteracao: Adicionado filtro por Gerencia
;Ultima Alteracao: 16/06/2014 por Luis Gustavo
;Motivo Alteracao: Ajustes no filtro Gerencia
;Ultima Alteracao: 13/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 17/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes TG_EMPRESATE                 

;*******************************************************************************
;* Rotina para Selecionar Registros                                            *
;*******************************************************************************
Proc Seleciona()
ClearAll
If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
   Then {Ask} TypeIn "\\Sistemas\\Cadastro\\Represen" Enter
           Moveto [Codigo do Representante] Example "crep"
           If VSDGGERENCIA<>""
              Then Moveto [Codigo da Gerencia     ] TypeIn "~VSDGGERENCIA"
              Else Moveto [Codigo da Gerencia     ] TypeIn "~VSDGGERENCIA.."
           EndIf
   
        {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter Check
        Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA2.."
        Moveto [Codigo do Representante] Example "crep" TypeIn "!, ~VSDGREPRESEN.."
        Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [Descricao da Destinacao] TypeIn "..VEND.."
        Moveto [Tipo Movimento Estoque ] TypeIn "S"
        Moveto [Situacao               ] TypeIn "Not C"
        Moveto [Tipo de Pagamento      ] TypeIn "V or P"
        Do_It!
        ClearAll
EndIf
EndProc   ; (Seleciona)


;*******************************************************************************
;* Rotina para Processar  Registros                                            *
;*******************************************************************************
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
   Else {Ask} {\\Sistemas\\Cadastro\\Empresas}
        Moveto [Codigo da Empresa      ] Example "cemp"
        Moveto [Nome Fantasia Empresa  ] Example "nemp"
        {Ask} TypeIn Tabela Enter TypeIn "Fast"
        Moveto [Codigo da Empresa      ] Example "cemp"
        Moveto [Nome Fantasia Empresa  ] TypeIn "changeto _nemp"
        Do_It!
        ClearAll
EndIf
EndProc

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = 9: Temp=False
     Case Evento["KeyCode"] = -59: Dialog_F1()
     Case Evento["KeyCode"] =  27:
          Temp=False
          Switch
            Case Campo="TG_EMPRESATE"       : Campo="NO"
            Case Campo="TG_REPRESEN"      : Campo="TG_EMPRESATE"
            Case Campo="TG_GERENCIA"      : Campo="TG_REPRESEN"
            Case Campo="TG_DTINI"         : Campo="TG_GERENCIA"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            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_GERENCIA"
                    Case Campo="TG_GERENCIA" :Campo="TG_DTINI"
                    Case Campo="TG_DTINI"    :Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"    :Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA =""
VSDGREPRESEN=""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()

While True

  ShowDialog "Representante/Cond.Pagto" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 13 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_NOMEMPRESA" 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 "Gerˆncia " For "TG_GERENCIA"
    Accept @ 05,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_GERENCIA" To VSDGGERENCIA
    Accept @ 05,09 Width 25 "A10"  Picture "!*!" Tag "TG_DESCGERENCIA" To VSDGDESCGERENCIA

    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

    Pushbutton @ 09,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 09,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, gerando o relat¢rio")
  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()

  Sort Tabela on "Tipo de Pagamento"
  ClearAll

  CopyReport "\\Base\\Sistemas\\Vendas\\Rm_1000" "11" Tabela "R"

  IMPRIREL (Tabela,"R")

  Delete Tabela
EndWhile
ClearAll
Release Vars All
Release Procs All
