;** Rm_0125  *******************************************************************
;*  Relatorio de Fretes/ICMS sobre os Fretes                                   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 06/08/2007 por Nikolas
;Ultima Alteracao: 10/07/2012 por Bruno Carvalho
;Motivo Alteracao: Adicionado filtro na ask do NotaI "Situacao_Item"

;*******************************************************************************
;* Rotina p/ Selecionar e Order 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       ] Example "cemp" TypeIn " ,~VSDGEMPRESA.."
        Moveto [Data de Emissao         ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [Numero do Documento     ] Example "num"
        Moveto [Serie do Documento      ] Example "ser"
        Moveto [Codigo da Transportadora] TypeIn "~VSDGTRANSP.."
        Moveto [Situacao                ] TypeIn "Not C"
        If VSDGOPRELANALSINT="1"
           Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaI" Enter Check
                 Moveto [Codigo da Empresa       ] Example "cemp"
                 Moveto [Numero do Documento     ] Example "num"
                 Moveto [Serie do Documento      ] Example "ser"
                 Moveto [Codigo do Item          ] Example "cit"
                 Moveto [Situacao_Item           ] TypeIn "Not C"
                 {Ask} {\\Sistemas\\Estoque\\Itens}
                 Moveto [Codigo do Item           ] Example "cit"
                 Moveto [Fator Conversao Embalagem] Check
                 Moveto [Unidade Embalagem        ] Check
           EndIf
        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_EMPRESA"       : Campo="NO"
            Case Campo="TG_TRANSP"        : Campo="TG_EMPRESA"
            Case Campo="TG_OPRELANALSINT" : Campo="TG_TRANSP"
            Case Campo="TG_DTINI"         : Campo="TG_OPRELANALSINT"
            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_TRANSP"
                    Case Campo="TG_TRANSP"       :Campo="TG_OPRELANALSINT"
                    Case Campo="TG_OPRELANALSINT":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      =""
VSDGTRANSP       =""
VSDGDTINI        =BlankDate()
VSDGDTFIM        =BlankDate()
VSDGOPRELANALSINT="1"

While True

  ShowDialog "Rela‡„o de Fretes e ICMS/Fretes" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 26,26   Height 13 Width 56

    Label  @ 00,00 "Empresa"  For "TG_EMPRESA"
    Accept @ 01,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_EMPRESA"    To VSDGEMPRESA
    Accept @ 01,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMEMPRESA" To VSDGNOMEMP

    Label  @ 02,00 "Transportadora"  For "TG_TRANSP"
    Accept @ 03,01 Width 06 "A4"  Picture "[&,#][#*#]" Tag "TG_TRANSP"    To VSDGTRANSP
    Accept @ 03,08 Width 23 "A20" Picture "!*!" Tag "TG_NOMTRANSP" To VSDGNOMTRANSP

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

    Label  @ 06,00 "Perˇodo Referente" For "TG_DTINI"
    Label  @ 07,00 "de" For "TG_DTINI"
    Accept @ 07,04 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTINI" To VSDGDTINI
    Label  @ 07,17 "at‚" For "TG_DTFIM"
    Accept @ 07,22 Width 13 "D"  Picture "#[#];.##;.##[##]" Tag "TG_DTFIM" To VSDGDTFIM

    Pushbutton @ 09,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 09,28 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
  EndWhile

  If IsEmpty("Answer")
     Then MENSAGEM("N„o existem registros a processar")
          Delete "Answer"
          Loop
  EndIf

  Rename "Answer" Tabela
  {Ask} TypeIn Tabela Enter
  Moveto [Numero do Documento      ] TypeIn "Calc count as Somatorio"
  Moveto [Codigo da Transportadora ] Check
  Do_It!
  ClearAll
  {Ask} TypeIn Tabela Enter
  Moveto [Codigo da Transportadora ] Example "a"
  Moveto [Peso Bruto               ] TypeIn "changeto " Example "b"
  {Ask} TypeIn "Answer" Enter
  Moveto [Codigo da Transportadora ] Example "a"
  Moveto [Somatorio                ] Example "b"
  Do_It!
  ClearAll

  If VSDGOPRELANALSINT="1"
     Then CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2000" "9" Tabela "R"
     Else CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2000" "9" Tabela "R"
  EndIf

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
