;** Rm_0256 ********************************************************************
;* Resumo das Saidas por Estado                                                *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 30/03/2007 por Nikolas
;Ultima Alteracao: 28/05/2014 por Bruno Carvalho
;Motivo Alteracao: Alterado Tabela RM_0243 para RM_0244
;Ultima Alteracao: 13/12/2016 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes showdialog                   

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

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC")
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter Check
        Moveto [Codigo da Empresa      ] TypeIn "~VSDGEMPRESA.."
;;;        Moveto [Tipo de Pagamento      ] TypeIn "V Or P"
        Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [CFO                    ] TypeIn "5.. or 6.."
        Moveto [Tipo Movimento Estoque ] TypeIn "S"
        Moveto [Situacao               ] TypeIn "NOT C"
        Do_It!
        ClearAll
EndIf
EndProc

;*******************************************************************************
;* Rotina p/ Selecionar Registros                                              *
;*******************************************************************************
Proc Processa()
ClearAll

{Ask} {Estados}
Moveto [UF       ] Example "est"
Moveto [Descricao] Check
{Ask} TypeIn Tabela Enter Check
Moveto [UF       ] Example "est" TypeIn "!"
Do_It!
ClearAll

View Tabela2
Edit "Answer"
Scan
   Moveto Tabela2
   Moveto [Codigo da Empresa]
   Locate [Answer->Codigo da Empresa],"0",[Answer->UF]
   If Not RetVal
      Then End Down
           [Codigo da Empresa       ]=[Answer->Codigo da Empresa       ]
           [Tipo Registro           ]="0"
           [Forma de Pagamento      ]=[Answer->UF                      ]
           [Descricao Tipo Pagamento]=[Answer->UF]+" - "+[Answer->Descricao]
           [Nome Fantasia Empresa   ]=[Answer->Nome Fantasia Empresa   ]
   EndIf

   If [Answer->Tipo de Pagamento]="V"
      Then [Valor a Vista]=[Valor a Vista]+[Answer->Valor Nota Fiscal]
           [Valor Total  ]=[Valor Total  ]+[Answer->Valor Nota Fiscal]
      Else [Valor a Prazo]=[Valor a Prazo]+[Answer->Valor Nota Fiscal]
           [Valor Total  ]=[Valor Total  ]+[Answer->Valor Nota Fiscal]
      EndIf

   Moveto "Answer"
EndScan
Do_It!
ClearAll

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_EMPRESA"       : Campo="NO"
            Case Campo="TG_DTINI"         : Campo="TG_EMPRESA"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            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_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=""
VSDGDTINI  =BlankDate()
VSDGDTFIM  =BlankDate()

While True

  ShowDialog "Resumo das Saidas por - Estado" 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 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 "Per¡odo Referente de" For "TG_DTINI"
    Accept @ 03,03 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 03,16 "at‚" For "TG_DTFIM"
    Accept @ 03,21 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Pushbutton @ 05,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 05,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(VSDGDTINI)  Or IsBlank(VSDGDTFIM)
     Then Mensagem("Data 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

  Empty "\\Base\\Sistemas\\Vendas\\Rm_0244"
  Tabela2=Cria_Temp(PrivDir())
  Copy "\\Base\\Sistemas\\Vendas\\Rm_0244"  Tabela2

  Processa()

  If IsEmpty(Tabela2)
     Then MENSAGEM("N„o existem registros a processar")
     Else IMPRIREL(Tabela2,"R")
  EndIf

  Delete "Answer"
  Delete Tabela
  Delete Tabela2

EndWhile
ClearAll
Release Vars All
Release Procs All

