;** Rm_0034  *******************************************************************
;* Relacao de Notas Fiscais por Fornecedor                                     *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido    : 27/05/2013 por Bruno Carvalho
;Ultima Alteracao: 28/12/2016 por Rafael Matos
;Motivo Alteracao: Ajustes ShowDialog TG_DESTINAT 

;*******************************************************************************
;* Rotina p/ Selecionar e Ordernar 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"
        Moveto [Serie do Documento     ] Example "Serie"
        If IsBlank(VSDGNCARREG)
           Then Moveto [Data de Emissao        ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        EndIf
        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 [Codigo do Item         ] Example "CodI" TypeIn ",~VSDGITEM.."
        Moveto [Situacao_Item          ] TypeIn "Not C"
        {Ask} {\\Sistemas\\Estoque\\Itens}
        Moveto [Codigo do Item         ] Check Example "CodI"
        Moveto [Codigo Fornecedor      ] Check TypeIn "~VSDGCODFOR.."
        Do_It!
        ClearAll
EndIf

EndProc   ; (Seleciona)

;*******************************************************************************
;* Rotina Ajusta Dados                                                         *
;*******************************************************************************
Proc Ajusta_Dados()
ClearAll
         
Sort Tabela On "Codigo da Empresa", "Numero do Documento", "Serie do Documento","Item do Documento"

CDEMPR="****"
NDOCTO="****"
SDOCTO="****"

CoEdit Tabela
Scan
 If [Codigo da Empresa  ]<>CDEMPR Or
    [Numero do Documento]<>NDOCTO Or
    [Serie do Documento ]<>SDOCTO
    Then CDEMPR=[Codigo da Empresa  ]
         NDOCTO=[Numero do Documento]
         SDOCTO=[Serie do Documento ]
    Else [Valor Nota Fiscal]=BlankNum()
         [Valor Mercadoria ]=BlankNum()
         [Peso Liquido     ]=BlankNum()
 EndIf
 [Nome do Autorizado]=""
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: 
          LOCALIZAR_ITEM_POR="CODFOR"
          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_DTINI"         : Campo="TG_DESTINAT"
            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_DESTINAT"
                    Case Campo="TG_DESTINAT": Campo="TG_DTINI"
                         If SYS_CODIGO_ITEM="0322"
                            Then VSDGDESTINAT=ControlValue("TG_DESTINAT")
                                 If Not IsBlank(VSDGDESTINAT)
                                    Then VSDGNOMDEST=""
                                         View NOWINDOW "\\Sistemas\\Estoque\\Itens"
                                         Moveto [Codigo Fornecedor]
                                         Locate VSDGDESTINAT
                                         If Not RetVal
                                            Then MENSAGEM("Fornecedor n„o encontrado !")
                                                 Campo="TG_DESTINAT"
                                         EndIf
                                         ClearImage
                                    Else VSDGNOMDEST="Todos"
                                 EndIf  
                         EndIf
                    Case Campo="TG_DTINI"   : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"   : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="":
     MSG_CAMPO=""
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

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

While True

  ShowDialog "Rela‡„o Notas Fiscais - Clientes" 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 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 "Item"  For "TG_ITEM"
    Accept @ 03,01 Width 13 "A10" Picture "#*#" Tag "TG_ITEM"     To VSDGITEM
    Accept @ 03,15 Width 37 "A35" Picture "!*!" Tag "TG_NOMITEM"  To VSDGNOMITEM

    Label  @ 04,00 "Pessoa"  For "TG_DESTINAT"
    Accept @ 05,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 05,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMDEST"  To VSDGNOMDEST

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  If Not Valida_ShowDialog("") Then 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
  Ajusta_Dados()

  CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2001" "R" Tabela "R"

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
