;** Rm_0370 *******************************************************************
;* Emissao da Posicao de Pedido                                                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 05/12/1995 por Anivel Augusto Rodrigues
;Ultima Alteracao: 06/05/2003 por Rodrigo Francisco Bento
;Motivo Alteracao: Mudan‡a Base
;Ultima Alteracao: 08/05/2013 por Bruno Carvalho  
;Motivo Alteracao: Ajustes para buscar Mˆs Fechado
;Ultima Alteracao: 14/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   
;Ultima Alteracao: 18/05/2020 por Bruno Carvalho
;Motivo Alteracao: Alterado CopyReport "...\\Vendas\\PedidoI" p/ "...\\Vendas\\RM_0042"

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar Registros                                   *
;*******************************************************************************
Proc Seleciona()
Clearall

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoC") And
   IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoI")
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoC" Enter
        Moveto [Numero do Pedido] Example "Pedido" TypeIn ",~VSDGPEDIDO.."
        If Not IsBlank(VSDGDTINI) And Not IsBlank(VSDGDTFIM)
           Then Moveto [Data de Emissao ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        EndIf
        {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"PedidoI" Enter Check
        Moveto [Numero do Pedido] Example "Pedido"
        Do_It!
        ClearAll
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_PEDIDO"        : Campo="NO"
            Case Campo="TG_DTINI"         : Campo="TG_PEDIDO"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="YES" Or Campo="NO": Campo="TG_PEDIDO"
          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_PEDIDO":
                         VSDGPEDIDO=ControlValue("TG_PEDIDO")
                         If Not Isblank(VSDGPEDIDO)
                            Then VSDGPEDIDO=ALINHM(VSDGPEDIDO,6)
                         EndIf
                         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_PEDIDO":
     MSG_CAMPO="Em Branco = Todos"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGPEDIDO =""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()

While True

  ShowDialog "Emiss„o da Posi‡„o de Pedidos" 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 09 Width 56

    Label  @ 00,00 "Pedido"  For "TG_PEDIDO"
    Accept @ 01,01 Width 09 "A6"  Picture "[&,#][#*#]" Tag "TG_PEDIDO" To VSDGPEDIDO
    Accept @ 01,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMPEDIDO" To VSDGNOMPEDIDO

    Label  @ 02,00 "Per¡odo Emiss„o" For "TG_DTINI"
    Label  @ 03,00 "de" For "TG_DTINI"
    Accept @ 03,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 03,17 "at‚" For "TG_DTFIM"
    Accept @ 03,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Pushbutton @ 05,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 05,28 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  If IsBlank(VSDGDTFIM)
     Then Mensagem("Data Final Invalida !")
  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_0042" "3" Tabela "R"

  IMPRIREL(Tabela,"R")
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
