;** Rm_0151 ********************************************************************
;*  Consumo Por Familia/Referencia                                             *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 17/07/2006 por Anivel Augusto Rodrigues
;Ultima Alteracao: 12/04/2011 por Anivel Augusto Rodrigues
;Motivo Alteracao: Inclusao TG_REFERENCIA/BIB_SHOW
;Ultima Alteracao: 26/04/2011 por Anivel Augusto Rodrigues
;Motivo Alteracao: Exlusao Agrupamento referencia 

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

If IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"RqCabeca") And
   IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"RqItem")
   Then {Ask} {\\Sistemas\\Estoque\\Itens}
        Moveto [Codigo do Item        ] Example "ITEN"
        Moveto [Codigo da Familia     ] Check TypeIn "~VSDGFAMI.."
        Moveto [Descricao da Familia  ] Check
        Moveto [Codigo da Marca       ] TypeIn "~VSDGMARCA.."
        Moveto [Referencia            ] TypeIn "~VSDGREFERENCIA.."
        {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"RqCabeca" Enter
        Moveto [Numero do Lancamento  ] Example "NUMERO"
        Moveto [Codigo da Empresa     ] Check TypeIn "~VSDGEMPRESA.."
        Moveto [Nome Fantasia Empresa ] Check
        Moveto [Data de Ocorrencia    ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [Tipo de Movimento     ] TypeIn "S"
        Moveto [Tipo de Operacao      ] TypeIn "C"
        Moveto [Requisitante          ] Check
        Moveto [Descricao Requisitante] Check
        {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"RqItem" Enter
        Moveto [Numero do Lancamento  ] Example "NUMERO"
        Moveto [Tipo do Item          ] Check TypeIn "~VSDGTPITEM.."
        Moveto [Codigo do Item        ] Check Example "ITEN" TypeIn ", ~VSDGITEM.."
        Moveto [Descricao do Item     ] Check
        Moveto [Unidade Interna       ] Check
        Moveto [Quantidade            ] TypeIn "Calc Sum as Quantidade"
        Moveto [Custo Total           ] TypeIn "Calc Sum as Custo Total"
        Moveto [Aplicacao             ] Check
        Moveto [Descricao Aplicacao   ] Check
        Do_It!
        ClearAll

        Add "Answer" Tabela
        ClearAll

EndIf

If IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"RqCabeca") And
   IsTable("\\Sistemas\\Estoque\\"+Dir_Trab+"RqMatPri")
   Then {Ask} {\\Sistemas\\Estoque\\Itens}
        Moveto [Codigo do Item        ] Example "ITEN"
        Moveto [Codigo da Familia     ] Check TypeIn "~VSDGFAMI.."
        Moveto [Descricao da Familia  ] Check
        Moveto [Codigo da Marca       ] TypeIn "~VSDGMARCA.."
        Moveto [Referencia            ] TypeIn "~VSDGREFERENCIA.."
        {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"RqCabeca" Enter
        Moveto [Numero do Lancamento  ] Example "NUMERO"
        Moveto [Codigo da Empresa     ] Check TypeIn "~VSDGEMPRESA.."
        Moveto [Nome Fantasia Empresa ] Check
        Moveto [Data de Ocorrencia    ] TypeIn ">=~VSDGDTINI,<=~VSDGDTFIM"
        Moveto [Tipo de Movimento     ] TypeIn "E"
        Moveto [Tipo de Operacao      ] TypeIn "P"
        Moveto [Requisitante          ] Check
        Moveto [Descricao Requisitante] Check
        {Ask} TypeIn "\\Sistemas\\Estoque\\"+Dir_Trab+"RqMatPri" Enter
        Moveto [Numero do Lancamento  ] Example "NUMERO"
        Moveto [Tipo do Item          ] Check TypeIn "~VSDGTPITEM.."
        Moveto [Codigo do Item        ] Check Example "ITEN" TypeIn ", ~VSDGITEM.."
        Moveto [Descricao do Item     ] Check
        Moveto [Unidade Interna       ] Check
        Moveto [Quantidade            ] TypeIn "Calc Sum as Quantidade"
        Moveto [Custo Total           ] TypeIn "Calc Sum as Custo Total"
        Moveto [Aplicacao             ] Check
        Moveto [Descricao Aplicacao   ] Check
        Do_It!
        ClearAll

        Add "Answer" Tabela
        ClearAll
EndIf


EndProc    ;  (Seleciona)

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc Processa()

If VSDGGERAL="N"
   Then Return
EndIf

{Ask} TypeIn Tabela Enter TypeIn "Fast"
Moveto [Codigo da Empresa     ] TypeIn "Changeto Blank"
Moveto [Nome Fantasia Empresa ] TypeIn "Changeto GERAL"
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_TPITEM"         : Campo = "TG_EMPRESA"
            Case Campo = "TG_FAMI"           : Campo = "TG_TPITEM"
            Case Campo = "TG_MARCA"          : Campo = "TG_FAMI"
            Case Campo = "TG_ITEM"           : Campo = "TG_MARCA"
            Case Campo = "TG_REFERENCIA"     : Campo = "TG_ITEM"
            Case Campo = "TG_DTINI"          : Campo = "TG_REFERENCIA"
            Case Campo = "TG_DTFIM"          : Campo = "TG_DTINI"
            Case Campo = "TG_GERAL"          : Campo = "TG_DTFIM"
            Case Campo = "TG_OPRELANALSINT"  : Campo = "TG_GERAL"
            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_TPITEM"
                    Case Campo = "TG_TPITEM"   : Campo = "TG_FAMI"
                    Case Campo = "TG_FAMI"     : Campo = "TG_MARCA"
                    Case Campo = "TG_MARCA"    : Campo = "TG_ITEM"
                    Case Campo = "TG_ITEM"     : Campo = "TG_REFERENCIA"
                    Case Campo = "TG_REFERENCIA" : Campo = "TG_DTINI"
                    Case Campo = "TG_DTINI"    : Campo = "TG_DTFIM"
                    Case Campo = "TG_DTFIM"    : Campo = "TG_GERAL"
                    Case Campo = "TG_GERAL"    :
                         VSDGGERAL = ControlValue("TG_GERAL")
                         If Not IsBlank(VSDGGERAL)
                            Then Switch
                                   Case VSDGGERAL="S":
                                        VSDGDESCGERAL="AGRUPAR"
                                   Case VSDGGERAL="N":
                                        VSDGDESCGERAL="NAO AGRUPAR"
                                   OtherWise:
                                        VSDGGERAL="N"
                                        VSDGDESCGERAL="NAO AGRUPAR"
                                 EndSwitch
                                 Campo = "TG_OPRELANALSINT"
                         EndIf
                    Case Campo = "TG_OPRELANALSINT" : Campo = "YES"
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_GERAL":
       MSG_CAMPO="Informe(S = Agrupar, N = N„o Agrupar)"
EndSwitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

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

VSDGEMPRESA       = ""
VSDGTPITEM  =""
VSDGFAMI          = ""
VSDGITEM          = ""
VSDGDTINI         = BlankDate()
VSDGDTFIM         = BlankDate()
VSDGGERAL         = "N"
VSDGDESCGERAL     = "NAO AGRUPAR"
VSDGDESCGERAL     = ""
VSDGOPRELANALSINT = ""
VSDGMARCA    = ""
VSDGREFERENCIA  = ""

While True

  ShowDialog "Consumo por Familia/Referencia" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 03,05   Height 21 Width 65

    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 "Tipo Item"  For "TG_TPITEM"
    Accept @ 03,04 Width 04 "A1"  Picture "&" Tag "TG_TPITEM" To VSDGTPITEM
    Accept @ 03,09 Width 20 "A12"  Picture "!*!" Tag "TG_NOMTPITEM" To VSDGNOMTPITEM

    Label  @ 04,00 "Familia"  For "TG_FAMI"
    Accept @ 05,02 Width 06 "A3"   Picture "#*#" Tag "TG_FAMI"    To VSDGFAMI
    Accept @ 05,09 Width 28 "A25"  Picture "!*!" Tag "TG_NOMFAMI" To VSDGNOMFAMI

    Label  @ 06,00 "Marca"  For "TG_MARCA"
    Accept @ 07,02 Width 06 "A3"  Picture "#*#" Tag "TG_MARCA"    To VSDGMARCA
    Accept @ 07,09 Width 33 "A30" Picture "!*!" Tag "TG_NOMMARCA" To VSDGNOMMARCA

    Label  @ 08,00 "Item"  For "TG_ITEM"
    Accept @ 09,01 Width 13 "A10" Picture "#*#" Tag "TG_ITEM"     To VSDGITEM
    Accept @ 09,15 Width 33 "A30" Picture "!*!" Tag "TG_NOMITEM"  To VSDGNOMITEM

    Label  @ 08,48 "Referˆncia"  For "TG_REFERENCIA"
    Accept @ 09,49 Width 13 "A10" Picture "#*#" Tag "TG_REFERENCIA"     To VSDGREFERENCIA

    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,00 "Agrupar Empresas" For "TG_GERAL"
    Accept @ 13,01 Width 04 "A1"  Picture "S,N" Tag "TG_GERAL" To VSDGGERAL
    Accept @ 13,06 Width 25 "A25"  Picture "!*!" Tag "TG_DESCGERAL" To VSDGDESCGERAL

    Label  @ 14,00 "Op‡„o de Relat¢rio" For "TG_OPRELANALSINT"
    Accept @ 15,01 Width 04 "A1"   Picture "1,2" Tag "TG_OPRELANALSINT" To VSDGOPRELANALSINT
    Accept @ 15,06 Width 28 "A25"  Picture "!*!" Tag "TG_NOMOPRELANALSINT" To VSDGNOMOPRELANALSINT

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

  EndDialog
  If Not RetVal Then QuitLoop EndIf

  If IsBlank(VSDGGERAL)
     Then Loop
  EndIf

  DISPOSITIVO()
  If OK="" Then Loop EndIf

  AGUARDE("Aguarde, Gerando o Relat¢rio")
  Tabela=Cria_Temp(PrivDir())
  Copy "\\Base\\Sistemas\\Estoque\\Rm_0156" Tabela

  Monta_Dir_Trab(VSDGDTINI,VSDGDTFIM)
  While Novo_Dir_Trab()
    Seleciona()
  EndWhile

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

  If VSDGOPRELANALSINT="1"
     Then IMPRIREL(Tabela,"4")
     Else IMPRIREL(Tabela,"5")
  EndIf

  If IsTable("Answer")
     Then Delete "Answer"
  EndIf
  Delete Tabela

EndWhile
ClearAll
Release Vars All
Release Procs All
