;** RM_0084 ********************************************************************
;* Rela‡Æo de notas emitidas por item/lote                                     *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 19/08/2020 por Bruno Carvalho
;Ultima Alteracao:   /  /     por                                            
;Motivo Alteracao: 

;*******************************************************************************
;* Processa Dados                                                              *
;*******************************************************************************
Proc Seleciona()

If IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC") And
   IsTable("\\Sistemas\\Vendas\\"+Dir_Trab+"NotaIL") 
   Then {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaIL" Enter
        Moveto [Codigo da Empresa      ] Check TypeIn "~VSDGEMPRESA.."
        Moveto [Numero do Documento    ] Check 
        Moveto [Serie do Documento     ] Check 
        Moveto [LoteNumero             ] Check TypeIn "~VSDGLOTE.."
        Moveto [Codigo do Item         ] Check TypeIn "~VSDGITEM.."
        Moveto [LoteQuantidade         ] Check
        Moveto [LoteDataValidade       ] Check
        Moveto [LoteDataFabricacao     ] Check
        Do_It!
        ClearAll
        
        If Not IsEmpty("Answer")
           Then {Ask} {Answer} Check
                Moveto [Codigo da Empresa      ] Example "cemp"
                Moveto [Numero do Documento    ] Example "ndoc"
                Moveto [Serie do Documento     ] Example "sdoc"
                {Ask} TypeIn "\\Sistemas\\Vendas\\"+Dir_Trab+"NotaC" Enter
                Moveto [Codigo da Empresa      ] Example "cemp" 
                Moveto [Numero do Documento    ] Example "ndoc"
                Moveto [Serie do Documento     ] Example "sdoc"
                Moveto [Tipo do Documento      ] Check
                Moveto [Nome Fantasia Empresa  ] Check
                Moveto [Data de Emissao        ] Check TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
                Moveto [Tipo Movimento Estoque ] TypeIn "S"
                Moveto [Situacao               ] TypeIn "Not C"
                Moveto [CFO                    ] Check
                Moveto [Codigo                 ] Check
                Moveto [Nome Fantasia EmitDest ] Check
                Moveto [Cidade                 ] Check
                Moveto [UF                     ] Check
                Moveto [Bairro                 ] Check
                Do_It!
                ClearAll   
                
                {Ask} {Answer} Check
                Moveto [Codigo             ] Example "Cod"
                {Ask} {\\Sistemas\\Cadastro\\Destinat}
                Moveto [Codigo             ] Example "Cod"
                Moveto [Telefone           ] Check
                Moveto [Celular            ] Check
                Moveto [Endereco_Eletronico] Check
                Do_It!
                ClearAll
                
                {Ask} {Answer} Check
                Moveto [Codigo do Item     ] Example "Cod"
                {Ask} {\\Sistemas\\Estoque\\Itens}
                Moveto [Codigo do Item     ] Example "Cod"
                Moveto [Descricao do Item  ] Check
                Do_It!
                ClearAll
        EndIf        
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_ITEM"           : Campo="TG_EMPRESA"
            Case Campo="TG_LOTE"           : Campo="TG_ITEM"
            Case Campo="TG_DTINI"          : Campo="TG_LOTE"
            Case Campo="TG_DTFIM"          : Campo="TG_DTINI"
            Case Campo="TG_CODMODELO"      : 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_ITEM"
                    Case Campo="TG_ITEM"      : Campo="TG_LOTE" 
                    Case Campo="TG_LOTE"      : Campo="TG_DTINI"
                         VSDGLOTE = ControlValue("TG_LOTE")
                    Case Campo="TG_DTINI"     : Campo = "TG_DTFIM"
                    Case Campo="TG_DTFIM"     : Campo="TG_CODMODELO"   
                    Case Campo="TG_CODMODELO" : Campo="YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_LOTE":
       MSG_CAMPO="Informe o numero do lote"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc
              
;************************************
;* ROTINA PARA Ler/Gravacao Opcao   *
;************************************
Proc ModeloRelatoriosCarregaLista()

VSDGCODMODELO     = ""
VSDGMODELO        = ""

;ModeloRelatoriosAdd("XXX XX maximo 70 XX")

ModeloRelatoriosAdd("Criar") ; linha obrigatoria inicial
ModeloRelatoriosAdd("01  Rela‡Æo de Nota/Item/Lote")
ModeloRelatoriosAdd("02  Rela‡Æo de Item por Nota/Lote")
ModeloRelatoriosAdd("03  Rela‡Æo de Item por Lote/Nota")
;ModeloRelatoriosAdd("Separator")
ModeloRelatoriosAdd("Fim")
                           
EndProc ;ModeloRelatoriosCarregaLista
              
;*******************************************************************************
;* Rotina Principal                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()

SYS_VALOR()  

ModeloRelatoriosCarregaLista()

VSDGEMPRESA = ""
VSDGNOMEMP  = ""
VSDGLOTE    = ""
VSDGITEM    = ""
VSDGNOMITEM = ""
VSDGDTINI   = BlankDate()
VSDGDTFIM   = BlankDate()  

While True
  ShowDialog "Notas Emitidas por Item\\Lote" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"                                                   
  Key "F1","ENTER","ESC","TAB",
    -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 09,12 Height 16 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 "Item"  For "TG_ITEM"
    Accept @ 03,01 Width 13 "A10"  Picture "#*#" Tag "TG_ITEM"    To VSDGITEM
    Accept @ 03,15 Width 38 "A35" Picture "!*!" Tag "TG_NOMITEM"  To VSDGNOMITEM
    
    Label  @ 04,00 "Numero do Lote"  For "TG_LOTE"
    Accept @ 05,01 Width 17 "A14"  Picture "!*!" Tag "TG_LOTE"    To VSDGLOTE

    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
    
    Label  @ 09,00  "Modelo de ~R~elat¢rio"  For "TG_CODMODELO"
    Accept @ 10,01  Width 06 "A3"  Picture "!*!" Tag "TG_CODMODELO" To VSDGCODMODELO
    Accept @ 10,06  Width 47 "A60"  Picture "!*!" Tag "TG_MODELO" To VSDGMODELO           

    Pushbutton @ 12,22 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 12,36 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 Not IsAssigned(Tabela) Or Not IsTable(Tabela) Or IsEmpty(Tabela)
     Then MENSAGEM("N„o Existem Registros a Processar")
          If IsAssigned(Tabela) And IsTable(Tabela) Then Delete Tabela EndIf
          Loop
  EndIf

  Switch
    Case VSDGCODMODELO="01" : CopyReport "\\Base\\Sistemas\\Vendas\\RM_0084" "R" Tabela "R"
    Case VSDGCODMODELO="02" : CopyReport "\\Base\\Sistemas\\Vendas\\RM_0084" "1" Tabela "R"
    Case VSDGCODMODELO="03" : CopyReport "\\Base\\Sistemas\\Vendas\\RM_0084" "2" Tabela "R"
  EndSwitch
  IMPRIREL(Tabela,"R")

  Delete Tabela
EndWhile
Release Vars All
Release Procs All