;** Rm_0030  *******************************************************************
;* Relacao de Notas Fiscais por Clientes                                       *
;*******************************************************************************
;Autor: Visao/Vise
;Desenvolvido    : 06/04/2004 por Anivel Augusto Rodrigues
;Ultima Alteracao: 10/08/2004 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes
;ultima Alteracao: 21/01/2010 por Humberto F. sAntos.
;Motivo Alteracao: Inclusao Pedido de origem.
;ultima Alteracao: 25/11/2010 por Anivel Augusto Rodrigues
;Motivo Alteracao: Ajustes Layout Relatorio   

;*******************************************************************************
;* 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 [Codigo                 ] TypeIn "~VSDGDESTINAT.."
         Moveto [Forma de Pagamento     ] TypeIn "~VSDGTPPAGTO.."
         Moveto [Codigo da Destinacao   ] TypeIn "~VSDGDESTIN.."
         Moveto [Numero_Carregamento    ] TypeIn "~VSDGNCARREG.."
         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"
         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

{Ask} {PedidoC}
Moveto [Numero do Pedido    ] Example "nped"
Moveto [Numero Pedido Origem] Example "pedori"
{Ask} Typein tabela enter  
Moveto [Numero do Pedido    ] Example "nped"
Moveto [Nome do Autorizado  ] Typein "changeto " Example "pedori"   
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_DESTINAT"      : Campo="TG_EMPRESA"
            Case Campo="TG_DESTIN"        : Campo="TG_DESTINAT"
            Case Campo="TG_TPPAGTO"       : Campo="TG_DESTIN"
            Case Campo="TG_NCARREG"       : Campo="TG_TPPAGTO"
            Case Campo="TG_DTINI"         : Campo="TG_NCARREG"
            Case Campo="TG_DTFIM"         : Campo="TG_DTINI"
            Case Campo="TG_CSITENS"       : Campo="TG_DTFIM"
            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_DESTIN"
                    Case Campo="TG_DESTIN"  : Campo="TG_TPPAGTO"
                    Case Campo="TG_TPPAGTO" : Campo="TG_NCARREG"
                    Case Campo="TG_NCARREG" :
                         If IsBlank(VSDGNCARREG)
                            Then Campo="TG_DTINI"
                            Else Campo="TG_CSITENS"
                         EndIf
                    Case Campo="TG_DTINI"   : Campo="TG_DTFIM"
                    Case Campo="TG_DTFIM"   : Campo="TG_CSITENS"
                    Case Campo="TG_CSITENS" : 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=""
VSDGDESTIN  =""
VSDGTPPAGTO =""
VSDGCSITENS =""
VSDGDTINI   =BlankDate()
VSDGDTFIM   =BlankDate()
VSDGNCARREG =""

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 19 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 "Cliente"  For "TG_DESTINAT"
    Accept @ 03,01 Width 07 "A4"  Picture "[&,#][#*#]" Tag "TG_DESTINAT" To VSDGDESTINAT
    Accept @ 03,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMDEST"  To VSDGNOMDEST

    Label  @ 04,01 "Destinacao"  For "TG_DESTIN"
    Accept @ 05,02 Width 06 "A3"  Picture "#*#" Tag "TG_DESTIN"    To VSDGDESTIN
    Accept @ 05,09 Width 33 "A30" Picture "!*!" Tag "TG_NOMDESTIN" To VSDGNOMDESTIN

    Label  @ 06,02 "Forma de Pagamento"  For "TG_TPPAGTO"
    Accept @ 07,03 Width 05 "A2"  Picture "!*!" Tag "TG_TPPAGTO"    To VSDGTPPAGTO
    Accept @ 07,09 Width 23 "A20" Picture "!*!" Tag "TG_NOMTPPG"    To VSDGNOMTPPAGTO

    Label  @ 08,00 "N§ Carregamento" For "TG_NCARREG"
    Accept @ 09,01 Width 9  "A6"  Picture "#*#" Tag "TG_NCARREG"    To VSDGNCARREG
    Accept @ 09,11 Width 23 "A20" Picture "!*!" Tag "TG_NOMNCARREG" To VSDGNOMNCARREG

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

    Label  @ 12,03 "Op‡„o p/ Itens" For "TG_CSITENS"
    Accept @ 13,04 Width 04 "A1"  Picture "S,C" Tag "TG_CSITENS" To VSDGCSITENS
    Accept @ 13,09 Width 21 "A20"  Picture "!*!" Tag "TG_NOMCSITENS" To VSDGNOMCSITENS

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  If IsBlank(VSDGNCARREG) And 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
  Ajusta_Dados()

  If VSDGCSITENS="C"
     Then CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2001" "R" Tabela "R"
     Else CopyReport "\\Base\\Sistemas\\Vendas\\Rm_2001" "1" Tabela "R"
  EndIf

  IMPRIREL(Tabela,"R")

  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All



