;** Rm_0080 *******************************************************************
;* Relacao de Venda e Devolucao por Cliente                                    *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 05/12/1995 por Anivel Augusto Rodrigues
;Ultima Alteracao: 08/09/2004 por Jos‚ Luˇs Ameno da Silva
;Motivo Alteracao: PrivDir
;Ultima Alteracao: 10/07/2012 por Bruno Carvalho
;Motivo Alteracao: Adicionado filtro na ask do NotaI "Situacao_Item"
;Ultima Alteracao: 12/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog 
;Ultima Alteracao: 28/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes ShowDialog TG_DESTINAT                  
;Ultima Alteracao: 17/03/2020 por Bruno Carvalho
;Motivo Alteracao: Adicionado "Bib_Proc"  

;*******************************************************************************
;* Rotina p/ Selecionar Registros                                              *
;*******************************************************************************
Proc Seleciona()

ClearAll
If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC") And
   IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI")
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter Check
         Moveto [Codigo da Empresa      ] Example "Empresa" TypeIn ",~VSDGEMPRESA.."
         Moveto [Numero do Documento    ] Example "Docto" TypeIn ", >=~VSDGROINI,<=~VSDGROFIM"
         Moveto [Serie do Documento     ] Example "Serie"
         Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM "
         Moveto [Codigo                 ] TypeIn "~VSDGDESTINAT.."
         Moveto [Descricao da Destinacao] TypeIn "..VEND.. Or ..DEVOL.. Or ..SAIDA.. "
         Moveto [Codigo do Representante] TypeIn "~VSDGREPRESEN.."
         Moveto [Situacao               ] TypeIn "Not C"
         {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI" Enter Check
         Moveto [Codigo da Empresa      ] Example "Empresa"
         Moveto [Numero do Documento    ] Example "Docto"
         Moveto [Serie do Documento     ] Example "Serie"
         Moveto [Situacao_Item          ] TypeIn "Not C"
         Do_It!
         ClearAll

         If Not IsEmpty("Answer")
            Then {Ask} {Answer} TypeIn "Fast"
                 Moveto [Descricao da Destinacao] TypeIn "..DEVOL.."
                 Moveto [Quantidade             ] Example "qtde" TypeIn ",Changeto " Example "qtde" TypeIn "*-1"
                 Moveto [Preco Total            ] Example "vr" TypeIn ",Changeto " Example "vr" TypeIn "*-1"
                 Do_It!
                 ClearAll
         EndIf
EndIf

EndProc    ; (Seleciona)

;*******************************************************************************
;* 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_EMPRESA"       : Campo="NO"
            Case Campo="TG_DESTINAT"      : Campo="TG_EMPRESA"
            Case Campo="TG_REPRESEN"      : Campo="TG_DESTINAT"
            Case Campo="TG_DTINI"         : Campo="TG_REPRESEN"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_ROINI"         : Campo="TG_DTFIM"
            Case Campo="TG_ROFIM"         : Campo="TG_ROINI"
            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":Campo="TG_REPRESEN"
                    Case Campo="TG_REPRESEN":Campo="TG_DTINI"
                    Case Campo="TG_DTINI"   :Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"   :Campo="TG_ROINI"
                    Case Campo="TG_ROINI"   :
                         VSDGROINI = ControlValue("TG_ROINI")
                         If IsBlank(VSDGROINI)
                            Then VSDGROINI="000001"
                         Endif
                         Campo="TG_ROFIM"
                    Case Campo="TG_ROFIM"   :
                         VSDGROFIM = ControlValue("TG_ROFIM")
                         If IsBlank(VSDGROFIM)
                            Then VSDGROFIM="999999"
                         Endif
                         Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DTFIM":
     MSG_CAMPO="Informe a Data Final"
  Case Campo="TG_ROINI":
     MSG_CAMPO="Branco = 000001"
  Case Campo="TG_ROFIM":
     MSG_CAMPO="Branco = 999999"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc


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

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

VSDGROINI   =""
VSDGROFIM   =""

While True

  ShowDialog "Rela‡„o de Vendas - Devolu‡„o" 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 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_NOMEMPRESA" 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 25 "A20" Picture "!*!" Tag "TG_NOMDEST"  To VSDGNOMDEST

    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 "Romaneio Referente de" For "TG_ROINI"
    Accept @ 09,03 Width 13 "A6"  Picture "[&,#][#*#]" Tag "TG_ROINI" To VSDGROINI
    Label  @ 09,16 "at‚" For "TG_ROFIM"
    Accept @ 09,21 Width 13 "A6"  Picture "[&,#][#*#]" Tag "TG_ROFIM" To VSDGROFIM

    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, 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

  CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2000" "2" Tabela "R"

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All

